org.el 872 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726
  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 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
  8. ;; Version: 5.02
  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 2, 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://staff.science.uva.nl/~dominik/Tools/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://www.astro.uva.nl/~dominik/Tools/org/Changes
  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. (require 'easymenu)
  77. ;;;; Customization variables
  78. ;;; Version
  79. (defconst org-version "5.02"
  80. "The version number of the file org.el.")
  81. (defun org-version ()
  82. (interactive)
  83. (message "Org-mode version %s" org-version))
  84. ;;; Compatibility constants
  85. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  86. (defconst org-format-transports-properties-p
  87. (let ((x "a"))
  88. (add-text-properties 0 1 '(test t) x)
  89. (get-text-property 0 'test (format "%s" x)))
  90. "Does format transport text properties?")
  91. (defmacro org-unmodified (&rest body)
  92. "Execute body without changing buffer-modified-p."
  93. `(set-buffer-modified-p
  94. (prog1 (buffer-modified-p) ,@body)))
  95. (defmacro org-re (s)
  96. "Replace posix classes in regular expression."
  97. (if (featurep 'xemacs)
  98. (let ((ss s))
  99. (save-match-data
  100. (while (string-match "\\[:alnum:\\]" ss)
  101. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  102. ss))
  103. s))
  104. (defmacro org-preserve-lc (&rest body)
  105. `(let ((_line (org-current-line))
  106. (_col (current-column)))
  107. (unwind-protect
  108. (progn ,@body)
  109. (goto-line _line)
  110. (move-to-column _col))))
  111. ;;; The custom variables
  112. (defgroup org nil
  113. "Outline-based notes management and organizer."
  114. :tag "Org"
  115. :group 'outlines
  116. :group 'hypermedia
  117. :group 'calendar)
  118. (defgroup org-startup nil
  119. "Options concerning startup of Org-mode."
  120. :tag "Org Startup"
  121. :group 'org)
  122. (defcustom org-startup-folded t
  123. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  124. This can also be configured on a per-file basis by adding one of
  125. the following lines anywhere in the buffer:
  126. #+STARTUP: fold
  127. #+STARTUP: nofold
  128. #+STARTUP: content"
  129. :group 'org-startup
  130. :type '(choice
  131. (const :tag "nofold: show all" nil)
  132. (const :tag "fold: overview" t)
  133. (const :tag "content: all headlines" content)))
  134. (defcustom org-startup-truncated t
  135. "Non-nil means, entering Org-mode will set `truncate-lines'.
  136. This is useful since some lines containing links can be very long and
  137. uninteresting. Also tables look terrible when wrapped."
  138. :group 'org-startup
  139. :type 'boolean)
  140. (defcustom org-startup-align-all-tables nil
  141. "Non-nil means, align all tables when visiting a file.
  142. This is useful when the column width in tables is forced with <N> cookies
  143. in table fields. Such tables will look correct only after the first re-align.
  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: align
  147. #+STARTUP: noalign"
  148. :group 'org-startup
  149. :type 'boolean)
  150. (defcustom org-insert-mode-line-in-empty-file nil
  151. "Non-nil means insert the first line setting Org-mode in empty files.
  152. When the function `org-mode' is called interactively in an empty file, this
  153. normally means that the file name does not automatically trigger Org-mode.
  154. To ensure that the file will always be in Org-mode in the future, a
  155. line enforcing Org-mode will be inserted into the buffer, if this option
  156. has been set."
  157. :group 'org-startup
  158. :type 'boolean)
  159. (defcustom org-replace-disputed-keys nil
  160. "Non-nil means use alternative key bindings for some keys.
  161. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  162. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  163. If you want to use Org-mode together with one of these other modes,
  164. or more generally if you would like to move some Org-mode commands to
  165. other keys, set this variable and configure the keys with the variable
  166. `org-disputed-keys'.
  167. This option is only relevant at load-time of Org-mode, and must be set
  168. *before* org.el is loaded. Changing it requires a restart of Emacs to
  169. become effective."
  170. :group 'org-startup
  171. :type 'boolean)
  172. (if (fboundp 'defvaralias)
  173. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  174. (defcustom org-disputed-keys
  175. '(([(shift up)] . [(meta p)])
  176. ([(shift down)] . [(meta n)])
  177. ([(shift left)] . [(meta -)])
  178. ([(shift right)] . [(meta +)])
  179. ([(control shift right)] . [(meta shift +)])
  180. ([(control shift left)] . [(meta shift -)]))
  181. "Keys for which Org-mode and other modes compete.
  182. This is an alist, cars are the default keys, second element specifies
  183. the alternative to use when `org-replace-disputed-keys' is t.
  184. Keys can be specified in any syntax supported by `define-key'.
  185. The value of this option takes effect only at Org-mode's startup,
  186. therefore you'll have to restart Emacs to apply it after changing."
  187. :group 'org-startup
  188. :type 'alist)
  189. (defun org-key (key)
  190. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  191. Or return the original if not disputed."
  192. (if org-replace-disputed-keys
  193. (let* ((nkey (key-description key))
  194. (x (org-find-if (lambda (x)
  195. (equal (key-description (car x)) nkey))
  196. org-disputed-keys)))
  197. (if x (cdr x) key))
  198. key))
  199. (defun org-find-if (predicate seq)
  200. (catch 'exit
  201. (while seq
  202. (if (funcall predicate (car seq))
  203. (throw 'exit (car seq))
  204. (pop seq)))))
  205. (defun org-defkey (keymap key def)
  206. "Define a key, possibly translated, as returned by `org-key'."
  207. (define-key keymap (org-key key) def))
  208. (defcustom org-ellipsis nil
  209. "The ellipsis to use in the Org-mode outline.
  210. When nil, just use the standard three dots. When a string, use that instead,
  211. and just in Org-mode (which will then use its own display table).
  212. Changing this requires executing `M-x org-mode' in a buffer to become
  213. effective."
  214. :group 'org-startup
  215. :type '(choice (const :tag "Default" nil)
  216. (string :tag "String" :value "...#")))
  217. (defvar org-display-table nil
  218. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  219. (defgroup org-keywords nil
  220. "Keywords in Org-mode."
  221. :tag "Org Keywords"
  222. :group 'org)
  223. (defcustom org-deadline-string "DEADLINE:"
  224. "String to mark deadline entries.
  225. A deadline is this string, followed by a time stamp. Should be a word,
  226. terminated by a colon. You can insert a schedule keyword and
  227. a timestamp with \\[org-deadline].
  228. Changes become only effective after restarting Emacs."
  229. :group 'org-keywords
  230. :type 'string)
  231. (defcustom org-scheduled-string "SCHEDULED:"
  232. "String to mark scheduled TODO entries.
  233. A schedule is this string, followed by a time stamp. Should be a word,
  234. terminated by a colon. You can insert a schedule keyword and
  235. a timestamp with \\[org-schedule].
  236. Changes become only effective after restarting Emacs."
  237. :group 'org-keywords
  238. :type 'string)
  239. (defcustom org-closed-string "CLOSED:"
  240. "String used as the prefix for timestamps logging closing a TODO entry."
  241. :group 'org-keywords
  242. :type 'string)
  243. (defcustom org-archived-string "ARCHIVED:"
  244. "String used as the prefix for timestamps logging archiving a TODO entry."
  245. :group 'org-keywords
  246. :type 'string)
  247. (defcustom org-clock-string "CLOCK:"
  248. "String used as prefix for timestamps clocking work hours on an item."
  249. :group 'org-keywords
  250. :type 'string)
  251. (defcustom org-comment-string "COMMENT"
  252. "Entries starting with this keyword will never be exported.
  253. An entry can be toggled between COMMENT and normal with
  254. \\[org-toggle-comment].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-quote-string "QUOTE"
  259. "Entries starting with this keyword will be exported in fixed-width font.
  260. Quoting applies only to the text in the entry following the headline, and does
  261. not extend beyond the next headline, even if that is lower level.
  262. An entry can be toggled between QUOTE and normal with
  263. \\[org-toggle-fixed-width-section]."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defconst org-repeat-re
  267. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  268. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  269. "Regular expression for specifying repeated events.
  270. After a match, group 1 contains the repeat expression.")
  271. (defgroup org-structure nil
  272. "Options concerning the general structure of Org-mode files."
  273. :tag "Org Structure"
  274. :group 'org)
  275. (defgroup org-reveal-location nil
  276. "Options about how to make context of a location visible."
  277. :tag "Org Reveal Location"
  278. :group 'org-structure)
  279. (defcustom org-show-hierarchy-above '((default . t))
  280. "Non-nil means, show full hierarchy when revealing a location.
  281. Org-mode often shows locations in an org-mode file which might have
  282. been invisible before. When this is set, the hierarchy of headings
  283. above the exposed location is shown.
  284. Turning this off for example for sparse trees makes them very compact.
  285. Instead of t, this can also be an alist specifying this option for different
  286. contexts. Valid contexts are
  287. agenda when exposing an entry from the agenda
  288. org-goto when using the command `org-goto' on key C-c C-j
  289. occur-tree when using the command `org-occur' on key C-c /
  290. tags-tree when constructing a sparse tree based on tags matches
  291. link-search when exposing search matches associated with a link
  292. mark-goto when exposing the jump goal of a mark
  293. bookmark-jump when exposing a bookmark location
  294. isearch when exiting from an incremental search
  295. default default for all contexts not set explicitly"
  296. :group 'org-reveal-location
  297. :type '(choice
  298. (const :tag "Always" t)
  299. (const :tag "Never" nil)
  300. (repeat :greedy t :tag "Individual contexts"
  301. (cons
  302. (choice :tag "Context"
  303. (const agenda)
  304. (const org-goto)
  305. (const occur-tree)
  306. (const tags-tree)
  307. (const link-search)
  308. (const mark-goto)
  309. (const bookmark-jump)
  310. (const isearch)
  311. (const default))
  312. (boolean)))))
  313. (defcustom org-show-following-heading '((default . nil))
  314. "Non-nil means, show following heading when revealing a location.
  315. Org-mode often shows locations in an org-mode file which might have
  316. been invisible before. When this is set, the heading following the
  317. match is shown.
  318. Turning this off for example for sparse trees makes them very compact,
  319. but makes it harder to edit the location of the match. In such a case,
  320. use the command \\[org-reveal] to show more context.
  321. Instead of t, this can also be an alist specifying this option for different
  322. contexts. See `org-show-hierarchy-above' for valid contexts."
  323. :group 'org-reveal-location
  324. :type '(choice
  325. (const :tag "Always" t)
  326. (const :tag "Never" nil)
  327. (repeat :greedy t :tag "Individual contexts"
  328. (cons
  329. (choice :tag "Context"
  330. (const agenda)
  331. (const org-goto)
  332. (const occur-tree)
  333. (const tags-tree)
  334. (const link-search)
  335. (const mark-goto)
  336. (const bookmark-jump)
  337. (const isearch)
  338. (const default))
  339. (boolean)))))
  340. (defcustom org-show-siblings '((default . nil) (isearch t))
  341. "Non-nil means, show all sibling heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the sibling of the current entry
  344. heading are all made visible. If `org-show-hierarchy-above' is t,
  345. the same happens on each level of the hierarchy above the current entry.
  346. By default this is on for the isearch context, off for all other contexts.
  347. Turning this off for example for sparse trees makes them very compact,
  348. but makes it harder to edit the location of the match. In such a case,
  349. use the command \\[org-reveal] to show more context.
  350. Instead of t, this can also be an alist specifying this option for different
  351. contexts. See `org-show-hierarchy-above' for valid contexts."
  352. :group 'org-reveal-location
  353. :type '(choice
  354. (const :tag "Always" t)
  355. (const :tag "Never" nil)
  356. (repeat :greedy t :tag "Individual contexts"
  357. (cons
  358. (choice :tag "Context"
  359. (const agenda)
  360. (const org-goto)
  361. (const occur-tree)
  362. (const tags-tree)
  363. (const link-search)
  364. (const mark-goto)
  365. (const bookmark-jump)
  366. (const isearch)
  367. (const default))
  368. (boolean)))))
  369. (defgroup org-cycle nil
  370. "Options concerning visibility cycling in Org-mode."
  371. :tag "Org Cycle"
  372. :group 'org-structure)
  373. (defcustom org-drawers '("PROPERTIES")
  374. "Names of drawers. Drawers are not opened by cycling on the headline above.
  375. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  376. this:
  377. :DRAWERNAME:
  378. .....
  379. :END:
  380. The drawer \"PROPERTIES\" is special for capturing properties through
  381. the property API."
  382. :group 'org-structure
  383. :type '(repeat (string :tag "Drawer Name")))
  384. (defcustom org-cycle-global-at-bob t
  385. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  386. This makes it possible to do global cycling without having to use S-TAB or
  387. C-u TAB. For this special case to work, the first line of the buffer
  388. must not be a headline - it may be empty ot some other text. When used in
  389. this way, `org-cycle-hook' is disables temporarily, to make sure the
  390. cursor stays at the beginning of the buffer.
  391. When this option is nil, don't do anything special at the beginning
  392. of the buffer."
  393. :group 'org-cycle
  394. :type 'boolean)
  395. (defcustom org-cycle-emulate-tab t
  396. "Where should `org-cycle' emulate TAB.
  397. nil Never
  398. white Only in completely white lines
  399. whitestart Only at the beginning of lines, before the first non-white char.
  400. t Everywhere except in headlines
  401. exc-hl-bol Everywhere except at the start of a headline
  402. If TAB is used in a place where it does not emulate TAB, the current subtree
  403. visibility is cycled."
  404. :group 'org-cycle
  405. :type '(choice (const :tag "Never" nil)
  406. (const :tag "Only in completely white lines" white)
  407. (const :tag "Before first char in a line" whitestart)
  408. (const :tag "Everywhere except in headlines" t)
  409. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  410. ))
  411. (defcustom org-cycle-separator-lines 2
  412. "Number of empty lines needed to keep an empty line between collapsed trees.
  413. If you leave an empty line between the end of a subtree and the following
  414. headline, this empty line is hidden when the subtree is folded.
  415. Org-mode will leave (exactly) one empty line visible if the number of
  416. empty lines is equal or larger to the number given in this variable.
  417. So the default 2 means, at least 2 empty lines after the end of a subtree
  418. are needed to produce free space between a collapsed subtree and the
  419. following headline.
  420. Special case: when 0, never leave empty lines in collapsed view."
  421. :group 'org-cycle
  422. :type 'integer)
  423. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  424. org-cycle-hide-drawers
  425. org-cycle-show-empty-lines
  426. org-optimize-window-after-visibility-change)
  427. "Hook that is run after `org-cycle' has changed the buffer visibility.
  428. The function(s) in this hook must accept a single argument which indicates
  429. the new state that was set by the most recent `org-cycle' command. The
  430. argument is a symbol. After a global state change, it can have the values
  431. `overview', `content', or `all'. After a local state change, it can have
  432. the values `folded', `children', or `subtree'."
  433. :group 'org-cycle
  434. :type 'hook)
  435. (defgroup org-edit-structure nil
  436. "Options concerning structure editing in Org-mode."
  437. :tag "Org Edit Structure"
  438. :group 'org-structure)
  439. (defcustom org-special-ctrl-a nil
  440. "Non-nil means `C-a' behaves specially in headlines.
  441. When set, `C-a' will bring back the cursor to the beginning of the
  442. headline text, i.e. after the stars and after a possible TODO keyword.
  443. When the cursor is already at that position, another `C-a' will bring
  444. it to the beginning of the line."
  445. :group 'org-edit-structure
  446. :type 'boolean)
  447. (defcustom org-odd-levels-only nil
  448. "Non-nil means, skip even levels and only use odd levels for the outline.
  449. This has the effect that two stars are being added/taken away in
  450. promotion/demotion commands. It also influences how levels are
  451. handled by the exporters.
  452. Changing it requires restart of `font-lock-mode' to become effective
  453. for fontification also in regions already fontified.
  454. You may also set this on a per-file basis by adding one of the following
  455. lines to the buffer:
  456. #+STARTUP: odd
  457. #+STARTUP: oddeven"
  458. :group 'org-edit-structure
  459. :group 'org-font-lock
  460. :type 'boolean)
  461. (defcustom org-adapt-indentation t
  462. "Non-nil means, adapt indentation when promoting and demoting.
  463. When this is set and the *entire* text in an entry is indented, the
  464. indentation is increased by one space in a demotion command, and
  465. decreased by one in a promotion command. If any line in the entry
  466. body starts at column 0, indentation is not changed at all."
  467. :group 'org-edit-structure
  468. :type 'boolean)
  469. (defcustom org-blank-before-new-entry '((heading . nil)
  470. (plain-list-item . nil))
  471. "Should `org-insert-heading' leave a blank line before new heading/item?
  472. The value is an alist, with `heading' and `plain-list-item' as car,
  473. and a boolean flag as cdr."
  474. :group 'org-edit-structure
  475. :type '(list
  476. (cons (const heading) (boolean))
  477. (cons (const plain-list-item) (boolean))))
  478. (defcustom org-insert-heading-hook nil
  479. "Hook being run after inserting a new heading."
  480. :group 'org-edit-structure
  481. :type 'boolean)
  482. (defcustom org-enable-fixed-width-editor t
  483. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  484. This currently only means, they are never auto-wrapped.
  485. When nil, such lines will be treated like ordinary lines.
  486. See also the QUOTE keyword."
  487. :group 'org-edit-structure
  488. :type 'boolean)
  489. (defgroup org-sparse-trees nil
  490. "Options concerning sparse trees in Org-mode."
  491. :tag "Org Sparse Trees"
  492. :group 'org-structure)
  493. (defcustom org-highlight-sparse-tree-matches t
  494. "Non-nil means, highlight all matches that define a sparse tree.
  495. The highlights will automatically disappear the next time the buffer is
  496. changed by an edit command."
  497. :group 'org-sparse-trees
  498. :type 'boolean)
  499. (defcustom org-remove-highlights-with-change t
  500. "Non-nil means, any change to the buffer will remove temporary highlights.
  501. Such highlights are created by `org-occur' and `org-clock-display'.
  502. When nil, `C-c C-c needs to be used to get rid of the highlights.
  503. The highlights created by `org-preview-latex-fragment' always need
  504. `C-c C-c' to be removed."
  505. :group 'org-sparse-trees
  506. :group 'org-time
  507. :type 'boolean)
  508. (defcustom org-occur-hook '(org-first-headline-recenter)
  509. "Hook that is run after `org-occur' has constructed a sparse tree.
  510. This can be used to recenter the window to show as much of the structure
  511. as possible."
  512. :group 'org-sparse-trees
  513. :type 'hook)
  514. (defgroup org-plain-lists nil
  515. "Options concerning plain lists in Org-mode."
  516. :tag "Org Plain lists"
  517. :group 'org-structure)
  518. (defcustom org-cycle-include-plain-lists nil
  519. "Non-nil means, include plain lists into visibility cycling.
  520. This means that during cycling, plain list items will *temporarily* be
  521. interpreted as outline headlines with a level given by 1000+i where i is the
  522. indentation of the bullet. In all other operations, plain list items are
  523. not seen as headlines. For example, you cannot assign a TODO keyword to
  524. such an item."
  525. :group 'org-plain-lists
  526. :type 'boolean)
  527. (defcustom org-plain-list-ordered-item-terminator t
  528. "The character that makes a line with leading number an ordered list item.
  529. Valid values are ?. and ?\). To get both terminators, use t. While
  530. ?. may look nicer, it creates the danger that a line with leading
  531. number may be incorrectly interpreted as an item. ?\) therefore is
  532. the safe choice."
  533. :group 'org-plain-lists
  534. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  535. (const :tag "paren like in \"2)\"" ?\))
  536. (const :tab "both" t)))
  537. (defcustom org-auto-renumber-ordered-lists t
  538. "Non-nil means, automatically renumber ordered plain lists.
  539. Renumbering happens when the sequence have been changed with
  540. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  541. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  542. :group 'org-plain-lists
  543. :type 'boolean)
  544. (defcustom org-provide-checkbox-statistics t
  545. "Non-nil means, update checkbox statistics after insert and toggle.
  546. When this is set, checkbox statistics is updated each time you either insert
  547. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  548. with \\[org-ctrl-c-ctrl-c\\]."
  549. :group 'org-plain-lists
  550. :type 'boolean)
  551. (defgroup org-archive nil
  552. "Options concerning archiving in Org-mode."
  553. :tag "Org Archive"
  554. :group 'org-structure)
  555. (defcustom org-archive-tag "ARCHIVE"
  556. "The tag that marks a subtree as archived.
  557. An archived subtree does not open during visibility cycling, and does
  558. not contribute to the agenda listings."
  559. :group 'org-archive
  560. :group 'org-keywords
  561. :type 'string)
  562. (defcustom org-agenda-skip-archived-trees t
  563. "Non-nil means, the agenda will skip any items located in archived trees.
  564. An archived tree is a tree marked with the tag ARCHIVE."
  565. :group 'org-archive
  566. :group 'org-agenda-skip
  567. :type 'boolean)
  568. (defcustom org-cycle-open-archived-trees nil
  569. "Non-nil means, `org-cycle' will open archived trees.
  570. An archived tree is a tree marked with the tag ARCHIVE.
  571. When nil, archived trees will stay folded. You can still open them with
  572. normal outline commands like `show-all', but not with the cycling commands."
  573. :group 'org-archive
  574. :group 'org-cycle
  575. :type 'boolean)
  576. (defcustom org-sparse-tree-open-archived-trees nil
  577. "Non-nil means sparse tree construction shows matches in archived trees.
  578. When nil, matches in these trees are highlighted, but the trees are kept in
  579. collapsed state."
  580. :group 'org-archive
  581. :group 'org-sparse-trees
  582. :type 'boolean)
  583. (defcustom org-archive-location "%s_archive::"
  584. "The location where subtrees should be archived.
  585. This string consists of two parts, separated by a double-colon.
  586. The first part is a file name - when omitted, archiving happens in the same
  587. file. %s will be replaced by the current file name (without directory part).
  588. Archiving to a different file is useful to keep archived entries from
  589. contributing to the Org-mode Agenda.
  590. The part after the double colon is a headline. The archived entries will be
  591. filed under that headline. When omitted, the subtrees are simply filed away
  592. at the end of the file, as top-level entries.
  593. Here are a few examples:
  594. \"%s_archive::\"
  595. If the current file is Projects.org, archive in file
  596. Projects.org_archive, as top-level trees. This is the default.
  597. \"::* Archived Tasks\"
  598. Archive in the current file, under the top-level headline
  599. \"* Archived Tasks\".
  600. \"~/org/archive.org::\"
  601. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  602. \"basement::** Finished Tasks\"
  603. Archive in file ./basement (relative path), as level 3 trees
  604. below the level 2 heading \"** Finished Tasks\".
  605. You may set this option on a per-file basis by adding to the buffer a
  606. line like
  607. #+ARCHIVE: basement::** Finished Tasks"
  608. :group 'org-archive
  609. :type 'string)
  610. (defcustom org-archive-mark-done t
  611. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  612. :group 'org-archive
  613. :type 'boolean)
  614. (defcustom org-archive-stamp-time t
  615. "Non-nil means, add a time stamp to entries moved to an archive file."
  616. :group 'org-archive
  617. :type 'boolean)
  618. (defgroup org-table nil
  619. "Options concerning tables in Org-mode."
  620. :tag "Org Table"
  621. :group 'org)
  622. (defcustom org-enable-table-editor 'optimized
  623. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  624. When nil, such lines will be treated like ordinary lines.
  625. When equal to the symbol `optimized', the table editor will be optimized to
  626. do the following:
  627. - Automatic overwrite mode in front of whitespace in table fields.
  628. This makes the structure of the table stay in tact as long as the edited
  629. field does not exceed the column width.
  630. - Minimize the number of realigns. Normally, the table is aligned each time
  631. TAB or RET are pressed to move to another field. With optimization this
  632. happens only if changes to a field might have changed the column width.
  633. Optimization requires replacing the functions `self-insert-command',
  634. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  635. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  636. very good at guessing when a re-align will be necessary, but you can always
  637. force one with \\[org-ctrl-c-ctrl-c].
  638. If you would like to use the optimized version in Org-mode, but the
  639. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  640. This variable can be used to turn on and off the table editor during a session,
  641. but in order to toggle optimization, a restart is required.
  642. See also the variable `org-table-auto-blank-field'."
  643. :group 'org-table
  644. :type '(choice
  645. (const :tag "off" nil)
  646. (const :tag "on" t)
  647. (const :tag "on, optimized" optimized)))
  648. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  649. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  650. In the optimized version, the table editor takes over all simple keys that
  651. normally just insert a character. In tables, the characters are inserted
  652. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  653. for empty fields). Outside tables, the correct binding of the keys is
  654. restored.
  655. The default for this option is t if the optimized version is also used in
  656. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  657. this variable requires a restart of Emacs to become effective."
  658. :group 'org-table
  659. :type 'boolean)
  660. (defcustom orgtbl-radio-table-templates
  661. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  662. % END RECEIVE ORGTBL %n
  663. \\begin{comment}
  664. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  665. | | |
  666. \\end{comment}\n")
  667. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  668. @c END RECEIVE ORGTBL %n
  669. @ignore
  670. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  671. | | |
  672. @end ignore\n")
  673. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  674. <!-- END RECEIVE ORGTBL %n -->
  675. <!--
  676. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  677. | | |
  678. -->\n"))
  679. "Templates for radio tables in different major modes.
  680. All occurrences of %n in a template will be replaced with the name of the
  681. table, obtained by prompting the user."
  682. :group 'org-table
  683. :type '(repeat
  684. (list (symbol :tag "Major mode")
  685. (string :tag "Format"))))
  686. (defgroup org-table-settings nil
  687. "Settings for tables in Org-mode."
  688. :tag "Org Table Settings"
  689. :group 'org-table)
  690. (defcustom org-table-default-size "5x2"
  691. "The default size for newly created tables, Columns x Rows."
  692. :group 'org-table-settings
  693. :type 'string)
  694. (defcustom org-table-number-regexp
  695. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\)$"
  696. "Regular expression for recognizing numbers in table columns.
  697. If a table column contains mostly numbers, it will be aligned to the
  698. right. If not, it will be aligned to the left.
  699. The default value of this option is a regular expression which allows
  700. anything which looks remotely like a number as used in scientific
  701. context. For example, all of the following will be considered a
  702. number:
  703. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  704. Other options offered by the customize interface are more restrictive."
  705. :group 'org-table-settings
  706. :type '(choice
  707. (const :tag "Positive Integers"
  708. "^[0-9]+$")
  709. (const :tag "Integers"
  710. "^[-+]?[0-9]+$")
  711. (const :tag "Floating Point Numbers"
  712. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  713. (const :tag "Floating Point Number or Integer"
  714. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  715. (const :tag "Exponential, Floating point, Integer"
  716. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  717. (const :tag "Very General Number-Like, including hex"
  718. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\)$")
  719. (string :tag "Regexp:")))
  720. (defcustom org-table-number-fraction 0.5
  721. "Fraction of numbers in a column required to make the column align right.
  722. In a column all non-white fields are considered. If at least this
  723. fraction of fields is matched by `org-table-number-fraction',
  724. alignment to the right border applies."
  725. :group 'org-table-settings
  726. :type 'number)
  727. (defgroup org-table-editing nil
  728. "Bahavior of tables during editing in Org-mode."
  729. :tag "Org Table Editing"
  730. :group 'org-table)
  731. (defcustom org-table-automatic-realign t
  732. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  733. When nil, aligning is only done with \\[org-table-align], or after column
  734. removal/insertion."
  735. :group 'org-table-editing
  736. :type 'boolean)
  737. (defcustom org-table-auto-blank-field t
  738. "Non-nil means, automatically blank table field when starting to type into it.
  739. This only happens when typing immediately after a field motion
  740. command (TAB, S-TAB or RET).
  741. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  742. :group 'org-table-editing
  743. :type 'boolean)
  744. (defcustom org-table-tab-jumps-over-hlines t
  745. "Non-nil means, tab in the last column of a table with jump over a hline.
  746. If a horizontal separator line is following the current line,
  747. `org-table-next-field' can either create a new row before that line, or jump
  748. over the line. When this option is nil, a new line will be created before
  749. this line."
  750. :group 'org-table-editing
  751. :type 'boolean)
  752. (defcustom org-table-tab-recognizes-table.el t
  753. "Non-nil means, TAB will automatically notice a table.el table.
  754. When it sees such a table, it moves point into it and - if necessary -
  755. calls `table-recognize-table'."
  756. :group 'org-table-editing
  757. :type 'boolean)
  758. (defgroup org-table-calculation nil
  759. "Options concerning tables in Org-mode."
  760. :tag "Org Table Calculation"
  761. :group 'org-table)
  762. (defcustom org-table-use-standard-references t
  763. "Should org-mode work with table refrences like B3 instead of @3$2?
  764. Possible values are:
  765. nil never use them
  766. from accept as input, do not present for editing
  767. t: accept as input and present for editing"
  768. :group 'org-table-calculation
  769. :type '(choice
  770. (const :tag "Never, don't even check unser input for them" nil)
  771. (const :tag "Always, both as user input, and when editing" t)
  772. (const :tag "Convert user input, don't offer during editing" 'from)))
  773. (defcustom org-table-copy-increment t
  774. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  775. :group 'org-table-calculation
  776. :type 'boolean)
  777. (defcustom org-calc-default-modes
  778. '(calc-internal-prec 12
  779. calc-float-format (float 5)
  780. calc-angle-mode deg
  781. calc-prefer-frac nil
  782. calc-symbolic-mode nil
  783. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  784. calc-display-working-message t
  785. )
  786. "List with Calc mode settings for use in calc-eval for table formulas.
  787. The list must contain alternating symbols (Calc modes variables and values).
  788. Don't remove any of the default settings, just change the values. Org-mode
  789. relies on the variables to be present in the list."
  790. :group 'org-table-calculation
  791. :type 'plist)
  792. (defcustom org-table-formula-evaluate-inline t
  793. "Non-nil means, TAB and RET evaluate a formula in current table field.
  794. If the current field starts with an equal sign, it is assumed to be a formula
  795. which should be evaluated as described in the manual and in the documentation
  796. string of the command `org-table-eval-formula'. This feature requires the
  797. Emacs calc package.
  798. When this variable is nil, formula calculation is only available through
  799. the command \\[org-table-eval-formula]."
  800. :group 'org-table-calculation
  801. :type 'boolean)
  802. (defcustom org-table-formula-use-constants t
  803. "Non-nil means, interpret constants in formulas in tables.
  804. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  805. by the value given in `org-table-formula-constants', or by a value obtained
  806. from the `constants.el' package."
  807. :group 'org-table-calculation
  808. :type 'boolean)
  809. (defcustom org-table-formula-constants nil
  810. "Alist with constant names and values, for use in table formulas.
  811. The car of each element is a name of a constant, without the `$' before it.
  812. The cdr is the value as a string. For example, if you'd like to use the
  813. speed of light in a formula, you would configure
  814. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  815. and then use it in an equation like `$1*$c'.
  816. Constants can also be defined on a per-file basis using a line like
  817. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  818. :group 'org-table-calculation
  819. :type '(repeat
  820. (cons (string :tag "name")
  821. (string :tag "value"))))
  822. (defvar org-table-formula-constants-local nil
  823. "Local version of `org-table-formula-constants'.")
  824. (make-variable-buffer-local 'org-table-formula-constants-local)
  825. (defcustom org-table-allow-automatic-line-recalculation t
  826. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  827. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  828. :group 'org-table-calculation
  829. :type 'boolean)
  830. (defgroup org-link nil
  831. "Options concerning links in Org-mode."
  832. :tag "Org Link"
  833. :group 'org)
  834. (defvar org-link-abbrev-alist-local nil
  835. "Buffer-local version of `org-link-abbrev-alist', which see.
  836. The value of this is taken from the #+LINK lines.")
  837. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  838. (defcustom org-link-abbrev-alist nil
  839. "Alist of link abbreviations.
  840. The car of each element is a string, to be replaced at the start of a link.
  841. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  842. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  843. [[linkkey:tag][description]]
  844. If REPLACE is a string, the tag will simply be appended to create the link.
  845. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  846. also be a function that will be called with the tag as the only argument to
  847. create the link. See the manual for examples."
  848. :group 'org-link
  849. :type 'alist)
  850. (defcustom org-descriptive-links t
  851. "Non-nil means, hide link part and only show description of bracket links.
  852. Bracket links are like [[link][descritpion]]. This variable sets the initial
  853. state in new org-mode buffers. The setting can then be toggled on a
  854. per-buffer basis from the Org->Hyperlinks menu."
  855. :group 'org-link
  856. :type 'boolean)
  857. (defcustom org-link-file-path-type 'adaptive
  858. "How the path name in file links should be stored.
  859. Valid values are:
  860. relative relative to the current directory, i.e. the directory of the file
  861. into which the link is being inserted.
  862. absolute absolute path, if possible with ~ for home directory.
  863. noabbrev absolute path, no abbreviation of home directory.
  864. adaptive Use relative path for files in the current directory and sub-
  865. directories of it. For other files, use an absolute path."
  866. :group 'org-link
  867. :type '(choice
  868. (const relative)
  869. (const absolute)
  870. (const noabbrev)
  871. (const adaptive)))
  872. (defcustom org-activate-links '(bracket angle plain radio tag date)
  873. "Types of links that should be activated in Org-mode files.
  874. This is a list of symbols, each leading to the activation of a certain link
  875. type. In principle, it does not hurt to turn on most link types - there may
  876. be a small gain when turning off unused link types. The types are:
  877. bracket The recommended [[link][description]] or [[link]] links with hiding.
  878. angular Links in angular brackes that may contain whitespace like
  879. <bbdb:Carsten Dominik>.
  880. plain Plain links in normal text, no whitespace, like http://google.com.
  881. radio Text that is matched by a radio target, see manual for details.
  882. tag Tag settings in a headline (link to tag search).
  883. date Time stamps (link to calendar).
  884. Changing this variable requires a restart of Emacs to become effective."
  885. :group 'org-link
  886. :type '(set (const :tag "Double bracket links (new style)" bracket)
  887. (const :tag "Angular bracket links (old style)" angular)
  888. (const :tag "plain text links" plain)
  889. (const :tag "Radio target matches" radio)
  890. (const :tag "Tags" tag)
  891. (const :tag "Tags" target)
  892. (const :tag "Timestamps" date)))
  893. (defgroup org-link-store nil
  894. "Options concerning storing links in Org-mode"
  895. :tag "Org Store Link"
  896. :group 'org-link)
  897. (defcustom org-email-link-description-format "Email %c: %.30s"
  898. "Format of the description part of a link to an email or usenet message.
  899. The following %-excapes will be replaced by corresponding information:
  900. %F full \"From\" field
  901. %f name, taken from \"From\" field, address if no name
  902. %T full \"To\" field
  903. %t first name in \"To\" field, address if no name
  904. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  905. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  906. %s subject
  907. %m message-id.
  908. You may use normal field width specification between the % and the letter.
  909. This is for example useful to limit the length of the subject.
  910. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  911. :group 'org-link-store
  912. :type 'string)
  913. (defcustom org-from-is-user-regexp
  914. (let (r1 r2)
  915. (when (and user-mail-address (not (string= user-mail-address "")))
  916. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  917. (when (and user-full-name (not (string= user-full-name "")))
  918. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  919. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  920. "Regexp mached against the \"From:\" header of an email or usenet message.
  921. It should match if the message is from the user him/herself."
  922. :group 'org-link-store
  923. :type 'regexp)
  924. (defcustom org-context-in-file-links t
  925. "Non-nil means, file links from `org-store-link' contain context.
  926. A search string will be added to the file name with :: as separator and
  927. used to find the context when the link is activated by the command
  928. `org-open-at-point'.
  929. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  930. negates this setting for the duration of the command."
  931. :group 'org-link-store
  932. :type 'boolean)
  933. (defcustom org-keep-stored-link-after-insertion nil
  934. "Non-nil means, keep link in list for entire session.
  935. The command `org-store-link' adds a link pointing to the current
  936. location to an internal list. These links accumulate during a session.
  937. The command `org-insert-link' can be used to insert links into any
  938. Org-mode file (offering completion for all stored links). When this
  939. option is nil, every link which has been inserted once using \\[org-insert-link]
  940. will be removed from the list, to make completing the unused links
  941. more efficient."
  942. :group 'org-link-store
  943. :type 'boolean)
  944. (defcustom org-usenet-links-prefer-google nil
  945. "Non-nil means, `org-store-link' will create web links to Google groups.
  946. When nil, Gnus will be used for such links.
  947. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  948. negates this setting for the duration of the command."
  949. :group 'org-link-store
  950. :type 'boolean)
  951. (defgroup org-link-follow nil
  952. "Options concerning following links in Org-mode"
  953. :tag "Org Follow Link"
  954. :group 'org-link)
  955. (defcustom org-tab-follows-link nil
  956. "Non-nil means, on links TAB will follow the link.
  957. Needs to be set before org.el is loaded."
  958. :group 'org-link-follow
  959. :type 'boolean)
  960. (defcustom org-return-follows-link nil
  961. "Non-nil means, on links RET will follow the link.
  962. Needs to be set before org.el is loaded."
  963. :group 'org-link-follow
  964. :type 'boolean)
  965. (defcustom org-mouse-1-follows-link t
  966. "Non-nil means, mouse-1 on a link will follow the link.
  967. A longer mouse click will still set point. Does not wortk on XEmacs.
  968. Needs to be set before org.el is loaded."
  969. :group 'org-link-follow
  970. :type 'boolean)
  971. (defcustom org-mark-ring-length 4
  972. "Number of different positions to be recorded in the ring
  973. Changing this requires a restart of Emacs to work correctly."
  974. :group 'org-link-follow
  975. :type 'interger)
  976. (defcustom org-link-frame-setup
  977. '((vm . vm-visit-folder-other-frame)
  978. (gnus . gnus-other-frame)
  979. (file . find-file-other-window))
  980. "Setup the frame configuration for following links.
  981. When following a link with Emacs, it may often be useful to display
  982. this link in another window or frame. This variable can be used to
  983. set this up for the different types of links.
  984. For VM, use any of
  985. `vm-visit-folder'
  986. `vm-visit-folder-other-frame'
  987. For Gnus, use any of
  988. `gnus'
  989. `gnus-other-frame'
  990. For FILE, use any of
  991. `find-file'
  992. `find-file-other-window'
  993. `find-file-other-frame'
  994. For the calendar, use the variable `calendar-setup'.
  995. For BBDB, it is currently only possible to display the matches in
  996. another window."
  997. :group 'org-link-follow
  998. :type '(list
  999. (cons (const vm)
  1000. (choice
  1001. (const vm-visit-folder)
  1002. (const vm-visit-folder-other-window)
  1003. (const vm-visit-folder-other-frame)))
  1004. (cons (const gnus)
  1005. (choice
  1006. (const gnus)
  1007. (const gnus-other-frame)))
  1008. (cons (const file)
  1009. (choice
  1010. (const find-file)
  1011. (const find-file-other-window)
  1012. (const find-file-other-frame)))))
  1013. (defcustom org-display-internal-link-with-indirect-buffer nil
  1014. "Non-nil means, use indirect buffer to display infile links.
  1015. Activating internal links (from one location in a file to another location
  1016. in the same file) normally just jumps to the location. When the link is
  1017. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1018. another window. When this option is set, the other window actually displays
  1019. an indirect buffer clone of the current buffer, to avoid any visibility
  1020. changes to the current buffer."
  1021. :group 'org-link-follow
  1022. :type 'boolean)
  1023. (defcustom org-open-non-existing-files nil
  1024. "Non-nil means, `org-open-file' will open non-existing files.
  1025. When nil, an error will be generated."
  1026. :group 'org-link-follow
  1027. :type 'boolean)
  1028. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1029. "Function and arguments to call for following mailto links.
  1030. This is a list with the first element being a lisp function, and the
  1031. remaining elements being arguments to the function. In string arguments,
  1032. %a will be replaced by the address, and %s will be replaced by the subject
  1033. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1034. :group 'org-link-follow
  1035. :type '(choice
  1036. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1037. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1038. (const :tag "message-mail" (message-mail "%a" "%s"))
  1039. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1040. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1041. "Non-nil means, ask for confirmation before executing shell links.
  1042. Shell links can be dangerous, just thing about a link
  1043. [[shell:rm -rf ~/*][Google Search]]
  1044. This link would show up in your Org-mode document as \"Google Search\"
  1045. but really it would remove your entire home directory.
  1046. Therefore I *definitely* advise against setting this variable to nil.
  1047. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1048. rather than having to type \"yes\"."
  1049. :group 'org-link-follow
  1050. :type '(choice
  1051. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1052. (const :tag "with y-or-n (faster)" y-or-n-p)
  1053. (const :tag "no confirmation (dangerous)" nil)))
  1054. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1055. "Non-nil means, ask for confirmation before executing elisp links.
  1056. Elisp links can be dangerous, just think about a link
  1057. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1058. This link would show up in your Org-mode document as \"Google Search\"
  1059. but really it would remove your entire home directory.
  1060. Therefore I *definitely* advise against setting this variable to nil.
  1061. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1062. rather than having to type \"yes\"."
  1063. :group 'org-link-follow
  1064. :type '(choice
  1065. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1066. (const :tag "with y-or-n (faster)" y-or-n-p)
  1067. (const :tag "no confirmation (dangerous)" nil)))
  1068. (defconst org-file-apps-defaults-gnu
  1069. '((remote . emacs)
  1070. (t . mailcap))
  1071. "Default file applications on a UNIX or GNU/Linux system.
  1072. See `org-file-apps'.")
  1073. (defconst org-file-apps-defaults-macosx
  1074. '((remote . emacs)
  1075. (t . "open %s")
  1076. ("ps" . "gv %s")
  1077. ("ps.gz" . "gv %s")
  1078. ("eps" . "gv %s")
  1079. ("eps.gz" . "gv %s")
  1080. ("dvi" . "xdvi %s")
  1081. ("fig" . "xfig %s"))
  1082. "Default file applications on a MacOS X system.
  1083. The system \"open\" is known as a default, but we use X11 applications
  1084. for some files for which the OS does not have a good default.
  1085. See `org-file-apps'.")
  1086. (defconst org-file-apps-defaults-windowsnt
  1087. (list
  1088. '(remote . emacs)
  1089. (cons t
  1090. (list (if (featurep 'xemacs)
  1091. 'mswindows-shell-execute
  1092. 'w32-shell-execute)
  1093. "open" 'file)))
  1094. "Default file applications on a Windows NT system.
  1095. The system \"open\" is used for most files.
  1096. See `org-file-apps'.")
  1097. (defcustom org-file-apps
  1098. '(
  1099. ("txt" . emacs)
  1100. ("tex" . emacs)
  1101. ("ltx" . emacs)
  1102. ("org" . emacs)
  1103. ("el" . emacs)
  1104. ("bib" . emacs)
  1105. )
  1106. "External applications for opening `file:path' items in a document.
  1107. Org-mode uses system defaults for different file types, but
  1108. you can use this variable to set the application for a given file
  1109. extension. The entries in this list are cons cells where the car identifies
  1110. files and the cdr the corresponding command. Possible values for the
  1111. file identifier are
  1112. \"ext\" A string identifying an extension
  1113. `directory' Matches a directory
  1114. `remote' Matches a remote file, accessible through tramp or efs.
  1115. Remote files most likely should be visited through Emacs
  1116. because external applications cannot handle such paths.
  1117. t Default for all remaining files
  1118. Possible values for the command are:
  1119. `emacs' The file will be visited by the current Emacs process.
  1120. `default' Use the default application for this file type.
  1121. string A command to be executed by a shell; %s will be replaced
  1122. by the path to the file.
  1123. sexp A Lisp form which will be evaluated. The file path will
  1124. be available in the Lisp variable `file'.
  1125. For more examples, see the system specific constants
  1126. `org-file-apps-defaults-macosx'
  1127. `org-file-apps-defaults-windowsnt'
  1128. `org-file-apps-defaults-gnu'."
  1129. :group 'org-link-follow
  1130. :type '(repeat
  1131. (cons (choice :value ""
  1132. (string :tag "Extension")
  1133. (const :tag "Default for unrecognized files" t)
  1134. (const :tag "Remote file" remote)
  1135. (const :tag "Links to a directory" directory))
  1136. (choice :value ""
  1137. (const :tag "Visit with Emacs" emacs)
  1138. (const :tag "Use system default" default)
  1139. (string :tag "Command")
  1140. (sexp :tag "Lisp form")))))
  1141. (defcustom org-mhe-search-all-folders nil
  1142. "Non-nil means, that the search for the mh-message will be extended to
  1143. all folders if the message cannot be found in the folder given in the link.
  1144. Searching all folders is very efficient with one of the search engines
  1145. supported by MH-E, but will be slow with pick."
  1146. :group 'org-link-follow
  1147. :type 'boolean)
  1148. (defgroup org-remember nil
  1149. "Options concerning interaction with remember.el."
  1150. :tag "Org Remember"
  1151. :group 'org)
  1152. (defcustom org-directory "~/org"
  1153. "Directory with org files.
  1154. This directory will be used as default to prompt for org files.
  1155. Used by the hooks for remember.el."
  1156. :group 'org-remember
  1157. :type 'directory)
  1158. (defcustom org-default-notes-file "~/.notes"
  1159. "Default target for storing notes.
  1160. Used by the hooks for remember.el. This can be a string, or nil to mean
  1161. the value of `remember-data-file'.
  1162. You can set this on a per-template basis with the variable
  1163. `org-remember-templates'."
  1164. :group 'org-remember
  1165. :type '(choice
  1166. (const :tag "Default from remember-data-file" nil)
  1167. file))
  1168. (defcustom org-remember-default-headline ""
  1169. "The headline that should be the default location in the notes file.
  1170. When filing remember notes, the cursor will start at that position.
  1171. You can set this on a per-template basis with the variable
  1172. `org-remember-templates'."
  1173. :group 'org-remember
  1174. :type 'string)
  1175. (defcustom org-remember-templates nil
  1176. "Templates for the creation of remember buffers.
  1177. When nil, just let remember make the buffer.
  1178. When not nil, this is a list of 4-element lists. In each entry, the first
  1179. element is a character, a unique key to select this template.
  1180. The second element is the template. The third element is optional and can
  1181. specify a destination file for remember items created with this template.
  1182. The default file is given by `org-default-notes-file'. An optional forth
  1183. element can specify the headline in that file that should be offered
  1184. first when the user is asked to file the entry. The default headline is
  1185. given in the variable `org-remember-default-headline'.
  1186. The template specifies the structure of the remember buffer. It should have
  1187. a first line starting with a star, to act as the org-mode headline.
  1188. Furthermore, the following %-escapes will be replaced with content:
  1189. %^{prompt} prompt the user for a string and replace this sequence with it.
  1190. %t time stamp, date only
  1191. %T time stamp with date and time
  1192. %u, %U like the above, but inactive time stamps
  1193. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1194. You may define a prompt like %^{Please specify birthday}t
  1195. %n user name (taken from `user-full-name')
  1196. %a annotation, normally the link created with org-store-link
  1197. %i initial content, the region when remember is called with C-u.
  1198. If %i is indented, the entire inserted text will be indented
  1199. as well.
  1200. %? After completing the template, position cursor here.
  1201. Apart from these general escapes, you can access information specific to the
  1202. link type that is created. For example, calling `remember' in emails or gnus
  1203. will record the author and the subject of the message, which you can access
  1204. with %:author and %:subject, respectively. Here is a complete list of what
  1205. is recorded for each link type.
  1206. Link type | Available information
  1207. -------------------+------------------------------------------------------
  1208. bbdb | %:type %:name %:company
  1209. vm, wl, mh, rmail | %:type %:subject %:message-id
  1210. | %:from %:fromname %:fromaddress
  1211. | %:to %:toname %:toaddress
  1212. | %:fromto (either \"to NAME\" or \"from NAME\")
  1213. gnus | %:group, for messages also all email fields
  1214. w3, w3m | %:type %:url
  1215. info | %:type %:file %:node
  1216. calendar | %:type %:date"
  1217. :group 'org-remember
  1218. :get (lambda (var) ; Make sure all entries have 4 elements
  1219. (mapcar (lambda (x)
  1220. (cond ((= (length x) 3) (append x '("")))
  1221. ((= (length x) 2) (append x '("" "")))
  1222. (t x)))
  1223. (default-value var)))
  1224. :type '(repeat
  1225. :tag "enabled"
  1226. (list :value (?a "\n" nil nil)
  1227. (character :tag "Selection Key")
  1228. (string :tag "Template")
  1229. (file :tag "Destination file (optional)")
  1230. (string :tag "Destination headline (optional)"))))
  1231. (defcustom org-reverse-note-order nil
  1232. "Non-nil means, store new notes at the beginning of a file or entry.
  1233. When nil, new notes will be filed to the end of a file or entry."
  1234. :group 'org-remember
  1235. :type '(choice
  1236. (const :tag "Reverse always" t)
  1237. (const :tag "Reverse never" nil)
  1238. (repeat :tag "By file name regexp"
  1239. (cons regexp boolean))))
  1240. (defgroup org-todo nil
  1241. "Options concerning TODO items in Org-mode."
  1242. :tag "Org TODO"
  1243. :group 'org)
  1244. (defgroup org-progress nil
  1245. "Options concerning Progress logging in Org-mode."
  1246. :tag "Org Progress"
  1247. :group 'org-time)
  1248. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1249. "List of TODO entry keyword sequences and their interpretation.
  1250. \\<org-mode-map>This is a list of sequences.
  1251. Each sequence starts with a symbol, either `sequence' or `type',
  1252. indicating if the keywords should be interpreted as a sequence of
  1253. action steps, or as different types of TODO items. The first
  1254. keywords are states requiring action - these states will select a headline
  1255. for inclusion into the global TODO list Org-mode produces. If one of
  1256. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1257. signify that no further action is necessary. If \"|\" is not found,
  1258. the last keyword is treated as the only DONE state of the sequence.
  1259. The command \\[org-todo] cycles an entry through these states, and one
  1260. additional state where no keyword is present. For details about this
  1261. cycling, see the manual.
  1262. TODO keywords and interpretation can also be set on a per-file basis with
  1263. the special #+SEQ_TODO and #+TYP_TODO lines.
  1264. For backward compatibility, this variable may also be just a list
  1265. of keywords - in this case the interptetation (sequence or type) will be
  1266. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1267. :group 'org-todo
  1268. :group 'org-keywords
  1269. :type '(choice
  1270. (repeat :tag "Old syntax, just keywords"
  1271. (string :tag "Keyword"))
  1272. (repeat :tag "New syntax"
  1273. (cons
  1274. (choice
  1275. :tag "Interpretation"
  1276. (const :tag "Sequence (cycling hits every state)" sequence)
  1277. (const :tag "Type (cycling directly to DONE)" type))
  1278. (repeat
  1279. (string :tag "Keyword"))))))
  1280. (defvar org-todo-keywords-1 nil)
  1281. (make-variable-buffer-local 'org-todo-keywords-1)
  1282. (defvar org-todo-keywords-for-agenda nil)
  1283. (defvar org-done-keywords-for-agenda nil)
  1284. (defvar org-not-done-keywords nil)
  1285. (make-variable-buffer-local 'org-not-done-keywords)
  1286. (defvar org-done-keywords nil)
  1287. (make-variable-buffer-local 'org-done-keywords)
  1288. (defvar org-todo-heads nil)
  1289. (make-variable-buffer-local 'org-todo-heads)
  1290. (defvar org-todo-sets nil)
  1291. (make-variable-buffer-local 'org-todo-sets)
  1292. (defvar org-todo-kwd-alist nil)
  1293. (make-variable-buffer-local 'org-todo-kwd-alist)
  1294. (defcustom org-todo-interpretation 'sequence
  1295. "Controls how TODO keywords are interpreted.
  1296. This variable is in principle obsolete and is only used for
  1297. backward compatibility, if the interpretation of todo keywords is
  1298. not given already in `org-todo-keywords'. See that variable for
  1299. more information."
  1300. :group 'org-todo
  1301. :group 'org-keywords
  1302. :type '(choice (const sequence)
  1303. (const type)))
  1304. (defcustom org-after-todo-state-change-hook nil
  1305. "Hook which is run after the state of a TODO item was changed.
  1306. The new state (a string with a TODO keyword, or nil) is available in the
  1307. Lisp variable `state'."
  1308. :group 'org-todo
  1309. :type 'hook)
  1310. (defcustom org-log-done nil
  1311. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1312. When the state of an entry is changed from nothing to TODO, remove a previous
  1313. closing date.
  1314. This can also be a list of symbols indicating under which conditions
  1315. the time stamp recording the action should be annotated with a short note.
  1316. Valid members of this list are
  1317. done Offer to record a note when marking entries done
  1318. state Offer to record a note whenever changing the TODO state
  1319. of an item. This is only relevant if TODO keywords are
  1320. interpreted as sequence, see variable `org-todo-interpretation'.
  1321. When `state' is set, this includes tracking `done'.
  1322. clock-out Offer to record a note when clocking out of an item.
  1323. A separate window will then pop up and allow you to type a note.
  1324. After finishing with C-c C-c, the note will be added directly after the
  1325. timestamp, as a plain list item. See also the variable
  1326. `org-log-note-headings'.
  1327. Logging can also be configured on a per-file basis by adding one of
  1328. the following lines anywhere in the buffer:
  1329. #+STARTUP: logdone
  1330. #+STARTUP: nologging
  1331. #+STARTUP: lognotedone
  1332. #+STARTUP: lognotestate
  1333. #+STARTUP: lognoteclock-out"
  1334. :group 'org-todo
  1335. :group 'org-progress
  1336. :type '(choice
  1337. (const :tag "off" nil)
  1338. (const :tag "on" t)
  1339. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1340. (const :tag "when item is marked DONE" done)
  1341. (const :tag "when TODO state changes" state)
  1342. (const :tag "when clocking out" clock-out))))
  1343. (defcustom org-log-done-with-time t
  1344. "Non-nil means, the CLOSED time stamp will contain date and time.
  1345. When nil, only the date will be recorded."
  1346. :group 'org-progress
  1347. :type 'boolean)
  1348. (defcustom org-log-note-headings
  1349. '((done . "CLOSING NOTE %t")
  1350. (state . "State %-12s %t")
  1351. (clock-out . ""))
  1352. "Headings for notes added when clocking out or closing TODO items.
  1353. The value is an alist, with the car being a sympol indicating the note
  1354. context, and the cdr is the heading to be used. The heading may also be the
  1355. empty string.
  1356. %t in the heading will be replaced by a time stamp.
  1357. %s will be replaced by the new TODO state, in double quotes.
  1358. %u will be replaced by the user name.
  1359. %U will be replaced by the full user name."
  1360. :group 'org-todo
  1361. :group 'org-progress
  1362. :type '(list :greedy t
  1363. (cons (const :tag "Heading when closing an item" done) string)
  1364. (cons (const :tag
  1365. "Heading when changing todo state (todo sequence only)"
  1366. state) string)
  1367. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1368. (defcustom org-log-repeat t
  1369. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1370. When nil, no note will be taken."
  1371. :group 'org-todo
  1372. :group 'org-progress
  1373. :type 'boolean)
  1374. (defgroup org-priorities nil
  1375. "Priorities in Org-mode."
  1376. :tag "Org Priorities"
  1377. :group 'org-todo)
  1378. (defcustom org-highest-priority ?A
  1379. "The highest priority of TODO items. A character like ?A, ?B etc.
  1380. Must have a smaller ASCII number than `org-lowest-priority'."
  1381. :group 'org-priorities
  1382. :type 'character)
  1383. (defcustom org-lowest-priority ?C
  1384. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1385. Must have a larger ASCII number than `org-highest-priority'."
  1386. :group 'org-priorities
  1387. :type 'character)
  1388. (defcustom org-default-priority ?B
  1389. "The default priority of TODO items.
  1390. This is the priority an item get if no explicit priority is given."
  1391. :group 'org-priorities
  1392. :type 'character)
  1393. (defgroup org-time nil
  1394. "Options concerning time stamps and deadlines in Org-mode."
  1395. :tag "Org Time"
  1396. :group 'org)
  1397. (defcustom org-insert-labeled-timestamps-at-point nil
  1398. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1399. When nil, these labeled time stamps are forces into the second line of an
  1400. entry, just after the headline. When scheduling from the global TODO list,
  1401. the time stamp will always be forced into the second line."
  1402. :group 'org-time
  1403. :type 'boolean)
  1404. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1405. "Formats for `format-time-string' which are used for time stamps.
  1406. It is not recommended to change this constant.")
  1407. (defcustom org-time-stamp-rounding-minutes 0
  1408. "Number of minutes to round time stamps to upon insertion.
  1409. When zero, insert the time unmodified. Useful rounding numbers
  1410. should be factors of 60, so for example 5, 10, 15.
  1411. When this is not zero, you can still force an exact time-stamp by using
  1412. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1413. :group 'org-time
  1414. :type 'integer)
  1415. (defcustom org-display-custom-times nil
  1416. "Non-nil means, overlay custom formats over all time stamps.
  1417. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1418. To turn this on on a per-file basis, insert anywhere in the file:
  1419. #+STARTUP: customtime"
  1420. :group 'org-time
  1421. :set 'set-default
  1422. :type 'sexp)
  1423. (make-variable-buffer-local 'org-display-custom-times)
  1424. (defcustom org-time-stamp-custom-formats
  1425. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1426. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1427. These are overlayed over the default ISO format if the variable
  1428. `org-display-custom-times' is set. Time like %H:%M should be at the
  1429. end of the second format."
  1430. :group 'org-time
  1431. :type 'sexp)
  1432. (defun org-time-stamp-format (&optional long inactive)
  1433. "Get the right format for a time string."
  1434. (let ((f (if long (cdr org-time-stamp-formats)
  1435. (car org-time-stamp-formats))))
  1436. (if inactive
  1437. (concat "[" (substring f 1 -1) "]")
  1438. f)))
  1439. (defcustom org-deadline-warning-days 30
  1440. "No. of days before expiration during which a deadline becomes active.
  1441. This variable governs the display in sparse trees and in the agenda."
  1442. :group 'org-time
  1443. :type 'number)
  1444. (defcustom org-popup-calendar-for-date-prompt t
  1445. "Non-nil means, pop up a calendar when prompting for a date.
  1446. In the calendar, the date can be selected with mouse-1. However, the
  1447. minibuffer will also be active, and you can simply enter the date as well.
  1448. When nil, only the minibuffer will be available."
  1449. :group 'org-time
  1450. :type 'boolean)
  1451. (defcustom org-calendar-follow-timestamp-change t
  1452. "Non-nil means, make the calendar window follow timestamp changes.
  1453. When a timestamp is modified and the calendar window is visible, it will be
  1454. moved to the new date."
  1455. :group 'org-time
  1456. :type 'boolean)
  1457. (defgroup org-tags nil
  1458. "Options concerning tags in Org-mode."
  1459. :tag "Org Tags"
  1460. :group 'org)
  1461. (defcustom org-tag-alist nil
  1462. "List of tags allowed in Org-mode files.
  1463. When this list is nil, Org-mode will base TAG input on what is already in the
  1464. buffer.
  1465. The value of this variable is an alist, the car may be (and should) be a
  1466. character that is used to select that tag through the fast-tag-selection
  1467. interface. See the manual for details."
  1468. :group 'org-tags
  1469. :type '(repeat
  1470. (choice
  1471. (cons (string :tag "Tag name")
  1472. (character :tag "Access char"))
  1473. (const :tag "Start radio group" (:startgroup))
  1474. (const :tag "End radio group" (:endgroup)))))
  1475. (defcustom org-use-fast-tag-selection 'auto
  1476. "Non-nil means, use fast tag selection scheme.
  1477. This is a special interface to select and deselect tags with single keys.
  1478. When nil, fast selection is never used.
  1479. When the symbol `auto', fast selection is used if and only if selection
  1480. characters for tags have been configured, either through the variable
  1481. `org-tag-alist' or through a #+TAGS line in the buffer.
  1482. When t, fast selection is always used and selection keys are assigned
  1483. automatically if necessary."
  1484. :group 'org-tags
  1485. :type '(choice
  1486. (const :tag "Always" t)
  1487. (const :tag "Never" nil)
  1488. (const :tag "When selection characters are configured" 'auto)))
  1489. (defcustom org-fast-tag-selection-single-key nil
  1490. "Non-nil means, fast tag selection exits after first change.
  1491. When nil, you have to press RET to exit it.
  1492. During fast tag selection, you can toggle this flag with `C-c'.
  1493. This variable can also have the value `expert'. In this case, the window
  1494. displaying the tags menu is not even shown, until you press C-c again."
  1495. :group 'org-tags
  1496. :type '(choice
  1497. (const :tag "No" nil)
  1498. (const :tag "Yes" t)
  1499. (const :tag "Expert" expert)))
  1500. (defcustom org-tags-column 48
  1501. "The column to which tags should be indented in a headline.
  1502. If this number is positive, it specifies the column. If it is negative,
  1503. it means that the tags should be flushright to that column. For example,
  1504. -79 works well for a normal 80 character screen."
  1505. :group 'org-tags
  1506. :type 'integer)
  1507. (defcustom org-auto-align-tags t
  1508. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1509. These operations change the length of a headline and therefore shift
  1510. the tags around. With this options turned on, after each such operation
  1511. the tags are again aligned to `org-tags-column'."
  1512. :group 'org-tags
  1513. :type 'boolean)
  1514. (defcustom org-use-tag-inheritance t
  1515. "Non-nil means, tags in levels apply also for sublevels.
  1516. When nil, only the tags directly given in a specific line apply there.
  1517. If you turn off this option, you very likely want to turn on the
  1518. companion option `org-tags-match-list-sublevels'."
  1519. :group 'org-tags
  1520. :type 'boolean)
  1521. (defcustom org-tags-match-list-sublevels nil
  1522. "Non-nil means list also sublevels of headlines matching tag search.
  1523. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1524. the sublevels of a headline matching a tag search often also match
  1525. the same search. Listing all of them can create very long lists.
  1526. Setting this variable to nil causes subtrees of a match to be skipped.
  1527. This option is off by default, because inheritance in on. If you turn
  1528. inheritance off, you very likely want to turn this option on.
  1529. As a special case, if the tag search is restricted to TODO items, the
  1530. value of this variable is ignored and sublevels are always checked, to
  1531. make sure all corresponding TODO items find their way into the list."
  1532. :group 'org-tags
  1533. :type 'boolean)
  1534. (defvar org-tags-history nil
  1535. "History of minibuffer reads for tags.")
  1536. (defvar org-last-tags-completion-table nil
  1537. "The last used completion table for tags.")
  1538. (defgroup org-properties nil
  1539. "Options concerning properties in Org-mode."
  1540. :tag "Org Properties"
  1541. :group 'org)
  1542. (defcustom org-property-format "%-10s %s"
  1543. "How property key/value pairs should be formatted by `indent-line'.
  1544. When `indent-line' hits a property definition, it will format the line
  1545. according to this format, mainly to make sure that the values are
  1546. lined-up with respect to each other."
  1547. :group 'org-properties
  1548. :type 'string)
  1549. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1550. "The default column format, if no other format has been defined.
  1551. This variable can be set on the per-file basis by inserting a line
  1552. #+COLUMNS: %25ITEM ....."
  1553. :group 'org-properties
  1554. :type 'string)
  1555. (defgroup org-agenda nil
  1556. "Options concerning agenda views in Org-mode."
  1557. :tag "Org Agenda"
  1558. :group 'org)
  1559. (defvar org-category nil
  1560. "Variable used by org files to set a category for agenda display.
  1561. Such files should use a file variable to set it, for example
  1562. # -*- mode: org; org-category: \"ELisp\"
  1563. or contain a special line
  1564. #+CATEGORY: ELisp
  1565. If the file does not specify a category, then file's base name
  1566. is used instead.")
  1567. (make-variable-buffer-local 'org-category)
  1568. (defcustom org-agenda-files nil
  1569. "The files to be used for agenda display.
  1570. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1571. \\[org-remove-file]. You can also use customize to edit the list.
  1572. If the value of the variable is not a list but a single file name, then
  1573. the list of agenda files is actually stored and maintained in that file, one
  1574. agenda file per line."
  1575. :group 'org-agenda
  1576. :type '(choice
  1577. (repeat :tag "List of files" file)
  1578. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1579. (defcustom org-agenda-confirm-kill 1
  1580. "When set, remote killing from the agenda buffer needs confirmation.
  1581. When t, a confirmation is always needed. When a number N, confirmation is
  1582. only needed when the text to be killed contains more than N non-white lines."
  1583. :group 'org-agenda
  1584. :type '(choice
  1585. (const :tag "Never" nil)
  1586. (const :tag "Always" t)
  1587. (number :tag "When more than N lines")))
  1588. (defcustom org-calendar-to-agenda-key [?c]
  1589. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1590. The command `org-calendar-goto-agenda' will be bound to this key. The
  1591. default is the character `c' because then `c' can be used to switch back and
  1592. forth between agenda and calendar."
  1593. :group 'org-agenda
  1594. :type 'sexp)
  1595. (defgroup org-agenda-export nil
  1596. "Options concerning exporting agenda views in Org-mode."
  1597. :tag "Org Agenda Export"
  1598. :group 'org-agenda)
  1599. (defcustom org-agenda-with-colors t
  1600. "Non-nil means, use colors in agenda views."
  1601. :group 'org-agenda-export
  1602. :type 'boolean)
  1603. (defcustom org-agenda-exporter-settings nil
  1604. "Alist of variable/value pairs that should be active during agenda export.
  1605. This is a good place to set uptions for ps-print and for htmlize."
  1606. :group 'org-agenda-export
  1607. :type '(repeat
  1608. (list
  1609. (variable)
  1610. (sexp :tag "Value"))))
  1611. (defcustom org-agenda-export-html-style ""
  1612. "The style specification for exported HTML Agenda files.
  1613. If this variable contains a string, it will replace the default <style>
  1614. section as produced by `htmlize'.
  1615. Since there are different ways of setting style information, this variable
  1616. needs to contain the full HTML structure to provide a style, including the
  1617. surrounding HTML tags. The style specifications should include definitions
  1618. the fonts used by the agenda, here is an example:
  1619. <style type=\"text/css\">
  1620. p { font-weight: normal; color: gray; }
  1621. .org-agenda-structure {
  1622. font-size: 110%;
  1623. color: #003399;
  1624. font-weight: 600;
  1625. }
  1626. .org-todo {
  1627. color: #cc6666;Week-agenda:
  1628. font-weight: bold;
  1629. }
  1630. .org-done {
  1631. color: #339933;
  1632. }
  1633. .title { text-align: center; }
  1634. .todo, .deadline { color: red; }
  1635. .done { color: green; }
  1636. </style>
  1637. or, if you want to keep the style in a file,
  1638. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1639. As the value of this option simply gets inserted into the HTML <head> header,
  1640. you can \"misuse\" it to also add other text to the header. However,
  1641. <style>...</style> is required, if not present the variable will be ignored."
  1642. :group 'org-agenda-export
  1643. :group 'org-export-html
  1644. :type 'string)
  1645. (defgroup org-agenda-custom-commands nil
  1646. "Options concerning agenda views in Org-mode."
  1647. :tag "Org Agenda Custom Commands"
  1648. :group 'org-agenda)
  1649. (defcustom org-agenda-custom-commands nil
  1650. "Custom commands for the agenda.
  1651. These commands will be offered on the splash screen displayed by the
  1652. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1653. (key type match options files)
  1654. key The key (a single char as a string) to be associated with the command.
  1655. type The command type, any of the following symbols:
  1656. todo Entries with a specific TODO keyword, in all agenda files.
  1657. tags Tags match in all agenda files.
  1658. tags-todo Tags match in all agenda files, TODO entries only.
  1659. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1660. tags-tree Sparse tree with all tags matches in *current* file.
  1661. occur-tree Occur sparse tree for *current* file.
  1662. match What to search for:
  1663. - a single keyword for TODO keyword searches
  1664. - a tags match expression for tags searches
  1665. - a regular expression for occur searches
  1666. options A list of option setttings, similar to that in a let form, so like
  1667. this: ((opt1 val1) (opt2 val2) ...)
  1668. files A list of files file to write the produced agenda buffer to
  1669. with the command `org-store-agenda-views'.
  1670. If a file name ends in \".html\", an HTML version of the buffer
  1671. is written out. If it ends in \".ps\", a postscript version is
  1672. produced. Otherwide, only the plain text is written to the file.
  1673. You can also define a set of commands, to create a composite agenda buffer.
  1674. In this case, an entry looks like this:
  1675. (key desc (cmd1 cmd2 ...) general-options file)
  1676. where
  1677. desc A description string to be displayed in the dispatcher menu.
  1678. cmd An agenda command, similar to the above. However, tree commands
  1679. are no allowed, but instead you can get agenda and global todo list.
  1680. So valid commands for a set are:
  1681. (agenda)
  1682. (alltodo)
  1683. (stuck)
  1684. (todo \"match\" options files)
  1685. (tags \"match\" options files)
  1686. (tags-todo \"match\" options files)
  1687. Each command can carry a list of options, and another set of options can be
  1688. given for the whole set of commands. Individual command options take
  1689. precedence over the general options."
  1690. :group 'org-agenda-custom-commands
  1691. :type '(repeat
  1692. (choice :value ("a" tags "" nil)
  1693. (list :tag "Single command"
  1694. (string :tag "Key")
  1695. (choice
  1696. (const :tag "Agenda" agenda)
  1697. (const :tag "TODO list" alltodo)
  1698. (const :tag "Stuck projects" stuck)
  1699. (const :tag "Tags search (all agenda files)" tags)
  1700. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1701. (const :tag "TODO keyword search (all agenda files)" todo)
  1702. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1703. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1704. (const :tag "Occur tree (current buffer)" occur-tree)
  1705. (symbol :tag "Other, user-defined function"))
  1706. (string :tag "Match")
  1707. (repeat :tag "Local options"
  1708. (list (variable :tag "Option") (sexp :tag "Value")))
  1709. (option (repeat :tag "Export" (file :tag "Export to"))))
  1710. (list :tag "Command series, all agenda files"
  1711. (string :tag "Key")
  1712. (string :tag "Description")
  1713. (repeat
  1714. (choice
  1715. (const :tag "Agenda" (agenda))
  1716. (const :tag "TODO list" (alltodo))
  1717. (const :tag "Stuck projects" (stuck))
  1718. (list :tag "Tags search"
  1719. (const :format "" tags)
  1720. (string :tag "Match")
  1721. (repeat :tag "Local options"
  1722. (list (variable :tag "Option")
  1723. (sexp :tag "Value"))))
  1724. (list :tag "Tags search, TODO entries only"
  1725. (const :format "" tags-todo)
  1726. (string :tag "Match")
  1727. (repeat :tag "Local options"
  1728. (list (variable :tag "Option")
  1729. (sexp :tag "Value"))))
  1730. (list :tag "TODO keyword search"
  1731. (const :format "" todo)
  1732. (string :tag "Match")
  1733. (repeat :tag "Local options"
  1734. (list (variable :tag "Option")
  1735. (sexp :tag "Value"))))
  1736. (list :tag "Other, user-defined function"
  1737. (symbol :tag "function")
  1738. (string :tag "Match")
  1739. (repeat :tag "Local options"
  1740. (list (variable :tag "Option")
  1741. (sexp :tag "Value"))))))
  1742. (repeat :tag "General options"
  1743. (list (variable :tag "Option")
  1744. (sexp :tag "Value")))
  1745. (option (repeat :tag "Export" (file :tag "Export to")))))))
  1746. (defcustom org-stuck-projects
  1747. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1748. "How to identify stuck projects.
  1749. This is a list of four items:
  1750. 1. A tags/todo matcher string that is used to identify a project.
  1751. The entire tree below a headline matched by this is considered one project.
  1752. 2. A list of TODO keywords identifying non-stuck projects.
  1753. If the project subtree contains any headline with one of these todo
  1754. keywords, the project is considered to be not stuck. If you specify
  1755. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1756. 3. A list of tags identifying non-stuck projects.
  1757. If the project subtree contains any headline with one of these tags,
  1758. the project is considered to be not stuck. If you specify \"*\" as
  1759. a tag, any tag will mark the project unstuck.
  1760. 4. An arbitrary regular expression matching non-stuck projects.
  1761. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1762. or `C-c a #' to produce the list."
  1763. :group 'org-agenda-custom-commands
  1764. :type '(list
  1765. (string :tag "Tags/TODO match to identify a project")
  1766. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1767. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1768. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1769. (defgroup org-agenda-skip nil
  1770. "Options concerning skipping parts of agenda files."
  1771. :tag "Org Agenda Skip"
  1772. :group 'org-agenda)
  1773. (defcustom org-agenda-todo-list-sublevels t
  1774. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1775. When nil, the sublevels of a TODO entry are not checked, resulting in
  1776. potentially much shorter TODO lists."
  1777. :group 'org-agenda-skip
  1778. :group 'org-todo
  1779. :type 'boolean)
  1780. (defcustom org-agenda-todo-ignore-scheduled nil
  1781. "Non-nil means, don't show scheduled entries in the global todo list.
  1782. The idea behind this is that by scheduling it, you have already taken care
  1783. of this item."
  1784. :group 'org-agenda-skip
  1785. :group 'org-todo
  1786. :type 'boolean)
  1787. (defcustom org-agenda-todo-ignore-deadlines nil
  1788. "Non-nil means, don't show near deadline entries in the global todo list.
  1789. Near means closer than `org-deadline-warning-days' days.
  1790. The idea behind this is that such items will appear in the agenda anyway."
  1791. :group 'org-agenda-skip
  1792. :group 'org-todo
  1793. :type 'boolean)
  1794. (defcustom org-agenda-skip-scheduled-if-done nil
  1795. "Non-nil means don't show scheduled items in agenda when they are done.
  1796. This is relevant for the daily/weekly agenda, not for the TODO list."
  1797. :group 'org-agenda-skip
  1798. :type 'boolean)
  1799. (defcustom org-agenda-skip-deadline-if-done nil
  1800. "Non-nil means don't show deadines when the corresponding item is done.
  1801. When nil, the deadline is still shown and should give you a happy feeling.
  1802. This is relevant for the daily/weekly agenda."
  1803. :group 'org-agenda-skip
  1804. :type 'boolean)
  1805. (defcustom org-timeline-show-empty-dates 3
  1806. "Non-nil means, `org-timeline' also shows dates without an entry.
  1807. When nil, only the days which actually have entries are shown.
  1808. When t, all days between the first and the last date are shown.
  1809. When an integer, show also empty dates, but if there is a gap of more than
  1810. N days, just insert a special line indicating the size of the gap."
  1811. :group 'org-agenda-skip
  1812. :type '(choice
  1813. (const :tag "None" nil)
  1814. (const :tag "All" t)
  1815. (number :tag "at most")))
  1816. (defgroup org-agenda-startup nil
  1817. "Options concerning initial settings in the Agenda in Org Mode."
  1818. :tag "Org Agenda Startup"
  1819. :group 'org-agenda)
  1820. (defcustom org-finalize-agenda-hook nil
  1821. "Hook run just before displaying an agenda buffer."
  1822. :group 'org-agenda-startup
  1823. :type 'hook)
  1824. (defcustom org-agenda-mouse-1-follows-link nil
  1825. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1826. A longer mouse click will still set point. Does not wortk on XEmacs.
  1827. Needs to be set before org.el is loaded."
  1828. :group 'org-agenda-startup
  1829. :type 'boolean)
  1830. (defcustom org-agenda-start-with-follow-mode nil
  1831. "The initial value of follow-mode in a newly created agenda window."
  1832. :group 'org-agenda-startup
  1833. :type 'boolean)
  1834. (defgroup org-agenda-windows nil
  1835. "Options concerning the windows used by the Agenda in Org Mode."
  1836. :tag "Org Agenda Windows"
  1837. :group 'org-agenda)
  1838. (defcustom org-agenda-window-setup 'reorganize-frame
  1839. "How the agenda buffer should be displayed.
  1840. Possible values for this option are:
  1841. current-window Show agenda in the current window, keeping all other windows.
  1842. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1843. other-window Use `switch-to-buffer-other-window' to display agenda.
  1844. reorganize-frame Show only two windows on the current frame, the current
  1845. window and the agenda.
  1846. See also the variable `org-agenda-restore-windows-after-quit'."
  1847. :group 'org-agenda-windows
  1848. :type '(choice
  1849. (const current-window)
  1850. (const other-frame)
  1851. (const other-window)
  1852. (const reorganize-frame)))
  1853. (defcustom org-agenda-restore-windows-after-quit nil
  1854. "Non-nil means, restore window configuration open exiting agenda.
  1855. Before the window configuration is changed for displaying the agenda,
  1856. the current status is recorded. When the agenda is exited with
  1857. `q' or `x' and this option is set, the old state is restored. If
  1858. `org-agenda-window-setup' is `other-frame', the value of this
  1859. option will be ignored.."
  1860. :group 'org-agenda-windows
  1861. :type 'boolean)
  1862. (defcustom org-indirect-buffer-display 'other-window
  1863. "How should indirect tree buffers be displayed?
  1864. This applies to indirect buffers created with the commands
  1865. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1866. Valid values are:
  1867. current-window Display in the current window
  1868. other-window Just display in another window.
  1869. dedicated-frame Create one new frame, and re-use it each time.
  1870. new-frame Make a new frame each time."
  1871. :group 'org-structure
  1872. :group 'org-agenda-windows
  1873. :type '(choice
  1874. (const :tag "In current window" current-window)
  1875. (const :tag "In current frame, other window" other-window)
  1876. (const :tag "Each time a new frame" new-frame)
  1877. (const :tag "One dedicated frame" dedicated-frame)))
  1878. (defgroup org-agenda-daily/weekly nil
  1879. "Options concerning the daily/weekly agenda."
  1880. :tag "Org Agenda Daily/Weekly"
  1881. :group 'org-agenda)
  1882. (defcustom org-agenda-ndays 7
  1883. "Number of days to include in overview display.
  1884. Should be 1 or 7."
  1885. :group 'org-agenda-daily/weekly
  1886. :type 'number)
  1887. (defcustom org-agenda-start-on-weekday 1
  1888. "Non-nil means, start the overview always on the specified weekday.
  1889. 0 denotes Sunday, 1 denotes Monday etc.
  1890. When nil, always start on the current day."
  1891. :group 'org-agenda-daily/weekly
  1892. :type '(choice (const :tag "Today" nil)
  1893. (number :tag "Weekday No.")))
  1894. (defcustom org-agenda-show-all-dates t
  1895. "Non-nil means, `org-agenda' shows every day in the selected range.
  1896. When nil, only the days which actually have entries are shown."
  1897. :group 'org-agenda-daily/weekly
  1898. :type 'boolean)
  1899. (defcustom org-agenda-date-format "%A %d %B %Y"
  1900. "Format string for displaying dates in the agenda.
  1901. Used by the daily/weekly agenda and by the timeline. This should be
  1902. a format string understood by `format-time-string'.
  1903. FIXME: Not used currently, because of timezone problem."
  1904. :group 'org-agenda-daily/weekly
  1905. :type 'string)
  1906. (defcustom org-agenda-include-diary nil
  1907. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1908. :group 'org-agenda-daily/weekly
  1909. :type 'boolean)
  1910. (defcustom org-agenda-include-all-todo nil
  1911. "Set means weekly/daily agenda will always contain all TODO entries.
  1912. The TODO entries will be listed at the top of the agenda, before
  1913. the entries for specific days."
  1914. :group 'org-agenda-daily/weekly
  1915. :type 'boolean)
  1916. (defgroup org-agenda-time-grid nil
  1917. "Options concerning the time grid in the Org-mode Agenda."
  1918. :tag "Org Agenda Time Grid"
  1919. :group 'org-agenda)
  1920. (defcustom org-agenda-use-time-grid t
  1921. "Non-nil means, show a time grid in the agenda schedule.
  1922. A time grid is a set of lines for specific times (like every two hours between
  1923. 8:00 and 20:00). The items scheduled for a day at specific times are
  1924. sorted in between these lines.
  1925. For details about when the grid will be shown, and what it will look like, see
  1926. the variable `org-agenda-time-grid'."
  1927. :group 'org-agenda-time-grid
  1928. :type 'boolean)
  1929. (defcustom org-agenda-time-grid
  1930. '((daily today require-timed)
  1931. "----------------"
  1932. (800 1000 1200 1400 1600 1800 2000))
  1933. "The settings for time grid for agenda display.
  1934. This is a list of three items. The first item is again a list. It contains
  1935. symbols specifying conditions when the grid should be displayed:
  1936. daily if the agenda shows a single day
  1937. weekly if the agenda shows an entire week
  1938. today show grid on current date, independent of daily/weekly display
  1939. require-timed show grid only if at least one item has a time specification
  1940. The second item is a string which will be places behing the grid time.
  1941. The third item is a list of integers, indicating the times that should have
  1942. a grid line."
  1943. :group 'org-agenda-time-grid
  1944. :type
  1945. '(list
  1946. (set :greedy t :tag "Grid Display Options"
  1947. (const :tag "Show grid in single day agenda display" daily)
  1948. (const :tag "Show grid in weekly agenda display" weekly)
  1949. (const :tag "Always show grid for today" today)
  1950. (const :tag "Show grid only if any timed entries are present"
  1951. require-timed)
  1952. (const :tag "Skip grid times already present in an entry"
  1953. remove-match))
  1954. (string :tag "Grid String")
  1955. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1956. (defgroup org-agenda-sorting nil
  1957. "Options concerning sorting in the Org-mode Agenda."
  1958. :tag "Org Agenda Sorting"
  1959. :group 'org-agenda)
  1960. (let ((sorting-choice
  1961. '(choice
  1962. (const time-up) (const time-down)
  1963. (const category-keep) (const category-up) (const category-down)
  1964. (const tag-down) (const tag-up)
  1965. (const priority-up) (const priority-down))))
  1966. (defcustom org-agenda-sorting-strategy
  1967. '((agenda time-up category-keep priority-down)
  1968. (todo category-keep priority-down)
  1969. (tags category-keep priority-down))
  1970. "Sorting structure for the agenda items of a single day.
  1971. This is a list of symbols which will be used in sequence to determine
  1972. if an entry should be listed before another entry. The following
  1973. symbols are recognized:
  1974. time-up Put entries with time-of-day indications first, early first
  1975. time-down Put entries with time-of-day indications first, late first
  1976. category-keep Keep the default order of categories, corresponding to the
  1977. sequence in `org-agenda-files'.
  1978. category-up Sort alphabetically by category, A-Z.
  1979. category-down Sort alphabetically by category, Z-A.
  1980. tag-up Sort alphabetically by last tag, A-Z.
  1981. tag-down Sort alphabetically by last tag, Z-A.
  1982. priority-up Sort numerically by priority, high priority last.
  1983. priority-down Sort numerically by priority, high priority first.
  1984. The different possibilities will be tried in sequence, and testing stops
  1985. if one comparison returns a \"not-equal\". For example, the default
  1986. '(time-up category-keep priority-down)
  1987. means: Pull out all entries having a specified time of day and sort them,
  1988. in order to make a time schedule for the current day the first thing in the
  1989. agenda listing for the day. Of the entries without a time indication, keep
  1990. the grouped in categories, don't sort the categories, but keep them in
  1991. the sequence given in `org-agenda-files'. Within each category sort by
  1992. priority.
  1993. Leaving out `category-keep' would mean that items will be sorted across
  1994. categories by priority."
  1995. :group 'org-agenda-sorting
  1996. :type `(choice
  1997. (repeat :tag "General" ,sorting-choice)
  1998. (list :tag "Individually"
  1999. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2000. (repeat ,sorting-choice))
  2001. (cons (const :tag "Strategy for TODO lists" todo)
  2002. (repeat ,sorting-choice))
  2003. (cons (const :tag "Strategy for Tags matches" tags)
  2004. (repeat ,sorting-choice))))))
  2005. (defcustom org-sort-agenda-notime-is-late t
  2006. "Non-nil means, items without time are considered late.
  2007. This is only relevant for sorting. When t, items which have no explicit
  2008. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2009. do have a time. When nil, the default time is before 0:00. You can use this
  2010. option to decide if the schedule for today should come before or after timeless
  2011. agenda entries."
  2012. :group 'org-agenda-sorting
  2013. :type 'boolean)
  2014. (defgroup org-agenda-prefix nil
  2015. "Options concerning the entry prefix in the Org-mode agenda display."
  2016. :tag "Org Agenda Prefix"
  2017. :group 'org-agenda)
  2018. (defcustom org-agenda-prefix-format
  2019. '((agenda . " %-12:c%?-12t% s")
  2020. (timeline . " % s")
  2021. (todo . " %-12:c")
  2022. (tags . " %-12:c"))
  2023. "Format specifications for the prefix of items in the agenda views.
  2024. An alist with four entries, for the different agenda types. The keys to the
  2025. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2026. are format strings.
  2027. This format works similar to a printf format, with the following meaning:
  2028. %c the category of the item, \"Diary\" for entries from the diary, or
  2029. as given by the CATEGORY keyword or derived from the file name.
  2030. %T the *last* tag of the item. Last because inherited tags come
  2031. first in the list.
  2032. %t the time-of-day specification if one applies to the entry, in the
  2033. format HH:MM
  2034. %s Scheduling/Deadline information, a short string
  2035. All specifiers work basically like the standard `%s' of printf, but may
  2036. contain two additional characters: A question mark just after the `%' and
  2037. a whitespace/punctuation character just before the final letter.
  2038. If the first character after `%' is a question mark, the entire field
  2039. will only be included if the corresponding value applies to the
  2040. current entry. This is useful for fields which should have fixed
  2041. width when present, but zero width when absent. For example,
  2042. \"%?-12t\" will result in a 12 character time field if a time of the
  2043. day is specified, but will completely disappear in entries which do
  2044. not contain a time.
  2045. If there is punctuation or whitespace character just before the final
  2046. format letter, this character will be appended to the field value if
  2047. the value is not empty. For example, the format \"%-12:c\" leads to
  2048. \"Diary: \" if the category is \"Diary\". If the category were be
  2049. empty, no additional colon would be interted.
  2050. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2051. - Indent the line with two space characters
  2052. - Give the category in a 12 chars wide field, padded with whitespace on
  2053. the right (because of `-'). Append a colon if there is a category
  2054. (because of `:').
  2055. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2056. time, don't put in an empty field, just skip it (because of '?').
  2057. - Finally, put the scheduling information and append a whitespace.
  2058. As another example, if you don't want the time-of-day of entries in
  2059. the prefix, you could use:
  2060. (setq org-agenda-prefix-format \" %-11:c% s\")
  2061. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2062. `org-agenda-remove-tags'."
  2063. :type '(choice
  2064. (string :tag "General format")
  2065. (list :greedy t :tag "View dependent"
  2066. (cons (const agenda) (string :tag "Format"))
  2067. (cons (const timeline) (string :tag "Format"))
  2068. (cons (const todo) (string :tag "Format"))
  2069. (cons (const tags) (string :tag "Format"))))
  2070. :group 'org-agenda-prefix)
  2071. (defvar org-prefix-format-compiled nil
  2072. "The compiled version of the most recently used prefix format.
  2073. See the variable `org-agenda-prefix-format'.")
  2074. (defcustom org-agenda-remove-times-when-in-prefix t
  2075. "Non-nil means, remove duplicate time specifications in agenda items.
  2076. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2077. time-of-day specification in a headline or diary entry is extracted and
  2078. placed into the prefix. If this option is non-nil, the original specification
  2079. \(a timestamp or -range, or just a plain time(range) specification like
  2080. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2081. cluttered.
  2082. The option can be t or nil. It may also be the symbol `beg', indicating
  2083. that the time should only be removed what it is located at the beginning of
  2084. the headline/diary entry."
  2085. :group 'org-agenda-prefix
  2086. :type '(choice
  2087. (const :tag "Always" t)
  2088. (const :tag "Never" nil)
  2089. (const :tag "When at beginning of entry" beg)))
  2090. (defcustom org-agenda-default-appointment-duration nil
  2091. "Default duration for appointments that only have a starting time.
  2092. When nil, no duration is specified in such cases.
  2093. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2094. :group 'org-agenda-prefix
  2095. :type '(choice
  2096. (integer :tag "Minutes")
  2097. (const :tag "No default duration")))
  2098. (defcustom org-agenda-remove-tags nil
  2099. "Non-nil means, remove the tags from the headline copy in the agenda.
  2100. When this is the symbol `prefix', only remove tags when
  2101. `org-agenda-prefix-format' contains a `%T' specifier."
  2102. :group 'org-agenda-prefix
  2103. :type '(choice
  2104. (const :tag "Always" t)
  2105. (const :tag "Never" nil)
  2106. (const :tag "When prefix format contains %T" prefix)))
  2107. (if (fboundp 'defvaralias)
  2108. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2109. 'org-agenda-remove-tags))
  2110. (defcustom org-agenda-align-tags-to-column 65
  2111. "Shift tags in agenda items to this column."
  2112. :group 'org-agenda-prefix
  2113. :type 'integer)
  2114. (defgroup org-latex nil
  2115. "Options for embedding LaTeX code into Org-mode"
  2116. :tag "Org LaTeX"
  2117. :group 'org)
  2118. (defcustom org-format-latex-options
  2119. '(:foreground default :background default :scale 1.0
  2120. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2121. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2122. "Options for creating images from LaTeX fragments.
  2123. This is a property list with the following properties:
  2124. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2125. `default' means use the forground of the default face.
  2126. :background the background color, or \"Transparent\".
  2127. `default' means use the background of the default face.
  2128. :scale a scaling factor for the size of the images
  2129. :html-foreground, :html-background, :html-scale
  2130. The same numbers for HTML export.
  2131. :matchers a list indicating which matchers should be used to
  2132. find LaTeX fragments. Valid members of this list are:
  2133. \"begin\" find environments
  2134. \"$\" find math expressions surrounded by $...$
  2135. \"$$\" find math expressions surrounded by $$....$$
  2136. \"\\(\" find math expressions surrounded by \\(...\\)
  2137. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2138. :group 'org-latex
  2139. :type 'plist)
  2140. (defcustom org-format-latex-header "\\documentclass{article}
  2141. \\usepackage{fullpage} % do not remove
  2142. \\usepackage{amssymb}
  2143. \\usepackage[usenames]{color}
  2144. \\usepackage{amsmath}
  2145. \\usepackage{latexsym}
  2146. \\usepackage[mathscr]{eucal}
  2147. \\pagestyle{empty} % do not remove"
  2148. "The document header used for processing LaTeX fragments."
  2149. :group 'org-latex
  2150. :type 'string)
  2151. (defgroup org-export nil
  2152. "Options for exporting org-listings."
  2153. :tag "Org Export"
  2154. :group 'org)
  2155. (defgroup org-export-general nil
  2156. "General options for exporting Org-mode files."
  2157. :tag "Org Export General"
  2158. :group 'org-export)
  2159. (defcustom org-export-publishing-directory "."
  2160. "Path to the location where exported files should be located.
  2161. This path may be relative to the directory where the Org-mode file lives.
  2162. The default is to put them into the same directory as the Org-mode file.
  2163. The variable may also be an alist with export types `:html', `:ascii',
  2164. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  2165. is relative, it is interpreted relative to the directory where the exported
  2166. Org-mode files lives."
  2167. :group 'org-export-general
  2168. :type '(choice
  2169. (directory)
  2170. (repeat
  2171. (cons
  2172. (choice :tag "Type"
  2173. (const :html) (const :ascii) (const :ical) (const :xoxo))
  2174. (directory)))))
  2175. (defcustom org-export-language-setup
  2176. '(("en" "Author" "Date" "Table of Contents")
  2177. ("cs" "Autor" "Datum" "Obsah")
  2178. ("da" "Ophavsmand" "Dato" "Indhold")
  2179. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2180. ("es" "Autor" "Fecha" "\xccndice")
  2181. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  2182. ("it" "Autore" "Data" "Indice")
  2183. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2184. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2185. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2186. "Terms used in export text, translated to different languages.
  2187. Use the variable `org-export-default-language' to set the language,
  2188. or use the +OPTION lines for a per-file setting."
  2189. :group 'org-export-general
  2190. :type '(repeat
  2191. (list
  2192. (string :tag "HTML language tag")
  2193. (string :tag "Author")
  2194. (string :tag "Date")
  2195. (string :tag "Table of Contents"))))
  2196. (defcustom org-export-default-language "en"
  2197. "The default language of HTML export, as a string.
  2198. This should have an association in `org-export-language-setup'."
  2199. :group 'org-export-general
  2200. :type 'string)
  2201. (defcustom org-export-skip-text-before-1st-heading t
  2202. "Non-nil means, skip all text before the first headline when exporting.
  2203. When nil, that text is exported as well."
  2204. :group 'org-export-general
  2205. :type 'boolean)
  2206. (defcustom org-export-headline-levels 3
  2207. "The last level which is still exported as a headline.
  2208. Inferior levels will produce itemize lists when exported.
  2209. Note that a numeric prefix argument to an exporter function overrides
  2210. this setting.
  2211. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2212. :group 'org-export-general
  2213. :type 'number)
  2214. (defcustom org-export-with-section-numbers t
  2215. "Non-nil means, add section numbers to headlines when exporting.
  2216. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2217. :group 'org-export-general
  2218. :type 'boolean)
  2219. (defcustom org-export-with-toc t
  2220. "Non-nil means, create a table of contents in exported files.
  2221. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2222. When an integer, include levels up to N in the toc, this may then be
  2223. different from `org-export-headline-levels', but it will not be allowed
  2224. to be larger than the number of headline levels.
  2225. When nil, no table of contents is made.
  2226. Headlines which contain any TODO items will be marked with \"(*)\" in
  2227. ASCII export, and with red color in HTML output, if the option
  2228. `org-export-mark-todo-in-toc' is set.
  2229. In HTML output, the TOC will be clickable.
  2230. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2231. or \"toc:3\"."
  2232. :group 'org-export-general
  2233. :type '(choice
  2234. (const :tag "No Table of Contents" nil)
  2235. (const :tag "Full Table of Contents" t)
  2236. (integer :tag "TOC to level")))
  2237. (defcustom org-export-mark-todo-in-toc nil
  2238. "Non-nil means, mark TOC lines that contain any open TODO items."
  2239. :group 'org-export-general
  2240. :type 'boolean)
  2241. (defcustom org-export-preserve-breaks nil
  2242. "Non-nil means, preserve all line breaks when exporting.
  2243. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2244. export, line breaks will always be preserved, regardless of this variable.
  2245. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2246. :group 'org-export-general
  2247. :type 'boolean)
  2248. (defcustom org-export-with-archived-trees 'headline
  2249. "Whether subtrees with the ARCHIVE tag should be exported.
  2250. This can have three different values
  2251. nil Do not export, pretend this tree is not present
  2252. t Do export the entire tree
  2253. headline Only export the headline, but skip the tree below it."
  2254. :group 'org-export-general
  2255. :group 'org-archive
  2256. :type '(choice
  2257. (const :tag "not at all" nil)
  2258. (const :tag "headline only" 'headline)
  2259. (const :tag "entirely" t)))
  2260. (defcustom org-export-with-timestamps t
  2261. "If nil, do not export time stamps and associated keywords."
  2262. :group 'org-export-general
  2263. :type 'boolean)
  2264. (defcustom org-export-remove-timestamps-from-toc t
  2265. "If nil, remove timestamps from the table of contents entries."
  2266. :group 'org-export-general
  2267. :type 'boolean)
  2268. (defcustom org-export-with-tags 'not-in-toc
  2269. "If nil, do not export tags, just remove them from headlines.
  2270. If this is the symbol `not-in-toc', tags will be removed from table of
  2271. contents entries, but still be shown in the headlines of the document."
  2272. :group 'org-export-general
  2273. :type '(choice
  2274. (const :tag "Off" nil)
  2275. (const :tag "Not in TOC" not-in-toc)
  2276. (const :tag "On" t)))
  2277. (defcustom org-export-with-property-drawer nil
  2278. "Non-nil means, export property drawers.
  2279. When nil, these drawers are removed before export.
  2280. This option can also be set with the +OPTIONS line, e.g. \"p:t\"."
  2281. :group 'org-export-general
  2282. :type 'boolean)
  2283. (defgroup org-export-translation nil
  2284. "Options for translating special ascii sequences for the export backends."
  2285. :tag "Org Export Translation"
  2286. :group 'org-export)
  2287. (defcustom org-export-with-emphasize t
  2288. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2289. If the export target supports emphasizing text, the word will be
  2290. typeset in bold, italic, or underlined, respectively. Works only for
  2291. single words, but you can say: I *really* *mean* *this*.
  2292. Not all export backends support this.
  2293. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2294. :group 'org-export-translation
  2295. :type 'boolean)
  2296. (defcustom org-export-with-footnotes t
  2297. "If nil, export [1] as a footnote marker.
  2298. Lines starting with [1] will be formatted as footnotes.
  2299. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2300. :group 'org-export-translation
  2301. :type 'boolean)
  2302. (defcustom org-export-with-sub-superscripts t
  2303. "Non-nil means, interpret \"_\" and \"^\" for export.
  2304. When this option is turned on, you can use TeX-like syntax for sub- and
  2305. superscripts. Several characters after \"_\" or \"^\" will be
  2306. considered as a single item - so grouping with {} is normally not
  2307. needed. For example, the following things will be parsed as single
  2308. sub- or superscripts.
  2309. 10^24 or 10^tau several digits will be considered 1 item.
  2310. 10^-12 or 10^-tau a leading sign with digits or a word
  2311. x^2-y^3 will be read as x^2 - y^3, because items are
  2312. terminated by almost any nonword/nondigit char.
  2313. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2314. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2315. sub/superscript. If you set this variable to the symbol `{}',
  2316. the braces are *required* in order to trigger interpretations as
  2317. sub/superscript. This can be helpful in documents that need \"_\"
  2318. frequently in plain text.
  2319. Not all export backends support this, but HTML does.
  2320. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2321. :group 'org-export-translation
  2322. :type '(choice
  2323. (const :tag "Always interpret" t)
  2324. (const :tag "Only with braces" {})
  2325. (const :tag "Never interpret" nil)))
  2326. (defcustom org-export-with-TeX-macros t
  2327. "Non-nil means, interpret simple TeX-like macros when exporting.
  2328. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2329. No only real TeX macros will work here, but the standard HTML entities
  2330. for math can be used as macro names as well. For a list of supported
  2331. names in HTML export, see the constant `org-html-entities'.
  2332. Not all export backends support this.
  2333. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2334. :group 'org-export-translation
  2335. :group 'org-latex
  2336. :type 'boolean)
  2337. (defcustom org-export-with-LaTeX-fragments nil
  2338. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2339. When set, the exporter will find LaTeX environments if the \\begin line is
  2340. the first non-white thing on a line. It will also find the math delimiters
  2341. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2342. display math.
  2343. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2344. :group 'org-export-translation
  2345. :group 'org-latex
  2346. :type 'boolean)
  2347. (defcustom org-export-with-fixed-width t
  2348. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2349. This can be used to have pre-formatted text, fragments of code etc. For
  2350. example:
  2351. : ;; Some Lisp examples
  2352. : (while (defc cnt)
  2353. : (ding))
  2354. will be looking just like this in also HTML. See also the QUOTE keyword.
  2355. Not all export backends support this.
  2356. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2357. :group 'org-export-translation
  2358. :type 'boolean)
  2359. (defcustom org-match-sexp-depth 3
  2360. "Number of stacked braces for sub/superscript matching.
  2361. This has to be set before loading org.el to be effective."
  2362. :group 'org-export-translation
  2363. :type 'integer)
  2364. (defgroup org-export-tables nil
  2365. "Options for exporting tables in Org-mode."
  2366. :tag "Org Export Tables"
  2367. :group 'org-export)
  2368. (defcustom org-export-with-tables t
  2369. "If non-nil, lines starting with \"|\" define a table.
  2370. For example:
  2371. | Name | Address | Birthday |
  2372. |-------------+----------+-----------|
  2373. | Arthur Dent | England | 29.2.2100 |
  2374. Not all export backends support this.
  2375. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2376. :group 'org-export-tables
  2377. :type 'boolean)
  2378. (defcustom org-export-highlight-first-table-line t
  2379. "Non-nil means, highlight the first table line.
  2380. In HTML export, this means use <th> instead of <td>.
  2381. In tables created with table.el, this applies to the first table line.
  2382. In Org-mode tables, all lines before the first horizontal separator
  2383. line will be formatted with <th> tags."
  2384. :group 'org-export-tables
  2385. :type 'boolean)
  2386. (defcustom org-export-table-remove-special-lines t
  2387. "Remove special lines and marking characters in calculating tables.
  2388. This removes the special marking character column from tables that are set
  2389. up for spreadsheet calculations. It also removes the entire lines
  2390. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2391. the values of constants may be useful to have."
  2392. :group 'org-export-tables
  2393. :type 'boolean)
  2394. (defcustom org-export-prefer-native-exporter-for-tables nil
  2395. "Non-nil means, always export tables created with table.el natively.
  2396. Natively means, use the HTML code generator in table.el.
  2397. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2398. the table does not use row- or column-spanning). This has the
  2399. advantage, that the automatic HTML conversions for math symbols and
  2400. sub/superscripts can be applied. Org-mode's HTML generator is also
  2401. much faster."
  2402. :group 'org-export-tables
  2403. :type 'boolean)
  2404. (defgroup org-export-ascii nil
  2405. "Options specific for ASCII export of Org-mode files."
  2406. :tag "Org Export ASCII"
  2407. :group 'org-export)
  2408. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2409. "Characters for underlining headings in ASCII export.
  2410. In the given sequence, these characters will be used for level 1, 2, ..."
  2411. :group 'org-export-ascii
  2412. :type '(repeat character))
  2413. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2414. "Bullet characters for headlines converted to lists in ASCII export.
  2415. The first character is is used for the first lest level generated in this
  2416. way, and so on. If there are more levels than characters given here,
  2417. the list will be repeated.
  2418. Note that plain lists will keep the same bullets as the have in the
  2419. Org-mode file."
  2420. :group 'org-export-ascii
  2421. :type '(repeat character))
  2422. (defgroup org-export-xml nil
  2423. "Options specific for XML export of Org-mode files."
  2424. :tag "Org Export XML"
  2425. :group 'org-export)
  2426. (defgroup org-export-html nil
  2427. "Options specific for HTML export of Org-mode files."
  2428. :tag "Org Export HTML"
  2429. :group 'org-export)
  2430. (defcustom org-export-html-coding-system nil
  2431. ""
  2432. :group 'org-export-html
  2433. :type 'coding-system)
  2434. (defcustom org-export-html-style
  2435. "<style type=\"text/css\">
  2436. html {
  2437. font-family: Times, serif;
  2438. font-size: 12pt;
  2439. }
  2440. .title { text-align: center; }
  2441. .todo { color: red; }
  2442. .done { color: green; }
  2443. .timestamp { color: grey }
  2444. .timestamp-kwd { color: CadetBlue }
  2445. .tag { background-color:lightblue; font-weight:normal }
  2446. .target { background-color: lavender; }
  2447. pre {
  2448. border: 1pt solid #AEBDCC;
  2449. background-color: #F3F5F7;
  2450. padding: 5pt;
  2451. font-family: courier, monospace;
  2452. }
  2453. table { border-collapse: collapse; }
  2454. td, th {
  2455. vertical-align: top;
  2456. <!--border: 1pt solid #ADB9CC;-->
  2457. }
  2458. </style>"
  2459. "The default style specification for exported HTML files.
  2460. Since there are different ways of setting style information, this variable
  2461. needs to contain the full HTML structure to provide a style, including the
  2462. surrounding HTML tags. The style specifications should include definitions
  2463. for new classes todo, done, title, and deadline. For example, legal values
  2464. would be:
  2465. <style type=\"text/css\">
  2466. p { font-weight: normal; color: gray; }
  2467. h1 { color: black; }
  2468. .title { text-align: center; }
  2469. .todo, .deadline { color: red; }
  2470. .done { color: green; }
  2471. </style>
  2472. or, if you want to keep the style in a file,
  2473. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2474. As the value of this option simply gets inserted into the HTML <head> header,
  2475. you can \"misuse\" it to add arbitrary text to the header."
  2476. :group 'org-export-html
  2477. :type 'string)
  2478. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2479. "Format for typesetting the document title in HTML export."
  2480. :group 'org-export-html
  2481. :type 'string)
  2482. (defcustom org-export-html-toplevel-hlevel 2
  2483. "The <H> level for level 1 headings in HTML export."
  2484. :group 'org-export-html
  2485. :type 'string)
  2486. (defcustom org-export-html-link-org-files-as-html t
  2487. "Non-nil means, make file links to `file.org' point to `file.html'.
  2488. When org-mode is exporting an org-mode file to HTML, links to
  2489. non-html files are directly put into a href tag in HTML.
  2490. However, links to other Org-mode files (recognized by the
  2491. extension `.org.) should become links to the corresponding html
  2492. file, assuming that the linked org-mode file will also be
  2493. converted to HTML.
  2494. When nil, the links still point to the plain `.org' file."
  2495. :group 'org-export-html
  2496. :type 'boolean)
  2497. (defcustom org-export-html-inline-images 'maybe
  2498. "Non-nil means, inline images into exported HTML pages.
  2499. This is done using an <img> tag. When nil, an anchor with href is used to
  2500. link to the image. If this option is `maybe', then images in links with
  2501. an empty description will be inlined, while images with a description will
  2502. be linked only."
  2503. :group 'org-export-html
  2504. :type '(choice (const :tag "Never" nil)
  2505. (const :tag "Always" t)
  2506. (const :tag "When there is no description" maybe)))
  2507. ;; FIXME: rename
  2508. (defcustom org-export-html-expand t
  2509. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2510. When nil, these tags will be exported as plain text and therefore
  2511. not be interpreted by a browser.
  2512. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2513. :group 'org-export-html
  2514. :type 'boolean)
  2515. (defcustom org-export-html-table-tag
  2516. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2517. "The HTML tag that is used to start a table.
  2518. This must be a <table> tag, but you may change the options like
  2519. borders and spacing."
  2520. :group 'org-export-html
  2521. :type 'string)
  2522. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2523. "The opening tag for table header fields.
  2524. This is customizable so that alignment options can be specified."
  2525. :group 'org-export-tables
  2526. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2527. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2528. "The opening tag for table data fields.
  2529. This is customizable so that alignment options can be specified."
  2530. :group 'org-export-tables
  2531. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2532. (defcustom org-export-html-with-timestamp nil
  2533. "If non-nil, write `org-export-html-html-helper-timestamp'
  2534. into the exported HTML text. Otherwise, the buffer will just be saved
  2535. to a file."
  2536. :group 'org-export-html
  2537. :type 'boolean)
  2538. (defcustom org-export-html-html-helper-timestamp
  2539. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2540. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2541. :group 'org-export-html
  2542. :type 'string)
  2543. (defgroup org-export-icalendar nil
  2544. "Options specific for iCalendar export of Org-mode files."
  2545. :tag "Org Export iCalendar"
  2546. :group 'org-export)
  2547. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2548. "The file name for the iCalendar file covering all agenda files.
  2549. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2550. The file name should be absolute."
  2551. :group 'org-export-icalendar
  2552. :type 'file)
  2553. (defcustom org-icalendar-include-todo nil
  2554. "Non-nil means, export to iCalendar files should also cover TODO items."
  2555. :group 'org-export-icalendar
  2556. :type '(choice
  2557. (const :tag "None" nil)
  2558. (const :tag "Unfinished" t)
  2559. (const :tag "All" all)))
  2560. (defcustom org-icalendar-include-sexps t
  2561. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2562. These are entries like in the diary, but directly in an Org-mode file."
  2563. :group 'org-export-icalendar
  2564. :type 'boolean)
  2565. (defcustom org-icalendar-combined-name "OrgMode"
  2566. "Calendar name for the combined iCalendar representing all agenda files."
  2567. :group 'org-export-icalendar
  2568. :type 'string)
  2569. (defgroup org-font-lock nil
  2570. "Font-lock settings for highlighting in Org-mode."
  2571. :tag "Org Font Lock"
  2572. :group 'org)
  2573. (defcustom org-level-color-stars-only nil
  2574. "Non-nil means fontify only the stars in each headline.
  2575. When nil, the entire headline is fontified.
  2576. Changing it requires restart of `font-lock-mode' to become effective
  2577. also in regions already fontified."
  2578. :group 'org-font-lock
  2579. :type 'boolean)
  2580. (defcustom org-hide-leading-stars nil
  2581. "Non-nil means, hide the first N-1 stars in a headline.
  2582. This works by using the face `org-hide' for these stars. This
  2583. face is white for a light background, and black for a dark
  2584. background. You may have to customize the face `org-hide' to
  2585. make this work.
  2586. Changing it requires restart of `font-lock-mode' to become effective
  2587. also in regions already fontified.
  2588. You may also set this on a per-file basis by adding one of the following
  2589. lines to the buffer:
  2590. #+STARTUP: hidestars
  2591. #+STARTUP: showstars"
  2592. :group 'org-font-lock
  2593. :type 'boolean)
  2594. (defcustom org-fontify-done-headline nil
  2595. "Non-nil means, change the face of a headline if it is marked DONE.
  2596. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2597. When this is non-nil, the headline after the keyword is set to the
  2598. `org-headline-done' as an additional indication."
  2599. :group 'org-font-lock
  2600. :type 'boolean)
  2601. (defcustom org-fontify-emphasized-text t
  2602. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2603. Changing this variable requires a restart of Emacs to take effect."
  2604. :group 'org-font-lock
  2605. :type 'boolean)
  2606. (defvar org-emph-re nil
  2607. "Regular expression for matching emphasis.")
  2608. (defvar org-emphasis-regexp-components) ; defined just below
  2609. (defvar org-emphasis-alist) ; defined just below
  2610. (defun org-set-emph-re (var val)
  2611. "Set variable and compute the emphasis regular expression."
  2612. (set var val)
  2613. (when (and (boundp 'org-emphasis-alist)
  2614. (boundp 'org-emphasis-regexp-components)
  2615. org-emphasis-alist org-emphasis-regexp-components)
  2616. (let* ((e org-emphasis-regexp-components)
  2617. (pre (car e))
  2618. (post (nth 1 e))
  2619. (border (nth 2 e))
  2620. (body (nth 3 e))
  2621. (nl (nth 4 e))
  2622. (stacked (nth 5 e))
  2623. (body1 (concat body "*?"))
  2624. (markers (mapconcat 'car org-emphasis-alist "")))
  2625. ;; make sure special characters appear at the right position in the class
  2626. (if (string-match "\\^" markers)
  2627. (setq markers (concat (replace-match "" t t markers) "^")))
  2628. (if (string-match "-" markers)
  2629. (setq markers (concat (replace-match "" t t markers) "-")))
  2630. (if (> nl 0)
  2631. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2632. (int-to-string nl) "\\}")))
  2633. ;; Make the regexp
  2634. (setq org-emph-re
  2635. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2636. "\\("
  2637. "\\([" markers "]\\)"
  2638. "\\("
  2639. "[^" border (if (and nil stacked) markers) "]"
  2640. body1
  2641. "[^" border (if (and nil stacked) markers) "]"
  2642. "\\)"
  2643. "\\3\\)"
  2644. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2645. (defcustom org-emphasis-regexp-components
  2646. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2647. "Components used to build the reqular expression for emphasis.
  2648. This is a list with 6 entries. Terminology: In an emphasis string
  2649. like \" *strong word* \", we call the initial space PREMATCH, the final
  2650. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2651. and \"trong wor\" is the body. The different components in this variable
  2652. specify what is allowed/forbidden in each part:
  2653. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2654. post Chars allowed as postmatch. End of line will be allowed too.
  2655. border The chars *forbidden* as border characters.
  2656. body-regexp A regexp like \".\" to match a body character. Don't use
  2657. non-shy groups here, and don't allow newline here.
  2658. newline The maximum number of newlines allowed in an emphasis exp.
  2659. stacked Non-nil means, allow stacked styles. This works only in HTML
  2660. export. When this is set, all marker characters (as given in
  2661. `org-emphasis-alist') will be allowed as pre/post, aiding
  2662. inside-out matching.
  2663. Use customize to modify this, or restart Emacs after changing it."
  2664. :group 'org-font-lock
  2665. :set 'org-set-emph-re
  2666. :type '(list
  2667. (sexp :tag "Allowed chars in pre ")
  2668. (sexp :tag "Allowed chars in post ")
  2669. (sexp :tag "Forbidden chars in border ")
  2670. (sexp :tag "Regexp for body ")
  2671. (integer :tag "number of newlines allowed")
  2672. (boolean :tag "Stacking allowed ")))
  2673. (defcustom org-emphasis-alist
  2674. '(("*" bold "<b>" "</b>")
  2675. ("/" italic "<i>" "</i>")
  2676. ("_" underline "<u>" "</u>")
  2677. ("=" shadow "<code>" "</code>")
  2678. ("+" (:strike-through t) "<del>" "</del>")
  2679. )
  2680. "Special syntax for emphasized text.
  2681. Text starting and ending with a special character will be emphasized, for
  2682. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2683. characters, the face to be used by font-lock for highlighting in Org-mode
  2684. Emacs buffers, and the HTML tags to be used for this.
  2685. Use customize to modify this, or restart Emacs after changing it."
  2686. :group 'org-font-lock
  2687. :set 'org-set-emph-re
  2688. :type '(repeat
  2689. (list
  2690. (string :tag "Marker character")
  2691. (choice
  2692. (face :tag "Font-lock-face")
  2693. (plist :tag "Face property list"))
  2694. (string :tag "HTML start tag")
  2695. (string :tag "HTML end tag"))))
  2696. ;;; The faces
  2697. (defgroup org-faces nil
  2698. "Faces in Org-mode."
  2699. :tag "Org Faces"
  2700. :group 'org-font-lock)
  2701. ;; FIXME: convert that into a macro? Not critical, because this
  2702. ;; is only executed a few times at load time.
  2703. (defun org-compatible-face (specs)
  2704. "Make a compatible face specification.
  2705. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2706. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2707. to the top of the list. The `min-colors' attribute will be removed from
  2708. any other entries, and any resulting duplicates will be removed entirely."
  2709. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2710. (let (r e a)
  2711. (while (setq e (pop specs))
  2712. (cond
  2713. ((memq (car e) '(t default)) (push e r))
  2714. ((setq a (member '(min-colors 8) (car e)))
  2715. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2716. (cdr e)))))
  2717. ((setq a (assq 'min-colors (car e)))
  2718. (setq e (cons (delq a (car e)) (cdr e)))
  2719. (or (assoc (car e) r) (push e r)))
  2720. (t (or (assoc (car e) r) (push e r)))))
  2721. (nreverse r))
  2722. specs))
  2723. (defface org-hide
  2724. '((((background light)) (:foreground "white"))
  2725. (((background dark)) (:foreground "black")))
  2726. "Face used to hide leading stars in headlines.
  2727. The forground color of this face should be equal to the background
  2728. color of the frame."
  2729. :group 'org-faces)
  2730. (defface org-level-1 ;; font-lock-function-name-face
  2731. (org-compatible-face
  2732. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2733. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2734. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2735. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2736. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2737. (t (:bold t))))
  2738. "Face used for level 1 headlines."
  2739. :group 'org-faces)
  2740. (defface org-level-2 ;; font-lock-variable-name-face
  2741. (org-compatible-face
  2742. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2743. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2744. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2745. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2746. (t (:bold t))))
  2747. "Face used for level 2 headlines."
  2748. :group 'org-faces)
  2749. (defface org-level-3 ;; font-lock-keyword-face
  2750. (org-compatible-face
  2751. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2752. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2753. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2754. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2755. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2756. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2757. (t (:bold t))))
  2758. "Face used for level 3 headlines."
  2759. :group 'org-faces)
  2760. (defface org-level-4 ;; font-lock-comment-face
  2761. (org-compatible-face
  2762. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2763. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2764. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2765. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2766. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2767. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2768. (t (:bold t))))
  2769. "Face used for level 4 headlines."
  2770. :group 'org-faces)
  2771. (defface org-level-5 ;; font-lock-type-face
  2772. (org-compatible-face
  2773. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2774. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2775. (((class color) (min-colors 8)) (:foreground "green"))))
  2776. "Face used for level 5 headlines."
  2777. :group 'org-faces)
  2778. (defface org-level-6 ;; font-lock-constant-face
  2779. (org-compatible-face
  2780. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2781. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2782. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2783. "Face used for level 6 headlines."
  2784. :group 'org-faces)
  2785. (defface org-level-7 ;; font-lock-builtin-face
  2786. (org-compatible-face
  2787. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2788. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2789. (((class color) (min-colors 8)) (:foreground "blue"))))
  2790. "Face used for level 7 headlines."
  2791. :group 'org-faces)
  2792. (defface org-level-8 ;; font-lock-string-face
  2793. (org-compatible-face
  2794. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2795. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2796. (((class color) (min-colors 8)) (:foreground "green"))))
  2797. "Face used for level 8 headlines."
  2798. :group 'org-faces)
  2799. (defface org-special-keyword ;; font-lock-string-face
  2800. (org-compatible-face
  2801. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2802. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2803. (t (:italic t))))
  2804. "Face used for special keywords."
  2805. :group 'org-faces)
  2806. (defface org-drawer ;; font-lock-function-name-face
  2807. (org-compatible-face
  2808. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2809. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2810. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2811. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2812. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2813. (t (:bold t))))
  2814. "Face used for drawers."
  2815. :group 'org-faces)
  2816. (defface org-property-value nil
  2817. "Face used for the value of a property."
  2818. :group 'org-faces)
  2819. (defface org-column
  2820. (org-compatible-face
  2821. '((((class color) (min-colors 16) (background light))
  2822. (:background "grey90"))
  2823. (((class color) (min-colors 16) (background dark))
  2824. (:background "grey30"))
  2825. (((class color) (min-colors 8))
  2826. (:background "cyan" :foreground "black"))
  2827. (t (:inverse-video t))))
  2828. "Face for column display of entry properties."
  2829. :group 'org-faces)
  2830. (when (fboundp 'set-face-attribute)
  2831. ;; Make sure that a fixed-width face is used when we have a column table.
  2832. (set-face-attribute 'org-column nil
  2833. :height (face-attribute 'default :height)
  2834. :family (face-attribute 'default :family)))
  2835. (defface org-warning ;; font-lock-warning-face
  2836. (org-compatible-face
  2837. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2838. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2839. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2840. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2841. (t (:bold t))))
  2842. "Face for deadlines and TODO keywords."
  2843. :group 'org-faces)
  2844. (defface org-archived ; similar to shadow
  2845. (org-compatible-face
  2846. '((((class color grayscale) (min-colors 88) (background light))
  2847. (:foreground "grey50"))
  2848. (((class color grayscale) (min-colors 88) (background dark))
  2849. (:foreground "grey70"))
  2850. (((class color) (min-colors 8) (background light))
  2851. (:foreground "green"))
  2852. (((class color) (min-colors 8) (background dark))
  2853. (:foreground "yellow"))))
  2854. "Face for headline with the ARCHIVE tag."
  2855. :group 'org-faces)
  2856. (defface org-link
  2857. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2858. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2859. (t (:underline t)))
  2860. "Face for links."
  2861. :group 'org-faces)
  2862. (defface org-target
  2863. '((((class color) (background light)) (:underline t))
  2864. (((class color) (background dark)) (:underline t))
  2865. (t (:underline t)))
  2866. "Face for links."
  2867. :group 'org-faces)
  2868. (defface org-date
  2869. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2870. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2871. (t (:underline t)))
  2872. "Face for links."
  2873. :group 'org-faces)
  2874. (defface org-sexp-date
  2875. '((((class color) (background light)) (:foreground "Purple"))
  2876. (((class color) (background dark)) (:foreground "Cyan"))
  2877. (t (:underline t)))
  2878. "Face for links."
  2879. :group 'org-faces)
  2880. (defface org-tag
  2881. '((t (:bold t)))
  2882. "Face for tags."
  2883. :group 'org-faces)
  2884. (defface org-todo ;; font-lock-warning-face
  2885. (org-compatible-face
  2886. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2887. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2888. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2889. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2890. (t (:inverse-video t :bold t))))
  2891. "Face for TODO keywords."
  2892. :group 'org-faces)
  2893. (defface org-done ;; font-lock-type-face
  2894. (org-compatible-face
  2895. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2896. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2897. (((class color) (min-colors 8)) (:foreground "green"))
  2898. (t (:bold t))))
  2899. "Face used for todo keywords that indicate DONE items."
  2900. :group 'org-faces)
  2901. (defface org-headline-done ;; font-lock-string-face
  2902. (org-compatible-face
  2903. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2904. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2905. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2906. "Face used to indicate that a headline is DONE.
  2907. This face is only used if `org-fontify-done-headline' is set. If applies
  2908. to the part of the headline after the DONE keyword."
  2909. :group 'org-faces)
  2910. (defface org-table ;; font-lock-function-name-face
  2911. (org-compatible-face
  2912. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2913. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2914. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2915. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2916. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2917. (((class color) (min-colors 8) (background dark)))))
  2918. "Face used for tables."
  2919. :group 'org-faces)
  2920. (defface org-formula
  2921. (org-compatible-face
  2922. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2923. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2924. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2925. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2926. (t (:bold t :italic t))))
  2927. "Face for formulas."
  2928. :group 'org-faces)
  2929. (defface org-agenda-structure ;; font-lock-function-name-face
  2930. (org-compatible-face
  2931. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2932. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2933. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2934. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2935. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2936. (t (:bold t))))
  2937. "Face used in agenda for captions and dates."
  2938. :group 'org-faces)
  2939. (defface org-scheduled-today
  2940. (org-compatible-face
  2941. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2942. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2943. (((class color) (min-colors 8)) (:foreground "green"))
  2944. (t (:bold t :italic t))))
  2945. "Face for items scheduled for a certain day."
  2946. :group 'org-faces)
  2947. (defface org-scheduled-previously
  2948. (org-compatible-face
  2949. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2950. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2951. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2952. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2953. (t (:bold t))))
  2954. "Face for items scheduled previously, and not yet done."
  2955. :group 'org-faces)
  2956. (defface org-upcoming-deadline
  2957. (org-compatible-face
  2958. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2959. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2960. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2961. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2962. (t (:bold t))))
  2963. "Face for items scheduled previously, and not yet done."
  2964. :group 'org-faces)
  2965. (defface org-time-grid ;; font-lock-variable-name-face
  2966. (org-compatible-face
  2967. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2968. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2969. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2970. "Face used for time grids."
  2971. :group 'org-faces)
  2972. (defconst org-level-faces
  2973. '(org-level-1 org-level-2 org-level-3 org-level-4
  2974. org-level-5 org-level-6 org-level-7 org-level-8
  2975. ))
  2976. (defconst org-n-levels (length org-level-faces))
  2977. ;;; Variables for pre-computed regular expressions, all buffer local
  2978. (defvar org-drawer-regexp nil
  2979. "Matches first line of a hidden block.")
  2980. (make-variable-buffer-local 'org-drawer-regexp)
  2981. (defvar org-todo-regexp nil
  2982. "Matches any of the TODO state keywords.")
  2983. (make-variable-buffer-local 'org-todo-regexp)
  2984. (defvar org-not-done-regexp nil
  2985. "Matches any of the TODO state keywords except the last one.")
  2986. (make-variable-buffer-local 'org-not-done-regexp)
  2987. (defvar org-todo-line-regexp nil
  2988. "Matches a headline and puts TODO state into group 2 if present.")
  2989. (make-variable-buffer-local 'org-todo-line-regexp)
  2990. (defvar org-todo-line-tags-regexp nil
  2991. "Matches a headline and puts TODO state into group 2 if present.
  2992. Also put tags into group 4 if tags are present.")
  2993. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2994. (defvar org-nl-done-regexp nil
  2995. "Matches newline followed by a headline with the DONE keyword.")
  2996. (make-variable-buffer-local 'org-nl-done-regexp)
  2997. (defvar org-looking-at-done-regexp nil
  2998. "Matches the DONE keyword a point.")
  2999. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3000. (defvar org-ds-keyword-length 12
  3001. "Maximum length of the Deadline and SCHEDULED keywords.")
  3002. (make-variable-buffer-local 'org-ds-keyword-length)
  3003. (defvar org-deadline-regexp nil
  3004. "Matches the DEADLINE keyword.")
  3005. (make-variable-buffer-local 'org-deadline-regexp)
  3006. (defvar org-deadline-time-regexp nil
  3007. "Matches the DEADLINE keyword together with a time stamp.")
  3008. (make-variable-buffer-local 'org-deadline-time-regexp)
  3009. (defvar org-deadline-line-regexp nil
  3010. "Matches the DEADLINE keyword and the rest of the line.")
  3011. (make-variable-buffer-local 'org-deadline-line-regexp)
  3012. (defvar org-scheduled-regexp nil
  3013. "Matches the SCHEDULED keyword.")
  3014. (make-variable-buffer-local 'org-scheduled-regexp)
  3015. (defvar org-scheduled-time-regexp nil
  3016. "Matches the SCHEDULED keyword together with a time stamp.")
  3017. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3018. (defvar org-closed-time-regexp nil
  3019. "Matches the CLOSED keyword together with a time stamp.")
  3020. (make-variable-buffer-local 'org-closed-time-regexp)
  3021. (defvar org-keyword-time-regexp nil
  3022. "Matches any of the 4 keywords, together with the time stamp.")
  3023. (make-variable-buffer-local 'org-keyword-time-regexp)
  3024. (defvar org-keyword-time-not-clock-regexp nil
  3025. "Matches any of the 3 keywords, together with the time stamp.")
  3026. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3027. (defvar org-maybe-keyword-time-regexp nil
  3028. "Matches a timestamp, possibly preceeded by a keyword.")
  3029. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3030. (defvar org-planning-or-clock-line-re nil
  3031. "Matches a line with planning or clock info.")
  3032. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3033. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3034. rear-nonsticky t mouse-map t fontified t)
  3035. "Properties to remove when a string without properties is wanted.")
  3036. (defsubst org-match-string-no-properties (num &optional string)
  3037. (if (featurep 'xemacs)
  3038. (let ((s (match-string num string)))
  3039. (remove-text-properties 0 (length s) org-rm-props s)
  3040. s)
  3041. (match-string-no-properties num string)))
  3042. (defsubst org-no-properties (s)
  3043. (if (fboundp 'set-text-properties)
  3044. (set-text-properties 0 (length s) nil s)
  3045. (remove-text-properties 0 (length s) org-rm-props s))
  3046. s)
  3047. (defsubst org-get-alist-option (option key)
  3048. (cond ((eq key t) t)
  3049. ((eq option t) t)
  3050. ((assoc key option) (cdr (assoc key option)))
  3051. (t (cdr (assq 'default option)))))
  3052. (defsubst org-inhibit-invisibility ()
  3053. "Modified `buffer-invisibility-spec' for Emacs 21.
  3054. Some ops with invisible text do not work correctly on Emacs 21. For these
  3055. we turn off invisibility temporarily. Use this in a `let' form."
  3056. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3057. (defsubst org-set-local (var value)
  3058. "Make VAR local in current buffer and set it to VALUE."
  3059. (set (make-variable-buffer-local var) value))
  3060. (defsubst org-mode-p ()
  3061. "Check if the current buffer is in Org-mode."
  3062. (eq major-mode 'org-mode))
  3063. (defsubst org-last (list)
  3064. "Return the last element of LIST."
  3065. (car (last list)))
  3066. (defun org-let (list &rest body)
  3067. (eval (cons 'let (cons list body))))
  3068. (put 'org-let 'lisp-indent-function 1)
  3069. (defun org-let2 (list1 list2 &rest body)
  3070. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3071. (put 'org-let2 'lisp-indent-function 2)
  3072. (defconst org-startup-options
  3073. '(("fold" org-startup-folded t)
  3074. ("overview" org-startup-folded t)
  3075. ("nofold" org-startup-folded nil)
  3076. ("showall" org-startup-folded nil)
  3077. ("content" org-startup-folded content)
  3078. ("hidestars" org-hide-leading-stars t)
  3079. ("showstars" org-hide-leading-stars nil)
  3080. ("odd" org-odd-levels-only t)
  3081. ("oddeven" org-odd-levels-only nil)
  3082. ("align" org-startup-align-all-tables t)
  3083. ("noalign" org-startup-align-all-tables nil)
  3084. ("customtime" org-display-custom-times t)
  3085. ("logging" org-log-done t)
  3086. ("logdone" org-log-done t)
  3087. ("nologging" org-log-done nil)
  3088. ("lognotedone" org-log-done done push)
  3089. ("lognotestate" org-log-done state push)
  3090. ("lognoteclock-out" org-log-done clock-out push)
  3091. ("logrepeat" org-log-repeat t)
  3092. ("nologrepeat" org-log-repeat nil)
  3093. ("constcgs" constants-unit-system cgs)
  3094. ("constSI" constants-unit-system SI))
  3095. "Variable associated with STARTUP options for org-mode.
  3096. Each element is a list of three items: The startup options as written
  3097. in the #+STARTUP line, the corresponding variable, and the value to
  3098. set this variable to if the option is found. An optional forth element PUSH
  3099. means to push this value onto the list in the variable.")
  3100. (defun org-set-regexps-and-options ()
  3101. "Precompute regular expressions for current buffer."
  3102. (when (org-mode-p)
  3103. (org-set-local 'org-todo-kwd-alist nil)
  3104. (org-set-local 'org-todo-keywords-1 nil)
  3105. (org-set-local 'org-done-keywords nil)
  3106. (org-set-local 'org-todo-heads nil)
  3107. (org-set-local 'org-todo-sets nil)
  3108. (let ((re (org-make-options-regexp
  3109. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO" "COLUMNS"
  3110. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3111. "CONSTANTS")))
  3112. (splitre "[ \t]+")
  3113. kwds key value cat arch tags const links hw dws tail sep kws1 prio)
  3114. (save-excursion
  3115. (save-restriction
  3116. (widen)
  3117. (goto-char (point-min))
  3118. (while (re-search-forward re nil t)
  3119. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3120. (cond
  3121. ((equal key "CATEGORY")
  3122. (if (string-match "[ \t]+$" value)
  3123. (setq value (replace-match "" t t value)))
  3124. (setq cat (intern value)))
  3125. ((equal key "SEQ_TODO")
  3126. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3127. ((equal key "TYP_TODO")
  3128. (push (cons 'type (org-split-string value splitre)) kwds))
  3129. ((equal key "TAGS")
  3130. (setq tags (append tags (org-split-string value splitre))))
  3131. ((equal key "COLUMNS")
  3132. (org-set-local 'org-columns-default-format value))
  3133. ((equal key "LINK")
  3134. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3135. (push (cons (match-string 1 value)
  3136. (org-trim (match-string 2 value)))
  3137. links)))
  3138. ((equal key "PRIORITIES")
  3139. (setq prio (org-split-string value " +")))
  3140. ((equal key "CONSTANTS")
  3141. (setq const (append const (org-split-string value splitre))))
  3142. ((equal key "STARTUP")
  3143. (let ((opts (org-split-string value splitre))
  3144. l var val)
  3145. (while (setq l (pop opts))
  3146. (when (setq l (assoc l org-startup-options))
  3147. (setq var (nth 1 l) val (nth 2 l))
  3148. (if (not (nth 3 l))
  3149. (set (make-local-variable var) val)
  3150. (if (not (listp (symbol-value var)))
  3151. (set (make-local-variable var) nil))
  3152. (set (make-local-variable var) (symbol-value var))
  3153. (add-to-list var val))))))
  3154. ((equal key "ARCHIVE")
  3155. (string-match " *$" value)
  3156. (setq arch (replace-match "" t t value))
  3157. (remove-text-properties 0 (length arch)
  3158. '(face t fontified t) arch)))
  3159. )))
  3160. (and cat (org-set-local 'org-category cat))
  3161. (when prio
  3162. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3163. (setq prio (mapcar 'string-to-char prio))
  3164. (org-set-local 'org-highest-priority (nth 0 prio))
  3165. (org-set-local 'org-lowest-priority (nth 1 prio))
  3166. (org-set-local 'org-default-priority (nth 2 prio)))
  3167. (and arch (org-set-local 'org-archive-location arch))
  3168. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3169. ;; Process the TODO keywords
  3170. (unless kwds
  3171. ;; Use the global values as if they had been given locally.
  3172. (setq kwds (default-value 'org-todo-keywords))
  3173. (if (stringp (car kwds))
  3174. (setq kwds (list (cons org-todo-interpretation
  3175. (default-value 'org-todo-keywords)))))
  3176. (setq kwds (reverse kwds)))
  3177. (setq kwds (nreverse kwds))
  3178. (let (inter kws)
  3179. (while (setq kws (pop kwds))
  3180. (setq inter (pop kws) sep (member "|" kws)
  3181. kws1 (delete "|" (copy-sequence kws))
  3182. hw (car kws1)
  3183. dws (if sep (cdr sep) (last kws1))
  3184. tail (list inter hw (car dws) (org-last dws)))
  3185. (add-to-list 'org-todo-heads hw 'append)
  3186. (push kws1 org-todo-sets)
  3187. (setq org-done-keywords (append org-done-keywords dws nil))
  3188. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3189. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3190. (setq org-todo-sets (nreverse org-todo-sets)
  3191. org-todo-kwd-alist (nreverse org-todo-kwd-alist)))
  3192. ;; Process the constants
  3193. (when const
  3194. (let (e cst)
  3195. (while (setq e (pop const))
  3196. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3197. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3198. (setq org-table-formula-constants-local cst)))
  3199. ;; Process the tags.
  3200. (when tags
  3201. (let (e tgs)
  3202. (while (setq e (pop tags))
  3203. (cond
  3204. ((equal e "{") (push '(:startgroup) tgs))
  3205. ((equal e "}") (push '(:endgroup) tgs))
  3206. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3207. (push (cons (match-string 1 e)
  3208. (string-to-char (match-string 2 e)))
  3209. tgs))
  3210. (t (push (list e) tgs))))
  3211. (org-set-local 'org-tag-alist nil)
  3212. (while (setq e (pop tgs))
  3213. (or (and (stringp (car e))
  3214. (assoc (car e) org-tag-alist))
  3215. (push e org-tag-alist))))))
  3216. ;; Compute the regular expressions and other local variables
  3217. (if (not org-done-keywords)
  3218. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3219. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3220. (length org-scheduled-string)))
  3221. org-drawer-regexp
  3222. (concat "^[ \t]*:\\("
  3223. (mapconcat 'regexp-quote org-drawers "\\|")
  3224. "\\):[ \t]*$")
  3225. org-not-done-keywords
  3226. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3227. org-todo-regexp
  3228. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3229. "\\|") "\\)\\>")
  3230. org-not-done-regexp
  3231. (concat "\\<\\("
  3232. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3233. "\\)\\>")
  3234. org-todo-line-regexp
  3235. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3236. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3237. "\\)\\>\\)? *\\(.*\\)")
  3238. org-nl-done-regexp
  3239. (concat "\n\\*+[ \t]+"
  3240. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3241. "\\)" "\\>")
  3242. org-todo-line-tags-regexp
  3243. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3244. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3245. (org-re
  3246. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3247. org-looking-at-done-regexp
  3248. (concat "^" "\\(?:"
  3249. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3250. "\\>")
  3251. org-deadline-regexp (concat "\\<" org-deadline-string)
  3252. org-deadline-time-regexp
  3253. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3254. org-deadline-line-regexp
  3255. (concat "\\<\\(" org-deadline-string "\\).*")
  3256. org-scheduled-regexp
  3257. (concat "\\<" org-scheduled-string)
  3258. org-scheduled-time-regexp
  3259. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3260. org-closed-time-regexp
  3261. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3262. org-keyword-time-regexp
  3263. (concat "\\<\\(" org-scheduled-string
  3264. "\\|" org-deadline-string
  3265. "\\|" org-closed-string
  3266. "\\|" org-archived-string
  3267. "\\|" org-clock-string "\\)"
  3268. " *[[<]\\([^]>]+\\)[]>]")
  3269. org-keyword-time-not-clock-regexp
  3270. (concat "\\<\\(" org-scheduled-string
  3271. "\\|" org-deadline-string
  3272. "\\|" org-closed-string
  3273. "\\|" org-archived-string
  3274. "\\)"
  3275. " *[[<]\\([^]>]+\\)[]>]")
  3276. org-maybe-keyword-time-regexp
  3277. (concat "\\(\\<\\(" org-scheduled-string
  3278. "\\|" org-deadline-string
  3279. "\\|" org-closed-string
  3280. "\\|" org-archived-string
  3281. "\\|" org-clock-string "\\)\\)?"
  3282. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3283. org-planning-or-clock-line-re
  3284. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3285. "\\|" org-deadline-string
  3286. "\\|" org-closed-string "\\|" org-clock-string
  3287. "\\|" org-archived-string "\\)\\>\\)")
  3288. )
  3289. (org-set-font-lock-defaults)))
  3290. ;;; Some variables ujsed in various places
  3291. (defvar org-window-configuration nil
  3292. "Used in various places to store a window configuration.")
  3293. (defvar org-finish-function nil
  3294. "Function to be called when `C-c C-c' is used.
  3295. This is for getting out of special buffers like remember.")
  3296. ;;; Foreign variables, to inform the compiler
  3297. ;; XEmacs only
  3298. (defvar outline-mode-menu-heading)
  3299. (defvar outline-mode-menu-show)
  3300. (defvar outline-mode-menu-hide)
  3301. (defvar zmacs-regions) ; XEmacs regions
  3302. ;; Emacs only
  3303. (defvar mark-active)
  3304. ;; Packages that org-mode interacts with
  3305. (defvar calc-embedded-close-formula)
  3306. (defvar calc-embedded-open-formula)
  3307. (defvar font-lock-unfontify-region-function)
  3308. (defvar org-goto-start-pos)
  3309. (defvar vm-message-pointer)
  3310. (defvar vm-folder-directory)
  3311. (defvar wl-summary-buffer-elmo-folder)
  3312. (defvar wl-summary-buffer-folder-name)
  3313. (defvar gnus-other-frame-object)
  3314. (defvar gnus-group-name)
  3315. (defvar gnus-article-current)
  3316. (defvar w3m-current-url)
  3317. (defvar w3m-current-title)
  3318. (defvar mh-progs)
  3319. (defvar mh-current-folder)
  3320. (defvar mh-show-folder-buffer)
  3321. (defvar mh-index-folder)
  3322. (defvar mh-searcher)
  3323. (defvar calendar-mode-map)
  3324. (defvar Info-current-file)
  3325. (defvar Info-current-node)
  3326. (defvar texmathp-why)
  3327. (defvar remember-save-after-remembering)
  3328. (defvar remember-data-file)
  3329. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3330. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3331. (defvar org-latex-regexps)
  3332. (defvar constants-unit-system)
  3333. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3334. ;; FIXME: Occasionally check by commenting these, to make sure
  3335. ;; no other functions uses these, forgetting to let-bind them.
  3336. (defvar entry)
  3337. (defvar state)
  3338. (defvar last-state)
  3339. (defvar date)
  3340. (defvar description)
  3341. ;; Defined somewhere in this file, but used before definition.
  3342. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3343. (defvar org-agenda-undo-list)
  3344. (defvar org-agenda-pending-undo-list)
  3345. (defvar org-agenda-overriding-header)
  3346. (defvar orgtbl-mode)
  3347. (defvar org-html-entities)
  3348. (defvar org-struct-menu)
  3349. (defvar org-org-menu)
  3350. (defvar org-tbl-menu)
  3351. (defvar org-agenda-keymap)
  3352. (defvar org-category-table)
  3353. ;;;; Emacs/XEmacs compatibility
  3354. ;; Overlay compatibility functions
  3355. (defun org-make-overlay (beg end &optional buffer)
  3356. (if (featurep 'xemacs)
  3357. (make-extent beg end buffer)
  3358. (make-overlay beg end buffer)))
  3359. (defun org-delete-overlay (ovl)
  3360. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3361. (defun org-detach-overlay (ovl)
  3362. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3363. (defun org-move-overlay (ovl beg end &optional buffer)
  3364. (if (featurep 'xemacs)
  3365. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3366. (move-overlay ovl beg end buffer)))
  3367. (defun org-overlay-put (ovl prop value)
  3368. (if (featurep 'xemacs)
  3369. (set-extent-property ovl prop value)
  3370. (overlay-put ovl prop value)))
  3371. (defun org-overlay-display (ovl text &optional face evap)
  3372. "Make overlay OVL display TEXT with face FACE."
  3373. (if (featurep 'xemacs)
  3374. (let ((gl (make-glyph text)))
  3375. (and face (set-glyph-face gl face))
  3376. (set-extent-property ovl 'invisible t)
  3377. (set-extent-property ovl 'end-glyph gl))
  3378. (overlay-put ovl 'display text)
  3379. (if face (overlay-put ovl 'face face))
  3380. (if evap (overlay-put ovl 'evaporate t))))
  3381. (defun org-overlay-before-string (ovl text &optional face evap)
  3382. "Make overlay OVL display TEXT with face FACE."
  3383. (if (featurep 'xemacs)
  3384. (let ((gl (make-glyph text)))
  3385. (and face (set-glyph-face gl face))
  3386. (set-extent-property ovl 'begin-glyph gl))
  3387. (if face (org-add-props text nil 'face face))
  3388. (overlay-put ovl 'before-string text)
  3389. (if evap (overlay-put ovl 'evaporate t))))
  3390. (defun org-overlay-get (ovl prop)
  3391. (if (featurep 'xemacs)
  3392. (extent-property ovl prop)
  3393. (overlay-get ovl prop)))
  3394. (defun org-overlays-at (pos)
  3395. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3396. ;; FIXME: this is currently not used
  3397. (defun org-overlays-in (&optional start end)
  3398. (if (featurep 'xemacs)
  3399. (extent-list nil start end)
  3400. (overlays-in start end)))
  3401. (defun org-overlay-start (o)
  3402. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3403. (defun org-overlay-end (o)
  3404. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3405. ;; FIXME: this is currently not used
  3406. (defun org-find-overlays (prop &optional pos delete)
  3407. "Find all overlays specifying PROP at POS or point.
  3408. If DELETE is non-nil, delete all those overlays."
  3409. (let ((overlays (org-overlays-at (or pos (point))))
  3410. ov found)
  3411. (while (setq ov (pop overlays))
  3412. (if (org-overlay-get ov prop)
  3413. (if delete (org-delete-overlay ov) (push ov found))))
  3414. found))
  3415. ;; Region compatibility
  3416. (defun org-add-hook (hook function &optional append local)
  3417. "Add-hook, compatible with both Emacsen."
  3418. (if (and local (featurep 'xemacs))
  3419. (add-local-hook hook function append)
  3420. (add-hook hook function append local)))
  3421. (defvar org-ignore-region nil
  3422. "To temporarily disable the active region.")
  3423. (defun org-region-active-p ()
  3424. "Is `transient-mark-mode' on and the region active?
  3425. Works on both Emacs and XEmacs."
  3426. (if org-ignore-region
  3427. nil
  3428. (if (featurep 'xemacs)
  3429. (and zmacs-regions (region-active-p))
  3430. (and transient-mark-mode mark-active))))
  3431. ;; Invisibility compatibility
  3432. (defun org-add-to-invisibility-spec (arg)
  3433. "Add elements to `buffer-invisibility-spec'.
  3434. See documentation for `buffer-invisibility-spec' for the kind of elements
  3435. that can be added."
  3436. (cond
  3437. ((fboundp 'add-to-invisibility-spec)
  3438. (add-to-invisibility-spec arg))
  3439. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3440. (setq buffer-invisibility-spec (list arg)))
  3441. (t
  3442. (setq buffer-invisibility-spec
  3443. (cons arg buffer-invisibility-spec)))))
  3444. (defun org-remove-from-invisibility-spec (arg)
  3445. "Remove elements from `buffer-invisibility-spec'."
  3446. (if (fboundp 'remove-from-invisibility-spec)
  3447. (remove-from-invisibility-spec arg)
  3448. (if (consp buffer-invisibility-spec)
  3449. (setq buffer-invisibility-spec
  3450. (delete arg buffer-invisibility-spec)))))
  3451. ;; FIXME: this is currently not used
  3452. (defun org-in-invisibility-spec-p (arg)
  3453. "Is ARG a member of `buffer-invisibility-spec'?"
  3454. (if (consp buffer-invisibility-spec)
  3455. (member arg buffer-invisibility-spec)
  3456. nil))
  3457. ;;;; Define the Org-mode
  3458. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3459. (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."))
  3460. ;; We use a before-change function to check if a table might need
  3461. ;; an update.
  3462. (defvar org-table-may-need-update t
  3463. "Indicates that a table might need an update.
  3464. This variable is set by `org-before-change-function'.
  3465. `org-table-align' sets it back to nil.")
  3466. (defvar org-mode-map)
  3467. (defvar org-mode-hook nil)
  3468. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3469. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3470. (defvar org-table-buffer-is-an nil)
  3471. ;;;###autoload
  3472. (define-derived-mode org-mode outline-mode "Org"
  3473. "Outline-based notes management and organizer, alias
  3474. \"Carsten's outline-mode for keeping track of everything.\"
  3475. Org-mode develops organizational tasks around a NOTES file which
  3476. contains information about projects as plain text. Org-mode is
  3477. implemented on top of outline-mode, which is ideal to keep the content
  3478. of large files well structured. It supports ToDo items, deadlines and
  3479. time stamps, which magically appear in the diary listing of the Emacs
  3480. calendar. Tables are easily created with a built-in table editor.
  3481. Plain text URL-like links connect to websites, emails (VM), Usenet
  3482. messages (Gnus), BBDB entries, and any files related to the project.
  3483. For printing and sharing of notes, an Org-mode file (or a part of it)
  3484. can be exported as a structured ASCII or HTML file.
  3485. The following commands are available:
  3486. \\{org-mode-map}"
  3487. ;; Get rid of Outline menus, they are not needed
  3488. ;; Need to do this here because define-derived-mode sets up
  3489. ;; the keymap so late. Still, it is a waste to call this each time
  3490. ;; we switch another buffer into org-mode.
  3491. (if (featurep 'xemacs)
  3492. (when (boundp 'outline-mode-menu-heading)
  3493. ;; Assume this is Greg's port, it used easymenu
  3494. (easy-menu-remove outline-mode-menu-heading)
  3495. (easy-menu-remove outline-mode-menu-show)
  3496. (easy-menu-remove outline-mode-menu-hide))
  3497. (define-key org-mode-map [menu-bar headings] 'undefined)
  3498. (define-key org-mode-map [menu-bar hide] 'undefined)
  3499. (define-key org-mode-map [menu-bar show] 'undefined))
  3500. (easy-menu-add org-org-menu)
  3501. (easy-menu-add org-tbl-menu)
  3502. (org-install-agenda-files-menu)
  3503. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3504. (org-add-to-invisibility-spec '(org-cwidth))
  3505. (when (featurep 'xemacs)
  3506. (org-set-local 'line-move-ignore-invisible t))
  3507. (setq outline-regexp "\\*+ ")
  3508. (setq outline-level 'org-outline-level)
  3509. (when (and org-ellipsis (stringp org-ellipsis)
  3510. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table))
  3511. (unless org-display-table
  3512. (setq org-display-table (make-display-table)))
  3513. (set-display-table-slot org-display-table
  3514. 4 (string-to-vector org-ellipsis))
  3515. (setq buffer-display-table org-display-table))
  3516. (org-set-regexps-and-options)
  3517. ;; Calc embedded
  3518. (org-set-local 'calc-embedded-open-mode "# ")
  3519. (modify-syntax-entry ?# "<")
  3520. (modify-syntax-entry ?@ "w")
  3521. (if org-startup-truncated (setq truncate-lines t))
  3522. (org-set-local 'font-lock-unfontify-region-function
  3523. 'org-unfontify-region)
  3524. ;; Activate before-change-function
  3525. (org-set-local 'org-table-may-need-update t)
  3526. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3527. 'local)
  3528. ;; Check for running clock before killing a buffer
  3529. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3530. ;; Paragraphs and auto-filling
  3531. (org-set-autofill-regexps)
  3532. (setq indent-line-function 'org-indent-line-function)
  3533. (org-update-radio-target-regexp)
  3534. ;; Comment characters
  3535. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3536. (org-set-local 'comment-padding " ")
  3537. ;; Make isearch reveal context
  3538. (if (or (featurep 'xemacs)
  3539. (not (boundp 'outline-isearch-open-invisible-function)))
  3540. ;; Emacs 21 and XEmacs make use of the hook
  3541. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3542. ;; Emacs 22 deals with this through a special variable
  3543. (org-set-local 'outline-isearch-open-invisible-function
  3544. (lambda (&rest ignore) (org-show-context 'isearch))))
  3545. ;; If empty file that did not turn on org-mode automatically, make it to.
  3546. (if (and org-insert-mode-line-in-empty-file
  3547. (interactive-p)
  3548. (= (point-min) (point-max)))
  3549. (insert "# -*- mode: org -*-\n\n"))
  3550. (unless org-inhibit-startup
  3551. (when org-startup-align-all-tables
  3552. (let ((bmp (buffer-modified-p)))
  3553. (org-table-map-tables 'org-table-align)
  3554. (set-buffer-modified-p bmp)))
  3555. (cond
  3556. ((eq org-startup-folded t)
  3557. (org-cycle '(4)))
  3558. ((eq org-startup-folded 'content)
  3559. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3560. (org-cycle '(4)) (org-cycle '(4)))))))
  3561. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3562. (defsubst org-call-with-arg (command arg)
  3563. "Call COMMAND interactively, but pretend prefix are was ARG."
  3564. (let ((current-prefix-arg arg)) (call-interactively command)))
  3565. (defsubst org-current-line (&optional pos)
  3566. (save-excursion
  3567. (and pos (goto-char pos))
  3568. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3569. (defun org-current-time ()
  3570. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3571. (if (> org-time-stamp-rounding-minutes 0)
  3572. (let ((r org-time-stamp-rounding-minutes)
  3573. (time (decode-time)))
  3574. (apply 'encode-time
  3575. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3576. (nthcdr 2 time))))
  3577. (current-time)))
  3578. (defun org-add-props (string plist &rest props)
  3579. "Add text properties to entire string, from beginning to end.
  3580. PLIST may be a list of properties, PROPS are individual properties and values
  3581. that will be added to PLIST. Returns the string that was modified."
  3582. (add-text-properties
  3583. 0 (length string) (if props (append plist props) plist) string)
  3584. string)
  3585. (put 'org-add-props 'lisp-indent-function 2)
  3586. ;;;; Font-Lock stuff, including the activators
  3587. (defvar org-mouse-map (make-sparse-keymap))
  3588. (org-defkey org-mouse-map
  3589. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3590. (org-defkey org-mouse-map
  3591. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3592. (when org-mouse-1-follows-link
  3593. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3594. (when org-tab-follows-link
  3595. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3596. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3597. (when org-return-follows-link
  3598. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3599. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3600. (require 'font-lock)
  3601. (defconst org-non-link-chars "]\t\n\r<>")
  3602. (defconst org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3603. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3604. (defconst org-link-re-with-space
  3605. (concat
  3606. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3607. "\\([^" org-non-link-chars " ]"
  3608. "[^" org-non-link-chars "]*"
  3609. "[^" org-non-link-chars " ]\\)>?")
  3610. "Matches a link with spaces, optional angular brackets around it.")
  3611. (defconst org-link-re-with-space2
  3612. (concat
  3613. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3614. "\\([^" org-non-link-chars " ]"
  3615. "[^]\t\n\r]*"
  3616. "[^" org-non-link-chars " ]\\)>?")
  3617. "Matches a link with spaces, optional angular brackets around it.")
  3618. (defconst org-angle-link-re
  3619. (concat
  3620. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3621. "\\([^" org-non-link-chars " ]"
  3622. "[^" org-non-link-chars "]*"
  3623. "\\)>")
  3624. "Matches link with angular brackets, spaces are allowed.")
  3625. (defconst org-plain-link-re
  3626. (concat
  3627. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3628. "\\([^]\t\n\r<>,;() ]+\\)")
  3629. "Matches plain link, without spaces.")
  3630. (defconst org-bracket-link-regexp
  3631. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3632. "Matches a link in double brackets.")
  3633. (defconst org-bracket-link-analytic-regexp
  3634. (concat
  3635. "\\[\\["
  3636. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3637. "\\([^]]+\\)"
  3638. "\\]"
  3639. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3640. "\\]"))
  3641. ; 1: http:
  3642. ; 2: http
  3643. ; 3: path
  3644. ; 4: [desc]
  3645. ; 5: desc
  3646. (defconst org-any-link-re
  3647. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3648. org-angle-link-re "\\)\\|\\("
  3649. org-plain-link-re "\\)")
  3650. "Regular expression matching any link.")
  3651. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3652. "Regular expression for fast time stamp matching.")
  3653. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3654. "Regular expression for fast time stamp matching.")
  3655. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3656. "Regular expression matching time strings for analysis.
  3657. This one does not require the space after the date.")
  3658. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3659. "Regular expression matching time strings for analysis.")
  3660. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,11\\}>")
  3661. "Regular expression matching time stamps, with groups.")
  3662. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,11\\}[]>]")
  3663. "Regular expression matching time stamps (also [..]), with groups.")
  3664. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3665. "Regular expression matching a time stamp range.")
  3666. (defconst org-tr-regexp-both
  3667. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3668. "Regular expression matching a time stamp range.")
  3669. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3670. org-ts-regexp "\\)?")
  3671. "Regular expression matching a time stamp or time stamp range.")
  3672. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3673. org-ts-regexp-both "\\)?")
  3674. "Regular expression matching a time stamp or time stamp range.
  3675. The time stamps may be either active or inactive.")
  3676. (defvar org-emph-face nil)
  3677. (defun org-do-emphasis-faces (limit)
  3678. "Run through the buffer and add overlays to links."
  3679. (let (rtn)
  3680. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3681. (if (not (= (char-after (match-beginning 3))
  3682. (char-after (match-beginning 4))))
  3683. (progn
  3684. (setq rtn t)
  3685. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3686. 'face
  3687. (nth 1 (assoc (match-string 3)
  3688. org-emphasis-alist)))
  3689. (add-text-properties (match-beginning 2) (match-end 2)
  3690. '(font-lock-multiline t))
  3691. (backward-char 1))))
  3692. rtn))
  3693. (defun org-emphasize (&optional char)
  3694. "Insert or change an emphasis, i.e. a font like bold or italic.
  3695. If there is an active region, change that region to a new emphasis.
  3696. If there is no region, just insert the marker characters and position
  3697. the cursor between them.
  3698. CHAR should be either the marker character, or the first character of the
  3699. HTML tag associated with that emphasis. If CHAR is a space, the means
  3700. to remove the emphasis of the selected region.
  3701. If char is not given (for example in an interactive call) it
  3702. will be prompted for."
  3703. (interactive)
  3704. (let ((eal org-emphasis-alist) e det
  3705. (erc org-emphasis-regexp-components)
  3706. (prompt "")
  3707. (string "") beg end move tag c s)
  3708. (if (org-region-active-p)
  3709. (setq beg (region-beginning) end (region-end)
  3710. string (buffer-substring beg end))
  3711. (setq move t))
  3712. (while (setq e (pop eal))
  3713. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3714. c (aref tag 0))
  3715. (push (cons c (string-to-char (car e))) det)
  3716. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3717. (substring tag 1)))))
  3718. (unless char
  3719. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3720. (setq char (read-char-exclusive)))
  3721. (setq char (or (cdr (assoc char det)) char))
  3722. (if (equal char ?\ )
  3723. (setq s "" move nil)
  3724. (unless (assoc (char-to-string char) org-emphasis-alist)
  3725. (error "No such emphasis marker: \"%c\"" char))
  3726. (setq s (char-to-string char)))
  3727. (while (and (> (length string) 1)
  3728. (equal (substring string 0 1) (substring string -1))
  3729. (assoc (substring string 0 1) org-emphasis-alist))
  3730. (setq string (substring string 1 -1)))
  3731. (setq string (concat s string s))
  3732. (if beg (delete-region beg end))
  3733. (unless (or (bolp)
  3734. (string-match (concat "[" (nth 0 erc) "\n]")
  3735. (char-to-string (char-before (point)))))
  3736. (insert " "))
  3737. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3738. (char-to-string (char-after (point))))
  3739. (insert " ") (backward-char 1))
  3740. (insert string)
  3741. (and move (backward-char 1))))
  3742. (defun org-activate-plain-links (limit)
  3743. "Run through the buffer and add overlays to links."
  3744. (catch 'exit
  3745. (let (f)
  3746. (while (re-search-forward org-plain-link-re limit t)
  3747. (setq f (get-text-property (match-beginning 0) 'face))
  3748. (if (or (eq f 'org-tag)
  3749. (and (listp f) (memq 'org-tag f)))
  3750. nil
  3751. (add-text-properties (match-beginning 0) (match-end 0)
  3752. (list 'mouse-face 'highlight
  3753. 'rear-nonsticky t
  3754. 'keymap org-mouse-map
  3755. ))
  3756. (throw 'exit t))))))
  3757. (defun org-activate-angle-links (limit)
  3758. "Run through the buffer and add overlays to links."
  3759. (if (re-search-forward org-angle-link-re limit t)
  3760. (progn
  3761. (add-text-properties (match-beginning 0) (match-end 0)
  3762. (list 'mouse-face 'highlight
  3763. 'rear-nonsticky t
  3764. 'keymap org-mouse-map
  3765. ))
  3766. t)))
  3767. (defmacro org-maybe-intangible (props)
  3768. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3769. In emacs 21, invisible text is not avoided by the command loop, so the
  3770. intangible property is needed to make sure point skips this text.
  3771. In Emacs 22, this is not necessary. The intangible text property has
  3772. led to problems with flyspell. These problems are fixed in flyspell.el,
  3773. but we still avoid setting the property in Emacs 22 and later.
  3774. We use a macro so that the test can happen at compilation time."
  3775. (if (< emacs-major-version 22)
  3776. `(append '(intangible t) ,props)
  3777. props))
  3778. (defun org-activate-bracket-links (limit)
  3779. "Run through the buffer and add overlays to bracketed links."
  3780. (if (re-search-forward org-bracket-link-regexp limit t)
  3781. (let* ((help (concat "LINK: "
  3782. (org-match-string-no-properties 1)))
  3783. ;; FIXME: above we should remove the escapes.
  3784. ;; but that requires another match, protecting match data,
  3785. ;; a lot of overhead for font-lock.
  3786. (ip (org-maybe-intangible
  3787. (list 'invisible 'org-link 'rear-nonsticky t
  3788. 'keymap org-mouse-map 'mouse-face 'highlight
  3789. 'help-echo help)))
  3790. (vp (list 'rear-nonsticky t
  3791. 'keymap org-mouse-map 'mouse-face 'highlight
  3792. 'help-echo help)))
  3793. ;; We need to remove the invisible property here. Table narrowing
  3794. ;; may have made some of this invisible.
  3795. (remove-text-properties (match-beginning 0) (match-end 0)
  3796. '(invisible nil))
  3797. (if (match-end 3)
  3798. (progn
  3799. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3800. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3801. (add-text-properties (match-end 3) (match-end 0) ip))
  3802. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3803. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3804. (add-text-properties (match-end 1) (match-end 0) ip))
  3805. t)))
  3806. (defun org-activate-dates (limit)
  3807. "Run through the buffer and add overlays to dates."
  3808. (if (re-search-forward org-tsr-regexp-both limit t)
  3809. (progn
  3810. (add-text-properties (match-beginning 0) (match-end 0)
  3811. (list 'mouse-face 'highlight
  3812. 'rear-nonsticky t
  3813. 'keymap org-mouse-map))
  3814. (when org-display-custom-times
  3815. (if (match-end 3)
  3816. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3817. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3818. t)))
  3819. (defvar org-target-link-regexp nil
  3820. "Regular expression matching radio targets in plain text.")
  3821. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3822. "Regular expression matching a link target.")
  3823. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3824. "Regular expression matching a radio target.")
  3825. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3826. "Regular expression matching any target.")
  3827. (defun org-activate-target-links (limit)
  3828. "Run through the buffer and add overlays to target matches."
  3829. (when org-target-link-regexp
  3830. (let ((case-fold-search t))
  3831. (if (re-search-forward org-target-link-regexp limit t)
  3832. (progn
  3833. (add-text-properties (match-beginning 0) (match-end 0)
  3834. (list 'mouse-face 'highlight
  3835. 'rear-nonsticky t
  3836. 'keymap org-mouse-map
  3837. 'help-echo "Radio target link"
  3838. 'org-linked-text t))
  3839. t)))))
  3840. (defun org-update-radio-target-regexp ()
  3841. "Find all radio targets in this file and update the regular expression."
  3842. (interactive)
  3843. (when (memq 'radio org-activate-links)
  3844. (setq org-target-link-regexp
  3845. (org-make-target-link-regexp (org-all-targets 'radio)))
  3846. (org-restart-font-lock)))
  3847. (defun org-hide-wide-columns (limit)
  3848. (let (s e)
  3849. (setq s (text-property-any (point) (or limit (point-max))
  3850. 'org-cwidth t))
  3851. (when s
  3852. (setq e (next-single-property-change s 'org-cwidth))
  3853. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3854. (goto-char e)
  3855. t)))
  3856. (defun org-restart-font-lock ()
  3857. "Restart font-lock-mode, to force refontification."
  3858. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3859. (font-lock-mode -1)
  3860. (font-lock-mode 1)))
  3861. (defun org-all-targets (&optional radio)
  3862. "Return a list of all targets in this file.
  3863. With optional argument RADIO, only find radio targets."
  3864. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3865. rtn)
  3866. (save-excursion
  3867. (goto-char (point-min))
  3868. (while (re-search-forward re nil t)
  3869. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3870. rtn)))
  3871. (defun org-make-target-link-regexp (targets)
  3872. "Make regular expression matching all strings in TARGETS.
  3873. The regular expression finds the targets also if there is a line break
  3874. between words."
  3875. (and targets
  3876. (concat
  3877. "\\<\\("
  3878. (mapconcat
  3879. (lambda (x)
  3880. (while (string-match " +" x)
  3881. (setq x (replace-match "\\s-+" t t x)))
  3882. x)
  3883. targets
  3884. "\\|")
  3885. "\\)\\>")))
  3886. (defun org-activate-tags (limit)
  3887. (if (re-search-forward (org-re "[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3888. (progn
  3889. (add-text-properties (match-beginning 1) (match-end 1)
  3890. (list 'mouse-face 'highlight
  3891. 'rear-nonsticky t
  3892. 'keymap org-mouse-map))
  3893. t)))
  3894. (defun org-outline-level ()
  3895. (save-excursion
  3896. (looking-at outline-regexp)
  3897. (if (match-beginning 1)
  3898. (+ (org-get-string-indentation (match-string 1)) 1000)
  3899. (1- (- (match-end 0) (match-beginning 0))))))
  3900. (defvar org-font-lock-keywords nil)
  3901. (defconst org-property-re "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(\\S-.*\\)"
  3902. "Regular expression matching a property line.")
  3903. (defun org-set-font-lock-defaults ()
  3904. (let* ((em org-fontify-emphasized-text)
  3905. (lk org-activate-links)
  3906. (org-font-lock-extra-keywords
  3907. ;; Headlines
  3908. (list
  3909. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3910. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3911. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3912. (1 'org-table))
  3913. ;; Links
  3914. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3915. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3916. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3917. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3918. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3919. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3920. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3921. '(org-hide-wide-columns (0 nil append))
  3922. ;; TODO lines
  3923. (list (concat "^\\*+[ \t]+" org-not-done-regexp)
  3924. '(1 'org-todo t))
  3925. ;; Priorities
  3926. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3927. ;; Special keywords
  3928. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3929. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3930. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3931. (list (concat "\\<" org-archived-string) '(0 'org-special-keyword t))
  3932. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3933. ;; Emphasis
  3934. (if em
  3935. (if (featurep 'xemacs)
  3936. '(org-do-emphasis-faces (0 nil append))
  3937. '(org-do-emphasis-faces)))
  3938. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  3939. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  3940. 2 'bold prepend)
  3941. (if org-provide-checkbox-statistics
  3942. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3943. (0 (org-get-checkbox-statistics-face) t)))
  3944. ;; COMMENT
  3945. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3946. "\\|" org-quote-string "\\)\\>")
  3947. '(1 'org-special-keyword t))
  3948. '("^#.*" (0 'font-lock-comment-face t))
  3949. ;; DONE
  3950. (if org-fontify-done-headline
  3951. (list (concat "^[*]+ +\\<\\("
  3952. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3953. "\\)\\(.*\\)")
  3954. '(1 'org-done t) '(2 'org-headline-done t))
  3955. (list (concat "^[*]+ +\\<\\("
  3956. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3957. "\\)\\>")
  3958. '(1 'org-done t)))
  3959. ;; Table stuff
  3960. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  3961. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3962. ; '("^[ \t]*| *\\([#!$*_^/]\\) *|" (1 'org-formula t))
  3963. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3964. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3965. ;; Drawers
  3966. ; (list org-drawer-regexp '(0 'org-drawer t))
  3967. ; (list "^[ \t]*:END:" '(0 'org-drawer t))
  3968. (list org-drawer-regexp '(0 'org-special-keyword t))
  3969. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3970. ;; Properties
  3971. (list org-property-re
  3972. '(1 'org-special-keyword t)
  3973. '(3 'org-property-value t))
  3974. (if org-format-transports-properties-p
  3975. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3976. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  3977. )))
  3978. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3979. ;; Now set the full font-lock-keywords
  3980. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3981. (org-set-local 'font-lock-defaults
  3982. '(org-font-lock-keywords t nil nil backward-paragraph))
  3983. (kill-local-variable 'font-lock-keywords) nil))
  3984. (defvar org-m nil)
  3985. (defvar org-l nil)
  3986. (defvar org-f nil)
  3987. (defun org-get-level-face (n)
  3988. "Get the right face for match N in font-lock matching of healdines."
  3989. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3990. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3991. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  3992. (cond
  3993. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3994. ((eq n 2) org-f)
  3995. (t (if org-level-color-stars-only nil org-f))))
  3996. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3997. "Remove fontification and activation overlays from links."
  3998. (font-lock-default-unfontify-region beg end)
  3999. (let* ((buffer-undo-list t)
  4000. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4001. (inhibit-modification-hooks t)
  4002. deactivate-mark buffer-file-name buffer-file-truename)
  4003. (remove-text-properties beg end
  4004. '(mouse-face t keymap t org-linked-text t
  4005. rear-nonsticky t
  4006. invisible t intangible t))))
  4007. ;;;; Visibility cycling, including org-goto and indirect buffer
  4008. ;;; Cycling
  4009. (defvar org-cycle-global-status nil)
  4010. (make-variable-buffer-local 'org-cycle-global-status)
  4011. (defvar org-cycle-subtree-status nil)
  4012. (make-variable-buffer-local 'org-cycle-subtree-status)
  4013. ;;;###autoload
  4014. (defun org-cycle (&optional arg)
  4015. "Visibility cycling for Org-mode.
  4016. - When this function is called with a prefix argument, rotate the entire
  4017. buffer through 3 states (global cycling)
  4018. 1. OVERVIEW: Show only top-level headlines.
  4019. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4020. 3. SHOW ALL: Show everything.
  4021. - When point is at the beginning of a headline, rotate the subtree started
  4022. by this line through 3 different states (local cycling)
  4023. 1. FOLDED: Only the main headline is shown.
  4024. 2. CHILDREN: The main headline and the direct children are shown.
  4025. From this state, you can move to one of the children
  4026. and zoom in further.
  4027. 3. SUBTREE: Show the entire subtree, including body text.
  4028. - When there is a numeric prefix, go up to a heading with level ARG, do
  4029. a `show-subtree' and return to the previous cursor position. If ARG
  4030. is negative, go up that many levels.
  4031. - When point is not at the beginning of a headline, execute
  4032. `indent-relative', like TAB normally does. See the option
  4033. `org-cycle-emulate-tab' for details.
  4034. - Special case: if point is at the beginning of the buffer and there is
  4035. no headline in line 1, this function will act as if called with prefix arg."
  4036. (interactive "P")
  4037. (let* ((outline-regexp
  4038. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4039. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4040. outline-regexp))
  4041. (bob-special (and org-cycle-global-at-bob (bobp)
  4042. (not (looking-at outline-regexp))))
  4043. (org-cycle-hook
  4044. (if bob-special
  4045. (delq 'org-optimize-window-after-visibility-change
  4046. (copy-sequence org-cycle-hook))
  4047. org-cycle-hook))
  4048. (pos (point)))
  4049. (if (or bob-special (equal arg '(4)))
  4050. ;; special case: use global cycling
  4051. (setq arg t))
  4052. (cond
  4053. ((org-at-table-p 'any)
  4054. ;; Enter the table or move to the next field in the table
  4055. (or (org-table-recognize-table.el)
  4056. (progn
  4057. (if arg (org-table-edit-field t)
  4058. (org-table-justify-field-maybe)
  4059. (call-interactively 'org-table-next-field)))))
  4060. ((eq arg t) ;; Global cycling
  4061. (cond
  4062. ((and (eq last-command this-command)
  4063. (eq org-cycle-global-status 'overview))
  4064. ;; We just created the overview - now do table of contents
  4065. ;; This can be slow in very large buffers, so indicate action
  4066. (message "CONTENTS...")
  4067. (org-content)
  4068. (message "CONTENTS...done")
  4069. (setq org-cycle-global-status 'contents)
  4070. (run-hook-with-args 'org-cycle-hook 'contents))
  4071. ((and (eq last-command this-command)
  4072. (eq org-cycle-global-status 'contents))
  4073. ;; We just showed the table of contents - now show everything
  4074. (show-all)
  4075. (message "SHOW ALL")
  4076. (setq org-cycle-global-status 'all)
  4077. (run-hook-with-args 'org-cycle-hook 'all))
  4078. (t
  4079. ;; Default action: go to overview
  4080. (org-overview)
  4081. (message "OVERVIEW")
  4082. (setq org-cycle-global-status 'overview)
  4083. (run-hook-with-args 'org-cycle-hook 'overview))))
  4084. ((and org-drawers
  4085. (save-excursion
  4086. (beginning-of-line 1)
  4087. (looking-at org-drawer-regexp)))
  4088. ;; Toggle block visibility
  4089. (org-flag-drawer
  4090. (not (get-char-property (match-end 0) 'invisible))))
  4091. ((integerp arg)
  4092. ;; Show-subtree, ARG levels up from here.
  4093. (save-excursion
  4094. (org-back-to-heading)
  4095. (outline-up-heading (if (< arg 0) (- arg)
  4096. (- (funcall outline-level) arg)))
  4097. (org-show-subtree)))
  4098. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4099. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4100. ;; At a heading: rotate between three different views
  4101. (org-back-to-heading)
  4102. (let ((goal-column 0) eoh eol eos)
  4103. ;; First, some boundaries
  4104. (save-excursion
  4105. (org-back-to-heading)
  4106. (save-excursion
  4107. (beginning-of-line 2)
  4108. (while (and (not (eobp)) ;; this is like `next-line'
  4109. (get-char-property (1- (point)) 'invisible))
  4110. (beginning-of-line 2)) (setq eol (point)))
  4111. (outline-end-of-heading) (setq eoh (point))
  4112. (org-end-of-subtree t)
  4113. (unless (eobp)
  4114. (skip-chars-forward " \t\n")
  4115. (beginning-of-line 1) ; in case this is an item
  4116. )
  4117. (setq eos (1- (point))))
  4118. ;; Find out what to do next and set `this-command'
  4119. (cond
  4120. ((= eos eoh)
  4121. ;; Nothing is hidden behind this heading
  4122. (message "EMPTY ENTRY")
  4123. (setq org-cycle-subtree-status nil)
  4124. (save-excursion
  4125. (goto-char eos)
  4126. (outline-next-heading)
  4127. (if (org-invisible-p) (org-flag-heading nil))))
  4128. ((>= eol eos)
  4129. ;; Entire subtree is hidden in one line: open it
  4130. (org-show-entry)
  4131. (show-children)
  4132. (message "CHILDREN")
  4133. (save-excursion
  4134. (goto-char eos)
  4135. (outline-next-heading)
  4136. (if (org-invisible-p) (org-flag-heading nil)))
  4137. (setq org-cycle-subtree-status 'children)
  4138. (run-hook-with-args 'org-cycle-hook 'children))
  4139. ((and (eq last-command this-command)
  4140. (eq org-cycle-subtree-status 'children))
  4141. ;; We just showed the children, now show everything.
  4142. (org-show-subtree)
  4143. (message "SUBTREE")
  4144. (setq org-cycle-subtree-status 'subtree)
  4145. (run-hook-with-args 'org-cycle-hook 'subtree))
  4146. (t
  4147. ;; Default action: hide the subtree.
  4148. (hide-subtree)
  4149. (message "FOLDED")
  4150. (setq org-cycle-subtree-status 'folded)
  4151. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4152. ;; TAB emulation
  4153. (buffer-read-only (org-back-to-heading))
  4154. ((org-try-cdlatex-tab))
  4155. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4156. (or (not (bolp))
  4157. (not (looking-at outline-regexp))))
  4158. (call-interactively (global-key-binding "\t")))
  4159. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4160. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4161. (or (and (eq org-cycle-emulate-tab 'white)
  4162. (= (match-end 0) (point-at-eol)))
  4163. (and (eq org-cycle-emulate-tab 'whitestart)
  4164. (>= (match-end 0) pos))))
  4165. t
  4166. (eq org-cycle-emulate-tab t))
  4167. (if (and (looking-at "[ \n\r\t]")
  4168. (string-match "^[ \t]*$" (buffer-substring
  4169. (point-at-bol) (point))))
  4170. (progn
  4171. (beginning-of-line 1)
  4172. (and (looking-at "[ \t]+") (replace-match ""))))
  4173. (call-interactively (global-key-binding "\t")))
  4174. (t (save-excursion
  4175. (org-back-to-heading)
  4176. (org-cycle))))))
  4177. ;;;###autoload
  4178. (defun org-global-cycle (&optional arg)
  4179. "Cycle the global visibility. For details see `org-cycle'."
  4180. (interactive "P")
  4181. (let ((org-cycle-include-plain-lists
  4182. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4183. (if (integerp arg)
  4184. (progn
  4185. (show-all)
  4186. (hide-sublevels arg)
  4187. (setq org-cycle-global-status 'contents))
  4188. (org-cycle '(4)))))
  4189. (defun org-overview ()
  4190. "Switch to overview mode, shoing only top-level headlines.
  4191. Really, this shows all headlines with level equal or greater than the level
  4192. of the first headline in the buffer. This is important, because if the
  4193. first headline is not level one, then (hide-sublevels 1) gives confusing
  4194. results."
  4195. (interactive)
  4196. (let ((level (save-excursion
  4197. (goto-char (point-min))
  4198. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4199. (progn
  4200. (goto-char (match-beginning 0))
  4201. (funcall outline-level))))))
  4202. (and level (hide-sublevels level))))
  4203. (defun org-content (&optional arg)
  4204. "Show all headlines in the buffer, like a table of contents.
  4205. With numerical argument N, show content up to level N."
  4206. (interactive "P")
  4207. (save-excursion
  4208. ;; Visit all headings and show their offspring
  4209. (and (integerp arg) (org-overview))
  4210. (goto-char (point-max))
  4211. (catch 'exit
  4212. (while (and (progn (condition-case nil
  4213. (outline-previous-visible-heading 1)
  4214. (error (goto-char (point-min))))
  4215. t)
  4216. (looking-at outline-regexp))
  4217. (if (integerp arg)
  4218. (show-children (1- arg))
  4219. (show-branches))
  4220. (if (bobp) (throw 'exit nil))))))
  4221. (defun org-optimize-window-after-visibility-change (state)
  4222. "Adjust the window after a change in outline visibility.
  4223. This function is the default value of the hook `org-cycle-hook'."
  4224. (when (get-buffer-window (current-buffer))
  4225. (cond
  4226. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4227. ; ((eq state 'overview) (org-beginning-of-line))
  4228. ((eq state 'content) nil)
  4229. ((eq state 'all) nil)
  4230. ((eq state 'folded) nil)
  4231. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4232. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4233. (defun org-cycle-show-empty-lines (state)
  4234. "Show empty lines above all visible headlines.
  4235. The region to be covered depends on STATE when called through
  4236. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4237. entire buffer covered. Note that an empty line is only shown if there
  4238. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4239. (when (> org-cycle-separator-lines 0)
  4240. (save-excursion
  4241. (let* ((n org-cycle-separator-lines)
  4242. (re (cond
  4243. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4244. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4245. (t (let ((ns (number-to-string (- n 2))))
  4246. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4247. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4248. beg end)
  4249. (cond
  4250. ((memq state '(overview contents t))
  4251. (setq beg (point-min) end (point-max)))
  4252. ((memq state '(children folded))
  4253. (setq beg (point) end (progn (org-end-of-subtree t t)
  4254. (beginning-of-line 2)
  4255. (point)))))
  4256. (when beg
  4257. (goto-char beg)
  4258. (while (re-search-forward re end t)
  4259. (if (not (get-char-property (match-end 1) 'invisible))
  4260. (outline-flag-region
  4261. (match-beginning 1) (match-end 1) nil)))))))
  4262. ;; Never hide empty lines at the end of the file.
  4263. (save-excursion
  4264. (goto-char (point-max))
  4265. (outline-previous-heading)
  4266. (outline-end-of-heading)
  4267. (if (and (looking-at "[ \t\n]+")
  4268. (= (match-end 0) (point-max)))
  4269. (outline-flag-region (point) (match-end 0) nil))))
  4270. (defun org-subtree-end-visible-p ()
  4271. "Is the end of the current subtree visible?"
  4272. (pos-visible-in-window-p
  4273. (save-excursion (org-end-of-subtree t) (point))))
  4274. (defun org-first-headline-recenter (&optional N)
  4275. "Move cursor to the first headline and recenter the headline.
  4276. Optional argument N means, put the headline into the Nth line of the window."
  4277. (goto-char (point-min))
  4278. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4279. (beginning-of-line)
  4280. (recenter (prefix-numeric-value N))))
  4281. ;;; Org-goto
  4282. (defvar org-goto-window-configuration nil)
  4283. (defvar org-goto-marker nil)
  4284. (defvar org-goto-map (make-sparse-keymap))
  4285. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  4286. (while (setq cmd (pop cmds))
  4287. (substitute-key-definition cmd cmd org-goto-map global-map)))
  4288. (org-defkey org-goto-map "\C-m" 'org-goto-ret)
  4289. (org-defkey org-goto-map [(left)] 'org-goto-left)
  4290. (org-defkey org-goto-map [(right)] 'org-goto-right)
  4291. (org-defkey org-goto-map [(?q)] 'org-goto-quit)
  4292. (org-defkey org-goto-map [(control ?g)] 'org-goto-quit)
  4293. (org-defkey org-goto-map "\C-i" 'org-cycle)
  4294. (org-defkey org-goto-map [(tab)] 'org-cycle)
  4295. (org-defkey org-goto-map [(down)] 'outline-next-visible-heading)
  4296. (org-defkey org-goto-map [(up)] 'outline-previous-visible-heading)
  4297. (org-defkey org-goto-map "n" 'outline-next-visible-heading)
  4298. (org-defkey org-goto-map "p" 'outline-previous-visible-heading)
  4299. (org-defkey org-goto-map "f" 'outline-forward-same-level)
  4300. (org-defkey org-goto-map "b" 'outline-backward-same-level)
  4301. (org-defkey org-goto-map "u" 'outline-up-heading)
  4302. (org-defkey org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  4303. (org-defkey org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  4304. (org-defkey org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  4305. (org-defkey org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  4306. (org-defkey org-goto-map "\C-c\C-u" 'outline-up-heading)
  4307. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4308. (while l (org-defkey org-goto-map (int-to-string (pop l)) 'digit-argument)))
  4309. (defconst org-goto-help
  4310. "Select a location to jump to, press RET
  4311. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  4312. (defun org-goto ()
  4313. "Go to a different location of the document, keeping current visibility.
  4314. When you want to go to a different location in a document, the fastest way
  4315. is often to fold the entire buffer and then dive into the tree. This
  4316. method has the disadvantage, that the previous location will be folded,
  4317. which may not be what you want.
  4318. This command works around this by showing a copy of the current buffer in
  4319. overview mode. You can dive into the tree in that copy, to find the
  4320. location you want to reach. When pressing RET, the command returns to the
  4321. original buffer in which the visibility is still unchanged. It then jumps
  4322. to the new location, making it and the headline hierarchy above it visible."
  4323. (interactive)
  4324. (let* ((org-goto-start-pos (point))
  4325. (selected-point
  4326. (org-get-location (current-buffer) org-goto-help)))
  4327. (if selected-point
  4328. (progn
  4329. (org-mark-ring-push org-goto-start-pos)
  4330. (goto-char selected-point)
  4331. (if (or (org-invisible-p) (org-invisible-p2))
  4332. (org-show-context 'org-goto)))
  4333. (error "Quit"))))
  4334. (defvar org-selected-point nil) ; dynamically scoped parameter
  4335. (defun org-get-location (buf help)
  4336. "Let the user select a location in the Org-mode buffer BUF.
  4337. This function uses a recursive edit. It returns the selected position
  4338. or nil."
  4339. (let (org-selected-point)
  4340. (save-excursion
  4341. (save-window-excursion
  4342. (delete-other-windows)
  4343. (switch-to-buffer (get-buffer-create "*org-goto*"))
  4344. (with-output-to-temp-buffer "*Help*"
  4345. (princ help))
  4346. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4347. (setq buffer-read-only nil)
  4348. (erase-buffer)
  4349. (insert-buffer-substring buf)
  4350. (let ((org-startup-truncated t)
  4351. (org-startup-folded t)
  4352. (org-startup-align-all-tables nil))
  4353. (org-mode))
  4354. (setq buffer-read-only t)
  4355. (if (and (boundp 'org-goto-start-pos)
  4356. (integer-or-marker-p org-goto-start-pos))
  4357. (let ((org-show-hierarchy-above t)
  4358. (org-show-siblings t)
  4359. (org-show-following-heading t))
  4360. (goto-char org-goto-start-pos)
  4361. (and (org-invisible-p) (org-show-context)))
  4362. (goto-char (point-min)))
  4363. (org-beginning-of-line)
  4364. (message "Select location and press RET")
  4365. ;; now we make sure that during selection, ony very few keys work
  4366. ;; and that it is impossible to switch to another window.
  4367. (let ((gm (current-global-map))
  4368. (overriding-local-map org-goto-map))
  4369. (unwind-protect
  4370. (progn
  4371. (use-global-map org-goto-map)
  4372. (recursive-edit))
  4373. (use-global-map gm)))))
  4374. (kill-buffer "*org-goto*")
  4375. org-selected-point))
  4376. (defun org-goto-ret (&optional arg)
  4377. "Finish `org-goto' by going to the new location."
  4378. (interactive "P")
  4379. (setq org-selected-point (point)
  4380. current-prefix-arg arg)
  4381. (throw 'exit nil))
  4382. (defun org-goto-left ()
  4383. "Finish `org-goto' by going to the new location."
  4384. (interactive)
  4385. (if (org-on-heading-p)
  4386. (progn
  4387. (beginning-of-line 1)
  4388. (setq org-selected-point (point)
  4389. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4390. (throw 'exit nil))
  4391. (error "Not on a heading")))
  4392. (defun org-goto-right ()
  4393. "Finish `org-goto' by going to the new location."
  4394. (interactive)
  4395. (if (org-on-heading-p)
  4396. (progn
  4397. (outline-end-of-subtree)
  4398. (or (eobp) (forward-char 1))
  4399. (setq org-selected-point (point)
  4400. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4401. (throw 'exit nil))
  4402. (error "Not on a heading")))
  4403. (defun org-goto-quit ()
  4404. "Finish `org-goto' without cursor motion."
  4405. (interactive)
  4406. (setq org-selected-point nil)
  4407. (throw 'exit nil))
  4408. ;;; Indirect buffer display of subtrees
  4409. (defvar org-indirect-dedicated-frame nil
  4410. "This is the frame being used for indirect tree display.")
  4411. (defvar org-last-indirect-buffer nil)
  4412. (defun org-tree-to-indirect-buffer (&optional arg)
  4413. "Create indirect buffer and narrow it to current subtree.
  4414. With numerical prefix ARG, go up to this level and then take that tree.
  4415. If ARG is negative, go up that many levels.
  4416. Normally this command removes the indirect buffer previously made
  4417. with this command. However, when called with a C-u prefix, the last buffer
  4418. is kept so that you can work with several indirect buffers at the same time.
  4419. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4420. requests that a new frame be made for the new buffer, so that the dedicated
  4421. frame is not changed."
  4422. (interactive "P")
  4423. (let ((cbuf (current-buffer))
  4424. (cwin (selected-window))
  4425. (pos (point))
  4426. beg end level heading ibuf)
  4427. (save-excursion
  4428. (org-back-to-heading t)
  4429. (when (numberp arg)
  4430. (setq level (org-outline-level))
  4431. (if (< arg 0) (setq arg (+ level arg)))
  4432. (while (> (setq level (org-outline-level)) arg)
  4433. (outline-up-heading 1 t)))
  4434. (setq beg (point)
  4435. heading (org-get-heading))
  4436. (org-end-of-subtree t) (setq end (point)))
  4437. (if (and (not arg)
  4438. (buffer-live-p org-last-indirect-buffer))
  4439. (kill-buffer org-last-indirect-buffer))
  4440. (setq ibuf (org-get-indirect-buffer cbuf)
  4441. org-last-indirect-buffer ibuf)
  4442. (cond
  4443. ((or (eq org-indirect-buffer-display 'new-frame)
  4444. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4445. (select-frame (make-frame))
  4446. (delete-other-windows)
  4447. (switch-to-buffer ibuf)
  4448. (org-set-frame-title heading))
  4449. ((eq org-indirect-buffer-display 'dedicated-frame)
  4450. (raise-frame
  4451. (select-frame (or (and org-indirect-dedicated-frame
  4452. (frame-live-p org-indirect-dedicated-frame)
  4453. org-indirect-dedicated-frame)
  4454. (setq org-indirect-dedicated-frame (make-frame)))))
  4455. (delete-other-windows)
  4456. (switch-to-buffer ibuf)
  4457. (org-set-frame-title (concat "Indirect: " heading)))
  4458. ((eq org-indirect-buffer-display 'current-window)
  4459. (switch-to-buffer ibuf))
  4460. ((eq org-indirect-buffer-display 'other-window)
  4461. (pop-to-buffer ibuf))
  4462. (t (error "Invalid value.")))
  4463. (if (featurep 'xemacs)
  4464. (save-excursion (org-mode) (turn-on-font-lock)))
  4465. (narrow-to-region beg end)
  4466. (show-all)
  4467. (goto-char pos)
  4468. (and (window-live-p cwin) (select-window cwin))))
  4469. (defun org-get-indirect-buffer (&optional buffer)
  4470. (setq buffer (or buffer (current-buffer)))
  4471. (let ((n 1) (base (buffer-name buffer)) bname)
  4472. (while (buffer-live-p
  4473. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4474. (setq n (1+ n)))
  4475. (condition-case nil
  4476. (make-indirect-buffer buffer bname 'clone)
  4477. (error (make-indirect-buffer buffer bname)))))
  4478. (defun org-set-frame-title (title)
  4479. "Set the title of the current frame to the string TITLE."
  4480. ;; FIXME: how to name a single frame in XEmacs???
  4481. (unless (featurep 'xemacs)
  4482. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4483. ;;;; Structure editing
  4484. ;;; Inserting headlines
  4485. (defun org-insert-heading (&optional force-heading)
  4486. "Insert a new heading or item with same depth at point.
  4487. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4488. If point is at the beginning of a headline, insert a sibling before the
  4489. current headline. If point is in the middle of a headline, split the headline
  4490. at that position and make the rest of the headline part of the sibling below
  4491. the current headline."
  4492. (interactive "P")
  4493. (if (= (buffer-size) 0)
  4494. (insert "\n* ")
  4495. (when (or force-heading (not (org-insert-item)))
  4496. (let* ((head (save-excursion
  4497. (condition-case nil
  4498. (progn
  4499. (org-back-to-heading)
  4500. (match-string 0))
  4501. (error "*"))))
  4502. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4503. pos)
  4504. (cond
  4505. ((and (org-on-heading-p) (bolp)
  4506. (or (bobp)
  4507. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4508. (open-line (if blank 2 1)))
  4509. ((and (bolp)
  4510. (or (bobp)
  4511. (save-excursion
  4512. (backward-char 1) (not (org-invisible-p)))))
  4513. nil)
  4514. (t (newline (if blank 2 1))))
  4515. (insert head) (just-one-space)
  4516. (setq pos (point))
  4517. (end-of-line 1)
  4518. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4519. (run-hooks 'org-insert-heading-hook)))))
  4520. (defun org-insert-todo-heading (arg)
  4521. "Insert a new heading with the same level and TODO state as current heading.
  4522. If the heading has no TODO state, or if the state is DONE, use the first
  4523. state (TODO by default). Also with prefix arg, force first state."
  4524. (interactive "P")
  4525. (when (not (org-insert-item 'checkbox))
  4526. (org-insert-heading)
  4527. (save-excursion
  4528. (org-back-to-heading)
  4529. (outline-previous-heading)
  4530. (looking-at org-todo-line-regexp))
  4531. (if (or arg
  4532. (not (match-beginning 2))
  4533. (member (match-string 2) org-done-keywords))
  4534. (insert (car org-todo-keywords-1) " ")
  4535. (insert (match-string 2) " "))))
  4536. ;;; Promotion and Demotion
  4537. (defun org-promote-subtree ()
  4538. "Promote the entire subtree.
  4539. See also `org-promote'."
  4540. (interactive)
  4541. (save-excursion
  4542. (org-map-tree 'org-promote))
  4543. (org-fix-position-after-promote))
  4544. (defun org-demote-subtree ()
  4545. "Demote the entire subtree. See `org-demote'.
  4546. See also `org-promote'."
  4547. (interactive)
  4548. (save-excursion
  4549. (org-map-tree 'org-demote))
  4550. (org-fix-position-after-promote))
  4551. (defun org-do-promote ()
  4552. "Promote the current heading higher up the tree.
  4553. If the region is active in `transient-mark-mode', promote all headings
  4554. in the region."
  4555. (interactive)
  4556. (save-excursion
  4557. (if (org-region-active-p)
  4558. (org-map-region 'org-promote (region-beginning) (region-end))
  4559. (org-promote)))
  4560. (org-fix-position-after-promote))
  4561. (defun org-do-demote ()
  4562. "Demote the current heading lower down the tree.
  4563. If the region is active in `transient-mark-mode', demote all headings
  4564. in the region."
  4565. (interactive)
  4566. (save-excursion
  4567. (if (org-region-active-p)
  4568. (org-map-region 'org-demote (region-beginning) (region-end))
  4569. (org-demote)))
  4570. (org-fix-position-after-promote))
  4571. (defun org-fix-position-after-promote ()
  4572. "Make sure that after pro/demotion cursor position is right."
  4573. (let ((pos (point)))
  4574. (when (save-excursion
  4575. (beginning-of-line 1)
  4576. (looking-at org-todo-line-regexp)
  4577. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4578. (cond ((eobp) (insert " "))
  4579. ((eolp) (insert " "))
  4580. ((equal (char-after) ?\ ) (forward-char 1))))))
  4581. (defun org-reduced-level (l)
  4582. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4583. (defun org-get-legal-level (level &optional change)
  4584. "Rectify a level change under the influence of `org-odd-levels-only'
  4585. LEVEL is a current level, CHANGE is by how much the level should be
  4586. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4587. even level numbers will become the next higher odd number."
  4588. (if org-odd-levels-only
  4589. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4590. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4591. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4592. (max 1 (+ level change))))
  4593. (defun org-promote ()
  4594. "Promote the current heading higher up the tree.
  4595. If the region is active in `transient-mark-mode', promote all headings
  4596. in the region."
  4597. (org-back-to-heading t)
  4598. (let* ((level (save-match-data (funcall outline-level)))
  4599. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  4600. (diff (abs (- level (length up-head) -1))))
  4601. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4602. (replace-match up-head nil t)
  4603. ;; Fixup tag positioning
  4604. (and org-auto-align-tags (org-set-tags nil t))
  4605. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4606. (defun org-demote ()
  4607. "Demote the current heading lower down the tree.
  4608. If the region is active in `transient-mark-mode', demote all headings
  4609. in the region."
  4610. (org-back-to-heading t)
  4611. (let* ((level (save-match-data (funcall outline-level)))
  4612. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  4613. (diff (abs (- level (length down-head) -1))))
  4614. (replace-match down-head nil t)
  4615. ;; Fixup tag positioning
  4616. (and org-auto-align-tags (org-set-tags nil t))
  4617. (if org-adapt-indentation (org-fixup-indentation diff))))
  4618. (defun org-map-tree (fun)
  4619. "Call FUN for every heading underneath the current one."
  4620. (org-back-to-heading)
  4621. (let ((level (funcall outline-level)))
  4622. (save-excursion
  4623. (funcall fun)
  4624. (while (and (progn
  4625. (outline-next-heading)
  4626. (> (funcall outline-level) level))
  4627. (not (eobp)))
  4628. (funcall fun)))))
  4629. (defun org-map-region (fun beg end)
  4630. "Call FUN for every heading between BEG and END."
  4631. (let ((org-ignore-region t))
  4632. (save-excursion
  4633. (setq end (copy-marker end))
  4634. (goto-char beg)
  4635. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4636. (< (point) end))
  4637. (funcall fun))
  4638. (while (and (progn
  4639. (outline-next-heading)
  4640. (< (point) end))
  4641. (not (eobp)))
  4642. (funcall fun)))))
  4643. (defun org-fixup-indentation (diff)
  4644. "Change the indentation in the current entry by DIFF
  4645. However, if any line in the current entry has no indentation, or if it
  4646. would end up with no indentation after the change, nothing at all is done."
  4647. (save-excursion
  4648. (let ((end (save-excursion (outline-next-heading)
  4649. (point-marker)))
  4650. (prohibit (if (> diff 0)
  4651. "^\\S-"
  4652. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4653. col)
  4654. (unless (save-excursion (re-search-forward prohibit end t))
  4655. (while (re-search-forward "^[ \t]+" end t)
  4656. (goto-char (match-end 0))
  4657. (setq col (current-column))
  4658. (if (< diff 0) (replace-match ""))
  4659. (indent-to (+ diff col))))
  4660. (move-marker end nil))))
  4661. (defun org-convert-to-odd-levels ()
  4662. "Convert an org-mode file with all levels allowed to one with odd levels.
  4663. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4664. level 5 etc."
  4665. (interactive)
  4666. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4667. (let ((org-odd-levels-only nil) n)
  4668. (save-excursion
  4669. (goto-char (point-min))
  4670. (while (re-search-forward "^\\*\\*+ " nil t)
  4671. (setq n (- (length (match-string 0)) 2))
  4672. (while (>= (setq n (1- n)) 0)
  4673. (org-demote))
  4674. (end-of-line 1))))))
  4675. (defun org-convert-to-oddeven-levels ()
  4676. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4677. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4678. section with an even level, conversion would destroy the structure of the file. An error
  4679. is signaled in this case."
  4680. (interactive)
  4681. (goto-char (point-min))
  4682. ;; First check if there are no even levels
  4683. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4684. (org-show-context t)
  4685. (error "Not all levels are odd in this file. Conversion not possible."))
  4686. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4687. (let ((org-odd-levels-only nil) n)
  4688. (save-excursion
  4689. (goto-char (point-min))
  4690. (while (re-search-forward "^\\*\\*+ " nil t)
  4691. (setq n (/ (length (1- (match-string 0))) 2))
  4692. (while (>= (setq n (1- n)) 0)
  4693. (org-promote))
  4694. (end-of-line 1))))))
  4695. (defun org-tr-level (n)
  4696. "Make N odd if required."
  4697. (if org-odd-levels-only (1+ (/ n 2)) n))
  4698. ;;; Vertical tree motion, cutting and pasting of subtrees
  4699. (defun org-move-subtree-up (&optional arg)
  4700. "Move the current subtree up past ARG headlines of the same level."
  4701. (interactive "p")
  4702. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4703. (defun org-move-subtree-down (&optional arg)
  4704. "Move the current subtree down past ARG headlines of the same level."
  4705. (interactive "p")
  4706. (setq arg (prefix-numeric-value arg))
  4707. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4708. 'outline-get-last-sibling))
  4709. (ins-point (make-marker))
  4710. (cnt (abs arg))
  4711. beg end txt folded)
  4712. ;; Select the tree
  4713. (org-back-to-heading)
  4714. (setq beg (point))
  4715. (save-match-data
  4716. (save-excursion (outline-end-of-heading)
  4717. (setq folded (org-invisible-p)))
  4718. (outline-end-of-subtree))
  4719. (outline-next-heading)
  4720. (setq end (point))
  4721. ;; Find insertion point, with error handling
  4722. (goto-char beg)
  4723. (while (> cnt 0)
  4724. (or (and (funcall movfunc) (looking-at outline-regexp))
  4725. (progn (goto-char beg)
  4726. (error "Cannot move past superior level or buffer limit")))
  4727. (setq cnt (1- cnt)))
  4728. (if (> arg 0)
  4729. ;; Moving forward - still need to move over subtree
  4730. (progn (outline-end-of-subtree)
  4731. (outline-next-heading)
  4732. (if (not (or (looking-at (concat "^" outline-regexp))
  4733. (bolp)))
  4734. (newline))))
  4735. (move-marker ins-point (point))
  4736. (setq txt (buffer-substring beg end))
  4737. (delete-region beg end)
  4738. (insert txt)
  4739. (or (bolp) (insert "\n"))
  4740. (goto-char ins-point)
  4741. (if folded (hide-subtree))
  4742. (move-marker ins-point nil)))
  4743. (defvar org-subtree-clip ""
  4744. "Clipboard for cut and paste of subtrees.
  4745. This is actually only a copy of the kill, because we use the normal kill
  4746. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4747. (defvar org-subtree-clip-folded nil
  4748. "Was the last copied subtree folded?
  4749. This is used to fold the tree back after pasting.")
  4750. (defun org-cut-subtree ()
  4751. "Cut the current subtree into the clipboard.
  4752. This is a short-hand for marking the subtree and then cutting it."
  4753. (interactive)
  4754. (org-copy-subtree 'cut))
  4755. (defun org-copy-subtree (&optional cut)
  4756. "Cut the current subtree into the clipboard.
  4757. This is a short-hand for marking the subtree and then copying it.
  4758. If CUT is non-nil, actually cut the subtree."
  4759. (interactive)
  4760. (let (beg end folded)
  4761. (if (interactive-p)
  4762. (org-back-to-heading nil) ; take what looks like a subtree
  4763. (org-back-to-heading t)) ; take what is really there
  4764. (setq beg (point))
  4765. (save-match-data
  4766. (save-excursion (outline-end-of-heading)
  4767. (setq folded (org-invisible-p)))
  4768. (outline-end-of-subtree))
  4769. (if (equal (char-after) ?\n) (forward-char 1))
  4770. (setq end (point))
  4771. (goto-char beg)
  4772. (when (> end beg)
  4773. (setq org-subtree-clip-folded folded)
  4774. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4775. (setq org-subtree-clip (current-kill 0))
  4776. (message "%s: Subtree with %d characters"
  4777. (if cut "Cut" "Copied")
  4778. (length org-subtree-clip)))))
  4779. (defun org-paste-subtree (&optional level tree)
  4780. "Paste the clipboard as a subtree, with modification of headline level.
  4781. The entire subtree is promoted or demoted in order to match a new headline
  4782. level. By default, the new level is derived from the visible headings
  4783. before and after the insertion point, and taken to be the inferior headline
  4784. level of the two. So if the previous visible heading is level 3 and the
  4785. next is level 4 (or vice versa), level 4 will be used for insertion.
  4786. This makes sure that the subtree remains an independent subtree and does
  4787. not swallow low level entries.
  4788. You can also force a different level, either by using a numeric prefix
  4789. argument, or by inserting the heading marker by hand. For example, if the
  4790. cursor is after \"*****\", then the tree will be shifted to level 5.
  4791. If you want to insert the tree as is, just use \\[yank].
  4792. If optional TREE is given, use this text instead of the kill ring."
  4793. (interactive "P")
  4794. (unless (org-kill-is-subtree-p tree)
  4795. (error
  4796. (substitute-command-keys
  4797. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4798. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4799. (^re (concat "^\\(" outline-regexp "\\)"))
  4800. (re (concat "\\(" outline-regexp "\\)"))
  4801. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  4802. (old-level (if (string-match ^re txt)
  4803. (- (match-end 0) (match-beginning 0))
  4804. -1))
  4805. (force-level (cond (level (prefix-numeric-value level))
  4806. ((string-match
  4807. ^re_ (buffer-substring (point-at-bol) (point)))
  4808. (- (match-end 0) (match-beginning 0)))
  4809. (t nil)))
  4810. (previous-level (save-excursion
  4811. (condition-case nil
  4812. (progn
  4813. (outline-previous-visible-heading 1)
  4814. (if (looking-at re)
  4815. (- (match-end 0) (match-beginning 0))
  4816. 1))
  4817. (error 1))))
  4818. (next-level (save-excursion
  4819. (condition-case nil
  4820. (progn
  4821. (outline-next-visible-heading 1)
  4822. (if (looking-at re)
  4823. (- (match-end 0) (match-beginning 0))
  4824. 1))
  4825. (error 1))))
  4826. (new-level (or force-level (max previous-level next-level)))
  4827. (shift (if (or (= old-level -1)
  4828. (= new-level -1)
  4829. (= old-level new-level))
  4830. 0
  4831. (- new-level old-level)))
  4832. (shift1 shift)
  4833. (delta (if (> shift 0) -1 1))
  4834. (func (if (> shift 0) 'org-demote 'org-promote))
  4835. (org-odd-levels-only nil)
  4836. beg end)
  4837. ;; Remove the forces level indicator
  4838. (if force-level
  4839. (delete-region (point-at-bol) (point)))
  4840. ;; Make sure we start at the beginning of an empty line
  4841. (if (not (bolp)) (insert "\n"))
  4842. (if (not (looking-at "[ \t]*$"))
  4843. (progn (insert "\n") (backward-char 1)))
  4844. ;; Paste
  4845. (setq beg (point))
  4846. (if (string-match "[ \t\r\n]+\\'" txt)
  4847. (setq txt (replace-match "\n" t t txt)))
  4848. (insert txt)
  4849. (setq end (point))
  4850. (if (looking-at "[ \t\r\n]+")
  4851. (replace-match "\n"))
  4852. (goto-char beg)
  4853. ;; Shift if necessary
  4854. (if (= shift 0)
  4855. (message "Pasted at level %d, without shift" new-level)
  4856. (save-restriction
  4857. (narrow-to-region beg end)
  4858. (while (not (= shift 0))
  4859. (org-map-region func (point-min) (point-max))
  4860. (setq shift (+ delta shift)))
  4861. (goto-char (point-min))
  4862. (message "Pasted at level %d, with shift by %d levels"
  4863. new-level shift1)))
  4864. (if (and kill-ring
  4865. (eq org-subtree-clip (current-kill 0))
  4866. org-subtree-clip-folded)
  4867. ;; The tree was folded before it was killed/copied
  4868. (hide-subtree))))
  4869. (defun org-kill-is-subtree-p (&optional txt)
  4870. "Check if the current kill is an outline subtree, or a set of trees.
  4871. Returns nil if kill does not start with a headline, or if the first
  4872. headline level is not the largest headline level in the tree.
  4873. So this will actually accept several entries of equal levels as well,
  4874. which is OK for `org-paste-subtree'.
  4875. If optional TXT is given, check this string instead of the current kill."
  4876. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4877. (start-level (and kill
  4878. (string-match (concat "\\`" outline-regexp) kill)
  4879. (- (match-end 0) (match-beginning 0))))
  4880. (re (concat "^" outline-regexp))
  4881. (start 1))
  4882. (if (not start-level)
  4883. nil ;; does not even start with a heading
  4884. (catch 'exit
  4885. (while (setq start (string-match re kill (1+ start)))
  4886. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  4887. (throw 'exit nil)))
  4888. t))))
  4889. (defun org-narrow-to-subtree ()
  4890. "Narrow buffer to the current subtree."
  4891. (interactive)
  4892. (save-excursion
  4893. (narrow-to-region
  4894. (progn (org-back-to-heading) (point))
  4895. (progn (org-end-of-subtree t t) (point)))))
  4896. ;;; Outline Sorting
  4897. (defun org-sort (with-case)
  4898. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  4899. (interactive "P")
  4900. (if (org-at-table-p)
  4901. (org-call-with-arg 'org-table-sort-lines with-case)
  4902. (org-call-with-arg 'org-sort-entries with-case)))
  4903. (defun org-sort-entries (&optional with-case sorting-type)
  4904. "Sort entries on a certain level of an outline tree.
  4905. If there is an active region, the entries in the region are sorted.
  4906. Else, if the cursor is before the first entry, sort the top-level items.
  4907. Else, the children of the entry at point are sorted.
  4908. Sorting can be alphabetically, numerically, and by date/time as given by
  4909. the first time stamp in the entry. The command prompts for the sorting
  4910. type unless it has been given to the function through the SORTING-TYPE
  4911. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  4912. Comparing entries ignores case by default. However, with an optional argument
  4913. WITH-CASE, the sorting considers case as well. With two prefix arguments
  4914. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  4915. (interactive "P")
  4916. (let ((unique (equal with-case '(16)))
  4917. start beg end entries stars re re2 p nentries (nremoved 0)
  4918. last txt what)
  4919. ;; Find beginning and end of region to sort
  4920. (cond
  4921. ((org-region-active-p)
  4922. ;; we will sort the region
  4923. (setq end (region-end)
  4924. what "region")
  4925. (goto-char (region-beginning))
  4926. (if (not (org-on-heading-p)) (outline-next-heading))
  4927. (setq start (point)))
  4928. ((or (org-on-heading-p)
  4929. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4930. ;; we will sort the children of the current headline
  4931. (org-back-to-heading)
  4932. (setq start (point) end (org-end-of-subtree) what "children")
  4933. (goto-char start)
  4934. (show-subtree)
  4935. (outline-next-heading))
  4936. (t
  4937. ;; we will sort the top-level entries in this file
  4938. (goto-char (point-min))
  4939. (or (org-on-heading-p) (outline-next-heading))
  4940. (setq start (point) end (point-max) what "top-level")
  4941. (goto-char start)
  4942. (show-all)))
  4943. (setq beg (point))
  4944. (if (>= (point) end) (error "Nothing to sort"))
  4945. (looking-at "\\(\\*+\\)")
  4946. (setq stars (match-string 1)
  4947. re (concat "^" (regexp-quote stars) " +")
  4948. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4949. txt (buffer-substring beg end))
  4950. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4951. (if (and (not (equal stars "*")) (string-match re2 txt))
  4952. (error "Region to sort contains a level above the first entry"))
  4953. ;; Make a list that can be sorted.
  4954. ;; The car is the string for comparison, the cdr is the subtree
  4955. (message "Sorting entries...")
  4956. (setq entries
  4957. (mapcar
  4958. (lambda (x)
  4959. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  4960. (cons (match-string 0 x) x))
  4961. (org-split-string txt re)))
  4962. ;; Sort the list
  4963. (save-excursion
  4964. (goto-char start)
  4965. (setq entries (org-do-sort entries what with-case sorting-type)))
  4966. ;; Delete the old stuff
  4967. (goto-char beg)
  4968. (kill-region beg end)
  4969. (setq nentries (length entries))
  4970. ;; Insert the sorted entries, and remove duplicates if this is required
  4971. (while (setq p (pop entries))
  4972. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  4973. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  4974. (insert stars " " (cdr p))))
  4975. (goto-char start)
  4976. (message "Sorting entries...done (%d entries%s)"
  4977. nentries
  4978. (if unique (format ", %d duplicates removed" nremoved) ""))))
  4979. (defun org-do-sort (table what &optional with-case sorting-type)
  4980. "Sort TABLE of WHAT according to SORTING-TYPE.
  4981. The user will be prompted for the SORTING-TYPE if the call to this
  4982. function does not specify it. WHAT is only for the prompt, to indicate
  4983. what is being sorted. The sorting key will be extracted from
  4984. the car of the elements of the table.
  4985. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4986. (unless sorting-type
  4987. (message
  4988. "Sort %s: [a]lphabetically [n]umerically [t]ime. A/N/T means reversed:"
  4989. what)
  4990. (setq sorting-type (read-char-exclusive)))
  4991. (let ((dcst (downcase sorting-type))
  4992. extractfun comparefun)
  4993. ;; Define the appropriate functions
  4994. (cond
  4995. ((= dcst ?n)
  4996. (setq extractfun 'string-to-number
  4997. comparefun (if (= dcst sorting-type) '< '>)))
  4998. ((= dcst ?a)
  4999. (setq extractfun (if with-case 'identity 'downcase)
  5000. comparefun (if (= dcst sorting-type)
  5001. 'string<
  5002. (lambda (a b) (and (not (string< a b))
  5003. (not (string= a b)))))))
  5004. ((= dcst ?t)
  5005. (setq extractfun
  5006. (lambda (x)
  5007. (if (string-match org-ts-regexp x)
  5008. (time-to-seconds
  5009. (org-time-string-to-time (match-string 0 x)))
  5010. 0))
  5011. comparefun (if (= dcst sorting-type) '< '>)))
  5012. (t (error "Invalid sorting type `%c'" sorting-type)))
  5013. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5014. table)
  5015. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5016. ;;;; Plain list items, including checkboxes
  5017. ;;; Plain list items
  5018. (defun org-at-item-p ()
  5019. "Is point in a line starting a hand-formatted item?"
  5020. (let ((llt org-plain-list-ordered-item-terminator))
  5021. (save-excursion
  5022. (goto-char (point-at-bol))
  5023. (looking-at
  5024. (cond
  5025. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5026. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5027. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5028. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5029. (defun org-in-item-p ()
  5030. "It the cursor inside a plain list item.
  5031. Does not have to be the first line."
  5032. (save-excursion
  5033. (condition-case nil
  5034. (progn
  5035. (org-beginning-of-item)
  5036. (org-at-item-p)
  5037. t)
  5038. (error nil))))
  5039. (defun org-insert-item (&optional checkbox)
  5040. "Insert a new item at the current level.
  5041. Return t when things worked, nil when we are not in an item."
  5042. (when (save-excursion
  5043. (condition-case nil
  5044. (progn
  5045. (org-beginning-of-item)
  5046. (org-at-item-p)
  5047. (if (org-invisible-p) (error "Invisible item"))
  5048. t)
  5049. (error nil)))
  5050. (let* ((bul (match-string 0))
  5051. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5052. (match-end 0)))
  5053. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5054. pos)
  5055. (cond
  5056. ((and (org-at-item-p) (<= (point) eow))
  5057. ;; before the bullet
  5058. (beginning-of-line 1)
  5059. (open-line (if blank 2 1)))
  5060. ((<= (point) eow)
  5061. (beginning-of-line 1))
  5062. (t (newline (if blank 2 1))))
  5063. (insert bul (if checkbox "[ ]" ""))
  5064. (just-one-space)
  5065. (setq pos (point))
  5066. (end-of-line 1)
  5067. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5068. (org-maybe-renumber-ordered-list)
  5069. (and checkbox (org-update-checkbox-count-maybe))
  5070. t))
  5071. ;;; Checkboxes
  5072. (defun org-at-item-checkbox-p ()
  5073. "Is point at a line starting a plain-list item with a checklet?"
  5074. (and (org-at-item-p)
  5075. (save-excursion
  5076. (goto-char (match-end 0))
  5077. (skip-chars-forward " \t")
  5078. (looking-at "\\[[ X]\\]"))))
  5079. (defun org-toggle-checkbox (&optional arg)
  5080. "Toggle the checkbox in the current line."
  5081. (interactive "P")
  5082. (catch 'exit
  5083. (let (beg end status (firstnew 'unknown))
  5084. (cond
  5085. ((org-region-active-p)
  5086. (setq beg (region-beginning) end (region-end)))
  5087. ((org-on-heading-p)
  5088. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5089. ((org-at-item-checkbox-p)
  5090. (save-excursion
  5091. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  5092. (throw 'exit t))
  5093. (t (error "Not at a checkbox or heading, and no active region")))
  5094. (save-excursion
  5095. (goto-char beg)
  5096. (while (< (point) end)
  5097. (when (org-at-item-checkbox-p)
  5098. (setq status (equal (match-string 0) "[X]"))
  5099. (when (eq firstnew 'unknown)
  5100. (setq firstnew (not status)))
  5101. (replace-match
  5102. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5103. (beginning-of-line 2)))))
  5104. (org-update-checkbox-count-maybe))
  5105. (defun org-update-checkbox-count-maybe ()
  5106. "Update checkbox statistics unless turned off by user."
  5107. (when org-provide-checkbox-statistics
  5108. (org-update-checkbox-count)))
  5109. (defun org-update-checkbox-count (&optional all)
  5110. "Update the checkbox statistics in the current section.
  5111. This will find all statistic cookies like [57%] and [6/12] and update them
  5112. with the current numbers. With optional prefix argument ALL, do this for
  5113. the whole buffer."
  5114. (interactive "P")
  5115. (save-excursion
  5116. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5117. (beg (condition-case nil
  5118. (progn (outline-back-to-heading) (point))
  5119. (error (point-min))))
  5120. (end (move-marker (make-marker)
  5121. (progn (outline-next-heading) (point))))
  5122. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5123. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  5124. b1 e1 f1 c-on c-off lim (cstat 0))
  5125. (when all
  5126. (goto-char (point-min))
  5127. (outline-next-heading)
  5128. (setq beg (point) end (point-max)))
  5129. (goto-char beg)
  5130. (while (re-search-forward re end t)
  5131. (setq cstat (1+ cstat)
  5132. b1 (match-beginning 0)
  5133. e1 (match-end 0)
  5134. f1 (match-beginning 1)
  5135. lim (cond
  5136. ((org-on-heading-p) (outline-next-heading) (point))
  5137. ((org-at-item-p) (org-end-of-item) (point))
  5138. (t nil))
  5139. c-on 0 c-off 0)
  5140. (goto-char e1)
  5141. (when lim
  5142. (while (re-search-forward re-box lim t)
  5143. (if (equal (match-string 2) "[ ]")
  5144. (setq c-off (1+ c-off))
  5145. (setq c-on (1+ c-on))))
  5146. (delete-region b1 e1)
  5147. (goto-char b1)
  5148. (insert (if f1
  5149. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5150. (format "[%d/%d]" c-on (+ c-on c-off))))))
  5151. (when (interactive-p)
  5152. (message "Checkbox satistics updated %s (%d places)"
  5153. (if all "in entire file" "in current outline entry") cstat)))))
  5154. (defun org-get-checkbox-statistics-face ()
  5155. "Select the face for checkbox statistics.
  5156. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5157. it will be `org-todo'."
  5158. (if (match-end 1)
  5159. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5160. (if (and (> (match-end 2) (match-beginning 2))
  5161. (equal (match-string 2) (match-string 3)))
  5162. 'org-done
  5163. 'org-todo)))
  5164. (defun org-get-indentation (&optional line)
  5165. "Get the indentation of the current line, interpreting tabs.
  5166. When LINE is given, assume it represents a line and compute its indentation."
  5167. (if line
  5168. (if (string-match "^ *" (org-remove-tabs line))
  5169. (match-end 0))
  5170. (save-excursion
  5171. (beginning-of-line 1)
  5172. (skip-chars-forward " \t")
  5173. (current-column))))
  5174. (defun org-remove-tabs (s &optional width)
  5175. "Replace tabulators in S with spaces.
  5176. Assumes that s is a single line, starting in column 0."
  5177. (setq width (or width tab-width))
  5178. (while (string-match "\t" s)
  5179. (setq s (replace-match
  5180. (make-string
  5181. (- (* width (/ (+ (match-beginning 0) width) width))
  5182. (match-beginning 0)) ?\ )
  5183. t t s)))
  5184. s)
  5185. (defun org-fix-indentation (line ind)
  5186. "Fix indentation in LINE.
  5187. IND is a cons cell with target and minimum indentation.
  5188. If the current indenation in LINE is smaller than the minimum,
  5189. leave it alone. If it is larger than ind, set it to the target."
  5190. (let* ((l (org-remove-tabs line))
  5191. (i (org-get-indentation l))
  5192. (i1 (car ind)) (i2 (cdr ind)))
  5193. (if (>= i i2) (setq l (substring line i2)))
  5194. (if (> i1 0)
  5195. (concat (make-string i1 ?\ ) l)
  5196. l)))
  5197. (defcustom org-empty-line-terminates-plain-lists nil
  5198. "Non-nil means, an empty line ends all plain list levels.
  5199. When nil, empty lines are part of the preceeding item."
  5200. :group 'org-plain-lists
  5201. :type 'boolean)
  5202. (defun org-beginning-of-item ()
  5203. "Go to the beginning of the current hand-formatted item.
  5204. If the cursor is not in an item, throw an error."
  5205. (interactive)
  5206. (let ((pos (point))
  5207. (limit (save-excursion
  5208. (condition-case nil
  5209. (progn
  5210. (org-back-to-heading)
  5211. (beginning-of-line 2) (point))
  5212. (error (point-min)))))
  5213. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5214. ind ind1)
  5215. (if (org-at-item-p)
  5216. (beginning-of-line 1)
  5217. (beginning-of-line 1)
  5218. (skip-chars-forward " \t")
  5219. (setq ind (current-column))
  5220. (if (catch 'exit
  5221. (while t
  5222. (beginning-of-line 0)
  5223. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5224. (if (looking-at "[ \t]*$")
  5225. (setq ind1 ind-empty)
  5226. (skip-chars-forward " \t")
  5227. (setq ind1 (current-column)))
  5228. (if (< ind1 ind)
  5229. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5230. nil
  5231. (goto-char pos)
  5232. (error "Not in an item")))))
  5233. (defun org-end-of-item ()
  5234. "Go to the end of the current hand-formatted item.
  5235. If the cursor is not in an item, throw an error."
  5236. (interactive)
  5237. (let* ((pos (point))
  5238. ind1
  5239. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5240. (limit (save-excursion (outline-next-heading) (point)))
  5241. (ind (save-excursion
  5242. (org-beginning-of-item)
  5243. (skip-chars-forward " \t")
  5244. (current-column)))
  5245. (end (catch 'exit
  5246. (while t
  5247. (beginning-of-line 2)
  5248. (if (eobp) (throw 'exit (point)))
  5249. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5250. (if (looking-at "[ \t]*$")
  5251. (setq ind1 ind-empty)
  5252. (skip-chars-forward " \t")
  5253. (setq ind1 (current-column)))
  5254. (if (<= ind1 ind)
  5255. (throw 'exit (point-at-bol)))))))
  5256. (if end
  5257. (goto-char end)
  5258. (goto-char pos)
  5259. (error "Not in an item"))))
  5260. (defun org-next-item ()
  5261. "Move to the beginning of the next item in the current plain list.
  5262. Error if not at a plain list, or if this is the last item in the list."
  5263. (interactive)
  5264. (let (ind ind1 (pos (point)))
  5265. (org-beginning-of-item)
  5266. (setq ind (org-get-indentation))
  5267. (org-end-of-item)
  5268. (setq ind1 (org-get-indentation))
  5269. (unless (and (org-at-item-p) (= ind ind1))
  5270. (goto-char pos)
  5271. (error "On last item"))))
  5272. (defun org-previous-item ()
  5273. "Move to the beginning of the previous item in the current plain list.
  5274. Error if not at a plain list, or if this is the first item in the list."
  5275. (interactive)
  5276. (let (beg ind ind1 (pos (point)))
  5277. (org-beginning-of-item)
  5278. (setq beg (point))
  5279. (setq ind (org-get-indentation))
  5280. (goto-char beg)
  5281. (catch 'exit
  5282. (while t
  5283. (beginning-of-line 0)
  5284. (if (looking-at "[ \t]*$")
  5285. nil
  5286. (if (<= (setq ind1 (org-get-indentation)) ind)
  5287. (throw 'exit t)))))
  5288. (condition-case nil
  5289. (if (or (not (org-at-item-p))
  5290. (< ind1 (1- ind)))
  5291. (error "")
  5292. (org-beginning-of-item))
  5293. (error (goto-char pos)
  5294. (error "On first item")))))
  5295. (defun org-move-item-down ()
  5296. "Move the plain list item at point down, i.e. swap with following item.
  5297. Subitems (items with larger indentation) are considered part of the item,
  5298. so this really moves item trees."
  5299. (interactive)
  5300. (let (beg end ind ind1 (pos (point)) txt)
  5301. (org-beginning-of-item)
  5302. (setq beg (point))
  5303. (setq ind (org-get-indentation))
  5304. (org-end-of-item)
  5305. (setq end (point))
  5306. (setq ind1 (org-get-indentation))
  5307. (if (and (org-at-item-p) (= ind ind1))
  5308. (progn
  5309. (org-end-of-item)
  5310. (setq txt (buffer-substring beg end))
  5311. (save-excursion
  5312. (delete-region beg end))
  5313. (setq pos (point))
  5314. (insert txt)
  5315. (goto-char pos)
  5316. (org-maybe-renumber-ordered-list))
  5317. (goto-char pos)
  5318. (error "Cannot move this item further down"))))
  5319. (defun org-move-item-up (arg)
  5320. "Move the plain list item at point up, i.e. swap with previous item.
  5321. Subitems (items with larger indentation) are considered part of the item,
  5322. so this really moves item trees."
  5323. (interactive "p")
  5324. (let (beg end ind ind1 (pos (point)) txt)
  5325. (org-beginning-of-item)
  5326. (setq beg (point))
  5327. (setq ind (org-get-indentation))
  5328. (org-end-of-item)
  5329. (setq end (point))
  5330. (goto-char beg)
  5331. (catch 'exit
  5332. (while t
  5333. (beginning-of-line 0)
  5334. (if (looking-at "[ \t]*$")
  5335. (if org-empty-line-terminates-plain-lists
  5336. (progn
  5337. (goto-char pos)
  5338. (error "Cannot move this item further up"))
  5339. nil)
  5340. (if (<= (setq ind1 (org-get-indentation)) ind)
  5341. (throw 'exit t)))))
  5342. (condition-case nil
  5343. (org-beginning-of-item)
  5344. (error (goto-char beg)
  5345. (error "Cannot move this item further up")))
  5346. (setq ind1 (org-get-indentation))
  5347. (if (and (org-at-item-p) (= ind ind1))
  5348. (progn
  5349. (setq txt (buffer-substring beg end))
  5350. (save-excursion
  5351. (delete-region beg end))
  5352. (setq pos (point))
  5353. (insert txt)
  5354. (goto-char pos)
  5355. (org-maybe-renumber-ordered-list))
  5356. (goto-char pos)
  5357. (error "Cannot move this item further up"))))
  5358. (defun org-maybe-renumber-ordered-list ()
  5359. "Renumber the ordered list at point if setup allows it.
  5360. This tests the user option `org-auto-renumber-ordered-lists' before
  5361. doing the renumbering."
  5362. (interactive)
  5363. (when (and org-auto-renumber-ordered-lists
  5364. (org-at-item-p))
  5365. (if (match-beginning 3)
  5366. (org-renumber-ordered-list 1)
  5367. (org-fix-bullet-type 1))))
  5368. (defun org-maybe-renumber-ordered-list-safe ()
  5369. (condition-case nil
  5370. (save-excursion
  5371. (org-maybe-renumber-ordered-list))
  5372. (error nil)))
  5373. (defun org-cycle-list-bullet (&optional which)
  5374. "Cycle through the different itemize/enumerate bullets.
  5375. This cycle the entire list level through the sequence:
  5376. `-' -> `+' -> `*' -> `1.' -> `1)'
  5377. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5378. 0 meand `-', 1 means `+' etc."
  5379. (interactive "P")
  5380. (org-preserve-lc
  5381. (org-beginning-of-item-list)
  5382. (org-at-item-p)
  5383. (beginning-of-line 1)
  5384. (let ((current (match-string 0)) new)
  5385. (setq new (cond
  5386. ((and which (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5387. ((string-match "-" current) "+")
  5388. ((string-match "\\+" current)
  5389. (if (looking-at "\\S-") "1." "*"))
  5390. ((string-match "\\*" current) "1.")
  5391. ((string-match "\\." current) "1)")
  5392. ((string-match ")" current) "-")
  5393. (t (error "This should not happen"))))
  5394. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5395. (org-fix-bullet-type 1)
  5396. (org-maybe-renumber-ordered-list))))
  5397. (defun org-get-string-indentation (s)
  5398. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5399. (let ((n -1) (i 0) (w tab-width) c)
  5400. (catch 'exit
  5401. (while (< (setq n (1+ n)) (length s))
  5402. (setq c (aref s n))
  5403. (cond ((= c ?\ ) (setq i (1+ i)))
  5404. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5405. (t (throw 'exit t)))))
  5406. i))
  5407. (defun org-renumber-ordered-list (arg)
  5408. "Renumber an ordered plain list.
  5409. Cursor needs to be in the first line of an item, the line that starts
  5410. with something like \"1.\" or \"2)\"."
  5411. (interactive "p")
  5412. (unless (and (org-at-item-p)
  5413. (match-beginning 3))
  5414. (error "This is not an ordered list"))
  5415. (let ((line (org-current-line))
  5416. (col (current-column))
  5417. (ind (org-get-string-indentation
  5418. (buffer-substring (point-at-bol) (match-beginning 3))))
  5419. ;; (term (substring (match-string 3) -1))
  5420. ind1 (n (1- arg))
  5421. fmt)
  5422. ;; find where this list begins
  5423. (org-beginning-of-item-list)
  5424. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5425. (setq fmt (concat "%d" (match-string 1)))
  5426. (beginning-of-line 0)
  5427. ;; walk forward and replace these numbers
  5428. (catch 'exit
  5429. (while t
  5430. (catch 'next
  5431. (beginning-of-line 2)
  5432. (if (eobp) (throw 'exit nil))
  5433. (if (looking-at "[ \t]*$") (throw 'next nil))
  5434. (skip-chars-forward " \t") (setq ind1 (current-column))
  5435. (if (> ind1 ind) (throw 'next t))
  5436. (if (< ind1 ind) (throw 'exit t))
  5437. (if (not (org-at-item-p)) (throw 'exit nil))
  5438. (delete-region (match-beginning 2) (match-end 2))
  5439. (goto-char (match-beginning 2))
  5440. (insert (format fmt (setq n (1+ n)))))))
  5441. (goto-line line)
  5442. (move-to-column col)))
  5443. (defun org-fix-bullet-type (arg)
  5444. "Make sure all items in this list have the same bullet."
  5445. (interactive "p")
  5446. (unless (org-at-item-p) (error "This is not a list"))
  5447. (let ((line (org-current-line))
  5448. (col (current-column))
  5449. (ind (current-indentation))
  5450. ind1 bullet)
  5451. ;; find where this list begins
  5452. (org-beginning-of-item-list)
  5453. (beginning-of-line 1)
  5454. ;; find out what the bullet type is
  5455. (looking-at "[ \t]*\\(\\S-+\\)")
  5456. (setq bullet (match-string 1))
  5457. ;; walk forward and replace these numbers
  5458. (beginning-of-line 0)
  5459. (catch 'exit
  5460. (while t
  5461. (catch 'next
  5462. (beginning-of-line 2)
  5463. (if (eobp) (throw 'exit nil))
  5464. (if (looking-at "[ \t]*$") (throw 'next nil))
  5465. (skip-chars-forward " \t") (setq ind1 (current-column))
  5466. (if (> ind1 ind) (throw 'next t))
  5467. (if (< ind1 ind) (throw 'exit t))
  5468. (if (not (org-at-item-p)) (throw 'exit nil))
  5469. (skip-chars-forward " \t")
  5470. (looking-at "\\S-+")
  5471. (replace-match bullet))))
  5472. (goto-line line)
  5473. (move-to-column col)
  5474. (if (string-match "[0-9]" bullet)
  5475. (org-renumber-ordered-list 1))))
  5476. (defun org-beginning-of-item-list ()
  5477. "Go to the beginning of the current item list.
  5478. I.e. to the first item in this list."
  5479. (interactive)
  5480. (org-beginning-of-item)
  5481. (let ((pos (point-at-bol))
  5482. (ind (org-get-indentation))
  5483. ind1)
  5484. ;; find where this list begins
  5485. (catch 'exit
  5486. (while t
  5487. (catch 'next
  5488. (beginning-of-line 0)
  5489. (if (looking-at "[ \t]*$") (throw 'next t))
  5490. (skip-chars-forward " \t") (setq ind1 (current-column))
  5491. (if (or (< ind1 ind)
  5492. (and (= ind1 ind)
  5493. (not (org-at-item-p))))
  5494. (throw 'exit t)
  5495. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5496. (goto-char pos)))
  5497. (defvar org-last-indent-begin-marker (make-marker))
  5498. (defvar org-last-indent-end-marker (make-marker))
  5499. (defun org-outdent-item (arg)
  5500. "Outdent a local list item."
  5501. (interactive "p")
  5502. (org-indent-item (- arg)))
  5503. (defun org-indent-item (arg)
  5504. "Indent a local list item."
  5505. (interactive "p")
  5506. (unless (org-at-item-p)
  5507. (error "Not on an item"))
  5508. (save-excursion
  5509. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5510. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5511. (setq beg org-last-indent-begin-marker
  5512. end org-last-indent-end-marker)
  5513. (org-beginning-of-item)
  5514. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5515. (org-end-of-item)
  5516. (setq end (move-marker org-last-indent-end-marker (point))))
  5517. (goto-char beg)
  5518. (setq tmp (org-item-indent-positions)
  5519. ind (car tmp)
  5520. ind-down (nth 2 tmp)
  5521. ind-up (nth 1 tmp)
  5522. delta (if (> arg 0)
  5523. (if ind-down (- ind-down ind) (+ 2 ind))
  5524. (if ind-up (- ind-up ind) (- ind 2))))
  5525. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5526. (while (< (point) end)
  5527. (beginning-of-line 1)
  5528. (skip-chars-forward " \t") (setq ind1 (current-column))
  5529. (delete-region (point-at-bol) (point))
  5530. (or (eolp) (indent-to-column (+ ind1 delta)))
  5531. (beginning-of-line 2))))
  5532. (org-maybe-renumber-ordered-list-safe)
  5533. (save-excursion
  5534. (beginning-of-line 0)
  5535. (condition-case nil (org-beginning-of-item) (error nil))
  5536. (org-maybe-renumber-ordered-list-safe)))
  5537. (defun org-item-indent-positions ()
  5538. "Assumes cursor in item line. FIXME"
  5539. (let* ((bolpos (point-at-bol))
  5540. (ind (org-get-indentation))
  5541. ind-down ind-up pos)
  5542. (save-excursion
  5543. (org-beginning-of-item-list)
  5544. (skip-chars-backward "\n\r \t")
  5545. (when (org-in-item-p)
  5546. (org-beginning-of-item)
  5547. (setq ind-up (org-get-indentation))))
  5548. (setq pos (point))
  5549. (save-excursion
  5550. (cond
  5551. ((and (condition-case nil (progn (org-previous-item) t)
  5552. (error nil))
  5553. (or (forward-char 1) t)
  5554. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5555. (setq ind-down (org-get-indentation)))
  5556. ((and (goto-char pos)
  5557. (org-at-item-p))
  5558. (goto-char (match-end 0))
  5559. (skip-chars-forward " \t")
  5560. (setq ind-down (current-column)))))
  5561. (list ind ind-up ind-down)))
  5562. ;;; The orgstruct minor mode
  5563. ;; Define a minor mode which can be used in other modes in order to
  5564. ;; integrate the org-mode structure editing commands.
  5565. ;; This is really a hack, because the org-mode structure commands use
  5566. ;; keys which normally belong to the major mode. Here is how it
  5567. ;; works: The minor mode defines all the keys necessary to operate the
  5568. ;; structure commands, but wraps the commands into a function which
  5569. ;; tests if the cursor is currently at a headline or a plain list
  5570. ;; item. If that is the case, the structure command is used,
  5571. ;; temporarily setting many Org-mode variables like regular
  5572. ;; expressions for filling etc. However, when any of those keys is
  5573. ;; used at a different location, function uses `key-binding' to look
  5574. ;; up if the key has an associated command in another currently active
  5575. ;; keymap (minor modes, major mode, global), and executes that
  5576. ;; command. There might be problems if any of the keys is otherwise
  5577. ;; used as a prefix key.
  5578. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5579. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5580. ;; addresses this by checking explicitly for both bindings.
  5581. (defvar orgstruct-mode-map (make-sparse-keymap)
  5582. "Keymap for the minor `org-cdlatex-mode'.")
  5583. ;;;###autoload
  5584. (define-minor-mode orgstruct-mode
  5585. "Toggle the minor more `orgstruct-mode'.
  5586. This mode is for using Org-mode structure commands in other modes.
  5587. The following key behave as if Org-mode was active, if the cursor
  5588. is on a headline, or on a plain list item (both in the definition
  5589. of Org-mode).
  5590. M-up Move entry/item up
  5591. M-down Move entry/item down
  5592. M-left Promote
  5593. M-right Demote
  5594. M-S-up Move entry/item up
  5595. M-S-down Move entry/item down
  5596. M-S-left Promote subtree
  5597. M-S-right Demote subtree
  5598. M-q Fill paragraph and items like in Org-mode
  5599. C-c ^ Sort entries
  5600. C-c - Cycle list bullet
  5601. TAB Cycle item visibility
  5602. M-RET Insert new heading/item
  5603. S-M-RET Insert new TODO heading / Chekbox item
  5604. C-c C-c Set tags / toggle checkbox"
  5605. nil " OrgStruct" nil
  5606. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5607. ;;;###autoload
  5608. (defun turn-on-orgstruct ()
  5609. "Unconditionally turn on `orgstruct-mode'."
  5610. (orgstruct-mode 1))
  5611. (defun orgstruct-error ()
  5612. "Error when there is no default binding for a structure key."
  5613. (interactive)
  5614. (error "This key is has no function outside structure elements"))
  5615. (defvar org-local-vars nil
  5616. "List of local variables, for use by `orgstruct-mode'")
  5617. (defun orgstruct-setup ()
  5618. "Setup orgstruct keymaps."
  5619. (let ((nfunc 0)
  5620. (bindings
  5621. (list
  5622. '([(meta up)] org-metaup)
  5623. '([(meta down)] org-metadown)
  5624. '([(meta left)] org-metaleft)
  5625. '([(meta right)] org-metaright)
  5626. '([(meta shift up)] org-shiftmetaup)
  5627. '([(meta shift down)] org-shiftmetadown)
  5628. '([(meta shift left)] org-shiftmetaleft)
  5629. '([(meta shift right)] org-shiftmetaright)
  5630. '([(shift up)] org-shiftup)
  5631. '([(shift down)] org-shiftdown)
  5632. '("\M-q" fill-paragraph)
  5633. '("\C-c^" org-sort)
  5634. '("\C-c-" org-cycle-list-bullet)))
  5635. elt key fun cmd)
  5636. (while (setq elt (pop bindings))
  5637. (setq nfunc (1+ nfunc))
  5638. (setq key (org-key (car elt))
  5639. fun (nth 1 elt)
  5640. cmd (orgstruct-make-binding fun nfunc key))
  5641. (org-defkey orgstruct-mode-map key cmd))
  5642. ;; Special treatment needed for TAB and RET
  5643. (org-defkey orgstruct-mode-map [(tab)]
  5644. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5645. (org-defkey orgstruct-mode-map "\C-i"
  5646. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5647. (org-defkey orgstruct-mode-map "\M-\C-m"
  5648. (orgstruct-make-binding 'org-insert-heading 105
  5649. "\M-\C-m" [(meta return)]))
  5650. (org-defkey orgstruct-mode-map [(meta return)]
  5651. (orgstruct-make-binding 'org-insert-heading 106
  5652. [(meta return)] "\M-\C-m"))
  5653. (org-defkey orgstruct-mode-map [(shift meta return)]
  5654. (orgstruct-make-binding 'org-insert-todo-heading 107
  5655. [(meta return)] "\M-\C-m"))
  5656. (org-defkey orgstruct-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  5657. (setq org-local-vars (org-get-local-variables))
  5658. t))
  5659. (defun orgstruct-make-binding (fun n &rest keys)
  5660. "Create a function for binding in the structure minor mode.
  5661. FUN is the command to call inside a table. N is used to create a unique
  5662. command name. KEYS are keys that should be checked in for a command
  5663. to execute outside of tables."
  5664. (eval
  5665. (list 'defun
  5666. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5667. '(arg)
  5668. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5669. "Outside of structure, run the binding of `"
  5670. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5671. "'.")
  5672. '(interactive "p")
  5673. (list 'if
  5674. '(org-context-p 'headline 'item)
  5675. (list 'org-run-like-in-org-mode (list 'quote fun))
  5676. (list 'let '(orgstruct-mode)
  5677. (list 'call-interactively
  5678. (append '(or)
  5679. (mapcar (lambda (k)
  5680. (list 'key-binding k))
  5681. keys)
  5682. '('orgstruct-error))))))))
  5683. (defun org-context-p (&rest contexts)
  5684. "FIXME:"
  5685. (let ((pos (point)))
  5686. (goto-char (point-at-bol))
  5687. (prog1 (or (and (memq 'table contexts)
  5688. (looking-at "[ \t]*|"))
  5689. (and (memq 'headline contexts)
  5690. (looking-at "\\*+"))
  5691. (and (memq 'item contexts)
  5692. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5693. (goto-char pos))))
  5694. (defun org-get-local-variables ()
  5695. "Return a list of all local variables in an org-mode buffer."
  5696. (let (varlist)
  5697. (with-current-buffer (get-buffer-create "*Org tmp*")
  5698. (erase-buffer)
  5699. (org-mode)
  5700. (setq varlist (buffer-local-variables)))
  5701. (kill-buffer "*Org tmp*")
  5702. (delq nil
  5703. (mapcar
  5704. (lambda (x)
  5705. (setq x
  5706. (if (symbolp x)
  5707. (list x)
  5708. (list (car x) (list 'quote (cdr x)))))
  5709. (if (string-match
  5710. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5711. (symbol-name (car x)))
  5712. x nil))
  5713. varlist))))
  5714. (defun org-run-like-in-org-mode (cmd)
  5715. (eval (list 'let org-local-vars
  5716. (list 'call-interactively (list 'quote cmd)))))
  5717. ;;;; Archiving
  5718. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5719. (defun org-archive-subtree (&optional find-done)
  5720. "Move the current subtree to the archive.
  5721. The archive can be a certain top-level heading in the current file, or in
  5722. a different file. The tree will be moved to that location, the subtree
  5723. heading be marked DONE, and the current time will be added.
  5724. When called with prefix argument FIND-DONE, find whole trees without any
  5725. open TODO items and archive them (after getting confirmation from the user).
  5726. If the cursor is not at a headline when this comand is called, try all level
  5727. 1 trees. If the cursor is on a headline, only try the direct children of
  5728. this heading."
  5729. (interactive "P")
  5730. (if find-done
  5731. (org-archive-all-done)
  5732. ;; Save all relevant TODO keyword-relatex variables
  5733. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5734. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5735. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5736. (tr-org-done-keywords org-done-keywords)
  5737. (tr-org-todo-regexp org-todo-regexp)
  5738. (tr-org-todo-line-regexp org-todo-line-regexp)
  5739. (tr-org-odd-levels-only org-odd-levels-only)
  5740. (this-buffer (current-buffer))
  5741. (org-archive-location org-archive-location)
  5742. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5743. file heading buffer level newfile-p)
  5744. ;; Try to find a local archive location
  5745. (save-excursion
  5746. (save-restriction
  5747. (widen)
  5748. (if (or (re-search-backward re nil t) (re-search-forward re nil t))
  5749. (setq org-archive-location (match-string 1)))))
  5750. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5751. (progn
  5752. (setq file (format (match-string 1 org-archive-location)
  5753. (file-name-nondirectory buffer-file-name))
  5754. heading (match-string 2 org-archive-location)))
  5755. (error "Invalid `org-archive-location'"))
  5756. (if (> (length file) 0)
  5757. (setq newfile-p (not (file-exists-p file))
  5758. buffer (find-file-noselect file))
  5759. (setq buffer (current-buffer)))
  5760. (unless buffer
  5761. (error "Cannot access file \"%s\"" file))
  5762. (if (and (> (length heading) 0)
  5763. (string-match "^\\*+" heading))
  5764. (setq level (match-end 0))
  5765. (setq heading nil level 0))
  5766. (save-excursion
  5767. ;; We first only copy, in case something goes wrong
  5768. ;; we need to protect this-command, to avoid kill-region sets it,
  5769. ;; which would lead to duplication of subtrees
  5770. (let (this-command) (org-copy-subtree))
  5771. (set-buffer buffer)
  5772. ;; Enforce org-mode for the archive buffer
  5773. (if (not (org-mode-p))
  5774. ;; Force the mode for future visits.
  5775. (let ((org-insert-mode-line-in-empty-file t)
  5776. (org-inhibit-startup t))
  5777. (call-interactively 'org-mode)))
  5778. (when newfile-p
  5779. (goto-char (point-max))
  5780. (insert (format "\nArchived entries from file %s\n\n"
  5781. (buffer-file-name this-buffer))))
  5782. ;; Force the TODO keywords of the original buffer
  5783. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5784. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5785. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5786. (org-done-keywords tr-org-done-keywords)
  5787. (org-todo-regexp tr-org-todo-regexp)
  5788. (org-todo-line-regexp tr-org-todo-line-regexp)
  5789. (org-odd-levels-only
  5790. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5791. org-odd-levels-only
  5792. tr-org-odd-levels-only)))
  5793. (goto-char (point-min))
  5794. (if heading
  5795. (progn
  5796. (if (re-search-forward
  5797. (concat "^" (regexp-quote heading)
  5798. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5799. nil t)
  5800. (goto-char (match-end 0))
  5801. ;; Heading not found, just insert it at the end
  5802. (goto-char (point-max))
  5803. (or (bolp) (insert "\n"))
  5804. (insert "\n" heading "\n")
  5805. (end-of-line 0))
  5806. ;; Make the subtree visible
  5807. (show-subtree)
  5808. (org-end-of-subtree t)
  5809. (skip-chars-backward " \t\r\n")
  5810. (and (looking-at "[ \t\r\n]*")
  5811. (replace-match "\n\n")))
  5812. ;; No specific heading, just go to end of file.
  5813. (goto-char (point-max)) (insert "\n"))
  5814. ;; Paste
  5815. (org-paste-subtree (org-get-legal-level level 1))
  5816. ;; Mark the entry as done, i.e. set to last word in org-todo-keywords-1 FIXME: not right anymore!!!!!!!
  5817. (when (and org-archive-mark-done
  5818. (looking-at org-todo-line-regexp)
  5819. (or (not (match-end 3))
  5820. (not (member (match-string 3) org-done-keywords))))
  5821. (let (org-log-done)
  5822. (org-todo (car org-done-keywords))))
  5823. ;; Move cursor to right after the TODO keyword
  5824. (when org-archive-stamp-time
  5825. (org-add-planning-info 'archived (org-current-time)))
  5826. ;; Save the buffer, if it is not the same buffer.
  5827. (if (not (eq this-buffer buffer)) (save-buffer))))
  5828. ;; Here we are back in the original buffer. Everything seems to have
  5829. ;; worked. So now cut the tree and finish up.
  5830. (let (this-command) (org-cut-subtree))
  5831. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5832. (message "Subtree archived %s"
  5833. (if (eq this-buffer buffer)
  5834. (concat "under heading: " heading)
  5835. (concat "in file: " (abbreviate-file-name file)))))))
  5836. (defun org-archive-all-done (&optional tag)
  5837. "Archive sublevels of the current tree without open TODO items.
  5838. If the cursor is not on a headline, try all level 1 trees. If
  5839. it is on a headline, try all direct children.
  5840. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5841. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5842. (rea (concat ".*:" org-archive-tag ":"))
  5843. (begm (make-marker))
  5844. (endm (make-marker))
  5845. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5846. "Move subtree to archive (no open TODO items)? "))
  5847. beg end (cntarch 0))
  5848. (if (org-on-heading-p)
  5849. (progn
  5850. (setq re1 (concat "^" (regexp-quote
  5851. (make-string
  5852. (1+ (- (match-end 0) (match-beginning 0)))
  5853. ?*))
  5854. " "))
  5855. (move-marker begm (point))
  5856. (move-marker endm (org-end-of-subtree t)))
  5857. (setq re1 "^* ")
  5858. (move-marker begm (point-min))
  5859. (move-marker endm (point-max)))
  5860. (save-excursion
  5861. (goto-char begm)
  5862. (while (re-search-forward re1 endm t)
  5863. (setq beg (match-beginning 0)
  5864. end (save-excursion (org-end-of-subtree t) (point)))
  5865. (goto-char beg)
  5866. (if (re-search-forward re end t)
  5867. (goto-char end)
  5868. (goto-char beg)
  5869. (if (and (or (not tag) (not (looking-at rea)))
  5870. (y-or-n-p question))
  5871. (progn
  5872. (if tag
  5873. (org-toggle-tag org-archive-tag 'on)
  5874. (org-archive-subtree))
  5875. (setq cntarch (1+ cntarch)))
  5876. (goto-char end)))))
  5877. (message "%d trees archived" cntarch)))
  5878. (defun org-cycle-hide-drawers (state)
  5879. "Re-hide all archived subtrees after a visibility state change."
  5880. (when (not (memq state '(overview folded)))
  5881. (save-excursion
  5882. (let* ((globalp (memq state '(contents all)))
  5883. (beg (if globalp (point-min) (point)))
  5884. (end (if globalp (point-max) (org-end-of-subtree t))))
  5885. (goto-char beg)
  5886. (while (re-search-forward org-drawer-regexp end t)
  5887. (org-flag-drawer t))))))
  5888. (defun org-flag-drawer (flag)
  5889. (save-excursion
  5890. (beginning-of-line 1)
  5891. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5892. (let ((b (match-end 0)))
  5893. (if (re-search-forward
  5894. "^[ \t]*:END:"
  5895. (save-excursion (outline-next-heading) (point)) t)
  5896. (outline-flag-region b (point-at-eol) flag)
  5897. (error ":END: line missing"))))))
  5898. (defun org-cycle-hide-archived-subtrees (state)
  5899. "Re-hide all archived subtrees after a visibility state change."
  5900. (when (and (not org-cycle-open-archived-trees)
  5901. (not (memq state '(overview folded))))
  5902. (save-excursion
  5903. (let* ((globalp (memq state '(contents all)))
  5904. (beg (if globalp (point-min) (point)))
  5905. (end (if globalp (point-max) (org-end-of-subtree t))))
  5906. (org-hide-archived-subtrees beg end)
  5907. (goto-char beg)
  5908. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5909. (message (substitute-command-keys
  5910. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5911. (defun org-force-cycle-archived ()
  5912. "Cycle subtree even if it is archived."
  5913. (interactive)
  5914. (setq this-command 'org-cycle)
  5915. (let ((org-cycle-open-archived-trees t))
  5916. (call-interactively 'org-cycle)))
  5917. (defun org-hide-archived-subtrees (beg end)
  5918. "Re-hide all archived subtrees after a visibility state change."
  5919. (save-excursion
  5920. (let* ((re (concat ":" org-archive-tag ":")))
  5921. (goto-char beg)
  5922. (while (re-search-forward re end t)
  5923. (and (org-on-heading-p) (hide-subtree))
  5924. (org-end-of-subtree t)))))
  5925. (defun org-toggle-tag (tag &optional onoff)
  5926. "Toggle the tag TAG for the current line.
  5927. If ONOFF is `on' or `off', don't toggle but set to this state."
  5928. (unless (org-on-heading-p t) (error "Not on headling"))
  5929. (let (res current)
  5930. (save-excursion
  5931. (beginning-of-line)
  5932. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  5933. (point-at-eol) t)
  5934. (progn
  5935. (setq current (match-string 1))
  5936. (replace-match ""))
  5937. (setq current ""))
  5938. (setq current (nreverse (org-split-string current ":")))
  5939. (cond
  5940. ((eq onoff 'on)
  5941. (setq res t)
  5942. (or (member tag current) (push tag current)))
  5943. ((eq onoff 'off)
  5944. (or (not (member tag current)) (setq current (delete tag current))))
  5945. (t (if (member tag current)
  5946. (setq current (delete tag current))
  5947. (setq res t)
  5948. (push tag current))))
  5949. (end-of-line 1)
  5950. (when current
  5951. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  5952. (org-set-tags nil t))
  5953. res))
  5954. (defun org-toggle-archive-tag (&optional arg)
  5955. "Toggle the archive tag for the current headline.
  5956. With prefix ARG, check all children of current headline and offer tagging
  5957. the children that do not contain any open TODO items."
  5958. (interactive "P")
  5959. (if arg
  5960. (org-archive-all-done 'tag)
  5961. (let (set)
  5962. (save-excursion
  5963. (org-back-to-heading t)
  5964. (setq set (org-toggle-tag org-archive-tag))
  5965. (when set (hide-subtree)))
  5966. (and set (beginning-of-line 1))
  5967. (message "Subtree %s" (if set "archived" "unarchived")))))
  5968. ;;;; Tables
  5969. ;;; The table editor
  5970. ;; Watch out: Here we are talking about two different kind of tables.
  5971. ;; Most of the code is for the tables created with the Org-mode table editor.
  5972. ;; Sometimes, we talk about tables created and edited with the table.el
  5973. ;; Emacs package. We call the former org-type tables, and the latter
  5974. ;; table.el-type tables.
  5975. (defun org-before-change-function (beg end)
  5976. "Every change indicates that a table might need an update."
  5977. (setq org-table-may-need-update t))
  5978. (defconst org-table-line-regexp "^[ \t]*|"
  5979. "Detects an org-type table line.")
  5980. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  5981. "Detects an org-type table line.")
  5982. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  5983. "Detects a table line marked for automatic recalculation.")
  5984. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  5985. "Detects a table line marked for automatic recalculation.")
  5986. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  5987. "Detects a table line marked for automatic recalculation.")
  5988. (defconst org-table-hline-regexp "^[ \t]*|-"
  5989. "Detects an org-type table hline.")
  5990. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  5991. "Detects a table-type table hline.")
  5992. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  5993. "Detects an org-type or table-type table.")
  5994. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  5995. "Searching from within a table (any type) this finds the first line
  5996. outside the table.")
  5997. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  5998. "Searching from within a table (any type) this finds the first line
  5999. outside the table.")
  6000. (defvar org-table-last-highlighted-reference nil)
  6001. (defvar org-table-formula-history nil)
  6002. (defvar org-table-column-names nil
  6003. "Alist with column names, derived from the `!' line.")
  6004. (defvar org-table-column-name-regexp nil
  6005. "Regular expression matching the current column names.")
  6006. (defvar org-table-local-parameters nil
  6007. "Alist with parameter names, derived from the `$' line.")
  6008. (defvar org-table-named-field-locations nil
  6009. "Alist with locations of named fields.")
  6010. (defvar org-table-current-line-types nil
  6011. "Table row types, non-nil only for the duration of a comand.")
  6012. (defvar org-table-current-begin-line nil
  6013. "Table begin line, non-nil only for the duration of a comand.")
  6014. (defvar org-table-current-begin-pos nil
  6015. "Table begin position, non-nil only for the duration of a comand.")
  6016. (defvar org-table-dlines nil
  6017. "Vector of data line line numbers in the current table.")
  6018. (defvar org-table-hlines nil
  6019. "Vector of hline line numbers in the current table.")
  6020. (defconst org-table-range-regexp
  6021. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6022. ;; 1 2 3 4 5
  6023. "Regular expression for matching ranges in formulas.")
  6024. (defconst org-table-range-regexp2
  6025. (concat
  6026. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6027. "\\.\\."
  6028. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6029. "Match a range for reference display.")
  6030. (defconst org-table-translate-regexp
  6031. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6032. "Match a reference that needs translation, for reference display.")
  6033. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6034. (defun org-table-create-with-table.el ()
  6035. "Use the table.el package to insert a new table.
  6036. If there is already a table at point, convert between Org-mode tables
  6037. and table.el tables."
  6038. (interactive)
  6039. (require 'table)
  6040. (cond
  6041. ((org-at-table.el-p)
  6042. (if (y-or-n-p "Convert table to Org-mode table? ")
  6043. (org-table-convert)))
  6044. ((org-at-table-p)
  6045. (if (y-or-n-p "Convert table to table.el table? ")
  6046. (org-table-convert)))
  6047. (t (call-interactively 'table-insert))))
  6048. (defun org-table-create-or-convert-from-region (arg)
  6049. "Convert region to table, or create an empty table.
  6050. If there is an active region, convert it to a table, using the function
  6051. `org-table-convert-region'.
  6052. If there is no such region, create an empty table with `org-table-create'."
  6053. (interactive "P")
  6054. (if (org-region-active-p)
  6055. (org-table-convert-region (region-beginning) (region-end) arg)
  6056. (org-table-create arg)))
  6057. (defun org-table-create (&optional size)
  6058. "Query for a size and insert a table skeleton.
  6059. SIZE is a string Columns x Rows like for example \"3x2\"."
  6060. (interactive "P")
  6061. (unless size
  6062. (setq size (read-string
  6063. (concat "Table size Columns x Rows [e.g. "
  6064. org-table-default-size "]: ")
  6065. "" nil org-table-default-size)))
  6066. (let* ((pos (point))
  6067. (indent (make-string (current-column) ?\ ))
  6068. (split (org-split-string size " *x *"))
  6069. (rows (string-to-number (nth 1 split)))
  6070. (columns (string-to-number (car split)))
  6071. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6072. "\n")))
  6073. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6074. (point-at-bol) (point)))
  6075. (beginning-of-line 1)
  6076. (newline))
  6077. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6078. (dotimes (i rows) (insert line))
  6079. (goto-char pos)
  6080. (if (> rows 1)
  6081. ;; Insert a hline after the first row.
  6082. (progn
  6083. (end-of-line 1)
  6084. (insert "\n|-")
  6085. (goto-char pos)))
  6086. (org-table-align)))
  6087. (defun org-table-convert-region (beg0 end0 &optional nspace)
  6088. "Convert region to a table.
  6089. The region goes from BEG0 to END0, but these borders will be moved
  6090. slightly, to make sure a beginning of line in the first line is included.
  6091. When NSPACE is non-nil, it indicates the minimum number of spaces that
  6092. separate columns. By default, the function first checks if every line
  6093. contains at lease one TAB. If yes, it assumes that the material is TAB
  6094. separated. If not, it assumes a single space as separator."
  6095. (interactive "rP")
  6096. (let* ((beg (min beg0 end0))
  6097. (end (max beg0 end0))
  6098. (tabsep t)
  6099. re)
  6100. (goto-char beg)
  6101. (beginning-of-line 1)
  6102. (setq beg (move-marker (make-marker) (point)))
  6103. (goto-char end)
  6104. (if (bolp) (backward-char 1) (end-of-line 1))
  6105. (setq end (move-marker (make-marker) (point)))
  6106. ;; Lets see if this is tab-separated material. If every nonempty line
  6107. ;; contains a tab, we will assume that it is tab-separated material
  6108. (if nspace
  6109. (setq tabsep nil)
  6110. (goto-char beg)
  6111. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  6112. (if nspace (setq tabsep nil))
  6113. (if tabsep
  6114. (setq re "^\\|\t")
  6115. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  6116. (max 1 (prefix-numeric-value nspace)))))
  6117. (goto-char beg)
  6118. (while (re-search-forward re end t)
  6119. (replace-match "| " t t))
  6120. (goto-char beg)
  6121. (insert " ")
  6122. (org-table-align)))
  6123. (defun org-table-import (file arg)
  6124. "Import FILE as a table.
  6125. The file is assumed to be tab-separated. Such files can be produced by most
  6126. spreadsheet and database applications. If no tabs (at least one per line)
  6127. are found, lines will be split on whitespace into fields."
  6128. (interactive "f\nP")
  6129. (or (bolp) (newline))
  6130. (let ((beg (point))
  6131. (pm (point-max)))
  6132. (insert-file-contents file)
  6133. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  6134. (defun org-table-export ()
  6135. "Export table as a tab-separated file.
  6136. Such a file can be imported into a spreadsheet program like Excel."
  6137. (interactive)
  6138. (let* ((beg (org-table-begin))
  6139. (end (org-table-end))
  6140. (table (buffer-substring beg end))
  6141. (file (read-file-name "Export table to: "))
  6142. buf)
  6143. (unless (or (not (file-exists-p file))
  6144. (y-or-n-p (format "Overwrite file %s? " file)))
  6145. (error "Abort"))
  6146. (with-current-buffer (find-file-noselect file)
  6147. (setq buf (current-buffer))
  6148. (erase-buffer)
  6149. (fundamental-mode)
  6150. (insert table)
  6151. (goto-char (point-min))
  6152. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  6153. (replace-match "" t t)
  6154. (end-of-line 1))
  6155. (goto-char (point-min))
  6156. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  6157. (replace-match "" t t)
  6158. (goto-char (min (1+ (point)) (point-max))))
  6159. (goto-char (point-min))
  6160. (while (re-search-forward "^-[-+]*$" nil t)
  6161. (replace-match "")
  6162. (if (looking-at "\n")
  6163. (delete-char 1)))
  6164. (goto-char (point-min))
  6165. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  6166. (replace-match "\t" t t))
  6167. (save-buffer))
  6168. (kill-buffer buf)))
  6169. (defvar org-table-aligned-begin-marker (make-marker)
  6170. "Marker at the beginning of the table last aligned.
  6171. Used to check if cursor still is in that table, to minimize realignment.")
  6172. (defvar org-table-aligned-end-marker (make-marker)
  6173. "Marker at the end of the table last aligned.
  6174. Used to check if cursor still is in that table, to minimize realignment.")
  6175. (defvar org-table-last-alignment nil
  6176. "List of flags for flushright alignment, from the last re-alignment.
  6177. This is being used to correctly align a single field after TAB or RET.")
  6178. (defvar org-table-last-column-widths nil
  6179. "List of max width of fields in each column.
  6180. This is being used to correctly align a single field after TAB or RET.")
  6181. (defvar org-table-overlay-coordinates nil
  6182. "Overlay coordinates after each align of a table.")
  6183. (make-variable-buffer-local 'org-table-overlay-coordinates)
  6184. (defvar org-last-recalc-line nil)
  6185. (defconst org-narrow-column-arrow "=>"
  6186. "Used as display property in narrowed table columns.")
  6187. (defun org-table-align ()
  6188. "Align the table at point by aligning all vertical bars."
  6189. (interactive)
  6190. (let* (
  6191. ;; Limits of table
  6192. (beg (org-table-begin))
  6193. (end (org-table-end))
  6194. ;; Current cursor position
  6195. (linepos (org-current-line))
  6196. (colpos (org-table-current-column))
  6197. (winstart (window-start))
  6198. (winstartline (org-current-line (min winstart (1- (point-max)))))
  6199. lines (new "") lengths l typenums ty fields maxfields i
  6200. column
  6201. (indent "") cnt frac
  6202. rfmt hfmt
  6203. (spaces '(1 . 1))
  6204. (sp1 (car spaces))
  6205. (sp2 (cdr spaces))
  6206. (rfmt1 (concat
  6207. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  6208. (hfmt1 (concat
  6209. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  6210. emptystrings links dates narrow fmax f1 len c e)
  6211. (untabify beg end)
  6212. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  6213. ;; Check if we have links or dates
  6214. (goto-char beg)
  6215. (setq links (re-search-forward org-bracket-link-regexp end t))
  6216. (goto-char beg)
  6217. (setq dates (and org-display-custom-times
  6218. (re-search-forward org-ts-regexp-both end t)))
  6219. ;; Make sure the link properties are right
  6220. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  6221. ;; Make sure the date properties are right
  6222. (when dates (goto-char beg) (while (org-activate-dates end)))
  6223. ;; Check if we are narrowing any columns
  6224. (goto-char beg)
  6225. (setq narrow (and org-format-transports-properties-p
  6226. (re-search-forward "<[0-9]+>" end t)))
  6227. ;; Get the rows
  6228. (setq lines (org-split-string
  6229. (buffer-substring beg end) "\n"))
  6230. ;; Store the indentation of the first line
  6231. (if (string-match "^ *" (car lines))
  6232. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  6233. ;; Mark the hlines by setting the corresponding element to nil
  6234. ;; At the same time, we remove trailing space.
  6235. (setq lines (mapcar (lambda (l)
  6236. (if (string-match "^ *|-" l)
  6237. nil
  6238. (if (string-match "[ \t]+$" l)
  6239. (substring l 0 (match-beginning 0))
  6240. l)))
  6241. lines))
  6242. ;; Get the data fields by splitting the lines.
  6243. (setq fields (mapcar
  6244. (lambda (l)
  6245. (org-split-string l " *| *"))
  6246. (delq nil (copy-sequence lines))))
  6247. ;; How many fields in the longest line?
  6248. (condition-case nil
  6249. (setq maxfields (apply 'max (mapcar 'length fields)))
  6250. (error
  6251. (kill-region beg end)
  6252. (org-table-create org-table-default-size)
  6253. (error "Empty table - created default table")))
  6254. ;; A list of empty strings to fill any short rows on output
  6255. (setq emptystrings (make-list maxfields ""))
  6256. ;; Check for special formatting.
  6257. (setq i -1)
  6258. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  6259. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  6260. ;; Check if there is an explicit width specified
  6261. (when narrow
  6262. (setq c column fmax nil)
  6263. (while c
  6264. (setq e (pop c))
  6265. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  6266. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  6267. ;; Find fields that are wider than fmax, and shorten them
  6268. (when fmax
  6269. (loop for xx in column do
  6270. (when (and (stringp xx)
  6271. (> (org-string-width xx) fmax))
  6272. (org-add-props xx nil
  6273. 'help-echo
  6274. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  6275. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  6276. (unless (> f1 1)
  6277. (error "Cannot narrow field starting with wide link \"%s\""
  6278. (match-string 0 xx)))
  6279. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  6280. (add-text-properties (- f1 2) f1
  6281. (list 'display org-narrow-column-arrow)
  6282. xx)))))
  6283. ;; Get the maximum width for each column
  6284. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  6285. ;; Get the fraction of numbers, to decide about alignment of the column
  6286. (setq cnt 0 frac 0.0)
  6287. (loop for x in column do
  6288. (if (equal x "")
  6289. nil
  6290. (setq frac ( / (+ (* frac cnt)
  6291. (if (string-match org-table-number-regexp x) 1 0))
  6292. (setq cnt (1+ cnt))))))
  6293. (push (>= frac org-table-number-fraction) typenums))
  6294. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  6295. ;; Store the alignment of this table, for later editing of single fields
  6296. (setq org-table-last-alignment typenums
  6297. org-table-last-column-widths lengths)
  6298. ;; With invisible characters, `format' does not get the field width right
  6299. ;; So we need to make these fields wide by hand.
  6300. (when links
  6301. (loop for i from 0 upto (1- maxfields) do
  6302. (setq len (nth i lengths))
  6303. (loop for j from 0 upto (1- (length fields)) do
  6304. (setq c (nthcdr i (car (nthcdr j fields))))
  6305. (if (and (stringp (car c))
  6306. (string-match org-bracket-link-regexp (car c))
  6307. (< (org-string-width (car c)) len))
  6308. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  6309. ;; Compute the formats needed for output of the table
  6310. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  6311. (while (setq l (pop lengths))
  6312. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  6313. (setq rfmt (concat rfmt (format rfmt1 ty l))
  6314. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  6315. (setq rfmt (concat rfmt "\n")
  6316. hfmt (concat (substring hfmt 0 -1) "|\n"))
  6317. (setq new (mapconcat
  6318. (lambda (l)
  6319. (if l (apply 'format rfmt
  6320. (append (pop fields) emptystrings))
  6321. hfmt))
  6322. lines ""))
  6323. ;; Replace the old one
  6324. (delete-region beg end)
  6325. (move-marker end nil)
  6326. (move-marker org-table-aligned-begin-marker (point))
  6327. (insert new)
  6328. (move-marker org-table-aligned-end-marker (point))
  6329. (when (and orgtbl-mode (not (org-mode-p)))
  6330. (goto-char org-table-aligned-begin-marker)
  6331. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  6332. ;; Try to move to the old location
  6333. (goto-line winstartline)
  6334. (setq winstart (point-at-bol))
  6335. (goto-line linepos)
  6336. (set-window-start (selected-window) winstart 'noforce)
  6337. (org-table-goto-column colpos)
  6338. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  6339. (setq org-table-may-need-update nil)
  6340. ))
  6341. (defun org-string-width (s)
  6342. "Compute width of string, ignoring invisible characters.
  6343. This ignores character with invisibility property `org-link', and also
  6344. characters with property `org-cwidth', because these will become invisible
  6345. upon the next fontification round."
  6346. (let (b l)
  6347. (when (or (eq t buffer-invisibility-spec)
  6348. (assq 'org-link buffer-invisibility-spec))
  6349. (while (setq b (text-property-any 0 (length s)
  6350. 'invisible 'org-link s))
  6351. (setq s (concat (substring s 0 b)
  6352. (substring s (or (next-single-property-change
  6353. b 'invisible s) (length s)))))))
  6354. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  6355. (setq s (concat (substring s 0 b)
  6356. (substring s (or (next-single-property-change
  6357. b 'org-cwidth s) (length s))))))
  6358. (setq l (string-width s) b -1)
  6359. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  6360. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  6361. l))
  6362. (defun org-table-begin (&optional table-type)
  6363. "Find the beginning of the table and return its position.
  6364. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  6365. (save-excursion
  6366. (if (not (re-search-backward
  6367. (if table-type org-table-any-border-regexp
  6368. org-table-border-regexp)
  6369. nil t))
  6370. (progn (goto-char (point-min)) (point))
  6371. (goto-char (match-beginning 0))
  6372. (beginning-of-line 2)
  6373. (point))))
  6374. (defun org-table-end (&optional table-type)
  6375. "Find the end of the table and return its position.
  6376. With argument TABLE-TYPE, go to the end of a table.el-type table."
  6377. (save-excursion
  6378. (if (not (re-search-forward
  6379. (if table-type org-table-any-border-regexp
  6380. org-table-border-regexp)
  6381. nil t))
  6382. (goto-char (point-max))
  6383. (goto-char (match-beginning 0)))
  6384. (point-marker)))
  6385. (defun org-table-justify-field-maybe (&optional new)
  6386. "Justify the current field, text to left, number to right.
  6387. Optional argument NEW may specify text to replace the current field content."
  6388. (cond
  6389. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  6390. ((org-at-table-hline-p))
  6391. ((and (not new)
  6392. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  6393. (current-buffer)))
  6394. (< (point) org-table-aligned-begin-marker)
  6395. (>= (point) org-table-aligned-end-marker)))
  6396. ;; This is not the same table, force a full re-align
  6397. (setq org-table-may-need-update t))
  6398. (t ;; realign the current field, based on previous full realign
  6399. (let* ((pos (point)) s
  6400. (col (org-table-current-column))
  6401. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  6402. l f n o e)
  6403. (when (> col 0)
  6404. (skip-chars-backward "^|\n")
  6405. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  6406. (progn
  6407. (setq s (match-string 1)
  6408. o (match-string 0)
  6409. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  6410. e (not (= (match-beginning 2) (match-end 2))))
  6411. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  6412. l (if e "|" (setq org-table-may-need-update t) ""))
  6413. n (format f s))
  6414. (if new
  6415. (if (<= (length new) l) ;; FIXME: length -> str-width?
  6416. (setq n (format f new))
  6417. (setq n (concat new "|") org-table-may-need-update t)))
  6418. (or (equal n o)
  6419. (let (org-table-may-need-update)
  6420. (replace-match n))))
  6421. (setq org-table-may-need-update t))
  6422. (goto-char pos))))))
  6423. (defun org-table-next-field ()
  6424. "Go to the next field in the current table, creating new lines as needed.
  6425. Before doing so, re-align the table if necessary."
  6426. (interactive)
  6427. (org-table-maybe-eval-formula)
  6428. (org-table-maybe-recalculate-line)
  6429. (if (and org-table-automatic-realign
  6430. org-table-may-need-update)
  6431. (org-table-align))
  6432. (let ((end (org-table-end)))
  6433. (if (org-at-table-hline-p)
  6434. (end-of-line 1))
  6435. (condition-case nil
  6436. (progn
  6437. (re-search-forward "|" end)
  6438. (if (looking-at "[ \t]*$")
  6439. (re-search-forward "|" end))
  6440. (if (and (looking-at "-")
  6441. org-table-tab-jumps-over-hlines
  6442. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  6443. (goto-char (match-beginning 1)))
  6444. (if (looking-at "-")
  6445. (progn
  6446. (beginning-of-line 0)
  6447. (org-table-insert-row 'below))
  6448. (if (looking-at " ") (forward-char 1))))
  6449. (error
  6450. (org-table-insert-row 'below)))))
  6451. (defun org-table-previous-field ()
  6452. "Go to the previous field in the table.
  6453. Before doing so, re-align the table if necessary."
  6454. (interactive)
  6455. (org-table-justify-field-maybe)
  6456. (org-table-maybe-recalculate-line)
  6457. (if (and org-table-automatic-realign
  6458. org-table-may-need-update)
  6459. (org-table-align))
  6460. (if (org-at-table-hline-p)
  6461. (end-of-line 1))
  6462. (re-search-backward "|" (org-table-begin))
  6463. (re-search-backward "|" (org-table-begin))
  6464. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  6465. (re-search-backward "|" (org-table-begin)))
  6466. (if (looking-at "| ?")
  6467. (goto-char (match-end 0))))
  6468. (defun org-table-next-row ()
  6469. "Go to the next row (same column) in the current table.
  6470. Before doing so, re-align the table if necessary."
  6471. (interactive)
  6472. (org-table-maybe-eval-formula)
  6473. (org-table-maybe-recalculate-line)
  6474. (if (or (looking-at "[ \t]*$")
  6475. (save-excursion (skip-chars-backward " \t") (bolp)))
  6476. (newline)
  6477. (if (and org-table-automatic-realign
  6478. org-table-may-need-update)
  6479. (org-table-align))
  6480. (let ((col (org-table-current-column)))
  6481. (beginning-of-line 2)
  6482. (if (or (not (org-at-table-p))
  6483. (org-at-table-hline-p))
  6484. (progn
  6485. (beginning-of-line 0)
  6486. (org-table-insert-row 'below)))
  6487. (org-table-goto-column col)
  6488. (skip-chars-backward "^|\n\r")
  6489. (if (looking-at " ") (forward-char 1)))))
  6490. (defun org-table-copy-down (n)
  6491. "Copy a field down in the current column.
  6492. If the field at the cursor is empty, copy into it the content of the nearest
  6493. non-empty field above. With argument N, use the Nth non-empty field.
  6494. If the current field is not empty, it is copied down to the next row, and
  6495. the cursor is moved with it. Therefore, repeating this command causes the
  6496. column to be filled row-by-row.
  6497. If the variable `org-table-copy-increment' is non-nil and the field is an
  6498. integer or a timestamp, it will be incremented while copying. In the case of
  6499. a timestamp, if the cursor is on the year, change the year. If it is on the
  6500. month or the day, change that. Point will stay on the current date field
  6501. in order to easily repeat the interval."
  6502. (interactive "p")
  6503. (let* ((colpos (org-table-current-column))
  6504. (col (current-column))
  6505. (field (org-table-get-field))
  6506. (non-empty (string-match "[^ \t]" field))
  6507. (beg (org-table-begin))
  6508. txt)
  6509. (org-table-check-inside-data-field)
  6510. (if non-empty
  6511. (progn
  6512. (setq txt (org-trim field))
  6513. (org-table-next-row)
  6514. (org-table-blank-field))
  6515. (save-excursion
  6516. (setq txt
  6517. (catch 'exit
  6518. (while (progn (beginning-of-line 1)
  6519. (re-search-backward org-table-dataline-regexp
  6520. beg t))
  6521. (org-table-goto-column colpos t)
  6522. (if (and (looking-at
  6523. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  6524. (= (setq n (1- n)) 0))
  6525. (throw 'exit (match-string 1))))))))
  6526. (if txt
  6527. (progn
  6528. (if (and org-table-copy-increment
  6529. (string-match "^[0-9]+$" txt))
  6530. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  6531. (insert txt)
  6532. (move-to-column col)
  6533. (if (and org-table-copy-increment (org-at-timestamp-p t))
  6534. (org-timestamp-up 1)
  6535. (org-table-maybe-recalculate-line))
  6536. (org-table-align)
  6537. (move-to-column col))
  6538. (error "No non-empty field found"))))
  6539. (defun org-table-check-inside-data-field ()
  6540. "Is point inside a table data field?
  6541. I.e. not on a hline or before the first or after the last column?
  6542. This actually throws an error, so it aborts the current command."
  6543. (if (or (not (org-at-table-p))
  6544. (= (org-table-current-column) 0)
  6545. (org-at-table-hline-p)
  6546. (looking-at "[ \t]*$"))
  6547. (error "Not in table data field")))
  6548. (defvar org-table-clip nil
  6549. "Clipboard for table regions.")
  6550. (defun org-table-blank-field ()
  6551. "Blank the current table field or active region."
  6552. (interactive)
  6553. (org-table-check-inside-data-field)
  6554. (if (and (interactive-p) (org-region-active-p))
  6555. (let (org-table-clip)
  6556. (org-table-cut-region (region-beginning) (region-end)))
  6557. (skip-chars-backward "^|")
  6558. (backward-char 1)
  6559. (if (looking-at "|[^|\n]+")
  6560. (let* ((pos (match-beginning 0))
  6561. (match (match-string 0))
  6562. (len (org-string-width match)))
  6563. (replace-match (concat "|" (make-string (1- len) ?\ )))
  6564. (goto-char (+ 2 pos))
  6565. (substring match 1)))))
  6566. (defun org-table-get-field (&optional n replace)
  6567. "Return the value of the field in column N of current row.
  6568. N defaults to current field.
  6569. If REPLACE is a string, replace field with this value. The return value
  6570. is always the old value."
  6571. (and n (org-table-goto-column n))
  6572. (skip-chars-backward "^|\n")
  6573. (backward-char 1)
  6574. (if (looking-at "|[^|\r\n]*")
  6575. (let* ((pos (match-beginning 0))
  6576. (val (buffer-substring (1+ pos) (match-end 0))))
  6577. (if replace
  6578. (replace-match (concat "|" replace) t t))
  6579. (goto-char (min (point-at-eol) (+ 2 pos)))
  6580. val)
  6581. (forward-char 1) ""))
  6582. (defun org-table-field-info (arg)
  6583. "Show info about the current field, and highlight any reference at point."
  6584. (interactive "P")
  6585. (org-table-get-specials)
  6586. (save-excursion
  6587. (let* ((pos (point))
  6588. (col (org-table-current-column))
  6589. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  6590. (name (car (rassoc (list (org-current-line) col)
  6591. org-table-named-field-locations)))
  6592. (eql (org-table-get-stored-formulas))
  6593. (dline (org-table-current-dline))
  6594. (ref (format "@%d$%d" dline col))
  6595. (ref1 (org-table-convert-refs-to-an ref))
  6596. (fequation (or (assoc name eql) (assoc ref eql)))
  6597. (cequation (assoc (int-to-string col) eql))
  6598. (eqn (or fequation cequation)))
  6599. (goto-char pos)
  6600. (condition-case nil
  6601. (org-table-show-reference 'local)
  6602. (error nil))
  6603. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  6604. dline col
  6605. (if cname (concat " or $" cname) "")
  6606. dline col ref1
  6607. (if name (concat " or $" name) "")
  6608. ;; FIXME: formula info not correct if special table line
  6609. (if eqn
  6610. (concat ", formula: "
  6611. (org-table-formula-to-user
  6612. (concat
  6613. (if (string-match "^[$@]"(car eqn)) "" "$")
  6614. (car eqn) "=" (cdr eqn))))
  6615. "")))))
  6616. (defun org-table-current-column ()
  6617. "Find out which column we are in.
  6618. When called interactively, column is also displayed in echo area."
  6619. (interactive)
  6620. (if (interactive-p) (org-table-check-inside-data-field))
  6621. (save-excursion
  6622. (let ((cnt 0) (pos (point)))
  6623. (beginning-of-line 1)
  6624. (while (search-forward "|" pos t)
  6625. (setq cnt (1+ cnt)))
  6626. (if (interactive-p) (message "This is table column %d" cnt))
  6627. cnt)))
  6628. (defun org-table-current-dline ()
  6629. "Find out what table data line we are in.
  6630. Only datalins count for this."
  6631. (interactive)
  6632. (if (interactive-p) (org-table-check-inside-data-field))
  6633. (save-excursion
  6634. (let ((cnt 0) (pos (point)))
  6635. (goto-char (org-table-begin))
  6636. (while (<= (point) pos)
  6637. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  6638. (beginning-of-line 2))
  6639. (if (interactive-p) (message "This is table line %d" cnt))
  6640. cnt)))
  6641. (defun org-table-goto-column (n &optional on-delim force)
  6642. "Move the cursor to the Nth column in the current table line.
  6643. With optional argument ON-DELIM, stop with point before the left delimiter
  6644. of the field.
  6645. If there are less than N fields, just go to after the last delimiter.
  6646. However, when FORCE is non-nil, create new columns if necessary."
  6647. (interactive "p")
  6648. (let ((pos (point-at-eol)))
  6649. (beginning-of-line 1)
  6650. (when (> n 0)
  6651. (while (and (> (setq n (1- n)) -1)
  6652. (or (search-forward "|" pos t)
  6653. (and force
  6654. (progn (end-of-line 1)
  6655. (skip-chars-backward "^|")
  6656. (insert " | "))))))
  6657. ; (backward-char 2) t)))))
  6658. (when (and force (not (looking-at ".*|")))
  6659. (save-excursion (end-of-line 1) (insert " | ")))
  6660. (if on-delim
  6661. (backward-char 1)
  6662. (if (looking-at " ") (forward-char 1))))))
  6663. (defun org-at-table-p (&optional table-type)
  6664. "Return t if the cursor is inside an org-type table.
  6665. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  6666. (if org-enable-table-editor
  6667. (save-excursion
  6668. (beginning-of-line 1)
  6669. (looking-at (if table-type org-table-any-line-regexp
  6670. org-table-line-regexp)))
  6671. nil))
  6672. (defun org-at-table.el-p ()
  6673. "Return t if and only if we are at a table.el table."
  6674. (and (org-at-table-p 'any)
  6675. (save-excursion
  6676. (goto-char (org-table-begin 'any))
  6677. (looking-at org-table1-hline-regexp))))
  6678. (defun org-table-recognize-table.el ()
  6679. "If there is a table.el table nearby, recognize it and move into it."
  6680. (if org-table-tab-recognizes-table.el
  6681. (if (org-at-table.el-p)
  6682. (progn
  6683. (beginning-of-line 1)
  6684. (if (looking-at org-table-dataline-regexp)
  6685. nil
  6686. (if (looking-at org-table1-hline-regexp)
  6687. (progn
  6688. (beginning-of-line 2)
  6689. (if (looking-at org-table-any-border-regexp)
  6690. (beginning-of-line -1)))))
  6691. (if (re-search-forward "|" (org-table-end t) t)
  6692. (progn
  6693. (require 'table)
  6694. (if (table--at-cell-p (point))
  6695. t
  6696. (message "recognizing table.el table...")
  6697. (table-recognize-table)
  6698. (message "recognizing table.el table...done")))
  6699. (error "This should not happen..."))
  6700. t)
  6701. nil)
  6702. nil))
  6703. (defun org-at-table-hline-p ()
  6704. "Return t if the cursor is inside a hline in a table."
  6705. (if org-enable-table-editor
  6706. (save-excursion
  6707. (beginning-of-line 1)
  6708. (looking-at org-table-hline-regexp))
  6709. nil))
  6710. (defun org-table-insert-column ()
  6711. "Insert a new column into the table."
  6712. (interactive)
  6713. (if (not (org-at-table-p))
  6714. (error "Not at a table"))
  6715. (org-table-find-dataline)
  6716. (let* ((col (max 1 (org-table-current-column)))
  6717. (beg (org-table-begin))
  6718. (end (org-table-end))
  6719. ;; Current cursor position
  6720. (linepos (org-current-line))
  6721. (colpos col))
  6722. (goto-char beg)
  6723. (while (< (point) end)
  6724. (if (org-at-table-hline-p)
  6725. nil
  6726. (org-table-goto-column col t)
  6727. (insert "| "))
  6728. (beginning-of-line 2))
  6729. (move-marker end nil)
  6730. (goto-line linepos)
  6731. (org-table-goto-column colpos)
  6732. (org-table-align)
  6733. (org-table-fix-formulas "$" nil (1- col) 1)))
  6734. (defun org-table-find-dataline ()
  6735. "Find a dataline in the current table, which is needed for column commands."
  6736. (if (and (org-at-table-p)
  6737. (not (org-at-table-hline-p)))
  6738. t
  6739. (let ((col (current-column))
  6740. (end (org-table-end)))
  6741. (move-to-column col)
  6742. (while (and (< (point) end)
  6743. (or (not (= (current-column) col))
  6744. (org-at-table-hline-p)))
  6745. (beginning-of-line 2)
  6746. (move-to-column col))
  6747. (if (and (org-at-table-p)
  6748. (not (org-at-table-hline-p)))
  6749. t
  6750. (error
  6751. "Please position cursor in a data line for column operations")))))
  6752. (defun org-table-delete-column ()
  6753. "Delete a column from the table."
  6754. (interactive)
  6755. (if (not (org-at-table-p))
  6756. (error "Not at a table"))
  6757. (org-table-find-dataline)
  6758. (org-table-check-inside-data-field)
  6759. (let* ((col (org-table-current-column))
  6760. (beg (org-table-begin))
  6761. (end (org-table-end))
  6762. ;; Current cursor position
  6763. (linepos (org-current-line))
  6764. (colpos col))
  6765. (goto-char beg)
  6766. (while (< (point) end)
  6767. (if (org-at-table-hline-p)
  6768. nil
  6769. (org-table-goto-column col t)
  6770. (and (looking-at "|[^|\n]+|")
  6771. (replace-match "|")))
  6772. (beginning-of-line 2))
  6773. (move-marker end nil)
  6774. (goto-line linepos)
  6775. (org-table-goto-column colpos)
  6776. (org-table-align)
  6777. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  6778. col -1 col)))
  6779. (defun org-table-move-column-right ()
  6780. "Move column to the right."
  6781. (interactive)
  6782. (org-table-move-column nil))
  6783. (defun org-table-move-column-left ()
  6784. "Move column to the left."
  6785. (interactive)
  6786. (org-table-move-column 'left))
  6787. (defun org-table-move-column (&optional left)
  6788. "Move the current column to the right. With arg LEFT, move to the left."
  6789. (interactive "P")
  6790. (if (not (org-at-table-p))
  6791. (error "Not at a table"))
  6792. (org-table-find-dataline)
  6793. (org-table-check-inside-data-field)
  6794. (let* ((col (org-table-current-column))
  6795. (col1 (if left (1- col) col))
  6796. (beg (org-table-begin))
  6797. (end (org-table-end))
  6798. ;; Current cursor position
  6799. (linepos (org-current-line))
  6800. (colpos (if left (1- col) (1+ col))))
  6801. (if (and left (= col 1))
  6802. (error "Cannot move column further left"))
  6803. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  6804. (error "Cannot move column further right"))
  6805. (goto-char beg)
  6806. (while (< (point) end)
  6807. (if (org-at-table-hline-p)
  6808. nil
  6809. (org-table-goto-column col1 t)
  6810. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  6811. (replace-match "|\\2|\\1|")))
  6812. (beginning-of-line 2))
  6813. (move-marker end nil)
  6814. (goto-line linepos)
  6815. (org-table-goto-column colpos)
  6816. (org-table-align)
  6817. (org-table-fix-formulas
  6818. "$" (list (cons (number-to-string col) (number-to-string colpos))
  6819. (cons (number-to-string colpos) (number-to-string col))))))
  6820. (defun org-table-move-row-down ()
  6821. "Move table row down."
  6822. (interactive)
  6823. (org-table-move-row nil))
  6824. (defun org-table-move-row-up ()
  6825. "Move table row up."
  6826. (interactive)
  6827. (org-table-move-row 'up))
  6828. (defun org-table-move-row (&optional up)
  6829. "Move the current table line down. With arg UP, move it up."
  6830. (interactive "P")
  6831. (let* ((col (current-column))
  6832. (pos (point))
  6833. (hline1p (save-excursion (beginning-of-line 1)
  6834. (looking-at org-table-hline-regexp)))
  6835. (dline1 (org-table-current-dline))
  6836. (dline2 (+ dline1 (if up -1 1)))
  6837. (tonew (if up 0 2))
  6838. txt hline2p)
  6839. (beginning-of-line tonew)
  6840. (unless (org-at-table-p)
  6841. (goto-char pos)
  6842. (error "Cannot move row further"))
  6843. (setq hline2p (looking-at org-table-hline-regexp))
  6844. (goto-char pos)
  6845. (beginning-of-line 1)
  6846. (setq pos (point))
  6847. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  6848. (delete-region (point) (1+ (point-at-eol)))
  6849. (beginning-of-line tonew)
  6850. (insert txt)
  6851. (beginning-of-line 0)
  6852. (move-to-column col)
  6853. (unless (or hline1p hline2p)
  6854. (org-table-fix-formulas
  6855. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  6856. (cons (number-to-string dline2) (number-to-string dline1)))))))
  6857. (defun org-table-insert-row (&optional arg)
  6858. "Insert a new row above the current line into the table.
  6859. With prefix ARG, insert below the current line."
  6860. (interactive "P")
  6861. (if (not (org-at-table-p))
  6862. (error "Not at a table"))
  6863. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  6864. (new (org-table-clean-line line)))
  6865. ;; Fix the first field if necessary
  6866. (if (string-match "^[ \t]*| *[#$] *|" line)
  6867. (setq new (replace-match (match-string 0 line) t t new)))
  6868. (beginning-of-line (if arg 2 1))
  6869. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  6870. (beginning-of-line 0)
  6871. (re-search-forward "| ?" (point-at-eol) t)
  6872. (and (or org-table-may-need-update org-table-overlay-coordinates)
  6873. (org-table-align))
  6874. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  6875. (defun org-table-insert-hline (&optional above)
  6876. "Insert a horizontal-line below the current line into the table.
  6877. With prefix ABOVE, insert above the current line."
  6878. (interactive "P")
  6879. (if (not (org-at-table-p))
  6880. (error "Not at a table"))
  6881. (let ((line (org-table-clean-line
  6882. (buffer-substring (point-at-bol) (point-at-eol))))
  6883. (col (current-column)))
  6884. (while (string-match "|\\( +\\)|" line)
  6885. (setq line (replace-match
  6886. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  6887. ?-) "|") t t line)))
  6888. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  6889. (beginning-of-line (if above 1 2))
  6890. (insert line "\n")
  6891. (beginning-of-line (if above 1 -1))
  6892. (move-to-column col)
  6893. (and org-table-overlay-coordinates (org-table-align))))
  6894. (defun org-table-hline-and-move (&optional same-column)
  6895. "Insert a hline and move to the row below that line."
  6896. (interactive "P")
  6897. (let ((col (org-table-current-column)))
  6898. (org-table-maybe-eval-formula)
  6899. (org-table-maybe-recalculate-line)
  6900. (org-table-insert-hline)
  6901. (end-of-line 2)
  6902. (if (looking-at "\n[ \t]*|-")
  6903. (progn (insert "\n|") (org-table-align))
  6904. (org-table-next-field))
  6905. (if same-column (org-table-goto-column col))))
  6906. (defun org-table-clean-line (s)
  6907. "Convert a table line S into a string with only \"|\" and space.
  6908. In particular, this does handle wide and invisible characters."
  6909. (if (string-match "^[ \t]*|-" s)
  6910. ;; It's a hline, just map the characters
  6911. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  6912. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  6913. (setq s (replace-match
  6914. (concat "|" (make-string (org-string-width (match-string 1 s))
  6915. ?\ ) "|")
  6916. t t s)))
  6917. s))
  6918. (defun org-table-kill-row ()
  6919. "Delete the current row or horizontal line from the table."
  6920. (interactive)
  6921. (if (not (org-at-table-p))
  6922. (error "Not at a table"))
  6923. (let ((col (current-column))
  6924. (dline (org-table-current-dline)))
  6925. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  6926. (if (not (org-at-table-p)) (beginning-of-line 0))
  6927. (move-to-column col)
  6928. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  6929. dline -1 dline)))
  6930. (defun org-table-sort-lines (with-case &optional sorting-type)
  6931. "Sort table lines according to the column at point.
  6932. The position of point indicates the column to be used for
  6933. sorting, and the range of lines is the range between the nearest
  6934. horizontal separator lines, or the entire table of no such lines
  6935. exist. If point is before the first column, you will be prompted
  6936. for the sorting column. If there is an active region, the mark
  6937. specifies the first line and the sorting column, while point
  6938. should be in the last line to be included into the sorting.
  6939. The command then prompts for the sorting type which can be
  6940. alphabetically, numerically, or by time (as given in a time stamp
  6941. in the field). Sorting in reverse order is also possible.
  6942. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  6943. If SORTING-TYPE is specified when this function is called from a Lisp
  6944. program, no prompting will take place. SORTING-TYPE must be a character,
  6945. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  6946. should be done in reverse order."
  6947. (interactive "P")
  6948. (let* ((thisline (org-current-line))
  6949. (thiscol (org-table-current-column))
  6950. beg end bcol ecol tend tbeg column lns pos)
  6951. (when (equal thiscol 0)
  6952. (if (interactive-p)
  6953. (setq thiscol
  6954. (string-to-number
  6955. (read-string "Use column N for sorting: ")))
  6956. (setq thiscol 1))
  6957. (org-table-goto-column thiscol))
  6958. (org-table-check-inside-data-field)
  6959. (if (org-region-active-p)
  6960. (progn
  6961. (setq beg (region-beginning) end (region-end))
  6962. (goto-char beg)
  6963. (setq column (org-table-current-column)
  6964. beg (point-at-bol))
  6965. (goto-char end)
  6966. (setq end (point-at-bol 2)))
  6967. (setq column (org-table-current-column)
  6968. pos (point)
  6969. tbeg (org-table-begin)
  6970. tend (org-table-end))
  6971. (if (re-search-backward org-table-hline-regexp tbeg t)
  6972. (setq beg (point-at-bol 2))
  6973. (goto-char tbeg)
  6974. (setq beg (point-at-bol 1)))
  6975. (goto-char pos)
  6976. (if (re-search-forward org-table-hline-regexp tend t)
  6977. (setq end (point-at-bol 1))
  6978. (goto-char tend)
  6979. (setq end (point-at-bol))))
  6980. (setq beg (move-marker (make-marker) beg)
  6981. end (move-marker (make-marker) end))
  6982. (untabify beg end)
  6983. (goto-char beg)
  6984. (org-table-goto-column column)
  6985. (skip-chars-backward "^|")
  6986. (setq bcol (current-column))
  6987. (org-table-goto-column (1+ column))
  6988. (skip-chars-backward "^|")
  6989. (setq ecol (1- (current-column)))
  6990. (org-table-goto-column column)
  6991. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  6992. (org-split-string (buffer-substring beg end) "\n")))
  6993. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  6994. (delete-region beg end)
  6995. (move-marker beg nil)
  6996. (move-marker end nil)
  6997. (insert (mapconcat 'cdr lns "\n") "\n")
  6998. (goto-line thisline)
  6999. (org-table-goto-column thiscol)
  7000. (message "%d lines sorted, based on column %d" (length lns) column)))
  7001. (defun org-table-cut-region (beg end)
  7002. "Copy region in table to the clipboard and blank all relevant fields."
  7003. (interactive "r")
  7004. (org-table-copy-region beg end 'cut))
  7005. (defun org-table-copy-region (beg end &optional cut)
  7006. "Copy rectangular region in table to clipboard.
  7007. A special clipboard is used which can only be accessed
  7008. with `org-table-paste-rectangle'."
  7009. (interactive "rP")
  7010. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  7011. region cols
  7012. (rpl (if cut " " nil)))
  7013. (goto-char beg)
  7014. (org-table-check-inside-data-field)
  7015. (setq l01 (org-current-line)
  7016. c01 (org-table-current-column))
  7017. (goto-char end)
  7018. (org-table-check-inside-data-field)
  7019. (setq l02 (org-current-line)
  7020. c02 (org-table-current-column))
  7021. (setq l1 (min l01 l02) l2 (max l01 l02)
  7022. c1 (min c01 c02) c2 (max c01 c02))
  7023. (catch 'exit
  7024. (while t
  7025. (catch 'nextline
  7026. (if (> l1 l2) (throw 'exit t))
  7027. (goto-line l1)
  7028. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7029. (setq cols nil ic1 c1 ic2 c2)
  7030. (while (< ic1 (1+ ic2))
  7031. (push (org-table-get-field ic1 rpl) cols)
  7032. (setq ic1 (1+ ic1)))
  7033. (push (nreverse cols) region)
  7034. (setq l1 (1+ l1)))))
  7035. (setq org-table-clip (nreverse region))
  7036. (if cut (org-table-align))
  7037. org-table-clip))
  7038. (defun org-table-paste-rectangle ()
  7039. "Paste a rectangular region into a table.
  7040. The upper right corner ends up in the current field. All involved fields
  7041. will be overwritten. If the rectangle does not fit into the present table,
  7042. the table is enlarged as needed. The process ignores horizontal separator
  7043. lines."
  7044. (interactive)
  7045. (unless (and org-table-clip (listp org-table-clip))
  7046. (error "First cut/copy a region to paste!"))
  7047. (org-table-check-inside-data-field)
  7048. (let* ((clip org-table-clip)
  7049. (line (org-current-line))
  7050. (col (org-table-current-column))
  7051. (org-enable-table-editor t)
  7052. (org-table-automatic-realign nil)
  7053. c cols field)
  7054. (while (setq cols (pop clip))
  7055. (while (org-at-table-hline-p) (beginning-of-line 2))
  7056. (if (not (org-at-table-p))
  7057. (progn (end-of-line 0) (org-table-next-field)))
  7058. (setq c col)
  7059. (while (setq field (pop cols))
  7060. (org-table-goto-column c nil 'force)
  7061. (org-table-get-field nil field)
  7062. (setq c (1+ c)))
  7063. (beginning-of-line 2))
  7064. (goto-line line)
  7065. (org-table-goto-column col)
  7066. (org-table-align)))
  7067. (defun org-table-convert ()
  7068. "Convert from `org-mode' table to table.el and back.
  7069. Obviously, this only works within limits. When an Org-mode table is
  7070. converted to table.el, all horizontal separator lines get lost, because
  7071. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7072. A table.el table can be converted to an Org-mode table only if it does not
  7073. do row or column spanning. Multiline cells will become multiple cells.
  7074. Beware, Org-mode does not test if the table can be successfully converted - it
  7075. blindly applies a recipe that works for simple tables."
  7076. (interactive)
  7077. (require 'table)
  7078. (if (org-at-table.el-p)
  7079. ;; convert to Org-mode table
  7080. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7081. (end (move-marker (make-marker) (org-table-end t))))
  7082. (table-unrecognize-region beg end)
  7083. (goto-char beg)
  7084. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7085. (replace-match ""))
  7086. (goto-char beg))
  7087. (if (org-at-table-p)
  7088. ;; convert to table.el table
  7089. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7090. (end (move-marker (make-marker) (org-table-end))))
  7091. ;; first, get rid of all horizontal lines
  7092. (goto-char beg)
  7093. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7094. (replace-match ""))
  7095. ;; insert a hline before first
  7096. (goto-char beg)
  7097. (org-table-insert-hline 'above)
  7098. (beginning-of-line -1)
  7099. ;; insert a hline after each line
  7100. (while (progn (beginning-of-line 3) (< (point) end))
  7101. (org-table-insert-hline))
  7102. (goto-char beg)
  7103. (setq end (move-marker end (org-table-end)))
  7104. ;; replace "+" at beginning and ending of hlines
  7105. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  7106. (replace-match "\\1+-"))
  7107. (goto-char beg)
  7108. (while (re-search-forward "-|[ \t]*$" end t)
  7109. (replace-match "-+"))
  7110. (goto-char beg)))))
  7111. (defun org-table-wrap-region (arg)
  7112. "Wrap several fields in a column like a paragraph.
  7113. This is useful if you'd like to spread the contents of a field over several
  7114. lines, in order to keep the table compact.
  7115. If there is an active region, and both point and mark are in the same column,
  7116. the text in the column is wrapped to minimum width for the given number of
  7117. lines. Generally, this makes the table more compact. A prefix ARG may be
  7118. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  7119. formats the selected text to two lines. If the region was longer than two
  7120. lines, the remaining lines remain empty. A negative prefix argument reduces
  7121. the current number of lines by that amount. The wrapped text is pasted back
  7122. into the table. If you formatted it to more lines than it was before, fields
  7123. further down in the table get overwritten - so you might need to make space in
  7124. the table first.
  7125. If there is no region, the current field is split at the cursor position and
  7126. the text fragment to the right of the cursor is prepended to the field one
  7127. line down.
  7128. If there is no region, but you specify a prefix ARG, the current field gets
  7129. blank, and the content is appended to the field above."
  7130. (interactive "P")
  7131. (org-table-check-inside-data-field)
  7132. (if (org-region-active-p)
  7133. ;; There is a region: fill as a paragraph
  7134. (let* ((beg (region-beginning))
  7135. (cline (save-excursion (goto-char beg) (org-current-line)))
  7136. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  7137. nlines)
  7138. (org-table-cut-region (region-beginning) (region-end))
  7139. (if (> (length (car org-table-clip)) 1)
  7140. (error "Region must be limited to single column"))
  7141. (setq nlines (if arg
  7142. (if (< arg 1)
  7143. (+ (length org-table-clip) arg)
  7144. arg)
  7145. (length org-table-clip)))
  7146. (setq org-table-clip
  7147. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  7148. nil nlines)))
  7149. (goto-line cline)
  7150. (org-table-goto-column ccol)
  7151. (org-table-paste-rectangle))
  7152. ;; No region, split the current field at point
  7153. (if arg
  7154. ;; combine with field above
  7155. (let ((s (org-table-blank-field))
  7156. (col (org-table-current-column)))
  7157. (beginning-of-line 0)
  7158. (while (org-at-table-hline-p) (beginning-of-line 0))
  7159. (org-table-goto-column col)
  7160. (skip-chars-forward "^|")
  7161. (skip-chars-backward " ")
  7162. (insert " " (org-trim s))
  7163. (org-table-align))
  7164. ;; split field
  7165. (when (looking-at "\\([^|]+\\)+|")
  7166. (let ((s (match-string 1)))
  7167. (replace-match " |")
  7168. (goto-char (match-beginning 0))
  7169. (org-table-next-row)
  7170. (insert (org-trim s) " ")
  7171. (org-table-align))))))
  7172. (defvar org-field-marker nil)
  7173. (defun org-table-edit-field (arg)
  7174. "Edit table field in a different window.
  7175. This is mainly useful for fields that contain hidden parts.
  7176. When called with a \\[universal-argument] prefix, just make the full field visible so that
  7177. it can be edited in place."
  7178. (interactive "P")
  7179. (if arg
  7180. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  7181. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  7182. (remove-text-properties b e '(org-cwidth t invisible t
  7183. display t intangible t))
  7184. (if (and (boundp 'font-lock-mode) font-lock-mode)
  7185. (font-lock-fontify-block)))
  7186. (let ((pos (move-marker (make-marker) (point)))
  7187. (field (org-table-get-field))
  7188. (cw (current-window-configuration))
  7189. p)
  7190. (switch-to-buffer-other-window "*Org tmp*")
  7191. (erase-buffer)
  7192. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  7193. (let ((org-inhibit-startup t)) (org-mode))
  7194. (goto-char (setq p (point-max)))
  7195. (insert (org-trim field))
  7196. (remove-text-properties p (point-max)
  7197. '(invisible t org-cwidth t display t
  7198. intangible t))
  7199. (goto-char p)
  7200. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  7201. (org-set-local 'org-window-configuration cw)
  7202. (org-set-local 'org-field-marker pos)
  7203. (message "Edit and finish with C-c C-c"))))
  7204. (defun org-table-finish-edit-field ()
  7205. "Finish editing a table data field.
  7206. Remove all newline characters, insert the result into the table, realign
  7207. the table and kill the editing buffer."
  7208. (let ((pos org-field-marker)
  7209. (cw org-window-configuration)
  7210. (cb (current-buffer))
  7211. text)
  7212. (goto-char (point-min))
  7213. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  7214. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  7215. (replace-match " "))
  7216. (setq text (org-trim (buffer-string)))
  7217. (set-window-configuration cw)
  7218. (kill-buffer cb)
  7219. (select-window (get-buffer-window (marker-buffer pos)))
  7220. (goto-char pos)
  7221. (move-marker pos nil)
  7222. (org-table-check-inside-data-field)
  7223. (org-table-get-field nil text)
  7224. (org-table-align)
  7225. (message "New field value inserted")))
  7226. (defun org-trim (s)
  7227. "Remove whitespace at beginning and end of string."
  7228. (if (string-match "^[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  7229. (if (string-match "[ \t\n\r]+$" s) (setq s (replace-match "" t t s)))
  7230. s)
  7231. (defun org-wrap (string &optional width lines)
  7232. "Wrap string to either a number of lines, or a width in characters.
  7233. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  7234. that costs. If there is a word longer than WIDTH, the text is actually
  7235. wrapped to the length of that word.
  7236. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  7237. many lines, whatever width that takes.
  7238. The return value is a list of lines, without newlines at the end."
  7239. (let* ((words (org-split-string string "[ \t\n]+"))
  7240. (maxword (apply 'max (mapcar 'org-string-width words)))
  7241. w ll)
  7242. (cond (width
  7243. (org-do-wrap words (max maxword width)))
  7244. (lines
  7245. (setq w maxword)
  7246. (setq ll (org-do-wrap words maxword))
  7247. (if (<= (length ll) lines)
  7248. ll
  7249. (setq ll words)
  7250. (while (> (length ll) lines)
  7251. (setq w (1+ w))
  7252. (setq ll (org-do-wrap words w)))
  7253. ll))
  7254. (t (error "Cannot wrap this")))))
  7255. (defun org-do-wrap (words width)
  7256. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  7257. (let (lines line)
  7258. (while words
  7259. (setq line (pop words))
  7260. (while (and words (< (+ (length line) (length (car words))) width))
  7261. (setq line (concat line " " (pop words))))
  7262. (setq lines (push line lines)))
  7263. (nreverse lines)))
  7264. (defun org-split-string (string &optional separators)
  7265. "Splits STRING into substrings at SEPARATORS.
  7266. No empty strings are returned if there are matches at the beginning
  7267. and end of string."
  7268. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  7269. (start 0)
  7270. notfirst
  7271. (list nil))
  7272. (while (and (string-match rexp string
  7273. (if (and notfirst
  7274. (= start (match-beginning 0))
  7275. (< start (length string)))
  7276. (1+ start) start))
  7277. (< (match-beginning 0) (length string)))
  7278. (setq notfirst t)
  7279. (or (eq (match-beginning 0) 0)
  7280. (and (eq (match-beginning 0) (match-end 0))
  7281. (eq (match-beginning 0) start))
  7282. (setq list
  7283. (cons (substring string start (match-beginning 0))
  7284. list)))
  7285. (setq start (match-end 0)))
  7286. (or (eq start (length string))
  7287. (setq list
  7288. (cons (substring string start)
  7289. list)))
  7290. (nreverse list)))
  7291. (defun org-table-map-tables (function)
  7292. "Apply FUNCTION to the start of all tables in the buffer."
  7293. (save-excursion
  7294. (save-restriction
  7295. (widen)
  7296. (goto-char (point-min))
  7297. (while (re-search-forward org-table-any-line-regexp nil t)
  7298. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  7299. (beginning-of-line 1)
  7300. (if (looking-at org-table-line-regexp)
  7301. (save-excursion (funcall function)))
  7302. (re-search-forward org-table-any-border-regexp nil 1))))
  7303. (message "Mapping tables: done"))
  7304. (defvar org-timecnt) ; dynamically scoped parameter
  7305. (defun org-table-sum (&optional beg end nlast)
  7306. "Sum numbers in region of current table column.
  7307. The result will be displayed in the echo area, and will be available
  7308. as kill to be inserted with \\[yank].
  7309. If there is an active region, it is interpreted as a rectangle and all
  7310. numbers in that rectangle will be summed. If there is no active
  7311. region and point is located in a table column, sum all numbers in that
  7312. column.
  7313. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  7314. numbers are assumed to be times as well (in decimal hours) and the
  7315. numbers are added as such.
  7316. If NLAST is a number, only the NLAST fields will actually be summed."
  7317. (interactive)
  7318. (save-excursion
  7319. (let (col (org-timecnt 0) diff h m s org-table-clip)
  7320. (cond
  7321. ((and beg end)) ; beg and end given explicitly
  7322. ((org-region-active-p)
  7323. (setq beg (region-beginning) end (region-end)))
  7324. (t
  7325. (setq col (org-table-current-column))
  7326. (goto-char (org-table-begin))
  7327. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  7328. (error "No table data"))
  7329. (org-table-goto-column col)
  7330. (setq beg (point))
  7331. (goto-char (org-table-end))
  7332. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  7333. (error "No table data"))
  7334. (org-table-goto-column col)
  7335. (setq end (point))))
  7336. (let* ((items (apply 'append (org-table-copy-region beg end)))
  7337. (items1 (cond ((not nlast) items)
  7338. ((>= nlast (length items)) items)
  7339. (t (setq items (reverse items))
  7340. (setcdr (nthcdr (1- nlast) items) nil)
  7341. (nreverse items))))
  7342. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  7343. items1)))
  7344. (res (apply '+ numbers))
  7345. (sres (if (= org-timecnt 0)
  7346. (format "%g" res)
  7347. (setq diff (* 3600 res)
  7348. h (floor (/ diff 3600)) diff (mod diff 3600)
  7349. m (floor (/ diff 60)) diff (mod diff 60)
  7350. s diff)
  7351. (format "%d:%02d:%02d" h m s))))
  7352. (kill-new sres)
  7353. (if (interactive-p)
  7354. (message "%s"
  7355. (substitute-command-keys
  7356. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  7357. (length numbers) sres))))
  7358. sres))))
  7359. (defun org-table-get-number-for-summing (s)
  7360. (let (n)
  7361. (if (string-match "^ *|? *" s)
  7362. (setq s (replace-match "" nil nil s)))
  7363. (if (string-match " *|? *$" s)
  7364. (setq s (replace-match "" nil nil s)))
  7365. (setq n (string-to-number s))
  7366. (cond
  7367. ((and (string-match "0" s)
  7368. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  7369. ((string-match "\\`[ \t]+\\'" s) nil)
  7370. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  7371. (let ((h (string-to-number (or (match-string 1 s) "0")))
  7372. (m (string-to-number (or (match-string 2 s) "0")))
  7373. (s (string-to-number (or (match-string 4 s) "0"))))
  7374. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  7375. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  7376. ((equal n 0) nil)
  7377. (t n))))
  7378. (defun org-table-current-field-formula (&optional key noerror)
  7379. "Return the formula active for the current field.
  7380. Assumes that specials are in place.
  7381. If KEY is given, return the key to this formula.
  7382. Otherwise return the formula preceeded with \"=\" or \":=\"."
  7383. (let* ((name (car (rassoc (list (org-current-line)
  7384. (org-table-current-column))
  7385. org-table-named-field-locations)))
  7386. (col (org-table-current-column))
  7387. (scol (int-to-string col))
  7388. (ref (format "@%d$%d" (org-table-current-dline) col))
  7389. (stored-list (org-table-get-stored-formulas noerror))
  7390. (ass (or (assoc name stored-list)
  7391. (assoc ref stored-list)
  7392. (assoc scol stored-list))))
  7393. (if key
  7394. (car ass)
  7395. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  7396. (cdr ass))))))
  7397. (defun org-table-get-formula (&optional equation named)
  7398. "Read a formula from the minibuffer, offer stored formula as default.
  7399. When NAMED is non-nil, look for a named equation."
  7400. (let* ((stored-list (org-table-get-stored-formulas))
  7401. (name (car (rassoc (list (org-current-line)
  7402. (org-table-current-column))
  7403. org-table-named-field-locations)))
  7404. (ref (format "@%d$%d" (org-table-current-dline)
  7405. (org-table-current-column)))
  7406. (refass (assoc ref stored-list))
  7407. (scol (if named
  7408. (if name name ref)
  7409. (int-to-string (org-table-current-column))))
  7410. (dummy (and (or name refass) (not named)
  7411. (not (y-or-n-p "Replace field formula with column formula? " ))
  7412. (error "Abort")))
  7413. (name (or name ref))
  7414. (org-table-may-need-update nil)
  7415. (stored (cdr (assoc scol stored-list)))
  7416. (eq (cond
  7417. ((and stored equation (string-match "^ *=? *$" equation))
  7418. stored)
  7419. ((stringp equation)
  7420. equation)
  7421. (t (org-table-formula-from-user
  7422. (read-string
  7423. (org-table-formula-to-user
  7424. (format "%s formula %s%s="
  7425. (if named "Field" "Column")
  7426. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  7427. scol))
  7428. (if stored (org-table-formula-to-user stored) "")
  7429. 'org-table-formula-history
  7430. )))))
  7431. mustsave)
  7432. (when (not (string-match "\\S-" eq))
  7433. ;; remove formula
  7434. (setq stored-list (delq (assoc scol stored-list) stored-list))
  7435. (org-table-store-formulas stored-list)
  7436. (error "Formula removed"))
  7437. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  7438. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  7439. (if (and name (not named))
  7440. ;; We set the column equation, delete the named one.
  7441. (setq stored-list (delq (assoc name stored-list) stored-list)
  7442. mustsave t))
  7443. (if stored
  7444. (setcdr (assoc scol stored-list) eq)
  7445. (setq stored-list (cons (cons scol eq) stored-list)))
  7446. (if (or mustsave (not (equal stored eq)))
  7447. (org-table-store-formulas stored-list))
  7448. eq))
  7449. (defun org-table-store-formulas (alist)
  7450. "Store the list of formulas below the current table."
  7451. (setq alist (sort alist 'org-table-formula-less-p))
  7452. (save-excursion
  7453. (goto-char (org-table-end))
  7454. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  7455. (progn
  7456. ;; don't overwrite TBLFM, we might use text properties to store stuff
  7457. (goto-char (match-beginning 2))
  7458. (delete-region (match-beginning 2) (match-end 0)))
  7459. (insert "#+TBLFM:"))
  7460. (insert " "
  7461. (mapconcat (lambda (x)
  7462. (concat
  7463. (if (equal (string-to-char (car x)) ?@) "" "$")
  7464. (car x) "=" (cdr x)))
  7465. alist "::")
  7466. "\n")))
  7467. (defsubst org-table-formula-make-cmp-string (a)
  7468. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  7469. (concat
  7470. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  7471. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  7472. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  7473. (defun org-table-formula-less-p (a b)
  7474. "Compare two formulas for sorting."
  7475. (let ((as (org-table-formula-make-cmp-string (car a)))
  7476. (bs (org-table-formula-make-cmp-string (car b))))
  7477. (and as bs (string< as bs))))
  7478. (defun org-table-get-stored-formulas (&optional noerror)
  7479. "Return an alist with the stored formulas directly after current table."
  7480. (interactive)
  7481. (let (scol eq eq-alist strings string seen)
  7482. (save-excursion
  7483. (goto-char (org-table-end))
  7484. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  7485. (setq strings (org-split-string (match-string 2) " *:: *"))
  7486. (while (setq string (pop strings))
  7487. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  7488. (setq scol (if (match-end 2)
  7489. (match-string 2 string)
  7490. (match-string 1 string))
  7491. eq (match-string 3 string)
  7492. eq-alist (cons (cons scol eq) eq-alist))
  7493. (if (member scol seen)
  7494. (if noerror
  7495. (progn
  7496. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  7497. (ding)
  7498. (sit-for 2))
  7499. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  7500. (push scol seen))))))
  7501. (nreverse eq-alist)))
  7502. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  7503. "Modify the equations after the table structure has been edited.
  7504. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  7505. For all numbers larger than LIMIT, shift them by DELTA."
  7506. (save-excursion
  7507. (goto-char (org-table-end))
  7508. (when (looking-at "#\\+TBLFM:")
  7509. (let ((re (concat key "\\([0-9]+\\)"))
  7510. (re2
  7511. (when remove
  7512. (if (equal key "$")
  7513. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  7514. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  7515. s n a)
  7516. (when remove
  7517. (while (re-search-forward re2 (point-at-eol) t)
  7518. (replace-match "")))
  7519. (while (re-search-forward re (point-at-eol) t)
  7520. (setq s (match-string 1) n (string-to-number s))
  7521. (cond
  7522. ((setq a (assoc s replace))
  7523. (replace-match (concat key (cdr a)) t t))
  7524. ((and limit (> n limit))
  7525. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  7526. (defun org-table-get-specials ()
  7527. "Get the column names and local parameters for this table."
  7528. (save-excursion
  7529. (let ((beg (org-table-begin)) (end (org-table-end))
  7530. names name fields fields1 field cnt
  7531. c v l line col types dlines hlines)
  7532. (setq org-table-column-names nil
  7533. org-table-local-parameters nil
  7534. org-table-named-field-locations nil
  7535. org-table-current-begin-line nil
  7536. org-table-current-begin-pos nil
  7537. org-table-current-line-types nil)
  7538. (goto-char beg)
  7539. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  7540. (setq names (org-split-string (match-string 1) " *| *")
  7541. cnt 1)
  7542. (while (setq name (pop names))
  7543. (setq cnt (1+ cnt))
  7544. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  7545. (push (cons name (int-to-string cnt)) org-table-column-names))))
  7546. (setq org-table-column-names (nreverse org-table-column-names))
  7547. (setq org-table-column-name-regexp
  7548. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  7549. (goto-char beg)
  7550. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  7551. (setq fields (org-split-string (match-string 1) " *| *"))
  7552. (while (setq field (pop fields))
  7553. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  7554. (push (cons (match-string 1 field) (match-string 2 field))
  7555. org-table-local-parameters))))
  7556. (goto-char beg)
  7557. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  7558. (setq c (match-string 1)
  7559. fields (org-split-string (match-string 2) " *| *"))
  7560. (save-excursion
  7561. (beginning-of-line (if (equal c "_") 2 0))
  7562. (setq line (org-current-line) col 1)
  7563. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  7564. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  7565. (while (and fields1 (setq field (pop fields)))
  7566. (setq v (pop fields1) col (1+ col))
  7567. (when (and (stringp field) (stringp v)
  7568. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  7569. (push (cons field v) org-table-local-parameters)
  7570. (push (list field line col) org-table-named-field-locations))))
  7571. ;; Analyse the line types
  7572. (goto-char beg)
  7573. (setq org-table-current-begin-line (org-current-line)
  7574. org-table-current-begin-pos (point)
  7575. l org-table-current-begin-line)
  7576. (while (looking-at "[ \t]*|\\(-\\)?")
  7577. (push (if (match-end 1) 'hline 'dline) types)
  7578. (if (match-end 1) (push l hlines) (push l dlines))
  7579. (beginning-of-line 2)
  7580. (setq l (1+ l)))
  7581. (setq org-table-current-line-types (apply 'vector (nreverse types))
  7582. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  7583. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  7584. (defun org-table-maybe-eval-formula ()
  7585. "Check if the current field starts with \"=\" or \":=\".
  7586. If yes, store the formula and apply it."
  7587. ;; We already know we are in a table. Get field will only return a formula
  7588. ;; when appropriate. It might return a separator line, but no problem.
  7589. (when org-table-formula-evaluate-inline
  7590. (let* ((field (org-trim (or (org-table-get-field) "")))
  7591. named eq)
  7592. (when (string-match "^:?=\\(.*\\)" field)
  7593. (setq named (equal (string-to-char field) ?:)
  7594. eq (match-string 1 field))
  7595. (if (or (fboundp 'calc-eval)
  7596. (equal (substring eq 0 (min 2 (length eq))) "'("))
  7597. (org-table-eval-formula (if named '(4) nil)
  7598. (org-table-formula-from-user eq))
  7599. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  7600. (defvar org-recalc-commands nil
  7601. "List of commands triggering the recalculation of a line.
  7602. Will be filled automatically during use.")
  7603. (defvar org-recalc-marks
  7604. '((" " . "Unmarked: no special line, no automatic recalculation")
  7605. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  7606. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  7607. ("!" . "Column name definition line. Reference in formula as $name.")
  7608. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  7609. ("_" . "Names for values in row below this one.")
  7610. ("^" . "Names for values in row above this one.")))
  7611. (defun org-table-rotate-recalc-marks (&optional newchar)
  7612. "Rotate the recalculation mark in the first column.
  7613. If in any row, the first field is not consistent with a mark,
  7614. insert a new column for the markers.
  7615. When there is an active region, change all the lines in the region,
  7616. after prompting for the marking character.
  7617. After each change, a message will be displayed indicating the meaning
  7618. of the new mark."
  7619. (interactive)
  7620. (unless (org-at-table-p) (error "Not at a table"))
  7621. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  7622. (beg (org-table-begin))
  7623. (end (org-table-end))
  7624. (l (org-current-line))
  7625. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  7626. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  7627. (have-col
  7628. (save-excursion
  7629. (goto-char beg)
  7630. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  7631. (col (org-table-current-column))
  7632. (forcenew (car (assoc newchar org-recalc-marks)))
  7633. epos new)
  7634. (when l1
  7635. (message "Change region to what mark? Type # * ! $ or SPC: ")
  7636. (setq newchar (char-to-string (read-char-exclusive))
  7637. forcenew (car (assoc newchar org-recalc-marks))))
  7638. (if (and newchar (not forcenew))
  7639. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  7640. newchar))
  7641. (if l1 (goto-line l1))
  7642. (save-excursion
  7643. (beginning-of-line 1)
  7644. (unless (looking-at org-table-dataline-regexp)
  7645. (error "Not at a table data line")))
  7646. (unless have-col
  7647. (org-table-goto-column 1)
  7648. (org-table-insert-column)
  7649. (org-table-goto-column (1+ col)))
  7650. (setq epos (point-at-eol))
  7651. (save-excursion
  7652. (beginning-of-line 1)
  7653. (org-table-get-field
  7654. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  7655. (concat " "
  7656. (setq new (or forcenew
  7657. (cadr (member (match-string 1) marks))))
  7658. " ")
  7659. " # ")))
  7660. (if (and l1 l2)
  7661. (progn
  7662. (goto-line l1)
  7663. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  7664. (and (looking-at org-table-dataline-regexp)
  7665. (org-table-get-field 1 (concat " " new " "))))
  7666. (goto-line l1)))
  7667. (if (not (= epos (point-at-eol))) (org-table-align))
  7668. (goto-line l)
  7669. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  7670. (defun org-table-maybe-recalculate-line ()
  7671. "Recompute the current line if marked for it, and if we haven't just done it."
  7672. (interactive)
  7673. (and org-table-allow-automatic-line-recalculation
  7674. (not (and (memq last-command org-recalc-commands)
  7675. (equal org-last-recalc-line (org-current-line))))
  7676. (save-excursion (beginning-of-line 1)
  7677. (looking-at org-table-auto-recalculate-regexp))
  7678. (org-table-recalculate) t))
  7679. (defvar org-table-formula-debug nil
  7680. "Non-nil means, debug table formulas.
  7681. When nil, simply write \"#ERROR\" in corrupted fields.")
  7682. (make-variable-buffer-local 'org-table-formula-debug)
  7683. (defvar modes)
  7684. (defsubst org-set-calc-mode (var &optional value)
  7685. (if (stringp var)
  7686. (setq var (assoc var '(("D" calc-angle-mode deg)
  7687. ("R" calc-angle-mode rad)
  7688. ("F" calc-prefer-frac t)
  7689. ("S" calc-symbolic-mode t)))
  7690. value (nth 2 var) var (nth 1 var)))
  7691. (if (memq var modes)
  7692. (setcar (cdr (memq var modes)) value)
  7693. (cons var (cons value modes)))
  7694. modes)
  7695. (defun org-table-eval-formula (&optional arg equation
  7696. suppress-align suppress-const
  7697. suppress-store suppress-analysis)
  7698. "Replace the table field value at the cursor by the result of a calculation.
  7699. This function makes use of Dave Gillespie's Calc package, in my view the
  7700. most exciting program ever written for GNU Emacs. So you need to have Calc
  7701. installed in order to use this function.
  7702. In a table, this command replaces the value in the current field with the
  7703. result of a formula. It also installs the formula as the \"current\" column
  7704. formula, by storing it in a special line below the table. When called
  7705. with a `C-u' prefix, the current field must ba a named field, and the
  7706. formula is installed as valid in only this specific field.
  7707. When called with two `C-u' prefixes, insert the active equation
  7708. for the field back into the current field, so that it can be
  7709. edited there. This is useful in order to use \\[org-table-show-reference]
  7710. to check the referenced fields.
  7711. When called, the command first prompts for a formula, which is read in
  7712. the minibuffer. Previously entered formulas are available through the
  7713. history list, and the last used formula is offered as a default.
  7714. These stored formulas are adapted correctly when moving, inserting, or
  7715. deleting columns with the corresponding commands.
  7716. The formula can be any algebraic expression understood by the Calc package.
  7717. For details, see the Org-mode manual.
  7718. This function can also be called from Lisp programs and offers
  7719. additional arguments: EQUATION can be the formula to apply. If this
  7720. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  7721. used to speed-up recursive calls by by-passing unnecessary aligns.
  7722. SUPPRESS-CONST suppresses the interpretation of constants in the
  7723. formula, assuming that this has been done already outside the function.
  7724. SUPPRESS-STORE means the formula should not be stored, either because
  7725. it is already stored, or because it is a modified equation that should
  7726. not overwrite the stored one."
  7727. (interactive "P")
  7728. (org-table-check-inside-data-field)
  7729. (or suppress-analysis (org-table-get-specials))
  7730. (if (equal arg '(16))
  7731. (let ((eq (org-table-current-field-formula)))
  7732. (or eq (error "No equation active for current field"))
  7733. (org-table-get-field nil eq)
  7734. (org-table-align)
  7735. (setq org-table-may-need-update t))
  7736. (let* (fields
  7737. (ndown (if (integerp arg) arg 1))
  7738. (org-table-automatic-realign nil)
  7739. (case-fold-search nil)
  7740. (down (> ndown 1))
  7741. (formula (if (and equation suppress-store)
  7742. equation
  7743. (org-table-get-formula equation (equal arg '(4)))))
  7744. (n0 (org-table-current-column))
  7745. (modes (copy-sequence org-calc-default-modes))
  7746. (numbers nil) ; was a variable, now fixed default
  7747. (keep-empty nil)
  7748. n form form0 bw fmt x ev orig c lispp literal)
  7749. ;; Parse the format string. Since we have a lot of modes, this is
  7750. ;; a lot of work. However, I think calc still uses most of the time.
  7751. (if (string-match ";" formula)
  7752. (let ((tmp (org-split-string formula ";")))
  7753. (setq formula (car tmp)
  7754. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  7755. (nth 1 tmp)))
  7756. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  7757. (setq c (string-to-char (match-string 1 fmt))
  7758. n (string-to-number (match-string 2 fmt)))
  7759. (if (= c ?p)
  7760. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  7761. (setq modes (org-set-calc-mode
  7762. 'calc-float-format
  7763. (list (cdr (assoc c '((?n . float) (?f . fix)
  7764. (?s . sci) (?e . eng))))
  7765. n))))
  7766. (setq fmt (replace-match "" t t fmt)))
  7767. (if (string-match "[NT]" fmt)
  7768. (setq numbers (equal (match-string 0 fmt) "N")
  7769. fmt (replace-match "" t t fmt)))
  7770. (if (string-match "L" fmt)
  7771. (setq literal t
  7772. fmt (replace-match "" t t fmt)))
  7773. (if (string-match "E" fmt)
  7774. (setq keep-empty t
  7775. fmt (replace-match "" t t fmt)))
  7776. (while (string-match "[DRFS]" fmt)
  7777. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  7778. (setq fmt (replace-match "" t t fmt)))
  7779. (unless (string-match "\\S-" fmt)
  7780. (setq fmt nil))))
  7781. (if (and (not suppress-const) org-table-formula-use-constants)
  7782. (setq formula (org-table-formula-substitute-names formula)))
  7783. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  7784. (while (> ndown 0)
  7785. (setq fields (org-split-string
  7786. (org-no-properties
  7787. (buffer-substring (point-at-bol) (point-at-eol)))
  7788. " *| *"))
  7789. (if (eq numbers t)
  7790. (setq fields (mapcar
  7791. (lambda (x) (number-to-string (string-to-number x)))
  7792. fields)))
  7793. (setq ndown (1- ndown))
  7794. (setq form (copy-sequence formula)
  7795. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  7796. (if (and lispp literal) (setq lispp 'literal))
  7797. ;; Check for old vertical references
  7798. (setq form (org-rewrite-old-row-references form))
  7799. ;; Insert complex ranges
  7800. (while (string-match org-table-range-regexp form)
  7801. (setq form
  7802. (replace-match
  7803. (save-match-data
  7804. (org-table-make-reference
  7805. (org-table-get-range (match-string 0 form) nil n0)
  7806. keep-empty numbers lispp))
  7807. t t form)))
  7808. ;; Insert simple ranges
  7809. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  7810. (setq form
  7811. (replace-match
  7812. (save-match-data
  7813. (org-table-make-reference
  7814. (org-sublist
  7815. fields (string-to-number (match-string 1 form))
  7816. (string-to-number (match-string 2 form)))
  7817. keep-empty numbers lispp))
  7818. t t form)))
  7819. (setq form0 form)
  7820. ;; Insert the references to fields in same row
  7821. (while (string-match "\\$\\([0-9]+\\)" form)
  7822. (setq n (string-to-number (match-string 1 form))
  7823. x (nth (1- (if (= n 0) n0 n)) fields))
  7824. (unless x (error "Invalid field specifier \"%s\""
  7825. (match-string 0 form)))
  7826. (setq form (replace-match
  7827. (save-match-data
  7828. (org-table-make-reference x nil numbers lispp))
  7829. t t form)))
  7830. (if lispp
  7831. (setq ev (condition-case nil
  7832. (eval (eval (read form)))
  7833. (error "#ERROR"))
  7834. ev (if (numberp ev) (number-to-string ev) ev))
  7835. (or (fboundp 'calc-eval)
  7836. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  7837. (setq ev (calc-eval (cons form modes)
  7838. (if numbers 'num))))
  7839. (when org-table-formula-debug
  7840. (with-output-to-temp-buffer "*Substitution History*"
  7841. (princ (format "Substitution history of formula
  7842. Orig: %s
  7843. $xyz-> %s
  7844. @r$c-> %s
  7845. $1-> %s\n" orig formula form0 form))
  7846. (if (listp ev)
  7847. (princ (format " %s^\nError: %s"
  7848. (make-string (car ev) ?\-) (nth 1 ev)))
  7849. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  7850. ev (or fmt "NONE")
  7851. (if fmt (format fmt (string-to-number ev)) ev)))))
  7852. (setq bw (get-buffer-window "*Substitution History*"))
  7853. (shrink-window-if-larger-than-buffer bw)
  7854. (unless (and (interactive-p) (not ndown))
  7855. (unless (let (inhibit-redisplay)
  7856. (y-or-n-p "Debugging Formula. Continue to next? "))
  7857. (org-table-align)
  7858. (error "Abort"))
  7859. (delete-window bw)
  7860. (message "")))
  7861. (if (listp ev) (setq fmt nil ev "#ERROR"))
  7862. (org-table-justify-field-maybe
  7863. (if fmt (format fmt (string-to-number ev)) ev))
  7864. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  7865. (call-interactively 'org-return)
  7866. (setq ndown 0)))
  7867. (and down (org-table-maybe-recalculate-line))
  7868. (or suppress-align (and org-table-may-need-update
  7869. (org-table-align))))))
  7870. (defun org-table-put-field-property (prop value)
  7871. (save-excursion
  7872. (put-text-property (progn (skip-chars-backward "^|") (point))
  7873. (progn (skip-chars-forward "^|") (point))
  7874. prop value)))
  7875. (defun org-table-get-range (desc &optional tbeg col highlight)
  7876. "Get a calc vector from a column, accorting to descriptor DESC.
  7877. Optional arguments TBEG and COL can give the beginning of the table and
  7878. the current column, to avoid unnecessary parsing.
  7879. HIGHLIGHT means, just highlight the range."
  7880. (if (not (equal (string-to-char desc) ?@))
  7881. (setq desc (concat "@" desc)))
  7882. (save-excursion
  7883. (or tbeg (setq tbeg (org-table-begin)))
  7884. (or col (setq col (org-table-current-column)))
  7885. (let ((thisline (org-current-line))
  7886. beg end c1 c2 r1 r2 rangep tmp)
  7887. (unless (string-match org-table-range-regexp desc)
  7888. (error "Invalid table range specifier `%s'" desc))
  7889. (setq rangep (match-end 3)
  7890. r1 (and (match-end 1) (match-string 1 desc))
  7891. r2 (and (match-end 4) (match-string 4 desc))
  7892. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  7893. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  7894. (and c1 (setq c1 (+ (string-to-number c1)
  7895. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  7896. (and c2 (setq c2 (+ (string-to-number c2)
  7897. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  7898. (if (equal r1 "") (setq r1 nil))
  7899. (if (equal r2 "") (setq r2 nil))
  7900. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  7901. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  7902. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  7903. (if (not r1) (setq r1 thisline))
  7904. (if (not r2) (setq r2 thisline))
  7905. (if (not c1) (setq c1 col))
  7906. (if (not c2) (setq c2 col))
  7907. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  7908. ;; just one field
  7909. (progn
  7910. (goto-line r1)
  7911. (while (not (looking-at org-table-dataline-regexp))
  7912. (beginning-of-line 2))
  7913. (prog1 (org-table-get-field c1)
  7914. (if highlight (org-table-highlight-rectangle (point) (point)))))
  7915. ;; A range, return a vector
  7916. ;; First sort the numbers to get a regular ractangle
  7917. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  7918. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  7919. (goto-line r1)
  7920. (while (not (looking-at org-table-dataline-regexp))
  7921. (beginning-of-line 2))
  7922. (org-table-goto-column c1)
  7923. (setq beg (point))
  7924. (goto-line r2)
  7925. (while (not (looking-at org-table-dataline-regexp))
  7926. (beginning-of-line 0))
  7927. (org-table-goto-column c2)
  7928. (setq end (point))
  7929. (if highlight
  7930. (org-table-highlight-rectangle
  7931. beg (progn (skip-chars-forward "^|\n") (point))))
  7932. ;; return string representation of calc vector
  7933. (apply 'append (org-table-copy-region beg end))))))
  7934. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  7935. "Analyze descriptor DESC and retrieve the corresponding line number.
  7936. The cursor is currently in line CLINE, the table begins in line BLINE,
  7937. and TABLE is a vector with line types."
  7938. (if (string-match "^[0-9]+$" desc)
  7939. (aref org-table-dlines (string-to-number desc))
  7940. (setq cline (or cline (org-current-line))
  7941. bline (or bline org-table-current-begin-line)
  7942. table (or table org-table-current-line-types))
  7943. (if (or
  7944. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  7945. ;; 1 2 3 4 5 6
  7946. (and (not (match-end 3)) (not (match-end 6)))
  7947. (and (match-end 3) (match-end 6) (not (match-end 5))))
  7948. (error "invalid row descriptor `%s'" desc))
  7949. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  7950. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  7951. (odir (and (match-end 5) (match-string 5 desc)))
  7952. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  7953. (i (- cline bline))
  7954. (rel (and (match-end 6)
  7955. (or (and (match-end 1) (not (match-end 3)))
  7956. (match-end 5)))))
  7957. (if (and hn (not hdir))
  7958. (progn
  7959. (setq i 0 hdir "+")
  7960. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  7961. (if (and (not hn) on (not odir))
  7962. (error "should never happen");;(aref org-table-dlines on)
  7963. (if (and hn (> hn 0))
  7964. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  7965. (if on
  7966. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  7967. (+ bline i)))))
  7968. (defun org-find-row-type (table i type backwards relative n)
  7969. (let ((l (length table)))
  7970. (while (> n 0)
  7971. (while (and (setq i (+ i (if backwards -1 1)))
  7972. (>= i 0) (< i l)
  7973. (not (eq (aref table i) type))
  7974. (if (and relative (eq (aref table i) 'hline))
  7975. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  7976. t)))
  7977. (setq n (1- n)))
  7978. (if (or (< i 0) (>= i l))
  7979. (error "Row descriptior leads outside table")
  7980. i)))
  7981. (defun org-rewrite-old-row-references (s)
  7982. (if (string-match "&[-+0-9I]" s)
  7983. (error "Formula contains old &row reference, please rewrite using @-syntax")
  7984. s))
  7985. (defun org-table-make-reference (elements keep-empty numbers lispp)
  7986. "Convert list ELEMENTS to something appropriate to insert into formula.
  7987. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  7988. NUMBERS indicates that everything should be converted to numbers.
  7989. LISPP means to return something appropriate for a Lisp list."
  7990. (if (stringp elements) ; just a single val
  7991. (if lispp
  7992. (if (eq lispp 'literal)
  7993. elements
  7994. (prin1-to-string (if numbers (string-to-number elements) elements)))
  7995. (if (equal elements "") (setq elements "0"))
  7996. (if numbers (number-to-string (string-to-number elements)) elements))
  7997. (unless keep-empty
  7998. (setq elements
  7999. (delq nil
  8000. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  8001. elements))))
  8002. (setq elements (or elements '("0")))
  8003. (if lispp
  8004. (mapconcat
  8005. (lambda (x)
  8006. (if (eq lispp 'literal)
  8007. x
  8008. (prin1-to-string (if numbers (string-to-number x) x))))
  8009. " ")
  8010. (concat "[" (mapconcat
  8011. (lambda (x)
  8012. (if numbers (number-to-string (string-to-number x)) x))
  8013. elements
  8014. ",") "]"))))
  8015. (defun org-table-recalculate (&optional all noalign)
  8016. "Recalculate the current table line by applying all stored formulas.
  8017. With prefix arg ALL, do this for all lines in the table."
  8018. (interactive "P")
  8019. (or (memq this-command org-recalc-commands)
  8020. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8021. (unless (org-at-table-p) (error "Not at a table"))
  8022. (if (equal all '(16))
  8023. (org-table-iterate)
  8024. (org-table-get-specials)
  8025. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8026. (lambda (a b) (string< (car a) (car b)))))
  8027. (inhibit-redisplay (not debug-on-error))
  8028. (line-re org-table-dataline-regexp)
  8029. (thisline (org-current-line))
  8030. (thiscol (org-table-current-column))
  8031. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8032. ;; Insert constants in all formulas
  8033. (setq eqlist
  8034. (mapcar (lambda (x)
  8035. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8036. x)
  8037. eqlist))
  8038. ;; Split the equation list
  8039. (while (setq eq (pop eqlist))
  8040. (if (<= (string-to-char (car eq)) ?9)
  8041. (push eq eqlnum)
  8042. (push eq eqlname)))
  8043. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8044. (if all
  8045. (progn
  8046. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8047. (goto-char (setq beg (org-table-begin)))
  8048. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8049. ;; This is a table with marked lines, compute selected lines
  8050. (setq line-re org-table-recalculate-regexp)
  8051. ;; Move forward to the first non-header line
  8052. (if (and (re-search-forward org-table-dataline-regexp end t)
  8053. (re-search-forward org-table-hline-regexp end t)
  8054. (re-search-forward org-table-dataline-regexp end t))
  8055. (setq beg (match-beginning 0))
  8056. nil))) ;; just leave beg where it is
  8057. (setq beg (point-at-bol)
  8058. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8059. (goto-char beg)
  8060. (and all (message "Re-applying formulas to full table..."))
  8061. ;; First find the named fields, and mark them untouchanble
  8062. (remove-text-properties beg end '(org-untouchable t))
  8063. (while (setq eq (pop eqlname))
  8064. (setq name (car eq)
  8065. a (assoc name org-table-named-field-locations))
  8066. (and (not a)
  8067. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8068. (setq a (list name
  8069. (aref org-table-dlines
  8070. (string-to-number (match-string 1 name)))
  8071. (string-to-number (match-string 2 name)))))
  8072. (when (and a (or all (equal (nth 1 a) thisline)))
  8073. (message "Re-applying formula to field: %s" name)
  8074. (goto-line (nth 1 a))
  8075. (org-table-goto-column (nth 2 a))
  8076. (push (append a (list (cdr eq))) eqlname1)
  8077. ;; FIXME (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
  8078. ;; FIXME 'nostore 'noanalysis)
  8079. (org-table-put-field-property :org-untouchable t)))
  8080. ;; Now evauluate the column formulas, but skip fields covered by
  8081. ;; field formulas
  8082. (goto-char beg)
  8083. (while (re-search-forward line-re end t)
  8084. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8085. ;; Unprotected line, recalculate
  8086. (and all (message "Re-applying formulas to full table...(line %d)"
  8087. (setq cnt (1+ cnt))))
  8088. (setq org-last-recalc-line (org-current-line))
  8089. (setq eql eqlnum)
  8090. (while (setq entry (pop eql))
  8091. (goto-line org-last-recalc-line)
  8092. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8093. (unless (get-text-property (point) :org-untouchable)
  8094. (org-table-eval-formula nil (cdr entry)
  8095. 'noalign 'nocst 'nostore 'noanalysis)))))
  8096. ;; Now evaluate the field formulas
  8097. (while (setq eq (pop eqlname1))
  8098. (message "Re-applying formula to field: %s" (car eq))
  8099. (goto-line (nth 1 eq))
  8100. (org-table-goto-column (nth 2 eq))
  8101. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  8102. 'nostore 'noanalysis))
  8103. (goto-line thisline)
  8104. (org-table-goto-column thiscol)
  8105. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  8106. (or noalign (and org-table-may-need-update (org-table-align))
  8107. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  8108. ;; back to initial position
  8109. (message "Re-applying formulas...done")
  8110. (goto-line thisline)
  8111. (org-table-goto-column thiscol)
  8112. (or noalign (and org-table-may-need-update (org-table-align))
  8113. (and all (message "Re-applying formulas...done"))))))
  8114. (defun org-table-iterate (&optional arg)
  8115. "Recalculate the table until it does not change anymore."
  8116. (interactive "P")
  8117. (let ((imax (if arg (prefix-numeric-value arg) 10))
  8118. (i 0)
  8119. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  8120. thistbl)
  8121. (catch 'exit
  8122. (while (< i imax)
  8123. (setq i (1+ i))
  8124. (org-table-recalculate 'all)
  8125. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  8126. (if (not (string= lasttbl thistbl))
  8127. (setq lasttbl thistbl)
  8128. (if (> i 1)
  8129. (message "Convergence after %d iterations" i)
  8130. (message "Table was already stable"))
  8131. (throw 'exit t)))
  8132. (error "No convergence after %d iterations" i))))
  8133. (defun org-table-formula-substitute-names (f)
  8134. "Replace $const with values in string F."
  8135. (let ((start 0) a (f1 f))
  8136. ;; First, check for column names
  8137. (while (setq start (string-match org-table-column-name-regexp f start))
  8138. (setq start (1+ start))
  8139. (setq a (assoc (match-string 1 f) org-table-column-names))
  8140. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  8141. ;; Parameters and constants
  8142. (setq start 0)
  8143. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  8144. (setq start (1+ start))
  8145. (if (setq a (save-match-data
  8146. (org-table-get-constant (match-string 1 f))))
  8147. (setq f (replace-match (concat "(" a ")") t t f))))
  8148. (if org-table-formula-debug
  8149. (put-text-property 0 (length f) :orig-formula f1 f))
  8150. f))
  8151. (defun org-table-get-constant (const)
  8152. "Find the value for a parameter or constant in a formula.
  8153. Parameters get priority."
  8154. (or (cdr (assoc const org-table-local-parameters))
  8155. (cdr (assoc const org-table-formula-constants-local))
  8156. (cdr (assoc const org-table-formula-constants))
  8157. (and (fboundp 'constants-get) (constants-get const))
  8158. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  8159. (org-entry-get nil (substring const 5) 'inherit))
  8160. "#UNDEFINED_NAME"))
  8161. (defvar org-table-fedit-map (make-sparse-keymap))
  8162. (org-defkey org-table-fedit-map "\C-x\C-s" 'org-table-fedit-finish)
  8163. (org-defkey org-table-fedit-map "\C-c\C-s" 'org-table-fedit-finish)
  8164. (org-defkey org-table-fedit-map "\C-c\C-c" 'org-table-fedit-finish)
  8165. (org-defkey org-table-fedit-map "\C-c\C-q" 'org-table-fedit-abort)
  8166. (org-defkey org-table-fedit-map "\C-c?" 'org-table-show-reference)
  8167. (org-defkey org-table-fedit-map [(meta shift up)] 'org-table-fedit-line-up)
  8168. (org-defkey org-table-fedit-map [(meta shift down)] 'org-table-fedit-line-down)
  8169. (org-defkey org-table-fedit-map [(shift up)] 'org-table-fedit-ref-up)
  8170. (org-defkey org-table-fedit-map [(shift down)] 'org-table-fedit-ref-down)
  8171. (org-defkey org-table-fedit-map [(shift left)] 'org-table-fedit-ref-left)
  8172. (org-defkey org-table-fedit-map [(shift right)] 'org-table-fedit-ref-right)
  8173. (org-defkey org-table-fedit-map [(meta up)] 'org-table-fedit-scroll-down)
  8174. (org-defkey org-table-fedit-map [(meta down)] 'org-table-fedit-scroll)
  8175. (org-defkey org-table-fedit-map [(meta tab)] 'lisp-complete-symbol)
  8176. (org-defkey org-table-fedit-map "\M-\C-i" 'lisp-complete-symbol)
  8177. (org-defkey org-table-fedit-map [(tab)] 'org-table-fedit-lisp-indent)
  8178. (org-defkey org-table-fedit-map "\C-i" 'org-table-fedit-lisp-indent)
  8179. (org-defkey org-table-fedit-map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  8180. (org-defkey org-table-fedit-map "\C-c}" 'org-table-fedit-toggle-coordinates)
  8181. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  8182. '("Edit-Formulas"
  8183. ["Finish and Install" org-table-fedit-finish t]
  8184. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  8185. ["Abort" org-table-fedit-abort t]
  8186. "--"
  8187. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  8188. ["Complete Lisp Symbol" lisp-complete-symbol t]
  8189. "--"
  8190. "Shift Reference at Point"
  8191. ["Up" org-table-fedit-ref-up t]
  8192. ["Down" org-table-fedit-ref-down t]
  8193. ["Left" org-table-fedit-ref-left t]
  8194. ["Right" org-table-fedit-ref-right t]
  8195. "-"
  8196. "Change Test Row for Column Formulas"
  8197. ["Up" org-table-fedit-line-up t]
  8198. ["Down" org-table-fedit-line-down t]
  8199. "--"
  8200. ["Scroll Table Window" org-table-fedit-scroll t]
  8201. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  8202. ["Show Table Grid" org-table-fedit-toggle-coordinates
  8203. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  8204. org-table-overlay-coordinates)]
  8205. "--"
  8206. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  8207. :style toggle :selected org-table-buffer-is-an]))
  8208. (defvar org-pos)
  8209. (defun org-table-edit-formulas ()
  8210. "Edit the formulas of the current table in a separate buffer."
  8211. (interactive)
  8212. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  8213. (beginning-of-line 0))
  8214. (unless (org-at-table-p) (error "Not at a table"))
  8215. (org-table-get-specials)
  8216. (let ((key (org-table-current-field-formula 'key 'noerror))
  8217. (eql (sort (org-table-get-stored-formulas 'noerror)
  8218. 'org-table-formula-less-p))
  8219. (pos (move-marker (make-marker) (point)))
  8220. (startline 1)
  8221. (wc (current-window-configuration))
  8222. (titles '((column . "# Column Formulas\n")
  8223. (field . "# Field Formulas\n")
  8224. (named . "# Named Field Formulas\n")))
  8225. entry s type title)
  8226. (switch-to-buffer-other-window "*Edit Formulas*")
  8227. (erase-buffer)
  8228. ;; Keep global-font-lock-mode from turning on font-lock-mode
  8229. (let ((font-lock-global-modes '(not fundamental-mode)))
  8230. (fundamental-mode))
  8231. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  8232. (org-set-local 'org-pos pos)
  8233. (org-set-local 'org-window-configuration wc)
  8234. (use-local-map org-table-fedit-map)
  8235. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  8236. (easy-menu-add org-table-fedit-menu)
  8237. (setq startline (org-current-line))
  8238. (while (setq entry (pop eql))
  8239. (setq type (cond
  8240. ((equal (string-to-char (car entry)) ?@) 'field)
  8241. ((string-match "^[0-9]" (car entry)) 'column)
  8242. (t 'named)))
  8243. (when (setq title (assq type titles))
  8244. (or (bobp) (insert "\n"))
  8245. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  8246. (setq titles (delq title titles)))
  8247. (if (equal key (car entry)) (setq startline (org-current-line)))
  8248. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  8249. (car entry) " = " (cdr entry) "\n"))
  8250. (remove-text-properties 0 (length s) '(face nil) s)
  8251. (insert s))
  8252. (if (eq org-table-use-standard-references t)
  8253. (org-table-fedit-toggle-ref-type))
  8254. (goto-line startline)
  8255. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  8256. (defun org-table-fedit-post-command ()
  8257. (when (not (memq this-command '(lisp-complete-symbol)))
  8258. (let ((win (selected-window)))
  8259. (save-excursion
  8260. (condition-case nil
  8261. (org-table-show-reference)
  8262. (error nil))
  8263. (select-window win)))))
  8264. (defun org-table-formula-to-user (s)
  8265. "Convert a formula from internal to user representation."
  8266. (if (eq org-table-use-standard-references t)
  8267. (org-table-convert-refs-to-an s)
  8268. s))
  8269. (defun org-table-formula-from-user (s)
  8270. "Convert a formula from user to internal representation."
  8271. (if org-table-use-standard-references
  8272. (org-table-convert-refs-to-rc s)
  8273. s))
  8274. (defun org-table-convert-refs-to-rc (s)
  8275. "Convert spreadsheet references from AB7 to @7$28.
  8276. Works for single references, but also for entire formulas and even the
  8277. full TBLFM line."
  8278. (let ((start 0))
  8279. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  8280. (cond
  8281. ((match-end 3)
  8282. ;; format match, just advance
  8283. (setq start (match-end 0)))
  8284. ((and (> (match-beginning 0) 0)
  8285. (equal ?. (aref s (max (1- (match-beginning 0)) 0))))
  8286. ;; 3.e5 or something like this. FIXME: is this ok????
  8287. (setq start (match-end 0)))
  8288. (t
  8289. (setq start (match-beginning 0)
  8290. s (replace-match
  8291. (if (equal (match-string 2 s) "&")
  8292. (format "$%d" (org-letters-to-number (match-string 1 s)))
  8293. (format "@%d$%d"
  8294. (string-to-number (match-string 2 s))
  8295. (org-letters-to-number (match-string 1 s))))
  8296. t t s)))))
  8297. s))
  8298. (defun org-table-convert-refs-to-an (s)
  8299. "Convert spreadsheet references from to @7$28 to AB7.
  8300. Works for single references, but also for entire formulas and even the
  8301. full TBLFM line."
  8302. (while (string-match "@\\([0-9]+\\)$\\([0-9]+\\)" s)
  8303. (setq s (replace-match
  8304. (format "%s%d"
  8305. (org-number-to-letters
  8306. (string-to-number (match-string 2 s)))
  8307. (string-to-number (match-string 1 s)))
  8308. t t s)))
  8309. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  8310. (setq s (replace-match (concat "\\1"
  8311. (org-number-to-letters
  8312. (string-to-number (match-string 2 s))) "&")
  8313. t nil s)))
  8314. s)
  8315. (defun org-letters-to-number (s)
  8316. "Convert a base 26 number represented by letters into an integer.
  8317. For example: AB -> 28."
  8318. (let ((n 0))
  8319. (setq s (upcase s))
  8320. (while (> (length s) 0)
  8321. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  8322. s (substring s 1)))
  8323. n))
  8324. (defun org-number-to-letters (n)
  8325. "Convert an integer into a base 26 number represented by letters.
  8326. For example: 28 -> AB."
  8327. (let ((s ""))
  8328. (while (> n 0)
  8329. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  8330. n (/ (1- n) 26)))
  8331. s))
  8332. (defun org-table-fedit-convert-buffer (function)
  8333. "Convert all references in this buffer, using FUNTION."
  8334. (let ((line (org-current-line)))
  8335. (goto-char (point-min))
  8336. (while (not (eobp))
  8337. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  8338. (delete-region (point) (point-at-eol))
  8339. (or (eobp) (forward-char 1)))
  8340. (goto-line line)))
  8341. (defun org-table-fedit-toggle-ref-type ()
  8342. "Convert all references in the buffer from B3 to @3$2 and back."
  8343. (interactive)
  8344. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  8345. (org-table-fedit-convert-buffer
  8346. (if org-table-buffer-is-an
  8347. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  8348. (message "Reference type switched to %s"
  8349. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  8350. (defun org-table-fedit-ref-up ()
  8351. "Shift the reference at point one row/hline up."
  8352. (interactive)
  8353. (org-table-fedit-shift-reference 'up))
  8354. (defun org-table-fedit-ref-down ()
  8355. "Shift the reference at point one row/hline down."
  8356. (interactive)
  8357. (org-table-fedit-shift-reference 'down))
  8358. (defun org-table-fedit-ref-left ()
  8359. "Shift the reference at point one field to the left."
  8360. (interactive)
  8361. (org-table-fedit-shift-reference 'left))
  8362. (defun org-table-fedit-ref-right ()
  8363. "Shift the reference at point one field to the right."
  8364. (interactive)
  8365. (org-table-fedit-shift-reference 'right))
  8366. (defun org-table-fedit-shift-reference (dir)
  8367. (cond
  8368. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  8369. (if (memq dir '(left right))
  8370. (org-rematch-and-replace 1 (eq dir 'left))
  8371. (error "Cannot shift reference in this direction")))
  8372. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  8373. ;; A B3-like reference
  8374. (if (memq dir '(up down))
  8375. (org-rematch-and-replace 2 (eq dir 'up))
  8376. (org-rematch-and-replace 1 (eq dir 'left))))
  8377. ((org-at-regexp-p
  8378. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  8379. ;; An internal reference
  8380. (if (memq dir '(up down))
  8381. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  8382. (org-rematch-and-replace 5 (eq dir 'left))))))
  8383. (defun org-rematch-and-replace (n &optional decr hline)
  8384. "Re-match the group N, and replace it with the shifted refrence."
  8385. (or (match-end n) (error "Cannot shift reference in this direction"))
  8386. (goto-char (match-beginning n))
  8387. (and (looking-at (regexp-quote (match-string n)))
  8388. (replace-match (org-shift-refpart (match-string 0) decr hline)
  8389. t t)))
  8390. (defun org-shift-refpart (ref &optional decr hline)
  8391. "Shift a refrence part REF.
  8392. If DECR is set, decrease the references row/column, else increase.
  8393. If HLINE is set, this may be a hline reference, it certainly is not
  8394. a translation reference."
  8395. (save-match-data
  8396. (let* ((sign (string-match "^[-+]" ref)) n)
  8397. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  8398. (cond
  8399. ((and hline (string-match "^I+" ref))
  8400. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  8401. (setq n (+ n (if decr -1 1)))
  8402. (if (= n 0) (setq n (+ n (if decr -1 1))))
  8403. (if sign
  8404. (setq sign (if (< n 0) "-" "+") n (abs n))
  8405. (setq n (max 1 n)))
  8406. (concat sign (make-string n ?I)))
  8407. ((string-match "^[0-9]+" ref)
  8408. (setq n (string-to-number (concat sign ref)))
  8409. (setq n (+ n (if decr -1 1)))
  8410. (if sign
  8411. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  8412. (number-to-string (max 1 n))))
  8413. ((string-match "^[a-zA-Z]+" ref)
  8414. (org-number-to-letters
  8415. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  8416. (t (error "Cannot shift reference"))))))
  8417. (defun org-table-fedit-toggle-coordinates ()
  8418. "Toggle the display of coordinates in the refrenced table."
  8419. (interactive)
  8420. (let ((pos (marker-position org-pos)))
  8421. (with-current-buffer (marker-buffer org-pos)
  8422. (save-excursion
  8423. (goto-char pos)
  8424. (org-table-toggle-coordinate-overlays)))))
  8425. (defun org-table-fedit-finish (&optional arg)
  8426. "Parse the buffer for formula definitions and install them.
  8427. With prefix ARG, apply the new formulas to the table."
  8428. (interactive "P")
  8429. (org-table-remove-rectangle-highlight)
  8430. (if org-table-use-standard-references
  8431. (progn
  8432. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  8433. (setq org-table-buffer-is-an nil)))
  8434. (let ((pos org-pos) eql var form)
  8435. (goto-char (point-min))
  8436. (while (re-search-forward
  8437. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  8438. nil t)
  8439. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  8440. form (match-string 3))
  8441. (setq form (org-trim form))
  8442. (when (not (equal form ""))
  8443. (while (string-match "[ \t]*\n[ \t]*" form)
  8444. (setq form (replace-match " " t t form)))
  8445. (when (assoc var eql)
  8446. (error "Double formulas for %s" var))
  8447. (push (cons var form) eql)))
  8448. (setq org-pos nil)
  8449. (set-window-configuration org-window-configuration)
  8450. (select-window (get-buffer-window (marker-buffer pos)))
  8451. (goto-char pos)
  8452. (unless (org-at-table-p)
  8453. (error "Lost table position - cannot install formulae"))
  8454. (org-table-store-formulas eql)
  8455. (move-marker pos nil)
  8456. (kill-buffer "*Edit Formulas*")
  8457. (if arg
  8458. (org-table-recalculate 'all)
  8459. (message "New formulas installed - press C-u C-c C-c to apply."))))
  8460. (defun org-table-fedit-abort ()
  8461. "Abort editing formulas, without installing the changes."
  8462. (interactive)
  8463. (org-table-remove-rectangle-highlight)
  8464. (let ((pos org-pos))
  8465. (set-window-configuration org-window-configuration)
  8466. (select-window (get-buffer-window (marker-buffer pos)))
  8467. (goto-char pos)
  8468. (move-marker pos nil)
  8469. (message "Formula editing aborted without installing changes")))
  8470. (defun org-table-fedit-lisp-indent ()
  8471. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  8472. (interactive)
  8473. (let ((pos (point)) beg end ind)
  8474. (beginning-of-line 1)
  8475. (cond
  8476. ((looking-at "[ \t]")
  8477. (goto-char pos)
  8478. (call-interactively 'lisp-indent-line))
  8479. ((looking-at "[$@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  8480. ((not (fboundp 'pp-buffer))
  8481. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  8482. ((looking-at "[$@0-9a-zA-Z]+ *= *'(")
  8483. (goto-char (- (match-end 0) 2))
  8484. (setq beg (point))
  8485. (setq ind (make-string (current-column) ?\ ))
  8486. (condition-case nil (forward-sexp 1)
  8487. (error
  8488. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  8489. (setq end (point))
  8490. (save-restriction
  8491. (narrow-to-region beg end)
  8492. (if (eq last-command this-command)
  8493. (progn
  8494. (goto-char (point-min))
  8495. (setq this-command nil)
  8496. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  8497. (replace-match " ")))
  8498. (pp-buffer)
  8499. (untabify (point-min) (point-max))
  8500. (goto-char (1+ (point-min)))
  8501. (while (re-search-forward "^." nil t)
  8502. (beginning-of-line 1)
  8503. (insert ind))
  8504. (goto-char (point-max))
  8505. (backward-delete-char 1)))
  8506. (goto-char beg))
  8507. (t nil))))
  8508. (defvar org-show-positions nil)
  8509. (defun org-table-show-reference (&optional local)
  8510. "Show the location/value of the $ expression at point."
  8511. (interactive)
  8512. (org-table-remove-rectangle-highlight)
  8513. (catch 'exit
  8514. (let ((pos (if local (point) org-pos))
  8515. (face2 'highlight)
  8516. (org-inhibit-highlight-removal t)
  8517. (win (selected-window))
  8518. (org-show-positions nil)
  8519. var name e what match dest)
  8520. (if local (org-table-get-specials))
  8521. (setq what (cond
  8522. ((or (org-at-regexp-p org-table-range-regexp2)
  8523. (org-at-regexp-p org-table-translate-regexp)
  8524. (org-at-regexp-p org-table-range-regexp))
  8525. (setq match
  8526. (save-match-data
  8527. (org-table-convert-refs-to-rc (match-string 0))))
  8528. 'range)
  8529. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  8530. ((org-at-regexp-p "\\$[0-9]+") 'column)
  8531. ((not local) nil)
  8532. (t (error "No reference at point")))
  8533. match (and what (or match (match-string 0))))
  8534. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  8535. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  8536. 'secondary-selection))
  8537. (org-add-hook 'before-change-functions
  8538. 'org-table-remove-rectangle-highlight)
  8539. (if (eq what 'name) (setq var (substring match 1)))
  8540. (when (eq what 'range)
  8541. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  8542. (setq match (org-table-formula-substitute-names match)))
  8543. (unless local
  8544. (save-excursion
  8545. (end-of-line 1)
  8546. (re-search-backward "^\\S-" nil t)
  8547. (beginning-of-line 1)
  8548. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  8549. (setq dest
  8550. (save-match-data
  8551. (org-table-convert-refs-to-rc (match-string 1))))
  8552. (org-table-add-rectangle-overlay
  8553. (match-beginning 1) (match-end 1) face2))))
  8554. (if (and (markerp pos) (marker-buffer pos))
  8555. (if (get-buffer-window (marker-buffer pos))
  8556. (select-window (get-buffer-window (marker-buffer pos)))
  8557. (switch-to-buffer-other-window (get-buffer-window
  8558. (marker-buffer pos)))))
  8559. (goto-char pos)
  8560. (org-table-force-dataline)
  8561. (when dest
  8562. (setq name (substring dest 1))
  8563. (cond
  8564. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  8565. (setq e (assoc name org-table-named-field-locations))
  8566. (goto-line (nth 1 e))
  8567. (org-table-goto-column (nth 2 e)))
  8568. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  8569. (let ((l (string-to-number (match-string 1 dest)))
  8570. (c (string-to-number (match-string 2 dest))))
  8571. (goto-line (aref org-table-dlines l))
  8572. (org-table-goto-column c)))
  8573. (t (org-table-goto-column (string-to-number name))))
  8574. (move-marker pos (point))
  8575. (org-table-highlight-rectangle nil nil face2))
  8576. (cond
  8577. ((equal dest match))
  8578. ((not match))
  8579. ((eq what 'range)
  8580. (condition-case nil
  8581. (save-excursion
  8582. (org-table-get-range match nil nil 'highlight))
  8583. (error nil)))
  8584. ((setq e (assoc var org-table-named-field-locations))
  8585. (goto-line (nth 1 e))
  8586. (org-table-goto-column (nth 2 e))
  8587. (org-table-highlight-rectangle (point) (point))
  8588. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  8589. ((setq e (assoc var org-table-column-names))
  8590. (org-table-goto-column (string-to-number (cdr e)))
  8591. (org-table-highlight-rectangle (point) (point))
  8592. (goto-char (org-table-begin))
  8593. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  8594. (org-table-end) t)
  8595. (progn
  8596. (goto-char (match-beginning 1))
  8597. (org-table-highlight-rectangle)
  8598. (message "Named column (column %s)" (cdr e)))
  8599. (error "Column name not found")))
  8600. ((eq what 'column)
  8601. ;; column number
  8602. (org-table-goto-column (string-to-number (substring match 1)))
  8603. (org-table-highlight-rectangle (point) (point))
  8604. (message "Column %s" (substring match 1)))
  8605. ((setq e (assoc var org-table-local-parameters))
  8606. (goto-char (org-table-begin))
  8607. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  8608. (progn
  8609. (goto-char (match-beginning 1))
  8610. (org-table-highlight-rectangle)
  8611. (message "Local parameter."))
  8612. (error "Parameter not found")))
  8613. (t
  8614. (cond
  8615. ((not var) (error "No reference at point"))
  8616. ((setq e (assoc var org-table-formula-constants-local))
  8617. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  8618. var (cdr e)))
  8619. ((setq e (assoc var org-table-formula-constants))
  8620. (message "Constant: $%s=%s in `org-table-formula-constants'."
  8621. var (cdr e)))
  8622. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  8623. (message "Constant: $%s=%s, from `constants.el'%s."
  8624. var e (format " (%s units)" constants-unit-system)))
  8625. (t (error "Undefined name $%s" var)))))
  8626. (goto-char pos)
  8627. (when (and org-show-positions
  8628. (not (memq this-command '(org-table-fedit-scroll
  8629. org-table-fedit-scroll-down))))
  8630. (push pos org-show-positions)
  8631. (push org-table-current-begin-pos org-show-positions)
  8632. (let ((min (apply 'min org-show-positions))
  8633. (max (apply 'max org-show-positions)))
  8634. (goto-char min) (recenter 0)
  8635. (goto-char max)
  8636. (or (pos-visible-in-window-p max) (recenter -1))))
  8637. (select-window win))))
  8638. (defun org-table-force-dataline ()
  8639. "Make sure the cursor is in a dataline in a table."
  8640. (unless (save-excursion
  8641. (beginning-of-line 1)
  8642. (looking-at org-table-dataline-regexp))
  8643. (let* ((re org-table-dataline-regexp)
  8644. (p1 (save-excursion (re-search-forward re nil 'move)))
  8645. (p2 (save-excursion (re-search-backward re nil 'move))))
  8646. (cond ((and p1 p2)
  8647. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  8648. p1 p2)))
  8649. ((or p1 p2) (goto-char (or p1 p2)))
  8650. (t (error "No table dataline around here"))))))
  8651. (defun org-table-fedit-line-up ()
  8652. "Move cursor one line up in the window showing the table."
  8653. (interactive)
  8654. (org-table-fedit-move 'previous-line))
  8655. (defun org-table-fedit-line-down ()
  8656. "Move cursor one line down in the window showing the table."
  8657. (interactive)
  8658. (org-table-fedit-move 'next-line))
  8659. (defun org-table-fedit-move (command)
  8660. "Move the cursor in the window shoinw the table.
  8661. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  8662. (let ((org-table-allow-automatic-line-recalculation nil)
  8663. (pos org-pos) (win (selected-window)) p)
  8664. (select-window (get-buffer-window (marker-buffer org-pos)))
  8665. (setq p (point))
  8666. (call-interactively command)
  8667. (while (and (org-at-table-p)
  8668. (org-at-table-hline-p))
  8669. (call-interactively command))
  8670. (or (org-at-table-p) (goto-char p))
  8671. (move-marker pos (point))
  8672. (select-window win)))
  8673. (defun org-table-fedit-scroll (N)
  8674. (interactive "p")
  8675. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  8676. (scroll-other-window N)))
  8677. (defun org-table-fedit-scroll-down (N)
  8678. (interactive "p")
  8679. (org-table-fedit-scroll (- N)))
  8680. (defvar org-table-rectangle-overlays nil)
  8681. (defun org-table-add-rectangle-overlay (beg end &optional face)
  8682. "Add a new overlay."
  8683. (let ((ov (org-make-overlay beg end)))
  8684. (org-overlay-put ov 'face (or face 'secondary-selection))
  8685. (push ov org-table-rectangle-overlays)))
  8686. (defun org-table-highlight-rectangle (&optional beg end face)
  8687. "Highlight rectangular region in a table."
  8688. (setq beg (or beg (point)) end (or end (point)))
  8689. (let ((b (min beg end))
  8690. (e (max beg end))
  8691. l1 c1 l2 c2 tmp)
  8692. (and (boundp 'org-show-positions)
  8693. (setq org-show-positions (cons b (cons e org-show-positions))))
  8694. (goto-char (min beg end))
  8695. (setq l1 (org-current-line)
  8696. c1 (org-table-current-column))
  8697. (goto-char (max beg end))
  8698. (setq l2 (org-current-line)
  8699. c2 (org-table-current-column))
  8700. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  8701. (goto-line l1)
  8702. (beginning-of-line 1)
  8703. (loop for line from l1 to l2 do
  8704. (when (looking-at org-table-dataline-regexp)
  8705. (org-table-goto-column c1)
  8706. (skip-chars-backward "^|\n") (setq beg (point))
  8707. (org-table-goto-column c2)
  8708. (skip-chars-forward "^|\n") (setq end (point))
  8709. (org-table-add-rectangle-overlay beg end face))
  8710. (beginning-of-line 2))
  8711. (goto-char b))
  8712. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  8713. (defun org-table-remove-rectangle-highlight (&rest ignore)
  8714. "Remove the rectangle overlays."
  8715. (unless org-inhibit-highlight-removal
  8716. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  8717. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  8718. (setq org-table-rectangle-overlays nil)))
  8719. (defvar org-table-coordinate-overlays nil
  8720. "Collects the cooordinate grid overlays, so that they can be removed.")
  8721. (make-variable-buffer-local 'org-table-coordinate-overlays)
  8722. (defun org-table-overlay-coordinates ()
  8723. "Add overlays to the table at point, to show row/column coordinates."
  8724. (interactive)
  8725. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8726. (setq org-table-coordinate-overlays nil)
  8727. (save-excursion
  8728. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  8729. (goto-char (org-table-begin))
  8730. (while (org-at-table-p)
  8731. (setq eol (point-at-eol))
  8732. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  8733. (push ov org-table-coordinate-overlays)
  8734. (setq hline (looking-at org-table-hline-regexp))
  8735. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  8736. (format "%4d" (setq id (1+ id)))))
  8737. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  8738. (when hline
  8739. (setq ic 0)
  8740. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  8741. (setq beg (1+ (match-beginning 0))
  8742. ic (1+ ic)
  8743. s1 (concat "$" (int-to-string ic))
  8744. s2 (org-number-to-letters ic)
  8745. str (if (eq org-table-use-standard-references t) s2 s1))
  8746. (setq ov (org-make-overlay beg (+ beg (length str))))
  8747. (push ov org-table-coordinate-overlays)
  8748. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  8749. (beginning-of-line 2)))))
  8750. (defun org-table-toggle-coordinate-overlays ()
  8751. "Toggle the display of Row/Column numbers in tables."
  8752. (interactive)
  8753. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  8754. (message "Row/Column number display turned %s"
  8755. (if org-table-overlay-coordinates "on" "off"))
  8756. (if (and (org-at-table-p) org-table-overlay-coordinates)
  8757. (org-table-align))
  8758. (unless org-table-overlay-coordinates
  8759. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8760. (setq org-table-coordinate-overlays nil)))
  8761. (defun org-table-toggle-formula-debugger ()
  8762. "Toggle the formula debugger in tables."
  8763. (interactive)
  8764. (setq org-table-formula-debug (not org-table-formula-debug))
  8765. (message "Formula debugging has been turned %s"
  8766. (if org-table-formula-debug "on" "off")))
  8767. ;;; The orgtbl minor mode
  8768. ;; Define a minor mode which can be used in other modes in order to
  8769. ;; integrate the org-mode table editor.
  8770. ;; This is really a hack, because the org-mode table editor uses several
  8771. ;; keys which normally belong to the major mode, for example the TAB and
  8772. ;; RET keys. Here is how it works: The minor mode defines all the keys
  8773. ;; necessary to operate the table editor, but wraps the commands into a
  8774. ;; function which tests if the cursor is currently inside a table. If that
  8775. ;; is the case, the table editor command is executed. However, when any of
  8776. ;; those keys is used outside a table, the function uses `key-binding' to
  8777. ;; look up if the key has an associated command in another currently active
  8778. ;; keymap (minor modes, major mode, global), and executes that command.
  8779. ;; There might be problems if any of the keys used by the table editor is
  8780. ;; otherwise used as a prefix key.
  8781. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  8782. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  8783. ;; addresses this by checking explicitly for both bindings.
  8784. ;; The optimized version (see variable `orgtbl-optimized') takes over
  8785. ;; all keys which are bound to `self-insert-command' in the *global map*.
  8786. ;; Some modes bind other commands to simple characters, for example
  8787. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  8788. ;; active, this binding is ignored inside tables and replaced with a
  8789. ;; modified self-insert.
  8790. (defvar orgtbl-mode nil
  8791. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  8792. table editor in arbitrary modes.")
  8793. (make-variable-buffer-local 'orgtbl-mode)
  8794. (defvar orgtbl-mode-map (make-keymap)
  8795. "Keymap for `orgtbl-mode'.")
  8796. ;;;###autoload
  8797. (defun turn-on-orgtbl ()
  8798. "Unconditionally turn on `orgtbl-mode'."
  8799. (orgtbl-mode 1))
  8800. (defvar org-old-auto-fill-inhibit-regexp nil
  8801. "Local variable used by `orgtbl-mode'")
  8802. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  8803. "Matches a line belonging to an orgtbl.")
  8804. (defconst orgtbl-extra-font-lock-keywords
  8805. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  8806. 0 (quote 'org-table) 'prepend))
  8807. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  8808. ;;;###autoload
  8809. (defun orgtbl-mode (&optional arg)
  8810. "The `org-mode' table editor as a minor mode for use in other modes."
  8811. (interactive)
  8812. (if (org-mode-p)
  8813. ;; Exit without error, in case some hook functions calls this
  8814. ;; by accident in org-mode.
  8815. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  8816. (setq orgtbl-mode
  8817. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  8818. (if orgtbl-mode
  8819. (progn
  8820. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  8821. ;; Make sure we are first in minor-mode-map-alist
  8822. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  8823. (and c (setq minor-mode-map-alist
  8824. (cons c (delq c minor-mode-map-alist)))))
  8825. (org-set-local (quote org-table-may-need-update) t)
  8826. (org-add-hook 'before-change-functions 'org-before-change-function
  8827. nil 'local)
  8828. (org-set-local 'org-old-auto-fill-inhibit-regexp
  8829. auto-fill-inhibit-regexp)
  8830. (org-set-local 'auto-fill-inhibit-regexp
  8831. (if auto-fill-inhibit-regexp
  8832. (concat orgtbl-line-start-regexp "\\|"
  8833. auto-fill-inhibit-regexp)
  8834. orgtbl-line-start-regexp))
  8835. (org-add-to-invisibility-spec '(org-cwidth))
  8836. (when (fboundp 'font-lock-add-keywords)
  8837. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  8838. (org-restart-font-lock))
  8839. (easy-menu-add orgtbl-mode-menu)
  8840. (run-hooks 'orgtbl-mode-hook))
  8841. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  8842. (org-cleanup-narrow-column-properties)
  8843. (org-remove-from-invisibility-spec '(org-cwidth))
  8844. (remove-hook 'before-change-functions 'org-before-change-function t)
  8845. (when (fboundp 'font-lock-remove-keywords)
  8846. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  8847. (org-restart-font-lock))
  8848. (easy-menu-remove orgtbl-mode-menu)
  8849. (force-mode-line-update 'all))))
  8850. (defun org-cleanup-narrow-column-properties ()
  8851. "Remove all properties related to narrow-column invisibility."
  8852. (let ((s 1))
  8853. (while (setq s (text-property-any s (point-max)
  8854. 'display org-narrow-column-arrow))
  8855. (remove-text-properties s (1+ s) '(display t)))
  8856. (setq s 1)
  8857. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  8858. (remove-text-properties s (1+ s) '(org-cwidth t)))
  8859. (setq s 1)
  8860. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  8861. (remove-text-properties s (1+ s) '(invisible t)))))
  8862. ;; Install it as a minor mode.
  8863. (put 'orgtbl-mode :included t)
  8864. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  8865. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  8866. (defun orgtbl-make-binding (fun n &rest keys)
  8867. "Create a function for binding in the table minor mode.
  8868. FUN is the command to call inside a table. N is used to create a unique
  8869. command name. KEYS are keys that should be checked in for a command
  8870. to execute outside of tables."
  8871. (eval
  8872. (list 'defun
  8873. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  8874. '(arg)
  8875. (concat "In tables, run `" (symbol-name fun) "'.\n"
  8876. "Outside of tables, run the binding of `"
  8877. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  8878. "'.")
  8879. '(interactive "p")
  8880. (list 'if
  8881. '(org-at-table-p)
  8882. (list 'call-interactively (list 'quote fun))
  8883. (list 'let '(orgtbl-mode)
  8884. (list 'call-interactively
  8885. (append '(or)
  8886. (mapcar (lambda (k)
  8887. (list 'key-binding k))
  8888. keys)
  8889. '('orgtbl-error))))))))
  8890. (defun orgtbl-error ()
  8891. "Error when there is no default binding for a table key."
  8892. (interactive)
  8893. (error "This key is has no function outside tables"))
  8894. (defun orgtbl-setup ()
  8895. "Setup orgtbl keymaps."
  8896. (let ((nfunc 0)
  8897. (bindings
  8898. (list
  8899. '([(meta shift left)] org-table-delete-column)
  8900. '([(meta left)] org-table-move-column-left)
  8901. '([(meta right)] org-table-move-column-right)
  8902. '([(meta shift right)] org-table-insert-column)
  8903. '([(meta shift up)] org-table-kill-row)
  8904. '([(meta shift down)] org-table-insert-row)
  8905. '([(meta up)] org-table-move-row-up)
  8906. '([(meta down)] org-table-move-row-down)
  8907. '("\C-c\C-w" org-table-cut-region)
  8908. '("\C-c\M-w" org-table-copy-region)
  8909. '("\C-c\C-y" org-table-paste-rectangle)
  8910. '("\C-c-" org-table-insert-hline)
  8911. '("\C-c}" org-table-toggle-coordinate-overlays)
  8912. '("\C-c{" org-table-toggle-formula-debugger)
  8913. '("\C-m" org-table-next-row)
  8914. '([(shift return)] org-table-copy-down)
  8915. '("\C-c\C-q" org-table-wrap-region)
  8916. '("\C-c?" org-table-field-info)
  8917. '("\C-c " org-table-blank-field)
  8918. '("\C-c+" org-table-sum)
  8919. '("\C-c=" org-table-eval-formula)
  8920. '("\C-c'" org-table-edit-formulas)
  8921. '("\C-c`" org-table-edit-field)
  8922. '("\C-c*" org-table-recalculate)
  8923. '("\C-c|" org-table-create-or-convert-from-region)
  8924. '("\C-c^" org-table-sort-lines)
  8925. '([(control ?#)] org-table-rotate-recalc-marks)))
  8926. elt key fun cmd)
  8927. (while (setq elt (pop bindings))
  8928. (setq nfunc (1+ nfunc))
  8929. (setq key (org-key (car elt))
  8930. fun (nth 1 elt)
  8931. cmd (orgtbl-make-binding fun nfunc key))
  8932. (org-defkey orgtbl-mode-map key cmd))
  8933. ;; Special treatment needed for TAB and RET
  8934. (org-defkey orgtbl-mode-map [(return)]
  8935. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  8936. (org-defkey orgtbl-mode-map "\C-m"
  8937. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  8938. (org-defkey orgtbl-mode-map [(tab)]
  8939. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  8940. (org-defkey orgtbl-mode-map "\C-i"
  8941. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  8942. (org-defkey orgtbl-mode-map [(shift tab)]
  8943. (orgtbl-make-binding 'org-table-previous-field 104
  8944. [(shift tab)] [(tab)] "\C-i"))
  8945. (org-defkey orgtbl-mode-map "\M-\C-m"
  8946. (orgtbl-make-binding 'org-table-wrap-region 105
  8947. "\M-\C-m" [(meta return)]))
  8948. (org-defkey orgtbl-mode-map [(meta return)]
  8949. (orgtbl-make-binding 'org-table-wrap-region 106
  8950. [(meta return)] "\M-\C-m"))
  8951. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  8952. (when orgtbl-optimized
  8953. ;; If the user wants maximum table support, we need to hijack
  8954. ;; some standard editing functions
  8955. (org-remap orgtbl-mode-map
  8956. 'self-insert-command 'orgtbl-self-insert-command
  8957. 'delete-char 'org-delete-char
  8958. 'delete-backward-char 'org-delete-backward-char)
  8959. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  8960. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  8961. '("OrgTbl"
  8962. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  8963. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  8964. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  8965. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  8966. "--"
  8967. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  8968. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  8969. ["Copy Field from Above"
  8970. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  8971. "--"
  8972. ("Column"
  8973. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  8974. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  8975. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  8976. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  8977. ("Row"
  8978. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  8979. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  8980. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  8981. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  8982. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  8983. "--"
  8984. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  8985. ("Rectangle"
  8986. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  8987. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  8988. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  8989. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  8990. "--"
  8991. ("Radio tables"
  8992. ["Insert table template" orgtbl-insert-radio-table
  8993. (assq major-mode orgtbl-radio-table-templates)]
  8994. ["Comment/uncomment table" orgtbl-toggle-comment t])
  8995. "--"
  8996. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  8997. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  8998. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  8999. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9000. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9001. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  9002. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9003. ["Sum Column/Rectangle" org-table-sum
  9004. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9005. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9006. ["Debug Formulas"
  9007. org-table-toggle-formula-debugger :active (org-at-table-p)
  9008. :keys "C-c {"
  9009. :style toggle :selected org-table-formula-debug]
  9010. ["Show Col/Row Numbers"
  9011. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9012. :keys "C-c }"
  9013. :style toggle :selected org-table-overlay-coordinates]
  9014. ))
  9015. t))
  9016. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9017. "If the cursor is inside a table, realign the table.
  9018. It it is a table to be sent away to a receiver, do it.
  9019. With prefix arg, also recompute table."
  9020. (interactive "P")
  9021. (let ((pos (point)) action)
  9022. (save-excursion
  9023. (beginning-of-line 1)
  9024. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9025. ((looking-at "[ \t]*|") pos)
  9026. ((looking-at "#\\+TBLFM:") 'recalc))))
  9027. (cond
  9028. ((integerp action)
  9029. (goto-char action)
  9030. (org-table-maybe-eval-formula)
  9031. (if arg
  9032. (call-interactively 'org-table-recalculate)
  9033. (org-table-maybe-recalculate-line))
  9034. (call-interactively 'org-table-align)
  9035. (orgtbl-send-table 'maybe))
  9036. ((eq action 'recalc)
  9037. (save-excursion
  9038. (beginning-of-line 1)
  9039. (skip-chars-backward " \r\n\t")
  9040. (if (org-at-table-p)
  9041. (org-call-with-arg 'org-table-recalculate t))))
  9042. (t (let (orgtbl-mode)
  9043. (call-interactively (key-binding "\C-c\C-c")))))))
  9044. (defun orgtbl-tab (arg)
  9045. "Justification and field motion for `orgtbl-mode'."
  9046. (interactive "P")
  9047. (if arg (org-table-edit-field t)
  9048. (org-table-justify-field-maybe)
  9049. (org-table-next-field)))
  9050. (defun orgtbl-ret ()
  9051. "Justification and field motion for `orgtbl-mode'."
  9052. (interactive)
  9053. (org-table-justify-field-maybe)
  9054. (org-table-next-row))
  9055. (defun orgtbl-self-insert-command (N)
  9056. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9057. If the cursor is in a table looking at whitespace, the whitespace is
  9058. overwritten, and the table is not marked as requiring realignment."
  9059. (interactive "p")
  9060. (if (and (org-at-table-p)
  9061. (or
  9062. (and org-table-auto-blank-field
  9063. (member last-command
  9064. '(orgtbl-hijacker-command-100
  9065. orgtbl-hijacker-command-101
  9066. orgtbl-hijacker-command-102
  9067. orgtbl-hijacker-command-103
  9068. orgtbl-hijacker-command-104
  9069. orgtbl-hijacker-command-105))
  9070. (org-table-blank-field))
  9071. t)
  9072. (eq N 1)
  9073. (looking-at "[^|\n]* +|"))
  9074. (let (org-table-may-need-update)
  9075. (goto-char (1- (match-end 0)))
  9076. (delete-backward-char 1)
  9077. (goto-char (match-beginning 0))
  9078. (self-insert-command N))
  9079. (setq org-table-may-need-update t)
  9080. (let (orgtbl-mode)
  9081. (call-interactively (key-binding (vector last-input-event))))))
  9082. (defun org-force-self-insert (N)
  9083. "Needed to enforce self-insert under remapping."
  9084. (interactive "p")
  9085. (self-insert-command N))
  9086. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9087. "Regula expression matching exponentials as produced by calc.")
  9088. (defvar org-table-clean-did-remove-column nil)
  9089. (defun orgtbl-export (table target)
  9090. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9091. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9092. org-table-last-alignment org-table-last-column-widths
  9093. maxcol column)
  9094. (if (not (fboundp func))
  9095. (error "Cannot export orgtbl table to %s" target))
  9096. (setq lines (org-table-clean-before-export lines))
  9097. (setq table
  9098. (mapcar
  9099. (lambda (x)
  9100. (if (string-match org-table-hline-regexp x)
  9101. 'hline
  9102. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  9103. lines))
  9104. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  9105. table)))
  9106. (loop for i from (1- maxcol) downto 0 do
  9107. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  9108. (setq column (delq nil column))
  9109. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  9110. (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))
  9111. (funcall func table nil)))
  9112. (defun orgtbl-send-table (&optional maybe)
  9113. "Send a tranformed version of this table to the receiver position.
  9114. With argument MAYBE, fail quietly if no transformation is defined for
  9115. this table."
  9116. (interactive)
  9117. (catch 'exit
  9118. (unless (org-at-table-p) (error "Not at a table"))
  9119. ;; when non-interactive, we assume align has just happened.
  9120. (when (interactive-p) (org-table-align))
  9121. (save-excursion
  9122. (goto-char (org-table-begin))
  9123. (beginning-of-line 0)
  9124. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  9125. (if maybe
  9126. (throw 'exit nil)
  9127. (error "Don't know how to transform this table."))))
  9128. (let* ((name (match-string 1))
  9129. beg
  9130. (transform (intern (match-string 2)))
  9131. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  9132. (skip (plist-get params :skip))
  9133. (skipcols (plist-get params :skipcols))
  9134. (txt (buffer-substring-no-properties
  9135. (org-table-begin) (org-table-end)))
  9136. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  9137. (lines (org-table-clean-before-export lines))
  9138. (i0 (if org-table-clean-did-remove-column 2 1))
  9139. (table (mapcar
  9140. (lambda (x)
  9141. (if (string-match org-table-hline-regexp x)
  9142. 'hline
  9143. (org-remove-by-index
  9144. (org-split-string (org-trim x) "\\s-*|\\s-*")
  9145. skipcols i0)))
  9146. lines))
  9147. (fun (if (= i0 2) 'cdr 'identity))
  9148. (org-table-last-alignment
  9149. (org-remove-by-index (funcall fun org-table-last-alignment)
  9150. skipcols i0))
  9151. (org-table-last-column-widths
  9152. (org-remove-by-index (funcall fun org-table-last-column-widths)
  9153. skipcols i0)))
  9154. (unless (fboundp transform)
  9155. (error "No such transformation function %s" transform))
  9156. (setq txt (funcall transform table params))
  9157. ;; Find the insertion place
  9158. (save-excursion
  9159. (goto-char (point-min))
  9160. (unless (re-search-forward
  9161. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  9162. (error "Don't know where to insert translated table"))
  9163. (goto-char (match-beginning 0))
  9164. (beginning-of-line 2)
  9165. (setq beg (point))
  9166. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  9167. (error "Cannot find end of insertion region"))
  9168. (beginning-of-line 1)
  9169. (delete-region beg (point))
  9170. (goto-char beg)
  9171. (insert txt "\n"))
  9172. (message "Table converted and installed at receiver location"))))
  9173. (defun org-remove-by-index (list indices &optional i0)
  9174. "Remove the elements in LIST with indices in INDICES.
  9175. First element has index 0, or I0 if given."
  9176. (if (not indices)
  9177. list
  9178. (if (integerp indices) (setq indices (list indices)))
  9179. (setq i0 (1- (or i0 0)))
  9180. (delq :rm (mapcar (lambda (x)
  9181. (setq i0 (1+ i0))
  9182. (if (memq i0 indices) :rm x))
  9183. list))))
  9184. (defun orgtbl-toggle-comment ()
  9185. "Comment or uncomment the orgtbl at point."
  9186. (interactive)
  9187. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  9188. (re2 (concat "^" orgtbl-line-start-regexp))
  9189. (commented (save-excursion (beginning-of-line 1)
  9190. (cond ((looking-at re1) t)
  9191. ((looking-at re2) nil)
  9192. (t (error "Not at an org table")))))
  9193. (re (if commented re1 re2))
  9194. beg end)
  9195. (save-excursion
  9196. (beginning-of-line 1)
  9197. (while (looking-at re) (beginning-of-line 0))
  9198. (beginning-of-line 2)
  9199. (setq beg (point))
  9200. (while (looking-at re) (beginning-of-line 2))
  9201. (setq end (point)))
  9202. (comment-region beg end (if commented '(4) nil))))
  9203. (defun orgtbl-insert-radio-table ()
  9204. "Insert a radio table template appropriate for this major mode."
  9205. (interactive)
  9206. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  9207. (txt (nth 1 e))
  9208. name pos)
  9209. (unless e (error "No radio table setup defined for %s" major-mode))
  9210. (setq name (read-string "Table name: "))
  9211. (while (string-match "%n" txt)
  9212. (setq txt (replace-match name t t txt)))
  9213. (or (bolp) (insert "\n"))
  9214. (setq pos (point))
  9215. (insert txt)
  9216. (goto-char pos)))
  9217. (defun org-get-param (params header i sym &optional hsym)
  9218. "Get parameter value for symbol SYM.
  9219. If this is a header line, actually get the value for the symbol with an
  9220. additional \"h\" inserted after the colon.
  9221. If the value is a protperty list, get the element for the current column.
  9222. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  9223. (let ((val (plist-get params sym)))
  9224. (and hsym header (setq val (or (plist-get params hsym) val)))
  9225. (if (consp val) (plist-get val i) val)))
  9226. (defun orgtbl-to-generic (table params)
  9227. "Convert the orgtbl-mode TABLE to some other format.
  9228. This generic routine can be used for many standard cases.
  9229. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9230. separator line, or a list of fields for that line.
  9231. PARAMS is a property list of parameters that can influence the conversion.
  9232. For the generic converter, some parameters are obligatory: You need to
  9233. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  9234. :splice, you must have :tstart and :tend.
  9235. Valid parameters are
  9236. :tstart String to start the table. Ignored when :splice is t.
  9237. :tend String to end the table. Ignored when :splice is t.
  9238. :splice When set to t, return only table body lines, don't wrap
  9239. them into :tstart and :tend. Default is nil.
  9240. :hline String to be inserted on horizontal separation lines.
  9241. May be nil to ignore hlines.
  9242. :lstart String to start a new table line.
  9243. :lend String to end a table line
  9244. :sep Separator between two fields
  9245. :lfmt Format for entire line, with enough %s to capture all fields.
  9246. If this is present, :lstart, :lend, and :sep are ignored.
  9247. :fmt A format to be used to wrap the field, should contain
  9248. %s for the original field value. For example, to wrap
  9249. everything in dollars, you could use :fmt \"$%s$\".
  9250. This may also be a property list with column numbers and
  9251. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9252. :hlstart :hlend :hlsep :hlfmt :hfmt
  9253. Same as above, specific for the header lines in the table.
  9254. All lines before the first hline are treated as header.
  9255. If any of these is not present, the data line value is used.
  9256. :efmt Use this format to print numbers with exponentials.
  9257. The format should have %s twice for inserting mantissa
  9258. and exponent, for example \"%s\\\\times10^{%s}\". This
  9259. may also be a property list with column numbers and
  9260. formats. :fmt will still be applied after :efmt.
  9261. In addition to this, the parameters :skip and :skipcols are always handled
  9262. directly by `orgtbl-send-table'. See manual."
  9263. (interactive)
  9264. (let* ((p params)
  9265. (splicep (plist-get p :splice))
  9266. (hline (plist-get p :hline))
  9267. rtn line i fm efm lfmt h)
  9268. ;; Do we have a header?
  9269. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  9270. (setq h t))
  9271. ;; Put header
  9272. (unless splicep
  9273. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  9274. ;; Now loop over all lines
  9275. (while (setq line (pop table))
  9276. (if (eq line 'hline)
  9277. ;; A horizontal separator line
  9278. (progn (if hline (push hline rtn))
  9279. (setq h nil)) ; no longer in header
  9280. ;; A normal line. Convert the fields, push line onto the result list
  9281. (setq i 0)
  9282. (setq line
  9283. (mapcar
  9284. (lambda (f)
  9285. (setq i (1+ i)
  9286. fm (org-get-param p h i :fmt :hfmt)
  9287. efm (org-get-param p h i :efmt))
  9288. (if (and efm (string-match orgtbl-exp-regexp f))
  9289. (setq f (format
  9290. efm (match-string 1 f) (match-string 2 f))))
  9291. (if fm (setq f (format fm f)))
  9292. f)
  9293. line))
  9294. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  9295. (push (apply 'format lfmt line) rtn)
  9296. (push (concat
  9297. (org-get-param p h i :lstart :hlstart)
  9298. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  9299. (org-get-param p h i :lend :hlend))
  9300. rtn))))
  9301. (unless splicep
  9302. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  9303. (mapconcat 'identity (nreverse rtn) "\n")))
  9304. (defun orgtbl-to-latex (table params)
  9305. "Convert the orgtbl-mode TABLE to LaTeX.
  9306. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9307. separator line, or a list of fields for that line.
  9308. PARAMS is a property list of parameters that can influence the conversion.
  9309. Supports all parameters from `orgtbl-to-generic'. Most important for
  9310. LaTeX are:
  9311. :splice When set to t, return only table body lines, don't wrap
  9312. them into a tabular environment. Default is nil.
  9313. :fmt A format to be used to wrap the field, should contain %s for the
  9314. original field value. For example, to wrap everything in dollars,
  9315. use :fmt \"$%s$\". This may also be a property list with column
  9316. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9317. :efmt Format for transforming numbers with exponentials. The format
  9318. should have %s twice for inserting mantissa and exponent, for
  9319. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  9320. This may also be a property list with column numbers and formats.
  9321. The general parameters :skip and :skipcols have already been applied when
  9322. this function is called."
  9323. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  9324. org-table-last-alignment ""))
  9325. (params2
  9326. (list
  9327. :tstart (concat "\\begin{tabular}{" alignment "}")
  9328. :tend "\\end{tabular}"
  9329. :lstart "" :lend " \\\\" :sep " & "
  9330. :efmt "%s\\,(%s)" :hline "\\hline")))
  9331. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9332. (defun orgtbl-to-html (table params)
  9333. "Convert the orgtbl-mode TABLE to LaTeX.
  9334. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9335. separator line, or a list of fields for that line.
  9336. PARAMS is a property list of parameters that can influence the conversion.
  9337. Currently this function recognizes the following parameters:
  9338. :splice When set to t, return only table body lines, don't wrap
  9339. them into a <table> environment. Default is nil.
  9340. The general parameters :skip and :skipcols have already been applied when
  9341. this function is called. The function does *not* use `orgtbl-to-generic',
  9342. so you cannot specify parameters for it."
  9343. (let* ((splicep (plist-get params :splice))
  9344. html)
  9345. ;; Just call the formatter we already have
  9346. ;; We need to make text lines for it, so put the fields back together.
  9347. (setq html (org-format-org-table-html
  9348. (mapcar
  9349. (lambda (x)
  9350. (if (eq x 'hline)
  9351. "|----+----|"
  9352. (concat "| " (mapconcat 'identity x " | ") " |")))
  9353. table)
  9354. splicep))
  9355. (if (string-match "\n+\\'" html)
  9356. (setq html (replace-match "" t t html)))
  9357. html))
  9358. (defun orgtbl-to-texinfo (table params)
  9359. "Convert the orgtbl-mode TABLE to TeXInfo.
  9360. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9361. separator line, or a list of fields for that line.
  9362. PARAMS is a property list of parameters that can influence the conversion.
  9363. Supports all parameters from `orgtbl-to-generic'. Most important for
  9364. TeXInfo are:
  9365. :splice nil/t When set to t, return only table body lines, don't wrap
  9366. them into a multitable environment. Default is nil.
  9367. :fmt fmt A format to be used to wrap the field, should contain
  9368. %s for the original field value. For example, to wrap
  9369. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  9370. This may also be a property list with column numbers and
  9371. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  9372. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  9373. are computed automatically from the width of the columns
  9374. under org-mode.
  9375. The general parameters :skip and :skipcols have already been applied when
  9376. this function is called."
  9377. (let* ((total (float (apply '+ org-table-last-column-widths)))
  9378. (colfrac (or (plist-get params :cf)
  9379. (mapconcat
  9380. (lambda (x) (format "%.3f" (/ (float x) total)))
  9381. org-table-last-column-widths " ")))
  9382. (params2
  9383. (list
  9384. :tstart (concat "@multitable @columnfractions " colfrac)
  9385. :tend "@end multitable"
  9386. :lstart "@item " :lend "" :sep " @tab "
  9387. :hlstart "@headitem ")))
  9388. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9389. ;;;; Link Stuff
  9390. ;;; Link abbreviations
  9391. (defun org-link-expand-abbrev (link)
  9392. "Apply replacements as defined in `org-link-abbrev-alist."
  9393. (if (string-match "^\\([a-zA-Z]+\\)\\(::?\\(.*\\)\\)?$" link)
  9394. (let* ((key (match-string 1 link))
  9395. (as (or (assoc key org-link-abbrev-alist-local)
  9396. (assoc key org-link-abbrev-alist)))
  9397. (tag (and (match-end 2) (match-string 3 link)))
  9398. rpl)
  9399. (if (not as)
  9400. link
  9401. (setq rpl (cdr as))
  9402. (cond
  9403. ((symbolp rpl) (funcall rpl tag))
  9404. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9405. (t (concat rpl tag)))))
  9406. link))
  9407. ;;; Storing and inserting links
  9408. (defvar org-insert-link-history nil
  9409. "Minibuffer history for links inserted with `org-insert-link'.")
  9410. (defvar org-stored-links nil
  9411. "Contains the links stored with `org-store-link'.")
  9412. (defvar org-store-link-plist nil
  9413. "Plist with info about the most recently link created with `org-store-link'.")
  9414. ;;;###autoload
  9415. (defun org-store-link (arg)
  9416. "\\<org-mode-map>Store an org-link to the current location.
  9417. This link can later be inserted into an org-buffer with
  9418. \\[org-insert-link].
  9419. For some link types, a prefix arg is interpreted:
  9420. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9421. For file links, arg negates `org-context-in-file-links'."
  9422. (interactive "P")
  9423. (setq org-store-link-plist nil) ; reset
  9424. (let (link cpltxt desc description search txt)
  9425. (cond
  9426. ((eq major-mode 'bbdb-mode)
  9427. (let ((name (bbdb-record-name (bbdb-current-record)))
  9428. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  9429. (setq cpltxt (concat "bbdb:" (or name company))
  9430. link (org-make-link cpltxt))
  9431. (org-store-link-props :type "bbdb" :name name :company company)))
  9432. ((eq major-mode 'Info-mode)
  9433. (setq link (org-make-link "info:"
  9434. (file-name-nondirectory Info-current-file)
  9435. ":" Info-current-node))
  9436. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9437. ":" Info-current-node))
  9438. (org-store-link-props :type "info" :file Info-current-file
  9439. :node Info-current-node))
  9440. ((eq major-mode 'calendar-mode)
  9441. (let ((cd (calendar-cursor-to-date)))
  9442. (setq link
  9443. (format-time-string
  9444. (car org-time-stamp-formats)
  9445. (apply 'encode-time
  9446. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9447. nil nil nil))))
  9448. (org-store-link-props :type "calendar" :date cd)))
  9449. ((or (eq major-mode 'vm-summary-mode)
  9450. (eq major-mode 'vm-presentation-mode))
  9451. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9452. (vm-follow-summary-cursor)
  9453. (save-excursion
  9454. (vm-select-folder-buffer)
  9455. (let* ((message (car vm-message-pointer))
  9456. (folder buffer-file-name)
  9457. (subject (vm-su-subject message))
  9458. (to (vm-get-header-contents message "To"))
  9459. (from (vm-get-header-contents message "From"))
  9460. (message-id (vm-su-message-id message)))
  9461. (org-store-link-props :type "vm" :from from :to to :subject subject
  9462. :message-id message-id)
  9463. (setq message-id (org-remove-angle-brackets message-id))
  9464. (setq folder (abbreviate-file-name folder))
  9465. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9466. folder)
  9467. (setq folder (replace-match "" t t folder)))
  9468. (setq cpltxt (org-email-link-description))
  9469. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9470. ((eq major-mode 'wl-summary-mode)
  9471. (let* ((msgnum (wl-summary-message-number))
  9472. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9473. msgnum 'message-id))
  9474. (wl-message-entity
  9475. (if (fboundp 'elmo-message-entity)
  9476. (elmo-message-entity
  9477. wl-summary-buffer-elmo-folder msgnum)
  9478. (elmo-msgdb-overview-get-entity
  9479. msgnum (wl-summary-buffer-msgdb))))
  9480. (from (wl-summary-line-from))
  9481. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  9482. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  9483. (wl-summary-line-subject))))
  9484. (org-store-link-props :type "wl" :from from :to to
  9485. :subject subject :message-id message-id)
  9486. (setq message-id (org-remove-angle-brackets message-id))
  9487. (setq cpltxt (org-email-link-description))
  9488. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9489. "#" message-id))))
  9490. ((or (equal major-mode 'mh-folder-mode)
  9491. (equal major-mode 'mh-show-mode))
  9492. (let ((from (org-mhe-get-header "From:"))
  9493. (to (org-mhe-get-header "To:"))
  9494. (message-id (org-mhe-get-header "Message-Id:"))
  9495. (subject (org-mhe-get-header "Subject:")))
  9496. (org-store-link-props :type "mh" :from from :to to
  9497. :subject subject :message-id message-id)
  9498. (setq cpltxt (org-email-link-description))
  9499. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9500. (org-remove-angle-brackets message-id)))))
  9501. ((eq major-mode 'rmail-mode)
  9502. (save-excursion
  9503. (save-restriction
  9504. (rmail-narrow-to-non-pruned-header)
  9505. (let ((folder buffer-file-name)
  9506. (message-id (mail-fetch-field "message-id"))
  9507. (from (mail-fetch-field "from"))
  9508. (to (mail-fetch-field "to"))
  9509. (subject (mail-fetch-field "subject")))
  9510. (org-store-link-props
  9511. :type "rmail" :from from :to to
  9512. :subject subject :message-id message-id)
  9513. (setq message-id (org-remove-angle-brackets message-id))
  9514. (setq cpltxt (org-email-link-description))
  9515. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9516. ((eq major-mode 'gnus-group-mode)
  9517. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9518. (gnus-group-group-name)) ; version
  9519. ((fboundp 'gnus-group-name)
  9520. (gnus-group-name))
  9521. (t "???"))))
  9522. (unless group (error "Not on a group"))
  9523. (org-store-link-props :type "gnus" :group group)
  9524. (setq cpltxt (concat
  9525. (if (org-xor arg org-usenet-links-prefer-google)
  9526. "http://groups.google.com/groups?group="
  9527. "gnus:")
  9528. group)
  9529. link (org-make-link cpltxt))))
  9530. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9531. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9532. (let* ((group gnus-newsgroup-name)
  9533. (article (gnus-summary-article-number))
  9534. (header (gnus-summary-article-header article))
  9535. (from (mail-header-from header))
  9536. (message-id (mail-header-id header))
  9537. (date (mail-header-date header))
  9538. (subject (gnus-summary-subject-string)))
  9539. (org-store-link-props :type "gnus" :from from :subject subject
  9540. :message-id message-id :group group)
  9541. (setq cpltxt (org-email-link-description))
  9542. (if (org-xor arg org-usenet-links-prefer-google)
  9543. (setq link
  9544. (concat
  9545. cpltxt "\n "
  9546. (format "http://groups.google.com/groups?as_umsgid=%s"
  9547. (org-fixup-message-id-for-http message-id))))
  9548. (setq link (org-make-link "gnus:" group
  9549. "#" (number-to-string article))))))
  9550. ((eq major-mode 'w3-mode)
  9551. (setq cpltxt (url-view-url t)
  9552. link (org-make-link cpltxt))
  9553. (org-store-link-props :type "w3" :url (url-view-url t)))
  9554. ((eq major-mode 'w3m-mode)
  9555. (setq cpltxt (or w3m-current-title w3m-current-url)
  9556. link (org-make-link w3m-current-url))
  9557. (org-store-link-props :type "w3m" :url (url-view-url t)))
  9558. ((setq search (run-hook-with-args-until-success
  9559. 'org-create-file-search-functions))
  9560. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9561. "::" search))
  9562. (setq cpltxt (or description link)))
  9563. ((eq major-mode 'image-mode)
  9564. (setq cpltxt (concat "file:"
  9565. (abbreviate-file-name buffer-file-name))
  9566. link (org-make-link cpltxt))
  9567. (org-store-link-props :type "image" :file buffer-file-name))
  9568. ((eq major-mode 'dired-mode)
  9569. ;; link to the file in the current line
  9570. (setq cpltxt (concat "file:"
  9571. (abbreviate-file-name
  9572. (expand-file-name
  9573. (dired-get-filename nil t))))
  9574. link (org-make-link cpltxt)))
  9575. ((and buffer-file-name (org-mode-p))
  9576. ;; Just link to current headline
  9577. (setq cpltxt (concat "file:"
  9578. (abbreviate-file-name buffer-file-name)))
  9579. ;; Add a context search string
  9580. (when (org-xor org-context-in-file-links arg)
  9581. ;; Check if we are on a target
  9582. (if (org-in-regexp "<<\\(.*?\\)>>")
  9583. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9584. (setq txt (cond
  9585. ((org-on-heading-p) nil)
  9586. ((org-region-active-p)
  9587. (buffer-substring (region-beginning) (region-end)))
  9588. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9589. (when (or (null txt) (string-match "\\S-" txt))
  9590. (setq cpltxt
  9591. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9592. desc "NONE"))))
  9593. (if (string-match "::\\'" cpltxt)
  9594. (setq cpltxt (substring cpltxt 0 -2)))
  9595. (setq link (org-make-link cpltxt)))
  9596. (buffer-file-name
  9597. ;; Just link to this file here.
  9598. (setq cpltxt (concat "file:"
  9599. (abbreviate-file-name buffer-file-name)))
  9600. ;; Add a context string
  9601. (when (org-xor org-context-in-file-links arg)
  9602. (setq txt (if (org-region-active-p)
  9603. (buffer-substring (region-beginning) (region-end))
  9604. (buffer-substring (point-at-bol) (point-at-eol))))
  9605. ;; Only use search option if there is some text.
  9606. (when (string-match "\\S-" txt)
  9607. (setq cpltxt
  9608. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9609. desc "NONE")))
  9610. (setq link (org-make-link cpltxt)))
  9611. ((interactive-p)
  9612. (error "Cannot link to a buffer which is not visiting a file"))
  9613. (t (setq link nil)))
  9614. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9615. (setq link (or link cpltxt)
  9616. desc (or desc cpltxt))
  9617. (if (equal desc "NONE") (setq desc nil))
  9618. (if (and (interactive-p) link)
  9619. (progn
  9620. (setq org-stored-links
  9621. (cons (list cpltxt link desc) org-stored-links))
  9622. (message "Stored: %s" (or cpltxt link)))
  9623. (org-make-link-string link desc))))
  9624. (defun org-store-link-props (&rest plist)
  9625. "Store link properties, extract names and addresses."
  9626. (let (x adr)
  9627. (when (setq x (plist-get plist :from))
  9628. (setq adr (mail-extract-address-components x))
  9629. (plist-put plist :fromname (car adr))
  9630. (plist-put plist :fromaddress (nth 1 adr)))
  9631. (when (setq x (plist-get plist :to))
  9632. (setq adr (mail-extract-address-components x))
  9633. (plist-put plist :toname (car adr))
  9634. (plist-put plist :toaddress (nth 1 adr))))
  9635. (let ((from (plist-get plist :from))
  9636. (to (plist-get plist :to)))
  9637. (when (and from to org-from-is-user-regexp)
  9638. (plist-put plist :fromto
  9639. (if (string-match org-from-is-user-regexp from)
  9640. (concat "to %t")
  9641. (concat "from %f")))))
  9642. (setq org-store-link-plist plist))
  9643. (defun org-email-link-description (&optional fmt)
  9644. "Return the description part of an email link.
  9645. This takes information from `org-store-link-plist' and formats it
  9646. according to FMT (default from `org-email-link-description-format')."
  9647. (setq fmt (or fmt org-email-link-description-format))
  9648. (let* ((p org-store-link-plist)
  9649. (to (plist-get p :toaddress))
  9650. (from (plist-get p :fromaddress))
  9651. (table
  9652. (list
  9653. (cons "%c" (plist-get p :fromto))
  9654. (cons "%F" (plist-get p :from))
  9655. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  9656. (cons "%T" (plist-get p :to))
  9657. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  9658. (cons "%s" (plist-get p :subject))
  9659. (cons "%m" (plist-get p :message-id)))))
  9660. (when (string-match "%c" fmt)
  9661. ;; Check if the user wrote this message
  9662. (if (and org-from-is-user-regexp from to
  9663. (save-match-data (string-match org-from-is-user-regexp from)))
  9664. (setq fmt (replace-match "to %t" t t fmt))
  9665. (setq fmt (replace-match "from %f" t t fmt))))
  9666. (org-replace-escapes fmt table)))
  9667. (defun org-make-org-heading-search-string (&optional string heading)
  9668. "Make search string for STRING or current headline."
  9669. (interactive)
  9670. (let ((s (or string (org-get-heading))))
  9671. (unless (and string (not heading))
  9672. ;; We are using a headline, clean up garbage in there.
  9673. (if (string-match org-todo-regexp s)
  9674. (setq s (replace-match "" t t s)))
  9675. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  9676. (setq s (replace-match "" t t s)))
  9677. (setq s (org-trim s))
  9678. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9679. org-comment-string "\\)") s)
  9680. (setq s (replace-match "" t t s)))
  9681. (while (string-match org-ts-regexp s)
  9682. (setq s (replace-match "" t t s))))
  9683. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9684. (setq s (replace-match " " t t s)))
  9685. (or string (setq s (concat "*" s))) ; Add * for headlines
  9686. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9687. (defun org-make-link (&rest strings)
  9688. "Concatenate STRINGS, format resulting string with `org-link-format'."
  9689. (apply 'concat strings))
  9690. (defun org-make-link-string (link &optional description)
  9691. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9692. (when (stringp description)
  9693. ;; Remove brackets from the description, they are fatal.
  9694. (while (string-match "\\[\\|\\]" description)
  9695. (setq description (replace-match "" t t description))))
  9696. (when (equal (org-link-escape link) description)
  9697. ;; No description needed, it is identical
  9698. (setq description nil))
  9699. (when (and (not description)
  9700. (not (equal link (org-link-escape link))))
  9701. (setq description link))
  9702. (concat "[[" (org-link-escape link) "]"
  9703. (if description (concat "[" description "]") "")
  9704. "]"))
  9705. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  9706. "Association list of escapes for some characters problematic in links.")
  9707. (defun org-link-escape (text)
  9708. "Escape charaters in TEXT that are problematic for links."
  9709. (when text
  9710. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9711. org-link-escape-chars "\\|")))
  9712. (while (string-match re text)
  9713. (setq text
  9714. (replace-match
  9715. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9716. t t text)))
  9717. text)))
  9718. (defun org-link-unescape (text)
  9719. "Reverse the action of `org-link-escape'."
  9720. (when text
  9721. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9722. org-link-escape-chars "\\|")))
  9723. (while (string-match re text)
  9724. (setq text
  9725. (replace-match
  9726. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9727. t t text)))
  9728. text)))
  9729. (defun org-xor (a b)
  9730. "Exclusive or."
  9731. (if a (not b) b))
  9732. (defun org-get-header (header)
  9733. "Find a header field in the current buffer."
  9734. (save-excursion
  9735. (goto-char (point-min))
  9736. (let ((case-fold-search t) s)
  9737. (cond
  9738. ((eq header 'from)
  9739. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9740. (setq s (match-string 1)))
  9741. (while (string-match "\"" s)
  9742. (setq s (replace-match "" t t s)))
  9743. (if (string-match "[<(].*" s)
  9744. (setq s (replace-match "" t t s))))
  9745. ((eq header 'message-id)
  9746. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9747. (setq s (match-string 1))))
  9748. ((eq header 'subject)
  9749. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9750. (setq s (match-string 1)))))
  9751. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9752. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9753. s)))
  9754. (defun org-fixup-message-id-for-http (s)
  9755. "Replace special characters in a message id, so it can be used in an http query."
  9756. (while (string-match "<" s)
  9757. (setq s (replace-match "%3C" t t s)))
  9758. (while (string-match ">" s)
  9759. (setq s (replace-match "%3E" t t s)))
  9760. (while (string-match "@" s)
  9761. (setq s (replace-match "%40" t t s)))
  9762. s)
  9763. (defun org-insert-link (&optional complete-file)
  9764. "Insert a link. At the prompt, enter the link.
  9765. Completion can be used to select a link previously stored with
  9766. `org-store-link'. When the empty string is entered (i.e. if you just
  9767. press RET at the prompt), the link defaults to the most recently
  9768. stored link. As SPC triggers completion in the minibuffer, you need to
  9769. use M-SPC or C-q SPC to force the insertion of a space character.
  9770. You will also be prompted for a description, and if one is given, it will
  9771. be displayed in the buffer instead of the link.
  9772. If there is already a link at point, this command will allow you to edit link
  9773. and description parts.
  9774. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  9775. selected using completion. The path to the file will be relative to
  9776. the current directory if the file is in the current directory or a
  9777. subdirectory. Otherwise, the link will be the absolute path as
  9778. completed in the minibuffer (i.e. normally ~/path/to/file).
  9779. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  9780. is in the current directory or below.
  9781. With three \\[universal-argument] prefixes, negate the meaning of
  9782. `org-keep-stored-link-after-insertion'."
  9783. (interactive "P")
  9784. (let ((wcf (current-window-configuration))
  9785. (region (if (org-region-active-p)
  9786. (prog1 (buffer-substring (region-beginning) (region-end))
  9787. (delete-region (region-beginning) (region-end)))))
  9788. tmphist ; byte-compile incorrectly complains about this
  9789. link desc entry remove file)
  9790. (cond
  9791. ((org-in-regexp org-bracket-link-regexp 1)
  9792. ;; We do have a link at point, and we are going to edit it.
  9793. (setq remove (list (match-beginning 0) (match-end 0)))
  9794. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9795. (setq link (read-string "Link: "
  9796. (org-link-unescape
  9797. (org-match-string-no-properties 1)))))
  9798. ((or (org-in-regexp org-angle-link-re)
  9799. (org-in-regexp org-plain-link-re))
  9800. ;; Convert to bracket link
  9801. (setq remove (list (match-beginning 0) (match-end 0))
  9802. link (read-string "Link: "
  9803. (org-remove-angle-brackets (match-string 0)))))
  9804. ((equal complete-file '(4))
  9805. ;; Completing read for file names.
  9806. (setq file (read-file-name "File: "))
  9807. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9808. (pwd1 (file-name-as-directory (abbreviate-file-name
  9809. (expand-file-name ".")))))
  9810. (cond
  9811. ((equal complete-file '(16))
  9812. (setq link (org-make-link
  9813. "file:"
  9814. (abbreviate-file-name (expand-file-name file)))))
  9815. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9816. (setq link (org-make-link "file:" (match-string 1 file))))
  9817. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9818. (expand-file-name file))
  9819. (setq link (org-make-link
  9820. "file:" (match-string 1 (expand-file-name file)))))
  9821. (t (setq link (org-make-link "file:" file))))))
  9822. (t
  9823. ;; Read link, with completion for stored links.
  9824. (with-output-to-temp-buffer "*Org Links*"
  9825. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  9826. (when org-stored-links
  9827. (princ "\nStored links are available with <up>/<down> (most recent with RET):\n\n")
  9828. (princ (mapconcat 'car (reverse org-stored-links) "\n"))))
  9829. (let ((cw (selected-window)))
  9830. (select-window (get-buffer-window "*Org Links*"))
  9831. (shrink-window-if-larger-than-buffer)
  9832. (setq truncate-lines t)
  9833. (select-window cw))
  9834. ;; Fake a link history, containing the stored links.
  9835. (setq tmphist (append (mapcar 'car org-stored-links)
  9836. org-insert-link-history))
  9837. (unwind-protect
  9838. (setq link (org-completing-read
  9839. "Link: "
  9840. (append
  9841. (mapcar (lambda (x) (concat (car x) ":"))
  9842. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  9843. (mapcar (lambda (x) (concat x ":")) org-link-types))
  9844. nil nil nil
  9845. 'tmphist
  9846. (or (car (car org-stored-links)))))
  9847. (set-window-configuration wcf)
  9848. (kill-buffer "*Org Links*"))
  9849. (setq entry (assoc link org-stored-links))
  9850. (or entry (push link org-insert-link-history))
  9851. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9852. (not org-keep-stored-link-after-insertion))
  9853. (setq org-stored-links (delq (assoc link org-stored-links)
  9854. org-stored-links)))
  9855. (setq link (if entry (nth 1 entry) link)
  9856. desc (or region desc (nth 2 entry)))))
  9857. (if (string-match org-plain-link-re link)
  9858. ;; URL-like link, normalize the use of angular brackets.
  9859. (setq link (org-make-link (org-remove-angle-brackets link))))
  9860. ;; Check if we are linking to the current file with a search option
  9861. ;; If yes, simplify the link by using only the search option.
  9862. (when (and buffer-file-name
  9863. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  9864. (let* ((path (match-string 1 link))
  9865. (case-fold-search nil)
  9866. (search (match-string 2 link)))
  9867. (save-match-data
  9868. (if (equal (file-truename buffer-file-name) (file-truename path))
  9869. ;; We are linking to this same file, with a search option
  9870. (setq link search)))))
  9871. ;; Check if we can/should use a relative path. If yes, simplify the link
  9872. (when (string-match "\\<file:\\(.*\\)" link)
  9873. (let* ((path (match-string 1 link))
  9874. (case-fold-search nil))
  9875. (cond
  9876. ((eq org-link-file-path-type 'absolute)
  9877. (setq path (abbreviate-file-name (expand-file-name path))))
  9878. ((eq org-link-file-path-type 'noabbrev)
  9879. (setq path (expand-file-name path)))
  9880. ((eq org-link-file-path-type 'relative)
  9881. (setq path (file-relative-name path)))
  9882. (t
  9883. (save-match-data
  9884. (if (string-match (concat "^" (regexp-quote
  9885. (file-name-as-directory
  9886. (expand-file-name "."))))
  9887. (expand-file-name path))
  9888. ;; We are linking a file with relative path name.
  9889. (setq path (substring (expand-file-name path)
  9890. (match-end 0)))))))
  9891. (setq link (concat "file:" path))))
  9892. (setq desc (read-string "Description: " desc))
  9893. (unless (string-match "\\S-" desc) (setq desc nil))
  9894. (if remove (apply 'delete-region remove))
  9895. (insert (org-make-link-string link desc))))
  9896. (defun org-completing-read (&rest args)
  9897. (let ((minibuffer-local-completion-map
  9898. (copy-keymap minibuffer-local-completion-map)))
  9899. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9900. (apply 'completing-read args)))
  9901. ;;; Opening/following a link
  9902. (defvar org-link-search-failed nil)
  9903. (defun org-next-link ()
  9904. "Move forward to the next link.
  9905. If the link is in hidden text, expose it."
  9906. (interactive)
  9907. (when (and org-link-search-failed (eq this-command last-command))
  9908. (goto-char (point-min))
  9909. (message "Link search wrapped back to beginning of buffer"))
  9910. (setq org-link-search-failed nil)
  9911. (let* ((pos (point))
  9912. (ct (org-context))
  9913. (a (assoc :link ct)))
  9914. (if a (goto-char (nth 2 a)))
  9915. (if (re-search-forward org-any-link-re nil t)
  9916. (progn
  9917. (goto-char (match-beginning 0))
  9918. (if (org-invisible-p) (org-show-context)))
  9919. (goto-char pos)
  9920. (setq org-link-search-failed t)
  9921. (error "No further link found"))))
  9922. (defun org-previous-link ()
  9923. "Move backward to the previous link.
  9924. If the link is in hidden text, expose it."
  9925. (interactive)
  9926. (when (and org-link-search-failed (eq this-command last-command))
  9927. (goto-char (point-max))
  9928. (message "Link search wrapped back to end of buffer"))
  9929. (setq org-link-search-failed nil)
  9930. (let* ((pos (point))
  9931. (ct (org-context))
  9932. (a (assoc :link ct)))
  9933. (if a (goto-char (nth 1 a)))
  9934. (if (re-search-backward org-any-link-re nil t)
  9935. (progn
  9936. (goto-char (match-beginning 0))
  9937. (if (org-invisible-p) (org-show-context)))
  9938. (goto-char pos)
  9939. (setq org-link-search-failed t)
  9940. (error "No further link found"))))
  9941. (defun org-find-file-at-mouse (ev)
  9942. "Open file link or URL at mouse."
  9943. (interactive "e")
  9944. (mouse-set-point ev)
  9945. (org-open-at-point 'in-emacs))
  9946. (defun org-open-at-mouse (ev)
  9947. "Open file link or URL at mouse."
  9948. (interactive "e")
  9949. (mouse-set-point ev)
  9950. (org-open-at-point))
  9951. (defvar org-window-config-before-follow-link nil
  9952. "The window configuration before following a link.
  9953. This is saved in case the need arises to restore it.")
  9954. (defvar org-open-link-marker (make-marker)
  9955. "Marker pointing to the location where `org-open-at-point; was called.")
  9956. (defun org-open-at-point (&optional in-emacs)
  9957. "Open link at or after point.
  9958. If there is no link at point, this function will search forward up to
  9959. the end of the current subtree.
  9960. Normally, files will be opened by an appropriate application. If the
  9961. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  9962. (interactive "P")
  9963. (move-marker org-open-link-marker (point))
  9964. (setq org-window-config-before-follow-link (current-window-configuration))
  9965. (org-remove-occur-highlights nil nil t)
  9966. (if (org-at-timestamp-p t)
  9967. (org-follow-timestamp-link)
  9968. (let (type path link line search (pos (point)))
  9969. (catch 'match
  9970. (save-excursion
  9971. (skip-chars-forward "^]\n\r")
  9972. (when (org-in-regexp org-bracket-link-regexp)
  9973. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  9974. (while (string-match " *\n *" link)
  9975. (setq link (replace-match " " t t link)))
  9976. (setq link (org-link-expand-abbrev link))
  9977. (if (string-match org-link-re-with-space2 link)
  9978. (setq type (match-string 1 link) path (match-string 2 link))
  9979. (setq type "thisfile" path link))
  9980. (throw 'match t)))
  9981. (when (get-text-property (point) 'org-linked-text)
  9982. (setq type "thisfile"
  9983. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9984. (1+ (point)) (point))
  9985. path (buffer-substring
  9986. (previous-single-property-change pos 'org-linked-text)
  9987. (next-single-property-change pos 'org-linked-text)))
  9988. (throw 'match t))
  9989. (save-excursion
  9990. (when (or (org-in-regexp org-angle-link-re)
  9991. (org-in-regexp org-plain-link-re))
  9992. (setq type (match-string 1) path (match-string 2))
  9993. (throw 'match t)))
  9994. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  9995. (setq type "tree-match"
  9996. path (match-string 1))
  9997. (throw 'match t))
  9998. (save-excursion
  9999. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  10000. (setq type "tags"
  10001. path (match-string 1))
  10002. (while (string-match ":" path)
  10003. (setq path (replace-match "+" t t path)))
  10004. (throw 'match t))))
  10005. (unless path
  10006. (error "No link found"))
  10007. ;; Remove any trailing spaces in path
  10008. (if (string-match " +\\'" path)
  10009. (setq path (replace-match "" t t path)))
  10010. (cond
  10011. ((equal type "mailto")
  10012. (let ((cmd (car org-link-mailto-program))
  10013. (args (cdr org-link-mailto-program)) args1
  10014. (address path) (subject "") a)
  10015. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  10016. (setq address (match-string 1 path)
  10017. subject (org-link-escape (match-string 2 path))))
  10018. (while args
  10019. (cond
  10020. ((not (stringp (car args))) (push (pop args) args1))
  10021. (t (setq a (pop args))
  10022. (if (string-match "%a" a)
  10023. (setq a (replace-match address t t a)))
  10024. (if (string-match "%s" a)
  10025. (setq a (replace-match subject t t a)))
  10026. (push a args1))))
  10027. (apply cmd (nreverse args1))))
  10028. ((member type '("http" "https" "ftp" "news"))
  10029. (browse-url (concat type ":" path)))
  10030. ((string= type "tags")
  10031. (org-tags-view in-emacs path))
  10032. ((string= type "thisfile")
  10033. (if in-emacs
  10034. (switch-to-buffer-other-window
  10035. (org-get-buffer-for-internal-link (current-buffer)))
  10036. (org-mark-ring-push))
  10037. (let ((cmd `(org-link-search
  10038. ,path
  10039. ,(cond ((equal in-emacs '(4)) 'occur)
  10040. ((equal in-emacs '(16)) 'org-occur)
  10041. (t nil))
  10042. ,pos)))
  10043. (condition-case nil (eval cmd)
  10044. (error (progn (widen) (eval cmd))))))
  10045. ((string= type "tree-match")
  10046. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  10047. ((string= type "file")
  10048. (if (string-match "::\\([0-9]+\\)\\'" path)
  10049. (setq line (string-to-number (match-string 1 path))
  10050. path (substring path 0 (match-beginning 0)))
  10051. (if (string-match "::\\(.+\\)\\'" path)
  10052. (setq search (match-string 1 path)
  10053. path (substring path 0 (match-beginning 0)))))
  10054. (org-open-file path in-emacs line search))
  10055. ((string= type "news")
  10056. (org-follow-gnus-link path))
  10057. ((string= type "bbdb")
  10058. (org-follow-bbdb-link path))
  10059. ((string= type "info")
  10060. (org-follow-info-link path))
  10061. ((string= type "gnus")
  10062. (let (group article)
  10063. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10064. (error "Error in Gnus link"))
  10065. (setq group (match-string 1 path)
  10066. article (match-string 3 path))
  10067. (org-follow-gnus-link group article)))
  10068. ((string= type "vm")
  10069. (let (folder article)
  10070. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10071. (error "Error in VM link"))
  10072. (setq folder (match-string 1 path)
  10073. article (match-string 3 path))
  10074. ;; in-emacs is the prefix arg, will be interpreted as read-only
  10075. (org-follow-vm-link folder article in-emacs)))
  10076. ((string= type "wl")
  10077. (let (folder article)
  10078. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10079. (error "Error in Wanderlust link"))
  10080. (setq folder (match-string 1 path)
  10081. article (match-string 3 path))
  10082. (org-follow-wl-link folder article)))
  10083. ((string= type "mhe")
  10084. (let (folder article)
  10085. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10086. (error "Error in MHE link"))
  10087. (setq folder (match-string 1 path)
  10088. article (match-string 3 path))
  10089. (org-follow-mhe-link folder article)))
  10090. ((string= type "rmail")
  10091. (let (folder article)
  10092. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10093. (error "Error in RMAIL link"))
  10094. (setq folder (match-string 1 path)
  10095. article (match-string 3 path))
  10096. (org-follow-rmail-link folder article)))
  10097. ((string= type "shell")
  10098. (let ((cmd path))
  10099. ;; FIXME: the following is only for backward compatibility
  10100. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  10101. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  10102. (if (or (not org-confirm-shell-link-function)
  10103. (funcall org-confirm-shell-link-function
  10104. (format "Execute \"%s\" in shell? "
  10105. (org-add-props cmd nil
  10106. 'face 'org-warning))))
  10107. (progn
  10108. (message "Executing %s" cmd)
  10109. (shell-command cmd))
  10110. (error "Abort"))))
  10111. ((string= type "elisp")
  10112. (let ((cmd path))
  10113. (if (or (not org-confirm-elisp-link-function)
  10114. (funcall org-confirm-elisp-link-function
  10115. (format "Execute \"%s\" as elisp? "
  10116. (org-add-props cmd nil
  10117. 'face 'org-warning))))
  10118. (message "%s => %s" cmd (eval (read cmd)))
  10119. (error "Abort"))))
  10120. (t
  10121. (browse-url-at-point)))))
  10122. (move-marker org-open-link-marker nil))
  10123. ;;; File search
  10124. (defvar org-create-file-search-functions nil
  10125. "List of functions to construct the right search string for a file link.
  10126. These functions are called in turn with point at the location to
  10127. which the link should point.
  10128. A function in the hook should first test if it would like to
  10129. handle this file type, for example by checking the major-mode or
  10130. the file extension. If it decides not to handle this file, it
  10131. should just return nil to give other functions a chance. If it
  10132. does handle the file, it must return the search string to be used
  10133. when following the link. The search string will be part of the
  10134. file link, given after a double colon, and `org-open-at-point'
  10135. will automatically search for it. If special measures must be
  10136. taken to make the search successful, another function should be
  10137. added to the companion hook `org-execute-file-search-functions',
  10138. which see.
  10139. A function in this hook may also use `setq' to set the variable
  10140. `description' to provide a suggestion for the descriptive text to
  10141. be used for this link when it gets inserted into an Org-mode
  10142. buffer with \\[org-insert-link].")
  10143. (defvar org-execute-file-search-functions nil
  10144. "List of functions to execute a file search triggered by a link.
  10145. Functions added to this hook must accept a single argument, the
  10146. search string that was part of the file link, the part after the
  10147. double colon. The function must first check if it would like to
  10148. handle this search, for example by checking the major-mode or the
  10149. file extension. If it decides not to handle this search, it
  10150. should just return nil to give other functions a chance. If it
  10151. does handle the search, it must return a non-nil value to keep
  10152. other functions from trying.
  10153. Each function can access the current prefix argument through the
  10154. variable `current-prefix-argument'. Note that a single prefix is
  10155. used to force opening a link in Emacs, so it may be good to only
  10156. use a numeric or double prefix to guide the search function.
  10157. In case this is needed, a function in this hook can also restore
  10158. the window configuration before `org-open-at-point' was called using:
  10159. (set-window-configuration org-window-config-before-follow-link)")
  10160. (defun org-link-search (s &optional type avoid-pos)
  10161. "Search for a link search option.
  10162. If S is surrounded by forward slashes, it is interpreted as a
  10163. regular expression. In org-mode files, this will create an `org-occur'
  10164. sparse tree. In ordinary files, `occur' will be used to list matches.
  10165. If the current buffer is in `dired-mode', grep will be used to search
  10166. in all files. If AVOID-POS is given, ignore matches near that position."
  10167. (let ((case-fold-search t)
  10168. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  10169. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  10170. (append '(("") (" ") ("\t") ("\n"))
  10171. org-emphasis-alist)
  10172. "\\|") "\\)"))
  10173. (pos (point))
  10174. (pre "") (post "")
  10175. words re0 re1 re2 re3 re4 re5 re2a reall)
  10176. (cond
  10177. ;; First check if there are any special
  10178. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  10179. ;; Now try the builtin stuff
  10180. ((save-excursion
  10181. (goto-char (point-min))
  10182. (and
  10183. (re-search-forward
  10184. (concat "<<" (regexp-quote s0) ">>") nil t)
  10185. (setq pos (match-beginning 0))))
  10186. ;; There is an exact target for this
  10187. (goto-char pos))
  10188. ((string-match "^/\\(.*\\)/$" s)
  10189. ;; A regular expression
  10190. (cond
  10191. ((org-mode-p)
  10192. (org-occur (match-string 1 s)))
  10193. ;;((eq major-mode 'dired-mode)
  10194. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  10195. (t (org-do-occur (match-string 1 s)))))
  10196. (t
  10197. ;; A normal search strings
  10198. (when (equal (string-to-char s) ?*)
  10199. ;; Anchor on headlines, post may include tags.
  10200. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  10201. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  10202. s (substring s 1)))
  10203. (remove-text-properties
  10204. 0 (length s)
  10205. '(face nil mouse-face nil keymap nil fontified nil) s)
  10206. ;; Make a series of regular expressions to find a match
  10207. (setq words (org-split-string s "[ \n\r\t]+")
  10208. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10209. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10210. "\\)" markers)
  10211. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  10212. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10213. re1 (concat pre re2 post)
  10214. re3 (concat pre re4 post)
  10215. re5 (concat pre ".*" re4)
  10216. re2 (concat pre re2)
  10217. re2a (concat pre re2a)
  10218. re4 (concat pre re4)
  10219. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10220. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10221. re5 "\\)"
  10222. ))
  10223. (cond
  10224. ((eq type 'org-occur) (org-occur reall))
  10225. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10226. (t (goto-char (point-min))
  10227. (if (or (org-search-not-self 1 re0 nil t)
  10228. (org-search-not-self 1 re1 nil t)
  10229. (org-search-not-self 1 re2 nil t)
  10230. (org-search-not-self 1 re2a nil t)
  10231. (org-search-not-self 1 re3 nil t)
  10232. (org-search-not-self 1 re4 nil t)
  10233. (org-search-not-self 1 re5 nil t)
  10234. )
  10235. (goto-char (match-beginning 1))
  10236. (goto-char pos)
  10237. (error "No match")))))
  10238. (t
  10239. ;; Normal string-search
  10240. (goto-char (point-min))
  10241. (if (search-forward s nil t)
  10242. (goto-char (match-beginning 0))
  10243. (error "No match"))))
  10244. (and (org-mode-p) (org-show-context 'link-search))))
  10245. (defun org-search-not-self (group &rest args)
  10246. "Execute `re-search-forward', but only accept matches that do not
  10247. enclose the position of `org-open-link-marker'."
  10248. (let ((m org-open-link-marker))
  10249. (catch 'exit
  10250. (while (apply 're-search-forward args)
  10251. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10252. (goto-char (match-end group))
  10253. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10254. (> (match-beginning 0) (marker-position m))
  10255. (< (match-end 0) (marker-position m)))
  10256. (save-match-data
  10257. (or (not (org-in-regexp
  10258. org-bracket-link-analytic-regexp 1))
  10259. (not (match-end 4)) ; no description
  10260. (and (<= (match-beginning 4) (point))
  10261. (>= (match-end 4) (point))))))
  10262. (throw 'exit (point))))))))
  10263. (defun org-get-buffer-for-internal-link (buffer)
  10264. "Return a buffer to be used for displaying the link target of internal links."
  10265. (cond
  10266. ((not org-display-internal-link-with-indirect-buffer)
  10267. buffer)
  10268. ((string-match "(Clone)$" (buffer-name buffer))
  10269. (message "Buffer is already a clone, not making another one")
  10270. ;; we also do not modify visibility in this case
  10271. buffer)
  10272. (t ; make a new indirect buffer for displaying the link
  10273. (let* ((bn (buffer-name buffer))
  10274. (ibn (concat bn "(Clone)"))
  10275. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10276. (with-current-buffer ib (org-overview))
  10277. ib))))
  10278. (defun org-do-occur (regexp &optional cleanup)
  10279. "Call the Emacs command `occur'.
  10280. If CLEANUP is non-nil, remove the printout of the regular expression
  10281. in the *Occur* buffer. This is useful if the regex is long and not useful
  10282. to read."
  10283. (occur regexp)
  10284. (when cleanup
  10285. (let ((cwin (selected-window)) win beg end)
  10286. (when (setq win (get-buffer-window "*Occur*"))
  10287. (select-window win))
  10288. (goto-char (point-min))
  10289. (when (re-search-forward "match[a-z]+" nil t)
  10290. (setq beg (match-end 0))
  10291. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10292. (setq end (1- (match-beginning 0)))))
  10293. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  10294. (goto-char (point-min))
  10295. (select-window cwin))))
  10296. ;;; The mark ring for links jumps
  10297. (defvar org-mark-ring nil
  10298. "Mark ring for positions before jumps in Org-mode.")
  10299. (defvar org-mark-ring-last-goto nil
  10300. "Last position in the mark ring used to go back.")
  10301. ;; Fill and close the ring
  10302. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10303. (loop for i from 1 to org-mark-ring-length do
  10304. (push (make-marker) org-mark-ring))
  10305. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10306. org-mark-ring)
  10307. (defun org-mark-ring-push (&optional pos buffer)
  10308. "Put the current position or POS into the mark ring and rotate it."
  10309. (interactive)
  10310. (setq pos (or pos (point)))
  10311. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10312. (move-marker (car org-mark-ring)
  10313. (or pos (point))
  10314. (or buffer (current-buffer)))
  10315. (message
  10316. (substitute-command-keys
  10317. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10318. (defun org-mark-ring-goto (&optional n)
  10319. "Jump to the previous position in the mark ring.
  10320. With prefix arg N, jump back that many stored positions. When
  10321. called several times in succession, walk through the entire ring.
  10322. Org-mode commands jumping to a different position in the current file,
  10323. or to another Org-mode file, automatically push the old position
  10324. onto the ring."
  10325. (interactive "p")
  10326. (let (p m)
  10327. (if (eq last-command this-command)
  10328. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10329. (setq p org-mark-ring))
  10330. (setq org-mark-ring-last-goto p)
  10331. (setq m (car p))
  10332. (switch-to-buffer (marker-buffer m))
  10333. (goto-char m)
  10334. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10335. (defun org-remove-angle-brackets (s)
  10336. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10337. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10338. s)
  10339. (defun org-add-angle-brackets (s)
  10340. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10341. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10342. s)
  10343. ;;; Following specific links
  10344. (defun org-follow-timestamp-link ()
  10345. (cond
  10346. ((org-at-date-range-p t)
  10347. (let ((org-agenda-start-on-weekday)
  10348. (t1 (match-string 1))
  10349. (t2 (match-string 2)))
  10350. (setq t1 (time-to-days (org-time-string-to-time t1))
  10351. t2 (time-to-days (org-time-string-to-time t2)))
  10352. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  10353. ((org-at-timestamp-p t)
  10354. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10355. (substring (match-string 1) 0 10)))
  10356. 1))
  10357. (t (error "This should not happen"))))
  10358. (defun org-follow-bbdb-link (name)
  10359. "Follow a BBDB link to NAME."
  10360. (require 'bbdb)
  10361. (let ((inhibit-redisplay (not debug-on-error))
  10362. (bbdb-electric-p nil))
  10363. (catch 'exit
  10364. ;; Exact match on name
  10365. (bbdb-name (concat "\\`" name "\\'") nil)
  10366. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10367. ;; Exact match on name
  10368. (bbdb-company (concat "\\`" name "\\'") nil)
  10369. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10370. ;; Partial match on name
  10371. (bbdb-name name nil)
  10372. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10373. ;; Partial match on company
  10374. (bbdb-company name nil)
  10375. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10376. ;; General match including network address and notes
  10377. (bbdb name nil)
  10378. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  10379. (delete-window (get-buffer-window "*BBDB*"))
  10380. (error "No matching BBDB record")))))
  10381. (defun org-follow-info-link (name)
  10382. "Follow an info file & node link to NAME."
  10383. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  10384. (string-match "\\(.*\\)" name))
  10385. (progn
  10386. (require 'info)
  10387. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  10388. (Info-find-node (match-string 1 name) (match-string 2 name))
  10389. (Info-find-node (match-string 1 name) "Top")))
  10390. (message (concat "Could not open: " name))))
  10391. (defun org-follow-gnus-link (&optional group article)
  10392. "Follow a Gnus link to GROUP and ARTICLE."
  10393. (require 'gnus)
  10394. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  10395. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  10396. (cond ((and group article)
  10397. (gnus-group-read-group 1 nil group)
  10398. (gnus-summary-goto-article (string-to-number article) nil t))
  10399. (group (gnus-group-jump-to-group group))))
  10400. (defun org-follow-vm-link (&optional folder article readonly)
  10401. "Follow a VM link to FOLDER and ARTICLE."
  10402. (require 'vm)
  10403. (setq article (org-add-angle-brackets article))
  10404. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  10405. ;; ange-ftp or efs or tramp access
  10406. (let ((user (or (match-string 1 folder) (user-login-name)))
  10407. (host (match-string 2 folder))
  10408. (file (match-string 3 folder)))
  10409. (cond
  10410. ((featurep 'tramp)
  10411. ;; use tramp to access the file
  10412. (if (featurep 'xemacs)
  10413. (setq folder (format "[%s@%s]%s" user host file))
  10414. (setq folder (format "/%s@%s:%s" user host file))))
  10415. (t
  10416. ;; use ange-ftp or efs
  10417. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  10418. (setq folder (format "/%s@%s:%s" user host file))))))
  10419. (when folder
  10420. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  10421. (sit-for 0.1)
  10422. (when article
  10423. (vm-select-folder-buffer)
  10424. (widen)
  10425. (let ((case-fold-search t))
  10426. (goto-char (point-min))
  10427. (if (not (re-search-forward
  10428. (concat "^" "message-id: *" (regexp-quote article))))
  10429. (error "Could not find the specified message in this folder"))
  10430. (vm-isearch-update)
  10431. (vm-isearch-narrow)
  10432. (vm-beginning-of-message)
  10433. (vm-summarize)))))
  10434. (defun org-follow-wl-link (folder article)
  10435. "Follow a Wanderlust link to FOLDER and ARTICLE."
  10436. (if (and (string= folder "%")
  10437. article
  10438. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  10439. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  10440. ;; Thus, we recompose folder and article ids.
  10441. (setq folder (format "%s#%s" folder (match-string 1 article))
  10442. article (match-string 3 article)))
  10443. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  10444. (error "No such folder: %s" folder))
  10445. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  10446. (and article
  10447. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  10448. (wl-summary-redisplay)))
  10449. (defun org-follow-rmail-link (folder article)
  10450. "Follow an RMAIL link to FOLDER and ARTICLE."
  10451. (setq article (org-add-angle-brackets article))
  10452. (let (message-number)
  10453. (save-excursion
  10454. (save-window-excursion
  10455. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10456. (setq message-number
  10457. (save-restriction
  10458. (widen)
  10459. (goto-char (point-max))
  10460. (if (re-search-backward
  10461. (concat "^Message-ID:\\s-+" (regexp-quote
  10462. (or article "")))
  10463. nil t)
  10464. (rmail-what-message))))))
  10465. (if message-number
  10466. (progn
  10467. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10468. (rmail-show-message message-number)
  10469. message-number)
  10470. (error "Message not found"))))
  10471. ;;; mh-e integration based on planner-mode
  10472. (defun org-mhe-get-message-real-folder ()
  10473. "Return the name of the current message real folder, so if you use
  10474. sequences, it will now work."
  10475. (save-excursion
  10476. (let* ((folder
  10477. (if (equal major-mode 'mh-folder-mode)
  10478. mh-current-folder
  10479. ;; Refer to the show buffer
  10480. mh-show-folder-buffer))
  10481. (end-index
  10482. (if (boundp 'mh-index-folder)
  10483. (min (length mh-index-folder) (length folder))))
  10484. )
  10485. ;; a simple test on mh-index-data does not work, because
  10486. ;; mh-index-data is always nil in a show buffer.
  10487. (if (and (boundp 'mh-index-folder)
  10488. (string= mh-index-folder (substring folder 0 end-index)))
  10489. (if (equal major-mode 'mh-show-mode)
  10490. (save-window-excursion
  10491. (when (buffer-live-p (get-buffer folder))
  10492. (progn
  10493. (pop-to-buffer folder)
  10494. (org-mhe-get-message-folder-from-index)
  10495. )
  10496. ))
  10497. (org-mhe-get-message-folder-from-index)
  10498. )
  10499. folder
  10500. )
  10501. )))
  10502. (defun org-mhe-get-message-folder-from-index ()
  10503. "Returns the name of the message folder in a index folder buffer."
  10504. (save-excursion
  10505. (mh-index-previous-folder)
  10506. (re-search-forward "^\\(+.*\\)$" nil t)
  10507. (message (match-string 1))))
  10508. (defun org-mhe-get-message-folder ()
  10509. "Return the name of the current message folder. Be careful if you
  10510. use sequences."
  10511. (save-excursion
  10512. (if (equal major-mode 'mh-folder-mode)
  10513. mh-current-folder
  10514. ;; Refer to the show buffer
  10515. mh-show-folder-buffer)))
  10516. (defun org-mhe-get-message-num ()
  10517. "Return the number of the current message. Be careful if you
  10518. use sequences."
  10519. (save-excursion
  10520. (if (equal major-mode 'mh-folder-mode)
  10521. (mh-get-msg-num nil)
  10522. ;; Refer to the show buffer
  10523. (mh-show-buffer-message-number))))
  10524. (defun org-mhe-get-header (header)
  10525. "Return a header of the message in folder mode. This will create a
  10526. show buffer for the corresponding message. If you have a more clever
  10527. idea..."
  10528. (let* ((folder (org-mhe-get-message-folder))
  10529. (num (org-mhe-get-message-num))
  10530. (buffer (get-buffer-create (concat "show-" folder)))
  10531. (header-field))
  10532. (with-current-buffer buffer
  10533. (mh-display-msg num folder)
  10534. (if (equal major-mode 'mh-folder-mode)
  10535. (mh-header-display)
  10536. (mh-show-header-display))
  10537. (set-buffer buffer)
  10538. (setq header-field (mh-get-header-field header))
  10539. (if (equal major-mode 'mh-folder-mode)
  10540. (mh-show)
  10541. (mh-show-show))
  10542. header-field)))
  10543. (defun org-follow-mhe-link (folder article)
  10544. "Follow an MHE link to FOLDER and ARTICLE.
  10545. If ARTICLE is nil FOLDER is shown. If the configuration variable
  10546. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  10547. ARTICLE is searched in all folders. Indexed searches (swish++,
  10548. namazu, and others supported by MH-E) will always search in all
  10549. folders."
  10550. (require 'mh-e)
  10551. (require 'mh-search)
  10552. (require 'mh-utils)
  10553. (mh-find-path)
  10554. (if (not article)
  10555. (mh-visit-folder (mh-normalize-folder-name folder))
  10556. (setq article (org-add-angle-brackets article))
  10557. (mh-search-choose)
  10558. (if (equal mh-searcher 'pick)
  10559. (progn
  10560. (mh-search folder (list "--message-id" article))
  10561. (when (and org-mhe-search-all-folders
  10562. (not (org-mhe-get-message-real-folder)))
  10563. (kill-this-buffer)
  10564. (mh-search "+" (list "--message-id" article))))
  10565. (mh-search "+" article))
  10566. (if (org-mhe-get-message-real-folder)
  10567. (mh-show-msg 1)
  10568. (kill-this-buffer)
  10569. (error "Message not found"))))
  10570. ;;; BibTeX links
  10571. ;; Use the custom search meachnism to construct and use search strings for
  10572. ;; file links to BibTeX database entries.
  10573. (defun org-create-file-search-in-bibtex ()
  10574. "Create the search string and description for a BibTeX database entry."
  10575. (when (eq major-mode 'bibtex-mode)
  10576. ;; yes, we want to construct this search string.
  10577. ;; Make a good description for this entry, using names, year and the title
  10578. ;; Put it into the `description' variable which is dynamically scoped.
  10579. (let ((bibtex-autokey-names 1)
  10580. (bibtex-autokey-names-stretch 1)
  10581. (bibtex-autokey-name-case-convert-function 'identity)
  10582. (bibtex-autokey-name-separator " & ")
  10583. (bibtex-autokey-additional-names " et al.")
  10584. (bibtex-autokey-year-length 4)
  10585. (bibtex-autokey-name-year-separator " ")
  10586. (bibtex-autokey-titlewords 3)
  10587. (bibtex-autokey-titleword-separator " ")
  10588. (bibtex-autokey-titleword-case-convert-function 'identity)
  10589. (bibtex-autokey-titleword-length 'infty)
  10590. (bibtex-autokey-year-title-separator ": "))
  10591. (setq description (bibtex-generate-autokey)))
  10592. ;; Now parse the entry, get the key and return it.
  10593. (save-excursion
  10594. (bibtex-beginning-of-entry)
  10595. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  10596. (defun org-execute-file-search-in-bibtex (s)
  10597. "Find the link search string S as a key for a database entry."
  10598. (when (eq major-mode 'bibtex-mode)
  10599. ;; Yes, we want to do the search in this file.
  10600. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  10601. (goto-char (point-min))
  10602. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  10603. (regexp-quote s) "[ \t\n]*,") nil t)
  10604. (goto-char (match-beginning 0)))
  10605. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  10606. ;; Use double prefix to indicate that any web link should be browsed
  10607. (let ((b (current-buffer)) (p (point)))
  10608. ;; Restore the window configuration because we just use the web link
  10609. (set-window-configuration org-window-config-before-follow-link)
  10610. (save-excursion (set-buffer b) (goto-char p)
  10611. (bibtex-url)))
  10612. (recenter 0)) ; Move entry start to beginning of window
  10613. ;; return t to indicate that the search is done.
  10614. t))
  10615. ;; Finally add the functions to the right hooks.
  10616. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  10617. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  10618. ;; end of Bibtex link setup
  10619. ;;; Following file links
  10620. (defun org-open-file (path &optional in-emacs line search)
  10621. "Open the file at PATH.
  10622. First, this expands any special file name abbreviations. Then the
  10623. configuration variable `org-file-apps' is checked if it contains an
  10624. entry for this file type, and if yes, the corresponding command is launched.
  10625. If no application is found, Emacs simply visits the file.
  10626. With optional argument IN-EMACS, Emacs will visit the file.
  10627. Optional LINE specifies a line to go to, optional SEARCH a string to
  10628. search for. If LINE or SEARCH is given, the file will always be
  10629. opened in Emacs.
  10630. If the file does not exist, an error is thrown."
  10631. (setq in-emacs (or in-emacs line search))
  10632. (let* ((file (if (equal path "")
  10633. buffer-file-name
  10634. (substitute-in-file-name (expand-file-name path))))
  10635. (apps (append org-file-apps (org-default-apps)))
  10636. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10637. (dirp (if remp nil (file-directory-p file)))
  10638. (dfile (downcase file))
  10639. (old-buffer (current-buffer))
  10640. (old-pos (point))
  10641. (old-mode major-mode)
  10642. ext cmd)
  10643. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10644. (setq ext (match-string 1 dfile))
  10645. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10646. (setq ext (match-string 1 dfile))))
  10647. (if in-emacs
  10648. (setq cmd 'emacs)
  10649. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10650. (and dirp (cdr (assoc 'directory apps)))
  10651. (cdr (assoc ext apps))
  10652. (cdr (assoc t apps)))))
  10653. (when (eq cmd 'mailcap)
  10654. (require 'mailcap)
  10655. (mailcap-parse-mailcaps)
  10656. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10657. (command (mailcap-mime-info mime-type)))
  10658. (if (stringp command)
  10659. (setq cmd command)
  10660. (setq cmd 'emacs))))
  10661. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10662. (not (file-exists-p file))
  10663. (not org-open-non-existing-files))
  10664. (error "No such file: %s" file))
  10665. (cond
  10666. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10667. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10668. (if (string-match "['\"]%s['\"]" cmd)
  10669. (setq cmd (replace-match "%s" t t cmd)))
  10670. (setq cmd (format cmd (shell-quote-argument file)))
  10671. (save-window-excursion
  10672. (shell-command (concat cmd " &"))))
  10673. ((or (stringp cmd)
  10674. (eq cmd 'emacs))
  10675. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10676. (widen)
  10677. (if line (goto-line line)
  10678. (if search (org-link-search search))))
  10679. ((consp cmd)
  10680. (eval cmd))
  10681. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10682. (and (org-mode-p) (eq old-mode 'org-mode)
  10683. (or (not (equal old-buffer (current-buffer)))
  10684. (not (equal old-pos (point))))
  10685. (org-mark-ring-push old-pos old-buffer))))
  10686. (defun org-default-apps ()
  10687. "Return the default applications for this operating system."
  10688. (cond
  10689. ((eq system-type 'darwin)
  10690. org-file-apps-defaults-macosx)
  10691. ((eq system-type 'windows-nt)
  10692. org-file-apps-defaults-windowsnt)
  10693. (t org-file-apps-defaults-gnu)))
  10694. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10695. (defun org-file-remote-p (file)
  10696. "Test whether FILE specifies a location on a remote system.
  10697. Return non-nil if the location is indeed remote.
  10698. For example, the filename \"/user@host:/foo\" specifies a location
  10699. on the system \"/user@host:\"."
  10700. (cond ((fboundp 'file-remote-p)
  10701. (file-remote-p file))
  10702. ((fboundp 'tramp-handle-file-remote-p)
  10703. (tramp-handle-file-remote-p file))
  10704. ((and (boundp 'ange-ftp-name-format)
  10705. (string-match (car ange-ftp-name-format) file))
  10706. t)
  10707. (t nil)))
  10708. ;;;; Hooks for remember.el
  10709. ;;;###autoload
  10710. (defun org-remember-annotation ()
  10711. "Return a link to the current location as an annotation for remember.el.
  10712. If you are using Org-mode files as target for data storage with
  10713. remember.el, then the annotations should include a link compatible with the
  10714. conventions in Org-mode. This function returns such a link."
  10715. (org-store-link nil))
  10716. (defconst org-remember-help
  10717. "Select a destination location for the note.
  10718. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  10719. RET at beg-of-buf -> Append to file as level 2 headline
  10720. RET on headline -> Store as sublevel entry to current headline
  10721. <left>/<right> -> before/after current headline, same headings level")
  10722. ;;;###autoload
  10723. (defun org-remember-apply-template (&optional use-char skip-interactive)
  10724. "Initialize *remember* buffer with template, invoke `org-mode'.
  10725. This function should be placed into `remember-mode-hook' and in fact requires
  10726. to be run from that hook to fucntion properly."
  10727. (if org-remember-templates
  10728. (let* ((char (or use-char
  10729. (if (= (length org-remember-templates) 1)
  10730. (caar org-remember-templates)
  10731. (message "Select template: %s"
  10732. (mapconcat
  10733. (lambda (x) (char-to-string (car x)))
  10734. org-remember-templates " "))
  10735. (read-char-exclusive))))
  10736. (entry (cdr (assoc char org-remember-templates)))
  10737. (tpl (car entry))
  10738. (plist-p (if org-store-link-plist t nil))
  10739. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  10740. (string-match "\\S-" (nth 1 entry)))
  10741. (nth 1 entry)
  10742. org-default-notes-file))
  10743. (headline (nth 2 entry))
  10744. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  10745. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  10746. (v-u (concat "[" (substring v-t 1 -1) "]"))
  10747. (v-U (concat "[" (substring v-T 1 -1) "]"))
  10748. (v-i initial) ; defined in `remember-mode'
  10749. (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
  10750. (v-n user-full-name)
  10751. (org-startup-folded nil)
  10752. org-time-was-given org-end-time-was-given x prompt char time)
  10753. (setq org-store-link-plist
  10754. (append (list :annotation v-a :initial v-i)))
  10755. (unless tpl (setq tpl "") (message "No template") (ding))
  10756. (erase-buffer)
  10757. (insert (substitute-command-keys
  10758. (format
  10759. "## `C-c C-c' to file interactively, `C-u C-c C-c' to file directly.
  10760. ## Target file \"%s\", headline \"%s\"
  10761. ## To switch templates, use `\\[org-remember]'.\n\n"
  10762. (abbreviate-file-name (or file org-default-notes-file))
  10763. (or headline ""))))
  10764. (insert tpl) (goto-char (point-min))
  10765. ;; Simple %-escapes
  10766. (while (re-search-forward "%\\([tTuUai]\\)" nil t)
  10767. (when (and initial (equal (match-string 0) "%i"))
  10768. (save-match-data
  10769. (let* ((lead (buffer-substring
  10770. (point-at-bol) (match-beginning 0))))
  10771. (setq v-i (mapconcat 'identity
  10772. (org-split-string initial "\n")
  10773. (concat "\n" lead))))))
  10774. (replace-match
  10775. (or (eval (intern (concat "v-" (match-string 1)))) "")
  10776. t t))
  10777. ;; From the property list
  10778. (when plist-p
  10779. (goto-char (point-min))
  10780. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  10781. (and (setq x (plist-get org-store-link-plist
  10782. (intern (match-string 1))))
  10783. (replace-match x t t))))
  10784. ;; Turn on org-mode in the remember buffer, set local variables
  10785. (org-mode)
  10786. (org-set-local 'org-finish-function 'remember-buffer)
  10787. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  10788. (org-set-local 'org-default-notes-file file))
  10789. (if (and headline (stringp headline) (string-match "\\S-" headline))
  10790. (org-set-local 'org-remember-default-headline headline))
  10791. ;; Interactive template entries
  10792. (goto-char (point-min))
  10793. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  10794. (setq char (if (match-end 3) (match-string 3))
  10795. prompt (if (match-end 2) (match-string 2)))
  10796. (goto-char (match-beginning 0))
  10797. (replace-match "")
  10798. (cond
  10799. ((member char '("G" "g"))
  10800. (let* ((org-last-tags-completion-table
  10801. (org-global-tags-completion-table
  10802. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  10803. (org-add-colon-after-tag-completion t)
  10804. (ins (completing-read
  10805. (if prompt (concat prompt ": ") "Tags: ")
  10806. 'org-tags-completion-function nil nil nil
  10807. 'org-tags-history)))
  10808. (setq ins (mapconcat 'identity
  10809. (org-split-string ins (org-re "[^[:alnum:]]+"))
  10810. ":"))
  10811. (when (string-match "\\S-" ins)
  10812. (or (equal (char-before) ?:) (insert ":"))
  10813. (insert ins)
  10814. (or (equal (char-after) ?:) (insert ":")))))
  10815. (char
  10816. (setq org-time-was-given (equal (upcase char) char))
  10817. (setq time (org-read-date (equal (upcase char) "U") t nil
  10818. prompt))
  10819. (org-insert-time-stamp time org-time-was-given
  10820. (member char '("u" "U"))
  10821. nil nil (list org-end-time-was-given)))
  10822. (t
  10823. (insert (read-string
  10824. (if prompt (concat prompt ": ") "Enter string"))))))
  10825. (goto-char (point-min))
  10826. (if (re-search-forward "%\\?" nil t)
  10827. (replace-match "")
  10828. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  10829. (org-mode)
  10830. (org-set-local 'org-finish-function 'remember-buffer)))
  10831. ;;;###autoload
  10832. (defun org-remember ()
  10833. "Call `remember'. If this is already a remember buffer, re-apply template.
  10834. If there is an active region, make sure remember uses it as initial content
  10835. of the remember buffer."
  10836. (interactive)
  10837. (if (eq org-finish-function 'remember-buffer)
  10838. (progn
  10839. (when (< (length org-remember-templates) 2)
  10840. (error "No other template available"))
  10841. (erase-buffer)
  10842. (let ((annotation (plist-get org-store-link-plist :annotation))
  10843. (initial (plist-get org-store-link-plist :initial)))
  10844. (org-remember-apply-template))
  10845. (message "Press C-c C-c to remember data"))
  10846. (if (org-region-active-p)
  10847. (remember (buffer-substring (point) (mark)))
  10848. (call-interactively 'remember))))
  10849. ;;;###autoload
  10850. (defun org-remember-handler ()
  10851. "Store stuff from remember.el into an org file.
  10852. First prompts for an org file. If the user just presses return, the value
  10853. of `org-default-notes-file' is used.
  10854. Then the command offers the headings tree of the selected file in order to
  10855. file the text at a specific location.
  10856. You can either immediately press RET to get the note appended to the
  10857. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  10858. find a better place. Then press RET or <left> or <right> in insert the note.
  10859. Key Cursor position Note gets inserted
  10860. -----------------------------------------------------------------------------
  10861. RET buffer-start as level 2 heading at end of file
  10862. RET on headline as sublevel of the heading at cursor
  10863. RET no heading at cursor position, level taken from context.
  10864. Or use prefix arg to specify level manually.
  10865. <left> on headline as same level, before current heading
  10866. <right> on headline as same level, after current heading
  10867. So the fastest way to store the note is to press RET RET to append it to
  10868. the default file. This way your current train of thought is not
  10869. interrupted, in accordance with the principles of remember.el. But with
  10870. little extra effort, you can push it directly to the correct location.
  10871. Before being stored away, the function ensures that the text has a
  10872. headline, i.e. a first line that starts with a \"*\". If not, a headline
  10873. is constructed from the current date and some additional data.
  10874. If the variable `org-adapt-indentation' is non-nil, the entire text is
  10875. also indented so that it starts in the same column as the headline
  10876. \(i.e. after the stars).
  10877. See also the variable `org-reverse-note-order'."
  10878. (goto-char (point-min))
  10879. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  10880. (replace-match ""))
  10881. (catch 'quit
  10882. (let* ((txt (buffer-substring (point-min) (point-max)))
  10883. (fastp (equal current-prefix-arg '(4)))
  10884. (file (if fastp org-default-notes-file (org-get-org-file)))
  10885. (heading org-remember-default-headline)
  10886. (visiting (org-find-base-buffer-visiting file))
  10887. (org-startup-folded nil)
  10888. (org-startup-align-all-tables nil)
  10889. (org-goto-start-pos 1)
  10890. spos level indent reversed)
  10891. (setq current-prefix-arg nil)
  10892. ;; Modify text so that it becomes a nice subtree which can be inserted
  10893. ;; into an org tree.
  10894. (let* ((lines (split-string txt "\n"))
  10895. first)
  10896. (setq first (car lines) lines (cdr lines))
  10897. (if (string-match "^\\*+ " first)
  10898. ;; Is already a headline
  10899. (setq indent nil)
  10900. ;; We need to add a headline: Use time and first buffer line
  10901. (setq lines (cons first lines)
  10902. first (concat "* " (current-time-string)
  10903. " (" (remember-buffer-desc) ")")
  10904. indent " "))
  10905. (if (and org-adapt-indentation indent)
  10906. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  10907. (setq txt (concat first "\n"
  10908. (mapconcat 'identity lines "\n"))))
  10909. ;; Find the file
  10910. (if (not visiting) (find-file-noselect file))
  10911. (with-current-buffer (or visiting (get-file-buffer file))
  10912. (save-excursion
  10913. (save-restriction
  10914. (widen)
  10915. (and (goto-char (point-min))
  10916. (not (re-search-forward "^\\* " nil t))
  10917. (insert "\n* Notes\n"))
  10918. (setq reversed (org-notes-order-reversed-p))
  10919. ;; Find the default location
  10920. (when (and heading (stringp heading) (string-match "\\S-" heading))
  10921. (goto-char (point-min))
  10922. (if (re-search-forward
  10923. (concat "^\\*+[ \t]+" (regexp-quote heading)
  10924. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  10925. nil t)
  10926. (setq org-goto-start-pos (match-beginning 0))))
  10927. ;; Ask the User for a location
  10928. (setq spos (if fastp
  10929. org-goto-start-pos
  10930. (org-get-location (current-buffer) org-remember-help)))
  10931. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  10932. ; not handle this note
  10933. (goto-char spos)
  10934. (cond ((and (bobp) (not reversed))
  10935. ;; Put it at the end, one level below level 1
  10936. (save-restriction
  10937. (widen)
  10938. (goto-char (point-max))
  10939. (if (not (bolp)) (newline))
  10940. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  10941. ((and (bobp) reversed)
  10942. ;; Put it at the start, as level 1
  10943. (save-restriction
  10944. (widen)
  10945. (goto-char (point-min))
  10946. (re-search-forward "^\\*+ " nil t)
  10947. (beginning-of-line 1)
  10948. (org-paste-subtree 1 txt)))
  10949. ((and (org-on-heading-p t) (not current-prefix-arg))
  10950. ;; Put it below this entry, at the beg/end of the subtree
  10951. (org-back-to-heading t)
  10952. (setq level (funcall outline-level))
  10953. (if reversed
  10954. (outline-next-heading)
  10955. (org-end-of-subtree t))
  10956. (if (not (bolp)) (newline))
  10957. (beginning-of-line 1)
  10958. (org-paste-subtree (org-get-legal-level level 1) txt))
  10959. (t
  10960. ;; Put it right there, with automatic level determined by
  10961. ;; org-paste-subtree or from prefix arg
  10962. (org-paste-subtree
  10963. (if (numberp current-prefix-arg) current-prefix-arg)
  10964. txt)))
  10965. (when remember-save-after-remembering
  10966. (save-buffer)
  10967. (if (not visiting) (kill-buffer (current-buffer)))))))))
  10968. t) ;; return t to indicate that we took care of this note.
  10969. (defun org-get-org-file ()
  10970. "Read a filename, with default directory `org-directory'."
  10971. (let ((default (or org-default-notes-file remember-data-file)))
  10972. (read-file-name (format "File name [%s]: " default)
  10973. (file-name-as-directory org-directory)
  10974. default)))
  10975. (defun org-notes-order-reversed-p ()
  10976. "Check if the current file should receive notes in reversed order."
  10977. (cond
  10978. ((not org-reverse-note-order) nil)
  10979. ((eq t org-reverse-note-order) t)
  10980. ((not (listp org-reverse-note-order)) nil)
  10981. (t (catch 'exit
  10982. (let ((all org-reverse-note-order)
  10983. entry)
  10984. (while (setq entry (pop all))
  10985. (if (string-match (car entry) buffer-file-name)
  10986. (throw 'exit (cdr entry))))
  10987. nil)))))
  10988. ;;;; Dynamic blocks
  10989. (defun org-find-dblock (name)
  10990. "Find the first dynamic block with name NAME in the buffer.
  10991. If not found, stay at current position and return nil."
  10992. (let (pos)
  10993. (save-excursion
  10994. (goto-char (point-min))
  10995. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  10996. nil t)
  10997. (match-beginning 0))))
  10998. (if pos (goto-char pos))
  10999. pos))
  11000. (defconst org-dblock-start-re
  11001. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  11002. "Matches the startline of a dynamic block, with parameters.")
  11003. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  11004. "Matches the end of a dyhamic block.")
  11005. (defun org-create-dblock (plist)
  11006. "Create a dynamic block section, with parameters taken from PLIST.
  11007. PLIST must containe a :name entry which is used as name of the block."
  11008. (unless (bolp) (newline))
  11009. (let ((name (plist-get plist :name)))
  11010. (insert "#+BEGIN: " name)
  11011. (while plist
  11012. (if (eq (car plist) :name)
  11013. (setq plist (cddr plist))
  11014. (insert " " (prin1-to-string (pop plist)))))
  11015. (insert "\n\n#+END:\n")
  11016. (beginning-of-line -2)))
  11017. (defun org-prepare-dblock ()
  11018. "Prepare dynamic block for refresh.
  11019. This empties the block, puts the cursor at the insert position and returns
  11020. the property list including an extra property :name with the block name."
  11021. (unless (looking-at org-dblock-start-re)
  11022. (error "Not at a dynamic block"))
  11023. (let* ((begdel (1+ (match-end 0)))
  11024. (name (match-string 1))
  11025. (params (append (list :name name)
  11026. (read (concat "(" (match-string 3) ")")))))
  11027. (unless (re-search-forward org-dblock-end-re nil t)
  11028. (error "Dynamic block not terminated"))
  11029. (delete-region begdel (match-beginning 0))
  11030. (goto-char begdel)
  11031. (open-line 1)
  11032. params))
  11033. (defun org-map-dblocks (&optional command)
  11034. "Apply COMMAND to all dynamic blocks in the current buffer.
  11035. If COMMAND is not given, use `org-update-dblock'."
  11036. (let ((cmd (or command 'org-update-dblock))
  11037. pos)
  11038. (save-excursion
  11039. (goto-char (point-min))
  11040. (while (re-search-forward org-dblock-start-re nil t)
  11041. (goto-char (setq pos (match-beginning 0)))
  11042. (condition-case nil
  11043. (funcall cmd)
  11044. (error (message "Error during update of dynamic block")))
  11045. (goto-char pos)
  11046. (unless (re-search-forward org-dblock-end-re nil t)
  11047. (error "Dynamic block not terminated"))))))
  11048. (defun org-dblock-update (&optional arg)
  11049. "User command for updating dynamic blocks.
  11050. Update the dynamic block at point. With prefix ARG, update all dynamic
  11051. blocks in the buffer."
  11052. (interactive "P")
  11053. (if arg
  11054. (org-update-all-dblocks)
  11055. (or (looking-at org-dblock-start-re)
  11056. (org-beginning-of-dblock))
  11057. (org-update-dblock)))
  11058. (defun org-update-dblock ()
  11059. "Update the dynamic block at point
  11060. This means to empty the block, parse for parameters and then call
  11061. the correct writing function."
  11062. (let* ((pos (point))
  11063. (params (org-prepare-dblock))
  11064. (name (plist-get params :name))
  11065. (cmd (intern (concat "org-dblock-write:" name))))
  11066. (funcall cmd params)
  11067. (goto-char pos)))
  11068. (defun org-beginning-of-dblock ()
  11069. "Find the beginning of the dynamic block at point.
  11070. Error if there is no scuh block at point."
  11071. (let ((pos (point))
  11072. beg)
  11073. (end-of-line 1)
  11074. (if (and (re-search-backward org-dblock-start-re nil t)
  11075. (setq beg (match-beginning 0))
  11076. (re-search-forward org-dblock-end-re nil t)
  11077. (> (match-end 0) pos))
  11078. (goto-char beg)
  11079. (goto-char pos)
  11080. (error "Not in a dynamic block"))))
  11081. (defun org-update-all-dblocks ()
  11082. "Update all dynamic blocks in the buffer.
  11083. This function can be used in a hook."
  11084. (when (org-mode-p)
  11085. (org-map-dblocks 'org-update-dblock)))
  11086. ;;;; Completion
  11087. (defun org-complete (&optional arg)
  11088. "Perform completion on word at point.
  11089. At the beginning of a headline, this completes TODO keywords as given in
  11090. `org-todo-keywords'.
  11091. If the current word is preceded by a backslash, completes the TeX symbols
  11092. that are supported for HTML support.
  11093. If the current word is preceded by \"#+\", completes special words for
  11094. setting file options.
  11095. In the line after \"#+STARTUP:, complete valid keywords.\"
  11096. At all other locations, this simply calls `ispell-complete-word'."
  11097. (interactive "P")
  11098. (catch 'exit
  11099. (let* ((end (point))
  11100. (beg1 (save-excursion
  11101. (skip-chars-backward (org-re "[:alnum:]_@"))
  11102. (point)))
  11103. (beg (save-excursion
  11104. (skip-chars-backward "a-zA-Z0-9_:$")
  11105. (point)))
  11106. (confirm (lambda (x) (stringp (car x))))
  11107. (searchhead (equal (char-before beg) ?*))
  11108. (tag (and (equal (char-before beg1) ?:)
  11109. (equal (char-after (point-at-bol)) ?*)))
  11110. (prop (and (equal (char-before beg1) ?:)
  11111. (not (equal (char-after (point-at-bol)) ?*))))
  11112. (texp (equal (char-before beg) ?\\))
  11113. (link (equal (char-before beg) ?\[))
  11114. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  11115. beg)
  11116. "#+"))
  11117. (startup (string-match "^#\\+STARTUP:.*"
  11118. (buffer-substring (point-at-bol) (point))))
  11119. (completion-ignore-case opt)
  11120. (type nil)
  11121. (tbl nil)
  11122. (table (cond
  11123. (opt
  11124. (setq type :opt)
  11125. (mapcar (lambda (x)
  11126. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  11127. (cons (match-string 2 x) (match-string 1 x)))
  11128. (org-split-string (org-get-current-options) "\n")))
  11129. (startup
  11130. (setq type :startup)
  11131. org-startup-options)
  11132. (link (append org-link-abbrev-alist-local
  11133. org-link-abbrev-alist))
  11134. (texp
  11135. (setq type :tex)
  11136. org-html-entities)
  11137. ((string-match "\\`\\*+[ \t]+\\'"
  11138. (buffer-substring (point-at-bol) beg))
  11139. (setq type :todo)
  11140. (mapcar 'list org-todo-keywords-1))
  11141. (searchhead
  11142. (setq type :searchhead)
  11143. (save-excursion
  11144. (goto-char (point-min))
  11145. (while (re-search-forward org-todo-line-regexp nil t)
  11146. (push (list
  11147. (org-make-org-heading-search-string
  11148. (match-string 3) t))
  11149. tbl)))
  11150. tbl)
  11151. (tag (setq type :tag beg beg1)
  11152. (or org-tag-alist (org-get-buffer-tags)))
  11153. (prop (setq type :prop beg beg1)
  11154. (mapcar 'list (org-buffer-property-keys)))
  11155. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  11156. (pattern (buffer-substring-no-properties beg end))
  11157. (completion (try-completion pattern table confirm)))
  11158. (cond ((eq completion t)
  11159. (if (equal type :opt)
  11160. (insert (substring (cdr (assoc (upcase pattern) table))
  11161. (length pattern)))
  11162. (if (memq type '(:tag :prop)) (insert ":"))))
  11163. ((null completion)
  11164. (message "Can't find completion for \"%s\"" pattern)
  11165. (ding))
  11166. ((not (string= pattern completion))
  11167. (delete-region beg end)
  11168. (if (string-match " +$" completion)
  11169. (setq completion (replace-match "" t t completion)))
  11170. (insert completion)
  11171. (if (get-buffer-window "*Completions*")
  11172. (delete-window (get-buffer-window "*Completions*")))
  11173. (if (assoc completion table)
  11174. (if (eq type :todo) (insert " ")
  11175. (if (memq type '(:tag :prop)) (insert ":"))))
  11176. (if (and (equal type :opt) (assoc completion table))
  11177. (message "%s" (substitute-command-keys
  11178. "Press \\[org-complete] again to insert example settings"))))
  11179. (t
  11180. (message "Making completion list...")
  11181. (let ((list (sort (all-completions pattern table confirm)
  11182. 'string<)))
  11183. (with-output-to-temp-buffer "*Completions*"
  11184. (condition-case nil
  11185. ;; Protection needed for XEmacs and emacs 21
  11186. (display-completion-list list pattern)
  11187. (error (display-completion-list list)))))
  11188. (message "Making completion list...%s" "done"))))))
  11189. ;;;; TODO, DEADLINE, Comments
  11190. (defun org-toggle-comment ()
  11191. "Change the COMMENT state of an entry."
  11192. (interactive)
  11193. (save-excursion
  11194. (org-back-to-heading)
  11195. (if (looking-at (concat outline-regexp
  11196. "\\( *\\<" org-comment-string "\\>\\)"))
  11197. (replace-match "" t t nil 1)
  11198. (if (looking-at outline-regexp)
  11199. (progn
  11200. (goto-char (match-end 0))
  11201. (insert org-comment-string " "))))))
  11202. (defvar org-last-todo-state-is-todo nil
  11203. "This is non-nil when the last TODO state change led to a TODO state.
  11204. If the last change removed the TODO tag or switched to DONE, then
  11205. this is nil.")
  11206. (defun org-todo (&optional arg)
  11207. "Change the TODO state of an item.
  11208. The state of an item is given by a keyword at the start of the heading,
  11209. like
  11210. *** TODO Write paper
  11211. *** DONE Call mom
  11212. The different keywords are specified in the variable `org-todo-keywords'.
  11213. By default the available states are \"TODO\" and \"DONE\".
  11214. So for this example: when the item starts with TODO, it is changed to DONE.
  11215. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11216. DONE are present, add TODO at the beginning of the heading.
  11217. With C-u prefix arg, use completion to determine the new state.
  11218. With numeric prefix arg, switch to that state.
  11219. For calling through lisp, arg is also interpreted in the following way:
  11220. 'none -> empty state
  11221. \"\"(empty string) -> switch to empty state
  11222. 'done -> switch to DONE
  11223. 'nextset -> switch to the next set of keywords
  11224. 'previousset -> switch to the previous set of keywords
  11225. \"WAITING\" -> switch to the specified keyword, but only if it
  11226. really is a member of `org-todo-keywords'."
  11227. (interactive "P")
  11228. (save-excursion
  11229. (org-back-to-heading)
  11230. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  11231. (or (looking-at (concat " +" org-todo-regexp " *"))
  11232. (looking-at " *"))
  11233. (let* ((this (match-string 1))
  11234. (head (org-get-todo-sequence-head this))
  11235. (ass (assoc head org-todo-kwd-alist))
  11236. (interpret (nth 1 ass))
  11237. (done-word (nth 3 ass))
  11238. (final-done-word (nth 4 ass))
  11239. (last-state (or this ""))
  11240. (completion-ignore-case t)
  11241. (member (member this org-todo-keywords-1))
  11242. (tail (cdr member))
  11243. (state (cond
  11244. ((equal arg '(4))
  11245. ;; Read a state with completion
  11246. (completing-read "State: " (mapcar (lambda(x) (list x))
  11247. org-todo-keywords-1)
  11248. nil t))
  11249. ((eq arg 'right)
  11250. (if this
  11251. (if tail (car tail) nil)
  11252. (car org-todo-keywords-1)))
  11253. ((eq arg 'left)
  11254. (if (equal member org-todo-keywords-1)
  11255. nil
  11256. (if this
  11257. (nth (- (length org-todo-keywords-1) (length tail) 2)
  11258. org-todo-keywords-1)
  11259. (org-last org-todo-keywords-1))))
  11260. (arg
  11261. ;; user or caller requests a specific state
  11262. (cond
  11263. ((equal arg "") nil)
  11264. ((eq arg 'none) nil)
  11265. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11266. ((eq arg 'nextset)
  11267. (or (car (cdr (member head org-todo-heads)))
  11268. (car org-todo-heads)))
  11269. ((eq arg 'previousset)
  11270. (let ((org-todo-heads (reverse org-todo-heads)))
  11271. (or (car (cdr (member head org-todo-heads)))
  11272. (car org-todo-heads))))
  11273. ((car (member arg org-todo-keywords-1)))
  11274. ((nth (1- (prefix-numeric-value arg))
  11275. org-todo-keywords-1))))
  11276. ((null member) (or head (car org-todo-keywords-1)))
  11277. ((equal this final-done-word) nil) ;; -> make empty
  11278. ((null tail) nil) ;; -> first entry
  11279. ((eq interpret 'sequence)
  11280. (car tail))
  11281. ((memq interpret '(type priority))
  11282. (if (eq this-command last-command)
  11283. (car tail)
  11284. (if (> (length tail) 0)
  11285. (or done-word (car org-done-keywords))
  11286. nil)))
  11287. (t nil)))
  11288. (next (if state (concat " " state " ") " "))
  11289. dostates)
  11290. (replace-match next t t)
  11291. (unless head
  11292. (setq head (org-get-todo-sequence-head state)
  11293. ass (assoc head org-todo-kwd-alist)
  11294. interpret (nth 1 ass)
  11295. done-word (nth 3 ass)
  11296. final-done-word (nth 4 ass)))
  11297. (when (memq arg '(nextset previousset))
  11298. (message "Keyword set: %s"
  11299. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  11300. (setq org-last-todo-state-is-todo
  11301. (not (member state org-done-keywords)))
  11302. (when (and org-log-done (not (memq arg '(nextset previousset))))
  11303. (setq dostates (and (eq interpret 'sequence)
  11304. (listp org-log-done) (memq 'state org-log-done)))
  11305. (cond
  11306. ((and state (not this))
  11307. (org-add-planning-info nil nil 'closed)
  11308. (and dostates (org-add-log-maybe 'state state 'findpos)))
  11309. ((and state dostates)
  11310. (org-add-log-maybe 'state state 'findpos))
  11311. ((member state org-done-keywords)
  11312. ;; Planning info calls the note-setting command.
  11313. (org-add-planning-info 'closed (org-current-time)
  11314. (if (org-get-repeat) nil 'scheduled))
  11315. (org-add-log-maybe 'done state 'findpos))))
  11316. ;; Fixup tag positioning
  11317. (and org-auto-align-tags (org-set-tags nil t))
  11318. (run-hooks 'org-after-todo-state-change-hook)
  11319. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  11320. (if (and arg (not (member state org-done-keywords)))
  11321. (setq head (org-get-todo-sequence-head state)))
  11322. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
  11323. ;; Fixup cursor location if close to the keyword
  11324. (if (and (outline-on-heading-p)
  11325. (not (bolp))
  11326. (save-excursion (beginning-of-line 1)
  11327. (looking-at org-todo-line-regexp))
  11328. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11329. (progn
  11330. (goto-char (or (match-end 2) (match-end 1)))
  11331. (just-one-space))))
  11332. (defun org-get-todo-sequence-head (kwd)
  11333. "Return the head of the TODO sequence to which KWD belongs.
  11334. If KWD is not set, check if there is a text property remembering the
  11335. right sequence."
  11336. (let (p)
  11337. (cond
  11338. ((not kwd)
  11339. (or (get-text-property (point-at-bol) 'org-todo-head)
  11340. (progn
  11341. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11342. nil (point-at-eol)))
  11343. (get-text-property p 'org-todo-head))))
  11344. ((not (member kwd org-todo-keywords-1))
  11345. (car org-todo-keywords-1))
  11346. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11347. (defun org-get-repeat ()
  11348. "Check if tere is a deadline/schedule with repeater in this entry."
  11349. (save-match-data
  11350. (save-excursion
  11351. (org-back-to-heading t)
  11352. (if (re-search-forward
  11353. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  11354. (match-string 1)))))
  11355. (defvar org-last-changed-timestamp)
  11356. (defvar org-log-post-message)
  11357. (defun org-auto-repeat-maybe ()
  11358. "Check if the current headline contains a repeated deadline/schedule.
  11359. If yes, set TODO state back to what it was and change the base date
  11360. of repeating deadline/scheduled time stamps to new date.
  11361. This function should be run in the `org-after-todo-state-change-hook'."
  11362. ;; last-state is dynamically scoped into this function
  11363. (let* ((repeat (org-get-repeat))
  11364. (aa (assoc last-state org-todo-kwd-alist))
  11365. (interpret (nth 1 aa))
  11366. (head (nth 2 aa))
  11367. (done-word (nth 3 aa))
  11368. (whata '(("d" . day) ("m" . month) ("y" . year)))
  11369. (msg "Entry repeats: ")
  11370. (org-log-done)
  11371. re type n what ts)
  11372. (when repeat
  11373. (org-todo (if (eq interpret 'type) last-state head))
  11374. (when (and org-log-repeat
  11375. (not (memq 'org-add-log-note
  11376. (default-value 'post-command-hook))))
  11377. ;; Make sure a note is taken
  11378. (let ((org-log-done '(done)))
  11379. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  11380. 'findpos)))
  11381. (org-back-to-heading t)
  11382. (org-add-planning-info nil nil 'closed)
  11383. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11384. org-deadline-time-regexp "\\)"))
  11385. (while (re-search-forward
  11386. re (save-excursion (outline-next-heading) (point)) t)
  11387. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  11388. ts (match-string (if (match-end 2) 2 4)))
  11389. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  11390. (setq n (string-to-number (match-string 1 ts))
  11391. what (match-string 2 ts))
  11392. (if (equal what "w") (setq n (* n 7) what "d"))
  11393. (org-timestamp-change n (cdr (assoc what whata))))
  11394. (setq msg (concat msg type org-last-changed-timestamp " ")))
  11395. (setq org-log-post-message msg)
  11396. (message msg))))
  11397. (defun org-show-todo-tree (arg)
  11398. "Make a compact tree which shows all headlines marked with TODO.
  11399. The tree will show the lines where the regexp matches, and all higher
  11400. headlines above the match.
  11401. With \\[universal-argument] prefix, also show the DONE entries.
  11402. With a numeric prefix N, construct a sparse tree for the Nth element
  11403. of `org-todo-keywords-1'."
  11404. (interactive "P")
  11405. (let ((case-fold-search nil)
  11406. (kwd-re
  11407. (cond ((null arg) org-not-done-regexp)
  11408. ((equal arg '(4))
  11409. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  11410. (mapcar 'list org-todo-keywords-1))))
  11411. (concat "\\("
  11412. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11413. "\\)\\>")))
  11414. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11415. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11416. org-todo-keywords-1)))
  11417. (t (error "Invalid prefix argument: %s" arg)))))
  11418. (message "%d TODO entries found"
  11419. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  11420. (defun org-deadline ()
  11421. "Insert the DEADLINE: string to make a deadline.
  11422. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11423. to modify it to the correct date."
  11424. (interactive)
  11425. (org-add-planning-info 'deadline nil 'closed))
  11426. (defun org-schedule ()
  11427. "Insert the SCHEDULED: string to schedule a TODO item.
  11428. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11429. to modify it to the correct date."
  11430. (interactive)
  11431. (org-add-planning-info 'scheduled nil 'closed))
  11432. (defun org-add-planning-info (what &optional time &rest remove)
  11433. "Insert new timestamp with keyword in the line directly after the headline.
  11434. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  11435. If non is given, the user is prompted for a date.
  11436. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11437. be removed."
  11438. (interactive)
  11439. (let (org-time-was-given org-end-time-was-given)
  11440. (when what (setq time (or time (org-read-date nil 'to-time))))
  11441. (when (and org-insert-labeled-timestamps-at-point
  11442. (member what '(scheduled deadline)))
  11443. (insert
  11444. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11445. (org-insert-time-stamp time org-time-was-given
  11446. nil nil nil (list org-end-time-was-given))
  11447. (setq what nil))
  11448. (save-excursion
  11449. (save-restriction
  11450. (let (col list elt ts buffer-invisibility-spec)
  11451. (org-back-to-heading t)
  11452. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  11453. (goto-char (match-end 1))
  11454. (setq col (current-column))
  11455. (goto-char (match-end 0))
  11456. (if (eobp) (insert "\n"))
  11457. (forward-char 1)
  11458. (when (looking-at "[ \t]*:PROPERTIES:[ \t]*$")
  11459. (goto-char (match-end 0))
  11460. (if (eobp) (insert "\n"))
  11461. (forward-char 1))
  11462. (if (and (not (looking-at outline-regexp))
  11463. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11464. "[^\r\n]*"))
  11465. (not (equal (match-string 1) org-clock-string)))
  11466. (narrow-to-region (match-beginning 0) (match-end 0))
  11467. (insert "\n")
  11468. (backward-char 1)
  11469. (narrow-to-region (point) (point))
  11470. (indent-to-column col))
  11471. ;; Check if we have to remove something.
  11472. (setq list (cons what remove))
  11473. (while list
  11474. (setq elt (pop list))
  11475. (goto-char (point-min))
  11476. (when (or (and (eq elt 'scheduled)
  11477. (re-search-forward org-scheduled-time-regexp nil t))
  11478. (and (eq elt 'deadline)
  11479. (re-search-forward org-deadline-time-regexp nil t))
  11480. (and (eq elt 'closed)
  11481. (re-search-forward org-closed-time-regexp nil t)))
  11482. (replace-match "")
  11483. (if (looking-at "--+<[^>]+>") (replace-match ""))
  11484. (if (looking-at " +") (replace-match ""))))
  11485. (goto-char (point-max))
  11486. (when what
  11487. (insert
  11488. (if (not (equal (char-before) ?\ )) " " "")
  11489. (cond ((eq what 'scheduled) org-scheduled-string)
  11490. ((eq what 'deadline) org-deadline-string)
  11491. ((eq what 'closed) org-closed-string)
  11492. ((eq what 'archived) org-archived-string))
  11493. " ")
  11494. (org-insert-time-stamp
  11495. time
  11496. (or org-time-was-given
  11497. (and (eq what 'closed) org-log-done-with-time))
  11498. (eq what 'closed)
  11499. nil nil (list org-end-time-was-given))
  11500. (end-of-line 1))
  11501. (goto-char (point-min))
  11502. (widen)
  11503. (if (looking-at "[ \t]+\r?\n")
  11504. (replace-match ""))
  11505. ts)))))
  11506. (defvar org-log-note-marker (make-marker))
  11507. (defvar org-log-note-purpose nil)
  11508. (defvar org-log-note-state nil)
  11509. (defvar org-log-note-window-configuration nil)
  11510. (defvar org-log-note-return-to (make-marker))
  11511. (defvar org-log-post-message nil
  11512. "Message to be displayed after a log note has been stored.
  11513. The auto-repeater uses this.")
  11514. (defun org-add-log-maybe (&optional purpose state findpos)
  11515. "Set up the post command hook to take a note."
  11516. (save-excursion
  11517. (when (and (listp org-log-done)
  11518. (memq purpose org-log-done))
  11519. (when findpos
  11520. (org-back-to-heading t)
  11521. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  11522. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11523. "[^\r\n]*\\)?"))
  11524. (goto-char (match-end 0)))
  11525. (move-marker org-log-note-marker (point))
  11526. (setq org-log-note-purpose purpose)
  11527. (setq org-log-note-state state)
  11528. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  11529. (defun org-add-log-note (&optional purpose)
  11530. "Pop up a window for taking a note, and add this note later at point."
  11531. (remove-hook 'post-command-hook 'org-add-log-note)
  11532. (setq org-log-note-window-configuration (current-window-configuration))
  11533. (delete-other-windows)
  11534. (move-marker org-log-note-return-to (point))
  11535. (switch-to-buffer (marker-buffer org-log-note-marker))
  11536. (goto-char org-log-note-marker)
  11537. (switch-to-buffer-other-window "*Org Note*")
  11538. (erase-buffer)
  11539. (let ((org-inhibit-startup t)) (org-mode))
  11540. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  11541. (cond
  11542. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11543. ((eq org-log-note-purpose 'done) "closed todo item")
  11544. ((eq org-log-note-purpose 'state) "state change")
  11545. (t (error "This should not happen")))))
  11546. (org-set-local 'org-finish-function 'org-store-log-note))
  11547. (defun org-store-log-note ()
  11548. "Finish taking a log note, and insert it to where it belongs."
  11549. (let ((txt (buffer-string))
  11550. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11551. lines ind)
  11552. (kill-buffer (current-buffer))
  11553. (if (string-match "^#.*\n[ \t\n]*" txt)
  11554. (setq txt (replace-match "" t t txt)))
  11555. (if (string-match "\\s-+\\'" txt)
  11556. (setq txt (replace-match "" t t txt)))
  11557. (setq lines (org-split-string txt "\n"))
  11558. (when (and note (string-match "\\S-" note))
  11559. (setq note
  11560. (org-replace-escapes
  11561. note
  11562. (list (cons "%u" (user-login-name))
  11563. (cons "%U" user-full-name)
  11564. (cons "%t" (format-time-string
  11565. (org-time-stamp-format 'long 'inactive)
  11566. (current-time)))
  11567. (cons "%s" (if org-log-note-state
  11568. (concat "\"" org-log-note-state "\"")
  11569. "")))))
  11570. (if lines (setq note (concat note " \\\\")))
  11571. (push note lines))
  11572. (when lines
  11573. (save-excursion
  11574. (set-buffer (marker-buffer org-log-note-marker))
  11575. (save-excursion
  11576. (goto-char org-log-note-marker)
  11577. (move-marker org-log-note-marker nil)
  11578. (end-of-line 1)
  11579. (if (not (bolp)) (insert "\n")) (indent-relative nil)
  11580. (setq ind (concat (buffer-substring (point-at-bol) (point)) " "))
  11581. (insert " - " (pop lines))
  11582. (while lines
  11583. (insert "\n" ind (pop lines)))))))
  11584. (set-window-configuration org-log-note-window-configuration)
  11585. (with-current-buffer (marker-buffer org-log-note-return-to)
  11586. (goto-char org-log-note-return-to))
  11587. (move-marker org-log-note-return-to nil)
  11588. (and org-log-post-message (message org-log-post-message)))
  11589. (defvar org-occur-highlights nil)
  11590. (make-variable-buffer-local 'org-occur-highlights)
  11591. (defun org-occur (regexp &optional keep-previous callback)
  11592. "Make a compact tree which shows all matches of REGEXP.
  11593. The tree will show the lines where the regexp matches, and all higher
  11594. headlines above the match. It will also show the heading after the match,
  11595. to make sure editing the matching entry is easy.
  11596. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11597. call to `org-occur' will be kept, to allow stacking of calls to this
  11598. command.
  11599. If CALLBACK is non-nil, it is a function which is called to confirm
  11600. that the match should indeed be shown."
  11601. (interactive "sRegexp: \nP")
  11602. (or keep-previous (org-remove-occur-highlights nil nil t))
  11603. (let ((cnt 0))
  11604. (save-excursion
  11605. (goto-char (point-min))
  11606. (if (or (not keep-previous) ; do not want to keep
  11607. (not org-occur-highlights)) ; no previous matches
  11608. ;; hide everything
  11609. (org-overview))
  11610. (while (re-search-forward regexp nil t)
  11611. (when (or (not callback)
  11612. (save-match-data (funcall callback)))
  11613. (setq cnt (1+ cnt))
  11614. (when org-highlight-sparse-tree-matches
  11615. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11616. (org-show-context 'occur-tree))))
  11617. (when org-remove-highlights-with-change
  11618. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11619. nil 'local))
  11620. (unless org-sparse-tree-open-archived-trees
  11621. (org-hide-archived-subtrees (point-min) (point-max)))
  11622. (run-hooks 'org-occur-hook)
  11623. (if (interactive-p)
  11624. (message "%d match(es) for regexp %s" cnt regexp))
  11625. cnt))
  11626. (defun org-show-context (&optional key)
  11627. "Make sure point and context and visible.
  11628. How much context is shown depends upon the variables
  11629. `org-show-hierarchy-above', `org-show-following-heading'. and
  11630. `org-show-siblings'."
  11631. (let ((heading-p (org-on-heading-p t))
  11632. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11633. (following-p (org-get-alist-option org-show-following-heading key))
  11634. (siblings-p (org-get-alist-option org-show-siblings key)))
  11635. (catch 'exit
  11636. ;; Show heading or entry text
  11637. (if heading-p
  11638. (org-flag-heading nil) ; only show the heading
  11639. (and (or (org-invisible-p) (org-invisible-p2))
  11640. (org-show-hidden-entry))) ; show entire entry
  11641. (when following-p
  11642. ;; Show next sibling, or heading below text
  11643. (save-excursion
  11644. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11645. (org-flag-heading nil))))
  11646. (when siblings-p (org-show-siblings))
  11647. (when hierarchy-p
  11648. ;; show all higher headings, possibly with siblings
  11649. (save-excursion
  11650. (while (and (condition-case nil
  11651. (progn (org-up-heading-all 1) t)
  11652. (error nil))
  11653. (not (bobp)))
  11654. (org-flag-heading nil)
  11655. (when siblings-p (org-show-siblings))))))))
  11656. (defun org-reveal (&optional siblings)
  11657. "Show current entry, hierarchy above it, and the following headline.
  11658. This can be used to show a consistent set of context around locations
  11659. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11660. not t for the search context.
  11661. With optional argument SIBLINGS, on each level of the hierarchy all
  11662. siblings are shown. This repairs the tree structure to what it would
  11663. look like when opened with hierarchical calls to `org-cycle'."
  11664. (interactive "P")
  11665. (let ((org-show-hierarchy-above t)
  11666. (org-show-following-heading t)
  11667. (org-show-siblings (if siblings t org-show-siblings)))
  11668. (org-show-context nil)))
  11669. (defun org-highlight-new-match (beg end)
  11670. "Highlight from BEG to END and mark the highlight is an occur headline."
  11671. (let ((ov (org-make-overlay beg end)))
  11672. (org-overlay-put ov 'face 'secondary-selection)
  11673. (push ov org-occur-highlights)))
  11674. (defun org-remove-occur-highlights (&optional beg end noremove)
  11675. "Remove the occur highlights from the buffer.
  11676. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11677. from the `before-change-functions' in the current buffer."
  11678. (interactive)
  11679. (unless org-inhibit-highlight-removal
  11680. (mapc 'org-delete-overlay org-occur-highlights)
  11681. (setq org-occur-highlights nil)
  11682. (unless noremove
  11683. (remove-hook 'before-change-functions
  11684. 'org-remove-occur-highlights 'local))))
  11685. ;;;; Priorities
  11686. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11687. "Regular expression matching the priority indicator.")
  11688. (defvar org-remove-priority-next-time nil)
  11689. (defun org-priority-up ()
  11690. "Increase the priority of the current item."
  11691. (interactive)
  11692. (org-priority 'up))
  11693. (defun org-priority-down ()
  11694. "Decrease the priority of the current item."
  11695. (interactive)
  11696. (org-priority 'down))
  11697. (defun org-priority (&optional action)
  11698. "Change the priority of an item by ARG.
  11699. ACTION can be `set', `up', `down', or a character."
  11700. (interactive)
  11701. (setq action (or action 'set))
  11702. (let (current new news have remove)
  11703. (save-excursion
  11704. (org-back-to-heading)
  11705. (if (looking-at org-priority-regexp)
  11706. (setq current (string-to-char (match-string 2))
  11707. have t)
  11708. (setq current org-default-priority))
  11709. (cond
  11710. ((or (eq action 'set) (integerp action))
  11711. (if (integerp action)
  11712. (setq new action)
  11713. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  11714. (setq new (read-char-exclusive)))
  11715. (cond ((equal new ?\ ) (setq remove t))
  11716. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11717. (error "Priority must be between `%c' and `%c'"
  11718. org-highest-priority org-lowest-priority))))
  11719. ((eq action 'up)
  11720. (setq new (1- current)))
  11721. ((eq action 'down)
  11722. (setq new (1+ current)))
  11723. (t (error "Invalid action")))
  11724. (setq new (min (max org-highest-priority (upcase new)) org-lowest-priority))
  11725. (setq news (format "%c" new))
  11726. (if have
  11727. (if remove
  11728. (replace-match "" t t nil 1)
  11729. (replace-match news t t nil 2))
  11730. (if remove
  11731. (error "No priority cookie found in line")
  11732. (looking-at org-todo-line-regexp)
  11733. (if (match-end 2)
  11734. (progn
  11735. (goto-char (match-end 2))
  11736. (insert " [#" news "]"))
  11737. (goto-char (match-beginning 3))
  11738. (insert "[#" news "] ")))))
  11739. (org-preserve-lc (org-set-tags nil 'align))
  11740. (if remove
  11741. (message "Priority removed")
  11742. (message "Priority of current item set to %s" news))))
  11743. (defun org-get-priority (s)
  11744. "Find priority cookie and return priority."
  11745. (save-match-data
  11746. (if (not (string-match org-priority-regexp s))
  11747. (* 1000 (- org-lowest-priority org-default-priority))
  11748. (* 1000 (- org-lowest-priority
  11749. (string-to-char (match-string 2 s)))))))
  11750. ;;;; Tags
  11751. (defun org-scan-tags (action matcher &optional todo-only)
  11752. "Scan headline tags with inheritance and produce output ACTION.
  11753. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  11754. evaluated, testing if a given set of tags qualifies a headline for
  11755. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  11756. are included in the output."
  11757. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  11758. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11759. (org-re
  11760. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  11761. (props (list 'face nil
  11762. 'done-face 'org-done
  11763. 'undone-face nil
  11764. 'mouse-face 'highlight
  11765. 'org-not-done-regexp org-not-done-regexp
  11766. 'org-todo-regexp org-todo-regexp
  11767. 'keymap org-agenda-keymap
  11768. 'help-echo
  11769. (format "mouse-2 or RET jump to org file %s"
  11770. (abbreviate-file-name buffer-file-name))))
  11771. (case-fold-search nil)
  11772. lspos
  11773. tags tags-list tags-alist (llast 0) rtn level category i txt
  11774. todo marker entry priority)
  11775. (save-excursion
  11776. (goto-char (point-min))
  11777. (when (eq action 'sparse-tree) (org-overview))
  11778. (while (re-search-forward re nil t)
  11779. (catch :skip
  11780. (setq todo (if (match-end 1) (match-string 2))
  11781. tags (if (match-end 4) (match-string 4)))
  11782. (goto-char (setq lspos (1+ (match-beginning 0))))
  11783. (setq level (org-reduced-level (funcall outline-level))
  11784. category (org-get-category))
  11785. (setq i llast llast level)
  11786. ;; remove tag lists from same and sublevels
  11787. (while (>= i level)
  11788. (when (setq entry (assoc i tags-alist))
  11789. (setq tags-alist (delete entry tags-alist)))
  11790. (setq i (1- i)))
  11791. ;; add the nex tags
  11792. (when tags
  11793. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  11794. tags-alist
  11795. (cons (cons level tags) tags-alist)))
  11796. ;; compile tags for current headline
  11797. (setq tags-list
  11798. (if org-use-tag-inheritance
  11799. (apply 'append (mapcar 'cdr tags-alist))
  11800. tags))
  11801. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  11802. (eval matcher)
  11803. (or (not org-agenda-skip-archived-trees)
  11804. (not (member org-archive-tag tags-list))))
  11805. (and (eq action 'agenda) (org-agenda-skip))
  11806. ;; list this headline
  11807. (if (eq action 'sparse-tree)
  11808. (progn
  11809. (org-show-context 'tags-tree))
  11810. (setq txt (org-format-agenda-item
  11811. ""
  11812. (concat
  11813. (if org-tags-match-list-sublevels
  11814. (make-string (1- level) ?.) "")
  11815. (org-get-heading))
  11816. category tags-list)
  11817. priority (org-get-priority txt))
  11818. (goto-char lspos)
  11819. (setq marker (org-agenda-new-marker))
  11820. (org-add-props txt props
  11821. 'org-marker marker 'org-hd-marker marker 'org-category category
  11822. 'priority priority 'type "tagsmatch")
  11823. (push txt rtn))
  11824. ;; if we are to skip sublevels, jump to end of subtree
  11825. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  11826. (when (and (eq action 'sparse-tree)
  11827. (not org-sparse-tree-open-archived-trees))
  11828. (org-hide-archived-subtrees (point-min) (point-max)))
  11829. (nreverse rtn)))
  11830. (defvar todo-only) ;; dynamically scoped
  11831. (defun org-tags-sparse-tree (&optional todo-only match)
  11832. "Create a sparse tree according to tags string MATCH.
  11833. MATCH can contain positive and negative selection of tags, like
  11834. \"+WORK+URGENT-WITHBOSS\".
  11835. If optional argument TODO_ONLY is non-nil, only select lines that are
  11836. also TODO lines."
  11837. (interactive "P")
  11838. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11839. (defvar org-cached-props nil)
  11840. (defun org-cached-entry-get (pom property)
  11841. (cdr (assoc property (or org-cached-props
  11842. (setq org-cached-props
  11843. (org-entry-properties pom))))))
  11844. (defun org-global-tags-completion-table (&optional files)
  11845. "Return the list of all tags in all agenda buffer/files."
  11846. (save-excursion
  11847. (org-uniquify
  11848. (apply 'append
  11849. (mapcar
  11850. (lambda (file)
  11851. (set-buffer (find-file-noselect file))
  11852. (org-get-buffer-tags))
  11853. (if (and files (car files))
  11854. files
  11855. (org-agenda-files)))))))
  11856. (defun org-make-tags-matcher (match)
  11857. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11858. ;; todo-only is scoped dynamically into this function, and the function
  11859. ;; may change it it the matcher asksk for it.
  11860. (unless match
  11861. ;; Get a new match request, with completion
  11862. (let ((org-last-tags-completion-table
  11863. (org-global-tags-completion-table)))
  11864. (setq match (completing-read
  11865. "Match: " 'org-tags-completion-function nil nil nil
  11866. 'org-tags-history))))
  11867. ;; Parse the string and create a lisp form
  11868. (let ((match0 match)
  11869. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  11870. minus tag mm
  11871. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11872. orterms term orlist re-p level-p prop-p pn pv)
  11873. (if (string-match "/+" match)
  11874. ;; match contains also a todo-matching request
  11875. (progn
  11876. (setq tagsmatch (substring match 0 (match-beginning 0))
  11877. todomatch (substring match (match-end 0)))
  11878. (if (string-match "^!" todomatch)
  11879. (setq todo-only t todomatch (substring todomatch 1)))
  11880. (if (string-match "^\\s-*$" todomatch)
  11881. (setq todomatch nil)))
  11882. ;; only matching tags
  11883. (setq tagsmatch match todomatch nil))
  11884. ;; Make the tags matcher
  11885. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11886. (setq tagsmatcher t)
  11887. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11888. (while (setq term (pop orterms))
  11889. (while (and (equal (substring term -1) "\\") orterms)
  11890. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11891. (while (string-match re term)
  11892. (setq minus (and (match-end 1)
  11893. (equal (match-string 1 term) "-"))
  11894. tag (match-string 2 term)
  11895. re-p (equal (string-to-char tag) ?{)
  11896. level-p (match-end 3)
  11897. prop-p (match-end 4)
  11898. mm (cond
  11899. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11900. (level-p `(= level ,(string-to-number
  11901. (match-string 3 term))))
  11902. (prop-p
  11903. (setq pn (match-string 4 term)
  11904. pv (match-string 5 term)
  11905. re-p (equal (string-to-char pv) ?{)
  11906. pv (substring pv 1 -1))
  11907. (if re-p
  11908. `(string-match ,pv (org-cached-entry-get nil ,pn))
  11909. `(equal ,pv (org-cached-entry-get nil ,pn))))
  11910. (t `(member ,(downcase tag) tags-list)))
  11911. mm (if minus (list 'not mm) mm)
  11912. term (substring term (match-end 0)))
  11913. (push mm tagsmatcher))
  11914. (push (if (> (length tagsmatcher) 1)
  11915. (cons 'and tagsmatcher)
  11916. (car tagsmatcher))
  11917. orlist)
  11918. (setq tagsmatcher nil))
  11919. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11920. (setq tagsmatcher
  11921. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11922. ;; Make the todo matcher
  11923. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11924. (setq todomatcher t)
  11925. (setq orterms (org-split-string todomatch "|") orlist nil)
  11926. (while (setq term (pop orterms))
  11927. (while (string-match re term)
  11928. (setq minus (and (match-end 1)
  11929. (equal (match-string 1 term) "-"))
  11930. kwd (match-string 2 term)
  11931. re-p (equal (string-to-char kwd) ?{)
  11932. term (substring term (match-end 0))
  11933. mm (if re-p
  11934. `(string-match ,(substring kwd 1 -1) todo)
  11935. (list 'equal 'todo kwd))
  11936. mm (if minus (list 'not mm) mm))
  11937. (push mm todomatcher))
  11938. (push (if (> (length todomatcher) 1)
  11939. (cons 'and todomatcher)
  11940. (car todomatcher))
  11941. orlist)
  11942. (setq todomatcher nil))
  11943. (setq todomatcher (if (> (length orlist) 1)
  11944. (cons 'or orlist) (car orlist))))
  11945. ;; Return the string and lisp forms of the matcher
  11946. (setq matcher (if todomatcher
  11947. (list 'and tagsmatcher todomatcher)
  11948. tagsmatcher))
  11949. (cons match0 matcher)))
  11950. (defun org-match-any-p (re list)
  11951. "Does re match any element of list?"
  11952. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11953. (delq nil list))
  11954. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  11955. (defvar org-tags-overlay (org-make-overlay 1 1))
  11956. (org-detach-overlay org-tags-overlay)
  11957. (defun org-set-tags (&optional arg just-align)
  11958. "Set the tags for the current headline.
  11959. With prefix ARG, realign all tags in headings in the current buffer."
  11960. (interactive "P")
  11961. (let* ((re (concat "^" outline-regexp))
  11962. (current (org-get-tags))
  11963. table current-tags inherited-tags ; computed below when needed
  11964. tags p0 c0 c1 rpl)
  11965. (if arg
  11966. (save-excursion
  11967. (goto-char (point-min))
  11968. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11969. (while (re-search-forward re nil t)
  11970. (org-set-tags nil t)
  11971. (end-of-line 1)))
  11972. (message "All tags realigned to column %d" org-tags-column))
  11973. (if just-align
  11974. (setq tags current)
  11975. ;; Get a new set of tags from the user
  11976. (save-excursion
  11977. (setq table (or org-tag-alist (org-get-buffer-tags))
  11978. org-last-tags-completion-table table
  11979. current-tags (org-split-string current ":")
  11980. inherited-tags (nreverse
  11981. (nthcdr (length current-tags)
  11982. (nreverse (org-get-tags-at))))
  11983. tags
  11984. (if (or (eq t org-use-fast-tag-selection)
  11985. (and org-use-fast-tag-selection
  11986. (delq nil (mapcar 'cdr table))))
  11987. (org-fast-tag-selection current-tags inherited-tags table)
  11988. (let ((org-add-colon-after-tag-completion t))
  11989. (org-trim
  11990. (completing-read "Tags: " 'org-tags-completion-function
  11991. nil nil current 'org-tags-history))))))
  11992. (while (string-match "[-+&]+" tags)
  11993. ;; No boolean logic, just a list
  11994. (setq tags (replace-match ":" t t tags))))
  11995. (if (string-match "\\`[\t ]*\\'" tags)
  11996. (setq tags "")
  11997. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11998. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11999. ;; Insert new tags at the correct column
  12000. (beginning-of-line 1)
  12001. (if (re-search-forward
  12002. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12003. (point-at-eol) t)
  12004. (progn
  12005. (if (equal tags "")
  12006. (setq rpl "")
  12007. (goto-char (match-beginning 0))
  12008. (setq c0 (current-column) p0 (point)
  12009. c1 (max (1+ c0) (if (> org-tags-column 0)
  12010. org-tags-column
  12011. (- (- org-tags-column) (length tags))))
  12012. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12013. (replace-match rpl t t)
  12014. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  12015. tags)
  12016. (error "Tags alignment failed")))))
  12017. (defun org-tags-completion-function (string predicate &optional flag)
  12018. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12019. (confirm (lambda (x) (stringp (car x)))))
  12020. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  12021. (setq s1 (match-string 1 string)
  12022. s2 (match-string 2 string))
  12023. (setq s1 "" s2 string))
  12024. (cond
  12025. ((eq flag nil)
  12026. ;; try completion
  12027. (setq rtn (try-completion s2 ctable confirm))
  12028. (if (stringp rtn)
  12029. (setq rtn
  12030. (concat s1 s2 (substring rtn (length s2))
  12031. (if (and org-add-colon-after-tag-completion
  12032. (assoc rtn ctable))
  12033. ":" ""))))
  12034. rtn)
  12035. ((eq flag t)
  12036. ;; all-completions
  12037. (all-completions s2 ctable confirm)
  12038. )
  12039. ((eq flag 'lambda)
  12040. ;; exact match?
  12041. (assoc s2 ctable)))
  12042. ))
  12043. (defun org-fast-tag-insert (kwd tags face &optional end)
  12044. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  12045. (insert (format "%-12s" (concat kwd ":"))
  12046. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12047. (or end "")))
  12048. (defun org-fast-tag-show-exit (flag)
  12049. (save-excursion
  12050. (goto-line 3)
  12051. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12052. (replace-match ""))
  12053. (when flag
  12054. (end-of-line 1)
  12055. (move-to-column (- (window-width) 19) t)
  12056. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12057. (defun org-set-current-tags-overlay (current prefix)
  12058. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12059. (if (featurep 'xemacs)
  12060. (org-overlay-display org-tags-overlay (concat prefix s)
  12061. 'secondary-selection)
  12062. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12063. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12064. (defun org-fast-tag-selection (current inherited table)
  12065. "Fast tag selection with single keys.
  12066. CURRENT is the current list of tags in the headline, INHERITED is the
  12067. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12068. possibly with grouping information.
  12069. If the keys are nil, a-z are automatically assigned.
  12070. Returns the new tags string, or nil to not change the current settings."
  12071. (let* ((maxlen (apply 'max (mapcar
  12072. (lambda (x)
  12073. (if (stringp (car x)) (string-width (car x)) 0))
  12074. table)))
  12075. (buf (current-buffer))
  12076. (expert (eq org-fast-tag-selection-single-key 'expert))
  12077. (buffer-tags nil)
  12078. (fwidth (+ maxlen 3 1 3))
  12079. (ncol (/ (- (window-width) 4) fwidth))
  12080. (i-face 'org-done)
  12081. (c-face 'org-todo)
  12082. tg cnt e c char c1 c2 ntable tbl rtn
  12083. ov-start ov-end ov-prefix
  12084. (exit-after-next org-fast-tag-selection-single-key)
  12085. groups ingroup)
  12086. (save-excursion
  12087. (beginning-of-line 1)
  12088. (if (looking-at
  12089. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12090. (setq ov-start (match-beginning 1)
  12091. ov-end (match-end 1)
  12092. ov-prefix "")
  12093. (setq ov-start (1- (point-at-eol))
  12094. ov-end (1+ ov-start))
  12095. (skip-chars-forward "^\n\r")
  12096. (setq ov-prefix
  12097. (concat
  12098. (buffer-substring (1- (point)) (point))
  12099. (if (> (current-column) org-tags-column)
  12100. " "
  12101. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12102. (org-move-overlay org-tags-overlay ov-start ov-end)
  12103. (save-window-excursion
  12104. (if expert
  12105. (set-buffer (get-buffer-create " *Org tags*"))
  12106. (delete-other-windows)
  12107. (split-window-vertically)
  12108. (switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12109. (erase-buffer)
  12110. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12111. (org-fast-tag-insert "Current" current c-face "\n\n")
  12112. (org-fast-tag-show-exit exit-after-next)
  12113. (org-set-current-tags-overlay current ov-prefix)
  12114. (setq tbl table char ?a cnt 0)
  12115. (while (setq e (pop tbl))
  12116. (cond
  12117. ((equal e '(:startgroup))
  12118. (push '() groups) (setq ingroup t)
  12119. (when (not (= cnt 0))
  12120. (setq cnt 0)
  12121. (insert "\n"))
  12122. (insert "{ "))
  12123. ((equal e '(:endgroup))
  12124. (setq ingroup nil cnt 0)
  12125. (insert "}\n"))
  12126. (t
  12127. (setq tg (car e) c2 nil)
  12128. (if (cdr e)
  12129. (setq c (cdr e))
  12130. ;; automatically assign a character.
  12131. (setq c1 (string-to-char
  12132. (downcase (substring
  12133. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12134. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12135. (while (or (rassoc char ntable) (rassoc char table))
  12136. (setq char (1+ char)))
  12137. (setq c2 c1))
  12138. (setq c (or c2 char)))
  12139. (if ingroup (push tg (car groups)))
  12140. (setq tg (org-add-props tg nil 'face
  12141. (cond
  12142. ((member tg current) c-face)
  12143. ((member tg inherited) i-face)
  12144. (t nil))))
  12145. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12146. (insert "[" c "] " tg (make-string
  12147. (- fwidth 4 (length tg)) ?\ ))
  12148. (push (cons tg c) ntable)
  12149. (when (= (setq cnt (1+ cnt)) ncol)
  12150. (insert "\n")
  12151. (if ingroup (insert " "))
  12152. (setq cnt 0)))))
  12153. (setq ntable (nreverse ntable))
  12154. (insert "\n")
  12155. (goto-char (point-min))
  12156. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12157. (fit-window-to-buffer))
  12158. (setq rtn
  12159. (catch 'exit
  12160. (while t
  12161. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  12162. (if groups " [!] no groups" " [!]groups")
  12163. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12164. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12165. (cond
  12166. ((= c ?\r) (throw 'exit t))
  12167. ((= c ?!)
  12168. (setq groups (not groups))
  12169. (goto-char (point-min))
  12170. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12171. ((= c ?\C-c)
  12172. (if (not expert)
  12173. (org-fast-tag-show-exit
  12174. (setq exit-after-next (not exit-after-next)))
  12175. (setq expert nil)
  12176. (delete-other-windows)
  12177. (split-window-vertically)
  12178. (switch-to-buffer-other-window " *Org tags*")
  12179. (and (fboundp 'fit-window-to-buffer)
  12180. (fit-window-to-buffer))))
  12181. ((or (= c ?\C-g)
  12182. (and (= c ?q) (not (rassoc c ntable))))
  12183. (org-detach-overlay org-tags-overlay)
  12184. (setq quit-flag t))
  12185. ((= c ?\ )
  12186. (setq current nil)
  12187. (if exit-after-next (setq exit-after-next 'now)))
  12188. ((= c ?\t)
  12189. (condition-case nil
  12190. (setq tg (completing-read
  12191. "Tag: "
  12192. (or buffer-tags
  12193. (with-current-buffer buf
  12194. (org-get-buffer-tags)))))
  12195. (quit (setq tg "")))
  12196. (when (string-match "\\S-" tg)
  12197. (add-to-list 'buffer-tags (list tg))
  12198. (if (member tg current)
  12199. (setq current (delete tg current))
  12200. (push tg current)))
  12201. (if exit-after-next (setq exit-after-next 'now)))
  12202. ((setq e (rassoc c ntable) tg (car e))
  12203. (if (member tg current)
  12204. (setq current (delete tg current))
  12205. (loop for g in groups do
  12206. (if (member tg g)
  12207. (mapcar (lambda (x)
  12208. (setq current (delete x current)))
  12209. g)))
  12210. (push tg current))
  12211. (if exit-after-next (setq exit-after-next 'now))))
  12212. ;; Create a sorted list
  12213. (setq current
  12214. (sort current
  12215. (lambda (a b)
  12216. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12217. (if (eq exit-after-next 'now) (throw 'exit t))
  12218. (goto-char (point-min))
  12219. (beginning-of-line 2)
  12220. (delete-region (point) (point-at-eol))
  12221. (org-fast-tag-insert "Current" current c-face)
  12222. (org-set-current-tags-overlay current ov-prefix)
  12223. (while (re-search-forward
  12224. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  12225. (setq tg (match-string 1))
  12226. (add-text-properties (match-beginning 1) (match-end 1)
  12227. (list 'face
  12228. (cond
  12229. ((member tg current) c-face)
  12230. ((member tg inherited) i-face)
  12231. (t nil)))))
  12232. (goto-char (point-min)))))
  12233. (org-detach-overlay org-tags-overlay)
  12234. (if rtn
  12235. (mapconcat 'identity current ":")
  12236. nil))))
  12237. (defun org-get-tags ()
  12238. "Get the TAGS string in the current headline."
  12239. (unless (org-on-heading-p t)
  12240. (error "Not on a heading"))
  12241. (save-excursion
  12242. (beginning-of-line 1)
  12243. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12244. (org-match-string-no-properties 1)
  12245. "")))
  12246. (defun org-get-buffer-tags ()
  12247. "Get a table of all tags used in the buffer, for completion."
  12248. (let (tags)
  12249. (save-excursion
  12250. (goto-char (point-min))
  12251. (while (re-search-forward
  12252. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  12253. (when (equal (char-after (point-at-bol 0)) ?*)
  12254. (mapc (lambda (x) (add-to-list 'tags x))
  12255. (org-split-string (org-match-string-no-properties 1) ":")))))
  12256. (mapcar 'list tags)))
  12257. ;;;; Properties
  12258. ;;; Setting and retrieving properties
  12259. (defconst org-special-properties
  12260. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED"
  12261. "CLOCK" "PRIORITY")
  12262. "The special properties valid in Org-mode.
  12263. These are properties that are not defined in the property drawer,
  12264. but in some other way.")
  12265. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12266. "Regular expression matching the first line of a property drawer.")
  12267. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12268. "Regular expression matching the first line of a property drawer.")
  12269. (defun org-property-action ()
  12270. "Do an action on properties."
  12271. (interactive)
  12272. (let (c prop)
  12273. (org-at-property-p)
  12274. (setq prop (match-string 2))
  12275. (message "Property Action: [s]et [d]elete [D]delete globally")
  12276. (setq c (read-char-exclusive))
  12277. (cond
  12278. ((equal c ?s)
  12279. (call-interactively 'org-set-property))
  12280. ((equal c ?d)
  12281. (call-interactively 'org-delete-property))
  12282. ((equal c ?D)
  12283. (call-interactively 'org-delete-property-globally))
  12284. (t (error "No such property action %c" c)))))
  12285. (defun org-at-property-p ()
  12286. "Is the cursor in a property line?"
  12287. ;; FIXME: Does not check if we are actually in the drawer.
  12288. ;; FIXME: also returns true on any drawers.....
  12289. ;; This is used by C-c C-c for property action.
  12290. (save-excursion
  12291. (beginning-of-line 1)
  12292. (looking-at "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(.*\\)")))
  12293. (defmacro org-with-point-at (pom &rest body)
  12294. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  12295. (declare (indent 1) (debug t))
  12296. `(save-excursion
  12297. (if (markerp pom) (set-buffer (marker-buffer pom)))
  12298. (save-excursion
  12299. (goto-char (or pom (point)))
  12300. ,@body)))
  12301. (defun org-get-property-block (&optional beg end force)
  12302. "Return the (beg . end) range of the body of the property drawer.
  12303. BEG and END can be beginning and end of subtree, if not given
  12304. they will be found.
  12305. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12306. (catch 'exit
  12307. (save-excursion
  12308. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12309. (end (or end (progn (outline-next-heading) (point)))))
  12310. (goto-char beg)
  12311. (if (re-search-forward org-property-start-re end t)
  12312. (setq beg (1+ (match-end 0)))
  12313. (if force
  12314. (save-excursion
  12315. (org-insert-property-drawer)
  12316. (setq end (progn (outline-next-heading) (point))))
  12317. (throw 'exit nil))
  12318. (goto-char beg)
  12319. (if (re-search-forward org-property-start-re end t)
  12320. (setq beg (1+ (match-end 0)))))
  12321. (if (re-search-forward org-property-end-re end t)
  12322. (setq end (match-beginning 0))
  12323. (or force (throw 'exit nil))
  12324. (goto-char beg)
  12325. (setq end beg)
  12326. (org-indent-line-function)
  12327. (insert ":END:\n"))
  12328. (cons beg end)))))
  12329. (defun org-entry-properties (&optional pom which)
  12330. "Get all properties of the entry at point-or-marker POM.
  12331. This includes the TODO keyword, the tags, time strings for deadline,
  12332. scheduled, and clocking, and any additional properties defined in the
  12333. entry. The return value is an alist, keys may occur multiple times
  12334. if the property key was used several times.
  12335. POM may also be nil, in which case the current entry is used.
  12336. If WHICH is nil or `all', get all properties. If WHICH is
  12337. `special' or `standard', only get that subclass."
  12338. (setq which (or which 'all))
  12339. (org-with-point-at pom
  12340. (let ((clockstr (substring org-clock-string 0 -1))
  12341. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  12342. beg end range props sum-props key value)
  12343. (save-excursion
  12344. (when (condition-case nil (org-back-to-heading t) (error nil))
  12345. (setq beg (point))
  12346. (setq sum-props (get-text-property (point) 'org-summaries))
  12347. (outline-next-heading)
  12348. (setq end (point))
  12349. (when (memq which '(all special))
  12350. ;; Get the special properties, like TODO and tags
  12351. (goto-char beg)
  12352. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  12353. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12354. (when (looking-at org-priority-regexp)
  12355. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12356. (when (and (setq value (org-get-tags)) (string-match "\\S-" value))
  12357. (push (cons "TAGS" value) props))
  12358. (when (setq value (org-get-tags-at))
  12359. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  12360. props))
  12361. (while (re-search-forward org-keyword-time-regexp end t)
  12362. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  12363. (unless (member key excluded) (push key excluded))
  12364. (push (cons key
  12365. (if (equal key clockstr)
  12366. (org-no-properties
  12367. (org-trim
  12368. (buffer-substring
  12369. (match-beginning 2) (point-at-eol))))
  12370. (org-match-string-no-properties 2)))
  12371. props)))
  12372. (when (memq which '(all standard))
  12373. ;; Get the standard properties, like :PORP: ...
  12374. (setq range (org-get-property-block beg end))
  12375. (when range
  12376. (goto-char (car range))
  12377. (while (re-search-forward
  12378. "^[ \t]*:\\([a-zA-Z][a-zA-Z_0-9]*\\):[ \t]*\\(\\S-.*\\)?"
  12379. (cdr range) t)
  12380. (setq key (org-match-string-no-properties 1)
  12381. value (org-trim (or (org-match-string-no-properties 2) "")))
  12382. (unless (member key excluded)
  12383. (push (cons key (or value "")) props)))))
  12384. (append sum-props (nreverse props)))))))
  12385. (defun org-entry-get (pom property &optional inherit)
  12386. "Get value of PROPERTY for entry at point-or-marker POM.
  12387. If INHERIT is non-nil and the entry does not have the property,
  12388. then also check higher levels of the hierarchy.
  12389. If the property is present but empty, the return value is the empty string.
  12390. If the property is not present at all, nil is returned."
  12391. (org-with-point-at pom
  12392. (if inherit
  12393. (org-entry-get-with-inheritance property)
  12394. (if (member property org-special-properties)
  12395. ;; We need a special property. Use brute force, get all properties.
  12396. (cdr (assoc property (org-entry-properties nil 'special)))
  12397. (let ((range (org-get-property-block)))
  12398. (if (and range
  12399. (goto-char (car range))
  12400. (re-search-forward
  12401. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  12402. (cdr range) t))
  12403. ;; Found the property, return it.
  12404. (if (match-end 1)
  12405. (org-match-string-no-properties 1)
  12406. "")))))))
  12407. (defun org-entry-delete (pom property)
  12408. "Delete the property PROPERTY from entry at point-or-marker POM."
  12409. (org-with-point-at pom
  12410. (if (member property org-special-properties)
  12411. nil ; cannot delete these properties.
  12412. (let ((range (org-get-property-block)))
  12413. (if (and range
  12414. (goto-char (car range))
  12415. (re-search-forward
  12416. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  12417. (cdr range) t))
  12418. (progn
  12419. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12420. t)
  12421. nil)))))
  12422. (defvar org-entry-property-inherited-from (make-marker))
  12423. (defun org-entry-get-with-inheritance (property)
  12424. "Get entry property, and search higher levels if not present."
  12425. (let (tmp)
  12426. (save-excursion
  12427. (catch 'ex
  12428. (while t
  12429. (when (setq tmp (org-entry-get nil property))
  12430. (org-back-to-heading t)
  12431. (move-marker org-entry-property-inherited-from (point))
  12432. (throw 'ex tmp))
  12433. (condition-case nil
  12434. (org-up-heading-all 1)
  12435. (error (throw 'ex nil))))))))
  12436. (defun org-entry-put (pom property value)
  12437. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12438. (org-with-point-at pom
  12439. (org-back-to-heading t)
  12440. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12441. range)
  12442. (cond
  12443. ((equal property "TODO")
  12444. (when (and (stringp value) (string-match "\\S-" value)
  12445. (not (member value org-todo-keywords-1)))
  12446. (error "\"%s\" is not a valid TODO state" value))
  12447. (if (or (not value)
  12448. (not (string-match "\\S-" value)))
  12449. (setq value 'none))
  12450. (org-todo value)
  12451. (org-set-tags nil 'align))
  12452. ((equal property "PRIORITY")
  12453. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12454. (string-to-char value) ?\ ))
  12455. (org-set-tags nil 'align))
  12456. ((member property org-special-properties)
  12457. (error "The %s property can not yet be set with `org-entry-put'"
  12458. property))
  12459. (t ; a non-special property
  12460. (setq range (org-get-property-block beg end 'force))
  12461. (goto-char (car range))
  12462. (if (re-search-forward
  12463. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12464. (progn
  12465. (delete-region (match-beginning 1) (match-end 1))
  12466. (goto-char (match-beginning 1)))
  12467. (goto-char (cdr range))
  12468. (insert "\n")
  12469. (backward-char 1)
  12470. (org-indent-line-function)
  12471. (insert ":" property ":"))
  12472. (and value (insert " " value))
  12473. (org-indent-line-function))))))
  12474. (defun org-buffer-property-keys (&optional include-specials)
  12475. "Get all property keys in the current buffer."
  12476. (let (rtn range)
  12477. (save-excursion
  12478. (save-restriction
  12479. (widen)
  12480. (goto-char (point-min))
  12481. (while (re-search-forward org-property-start-re nil t)
  12482. (setq range (org-get-property-block))
  12483. (goto-char (car range))
  12484. (while (re-search-forward "^[ \t]*:\\([a-zA-Z0-9]+\\):" (cdr range) t)
  12485. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12486. (outline-next-heading))))
  12487. (when include-specials
  12488. (setq rtn (append org-special-properties rtn)))
  12489. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12490. (defun org-insert-property-drawer ()
  12491. "Insert a property drawer into the current entry."
  12492. (interactive)
  12493. (org-back-to-heading t)
  12494. (let ((beg (point))
  12495. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12496. end hiddenp)
  12497. (outline-next-heading)
  12498. (setq end (point))
  12499. (goto-char beg)
  12500. (while (re-search-forward re end t))
  12501. (setq hiddenp (org-invisible-p))
  12502. (end-of-line 1)
  12503. (insert "\n:PROPERTIES:\n:END:")
  12504. (beginning-of-line 0)
  12505. (org-indent-line-function)
  12506. (beginning-of-line 2)
  12507. (org-indent-line-function)
  12508. (beginning-of-line 0)
  12509. (if hiddenp
  12510. (save-excursion
  12511. (org-back-to-heading t)
  12512. (hide-entry))
  12513. (org-flag-drawer t))))
  12514. (defun org-set-property (property value)
  12515. "In the current entry, set PROPERTY to VALUE."
  12516. (interactive
  12517. (let* ((prop (completing-read "Property: "
  12518. (mapcar 'list (org-buffer-property-keys))))
  12519. (cur (org-entry-get nil prop))
  12520. (allowed (org-property-get-allowed-values nil prop 'table))
  12521. (val (if allowed
  12522. (completing-read "Value: " allowed nil 'req-match)
  12523. (read-string
  12524. (concat "Value" (if (and cur (string-match "\\S-" cur))
  12525. (concat "[" cur "]") "")
  12526. ": ")
  12527. "" cur))))
  12528. (list prop (if (equal val "") cur val))))
  12529. (unless (equal (org-entry-get nil property) value)
  12530. (org-entry-put nil property value)))
  12531. (defun org-delete-property (property)
  12532. "In the current entry, delete PROPERTY."
  12533. (interactive
  12534. (let* ((prop (completing-read
  12535. "Property: " (org-entry-properties nil 'standard))))
  12536. (list prop)))
  12537. (message (concat "Property " property
  12538. (if (org-entry-delete nil property)
  12539. " deleted"
  12540. " was not present in the entry"))))
  12541. (defun org-delete-property-globally (property)
  12542. "Remove PROPERTY globally, from all entries."
  12543. (interactive
  12544. (let* ((prop (completing-read
  12545. "Globally remove property: "
  12546. (mapcar 'list (org-buffer-property-keys)))))
  12547. (list prop)))
  12548. (save-excursion
  12549. (save-restriction
  12550. (widen)
  12551. (goto-char (point-min))
  12552. (let ((cnt 0))
  12553. (while (re-search-forward
  12554. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12555. nil t)
  12556. (setq cnt (1+ cnt))
  12557. (replace-match ""))
  12558. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12559. (defun org-property-get-allowed-values (pom property &optional table)
  12560. "Get allowed values for the property PROPERTY.
  12561. When TABLE is non-nil, return an alist that can directly be used for
  12562. completion."
  12563. (let (vals)
  12564. (cond
  12565. ((equal property "TODO")
  12566. (setq vals (org-with-point-at pom
  12567. (append org-todo-keywords-1 '("")))))
  12568. ((equal property "PRIORITY")
  12569. (let ((n org-lowest-priority))
  12570. (while (>= n org-highest-priority)
  12571. (push (char-to-string n) vals)
  12572. (setq n (1- n)))))
  12573. ((member property org-special-properties))
  12574. (t
  12575. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12576. (when (and vals (string-match "\\S-" vals))
  12577. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12578. (setq vals (mapcar (lambda (x)
  12579. (cond ((stringp x) x)
  12580. ((numberp x) (number-to-string x))
  12581. ((symbolp x) (symbol-name x))
  12582. (t "???")))
  12583. vals)))))
  12584. (if table (mapcar 'list vals) vals)))
  12585. ;;; Column View
  12586. (defvar org-columns-overlays nil
  12587. "Holds the list of current column overlays.")
  12588. (defvar org-columns-current-fmt nil
  12589. "Local variable, holds the currently active column format.")
  12590. (defvar org-columns-current-fmt-compiled nil
  12591. "Local variable, holds the currently active column format.
  12592. This is the compiled version of the format.")
  12593. (defvar org-columns-current-maxwidths nil
  12594. "Loval variable, holds the currently active maximum column widths.")
  12595. (defvar org-columns-begin-marker (make-marker)
  12596. "Points to the position where last a column creation command was called.")
  12597. (defvar org-columns-top-level-marker (make-marker)
  12598. "Points to the position where current columns region starts.")
  12599. (defvar org-columns-map (make-sparse-keymap)
  12600. "The keymap valid in column display.")
  12601. (defun org-columns-content ()
  12602. "Switch to contents view while in columns view."
  12603. (interactive)
  12604. (org-overview)
  12605. (org-content))
  12606. (org-defkey org-columns-map "c" 'org-columns-content)
  12607. (org-defkey org-columns-map "o" 'org-overview)
  12608. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  12609. (org-defkey org-columns-map "v" 'org-columns-show-value)
  12610. (org-defkey org-columns-map "q" 'org-columns-quit)
  12611. (org-defkey org-columns-map "r" 'org-columns-redo)
  12612. (org-defkey org-columns-map [left] 'backward-char)
  12613. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  12614. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  12615. (org-defkey org-columns-map [right] 'forward-char)
  12616. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  12617. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-next-allowed-value)
  12618. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  12619. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  12620. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  12621. (org-defkey org-columns-map "<" 'org-columns-narrow)
  12622. (org-defkey org-columns-map ">" 'org-columns-widen)
  12623. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  12624. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  12625. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  12626. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  12627. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  12628. '("Column"
  12629. ["Edit property" org-columns-edit-value t]
  12630. ["Next allowed value" org-columns-next-allowed-value t]
  12631. ["Previous allowed value" org-columns-previous-allowed-value t]
  12632. ["Show full value" org-columns-show-value t]
  12633. ["Edit allowed" org-columns-edit-allowed t]
  12634. "--"
  12635. ["Edit column attributes" org-columns-edit-attributes t]
  12636. ["Increase column width" org-columns-widen t]
  12637. ["Decrease column width" org-columns-narrow t]
  12638. "--"
  12639. ["Move column right" org-columns-move-right t]
  12640. ["Move column left" org-columns-move-left t]
  12641. ["Add column" org-columns-new t]
  12642. ["Delete column" org-columns-delete t]
  12643. "--"
  12644. ["CONTENTS" org-columns-content t]
  12645. ["OVERVIEW" org-overview t]
  12646. ["Refresh columns display" org-columns-redo t]
  12647. "--"
  12648. ["Quit" org-columns-quit t]))
  12649. (defun org-columns-new-overlay (beg end &optional string face)
  12650. "Create a new column overlay and add it to the list."
  12651. (let ((ov (org-make-overlay beg end)))
  12652. (org-overlay-put ov 'face (or face 'secondary-selection))
  12653. (org-overlay-display ov string face)
  12654. (push ov org-columns-overlays)
  12655. ov))
  12656. (defun org-columns-display-here (&optional props)
  12657. "Overlay the current line with column display."
  12658. (interactive)
  12659. (let* ((fmt org-columns-current-fmt-compiled)
  12660. (beg (point-at-bol))
  12661. (color (list :foreground
  12662. (face-attribute
  12663. (or (get-text-property beg 'face) 'default)
  12664. :foreground)))
  12665. props pom property ass width f string ov column)
  12666. ;; Check if the entry is in another buffer.
  12667. (unless props
  12668. (if (eq major-mode 'org-agenda-mode)
  12669. (setq pom (or (get-text-property (point) 'org-hd-marker)
  12670. (get-text-property (point) 'org-marker))
  12671. props (if pom (org-entry-properties pom) nil))
  12672. (setq props (org-entry-properties nil))))
  12673. ;; Walk the format
  12674. (while (setq column (pop fmt))
  12675. (setq property (car column)
  12676. ass (if (equal property "ITEM")
  12677. (cons "ITEM"
  12678. (save-match-data
  12679. (org-no-properties
  12680. (org-remove-tabs
  12681. (buffer-substring-no-properties
  12682. (point-at-bol) (point-at-eol))))))
  12683. (assoc property props))
  12684. width (or (cdr (assoc property org-columns-current-maxwidths))
  12685. (nth 2 column))
  12686. f (format "%%-%d.%ds | " width width)
  12687. string (format f (or (cdr ass) "")))
  12688. ;; Create the overlay
  12689. (org-unmodified
  12690. (setq ov (org-columns-new-overlay
  12691. beg (setq beg (1+ beg)) string
  12692. (list color 'org-column)))
  12693. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  12694. (org-overlay-put ov 'keymap org-columns-map)
  12695. (org-overlay-put ov 'org-columns-key property)
  12696. (org-overlay-put ov 'org-columns-value (cdr ass))
  12697. (org-overlay-put ov 'org-columns-pom pom)
  12698. (org-overlay-put ov 'org-columns-format f))
  12699. (if (or (not (char-after beg))
  12700. (equal (char-after beg) ?\n))
  12701. (let ((inhibit-read-only t))
  12702. (save-excursion
  12703. (goto-char beg)
  12704. (insert " ")))))
  12705. ;; Make the rest of the line disappear.
  12706. (org-unmodified
  12707. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  12708. (org-overlay-put ov 'invisible t)
  12709. (org-overlay-put ov 'keymap org-columns-map)
  12710. (push ov org-columns-overlays)
  12711. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  12712. (org-overlay-put ov 'keymap org-columns-map)
  12713. (push ov org-columns-overlays)
  12714. (let ((inhibit-read-only t))
  12715. (put-text-property (1- (point-at-bol))
  12716. (min (point-max) (1+ (point-at-eol)))
  12717. 'read-only "Type `e' to edit property")))))
  12718. (defvar org-previous-header-line-format nil
  12719. "The header line format before column view was turned on.")
  12720. (defvar org-columns-inhibit-recalculation nil
  12721. "Inhibit recomputing of columns on column view startup.")
  12722. (defvar header-line-format)
  12723. (defun org-columns-display-here-title ()
  12724. "Overlay the newline before the current line with the table title."
  12725. (interactive)
  12726. (let ((fmt org-columns-current-fmt-compiled)
  12727. string (title "")
  12728. property width f column str)
  12729. (while (setq column (pop fmt))
  12730. (setq property (car column)
  12731. str (or (nth 1 column) property)
  12732. width (or (cdr (assoc property org-columns-current-maxwidths))
  12733. (nth 2 column))
  12734. f (format "%%-%d.%ds | " width width)
  12735. string (format f str)
  12736. title (concat title string)))
  12737. (setq title (concat
  12738. (org-add-props " " nil 'display '(space :align-to 0))
  12739. (org-add-props title nil 'face '(:weight bold :underline t))))
  12740. (org-set-local 'org-previous-header-line-format header-line-format)
  12741. (setq header-line-format title)))
  12742. (defun org-columns-remove-overlays ()
  12743. "Remove all currently active column overlays."
  12744. (interactive)
  12745. (when (marker-buffer org-columns-begin-marker)
  12746. (with-current-buffer (marker-buffer org-columns-begin-marker)
  12747. (when (local-variable-p 'org-previous-header-line-format)
  12748. (setq header-line-format org-previous-header-line-format)
  12749. (kill-local-variable 'org-previous-header-line-format))
  12750. (move-marker org-columns-begin-marker nil)
  12751. (move-marker org-columns-top-level-marker nil)
  12752. (org-unmodified
  12753. (mapc 'org-delete-overlay org-columns-overlays)
  12754. (setq org-columns-overlays nil)
  12755. (let ((inhibit-read-only t))
  12756. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  12757. (defun org-columns-show-value ()
  12758. "Show the full value of the property."
  12759. (interactive)
  12760. (let ((value (get-char-property (point) 'org-columns-value)))
  12761. (message "Value is: %s" (or value ""))))
  12762. (defun org-columns-quit ()
  12763. "Remove the column overlays and in this way exit column editing."
  12764. (interactive)
  12765. (org-unmodified
  12766. (org-columns-remove-overlays)
  12767. (let ((inhibit-read-only t))
  12768. ;; FIXME: is this safe???
  12769. ;; or are there other reasons why there may be a read-only property????
  12770. (remove-text-properties (point-min) (point-max) '(read-only t))))
  12771. (when (eq major-mode 'org-agenda-mode)
  12772. (message "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  12773. (defun org-columns-edit-value ()
  12774. "Edit the value of the property at point in column view.
  12775. Where possible, use the standard interface for changing this line."
  12776. (interactive)
  12777. (let* ((col (current-column))
  12778. (key (get-char-property (point) 'org-columns-key))
  12779. (value (get-char-property (point) 'org-columns-value))
  12780. (bol (point-at-bol)) (eol (point-at-eol))
  12781. (pom (or (get-text-property bol 'org-hd-marker)
  12782. (point))) ; keep despite of compiler waring
  12783. (line-overlays
  12784. (delq nil (mapcar (lambda (x)
  12785. (and (eq (overlay-buffer x) (current-buffer))
  12786. (>= (overlay-start x) bol)
  12787. (<= (overlay-start x) eol)
  12788. x))
  12789. org-columns-overlays)))
  12790. nval eval allowed)
  12791. (when (equal key "ITEM")
  12792. (error "Cannot edit item headline from here"))
  12793. (cond
  12794. ((equal key "TODO")
  12795. (setq eval '(org-with-point-at pom
  12796. (let ((current-prefix-arg '(4))) (org-todo '(4))))))
  12797. ((equal key "PRIORITY")
  12798. (setq eval '(org-with-point-at pom
  12799. (call-interactively 'org-priority))))
  12800. ((equal key "TAGS")
  12801. (setq eval '(org-with-point-at pom
  12802. (let ((org-fast-tag-selection-single-key
  12803. (if (eq org-fast-tag-selection-single-key 'expert)
  12804. t org-fast-tag-selection-single-key)))
  12805. (call-interactively 'org-set-tags)))))
  12806. ((equal key "DEADLINE")
  12807. (setq eval '(org-with-point-at pom
  12808. (call-interactively 'org-deadline))))
  12809. ((equal key "SCHEDULED")
  12810. (setq eval '(org-with-point-at pom
  12811. (call-interactively 'org-deadline))))
  12812. (t
  12813. (setq allowed (org-property-get-allowed-values pom key 'table))
  12814. (if allowed
  12815. (setq nval (completing-read "Value: " allowed nil t))
  12816. (setq nval (read-string "Edit: " value)))
  12817. (setq nval (org-trim nval))
  12818. (when (not (equal nval value))
  12819. (setq eval '(org-entry-put pom key nval)))))
  12820. (when eval
  12821. (let ((inhibit-read-only t))
  12822. (remove-text-properties (1- bol) eol '(read-only t))
  12823. (unwind-protect
  12824. (progn
  12825. (setq org-columns-overlays
  12826. (org-delete-all line-overlays org-columns-overlays))
  12827. (mapc 'org-delete-overlay line-overlays)
  12828. (org-columns-eval eval))
  12829. (org-columns-display-here))))
  12830. (move-to-column col)
  12831. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  12832. (org-columns-update key))))
  12833. (defun org-columns-edit-allowed ()
  12834. "Edit the list of allowed values for the current property."
  12835. (interactive)
  12836. (let* ((col (current-column))
  12837. (key (get-char-property (point) 'org-columns-key))
  12838. (key1 (concat key "_ALL"))
  12839. (value (get-char-property (point) 'org-columns-value))
  12840. (allowed (org-entry-get (point) key1 t))
  12841. nval)
  12842. (setq nval (read-string "Allowed: " allowed))
  12843. (org-entry-put
  12844. (cond ((marker-position org-entry-property-inherited-from)
  12845. org-entry-property-inherited-from)
  12846. ((marker-position org-columns-top-level-marker)
  12847. org-columns-top-level-marker))
  12848. key1 nval)))
  12849. (defun org-columns-eval (form)
  12850. (let (hidep)
  12851. (save-excursion
  12852. (beginning-of-line 1)
  12853. (next-line 1)
  12854. (setq hidep (org-on-heading-p 1)))
  12855. (eval form)
  12856. (and hidep (hide-entry))))
  12857. (defun org-columns-previous-allowed-value ()
  12858. "Switch to the previous allowed value for this column."
  12859. (interactive)
  12860. (org-columns-next-allowed-value t))
  12861. (defun org-columns-next-allowed-value (&optional previous)
  12862. "Switch to the next allowed value for this column."
  12863. (interactive)
  12864. (let* ((col (current-column))
  12865. (key (get-char-property (point) 'org-columns-key))
  12866. (value (get-char-property (point) 'org-columns-value))
  12867. (bol (point-at-bol)) (eol (point-at-eol))
  12868. (pom (or (get-text-property bol 'org-hd-marker)
  12869. (point))) ; keep despite of compiler waring
  12870. (line-overlays
  12871. (delq nil (mapcar (lambda (x)
  12872. (and (eq (overlay-buffer x) (current-buffer))
  12873. (>= (overlay-start x) bol)
  12874. (<= (overlay-start x) eol)
  12875. x))
  12876. org-columns-overlays)))
  12877. (allowed (or (org-property-get-allowed-values pom key)
  12878. (and (equal
  12879. (nth 4 (assoc key org-columns-current-fmt-compiled))
  12880. 'checkbox) '("[ ]" "[X]"))))
  12881. nval)
  12882. (when (equal key "ITEM")
  12883. (error "Cannot edit item headline from here"))
  12884. (unless allowed
  12885. (error "Allowed values for this property have not been defined"))
  12886. (if previous (setq allowed (reverse allowed)))
  12887. (if (member value allowed)
  12888. (setq nval (car (cdr (member value allowed)))))
  12889. (setq nval (or nval (car allowed)))
  12890. (if (equal nval value)
  12891. (error "Only one allowed value for this property"))
  12892. (let ((inhibit-read-only t))
  12893. (remove-text-properties (1- bol) eol '(read-only t))
  12894. (unwind-protect
  12895. (progn
  12896. (setq org-columns-overlays
  12897. (org-delete-all line-overlays org-columns-overlays))
  12898. (mapc 'org-delete-overlay line-overlays)
  12899. (org-columns-eval '(org-entry-put pom key nval)))
  12900. (org-columns-display-here)))
  12901. (move-to-column col)
  12902. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  12903. (org-columns-update key))))
  12904. (defun org-verify-version (task)
  12905. (cond
  12906. ((eq task 'columns)
  12907. (if (or (featurep 'xemacs)
  12908. (< emacs-major-version 22))
  12909. (error "Emacs 22 is required for the columns feature")))))
  12910. (defun org-columns ()
  12911. "Turn on column view on an org-mode file."
  12912. (interactive)
  12913. (org-verify-version 'columns)
  12914. (org-columns-remove-overlays)
  12915. (move-marker org-columns-begin-marker (point))
  12916. (let (beg end fmt cache maxwidths)
  12917. (when (condition-case nil (org-back-to-heading) (error nil))
  12918. (move-marker org-entry-property-inherited-from nil)
  12919. (setq fmt (org-entry-get nil "COLUMNS" t)))
  12920. (setq fmt (or fmt org-columns-default-format))
  12921. (org-set-local 'org-columns-current-fmt fmt)
  12922. (org-columns-compile-format fmt)
  12923. (save-excursion
  12924. (if (marker-position org-entry-property-inherited-from)
  12925. (goto-char org-entry-property-inherited-from))
  12926. (setq beg (point))
  12927. (move-marker org-columns-top-level-marker (point))
  12928. (unless org-columns-inhibit-recalculation
  12929. (org-columns-compute-all))
  12930. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  12931. (point-max)))
  12932. (goto-char beg)
  12933. ;; Get and cache the properties
  12934. (while (re-search-forward (concat "^" outline-regexp) end t)
  12935. (push (cons (org-current-line) (org-entry-properties)) cache))
  12936. (when cache
  12937. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  12938. (org-set-local 'org-columns-current-maxwidths maxwidths)
  12939. (goto-line (car (org-last cache)))
  12940. (org-columns-display-here-title)
  12941. (mapc (lambda (x)
  12942. (goto-line (car x))
  12943. (org-columns-display-here (cdr x)))
  12944. cache)))))
  12945. (defun org-columns-new (&optional prop title width op fmt)
  12946. "Insert a new column, to the leeft o the current column."
  12947. (interactive)
  12948. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  12949. cell)
  12950. (setq prop (completing-read
  12951. "Property: " (mapcar 'list (org-buffer-property-keys t))
  12952. nil nil prop))
  12953. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  12954. (setq width (read-string "Column width: " (if width (number-to-string width))))
  12955. (if (string-match "\\S-" width)
  12956. (setq width (string-to-number width))
  12957. (setq width nil))
  12958. (setq fmt (completing-read "Summary [none]: "
  12959. '(("none") ("add_numbers") ("add_times") ("checkbox"))
  12960. nil t))
  12961. (if (string-match "\\S-" fmt)
  12962. (setq fmt (intern fmt))
  12963. (setq fmt nil))
  12964. (if (eq fmt 'none) (setq fmt nil))
  12965. (if editp
  12966. (progn
  12967. (setcar editp prop)
  12968. (setcdr editp (list title width nil fmt)))
  12969. (setq cell (nthcdr (1- (current-column))
  12970. org-columns-current-fmt-compiled))
  12971. (setcdr cell (cons (list prop title width nil fmt)
  12972. (cdr cell))))
  12973. (org-columns-store-format)
  12974. (org-columns-redo)))
  12975. (defun org-columns-delete ()
  12976. "Delete the column at point from columns view."
  12977. (interactive)
  12978. (let* ((n (current-column))
  12979. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  12980. (when (y-or-n-p
  12981. (format "Are you sure you want to remove column \"%s\"? " title))
  12982. (setq org-columns-current-fmt-compiled
  12983. (delq (nth n org-columns-current-fmt-compiled)
  12984. org-columns-current-fmt-compiled))
  12985. (org-columns-store-format)
  12986. (org-columns-redo)
  12987. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  12988. (backward-char 1)))))
  12989. (defun org-columns-edit-attributes ()
  12990. "Edit the attributes of the current column."
  12991. (interactive)
  12992. (let* ((n (current-column))
  12993. (info (nth n org-columns-current-fmt-compiled)))
  12994. (apply 'org-columns-new info)))
  12995. (defun org-columns-widen (arg)
  12996. "Make the column wider by ARG characters."
  12997. (interactive "p")
  12998. (let* ((n (current-column))
  12999. (entry (nth n org-columns-current-fmt-compiled))
  13000. (width (or (nth 2 entry)
  13001. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  13002. (setq width (max 1 (+ width arg)))
  13003. (setcar (nthcdr 2 entry) width)
  13004. (org-columns-store-format)
  13005. (org-columns-redo)))
  13006. (defun org-columns-narrow (arg)
  13007. "Make the column nrrower by ARG characters."
  13008. (interactive "p")
  13009. (org-columns-widen (- arg)))
  13010. (defun org-columns-move-right ()
  13011. "Swap this column with the one to the right."
  13012. (interactive)
  13013. (let* ((n (current-column))
  13014. (cell (nthcdr n org-columns-current-fmt-compiled))
  13015. e)
  13016. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  13017. (error "Cannot shift this column further to the right"))
  13018. (setq e (car cell))
  13019. (setcar cell (car (cdr cell)))
  13020. (setcdr cell (cons e (cdr (cdr cell))))
  13021. (org-columns-store-format)
  13022. (org-columns-redo)
  13023. (forward-char 1)))
  13024. (defun org-columns-move-left ()
  13025. "Swap this column with the one to the left."
  13026. (interactive)
  13027. (let* ((n (current-column)))
  13028. (when (= n 0)
  13029. (error "Cannot shift this column further to the left"))
  13030. (backward-char 1)
  13031. (org-columns-move-right)
  13032. (backward-char 1)))
  13033. (defun org-columns-store-format ()
  13034. "Store the text version of the current columns format in appropriate place.
  13035. This is either in the COLUMNS property of the node starting the current column
  13036. display, or in the #+COLUMNS line of the current buffer."
  13037. (let (fmt)
  13038. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  13039. (if (marker-position org-columns-top-level-marker)
  13040. (save-excursion
  13041. (goto-char org-columns-top-level-marker)
  13042. (if (org-entry-get nil "COLUMNS")
  13043. (org-entry-put nil "COLUMNS" fmt)
  13044. (goto-char (point-min))
  13045. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  13046. (replace-match (concat "#+COLUMNS: " fmt t t)))))
  13047. (setq org-columns-current-fmt fmt))))
  13048. (defvar org-overriding-columns-format nil
  13049. "When set, overrides any other definition.")
  13050. (defvar org-agenda-view-columns-initially nil
  13051. "When set, switch to columns view immediately after creating the agenda.")
  13052. (defun org-agenda-columns ()
  13053. "Turn on column view in the agenda."
  13054. (interactive)
  13055. (org-verify-version 'columns)
  13056. (org-columns-remove-overlays)
  13057. (move-marker org-columns-begin-marker (point))
  13058. (let (fmt cache maxwidths m)
  13059. (cond
  13060. ((and (local-variable-p 'org-overriding-columns-format)
  13061. org-overriding-columns-format)
  13062. (setq fmt org-overriding-columns-format))
  13063. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  13064. (setq fmt (org-entry-get m "COLUMNS" t)))
  13065. ((and (boundp 'org-columns-current-fmt)
  13066. (local-variable-p 'org-columns-current-fmt)
  13067. org-columns-current-fmt)
  13068. (setq fmt org-columns-current-fmt))
  13069. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  13070. (setq m (get-text-property m 'org-hd-marker))
  13071. (setq fmt (org-entry-get m "COLUMNS" t))))
  13072. (setq fmt (or fmt org-columns-default-format))
  13073. (org-set-local 'org-columns-current-fmt fmt)
  13074. (org-columns-compile-format fmt)
  13075. (save-excursion
  13076. ;; Get and cache the properties
  13077. (goto-char (point-min))
  13078. (while (not (eobp))
  13079. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  13080. (get-text-property (point) 'org-marker)))
  13081. (push (cons (org-current-line) (org-entry-properties m)) cache))
  13082. (beginning-of-line 2))
  13083. (when cache
  13084. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13085. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13086. (goto-line (car (org-last cache)))
  13087. (org-columns-display-here-title)
  13088. (mapc (lambda (x)
  13089. (goto-line (car x))
  13090. (org-columns-display-here (cdr x)))
  13091. cache)))))
  13092. (defun org-columns-get-autowidth-alist (s cache)
  13093. "Derive the maximum column widths from the format and the cache."
  13094. (let ((start 0) rtn)
  13095. (while (string-match "%\\([a-zA-Z]\\S-*\\)" s start)
  13096. (push (cons (match-string 1 s) 1) rtn)
  13097. (setq start (match-end 0)))
  13098. (mapc (lambda (x)
  13099. (setcdr x (apply 'max
  13100. (mapcar
  13101. (lambda (y)
  13102. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  13103. cache))))
  13104. rtn)
  13105. rtn))
  13106. (defun org-columns-compute-all ()
  13107. "Compute all columns that have operators defined."
  13108. (remove-text-properties (point-min) (point-max) '(org-summaries t))
  13109. (let ((columns org-columns-current-fmt-compiled) col)
  13110. (while (setq col (pop columns))
  13111. (when (nth 3 col)
  13112. (save-excursion
  13113. (org-columns-compute (car col)))))))
  13114. (defun org-columns-update (property)
  13115. "Recompute PROPERTY, and update the columns display for it."
  13116. (org-columns-compute property)
  13117. (let (fmt val pos)
  13118. (save-excursion
  13119. (mapc (lambda (ov)
  13120. (when (equal (org-overlay-get ov 'org-columns-key) property)
  13121. (setq pos (org-overlay-start ov))
  13122. (goto-char pos)
  13123. (when (setq val (cdr (assoc property
  13124. (get-text-property (point-at-bol) 'org-summaries))))
  13125. (setq fmt (org-overlay-get ov 'org-columns-format))
  13126. (org-overlay-put ov 'display (format fmt val)))))
  13127. org-columns-overlays))))
  13128. (defun org-columns-compute (property)
  13129. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  13130. (interactive)
  13131. (let* ((re (concat "^" outline-regexp))
  13132. (lmax 30) ; Does anyone use deeper levels???
  13133. (lsum (make-vector lmax 0))
  13134. (level 0)
  13135. (ass (assoc property org-columns-current-fmt-compiled))
  13136. (format (nth 4 ass))
  13137. (beg org-columns-top-level-marker)
  13138. last-level val end sumpos sum-alist sum str)
  13139. (save-excursion
  13140. ;; Find the region to compute
  13141. (goto-char beg)
  13142. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  13143. (goto-char end)
  13144. ;; Walk the tree from the back and do the computations
  13145. (while (re-search-backward re beg t)
  13146. (setq sumpos (match-beginning 0)
  13147. last-level level
  13148. level (org-outline-level)
  13149. val (org-entry-get nil property))
  13150. (cond
  13151. ((< level last-level)
  13152. ;; put the sum of lower levels here as a property
  13153. (setq sum (aref lsum last-level)
  13154. str (org-column-number-to-string sum format)
  13155. sum-alist (get-text-property sumpos 'org-summaries))
  13156. (if (assoc property sum-alist)
  13157. (setcdr (assoc property sum-alist) str)
  13158. (push (cons property str) sum-alist)
  13159. (add-text-properties sumpos (1+ sumpos)
  13160. (list 'org-summaries sum-alist)))
  13161. (when val
  13162. (org-entry-put nil property str))
  13163. ;; add current to current level accumulator
  13164. (aset lsum level (+ (aref lsum level) sum))
  13165. ;; clear accumulators for deeper levels
  13166. (loop for l from (1+ level) to (1- lmax) do (aset lsum l 0)))
  13167. ((>= level last-level)
  13168. ;; add what we have here to the accumulator for this level
  13169. (aset lsum level (+ (aref lsum level)
  13170. (org-column-string-to-number (or val "0") format))))
  13171. (t (error "This should not happen")))))))
  13172. (defun org-columns-redo ()
  13173. "Construct the column display again."
  13174. (interactive)
  13175. (message "Recomputing columns...")
  13176. (save-excursion
  13177. (if (marker-position org-columns-begin-marker)
  13178. (goto-char org-columns-begin-marker))
  13179. (org-columns-remove-overlays)
  13180. (if (org-mode-p)
  13181. (call-interactively 'org-columns)
  13182. (call-interactively 'org-agenda-columns)))
  13183. (message "Recomputing columns...done"))
  13184. (defun org-columns-not-in-agenda ()
  13185. (if (eq major-mode 'org-agenda-mode)
  13186. (error "This command is only allowed in Org-mode buffers")))
  13187. (defun org-string-to-number (s)
  13188. "Convert string to number, and interpret hh:mm:ss."
  13189. (if (not (string-match ":" s))
  13190. (string-to-number s)
  13191. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  13192. (while l
  13193. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  13194. sum)))
  13195. (defun org-column-number-to-string (n fmt)
  13196. "Convert a computed column number to a string value, according to FMT."
  13197. (cond
  13198. ((eq fmt 'add_times)
  13199. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  13200. (format "%d:%02d" h m)))
  13201. ((eq fmt 'checkbox)
  13202. (cond ((= n (floor n)) "[X]")
  13203. ((> n 1.) "[-]")
  13204. (t "[ ]")))
  13205. (t (number-to-string n))))
  13206. (defun org-column-string-to-number (s fmt)
  13207. "Convert a column value to a number that can be used for column computing."
  13208. (cond
  13209. ((string-match ":" s)
  13210. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  13211. (while l
  13212. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  13213. sum))
  13214. ((eq fmt 'checkbox)
  13215. (if (equal s "[X]") 1. 0.000001))
  13216. (t (string-to-number s))))
  13217. (defun org-columns-uncompile-format (cfmt)
  13218. "Turn the compiled columns format back into a string representation."
  13219. (let ((rtn "") e s prop title op width fmt)
  13220. (while (setq e (pop cfmt))
  13221. (setq prop (car e)
  13222. title (nth 1 e)
  13223. width (nth 2 e)
  13224. op (nth 3 e)
  13225. fmt (nth 4 e))
  13226. (cond
  13227. ((eq fmt 'add_times) (setq op ":"))
  13228. ((eq fmt 'checkbox) (setq op "X"))
  13229. ((eq fmt 'add_numbers) (setq op "+")))
  13230. (if (equal title prop) (setq title nil))
  13231. (setq s (concat "%" (if width (number-to-string width))
  13232. prop
  13233. (if title (concat "(" title ")"))
  13234. (if op (concat "{" op "}"))))
  13235. (setq rtn (concat rtn " " s)))
  13236. (org-trim rtn)))
  13237. (defun org-columns-compile-format (fmt)
  13238. "FIXME"
  13239. (let ((start 0) width prop title op f)
  13240. (setq org-columns-current-fmt-compiled nil)
  13241. (while (string-match "%\\([0-9]+\\)?\\([a-zA-Z_0-9]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*"
  13242. fmt start)
  13243. (setq start (match-end 0)
  13244. width (match-string 1 fmt)
  13245. prop (match-string 2 fmt)
  13246. title (or (match-string 3 fmt) prop)
  13247. op (match-string 4 fmt)
  13248. f nil)
  13249. (if width (setq width (string-to-number width)))
  13250. (cond
  13251. ((equal op "+") (setq f 'add_numbers))
  13252. ((equal op ":") (setq f 'add_times))
  13253. ((equal op "X") (setq f 'checkbox)))
  13254. (push (list prop title width op f) org-columns-current-fmt-compiled))
  13255. (setq org-columns-current-fmt-compiled
  13256. (nreverse org-columns-current-fmt-compiled))))
  13257. ;;;; Timestamps
  13258. (defvar org-last-changed-timestamp nil)
  13259. (defvar org-time-was-given) ; dynamically scoped parameter
  13260. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13261. (defvar org-ts-what) ; dynamically scoped parameter
  13262. (defun org-time-stamp (arg)
  13263. "Prompt for a date/time and insert a time stamp.
  13264. If the user specifies a time like HH:MM, or if this command is called
  13265. with a prefix argument, the time stamp will contain date and time.
  13266. Otherwise, only the date will be included. All parts of a date not
  13267. specified by the user will be filled in from the current date/time.
  13268. So if you press just return without typing anything, the time stamp
  13269. will represent the current date/time. If there is already a timestamp
  13270. at the cursor, it will be modified."
  13271. (interactive "P")
  13272. (let (org-time-was-given org-end-time-was-given time)
  13273. (cond
  13274. ((and (org-at-timestamp-p)
  13275. (eq last-command 'org-time-stamp)
  13276. (eq this-command 'org-time-stamp))
  13277. (insert "--")
  13278. (setq time (let ((this-command this-command))
  13279. (org-read-date arg 'totime)))
  13280. (org-insert-time-stamp time (or org-time-was-given arg)))
  13281. ((org-at-timestamp-p)
  13282. (setq time (let ((this-command this-command))
  13283. (org-read-date arg 'totime)))
  13284. (when (org-at-timestamp-p) ; just to get the match data
  13285. (replace-match "")
  13286. (setq org-last-changed-timestamp
  13287. (org-insert-time-stamp
  13288. time (or org-time-was-given arg)
  13289. nil nil nil (list org-end-time-was-given))))
  13290. (message "Timestamp updated"))
  13291. (t
  13292. (setq time (let ((this-command this-command))
  13293. (org-read-date arg 'totime)))
  13294. (org-insert-time-stamp time (or org-time-was-given arg)
  13295. nil nil nil (list org-end-time-was-given))))))
  13296. (defun org-time-stamp-inactive (&optional arg)
  13297. "Insert an inactive time stamp.
  13298. An inactive time stamp is enclosed in square brackets instead of angle
  13299. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13300. does not link to the calendar and cannot be changed with the S-cursor keys.
  13301. So these are more for recording a certain time/date."
  13302. (interactive "P")
  13303. (let (org-time-was-given org-end-time-was-given time)
  13304. (setq time (org-read-date arg 'totime))
  13305. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  13306. nil nil (list org-end-time-was-given))))
  13307. (defvar org-date-ovl (org-make-overlay 1 1))
  13308. (org-overlay-put org-date-ovl 'face 'org-warning)
  13309. (org-detach-overlay org-date-ovl)
  13310. (defvar org-ans1) ; dynamically scoped parameter
  13311. (defvar org-ans2) ; dynamically scoped parameter
  13312. (defvar org-plain-time-of-day-regexp) ; defined below
  13313. (defun org-read-date (&optional with-time to-time from-string prompt)
  13314. "Read a date and make things smooth for the user.
  13315. The prompt will suggest to enter an ISO date, but you can also enter anything
  13316. which will at least partially be understood by `parse-time-string'.
  13317. Unrecognized parts of the date will default to the current day, month, year,
  13318. hour and minute. For example,
  13319. 3-2-5 --> 2003-02-05
  13320. feb 15 --> currentyear-02-15
  13321. sep 12 9 --> 2009-09-12
  13322. 12:45 --> today 12:45
  13323. 22 sept 0:34 --> currentyear-09-22 0:34
  13324. 12 --> currentyear-currentmonth-12
  13325. Fri --> nearest Friday (today or later)
  13326. +4 --> four days from today (only if +N is the only thing given)
  13327. etc.
  13328. The function understands only English month and weekday abbreviations,
  13329. but this can be configured with the variables `parse-time-months' and
  13330. `parse-time-weekdays'.
  13331. While prompting, a calendar is popped up - you can also select the
  13332. date with the mouse (button 1). The calendar shows a period of three
  13333. months. To scroll it to other months, use the keys `>' and `<'.
  13334. If you don't like the calendar, turn it off with
  13335. \(setq org-popup-calendar-for-date-prompt nil)
  13336. With optional argument TO-TIME, the date will immediately be converted
  13337. to an internal time.
  13338. With an optional argument WITH-TIME, the prompt will suggest to also
  13339. insert a time. Note that when WITH-TIME is not set, you can still
  13340. enter a time, and this function will inform the calling routine about
  13341. this change. The calling routine may then choose to change the format
  13342. used to insert the time stamp into the buffer to include the time."
  13343. (require 'parse-time)
  13344. (let* ((org-time-stamp-rounding-minutes
  13345. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  13346. (ct (org-current-time))
  13347. (default-time
  13348. ;; Default time is either today, or, when entering a range,
  13349. ;; the range start.
  13350. (if (save-excursion
  13351. (re-search-backward
  13352. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13353. (- (point) 20) t))
  13354. (apply
  13355. 'encode-time
  13356. (mapcar (lambda(x) (or x 0))
  13357. (parse-time-string (match-string 1))))
  13358. ct))
  13359. (calendar-move-hook nil)
  13360. (view-diary-entries-initially nil)
  13361. (view-calendar-holidays-initially nil)
  13362. (timestr (format-time-string
  13363. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  13364. (prompt (concat (if prompt (concat prompt " ") "")
  13365. (format "Date and/or time (default [%s]): " timestr)))
  13366. ans (org-ans0 "") org-ans1 org-ans2 (deltadays 0)
  13367. second minute hour day month year tl wday wday1 pm)
  13368. (cond
  13369. (from-string (setq ans from-string))
  13370. (org-popup-calendar-for-date-prompt
  13371. (save-excursion
  13372. (save-window-excursion
  13373. (calendar)
  13374. (calendar-forward-day (- (time-to-days default-time)
  13375. (calendar-absolute-from-gregorian
  13376. (calendar-current-date))))
  13377. (org-eval-in-calendar nil t)
  13378. (let* ((old-map (current-local-map))
  13379. (map (copy-keymap calendar-mode-map))
  13380. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13381. (org-defkey map (kbd "RET") 'org-calendar-select)
  13382. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  13383. 'org-calendar-select-mouse)
  13384. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  13385. 'org-calendar-select-mouse)
  13386. (org-defkey minibuffer-local-map [(meta shift left)]
  13387. (lambda () (interactive)
  13388. (org-eval-in-calendar '(calendar-backward-month 1))))
  13389. (org-defkey minibuffer-local-map [(meta shift right)]
  13390. (lambda () (interactive)
  13391. (org-eval-in-calendar '(calendar-forward-month 1))))
  13392. (org-defkey minibuffer-local-map [(shift up)]
  13393. (lambda () (interactive)
  13394. (org-eval-in-calendar '(calendar-backward-week 1))))
  13395. (org-defkey minibuffer-local-map [(shift down)]
  13396. (lambda () (interactive)
  13397. (org-eval-in-calendar '(calendar-forward-week 1))))
  13398. (org-defkey minibuffer-local-map [(shift left)]
  13399. (lambda () (interactive)
  13400. (org-eval-in-calendar '(calendar-backward-day 1))))
  13401. (org-defkey minibuffer-local-map [(shift right)]
  13402. (lambda () (interactive)
  13403. (org-eval-in-calendar '(calendar-forward-day 1))))
  13404. (org-defkey minibuffer-local-map ">"
  13405. (lambda () (interactive)
  13406. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13407. (org-defkey minibuffer-local-map "<"
  13408. (lambda () (interactive)
  13409. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13410. (unwind-protect
  13411. (progn
  13412. (use-local-map map)
  13413. (setq org-ans0 (read-string prompt "" nil nil))
  13414. ;; org-ans0: from prompt
  13415. ;; org-ans1: from mouse click
  13416. ;; org-ans2: from calendar motion
  13417. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13418. (use-local-map old-map))))))
  13419. (t ; Naked prompt only
  13420. (setq ans (read-string prompt "" nil timestr))))
  13421. (org-detach-overlay org-date-ovl)
  13422. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" org-ans0)
  13423. (setq deltadays (string-to-number ans) ans ""))
  13424. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  13425. (when (string-match
  13426. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13427. (setq year (if (match-end 2)
  13428. (string-to-number (match-string 2 ans))
  13429. (string-to-number (format-time-string "%Y")))
  13430. month (string-to-number (match-string 3 ans))
  13431. day (string-to-number (match-string 4 ans)))
  13432. (if (< year 100) (setq year (+ 2000 year)))
  13433. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13434. t nil ans)))
  13435. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13436. ;; If there is a time with am/pm, and *no* time without it, we convert
  13437. ;; so that matching will be successful.
  13438. ;; FIXME: make this replace twice, so that we catch the end time.
  13439. (when (and (not (string-match "[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13440. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13441. (setq hour (string-to-number (match-string 1 ans))
  13442. minute (if (match-end 3) (string-to-number (match-string 3 ans)) 0)
  13443. pm (equal ?p (string-to-char (downcase (match-string 4 ans)))))
  13444. (if (and (= hour 12) (not pm))
  13445. (setq hour 0)
  13446. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13447. (setq ans (replace-match (format "%02d:%02d" hour minute) t t ans)))
  13448. ;; Check if there is a time range
  13449. (when (and (boundp 'org-end-time-was-given)
  13450. (string-match org-plain-time-of-day-regexp ans)
  13451. (match-end 8))
  13452. (setq org-end-time-was-given (match-string 8 ans))
  13453. (setq ans (concat (substring ans 0 (match-beginning 7))
  13454. (substring ans (match-end 7)))))
  13455. (setq tl (parse-time-string ans)
  13456. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  13457. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  13458. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  13459. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  13460. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  13461. second (or (nth 0 tl) 0)
  13462. wday (nth 6 tl))
  13463. (setq day (+ day deltadays))
  13464. (when (and wday (not (nth 3 tl)))
  13465. ;; Weekday was given, but no day, so pick that day in the week
  13466. ;; on or after the derived date.
  13467. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13468. (unless (equal wday wday1)
  13469. (setq day (+ day (% (- wday wday1 -7) 7)))))
  13470. (if (and (boundp 'org-time-was-given)
  13471. (nth 2 tl))
  13472. (setq org-time-was-given t))
  13473. (if (< year 100) (setq year (+ 2000 year)))
  13474. (if to-time
  13475. (encode-time second minute hour day month year)
  13476. (if (or (nth 1 tl) (nth 2 tl))
  13477. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  13478. (format "%04d-%02d-%02d" year month day)))))
  13479. (defun org-eval-in-calendar (form &optional keepdate)
  13480. "Eval FORM in the calendar window and return to current window.
  13481. Also, store the cursor date in variable org-ans2."
  13482. (let ((sw (selected-window)))
  13483. (select-window (get-buffer-window "*Calendar*"))
  13484. (eval form)
  13485. (when (and (not keepdate) (calendar-cursor-to-date))
  13486. (let* ((date (calendar-cursor-to-date))
  13487. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13488. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13489. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13490. (select-window sw)
  13491. ;; Update the prompt to show new default date
  13492. (save-excursion
  13493. (goto-char (point-min))
  13494. (when (and org-ans2
  13495. (re-search-forward "\\[[-0-9]+\\]" nil t)
  13496. (get-text-property (match-end 0) 'field))
  13497. (let ((inhibit-read-only t))
  13498. (replace-match (concat "[" org-ans2 "]") t t)
  13499. (add-text-properties (point-min) (1+ (match-end 0))
  13500. (text-properties-at (1+ (point-min)))))))))
  13501. (defun org-calendar-select ()
  13502. "Return to `org-read-date' with the date currently selected.
  13503. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13504. (interactive)
  13505. (when (calendar-cursor-to-date)
  13506. (let* ((date (calendar-cursor-to-date))
  13507. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13508. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13509. (if (active-minibuffer-window) (exit-minibuffer))))
  13510. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13511. "Insert a date stamp for the date given by the internal TIME.
  13512. WITH-HM means, use the stamp format that includes the time of the day.
  13513. INACTIVE means use square brackets instead of angular ones, so that the
  13514. stamp will not contribute to the agenda.
  13515. PRE and POST are optional strings to be inserted before and after the
  13516. stamp.
  13517. The command returns the inserted time stamp."
  13518. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13519. stamp)
  13520. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13521. (insert (or pre ""))
  13522. (insert (setq stamp (format-time-string fmt time)))
  13523. (when (listp extra)
  13524. (setq extra (car extra))
  13525. (if (and (stringp extra)
  13526. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13527. (setq extra (format "-%02d:%02d"
  13528. (string-to-number (match-string 1 extra))
  13529. (string-to-number (match-string 2 extra))))
  13530. (setq extra nil)))
  13531. (when extra
  13532. (backward-char 1)
  13533. (insert extra)
  13534. (forward-char 1))
  13535. (insert (or post ""))
  13536. stamp))
  13537. (defun org-toggle-time-stamp-overlays ()
  13538. "Toggle the use of custom time stamp formats."
  13539. (interactive)
  13540. (setq org-display-custom-times (not org-display-custom-times))
  13541. (unless org-display-custom-times
  13542. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13543. (while (setq p (next-single-property-change p 'display))
  13544. (if (and (get-text-property p 'display)
  13545. (eq (get-text-property p 'face) 'org-date))
  13546. (remove-text-properties
  13547. p (setq p (next-single-property-change p 'display))
  13548. '(display t))))
  13549. (set-buffer-modified-p bmp)))
  13550. (if (featurep 'xemacs)
  13551. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13552. (org-restart-font-lock)
  13553. (setq org-table-may-need-update t)
  13554. (if org-display-custom-times
  13555. (message "Time stamps are overlayed with custom format")
  13556. (message "Time stamp overlays removed")))
  13557. (defun org-display-custom-time (beg end)
  13558. "Overlay modified time stamp format over timestamp between BED and END."
  13559. (let* ((ts (buffer-substring beg end))
  13560. t1 w1 with-hm tf time str w2 (off 0))
  13561. (save-match-data
  13562. (setq t1 (org-parse-time-string ts t))
  13563. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  13564. (setq off (- (match-end 0) (match-beginning 0)))))
  13565. (setq end (- end off))
  13566. (setq w1 (- end beg)
  13567. with-hm (and (nth 1 t1) (nth 2 t1))
  13568. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13569. time (org-fix-decoded-time t1)
  13570. str (org-add-props
  13571. (format-time-string
  13572. (substring tf 1 -1) (apply 'encode-time time))
  13573. nil 'mouse-face 'highlight)
  13574. w2 (length str))
  13575. (if (not (= w2 w1))
  13576. (add-text-properties (1+ beg) (+ 2 beg)
  13577. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13578. (if (featurep 'xemacs)
  13579. (progn
  13580. (put-text-property beg end 'invisible t)
  13581. (put-text-property beg end 'end-glyph (make-glyph str)))
  13582. (put-text-property beg end 'display str))))
  13583. (defun org-translate-time (string)
  13584. "Translate all timestamps in STRING to custom format.
  13585. But do this only if the variable `org-display-custom-times' is set."
  13586. (when org-display-custom-times
  13587. (save-match-data
  13588. (let* ((start 0)
  13589. (re org-ts-regexp-both)
  13590. t1 with-hm inactive tf time str beg end)
  13591. (while (setq start (string-match re string start))
  13592. (setq beg (match-beginning 0)
  13593. end (match-end 0)
  13594. t1 (save-match-data
  13595. (org-parse-time-string (substring string beg end) t))
  13596. with-hm (and (nth 1 t1) (nth 2 t1))
  13597. inactive (equal (substring string beg (1+ beg)) "[")
  13598. tf (funcall (if with-hm 'cdr 'car)
  13599. org-time-stamp-custom-formats)
  13600. time (org-fix-decoded-time t1)
  13601. str (format-time-string
  13602. (concat
  13603. (if inactive "[" "<") (substring tf 1 -1)
  13604. (if inactive "]" ">"))
  13605. (apply 'encode-time time))
  13606. string (replace-match str t t string)
  13607. start (+ start (length str)))))))
  13608. string)
  13609. (defun org-fix-decoded-time (time)
  13610. "Set 0 instead of nil for the first 6 elements of time.
  13611. Don't touch the rest."
  13612. (let ((n 0))
  13613. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13614. (defun org-days-to-time (timestamp-string)
  13615. "Difference between TIMESTAMP-STRING and now in days."
  13616. (- (time-to-days (org-time-string-to-time timestamp-string))
  13617. (time-to-days (current-time))))
  13618. (defun org-deadline-close (timestamp-string &optional ndays)
  13619. "Is the time in TIMESTAMP-STRING close to the current date?"
  13620. (and (< (org-days-to-time timestamp-string)
  13621. (or ndays org-deadline-warning-days))
  13622. (not (org-entry-is-done-p))))
  13623. (defun org-calendar-select-mouse (ev)
  13624. "Return to `org-read-date' with the date currently selected.
  13625. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13626. (interactive "e")
  13627. (mouse-set-point ev)
  13628. (when (calendar-cursor-to-date)
  13629. (let* ((date (calendar-cursor-to-date))
  13630. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13631. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13632. (if (active-minibuffer-window) (exit-minibuffer))))
  13633. (defun org-check-deadlines (ndays)
  13634. "Check if there are any deadlines due or past due.
  13635. A deadline is considered due if it happens within `org-deadline-warning-days'
  13636. days from today's date. If the deadline appears in an entry marked DONE,
  13637. it is not shown. The prefix arg NDAYS can be used to test that many
  13638. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13639. (interactive "P")
  13640. (let* ((org-warn-days
  13641. (cond
  13642. ((equal ndays '(4)) 100000)
  13643. (ndays (prefix-numeric-value ndays))
  13644. (t org-deadline-warning-days)))
  13645. (case-fold-search nil)
  13646. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13647. (callback
  13648. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13649. (message "%d deadlines past-due or due within %d days"
  13650. (org-occur regexp nil callback)
  13651. org-warn-days)))
  13652. (defun org-evaluate-time-range (&optional to-buffer)
  13653. "Evaluate a time range by computing the difference between start and end.
  13654. Normally the result is just printed in the echo area, but with prefix arg
  13655. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13656. If the time range is actually in a table, the result is inserted into the
  13657. next column.
  13658. For time difference computation, a year is assumed to be exactly 365
  13659. days in order to avoid rounding problems."
  13660. (interactive "P")
  13661. (or
  13662. (org-clock-update-time-maybe)
  13663. (save-excursion
  13664. (unless (org-at-date-range-p)
  13665. (goto-char (point-at-bol))
  13666. (re-search-forward org-tr-regexp (point-at-eol) t))
  13667. (if (not (org-at-date-range-p))
  13668. (error "Not at a time-stamp range, and none found in current line")))
  13669. (let* ((ts1 (match-string 1))
  13670. (ts2 (match-string 2))
  13671. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13672. (match-end (match-end 0))
  13673. (time1 (org-time-string-to-time ts1))
  13674. (time2 (org-time-string-to-time ts2))
  13675. (t1 (time-to-seconds time1))
  13676. (t2 (time-to-seconds time2))
  13677. (diff (abs (- t2 t1)))
  13678. (negative (< (- t2 t1) 0))
  13679. ;; (ys (floor (* 365 24 60 60)))
  13680. (ds (* 24 60 60))
  13681. (hs (* 60 60))
  13682. (fy "%dy %dd %02d:%02d")
  13683. (fy1 "%dy %dd")
  13684. (fd "%dd %02d:%02d")
  13685. (fd1 "%dd")
  13686. (fh "%02d:%02d")
  13687. y d h m align)
  13688. (if havetime
  13689. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13690. y 0
  13691. d (floor (/ diff ds)) diff (mod diff ds)
  13692. h (floor (/ diff hs)) diff (mod diff hs)
  13693. m (floor (/ diff 60)))
  13694. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13695. y 0
  13696. d (floor (+ (/ diff ds) 0.5))
  13697. h 0 m 0))
  13698. (if (not to-buffer)
  13699. (message (org-make-tdiff-string y d h m))
  13700. (when (org-at-table-p)
  13701. (goto-char match-end)
  13702. (setq align t)
  13703. (and (looking-at " *|") (goto-char (match-end 0))))
  13704. (if (looking-at
  13705. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13706. (replace-match ""))
  13707. (if negative (insert " -"))
  13708. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13709. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13710. (insert " " (format fh h m))))
  13711. (if align (org-table-align))
  13712. (message "Time difference inserted")))))
  13713. (defun org-make-tdiff-string (y d h m)
  13714. (let ((fmt "")
  13715. (l nil))
  13716. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13717. l (push y l)))
  13718. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13719. l (push d l)))
  13720. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13721. l (push h l)))
  13722. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13723. l (push m l)))
  13724. (apply 'format fmt (nreverse l))))
  13725. (defun org-time-string-to-time (s)
  13726. (apply 'encode-time (org-parse-time-string s)))
  13727. (defun org-time-string-to-absolute (s &optional daynr)
  13728. "Convert a time stamp to an absolute day number.
  13729. If there is a specifyer for a cyclic time stamp, get the closest date to
  13730. DATE."
  13731. (cond
  13732. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13733. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13734. daynr
  13735. (+ daynr 1000)))
  13736. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13737. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13738. (time-to-days (current-time))) (match-string 0 s)))
  13739. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13740. (defun org-calendar-holiday ()
  13741. "List of holidays, for Diary display in Org-mode."
  13742. (let ((hl (check-calendar-holidays date)))
  13743. (if hl (mapconcat 'identity hl "; "))))
  13744. (defun org-diary-sexp-entry (sexp entry date)
  13745. "Process a SEXP diary ENTRY for DATE."
  13746. (let ((result (if calendar-debug-sexp
  13747. (let ((stack-trace-on-error t))
  13748. (eval (car (read-from-string sexp))))
  13749. (condition-case nil
  13750. (eval (car (read-from-string sexp)))
  13751. (error
  13752. (beep)
  13753. (message "Bad sexp at line %d in %s: %s"
  13754. (org-current-line)
  13755. (buffer-file-name) sexp)
  13756. (sleep-for 2))))))
  13757. (cond ((stringp result) result)
  13758. ((and (consp result)
  13759. (stringp (cdr result))) (cdr result))
  13760. (result entry)
  13761. (t nil))))
  13762. (defun org-diary-to-ical-string (frombuf)
  13763. "FIXME"
  13764. (let* ((tmpdir (if (featurep 'xemacs)
  13765. (temp-directory)
  13766. temporary-file-directory))
  13767. (tmpfile (make-temp-name
  13768. (expand-file-name "orgics" tmpdir)))
  13769. buf rtn b e)
  13770. (save-excursion
  13771. (set-buffer frombuf)
  13772. (icalendar-export-region (point-min) (point-max) tmpfile)
  13773. (setq buf (find-buffer-visiting tmpfile))
  13774. (set-buffer buf)
  13775. (goto-char (point-min))
  13776. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13777. (setq b (match-beginning 0)))
  13778. (goto-char (point-max))
  13779. (if (re-search-backward "^END:VEVENT" nil t)
  13780. (setq e (match-end 0)))
  13781. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13782. (kill-buffer buf)
  13783. (kill-buffer frombuf)
  13784. (delete-file tmpfile)
  13785. rtn))
  13786. (defun org-closest-date (start current change)
  13787. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  13788. ;; Make the proper lists from the dates
  13789. (catch 'exit
  13790. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13791. dn dw sday cday n1 n2
  13792. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13793. (setq start (org-date-to-gregorian start)
  13794. current (org-date-to-gregorian current)
  13795. sday (calendar-absolute-from-gregorian start)
  13796. cday (calendar-absolute-from-gregorian current))
  13797. (if (<= cday sday) (throw 'exit sday))
  13798. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13799. (setq dn (string-to-number (match-string 1 change))
  13800. dw (cdr (assoc (match-string 2 change) a1)))
  13801. (error "Invalid change specifyer: %s" change))
  13802. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13803. (cond
  13804. ((eq dw 'day)
  13805. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13806. n2 (+ n1 dn)))
  13807. ((eq dw 'year)
  13808. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13809. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13810. (setq date1 (list m d y1)
  13811. n1 (calendar-absolute-from-gregorian date1)
  13812. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13813. n2 (calendar-absolute-from-gregorian date2)))
  13814. ((eq dw 'month)
  13815. ;; approx number of month between the tow dates
  13816. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13817. ;; How often does dn fit in there?
  13818. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13819. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13820. m (+ m nm)
  13821. ny (floor (/ m 12))
  13822. y (+ y ny)
  13823. m (- m (* ny 12)))
  13824. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13825. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13826. (setq m2 (+ m dn) y2 y)
  13827. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13828. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13829. (while (< n2 cday)
  13830. (setq n1 n2 m m2 y y2)
  13831. (setq m2 (+ m dn) y2 y)
  13832. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13833. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13834. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1))))
  13835. (defun org-date-to-gregorian (date)
  13836. "Turn any specification of DATE into a gregorian date for the calendar."
  13837. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13838. ((and (listp date) (= (length date) 3)) date)
  13839. ((stringp date)
  13840. (setq date (org-parse-time-string date))
  13841. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13842. ((listp date)
  13843. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13844. (defun org-parse-time-string (s &optional nodefault)
  13845. "Parse the standard Org-mode time string.
  13846. This should be a lot faster than the normal `parse-time-string'.
  13847. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13848. hour and minute fields will be nil if not given."
  13849. (if (string-match org-ts-regexp0 s)
  13850. (list 0
  13851. (if (or (match-beginning 8) (not nodefault))
  13852. (string-to-number (or (match-string 8 s) "0")))
  13853. (if (or (match-beginning 7) (not nodefault))
  13854. (string-to-number (or (match-string 7 s) "0")))
  13855. (string-to-number (match-string 4 s))
  13856. (string-to-number (match-string 3 s))
  13857. (string-to-number (match-string 2 s))
  13858. nil nil nil)
  13859. (make-list 9 0)))
  13860. (defun org-timestamp-up (&optional arg)
  13861. "Increase the date item at the cursor by one.
  13862. If the cursor is on the year, change the year. If it is on the month or
  13863. the day, change that.
  13864. With prefix ARG, change by that many units."
  13865. (interactive "p")
  13866. (org-timestamp-change (prefix-numeric-value arg)))
  13867. (defun org-timestamp-down (&optional arg)
  13868. "Decrease the date item at the cursor by one.
  13869. If the cursor is on the year, change the year. If it is on the month or
  13870. the day, change that.
  13871. With prefix ARG, change by that many units."
  13872. (interactive "p")
  13873. (org-timestamp-change (- (prefix-numeric-value arg))))
  13874. (defun org-timestamp-up-day (&optional arg)
  13875. "Increase the date in the time stamp by one day.
  13876. With prefix ARG, change that many days."
  13877. (interactive "p")
  13878. (if (and (not (org-at-timestamp-p t))
  13879. (org-on-heading-p))
  13880. (org-todo 'up)
  13881. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13882. (defun org-timestamp-down-day (&optional arg)
  13883. "Decrease the date in the time stamp by one day.
  13884. With prefix ARG, change that many days."
  13885. (interactive "p")
  13886. (if (and (not (org-at-timestamp-p t))
  13887. (org-on-heading-p))
  13888. (org-todo 'down)
  13889. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13890. (defsubst org-pos-in-match-range (pos n)
  13891. (and (match-beginning n)
  13892. (<= (match-beginning n) pos)
  13893. (>= (match-end n) pos)))
  13894. (defun org-at-timestamp-p (&optional inactive-ok)
  13895. "Determine if the cursor is in or at a timestamp."
  13896. (interactive)
  13897. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13898. (pos (point))
  13899. (ans (or (looking-at tsr)
  13900. (save-excursion
  13901. (skip-chars-backward "^[<\n\r\t")
  13902. (if (> (point) 1) (backward-char 1))
  13903. (and (looking-at tsr)
  13904. (> (- (match-end 0) pos) -1))))))
  13905. (and (boundp 'org-ts-what)
  13906. (setq org-ts-what
  13907. (cond
  13908. ((org-pos-in-match-range pos 2) 'year)
  13909. ((org-pos-in-match-range pos 3) 'month)
  13910. ((org-pos-in-match-range pos 7) 'hour)
  13911. ((org-pos-in-match-range pos 8) 'minute)
  13912. ((or (org-pos-in-match-range pos 4)
  13913. (org-pos-in-match-range pos 5)) 'day)
  13914. ((and (> pos (or (match-end 8) (match-end 5)))
  13915. (< pos (match-end 0)))
  13916. (- pos (or (match-end 8) (match-end 5))))
  13917. (t 'day))))
  13918. ans))
  13919. (defun org-timestamp-change (n &optional what)
  13920. "Change the date in the time stamp at point.
  13921. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13922. `year', `minute', `second'. If WHAT is not given, the cursor position
  13923. in the timestamp determines what will be changed."
  13924. (let ((pos (point))
  13925. with-hm inactive
  13926. org-ts-what
  13927. extra
  13928. ts time time0)
  13929. (if (not (org-at-timestamp-p t))
  13930. (error "Not at a timestamp"))
  13931. (if (and (not what) (not (eq org-ts-what 'day))
  13932. org-display-custom-times
  13933. (get-text-property (point) 'display)
  13934. (not (get-text-property (1- (point)) 'display)))
  13935. (setq org-ts-what 'day))
  13936. (setq org-ts-what (or what org-ts-what)
  13937. inactive (= (char-after (match-beginning 0)) ?\[)
  13938. ts (match-string 0))
  13939. (replace-match "")
  13940. (if (string-match
  13941. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( \\+[0-9]+[dwmy]\\)?\\)[]>]"
  13942. ts)
  13943. (setq extra (match-string 1 ts)))
  13944. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13945. (setq with-hm t))
  13946. (setq time0 (org-parse-time-string ts))
  13947. (setq time
  13948. (apply 'encode-time
  13949. (append
  13950. (list (or (car time0) 0))
  13951. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  13952. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  13953. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  13954. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  13955. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  13956. (nthcdr 6 time0))))
  13957. (when (integerp org-ts-what)
  13958. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  13959. (if (eq what 'calendar)
  13960. (let ((cal-date
  13961. (save-excursion
  13962. (save-match-data
  13963. (set-buffer "*Calendar*")
  13964. (calendar-cursor-to-date)))))
  13965. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13966. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13967. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13968. (setcar time0 (or (car time0) 0))
  13969. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13970. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  13971. (setq time (apply 'encode-time time0))))
  13972. (setq org-last-changed-timestamp
  13973. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13974. (org-clock-update-time-maybe)
  13975. (goto-char pos)
  13976. ;; Try to recenter the calendar window, if any
  13977. (if (and org-calendar-follow-timestamp-change
  13978. (get-buffer-window "*Calendar*" t)
  13979. (memq org-ts-what '(day month year)))
  13980. (org-recenter-calendar (time-to-days time)))))
  13981. (defun org-modify-ts-extra (s pos n)
  13982. "FIXME"
  13983. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13984. ng h m new)
  13985. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13986. (cond
  13987. ((or (org-pos-in-match-range pos 2)
  13988. (org-pos-in-match-range pos 3))
  13989. (setq m (string-to-number (match-string 3 s))
  13990. h (string-to-number (match-string 2 s)))
  13991. (if (org-pos-in-match-range pos 2)
  13992. (setq h (+ h n))
  13993. (setq m (+ m n)))
  13994. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13995. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13996. (setq h (min 24 (max 0 h)))
  13997. (setq ng 1 new (format "-%02d:%02d" h m)))
  13998. ((org-pos-in-match-range pos 6)
  13999. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14000. ((org-pos-in-match-range pos 5)
  14001. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  14002. (when ng
  14003. (setq s (concat
  14004. (substring s 0 (match-beginning ng))
  14005. new
  14006. (substring s (match-end ng))))))
  14007. s))
  14008. (defun org-recenter-calendar (date)
  14009. "If the calendar is visible, recenter it to DATE."
  14010. (let* ((win (selected-window))
  14011. (cwin (get-buffer-window "*Calendar*" t))
  14012. (calendar-move-hook nil))
  14013. (when cwin
  14014. (select-window cwin)
  14015. (calendar-goto-date (if (listp date) date
  14016. (calendar-gregorian-from-absolute date)))
  14017. (select-window win))))
  14018. (defun org-goto-calendar (&optional arg)
  14019. "Go to the Emacs calendar at the current date.
  14020. If there is a time stamp in the current line, go to that date.
  14021. A prefix ARG can be used to force the current date."
  14022. (interactive "P")
  14023. (let ((tsr org-ts-regexp) diff
  14024. (calendar-move-hook nil)
  14025. (view-calendar-holidays-initially nil)
  14026. (view-diary-entries-initially nil))
  14027. (if (or (org-at-timestamp-p)
  14028. (save-excursion
  14029. (beginning-of-line 1)
  14030. (looking-at (concat ".*" tsr))))
  14031. (let ((d1 (time-to-days (current-time)))
  14032. (d2 (time-to-days
  14033. (org-time-string-to-time (match-string 1)))))
  14034. (setq diff (- d2 d1))))
  14035. (calendar)
  14036. (calendar-goto-today)
  14037. (if (and diff (not arg)) (calendar-forward-day diff))))
  14038. (defun org-date-from-calendar ()
  14039. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14040. If there is already a time stamp at the cursor position, update it."
  14041. (interactive)
  14042. (org-timestamp-change 0 'calendar))
  14043. ;;; The clock for measuring work time.
  14044. (defvar org-mode-line-string "")
  14045. (put 'org-mode-line-string 'risky-local-variable t)
  14046. (defvar org-mode-line-timer nil)
  14047. (defvar org-clock-heading "")
  14048. (defvar org-clock-start-time "")
  14049. (defun org-update-mode-line ()
  14050. (let* ((delta (- (time-to-seconds (current-time))
  14051. (time-to-seconds org-clock-start-time)))
  14052. (h (floor delta 3600))
  14053. (m (floor (- delta (* 3600 h)) 60)))
  14054. (setq org-mode-line-string
  14055. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  14056. 'help-echo "Org-mode clock is running"))
  14057. (force-mode-line-update)))
  14058. (defvar org-clock-marker (make-marker)
  14059. "Marker recording the last clock-in.")
  14060. (defvar org-clock-mode-line-entry nil
  14061. "Information for the modeline about the running clock.")
  14062. (defun org-clock-in ()
  14063. "Start the clock on the current item.
  14064. If necessary, clock-out of the currently active clock."
  14065. (interactive)
  14066. (org-clock-out t)
  14067. (let (ts)
  14068. (save-excursion
  14069. (org-back-to-heading t)
  14070. (if (looking-at org-todo-line-regexp)
  14071. (setq org-clock-heading (match-string 3))
  14072. (setq org-clock-heading "???"))
  14073. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  14074. (beginning-of-line 2)
  14075. (when (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  14076. (not (equal (match-string 1) org-clock-string)))
  14077. ;; First line hast scheduling info, move one further
  14078. (beginning-of-line 2)
  14079. (or (bolp) (newline)))
  14080. (insert "\n") (backward-char 1)
  14081. (indent-relative)
  14082. (insert org-clock-string " ")
  14083. (setq org-clock-start-time (current-time))
  14084. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  14085. (move-marker org-clock-marker (point) (buffer-base-buffer))
  14086. (or global-mode-string (setq global-mode-string '("")))
  14087. (or (memq 'org-mode-line-string global-mode-string)
  14088. (setq global-mode-string
  14089. (append global-mode-string '(org-mode-line-string))))
  14090. (org-update-mode-line)
  14091. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  14092. (message "Clock started at %s" ts))))
  14093. (defun org-clock-out (&optional fail-quietly)
  14094. "Stop the currently running clock.
  14095. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  14096. (interactive)
  14097. (catch 'exit
  14098. (if (not (marker-buffer org-clock-marker))
  14099. (if fail-quietly (throw 'exit t) (error "No active clock")))
  14100. (let (ts te s h m)
  14101. (save-excursion
  14102. (set-buffer (marker-buffer org-clock-marker))
  14103. (goto-char org-clock-marker)
  14104. (beginning-of-line 1)
  14105. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  14106. (equal (match-string 1) org-clock-string))
  14107. (setq ts (match-string 2))
  14108. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  14109. (goto-char (match-end 0))
  14110. (delete-region (point) (point-at-eol))
  14111. (insert "--")
  14112. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  14113. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  14114. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  14115. h (floor (/ s 3600))
  14116. s (- s (* 3600 h))
  14117. m (floor (/ s 60))
  14118. s (- s (* 60 s)))
  14119. (insert " => " (format "%2d:%02d" h m))
  14120. (move-marker org-clock-marker nil)
  14121. (org-add-log-maybe 'clock-out)
  14122. (when org-mode-line-timer
  14123. (cancel-timer org-mode-line-timer)
  14124. (setq org-mode-line-timer nil))
  14125. (setq global-mode-string
  14126. (delq 'org-mode-line-string global-mode-string))
  14127. (force-mode-line-update)
  14128. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  14129. (defun org-clock-cancel ()
  14130. "Cancel the running clock be removing the start timestamp."
  14131. (interactive)
  14132. (if (not (marker-buffer org-clock-marker))
  14133. (error "No active clock"))
  14134. (save-excursion
  14135. (set-buffer (marker-buffer org-clock-marker))
  14136. (goto-char org-clock-marker)
  14137. (delete-region (1- (point-at-bol)) (point-at-eol)))
  14138. (message "Clock canceled"))
  14139. (defvar org-clock-file-total-minutes nil
  14140. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  14141. (make-variable-buffer-local 'org-clock-file-total-minutes)
  14142. (defun org-clock-sum (&optional tstart tend)
  14143. "Sum the times for each subtree.
  14144. Puts the resulting times in minutes as a text property on each headline."
  14145. (interactive)
  14146. (let* ((bmp (buffer-modified-p))
  14147. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  14148. org-clock-string
  14149. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  14150. (lmax 30)
  14151. (ltimes (make-vector lmax 0))
  14152. (t1 0)
  14153. (level 0)
  14154. ts te dt
  14155. time)
  14156. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  14157. (save-excursion
  14158. (goto-char (point-max))
  14159. (while (re-search-backward re nil t)
  14160. (cond
  14161. ((match-end 2)
  14162. ;; Two time stamps
  14163. (setq ts (match-string 2)
  14164. te (match-string 3)
  14165. ts (time-to-seconds
  14166. (apply 'encode-time (org-parse-time-string ts)))
  14167. te (time-to-seconds
  14168. (apply 'encode-time (org-parse-time-string te)))
  14169. ts (if tstart (max ts tstart) ts)
  14170. te (if tend (min te tend) te)
  14171. dt (- te ts)
  14172. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  14173. ((match-end 4)
  14174. ;; A naket time
  14175. (setq t1 (+ t1 (string-to-number (match-string 5))
  14176. (* 60 (string-to-number (match-string 4))))))
  14177. (t ;; A headline
  14178. (setq level (- (match-end 1) (match-beginning 1)))
  14179. (when (or (> t1 0) (> (aref ltimes level) 0))
  14180. (loop for l from 0 to level do
  14181. (aset ltimes l (+ (aref ltimes l) t1)))
  14182. (setq t1 0 time (aref ltimes level))
  14183. (loop for l from level to (1- lmax) do
  14184. (aset ltimes l 0))
  14185. (goto-char (match-beginning 0))
  14186. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  14187. (setq org-clock-file-total-minutes (aref ltimes 0)))
  14188. (set-buffer-modified-p bmp)))
  14189. (defun org-clock-display (&optional total-only)
  14190. "Show subtree times in the entire buffer.
  14191. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  14192. in the echo area."
  14193. (interactive)
  14194. (org-remove-clock-overlays)
  14195. (let (time h m p)
  14196. (org-clock-sum)
  14197. (unless total-only
  14198. (save-excursion
  14199. (goto-char (point-min))
  14200. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  14201. (goto-char p)
  14202. (when (setq time (get-text-property p :org-clock-minutes))
  14203. (org-put-clock-overlay time (funcall outline-level))))
  14204. (setq h (/ org-clock-file-total-minutes 60)
  14205. m (- org-clock-file-total-minutes (* 60 h)))
  14206. ;; Arrange to remove the overlays upon next change.
  14207. (when org-remove-highlights-with-change
  14208. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  14209. nil 'local))))
  14210. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  14211. (defvar org-clock-overlays nil)
  14212. (make-variable-buffer-local 'org-clock-overlays)
  14213. (defun org-put-clock-overlay (time &optional level)
  14214. "Put an overlays on the current line, displaying TIME.
  14215. If LEVEL is given, prefix time with a corresponding number of stars.
  14216. This creates a new overlay and stores it in `org-clock-overlays', so that it
  14217. will be easy to remove."
  14218. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  14219. (l (if level (org-get-legal-level level 0) 0))
  14220. (off 0)
  14221. ov tx)
  14222. (move-to-column c)
  14223. (unless (eolp) (skip-chars-backward "^ \t"))
  14224. (skip-chars-backward " \t")
  14225. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  14226. tx (concat (buffer-substring (1- (point)) (point))
  14227. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  14228. (org-add-props (format "%s %2d:%02d%s"
  14229. (make-string l ?*) h m
  14230. (make-string (- 10 l) ?\ ))
  14231. '(face secondary-selection))
  14232. ""))
  14233. (if (not (featurep 'xemacs))
  14234. (org-overlay-put ov 'display tx)
  14235. (org-overlay-put ov 'invisible t)
  14236. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  14237. (push ov org-clock-overlays)))
  14238. (defun org-remove-clock-overlays (&optional beg end noremove)
  14239. "Remove the occur highlights from the buffer.
  14240. BEG and END are ignored. If NOREMOVE is nil, remove this function
  14241. from the `before-change-functions' in the current buffer."
  14242. (interactive)
  14243. (unless org-inhibit-highlight-removal
  14244. (mapc 'org-delete-overlay org-clock-overlays)
  14245. (setq org-clock-overlays nil)
  14246. (unless noremove
  14247. (remove-hook 'before-change-functions
  14248. 'org-remove-clock-overlays 'local))))
  14249. (defun org-clock-out-if-current ()
  14250. "Clock out if the current entry contains the running clock.
  14251. This is used to stop the clock after a TODO entry is marked DONE."
  14252. (when (and (member state org-done-keywords)
  14253. (equal (marker-buffer org-clock-marker) (current-buffer))
  14254. (< (point) org-clock-marker)
  14255. (> (save-excursion (outline-next-heading) (point))
  14256. org-clock-marker))
  14257. ;; Clock out, but don't accept a logging message for this.
  14258. (let ((org-log-done (if (and (listp org-log-done)
  14259. (member 'clock-out org-log-done))
  14260. '(done)
  14261. org-log-done)))
  14262. (org-clock-out))))
  14263. (add-hook 'org-after-todo-state-change-hook
  14264. 'org-clock-out-if-current)
  14265. (defun org-check-running-clock ()
  14266. "Check if the current buffer contains the running clock.
  14267. If yes, offer to stop it and to save the buffer with the changes."
  14268. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  14269. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  14270. (buffer-name))))
  14271. (org-clock-out)
  14272. (when (y-or-n-p "Save changed buffer?")
  14273. (save-buffer))))
  14274. (defun org-clock-report ()
  14275. "Create a table containing a report about clocked time.
  14276. If the buffer contains lines
  14277. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  14278. #+END: clocktable
  14279. then the table will be inserted between these lines, replacing whatever
  14280. is was there before. If these lines are not in the buffer, the table
  14281. is inserted at point, surrounded by the special lines.
  14282. The BEGIN line can contain parameters. Allowed are:
  14283. :maxlevel The maximum level to be included in the table. Default is 3.
  14284. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  14285. (interactive)
  14286. (org-remove-clock-overlays)
  14287. (unless (org-find-dblock "clocktable")
  14288. (org-create-dblock (list :name "clocktable"
  14289. :maxlevel 2 :emphasize nil)))
  14290. (org-update-dblock))
  14291. (defun org-clock-update-time-maybe ()
  14292. "If this is a CLOCK line, update it and return t.
  14293. Otherwise, return nil."
  14294. (interactive)
  14295. (save-excursion
  14296. (beginning-of-line 1)
  14297. (skip-chars-forward " \t")
  14298. (when (looking-at org-clock-string)
  14299. (let ((re (concat "[ \t]*" org-clock-string
  14300. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  14301. "\\([ \t]*=>.*\\)?"))
  14302. ts te h m s)
  14303. (if (not (looking-at re))
  14304. nil
  14305. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  14306. (end-of-line 1)
  14307. (setq ts (match-string 1)
  14308. te (match-string 2))
  14309. (setq s (- (time-to-seconds
  14310. (apply 'encode-time (org-parse-time-string te)))
  14311. (time-to-seconds
  14312. (apply 'encode-time (org-parse-time-string ts))))
  14313. h (floor (/ s 3600))
  14314. s (- s (* 3600 h))
  14315. m (floor (/ s 60))
  14316. s (- s (* 60 s)))
  14317. (insert " => " (format "%2d:%02d" h m))
  14318. t)))))
  14319. (defun org-clock-special-range (key &optional time as-strings)
  14320. "Return two times bordering a special time range.
  14321. Key is a symbol specifying the range and can be one of `today', `yesterday',
  14322. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  14323. A week starts Monday 0:00 and ends Sunday 24:00.
  14324. The range is determined relative to TIME. TIME defaults to the current time.
  14325. The return value is a cons cell with two internal times like the ones
  14326. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  14327. the returned times will be formatted strings."
  14328. (let* ((tm (decode-time (or time (current-time))))
  14329. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  14330. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  14331. (dow (nth 6 tm))
  14332. s1 m1 h1 d1 month1 y1 diff ts te fm)
  14333. (cond
  14334. ((eq key 'today)
  14335. (setq h 0 m 0 h1 24 m1 0))
  14336. ((eq key 'yesterday)
  14337. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  14338. ((eq key 'thisweek)
  14339. (setq diff (if (= dow 0) 6 (1- dow))
  14340. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  14341. ((eq key 'lastweek)
  14342. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  14343. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  14344. ((eq key 'thismonth)
  14345. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  14346. ((eq key 'lastmonth)
  14347. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  14348. ((eq key 'thisyear)
  14349. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  14350. ((eq key 'lastyear)
  14351. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  14352. (t (error "No such time block %s" key)))
  14353. (setq ts (encode-time s m h d month y)
  14354. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  14355. (or d1 d) (or month1 month) (or y1 y)))
  14356. (setq fm (cdr org-time-stamp-formats))
  14357. (if as-strings
  14358. (cons (format-time-string fm ts) (format-time-string fm te))
  14359. (cons ts te))))
  14360. (defun org-dblock-write:clocktable (params)
  14361. "Write the standard clocktable."
  14362. (let ((hlchars '((1 . "*") (2 . ?/)))
  14363. (emph nil)
  14364. (ins (make-marker))
  14365. ipos time h m p level hlc hdl maxlevel
  14366. ts te cc block)
  14367. (setq maxlevel (or (plist-get params :maxlevel) 3)
  14368. emph (plist-get params :emphasize)
  14369. ts (plist-get params :tstart)
  14370. te (plist-get params :tend)
  14371. block (plist-get params :block))
  14372. (when block
  14373. (setq cc (org-clock-special-range block nil t)
  14374. ts (car cc) te (cdr cc)))
  14375. (if ts (setq ts (time-to-seconds
  14376. (apply 'encode-time (org-parse-time-string ts)))))
  14377. (if te (setq te (time-to-seconds
  14378. (apply 'encode-time (org-parse-time-string te)))))
  14379. (move-marker ins (point))
  14380. (setq ipos (point))
  14381. (insert-before-markers "Clock summary at ["
  14382. (substring
  14383. (format-time-string (cdr org-time-stamp-formats))
  14384. 1 -1)
  14385. "]."
  14386. (if block
  14387. (format " Considered range is /%s/." block)
  14388. "")
  14389. "\n\n|L|Headline|Time|\n")
  14390. (org-clock-sum ts te)
  14391. (setq h (/ org-clock-file-total-minutes 60)
  14392. m (- org-clock-file-total-minutes (* 60 h)))
  14393. (insert-before-markers "|-\n|0|" "*Total file time*| "
  14394. (format "*%d:%02d*" h m)
  14395. "|\n")
  14396. (goto-char (point-min))
  14397. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  14398. (goto-char p)
  14399. (when (setq time (get-text-property p :org-clock-minutes))
  14400. (save-excursion
  14401. (beginning-of-line 1)
  14402. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  14403. (setq level (- (match-end 1) (match-beginning 1)))
  14404. (<= level maxlevel))
  14405. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  14406. hdl (match-string 2)
  14407. h (/ time 60)
  14408. m (- time (* 60 h)))
  14409. (goto-char ins)
  14410. (if (= level 1) (insert-before-markers "|-\n"))
  14411. (insert-before-markers
  14412. "| " (int-to-string level) "|" hlc hdl hlc " |"
  14413. (make-string (1- level) ?|)
  14414. hlc
  14415. (format "%d:%02d" h m)
  14416. hlc
  14417. " |\n")))))
  14418. (goto-char ins)
  14419. (backward-delete-char 1)
  14420. (goto-char ipos)
  14421. (skip-chars-forward "^|")
  14422. (org-table-align)))
  14423. ;; FIXME: I don't think anybody uses this, ask David
  14424. (defun org-collect-clock-time-entries ()
  14425. "Return an internal list with clocking information.
  14426. This list has one entry for each CLOCK interval.
  14427. FIXME: describe the elements."
  14428. (interactive)
  14429. (let ((re (concat "^[ \t]*" org-clock-string
  14430. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  14431. rtn beg end next cont level title total closedp leafp
  14432. clockpos titlepos h m donep)
  14433. (save-excursion
  14434. (org-clock-sum)
  14435. (goto-char (point-min))
  14436. (while (re-search-forward re nil t)
  14437. (setq clockpos (match-beginning 0)
  14438. beg (match-string 1) end (match-string 2)
  14439. cont (match-end 0))
  14440. (setq beg (apply 'encode-time (org-parse-time-string beg))
  14441. end (apply 'encode-time (org-parse-time-string end)))
  14442. (org-back-to-heading t)
  14443. (setq donep (org-entry-is-done-p))
  14444. (setq titlepos (point)
  14445. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  14446. h (/ total 60) m (- total (* 60 h))
  14447. total (cons h m))
  14448. (looking-at "\\(\\*+\\) +\\(.*\\)")
  14449. (setq level (- (match-end 1) (match-beginning 1))
  14450. title (org-match-string-no-properties 2))
  14451. (save-excursion (outline-next-heading) (setq next (point)))
  14452. (setq closedp (re-search-forward org-closed-time-regexp next t))
  14453. (goto-char next)
  14454. (setq leafp (and (looking-at "^\\*+ ")
  14455. (<= (- (match-end 0) (point)) level)))
  14456. (push (list beg end clockpos closedp donep
  14457. total title titlepos level leafp)
  14458. rtn)
  14459. (goto-char cont)))
  14460. (nreverse rtn)))
  14461. ;;;; Agenda, and Diary Integration
  14462. ;;; Define the Org-agenda-mode
  14463. (defvar org-agenda-mode-map (make-sparse-keymap)
  14464. "Keymap for `org-agenda-mode'.")
  14465. (defvar org-agenda-menu) ; defined later in this file.
  14466. (defvar org-agenda-follow-mode nil)
  14467. (defvar org-agenda-show-log nil)
  14468. (defvar org-agenda-redo-command nil)
  14469. (defvar org-agenda-mode-hook nil)
  14470. (defvar org-agenda-type nil)
  14471. (defvar org-agenda-force-single-file nil)
  14472. (defun org-agenda-mode ()
  14473. "Mode for time-sorted view on action items in Org-mode files.
  14474. The following commands are available:
  14475. \\{org-agenda-mode-map}"
  14476. (interactive)
  14477. (kill-all-local-variables)
  14478. (setq org-agenda-undo-list nil
  14479. org-agenda-pending-undo-list nil)
  14480. (setq major-mode 'org-agenda-mode)
  14481. ;; Keep global-font-lock-mode from turning on font-lock-mode
  14482. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  14483. (setq mode-name "Org-Agenda")
  14484. (use-local-map org-agenda-mode-map)
  14485. (easy-menu-add org-agenda-menu)
  14486. (if org-startup-truncated (setq truncate-lines t))
  14487. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  14488. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  14489. ;; Make sure properties are removed when copying text
  14490. (when (boundp 'buffer-substring-filters)
  14491. (org-set-local 'buffer-substring-filters
  14492. (cons (lambda (x)
  14493. (set-text-properties 0 (length x) nil x) x)
  14494. buffer-substring-filters)))
  14495. (unless org-agenda-keep-modes
  14496. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  14497. org-agenda-show-log nil))
  14498. (easy-menu-change
  14499. '("Agenda") "Agenda Files"
  14500. (append
  14501. (list
  14502. (vector
  14503. (if (get 'org-agenda-files 'org-restrict)
  14504. "Restricted to single file"
  14505. "Edit File List")
  14506. '(org-edit-agenda-file-list)
  14507. (not (get 'org-agenda-files 'org-restrict)))
  14508. "--")
  14509. (mapcar 'org-file-menu-entry (org-agenda-files))))
  14510. (org-agenda-set-mode-name)
  14511. (apply
  14512. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  14513. (list 'org-agenda-mode-hook)))
  14514. (substitute-key-definition 'undo 'org-agenda-undo
  14515. org-agenda-mode-map global-map)
  14516. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  14517. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  14518. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  14519. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  14520. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  14521. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  14522. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  14523. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  14524. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  14525. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  14526. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  14527. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  14528. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  14529. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  14530. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  14531. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  14532. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  14533. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  14534. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  14535. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  14536. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  14537. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  14538. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  14539. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  14540. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  14541. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  14542. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  14543. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  14544. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  14545. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  14546. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  14547. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  14548. (while l (org-defkey org-agenda-mode-map
  14549. (int-to-string (pop l)) 'digit-argument)))
  14550. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  14551. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  14552. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  14553. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  14554. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  14555. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  14556. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  14557. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  14558. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  14559. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  14560. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  14561. (org-defkey org-agenda-mode-map "n" 'next-line)
  14562. (org-defkey org-agenda-mode-map "p" 'previous-line)
  14563. (org-defkey org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  14564. (org-defkey org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  14565. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  14566. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  14567. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  14568. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  14569. (eval-after-load "calendar"
  14570. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  14571. 'org-calendar-goto-agenda))
  14572. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  14573. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  14574. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  14575. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  14576. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  14577. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  14578. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  14579. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  14580. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  14581. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  14582. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  14583. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  14584. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  14585. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  14586. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  14587. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  14588. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  14589. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  14590. "Local keymap for agenda entries from Org-mode.")
  14591. (org-defkey org-agenda-keymap
  14592. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  14593. (org-defkey org-agenda-keymap
  14594. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  14595. (when org-agenda-mouse-1-follows-link
  14596. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  14597. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  14598. '("Agenda"
  14599. ("Agenda Files")
  14600. "--"
  14601. ["Show" org-agenda-show t]
  14602. ["Go To (other window)" org-agenda-goto t]
  14603. ["Go To (this window)" org-agenda-switch-to t]
  14604. ["Follow Mode" org-agenda-follow-mode
  14605. :style toggle :selected org-agenda-follow-mode :active t]
  14606. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  14607. "--"
  14608. ["Cycle TODO" org-agenda-todo t]
  14609. ["Archive subtree" org-agenda-archive t]
  14610. ["Delete subtree" org-agenda-kill t]
  14611. "--"
  14612. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  14613. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  14614. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  14615. "--"
  14616. ("Tags and Properties"
  14617. ["Show all Tags" org-agenda-show-tags t]
  14618. ["Set Tags" org-agenda-set-tags t]
  14619. "--"
  14620. ["Column View" org-columns t])
  14621. ("Date/Schedule"
  14622. ["Schedule" org-agenda-schedule t]
  14623. ["Set Deadline" org-agenda-deadline t]
  14624. "--"
  14625. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  14626. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  14627. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  14628. ("Priority"
  14629. ["Set Priority" org-agenda-priority t]
  14630. ["Increase Priority" org-agenda-priority-up t]
  14631. ["Decrease Priority" org-agenda-priority-down t]
  14632. ["Show Priority" org-agenda-show-priority t])
  14633. ("Calendar/Diary"
  14634. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  14635. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  14636. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  14637. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  14638. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  14639. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  14640. "--"
  14641. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  14642. "--"
  14643. ("View"
  14644. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  14645. :style radio :selected (equal org-agenda-ndays 1)]
  14646. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  14647. :style radio :selected (equal org-agenda-ndays 7)]
  14648. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  14649. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  14650. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  14651. :style radio :selected (member org-agenda-ndays '(365 366))]
  14652. "--"
  14653. ["Show Logbook entries" org-agenda-log-mode
  14654. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  14655. ["Include Diary" org-agenda-toggle-diary
  14656. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  14657. ["Use Time Grid" org-agenda-toggle-time-grid
  14658. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  14659. ["Write view to file" org-write-agenda t]
  14660. ["Rebuild buffer" org-agenda-redo t]
  14661. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  14662. "--"
  14663. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  14664. "--"
  14665. ["Quit" org-agenda-quit t]
  14666. ["Exit and Release Buffers" org-agenda-exit t]
  14667. ))
  14668. ;;; Agenda undo
  14669. (defvar org-agenda-allow-remote-undo t
  14670. "Non-nil means, allow remote undo from the agenda buffer.")
  14671. (defvar org-agenda-undo-list nil
  14672. "List of undoable operations in the agenda since last refresh.")
  14673. (defvar org-agenda-undo-has-started-in nil
  14674. "Buffers that have already seen `undo-start' in the current undo sequence.")
  14675. (defvar org-agenda-pending-undo-list nil
  14676. "In a series of undo commands, this is the list of remaning undo items.")
  14677. (defmacro org-if-unprotected (&rest body)
  14678. "Execute BODY if ther is no `org-protected' text property at point."
  14679. (declare (debug t))
  14680. `(unless (get-text-property (point) 'org-protected)
  14681. ,@body))
  14682. (defmacro org-with-remote-undo (_buffer &rest _body)
  14683. "Execute BODY while recording undo information in two buffers."
  14684. (declare (indent 1) (debug t))
  14685. `(let ((_cline (org-current-line))
  14686. (_cmd this-command)
  14687. (_buf1 (current-buffer))
  14688. (_buf2 ,_buffer)
  14689. (_undo1 buffer-undo-list)
  14690. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  14691. _c1 _c2)
  14692. ,@_body
  14693. (when org-agenda-allow-remote-undo
  14694. (setq _c1 (org-verify-change-for-undo
  14695. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  14696. _c2 (org-verify-change-for-undo
  14697. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  14698. (when (or _c1 _c2)
  14699. ;; make sure there are undo boundaries
  14700. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  14701. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  14702. ;; remember which buffer to undo
  14703. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  14704. org-agenda-undo-list)))))
  14705. (defun org-agenda-undo ()
  14706. "Undo a remote editing step in the agenda.
  14707. This undoes changes both in the agenda buffer and in the remote buffer
  14708. that have been changed along."
  14709. (interactive)
  14710. (or org-agenda-allow-remote-undo
  14711. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  14712. (if (not (eq this-command last-command))
  14713. (setq org-agenda-undo-has-started-in nil
  14714. org-agenda-pending-undo-list org-agenda-undo-list))
  14715. (if (not org-agenda-pending-undo-list)
  14716. (error "No further undo information"))
  14717. (let* ((entry (pop org-agenda-pending-undo-list))
  14718. buf line cmd rembuf)
  14719. (setq cmd (pop entry) line (pop entry))
  14720. (setq rembuf (nth 2 entry))
  14721. (org-with-remote-undo rembuf
  14722. (while (bufferp (setq buf (pop entry)))
  14723. (if (pop entry)
  14724. (with-current-buffer buf
  14725. (let ((last-undo-buffer buf)
  14726. buffer-read-only)
  14727. (unless (memq buf org-agenda-undo-has-started-in)
  14728. (push buf org-agenda-undo-has-started-in)
  14729. (make-local-variable 'pending-undo-list)
  14730. (undo-start))
  14731. (while (and pending-undo-list
  14732. (listp pending-undo-list)
  14733. (not (car pending-undo-list)))
  14734. (pop pending-undo-list))
  14735. (undo-more 1))))))
  14736. (goto-line line)
  14737. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  14738. (defun org-verify-change-for-undo (l1 l2)
  14739. "Verify that a real change occurred between the undo lists L1 and L2."
  14740. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  14741. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  14742. (not (eq l1 l2)))
  14743. ;;; Agenda dispatch
  14744. (defvar org-agenda-restrict nil)
  14745. (defvar org-agenda-restrict-begin (make-marker))
  14746. (defvar org-agenda-restrict-end (make-marker))
  14747. (defvar org-agenda-last-dispatch-buffer nil)
  14748. ;;;###autoload
  14749. (defun org-agenda (arg)
  14750. "Dispatch agenda commands to collect entries to the agenda buffer.
  14751. Prompts for a character to select a command. Any prefix arg will be passed
  14752. on to the selected command. The default selections are:
  14753. g
  14754. a Call `org-agenda-list' to display the agenda for current day or week.
  14755. t Call `org-todo-list' to display the global todo list.
  14756. T Call `org-todo-list' to display the global todo list, select only
  14757. entries with a specific TODO keyword (the user gets a prompt).
  14758. m Call `org-tags-view' to display headlines with tags matching
  14759. a condition (the user is prompted for the condition).
  14760. M Like `m', but select only TODO entries, no ordinary headlines.
  14761. l Create a timeline for the current buffer.
  14762. e Export views to associated files.
  14763. More commands can be added by configuring the variable
  14764. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  14765. searches can be pre-defined in this way.
  14766. If the current buffer is in Org-mode and visiting a file, you can also
  14767. first press `1' to indicate that the agenda should be temporarily (until the
  14768. next use of \\[org-agenda]) restricted to the current file."
  14769. (interactive "P")
  14770. (catch 'exit
  14771. (let* ((buf (current-buffer))
  14772. (bfn (buffer-file-name (buffer-base-buffer)))
  14773. (restrict-ok (and bfn (org-mode-p)))
  14774. (custom org-agenda-custom-commands)
  14775. c entry key type match lprops)
  14776. ;; Turn off restriction
  14777. (put 'org-agenda-files 'org-restrict nil)
  14778. (setq org-agenda-restrict nil)
  14779. (move-marker org-agenda-restrict-begin nil)
  14780. (move-marker org-agenda-restrict-end nil)
  14781. ;; Remember where this call originated
  14782. (setq org-agenda-last-dispatch-buffer (current-buffer))
  14783. (save-window-excursion
  14784. (delete-other-windows)
  14785. (switch-to-buffer-other-window " *Agenda Commands*")
  14786. (erase-buffer)
  14787. (insert (eval-when-compile
  14788. (let ((header
  14789. "Press key for an agenda command:
  14790. -------------------------------- C Configure custom agenda commands
  14791. a Agenda for current week or day e Export agenda views
  14792. t List of all TODO entries T Entries with special TODO kwd
  14793. m Match a TAGS query M Like m, but only TODO entries
  14794. L Timeline for current buffer # List stuck projects (!=configure)
  14795. ")
  14796. (start 0))
  14797. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  14798. (setq start (match-end 0))
  14799. (add-text-properties (match-beginning 2) (match-end 2)
  14800. '(face bold) header))
  14801. header)))
  14802. (while (setq entry (pop custom))
  14803. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  14804. (insert (format "\n%-4s%-14s: %s"
  14805. (org-add-props (copy-sequence key)
  14806. '(face bold))
  14807. (cond
  14808. ((stringp type) type)
  14809. ((eq type 'agenda) "Agenda for current week or day")
  14810. ((eq type 'alltodo) "List of all TODO entries")
  14811. ((eq type 'stuck) "List of stuck projects")
  14812. ((eq type 'todo) "TODO keyword")
  14813. ((eq type 'tags) "Tags query")
  14814. ((eq type 'tags-todo) "Tags (TODO)")
  14815. ((eq type 'tags-tree) "Tags tree")
  14816. ((eq type 'todo-tree) "TODO kwd tree")
  14817. ((eq type 'occur-tree) "Occur tree")
  14818. ((functionp type) (symbol-name type))
  14819. (t "???"))
  14820. (if (stringp match)
  14821. (org-add-props match nil 'face 'org-warning)
  14822. (format "set of %d commands" (length match))))))
  14823. (if restrict-ok
  14824. (insert "\n"
  14825. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  14826. (goto-char (point-min))
  14827. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  14828. (message "Press key for agenda command%s"
  14829. (if restrict-ok ", or [1] or [0] to restrict" ""))
  14830. (setq c (read-char-exclusive))
  14831. (message "")
  14832. (when (memq c '(?L ?1 ?0))
  14833. (if restrict-ok
  14834. (put 'org-agenda-files 'org-restrict (list bfn))
  14835. (error "Cannot restrict agenda to current buffer"))
  14836. (with-current-buffer " *Agenda Commands*"
  14837. (goto-char (point-max))
  14838. (delete-region (point-at-bol) (point))
  14839. (goto-char (point-min)))
  14840. (when (eq c ?0)
  14841. (setq org-agenda-restrict t)
  14842. (with-current-buffer buf
  14843. (if (org-region-active-p)
  14844. (progn
  14845. (move-marker org-agenda-restrict-begin (region-beginning))
  14846. (move-marker org-agenda-restrict-end (region-end)))
  14847. (save-excursion
  14848. (org-back-to-heading t)
  14849. (move-marker org-agenda-restrict-begin (point))
  14850. (move-marker org-agenda-restrict-end
  14851. (progn (org-end-of-subtree t)))))))
  14852. (unless (eq c ?L)
  14853. (message "Press key for agenda command%s"
  14854. (if restrict-ok " (restricted to current file)" ""))
  14855. (setq c (read-char-exclusive)))
  14856. (message "")))
  14857. (require 'calendar) ; FIXME: can we avoid this for some commands?
  14858. ;; For example the todo list should not need it (but does...)
  14859. (cond
  14860. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  14861. (if (symbolp (nth 1 entry))
  14862. (progn
  14863. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  14864. lprops (nth 3 entry))
  14865. (cond
  14866. ((eq type 'agenda)
  14867. (org-let lprops '(org-agenda-list current-prefix-arg)))
  14868. ((eq type 'alltodo)
  14869. (org-let lprops '(org-todo-list current-prefix-arg)))
  14870. ((eq type 'stuck)
  14871. (org-let lprops '(org-agenda-list-stuck-projects
  14872. current-prefix-arg)))
  14873. ((eq type 'tags)
  14874. (org-let lprops '(org-tags-view current-prefix-arg match)))
  14875. ((eq type 'tags-todo)
  14876. (org-let lprops '(org-tags-view '(4) match)))
  14877. ((eq type 'todo)
  14878. (org-let lprops '(org-todo-list match)))
  14879. ((eq type 'tags-tree)
  14880. (org-check-for-org-mode)
  14881. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  14882. ((eq type 'todo-tree)
  14883. (org-check-for-org-mode)
  14884. (org-let lprops
  14885. '(org-occur (concat "^" outline-regexp "[ \t]*"
  14886. (regexp-quote match) "\\>"))))
  14887. ((eq type 'occur-tree)
  14888. (org-check-for-org-mode)
  14889. (org-let lprops '(org-occur match)))
  14890. ((fboundp type)
  14891. (org-let lprops '(funcall type match)))
  14892. (t (error "Invalid custom agenda command type %s" type))))
  14893. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  14894. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  14895. ((equal c ?a) (call-interactively 'org-agenda-list))
  14896. ((equal c ?t) (call-interactively 'org-todo-list))
  14897. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  14898. ((equal c ?m) (call-interactively 'org-tags-view))
  14899. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  14900. ((equal c ?e) (call-interactively 'org-store-agenda-views))
  14901. ((equal c ?L)
  14902. (unless restrict-ok
  14903. (error "This is not an Org-mode file"))
  14904. (org-call-with-arg 'org-timeline arg))
  14905. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  14906. ((equal c ?!) (customize-variable 'org-stuck-projects))
  14907. (t (error "Invalid key"))))))
  14908. (defun org-run-agenda-series (name series)
  14909. (org-prepare-agenda name)
  14910. (let* ((org-agenda-multi t)
  14911. (redo (list 'org-run-agenda-series name (list 'quote series)))
  14912. (cmds (car series))
  14913. (gprops (nth 1 series))
  14914. match ;; The byte compiler incorrectly complains about this. Keep it!
  14915. cmd type lprops)
  14916. (while (setq cmd (pop cmds))
  14917. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  14918. (cond
  14919. ((eq type 'agenda)
  14920. (org-let2 gprops lprops
  14921. '(call-interactively 'org-agenda-list)))
  14922. ((eq type 'alltodo)
  14923. (org-let2 gprops lprops
  14924. '(call-interactively 'org-todo-list)))
  14925. ((eq type 'stuck)
  14926. (org-let2 gprops lprops
  14927. '(call-interactively 'org-agenda-list-stuck-projects)))
  14928. ((eq type 'tags)
  14929. (org-let2 gprops lprops
  14930. '(org-tags-view current-prefix-arg match)))
  14931. ((eq type 'tags-todo)
  14932. (org-let2 gprops lprops
  14933. '(org-tags-view '(4) match)))
  14934. ((eq type 'todo)
  14935. (org-let2 gprops lprops
  14936. '(org-todo-list match)))
  14937. ((fboundp type)
  14938. (org-let2 gprops lprops
  14939. '(funcall type match)))
  14940. (t (error "Invalid type in command series"))))
  14941. (widen)
  14942. (setq org-agenda-redo-command redo)
  14943. (goto-char (point-min)))
  14944. (org-finalize-agenda))
  14945. ;;;###autoload
  14946. (defmacro org-batch-agenda (cmd-key &rest parameters)
  14947. "Run an agenda command in batch mode and send the result to STDOUT.
  14948. If CMD-KEY is a string of length 1, it is used as a key in
  14949. `org-agenda-custom-commands' and triggers this command. If it is a
  14950. longer string is is used as a tags/todo match string.
  14951. Paramters are alternating variable names and values that will be bound
  14952. before running the agenda command."
  14953. (let (pars)
  14954. (while parameters
  14955. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  14956. (if (> (length cmd-key) 1)
  14957. (eval (list 'let (nreverse pars)
  14958. (list 'org-tags-view nil cmd-key)))
  14959. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  14960. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  14961. (set-buffer "*Org Agenda*")
  14962. (princ (org-encode-for-stdout (buffer-string)))))
  14963. (defun org-encode-for-stdout (string)
  14964. (if (fboundp 'encode-coding-string)
  14965. (encode-coding-string string buffer-file-coding-system)
  14966. string))
  14967. (defvar org-agenda-info nil)
  14968. ;;;###autoload
  14969. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  14970. "Run an agenda command in batch mode and send the result to STDOUT.
  14971. If CMD-KEY is a string of length 1, it is used as a key in
  14972. `org-agenda-custom-commands' and triggers this command. If it is a
  14973. longer string is is used as a tags/todo match string.
  14974. Paramters are alternating variable names and values that will be bound
  14975. before running the agenda command.
  14976. The output gives a line for each selected agenda item. Each
  14977. item is a list of comma-separated values, like this:
  14978. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  14979. category The category of the item
  14980. head The headline, without TODO kwd, TAGS and PRIORITY
  14981. type The type of the agenda entry, can be
  14982. todo selected in TODO match
  14983. tagsmatch selected in tags match
  14984. diary imported from diary
  14985. deadline a deadline on given date
  14986. scheduled scheduled on given date
  14987. timestamp entry has timestamp on given date
  14988. closed entry was closed on given date
  14989. upcoming-deadline warning about deadline
  14990. past-scheduled forwarded scheduled item
  14991. block entry has date block including g. date
  14992. todo The todo keyword, if any
  14993. tags All tags including inherited ones, separated by colons
  14994. date The relevant date, like 2007-2-14
  14995. time The time, like 15:00-16:50
  14996. extra Sting with extra planning info
  14997. priority-l The priority letter if any was given
  14998. priority-n The computed numerical priority
  14999. agenda-day The day in the agenda where this is listed"
  15000. (let (pars)
  15001. (while parameters
  15002. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15003. (push (list 'org-agenda-remove-tags t) pars)
  15004. (if (> (length cmd-key) 1)
  15005. (eval (list 'let (nreverse pars)
  15006. (list 'org-tags-view nil cmd-key)))
  15007. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  15008. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  15009. (set-buffer "*Org Agenda*")
  15010. (let* ((lines (org-split-string (buffer-string) "\n"))
  15011. line)
  15012. (while (setq line (pop lines))
  15013. (catch 'next
  15014. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  15015. (setq org-agenda-info
  15016. (org-fix-agenda-info (text-properties-at 0 line)))
  15017. (princ
  15018. (org-encode-for-stdout
  15019. (mapconcat 'org-agenda-export-csv-mapper
  15020. '(org-category txt type todo tags date time-of-day extra
  15021. priority-letter priority agenda-day)
  15022. ",")))
  15023. (princ "\n"))))))
  15024. (defun org-fix-agenda-info (props)
  15025. "FIXME"
  15026. (let (tmp re)
  15027. (when (setq tmp (plist-get props 'tags))
  15028. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  15029. (when (setq tmp (plist-get props 'date))
  15030. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  15031. (let ((calendar-date-display-form '(year "-" month "-" day)))
  15032. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  15033. (setq tmp (calendar-date-string tmp)))
  15034. (setq props (plist-put props 'date tmp)))
  15035. (when (setq tmp (plist-get props 'day))
  15036. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  15037. (let ((calendar-date-display-form '(year "-" month "-" day)))
  15038. (setq tmp (calendar-date-string tmp)))
  15039. (setq props (plist-put props 'day tmp))
  15040. (setq props (plist-put props 'agenda-day tmp)))
  15041. (when (setq tmp (plist-get props 'txt))
  15042. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  15043. (plist-put props 'priority-letter (match-string 1 tmp))
  15044. (setq tmp (replace-match "" t t tmp)))
  15045. (when (and (setq re (plist-get props 'org-todo-regexp))
  15046. (setq re (concat "\\`\\.*" re " ?"))
  15047. (string-match re tmp))
  15048. (plist-put props 'todo (match-string 1 tmp))
  15049. (setq tmp (replace-match "" t t tmp)))
  15050. (plist-put props 'txt tmp)))
  15051. props)
  15052. (defun org-agenda-export-csv-mapper (prop)
  15053. (let ((res (plist-get org-agenda-info prop)))
  15054. (setq res
  15055. (cond
  15056. ((not res) "")
  15057. ((stringp res) res)
  15058. (t (prin1-to-string res))))
  15059. (while (string-match "," res)
  15060. (setq res (replace-match ";" t t res)))
  15061. (org-trim res)))
  15062. ;;;###autoload
  15063. (defun org-store-agenda-views (&rest parameters)
  15064. (interactive)
  15065. (eval (list 'org-batch-store-agenda-views)))
  15066. (defvar org-agenda-buffer-name)
  15067. ;; FIXME, why is this a macro?????
  15068. ;;;###autoload
  15069. (defmacro org-batch-store-agenda-views (&rest parameters)
  15070. "Run all custom agenda commands that have a file argument."
  15071. (let ((cmds org-agenda-custom-commands)
  15072. (dir (default-directory))
  15073. pars cmd thiscmdkey files opts)
  15074. (while parameters
  15075. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15076. (setq pars (reverse pars))
  15077. (save-window-excursion
  15078. (while cmds
  15079. (setq cmd (pop cmds)
  15080. thiscmdkey (car cmd)
  15081. opts (nth 3 cmd)
  15082. files (org-last cmd))
  15083. (if (stringp files) (setq files (list files)))
  15084. (when files
  15085. (flet ((read-char-exclusive () (string-to-char thiscmdkey)))
  15086. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  15087. '(org-agenda nil))))
  15088. (set-buffer "*Org Agenda*")
  15089. (while files
  15090. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  15091. (list 'org-write-agenda
  15092. (expand-file-name (pop files) dir) t)))))
  15093. (kill-buffer org-agenda-buffer-name)))))
  15094. (defun org-write-agenda (file &optional nosettings)
  15095. "Write the current buffer (an agenda view) as a file.
  15096. Depending on the extension of the file name, plain text (.txt),
  15097. HTML (.html or .htm) or Postscript (.ps) is produced.
  15098. If NOSETTINGS is given, do not scope the settings of
  15099. `org-agenda-exporter-settings' into the export commands. This is used when
  15100. the settings have already been scoped and we do not wish to overrule other,
  15101. higher priority settings."
  15102. (interactive "FWrite agenda to file: ")
  15103. (if (not (file-writable-p file))
  15104. (error "Cannot write agenda to file %s" file))
  15105. (cond
  15106. ((string-match "\\.html?\\'" file) (require 'htmlize))
  15107. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  15108. (org-let (if nosettings nil org-agenda-exporter-settings)
  15109. '(save-excursion
  15110. (save-window-excursion
  15111. (cond
  15112. ((string-match "\\.html?\\'" file)
  15113. (set-buffer (htmlize-buffer (current-buffer)))
  15114. (when (and org-agenda-export-html-style
  15115. (string-match "<style>" org-agenda-export-html-style))
  15116. ;; replace <style> section with org-agenda-export-html-style
  15117. (goto-char (point-min))
  15118. (kill-region (- (search-forward "<style") 6)
  15119. (search-forward "</style>"))
  15120. (insert org-agenda-export-html-style))
  15121. (write-file file)
  15122. (kill-buffer (current-buffer))
  15123. (message "HTML written to %s" file))
  15124. ((string-match "\\.ps\\'" file)
  15125. (ps-print-buffer-with-faces file)
  15126. (message "Postscript written to %s" file))
  15127. (t
  15128. (let ((bs (buffer-string)))
  15129. (find-file file)
  15130. (insert bs)
  15131. (save-buffer 0)
  15132. (kill-buffer (current-buffer))
  15133. (message "Plain text written to %s" file))))))
  15134. (set-buffer org-agenda-buffer-name)))
  15135. (defmacro org-no-read-only (&rest body)
  15136. "Inhibit read-only for BODY."
  15137. `(let ((inhibit-read-only t)) ,@body))
  15138. (defun org-check-for-org-mode ()
  15139. "Make sure current buffer is in org-mode. Error if not."
  15140. (or (org-mode-p)
  15141. (error "Cannot execute org-mode agenda command on buffer in %s."
  15142. major-mode)))
  15143. (defun org-fit-agenda-window ()
  15144. "Fit the window to the buffer size."
  15145. (and (memq org-agenda-window-setup '(reorganize-frame))
  15146. (fboundp 'fit-window-to-buffer)
  15147. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  15148. (/ (frame-height) 2))))
  15149. ;;; Agenda file list
  15150. (defun org-agenda-files (&optional unrestricted)
  15151. "Get the list of agenda files.
  15152. Optional UNRESTRICTED means return the full list even if a restriction
  15153. is currently in place."
  15154. (cond
  15155. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15156. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15157. ((listp org-agenda-files) org-agenda-files)
  15158. (t (error "Invalid value of `org-agenda-files'"))))
  15159. (defun org-edit-agenda-file-list ()
  15160. "Edit the list of agenda files.
  15161. Depending on setup, this either uses customize to edit the variable
  15162. `org-agenda-files', or it visits the file that is holding the list. In the
  15163. latter case, the buffer is set up in a way that saving it automatically kills
  15164. the buffer and restores the previous window configuration."
  15165. (interactive)
  15166. (if (stringp org-agenda-files)
  15167. (let ((cw (current-window-configuration)))
  15168. (find-file org-agenda-files)
  15169. (org-set-local 'org-window-configuration cw)
  15170. (org-add-hook 'after-save-hook
  15171. (lambda ()
  15172. (set-window-configuration
  15173. (prog1 org-window-configuration
  15174. (kill-buffer (current-buffer))))
  15175. (org-install-agenda-files-menu)
  15176. (message "New agenda file list installed"))
  15177. nil 'local)
  15178. (message (substitute-command-keys
  15179. "Edit list and finish with \\[save-buffer]")))
  15180. (customize-variable 'org-agenda-files)))
  15181. (defun org-store-new-agenda-file-list (list)
  15182. "Set new value for the agenda file list and save it correcly."
  15183. (if (stringp org-agenda-files)
  15184. (let ((f org-agenda-files) b)
  15185. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  15186. (with-temp-file f
  15187. (insert (mapconcat 'identity list "\n") "\n")))
  15188. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  15189. (setq org-agenda-files list)
  15190. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15191. (defun org-read-agenda-file-list ()
  15192. "Read the list of agenda files from a file."
  15193. (when (stringp org-agenda-files)
  15194. (with-temp-buffer
  15195. (insert-file-contents org-agenda-files)
  15196. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  15197. ;;;###autoload
  15198. (defun org-cycle-agenda-files ()
  15199. "Cycle through the files in `org-agenda-files'.
  15200. If the current buffer visits an agenda file, find the next one in the list.
  15201. If the current buffer does not, find the first agenda file."
  15202. (interactive)
  15203. (let* ((fs (org-agenda-files t))
  15204. (files (append fs (list (car fs))))
  15205. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15206. file)
  15207. (unless files (error "No agenda files"))
  15208. (catch 'exit
  15209. (while (setq file (pop files))
  15210. (if (equal (file-truename file) tcf)
  15211. (when (car files)
  15212. (find-file (car files))
  15213. (throw 'exit t))))
  15214. (find-file (car fs)))
  15215. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  15216. (defun org-agenda-file-to-front (&optional to-end)
  15217. "Move/add the current file to the top of the agenda file list.
  15218. If the file is not present in the list, it is added to the front. If it is
  15219. present, it is moved there. With optional argument TO-END, add/move to the
  15220. end of the list."
  15221. (interactive "P")
  15222. (let ((file-alist (mapcar (lambda (x)
  15223. (cons (file-truename x) x))
  15224. (org-agenda-files t)))
  15225. (ctf (file-truename buffer-file-name))
  15226. x had)
  15227. (setq x (assoc ctf file-alist) had x)
  15228. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15229. (if to-end
  15230. (setq file-alist (append (delq x file-alist) (list x)))
  15231. (setq file-alist (cons x (delq x file-alist))))
  15232. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15233. (org-install-agenda-files-menu)
  15234. (message "File %s to %s of agenda file list"
  15235. (if had "moved" "added") (if to-end "end" "front"))))
  15236. (defun org-remove-file (&optional file)
  15237. "Remove current file from the list of files in variable `org-agenda-files'.
  15238. These are the files which are being checked for agenda entries.
  15239. Optional argument FILE means, use this file instead of the current."
  15240. (interactive)
  15241. (let* ((file (or file buffer-file-name))
  15242. (true-file (file-truename file))
  15243. (afile (abbreviate-file-name file))
  15244. (files (delq nil (mapcar
  15245. (lambda (x)
  15246. (if (equal true-file
  15247. (file-truename x))
  15248. nil x))
  15249. (org-agenda-files t)))))
  15250. (if (not (= (length files) (length (org-agenda-files t))))
  15251. (progn
  15252. (org-store-new-agenda-file-list files)
  15253. (org-install-agenda-files-menu)
  15254. (message "Removed file: %s" afile))
  15255. (message "File was not in list: %s" afile))))
  15256. (defun org-file-menu-entry (file)
  15257. (vector file (list 'find-file file) t))
  15258. (defun org-check-agenda-file (file)
  15259. "Make sure FILE exists. If not, ask user what to do."
  15260. (when (not (file-exists-p file))
  15261. (message "non-existent file %s. [R]emove from list or [A]bort?"
  15262. (abbreviate-file-name file))
  15263. (let ((r (downcase (read-char-exclusive))))
  15264. (cond
  15265. ((equal r ?r)
  15266. (org-remove-file file)
  15267. (throw 'nextfile t))
  15268. (t (error "Abort"))))))
  15269. ;;; Agenda prepare and finalize
  15270. (defvar org-agenda-multi nil) ; dynammically scoped
  15271. (defvar org-agenda-buffer-name "*Org Agenda*")
  15272. (defvar org-pre-agenda-window-conf nil)
  15273. (defvar org-agenda-name nil)
  15274. (defun org-prepare-agenda (&optional name)
  15275. (setq org-todo-keywords-for-agenda nil)
  15276. (setq org-done-keywords-for-agenda nil)
  15277. (if org-agenda-multi
  15278. (progn
  15279. (setq buffer-read-only nil)
  15280. (goto-char (point-max))
  15281. (unless (= (point) 1)
  15282. (insert "\n" (make-string (window-width) ?=) "\n"))
  15283. (narrow-to-region (point) (point-max)))
  15284. (org-agenda-maybe-reset-markers 'force)
  15285. (org-prepare-agenda-buffers (org-agenda-files))
  15286. (setq org-todo-keywords-for-agenda
  15287. (org-uniquify org-todo-keywords-for-agenda))
  15288. (setq org-done-keywords-for-agenda
  15289. (org-uniquify org-done-keywords-for-agenda))
  15290. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  15291. (awin (get-buffer-window abuf)))
  15292. (cond
  15293. ((equal (current-buffer) abuf) nil)
  15294. (awin (select-window awin))
  15295. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  15296. ((equal org-agenda-window-setup 'current-window)
  15297. (switch-to-buffer abuf))
  15298. ((equal org-agenda-window-setup 'other-window)
  15299. (switch-to-buffer-other-window abuf))
  15300. ((equal org-agenda-window-setup 'other-frame)
  15301. (switch-to-buffer-other-frame abuf))
  15302. ((equal org-agenda-window-setup 'reorganize-frame)
  15303. (delete-other-windows)
  15304. (switch-to-buffer-other-window abuf))))
  15305. (setq buffer-read-only nil)
  15306. (erase-buffer)
  15307. (org-agenda-mode)
  15308. (and name (not org-agenda-name)
  15309. (org-set-local 'org-agenda-name name)))
  15310. (setq buffer-read-only nil))
  15311. (defun org-finalize-agenda ()
  15312. "Finishing touch for the agenda buffer, called just before displaying it."
  15313. (unless org-agenda-multi
  15314. (save-excursion
  15315. (let ((buffer-read-only))
  15316. (goto-char (point-min))
  15317. (while (org-activate-bracket-links (point-max))
  15318. (add-text-properties (match-beginning 0) (match-end 0)
  15319. '(face org-link)))
  15320. (org-agenda-align-tags)
  15321. (unless org-agenda-with-colors
  15322. (remove-text-properties (point-min) (point-max) '(face nil))))
  15323. (if (and (boundp 'org-overriding-columns-format)
  15324. org-overriding-columns-format)
  15325. (org-set-local 'org-overriding-columns-format
  15326. org-overriding-columns-format))
  15327. (if (and (boundp 'org-agenda-view-columns-initially)
  15328. org-agenda-view-columns-initially)
  15329. (org-agenda-columns))
  15330. (run-hooks 'org-finalize-agenda-hook))))
  15331. (defun org-prepare-agenda-buffers (files)
  15332. "Create buffers for all agenda files, protect archived trees and comments."
  15333. (interactive)
  15334. (let ((pa '(:org-archived t))
  15335. (pc '(:org-comment t))
  15336. (pall '(:org-archived t :org-comment t))
  15337. (rea (concat ":" org-archive-tag ":"))
  15338. bmp file re)
  15339. (save-excursion
  15340. (save-restriction
  15341. (while (setq file (pop files))
  15342. (org-check-agenda-file file)
  15343. (set-buffer (org-get-agenda-file-buffer file))
  15344. (widen)
  15345. (setq bmp (buffer-modified-p))
  15346. (setq org-todo-keywords-for-agenda
  15347. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15348. (setq org-done-keywords-for-agenda
  15349. (append org-done-keywords-for-agenda org-done-keywords))
  15350. (save-excursion
  15351. (remove-text-properties (point-min) (point-max) pall)
  15352. (when org-agenda-skip-archived-trees
  15353. (goto-char (point-min))
  15354. (while (re-search-forward rea nil t)
  15355. (if (org-on-heading-p t)
  15356. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15357. (goto-char (point-min))
  15358. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  15359. (while (re-search-forward re nil t)
  15360. (add-text-properties
  15361. (match-beginning 0) (org-end-of-subtree t) pc)))
  15362. (set-buffer-modified-p bmp))))))
  15363. (defvar org-agenda-skip-function nil
  15364. "Function to be called at each match during agenda construction.
  15365. If this function return nil, the current match should not be skipped.
  15366. Otherwise, the function must return a position from where the search
  15367. should be continued.
  15368. Never set this variable using `setq' or so, because then it will apply
  15369. to all future agenda commands. Instead, bind it with `let' to scope
  15370. it dynamically into the agenda-constructing command.")
  15371. (defun org-agenda-skip ()
  15372. "Throw to `:skip' in places that should be skipped.
  15373. Also moves point to the end of the skipped region, so that search can
  15374. continue from there."
  15375. (let ((p (point-at-bol)) to)
  15376. (and org-agenda-skip-archived-trees
  15377. (get-text-property p :org-archived)
  15378. (org-end-of-subtree t)
  15379. (throw :skip t))
  15380. (and (get-text-property p :org-comment)
  15381. (org-end-of-subtree t)
  15382. (throw :skip t))
  15383. (if (equal (char-after p) ?#) (throw :skip t))
  15384. (when (and (functionp org-agenda-skip-function)
  15385. (setq to (save-excursion
  15386. (save-match-data
  15387. (funcall org-agenda-skip-function)))))
  15388. (goto-char to)
  15389. (throw :skip t))))
  15390. (defvar org-agenda-markers nil
  15391. "List of all currently active markers created by `org-agenda'.")
  15392. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  15393. "Creation time of the last agenda marker.")
  15394. (defun org-agenda-new-marker (&optional pos)
  15395. "Return a new agenda marker.
  15396. Org-mode keeps a list of these markers and resets them when they are
  15397. no longer in use."
  15398. (let ((m (copy-marker (or pos (point)))))
  15399. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  15400. (push m org-agenda-markers)
  15401. m))
  15402. (defun org-agenda-maybe-reset-markers (&optional force)
  15403. "Reset markers created by `org-agenda'. But only if they are old enough."
  15404. (if (or (and force (not org-agenda-multi))
  15405. (> (- (time-to-seconds (current-time))
  15406. org-agenda-last-marker-time)
  15407. 5))
  15408. (while org-agenda-markers
  15409. (move-marker (pop org-agenda-markers) nil))))
  15410. (defvar org-agenda-new-buffers nil
  15411. "Buffers created to visit agenda files.")
  15412. (defun org-get-agenda-file-buffer (file)
  15413. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15414. it to the list of buffers which might be released later."
  15415. (let ((buf (org-find-base-buffer-visiting file)))
  15416. (if buf
  15417. buf ; just return it
  15418. ;; Make a new buffer and remember it
  15419. (setq buf (find-file-noselect file))
  15420. (if buf (push buf org-agenda-new-buffers))
  15421. buf)))
  15422. (defun org-release-buffers (blist)
  15423. "Release all buffers in list, asking the user for confirmation when needed.
  15424. When a buffer is unmodified, it is just killed. When modified, it is saved
  15425. \(if the user agrees) and then killed."
  15426. (let (buf file)
  15427. (while (setq buf (pop blist))
  15428. (setq file (buffer-file-name buf))
  15429. (when (and (buffer-modified-p buf)
  15430. file
  15431. (y-or-n-p (format "Save file %s? " file)))
  15432. (with-current-buffer buf (save-buffer)))
  15433. (kill-buffer buf))))
  15434. (defvar org-category-table nil)
  15435. (defun org-get-category-table ()
  15436. "Get the table of categories and positions in current buffer."
  15437. (let (tbl)
  15438. (save-excursion
  15439. (save-restriction
  15440. (widen)
  15441. (goto-char (point-min))
  15442. (while (re-search-forward "^#\\+CATEGORY:[ \t]*\\(.*\\)"
  15443. nil t)
  15444. (push (cons (match-beginning 1)
  15445. (org-trim (match-string 1))) tbl))))
  15446. tbl))
  15447. (defun org-get-category (&optional pos)
  15448. "Get the category applying to position POS."
  15449. (if (not org-category-table)
  15450. (cond
  15451. ((null org-category)
  15452. (setq org-category
  15453. (if buffer-file-name
  15454. (file-name-sans-extension
  15455. (file-name-nondirectory buffer-file-name))
  15456. "???")))
  15457. ((symbolp org-category) (symbol-name org-category))
  15458. (t org-category))
  15459. (let ((tbl org-category-table)
  15460. (pos (or pos (point))))
  15461. (while (and tbl (> (caar tbl) pos))
  15462. (pop tbl))
  15463. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  15464. org-category-table))))))
  15465. ;;; Agenda timeline
  15466. (defun org-timeline (&optional include-all)
  15467. "Show a time-sorted view of the entries in the current org file.
  15468. Only entries with a time stamp of today or later will be listed. With
  15469. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  15470. under the current date.
  15471. If the buffer contains an active region, only check the region for
  15472. dates."
  15473. (interactive "P")
  15474. (require 'calendar)
  15475. (org-compile-prefix-format 'timeline)
  15476. (org-set-sorting-strategy 'timeline)
  15477. (let* ((dopast t)
  15478. (dotodo include-all)
  15479. (doclosed org-agenda-show-log)
  15480. (entry buffer-file-name)
  15481. (date (calendar-current-date))
  15482. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  15483. (end (if (org-region-active-p) (region-end) (point-max)))
  15484. (day-numbers (org-get-all-dates beg end 'no-ranges
  15485. t doclosed ; always include today
  15486. org-timeline-show-empty-dates))
  15487. (today (time-to-days (current-time)))
  15488. (past t)
  15489. args
  15490. s e rtn d emptyp)
  15491. (setq org-agenda-redo-command
  15492. (list 'progn
  15493. (list 'switch-to-buffer-other-window (current-buffer))
  15494. (list 'org-timeline (list 'quote include-all))))
  15495. (if (not dopast)
  15496. ;; Remove past dates from the list of dates.
  15497. (setq day-numbers (delq nil (mapcar (lambda(x)
  15498. (if (>= x today) x nil))
  15499. day-numbers))))
  15500. (org-prepare-agenda (concat "Timeline "
  15501. (file-name-nondirectory buffer-file-name)))
  15502. (if doclosed (push :closed args))
  15503. (push :timestamp args)
  15504. (push :sexp args)
  15505. (if dotodo (push :todo args))
  15506. (while (setq d (pop day-numbers))
  15507. (if (and (listp d) (eq (car d) :omitted))
  15508. (progn
  15509. (setq s (point))
  15510. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  15511. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  15512. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  15513. (if (and (>= d today)
  15514. dopast
  15515. past)
  15516. (progn
  15517. (setq past nil)
  15518. (insert (make-string 79 ?-) "\n")))
  15519. (setq date (calendar-gregorian-from-absolute d))
  15520. (setq s (point))
  15521. (setq rtn (and (not emptyp)
  15522. (apply 'org-agenda-get-day-entries
  15523. entry date args)))
  15524. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  15525. (progn
  15526. (insert (calendar-day-name date) " "
  15527. (number-to-string (extract-calendar-day date)) " "
  15528. (calendar-month-name (extract-calendar-month date)) " "
  15529. (number-to-string (extract-calendar-year date)) "\n")
  15530. ; FIXME: this gives a timezone problem
  15531. ; (insert (format-time-string org-agenda-date-format
  15532. ; (calendar-time-from-absolute d 0))
  15533. ; "\n")
  15534. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  15535. (put-text-property s (1- (point)) 'org-date-line t)
  15536. (if (equal d today)
  15537. (put-text-property s (1- (point)) 'org-today t))
  15538. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  15539. (put-text-property s (1- (point)) 'day d)))))
  15540. (goto-char (point-min))
  15541. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  15542. (point-min)))
  15543. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  15544. (org-finalize-agenda)
  15545. (setq buffer-read-only t)))
  15546. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  15547. "Return a list of all relevant day numbers from BEG to END buffer positions.
  15548. If NO-RANGES is non-nil, include only the start and end dates of a range,
  15549. not every single day in the range. If FORCE-TODAY is non-nil, make
  15550. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  15551. inactive time stamps (those in square brackets) are included.
  15552. When EMPTY is non-nil, also include days without any entries."
  15553. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  15554. dates dates1 date day day1 day2 ts1 ts2)
  15555. (if force-today
  15556. (setq dates (list (time-to-days (current-time)))))
  15557. (save-excursion
  15558. (goto-char beg)
  15559. (while (re-search-forward re end t)
  15560. (setq day (time-to-days (org-time-string-to-time
  15561. (substring (match-string 1) 0 10))))
  15562. (or (memq day dates) (push day dates)))
  15563. (unless no-ranges
  15564. (goto-char beg)
  15565. (while (re-search-forward org-tr-regexp end t)
  15566. (setq ts1 (substring (match-string 1) 0 10)
  15567. ts2 (substring (match-string 2) 0 10)
  15568. day1 (time-to-days (org-time-string-to-time ts1))
  15569. day2 (time-to-days (org-time-string-to-time ts2)))
  15570. (while (< (setq day1 (1+ day1)) day2)
  15571. (or (memq day1 dates) (push day1 dates)))))
  15572. (setq dates (sort dates '<))
  15573. (when empty
  15574. (while (setq day (pop dates))
  15575. (setq day2 (car dates))
  15576. (push day dates1)
  15577. (when (and day2 empty)
  15578. (if (or (eq empty t)
  15579. (and (numberp empty) (<= (- day2 day) empty)))
  15580. (while (< (setq day (1+ day)) day2)
  15581. (push (list day) dates1))
  15582. (push (cons :omitted (- day2 day)) dates1))))
  15583. (setq dates (nreverse dates1)))
  15584. dates)))
  15585. ;;; Agenda Daily/Weekly
  15586. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  15587. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  15588. (defvar org-agenda-last-arguments nil
  15589. "The arguments of the previous call to org-agenda")
  15590. (defvar org-starting-day nil) ; local variable in the agenda buffer
  15591. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  15592. (defvar org-include-all-loc nil) ; local variable
  15593. ;;;###autoload
  15594. (defun org-agenda-list (&optional include-all start-day ndays)
  15595. "Produce a weekly view from all files in variable `org-agenda-files'.
  15596. The view will be for the current week, but from the overview buffer you
  15597. will be able to go to other weeks.
  15598. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  15599. also be shown, under the current date.
  15600. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  15601. on the days are also shown. See the variable `org-log-done' for how
  15602. to turn on logging.
  15603. START-DAY defaults to TODAY, or to the most recent match for the weekday
  15604. given in `org-agenda-start-on-weekday'.
  15605. NDAYS defaults to `org-agenda-ndays'."
  15606. (interactive "P")
  15607. (setq ndays (or ndays org-agenda-ndays)
  15608. start-day (or start-day org-agenda-start-day))
  15609. (if org-agenda-overriding-arguments
  15610. (setq include-all (car org-agenda-overriding-arguments)
  15611. start-day (nth 1 org-agenda-overriding-arguments)
  15612. ndays (nth 2 org-agenda-overriding-arguments)))
  15613. (if (stringp start-day)
  15614. ;; Convert to an absolute day number
  15615. (setq start-day (time-to-days (org-read-date nil t start-day))))
  15616. (setq org-agenda-last-arguments (list include-all start-day ndays))
  15617. (org-compile-prefix-format 'agenda)
  15618. (org-set-sorting-strategy 'agenda)
  15619. (require 'calendar)
  15620. (let* ((org-agenda-start-on-weekday
  15621. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  15622. org-agenda-start-on-weekday nil))
  15623. (thefiles (org-agenda-files))
  15624. (files thefiles)
  15625. (today (time-to-days (current-time)))
  15626. (sd (or start-day today))
  15627. (start (if (or (null org-agenda-start-on-weekday)
  15628. (< org-agenda-ndays 7))
  15629. sd
  15630. (let* ((nt (calendar-day-of-week
  15631. (calendar-gregorian-from-absolute sd)))
  15632. (n1 org-agenda-start-on-weekday)
  15633. (d (- nt n1)))
  15634. (- sd (+ (if (< d 0) 7 0) d)))))
  15635. (day-numbers (list start))
  15636. (inhibit-redisplay (not debug-on-error))
  15637. s e rtn rtnall file date d start-pos end-pos todayp nd)
  15638. (setq org-agenda-redo-command
  15639. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  15640. ;; Make the list of days
  15641. (setq ndays (or ndays org-agenda-ndays)
  15642. nd ndays)
  15643. (while (> ndays 1)
  15644. (push (1+ (car day-numbers)) day-numbers)
  15645. (setq ndays (1- ndays)))
  15646. (setq day-numbers (nreverse day-numbers))
  15647. (org-prepare-agenda "Day/Week")
  15648. (org-set-local 'org-starting-day (car day-numbers))
  15649. (org-set-local 'org-include-all-loc include-all)
  15650. (org-set-local 'org-agenda-span
  15651. (org-agenda-ndays-to-span nd))
  15652. (when (and (or include-all org-agenda-include-all-todo)
  15653. (member today day-numbers))
  15654. (setq files thefiles
  15655. rtnall nil)
  15656. (while (setq file (pop files))
  15657. (catch 'nextfile
  15658. (org-check-agenda-file file)
  15659. (setq date (calendar-gregorian-from-absolute today)
  15660. rtn (org-agenda-get-day-entries
  15661. file date :todo))
  15662. (setq rtnall (append rtnall rtn))))
  15663. (when rtnall
  15664. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  15665. (add-text-properties (point-min) (1- (point))
  15666. (list 'face 'org-agenda-structure))
  15667. (insert (org-finalize-agenda-entries rtnall) "\n")))
  15668. (setq s (point))
  15669. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  15670. "-agenda:\n")
  15671. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  15672. 'org-date-line t))
  15673. (while (setq d (pop day-numbers))
  15674. (setq date (calendar-gregorian-from-absolute d)
  15675. s (point))
  15676. (if (or (setq todayp (= d today))
  15677. (and (not start-pos) (= d sd)))
  15678. (setq start-pos (point))
  15679. (if (and start-pos (not end-pos))
  15680. (setq end-pos (point))))
  15681. (setq files thefiles
  15682. rtnall nil)
  15683. (while (setq file (pop files))
  15684. (catch 'nextfile
  15685. (org-check-agenda-file file)
  15686. (if org-agenda-show-log
  15687. (setq rtn (org-agenda-get-day-entries
  15688. file date
  15689. :deadline :scheduled :timestamp :sexp :closed))
  15690. (setq rtn (org-agenda-get-day-entries
  15691. file date
  15692. :deadline :scheduled :sexp :timestamp)))
  15693. (setq rtnall (append rtnall rtn))))
  15694. (if org-agenda-include-diary
  15695. (progn
  15696. (require 'diary-lib)
  15697. (setq rtn (org-get-entries-from-diary date))
  15698. (setq rtnall (append rtnall rtn))))
  15699. (if (or rtnall org-agenda-show-all-dates)
  15700. (progn
  15701. (insert (format "%-9s %2d %s %4d\n"
  15702. (calendar-day-name date)
  15703. (extract-calendar-day date)
  15704. (calendar-month-name (extract-calendar-month date))
  15705. (extract-calendar-year date)))
  15706. ; FIXME: this gives a timezone problem
  15707. ; (insert (format-time-string org-agenda-date-format
  15708. ; (calendar-time-from-absolute d 0)) "\n")
  15709. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  15710. (put-text-property s (1- (point)) 'org-date-line t)
  15711. (if todayp (put-text-property s (1- (point)) 'org-today t))
  15712. (if rtnall (insert
  15713. (org-finalize-agenda-entries
  15714. (org-agenda-add-time-grid-maybe
  15715. rtnall nd todayp))
  15716. "\n"))
  15717. (put-text-property s (1- (point)) 'day d))))
  15718. (goto-char (point-min))
  15719. (org-fit-agenda-window)
  15720. (unless (and (pos-visible-in-window-p (point-min))
  15721. (pos-visible-in-window-p (point-max)))
  15722. (goto-char (1- (point-max)))
  15723. (recenter -1)
  15724. (if (not (pos-visible-in-window-p (or start-pos 1)))
  15725. (progn
  15726. (goto-char (or start-pos 1))
  15727. (recenter 1))))
  15728. (goto-char (or start-pos 1))
  15729. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  15730. (org-finalize-agenda)
  15731. (setq buffer-read-only t)
  15732. (message "")))
  15733. (defun org-agenda-ndays-to-span (n)
  15734. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  15735. ;;; Agenda TODO list
  15736. (defvar org-select-this-todo-keyword nil)
  15737. (defvar org-last-arg nil)
  15738. ;;;###autoload
  15739. (defun org-todo-list (arg)
  15740. "Show all TODO entries from all agenda file in a single list.
  15741. The prefix arg can be used to select a specific TODO keyword and limit
  15742. the list to these. When using \\[universal-argument], you will be prompted
  15743. for a keyword. A numeric prefix directly selects the Nth keyword in
  15744. `org-todo-keywords-1'."
  15745. (interactive "P")
  15746. (require 'calendar)
  15747. (org-compile-prefix-format 'todo)
  15748. (org-set-sorting-strategy 'todo)
  15749. (org-prepare-agenda "TODO")
  15750. (let* ((today (time-to-days (current-time)))
  15751. (date (calendar-gregorian-from-absolute today))
  15752. (kwds org-todo-keywords-for-agenda)
  15753. (completion-ignore-case t)
  15754. (org-select-this-todo-keyword
  15755. (if (stringp arg) arg
  15756. (and arg (integerp arg) (> arg 0)
  15757. (nth (1- arg) kwds))))
  15758. rtn rtnall files file pos)
  15759. (when (equal arg '(4))
  15760. (setq org-select-this-todo-keyword
  15761. (completing-read "Keyword (or KWD1|K2D2|...): "
  15762. (mapcar 'list kwds) nil nil)))
  15763. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  15764. (org-set-local 'org-last-arg arg)
  15765. (setq org-agenda-redo-command
  15766. '(org-todo-list (or current-prefix-arg org-last-arg)))
  15767. (setq files (org-agenda-files)
  15768. rtnall nil)
  15769. (while (setq file (pop files))
  15770. (catch 'nextfile
  15771. (org-check-agenda-file file)
  15772. (setq rtn (org-agenda-get-day-entries file date :todo))
  15773. (setq rtnall (append rtnall rtn))))
  15774. (if org-agenda-overriding-header
  15775. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  15776. nil 'face 'org-agenda-structure) "\n")
  15777. (insert "Global list of TODO items of type: ")
  15778. (add-text-properties (point-min) (1- (point))
  15779. (list 'face 'org-agenda-structure))
  15780. (setq pos (point))
  15781. (insert (or org-select-this-todo-keyword "ALL") "\n")
  15782. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  15783. (setq pos (point))
  15784. (unless org-agenda-multi
  15785. (insert "Available with `N r': (0)ALL")
  15786. (let ((n 0) s)
  15787. (mapc (lambda (x)
  15788. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  15789. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  15790. (insert "\n "))
  15791. (insert " " s))
  15792. kwds))
  15793. (insert "\n"))
  15794. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  15795. (when rtnall
  15796. (insert (org-finalize-agenda-entries rtnall) "\n"))
  15797. (goto-char (point-min))
  15798. (org-fit-agenda-window)
  15799. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  15800. (org-finalize-agenda)
  15801. (setq buffer-read-only t)))
  15802. ;;; Agenda tags match
  15803. ;;;###autoload
  15804. (defun org-tags-view (&optional todo-only match)
  15805. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  15806. The prefix arg TODO-ONLY limits the search to TODO entries."
  15807. (interactive "P")
  15808. (org-compile-prefix-format 'tags)
  15809. (org-set-sorting-strategy 'tags)
  15810. (let* ((org-tags-match-list-sublevels
  15811. (if todo-only t org-tags-match-list-sublevels))
  15812. (completion-ignore-case t)
  15813. rtn rtnall files file pos matcher
  15814. buffer)
  15815. (setq matcher (org-make-tags-matcher match)
  15816. match (car matcher) matcher (cdr matcher))
  15817. (org-prepare-agenda (concat "TAGS " match))
  15818. (setq org-agenda-redo-command
  15819. (list 'org-tags-view (list 'quote todo-only)
  15820. (list 'if 'current-prefix-arg nil match)))
  15821. (setq files (org-agenda-files)
  15822. rtnall nil)
  15823. (while (setq file (pop files))
  15824. (catch 'nextfile
  15825. (org-check-agenda-file file)
  15826. (setq buffer (if (file-exists-p file)
  15827. (org-get-agenda-file-buffer file)
  15828. (error "No such file %s" file)))
  15829. (if (not buffer)
  15830. ;; If file does not exist, merror message to agenda
  15831. (setq rtn (list
  15832. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  15833. rtnall (append rtnall rtn))
  15834. (with-current-buffer buffer
  15835. (unless (org-mode-p)
  15836. (error "Agenda file %s is not in `org-mode'" file))
  15837. (setq org-category-table (org-get-category-table))
  15838. (save-excursion
  15839. (save-restriction
  15840. (if org-agenda-restrict
  15841. (narrow-to-region org-agenda-restrict-begin
  15842. org-agenda-restrict-end)
  15843. (widen))
  15844. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  15845. (setq rtnall (append rtnall rtn))))))))
  15846. (if org-agenda-overriding-header
  15847. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  15848. nil 'face 'org-agenda-structure) "\n")
  15849. (insert "Headlines with TAGS match: ")
  15850. (add-text-properties (point-min) (1- (point))
  15851. (list 'face 'org-agenda-structure))
  15852. (setq pos (point))
  15853. (insert match "\n")
  15854. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  15855. (setq pos (point))
  15856. (unless org-agenda-multi
  15857. (insert "Press `C-u r' to search again with new search string\n"))
  15858. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  15859. (when rtnall
  15860. (insert (org-finalize-agenda-entries rtnall) "\n"))
  15861. (goto-char (point-min))
  15862. (org-fit-agenda-window)
  15863. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  15864. (org-finalize-agenda)
  15865. (setq buffer-read-only t)))
  15866. ;;; Agenda Finding stuck projects
  15867. (defvar org-agenda-skip-regexp nil
  15868. "Regular expression used in skipping subtrees for the agenda.
  15869. This is basically a temporary global variable that can be set and then
  15870. used by user-defined selections using `org-agenda-skip-function'.")
  15871. (defvar org-agenda-overriding-header nil
  15872. "When this is set during todo and tags searches, will replace header.")
  15873. (defun org-agenda-skip-subtree-when-regexp-matches ()
  15874. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  15875. If yes, it returns the end position of this tree, causing agenda commands
  15876. to skip this subtree. This is a function that can be put into
  15877. `org-agenda-skip-function' for the duration of a command."
  15878. (save-match-data
  15879. (let ((end (save-excursion (org-end-of-subtree t)))
  15880. skip)
  15881. (save-excursion
  15882. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  15883. (and skip end))))
  15884. (defun org-agenda-list-stuck-projects (&rest ignore)
  15885. "Create agenda view for projects that are stuck.
  15886. Stuck projects are project that have no next actions. For the definitions
  15887. of what a project is and how to check if it stuck, customize the variable
  15888. `org-stuck-projects'.
  15889. MATCH is being ignored."
  15890. (interactive)
  15891. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  15892. (org-agenda-overriding-header "List of stuck projects: ")
  15893. (matcher (nth 0 org-stuck-projects))
  15894. (todo (nth 1 org-stuck-projects))
  15895. (todo-wds (if (member "*" todo)
  15896. (progn
  15897. (org-prepare-agenda-buffers (org-agenda-files))
  15898. (org-delete-all
  15899. org-done-keywords-for-agenda
  15900. (copy-sequence org-todo-keywords-for-agenda)))
  15901. todo))
  15902. (todo-re (concat "^\\*+[ \t]+\\("
  15903. (mapconcat 'identity todo-wds "\\|")
  15904. "\\)\\>"))
  15905. (tags (nth 2 org-stuck-projects))
  15906. (tags-re (if (member "*" tags)
  15907. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  15908. (concat "^\\*+ .*:\\("
  15909. (mapconcat 'identity tags "\\|")
  15910. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  15911. (gen-re (nth 3 org-stuck-projects))
  15912. (re-list
  15913. (delq nil
  15914. (list
  15915. (if todo todo-re)
  15916. (if tags tags-re)
  15917. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  15918. gen-re)))))
  15919. (setq org-agenda-skip-regexp
  15920. (if re-list
  15921. (mapconcat 'identity re-list "\\|")
  15922. (error "No information how to identify unstuck projects")))
  15923. (org-tags-view nil matcher)
  15924. (with-current-buffer org-agenda-buffer-name
  15925. (setq org-agenda-redo-command
  15926. '(org-agenda-list-stuck-projects
  15927. (or current-prefix-arg org-last-arg))))))
  15928. ;;; Diary integration
  15929. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  15930. (defun org-get-entries-from-diary (date)
  15931. "Get the (Emacs Calendar) diary entries for DATE."
  15932. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  15933. (diary-display-hook '(fancy-diary-display))
  15934. (list-diary-entries-hook
  15935. (cons 'org-diary-default-entry list-diary-entries-hook))
  15936. (diary-file-name-prefix-function nil) ; turn this feature off
  15937. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  15938. entries
  15939. (org-disable-agenda-to-diary t))
  15940. (save-excursion
  15941. (save-window-excursion
  15942. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  15943. (if (not (get-buffer fancy-diary-buffer))
  15944. (setq entries nil)
  15945. (with-current-buffer fancy-diary-buffer
  15946. (setq buffer-read-only nil)
  15947. (if (= (point-max) 1)
  15948. ;; No entries
  15949. (setq entries nil)
  15950. ;; Omit the date and other unnecessary stuff
  15951. (org-agenda-cleanup-fancy-diary)
  15952. ;; Add prefix to each line and extend the text properties
  15953. (if (= (point-max) 1)
  15954. (setq entries nil)
  15955. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  15956. (set-buffer-modified-p nil)
  15957. (kill-buffer fancy-diary-buffer)))
  15958. (when entries
  15959. (setq entries (org-split-string entries "\n"))
  15960. (setq entries
  15961. (mapcar
  15962. (lambda (x)
  15963. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  15964. ;; Extend the text properties to the beginning of the line
  15965. (org-add-props x (text-properties-at (1- (length x)) x)
  15966. 'type "diary" 'date date))
  15967. entries)))))
  15968. (defun org-agenda-cleanup-fancy-diary ()
  15969. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  15970. This gets rid of the date, the underline under the date, and
  15971. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  15972. date. It also removes lines that contain only whitespace."
  15973. (goto-char (point-min))
  15974. (if (looking-at ".*?:[ \t]*")
  15975. (progn
  15976. (replace-match "")
  15977. (re-search-forward "\n=+$" nil t)
  15978. (replace-match "")
  15979. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  15980. (re-search-forward "\n=+$" nil t)
  15981. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  15982. (goto-char (point-min))
  15983. (while (re-search-forward "^ +\n" nil t)
  15984. (replace-match ""))
  15985. (goto-char (point-min))
  15986. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  15987. (replace-match "")))
  15988. ;; Make sure entries from the diary have the right text properties.
  15989. (eval-after-load "diary-lib"
  15990. '(if (boundp 'diary-modify-entry-list-string-function)
  15991. ;; We can rely on the hook, nothing to do
  15992. nil
  15993. ;; Hook not avaiable, must use advice to make this work
  15994. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  15995. "Make the position visible."
  15996. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  15997. (stringp string)
  15998. buffer-file-name)
  15999. (setq string (org-modify-diary-entry-string string))))))
  16000. (defun org-modify-diary-entry-string (string)
  16001. "Add text properties to string, allowing org-mode to act on it."
  16002. (org-add-props string nil
  16003. 'mouse-face 'highlight
  16004. 'keymap org-agenda-keymap
  16005. 'help-echo (if buffer-file-name
  16006. (format "mouse-2 or RET jump to diary file %s"
  16007. (abbreviate-file-name buffer-file-name))
  16008. "")
  16009. 'org-agenda-diary-link t
  16010. 'org-marker (org-agenda-new-marker (point-at-bol))))
  16011. (defun org-diary-default-entry ()
  16012. "Add a dummy entry to the diary.
  16013. Needed to avoid empty dates which mess up holiday display."
  16014. ;; Catch the error if dealing with the new add-to-diary-alist
  16015. (when org-disable-agenda-to-diary
  16016. (condition-case nil
  16017. (add-to-diary-list original-date "Org-mode dummy" "")
  16018. (error
  16019. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  16020. ;;;###autoload
  16021. (defun org-diary (&rest args)
  16022. "Return diary information from org-files.
  16023. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  16024. It accesses org files and extracts information from those files to be
  16025. listed in the diary. The function accepts arguments specifying what
  16026. items should be listed. The following arguments are allowed:
  16027. :timestamp List the headlines of items containing a date stamp or
  16028. date range matching the selected date. Deadlines will
  16029. also be listed, on the expiration day.
  16030. :sexp FIXME
  16031. :deadline List any deadlines past due, or due within
  16032. `org-deadline-warning-days'. The listing occurs only
  16033. in the diary for *today*, not at any other date. If
  16034. an entry is marked DONE, it is no longer listed.
  16035. :scheduled List all items which are scheduled for the given date.
  16036. The diary for *today* also contains items which were
  16037. scheduled earlier and are not yet marked DONE.
  16038. :todo List all TODO items from the org-file. This may be a
  16039. long list - so this is not turned on by default.
  16040. Like deadlines, these entries only show up in the
  16041. diary for *today*, not at any other date.
  16042. The call in the diary file should look like this:
  16043. &%%(org-diary) ~/path/to/some/orgfile.org
  16044. Use a separate line for each org file to check. Or, if you omit the file name,
  16045. all files listed in `org-agenda-files' will be checked automatically:
  16046. &%%(org-diary)
  16047. If you don't give any arguments (as in the example above), the default
  16048. arguments (:deadline :scheduled :timestamp :sexp) are used.
  16049. So the example above may also be written as
  16050. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  16051. The function expects the lisp variables `entry' and `date' to be provided
  16052. by the caller, because this is how the calendar works. Don't use this
  16053. function from a program - use `org-agenda-get-day-entries' instead."
  16054. (org-agenda-maybe-reset-markers)
  16055. (org-compile-prefix-format 'agenda)
  16056. (org-set-sorting-strategy 'agenda)
  16057. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  16058. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  16059. (list entry)
  16060. (org-agenda-files t)))
  16061. file rtn results)
  16062. (org-prepare-agenda-buffers files)
  16063. ;; If this is called during org-agenda, don't return any entries to
  16064. ;; the calendar. Org Agenda will list these entries itself.
  16065. (if org-disable-agenda-to-diary (setq files nil))
  16066. (while (setq file (pop files))
  16067. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  16068. (setq results (append results rtn)))
  16069. (if results
  16070. (concat (org-finalize-agenda-entries results) "\n"))))
  16071. ;;; Agenda entry finders
  16072. (defun org-agenda-get-day-entries (file date &rest args)
  16073. "Does the work for `org-diary' and `org-agenda'.
  16074. FILE is the path to a file to be checked for entries. DATE is date like
  16075. the one returned by `calendar-current-date'. ARGS are symbols indicating
  16076. which kind of entries should be extracted. For details about these, see
  16077. the documentation of `org-diary'."
  16078. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  16079. (let* ((org-startup-folded nil)
  16080. (org-startup-align-all-tables nil)
  16081. (buffer (if (file-exists-p file)
  16082. (org-get-agenda-file-buffer file)
  16083. (error "No such file %s" file)))
  16084. arg results rtn)
  16085. (if (not buffer)
  16086. ;; If file does not exist, make sure an error message ends up in diary
  16087. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  16088. (with-current-buffer buffer
  16089. (unless (org-mode-p)
  16090. (error "Agenda file %s is not in `org-mode'" file))
  16091. (setq org-category-table (org-get-category-table))
  16092. (let ((case-fold-search nil))
  16093. (save-excursion
  16094. (save-restriction
  16095. (if org-agenda-restrict
  16096. (narrow-to-region org-agenda-restrict-begin
  16097. org-agenda-restrict-end)
  16098. (widen))
  16099. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  16100. (while (setq arg (pop args))
  16101. (cond
  16102. ((and (eq arg :todo)
  16103. (equal date (calendar-current-date)))
  16104. (setq rtn (org-agenda-get-todos))
  16105. (setq results (append results rtn)))
  16106. ((eq arg :timestamp)
  16107. (setq rtn (org-agenda-get-blocks))
  16108. (setq results (append results rtn))
  16109. (setq rtn (org-agenda-get-timestamps))
  16110. (setq results (append results rtn)))
  16111. ((eq arg :sexp)
  16112. (setq rtn (org-agenda-get-sexps))
  16113. (setq results (append results rtn)))
  16114. ((eq arg :scheduled)
  16115. (setq rtn (org-agenda-get-scheduled))
  16116. (setq results (append results rtn)))
  16117. ((eq arg :closed)
  16118. (setq rtn (org-agenda-get-closed))
  16119. (setq results (append results rtn)))
  16120. ((and (eq arg :deadline)
  16121. (equal date (calendar-current-date)))
  16122. (setq rtn (org-agenda-get-deadlines))
  16123. (setq results (append results rtn))))))))
  16124. results))))
  16125. ;; FIXME: this works only if the cursor is not at the
  16126. ;; beginning of the entry
  16127. (defun org-entry-is-done-p ()
  16128. "Is the current entry marked DONE?"
  16129. (save-excursion
  16130. (and (re-search-backward "[\r\n]\\* " nil t)
  16131. (looking-at org-nl-done-regexp))))
  16132. (defun org-at-date-range-p (&optional inactive-ok)
  16133. "Is the cursor inside a date range?"
  16134. (interactive)
  16135. (save-excursion
  16136. (catch 'exit
  16137. (let ((pos (point)))
  16138. (skip-chars-backward "^[<\r\n")
  16139. (skip-chars-backward "<[")
  16140. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  16141. (>= (match-end 0) pos)
  16142. (throw 'exit t))
  16143. (skip-chars-backward "^<[\r\n")
  16144. (skip-chars-backward "<[")
  16145. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  16146. (>= (match-end 0) pos)
  16147. (throw 'exit t)))
  16148. nil)))
  16149. (defun org-agenda-get-todos ()
  16150. "Return the TODO information for agenda display."
  16151. (let* ((props (list 'face nil
  16152. 'done-face 'org-done
  16153. 'org-not-done-regexp org-not-done-regexp
  16154. 'org-todo-regexp org-todo-regexp
  16155. 'mouse-face 'highlight
  16156. 'keymap org-agenda-keymap
  16157. 'help-echo
  16158. (format "mouse-2 or RET jump to org file %s"
  16159. (abbreviate-file-name buffer-file-name))))
  16160. ;; FIXME: get rid of the \n at some point but watch out
  16161. (regexp (concat "\n\\*+[ \t]+\\("
  16162. (if org-select-this-todo-keyword
  16163. (if (equal org-select-this-todo-keyword "*")
  16164. org-todo-regexp
  16165. (concat "\\<\\("
  16166. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  16167. "\\)\\>"))
  16168. org-not-done-regexp)
  16169. "[^\n\r]*\\)"))
  16170. marker priority category tags
  16171. ee txt beg end)
  16172. (goto-char (point-min))
  16173. (while (re-search-forward regexp nil t)
  16174. (catch :skip
  16175. (save-match-data
  16176. (beginning-of-line)
  16177. (setq beg (point) end (progn (outline-next-heading) (point)))
  16178. (when (or (and org-agenda-todo-ignore-scheduled (goto-char beg)
  16179. (re-search-forward org-scheduled-time-regexp end t))
  16180. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  16181. (re-search-forward org-deadline-time-regexp end t)
  16182. (org-deadline-close (match-string 1))))
  16183. (goto-char beg)
  16184. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  16185. (throw :skip nil)))
  16186. (goto-char beg)
  16187. (org-agenda-skip)
  16188. (goto-char (match-beginning 1))
  16189. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  16190. category (org-get-category)
  16191. tags (org-get-tags-at (point))
  16192. txt (org-format-agenda-item "" (match-string 1) category tags)
  16193. priority (1+ (org-get-priority txt)))
  16194. (org-add-props txt props
  16195. 'org-marker marker 'org-hd-marker marker
  16196. 'priority priority 'org-category category
  16197. 'type "todo")
  16198. (push txt ee)
  16199. (if org-agenda-todo-list-sublevels
  16200. (goto-char (match-end 1))
  16201. (org-end-of-subtree 'invisible))))
  16202. (nreverse ee)))
  16203. (defconst org-agenda-no-heading-message
  16204. "No heading for this item in buffer or region.")
  16205. (defun org-agenda-get-timestamps ()
  16206. "Return the date stamp information for agenda display."
  16207. (let* ((props (list 'face nil
  16208. 'org-not-done-regexp org-not-done-regexp
  16209. 'org-todo-regexp org-todo-regexp
  16210. 'mouse-face 'highlight
  16211. 'keymap org-agenda-keymap
  16212. 'help-echo
  16213. (format "mouse-2 or RET jump to org file %s"
  16214. (abbreviate-file-name buffer-file-name))))
  16215. ;???? (regexp (regexp-quote
  16216. ; (substring
  16217. ; (format-time-string
  16218. ; (car org-time-stamp-formats)
  16219. ; (apply 'encode-time ; DATE bound by calendar
  16220. ; (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16221. ; 0 11)))
  16222. (d1 (calendar-absolute-from-gregorian date))
  16223. (regexp
  16224. (concat
  16225. (regexp-quote
  16226. (substring
  16227. (format-time-string
  16228. (car org-time-stamp-formats)
  16229. (apply 'encode-time ; DATE bound by calendar
  16230. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16231. 0 11))
  16232. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  16233. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  16234. marker hdmarker deadlinep scheduledp donep tmp priority category
  16235. ee txt timestr tags b0 b3 e3)
  16236. (goto-char (point-min))
  16237. (while (re-search-forward regexp nil t)
  16238. (setq b0 (match-beginning 0)
  16239. b3 (match-beginning 3) e3 (match-end 3))
  16240. (catch :skip
  16241. (and (org-at-date-range-p) (throw :skip nil))
  16242. (org-agenda-skip)
  16243. (if (and (match-end 1)
  16244. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  16245. (throw :skip nil))
  16246. (if (and e3
  16247. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  16248. (throw :skip nil))
  16249. (setq marker (org-agenda-new-marker b0)
  16250. category (org-get-category b0)
  16251. tmp (buffer-substring (max (point-min)
  16252. (- b0 org-ds-keyword-length))
  16253. b0)
  16254. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  16255. deadlinep (string-match org-deadline-regexp tmp)
  16256. scheduledp (string-match org-scheduled-regexp tmp)
  16257. donep (org-entry-is-done-p))
  16258. (and org-agenda-skip-scheduled-if-done
  16259. scheduledp donep
  16260. (throw :skip t))
  16261. (and org-agenda-skip-deadline-if-done
  16262. deadlinep donep
  16263. (throw :skip t))
  16264. (if (string-match ">" timestr)
  16265. ;; substring should only run to end of time stamp
  16266. (setq timestr (substring timestr 0 (match-end 0))))
  16267. (save-excursion
  16268. (if (re-search-backward "^\\*+ " nil t)
  16269. (progn
  16270. (goto-char (match-beginning 0))
  16271. (setq hdmarker (org-agenda-new-marker)
  16272. tags (org-get-tags-at))
  16273. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16274. (setq txt (org-format-agenda-item
  16275. (format "%s%s"
  16276. (if deadlinep "Deadline: " "")
  16277. (if scheduledp "Scheduled: " ""))
  16278. (match-string 1) category tags timestr)))
  16279. (setq txt org-agenda-no-heading-message))
  16280. (setq priority (org-get-priority txt))
  16281. (org-add-props txt props
  16282. 'org-marker marker 'org-hd-marker hdmarker)
  16283. (if deadlinep
  16284. (org-add-props txt nil
  16285. 'face (if donep 'org-done 'org-warning)
  16286. 'type "deadline" 'date date
  16287. 'undone-face 'org-warning 'done-face 'org-done
  16288. 'org-category category 'priority (+ 100 priority))
  16289. (if scheduledp
  16290. (org-add-props txt nil
  16291. 'face 'org-scheduled-today
  16292. 'type "scheduled" 'date date
  16293. 'undone-face 'org-scheduled-today 'done-face 'org-done
  16294. 'org-category category 'priority (+ 99 priority))
  16295. (org-add-props txt nil 'priority priority
  16296. 'org-category category 'date date
  16297. 'type "timestamp")))
  16298. (push txt ee))
  16299. (outline-next-heading)))
  16300. (nreverse ee)))
  16301. (defun org-agenda-get-sexps ()
  16302. "Return the sexp information for agenda display."
  16303. (require 'diary-lib)
  16304. (let* ((props (list 'face nil
  16305. 'mouse-face 'highlight
  16306. 'keymap org-agenda-keymap
  16307. 'help-echo
  16308. (format "mouse-2 or RET jump to org file %s"
  16309. (abbreviate-file-name buffer-file-name))))
  16310. (regexp "^&?%%(")
  16311. marker category ee txt tags entry result beg b sexp sexp-entry)
  16312. (goto-char (point-min))
  16313. (while (re-search-forward regexp nil t)
  16314. (catch :skip
  16315. (org-agenda-skip)
  16316. (setq beg (match-beginning 0))
  16317. (goto-char (1- (match-end 0)))
  16318. (setq b (point))
  16319. (forward-sexp 1)
  16320. (setq sexp (buffer-substring b (point)))
  16321. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  16322. (org-trim (match-string 1))
  16323. ""))
  16324. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  16325. (when result
  16326. (setq marker (org-agenda-new-marker beg)
  16327. category (org-get-category beg))
  16328. (if (string-match "\\S-" result)
  16329. (setq txt result)
  16330. (setq txt "SEXP entry returned empty string"))
  16331. (setq txt (org-format-agenda-item
  16332. "" txt category tags 'time))
  16333. (org-add-props txt props 'org-marker marker)
  16334. (org-add-props txt nil
  16335. 'org-category category 'date date
  16336. 'type "sexp")
  16337. (push txt ee))))
  16338. (nreverse ee)))
  16339. (defun org-agenda-get-closed ()
  16340. "Return the logged TODO entries for agenda display."
  16341. (let* ((props (list 'mouse-face 'highlight
  16342. 'org-not-done-regexp org-not-done-regexp
  16343. 'org-todo-regexp org-todo-regexp
  16344. 'keymap org-agenda-keymap
  16345. 'help-echo
  16346. (format "mouse-2 or RET jump to org file %s"
  16347. (abbreviate-file-name buffer-file-name))))
  16348. (regexp (concat
  16349. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  16350. (regexp-quote
  16351. (substring
  16352. (format-time-string
  16353. (car org-time-stamp-formats)
  16354. (apply 'encode-time ; DATE bound by calendar
  16355. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16356. 1 11))))
  16357. marker hdmarker priority category tags closedp
  16358. ee txt timestr)
  16359. (goto-char (point-min))
  16360. (while (re-search-forward regexp nil t)
  16361. (catch :skip
  16362. (org-agenda-skip)
  16363. (setq marker (org-agenda-new-marker (match-beginning 0))
  16364. closedp (equal (match-string 1) org-closed-string)
  16365. category (org-get-category (match-beginning 0))
  16366. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  16367. ;; donep (org-entry-is-done-p)
  16368. )
  16369. (if (string-match "\\]" timestr)
  16370. ;; substring should only run to end of time stamp
  16371. (setq timestr (substring timestr 0 (match-end 0))))
  16372. (save-excursion
  16373. (if (re-search-backward "^\\*+ " nil t)
  16374. (progn
  16375. (goto-char (match-beginning 0))
  16376. (setq hdmarker (org-agenda-new-marker)
  16377. tags (org-get-tags-at))
  16378. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16379. (setq txt (org-format-agenda-item
  16380. (if closedp "Closed: " "Clocked: ")
  16381. (match-string 1) category tags timestr)))
  16382. (setq txt org-agenda-no-heading-message))
  16383. (setq priority 100000)
  16384. (org-add-props txt props
  16385. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  16386. 'priority priority 'org-category category
  16387. 'type "closed" 'date date
  16388. 'undone-face 'org-warning 'done-face 'org-done)
  16389. (push txt ee))
  16390. (outline-next-heading)))
  16391. (nreverse ee)))
  16392. (defun org-agenda-get-deadlines ()
  16393. "Return the deadline information for agenda display."
  16394. (let* ((wdays org-deadline-warning-days)
  16395. (props (list 'mouse-face 'highlight
  16396. 'org-not-done-regexp org-not-done-regexp
  16397. 'org-todo-regexp org-todo-regexp
  16398. 'keymap org-agenda-keymap
  16399. 'help-echo
  16400. (format "mouse-2 or RET jump to org file %s"
  16401. (abbreviate-file-name buffer-file-name))))
  16402. (regexp org-deadline-time-regexp)
  16403. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  16404. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  16405. d2 diff pos pos1 category tags
  16406. ee txt head face)
  16407. (goto-char (point-min))
  16408. (while (re-search-forward regexp nil t)
  16409. (catch :skip
  16410. (org-agenda-skip)
  16411. (setq pos (1- (match-beginning 1))
  16412. ;??? d2 (time-to-days
  16413. ;??? (org-time-string-to-time (match-string 1)))
  16414. d2 (org-time-string-to-absolute (match-string 1) d1)
  16415. diff (- d2 d1))
  16416. ;; When to show a deadline in the calendar:
  16417. ;; If the expiration is within wdays warning time.
  16418. ;; Past-due deadlines are only shown on the current date
  16419. (if (and (< diff wdays) todayp (not (= diff 0)))
  16420. (save-excursion
  16421. (setq category (org-get-category))
  16422. (if (re-search-backward "^\\*+[ \t]+" nil t)
  16423. (progn
  16424. (goto-char (match-end 0))
  16425. (setq pos1 (match-beginning 0))
  16426. (setq tags (org-get-tags-at pos1))
  16427. (setq head (buffer-substring-no-properties
  16428. (point)
  16429. (progn (skip-chars-forward "^\r\n")
  16430. (point))))
  16431. (if (string-match org-looking-at-done-regexp head)
  16432. (setq txt nil)
  16433. (setq txt (org-format-agenda-item
  16434. (format "In %3d d.: " diff) head category tags))))
  16435. (setq txt org-agenda-no-heading-message))
  16436. (when txt
  16437. (setq face (cond ((<= diff 0) 'org-warning)
  16438. ((<= diff 5) 'org-upcoming-deadline)
  16439. (t nil)))
  16440. (org-add-props txt props
  16441. 'org-marker (org-agenda-new-marker pos)
  16442. 'org-hd-marker (org-agenda-new-marker pos1)
  16443. 'priority (+ (- 10 diff) (org-get-priority txt))
  16444. 'org-category category
  16445. 'type "upcoming-deadline" 'date d2
  16446. 'face face 'undone-face face 'done-face 'org-done)
  16447. (push txt ee))))))
  16448. ee))
  16449. (defun org-agenda-get-scheduled ()
  16450. "Return the scheduled information for agenda display."
  16451. (let* ((props (list 'face 'org-scheduled-previously
  16452. 'org-not-done-regexp org-not-done-regexp
  16453. 'org-todo-regexp org-todo-regexp
  16454. 'undone-face 'org-scheduled-previously
  16455. 'done-face 'org-done
  16456. 'mouse-face 'highlight
  16457. 'keymap org-agenda-keymap
  16458. 'help-echo
  16459. (format "mouse-2 or RET jump to org file %s"
  16460. (abbreviate-file-name buffer-file-name))))
  16461. (regexp org-scheduled-time-regexp)
  16462. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  16463. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  16464. d2 diff pos pos1 category tags
  16465. ee txt head)
  16466. (goto-char (point-min))
  16467. (while (re-search-forward regexp nil t)
  16468. (catch :skip
  16469. (org-agenda-skip)
  16470. (setq pos (1- (match-beginning 1))
  16471. d2 (org-time-string-to-absolute (match-string 1) d1)
  16472. ;??? d2 (time-to-days
  16473. ;??? (org-time-string-to-time (match-string 1)))
  16474. diff (- d2 d1))
  16475. ;; When to show a scheduled item in the calendar:
  16476. ;; If it is on or past the date.
  16477. (if (and (< diff 0) todayp)
  16478. (save-excursion
  16479. (setq category (org-get-category))
  16480. (if (re-search-backward "^\\*+[ \t]+" nil t)
  16481. (progn
  16482. (goto-char (match-end 0))
  16483. (setq pos1 (match-beginning 0))
  16484. (setq tags (org-get-tags-at))
  16485. (setq head (buffer-substring-no-properties
  16486. (point)
  16487. (progn (skip-chars-forward "^\r\n") (point))))
  16488. (if (string-match org-looking-at-done-regexp head)
  16489. (setq txt nil)
  16490. (setq txt (org-format-agenda-item
  16491. (format "Sched.%2dx: " (- 1 diff)) head
  16492. category tags))))
  16493. (setq txt org-agenda-no-heading-message))
  16494. (when txt
  16495. (org-add-props txt props
  16496. 'org-marker (org-agenda-new-marker pos)
  16497. 'org-hd-marker (org-agenda-new-marker pos1)
  16498. 'type "past-scheduled" 'date d2
  16499. 'priority (+ (- 5 diff) (org-get-priority txt))
  16500. 'org-category category)
  16501. (push txt ee))))))
  16502. ee))
  16503. (defun org-agenda-get-blocks ()
  16504. "Return the date-range information for agenda display."
  16505. (let* ((props (list 'face nil
  16506. 'org-not-done-regexp org-not-done-regexp
  16507. 'org-todo-regexp org-todo-regexp
  16508. 'mouse-face 'highlight
  16509. 'keymap org-agenda-keymap
  16510. 'help-echo
  16511. (format "mouse-2 or RET jump to org file %s"
  16512. (abbreviate-file-name buffer-file-name))))
  16513. (regexp org-tr-regexp)
  16514. (d0 (calendar-absolute-from-gregorian date))
  16515. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  16516. (goto-char (point-min))
  16517. (while (re-search-forward regexp nil t)
  16518. (catch :skip
  16519. (org-agenda-skip)
  16520. (setq pos (point))
  16521. (setq timestr (match-string 0)
  16522. s1 (match-string 1)
  16523. s2 (match-string 2)
  16524. d1 (time-to-days (org-time-string-to-time s1))
  16525. d2 (time-to-days (org-time-string-to-time s2)))
  16526. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  16527. ;; Only allow days between the limits, because the normal
  16528. ;; date stamps will catch the limits.
  16529. (save-excursion
  16530. (setq marker (org-agenda-new-marker (point)))
  16531. (setq category (org-get-category))
  16532. (if (re-search-backward "^\\*+ " nil t)
  16533. (progn
  16534. (goto-char (match-beginning 0))
  16535. (setq hdmarker (org-agenda-new-marker (point)))
  16536. (setq tags (org-get-tags-at))
  16537. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16538. (setq txt (org-format-agenda-item
  16539. (format (if (= d1 d2) "" "(%d/%d): ")
  16540. (1+ (- d0 d1)) (1+ (- d2 d1)))
  16541. (match-string 1) category tags
  16542. (if (= d0 d1) timestr))))
  16543. (setq txt org-agenda-no-heading-message))
  16544. (org-add-props txt props
  16545. 'org-marker marker 'org-hd-marker hdmarker
  16546. 'type "block" 'date date
  16547. 'priority (org-get-priority txt) 'org-category category)
  16548. (push txt ee)))
  16549. (goto-char pos)))
  16550. ;; Sort the entries by expiration date.
  16551. (nreverse ee)))
  16552. ;;; Agenda presentation and sorting
  16553. (defconst org-plain-time-of-day-regexp
  16554. (concat
  16555. "\\(\\<[012]?[0-9]"
  16556. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  16557. "\\(--?"
  16558. "\\(\\<[012]?[0-9]"
  16559. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  16560. "\\)?")
  16561. "Regular expression to match a plain time or time range.
  16562. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  16563. groups carry important information:
  16564. 0 the full match
  16565. 1 the first time, range or not
  16566. 8 the second time, if it is a range.")
  16567. (defconst org-stamp-time-of-day-regexp
  16568. (concat
  16569. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  16570. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  16571. "\\(--?"
  16572. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  16573. "Regular expression to match a timestamp time or time range.
  16574. After a match, the following groups carry important information:
  16575. 0 the full match
  16576. 1 date plus weekday, for backreferencing to make sure both times on same day
  16577. 2 the first time, range or not
  16578. 4 the second time, if it is a range.")
  16579. (defvar org-prefix-has-time nil
  16580. "A flag, set by `org-compile-prefix-format'.
  16581. The flag is set if the currently compiled format contains a `%t'.")
  16582. (defvar org-prefix-has-tag nil
  16583. "A flag, set by `org-compile-prefix-format'.
  16584. The flag is set if the currently compiled format contains a `%T'.")
  16585. (defun org-format-agenda-item (extra txt &optional category tags dotime
  16586. noprefix)
  16587. "Format TXT to be inserted into the agenda buffer.
  16588. In particular, it adds the prefix and corresponding text properties. EXTRA
  16589. must be a string and replaces the `%s' specifier in the prefix format.
  16590. CATEGORY (string, symbol or nil) may be used to overrule the default
  16591. category taken from local variable or file name. It will replace the `%c'
  16592. specifier in the format. DOTIME, when non-nil, indicates that a
  16593. time-of-day should be extracted from TXT for sorting of this entry, and for
  16594. the `%t' specifier in the format. When DOTIME is a string, this string is
  16595. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  16596. only the correctly processes TXT should be returned - this is used by
  16597. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  16598. (save-match-data
  16599. ;; Diary entries sometimes have extra whitespace at the beginning
  16600. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  16601. (let* ((category (or category
  16602. org-category
  16603. (if buffer-file-name
  16604. (file-name-sans-extension
  16605. (file-name-nondirectory buffer-file-name))
  16606. "")))
  16607. (tag (if tags (nth (1- (length tags)) tags) ""))
  16608. time ; time and tag are needed for the eval of the prefix format
  16609. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  16610. (time-of-day (and dotime (org-get-time-of-day ts)))
  16611. stamp plain s0 s1 s2 rtn srp)
  16612. (when (and dotime time-of-day org-prefix-has-time)
  16613. ;; Extract starting and ending time and move them to prefix
  16614. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  16615. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  16616. (setq s0 (match-string 0 ts)
  16617. srp (and stamp (match-end 3))
  16618. s1 (match-string (if plain 1 2) ts)
  16619. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  16620. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  16621. ;; them, we might want to remove them there to avoid duplication.
  16622. ;; The user can turn this off with a variable.
  16623. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  16624. (string-match (concat (regexp-quote s0) " *") txt)
  16625. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  16626. (= (match-beginning 0) 0)
  16627. t))
  16628. (setq txt (replace-match "" nil nil txt))))
  16629. ;; Normalize the time(s) to 24 hour
  16630. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  16631. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  16632. (when (and s1 (not s2) org-agenda-default-appointment-duration
  16633. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  16634. (let ((m (+ (string-to-number (match-string 2 s1))
  16635. (* 60 (string-to-number (match-string 1 s1)))
  16636. org-agenda-default-appointment-duration))
  16637. h)
  16638. (setq h (/ m 60) m (- m (* h 60)))
  16639. (setq s2 (format "%02d:%02d" h m))))
  16640. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  16641. txt)
  16642. ;; Tags are in the string
  16643. (if (or (eq org-agenda-remove-tags t)
  16644. (and org-agenda-remove-tags
  16645. org-prefix-has-tag))
  16646. (setq txt (replace-match "" t t txt))
  16647. (setq txt (replace-match
  16648. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  16649. (match-string 2 txt))
  16650. t t txt))))
  16651. ;; Create the final string
  16652. (if noprefix
  16653. (setq rtn txt)
  16654. ;; Prepare the variables needed in the eval of the compiled format
  16655. (setq time (cond (s2 (concat s1 "-" s2))
  16656. (s1 (concat s1 "......"))
  16657. (t ""))
  16658. extra (or extra "")
  16659. category (if (symbolp category) (symbol-name category) category))
  16660. ;; Evaluate the compiled format
  16661. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  16662. ;; And finally add the text properties
  16663. (org-add-props rtn nil
  16664. 'org-category (downcase category) 'tags tags
  16665. 'prefix-length (- (length rtn) (length txt))
  16666. 'time-of-day time-of-day
  16667. 'txt txt
  16668. 'time time
  16669. 'extra extra
  16670. 'dotime dotime))))
  16671. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  16672. (defvar org-agenda-sorting-strategy-selected nil)
  16673. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  16674. (catch 'exit
  16675. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  16676. ((and todayp (member 'today (car org-agenda-time-grid))))
  16677. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  16678. ((member 'weekly (car org-agenda-time-grid)))
  16679. (t (throw 'exit list)))
  16680. (let* ((have (delq nil (mapcar
  16681. (lambda (x) (get-text-property 1 'time-of-day x))
  16682. list)))
  16683. (string (nth 1 org-agenda-time-grid))
  16684. (gridtimes (nth 2 org-agenda-time-grid))
  16685. (req (car org-agenda-time-grid))
  16686. (remove (member 'remove-match req))
  16687. new time)
  16688. (if (and (member 'require-timed req) (not have))
  16689. ;; don't show empty grid
  16690. (throw 'exit list))
  16691. (while (setq time (pop gridtimes))
  16692. (unless (and remove (member time have))
  16693. (setq time (int-to-string time))
  16694. (push (org-format-agenda-item
  16695. nil string "" nil
  16696. (concat (substring time 0 -2) ":" (substring time -2)))
  16697. new)
  16698. (put-text-property
  16699. 1 (length (car new)) 'face 'org-time-grid (car new))))
  16700. (if (member 'time-up org-agenda-sorting-strategy-selected)
  16701. (append new list)
  16702. (append list new)))))
  16703. (defun org-compile-prefix-format (key)
  16704. "Compile the prefix format into a Lisp form that can be evaluated.
  16705. The resulting form is returned and stored in the variable
  16706. `org-prefix-format-compiled'."
  16707. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  16708. (let ((s (cond
  16709. ((stringp org-agenda-prefix-format)
  16710. org-agenda-prefix-format)
  16711. ((assq key org-agenda-prefix-format)
  16712. (cdr (assq key org-agenda-prefix-format)))
  16713. (t " %-12:c%?-12t% s")))
  16714. (start 0)
  16715. varform vars var e c f opt)
  16716. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  16717. s start)
  16718. (setq var (cdr (assoc (match-string 4 s)
  16719. '(("c" . category) ("t" . time) ("s" . extra)
  16720. ("T" . tag))))
  16721. c (or (match-string 3 s) "")
  16722. opt (match-beginning 1)
  16723. start (1+ (match-beginning 0)))
  16724. (if (equal var 'time) (setq org-prefix-has-time t))
  16725. (if (equal var 'tag) (setq org-prefix-has-tag t))
  16726. (setq f (concat "%" (match-string 2 s) "s"))
  16727. (if opt
  16728. (setq varform
  16729. `(if (equal "" ,var)
  16730. ""
  16731. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  16732. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  16733. (setq s (replace-match "%s" t nil s))
  16734. (push varform vars))
  16735. (setq vars (nreverse vars))
  16736. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  16737. (defun org-set-sorting-strategy (key)
  16738. (if (symbolp (car org-agenda-sorting-strategy))
  16739. ;; the old format
  16740. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  16741. (setq org-agenda-sorting-strategy-selected
  16742. (or (cdr (assq key org-agenda-sorting-strategy))
  16743. (cdr (assq 'agenda org-agenda-sorting-strategy))
  16744. '(time-up category-keep priority-down)))))
  16745. (defun org-get-time-of-day (s &optional string mod24)
  16746. "Check string S for a time of day.
  16747. If found, return it as a military time number between 0 and 2400.
  16748. If not found, return nil.
  16749. The optional STRING argument forces conversion into a 5 character wide string
  16750. HH:MM."
  16751. (save-match-data
  16752. (when
  16753. (or
  16754. (string-match
  16755. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  16756. (string-match
  16757. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  16758. (let* ((h (string-to-number (match-string 1 s)))
  16759. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  16760. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  16761. (am-p (equal ampm "am"))
  16762. (h1 (cond ((not ampm) h)
  16763. ((= h 12) (if am-p 0 12))
  16764. (t (+ h (if am-p 0 12)))))
  16765. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  16766. (mod h1 24) h1))
  16767. (t0 (+ (* 100 h2) m))
  16768. (t1 (concat (if (>= h1 24) "+" " ")
  16769. (if (< t0 100) "0" "")
  16770. (if (< t0 10) "0" "")
  16771. (int-to-string t0))))
  16772. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  16773. (defun org-finalize-agenda-entries (list &optional nosort)
  16774. "Sort and concatenate the agenda items."
  16775. (setq list (mapcar 'org-agenda-highlight-todo list))
  16776. (if nosort
  16777. list
  16778. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  16779. (defun org-agenda-highlight-todo (x)
  16780. (let (re pl)
  16781. (if (eq x 'line)
  16782. (save-excursion
  16783. (beginning-of-line 1)
  16784. (setq re (get-text-property (point) 'org-not-done-regexp))
  16785. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  16786. (and (looking-at (concat "[ \t]*\\.*" re))
  16787. (add-text-properties (match-beginning 0) (match-end 0)
  16788. '(face org-todo))))
  16789. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  16790. pl (get-text-property 0 'prefix-length x))
  16791. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  16792. (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
  16793. '(face org-todo) x))
  16794. x)))
  16795. (defsubst org-cmp-priority (a b)
  16796. "Compare the priorities of string A and B."
  16797. (let ((pa (or (get-text-property 1 'priority a) 0))
  16798. (pb (or (get-text-property 1 'priority b) 0)))
  16799. (cond ((> pa pb) +1)
  16800. ((< pa pb) -1)
  16801. (t nil))))
  16802. (defsubst org-cmp-category (a b)
  16803. "Compare the string values of categories of strings A and B."
  16804. (let ((ca (or (get-text-property 1 'category a) ""))
  16805. (cb (or (get-text-property 1 'category b) "")))
  16806. (cond ((string-lessp ca cb) -1)
  16807. ((string-lessp cb ca) +1)
  16808. (t nil))))
  16809. (defsubst org-cmp-tag (a b)
  16810. "Compare the string values of categories of strings A and B."
  16811. (let ((ta (car (last (get-text-property 1 'tags a))))
  16812. (tb (car (last (get-text-property 1 'tags b)))))
  16813. (cond ((not ta) +1)
  16814. ((not tb) -1)
  16815. ((string-lessp ta tb) -1)
  16816. ((string-lessp tb ta) +1)
  16817. (t nil))))
  16818. (defsubst org-cmp-time (a b)
  16819. "Compare the time-of-day values of strings A and B."
  16820. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  16821. (ta (or (get-text-property 1 'time-of-day a) def))
  16822. (tb (or (get-text-property 1 'time-of-day b) def)))
  16823. (cond ((< ta tb) -1)
  16824. ((< tb ta) +1)
  16825. (t nil))))
  16826. (defun org-entries-lessp (a b)
  16827. "Predicate for sorting agenda entries."
  16828. ;; The following variables will be used when the form is evaluated.
  16829. ;; So even though the compiler complains, keep them.
  16830. (let* ((time-up (org-cmp-time a b))
  16831. (time-down (if time-up (- time-up) nil))
  16832. (priority-up (org-cmp-priority a b))
  16833. (priority-down (if priority-up (- priority-up) nil))
  16834. (category-up (org-cmp-category a b))
  16835. (category-down (if category-up (- category-up) nil))
  16836. (category-keep (if category-up +1 nil))
  16837. (tag-up (org-cmp-tag a b))
  16838. (tag-down (if tag-up (- tag-up) nil)))
  16839. (cdr (assoc
  16840. (eval (cons 'or org-agenda-sorting-strategy-selected))
  16841. '((-1 . t) (1 . nil) (nil . nil))))))
  16842. ;;; Agenda commands
  16843. (defun org-agenda-check-type (error &rest types)
  16844. "Check if agenda buffer is of allowed type.
  16845. If ERROR is non-nil, throw an error, otherwise just return nil."
  16846. (if (memq org-agenda-type types)
  16847. t
  16848. (if error
  16849. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  16850. nil)))
  16851. (defun org-agenda-quit ()
  16852. "Exit agenda by removing the window or the buffer."
  16853. (interactive)
  16854. (let ((buf (current-buffer)))
  16855. (if (not (one-window-p)) (delete-window))
  16856. (kill-buffer buf)
  16857. (org-agenda-maybe-reset-markers 'force)
  16858. (org-columns-remove-overlays))
  16859. ;; Maybe restore the pre-agenda window configuration.
  16860. (and org-agenda-restore-windows-after-quit
  16861. (not (eq org-agenda-window-setup 'other-frame))
  16862. org-pre-agenda-window-conf
  16863. (set-window-configuration org-pre-agenda-window-conf)))
  16864. (defun org-agenda-exit ()
  16865. "Exit agenda by removing the window or the buffer.
  16866. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  16867. Org-mode buffers visited directly by the user will not be touched."
  16868. (interactive)
  16869. (org-release-buffers org-agenda-new-buffers)
  16870. (setq org-agenda-new-buffers nil)
  16871. (org-agenda-quit))
  16872. (defun org-save-all-org-buffers ()
  16873. "Save all Org-mode buffers without user confirmation."
  16874. (interactive)
  16875. (message "Saving all Org-mode buffers...")
  16876. (save-some-buffers t 'org-mode-p)
  16877. (message "Saving all Org-mode buffers... done"))
  16878. (defun org-agenda-redo ()
  16879. "Rebuild Agenda.
  16880. When this is the global TODO list, a prefix argument will be interpreted."
  16881. (interactive)
  16882. (let* ((org-agenda-keep-modes t)
  16883. (line (org-current-line))
  16884. (window-line (- line (org-current-line (window-start)))))
  16885. (message "Rebuilding agenda buffer...")
  16886. (eval org-agenda-redo-command)
  16887. (setq org-agenda-undo-list nil
  16888. org-agenda-pending-undo-list nil)
  16889. (message "Rebuilding agenda buffer...done")
  16890. (goto-line line)
  16891. (recenter window-line)))
  16892. (defun org-agenda-goto-today ()
  16893. "Go to today."
  16894. (interactive)
  16895. (org-agenda-check-type t 'timeline 'agenda)
  16896. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  16897. (cond
  16898. (tdpos (goto-char tdpos))
  16899. ((eq org-agenda-type 'agenda)
  16900. (let* ((sd (time-to-days (current-time)))
  16901. (comp (org-agenda-compute-time-span sd org-agenda-span))
  16902. (org-agenda-overriding-arguments org-agenda-last-arguments))
  16903. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  16904. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  16905. (org-agenda-redo)
  16906. (org-agenda-find-today-or-agenda)))
  16907. (t (error "Cannot find today")))))
  16908. (defun org-agenda-find-today-or-agenda ()
  16909. (goto-char
  16910. (or (text-property-any (point-min) (point-max) 'org-today t)
  16911. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  16912. (point-min))))
  16913. (defun org-agenda-later (arg)
  16914. "Go forward in time by thee current span.
  16915. With prefix ARG, go forward that many times the current span."
  16916. (interactive "p")
  16917. (org-agenda-check-type t 'agenda)
  16918. (let* ((span org-agenda-span)
  16919. (sd org-starting-day)
  16920. (greg (calendar-gregorian-from-absolute sd))
  16921. greg2 nd)
  16922. (cond
  16923. ((eq span 'day)
  16924. (setq sd (+ arg sd) nd 1))
  16925. ((eq span 'week)
  16926. (setq sd (+ (* 7 arg) sd) nd 7))
  16927. ((eq span 'month)
  16928. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  16929. sd (calendar-absolute-from-gregorian greg2))
  16930. (setcar greg2 (1+ (car greg2)))
  16931. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  16932. ((eq span 'year)
  16933. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  16934. sd (calendar-absolute-from-gregorian greg2))
  16935. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  16936. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  16937. (let ((org-agenda-overriding-arguments
  16938. (list (car org-agenda-last-arguments) sd nd t)))
  16939. (org-agenda-redo)
  16940. (org-agenda-find-today-or-agenda))))
  16941. (defun org-agenda-earlier (arg)
  16942. "Go backward in time by the current span.
  16943. With prefix ARG, go backward that many times the current span."
  16944. (interactive "p")
  16945. (org-agenda-later (- arg)))
  16946. (defun org-agenda-day-view ()
  16947. "Switch to daily view for agenda."
  16948. (interactive)
  16949. (setq org-agenda-ndays 1)
  16950. (org-agenda-change-time-span 'day))
  16951. (defun org-agenda-week-view ()
  16952. "Switch to daily view for agenda."
  16953. (interactive)
  16954. (setq org-agenda-ndays 7)
  16955. (org-agenda-change-time-span 'week))
  16956. (defun org-agenda-month-view ()
  16957. "Switch to daily view for agenda."
  16958. (interactive)
  16959. (org-agenda-change-time-span 'month))
  16960. (defun org-agenda-year-view ()
  16961. "Switch to daily view for agenda."
  16962. (interactive)
  16963. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  16964. (org-agenda-change-time-span 'year)
  16965. (error "Abort")))
  16966. (defun org-agenda-change-time-span (span)
  16967. "Change the agenda view to SPAN.
  16968. SPAN may be `day', `week', `month', `year'."
  16969. (org-agenda-check-type t 'agenda)
  16970. (if (equal org-agenda-span span)
  16971. (error "Viewing span is already \"%s\"" span))
  16972. (let* ((sd (or (get-text-property (point) 'day)
  16973. org-starting-day))
  16974. (computed (org-agenda-compute-time-span sd span))
  16975. (org-agenda-overriding-arguments
  16976. (list (car org-agenda-last-arguments)
  16977. (car computed) (cdr computed) t)))
  16978. (org-agenda-redo)
  16979. (org-agenda-find-today-or-agenda))
  16980. (org-agenda-set-mode-name)
  16981. (message "Switched to %s view" span))
  16982. (defun org-agenda-compute-time-span (sd span)
  16983. "Compute starting date and number of days for agenda.
  16984. SPAN may be `day', `week', `month', `year'. The return value
  16985. is a cons cell with the starting date and the number of days,
  16986. so that the date SD will be in that range."
  16987. (let* ((greg (calendar-gregorian-from-absolute sd))
  16988. nd)
  16989. (cond
  16990. ((eq span 'day)
  16991. (setq nd 1))
  16992. ((eq span 'week)
  16993. (let* ((nt (calendar-day-of-week
  16994. (calendar-gregorian-from-absolute sd)))
  16995. (d (if org-agenda-start-on-weekday
  16996. (- nt org-agenda-start-on-weekday)
  16997. 0)))
  16998. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  16999. (setq nd 7)))
  17000. ((eq span 'month)
  17001. (setq sd (calendar-absolute-from-gregorian
  17002. (list (car greg) 1 (nth 2 greg)))
  17003. nd (- (calendar-absolute-from-gregorian
  17004. (list (1+ (car greg)) 1 (nth 2 greg)))
  17005. sd)))
  17006. ((eq span 'year)
  17007. (setq sd (calendar-absolute-from-gregorian
  17008. (list 1 1 (nth 2 greg)))
  17009. nd (- (calendar-absolute-from-gregorian
  17010. (list 1 1 (1+ (nth 2 greg))))
  17011. sd))))
  17012. (cons sd nd)))
  17013. ;; FIXME: this no longer works if user make date format that starts with a blank
  17014. (defun org-agenda-next-date-line (&optional arg)
  17015. "Jump to the next line indicating a date in agenda buffer."
  17016. (interactive "p")
  17017. (org-agenda-check-type t 'agenda 'timeline)
  17018. (beginning-of-line 1)
  17019. (if (looking-at "^\\S-") (forward-char 1))
  17020. (if (not (re-search-forward "^\\S-" nil t arg))
  17021. (progn
  17022. (backward-char 1)
  17023. (error "No next date after this line in this buffer")))
  17024. (goto-char (match-beginning 0)))
  17025. (defun org-agenda-previous-date-line (&optional arg)
  17026. "Jump to the previous line indicating a date in agenda buffer."
  17027. (interactive "p")
  17028. (org-agenda-check-type t 'agenda 'timeline)
  17029. (beginning-of-line 1)
  17030. (if (not (re-search-backward "^\\S-" nil t arg))
  17031. (error "No previous date before this line in this buffer")))
  17032. ;; Initialize the highlight
  17033. (defvar org-hl (org-make-overlay 1 1))
  17034. (org-overlay-put org-hl 'face 'highlight)
  17035. (defun org-highlight (begin end &optional buffer)
  17036. "Highlight a region with overlay."
  17037. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  17038. org-hl begin end (or buffer (current-buffer))))
  17039. (defun org-unhighlight ()
  17040. "Detach overlay INDEX."
  17041. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  17042. ;; FIXME this is currently not used.
  17043. (defun org-highlight-until-next-command (beg end &optional buffer)
  17044. (org-highlight beg end buffer)
  17045. (add-hook 'pre-command-hook 'org-unhighlight-once))
  17046. (defun org-unhighlight-once ()
  17047. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  17048. (org-unhighlight))
  17049. (defun org-agenda-follow-mode ()
  17050. "Toggle follow mode in an agenda buffer."
  17051. (interactive)
  17052. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  17053. (org-agenda-set-mode-name)
  17054. (message "Follow mode is %s"
  17055. (if org-agenda-follow-mode "on" "off")))
  17056. (defun org-agenda-log-mode ()
  17057. "Toggle log mode in an agenda buffer."
  17058. (interactive)
  17059. (org-agenda-check-type t 'agenda 'timeline)
  17060. (setq org-agenda-show-log (not org-agenda-show-log))
  17061. (org-agenda-set-mode-name)
  17062. (org-agenda-redo)
  17063. (message "Log mode is %s"
  17064. (if org-agenda-show-log "on" "off")))
  17065. (defun org-agenda-toggle-diary ()
  17066. "Toggle diary inclusion in an agenda buffer."
  17067. (interactive)
  17068. (org-agenda-check-type t 'agenda)
  17069. (setq org-agenda-include-diary (not org-agenda-include-diary))
  17070. (org-agenda-redo)
  17071. (org-agenda-set-mode-name)
  17072. (message "Diary inclusion turned %s"
  17073. (if org-agenda-include-diary "on" "off")))
  17074. (defun org-agenda-toggle-time-grid ()
  17075. "Toggle time grid in an agenda buffer."
  17076. (interactive)
  17077. (org-agenda-check-type t 'agenda)
  17078. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  17079. (org-agenda-redo)
  17080. (org-agenda-set-mode-name)
  17081. (message "Time-grid turned %s"
  17082. (if org-agenda-use-time-grid "on" "off")))
  17083. (defun org-agenda-set-mode-name ()
  17084. "Set the mode name to indicate all the small mode settings."
  17085. (setq mode-name
  17086. (concat "Org-Agenda"
  17087. (if (equal org-agenda-ndays 1) " Day" "")
  17088. (if (equal org-agenda-ndays 7) " Week" "")
  17089. (if org-agenda-follow-mode " Follow" "")
  17090. (if org-agenda-include-diary " Diary" "")
  17091. (if org-agenda-use-time-grid " Grid" "")
  17092. (if org-agenda-show-log " Log" "")))
  17093. (force-mode-line-update))
  17094. (defun org-agenda-post-command-hook ()
  17095. (and (eolp) (not (bolp)) (backward-char 1))
  17096. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  17097. (if (and org-agenda-follow-mode
  17098. (get-text-property (point) 'org-marker))
  17099. (org-agenda-show)))
  17100. (defun org-agenda-show-priority ()
  17101. "Show the priority of the current item.
  17102. This priority is composed of the main priority given with the [#A] cookies,
  17103. and by additional input from the age of a schedules or deadline entry."
  17104. (interactive)
  17105. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  17106. (message "Priority is %d" (if pri pri -1000))))
  17107. (defun org-agenda-show-tags ()
  17108. "Show the tags applicable to the current item."
  17109. (interactive)
  17110. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  17111. (if tags
  17112. (message "Tags are :%s:"
  17113. (org-no-properties (mapconcat 'identity tags ":")))
  17114. (message "No tags associated with this line"))))
  17115. (defun org-agenda-goto (&optional highlight)
  17116. "Go to the Org-mode file which contains the item at point."
  17117. (interactive)
  17118. (let* ((marker (or (get-text-property (point) 'org-marker)
  17119. (org-agenda-error)))
  17120. (buffer (marker-buffer marker))
  17121. (pos (marker-position marker)))
  17122. (switch-to-buffer-other-window buffer)
  17123. (widen)
  17124. (goto-char pos)
  17125. (when (org-mode-p)
  17126. (org-show-context 'agenda)
  17127. (save-excursion
  17128. (and (outline-next-heading)
  17129. (org-flag-heading nil)))) ; show the next heading
  17130. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  17131. (defun org-agenda-kill ()
  17132. "Kill the entry or subtree belonging to the current agenda entry."
  17133. (interactive)
  17134. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  17135. (let* ((marker (or (get-text-property (point) 'org-marker)
  17136. (org-agenda-error)))
  17137. (buffer (marker-buffer marker))
  17138. (pos (marker-position marker))
  17139. (type (get-text-property (point) 'type))
  17140. dbeg dend (n 0) conf)
  17141. (org-with-remote-undo buffer
  17142. (with-current-buffer buffer
  17143. (save-excursion
  17144. (goto-char pos)
  17145. (if (and (org-mode-p) (not (member type '("sexp"))))
  17146. (setq dbeg (progn (org-back-to-heading t) (point))
  17147. dend (org-end-of-subtree t))
  17148. (setq dbeg (point-at-bol)
  17149. dend (min (point-max) (1+ (point-at-eol)))))
  17150. (goto-char dbeg)
  17151. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  17152. (setq conf (or (eq t org-agenda-confirm-kill)
  17153. (and (numberp org-agenda-confirm-kill)
  17154. (> n org-agenda-confirm-kill))))
  17155. (and conf
  17156. (not (y-or-n-p
  17157. (format "Delete entry with %d lines in buffer \"%s\"? "
  17158. n (buffer-name buffer))))
  17159. (error "Abort"))
  17160. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  17161. (with-current-buffer buffer (delete-region dbeg dend))
  17162. (message "Agenda item and source killed"))))
  17163. (defun org-agenda-archive ()
  17164. "Kill the entry or subtree belonging to the current agenda entry."
  17165. (interactive)
  17166. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  17167. (let* ((marker (or (get-text-property (point) 'org-marker)
  17168. (org-agenda-error)))
  17169. (buffer (marker-buffer marker))
  17170. (pos (marker-position marker)))
  17171. (org-with-remote-undo buffer
  17172. (with-current-buffer buffer
  17173. (if (org-mode-p)
  17174. (save-excursion
  17175. (goto-char pos)
  17176. (org-remove-subtree-entries-from-agenda)
  17177. (org-back-to-heading t)
  17178. (org-archive-subtree))
  17179. (error "Archiving works only in Org-mode files"))))))
  17180. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  17181. "Remove all lines in the agenda that correspond to a given subtree.
  17182. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  17183. If this information is not given, the function uses the tree at point."
  17184. (let ((buf (or buf (current-buffer))) m p)
  17185. (save-excursion
  17186. (unless (and beg end)
  17187. (org-back-to-heading t)
  17188. (setq beg (point))
  17189. (org-end-of-subtree t)
  17190. (setq end (point)))
  17191. (set-buffer (get-buffer org-agenda-buffer-name))
  17192. (save-excursion
  17193. (goto-char (point-max))
  17194. (beginning-of-line 1)
  17195. (while (not (bobp))
  17196. (when (and (setq m (get-text-property (point) 'org-marker))
  17197. (equal buf (marker-buffer m))
  17198. (setq p (marker-position m))
  17199. (>= p beg)
  17200. (<= p end))
  17201. (let (buffer-read-only)
  17202. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  17203. (beginning-of-line 0))))))
  17204. (defun org-agenda-open-link ()
  17205. "Follow the link in the current line, if any."
  17206. (interactive)
  17207. (let ((eol (point-at-eol)))
  17208. (save-excursion
  17209. (if (or (re-search-forward org-bracket-link-regexp eol t)
  17210. (re-search-forward org-angle-link-re eol t)
  17211. (re-search-forward org-plain-link-re eol t))
  17212. (call-interactively 'org-open-at-point)
  17213. (error "No link in current line")))))
  17214. (defun org-agenda-switch-to (&optional delete-other-windows)
  17215. "Go to the Org-mode file which contains the item at point."
  17216. (interactive)
  17217. (let* ((marker (or (get-text-property (point) 'org-marker)
  17218. (org-agenda-error)))
  17219. (buffer (marker-buffer marker))
  17220. (pos (marker-position marker)))
  17221. (switch-to-buffer buffer)
  17222. (and delete-other-windows (delete-other-windows))
  17223. (widen)
  17224. (goto-char pos)
  17225. (when (org-mode-p)
  17226. (org-show-context 'agenda)
  17227. (save-excursion
  17228. (and (outline-next-heading)
  17229. (org-flag-heading nil)))))) ; show the next heading
  17230. (defun org-agenda-goto-mouse (ev)
  17231. "Go to the Org-mode file which contains the item at the mouse click."
  17232. (interactive "e")
  17233. (mouse-set-point ev)
  17234. (org-agenda-goto))
  17235. (defun org-agenda-show ()
  17236. "Display the Org-mode file which contains the item at point."
  17237. (interactive)
  17238. (let ((win (selected-window)))
  17239. (org-agenda-goto t)
  17240. (select-window win)))
  17241. (defun org-agenda-recenter (arg)
  17242. "Display the Org-mode file which contains the item at point and recenter."
  17243. (interactive "P")
  17244. (let ((win (selected-window)))
  17245. (org-agenda-goto t)
  17246. (recenter arg)
  17247. (select-window win)))
  17248. (defun org-agenda-show-mouse (ev)
  17249. "Display the Org-mode file which contains the item at the mouse click."
  17250. (interactive "e")
  17251. (mouse-set-point ev)
  17252. (org-agenda-show))
  17253. (defun org-agenda-check-no-diary ()
  17254. "Check if the entry is a diary link and abort if yes."
  17255. (if (get-text-property (point) 'org-agenda-diary-link)
  17256. (org-agenda-error)))
  17257. (defun org-agenda-error ()
  17258. (error "Command not allowed in this line"))
  17259. (defun org-agenda-tree-to-indirect-buffer ()
  17260. "Show the subtree corresponding to the current entry in an indirect buffer.
  17261. This calls the command `org-tree-to-indirect-buffer' from the original
  17262. Org-mode buffer.
  17263. With numerical prefix arg ARG, go up to this level and then take that tree.
  17264. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  17265. dedicated frame)."
  17266. (interactive)
  17267. (org-agenda-check-no-diary)
  17268. (let* ((marker (or (get-text-property (point) 'org-marker)
  17269. (org-agenda-error)))
  17270. (buffer (marker-buffer marker))
  17271. (pos (marker-position marker)))
  17272. (with-current-buffer buffer
  17273. (save-excursion
  17274. (goto-char pos)
  17275. (call-interactively 'org-tree-to-indirect-buffer)))))
  17276. (defvar org-last-heading-marker (make-marker)
  17277. "Marker pointing to the headline that last changed its TODO state
  17278. by a remote command from the agenda.")
  17279. (defun org-agenda-todo-nextset ()
  17280. "Switch TODO entry to next sequence."
  17281. (interactive)
  17282. (org-agenda-todo 'nextset))
  17283. (defun org-agenda-todo-previousset ()
  17284. "Switch TODO entry to previous sequence."
  17285. (interactive)
  17286. (org-agenda-todo 'previousset))
  17287. (defun org-agenda-todo (&optional arg)
  17288. "Cycle TODO state of line at point, also in Org-mode file.
  17289. This changes the line at point, all other lines in the agenda referring to
  17290. the same tree node, and the headline of the tree node in the Org-mode file."
  17291. (interactive "P")
  17292. (org-agenda-check-no-diary)
  17293. (let* ((col (current-column))
  17294. (marker (or (get-text-property (point) 'org-marker)
  17295. (org-agenda-error)))
  17296. (buffer (marker-buffer marker))
  17297. (pos (marker-position marker))
  17298. (hdmarker (get-text-property (point) 'org-hd-marker))
  17299. (buffer-read-only nil)
  17300. newhead)
  17301. (org-with-remote-undo buffer
  17302. (with-current-buffer buffer
  17303. (widen)
  17304. (goto-char pos)
  17305. (org-show-context 'agenda)
  17306. (save-excursion
  17307. (and (outline-next-heading)
  17308. (org-flag-heading nil))) ; show the next heading
  17309. (org-todo arg)
  17310. (and (bolp) (forward-char 1))
  17311. (setq newhead (org-get-heading))
  17312. (save-excursion
  17313. (org-back-to-heading)
  17314. (move-marker org-last-heading-marker (point))))
  17315. (beginning-of-line 1)
  17316. (save-excursion
  17317. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  17318. (move-to-column col))))
  17319. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  17320. "Change all lines in the agenda buffer which match HDMARKER.
  17321. The new content of the line will be NEWHEAD (as modified by
  17322. `org-format-agenda-item'). HDMARKER is checked with
  17323. `equal' against all `org-hd-marker' text properties in the file.
  17324. If FIXFACE is non-nil, the face of each item is modified acording to
  17325. the new TODO state."
  17326. (let* ((buffer-read-only nil)
  17327. props m pl undone-face done-face finish new dotime cat tags)
  17328. (save-excursion
  17329. (goto-char (point-max))
  17330. (beginning-of-line 1)
  17331. (while (not finish)
  17332. (setq finish (bobp))
  17333. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  17334. (equal m hdmarker))
  17335. (setq props (text-properties-at (point))
  17336. dotime (get-text-property (point) 'dotime)
  17337. cat (get-text-property (point) 'org-category)
  17338. tags (get-text-property (point) 'tags)
  17339. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  17340. pl (get-text-property (point) 'prefix-length)
  17341. undone-face (get-text-property (point) 'undone-face)
  17342. done-face (get-text-property (point) 'done-face))
  17343. (move-to-column pl)
  17344. (cond
  17345. ((equal new "")
  17346. (beginning-of-line 1)
  17347. (and (looking-at ".*\n?") (replace-match "")))
  17348. ((looking-at ".*")
  17349. (replace-match new t t)
  17350. (beginning-of-line 1)
  17351. (add-text-properties (point-at-bol) (point-at-eol) props)
  17352. (when fixface
  17353. (add-text-properties
  17354. (point-at-bol) (point-at-eol)
  17355. (list 'face
  17356. (if org-last-todo-state-is-todo
  17357. undone-face done-face))))
  17358. (org-agenda-highlight-todo 'line)
  17359. (beginning-of-line 1))
  17360. (t (error "Line update did not work"))))
  17361. (beginning-of-line 0)))
  17362. (org-finalize-agenda)))
  17363. ;; FIXME: allow negative value for org-agenda-align-tags-to-column
  17364. ;; See the code in set-tags for the way to do this.
  17365. (defun org-agenda-align-tags (&optional line)
  17366. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  17367. (let ((buffer-read-only))
  17368. (save-excursion
  17369. (goto-char (if line (point-at-bol) (point-min)))
  17370. (while (re-search-forward (org-re "\\([ \t]+\\):[[:alnum:]_@:]+:[ \t]*$")
  17371. (if line (point-at-eol) nil) t)
  17372. (delete-region (match-beginning 1) (match-end 1))
  17373. (goto-char (match-beginning 1))
  17374. (insert (org-add-props
  17375. (make-string (max 1 (- org-agenda-align-tags-to-column
  17376. (current-column))) ?\ )
  17377. (text-properties-at (point))))))))
  17378. (defun org-agenda-priority-up ()
  17379. "Increase the priority of line at point, also in Org-mode file."
  17380. (interactive)
  17381. (org-agenda-priority 'up))
  17382. (defun org-agenda-priority-down ()
  17383. "Decrease the priority of line at point, also in Org-mode file."
  17384. (interactive)
  17385. (org-agenda-priority 'down))
  17386. (defun org-agenda-priority (&optional force-direction)
  17387. "Set the priority of line at point, also in Org-mode file.
  17388. This changes the line at point, all other lines in the agenda referring to
  17389. the same tree node, and the headline of the tree node in the Org-mode file."
  17390. (interactive)
  17391. (org-agenda-check-no-diary)
  17392. (let* ((marker (or (get-text-property (point) 'org-marker)
  17393. (org-agenda-error)))
  17394. (buffer (marker-buffer marker))
  17395. (pos (marker-position marker))
  17396. (hdmarker (get-text-property (point) 'org-hd-marker))
  17397. (buffer-read-only nil)
  17398. newhead)
  17399. (org-with-remote-undo buffer
  17400. (with-current-buffer buffer
  17401. (widen)
  17402. (goto-char pos)
  17403. (org-show-context 'agenda)
  17404. (save-excursion
  17405. (and (outline-next-heading)
  17406. (org-flag-heading nil))) ; show the next heading
  17407. (funcall 'org-priority force-direction)
  17408. (end-of-line 1)
  17409. (setq newhead (org-get-heading)))
  17410. (org-agenda-change-all-lines newhead hdmarker)
  17411. (beginning-of-line 1))))
  17412. (defun org-get-tags-at (&optional pos)
  17413. "Get a list of all headline tags applicable at POS.
  17414. POS defaults to point. If tags are inherited, the list contains
  17415. the targets in the same sequence as the headlines appear, i.e.
  17416. the tags of the current headline come last."
  17417. (interactive)
  17418. (let (tags)
  17419. (save-excursion
  17420. (save-restriction
  17421. (widen)
  17422. (goto-char (or pos (point)))
  17423. (save-match-data
  17424. (org-back-to-heading t)
  17425. (condition-case nil
  17426. (while t
  17427. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  17428. (setq tags (append (org-split-string
  17429. (org-match-string-no-properties 1) ":")
  17430. tags)))
  17431. (or org-use-tag-inheritance (error ""))
  17432. (org-up-heading-all 1))
  17433. (error nil))))
  17434. tags)))
  17435. ;; FIXME: should fix the tags property of the agenda line.
  17436. (defun org-agenda-set-tags ()
  17437. "Set tags for the current headline."
  17438. (interactive)
  17439. (org-agenda-check-no-diary)
  17440. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  17441. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  17442. (org-agenda-error)))
  17443. (buffer (marker-buffer hdmarker))
  17444. (pos (marker-position hdmarker))
  17445. (buffer-read-only nil)
  17446. newhead)
  17447. (org-with-remote-undo buffer
  17448. (with-current-buffer buffer
  17449. (widen)
  17450. (goto-char pos)
  17451. (save-excursion
  17452. (org-show-context 'agenda))
  17453. (save-excursion
  17454. (and (outline-next-heading)
  17455. (org-flag-heading nil))) ; show the next heading
  17456. (goto-char pos)
  17457. (call-interactively 'org-set-tags)
  17458. (end-of-line 1)
  17459. (setq newhead (org-get-heading)))
  17460. (org-agenda-change-all-lines newhead hdmarker)
  17461. (beginning-of-line 1))))
  17462. (defun org-agenda-toggle-archive-tag ()
  17463. "Toggle the archive tag for the current entry."
  17464. (interactive)
  17465. (org-agenda-check-no-diary)
  17466. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  17467. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  17468. (org-agenda-error)))
  17469. (buffer (marker-buffer hdmarker))
  17470. (pos (marker-position hdmarker))
  17471. (buffer-read-only nil)
  17472. newhead)
  17473. (org-with-remote-undo buffer
  17474. (with-current-buffer buffer
  17475. (widen)
  17476. (goto-char pos)
  17477. (org-show-context 'agenda)
  17478. (save-excursion
  17479. (and (outline-next-heading)
  17480. (org-flag-heading nil))) ; show the next heading
  17481. (call-interactively 'org-toggle-archive-tag)
  17482. (end-of-line 1)
  17483. (setq newhead (org-get-heading)))
  17484. (org-agenda-change-all-lines newhead hdmarker)
  17485. (beginning-of-line 1))))
  17486. (defun org-agenda-date-later (arg &optional what)
  17487. "Change the date of this item to one day later."
  17488. (interactive "p")
  17489. (org-agenda-check-type t 'agenda 'timeline)
  17490. (org-agenda-check-no-diary)
  17491. (let* ((marker (or (get-text-property (point) 'org-marker)
  17492. (org-agenda-error)))
  17493. (buffer (marker-buffer marker))
  17494. (pos (marker-position marker)))
  17495. (org-with-remote-undo buffer
  17496. (with-current-buffer buffer
  17497. (widen)
  17498. (goto-char pos)
  17499. (if (not (org-at-timestamp-p))
  17500. (error "Cannot find time stamp"))
  17501. (org-timestamp-change arg (or what 'day)))
  17502. (org-agenda-show-new-time marker org-last-changed-timestamp))
  17503. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  17504. (defun org-agenda-date-earlier (arg &optional what)
  17505. "Change the date of this item to one day earlier."
  17506. (interactive "p")
  17507. (org-agenda-date-later (- arg) what))
  17508. (defun org-agenda-show-new-time (marker stamp)
  17509. "Show new date stamp via text properties."
  17510. ;; We use text properties to make this undoable
  17511. (let ((buffer-read-only nil))
  17512. (setq stamp (concat " => " stamp))
  17513. (save-excursion
  17514. (goto-char (point-max))
  17515. (while (not (bobp))
  17516. (when (equal marker (get-text-property (point) 'org-marker))
  17517. (move-to-column (- (window-width) (length stamp)) t)
  17518. (if (featurep 'xemacs)
  17519. ;; Use `duplicable' property to trigger undo recording
  17520. (let ((ex (make-extent nil nil))
  17521. (gl (make-glyph stamp)))
  17522. (set-glyph-face gl 'secondary-selection)
  17523. (set-extent-properties
  17524. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  17525. (insert-extent ex (1- (point)) (point-at-eol)))
  17526. (add-text-properties
  17527. (1- (point)) (point-at-eol)
  17528. (list 'display (org-add-props stamp nil
  17529. 'face 'secondary-selection))))
  17530. (beginning-of-line 1))
  17531. (beginning-of-line 0)))))
  17532. (defun org-agenda-date-prompt (arg)
  17533. "Change the date of this item. Date is prompted for, with default today.
  17534. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  17535. be used to request time specification in the time stamp."
  17536. (interactive "P")
  17537. (org-agenda-check-type t 'agenda 'timeline)
  17538. (org-agenda-check-no-diary)
  17539. (let* ((marker (or (get-text-property (point) 'org-marker)
  17540. (org-agenda-error)))
  17541. (buffer (marker-buffer marker))
  17542. (pos (marker-position marker)))
  17543. (org-with-remote-undo buffer
  17544. (with-current-buffer buffer
  17545. (widen)
  17546. (goto-char pos)
  17547. (if (not (org-at-timestamp-p))
  17548. (error "Cannot find time stamp"))
  17549. (org-time-stamp arg)
  17550. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  17551. (defun org-agenda-schedule (arg)
  17552. "Schedule the item at point."
  17553. (interactive "P")
  17554. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  17555. (org-agenda-check-no-diary)
  17556. (let* ((marker (or (get-text-property (point) 'org-marker)
  17557. (org-agenda-error)))
  17558. (buffer (marker-buffer marker))
  17559. (pos (marker-position marker))
  17560. (org-insert-labeled-timestamps-at-point nil)
  17561. ts)
  17562. (org-with-remote-undo buffer
  17563. (with-current-buffer buffer
  17564. (widen)
  17565. (goto-char pos)
  17566. (setq ts (org-schedule))
  17567. (message "Item scheduled for %s" ts)))))
  17568. (defun org-agenda-deadline (arg)
  17569. "Schedule the item at point."
  17570. (interactive "P")
  17571. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  17572. (org-agenda-check-no-diary)
  17573. (let* ((marker (or (get-text-property (point) 'org-marker)
  17574. (org-agenda-error)))
  17575. (buffer (marker-buffer marker))
  17576. (pos (marker-position marker))
  17577. (org-insert-labeled-timestamps-at-point nil)
  17578. ts)
  17579. (org-with-remote-undo buffer
  17580. (with-current-buffer buffer
  17581. (widen)
  17582. (goto-char pos)
  17583. (setq ts (org-deadline))
  17584. (message "Deadline for this item set to %s" ts)))))
  17585. (defun org-get-heading ()
  17586. "Return the heading of the current entry, without the stars."
  17587. (save-excursion
  17588. (org-back-to-heading t)
  17589. (if (looking-at "\\*+[ \t]+\\([^\r\n]*\\)") (match-string 1) "")))
  17590. (defun org-agenda-clock-in (&optional arg)
  17591. "Start the clock on the currently selected item."
  17592. (interactive "P")
  17593. (org-agenda-check-no-diary)
  17594. (let* ((marker (or (get-text-property (point) 'org-marker)
  17595. (org-agenda-error)))
  17596. (pos (marker-position marker)))
  17597. (org-with-remote-undo (marker-buffer marker)
  17598. (with-current-buffer (marker-buffer marker)
  17599. (widen)
  17600. (goto-char pos)
  17601. (org-clock-in)))))
  17602. (defun org-agenda-clock-out (&optional arg)
  17603. "Stop the currently running clock."
  17604. (interactive "P")
  17605. (unless (marker-buffer org-clock-marker)
  17606. (error "No running clock"))
  17607. (org-with-remote-undo (marker-buffer org-clock-marker)
  17608. (org-clock-out)))
  17609. (defun org-agenda-clock-cancel (&optional arg)
  17610. "Cancel the currently running clock."
  17611. (interactive "P")
  17612. (unless (marker-buffer org-clock-marker)
  17613. (error "No running clock"))
  17614. (org-with-remote-undo (marker-buffer org-clock-marker)
  17615. (org-clock-cancel)))
  17616. (defun org-agenda-diary-entry ()
  17617. "Make a diary entry, like the `i' command from the calendar.
  17618. All the standard commands work: block, weekly etc."
  17619. (interactive)
  17620. (org-agenda-check-type t 'agenda 'timeline)
  17621. (require 'diary-lib)
  17622. (let* ((char (progn
  17623. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  17624. (read-char-exclusive)))
  17625. (cmd (cdr (assoc char
  17626. '((?d . insert-diary-entry)
  17627. (?w . insert-weekly-diary-entry)
  17628. (?m . insert-monthly-diary-entry)
  17629. (?y . insert-yearly-diary-entry)
  17630. (?a . insert-anniversary-diary-entry)
  17631. (?b . insert-block-diary-entry)
  17632. (?c . insert-cyclic-diary-entry)))))
  17633. (oldf (symbol-function 'calendar-cursor-to-date))
  17634. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  17635. (point (point))
  17636. (mark (or (mark t) (point))))
  17637. (unless cmd
  17638. (error "No command associated with <%c>" char))
  17639. (unless (and (get-text-property point 'day)
  17640. (or (not (equal ?b char))
  17641. (get-text-property mark 'day)))
  17642. (error "Don't know which date to use for diary entry"))
  17643. ;; We implement this by hacking the `calendar-cursor-to-date' function
  17644. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  17645. (let ((calendar-mark-ring
  17646. (list (calendar-gregorian-from-absolute
  17647. (or (get-text-property mark 'day)
  17648. (get-text-property point 'day))))))
  17649. (unwind-protect
  17650. (progn
  17651. (fset 'calendar-cursor-to-date
  17652. (lambda (&optional error)
  17653. (calendar-gregorian-from-absolute
  17654. (get-text-property point 'day))))
  17655. (call-interactively cmd))
  17656. (fset 'calendar-cursor-to-date oldf)))))
  17657. (defun org-agenda-execute-calendar-command (cmd)
  17658. "Execute a calendar command from the agenda, with the date associated to
  17659. the cursor position."
  17660. (org-agenda-check-type t 'agenda 'timeline)
  17661. (require 'diary-lib)
  17662. (unless (get-text-property (point) 'day)
  17663. (error "Don't know which date to use for calendar command"))
  17664. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  17665. (point (point))
  17666. (date (calendar-gregorian-from-absolute
  17667. (get-text-property point 'day)))
  17668. ;; the following 3 vars are needed in the calendar
  17669. (displayed-day (extract-calendar-day date))
  17670. (displayed-month (extract-calendar-month date))
  17671. (displayed-year (extract-calendar-year date)))
  17672. (unwind-protect
  17673. (progn
  17674. (fset 'calendar-cursor-to-date
  17675. (lambda (&optional error)
  17676. (calendar-gregorian-from-absolute
  17677. (get-text-property point 'day))))
  17678. (call-interactively cmd))
  17679. (fset 'calendar-cursor-to-date oldf))))
  17680. (defun org-agenda-phases-of-moon ()
  17681. "Display the phases of the moon for the 3 months around the cursor date."
  17682. (interactive)
  17683. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  17684. (defun org-agenda-holidays ()
  17685. "Display the holidays for the 3 months around the cursor date."
  17686. (interactive)
  17687. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  17688. (defun org-agenda-sunrise-sunset (arg)
  17689. "Display sunrise and sunset for the cursor date.
  17690. Latitude and longitude can be specified with the variables
  17691. `calendar-latitude' and `calendar-longitude'. When called with prefix
  17692. argument, latitude and longitude will be prompted for."
  17693. (interactive "P")
  17694. (let ((calendar-longitude (if arg nil calendar-longitude))
  17695. (calendar-latitude (if arg nil calendar-latitude))
  17696. (calendar-location-name
  17697. (if arg "the given coordinates" calendar-location-name)))
  17698. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  17699. (defun org-agenda-goto-calendar ()
  17700. "Open the Emacs calendar with the date at the cursor."
  17701. (interactive)
  17702. (org-agenda-check-type t 'agenda 'timeline)
  17703. (let* ((day (or (get-text-property (point) 'day)
  17704. (error "Don't know which date to open in calendar")))
  17705. (date (calendar-gregorian-from-absolute day))
  17706. (calendar-move-hook nil)
  17707. (view-calendar-holidays-initially nil)
  17708. (view-diary-entries-initially nil))
  17709. (calendar)
  17710. (calendar-goto-date date)))
  17711. (defun org-calendar-goto-agenda ()
  17712. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  17713. This is a command that has to be installed in `calendar-mode-map'."
  17714. (interactive)
  17715. (org-agenda-list nil (calendar-absolute-from-gregorian
  17716. (calendar-cursor-to-date))
  17717. nil))
  17718. (defun org-agenda-convert-date ()
  17719. (interactive)
  17720. (org-agenda-check-type t 'agenda 'timeline)
  17721. (let ((day (get-text-property (point) 'day))
  17722. date s)
  17723. (unless day
  17724. (error "Don't know which date to convert"))
  17725. (setq date (calendar-gregorian-from-absolute day))
  17726. (setq s (concat
  17727. "Gregorian: " (calendar-date-string date) "\n"
  17728. "ISO: " (calendar-iso-date-string date) "\n"
  17729. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  17730. "Julian: " (calendar-julian-date-string date) "\n"
  17731. "Astron. JD: " (calendar-astro-date-string date)
  17732. " (Julian date number at noon UTC)\n"
  17733. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  17734. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  17735. "French: " (calendar-french-date-string date) "\n"
  17736. "Mayan: " (calendar-mayan-date-string date) "\n"
  17737. "Coptic: " (calendar-coptic-date-string date) "\n"
  17738. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  17739. "Persian: " (calendar-persian-date-string date) "\n"
  17740. "Chinese: " (calendar-chinese-date-string date) "\n"))
  17741. (with-output-to-temp-buffer "*Dates*"
  17742. (princ s))
  17743. (if (fboundp 'fit-window-to-buffer)
  17744. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  17745. ;;;; Embedded LaTeX
  17746. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17747. "Keymap for the minor `org-cdlatex-mode'.")
  17748. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17749. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17750. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17751. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17752. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  17753. (defvar org-cdlatex-texmathp-advice-is-done nil
  17754. "Flag remembering if we have applied the advice to texmathp already.")
  17755. (define-minor-mode org-cdlatex-mode
  17756. "Toggle the minor `org-cdlatex-mode'.
  17757. This mode supports entering LaTeX environment and math in LaTeX fragments
  17758. in Org-mode.
  17759. \\{org-cdlatex-mode-map}"
  17760. nil " OCDL" nil
  17761. (when org-cdlatex-mode (require 'cdlatex))
  17762. (unless org-cdlatex-texmathp-advice-is-done
  17763. (setq org-cdlatex-texmathp-advice-is-done t)
  17764. (defadvice texmathp (around org-math-always-on activate)
  17765. "Always return t in org-mode buffers.
  17766. This is because we want to insert math symbols without dollars even outside
  17767. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17768. en embedded LaTeX fragement, let texmathp do its job.
  17769. \\[org-cdlatex-mode-map]"
  17770. (interactive)
  17771. (let (p)
  17772. (cond
  17773. ((not (org-mode-p)) ad-do-it)
  17774. ((eq this-command 'cdlatex-math-symbol)
  17775. (setq ad-return-value t
  17776. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17777. (t
  17778. (let ((p (org-inside-LaTeX-fragment-p)))
  17779. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17780. (setq ad-return-value t
  17781. texmathp-why '("Org-mode embedded math" . 0))
  17782. (if p ad-do-it)))))))))
  17783. (defun turn-on-org-cdlatex ()
  17784. "Unconditionally turn on `org-cdlatex-mode'."
  17785. (org-cdlatex-mode 1))
  17786. (defun org-inside-LaTeX-fragment-p ()
  17787. "Test if point is inside a LaTeX fragment.
  17788. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17789. sequence appearing also before point.
  17790. Even though the matchers for math are configurable, this function assumes
  17791. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17792. delimiters are skipped when they have been removed by customization.
  17793. The return value is nil, or a cons cell with the delimiter and
  17794. and the position of this delimiter.
  17795. This function does a reasonably good job, but can locally be fooled by
  17796. for example currency specifications. For example it will assume being in
  17797. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17798. fragments that are properly closed, but during editing, we have to live
  17799. with the uncertainty caused by missing closing delimiters. This function
  17800. looks only before point, not after."
  17801. (catch 'exit
  17802. (let ((pos (point))
  17803. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17804. (lim (progn
  17805. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17806. (point)))
  17807. dd-on str (start 0) m re)
  17808. (goto-char pos)
  17809. (when dodollar
  17810. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17811. re (nth 1 (assoc "$" org-latex-regexps)))
  17812. (while (string-match re str start)
  17813. (cond
  17814. ((= (match-end 0) (length str))
  17815. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17816. ((= (match-end 0) (- (length str) 5))
  17817. (throw 'exit nil))
  17818. (t (setq start (match-end 0))))))
  17819. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17820. (goto-char pos)
  17821. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17822. (and (match-beginning 2) (throw 'exit nil))
  17823. ;; count $$
  17824. (while (re-search-backward "\\$\\$" lim t)
  17825. (setq dd-on (not dd-on)))
  17826. (goto-char pos)
  17827. (if dd-on (cons "$$" m))))))
  17828. (defun org-try-cdlatex-tab ()
  17829. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17830. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17831. - inside a LaTeX fragment, or
  17832. - after the first word in a line, where an abbreviation expansion could
  17833. insert a LaTeX environment."
  17834. (when org-cdlatex-mode
  17835. (cond
  17836. ((save-excursion
  17837. (skip-chars-backward "a-zA-Z0-9*")
  17838. (skip-chars-backward " \t")
  17839. (bolp))
  17840. (cdlatex-tab) t)
  17841. ((org-inside-LaTeX-fragment-p)
  17842. (cdlatex-tab) t)
  17843. (t nil))))
  17844. (defun org-cdlatex-underscore-caret (&optional arg)
  17845. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17846. Revert to the normal definition outside of these fragments."
  17847. (interactive "P")
  17848. (if (org-inside-LaTeX-fragment-p)
  17849. (call-interactively 'cdlatex-sub-superscript)
  17850. (let (org-cdlatex-mode)
  17851. (call-interactively (key-binding (vector last-input-event))))))
  17852. (defun org-cdlatex-math-modify (&optional arg)
  17853. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17854. Revert to the normal definition outside of these fragments."
  17855. (interactive "P")
  17856. (if (org-inside-LaTeX-fragment-p)
  17857. (call-interactively 'cdlatex-math-modify)
  17858. (let (org-cdlatex-mode)
  17859. (call-interactively (key-binding (vector last-input-event))))))
  17860. (defvar org-latex-fragment-image-overlays nil
  17861. "List of overlays carrying the images of latex fragments.")
  17862. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17863. (defun org-remove-latex-fragment-image-overlays ()
  17864. "Remove all overlays with LaTeX fragment images in current buffer."
  17865. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  17866. (setq org-latex-fragment-image-overlays nil))
  17867. (defun org-preview-latex-fragment (&optional subtree)
  17868. "Preview the LaTeX fragment at point, or all locally or globally.
  17869. If the cursor is in a LaTeX fragment, create the image and overlay
  17870. it over the source code. If there is no fragment at point, display
  17871. all fragments in the current text, from one headline to the next. With
  17872. prefix SUBTREE, display all fragments in the current subtree. With a
  17873. double prefix `C-u C-u', or when the cursor is before the first headline,
  17874. display all fragments in the buffer.
  17875. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  17876. (interactive "P")
  17877. (org-remove-latex-fragment-image-overlays)
  17878. (save-excursion
  17879. (save-restriction
  17880. (let (beg end at msg)
  17881. (cond
  17882. ((or (equal subtree '(16))
  17883. (not (save-excursion
  17884. (re-search-backward (concat "^" outline-regexp) nil t))))
  17885. (setq beg (point-min) end (point-max)
  17886. msg "Creating images for buffer...%s"))
  17887. ((equal subtree '(4))
  17888. (org-back-to-heading)
  17889. (setq beg (point) end (org-end-of-subtree t)
  17890. msg "Creating images for subtree...%s"))
  17891. (t
  17892. (if (setq at (org-inside-LaTeX-fragment-p))
  17893. (goto-char (max (point-min) (- (cdr at) 2)))
  17894. (org-back-to-heading))
  17895. (setq beg (point) end (progn (outline-next-heading) (point))
  17896. msg (if at "Creating image...%s"
  17897. "Creating images for entry...%s"))))
  17898. (message msg "")
  17899. (narrow-to-region beg end)
  17900. (goto-char beg)
  17901. (org-format-latex
  17902. (concat "ltxpng/" (file-name-sans-extension
  17903. (file-name-nondirectory
  17904. buffer-file-name)))
  17905. default-directory 'overlays msg at 'forbuffer)
  17906. (message msg "done. Use `C-c C-c' to remove images.")))))
  17907. (defvar org-latex-regexps
  17908. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  17909. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  17910. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  17911. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  17912. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  17913. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  17914. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  17915. "Regular expressions for matching embedded LaTeX.")
  17916. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  17917. "Replace LaTeX fragments with links to an image, and produce images."
  17918. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17919. (let* ((prefixnodir (file-name-nondirectory prefix))
  17920. (absprefix (expand-file-name prefix dir))
  17921. (todir (file-name-directory absprefix))
  17922. (opt org-format-latex-options)
  17923. (matchers (plist-get opt :matchers))
  17924. (re-list org-latex-regexps)
  17925. (cnt 0) txt link beg end re e checkdir
  17926. m n block linkfile movefile ov)
  17927. ;; Check if there are old images files with this prefix, and remove them
  17928. (when (file-directory-p todir)
  17929. (mapc 'delete-file
  17930. (directory-files
  17931. todir 'full
  17932. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  17933. ;; Check the different regular expressions
  17934. (while (setq e (pop re-list))
  17935. (setq m (car e) re (nth 1 e) n (nth 2 e)
  17936. block (if (nth 3 e) "\n\n" ""))
  17937. (when (member m matchers)
  17938. (goto-char (point-min))
  17939. (while (re-search-forward re nil t)
  17940. (when (or (not at) (equal (cdr at) (match-beginning n)))
  17941. (setq txt (match-string n)
  17942. beg (match-beginning n) end (match-end n)
  17943. cnt (1+ cnt)
  17944. linkfile (format "%s_%04d.png" prefix cnt)
  17945. movefile (format "%s_%04d.png" absprefix cnt)
  17946. link (concat block "[[file:" linkfile "]]" block))
  17947. (if msg (message msg cnt))
  17948. (goto-char beg)
  17949. (unless checkdir ; make sure the directory exists
  17950. (setq checkdir t)
  17951. (or (file-directory-p todir) (make-directory todir)))
  17952. (org-create-formula-image
  17953. txt movefile opt forbuffer)
  17954. (if overlays
  17955. (progn
  17956. (setq ov (org-make-overlay beg end))
  17957. (if (featurep 'xemacs)
  17958. (progn
  17959. (org-overlay-put ov 'invisible t)
  17960. (org-overlay-put
  17961. ov 'end-glyph
  17962. (make-glyph (vector 'png :file movefile))))
  17963. (org-overlay-put
  17964. ov 'display
  17965. (list 'image :type 'png :file movefile :ascent 'center)))
  17966. (push ov org-latex-fragment-image-overlays)
  17967. (goto-char end))
  17968. (delete-region beg end)
  17969. (insert link))))))))
  17970. ;; This function borrows from Ganesh Swami's latex2png.el
  17971. (defun org-create-formula-image (string tofile options buffer)
  17972. (let* ((tmpdir (if (featurep 'xemacs)
  17973. (temp-directory)
  17974. temporary-file-directory))
  17975. (texfilebase (make-temp-name
  17976. (expand-file-name "orgtex" tmpdir)))
  17977. (texfile (concat texfilebase ".tex"))
  17978. (dvifile (concat texfilebase ".dvi"))
  17979. (pngfile (concat texfilebase ".png"))
  17980. (fnh (face-attribute 'default :height nil))
  17981. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17982. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17983. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17984. "Black"))
  17985. (bg (or (plist-get options (if buffer :background :html-background))
  17986. "Transparent")))
  17987. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  17988. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  17989. (with-temp-file texfile
  17990. (insert org-format-latex-header
  17991. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  17992. (let ((dir default-directory))
  17993. (condition-case nil
  17994. (progn
  17995. (cd tmpdir)
  17996. (call-process "latex" nil nil nil texfile))
  17997. (error nil))
  17998. (cd dir))
  17999. (if (not (file-exists-p dvifile))
  18000. (progn (message "Failed to create dvi file from %s" texfile) nil)
  18001. (call-process "dvipng" nil nil nil
  18002. "-E" "-fg" fg "-bg" bg
  18003. "-D" dpi
  18004. ;;"-x" scale "-y" scale
  18005. "-T" "tight"
  18006. "-o" pngfile
  18007. dvifile)
  18008. (if (not (file-exists-p pngfile))
  18009. (progn (message "Failed to create png file from %s" texfile) nil)
  18010. ;; Use the requested file name and clean up
  18011. (copy-file pngfile tofile 'replace)
  18012. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  18013. (delete-file (concat texfilebase e)))
  18014. pngfile))))
  18015. (defun org-dvipng-color (attr)
  18016. "Return an rgb color specification for dvipng."
  18017. (apply 'format "rgb %s %s %s"
  18018. (mapcar 'org-normalize-color
  18019. (color-values (face-attribute 'default attr nil)))))
  18020. (defun org-normalize-color (value)
  18021. "Return string to be used as color value for an RGB component."
  18022. (format "%g" (/ value 65535.0)))
  18023. ;;;; Exporting
  18024. ;;; Variables, constants, and parameter plists
  18025. (defconst org-level-max 20)
  18026. (defvar org-export-html-preamble nil
  18027. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  18028. (defvar org-export-html-postamble nil
  18029. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  18030. (defvar org-export-html-auto-preamble t
  18031. "Should default preamble be inserted? Set by publishing functions.")
  18032. (defvar org-export-html-auto-postamble t
  18033. "Should default postamble be inserted? Set by publishing functions.")
  18034. (defvar org-current-export-file nil) ; dynamically scoped parameter
  18035. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  18036. (defconst org-export-plist-vars
  18037. '((:language . org-export-default-language)
  18038. (:customtime . org-display-custom-times)
  18039. (:headline-levels . org-export-headline-levels)
  18040. (:section-numbers . org-export-with-section-numbers)
  18041. (:table-of-contents . org-export-with-toc)
  18042. (:preserve-breaks . org-export-preserve-breaks)
  18043. (:archived-trees . org-export-with-archived-trees)
  18044. (:emphasize . org-export-with-emphasize)
  18045. (:sub-superscript . org-export-with-sub-superscripts)
  18046. (:footnotes . org-export-with-footnotes)
  18047. (:property-drawer . org-export-with-property-drawer)
  18048. (:TeX-macros . org-export-with-TeX-macros)
  18049. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  18050. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  18051. (:fixed-width . org-export-with-fixed-width)
  18052. (:timestamps . org-export-with-timestamps)
  18053. (:tables . org-export-with-tables)
  18054. (:table-auto-headline . org-export-highlight-first-table-line)
  18055. (:style . org-export-html-style)
  18056. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  18057. (:convert-org-links . org-export-html-link-org-files-as-html)
  18058. (:inline-images . org-export-html-inline-images)
  18059. (:expand-quoted-html . org-export-html-expand)
  18060. (:timestamp . org-export-html-with-timestamp)
  18061. (:publishing-directory . org-export-publishing-directory)
  18062. (:preamble . org-export-html-preamble)
  18063. (:postamble . org-export-html-postamble)
  18064. (:auto-preamble . org-export-html-auto-preamble)
  18065. (:auto-postamble . org-export-html-auto-postamble)
  18066. (:author . user-full-name)
  18067. (:email . user-mail-address)))
  18068. (defun org-default-export-plist ()
  18069. "Return the property list with default settings for the export variables."
  18070. (let ((l org-export-plist-vars) rtn e)
  18071. (while (setq e (pop l))
  18072. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  18073. rtn))
  18074. (defun org-infile-export-plist ()
  18075. "Return the property list with file-local settings for export."
  18076. (save-excursion
  18077. (goto-char 0)
  18078. (let ((re (org-make-options-regexp
  18079. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  18080. p key val text options)
  18081. (while (re-search-forward re nil t)
  18082. (setq key (org-match-string-no-properties 1)
  18083. val (org-match-string-no-properties 2))
  18084. (cond
  18085. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  18086. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  18087. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  18088. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  18089. ((string-equal key "TEXT")
  18090. (setq text (if text (concat text "\n" val) val)))
  18091. ((string-equal key "OPTIONS") (setq options val))))
  18092. (setq p (plist-put p :text text))
  18093. (when options
  18094. (let ((op '(("H" . :headline-levels)
  18095. ("num" . :section-numbers)
  18096. ("toc" . :table-of-contents)
  18097. ("\\n" . :preserve-breaks)
  18098. ("@" . :expand-quoted-html)
  18099. (":" . :fixed-width)
  18100. ("|" . :tables)
  18101. ("^" . :sub-superscript)
  18102. ("f" . :footnotes)
  18103. ("p" . :property-drawer)
  18104. ("*" . :emphasize)
  18105. ("TeX" . :TeX-macros)
  18106. ("LaTeX" . :LaTeX-fragments)
  18107. ("skip" . :skip-before-1st-heading)))
  18108. o)
  18109. (while (setq o (pop op))
  18110. (if (string-match (concat (regexp-quote (car o))
  18111. ":\\([^ \t\n\r;,.]*\\)")
  18112. options)
  18113. (setq p (plist-put p (cdr o)
  18114. (car (read-from-string
  18115. (match-string 1 options)))))))))
  18116. p)))
  18117. (defun org-export-directory (type plist)
  18118. (let* ((val (plist-get plist :publishing-directory))
  18119. (dir (if (listp val)
  18120. (or (cdr (assoc type val)) ".")
  18121. val)))
  18122. dir))
  18123. (defun org-skip-comments (lines)
  18124. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  18125. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  18126. (re2 "^\\(\\*+\\)[ \t\n\r]")
  18127. (case-fold-search nil)
  18128. rtn line level)
  18129. (while (setq line (pop lines))
  18130. (cond
  18131. ((and (string-match re1 line)
  18132. (setq level (- (match-end 1) (match-beginning 1))))
  18133. ;; Beginning of a COMMENT subtree. Skip it.
  18134. (while (and (setq line (pop lines))
  18135. (or (not (string-match re2 line))
  18136. (> (- (match-end 1) (match-beginning 1)) level))))
  18137. (setq lines (cons line lines)))
  18138. ((string-match "^#" line)
  18139. ;; an ordinary comment line
  18140. )
  18141. ((and org-export-table-remove-special-lines
  18142. (string-match "^[ \t]*|" line)
  18143. (or (string-match "^[ \t]*| *[!_^] *|" line)
  18144. (and (string-match "| *<[0-9]+> *|" line)
  18145. (not (string-match "| *[^ <|]" line)))))
  18146. ;; a special table line that should be removed
  18147. )
  18148. (t (setq rtn (cons line rtn)))))
  18149. (nreverse rtn)))
  18150. (defun org-export (&optional arg)
  18151. (interactive)
  18152. (let ((help "[t] insert the export option template
  18153. \[v] limit export to visible part of outline tree
  18154. \[a] export as ASCII
  18155. \[h] export as HTML
  18156. \[H] export as HTML to temporary buffer
  18157. \[b] export as HTML and browse immediately
  18158. \[x] export as XOXO
  18159. \[i] export current file as iCalendar file
  18160. \[I] export all agenda files as iCalendar files
  18161. \[c] export agenda files into combined iCalendar file
  18162. \[F] publish current file
  18163. \[P] publish current project
  18164. \[X] publish... (project will be prompted for)
  18165. \[A] publish all projects")
  18166. (cmds
  18167. '((?t . org-insert-export-options-template)
  18168. (?v . org-export-visible)
  18169. (?a . org-export-as-ascii)
  18170. (?h . org-export-as-html)
  18171. (?b . org-export-as-html-and-open)
  18172. (?H . org-export-as-html-to-buffer)
  18173. (?R . org-export-region-as-html)
  18174. (?x . org-export-as-xoxo)
  18175. (?i . org-export-icalendar-this-file)
  18176. (?I . org-export-icalendar-all-agenda-files)
  18177. (?c . org-export-icalendar-combine-agenda-files)
  18178. (?F . org-publish-current-file)
  18179. (?P . org-publish-current-project)
  18180. (?X . org-publish)
  18181. (?A . org-publish-all)))
  18182. r1 r2 ass)
  18183. (save-window-excursion
  18184. (delete-other-windows)
  18185. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  18186. (princ help))
  18187. (message "Select command: ")
  18188. (setq r1 (read-char-exclusive)))
  18189. (setq r2 (if (< r1 27) (+ r1 96) r1))
  18190. (if (setq ass (assq r2 cmds))
  18191. (call-interactively (cdr ass))
  18192. (error "No command associated with key %c" r1))))
  18193. (defconst org-html-entities
  18194. '(("nbsp")
  18195. ("iexcl")
  18196. ("cent")
  18197. ("pound")
  18198. ("curren")
  18199. ("yen")
  18200. ("brvbar")
  18201. ("vert" . "&#124;")
  18202. ("sect")
  18203. ("uml")
  18204. ("copy")
  18205. ("ordf")
  18206. ("laquo")
  18207. ("not")
  18208. ("shy")
  18209. ("reg")
  18210. ("macr")
  18211. ("deg")
  18212. ("plusmn")
  18213. ("sup2")
  18214. ("sup3")
  18215. ("acute")
  18216. ("micro")
  18217. ("para")
  18218. ("middot")
  18219. ("odot"."o")
  18220. ("star"."*")
  18221. ("cedil")
  18222. ("sup1")
  18223. ("ordm")
  18224. ("raquo")
  18225. ("frac14")
  18226. ("frac12")
  18227. ("frac34")
  18228. ("iquest")
  18229. ("Agrave")
  18230. ("Aacute")
  18231. ("Acirc")
  18232. ("Atilde")
  18233. ("Auml")
  18234. ("Aring") ("AA"."&Aring;")
  18235. ("AElig")
  18236. ("Ccedil")
  18237. ("Egrave")
  18238. ("Eacute")
  18239. ("Ecirc")
  18240. ("Euml")
  18241. ("Igrave")
  18242. ("Iacute")
  18243. ("Icirc")
  18244. ("Iuml")
  18245. ("ETH")
  18246. ("Ntilde")
  18247. ("Ograve")
  18248. ("Oacute")
  18249. ("Ocirc")
  18250. ("Otilde")
  18251. ("Ouml")
  18252. ("times")
  18253. ("Oslash")
  18254. ("Ugrave")
  18255. ("Uacute")
  18256. ("Ucirc")
  18257. ("Uuml")
  18258. ("Yacute")
  18259. ("THORN")
  18260. ("szlig")
  18261. ("agrave")
  18262. ("aacute")
  18263. ("acirc")
  18264. ("atilde")
  18265. ("auml")
  18266. ("aring")
  18267. ("aelig")
  18268. ("ccedil")
  18269. ("egrave")
  18270. ("eacute")
  18271. ("ecirc")
  18272. ("euml")
  18273. ("igrave")
  18274. ("iacute")
  18275. ("icirc")
  18276. ("iuml")
  18277. ("eth")
  18278. ("ntilde")
  18279. ("ograve")
  18280. ("oacute")
  18281. ("ocirc")
  18282. ("otilde")
  18283. ("ouml")
  18284. ("divide")
  18285. ("oslash")
  18286. ("ugrave")
  18287. ("uacute")
  18288. ("ucirc")
  18289. ("uuml")
  18290. ("yacute")
  18291. ("thorn")
  18292. ("yuml")
  18293. ("fnof")
  18294. ("Alpha")
  18295. ("Beta")
  18296. ("Gamma")
  18297. ("Delta")
  18298. ("Epsilon")
  18299. ("Zeta")
  18300. ("Eta")
  18301. ("Theta")
  18302. ("Iota")
  18303. ("Kappa")
  18304. ("Lambda")
  18305. ("Mu")
  18306. ("Nu")
  18307. ("Xi")
  18308. ("Omicron")
  18309. ("Pi")
  18310. ("Rho")
  18311. ("Sigma")
  18312. ("Tau")
  18313. ("Upsilon")
  18314. ("Phi")
  18315. ("Chi")
  18316. ("Psi")
  18317. ("Omega")
  18318. ("alpha")
  18319. ("beta")
  18320. ("gamma")
  18321. ("delta")
  18322. ("epsilon")
  18323. ("varepsilon"."&epsilon;")
  18324. ("zeta")
  18325. ("eta")
  18326. ("theta")
  18327. ("iota")
  18328. ("kappa")
  18329. ("lambda")
  18330. ("mu")
  18331. ("nu")
  18332. ("xi")
  18333. ("omicron")
  18334. ("pi")
  18335. ("rho")
  18336. ("sigmaf") ("varsigma"."&sigmaf;")
  18337. ("sigma")
  18338. ("tau")
  18339. ("upsilon")
  18340. ("phi")
  18341. ("chi")
  18342. ("psi")
  18343. ("omega")
  18344. ("thetasym") ("vartheta"."&thetasym;")
  18345. ("upsih")
  18346. ("piv")
  18347. ("bull") ("bullet"."&bull;")
  18348. ("hellip") ("dots"."&hellip;")
  18349. ("prime")
  18350. ("Prime")
  18351. ("oline")
  18352. ("frasl")
  18353. ("weierp")
  18354. ("image")
  18355. ("real")
  18356. ("trade")
  18357. ("alefsym")
  18358. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  18359. ("uarr") ("uparrow"."&uarr;")
  18360. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  18361. ("darr")("downarrow"."&darr;")
  18362. ("harr") ("leftrightarrow"."&harr;")
  18363. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  18364. ("lArr") ("Leftarrow"."&lArr;")
  18365. ("uArr") ("Uparrow"."&uArr;")
  18366. ("rArr") ("Rightarrow"."&rArr;")
  18367. ("dArr") ("Downarrow"."&dArr;")
  18368. ("hArr") ("Leftrightarrow"."&hArr;")
  18369. ("forall")
  18370. ("part") ("partial"."&part;")
  18371. ("exist") ("exists"."&exist;")
  18372. ("empty") ("emptyset"."&empty;")
  18373. ("nabla")
  18374. ("isin") ("in"."&isin;")
  18375. ("notin")
  18376. ("ni")
  18377. ("prod")
  18378. ("sum")
  18379. ("minus")
  18380. ("lowast") ("ast"."&lowast;")
  18381. ("radic")
  18382. ("prop") ("proptp"."&prop;")
  18383. ("infin") ("infty"."&infin;")
  18384. ("ang") ("angle"."&ang;")
  18385. ("and") ("vee"."&and;")
  18386. ("or") ("wedge"."&or;")
  18387. ("cap")
  18388. ("cup")
  18389. ("int")
  18390. ("there4")
  18391. ("sim")
  18392. ("cong") ("simeq"."&cong;")
  18393. ("asymp")("approx"."&asymp;")
  18394. ("ne") ("neq"."&ne;")
  18395. ("equiv")
  18396. ("le")
  18397. ("ge")
  18398. ("sub") ("subset"."&sub;")
  18399. ("sup") ("supset"."&sup;")
  18400. ("nsub")
  18401. ("sube")
  18402. ("supe")
  18403. ("oplus")
  18404. ("otimes")
  18405. ("perp")
  18406. ("sdot") ("cdot"."&sdot;")
  18407. ("lceil")
  18408. ("rceil")
  18409. ("lfloor")
  18410. ("rfloor")
  18411. ("lang")
  18412. ("rang")
  18413. ("loz") ("Diamond"."&loz;")
  18414. ("spades") ("spadesuit"."&spades;")
  18415. ("clubs") ("clubsuit"."&clubs;")
  18416. ("hearts") ("diamondsuit"."&hearts;")
  18417. ("diams") ("diamondsuit"."&diams;")
  18418. ("quot")
  18419. ("amp")
  18420. ("lt")
  18421. ("gt")
  18422. ("OElig")
  18423. ("oelig")
  18424. ("Scaron")
  18425. ("scaron")
  18426. ("Yuml")
  18427. ("circ")
  18428. ("tilde")
  18429. ("ensp")
  18430. ("emsp")
  18431. ("thinsp")
  18432. ("zwnj")
  18433. ("zwj")
  18434. ("lrm")
  18435. ("rlm")
  18436. ("ndash")
  18437. ("mdash")
  18438. ("lsquo")
  18439. ("rsquo")
  18440. ("sbquo")
  18441. ("ldquo")
  18442. ("rdquo")
  18443. ("bdquo")
  18444. ("dagger")
  18445. ("Dagger")
  18446. ("permil")
  18447. ("lsaquo")
  18448. ("rsaquo")
  18449. ("euro")
  18450. ("arccos"."arccos")
  18451. ("arcsin"."arcsin")
  18452. ("arctan"."arctan")
  18453. ("arg"."arg")
  18454. ("cos"."cos")
  18455. ("cosh"."cosh")
  18456. ("cot"."cot")
  18457. ("coth"."coth")
  18458. ("csc"."csc")
  18459. ("deg"."deg")
  18460. ("det"."det")
  18461. ("dim"."dim")
  18462. ("exp"."exp")
  18463. ("gcd"."gcd")
  18464. ("hom"."hom")
  18465. ("inf"."inf")
  18466. ("ker"."ker")
  18467. ("lg"."lg")
  18468. ("lim"."lim")
  18469. ("liminf"."liminf")
  18470. ("limsup"."limsup")
  18471. ("ln"."ln")
  18472. ("log"."log")
  18473. ("max"."max")
  18474. ("min"."min")
  18475. ("Pr"."Pr")
  18476. ("sec"."sec")
  18477. ("sin"."sin")
  18478. ("sinh"."sinh")
  18479. ("sup"."sup")
  18480. ("tan"."tan")
  18481. ("tanh"."tanh")
  18482. )
  18483. "Entities for TeX->HTML translation.
  18484. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  18485. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  18486. In that case, \"\\ent\" will be translated to \"&other;\".
  18487. The list contains HTML entities for Latin-1, Greek and other symbols.
  18488. It is supplemented by a number of commonly used TeX macros with appropriate
  18489. translations. There is currently no way for users to extend this.")
  18490. ;;; General functions for all backends
  18491. (defun org-cleaned-string-for-export (string &rest parameters)
  18492. "Cleanup a buffer substring so that links can be created safely."
  18493. (interactive)
  18494. (let* ((re-radio (and org-target-link-regexp
  18495. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  18496. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  18497. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  18498. (re-archive (concat ":" org-archive-tag ":"))
  18499. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  18500. (htmlp (plist-get parameters :for-html))
  18501. (inhibit-read-only t)
  18502. (outline-regexp "\\*+ ")
  18503. a b
  18504. rtn p)
  18505. (save-excursion
  18506. (set-buffer (get-buffer-create " org-mode-tmp"))
  18507. (erase-buffer)
  18508. (insert string)
  18509. ;; Remove license-to-kill stuff
  18510. (while (setq p (text-property-any (point-min) (point-max)
  18511. :org-license-to-kill t))
  18512. (delete-region p (next-single-property-change p :org-license-to-kill)))
  18513. (let ((org-inhibit-startup t)) (org-mode))
  18514. (untabify (point-min) (point-max))
  18515. ;; Get the correct stuff before the first headline
  18516. (when (plist-get parameters :skip-before-1st-heading)
  18517. (goto-char (point-min))
  18518. (when (re-search-forward "^\\*+[ \t]" nil t)
  18519. (delete-region (point-min) (match-beginning 0))
  18520. (goto-char (point-min))
  18521. (insert "\n")))
  18522. (when (plist-get parameters :add-text)
  18523. (goto-char (point-min))
  18524. (insert (plist-get parameters :add-text) "\n"))
  18525. ;; Get rid of archived trees
  18526. (when (not (eq org-export-with-archived-trees t))
  18527. (goto-char (point-min))
  18528. (while (re-search-forward re-archive nil t)
  18529. (if (not (org-on-heading-p t))
  18530. (org-end-of-subtree t)
  18531. (beginning-of-line 1)
  18532. (setq a (if org-export-with-archived-trees
  18533. (1+ (point-at-eol)) (point))
  18534. b (org-end-of-subtree t))
  18535. (if (> b a) (delete-region a b)))))
  18536. ;; Get rid of property drawers
  18537. (unless org-export-with-property-drawer
  18538. (goto-char (point-min))
  18539. (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t)
  18540. (replace-match "")))
  18541. ;; Protect stuff from HTML processing
  18542. (goto-char (point-min))
  18543. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  18544. (add-text-properties (match-beginning 0) (match-end 0)
  18545. '(org-protected t)))
  18546. (when htmlp
  18547. (goto-char (point-min))
  18548. (while (re-search-forward "^#\\+HTML:[ \t]*\\(.*\\)" nil t)
  18549. (replace-match "\\1" t)
  18550. (add-text-properties
  18551. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  18552. '(org-protected t))))
  18553. (goto-char (point-min))
  18554. (while (re-search-forward
  18555. "^#\\+BEGIN_HTML\\>.*\\(\\(\n.*\\)*?\n\\)#\\+END_HTML\\>.*\n?" nil t)
  18556. (if htmlp
  18557. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  18558. '(org-protected t))
  18559. (delete-region (match-beginning 0) (match-end 0))))
  18560. (goto-char (point-min))
  18561. (while (re-search-forward re-quote nil t)
  18562. (goto-char (match-beginning 0))
  18563. (end-of-line 1)
  18564. (add-text-properties (point) (org-end-of-subtree t)
  18565. '(org-protected t)))
  18566. ;; Find targets in comments and move them out of comments,
  18567. ;; but mark them as targets that should be invisible
  18568. (goto-char (point-min))
  18569. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  18570. (replace-match "\\1(INVISIBLE)"))
  18571. ;; Remove comments
  18572. (goto-char (point-min))
  18573. (while (re-search-forward "^#.*\n?" nil t)
  18574. (replace-match ""))
  18575. ;; Find matches for radio targets and turn them into internal links
  18576. (goto-char (point-min))
  18577. (when re-radio
  18578. (while (re-search-forward re-radio nil t)
  18579. (org-if-unprotected
  18580. (replace-match "\\1[[\\2]]"))))
  18581. ;; Find all links that contain a newline and put them into a single line
  18582. (goto-char (point-min))
  18583. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  18584. (org-if-unprotected
  18585. (replace-match "\\1 \\3")
  18586. (goto-char (match-beginning 0))))
  18587. ;; Convert LaTeX fragments to images
  18588. (when (plist-get parameters :LaTeX-fragments)
  18589. (org-format-latex
  18590. (concat "ltxpng/" (file-name-sans-extension
  18591. (file-name-nondirectory
  18592. org-current-export-file)))
  18593. org-current-export-dir nil "Creating LaTeX image %s"))
  18594. (message "Exporting...")
  18595. ;; Normalize links: Convert angle and plain links into bracket links
  18596. ;; Expand link abbreviations
  18597. (goto-char (point-min))
  18598. (while (re-search-forward re-plain-link nil t)
  18599. (goto-char (1- (match-end 0)))
  18600. (org-if-unprotected
  18601. (replace-match
  18602. (concat
  18603. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  18604. t t)))
  18605. (goto-char (point-min))
  18606. (while (re-search-forward re-angle-link nil t)
  18607. (goto-char (1- (match-end 0)))
  18608. (org-if-unprotected
  18609. (replace-match
  18610. (concat
  18611. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  18612. t t)))
  18613. (goto-char (point-min))
  18614. (while (re-search-forward org-bracket-link-regexp nil t)
  18615. (org-if-unprotected
  18616. (replace-match
  18617. (concat "[[" (save-match-data
  18618. (org-link-expand-abbrev (match-string 1)))
  18619. "]"
  18620. (if (match-end 3)
  18621. (match-string 2)
  18622. (concat "[" (match-string 1) "]"))
  18623. "]")
  18624. t t)))
  18625. ;; Find multiline emphasis and put them into single line
  18626. (when (plist-get parameters :emph-multiline)
  18627. (goto-char (point-min))
  18628. (while (re-search-forward org-emph-re nil t)
  18629. (if (not (= (char-after (match-beginning 3))
  18630. (char-after (match-beginning 4))))
  18631. (org-if-unprotected
  18632. (subst-char-in-region (match-beginning 0) (match-end 0)
  18633. ?\n ?\ t)
  18634. (goto-char (1- (match-end 0))))
  18635. (goto-char (1+ (match-beginning 0))))))
  18636. (setq rtn (buffer-string)))
  18637. (kill-buffer " org-mode-tmp")
  18638. rtn))
  18639. (defun org-export-grab-title-from-buffer ()
  18640. "Get a title for the current document, from looking at the buffer."
  18641. (let (buffer-read-only)
  18642. (save-excursion
  18643. (goto-char (point-min))
  18644. (let ((end (save-excursion (outline-next-heading) (point))))
  18645. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  18646. ;; Mark the line so that it will not be exported as normal text.
  18647. (org-unmodified
  18648. (add-text-properties (match-beginning 0) (match-end 0)
  18649. (list :org-license-to-kill t)))
  18650. ;; Return the title string
  18651. (org-trim (match-string 0)))))))
  18652. (defun org-solidify-link-text (s &optional alist)
  18653. "Take link text and make a safe target out of it."
  18654. (save-match-data
  18655. (let* ((rtn
  18656. (mapconcat
  18657. 'identity
  18658. (org-split-string s "[ \t\r\n]+") "--"))
  18659. (a (assoc rtn alist)))
  18660. (or (cdr a) rtn))))
  18661. ;; Variable holding the vector with section numbers
  18662. (defvar org-section-numbers (make-vector org-level-max 0))
  18663. (defun org-init-section-numbers ()
  18664. "Initialize the vector for the section numbers."
  18665. (let* ((level -1)
  18666. (numbers (nreverse (org-split-string "" "\\.")))
  18667. (depth (1- (length org-section-numbers)))
  18668. (i depth) number-string)
  18669. (while (>= i 0)
  18670. (if (> i level)
  18671. (aset org-section-numbers i 0)
  18672. (setq number-string (or (car numbers) "0"))
  18673. (if (string-match "\\`[A-Z]\\'" number-string)
  18674. (aset org-section-numbers i
  18675. (- (string-to-char number-string) ?A -1))
  18676. (aset org-section-numbers i (string-to-number number-string)))
  18677. (pop numbers))
  18678. (setq i (1- i)))))
  18679. (defun org-section-number (&optional level)
  18680. "Return a string with the current section number.
  18681. When LEVEL is non-nil, increase section numbers on that level."
  18682. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  18683. (when level
  18684. (when (> level -1)
  18685. (aset org-section-numbers
  18686. level (1+ (aref org-section-numbers level))))
  18687. (setq idx (1+ level))
  18688. (while (<= idx depth)
  18689. (if (not (= idx 1))
  18690. (aset org-section-numbers idx 0))
  18691. (setq idx (1+ idx))))
  18692. (setq idx 0)
  18693. (while (<= idx depth)
  18694. (setq n (aref org-section-numbers idx))
  18695. (setq string (concat string (if (not (string= string "")) "." "")
  18696. (int-to-string n)))
  18697. (setq idx (1+ idx)))
  18698. (save-match-data
  18699. (if (string-match "\\`\\([@0]\\.\\)+" string)
  18700. (setq string (replace-match "" t nil string)))
  18701. (if (string-match "\\(\\.0\\)+\\'" string)
  18702. (setq string (replace-match "" t nil string))))
  18703. string))
  18704. ;;; ASCII export
  18705. (defvar org-last-level nil) ; dynamically scoped variable
  18706. (defvar org-levels-open nil) ; dynamically scoped parameter
  18707. (defvar org-ascii-current-indentation nil) ; For communication
  18708. (defun org-export-as-ascii (arg)
  18709. "Export the outline as a pretty ASCII file.
  18710. If there is an active region, export only the region.
  18711. The prefix ARG specifies how many levels of the outline should become
  18712. underlined headlines. The default is 3."
  18713. (interactive "P")
  18714. (setq-default org-todo-line-regexp org-todo-line-regexp)
  18715. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  18716. (org-infile-export-plist)))
  18717. (custom-times org-display-custom-times)
  18718. (org-ascii-current-indentation '(0 . 0))
  18719. (level 0) line txt
  18720. (umax nil)
  18721. (umax-toc nil)
  18722. (case-fold-search nil)
  18723. (filename (concat (file-name-as-directory
  18724. (org-export-directory :ascii opt-plist))
  18725. (file-name-sans-extension
  18726. (file-name-nondirectory buffer-file-name))
  18727. ".txt"))
  18728. (buffer (find-file-noselect filename))
  18729. (org-levels-open (make-vector org-level-max nil))
  18730. (odd org-odd-levels-only)
  18731. (date (format-time-string "%Y/%m/%d" (current-time)))
  18732. (time (format-time-string "%X" (org-current-time)))
  18733. (author (plist-get opt-plist :author))
  18734. (title (or (plist-get opt-plist :title)
  18735. (and (not
  18736. (plist-get opt-plist :skip-before-1st-heading))
  18737. (org-export-grab-title-from-buffer))
  18738. (file-name-sans-extension
  18739. (file-name-nondirectory buffer-file-name))))
  18740. (email (plist-get opt-plist :email))
  18741. (language (plist-get opt-plist :language))
  18742. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  18743. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  18744. (todo nil)
  18745. (lang-words nil)
  18746. (region
  18747. (buffer-substring
  18748. (if (org-region-active-p) (region-beginning) (point-min))
  18749. (if (org-region-active-p) (region-end) (point-max))))
  18750. (lines (org-skip-comments
  18751. (org-split-string
  18752. (org-cleaned-string-for-export
  18753. region
  18754. :skip-before-1st-heading
  18755. (plist-get opt-plist :skip-before-1st-heading)
  18756. :add-text (plist-get opt-plist :text))
  18757. "[\r\n]"))) ;; FIXME: why \r here???/
  18758. thetoc have-headings first-heading-pos
  18759. table-open table-buffer)
  18760. (let (buffer-read-only)
  18761. (org-unmodified
  18762. (remove-text-properties (point-min) (point-max)
  18763. '(:org-license-to-kill t))))
  18764. (setq org-last-level 1)
  18765. (org-init-section-numbers)
  18766. (find-file-noselect filename)
  18767. (setq lang-words (or (assoc language org-export-language-setup)
  18768. (assoc "en" org-export-language-setup)))
  18769. (switch-to-buffer-other-window buffer)
  18770. (erase-buffer)
  18771. (fundamental-mode)
  18772. ;; create local variables for all options, to make sure all called
  18773. ;; functions get the correct information
  18774. (mapcar (lambda (x)
  18775. (set (make-local-variable (cdr x))
  18776. (plist-get opt-plist (car x))))
  18777. org-export-plist-vars)
  18778. (org-set-local 'org-odd-levels-only odd)
  18779. (setq umax (if arg (prefix-numeric-value arg)
  18780. org-export-headline-levels))
  18781. (setq umax-toc (if (integerp org-export-with-toc)
  18782. (min org-export-with-toc umax)
  18783. umax))
  18784. ;; File header
  18785. (if title (org-insert-centered title ?=))
  18786. (insert "\n")
  18787. (if (or author email)
  18788. (insert (concat (nth 1 lang-words) ": " (or author "")
  18789. (if email (concat " <" email ">") "")
  18790. "\n")))
  18791. (if (and date time)
  18792. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  18793. (insert "\n\n")
  18794. (if org-export-with-toc
  18795. (progn
  18796. (push (concat (nth 3 lang-words) "\n") thetoc)
  18797. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  18798. (mapcar '(lambda (line)
  18799. (if (string-match org-todo-line-regexp
  18800. line)
  18801. ;; This is a headline
  18802. (progn
  18803. (setq have-headings t)
  18804. (setq level (- (match-end 1) (match-beginning 1))
  18805. level (org-tr-level level)
  18806. txt (match-string 3 line)
  18807. todo
  18808. (or (and org-export-mark-todo-in-toc
  18809. (match-beginning 2)
  18810. (not (member (match-string 2 line)
  18811. org-done-keywords)))
  18812. ; TODO, not DONE
  18813. (and org-export-mark-todo-in-toc
  18814. (= level umax-toc)
  18815. (org-search-todo-below
  18816. line lines level))))
  18817. (setq txt (org-html-expand-for-ascii txt))
  18818. (if (and (memq org-export-with-tags '(not-in-toc nil))
  18819. (string-match
  18820. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  18821. txt))
  18822. (setq txt (replace-match "" t t txt)))
  18823. (if (string-match quote-re0 txt)
  18824. (setq txt (replace-match "" t t txt)))
  18825. (if org-export-with-section-numbers
  18826. (setq txt (concat (org-section-number level)
  18827. " " txt)))
  18828. (if (<= level umax-toc)
  18829. (progn
  18830. (push
  18831. (concat
  18832. (make-string (* (1- level) 4) ?\ )
  18833. (format (if todo "%s (*)\n" "%s\n") txt))
  18834. thetoc)
  18835. (setq org-last-level level))
  18836. ))))
  18837. lines)
  18838. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  18839. (org-init-section-numbers)
  18840. (while (setq line (pop lines))
  18841. ;; Remove the quoted HTML tags.
  18842. (setq line (org-html-expand-for-ascii line))
  18843. ;; Remove targets
  18844. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  18845. (setq line (replace-match "" t t line)))
  18846. ;; Replace internal links
  18847. (while (string-match org-bracket-link-regexp line)
  18848. (setq line (replace-match
  18849. (if (match-end 3) "[\\3]" "[\\1]")
  18850. t nil line)))
  18851. (when custom-times
  18852. (setq line (org-translate-time line)))
  18853. (cond
  18854. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  18855. ;; a Headline
  18856. (setq first-heading-pos (or first-heading-pos (point)))
  18857. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  18858. txt (match-string 2 line))
  18859. (org-ascii-level-start level txt umax lines))
  18860. ((and org-export-with-tables
  18861. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  18862. (if (not table-open)
  18863. ;; New table starts
  18864. (setq table-open t table-buffer nil))
  18865. ;; Accumulate lines
  18866. (setq table-buffer (cons line table-buffer))
  18867. (when (or (not lines)
  18868. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  18869. (car lines))))
  18870. (setq table-open nil
  18871. table-buffer (nreverse table-buffer))
  18872. (insert (mapconcat
  18873. (lambda (x)
  18874. (org-fix-indentation x org-ascii-current-indentation))
  18875. (org-format-table-ascii table-buffer)
  18876. "\n") "\n")))
  18877. (t
  18878. (setq line (org-fix-indentation line org-ascii-current-indentation))
  18879. (if (and org-export-with-fixed-width
  18880. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  18881. (setq line (replace-match "\\1" nil nil line)))
  18882. (insert line "\n"))))
  18883. (normal-mode)
  18884. ;; insert the table of contents
  18885. (when thetoc
  18886. (goto-char (point-min))
  18887. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  18888. (progn
  18889. (goto-char (match-beginning 0))
  18890. (replace-match ""))
  18891. (goto-char first-heading-pos))
  18892. (mapc 'insert thetoc)
  18893. (or (looking-at "[ \t]*\n[ \t]*\n")
  18894. (insert "\n\n")))
  18895. (save-buffer)
  18896. ;; remove display and invisible chars
  18897. (let (beg end)
  18898. (goto-char (point-min))
  18899. (while (setq beg (next-single-property-change (point) 'display))
  18900. (setq end (next-single-property-change beg 'display))
  18901. (delete-region beg end)
  18902. (goto-char beg)
  18903. (insert "=>"))
  18904. (goto-char (point-min))
  18905. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  18906. (setq end (next-single-property-change beg 'org-cwidth))
  18907. (delete-region beg end)
  18908. (goto-char beg)))
  18909. (goto-char (point-min))))
  18910. (defun org-search-todo-below (line lines level)
  18911. "Search the subtree below LINE for any TODO entries."
  18912. (let ((rest (cdr (memq line lines)))
  18913. (re org-todo-line-regexp)
  18914. line lv todo)
  18915. (catch 'exit
  18916. (while (setq line (pop rest))
  18917. (if (string-match re line)
  18918. (progn
  18919. (setq lv (- (match-end 1) (match-beginning 1))
  18920. todo (and (match-beginning 2)
  18921. (not (member (match-string 2 line)
  18922. org-done-keywords))))
  18923. ; TODO, not DONE
  18924. (if (<= lv level) (throw 'exit nil))
  18925. (if todo (throw 'exit t))))))))
  18926. (defun org-html-expand-for-ascii (line)
  18927. "Handle quoted HTML for ASCII export."
  18928. (if org-export-html-expand
  18929. (while (string-match "@<[^<>\n]*>" line)
  18930. ;; We just remove the tags for now.
  18931. (setq line (replace-match "" nil nil line))))
  18932. line)
  18933. (defun org-insert-centered (s &optional underline)
  18934. "Insert the string S centered and underline it with character UNDERLINE."
  18935. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  18936. (insert (make-string ind ?\ ) s "\n")
  18937. (if underline
  18938. (insert (make-string ind ?\ )
  18939. (make-string (string-width s) underline)
  18940. "\n"))))
  18941. (defun org-ascii-level-start (level title umax &optional lines)
  18942. "Insert a new level in ASCII export."
  18943. (let (char (n (- level umax 1)) (ind 0))
  18944. (if (> level umax)
  18945. (progn
  18946. (insert (make-string (* 2 n) ?\ )
  18947. (char-to-string (nth (% n (length org-export-ascii-bullets))
  18948. org-export-ascii-bullets))
  18949. " " title "\n")
  18950. ;; find the indentation of the next non-empty line
  18951. (catch 'stop
  18952. (while lines
  18953. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  18954. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  18955. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  18956. (pop lines)))
  18957. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  18958. (if (or (not (equal (char-before) ?\n))
  18959. (not (equal (char-before (1- (point))) ?\n)))
  18960. (insert "\n"))
  18961. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  18962. (unless org-export-with-tags
  18963. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  18964. (setq title (replace-match "" t t title))))
  18965. (if org-export-with-section-numbers
  18966. (setq title (concat (org-section-number level) " " title)))
  18967. (insert title "\n" (make-string (string-width title) char) "\n")
  18968. (setq org-ascii-current-indentation '(0 . 0)))))
  18969. (defun org-export-visible (type arg)
  18970. "Create a copy of the visible part of the current buffer, and export it.
  18971. The copy is created in a temporary buffer and removed after use.
  18972. TYPE is the final key (as a string) that also select the export command in
  18973. the `C-c C-e' export dispatcher.
  18974. As a special case, if the you type SPC at the prompt, the temporary
  18975. org-mode file will not be removed but presented to you so that you can
  18976. continue to use it. The prefix arg ARG is passed through to the exporting
  18977. command."
  18978. (interactive
  18979. (list (progn
  18980. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  18981. (read-char-exclusive))
  18982. current-prefix-arg))
  18983. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  18984. (error "Invalid export key"))
  18985. (let* ((binding (cdr (assoc type
  18986. '((?a . org-export-as-ascii)
  18987. (?\C-a . org-export-as-ascii)
  18988. (?b . org-export-as-html-and-open)
  18989. (?\C-b . org-export-as-html-and-open)
  18990. (?h . org-export-as-html)
  18991. (?H . org-export-as-html-to-buffer)
  18992. (?R . org-export-region-as-html)
  18993. (?x . org-export-as-xoxo)))))
  18994. (keepp (equal type ?\ ))
  18995. (file buffer-file-name)
  18996. (buffer (get-buffer-create "*Org Export Visible*"))
  18997. s e)
  18998. (with-current-buffer buffer (erase-buffer))
  18999. (save-excursion
  19000. (setq s (goto-char (point-min)))
  19001. (while (not (= (point) (point-max)))
  19002. (goto-char (org-find-invisible))
  19003. (append-to-buffer buffer s (point))
  19004. (setq s (goto-char (org-find-visible))))
  19005. (goto-char (point-min))
  19006. (unless keepp
  19007. ;; Copy all comment lines to the end, to make sure #+ settings are
  19008. ;; still available for the second export step. Kind of a hack, but
  19009. ;; does do the trick.
  19010. (if (looking-at "#[^\r\n]*")
  19011. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  19012. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  19013. (append-to-buffer buffer (1+ (match-beginning 0))
  19014. (min (point-max) (1+ (match-end 0))))))
  19015. (set-buffer buffer)
  19016. (let ((buffer-file-name file)
  19017. (org-inhibit-startup t))
  19018. (org-mode)
  19019. (show-all)
  19020. (unless keepp (funcall binding arg))))
  19021. (if (not keepp)
  19022. (kill-buffer buffer)
  19023. (switch-to-buffer-other-window buffer)
  19024. (goto-char (point-min)))))
  19025. (defun org-find-visible ()
  19026. (let ((s (point)))
  19027. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19028. (get-char-property s 'invisible)))
  19029. s))
  19030. (defun org-find-invisible ()
  19031. (let ((s (point)))
  19032. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19033. (not (get-char-property s 'invisible))))
  19034. s))
  19035. ;;; HTML export
  19036. (defun org-get-current-options ()
  19037. "Return a string with current options as keyword options.
  19038. Does include HTML export options as well as TODO and CATEGORY stuff."
  19039. (format
  19040. "#+TITLE: %s
  19041. #+AUTHOR: %s
  19042. #+EMAIL: %s
  19043. #+LANGUAGE: %s
  19044. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  19045. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s p:%s
  19046. #+CATEGORY: %s
  19047. #+SEQ_TODO: %s
  19048. #+TYP_TODO: %s
  19049. #+PRIORITIES: %c %c %c
  19050. #+STARTUP: %s %s %s %s %s
  19051. #+TAGS: %s
  19052. #+ARCHIVE: %s
  19053. #+LINK: %s
  19054. "
  19055. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  19056. org-export-headline-levels
  19057. org-export-with-section-numbers
  19058. org-export-with-toc
  19059. org-export-preserve-breaks
  19060. org-export-html-expand
  19061. org-export-with-fixed-width
  19062. org-export-with-tables
  19063. org-export-with-sub-superscripts
  19064. org-export-with-footnotes
  19065. org-export-with-emphasize
  19066. org-export-with-TeX-macros
  19067. org-export-with-LaTeX-fragments
  19068. org-export-skip-text-before-1st-heading
  19069. org-export-with-property-drawer
  19070. (file-name-nondirectory buffer-file-name)
  19071. "TODO FEEDBACK VERIFY DONE"
  19072. "Me Jason Marie DONE"
  19073. org-highest-priority org-lowest-priority org-default-priority
  19074. (cdr (assoc org-startup-folded
  19075. '((nil . "showall") (t . "overview") (content . "content"))))
  19076. (if org-odd-levels-only "odd" "oddeven")
  19077. (if org-hide-leading-stars "hidestars" "showstars")
  19078. (if org-startup-align-all-tables "align" "noalign")
  19079. (cond ((eq t org-log-done) "logdone")
  19080. ((not org-log-done) "nologging")
  19081. ((listp org-log-done)
  19082. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  19083. org-log-done " ")))
  19084. (or (mapconcat (lambda (x)
  19085. (cond
  19086. ((equal '(:startgroup) x) "{")
  19087. ((equal '(:endgroup) x) "}")
  19088. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  19089. (t (car x))))
  19090. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  19091. org-archive-location
  19092. "org file:~/org/%s.org"
  19093. ))
  19094. (defun org-insert-export-options-template ()
  19095. "Insert into the buffer a template with information for exporting."
  19096. (interactive)
  19097. (if (not (bolp)) (newline))
  19098. (let ((s (org-get-current-options)))
  19099. (and (string-match "#\\+CATEGORY" s)
  19100. (setq s (substring s 0 (match-beginning 0))))
  19101. (insert s)))
  19102. (defun org-toggle-fixed-width-section (arg)
  19103. "Toggle the fixed-width export.
  19104. If there is no active region, the QUOTE keyword at the current headline is
  19105. inserted or removed. When present, it causes the text between this headline
  19106. and the next to be exported as fixed-width text, and unmodified.
  19107. If there is an active region, this command adds or removes a colon as the
  19108. first character of this line. If the first character of a line is a colon,
  19109. this line is also exported in fixed-width font."
  19110. (interactive "P")
  19111. (let* ((cc 0)
  19112. (regionp (org-region-active-p))
  19113. (beg (if regionp (region-beginning) (point)))
  19114. (end (if regionp (region-end)))
  19115. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19116. (re "[ \t]*\\(:\\)")
  19117. off)
  19118. (if regionp
  19119. (save-excursion
  19120. (goto-char beg)
  19121. (setq cc (current-column))
  19122. (beginning-of-line 1)
  19123. (setq off (looking-at re))
  19124. (while (> nlines 0)
  19125. (setq nlines (1- nlines))
  19126. (beginning-of-line 1)
  19127. (cond
  19128. (arg
  19129. (move-to-column cc t)
  19130. (insert ":\n")
  19131. (forward-line -1))
  19132. ((and off (looking-at re))
  19133. (replace-match "" t t nil 1))
  19134. ((not off) (move-to-column cc t) (insert ":")))
  19135. (forward-line 1)))
  19136. (save-excursion
  19137. (org-back-to-heading)
  19138. (if (looking-at (concat outline-regexp
  19139. "\\( *\\<" org-quote-string "\\>\\)"))
  19140. (replace-match "" t t nil 1)
  19141. (if (looking-at outline-regexp)
  19142. (progn
  19143. (goto-char (match-end 0))
  19144. (insert org-quote-string " "))))))))
  19145. (defun org-export-as-html-and-open (arg)
  19146. "Export the outline as HTML and immediately open it with a browser.
  19147. If there is an active region, export only the region.
  19148. The prefix ARG specifies how many levels of the outline should become
  19149. headlines. The default is 3. Lower levels will become bulleted lists."
  19150. (interactive "P")
  19151. (org-export-as-html arg 'hidden)
  19152. (org-open-file buffer-file-name))
  19153. (defun org-export-as-html-batch ()
  19154. "Call `org-export-as-html', may be used in batch processing as
  19155. emacs --batch
  19156. --load=$HOME/lib/emacs/org.el
  19157. --eval \"(setq org-export-headline-levels 2)\"
  19158. --visit=MyFile --funcall org-export-as-html-batch"
  19159. (org-export-as-html org-export-headline-levels 'hidden))
  19160. (defun org-export-as-html-to-buffer (arg)
  19161. "Call `org-exort-as-html` with output to a temporary buffer.
  19162. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  19163. (interactive "P")
  19164. (org-export-as-html arg nil nil "*Org HTML Export*")
  19165. (switch-to-buffer-other-window "*Org HTML Export*"))
  19166. (defun org-replace-region-by-html (beg end)
  19167. "Assume the current region has org-mode syntax, and convert it to HTML.
  19168. This can be used in any buffer. For example, you could write an
  19169. itemized list in org-mode syntax in an HTML buffer and then use this
  19170. command to convert it."
  19171. (interactive "r")
  19172. (let (reg html buf)
  19173. (if (org-mode-p)
  19174. (setq html (org-export-region-as-html
  19175. beg end t 'string))
  19176. (setq reg (buffer-substring beg end)
  19177. buf (get-buffer-create "*Org tmp*"))
  19178. (save-excursion
  19179. (set-buffer buf)
  19180. (erase-buffer)
  19181. (insert reg)
  19182. (org-mode)
  19183. (setq html (org-export-region-as-html
  19184. (point-min) (point-max) t 'string)))
  19185. (kill-buffer buf))
  19186. (delete-region beg end)
  19187. (insert html)))
  19188. (defun org-export-region-as-html (beg end &optional body-only buffer)
  19189. "Convert region from BEG to END in org-mode buffer to HTML.
  19190. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  19191. contents, and only produce the region of converted text, useful for
  19192. cut-and-paste operations.
  19193. If BUFFER is a buffer or a string, use/create that buffer as a target
  19194. of the converted HTML. If BUFFER is the symbol `string', return the
  19195. produced HTML as a string and leave not buffer behind. For example,
  19196. a Lisp program could call this function in the following way:
  19197. (setq html (org-export-region-as-html beg end t 'string))
  19198. When called interactively, the output buffer is selected, and shown
  19199. in a window. A non-interactive call will only retunr the buffer."
  19200. (interactive "r\nP")
  19201. (when (interactive-p)
  19202. (setq buffer "*Org HTML EXPORT*"))
  19203. (let ((transient-mark-mode t) (zmacs-regions t)
  19204. rtn)
  19205. (goto-char end)
  19206. (set-mark (point)) ;; to activate the region
  19207. (goto-char beg)
  19208. (setq rtn (org-export-as-html
  19209. nil nil nil
  19210. buffer body-only))
  19211. (if (fboundp 'deactivate-mark) (deactivate-mark))
  19212. (if (and (interactive-p) (bufferp rtn))
  19213. (switch-to-buffer-other-window rtn)
  19214. rtn)))
  19215. (defun org-export-as-html (arg &optional hidden ext-plist
  19216. to-buffer body-only)
  19217. "Export the outline as a pretty HTML file.
  19218. If there is an active region, export only the region. The prefix
  19219. ARG specifies how many levels of the outline should become
  19220. headlines. The default is 3. Lower levels will become bulleted
  19221. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  19222. EXT-PLIST is a property list with external parameters overriding
  19223. org-mode's default settings, but still inferior to file-local
  19224. settings. When TO-BUFFER is non-nil, create a buffer with that
  19225. name and export to that buffer. If TO-BUFFER is the symbol `string',
  19226. don't leave any buffer behind but just return the resulting HTML as
  19227. a string. When BODY-ONLY is set, don't produce the file header and footer,
  19228. simply return the content of <body>...</body>, without even
  19229. the body tags themselves."
  19230. (interactive "P")
  19231. ;; Make sure we have a file name when we need it.
  19232. (when (and (not (or to-buffer body-only))
  19233. (not buffer-file-name))
  19234. (if (buffer-base-buffer)
  19235. (org-set-local 'buffer-file-name
  19236. (with-current-buffer (buffer-base-buffer)
  19237. buffer-file-name))
  19238. (error "Need a file name to be able to export.")))
  19239. (message "Exporting...")
  19240. (setq-default org-todo-line-regexp org-todo-line-regexp)
  19241. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  19242. (setq-default org-done-keywords org-done-keywords)
  19243. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  19244. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19245. ext-plist
  19246. (org-infile-export-plist)))
  19247. (style (plist-get opt-plist :style))
  19248. (link-validate (plist-get opt-plist :link-validation-function))
  19249. valid thetoc have-headings first-heading-pos
  19250. (odd org-odd-levels-only)
  19251. (region-p (org-region-active-p))
  19252. ;; The following two are dynamically scoped into other
  19253. ;; routines below.
  19254. (org-current-export-dir (org-export-directory :html opt-plist))
  19255. (org-current-export-file buffer-file-name)
  19256. (level 0) (line "") (origline "") txt todo
  19257. (umax nil)
  19258. (umax-toc nil)
  19259. (filename (if to-buffer nil
  19260. (concat (file-name-as-directory
  19261. (org-export-directory :html opt-plist))
  19262. (file-name-sans-extension
  19263. (file-name-nondirectory buffer-file-name))
  19264. ".html")))
  19265. (current-dir (if buffer-file-name
  19266. (file-name-directory buffer-file-name)
  19267. default-directory))
  19268. (buffer (if to-buffer
  19269. (cond
  19270. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  19271. (t (get-buffer-create to-buffer)))
  19272. (find-file-noselect filename)))
  19273. (org-levels-open (make-vector org-level-max nil))
  19274. (date (format-time-string "%Y/%m/%d" (current-time)))
  19275. (time (format-time-string "%X" (org-current-time)))
  19276. (author (plist-get opt-plist :author))
  19277. (title (or (plist-get opt-plist :title)
  19278. (and (not
  19279. (plist-get opt-plist :skip-before-1st-heading))
  19280. (org-export-grab-title-from-buffer))
  19281. (and buffer-file-name
  19282. (file-name-sans-extension
  19283. (file-name-nondirectory buffer-file-name)))
  19284. "UNTITLED"))
  19285. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  19286. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  19287. (inquote nil)
  19288. (infixed nil)
  19289. (in-local-list nil)
  19290. (local-list-num nil)
  19291. (local-list-indent nil)
  19292. (llt org-plain-list-ordered-item-terminator)
  19293. (email (plist-get opt-plist :email))
  19294. (language (plist-get opt-plist :language))
  19295. (lang-words nil)
  19296. (target-alist nil) tg
  19297. (head-count 0) cnt
  19298. (start 0)
  19299. (coding-system (and (boundp 'buffer-file-coding-system)
  19300. buffer-file-coding-system))
  19301. (coding-system-for-write (or org-export-html-coding-system
  19302. coding-system))
  19303. (save-buffer-coding-system (or org-export-html-coding-system
  19304. coding-system))
  19305. (charset (and coding-system-for-write
  19306. (fboundp 'coding-system-get)
  19307. (coding-system-get coding-system-for-write
  19308. 'mime-charset)))
  19309. (region
  19310. (buffer-substring
  19311. (if region-p (region-beginning) (point-min))
  19312. (if region-p (region-end) (point-max))))
  19313. (lines
  19314. (org-skip-comments (org-split-string
  19315. (org-cleaned-string-for-export
  19316. region
  19317. :emph-multiline t
  19318. :for-html t
  19319. :skip-before-1st-heading
  19320. (plist-get opt-plist :skip-before-1st-heading)
  19321. :add-text
  19322. (plist-get opt-plist :text)
  19323. :LaTeX-fragments
  19324. (plist-get opt-plist :LaTeX-fragments))
  19325. "[\r\n]")))
  19326. table-open type
  19327. table-buffer table-orig-buffer
  19328. ind start-is-num starter didclose
  19329. rpl path desc descp desc1 desc2 link
  19330. )
  19331. (let (buffer-read-only)
  19332. (org-unmodified
  19333. (remove-text-properties (point-min) (point-max)
  19334. '(:org-license-to-kill t))))
  19335. (message "Exporting...")
  19336. (setq org-last-level 1)
  19337. (org-init-section-numbers)
  19338. ;; Get the language-dependent settings
  19339. (setq lang-words (or (assoc language org-export-language-setup)
  19340. (assoc "en" org-export-language-setup)))
  19341. ;; Switch to the output buffer
  19342. (set-buffer buffer)
  19343. (erase-buffer)
  19344. (fundamental-mode)
  19345. (let ((case-fold-search nil)
  19346. (org-odd-levels-only odd))
  19347. ;; create local variables for all options, to make sure all called
  19348. ;; functions get the correct information
  19349. (mapcar (lambda (x)
  19350. (set (make-local-variable (cdr x))
  19351. (plist-get opt-plist (car x))))
  19352. org-export-plist-vars)
  19353. (setq umax (if arg (prefix-numeric-value arg)
  19354. org-export-headline-levels))
  19355. (setq umax-toc (if (integerp org-export-with-toc)
  19356. (min org-export-with-toc umax)
  19357. umax))
  19358. (unless body-only
  19359. ;; File header
  19360. (insert (format
  19361. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  19362. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  19363. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  19364. lang=\"%s\" xml:lang=\"%s\">
  19365. <head>
  19366. <title>%s</title>
  19367. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  19368. <meta name=\"generator\" content=\"Org-mode\"/>
  19369. <meta name=\"generated\" content=\"%s %s\"/>
  19370. <meta name=\"author\" content=\"%s\"/>
  19371. %s
  19372. </head><body>
  19373. "
  19374. language language (org-html-expand title)
  19375. (or charset "iso-8859-1") date time author style))
  19376. (insert (or (plist-get opt-plist :preamble) ""))
  19377. (when (plist-get opt-plist :auto-preamble)
  19378. (if title (insert (format org-export-html-title-format
  19379. (org-html-expand title))))))
  19380. (if (and org-export-with-toc (not body-only))
  19381. (progn
  19382. (push (format "<h%d>%s</h%d>\n"
  19383. org-export-html-toplevel-hlevel
  19384. (nth 3 lang-words)
  19385. org-export-html-toplevel-hlevel)
  19386. thetoc)
  19387. (push "<ul>\n<li>" thetoc)
  19388. (setq lines
  19389. (mapcar '(lambda (line)
  19390. (if (string-match org-todo-line-regexp line)
  19391. ;; This is a headline
  19392. (progn
  19393. (setq have-headings t)
  19394. (setq level (- (match-end 1) (match-beginning 1))
  19395. level (org-tr-level level)
  19396. txt (save-match-data
  19397. (org-html-expand
  19398. (org-export-cleanup-toc-line
  19399. (match-string 3 line))))
  19400. todo
  19401. (or (and org-export-mark-todo-in-toc
  19402. (match-beginning 2)
  19403. (not (member (match-string 2 line)
  19404. org-done-keywords)))
  19405. ; TODO, not DONE
  19406. (and org-export-mark-todo-in-toc
  19407. (= level umax-toc)
  19408. (org-search-todo-below
  19409. line lines level))))
  19410. (if (and (memq org-export-with-tags '(not-in-toc nil))
  19411. (string-match
  19412. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  19413. txt))
  19414. (setq txt (replace-match "" t t txt)))
  19415. (if (string-match quote-re0 txt)
  19416. (setq txt (replace-match "" t t txt)))
  19417. (if org-export-with-section-numbers
  19418. (setq txt (concat (org-section-number level)
  19419. " " txt)))
  19420. (if (<= level (max umax umax-toc))
  19421. (setq head-count (+ head-count 1)))
  19422. (if (<= level umax-toc)
  19423. (progn
  19424. (if (> level org-last-level)
  19425. (progn
  19426. (setq cnt (- level org-last-level))
  19427. (while (>= (setq cnt (1- cnt)) 0)
  19428. (push "\n<ul>\n<li>" thetoc))
  19429. (push "\n" thetoc)))
  19430. (if (< level org-last-level)
  19431. (progn
  19432. (setq cnt (- org-last-level level))
  19433. (while (>= (setq cnt (1- cnt)) 0)
  19434. (push "</li>\n</ul>" thetoc))
  19435. (push "\n" thetoc)))
  19436. ;; Check for targets
  19437. (while (string-match org-target-regexp line)
  19438. (setq tg (match-string 1 line)
  19439. line (replace-match
  19440. (concat "@<span class=\"target\">" tg "@</span> ")
  19441. t t line))
  19442. (push (cons (org-solidify-link-text tg)
  19443. (format "sec-%d" head-count))
  19444. target-alist))
  19445. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  19446. (setq txt (replace-match "" t t txt)))
  19447. (push
  19448. (format
  19449. (if todo
  19450. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  19451. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  19452. head-count txt) thetoc)
  19453. (setq org-last-level level))
  19454. )))
  19455. line)
  19456. lines))
  19457. (while (> org-last-level 0)
  19458. (setq org-last-level (1- org-last-level))
  19459. (push "</li>\n</ul>\n" thetoc))
  19460. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  19461. (setq head-count 0)
  19462. (org-init-section-numbers)
  19463. (while (setq line (pop lines) origline line)
  19464. (catch 'nextline
  19465. ;; end of quote section?
  19466. (when (and inquote (string-match "^\\*+ " line))
  19467. (insert "</pre>\n")
  19468. (setq inquote nil))
  19469. ;; inside a quote section?
  19470. (when inquote
  19471. (insert (org-html-protect line) "\n")
  19472. (throw 'nextline nil))
  19473. ;; verbatim lines
  19474. (when (and org-export-with-fixed-width
  19475. (string-match "^[ \t]*:\\(.*\\)" line))
  19476. (when (not infixed)
  19477. (setq infixed t)
  19478. (insert "<pre>\n"))
  19479. (insert (org-html-protect (match-string 1 line)) "\n")
  19480. (when (and lines
  19481. (not (string-match "^[ \t]*\\(:.*\\)"
  19482. (car lines))))
  19483. (setq infixed nil)
  19484. (insert "</pre>\n"))
  19485. (throw 'nextline nil))
  19486. ;; Protected HTML
  19487. (when (get-text-property 0 'org-protected line)
  19488. (let (par)
  19489. (when (re-search-backward
  19490. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  19491. (setq par (match-string 1))
  19492. (replace-match "\\2\n"))
  19493. (insert line "\n")
  19494. (while (and lines
  19495. (get-text-property 0 'org-protected (car lines)))
  19496. (insert (pop lines) "\n"))
  19497. (and par (insert "<p>\n")))
  19498. (throw 'nextline nil))
  19499. ;; Horizontal line
  19500. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  19501. (insert "\n<hr/>\n")
  19502. (throw 'nextline nil))
  19503. ;; make targets to anchors
  19504. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  19505. (cond
  19506. ((match-end 2)
  19507. (setq line (replace-match
  19508. (concat "@<a name=\""
  19509. (org-solidify-link-text (match-string 1 line))
  19510. "\">\\nbsp@</a>")
  19511. t t line)))
  19512. ((and org-export-with-toc (equal (string-to-char line) ?*))
  19513. (setq line (replace-match
  19514. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  19515. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  19516. t t line)))
  19517. (t
  19518. (setq line (replace-match
  19519. (concat "@<a name=\""
  19520. (org-solidify-link-text (match-string 1 line))
  19521. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  19522. t t line)))))
  19523. (setq line (org-html-handle-time-stamps line))
  19524. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  19525. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  19526. ;; Also handle sub_superscripts and checkboxes
  19527. (setq line (org-html-expand line))
  19528. ;; Format the links
  19529. (setq start 0)
  19530. (while (string-match org-bracket-link-analytic-regexp line start)
  19531. (setq start (match-beginning 0))
  19532. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  19533. (setq path (match-string 3 line))
  19534. (setq desc1 (if (match-end 5) (match-string 5 line))
  19535. desc2 (if (match-end 2) (concat type ":" path) path)
  19536. descp (and desc1 (not (equal desc1 desc2)))
  19537. desc (or desc1 desc2))
  19538. ;; Make an image out of the description if that is so wanted
  19539. (when (and descp (org-file-image-p desc))
  19540. (save-match-data
  19541. (if (string-match "^file:" desc)
  19542. (setq desc (substring desc (match-end 0)))))
  19543. (setq desc (concat "<img src=\"" desc "\"/>")))
  19544. ;; FIXME: do we need to unescape here somewhere?
  19545. (cond
  19546. ((equal type "internal")
  19547. (setq rpl
  19548. (concat
  19549. "<a href=\"#"
  19550. (org-solidify-link-text
  19551. (save-match-data (org-link-unescape path)) target-alist)
  19552. "\">" desc "</a>")))
  19553. ((member type '("http" "https")) ; FIXME: need to test this.
  19554. ;; standard URL, just check if we need to inline an image
  19555. (if (and (or (eq t org-export-html-inline-images)
  19556. (and org-export-html-inline-images (not descp)))
  19557. (org-file-image-p path))
  19558. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  19559. (setq link (concat type ":" path))
  19560. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  19561. ((member type '("ftp" "mailto" "news"))
  19562. ;; standard URL
  19563. (setq link (concat type ":" path))
  19564. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  19565. ((string= type "file")
  19566. ;; FILE link
  19567. (let* ((filename path)
  19568. (abs-p (file-name-absolute-p filename))
  19569. thefile file-is-image-p search)
  19570. (save-match-data
  19571. (if (string-match "::\\(.*\\)" filename)
  19572. (setq search (match-string 1 filename)
  19573. filename (replace-match "" t nil filename)))
  19574. (setq valid
  19575. (if (functionp link-validate)
  19576. (funcall link-validate filename current-dir)
  19577. t))
  19578. (setq file-is-image-p (org-file-image-p filename))
  19579. (setq thefile (if abs-p (expand-file-name filename) filename))
  19580. (when (and org-export-html-link-org-files-as-html
  19581. (string-match "\\.org$" thefile))
  19582. (setq thefile (concat (substring thefile 0
  19583. (match-beginning 0))
  19584. ".html"))
  19585. (if (and search
  19586. ;; make sure this is can be used as target search
  19587. (not (string-match "^[0-9]*$" search))
  19588. (not (string-match "^\\*" search))
  19589. (not (string-match "^/.*/$" search)))
  19590. (setq thefile (concat thefile "#"
  19591. (org-solidify-link-text
  19592. (org-link-unescape search)))))
  19593. (when (string-match "^file:" desc)
  19594. (setq desc (replace-match "" t t desc))
  19595. (if (string-match "\\.org$" desc)
  19596. (setq desc (replace-match "" t t desc))))))
  19597. (setq rpl (if (and file-is-image-p
  19598. (or (eq t org-export-html-inline-images)
  19599. (and org-export-html-inline-images
  19600. (not descp))))
  19601. (concat "<img src=\"" thefile "\"/>")
  19602. (concat "<a href=\"" thefile "\">" desc "</a>")))
  19603. (if (not valid) (setq rpl desc))))
  19604. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  19605. (setq rpl (concat "<i>&lt;" type ":"
  19606. (save-match-data (org-link-unescape path))
  19607. "&gt;</i>"))))
  19608. (setq line (replace-match rpl t t line)
  19609. start (+ start (length rpl))))
  19610. ;; TODO items
  19611. (if (and (string-match org-todo-line-regexp line)
  19612. (match-beginning 2))
  19613. (setq line
  19614. (concat (substring line 0 (match-beginning 2))
  19615. "<span class=\""
  19616. (if (member (match-string 2 line)
  19617. org-done-keywords)
  19618. "done" "todo")
  19619. "\">" (match-string 2 line)
  19620. "</span>" (substring line (match-end 2)))))
  19621. ;; Does this contain a reference to a footnote?
  19622. (when org-export-with-footnotes
  19623. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  19624. (let ((n (match-string 2 line)))
  19625. (setq line
  19626. (replace-match
  19627. (format
  19628. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  19629. (match-string 1 line) n n n)
  19630. t t line)))))
  19631. (cond
  19632. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  19633. ;; This is a headline
  19634. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  19635. txt (match-string 2 line))
  19636. (if (string-match quote-re0 txt)
  19637. (setq txt (replace-match "" t t txt)))
  19638. (if (<= level (max umax umax-toc))
  19639. (setq head-count (+ head-count 1)))
  19640. (when in-local-list
  19641. ;; Close any local lists before inserting a new header line
  19642. (while local-list-num
  19643. (org-close-li)
  19644. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  19645. (pop local-list-num))
  19646. (setq local-list-indent nil
  19647. in-local-list nil))
  19648. (setq first-heading-pos (or first-heading-pos (point)))
  19649. (org-html-level-start level txt umax
  19650. (and org-export-with-toc (<= level umax))
  19651. head-count)
  19652. ;; QUOTES
  19653. (when (string-match quote-re line)
  19654. (insert "<pre>")
  19655. (setq inquote t)))
  19656. ((and org-export-with-tables
  19657. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  19658. (if (not table-open)
  19659. ;; New table starts
  19660. (setq table-open t table-buffer nil table-orig-buffer nil))
  19661. ;; Accumulate lines
  19662. (setq table-buffer (cons line table-buffer)
  19663. table-orig-buffer (cons origline table-orig-buffer))
  19664. (when (or (not lines)
  19665. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  19666. (car lines))))
  19667. (setq table-open nil
  19668. table-buffer (nreverse table-buffer)
  19669. table-orig-buffer (nreverse table-orig-buffer))
  19670. (org-close-par-maybe)
  19671. (insert (org-format-table-html table-buffer table-orig-buffer))))
  19672. (t
  19673. ;; Normal lines
  19674. (when (string-match
  19675. (cond
  19676. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  19677. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  19678. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  19679. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  19680. line)
  19681. (setq ind (org-get-string-indentation line)
  19682. start-is-num (match-beginning 4)
  19683. starter (if (match-beginning 2)
  19684. (substring (match-string 2 line) 0 -1))
  19685. line (substring line (match-beginning 5)))
  19686. (unless (string-match "[^ \t]" line)
  19687. ;; empty line. Pretend indentation is large.
  19688. (setq ind (if org-empty-line-terminates-plain-lists
  19689. 0
  19690. (1+ (or (car local-list-indent) 1)))))
  19691. (setq didclose nil)
  19692. (while (and in-local-list
  19693. (or (and (= ind (car local-list-indent))
  19694. (not starter))
  19695. (< ind (car local-list-indent))))
  19696. (setq didclose t)
  19697. (org-close-li)
  19698. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  19699. (pop local-list-num) (pop local-list-indent)
  19700. (setq in-local-list local-list-indent))
  19701. (cond
  19702. ((and starter
  19703. (or (not in-local-list)
  19704. (> ind (car local-list-indent))))
  19705. ;; Start new (level of) list
  19706. (org-close-par-maybe)
  19707. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  19708. (push start-is-num local-list-num)
  19709. (push ind local-list-indent)
  19710. (setq in-local-list t))
  19711. (starter
  19712. ;; continue current list
  19713. (org-close-li)
  19714. (insert "<li>\n"))
  19715. (didclose
  19716. ;; we did close a list, normal text follows: need <p>
  19717. (org-open-par)))
  19718. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  19719. (setq line
  19720. (replace-match
  19721. (if (equal (match-string 1 line) "X")
  19722. "<b>[X]</b>"
  19723. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  19724. t t line))))
  19725. ;; Empty lines start a new paragraph. If hand-formatted lists
  19726. ;; are not fully interpreted, lines starting with "-", "+", "*"
  19727. ;; also start a new paragraph.
  19728. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  19729. ;; Is this the start of a footnote?
  19730. (when org-export-with-footnotes
  19731. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  19732. (org-close-par-maybe)
  19733. (let ((n (match-string 1 line)))
  19734. (setq line (replace-match
  19735. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  19736. ;; Check if the line break needs to be conserved
  19737. (cond
  19738. ((string-match "\\\\\\\\[ \t]*$" line)
  19739. (setq line (replace-match "<br/>" t t line)))
  19740. (org-export-preserve-breaks
  19741. (setq line (concat line "<br/>"))))
  19742. (insert line "\n")))))
  19743. ;; Properly close all local lists and other lists
  19744. (when inquote (insert "</pre>\n"))
  19745. (when in-local-list
  19746. ;; Close any local lists before inserting a new header line
  19747. (while local-list-num
  19748. (org-close-li)
  19749. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  19750. (pop local-list-num))
  19751. (setq local-list-indent nil
  19752. in-local-list nil))
  19753. (org-html-level-start 1 nil umax
  19754. (and org-export-with-toc (<= level umax))
  19755. head-count)
  19756. (unless body-only
  19757. (when (plist-get opt-plist :auto-postamble)
  19758. (when author
  19759. (insert "<p class=\"author\"> "
  19760. (nth 1 lang-words) ": " author "\n")
  19761. (when email
  19762. (insert "<a href=\"mailto:" email "\">&lt;"
  19763. email "&gt;</a>\n"))
  19764. (insert "</p>\n"))
  19765. (when (and date time)
  19766. (insert "<p class=\"date\"> "
  19767. (nth 2 lang-words) ": "
  19768. date " " time "</p>\n")))
  19769. (if org-export-html-with-timestamp
  19770. (insert org-export-html-html-helper-timestamp))
  19771. (insert (or (plist-get opt-plist :postamble) ""))
  19772. (insert "</body>\n</html>\n"))
  19773. (normal-mode)
  19774. (if (eq major-mode default-major-mode) (html-mode))
  19775. ;; insert the table of contents
  19776. (goto-char (point-min))
  19777. (when thetoc
  19778. (if (or (re-search-forward
  19779. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  19780. (re-search-forward
  19781. "\\[TABLE-OF-CONTENTS\\]" nil t))
  19782. (progn
  19783. (goto-char (match-beginning 0))
  19784. (replace-match ""))
  19785. (goto-char first-heading-pos)
  19786. (when (looking-at "\\s-*</p>")
  19787. (goto-char (match-end 0))
  19788. (insert "\n")))
  19789. (mapc 'insert thetoc))
  19790. ;; remove empty paragraphs and lists
  19791. (goto-char (point-min))
  19792. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  19793. (replace-match ""))
  19794. (goto-char (point-min))
  19795. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  19796. (replace-match ""))
  19797. (or to-buffer (save-buffer))
  19798. (goto-char (point-min))
  19799. (message "Exporting... done")
  19800. (if (eq to-buffer 'string)
  19801. (prog1 (buffer-substring (point-min) (point-max))
  19802. (kill-buffer (current-buffer)))
  19803. (current-buffer)))))
  19804. (defvar org-table-colgroup-info nil) ;; FIXME: mode to a better place
  19805. (defun org-format-table-ascii (lines)
  19806. "Format a table for ascii export."
  19807. (if (stringp lines)
  19808. (setq lines (org-split-string lines "\n")))
  19809. (if (not (string-match "^[ \t]*|" (car lines)))
  19810. ;; Table made by table.el - test for spanning
  19811. lines
  19812. ;; A normal org table
  19813. ;; Get rid of hlines at beginning and end
  19814. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  19815. (setq lines (nreverse lines))
  19816. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  19817. (setq lines (nreverse lines))
  19818. (when org-export-table-remove-special-lines
  19819. ;; Check if the table has a marking column. If yes remove the
  19820. ;; column and the special lines
  19821. (setq lines (org-table-clean-before-export lines)))
  19822. ;; Get rid of the vertical lines except for grouping
  19823. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  19824. rtn line vl1 start)
  19825. (while (setq line (pop lines))
  19826. (if (string-match org-table-hline-regexp line)
  19827. (and (string-match "|\\(.*\\)|" line)
  19828. (setq line (replace-match " \\1" t nil line)))
  19829. (setq start 0 vl1 vl)
  19830. (while (string-match "|" line start)
  19831. (setq start (match-end 0))
  19832. (or (pop vl1) (setq line (replace-match " " t t line)))))
  19833. (push line rtn))
  19834. (nreverse rtn))))
  19835. (defun org-colgroup-info-to-vline-list (info)
  19836. (let (vl new last)
  19837. (while info
  19838. (setq last new new (pop info))
  19839. (if (or (memq last '(:end :startend))
  19840. (memq new '(:start :startend)))
  19841. (push t vl)
  19842. (push nil vl)))
  19843. (setq vl (cons nil (nreverse vl)))))
  19844. (defun org-format-table-html (lines olines)
  19845. "Find out which HTML converter to use and return the HTML code."
  19846. (if (stringp lines)
  19847. (setq lines (org-split-string lines "\n")))
  19848. (if (string-match "^[ \t]*|" (car lines))
  19849. ;; A normal org table
  19850. (org-format-org-table-html lines)
  19851. ;; Table made by table.el - test for spanning
  19852. (let* ((hlines (delq nil (mapcar
  19853. (lambda (x)
  19854. (if (string-match "^[ \t]*\\+-" x) x
  19855. nil))
  19856. lines)))
  19857. (first (car hlines))
  19858. (ll (and (string-match "\\S-+" first)
  19859. (match-string 0 first)))
  19860. (re (concat "^[ \t]*" (regexp-quote ll)))
  19861. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  19862. hlines))))
  19863. (if (and (not spanning)
  19864. (not org-export-prefer-native-exporter-for-tables))
  19865. ;; We can use my own converter with HTML conversions
  19866. (org-format-table-table-html lines)
  19867. ;; Need to use the code generator in table.el, with the original text.
  19868. (org-format-table-table-html-using-table-generate-source olines)))))
  19869. (defun org-format-org-table-html (lines &optional splice)
  19870. "Format a table into HTML."
  19871. ;; Get rid of hlines at beginning and end
  19872. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  19873. (setq lines (nreverse lines))
  19874. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  19875. (setq lines (nreverse lines))
  19876. (when org-export-table-remove-special-lines
  19877. ;; Check if the table has a marking column. If yes remove the
  19878. ;; column and the special lines
  19879. (setq lines (org-table-clean-before-export lines)))
  19880. (let ((head (and org-export-highlight-first-table-line
  19881. (delq nil (mapcar
  19882. (lambda (x) (string-match "^[ \t]*|-" x))
  19883. (cdr lines)))))
  19884. (nlines 0) fnum i
  19885. tbopen line fields html gr colgropen)
  19886. (if splice (setq head nil))
  19887. (unless splice (push (if head "<thead>" "<tbody>") html))
  19888. (setq tbopen t)
  19889. (while (setq line (pop lines))
  19890. (catch 'next-line
  19891. (if (string-match "^[ \t]*|-" line)
  19892. (progn
  19893. (unless splice
  19894. (push (if head "</thead>" "</tbody>") html)
  19895. (if lines (push "<tbody>" html) (setq tbopen nil)))
  19896. (setq head nil) ;; head ends here, first time around
  19897. ;; ignore this line
  19898. (throw 'next-line t)))
  19899. ;; Break the line into fields
  19900. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  19901. (unless fnum (setq fnum (make-vector (length fields) 0)))
  19902. (setq nlines (1+ nlines) i -1)
  19903. (push (concat "<tr>"
  19904. (mapconcat
  19905. (lambda (x)
  19906. (setq i (1+ i))
  19907. (if (and (< i nlines)
  19908. (string-match org-table-number-regexp x))
  19909. (incf (aref fnum i)))
  19910. (if head
  19911. (concat (car org-export-table-header-tags) x
  19912. (cdr org-export-table-header-tags))
  19913. (concat (car org-export-table-data-tags) x
  19914. (cdr org-export-table-data-tags))))
  19915. fields "")
  19916. "</tr>")
  19917. html)))
  19918. (unless splice (if tbopen (push "</tbody>" html)))
  19919. (unless splice (push "</table>\n" html))
  19920. (setq html (nreverse html))
  19921. (unless splice
  19922. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  19923. (push (mapconcat
  19924. (lambda (x)
  19925. (setq gr (pop org-table-colgroup-info))
  19926. (format "%s<COL align=\"%s\"></COL>%s"
  19927. (if (memq gr '(:start :startend))
  19928. (prog1
  19929. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  19930. (setq colgropen t))
  19931. "")
  19932. (if (> (/ (float x) nlines) org-table-number-fraction)
  19933. "right" "left")
  19934. (if (memq gr '(:end :startend))
  19935. (progn (setq colgropen nil) "</colgroup>")
  19936. "")))
  19937. fnum "")
  19938. html)
  19939. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  19940. (push org-export-html-table-tag html))
  19941. (concat (mapconcat 'identity html "\n") "\n")))
  19942. (defun org-table-clean-before-export (lines)
  19943. "Check if the table has a marking column.
  19944. If yes remove the column and the special lines."
  19945. (setq org-table-colgroup-info nil)
  19946. (if (memq nil
  19947. (mapcar
  19948. (lambda (x) (or (string-match "^[ \t]*|-" x)
  19949. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  19950. lines))
  19951. (progn
  19952. (setq org-table-clean-did-remove-column nil)
  19953. (delq nil
  19954. (mapcar
  19955. (lambda (x)
  19956. (cond
  19957. ((string-match "^[ \t]*| */ *|" x)
  19958. (setq org-table-colgroup-info
  19959. (mapcar (lambda (x)
  19960. (cond ((member x '("<" "&lt;")) :start)
  19961. ((member x '(">" "&gt;")) :end)
  19962. ((member x '("<>" "&lt;&gt;")) :startend)
  19963. (t nil)))
  19964. (org-split-string x "[ \t]*|[ \t]*")))
  19965. nil)
  19966. (t x)))
  19967. lines)))
  19968. (setq org-table-clean-did-remove-column t)
  19969. (delq nil
  19970. (mapcar
  19971. (lambda (x)
  19972. (cond
  19973. ((string-match "^[ \t]*| */ *|" x)
  19974. (setq org-table-colgroup-info
  19975. (mapcar (lambda (x)
  19976. (cond ((member x '("<" "&lt;")) :start)
  19977. ((member x '(">" "&gt;")) :end)
  19978. ((member x '("<>" "&lt;&gt;")) :startend)
  19979. (t nil)))
  19980. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  19981. nil)
  19982. ((string-match "^[ \t]*| *[!_^/] *|" x)
  19983. nil) ; ignore this line
  19984. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  19985. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  19986. ;; remove the first column
  19987. (replace-match "\\1|" t nil x))
  19988. (t (error "This should not happen"))))
  19989. lines))))
  19990. (defun org-format-table-table-html (lines)
  19991. "Format a table generated by table.el into HTML.
  19992. This conversion does *not* use `table-generate-source' from table.el.
  19993. This has the advantage that Org-mode's HTML conversions can be used.
  19994. But it has the disadvantage, that no cell- or row-spanning is allowed."
  19995. (let (line field-buffer
  19996. (head org-export-highlight-first-table-line)
  19997. fields html empty)
  19998. (setq html (concat org-export-html-table-tag "\n"))
  19999. (while (setq line (pop lines))
  20000. (setq empty "&nbsp;")
  20001. (catch 'next-line
  20002. (if (string-match "^[ \t]*\\+-" line)
  20003. (progn
  20004. (if field-buffer
  20005. (progn
  20006. (setq
  20007. html
  20008. (concat
  20009. html
  20010. "<tr>"
  20011. (mapconcat
  20012. (lambda (x)
  20013. (if (equal x "") (setq x empty))
  20014. (if head
  20015. (concat (car org-export-table-header-tags) x
  20016. (cdr org-export-table-header-tags))
  20017. (concat (car org-export-table-data-tags) x
  20018. (cdr org-export-table-data-tags))))
  20019. field-buffer "\n")
  20020. "</tr>\n"))
  20021. (setq head nil)
  20022. (setq field-buffer nil)))
  20023. ;; Ignore this line
  20024. (throw 'next-line t)))
  20025. ;; Break the line into fields and store the fields
  20026. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  20027. (if field-buffer
  20028. (setq field-buffer (mapcar
  20029. (lambda (x)
  20030. (concat x "<br/>" (pop fields)))
  20031. field-buffer))
  20032. (setq field-buffer fields))))
  20033. (setq html (concat html "</table>\n"))
  20034. html))
  20035. (defun org-format-table-table-html-using-table-generate-source (lines)
  20036. "Format a table into html, using `table-generate-source' from table.el.
  20037. This has the advantage that cell- or row-spanning is allowed.
  20038. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  20039. (require 'table)
  20040. (with-current-buffer (get-buffer-create " org-tmp1 ")
  20041. (erase-buffer)
  20042. (insert (mapconcat 'identity lines "\n"))
  20043. (goto-char (point-min))
  20044. (if (not (re-search-forward "|[^+]" nil t))
  20045. (error "Error processing table"))
  20046. (table-recognize-table)
  20047. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  20048. (table-generate-source 'html " org-tmp2 ")
  20049. (set-buffer " org-tmp2 ")
  20050. (buffer-substring (point-min) (point-max))))
  20051. (defun org-html-handle-time-stamps (s)
  20052. "Format time stamps in string S, or remove them."
  20053. (catch 'exit
  20054. (let (r b)
  20055. (while (string-match org-maybe-keyword-time-regexp s)
  20056. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  20057. ;; never export CLOCK
  20058. (throw 'exit ""))
  20059. (or b (setq b (substring s 0 (match-beginning 0))))
  20060. (if (not org-export-with-timestamps)
  20061. (setq r (concat r (substring s 0 (match-beginning 0)))
  20062. s (substring s (match-end 0)))
  20063. (setq r (concat
  20064. r (substring s 0 (match-beginning 0))
  20065. (if (match-end 1)
  20066. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  20067. (match-string 1 s)))
  20068. (format " @<span class=\"timestamp\">%s@</span>"
  20069. (substring
  20070. (org-translate-time (match-string 3 s)) 1 -1)))
  20071. s (substring s (match-end 0)))))
  20072. ;; Line break if line started and ended with time stamp stuff
  20073. (if (not r)
  20074. s
  20075. (setq r (concat r s))
  20076. (unless (string-match "\\S-" (concat b s))
  20077. (setq r (concat r "@<br/>")))
  20078. r))))
  20079. (defun org-html-protect (s)
  20080. ;; convert & to &amp;, < to &lt; and > to &gt;
  20081. (let ((start 0))
  20082. (while (string-match "&" s start)
  20083. (setq s (replace-match "&amp;" t t s)
  20084. start (1+ (match-beginning 0))))
  20085. (while (string-match "<" s)
  20086. (setq s (replace-match "&lt;" t t s)))
  20087. (while (string-match ">" s)
  20088. (setq s (replace-match "&gt;" t t s))))
  20089. s)
  20090. (defun org-export-cleanup-toc-line (s)
  20091. "Remove tags and time staps from lines going into the toc."
  20092. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  20093. (setq s (replace-match "" t t s)))
  20094. (when org-export-remove-timestamps-from-toc
  20095. (while (string-match org-maybe-keyword-time-regexp s)
  20096. (setq s (replace-match "" t t s))))
  20097. (while (string-match org-bracket-link-regexp s)
  20098. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  20099. t t s)))
  20100. s)
  20101. (defun org-html-expand (string)
  20102. "Prepare STRING for HTML export. Applies all active conversions.
  20103. If there are links in the string, don't modify these."
  20104. (let* (m s l res)
  20105. (while (setq m (string-match org-bracket-link-regexp string))
  20106. (setq s (substring string 0 m)
  20107. l (match-string 0 string)
  20108. string (substring string (match-end 0)))
  20109. (push (org-html-do-expand s) res)
  20110. (push l res))
  20111. (push (org-html-do-expand string) res)
  20112. (apply 'concat (nreverse res))))
  20113. (defun org-html-do-expand (s)
  20114. "Apply all active conversions to translate special ASCII to HTML."
  20115. (setq s (org-html-protect s))
  20116. (if org-export-html-expand
  20117. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  20118. (setq s (replace-match "<\\1>" t nil s))))
  20119. (if org-export-with-emphasize
  20120. (setq s (org-export-html-convert-emphasize s)))
  20121. (if org-export-with-sub-superscripts
  20122. (setq s (org-export-html-convert-sub-super s)))
  20123. (if org-export-with-TeX-macros
  20124. (let ((start 0) wd ass)
  20125. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  20126. (setq wd (match-string 1 s))
  20127. (if (setq ass (assoc wd org-html-entities))
  20128. (setq s (replace-match (or (cdr ass)
  20129. (concat "&" (car ass) ";"))
  20130. t t s))
  20131. (setq start (+ start (length wd)))))))
  20132. s)
  20133. (defun org-create-multibrace-regexp (left right n)
  20134. "Create a regular expression which will match a balanced sexp.
  20135. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  20136. as single character strings.
  20137. The regexp returned will match the entire expression including the
  20138. delimiters. It will also define a single group which contains the
  20139. match except for the outermost delimiters. The maximum depth of
  20140. stacked delimiters is N. Escaping delimiters is not possible."
  20141. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  20142. (or "\\|")
  20143. (re nothing)
  20144. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  20145. (while (> n 1)
  20146. (setq n (1- n)
  20147. re (concat re or next)
  20148. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  20149. (concat left "\\(" re "\\)" right)))
  20150. (defvar org-match-substring-regexp
  20151. (concat
  20152. "\\([^\\]\\)\\([_^]\\)\\("
  20153. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  20154. "\\|"
  20155. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  20156. "\\|"
  20157. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  20158. "The regular expression matching a sub- or superscript.")
  20159. ;(let ((s "a\\_b"))
  20160. ; (and (string-match org-match-substring-regexp s)
  20161. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  20162. (defun org-export-html-convert-sub-super (string)
  20163. "Convert sub- and superscripts in STRING to HTML."
  20164. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  20165. (while (string-match org-match-substring-regexp string s)
  20166. (if (and requireb (match-end 8))
  20167. (setq s (match-end 2))
  20168. (setq s (match-end 1)
  20169. key (if (string= (match-string 2 string) "_") "sub" "sup")
  20170. c (or (match-string 8 string)
  20171. (match-string 6 string)
  20172. (match-string 5 string))
  20173. string (replace-match
  20174. (concat (match-string 1 string)
  20175. "<" key ">" c "</" key ">")
  20176. t t string))))
  20177. (while (string-match "\\\\\\([_^]\\)" string)
  20178. (setq string (replace-match (match-string 1 string) t t string)))
  20179. string))
  20180. (defun org-export-html-convert-emphasize (string)
  20181. "Apply emphasis."
  20182. (let ((s 0))
  20183. (while (string-match org-emph-re string s)
  20184. (if (not (equal
  20185. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  20186. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  20187. (setq string (replace-match
  20188. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  20189. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  20190. "\\5") t nil string))
  20191. (setq s (1+ s))))
  20192. string))
  20193. (defvar org-par-open nil)
  20194. (defun org-open-par ()
  20195. "Insert <p>, but first close previous paragraph if any."
  20196. (org-close-par-maybe)
  20197. (insert "\n<p>")
  20198. (setq org-par-open t))
  20199. (defun org-close-par-maybe ()
  20200. "Close paragraph if there is one open."
  20201. (when org-par-open
  20202. (insert "</p>")
  20203. (setq org-par-open nil)))
  20204. (defun org-close-li ()
  20205. "Close <li> if necessary."
  20206. (org-close-par-maybe)
  20207. (insert "</li>\n"))
  20208. (defvar body-only) ; dynamically scoped into this.
  20209. (defun org-html-level-start (level title umax with-toc head-count)
  20210. "Insert a new level in HTML export.
  20211. When TITLE is nil, just close all open levels."
  20212. (org-close-par-maybe)
  20213. (let ((l (1+ (max level umax))))
  20214. (while (<= l org-level-max)
  20215. (if (aref org-levels-open (1- l))
  20216. (progn
  20217. (org-html-level-close l)
  20218. (aset org-levels-open (1- l) nil)))
  20219. (setq l (1+ l)))
  20220. (when title
  20221. ;; If title is nil, this means this function is called to close
  20222. ;; all levels, so the rest is done only if title is given
  20223. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  20224. (setq title (replace-match
  20225. (if org-export-with-tags
  20226. (save-match-data
  20227. (concat
  20228. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  20229. (mapconcat 'identity (org-split-string
  20230. (match-string 1 title) ":")
  20231. "&nbsp;")
  20232. "</span>"))
  20233. "")
  20234. t t title)))
  20235. (if (> level umax)
  20236. (progn
  20237. (if (aref org-levels-open (1- level))
  20238. (progn
  20239. (org-close-li)
  20240. (insert "<li>" title "<br/>\n"))
  20241. (aset org-levels-open (1- level) t)
  20242. (org-close-par-maybe)
  20243. (insert "<ul>\n<li>" title "<br/>\n")))
  20244. (if (and org-export-with-section-numbers (not body-only))
  20245. (setq title (concat (org-section-number level) " " title)))
  20246. (setq level (+ level org-export-html-toplevel-hlevel -1))
  20247. (if with-toc
  20248. (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
  20249. level head-count title level))
  20250. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  20251. (org-open-par)))))
  20252. (defun org-html-level-close (&rest args)
  20253. "Terminate one level in HTML export."
  20254. (org-close-li)
  20255. (insert "</ul>\n"))
  20256. ;;; iCalendar export
  20257. ;;;###autoload
  20258. (defun org-export-icalendar-this-file ()
  20259. "Export current file as an iCalendar file.
  20260. The iCalendar file will be located in the same directory as the Org-mode
  20261. file, but with extension `.ics'."
  20262. (interactive)
  20263. (org-export-icalendar nil buffer-file-name))
  20264. ;;;###autoload
  20265. (defun org-export-icalendar-all-agenda-files ()
  20266. "Export all files in `org-agenda-files' to iCalendar .ics files.
  20267. Each iCalendar file will be located in the same directory as the Org-mode
  20268. file, but with extension `.ics'."
  20269. (interactive)
  20270. (apply 'org-export-icalendar nil (org-agenda-files t)))
  20271. ;;;###autoload
  20272. (defun org-export-icalendar-combine-agenda-files ()
  20273. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  20274. The file is stored under the name `org-combined-agenda-icalendar-file'."
  20275. (interactive)
  20276. (apply 'org-export-icalendar t (org-agenda-files t)))
  20277. (defun org-export-icalendar (combine &rest files)
  20278. "Create iCalendar files for all elements of FILES.
  20279. If COMBINE is non-nil, combine all calendar entries into a single large
  20280. file and store it under the name `org-combined-agenda-icalendar-file'."
  20281. (save-excursion
  20282. (org-prepare-agenda-buffers files)
  20283. (let* ((dir (org-export-directory
  20284. :ical (list :publishing-directory
  20285. org-export-publishing-directory)))
  20286. file ical-file ical-buffer category started org-agenda-new-buffers)
  20287. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  20288. (when combine
  20289. (setq ical-file
  20290. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  20291. org-combined-agenda-icalendar-file
  20292. (expand-file-name org-combined-agenda-icalendar-file dir))
  20293. ical-buffer (org-get-agenda-file-buffer ical-file))
  20294. (set-buffer ical-buffer) (erase-buffer))
  20295. (while (setq file (pop files))
  20296. (catch 'nextfile
  20297. (org-check-agenda-file file)
  20298. (set-buffer (org-get-agenda-file-buffer file))
  20299. (unless combine
  20300. (setq ical-file (concat (file-name-as-directory dir)
  20301. (file-name-sans-extension
  20302. (file-name-nondirectory buffer-file-name))
  20303. ".ics"))
  20304. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  20305. (with-current-buffer ical-buffer (erase-buffer)))
  20306. (setq category (or org-category
  20307. (file-name-sans-extension
  20308. (file-name-nondirectory buffer-file-name))))
  20309. (if (symbolp category) (setq category (symbol-name category)))
  20310. (let ((standard-output ical-buffer))
  20311. (if combine
  20312. (and (not started) (setq started t)
  20313. (org-start-icalendar-file org-icalendar-combined-name))
  20314. (org-start-icalendar-file category))
  20315. (org-print-icalendar-entries combine)
  20316. (when (or (and combine (not files)) (not combine))
  20317. (org-finish-icalendar-file)
  20318. (set-buffer ical-buffer)
  20319. (save-buffer)
  20320. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  20321. (org-release-buffers org-agenda-new-buffers))))
  20322. (defvar org-after-save-iCalendar-file-hook nil
  20323. "Hook run after an iCalendar file has been saved.
  20324. The iCalendar buffer is still current when this hook is run.
  20325. A good way to use this is to tell a desktop calenndar application to re-read
  20326. the iCalendar file.")
  20327. (defun org-print-icalendar-entries (&optional combine)
  20328. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  20329. When COMBINE is non nil, add the category to each line."
  20330. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  20331. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  20332. (org-category-table (org-get-category-table))
  20333. (dts (org-ical-ts-to-string
  20334. (format-time-string (cdr org-time-stamp-formats) (current-time))
  20335. "DTSTART"))
  20336. hd ts ts2 state status (inc t) pos b sexp rrule
  20337. scheduledp deadlinep tmp pri category
  20338. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  20339. (save-excursion
  20340. (goto-char (point-min))
  20341. (while (re-search-forward re1 nil t)
  20342. (catch :skip
  20343. (org-agenda-skip)
  20344. (setq pos (match-beginning 0)
  20345. ts (match-string 0)
  20346. inc t
  20347. hd (org-get-heading)
  20348. category (org-get-category))
  20349. (if (looking-at re2)
  20350. (progn
  20351. (goto-char (match-end 0))
  20352. (setq ts2 (match-string 1) inc nil))
  20353. (setq tmp (buffer-substring (max (point-min)
  20354. (- pos org-ds-keyword-length))
  20355. pos)
  20356. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  20357. (progn
  20358. (setq inc nil)
  20359. (replace-match "\\1" t nil ts))
  20360. ts)
  20361. deadlinep (string-match org-deadline-regexp tmp)
  20362. scheduledp (string-match org-scheduled-regexp tmp)
  20363. ;; donep (org-entry-is-done-p)
  20364. ))
  20365. (if (or (string-match org-tr-regexp hd)
  20366. (string-match org-ts-regexp hd))
  20367. (setq hd (replace-match "" t t hd)))
  20368. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  20369. (setq rrule
  20370. (concat "\nRRULE:FREQ="
  20371. (cdr (assoc
  20372. (match-string 2 ts)
  20373. '(("d" . "DAILY")("w" . "WEEKLY")
  20374. ("m" . "MONTHLY")("y" . "YEARLY"))))
  20375. ";INTERVAL=" (match-string 1 ts)))
  20376. (setq rrule ""))
  20377. (if (string-match org-bracket-link-regexp hd)
  20378. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  20379. (match-string 1 hd))
  20380. t t hd)))
  20381. (if deadlinep (setq hd (concat "DL: " hd)))
  20382. (if scheduledp (setq hd (concat "S: " hd)))
  20383. (if (string-match "\\`<%%" ts)
  20384. (with-current-buffer sexp-buffer
  20385. (insert (substring ts 1 -1) " " hd "\n"))
  20386. (princ (format "BEGIN:VEVENT
  20387. %s
  20388. %s%s
  20389. SUMMARY:%s
  20390. CATEGORIES:%s
  20391. END:VEVENT\n"
  20392. (org-ical-ts-to-string ts "DTSTART")
  20393. (org-ical-ts-to-string ts2 "DTEND" inc)
  20394. rrule hd category)))))
  20395. (when (and org-icalendar-include-sexps
  20396. (condition-case nil (require 'icalendar) (error nil))
  20397. (fboundp 'icalendar-export-region))
  20398. ;; Get all the literal sexps
  20399. (goto-char (point-min))
  20400. (while (re-search-forward "^&?%%(" nil t)
  20401. (catch :skip
  20402. (org-agenda-skip)
  20403. (setq b (match-beginning 0))
  20404. (goto-char (1- (match-end 0)))
  20405. (forward-sexp 1)
  20406. (end-of-line 1)
  20407. (setq sexp (buffer-substring b (point)))
  20408. (with-current-buffer sexp-buffer
  20409. (insert sexp "\n"))
  20410. (princ (org-diary-to-ical-string sexp-buffer)))))
  20411. (when org-icalendar-include-todo
  20412. (goto-char (point-min))
  20413. (while (re-search-forward org-todo-line-regexp nil t)
  20414. (catch :skip
  20415. (org-agenda-skip)
  20416. (setq state (match-string 2))
  20417. (setq status (if (member state org-done-keywords)
  20418. "COMPLETED" "NEEDS-ACTION"))
  20419. (when (and state
  20420. (or (not (member state org-done-keywords))
  20421. (eq org-icalendar-include-todo 'all))
  20422. (not (member org-archive-tag (org-get-tags-at)))
  20423. )
  20424. (setq hd (match-string 3))
  20425. (if (string-match org-bracket-link-regexp hd)
  20426. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  20427. (match-string 1 hd))
  20428. t t hd)))
  20429. (if (string-match org-priority-regexp hd)
  20430. (setq pri (string-to-char (match-string 2 hd))
  20431. hd (concat (substring hd 0 (match-beginning 1))
  20432. (substring hd (match-end 1))))
  20433. (setq pri org-default-priority))
  20434. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  20435. (- org-lowest-priority org-highest-priority))))))
  20436. (princ (format "BEGIN:VTODO
  20437. %s
  20438. SUMMARY:%s
  20439. CATEGORIES:%s
  20440. SEQUENCE:1
  20441. PRIORITY:%d
  20442. STATUS:%s
  20443. END:VTODO\n"
  20444. dts hd category pri status)))))))))
  20445. (defun org-start-icalendar-file (name)
  20446. "Start an iCalendar file by inserting the header."
  20447. (let ((user user-full-name)
  20448. (name (or name "unknown"))
  20449. (timezone (cadr (current-time-zone))))
  20450. (princ
  20451. (format "BEGIN:VCALENDAR
  20452. VERSION:2.0
  20453. X-WR-CALNAME:%s
  20454. PRODID:-//%s//Emacs with Org-mode//EN
  20455. X-WR-TIMEZONE:%s
  20456. CALSCALE:GREGORIAN\n" name user timezone))))
  20457. (defun org-finish-icalendar-file ()
  20458. "Finish an iCalendar file by inserting the END statement."
  20459. (princ "END:VCALENDAR\n"))
  20460. (defun org-ical-ts-to-string (s keyword &optional inc)
  20461. "Take a time string S and convert it to iCalendar format.
  20462. KEYWORD is added in front, to make a complete line like DTSTART....
  20463. When INC is non-nil, increase the hour by two (if time string contains
  20464. a time), or the day by one (if it does not contain a time)."
  20465. (let ((t1 (org-parse-time-string s 'nodefault))
  20466. t2 fmt have-time time)
  20467. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  20468. (setq t2 t1 have-time t)
  20469. (setq t2 (org-parse-time-string s)))
  20470. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  20471. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  20472. (when inc
  20473. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  20474. (setq time (encode-time s mi h d m y)))
  20475. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  20476. (concat keyword (format-time-string fmt time))))
  20477. ;;; XOXO export
  20478. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  20479. (with-current-buffer buffer
  20480. (apply 'insert output)))
  20481. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  20482. (defun org-export-as-xoxo (&optional buffer)
  20483. "Export the org buffer as XOXO.
  20484. The XOXO buffer is named *xoxo-<source buffer name>*"
  20485. (interactive (list (current-buffer)))
  20486. ;; A quickie abstraction
  20487. ;; Output everything as XOXO
  20488. (with-current-buffer (get-buffer buffer)
  20489. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  20490. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  20491. (org-infile-export-plist)))
  20492. (filename (concat (file-name-as-directory
  20493. (org-export-directory :xoxo opt-plist))
  20494. (file-name-sans-extension
  20495. (file-name-nondirectory buffer-file-name))
  20496. ".html"))
  20497. (out (find-file-noselect filename))
  20498. (last-level 1)
  20499. (hanging-li nil))
  20500. ;; Check the output buffer is empty.
  20501. (with-current-buffer out (erase-buffer))
  20502. ;; Kick off the output
  20503. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  20504. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  20505. (let* ((hd (match-string-no-properties 1))
  20506. (level (length hd))
  20507. (text (concat
  20508. (match-string-no-properties 2)
  20509. (save-excursion
  20510. (goto-char (match-end 0))
  20511. (let ((str ""))
  20512. (catch 'loop
  20513. (while 't
  20514. (forward-line)
  20515. (if (looking-at "^[ \t]\\(.*\\)")
  20516. (setq str (concat str (match-string-no-properties 1)))
  20517. (throw 'loop str)))))))))
  20518. ;; Handle level rendering
  20519. (cond
  20520. ((> level last-level)
  20521. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  20522. ((< level last-level)
  20523. (dotimes (- (- last-level level) 1)
  20524. (if hanging-li
  20525. (org-export-as-xoxo-insert-into out "</li>\n"))
  20526. (org-export-as-xoxo-insert-into out "</ol>\n"))
  20527. (when hanging-li
  20528. (org-export-as-xoxo-insert-into out "</li>\n")
  20529. (setq hanging-li nil)))
  20530. ((equal level last-level)
  20531. (if hanging-li
  20532. (org-export-as-xoxo-insert-into out "</li>\n")))
  20533. )
  20534. (setq last-level level)
  20535. ;; And output the new li
  20536. (setq hanging-li 't)
  20537. (if (equal ?+ (elt text 0))
  20538. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  20539. (org-export-as-xoxo-insert-into out "<li>" text))))
  20540. ;; Finally finish off the ol
  20541. (dotimes (- last-level 1)
  20542. (if hanging-li
  20543. (org-export-as-xoxo-insert-into out "</li>\n"))
  20544. (org-export-as-xoxo-insert-into out "</ol>\n"))
  20545. ;; Finish the buffer off and clean it up.
  20546. (switch-to-buffer-other-window out)
  20547. (indent-region (point-min) (point-max) nil)
  20548. (save-buffer)
  20549. (goto-char (point-min))
  20550. )))
  20551. ;;;; Key bindings
  20552. ;; Make `C-c C-x' a prefix key
  20553. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  20554. ;; TAB key with modifiers
  20555. (org-defkey org-mode-map "\C-i" 'org-cycle)
  20556. (org-defkey org-mode-map [(tab)] 'org-cycle)
  20557. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  20558. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  20559. (org-defkey org-mode-map "\M-\t" 'org-complete)
  20560. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  20561. ;; The following line is necessary under Suse GNU/Linux
  20562. (unless (featurep 'xemacs)
  20563. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  20564. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  20565. (define-key org-mode-map (kbd "<backtab>") 'org-shifttab)
  20566. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  20567. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  20568. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  20569. ;; Cursor keys with modifiers
  20570. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  20571. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  20572. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  20573. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  20574. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  20575. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  20576. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  20577. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  20578. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  20579. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  20580. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  20581. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  20582. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  20583. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  20584. ;;; Extra keys for tty access.
  20585. ;; We only set them when really needed because otherwise the
  20586. ;; menus don't show the simple keys
  20587. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  20588. (not window-system))
  20589. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  20590. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  20591. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  20592. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  20593. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  20594. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  20595. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  20596. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  20597. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  20598. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  20599. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  20600. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  20601. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  20602. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  20603. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  20604. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  20605. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  20606. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  20607. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  20608. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  20609. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  20610. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  20611. ;; All the other keys
  20612. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  20613. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  20614. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  20615. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  20616. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  20617. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  20618. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  20619. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  20620. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  20621. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  20622. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  20623. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  20624. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  20625. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  20626. (org-defkey org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  20627. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  20628. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  20629. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  20630. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  20631. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  20632. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  20633. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  20634. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  20635. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  20636. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  20637. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  20638. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  20639. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  20640. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  20641. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  20642. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  20643. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  20644. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  20645. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  20646. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  20647. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  20648. (org-defkey org-mode-map "\C-c^" 'org-sort)
  20649. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  20650. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  20651. (org-defkey org-mode-map "\C-m" 'org-return)
  20652. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  20653. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  20654. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  20655. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  20656. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  20657. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  20658. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  20659. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  20660. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  20661. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  20662. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  20663. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  20664. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  20665. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  20666. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  20667. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  20668. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  20669. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  20670. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  20671. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  20672. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  20673. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  20674. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  20675. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  20676. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  20677. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  20678. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  20679. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  20680. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  20681. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  20682. (when (featurep 'xemacs)
  20683. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  20684. (defsubst org-table-p () (org-at-table-p))
  20685. (defun org-self-insert-command (N)
  20686. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  20687. If the cursor is in a table looking at whitespace, the whitespace is
  20688. overwritten, and the table is not marked as requiring realignment."
  20689. (interactive "p")
  20690. (if (and (org-table-p)
  20691. (progn
  20692. ;; check if we blank the field, and if that triggers align
  20693. (and org-table-auto-blank-field
  20694. (member last-command
  20695. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  20696. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  20697. ;; got extra space, this field does not determine column width
  20698. (let (org-table-may-need-update) (org-table-blank-field))
  20699. ;; no extra space, this field may determine column width
  20700. (org-table-blank-field)))
  20701. t)
  20702. (eq N 1)
  20703. (looking-at "[^|\n]* |"))
  20704. (let (org-table-may-need-update)
  20705. (goto-char (1- (match-end 0)))
  20706. (delete-backward-char 1)
  20707. (goto-char (match-beginning 0))
  20708. (self-insert-command N))
  20709. (setq org-table-may-need-update t)
  20710. (self-insert-command N)))
  20711. (defun org-delete-backward-char (N)
  20712. "Like `delete-backward-char', insert whitespace at field end in tables.
  20713. When deleting backwards, in tables this function will insert whitespace in
  20714. front of the next \"|\" separator, to keep the table aligned. The table will
  20715. still be marked for re-alignment if the field did fill the entire column,
  20716. because, in this case the deletion might narrow the column."
  20717. (interactive "p")
  20718. (if (and (org-table-p)
  20719. (eq N 1)
  20720. (string-match "|" (buffer-substring (point-at-bol) (point)))
  20721. (looking-at ".*?|"))
  20722. (let ((pos (point))
  20723. (noalign (looking-at "[^|\n\r]* |"))
  20724. (c org-table-may-need-update))
  20725. (backward-delete-char N)
  20726. (skip-chars-forward "^|")
  20727. (insert " ")
  20728. (goto-char (1- pos))
  20729. ;; noalign: if there were two spaces at the end, this field
  20730. ;; does not determine the width of the column.
  20731. (if noalign (setq org-table-may-need-update c)))
  20732. (backward-delete-char N)))
  20733. (defun org-delete-char (N)
  20734. "Like `delete-char', but insert whitespace at field end in tables.
  20735. When deleting characters, in tables this function will insert whitespace in
  20736. front of the next \"|\" separator, to keep the table aligned. The table will
  20737. still be marked for re-alignment if the field did fill the entire column,
  20738. because, in this case the deletion might narrow the column."
  20739. (interactive "p")
  20740. (if (and (org-table-p)
  20741. (not (bolp))
  20742. (not (= (char-after) ?|))
  20743. (eq N 1))
  20744. (if (looking-at ".*?|")
  20745. (let ((pos (point))
  20746. (noalign (looking-at "[^|\n\r]* |"))
  20747. (c org-table-may-need-update))
  20748. (replace-match (concat
  20749. (substring (match-string 0) 1 -1)
  20750. " |"))
  20751. (goto-char pos)
  20752. ;; noalign: if there were two spaces at the end, this field
  20753. ;; does not determine the width of the column.
  20754. (if noalign (setq org-table-may-need-update c)))
  20755. (delete-char N))
  20756. (delete-char N)))
  20757. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  20758. (put 'org-self-insert-command 'delete-selection t)
  20759. (put 'orgtbl-self-insert-command 'delete-selection t)
  20760. (put 'org-delete-char 'delete-selection 'supersede)
  20761. (put 'org-delete-backward-char 'delete-selection 'supersede)
  20762. ;; Make `flyspell-mode' delay after some commands
  20763. (put 'org-self-insert-command 'flyspell-delayed t)
  20764. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  20765. (put 'org-delete-char 'flyspell-delayed t)
  20766. (put 'org-delete-backward-char 'flyspell-delayed t)
  20767. ;; How to do this: Measure non-white length of current string
  20768. ;; If equal to column width, we should realign.
  20769. (defun org-remap (map &rest commands)
  20770. "In MAP, remap the functions given in COMMANDS.
  20771. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  20772. (let (new old)
  20773. (while commands
  20774. (setq old (pop commands) new (pop commands))
  20775. (if (fboundp 'command-remapping)
  20776. (org-defkey map (vector 'remap old) new)
  20777. (substitute-key-definition old new map global-map)))))
  20778. (when (eq org-enable-table-editor 'optimized)
  20779. ;; If the user wants maximum table support, we need to hijack
  20780. ;; some standard editing functions
  20781. (org-remap org-mode-map
  20782. 'self-insert-command 'org-self-insert-command
  20783. 'delete-char 'org-delete-char
  20784. 'delete-backward-char 'org-delete-backward-char)
  20785. (org-defkey org-mode-map "|" 'org-force-self-insert))
  20786. (defun org-shiftcursor-error ()
  20787. "Throw an error because Shift-Cursor command was applied in wrong context."
  20788. (error "This command is active in special context like tables, headlines or timestamps"))
  20789. (defun org-shifttab (&optional arg)
  20790. "Global visibility cycling or move to previous table field.
  20791. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  20792. on context.
  20793. See the individual commands for more information."
  20794. (interactive "P")
  20795. (cond
  20796. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  20797. (arg (message "Content view to level: ")
  20798. (org-content (prefix-numeric-value arg))
  20799. (setq org-cycle-global-status 'overview))
  20800. (t (call-interactively 'org-global-cycle))))
  20801. (defun org-shiftmetaleft ()
  20802. "Promote subtree or delete table column.
  20803. Calls `org-promote-subtree', `org-outdent-item',
  20804. or `org-table-delete-column', depending on context.
  20805. See the individual commands for more information."
  20806. (interactive)
  20807. (cond
  20808. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  20809. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  20810. ((org-at-item-p) (call-interactively 'org-outdent-item))
  20811. (t (org-shiftcursor-error))))
  20812. (defun org-shiftmetaright ()
  20813. "Demote subtree or insert table column.
  20814. Calls `org-demote-subtree', `org-indent-item',
  20815. or `org-table-insert-column', depending on context.
  20816. See the individual commands for more information."
  20817. (interactive)
  20818. (cond
  20819. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  20820. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  20821. ((org-at-item-p) (call-interactively 'org-indent-item))
  20822. (t (org-shiftcursor-error))))
  20823. (defun org-shiftmetaup (&optional arg)
  20824. "Move subtree up or kill table row.
  20825. Calls `org-move-subtree-up' or `org-table-kill-row' or
  20826. `org-move-item-up' depending on context. See the individual commands
  20827. for more information."
  20828. (interactive "P")
  20829. (cond
  20830. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  20831. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  20832. ((org-at-item-p) (call-interactively 'org-move-item-up))
  20833. (t (org-shiftcursor-error))))
  20834. (defun org-shiftmetadown (&optional arg)
  20835. "Move subtree down or insert table row.
  20836. Calls `org-move-subtree-down' or `org-table-insert-row' or
  20837. `org-move-item-down', depending on context. See the individual
  20838. commands for more information."
  20839. (interactive "P")
  20840. (cond
  20841. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  20842. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  20843. ((org-at-item-p) (call-interactively 'org-move-item-down))
  20844. (t (org-shiftcursor-error))))
  20845. (defun org-metaleft (&optional arg)
  20846. "Promote heading or move table column to left.
  20847. Calls `org-do-promote' or `org-table-move-column', depending on context.
  20848. With no specific context, calls the Emacs default `backward-word'.
  20849. See the individual commands for more information."
  20850. (interactive "P")
  20851. (cond
  20852. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  20853. ((or (org-on-heading-p) (org-region-active-p))
  20854. (call-interactively 'org-do-promote))
  20855. ((org-at-item-p) (call-interactively 'org-outdent-item))
  20856. (t (call-interactively 'backward-word))))
  20857. (defun org-metaright (&optional arg)
  20858. "Demote subtree or move table column to right.
  20859. Calls `org-do-demote' or `org-table-move-column', depending on context.
  20860. With no specific context, calls the Emacs default `forward-word'.
  20861. See the individual commands for more information."
  20862. (interactive "P")
  20863. (cond
  20864. ((org-at-table-p) (call-interactively 'org-table-move-column))
  20865. ((or (org-on-heading-p) (org-region-active-p))
  20866. (call-interactively 'org-do-demote))
  20867. ((org-at-item-p) (call-interactively 'org-indent-item))
  20868. (t (call-interactively 'forward-word))))
  20869. (defun org-metaup (&optional arg)
  20870. "Move subtree up or move table row up.
  20871. Calls `org-move-subtree-up' or `org-table-move-row' or
  20872. `org-move-item-up', depending on context. See the individual commands
  20873. for more information."
  20874. (interactive "P")
  20875. (cond
  20876. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  20877. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  20878. ((org-at-item-p) (call-interactively 'org-move-item-up))
  20879. (t (org-shiftcursor-error))))
  20880. (defun org-metadown (&optional arg)
  20881. "Move subtree down or move table row down.
  20882. Calls `org-move-subtree-down' or `org-table-move-row' or
  20883. `org-move-item-down', depending on context. See the individual
  20884. commands for more information."
  20885. (interactive "P")
  20886. (cond
  20887. ((org-at-table-p) (call-interactively 'org-table-move-row))
  20888. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  20889. ((org-at-item-p) (call-interactively 'org-move-item-down))
  20890. (t (org-shiftcursor-error))))
  20891. (defun org-shiftup (&optional arg)
  20892. "Increase item in timestamp or increase priority of current headline.
  20893. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  20894. depending on context. See the individual commands for more information."
  20895. (interactive "P")
  20896. (cond
  20897. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  20898. ((org-on-heading-p) (call-interactively 'org-priority-up))
  20899. ((org-at-item-p) (call-interactively 'org-previous-item))
  20900. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  20901. (defun org-shiftdown (&optional arg)
  20902. "Decrease item in timestamp or decrease priority of current headline.
  20903. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  20904. depending on context. See the individual commands for more information."
  20905. (interactive "P")
  20906. (cond
  20907. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  20908. ((org-on-heading-p) (call-interactively 'org-priority-down))
  20909. (t (call-interactively 'org-next-item))))
  20910. (defun org-shiftright ()
  20911. "Next TODO keyword or timestamp one day later, depending on context."
  20912. (interactive)
  20913. (cond
  20914. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  20915. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  20916. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  20917. (t (org-shiftcursor-error))))
  20918. (defun org-shiftleft ()
  20919. "Previous TODO keyword or timestamp one day earlier, depending on context."
  20920. (interactive)
  20921. (cond
  20922. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  20923. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  20924. ((org-at-property-p)
  20925. (call-interactively 'org-property-previous-allowed-value))
  20926. (t (org-shiftcursor-error))))
  20927. (defun org-shiftcontrolright ()
  20928. "Switch to next TODO set."
  20929. (interactive)
  20930. (cond
  20931. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  20932. (t (org-shiftcursor-error))))
  20933. (defun org-shiftcontrolleft ()
  20934. "Switch to previous TODO set."
  20935. (interactive)
  20936. (cond
  20937. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  20938. (t (org-shiftcursor-error))))
  20939. (defun org-ctrl-c-ret ()
  20940. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  20941. (interactive)
  20942. (cond
  20943. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  20944. (t (call-interactively 'org-insert-heading))))
  20945. (defun org-copy-special ()
  20946. "Copy region in table or copy current subtree.
  20947. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  20948. See the individual commands for more information."
  20949. (interactive)
  20950. (call-interactively
  20951. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  20952. (defun org-cut-special ()
  20953. "Cut region in table or cut current subtree.
  20954. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  20955. See the individual commands for more information."
  20956. (interactive)
  20957. (call-interactively
  20958. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  20959. (defun org-paste-special (arg)
  20960. "Paste rectangular region into table, or past subtree relative to level.
  20961. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  20962. See the individual commands for more information."
  20963. (interactive "P")
  20964. (if (org-at-table-p)
  20965. (org-table-paste-rectangle)
  20966. (org-paste-subtree arg)))
  20967. (defun org-ctrl-c-ctrl-c (&optional arg)
  20968. "Set tags in headline, or update according to changed information at point.
  20969. This command does many different things, depending on context:
  20970. - If the cursor is in a headline, prompt for tags and insert them
  20971. into the current line, aligned to `org-tags-column'. When called
  20972. with prefix arg, realign all tags in the current buffer.
  20973. - If the cursor is in one of the special #+KEYWORD lines, this
  20974. triggers scanning the buffer for these lines and updating the
  20975. information.
  20976. - If the cursor is inside a table, realign the table. This command
  20977. works even if the automatic table editor has been turned off.
  20978. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  20979. the entire table.
  20980. - If the cursor is inside a table created by the table.el package,
  20981. activate that table.
  20982. - If the current buffer is a remember buffer, close note and file it.
  20983. with a prefix argument, file it without further interaction to the default
  20984. location.
  20985. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  20986. links in this buffer.
  20987. - If the cursor is on a numbered item in a plain list, renumber the
  20988. ordered list."
  20989. (interactive "P")
  20990. (let ((org-enable-table-editor t))
  20991. (cond
  20992. ((or org-clock-overlays
  20993. org-occur-highlights
  20994. org-latex-fragment-image-overlays)
  20995. (org-remove-clock-overlays)
  20996. (org-remove-occur-highlights)
  20997. (org-remove-latex-fragment-image-overlays)
  20998. (message "Temporary highlights/overlays removed from current buffer"))
  20999. ((and (local-variable-p 'org-finish-function (current-buffer))
  21000. (fboundp org-finish-function))
  21001. (funcall org-finish-function))
  21002. ((org-at-property-p)
  21003. (call-interactively 'org-property-action))
  21004. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  21005. ((org-on-heading-p) (call-interactively 'org-set-tags))
  21006. ((org-at-table.el-p)
  21007. (require 'table)
  21008. (beginning-of-line 1)
  21009. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  21010. (call-interactively 'table-recognize-table))
  21011. ((org-at-table-p)
  21012. (org-table-maybe-eval-formula)
  21013. (if arg
  21014. (call-interactively 'org-table-recalculate)
  21015. (org-table-maybe-recalculate-line))
  21016. (call-interactively 'org-table-align))
  21017. ((org-at-item-checkbox-p)
  21018. (call-interactively 'org-toggle-checkbox))
  21019. ((org-at-item-p)
  21020. (call-interactively 'org-maybe-renumber-ordered-list))
  21021. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  21022. (cond
  21023. ((equal (match-string 1) "TBLFM")
  21024. ;; Recalculate the table before this line
  21025. (save-excursion
  21026. (beginning-of-line 1)
  21027. (skip-chars-backward " \r\n\t")
  21028. (if (org-at-table-p)
  21029. (org-call-with-arg 'org-table-recalculate t))))
  21030. (t
  21031. (call-interactively 'org-mode-restart))))
  21032. (t (error "C-c C-c can do nothing useful at this location.")))))
  21033. (defun org-mode-restart ()
  21034. "Restart Org-mode, to scan again for special lines.
  21035. Also updates the keyword regular expressions."
  21036. (interactive)
  21037. (let ((org-inhibit-startup t)) (org-mode))
  21038. (message "Org-mode restarted to refresh keyword and special line setup"))
  21039. (defun org-return ()
  21040. "Goto next table row or insert a newline.
  21041. Calls `org-table-next-row' or `newline', depending on context.
  21042. See the individual commands for more information."
  21043. (interactive)
  21044. (cond
  21045. ((bobp) (newline))
  21046. ((org-at-table-p)
  21047. (org-table-justify-field-maybe)
  21048. (call-interactively 'org-table-next-row))
  21049. (t (newline))))
  21050. (defun org-ctrl-c-minus ()
  21051. "Insert separator line in table or modify bullet type in list.
  21052. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  21053. depending on context."
  21054. (interactive)
  21055. (cond
  21056. ((org-at-table-p)
  21057. (call-interactively 'org-table-insert-hline))
  21058. ((org-in-item-p)
  21059. (call-interactively 'org-cycle-list-bullet))
  21060. (t (error "`C-c -' does have no function here."))))
  21061. (defun org-meta-return (&optional arg)
  21062. "Insert a new heading or wrap a region in a table.
  21063. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  21064. See the individual commands for more information."
  21065. (interactive "P")
  21066. (cond
  21067. ((org-at-table-p)
  21068. (call-interactively 'org-table-wrap-region))
  21069. (t (call-interactively 'org-insert-heading))))
  21070. ;;; Menu entries
  21071. ;; Define the Org-mode menus
  21072. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  21073. '("Tbl"
  21074. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  21075. ["Next Field" org-cycle (org-at-table-p)]
  21076. ["Previous Field" org-shifttab (org-at-table-p)]
  21077. ["Next Row" org-return (org-at-table-p)]
  21078. "--"
  21079. ["Blank Field" org-table-blank-field (org-at-table-p)]
  21080. ["Edit Field" org-table-edit-field (org-at-table-p)]
  21081. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  21082. "--"
  21083. ("Column"
  21084. ["Move Column Left" org-metaleft (org-at-table-p)]
  21085. ["Move Column Right" org-metaright (org-at-table-p)]
  21086. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  21087. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  21088. ("Row"
  21089. ["Move Row Up" org-metaup (org-at-table-p)]
  21090. ["Move Row Down" org-metadown (org-at-table-p)]
  21091. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  21092. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  21093. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  21094. "--"
  21095. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  21096. ("Rectangle"
  21097. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  21098. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  21099. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  21100. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  21101. "--"
  21102. ("Calculate"
  21103. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  21104. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  21105. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  21106. "--"
  21107. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  21108. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  21109. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  21110. "--"
  21111. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  21112. "--"
  21113. ["Sum Column/Rectangle" org-table-sum
  21114. (or (org-at-table-p) (org-region-active-p))]
  21115. ["Which Column?" org-table-current-column (org-at-table-p)])
  21116. ["Debug Formulas"
  21117. org-table-toggle-formula-debugger
  21118. :style toggle :selected org-table-formula-debug]
  21119. ["Show Col/Row Numbers"
  21120. org-table-toggle-coordinate-overlays
  21121. :style toggle :selected org-table-overlay-coordinates]
  21122. "--"
  21123. ["Create" org-table-create (and (not (org-at-table-p))
  21124. org-enable-table-editor)]
  21125. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  21126. ["Import from File" org-table-import (not (org-at-table-p))]
  21127. ["Export to File" org-table-export (org-at-table-p)]
  21128. "--"
  21129. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  21130. (easy-menu-define org-org-menu org-mode-map "Org menu"
  21131. '("Org"
  21132. ("Show/Hide"
  21133. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  21134. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  21135. ["Sparse Tree" org-occur t]
  21136. ["Reveal Context" org-reveal t]
  21137. ["Show All" show-all t]
  21138. "--"
  21139. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  21140. "--"
  21141. ["New Heading" org-insert-heading t]
  21142. ("Navigate Headings"
  21143. ["Up" outline-up-heading t]
  21144. ["Next" outline-next-visible-heading t]
  21145. ["Previous" outline-previous-visible-heading t]
  21146. ["Next Same Level" outline-forward-same-level t]
  21147. ["Previous Same Level" outline-backward-same-level t]
  21148. "--"
  21149. ["Jump" org-goto t]
  21150. "--"
  21151. ["C-a finds headline start"
  21152. (setq org-special-ctrl-a (not org-special-ctrl-a))
  21153. :style toggle :selected org-special-ctrl-a])
  21154. ("Edit Structure"
  21155. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  21156. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  21157. "--"
  21158. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  21159. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  21160. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  21161. "--"
  21162. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  21163. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  21164. ["Demote Heading" org-metaright (not (org-at-table-p))]
  21165. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  21166. "--"
  21167. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  21168. "--"
  21169. ["Convert to odd levels" org-convert-to-odd-levels t]
  21170. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  21171. ("Editing"
  21172. ["Emphasis..." org-emphasize t])
  21173. ("Archive"
  21174. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  21175. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  21176. ; :active t :keys "C-u C-c C-x C-a"]
  21177. ["Sparse trees open ARCHIVE trees"
  21178. (setq org-sparse-tree-open-archived-trees
  21179. (not org-sparse-tree-open-archived-trees))
  21180. :style toggle :selected org-sparse-tree-open-archived-trees]
  21181. ["Cycling opens ARCHIVE trees"
  21182. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  21183. :style toggle :selected org-cycle-open-archived-trees]
  21184. ["Agenda includes ARCHIVE trees"
  21185. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  21186. :style toggle :selected (not org-agenda-skip-archived-trees)]
  21187. "--"
  21188. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  21189. ; ["Check and Move Children" (org-archive-subtree '(4))
  21190. ; :active t :keys "C-u C-c C-x C-s"]
  21191. )
  21192. "--"
  21193. ("TODO Lists"
  21194. ["TODO/DONE/-" org-todo t]
  21195. ("Select keyword"
  21196. ["Next keyword" org-shiftright (org-on-heading-p)]
  21197. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  21198. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  21199. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  21200. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  21201. ["Show TODO Tree" org-show-todo-tree t]
  21202. ["Global TODO list" org-todo-list t]
  21203. "--"
  21204. ["Set Priority" org-priority t]
  21205. ["Priority Up" org-shiftup t]
  21206. ["Priority Down" org-shiftdown t])
  21207. ("TAGS and Properties"
  21208. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  21209. ["Column view of properties" org-columns t])
  21210. ("Dates and Scheduling"
  21211. ["Timestamp" org-time-stamp t]
  21212. ["Timestamp (inactive)" org-time-stamp-inactive t]
  21213. ("Change Date"
  21214. ["1 Day Later" org-shiftright t]
  21215. ["1 Day Earlier" org-shiftleft t]
  21216. ["1 ... Later" org-shiftup t]
  21217. ["1 ... Earlier" org-shiftdown t])
  21218. ["Compute Time Range" org-evaluate-time-range t]
  21219. ["Schedule Item" org-schedule t]
  21220. ["Deadline" org-deadline t]
  21221. "--"
  21222. ["Custom time format" org-toggle-time-stamp-overlays
  21223. :style radio :selected org-display-custom-times]
  21224. "--"
  21225. ["Goto Calendar" org-goto-calendar t]
  21226. ["Date from Calendar" org-date-from-calendar t])
  21227. ("Logging work"
  21228. ["Clock in" org-clock-in t]
  21229. ["Clock out" org-clock-out t]
  21230. ["Clock cancel" org-clock-cancel t]
  21231. ["Display times" org-clock-display t]
  21232. ["Create clock table" org-clock-report t]
  21233. "--"
  21234. ["Record DONE time"
  21235. (progn (setq org-log-done (not org-log-done))
  21236. (message "Switching to %s will %s record a timestamp"
  21237. (car org-done-keywords)
  21238. (if org-log-done "automatically" "not")))
  21239. :style toggle :selected org-log-done])
  21240. "--"
  21241. ["Agenda Command..." org-agenda t]
  21242. ("File List for Agenda")
  21243. ("Special views current file"
  21244. ["TODO Tree" org-show-todo-tree t]
  21245. ["Check Deadlines" org-check-deadlines t]
  21246. ["Timeline" org-timeline t]
  21247. ["Tags Tree" org-tags-sparse-tree t])
  21248. "--"
  21249. ("Hyperlinks"
  21250. ["Store Link (Global)" org-store-link t]
  21251. ["Insert Link" org-insert-link t]
  21252. ["Follow Link" org-open-at-point t]
  21253. "--"
  21254. ["Next link" org-next-link t]
  21255. ["Previous link" org-previous-link t]
  21256. "--"
  21257. ["Descriptive Links"
  21258. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  21259. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  21260. ["Literal Links"
  21261. (progn
  21262. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  21263. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  21264. "--"
  21265. ["Export/Publish..." org-export t]
  21266. ("LaTeX"
  21267. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  21268. :selected org-cdlatex-mode]
  21269. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  21270. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  21271. ["Modify math symbol" org-cdlatex-math-modify
  21272. (org-inside-LaTeX-fragment-p)]
  21273. ["Export LaTeX fragments as images"
  21274. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  21275. :style toggle :selected org-export-with-LaTeX-fragments])
  21276. "--"
  21277. ("Documentation"
  21278. ["Show Version" org-version t]
  21279. ["Info Documentation" org-info t])
  21280. ("Customize"
  21281. ["Browse Org Group" org-customize t]
  21282. "--"
  21283. ["Expand This Menu" org-create-customize-menu
  21284. (fboundp 'customize-menu-create)])
  21285. "--"
  21286. ["Refresh setup" org-mode-restart t]
  21287. ))
  21288. (defun org-info (&optional node)
  21289. "Read documentation for Org-mode in the info system.
  21290. With optional NODE, go directly to that node."
  21291. (interactive)
  21292. (require 'info)
  21293. (Info-goto-node (format "(org)%s" (or node ""))))
  21294. (defun org-install-agenda-files-menu ()
  21295. (let ((bl (buffer-list)))
  21296. (save-excursion
  21297. (while bl
  21298. (set-buffer (pop bl))
  21299. (if (org-mode-p) (setq bl nil)))
  21300. (when (org-mode-p)
  21301. (easy-menu-change
  21302. '("Org") "File List for Agenda"
  21303. (append
  21304. (list
  21305. ["Edit File List" (org-edit-agenda-file-list) t]
  21306. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  21307. ["Remove Current File from List" org-remove-file t]
  21308. ["Cycle through agenda files" org-cycle-agenda-files t]
  21309. "--")
  21310. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  21311. ;;;; Documentation
  21312. (defun org-customize ()
  21313. "Call the customize function with org as argument."
  21314. (interactive)
  21315. (customize-browse 'org))
  21316. (defun org-create-customize-menu ()
  21317. "Create a full customization menu for Org-mode, insert it into the menu."
  21318. (interactive)
  21319. (if (fboundp 'customize-menu-create)
  21320. (progn
  21321. (easy-menu-change
  21322. '("Org") "Customize"
  21323. `(["Browse Org group" org-customize t]
  21324. "--"
  21325. ,(customize-menu-create 'org)
  21326. ["Set" Custom-set t]
  21327. ["Save" Custom-save t]
  21328. ["Reset to Current" Custom-reset-current t]
  21329. ["Reset to Saved" Custom-reset-saved t]
  21330. ["Reset to Standard Settings" Custom-reset-standard t]))
  21331. (message "\"Org\"-menu now contains full customization menu"))
  21332. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  21333. ;;;; Miscellaneous stuff
  21334. ;;; Generally useful functions
  21335. (defun org-context ()
  21336. "Return a list of contexts of the current cursor position.
  21337. If several contexts apply, all are returned.
  21338. Each context entry is a list with a symbol naming the context, and
  21339. two positions indicating start and end of the context. Possible
  21340. contexts are:
  21341. :headline anywhere in a headline
  21342. :headline-stars on the leading stars in a headline
  21343. :todo-keyword on a TODO keyword (including DONE) in a headline
  21344. :tags on the TAGS in a headline
  21345. :priority on the priority cookie in a headline
  21346. :item on the first line of a plain list item
  21347. :item-bullet on the bullet/number of a plain list item
  21348. :checkbox on the checkbox in a plain list item
  21349. :table in an org-mode table
  21350. :table-special on a special filed in a table
  21351. :table-table in a table.el table
  21352. :link on a hyperlink
  21353. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  21354. :target on a <<target>>
  21355. :radio-target on a <<<radio-target>>>
  21356. :latex-fragment on a LaTeX fragment
  21357. :latex-preview on a LaTeX fragment with overlayed preview image
  21358. This function expects the position to be visible because it uses font-lock
  21359. faces as a help to recognize the following contexts: :table-special, :link,
  21360. and :keyword."
  21361. (let* ((f (get-text-property (point) 'face))
  21362. (faces (if (listp f) f (list f)))
  21363. (p (point)) clist o)
  21364. ;; First the large context
  21365. (cond
  21366. ((org-on-heading-p t)
  21367. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  21368. (when (progn
  21369. (beginning-of-line 1)
  21370. (looking-at org-todo-line-tags-regexp))
  21371. (push (org-point-in-group p 1 :headline-stars) clist)
  21372. (push (org-point-in-group p 2 :todo-keyword) clist)
  21373. (push (org-point-in-group p 4 :tags) clist))
  21374. (goto-char p)
  21375. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  21376. (if (looking-at "\\[#[A-Z0-9]\\]")
  21377. (push (org-point-in-group p 0 :priority) clist)))
  21378. ((org-at-item-p)
  21379. (push (org-point-in-group p 2 :item-bullet) clist)
  21380. (push (list :item (point-at-bol)
  21381. (save-excursion (org-end-of-item) (point)))
  21382. clist)
  21383. (and (org-at-item-checkbox-p)
  21384. (push (org-point-in-group p 0 :checkbox) clist)))
  21385. ((org-at-table-p)
  21386. (push (list :table (org-table-begin) (org-table-end)) clist)
  21387. (if (memq 'org-formula faces)
  21388. (push (list :table-special
  21389. (previous-single-property-change p 'face)
  21390. (next-single-property-change p 'face)) clist)))
  21391. ((org-at-table-p 'any)
  21392. (push (list :table-table) clist)))
  21393. (goto-char p)
  21394. ;; Now the small context
  21395. (cond
  21396. ((org-at-timestamp-p)
  21397. (push (org-point-in-group p 0 :timestamp) clist))
  21398. ((memq 'org-link faces)
  21399. (push (list :link
  21400. (previous-single-property-change p 'face)
  21401. (next-single-property-change p 'face)) clist))
  21402. ((memq 'org-special-keyword faces)
  21403. (push (list :keyword
  21404. (previous-single-property-change p 'face)
  21405. (next-single-property-change p 'face)) clist))
  21406. ((org-on-target-p)
  21407. (push (org-point-in-group p 0 :target) clist)
  21408. (goto-char (1- (match-beginning 0)))
  21409. (if (looking-at org-radio-target-regexp)
  21410. (push (org-point-in-group p 0 :radio-target) clist))
  21411. (goto-char p))
  21412. ((setq o (car (delq nil
  21413. (mapcar
  21414. (lambda (x)
  21415. (if (memq x org-latex-fragment-image-overlays) x))
  21416. (org-overlays-at (point))))))
  21417. (push (list :latex-fragment
  21418. (org-overlay-start o) (org-overlay-end o)) clist)
  21419. (push (list :latex-preview
  21420. (org-overlay-start o) (org-overlay-end o)) clist))
  21421. ((org-inside-LaTeX-fragment-p)
  21422. ;; FIXME: positions wrong.
  21423. (push (list :latex-fragment (point) (point)) clist)))
  21424. (setq clist (nreverse (delq nil clist)))
  21425. clist))
  21426. ;; FIXME Compare with at-regexp-p
  21427. (defun org-in-regexp (re &optional nlines visually)
  21428. "Check if point is inside a match of regexp.
  21429. Normally only the current line is checked, but you can include NLINES extra
  21430. lines both before and after point into the search.
  21431. If VISUALLY is set, require that the cursor is not after the match but
  21432. really on, so that the block visually is on the match."
  21433. (catch 'exit
  21434. (let ((pos (point))
  21435. (eol (point-at-eol (+ 1 (or nlines 0))))
  21436. (inc (if visually 1 0)))
  21437. (save-excursion
  21438. (beginning-of-line (- 1 (or nlines 0)))
  21439. (while (re-search-forward re eol t)
  21440. (if (and (<= (match-beginning 0) pos)
  21441. (>= (+ inc (match-end 0)) pos))
  21442. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  21443. (defun org-at-regexp-p (regexp)
  21444. "Is point inside a match of REGEXP in the current line?"
  21445. (catch 'exit
  21446. (save-excursion
  21447. (let ((pos (point)) (end (point-at-eol)))
  21448. (beginning-of-line 1)
  21449. (while (re-search-forward regexp end t)
  21450. (if (and (<= (match-beginning 0) pos)
  21451. (>= (match-end 0) pos))
  21452. (throw 'exit t)))
  21453. nil))))
  21454. (defun org-uniquify (list)
  21455. "Remove duplicate elements from LIST."
  21456. (let (res)
  21457. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  21458. res))
  21459. (defun org-delete-all (elts list)
  21460. "Remove all elements in ELTS from LIST."
  21461. (while elts
  21462. (setq list (delete (pop elts) list)))
  21463. list)
  21464. (defun org-point-in-group (point group &optional context)
  21465. "Check if POINT is in match-group GROUP.
  21466. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  21467. match. If the match group does ot exist or point is not inside it,
  21468. return nil."
  21469. (and (match-beginning group)
  21470. (>= point (match-beginning group))
  21471. (<= point (match-end group))
  21472. (if context
  21473. (list context (match-beginning group) (match-end group))
  21474. t)))
  21475. (defun org-combine-plists (&rest plists)
  21476. "Create a single property list from all plists in PLISTS.
  21477. The process starts by copying the first list, and then setting properties
  21478. from the other lists. Settings in the last list are the most significant
  21479. ones and overrule settings in the other lists."
  21480. (let ((rtn (copy-sequence (pop plists)))
  21481. p v ls)
  21482. (while plists
  21483. (setq ls (pop plists))
  21484. (while ls
  21485. (setq p (pop ls) v (pop ls))
  21486. (setq rtn (plist-put rtn p v))))
  21487. rtn))
  21488. (defun org-move-line-down (arg)
  21489. "Move the current line down. With prefix argument, move it past ARG lines."
  21490. (interactive "p")
  21491. (let ((col (current-column))
  21492. beg end pos)
  21493. (beginning-of-line 1) (setq beg (point))
  21494. (beginning-of-line 2) (setq end (point))
  21495. (beginning-of-line (+ 1 arg))
  21496. (setq pos (move-marker (make-marker) (point)))
  21497. (insert (delete-and-extract-region beg end))
  21498. (goto-char pos)
  21499. (move-to-column col)))
  21500. (defun org-move-line-up (arg)
  21501. "Move the current line up. With prefix argument, move it past ARG lines."
  21502. (interactive "p")
  21503. (let ((col (current-column))
  21504. beg end pos)
  21505. (beginning-of-line 1) (setq beg (point))
  21506. (beginning-of-line 2) (setq end (point))
  21507. (beginning-of-line (- arg))
  21508. (setq pos (move-marker (make-marker) (point)))
  21509. (insert (delete-and-extract-region beg end))
  21510. (goto-char pos)
  21511. (move-to-column col)))
  21512. (defun org-replace-escapes (string table)
  21513. "Replace %-escapes in STRING with values in TABLE.
  21514. TABLE is an association list with keys line \"%a\" and string values.
  21515. The sequences in STRING may contain normal field width and padding information,
  21516. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  21517. so values can contain further %-escapes if they are define later in TABLE."
  21518. (let ((case-fold-search nil)
  21519. e re rpl)
  21520. (while (setq e (pop table))
  21521. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  21522. (while (string-match re string)
  21523. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  21524. (cdr e)))
  21525. (setq string (replace-match rpl t t string))))
  21526. string))
  21527. (defun org-sublist (list start end)
  21528. "Return a section of LIST, from START to END.
  21529. Counting starts at 1."
  21530. (let (rtn (c start))
  21531. (setq list (nthcdr (1- start) list))
  21532. (while (and list (<= c end))
  21533. (push (pop list) rtn)
  21534. (setq c (1+ c)))
  21535. (nreverse rtn)))
  21536. (defun org-find-base-buffer-visiting (file)
  21537. "Like `find-buffer-visiting' but alway return the base buffer and
  21538. not an indirect buffer"
  21539. (let ((buf (find-buffer-visiting file)))
  21540. (or (buffer-base-buffer buf) buf)))
  21541. (defun org-image-file-name-regexp ()
  21542. "Return regexp matching the file names of images."
  21543. (if (fboundp 'image-file-name-regexp)
  21544. (image-file-name-regexp)
  21545. (let ((image-file-name-extensions
  21546. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  21547. "xbm" "xpm" "pbm" "pgm" "ppm")))
  21548. (concat "\\."
  21549. (regexp-opt (nconc (mapcar 'upcase
  21550. image-file-name-extensions)
  21551. image-file-name-extensions)
  21552. t)
  21553. "\\'"))))
  21554. (defun org-file-image-p (file)
  21555. "Return non-nil if FILE is an image."
  21556. (save-match-data
  21557. (string-match (org-image-file-name-regexp) file)))
  21558. ;;; Paragraph filling stuff.
  21559. ;; We want this to be just right, so use the full arsenal.
  21560. (defun org-indent-line-function ()
  21561. "Indent line like previous, but further if previous was headline or item."
  21562. (interactive)
  21563. (let* ((pos (point))
  21564. (itemp (org-at-item-p))
  21565. column bpos bcol tpos tcol bullet btype bullet-type)
  21566. ;; Find the previous relevant line
  21567. (beginning-of-line 1)
  21568. (cond
  21569. ((looking-at "#") (setq column 0))
  21570. ((looking-at "\\*+ ") (setq column 0))
  21571. (t
  21572. (beginning-of-line 0)
  21573. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  21574. (beginning-of-line 0))
  21575. (cond
  21576. ((looking-at "\\*+[ \t]+")
  21577. (goto-char (match-end 0))
  21578. (setq column (current-column)))
  21579. ((org-in-item-p)
  21580. (org-beginning-of-item)
  21581. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  21582. (setq bpos (match-beginning 1) tpos (match-end 0)
  21583. bcol (progn (goto-char bpos) (current-column))
  21584. tcol (progn (goto-char tpos) (current-column))
  21585. bullet (match-string 1)
  21586. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  21587. (if (not itemp)
  21588. (setq column tcol)
  21589. (goto-char pos)
  21590. (beginning-of-line 1)
  21591. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  21592. (setq bullet (match-string 1)
  21593. btype (if (string-match "[0-9]" bullet) "n" bullet))
  21594. (setq column (if (equal btype bullet-type) bcol tcol))))
  21595. (t (setq column (org-get-indentation))))))
  21596. (goto-char pos)
  21597. (if (<= (current-column) (current-indentation))
  21598. (indent-line-to column)
  21599. (save-excursion (indent-line-to column)))
  21600. (setq column (current-column))
  21601. (beginning-of-line 1)
  21602. (if (looking-at
  21603. "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  21604. (replace-match (concat "\\1" (format org-property-format
  21605. (match-string 2) (match-string 3)))
  21606. t nil))
  21607. (move-to-column column)))
  21608. (defun org-set-autofill-regexps ()
  21609. (interactive)
  21610. ;; In the paragraph separator we include headlines, because filling
  21611. ;; text in a line directly attached to a headline would otherwise
  21612. ;; fill the headline as well.
  21613. (org-set-local 'comment-start-skip "^#+[ \t]*")
  21614. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  21615. ;; FIXME!!!!!!! (org-set-local 'paragraph-separate "\f\\|[ ]*$")
  21616. ;; The paragraph starter includes hand-formatted lists.
  21617. (org-set-local 'paragraph-start
  21618. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  21619. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  21620. ;; But only if the user has not turned off tables or fixed-width regions
  21621. (org-set-local
  21622. 'auto-fill-inhibit-regexp
  21623. (concat "\\*+ \\|#\\+"
  21624. "\\|[ \t]*" org-keyword-time-regexp
  21625. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  21626. (concat
  21627. "\\|[ \t]*["
  21628. (if org-enable-table-editor "|" "")
  21629. (if org-enable-fixed-width-editor ":" "")
  21630. "]"))))
  21631. ;; We use our own fill-paragraph function, to make sure that tables
  21632. ;; and fixed-width regions are not wrapped. That function will pass
  21633. ;; through to `fill-paragraph' when appropriate.
  21634. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21635. ; Adaptive filling: To get full control, first make sure that
  21636. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  21637. (org-set-local 'adaptive-fill-regexp "\000")
  21638. (org-set-local 'adaptive-fill-function
  21639. 'org-adaptive-fill-function))
  21640. (defun org-fill-paragraph (&optional justify)
  21641. "Re-align a table, pass through to fill-paragraph if no table."
  21642. (let ((table-p (org-at-table-p))
  21643. (table.el-p (org-at-table.el-p)))
  21644. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  21645. (table.el-p t) ; skip table.el tables
  21646. (table-p (org-table-align) t) ; align org-mode tables
  21647. (t nil)))) ; call paragraph-fill
  21648. ;; For reference, this is the default value of adaptive-fill-regexp
  21649. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  21650. (defun org-adaptive-fill-function ()
  21651. "Return a fill prefix for org-mode files.
  21652. In particular, this makes sure hanging paragraphs for hand-formatted lists
  21653. work correctly."
  21654. (cond ((looking-at "#[ \t]+")
  21655. (match-string 0))
  21656. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  21657. (save-excursion
  21658. (goto-char (match-end 0))
  21659. (make-string (current-column) ?\ )))
  21660. (t nil)))
  21661. ;;;; Functions extending outline functionality
  21662. ;; C-a should go to the beginning of a *visible* line, also in the
  21663. ;; new outline.el. I guess this should be patched into Emacs?
  21664. (defun org-beginning-of-line ()
  21665. "Go to the beginning of the current line. If that is invisible, continue
  21666. to a visible line beginning. This makes the function of C-a more intuitive."
  21667. (interactive)
  21668. (let ((pos (point)))
  21669. (beginning-of-line 1)
  21670. (if (bobp)
  21671. nil
  21672. (backward-char 1)
  21673. (if (org-invisible-p)
  21674. (while (and (not (bobp)) (org-invisible-p))
  21675. (backward-char 1)
  21676. (beginning-of-line 1))
  21677. (forward-char 1)))
  21678. (when (and org-special-ctrl-a (looking-at org-todo-line-regexp)
  21679. (= (char-after (match-end 1)) ?\ ))
  21680. (goto-char
  21681. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  21682. ((= pos (point)) (match-beginning 3))
  21683. (t (point)))))))
  21684. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21685. (defun org-invisible-p ()
  21686. "Check if point is at a character currently not visible."
  21687. ;; Early versions of noutline don't have `outline-invisible-p'.
  21688. (if (fboundp 'outline-invisible-p)
  21689. (outline-invisible-p)
  21690. (get-char-property (point) 'invisible)))
  21691. (defun org-invisible-p2 ()
  21692. "Check if point is at a character currently not visible."
  21693. (save-excursion
  21694. (if (and (eolp) (not (bobp))) (backward-char 1))
  21695. ;; Early versions of noutline don't have `outline-invisible-p'.
  21696. (if (fboundp 'outline-invisible-p)
  21697. (outline-invisible-p)
  21698. (get-char-property (point) 'invisible))))
  21699. (defalias 'org-back-to-heading 'outline-back-to-heading)
  21700. (defalias 'org-on-heading-p 'outline-on-heading-p)
  21701. (defalias 'org-at-heading-p 'outline-on-heading-p)
  21702. (defun org-at-heading-or-item-p ()
  21703. (or (org-on-heading-p) (org-at-item-p)))
  21704. (defun org-on-target-p ()
  21705. (or (org-in-regexp org-radio-target-regexp)
  21706. (org-in-regexp org-target-regexp)))
  21707. (defun org-up-heading-all (arg)
  21708. "Move to the heading line of which the present line is a subheading.
  21709. This function considers both visible and invisible heading lines.
  21710. With argument, move up ARG levels."
  21711. (if (fboundp 'outline-up-heading-all)
  21712. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21713. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21714. (defun org-goto-sibling (&optional previous)
  21715. "Goto the next sibling, even if it is invisible.
  21716. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21717. when a sibling was found. When none is found, return nil and don't
  21718. move point."
  21719. (let ((fun (if previous 're-search-backward 're-search-forward))
  21720. (pos (point))
  21721. (re (concat "^" outline-regexp))
  21722. level l)
  21723. (when (condition-case nil (org-back-to-heading t) (error nil))
  21724. (setq level (funcall outline-level))
  21725. (catch 'exit
  21726. (or previous (forward-char 1))
  21727. (while (funcall fun re nil t)
  21728. (setq l (funcall outline-level))
  21729. (when (< l level) (goto-char pos) (throw 'exit nil))
  21730. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21731. (goto-char pos)
  21732. nil))))
  21733. (defun org-show-siblings ()
  21734. "Show all siblings of the current headline."
  21735. (save-excursion
  21736. (while (org-goto-sibling) (org-flag-heading nil)))
  21737. (save-excursion
  21738. (while (org-goto-sibling 'previous)
  21739. (org-flag-heading nil))))
  21740. (defun org-show-hidden-entry ()
  21741. "Show an entry where even the heading is hidden."
  21742. (save-excursion
  21743. (org-show-entry)))
  21744. (defun org-flag-heading (flag &optional entry)
  21745. "Flag the current heading. FLAG non-nil means make invisible.
  21746. When ENTRY is non-nil, show the entire entry."
  21747. (save-excursion
  21748. (org-back-to-heading t)
  21749. ;; Check if we should show the entire entry
  21750. (if entry
  21751. (progn
  21752. (org-show-entry)
  21753. (save-excursion
  21754. (and (outline-next-heading)
  21755. (org-flag-heading nil))))
  21756. (outline-flag-region (max 1 (1- (point)))
  21757. (save-excursion (outline-end-of-heading) (point))
  21758. flag))))
  21759. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  21760. ;; This is an exact copy of the original function, but it uses
  21761. ;; `org-back-to-heading', to make it work also in invisible
  21762. ;; trees. And is uses an invisible-OK argument.
  21763. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21764. (org-back-to-heading invisible-OK)
  21765. (let ((first t)
  21766. (level (funcall outline-level)))
  21767. (while (and (not (eobp))
  21768. (or first (> (funcall outline-level) level)))
  21769. (setq first nil)
  21770. (outline-next-heading))
  21771. (unless to-heading
  21772. (if (memq (preceding-char) '(?\n ?\^M))
  21773. (progn
  21774. ;; Go to end of line before heading
  21775. (forward-char -1)
  21776. (if (memq (preceding-char) '(?\n ?\^M))
  21777. ;; leave blank line before heading
  21778. (forward-char -1))))))
  21779. (point))
  21780. (defun org-show-subtree ()
  21781. "Show everything after this heading at deeper levels."
  21782. (outline-flag-region
  21783. (point)
  21784. (save-excursion
  21785. (outline-end-of-subtree) (outline-next-heading) (point))
  21786. nil))
  21787. (defun org-show-entry ()
  21788. "Show the body directly following this heading.
  21789. Show the heading too, if it is currently invisible."
  21790. (interactive)
  21791. (save-excursion
  21792. (org-back-to-heading t)
  21793. (outline-flag-region
  21794. (max 1 (1- (point)))
  21795. (save-excursion
  21796. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  21797. (or (match-beginning 1) (point-max)))
  21798. nil)))
  21799. (defun org-make-options-regexp (kwds)
  21800. "Make a regular expression for keyword lines."
  21801. (concat
  21802. "^"
  21803. "#?[ \t]*\\+\\("
  21804. (mapconcat 'regexp-quote kwds "\\|")
  21805. "\\):[ \t]*"
  21806. "\\(.+\\)"))
  21807. ;; Make isearch reveal the necessary context
  21808. (defun org-isearch-end ()
  21809. "Reveal context after isearch exits."
  21810. (when isearch-success ; only if search was successful
  21811. (if (featurep 'xemacs)
  21812. ;; Under XEmacs, the hook is run in the correct place,
  21813. ;; we directly show the context.
  21814. (org-show-context 'isearch)
  21815. ;; In Emacs the hook runs *before* restoring the overlays.
  21816. ;; So we have to use a one-time post-command-hook to do this.
  21817. ;; (Emacs 22 has a special variable, see function `org-mode')
  21818. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21819. isearch-mode-end-hook-quit)
  21820. ;; Only when the isearch was not quitted.
  21821. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21822. 'append 'local)))))
  21823. (defun org-isearch-post-command ()
  21824. "Remove self from hook, and show context."
  21825. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21826. (org-show-context 'isearch))
  21827. ;;;; Address problems with some other packages
  21828. ;; Make flyspell not check words in links, to not mess up our keymap
  21829. (defun org-mode-flyspell-verify ()
  21830. "Don't let flyspell put overlays at active buttons."
  21831. (not (get-text-property (point) 'keymap)))
  21832. ;; Make `bookmark-jump' show the jump location if it was hidden.
  21833. (eval-after-load "bookmark"
  21834. '(if (boundp 'bookmark-after-jump-hook)
  21835. ;; We can use the hook
  21836. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21837. ;; Hook not available, use advice
  21838. (defadvice bookmark-jump (after org-make-visible activate)
  21839. "Make the position visible."
  21840. (org-bookmark-jump-unhide))))
  21841. (defun org-bookmark-jump-unhide ()
  21842. "Unhide the current position, to show the bookmark location."
  21843. (and (org-mode-p)
  21844. (or (org-invisible-p)
  21845. (save-excursion (goto-char (max (point-min) (1- (point))))
  21846. (org-invisible-p)))
  21847. (org-show-context 'bookmark-jump)))
  21848. ;; Make session.el ignore our circular variable
  21849. (eval-after-load "session"
  21850. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21851. ;;;; Experimental code
  21852. (defun org-closed-in-range ()
  21853. "Sparse tree of items closed in a certain time range.
  21854. Still experimental, may disappear in the furture."
  21855. (interactive)
  21856. ;; Get the time interval from the user.
  21857. (let* ((time1 (time-to-seconds
  21858. (org-read-date nil 'to-time nil "Starting date: ")))
  21859. (time2 (time-to-seconds
  21860. (org-read-date nil 'to-time nil "End date:")))
  21861. ;; callback function
  21862. (callback (lambda ()
  21863. (let ((time
  21864. (time-to-seconds
  21865. (apply 'encode-time
  21866. (org-parse-time-string
  21867. (match-string 1))))))
  21868. ;; check if time in interval
  21869. (and (>= time time1) (<= time time2))))))
  21870. ;; make tree, check each match with the callback
  21871. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21872. (defun org-fill-paragraph-experimental (&optional justify)
  21873. "Re-align a table, pass through to fill-paragraph if no table."
  21874. (let ((table-p (org-at-table-p))
  21875. (table.el-p (org-at-table.el-p)))
  21876. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  21877. (table.el-p t) ; skip table.el tables
  21878. (table-p (org-table-align) t) ; align org-mode tables
  21879. ((save-excursion
  21880. (let ((pos (1+ (point-at-eol))))
  21881. (backward-paragraph 1)
  21882. (re-search-forward "\\\\\\\\[ \t]*$" pos t)))
  21883. (save-excursion
  21884. (save-restriction
  21885. (narrow-to-region (1+ (match-end 0)) (point-max))
  21886. (fill-paragraph nil)
  21887. t)))
  21888. (t nil)))) ; call paragraph-fill
  21889. (defun org-property-previous-allowed-value (&optional previous)
  21890. "Switch to the next allowed value for this property."
  21891. (interactive)
  21892. (org-property-next-allowed-value t))
  21893. (defun org-property-next-allowed-value (&optional previous)
  21894. "Switch to the next allowed value for this property."
  21895. (interactive)
  21896. (unless (org-at-property-p)
  21897. (error "Not at a property"))
  21898. (let* ((key (match-string 2))
  21899. (value (match-string 3))
  21900. (allowed (or (org-property-get-allowed-values (point) key)
  21901. (and (member value '("[ ]" "[-]" "[X]"))
  21902. '("[ ]" "[X]"))))
  21903. nval)
  21904. (unless allowed
  21905. (error "Allowed values for this property have not been defined"))
  21906. (if previous (setq allowed (reverse allowed)))
  21907. (if (member value allowed)
  21908. (setq nval (car (cdr (member value allowed)))))
  21909. (setq nval (or nval (car allowed)))
  21910. (if (equal nval value)
  21911. (error "Only one allowed value for this property"))
  21912. (org-at-property-p)
  21913. (replace-match (concat " :" key ": " nval))
  21914. (org-indent-line-function)
  21915. (beginning-of-line 1)
  21916. (skip-chars-forward " \t")))
  21917. ;;;; Finish up
  21918. (provide 'org)
  21919. (run-hooks 'org-load-hook)
  21920. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  21921. ;;; org.el ends here