org.el 893 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277
  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.05
  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.05"
  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/e nil
  440. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  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. In an item, this will be the position after the bullet.
  444. When the cursor is already at that position, another `C-a' will bring
  445. it to the beginning of the line.
  446. `C-e' will jump to the end of the headline, ignoring the presence of tags
  447. in the headline. A second `C-e' will then jump to the true end of the
  448. line, after any tags."
  449. :group 'org-edit-structure
  450. :type 'boolean)
  451. (if (fboundp 'defvaralias)
  452. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  453. (defcustom org-odd-levels-only nil
  454. "Non-nil means, skip even levels and only use odd levels for the outline.
  455. This has the effect that two stars are being added/taken away in
  456. promotion/demotion commands. It also influences how levels are
  457. handled by the exporters.
  458. Changing it requires restart of `font-lock-mode' to become effective
  459. for fontification also in regions already fontified.
  460. You may also set this on a per-file basis by adding one of the following
  461. lines to the buffer:
  462. #+STARTUP: odd
  463. #+STARTUP: oddeven"
  464. :group 'org-edit-structure
  465. :group 'org-font-lock
  466. :type 'boolean)
  467. (defcustom org-adapt-indentation t
  468. "Non-nil means, adapt indentation when promoting and demoting.
  469. When this is set and the *entire* text in an entry is indented, the
  470. indentation is increased by one space in a demotion command, and
  471. decreased by one in a promotion command. If any line in the entry
  472. body starts at column 0, indentation is not changed at all."
  473. :group 'org-edit-structure
  474. :type 'boolean)
  475. (defcustom org-blank-before-new-entry '((heading . nil)
  476. (plain-list-item . nil))
  477. "Should `org-insert-heading' leave a blank line before new heading/item?
  478. The value is an alist, with `heading' and `plain-list-item' as car,
  479. and a boolean flag as cdr."
  480. :group 'org-edit-structure
  481. :type '(list
  482. (cons (const heading) (boolean))
  483. (cons (const plain-list-item) (boolean))))
  484. (defcustom org-insert-heading-hook nil
  485. "Hook being run after inserting a new heading."
  486. :group 'org-edit-structure
  487. :type 'boolean)
  488. (defcustom org-enable-fixed-width-editor t
  489. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  490. This currently only means, they are never auto-wrapped.
  491. When nil, such lines will be treated like ordinary lines.
  492. See also the QUOTE keyword."
  493. :group 'org-edit-structure
  494. :type 'boolean)
  495. (defgroup org-sparse-trees nil
  496. "Options concerning sparse trees in Org-mode."
  497. :tag "Org Sparse Trees"
  498. :group 'org-structure)
  499. (defcustom org-highlight-sparse-tree-matches t
  500. "Non-nil means, highlight all matches that define a sparse tree.
  501. The highlights will automatically disappear the next time the buffer is
  502. changed by an edit command."
  503. :group 'org-sparse-trees
  504. :type 'boolean)
  505. (defcustom org-remove-highlights-with-change t
  506. "Non-nil means, any change to the buffer will remove temporary highlights.
  507. Such highlights are created by `org-occur' and `org-clock-display'.
  508. When nil, `C-c C-c needs to be used to get rid of the highlights.
  509. The highlights created by `org-preview-latex-fragment' always need
  510. `C-c C-c' to be removed."
  511. :group 'org-sparse-trees
  512. :group 'org-time
  513. :type 'boolean)
  514. (defcustom org-occur-hook '(org-first-headline-recenter)
  515. "Hook that is run after `org-occur' has constructed a sparse tree.
  516. This can be used to recenter the window to show as much of the structure
  517. as possible."
  518. :group 'org-sparse-trees
  519. :type 'hook)
  520. (defgroup org-plain-lists nil
  521. "Options concerning plain lists in Org-mode."
  522. :tag "Org Plain lists"
  523. :group 'org-structure)
  524. (defcustom org-cycle-include-plain-lists nil
  525. "Non-nil means, include plain lists into visibility cycling.
  526. This means that during cycling, plain list items will *temporarily* be
  527. interpreted as outline headlines with a level given by 1000+i where i is the
  528. indentation of the bullet. In all other operations, plain list items are
  529. not seen as headlines. For example, you cannot assign a TODO keyword to
  530. such an item."
  531. :group 'org-plain-lists
  532. :type 'boolean)
  533. (defcustom org-plain-list-ordered-item-terminator t
  534. "The character that makes a line with leading number an ordered list item.
  535. Valid values are ?. and ?\). To get both terminators, use t. While
  536. ?. may look nicer, it creates the danger that a line with leading
  537. number may be incorrectly interpreted as an item. ?\) therefore is
  538. the safe choice."
  539. :group 'org-plain-lists
  540. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  541. (const :tag "paren like in \"2)\"" ?\))
  542. (const :tab "both" t)))
  543. (defcustom org-auto-renumber-ordered-lists t
  544. "Non-nil means, automatically renumber ordered plain lists.
  545. Renumbering happens when the sequence have been changed with
  546. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  547. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  548. :group 'org-plain-lists
  549. :type 'boolean)
  550. (defcustom org-provide-checkbox-statistics t
  551. "Non-nil means, update checkbox statistics after insert and toggle.
  552. When this is set, checkbox statistics is updated each time you either insert
  553. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  554. with \\[org-ctrl-c-ctrl-c\\]."
  555. :group 'org-plain-lists
  556. :type 'boolean)
  557. (defgroup org-archive nil
  558. "Options concerning archiving in Org-mode."
  559. :tag "Org Archive"
  560. :group 'org-structure)
  561. (defcustom org-archive-tag "ARCHIVE"
  562. "The tag that marks a subtree as archived.
  563. An archived subtree does not open during visibility cycling, and does
  564. not contribute to the agenda listings."
  565. :group 'org-archive
  566. :group 'org-keywords
  567. :type 'string)
  568. (defcustom org-agenda-skip-archived-trees t
  569. "Non-nil means, the agenda will skip any items located in archived trees.
  570. An archived tree is a tree marked with the tag ARCHIVE."
  571. :group 'org-archive
  572. :group 'org-agenda-skip
  573. :type 'boolean)
  574. (defcustom org-cycle-open-archived-trees nil
  575. "Non-nil means, `org-cycle' will open archived trees.
  576. An archived tree is a tree marked with the tag ARCHIVE.
  577. When nil, archived trees will stay folded. You can still open them with
  578. normal outline commands like `show-all', but not with the cycling commands."
  579. :group 'org-archive
  580. :group 'org-cycle
  581. :type 'boolean)
  582. (defcustom org-sparse-tree-open-archived-trees nil
  583. "Non-nil means sparse tree construction shows matches in archived trees.
  584. When nil, matches in these trees are highlighted, but the trees are kept in
  585. collapsed state."
  586. :group 'org-archive
  587. :group 'org-sparse-trees
  588. :type 'boolean)
  589. (defcustom org-archive-location "%s_archive::"
  590. "The location where subtrees should be archived.
  591. This string consists of two parts, separated by a double-colon.
  592. The first part is a file name - when omitted, archiving happens in the same
  593. file. %s will be replaced by the current file name (without directory part).
  594. Archiving to a different file is useful to keep archived entries from
  595. contributing to the Org-mode Agenda.
  596. The part after the double colon is a headline. The archived entries will be
  597. filed under that headline. When omitted, the subtrees are simply filed away
  598. at the end of the file, as top-level entries.
  599. Here are a few examples:
  600. \"%s_archive::\"
  601. If the current file is Projects.org, archive in file
  602. Projects.org_archive, as top-level trees. This is the default.
  603. \"::* Archived Tasks\"
  604. Archive in the current file, under the top-level headline
  605. \"* Archived Tasks\".
  606. \"~/org/archive.org::\"
  607. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  608. \"basement::** Finished Tasks\"
  609. Archive in file ./basement (relative path), as level 3 trees
  610. below the level 2 heading \"** Finished Tasks\".
  611. You may set this option on a per-file basis by adding to the buffer a
  612. line like
  613. #+ARCHIVE: basement::** Finished Tasks"
  614. :group 'org-archive
  615. :type 'string)
  616. (defcustom org-archive-mark-done t
  617. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  618. This can be a string to set the keyword to use. When t, Org-mode will
  619. use the first keyword in its list that means done."
  620. :group 'org-archive
  621. :type '(choice
  622. (const :tag "No" nil)
  623. (const :tag "Yes" t)
  624. (string :tag "Use this keyword")))
  625. (defcustom org-archive-stamp-time t
  626. "Non-nil means, add a time stamp to entries moved to an archive file."
  627. :group 'org-archive
  628. :type 'boolean)
  629. (defgroup org-table nil
  630. "Options concerning tables in Org-mode."
  631. :tag "Org Table"
  632. :group 'org)
  633. (defcustom org-enable-table-editor 'optimized
  634. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  635. When nil, such lines will be treated like ordinary lines.
  636. When equal to the symbol `optimized', the table editor will be optimized to
  637. do the following:
  638. - Automatic overwrite mode in front of whitespace in table fields.
  639. This makes the structure of the table stay in tact as long as the edited
  640. field does not exceed the column width.
  641. - Minimize the number of realigns. Normally, the table is aligned each time
  642. TAB or RET are pressed to move to another field. With optimization this
  643. happens only if changes to a field might have changed the column width.
  644. Optimization requires replacing the functions `self-insert-command',
  645. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  646. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  647. very good at guessing when a re-align will be necessary, but you can always
  648. force one with \\[org-ctrl-c-ctrl-c].
  649. If you would like to use the optimized version in Org-mode, but the
  650. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  651. This variable can be used to turn on and off the table editor during a session,
  652. but in order to toggle optimization, a restart is required.
  653. See also the variable `org-table-auto-blank-field'."
  654. :group 'org-table
  655. :type '(choice
  656. (const :tag "off" nil)
  657. (const :tag "on" t)
  658. (const :tag "on, optimized" optimized)))
  659. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  660. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  661. In the optimized version, the table editor takes over all simple keys that
  662. normally just insert a character. In tables, the characters are inserted
  663. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  664. for empty fields). Outside tables, the correct binding of the keys is
  665. restored.
  666. The default for this option is t if the optimized version is also used in
  667. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  668. this variable requires a restart of Emacs to become effective."
  669. :group 'org-table
  670. :type 'boolean)
  671. (defcustom orgtbl-radio-table-templates
  672. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  673. % END RECEIVE ORGTBL %n
  674. \\begin{comment}
  675. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  676. | | |
  677. \\end{comment}\n")
  678. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  679. @c END RECEIVE ORGTBL %n
  680. @ignore
  681. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  682. | | |
  683. @end ignore\n")
  684. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  685. <!-- END RECEIVE ORGTBL %n -->
  686. <!--
  687. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  688. | | |
  689. -->\n"))
  690. "Templates for radio tables in different major modes.
  691. All occurrences of %n in a template will be replaced with the name of the
  692. table, obtained by prompting the user."
  693. :group 'org-table
  694. :type '(repeat
  695. (list (symbol :tag "Major mode")
  696. (string :tag "Format"))))
  697. (defgroup org-table-settings nil
  698. "Settings for tables in Org-mode."
  699. :tag "Org Table Settings"
  700. :group 'org-table)
  701. (defcustom org-table-default-size "5x2"
  702. "The default size for newly created tables, Columns x Rows."
  703. :group 'org-table-settings
  704. :type 'string)
  705. (defcustom org-table-number-regexp
  706. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  707. "Regular expression for recognizing numbers in table columns.
  708. If a table column contains mostly numbers, it will be aligned to the
  709. right. If not, it will be aligned to the left.
  710. The default value of this option is a regular expression which allows
  711. anything which looks remotely like a number as used in scientific
  712. context. For example, all of the following will be considered a
  713. number:
  714. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  715. Other options offered by the customize interface are more restrictive."
  716. :group 'org-table-settings
  717. :type '(choice
  718. (const :tag "Positive Integers"
  719. "^[0-9]+$")
  720. (const :tag "Integers"
  721. "^[-+]?[0-9]+$")
  722. (const :tag "Floating Point Numbers"
  723. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  724. (const :tag "Floating Point Number or Integer"
  725. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  726. (const :tag "Exponential, Floating point, Integer"
  727. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  728. (const :tag "Very General Number-Like, including hex"
  729. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  730. (string :tag "Regexp:")))
  731. (defcustom org-table-number-fraction 0.5
  732. "Fraction of numbers in a column required to make the column align right.
  733. In a column all non-white fields are considered. If at least this
  734. fraction of fields is matched by `org-table-number-fraction',
  735. alignment to the right border applies."
  736. :group 'org-table-settings
  737. :type 'number)
  738. (defgroup org-table-editing nil
  739. "Bahavior of tables during editing in Org-mode."
  740. :tag "Org Table Editing"
  741. :group 'org-table)
  742. (defcustom org-table-automatic-realign t
  743. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  744. When nil, aligning is only done with \\[org-table-align], or after column
  745. removal/insertion."
  746. :group 'org-table-editing
  747. :type 'boolean)
  748. (defcustom org-table-auto-blank-field t
  749. "Non-nil means, automatically blank table field when starting to type into it.
  750. This only happens when typing immediately after a field motion
  751. command (TAB, S-TAB or RET).
  752. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  753. :group 'org-table-editing
  754. :type 'boolean)
  755. (defcustom org-table-tab-jumps-over-hlines t
  756. "Non-nil means, tab in the last column of a table with jump over a hline.
  757. If a horizontal separator line is following the current line,
  758. `org-table-next-field' can either create a new row before that line, or jump
  759. over the line. When this option is nil, a new line will be created before
  760. this line."
  761. :group 'org-table-editing
  762. :type 'boolean)
  763. (defcustom org-table-tab-recognizes-table.el t
  764. "Non-nil means, TAB will automatically notice a table.el table.
  765. When it sees such a table, it moves point into it and - if necessary -
  766. calls `table-recognize-table'."
  767. :group 'org-table-editing
  768. :type 'boolean)
  769. (defgroup org-table-calculation nil
  770. "Options concerning tables in Org-mode."
  771. :tag "Org Table Calculation"
  772. :group 'org-table)
  773. (defcustom org-table-use-standard-references t
  774. "Should org-mode work with table refrences like B3 instead of @3$2?
  775. Possible values are:
  776. nil never use them
  777. from accept as input, do not present for editing
  778. t: accept as input and present for editing"
  779. :group 'org-table-calculation
  780. :type '(choice
  781. (const :tag "Never, don't even check unser input for them" nil)
  782. (const :tag "Always, both as user input, and when editing" t)
  783. (const :tag "Convert user input, don't offer during editing" 'from)))
  784. (defcustom org-table-copy-increment t
  785. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  786. :group 'org-table-calculation
  787. :type 'boolean)
  788. (defcustom org-calc-default-modes
  789. '(calc-internal-prec 12
  790. calc-float-format (float 5)
  791. calc-angle-mode deg
  792. calc-prefer-frac nil
  793. calc-symbolic-mode nil
  794. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  795. calc-display-working-message t
  796. )
  797. "List with Calc mode settings for use in calc-eval for table formulas.
  798. The list must contain alternating symbols (Calc modes variables and values).
  799. Don't remove any of the default settings, just change the values. Org-mode
  800. relies on the variables to be present in the list."
  801. :group 'org-table-calculation
  802. :type 'plist)
  803. (defcustom org-table-formula-evaluate-inline t
  804. "Non-nil means, TAB and RET evaluate a formula in current table field.
  805. If the current field starts with an equal sign, it is assumed to be a formula
  806. which should be evaluated as described in the manual and in the documentation
  807. string of the command `org-table-eval-formula'. This feature requires the
  808. Emacs calc package.
  809. When this variable is nil, formula calculation is only available through
  810. the command \\[org-table-eval-formula]."
  811. :group 'org-table-calculation
  812. :type 'boolean)
  813. (defcustom org-table-formula-use-constants t
  814. "Non-nil means, interpret constants in formulas in tables.
  815. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  816. by the value given in `org-table-formula-constants', or by a value obtained
  817. from the `constants.el' package."
  818. :group 'org-table-calculation
  819. :type 'boolean)
  820. (defcustom org-table-formula-constants nil
  821. "Alist with constant names and values, for use in table formulas.
  822. The car of each element is a name of a constant, without the `$' before it.
  823. The cdr is the value as a string. For example, if you'd like to use the
  824. speed of light in a formula, you would configure
  825. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  826. and then use it in an equation like `$1*$c'.
  827. Constants can also be defined on a per-file basis using a line like
  828. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  829. :group 'org-table-calculation
  830. :type '(repeat
  831. (cons (string :tag "name")
  832. (string :tag "value"))))
  833. (defvar org-table-formula-constants-local nil
  834. "Local version of `org-table-formula-constants'.")
  835. (make-variable-buffer-local 'org-table-formula-constants-local)
  836. (defcustom org-table-allow-automatic-line-recalculation t
  837. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  838. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  839. :group 'org-table-calculation
  840. :type 'boolean)
  841. (defgroup org-link nil
  842. "Options concerning links in Org-mode."
  843. :tag "Org Link"
  844. :group 'org)
  845. (defvar org-link-abbrev-alist-local nil
  846. "Buffer-local version of `org-link-abbrev-alist', which see.
  847. The value of this is taken from the #+LINK lines.")
  848. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  849. (defcustom org-link-abbrev-alist nil
  850. "Alist of link abbreviations.
  851. The car of each element is a string, to be replaced at the start of a link.
  852. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  853. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  854. [[linkkey:tag][description]]
  855. If REPLACE is a string, the tag will simply be appended to create the link.
  856. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  857. also be a function that will be called with the tag as the only argument to
  858. create the link. See the manual for examples."
  859. :group 'org-link
  860. :type 'alist)
  861. (defcustom org-descriptive-links t
  862. "Non-nil means, hide link part and only show description of bracket links.
  863. Bracket links are like [[link][descritpion]]. This variable sets the initial
  864. state in new org-mode buffers. The setting can then be toggled on a
  865. per-buffer basis from the Org->Hyperlinks menu."
  866. :group 'org-link
  867. :type 'boolean)
  868. (defcustom org-link-file-path-type 'adaptive
  869. "How the path name in file links should be stored.
  870. Valid values are:
  871. relative relative to the current directory, i.e. the directory of the file
  872. into which the link is being inserted.
  873. absolute absolute path, if possible with ~ for home directory.
  874. noabbrev absolute path, no abbreviation of home directory.
  875. adaptive Use relative path for files in the current directory and sub-
  876. directories of it. For other files, use an absolute path."
  877. :group 'org-link
  878. :type '(choice
  879. (const relative)
  880. (const absolute)
  881. (const noabbrev)
  882. (const adaptive)))
  883. (defcustom org-activate-links '(bracket angle plain radio tag date)
  884. "Types of links that should be activated in Org-mode files.
  885. This is a list of symbols, each leading to the activation of a certain link
  886. type. In principle, it does not hurt to turn on most link types - there may
  887. be a small gain when turning off unused link types. The types are:
  888. bracket The recommended [[link][description]] or [[link]] links with hiding.
  889. angular Links in angular brackes that may contain whitespace like
  890. <bbdb:Carsten Dominik>.
  891. plain Plain links in normal text, no whitespace, like http://google.com.
  892. radio Text that is matched by a radio target, see manual for details.
  893. tag Tag settings in a headline (link to tag search).
  894. date Time stamps (link to calendar).
  895. Changing this variable requires a restart of Emacs to become effective."
  896. :group 'org-link
  897. :type '(set (const :tag "Double bracket links (new style)" bracket)
  898. (const :tag "Angular bracket links (old style)" angular)
  899. (const :tag "plain text links" plain)
  900. (const :tag "Radio target matches" radio)
  901. (const :tag "Tags" tag)
  902. (const :tag "Tags" target)
  903. (const :tag "Timestamps" date)))
  904. (defgroup org-link-store nil
  905. "Options concerning storing links in Org-mode"
  906. :tag "Org Store Link"
  907. :group 'org-link)
  908. (defcustom org-email-link-description-format "Email %c: %.30s"
  909. "Format of the description part of a link to an email or usenet message.
  910. The following %-excapes will be replaced by corresponding information:
  911. %F full \"From\" field
  912. %f name, taken from \"From\" field, address if no name
  913. %T full \"To\" field
  914. %t first name in \"To\" field, address if no name
  915. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  916. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  917. %s subject
  918. %m message-id.
  919. You may use normal field width specification between the % and the letter.
  920. This is for example useful to limit the length of the subject.
  921. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  922. :group 'org-link-store
  923. :type 'string)
  924. (defcustom org-from-is-user-regexp
  925. (let (r1 r2)
  926. (when (and user-mail-address (not (string= user-mail-address "")))
  927. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  928. (when (and user-full-name (not (string= user-full-name "")))
  929. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  930. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  931. "Regexp mached against the \"From:\" header of an email or usenet message.
  932. It should match if the message is from the user him/herself."
  933. :group 'org-link-store
  934. :type 'regexp)
  935. (defcustom org-context-in-file-links t
  936. "Non-nil means, file links from `org-store-link' contain context.
  937. A search string will be added to the file name with :: as separator and
  938. used to find the context when the link is activated by the command
  939. `org-open-at-point'.
  940. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  941. negates this setting for the duration of the command."
  942. :group 'org-link-store
  943. :type 'boolean)
  944. (defcustom org-keep-stored-link-after-insertion nil
  945. "Non-nil means, keep link in list for entire session.
  946. The command `org-store-link' adds a link pointing to the current
  947. location to an internal list. These links accumulate during a session.
  948. The command `org-insert-link' can be used to insert links into any
  949. Org-mode file (offering completion for all stored links). When this
  950. option is nil, every link which has been inserted once using \\[org-insert-link]
  951. will be removed from the list, to make completing the unused links
  952. more efficient."
  953. :group 'org-link-store
  954. :type 'boolean)
  955. (defcustom org-usenet-links-prefer-google nil
  956. "Non-nil means, `org-store-link' will create web links to Google groups.
  957. When nil, Gnus will be used for such links.
  958. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  959. negates this setting for the duration of the command."
  960. :group 'org-link-store
  961. :type 'boolean)
  962. (defgroup org-link-follow nil
  963. "Options concerning following links in Org-mode"
  964. :tag "Org Follow Link"
  965. :group 'org-link)
  966. (defcustom org-tab-follows-link nil
  967. "Non-nil means, on links TAB will follow the link.
  968. Needs to be set before org.el is loaded."
  969. :group 'org-link-follow
  970. :type 'boolean)
  971. (defcustom org-return-follows-link nil
  972. "Non-nil means, on links RET will follow the link.
  973. Needs to be set before org.el is loaded."
  974. :group 'org-link-follow
  975. :type 'boolean)
  976. (defcustom org-mouse-1-follows-link t
  977. "Non-nil means, mouse-1 on a link will follow the link.
  978. A longer mouse click will still set point. Does not wortk on XEmacs.
  979. Needs to be set before org.el is loaded."
  980. :group 'org-link-follow
  981. :type 'boolean)
  982. (defcustom org-mark-ring-length 4
  983. "Number of different positions to be recorded in the ring
  984. Changing this requires a restart of Emacs to work correctly."
  985. :group 'org-link-follow
  986. :type 'interger)
  987. (defcustom org-link-frame-setup
  988. '((vm . vm-visit-folder-other-frame)
  989. (gnus . gnus-other-frame)
  990. (file . find-file-other-window))
  991. "Setup the frame configuration for following links.
  992. When following a link with Emacs, it may often be useful to display
  993. this link in another window or frame. This variable can be used to
  994. set this up for the different types of links.
  995. For VM, use any of
  996. `vm-visit-folder'
  997. `vm-visit-folder-other-frame'
  998. For Gnus, use any of
  999. `gnus'
  1000. `gnus-other-frame'
  1001. For FILE, use any of
  1002. `find-file'
  1003. `find-file-other-window'
  1004. `find-file-other-frame'
  1005. For the calendar, use the variable `calendar-setup'.
  1006. For BBDB, it is currently only possible to display the matches in
  1007. another window."
  1008. :group 'org-link-follow
  1009. :type '(list
  1010. (cons (const vm)
  1011. (choice
  1012. (const vm-visit-folder)
  1013. (const vm-visit-folder-other-window)
  1014. (const vm-visit-folder-other-frame)))
  1015. (cons (const gnus)
  1016. (choice
  1017. (const gnus)
  1018. (const gnus-other-frame)))
  1019. (cons (const file)
  1020. (choice
  1021. (const find-file)
  1022. (const find-file-other-window)
  1023. (const find-file-other-frame)))))
  1024. (defcustom org-display-internal-link-with-indirect-buffer nil
  1025. "Non-nil means, use indirect buffer to display infile links.
  1026. Activating internal links (from one location in a file to another location
  1027. in the same file) normally just jumps to the location. When the link is
  1028. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1029. another window. When this option is set, the other window actually displays
  1030. an indirect buffer clone of the current buffer, to avoid any visibility
  1031. changes to the current buffer."
  1032. :group 'org-link-follow
  1033. :type 'boolean)
  1034. (defcustom org-open-non-existing-files nil
  1035. "Non-nil means, `org-open-file' will open non-existing files.
  1036. When nil, an error will be generated."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1040. "Function and arguments to call for following mailto links.
  1041. This is a list with the first element being a lisp function, and the
  1042. remaining elements being arguments to the function. In string arguments,
  1043. %a will be replaced by the address, and %s will be replaced by the subject
  1044. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1045. :group 'org-link-follow
  1046. :type '(choice
  1047. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1048. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1049. (const :tag "message-mail" (message-mail "%a" "%s"))
  1050. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1051. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1052. "Non-nil means, ask for confirmation before executing shell links.
  1053. Shell links can be dangerous, just thing about a link
  1054. [[shell:rm -rf ~/*][Google Search]]
  1055. This link would show up in your Org-mode document as \"Google Search\"
  1056. but really it would remove your entire home directory.
  1057. Therefore I *definitely* advise against setting this variable to nil.
  1058. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1059. rather than having to type \"yes\"."
  1060. :group 'org-link-follow
  1061. :type '(choice
  1062. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1063. (const :tag "with y-or-n (faster)" y-or-n-p)
  1064. (const :tag "no confirmation (dangerous)" nil)))
  1065. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1066. "Non-nil means, ask for confirmation before executing elisp links.
  1067. Elisp links can be dangerous, just think about a link
  1068. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1069. This link would show up in your Org-mode document as \"Google Search\"
  1070. but really it would remove your entire home directory.
  1071. Therefore I *definitely* advise against setting this variable to nil.
  1072. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1073. rather than having to type \"yes\"."
  1074. :group 'org-link-follow
  1075. :type '(choice
  1076. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1077. (const :tag "with y-or-n (faster)" y-or-n-p)
  1078. (const :tag "no confirmation (dangerous)" nil)))
  1079. (defconst org-file-apps-defaults-gnu
  1080. '((remote . emacs)
  1081. (t . mailcap))
  1082. "Default file applications on a UNIX or GNU/Linux system.
  1083. See `org-file-apps'.")
  1084. (defconst org-file-apps-defaults-macosx
  1085. '((remote . emacs)
  1086. (t . "open %s")
  1087. ("ps" . "gv %s")
  1088. ("ps.gz" . "gv %s")
  1089. ("eps" . "gv %s")
  1090. ("eps.gz" . "gv %s")
  1091. ("dvi" . "xdvi %s")
  1092. ("fig" . "xfig %s"))
  1093. "Default file applications on a MacOS X system.
  1094. The system \"open\" is known as a default, but we use X11 applications
  1095. for some files for which the OS does not have a good default.
  1096. See `org-file-apps'.")
  1097. (defconst org-file-apps-defaults-windowsnt
  1098. (list
  1099. '(remote . emacs)
  1100. (cons t
  1101. (list (if (featurep 'xemacs)
  1102. 'mswindows-shell-execute
  1103. 'w32-shell-execute)
  1104. "open" 'file)))
  1105. "Default file applications on a Windows NT system.
  1106. The system \"open\" is used for most files.
  1107. See `org-file-apps'.")
  1108. (defcustom org-file-apps
  1109. '(
  1110. ("txt" . emacs)
  1111. ("tex" . emacs)
  1112. ("ltx" . emacs)
  1113. ("org" . emacs)
  1114. ("el" . emacs)
  1115. ("bib" . emacs)
  1116. )
  1117. "External applications for opening `file:path' items in a document.
  1118. Org-mode uses system defaults for different file types, but
  1119. you can use this variable to set the application for a given file
  1120. extension. The entries in this list are cons cells where the car identifies
  1121. files and the cdr the corresponding command. Possible values for the
  1122. file identifier are
  1123. \"ext\" A string identifying an extension
  1124. `directory' Matches a directory
  1125. `remote' Matches a remote file, accessible through tramp or efs.
  1126. Remote files most likely should be visited through Emacs
  1127. because external applications cannot handle such paths.
  1128. t Default for all remaining files
  1129. Possible values for the command are:
  1130. `emacs' The file will be visited by the current Emacs process.
  1131. `default' Use the default application for this file type.
  1132. string A command to be executed by a shell; %s will be replaced
  1133. by the path to the file.
  1134. sexp A Lisp form which will be evaluated. The file path will
  1135. be available in the Lisp variable `file'.
  1136. For more examples, see the system specific constants
  1137. `org-file-apps-defaults-macosx'
  1138. `org-file-apps-defaults-windowsnt'
  1139. `org-file-apps-defaults-gnu'."
  1140. :group 'org-link-follow
  1141. :type '(repeat
  1142. (cons (choice :value ""
  1143. (string :tag "Extension")
  1144. (const :tag "Default for unrecognized files" t)
  1145. (const :tag "Remote file" remote)
  1146. (const :tag "Links to a directory" directory))
  1147. (choice :value ""
  1148. (const :tag "Visit with Emacs" emacs)
  1149. (const :tag "Use system default" default)
  1150. (string :tag "Command")
  1151. (sexp :tag "Lisp form")))))
  1152. (defcustom org-mhe-search-all-folders nil
  1153. "Non-nil means, that the search for the mh-message will be extended to
  1154. all folders if the message cannot be found in the folder given in the link.
  1155. Searching all folders is very efficient with one of the search engines
  1156. supported by MH-E, but will be slow with pick."
  1157. :group 'org-link-follow
  1158. :type 'boolean)
  1159. (defgroup org-remember nil
  1160. "Options concerning interaction with remember.el."
  1161. :tag "Org Remember"
  1162. :group 'org)
  1163. (defcustom org-directory "~/org"
  1164. "Directory with org files.
  1165. This directory will be used as default to prompt for org files.
  1166. Used by the hooks for remember.el."
  1167. :group 'org-remember
  1168. :type 'directory)
  1169. (defcustom org-default-notes-file "~/.notes"
  1170. "Default target for storing notes.
  1171. Used by the hooks for remember.el. This can be a string, or nil to mean
  1172. the value of `remember-data-file'.
  1173. You can set this on a per-template basis with the variable
  1174. `org-remember-templates'."
  1175. :group 'org-remember
  1176. :type '(choice
  1177. (const :tag "Default from remember-data-file" nil)
  1178. file))
  1179. (defcustom org-remember-store-without-prompt nil
  1180. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1181. In this case, you need `C-u C-c C-c' to get the prompts for
  1182. note file and headline.
  1183. When this variable is nil, `C-c C-c' give you the prompts, and
  1184. `C-u C-c C-c' trigger the fasttrack."
  1185. :group 'org-remember
  1186. :type 'boolean)
  1187. (defcustom org-remember-default-headline ""
  1188. "The headline that should be the default location in the notes file.
  1189. When filing remember notes, the cursor will start at that position.
  1190. You can set this on a per-template basis with the variable
  1191. `org-remember-templates'."
  1192. :group 'org-remember
  1193. :type 'string)
  1194. (defcustom org-remember-templates nil
  1195. "Templates for the creation of remember buffers.
  1196. When nil, just let remember make the buffer.
  1197. When not nil, this is a list of 4-element lists. In each entry, the first
  1198. element is a character, a unique key to select this template.
  1199. The second element is the template. The third element is optional and can
  1200. specify a destination file for remember items created with this template.
  1201. The default file is given by `org-default-notes-file'. An optional forth
  1202. element can specify the headline in that file that should be offered
  1203. first when the user is asked to file the entry. The default headline is
  1204. given in the variable `org-remember-default-headline'.
  1205. The template specifies the structure of the remember buffer. It should have
  1206. a first line starting with a star, to act as the org-mode headline.
  1207. Furthermore, the following %-escapes will be replaced with content:
  1208. %^{prompt} prompt the user for a string and replace this sequence with it.
  1209. %t time stamp, date only
  1210. %T time stamp with date and time
  1211. %u, %U like the above, but inactive time stamps
  1212. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1213. You may define a prompt like %^{Please specify birthday}t
  1214. %n user name (taken from `user-full-name')
  1215. %a annotation, normally the link created with org-store-link
  1216. %i initial content, the region when remember is called with C-u.
  1217. If %i is indented, the entire inserted text will be indented
  1218. as well.
  1219. %? After completing the template, position cursor here.
  1220. Apart from these general escapes, you can access information specific to the
  1221. link type that is created. For example, calling `remember' in emails or gnus
  1222. will record the author and the subject of the message, which you can access
  1223. with %:author and %:subject, respectively. Here is a complete list of what
  1224. is recorded for each link type.
  1225. Link type | Available information
  1226. -------------------+------------------------------------------------------
  1227. bbdb | %:type %:name %:company
  1228. vm, wl, mh, rmail | %:type %:subject %:message-id
  1229. | %:from %:fromname %:fromaddress
  1230. | %:to %:toname %:toaddress
  1231. | %:fromto (either \"to NAME\" or \"from NAME\")
  1232. gnus | %:group, for messages also all email fields
  1233. w3, w3m | %:type %:url
  1234. info | %:type %:file %:node
  1235. calendar | %:type %:date"
  1236. :group 'org-remember
  1237. :get (lambda (var) ; Make sure all entries have 4 elements
  1238. (mapcar (lambda (x)
  1239. (cond ((= (length x) 3) (append x '("")))
  1240. ((= (length x) 2) (append x '("" "")))
  1241. (t x)))
  1242. (default-value var)))
  1243. :type '(repeat
  1244. :tag "enabled"
  1245. (list :value (?a "\n" nil nil)
  1246. (character :tag "Selection Key")
  1247. (string :tag "Template")
  1248. (file :tag "Destination file (optional)")
  1249. (string :tag "Destination headline (optional)"))))
  1250. (defcustom org-reverse-note-order nil
  1251. "Non-nil means, store new notes at the beginning of a file or entry.
  1252. When nil, new notes will be filed to the end of a file or entry."
  1253. :group 'org-remember
  1254. :type '(choice
  1255. (const :tag "Reverse always" t)
  1256. (const :tag "Reverse never" nil)
  1257. (repeat :tag "By file name regexp"
  1258. (cons regexp boolean))))
  1259. (defgroup org-todo nil
  1260. "Options concerning TODO items in Org-mode."
  1261. :tag "Org TODO"
  1262. :group 'org)
  1263. (defgroup org-progress nil
  1264. "Options concerning Progress logging in Org-mode."
  1265. :tag "Org Progress"
  1266. :group 'org-time)
  1267. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1268. "List of TODO entry keyword sequences and their interpretation.
  1269. \\<org-mode-map>This is a list of sequences.
  1270. Each sequence starts with a symbol, either `sequence' or `type',
  1271. indicating if the keywords should be interpreted as a sequence of
  1272. action steps, or as different types of TODO items. The first
  1273. keywords are states requiring action - these states will select a headline
  1274. for inclusion into the global TODO list Org-mode produces. If one of
  1275. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1276. signify that no further action is necessary. If \"|\" is not found,
  1277. the last keyword is treated as the only DONE state of the sequence.
  1278. The command \\[org-todo] cycles an entry through these states, and one
  1279. additional state where no keyword is present. For details about this
  1280. cycling, see the manual.
  1281. TODO keywords and interpretation can also be set on a per-file basis with
  1282. the special #+SEQ_TODO and #+TYP_TODO lines.
  1283. For backward compatibility, this variable may also be just a list
  1284. of keywords - in this case the interptetation (sequence or type) will be
  1285. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1286. :group 'org-todo
  1287. :group 'org-keywords
  1288. :type '(choice
  1289. (repeat :tag "Old syntax, just keywords"
  1290. (string :tag "Keyword"))
  1291. (repeat :tag "New syntax"
  1292. (cons
  1293. (choice
  1294. :tag "Interpretation"
  1295. (const :tag "Sequence (cycling hits every state)" sequence)
  1296. (const :tag "Type (cycling directly to DONE)" type))
  1297. (repeat
  1298. (string :tag "Keyword"))))))
  1299. (defvar org-todo-keywords-1 nil)
  1300. (make-variable-buffer-local 'org-todo-keywords-1)
  1301. (defvar org-todo-keywords-for-agenda nil)
  1302. (defvar org-done-keywords-for-agenda nil)
  1303. (defvar org-not-done-keywords nil)
  1304. (make-variable-buffer-local 'org-not-done-keywords)
  1305. (defvar org-done-keywords nil)
  1306. (make-variable-buffer-local 'org-done-keywords)
  1307. (defvar org-todo-heads nil)
  1308. (make-variable-buffer-local 'org-todo-heads)
  1309. (defvar org-todo-sets nil)
  1310. (make-variable-buffer-local 'org-todo-sets)
  1311. (defvar org-todo-kwd-alist nil)
  1312. (make-variable-buffer-local 'org-todo-kwd-alist)
  1313. (defcustom org-todo-interpretation 'sequence
  1314. "Controls how TODO keywords are interpreted.
  1315. This variable is in principle obsolete and is only used for
  1316. backward compatibility, if the interpretation of todo keywords is
  1317. not given already in `org-todo-keywords'. See that variable for
  1318. more information."
  1319. :group 'org-todo
  1320. :group 'org-keywords
  1321. :type '(choice (const sequence)
  1322. (const type)))
  1323. (defcustom org-after-todo-state-change-hook nil
  1324. "Hook which is run after the state of a TODO item was changed.
  1325. The new state (a string with a TODO keyword, or nil) is available in the
  1326. Lisp variable `state'."
  1327. :group 'org-todo
  1328. :type 'hook)
  1329. (defcustom org-log-done nil
  1330. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1331. When the state of an entry is changed from nothing to TODO, remove a previous
  1332. closing date.
  1333. This can also be a list of symbols indicating under which conditions
  1334. the time stamp recording the action should be annotated with a short note.
  1335. Valid members of this list are
  1336. done Offer to record a note when marking entries done
  1337. state Offer to record a note whenever changing the TODO state
  1338. of an item. This is only relevant if TODO keywords are
  1339. interpreted as sequence, see variable `org-todo-interpretation'.
  1340. When `state' is set, this includes tracking `done'.
  1341. clock-out Offer to record a note when clocking out of an item.
  1342. A separate window will then pop up and allow you to type a note.
  1343. After finishing with C-c C-c, the note will be added directly after the
  1344. timestamp, as a plain list item. See also the variable
  1345. `org-log-note-headings'.
  1346. Logging can also be configured on a per-file basis by adding one of
  1347. the following lines anywhere in the buffer:
  1348. #+STARTUP: logdone
  1349. #+STARTUP: nologging
  1350. #+STARTUP: lognotedone
  1351. #+STARTUP: lognotestate
  1352. #+STARTUP: lognoteclock-out"
  1353. :group 'org-todo
  1354. :group 'org-progress
  1355. :type '(choice
  1356. (const :tag "off" nil)
  1357. (const :tag "on" t)
  1358. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1359. (const :tag "when item is marked DONE" done)
  1360. (const :tag "when TODO state changes" state)
  1361. (const :tag "when clocking out" clock-out))))
  1362. (defcustom org-log-done-with-time t
  1363. "Non-nil means, the CLOSED time stamp will contain date and time.
  1364. When nil, only the date will be recorded."
  1365. :group 'org-progress
  1366. :type 'boolean)
  1367. (defcustom org-log-note-headings
  1368. '((done . "CLOSING NOTE %t")
  1369. (state . "State %-12s %t")
  1370. (clock-out . ""))
  1371. "Headings for notes added when clocking out or closing TODO items.
  1372. The value is an alist, with the car being a symbol indicating the note
  1373. context, and the cdr is the heading to be used. The heading may also be the
  1374. empty string.
  1375. %t in the heading will be replaced by a time stamp.
  1376. %s will be replaced by the new TODO state, in double quotes.
  1377. %u will be replaced by the user name.
  1378. %U will be replaced by the full user name."
  1379. :group 'org-todo
  1380. :group 'org-progress
  1381. :type '(list :greedy t
  1382. (cons (const :tag "Heading when closing an item" done) string)
  1383. (cons (const :tag
  1384. "Heading when changing todo state (todo sequence only)"
  1385. state) string)
  1386. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1387. (defcustom org-log-states-order-reversed t
  1388. "Non-nil means, the latest state change note will be directly after heading.
  1389. When nil, the notes will be orderer according to time."
  1390. :group 'org-todo
  1391. :group 'org-progress
  1392. :type 'boolean)
  1393. (defcustom org-log-repeat t
  1394. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1395. When nil, no note will be taken."
  1396. :group 'org-todo
  1397. :group 'org-progress
  1398. :type 'boolean)
  1399. (defcustom org-clock-out-when-done t
  1400. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1401. Nil means, clock will keep running until stopped explicitly with
  1402. `C-c C-x C-o', or until the clock is started in a different item."
  1403. :group 'org-progress
  1404. :type 'boolean)
  1405. (defgroup org-priorities nil
  1406. "Priorities in Org-mode."
  1407. :tag "Org Priorities"
  1408. :group 'org-todo)
  1409. (defcustom org-highest-priority ?A
  1410. "The highest priority of TODO items. A character like ?A, ?B etc.
  1411. Must have a smaller ASCII number than `org-lowest-priority'."
  1412. :group 'org-priorities
  1413. :type 'character)
  1414. (defcustom org-lowest-priority ?C
  1415. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1416. Must have a larger ASCII number than `org-highest-priority'."
  1417. :group 'org-priorities
  1418. :type 'character)
  1419. (defcustom org-default-priority ?B
  1420. "The default priority of TODO items.
  1421. This is the priority an item get if no explicit priority is given."
  1422. :group 'org-priorities
  1423. :type 'character)
  1424. (defgroup org-time nil
  1425. "Options concerning time stamps and deadlines in Org-mode."
  1426. :tag "Org Time"
  1427. :group 'org)
  1428. (defcustom org-insert-labeled-timestamps-at-point nil
  1429. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1430. When nil, these labeled time stamps are forces into the second line of an
  1431. entry, just after the headline. When scheduling from the global TODO list,
  1432. the time stamp will always be forced into the second line."
  1433. :group 'org-time
  1434. :type 'boolean)
  1435. (defcustom org-insert-labeled-timestamps-before-properties-drawer t
  1436. "Non-nil means, always insert planning info before property drawer.
  1437. When this is nil and there is a property drawer *directly* after
  1438. the headline, move the planning info into the drawer. If the property
  1439. drawer separated from the headline by at least one line, this variable
  1440. has no effect."
  1441. :group 'org-time
  1442. :type 'boolean)
  1443. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1444. "Formats for `format-time-string' which are used for time stamps.
  1445. It is not recommended to change this constant.")
  1446. (defcustom org-time-stamp-rounding-minutes 0
  1447. "Number of minutes to round time stamps to upon insertion.
  1448. When zero, insert the time unmodified. Useful rounding numbers
  1449. should be factors of 60, so for example 5, 10, 15.
  1450. When this is not zero, you can still force an exact time-stamp by using
  1451. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1452. :group 'org-time
  1453. :type 'integer)
  1454. (defcustom org-display-custom-times nil
  1455. "Non-nil means, overlay custom formats over all time stamps.
  1456. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1457. To turn this on on a per-file basis, insert anywhere in the file:
  1458. #+STARTUP: customtime"
  1459. :group 'org-time
  1460. :set 'set-default
  1461. :type 'sexp)
  1462. (make-variable-buffer-local 'org-display-custom-times)
  1463. (defcustom org-time-stamp-custom-formats
  1464. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1465. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1466. These are overlayed over the default ISO format if the variable
  1467. `org-display-custom-times' is set. Time like %H:%M should be at the
  1468. end of the second format."
  1469. :group 'org-time
  1470. :type 'sexp)
  1471. (defun org-time-stamp-format (&optional long inactive)
  1472. "Get the right format for a time string."
  1473. (let ((f (if long (cdr org-time-stamp-formats)
  1474. (car org-time-stamp-formats))))
  1475. (if inactive
  1476. (concat "[" (substring f 1 -1) "]")
  1477. f)))
  1478. (defcustom org-deadline-warning-days 30
  1479. "No. of days before expiration during which a deadline becomes active.
  1480. This variable governs the display in sparse trees and in the agenda."
  1481. :group 'org-time
  1482. :type 'number)
  1483. (defcustom org-popup-calendar-for-date-prompt t
  1484. "Non-nil means, pop up a calendar when prompting for a date.
  1485. In the calendar, the date can be selected with mouse-1. However, the
  1486. minibuffer will also be active, and you can simply enter the date as well.
  1487. When nil, only the minibuffer will be available."
  1488. :group 'org-time
  1489. :type 'boolean)
  1490. (defcustom org-calendar-follow-timestamp-change t
  1491. "Non-nil means, make the calendar window follow timestamp changes.
  1492. When a timestamp is modified and the calendar window is visible, it will be
  1493. moved to the new date."
  1494. :group 'org-time
  1495. :type 'boolean)
  1496. (defgroup org-tags nil
  1497. "Options concerning tags in Org-mode."
  1498. :tag "Org Tags"
  1499. :group 'org)
  1500. (defcustom org-tag-alist nil
  1501. "List of tags allowed in Org-mode files.
  1502. When this list is nil, Org-mode will base TAG input on what is already in the
  1503. buffer.
  1504. The value of this variable is an alist, the car may be (and should) be a
  1505. character that is used to select that tag through the fast-tag-selection
  1506. interface. See the manual for details."
  1507. :group 'org-tags
  1508. :type '(repeat
  1509. (choice
  1510. (cons (string :tag "Tag name")
  1511. (character :tag "Access char"))
  1512. (const :tag "Start radio group" (:startgroup))
  1513. (const :tag "End radio group" (:endgroup)))))
  1514. (defcustom org-use-fast-tag-selection 'auto
  1515. "Non-nil means, use fast tag selection scheme.
  1516. This is a special interface to select and deselect tags with single keys.
  1517. When nil, fast selection is never used.
  1518. When the symbol `auto', fast selection is used if and only if selection
  1519. characters for tags have been configured, either through the variable
  1520. `org-tag-alist' or through a #+TAGS line in the buffer.
  1521. When t, fast selection is always used and selection keys are assigned
  1522. automatically if necessary."
  1523. :group 'org-tags
  1524. :type '(choice
  1525. (const :tag "Always" t)
  1526. (const :tag "Never" nil)
  1527. (const :tag "When selection characters are configured" 'auto)))
  1528. (defcustom org-fast-tag-selection-single-key nil
  1529. "Non-nil means, fast tag selection exits after first change.
  1530. When nil, you have to press RET to exit it.
  1531. During fast tag selection, you can toggle this flag with `C-c'.
  1532. This variable can also have the value `expert'. In this case, the window
  1533. displaying the tags menu is not even shown, until you press C-c again."
  1534. :group 'org-tags
  1535. :type '(choice
  1536. (const :tag "No" nil)
  1537. (const :tag "Yes" t)
  1538. (const :tag "Expert" expert)))
  1539. (defcustom org-tags-column 48
  1540. "The column to which tags should be indented in a headline.
  1541. If this number is positive, it specifies the column. If it is negative,
  1542. it means that the tags should be flushright to that column. For example,
  1543. -79 works well for a normal 80 character screen."
  1544. :group 'org-tags
  1545. :type 'integer)
  1546. (defcustom org-auto-align-tags t
  1547. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1548. These operations change the length of a headline and therefore shift
  1549. the tags around. With this options turned on, after each such operation
  1550. the tags are again aligned to `org-tags-column'."
  1551. :group 'org-tags
  1552. :type 'boolean)
  1553. (defcustom org-use-tag-inheritance t
  1554. "Non-nil means, tags in levels apply also for sublevels.
  1555. When nil, only the tags directly given in a specific line apply there.
  1556. If you turn off this option, you very likely want to turn on the
  1557. companion option `org-tags-match-list-sublevels'."
  1558. :group 'org-tags
  1559. :type 'boolean)
  1560. (defcustom org-tags-match-list-sublevels nil
  1561. "Non-nil means list also sublevels of headlines matching tag search.
  1562. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1563. the sublevels of a headline matching a tag search often also match
  1564. the same search. Listing all of them can create very long lists.
  1565. Setting this variable to nil causes subtrees of a match to be skipped.
  1566. This option is off by default, because inheritance in on. If you turn
  1567. inheritance off, you very likely want to turn this option on.
  1568. As a special case, if the tag search is restricted to TODO items, the
  1569. value of this variable is ignored and sublevels are always checked, to
  1570. make sure all corresponding TODO items find their way into the list."
  1571. :group 'org-tags
  1572. :type 'boolean)
  1573. (defvar org-tags-history nil
  1574. "History of minibuffer reads for tags.")
  1575. (defvar org-last-tags-completion-table nil
  1576. "The last used completion table for tags.")
  1577. (defgroup org-properties nil
  1578. "Options concerning properties in Org-mode."
  1579. :tag "Org Properties"
  1580. :group 'org)
  1581. (defcustom org-property-format "%-10s %s"
  1582. "How property key/value pairs should be formatted by `indent-line'.
  1583. When `indent-line' hits a property definition, it will format the line
  1584. according to this format, mainly to make sure that the values are
  1585. lined-up with respect to each other."
  1586. :group 'org-properties
  1587. :type 'string)
  1588. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1589. "The default column format, if no other format has been defined.
  1590. This variable can be set on the per-file basis by inserting a line
  1591. #+COLUMNS: %25ITEM ....."
  1592. :group 'org-properties
  1593. :type 'string)
  1594. (defcustom org-global-properties nil
  1595. "List of property/value pairs that can be inherited by any entry.
  1596. You can set buffer-local values for this by adding lines like
  1597. #+PROPERTY: NAME VALUE"
  1598. :group 'org-properties
  1599. :type '(repeat
  1600. (cons (string :tag "Property")
  1601. (string :tag "Value"))))
  1602. (defvar org-local-properties nil
  1603. "List of property/value pairs that can be inherited by any entry.
  1604. Valid for the current buffer.
  1605. This variable is populated from #+PROPERTY lines.")
  1606. (defgroup org-agenda nil
  1607. "Options concerning agenda views in Org-mode."
  1608. :tag "Org Agenda"
  1609. :group 'org)
  1610. (defvar org-category nil
  1611. "Variable used by org files to set a category for agenda display.
  1612. Such files should use a file variable to set it, for example
  1613. # -*- mode: org; org-category: \"ELisp\"
  1614. or contain a special line
  1615. #+CATEGORY: ELisp
  1616. If the file does not specify a category, then file's base name
  1617. is used instead.")
  1618. (make-variable-buffer-local 'org-category)
  1619. (defcustom org-agenda-files nil
  1620. "The files to be used for agenda display.
  1621. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1622. \\[org-remove-file]. You can also use customize to edit the list.
  1623. If the value of the variable is not a list but a single file name, then
  1624. the list of agenda files is actually stored and maintained in that file, one
  1625. agenda file per line."
  1626. :group 'org-agenda
  1627. :type '(choice
  1628. (repeat :tag "List of files" file)
  1629. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1630. (defcustom org-agenda-confirm-kill 1
  1631. "When set, remote killing from the agenda buffer needs confirmation.
  1632. When t, a confirmation is always needed. When a number N, confirmation is
  1633. only needed when the text to be killed contains more than N non-white lines."
  1634. :group 'org-agenda
  1635. :type '(choice
  1636. (const :tag "Never" nil)
  1637. (const :tag "Always" t)
  1638. (number :tag "When more than N lines")))
  1639. (defcustom org-calendar-to-agenda-key [?c]
  1640. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1641. The command `org-calendar-goto-agenda' will be bound to this key. The
  1642. default is the character `c' because then `c' can be used to switch back and
  1643. forth between agenda and calendar."
  1644. :group 'org-agenda
  1645. :type 'sexp)
  1646. (defgroup org-agenda-export nil
  1647. "Options concerning exporting agenda views in Org-mode."
  1648. :tag "Org Agenda Export"
  1649. :group 'org-agenda)
  1650. (defcustom org-agenda-with-colors t
  1651. "Non-nil means, use colors in agenda views."
  1652. :group 'org-agenda-export
  1653. :type 'boolean)
  1654. (defcustom org-agenda-exporter-settings nil
  1655. "Alist of variable/value pairs that should be active during agenda export.
  1656. This is a good place to set uptions for ps-print and for htmlize."
  1657. :group 'org-agenda-export
  1658. :type '(repeat
  1659. (list
  1660. (variable)
  1661. (sexp :tag "Value"))))
  1662. (defcustom org-agenda-export-html-style ""
  1663. "The style specification for exported HTML Agenda files.
  1664. If this variable contains a string, it will replace the default <style>
  1665. section as produced by `htmlize'.
  1666. Since there are different ways of setting style information, this variable
  1667. needs to contain the full HTML structure to provide a style, including the
  1668. surrounding HTML tags. The style specifications should include definitions
  1669. the fonts used by the agenda, here is an example:
  1670. <style type=\"text/css\">
  1671. p { font-weight: normal; color: gray; }
  1672. .org-agenda-structure {
  1673. font-size: 110%;
  1674. color: #003399;
  1675. font-weight: 600;
  1676. }
  1677. .org-todo {
  1678. color: #cc6666;Week-agenda:
  1679. font-weight: bold;
  1680. }
  1681. .org-done {
  1682. color: #339933;
  1683. }
  1684. .title { text-align: center; }
  1685. .todo, .deadline { color: red; }
  1686. .done { color: green; }
  1687. </style>
  1688. or, if you want to keep the style in a file,
  1689. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1690. As the value of this option simply gets inserted into the HTML <head> header,
  1691. you can \"misuse\" it to also add other text to the header. However,
  1692. <style>...</style> is required, if not present the variable will be ignored."
  1693. :group 'org-agenda-export
  1694. :group 'org-export-html
  1695. :type 'string)
  1696. (defgroup org-agenda-custom-commands nil
  1697. "Options concerning agenda views in Org-mode."
  1698. :tag "Org Agenda Custom Commands"
  1699. :group 'org-agenda)
  1700. (defcustom org-agenda-custom-commands nil
  1701. "Custom commands for the agenda.
  1702. These commands will be offered on the splash screen displayed by the
  1703. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1704. (key type match options files)
  1705. key The key (a single char as a string) to be associated with the command.
  1706. type The command type, any of the following symbols:
  1707. todo Entries with a specific TODO keyword, in all agenda files.
  1708. tags Tags match in all agenda files.
  1709. tags-todo Tags match in all agenda files, TODO entries only.
  1710. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1711. tags-tree Sparse tree with all tags matches in *current* file.
  1712. occur-tree Occur sparse tree for *current* file.
  1713. match What to search for:
  1714. - a single keyword for TODO keyword searches
  1715. - a tags match expression for tags searches
  1716. - a regular expression for occur searches
  1717. options A list of option settings, similar to that in a let form, so like
  1718. this: ((opt1 val1) (opt2 val2) ...)
  1719. files A list of files file to write the produced agenda buffer to
  1720. with the command `org-store-agenda-views'.
  1721. If a file name ends in \".html\", an HTML version of the buffer
  1722. is written out. If it ends in \".ps\", a postscript version is
  1723. produced. Otherwide, only the plain text is written to the file.
  1724. You can also define a set of commands, to create a composite agenda buffer.
  1725. In this case, an entry looks like this:
  1726. (key desc (cmd1 cmd2 ...) general-options file)
  1727. where
  1728. desc A description string to be displayed in the dispatcher menu.
  1729. cmd An agenda command, similar to the above. However, tree commands
  1730. are no allowed, but instead you can get agenda and global todo list.
  1731. So valid commands for a set are:
  1732. (agenda)
  1733. (alltodo)
  1734. (stuck)
  1735. (todo \"match\" options files)
  1736. (tags \"match\" options files)
  1737. (tags-todo \"match\" options files)
  1738. Each command can carry a list of options, and another set of options can be
  1739. given for the whole set of commands. Individual command options take
  1740. precedence over the general options."
  1741. :group 'org-agenda-custom-commands
  1742. :type '(repeat
  1743. (choice :value ("a" tags "" nil)
  1744. (list :tag "Single command"
  1745. (string :tag "Key")
  1746. (choice
  1747. (const :tag "Agenda" agenda)
  1748. (const :tag "TODO list" alltodo)
  1749. (const :tag "Stuck projects" stuck)
  1750. (const :tag "Tags search (all agenda files)" tags)
  1751. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1752. (const :tag "TODO keyword search (all agenda files)" todo)
  1753. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1754. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1755. (const :tag "Occur tree (current buffer)" occur-tree)
  1756. (symbol :tag "Other, user-defined function"))
  1757. (string :tag "Match")
  1758. (repeat :tag "Local options"
  1759. (list (variable :tag "Option") (sexp :tag "Value")))
  1760. (option (repeat :tag "Export" (file :tag "Export to"))))
  1761. (list :tag "Command series, all agenda files"
  1762. (string :tag "Key")
  1763. (string :tag "Description")
  1764. (repeat
  1765. (choice
  1766. (const :tag "Agenda" (agenda))
  1767. (const :tag "TODO list" (alltodo))
  1768. (const :tag "Stuck projects" (stuck))
  1769. (list :tag "Tags search"
  1770. (const :format "" tags)
  1771. (string :tag "Match")
  1772. (repeat :tag "Local options"
  1773. (list (variable :tag "Option")
  1774. (sexp :tag "Value"))))
  1775. (list :tag "Tags search, TODO entries only"
  1776. (const :format "" tags-todo)
  1777. (string :tag "Match")
  1778. (repeat :tag "Local options"
  1779. (list (variable :tag "Option")
  1780. (sexp :tag "Value"))))
  1781. (list :tag "TODO keyword search"
  1782. (const :format "" todo)
  1783. (string :tag "Match")
  1784. (repeat :tag "Local options"
  1785. (list (variable :tag "Option")
  1786. (sexp :tag "Value"))))
  1787. (list :tag "Other, user-defined function"
  1788. (symbol :tag "function")
  1789. (string :tag "Match")
  1790. (repeat :tag "Local options"
  1791. (list (variable :tag "Option")
  1792. (sexp :tag "Value"))))))
  1793. (repeat :tag "General options"
  1794. (list (variable :tag "Option")
  1795. (sexp :tag "Value")))
  1796. (option (repeat :tag "Export" (file :tag "Export to")))))))
  1797. (defcustom org-stuck-projects
  1798. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1799. "How to identify stuck projects.
  1800. This is a list of four items:
  1801. 1. A tags/todo matcher string that is used to identify a project.
  1802. The entire tree below a headline matched by this is considered one project.
  1803. 2. A list of TODO keywords identifying non-stuck projects.
  1804. If the project subtree contains any headline with one of these todo
  1805. keywords, the project is considered to be not stuck. If you specify
  1806. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1807. 3. A list of tags identifying non-stuck projects.
  1808. If the project subtree contains any headline with one of these tags,
  1809. the project is considered to be not stuck. If you specify \"*\" as
  1810. a tag, any tag will mark the project unstuck.
  1811. 4. An arbitrary regular expression matching non-stuck projects.
  1812. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1813. or `C-c a #' to produce the list."
  1814. :group 'org-agenda-custom-commands
  1815. :type '(list
  1816. (string :tag "Tags/TODO match to identify a project")
  1817. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1818. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1819. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1820. (defgroup org-agenda-skip nil
  1821. "Options concerning skipping parts of agenda files."
  1822. :tag "Org Agenda Skip"
  1823. :group 'org-agenda)
  1824. (defcustom org-agenda-todo-list-sublevels t
  1825. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1826. When nil, the sublevels of a TODO entry are not checked, resulting in
  1827. potentially much shorter TODO lists."
  1828. :group 'org-agenda-skip
  1829. :group 'org-todo
  1830. :type 'boolean)
  1831. (defcustom org-agenda-todo-ignore-scheduled nil
  1832. "Non-nil means, don't show scheduled entries in the global todo list.
  1833. The idea behind this is that by scheduling it, you have already taken care
  1834. of this item."
  1835. :group 'org-agenda-skip
  1836. :group 'org-todo
  1837. :type 'boolean)
  1838. (defcustom org-agenda-todo-ignore-deadlines nil
  1839. "Non-nil means, don't show near deadline entries in the global todo list.
  1840. Near means closer than `org-deadline-warning-days' days.
  1841. The idea behind this is that such items will appear in the agenda anyway."
  1842. :group 'org-agenda-skip
  1843. :group 'org-todo
  1844. :type 'boolean)
  1845. (defcustom org-agenda-skip-scheduled-if-done nil
  1846. "Non-nil means don't show scheduled items in agenda when they are done.
  1847. This is relevant for the daily/weekly agenda, not for the TODO list."
  1848. :group 'org-agenda-skip
  1849. :type 'boolean)
  1850. (defcustom org-agenda-skip-deadline-if-done nil
  1851. "Non-nil means don't show deadines when the corresponding item is done.
  1852. When nil, the deadline is still shown and should give you a happy feeling.
  1853. This is relevant for the daily/weekly agenda."
  1854. :group 'org-agenda-skip
  1855. :type 'boolean)
  1856. (defcustom org-timeline-show-empty-dates 3
  1857. "Non-nil means, `org-timeline' also shows dates without an entry.
  1858. When nil, only the days which actually have entries are shown.
  1859. When t, all days between the first and the last date are shown.
  1860. When an integer, show also empty dates, but if there is a gap of more than
  1861. N days, just insert a special line indicating the size of the gap."
  1862. :group 'org-agenda-skip
  1863. :type '(choice
  1864. (const :tag "None" nil)
  1865. (const :tag "All" t)
  1866. (number :tag "at most")))
  1867. (defgroup org-agenda-startup nil
  1868. "Options concerning initial settings in the Agenda in Org Mode."
  1869. :tag "Org Agenda Startup"
  1870. :group 'org-agenda)
  1871. (defcustom org-finalize-agenda-hook nil
  1872. "Hook run just before displaying an agenda buffer."
  1873. :group 'org-agenda-startup
  1874. :type 'hook)
  1875. (defcustom org-agenda-mouse-1-follows-link nil
  1876. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1877. A longer mouse click will still set point. Does not wortk on XEmacs.
  1878. Needs to be set before org.el is loaded."
  1879. :group 'org-agenda-startup
  1880. :type 'boolean)
  1881. (defcustom org-agenda-start-with-follow-mode nil
  1882. "The initial value of follow-mode in a newly created agenda window."
  1883. :group 'org-agenda-startup
  1884. :type 'boolean)
  1885. (defgroup org-agenda-windows nil
  1886. "Options concerning the windows used by the Agenda in Org Mode."
  1887. :tag "Org Agenda Windows"
  1888. :group 'org-agenda)
  1889. (defcustom org-agenda-window-setup 'reorganize-frame
  1890. "How the agenda buffer should be displayed.
  1891. Possible values for this option are:
  1892. current-window Show agenda in the current window, keeping all other windows.
  1893. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1894. other-window Use `switch-to-buffer-other-window' to display agenda.
  1895. reorganize-frame Show only two windows on the current frame, the current
  1896. window and the agenda.
  1897. See also the variable `org-agenda-restore-windows-after-quit'."
  1898. :group 'org-agenda-windows
  1899. :type '(choice
  1900. (const current-window)
  1901. (const other-frame)
  1902. (const other-window)
  1903. (const reorganize-frame)))
  1904. (defcustom org-agenda-restore-windows-after-quit nil
  1905. "Non-nil means, restore window configuration open exiting agenda.
  1906. Before the window configuration is changed for displaying the agenda,
  1907. the current status is recorded. When the agenda is exited with
  1908. `q' or `x' and this option is set, the old state is restored. If
  1909. `org-agenda-window-setup' is `other-frame', the value of this
  1910. option will be ignored.."
  1911. :group 'org-agenda-windows
  1912. :type 'boolean)
  1913. (defcustom org-indirect-buffer-display 'other-window
  1914. "How should indirect tree buffers be displayed?
  1915. This applies to indirect buffers created with the commands
  1916. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1917. Valid values are:
  1918. current-window Display in the current window
  1919. other-window Just display in another window.
  1920. dedicated-frame Create one new frame, and re-use it each time.
  1921. new-frame Make a new frame each time."
  1922. :group 'org-structure
  1923. :group 'org-agenda-windows
  1924. :type '(choice
  1925. (const :tag "In current window" current-window)
  1926. (const :tag "In current frame, other window" other-window)
  1927. (const :tag "Each time a new frame" new-frame)
  1928. (const :tag "One dedicated frame" dedicated-frame)))
  1929. (defgroup org-agenda-daily/weekly nil
  1930. "Options concerning the daily/weekly agenda."
  1931. :tag "Org Agenda Daily/Weekly"
  1932. :group 'org-agenda)
  1933. (defcustom org-agenda-ndays 7
  1934. "Number of days to include in overview display.
  1935. Should be 1 or 7."
  1936. :group 'org-agenda-daily/weekly
  1937. :type 'number)
  1938. (defcustom org-agenda-start-on-weekday 1
  1939. "Non-nil means, start the overview always on the specified weekday.
  1940. 0 denotes Sunday, 1 denotes Monday etc.
  1941. When nil, always start on the current day."
  1942. :group 'org-agenda-daily/weekly
  1943. :type '(choice (const :tag "Today" nil)
  1944. (number :tag "Weekday No.")))
  1945. (defcustom org-agenda-show-all-dates t
  1946. "Non-nil means, `org-agenda' shows every day in the selected range.
  1947. When nil, only the days which actually have entries are shown."
  1948. :group 'org-agenda-daily/weekly
  1949. :type 'boolean)
  1950. (defcustom org-agenda-date-format "%A %d %B %Y"
  1951. "Format string for displaying dates in the agenda.
  1952. Used by the daily/weekly agenda and by the timeline. This should be
  1953. a format string understood by `format-time-string'.
  1954. FIXME: Not used currently, because of timezone problem."
  1955. :group 'org-agenda-daily/weekly
  1956. :type 'string)
  1957. (defcustom org-agenda-include-diary nil
  1958. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1959. :group 'org-agenda-daily/weekly
  1960. :type 'boolean)
  1961. (defcustom org-agenda-include-all-todo nil
  1962. "Set means weekly/daily agenda will always contain all TODO entries.
  1963. The TODO entries will be listed at the top of the agenda, before
  1964. the entries for specific days."
  1965. :group 'org-agenda-daily/weekly
  1966. :type 'boolean)
  1967. (defcustom org-agenda-repeating-timestamp-show-all t
  1968. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  1969. When nil, only one occurence is shown, either today or the
  1970. nearest into the future."
  1971. :group 'org-agenda-daily/weekly
  1972. :type 'boolean)
  1973. (defgroup org-agenda-time-grid nil
  1974. "Options concerning the time grid in the Org-mode Agenda."
  1975. :tag "Org Agenda Time Grid"
  1976. :group 'org-agenda)
  1977. (defcustom org-agenda-use-time-grid t
  1978. "Non-nil means, show a time grid in the agenda schedule.
  1979. A time grid is a set of lines for specific times (like every two hours between
  1980. 8:00 and 20:00). The items scheduled for a day at specific times are
  1981. sorted in between these lines.
  1982. For details about when the grid will be shown, and what it will look like, see
  1983. the variable `org-agenda-time-grid'."
  1984. :group 'org-agenda-time-grid
  1985. :type 'boolean)
  1986. (defcustom org-agenda-time-grid
  1987. '((daily today require-timed)
  1988. "----------------"
  1989. (800 1000 1200 1400 1600 1800 2000))
  1990. "The settings for time grid for agenda display.
  1991. This is a list of three items. The first item is again a list. It contains
  1992. symbols specifying conditions when the grid should be displayed:
  1993. daily if the agenda shows a single day
  1994. weekly if the agenda shows an entire week
  1995. today show grid on current date, independent of daily/weekly display
  1996. require-timed show grid only if at least one item has a time specification
  1997. The second item is a string which will be places behing the grid time.
  1998. The third item is a list of integers, indicating the times that should have
  1999. a grid line."
  2000. :group 'org-agenda-time-grid
  2001. :type
  2002. '(list
  2003. (set :greedy t :tag "Grid Display Options"
  2004. (const :tag "Show grid in single day agenda display" daily)
  2005. (const :tag "Show grid in weekly agenda display" weekly)
  2006. (const :tag "Always show grid for today" today)
  2007. (const :tag "Show grid only if any timed entries are present"
  2008. require-timed)
  2009. (const :tag "Skip grid times already present in an entry"
  2010. remove-match))
  2011. (string :tag "Grid String")
  2012. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2013. (defgroup org-agenda-sorting nil
  2014. "Options concerning sorting in the Org-mode Agenda."
  2015. :tag "Org Agenda Sorting"
  2016. :group 'org-agenda)
  2017. (let ((sorting-choice
  2018. '(choice
  2019. (const time-up) (const time-down)
  2020. (const category-keep) (const category-up) (const category-down)
  2021. (const tag-down) (const tag-up)
  2022. (const priority-up) (const priority-down))))
  2023. (defcustom org-agenda-sorting-strategy
  2024. '((agenda time-up category-keep priority-down)
  2025. (todo category-keep priority-down)
  2026. (tags category-keep priority-down))
  2027. "Sorting structure for the agenda items of a single day.
  2028. This is a list of symbols which will be used in sequence to determine
  2029. if an entry should be listed before another entry. The following
  2030. symbols are recognized:
  2031. time-up Put entries with time-of-day indications first, early first
  2032. time-down Put entries with time-of-day indications first, late first
  2033. category-keep Keep the default order of categories, corresponding to the
  2034. sequence in `org-agenda-files'.
  2035. category-up Sort alphabetically by category, A-Z.
  2036. category-down Sort alphabetically by category, Z-A.
  2037. tag-up Sort alphabetically by last tag, A-Z.
  2038. tag-down Sort alphabetically by last tag, Z-A.
  2039. priority-up Sort numerically by priority, high priority last.
  2040. priority-down Sort numerically by priority, high priority first.
  2041. The different possibilities will be tried in sequence, and testing stops
  2042. if one comparison returns a \"not-equal\". For example, the default
  2043. '(time-up category-keep priority-down)
  2044. means: Pull out all entries having a specified time of day and sort them,
  2045. in order to make a time schedule for the current day the first thing in the
  2046. agenda listing for the day. Of the entries without a time indication, keep
  2047. the grouped in categories, don't sort the categories, but keep them in
  2048. the sequence given in `org-agenda-files'. Within each category sort by
  2049. priority.
  2050. Leaving out `category-keep' would mean that items will be sorted across
  2051. categories by priority."
  2052. :group 'org-agenda-sorting
  2053. :type `(choice
  2054. (repeat :tag "General" ,sorting-choice)
  2055. (list :tag "Individually"
  2056. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2057. (repeat ,sorting-choice))
  2058. (cons (const :tag "Strategy for TODO lists" todo)
  2059. (repeat ,sorting-choice))
  2060. (cons (const :tag "Strategy for Tags matches" tags)
  2061. (repeat ,sorting-choice))))))
  2062. (defcustom org-sort-agenda-notime-is-late t
  2063. "Non-nil means, items without time are considered late.
  2064. This is only relevant for sorting. When t, items which have no explicit
  2065. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2066. do have a time. When nil, the default time is before 0:00. You can use this
  2067. option to decide if the schedule for today should come before or after timeless
  2068. agenda entries."
  2069. :group 'org-agenda-sorting
  2070. :type 'boolean)
  2071. (defgroup org-agenda-prefix nil
  2072. "Options concerning the entry prefix in the Org-mode agenda display."
  2073. :tag "Org Agenda Prefix"
  2074. :group 'org-agenda)
  2075. (defcustom org-agenda-prefix-format
  2076. '((agenda . " %-12:c%?-12t% s")
  2077. (timeline . " % s")
  2078. (todo . " %-12:c")
  2079. (tags . " %-12:c"))
  2080. "Format specifications for the prefix of items in the agenda views.
  2081. An alist with four entries, for the different agenda types. The keys to the
  2082. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2083. are format strings.
  2084. This format works similar to a printf format, with the following meaning:
  2085. %c the category of the item, \"Diary\" for entries from the diary, or
  2086. as given by the CATEGORY keyword or derived from the file name.
  2087. %T the *last* tag of the item. Last because inherited tags come
  2088. first in the list.
  2089. %t the time-of-day specification if one applies to the entry, in the
  2090. format HH:MM
  2091. %s Scheduling/Deadline information, a short string
  2092. All specifiers work basically like the standard `%s' of printf, but may
  2093. contain two additional characters: A question mark just after the `%' and
  2094. a whitespace/punctuation character just before the final letter.
  2095. If the first character after `%' is a question mark, the entire field
  2096. will only be included if the corresponding value applies to the
  2097. current entry. This is useful for fields which should have fixed
  2098. width when present, but zero width when absent. For example,
  2099. \"%?-12t\" will result in a 12 character time field if a time of the
  2100. day is specified, but will completely disappear in entries which do
  2101. not contain a time.
  2102. If there is punctuation or whitespace character just before the final
  2103. format letter, this character will be appended to the field value if
  2104. the value is not empty. For example, the format \"%-12:c\" leads to
  2105. \"Diary: \" if the category is \"Diary\". If the category were be
  2106. empty, no additional colon would be interted.
  2107. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2108. - Indent the line with two space characters
  2109. - Give the category in a 12 chars wide field, padded with whitespace on
  2110. the right (because of `-'). Append a colon if there is a category
  2111. (because of `:').
  2112. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2113. time, don't put in an empty field, just skip it (because of '?').
  2114. - Finally, put the scheduling information and append a whitespace.
  2115. As another example, if you don't want the time-of-day of entries in
  2116. the prefix, you could use:
  2117. (setq org-agenda-prefix-format \" %-11:c% s\")
  2118. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2119. `org-agenda-remove-tags'."
  2120. :type '(choice
  2121. (string :tag "General format")
  2122. (list :greedy t :tag "View dependent"
  2123. (cons (const agenda) (string :tag "Format"))
  2124. (cons (const timeline) (string :tag "Format"))
  2125. (cons (const todo) (string :tag "Format"))
  2126. (cons (const tags) (string :tag "Format"))))
  2127. :group 'org-agenda-prefix)
  2128. (defvar org-prefix-format-compiled nil
  2129. "The compiled version of the most recently used prefix format.
  2130. See the variable `org-agenda-prefix-format'.")
  2131. (defcustom org-agenda-remove-times-when-in-prefix t
  2132. "Non-nil means, remove duplicate time specifications in agenda items.
  2133. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2134. time-of-day specification in a headline or diary entry is extracted and
  2135. placed into the prefix. If this option is non-nil, the original specification
  2136. \(a timestamp or -range, or just a plain time(range) specification like
  2137. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2138. cluttered.
  2139. The option can be t or nil. It may also be the symbol `beg', indicating
  2140. that the time should only be removed what it is located at the beginning of
  2141. the headline/diary entry."
  2142. :group 'org-agenda-prefix
  2143. :type '(choice
  2144. (const :tag "Always" t)
  2145. (const :tag "Never" nil)
  2146. (const :tag "When at beginning of entry" beg)))
  2147. (defcustom org-agenda-default-appointment-duration nil
  2148. "Default duration for appointments that only have a starting time.
  2149. When nil, no duration is specified in such cases.
  2150. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2151. :group 'org-agenda-prefix
  2152. :type '(choice
  2153. (integer :tag "Minutes")
  2154. (const :tag "No default duration")))
  2155. (defcustom org-agenda-remove-tags nil
  2156. "Non-nil means, remove the tags from the headline copy in the agenda.
  2157. When this is the symbol `prefix', only remove tags when
  2158. `org-agenda-prefix-format' contains a `%T' specifier."
  2159. :group 'org-agenda-prefix
  2160. :type '(choice
  2161. (const :tag "Always" t)
  2162. (const :tag "Never" nil)
  2163. (const :tag "When prefix format contains %T" prefix)))
  2164. (if (fboundp 'defvaralias)
  2165. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2166. 'org-agenda-remove-tags))
  2167. (defcustom org-agenda-align-tags-to-column 65
  2168. "Shift tags in agenda items to this column."
  2169. :group 'org-agenda-prefix
  2170. :type 'integer)
  2171. (defgroup org-latex nil
  2172. "Options for embedding LaTeX code into Org-mode"
  2173. :tag "Org LaTeX"
  2174. :group 'org)
  2175. (defcustom org-format-latex-options
  2176. '(:foreground default :background default :scale 1.0
  2177. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2178. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2179. "Options for creating images from LaTeX fragments.
  2180. This is a property list with the following properties:
  2181. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2182. `default' means use the forground of the default face.
  2183. :background the background color, or \"Transparent\".
  2184. `default' means use the background of the default face.
  2185. :scale a scaling factor for the size of the images
  2186. :html-foreground, :html-background, :html-scale
  2187. The same numbers for HTML export.
  2188. :matchers a list indicating which matchers should be used to
  2189. find LaTeX fragments. Valid members of this list are:
  2190. \"begin\" find environments
  2191. \"$\" find math expressions surrounded by $...$
  2192. \"$$\" find math expressions surrounded by $$....$$
  2193. \"\\(\" find math expressions surrounded by \\(...\\)
  2194. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2195. :group 'org-export-latex
  2196. :type 'plist)
  2197. (defcustom org-format-latex-header "\\documentclass{article}
  2198. \\usepackage{fullpage} % do not remove
  2199. \\usepackage{amssymb}
  2200. \\usepackage[usenames]{color}
  2201. \\usepackage{amsmath}
  2202. \\usepackage{latexsym}
  2203. \\usepackage[mathscr]{eucal}
  2204. \\pagestyle{empty} % do not remove"
  2205. "The document header used for processing LaTeX fragments."
  2206. :group 'org-export-latex
  2207. :type 'string)
  2208. (defgroup org-export nil
  2209. "Options for exporting org-listings."
  2210. :tag "Org Export"
  2211. :group 'org)
  2212. (defgroup org-export-general nil
  2213. "General options for exporting Org-mode files."
  2214. :tag "Org Export General"
  2215. :group 'org-export)
  2216. (defcustom org-export-publishing-directory "."
  2217. "Path to the location where exported files should be located.
  2218. This path may be relative to the directory where the Org-mode file lives.
  2219. The default is to put them into the same directory as the Org-mode file.
  2220. The variable may also be an alist with export types `:html', `:ascii',
  2221. `:ical', or `:xoxo' and the corresponding directories. If a directory path
  2222. is relative, it is interpreted relative to the directory where the exported
  2223. Org-mode files lives."
  2224. :group 'org-export-general
  2225. :type '(choice
  2226. (directory)
  2227. (repeat
  2228. (cons
  2229. (choice :tag "Type"
  2230. (const :html) (const :ascii) (const :ical) (const :xoxo))
  2231. (directory)))))
  2232. (defcustom org-export-language-setup
  2233. '(("en" "Author" "Date" "Table of Contents")
  2234. ("cs" "Autor" "Datum" "Obsah")
  2235. ("da" "Ophavsmand" "Dato" "Indhold")
  2236. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2237. ("es" "Autor" "Fecha" "\xccndice")
  2238. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  2239. ("it" "Autore" "Data" "Indice")
  2240. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2241. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2242. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2243. "Terms used in export text, translated to different languages.
  2244. Use the variable `org-export-default-language' to set the language,
  2245. or use the +OPTION lines for a per-file setting."
  2246. :group 'org-export-general
  2247. :type '(repeat
  2248. (list
  2249. (string :tag "HTML language tag")
  2250. (string :tag "Author")
  2251. (string :tag "Date")
  2252. (string :tag "Table of Contents"))))
  2253. (defcustom org-export-default-language "en"
  2254. "The default language of HTML export, as a string.
  2255. This should have an association in `org-export-language-setup'."
  2256. :group 'org-export-general
  2257. :type 'string)
  2258. (defcustom org-export-skip-text-before-1st-heading t
  2259. "Non-nil means, skip all text before the first headline when exporting.
  2260. When nil, that text is exported as well."
  2261. :group 'org-export-general
  2262. :type 'boolean)
  2263. (defcustom org-export-headline-levels 3
  2264. "The last level which is still exported as a headline.
  2265. Inferior levels will produce itemize lists when exported.
  2266. Note that a numeric prefix argument to an exporter function overrides
  2267. this setting.
  2268. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2269. :group 'org-export-general
  2270. :type 'number)
  2271. (defcustom org-export-with-section-numbers t
  2272. "Non-nil means, add section numbers to headlines when exporting.
  2273. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2274. :group 'org-export-general
  2275. :type 'boolean)
  2276. (defcustom org-export-with-toc t
  2277. "Non-nil means, create a table of contents in exported files.
  2278. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2279. When an integer, include levels up to N in the toc, this may then be
  2280. different from `org-export-headline-levels', but it will not be allowed
  2281. to be larger than the number of headline levels.
  2282. When nil, no table of contents is made.
  2283. Headlines which contain any TODO items will be marked with \"(*)\" in
  2284. ASCII export, and with red color in HTML output, if the option
  2285. `org-export-mark-todo-in-toc' is set.
  2286. In HTML output, the TOC will be clickable.
  2287. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2288. or \"toc:3\"."
  2289. :group 'org-export-general
  2290. :type '(choice
  2291. (const :tag "No Table of Contents" nil)
  2292. (const :tag "Full Table of Contents" t)
  2293. (integer :tag "TOC to level")))
  2294. (defcustom org-export-mark-todo-in-toc nil
  2295. "Non-nil means, mark TOC lines that contain any open TODO items."
  2296. :group 'org-export-general
  2297. :type 'boolean)
  2298. (defcustom org-export-preserve-breaks nil
  2299. "Non-nil means, preserve all line breaks when exporting.
  2300. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2301. export, line breaks will always be preserved, regardless of this variable.
  2302. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2303. :group 'org-export-general
  2304. :type 'boolean)
  2305. (defcustom org-export-with-archived-trees 'headline
  2306. "Whether subtrees with the ARCHIVE tag should be exported.
  2307. This can have three different values
  2308. nil Do not export, pretend this tree is not present
  2309. t Do export the entire tree
  2310. headline Only export the headline, but skip the tree below it."
  2311. :group 'org-export-general
  2312. :group 'org-archive
  2313. :type '(choice
  2314. (const :tag "not at all" nil)
  2315. (const :tag "headline only" 'headline)
  2316. (const :tag "entirely" t)))
  2317. (defcustom org-export-author-info t
  2318. "Non-nil means, insert author name and email into the exported file.
  2319. This option can also be set with the +OPTIONS line,
  2320. e.g. \"author-info:nil\"."
  2321. :group 'org-export-general
  2322. :type 'boolean)
  2323. (defcustom org-export-time-stamp-file t
  2324. "Non-nil means, insert a time stamp into the exported file.
  2325. The time stamp shows when the file was created.
  2326. This option can also be set with the +OPTIONS line,
  2327. e.g. \"timestamp:nil\"."
  2328. :group 'org-export-general
  2329. :type 'boolean)
  2330. (defcustom org-export-with-timestamps t
  2331. "If nil, do not export time stamps and associated keywords."
  2332. :group 'org-export-general
  2333. :type 'boolean)
  2334. (defcustom org-export-remove-timestamps-from-toc t
  2335. "If nil, remove timestamps from the table of contents entries."
  2336. :group 'org-export-general
  2337. :type 'boolean)
  2338. (defcustom org-export-with-tags 'not-in-toc
  2339. "If nil, do not export tags, just remove them from headlines.
  2340. If this is the symbol `not-in-toc', tags will be removed from table of
  2341. contents entries, but still be shown in the headlines of the document."
  2342. :group 'org-export-general
  2343. :type '(choice
  2344. (const :tag "Off" nil)
  2345. (const :tag "Not in TOC" not-in-toc)
  2346. (const :tag "On" t)))
  2347. (defcustom org-export-with-property-drawer nil
  2348. "Non-nil means, export property drawers.
  2349. When nil, these drawers are removed before export.
  2350. This option can also be set with the +OPTIONS line, e.g. \"p:t\"."
  2351. :group 'org-export-general
  2352. :type 'boolean)
  2353. (defgroup org-export-translation nil
  2354. "Options for translating special ascii sequences for the export backends."
  2355. :tag "Org Export Translation"
  2356. :group 'org-export)
  2357. (defcustom org-export-with-emphasize t
  2358. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2359. If the export target supports emphasizing text, the word will be
  2360. typeset in bold, italic, or underlined, respectively. Works only for
  2361. single words, but you can say: I *really* *mean* *this*.
  2362. Not all export backends support this.
  2363. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2364. :group 'org-export-translation
  2365. :type 'boolean)
  2366. (defcustom org-export-with-footnotes t
  2367. "If nil, export [1] as a footnote marker.
  2368. Lines starting with [1] will be formatted as footnotes.
  2369. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2370. :group 'org-export-translation
  2371. :type 'boolean)
  2372. (defcustom org-export-with-sub-superscripts t
  2373. "Non-nil means, interpret \"_\" and \"^\" for export.
  2374. When this option is turned on, you can use TeX-like syntax for sub- and
  2375. superscripts. Several characters after \"_\" or \"^\" will be
  2376. considered as a single item - so grouping with {} is normally not
  2377. needed. For example, the following things will be parsed as single
  2378. sub- or superscripts.
  2379. 10^24 or 10^tau several digits will be considered 1 item.
  2380. 10^-12 or 10^-tau a leading sign with digits or a word
  2381. x^2-y^3 will be read as x^2 - y^3, because items are
  2382. terminated by almost any nonword/nondigit char.
  2383. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2384. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2385. sub/superscript. If you set this variable to the symbol `{}',
  2386. the braces are *required* in order to trigger interpretations as
  2387. sub/superscript. This can be helpful in documents that need \"_\"
  2388. frequently in plain text.
  2389. Not all export backends support this, but HTML does.
  2390. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2391. :group 'org-export-translation
  2392. :type '(choice
  2393. (const :tag "Always interpret" t)
  2394. (const :tag "Only with braces" {})
  2395. (const :tag "Never interpret" nil)))
  2396. (defcustom org-export-with-TeX-macros t
  2397. "Non-nil means, interpret simple TeX-like macros when exporting.
  2398. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2399. No only real TeX macros will work here, but the standard HTML entities
  2400. for math can be used as macro names as well. For a list of supported
  2401. names in HTML export, see the constant `org-html-entities'.
  2402. Not all export backends support this.
  2403. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2404. :group 'org-export-translation
  2405. :group 'org-export-latex
  2406. :type 'boolean)
  2407. (defcustom org-export-with-LaTeX-fragments nil
  2408. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2409. When set, the exporter will find LaTeX environments if the \\begin line is
  2410. the first non-white thing on a line. It will also find the math delimiters
  2411. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2412. display math.
  2413. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2414. :group 'org-export-translation
  2415. :group 'org-export-latex
  2416. :type 'boolean)
  2417. (defcustom org-export-with-fixed-width t
  2418. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2419. This can be used to have pre-formatted text, fragments of code etc. For
  2420. example:
  2421. : ;; Some Lisp examples
  2422. : (while (defc cnt)
  2423. : (ding))
  2424. will be looking just like this in also HTML. See also the QUOTE keyword.
  2425. Not all export backends support this.
  2426. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2427. :group 'org-export-translation
  2428. :type 'boolean)
  2429. (defcustom org-match-sexp-depth 3
  2430. "Number of stacked braces for sub/superscript matching.
  2431. This has to be set before loading org.el to be effective."
  2432. :group 'org-export-translation
  2433. :type 'integer)
  2434. (defgroup org-export-tables nil
  2435. "Options for exporting tables in Org-mode."
  2436. :tag "Org Export Tables"
  2437. :group 'org-export)
  2438. (defcustom org-export-with-tables t
  2439. "If non-nil, lines starting with \"|\" define a table.
  2440. For example:
  2441. | Name | Address | Birthday |
  2442. |-------------+----------+-----------|
  2443. | Arthur Dent | England | 29.2.2100 |
  2444. Not all export backends support this.
  2445. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2446. :group 'org-export-tables
  2447. :type 'boolean)
  2448. (defcustom org-export-highlight-first-table-line t
  2449. "Non-nil means, highlight the first table line.
  2450. In HTML export, this means use <th> instead of <td>.
  2451. In tables created with table.el, this applies to the first table line.
  2452. In Org-mode tables, all lines before the first horizontal separator
  2453. line will be formatted with <th> tags."
  2454. :group 'org-export-tables
  2455. :type 'boolean)
  2456. (defcustom org-export-table-remove-special-lines t
  2457. "Remove special lines and marking characters in calculating tables.
  2458. This removes the special marking character column from tables that are set
  2459. up for spreadsheet calculations. It also removes the entire lines
  2460. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2461. the values of constants may be useful to have."
  2462. :group 'org-export-tables
  2463. :type 'boolean)
  2464. (defcustom org-export-prefer-native-exporter-for-tables nil
  2465. "Non-nil means, always export tables created with table.el natively.
  2466. Natively means, use the HTML code generator in table.el.
  2467. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2468. the table does not use row- or column-spanning). This has the
  2469. advantage, that the automatic HTML conversions for math symbols and
  2470. sub/superscripts can be applied. Org-mode's HTML generator is also
  2471. much faster."
  2472. :group 'org-export-tables
  2473. :type 'boolean)
  2474. (defgroup org-export-ascii nil
  2475. "Options specific for ASCII export of Org-mode files."
  2476. :tag "Org Export ASCII"
  2477. :group 'org-export)
  2478. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2479. "Characters for underlining headings in ASCII export.
  2480. In the given sequence, these characters will be used for level 1, 2, ..."
  2481. :group 'org-export-ascii
  2482. :type '(repeat character))
  2483. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2484. "Bullet characters for headlines converted to lists in ASCII export.
  2485. The first character is is used for the first lest level generated in this
  2486. way, and so on. If there are more levels than characters given here,
  2487. the list will be repeated.
  2488. Note that plain lists will keep the same bullets as the have in the
  2489. Org-mode file."
  2490. :group 'org-export-ascii
  2491. :type '(repeat character))
  2492. (defgroup org-export-xml nil
  2493. "Options specific for XML export of Org-mode files."
  2494. :tag "Org Export XML"
  2495. :group 'org-export)
  2496. (defgroup org-export-html nil
  2497. "Options specific for HTML export of Org-mode files."
  2498. :tag "Org Export HTML"
  2499. :group 'org-export)
  2500. (defcustom org-export-html-coding-system nil
  2501. ""
  2502. :group 'org-export-html
  2503. :type 'coding-system)
  2504. (defcustom org-export-html-style
  2505. "<style type=\"text/css\">
  2506. html {
  2507. font-family: Times, serif;
  2508. font-size: 12pt;
  2509. }
  2510. .title { text-align: center; }
  2511. .todo { color: red; }
  2512. .done { color: green; }
  2513. .timestamp { color: grey }
  2514. .timestamp-kwd { color: CadetBlue }
  2515. .tag { background-color:lightblue; font-weight:normal }
  2516. .target { background-color: lavender; }
  2517. pre {
  2518. border: 1pt solid #AEBDCC;
  2519. background-color: #F3F5F7;
  2520. padding: 5pt;
  2521. font-family: courier, monospace;
  2522. }
  2523. table { border-collapse: collapse; }
  2524. td, th {
  2525. vertical-align: top;
  2526. <!--border: 1pt solid #ADB9CC;-->
  2527. }
  2528. </style>"
  2529. "The default style specification for exported HTML files.
  2530. Since there are different ways of setting style information, this variable
  2531. needs to contain the full HTML structure to provide a style, including the
  2532. surrounding HTML tags. The style specifications should include definitions
  2533. for new classes todo, done, title, and deadline. For example, legal values
  2534. would be:
  2535. <style type=\"text/css\">
  2536. p { font-weight: normal; color: gray; }
  2537. h1 { color: black; }
  2538. .title { text-align: center; }
  2539. .todo, .deadline { color: red; }
  2540. .done { color: green; }
  2541. </style>
  2542. or, if you want to keep the style in a file,
  2543. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2544. As the value of this option simply gets inserted into the HTML <head> header,
  2545. you can \"misuse\" it to add arbitrary text to the header."
  2546. :group 'org-export-html
  2547. :type 'string)
  2548. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2549. "Format for typesetting the document title in HTML export."
  2550. :group 'org-export-html
  2551. :type 'string)
  2552. (defcustom org-export-html-toplevel-hlevel 2
  2553. "The <H> level for level 1 headings in HTML export."
  2554. :group 'org-export-html
  2555. :type 'string)
  2556. (defcustom org-export-html-link-org-files-as-html t
  2557. "Non-nil means, make file links to `file.org' point to `file.html'.
  2558. When org-mode is exporting an org-mode file to HTML, links to
  2559. non-html files are directly put into a href tag in HTML.
  2560. However, links to other Org-mode files (recognized by the
  2561. extension `.org.) should become links to the corresponding html
  2562. file, assuming that the linked org-mode file will also be
  2563. converted to HTML.
  2564. When nil, the links still point to the plain `.org' file."
  2565. :group 'org-export-html
  2566. :type 'boolean)
  2567. (defcustom org-export-html-inline-images 'maybe
  2568. "Non-nil means, inline images into exported HTML pages.
  2569. This is done using an <img> tag. When nil, an anchor with href is used to
  2570. link to the image. If this option is `maybe', then images in links with
  2571. an empty description will be inlined, while images with a description will
  2572. be linked only."
  2573. :group 'org-export-html
  2574. :type '(choice (const :tag "Never" nil)
  2575. (const :tag "Always" t)
  2576. (const :tag "When there is no description" maybe)))
  2577. ;; FIXME: rename
  2578. (defcustom org-export-html-expand t
  2579. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2580. When nil, these tags will be exported as plain text and therefore
  2581. not be interpreted by a browser.
  2582. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2583. :group 'org-export-html
  2584. :type 'boolean)
  2585. (defcustom org-export-html-table-tag
  2586. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2587. "The HTML tag that is used to start a table.
  2588. This must be a <table> tag, but you may change the options like
  2589. borders and spacing."
  2590. :group 'org-export-html
  2591. :type 'string)
  2592. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2593. "The opening tag for table header fields.
  2594. This is customizable so that alignment options can be specified."
  2595. :group 'org-export-tables
  2596. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2597. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2598. "The opening tag for table data fields.
  2599. This is customizable so that alignment options can be specified."
  2600. :group 'org-export-tables
  2601. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2602. (defcustom org-export-html-with-timestamp nil
  2603. "If non-nil, write `org-export-html-html-helper-timestamp'
  2604. into the exported HTML text. Otherwise, the buffer will just be saved
  2605. to a file."
  2606. :group 'org-export-html
  2607. :type 'boolean)
  2608. (defcustom org-export-html-html-helper-timestamp
  2609. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2610. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2611. :group 'org-export-html
  2612. :type 'string)
  2613. (defgroup org-export-icalendar nil
  2614. "Options specific for iCalendar export of Org-mode files."
  2615. :tag "Org Export iCalendar"
  2616. :group 'org-export)
  2617. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2618. "The file name for the iCalendar file covering all agenda files.
  2619. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2620. The file name should be absolute."
  2621. :group 'org-export-icalendar
  2622. :type 'file)
  2623. (defcustom org-icalendar-include-todo nil
  2624. "Non-nil means, export to iCalendar files should also cover TODO items."
  2625. :group 'org-export-icalendar
  2626. :type '(choice
  2627. (const :tag "None" nil)
  2628. (const :tag "Unfinished" t)
  2629. (const :tag "All" all)))
  2630. (defcustom org-icalendar-include-sexps t
  2631. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2632. These are entries like in the diary, but directly in an Org-mode file."
  2633. :group 'org-export-icalendar
  2634. :type 'boolean)
  2635. (defcustom org-icalendar-combined-name "OrgMode"
  2636. "Calendar name for the combined iCalendar representing all agenda files."
  2637. :group 'org-export-icalendar
  2638. :type 'string)
  2639. (defgroup org-font-lock nil
  2640. "Font-lock settings for highlighting in Org-mode."
  2641. :tag "Org Font Lock"
  2642. :group 'org)
  2643. (defcustom org-level-color-stars-only nil
  2644. "Non-nil means fontify only the stars in each headline.
  2645. When nil, the entire headline is fontified.
  2646. Changing it requires restart of `font-lock-mode' to become effective
  2647. also in regions already fontified."
  2648. :group 'org-font-lock
  2649. :type 'boolean)
  2650. (defcustom org-hide-leading-stars nil
  2651. "Non-nil means, hide the first N-1 stars in a headline.
  2652. This works by using the face `org-hide' for these stars. This
  2653. face is white for a light background, and black for a dark
  2654. background. You may have to customize the face `org-hide' to
  2655. make this work.
  2656. Changing it requires restart of `font-lock-mode' to become effective
  2657. also in regions already fontified.
  2658. You may also set this on a per-file basis by adding one of the following
  2659. lines to the buffer:
  2660. #+STARTUP: hidestars
  2661. #+STARTUP: showstars"
  2662. :group 'org-font-lock
  2663. :type 'boolean)
  2664. (defcustom org-fontify-done-headline nil
  2665. "Non-nil means, change the face of a headline if it is marked DONE.
  2666. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2667. When this is non-nil, the headline after the keyword is set to the
  2668. `org-headline-done' as an additional indication."
  2669. :group 'org-font-lock
  2670. :type 'boolean)
  2671. (defcustom org-fontify-emphasized-text t
  2672. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2673. Changing this variable requires a restart of Emacs to take effect."
  2674. :group 'org-font-lock
  2675. :type 'boolean)
  2676. (defvar org-emph-re nil
  2677. "Regular expression for matching emphasis.")
  2678. (defvar org-emphasis-regexp-components) ; defined just below
  2679. (defvar org-emphasis-alist) ; defined just below
  2680. (defun org-set-emph-re (var val)
  2681. "Set variable and compute the emphasis regular expression."
  2682. (set var val)
  2683. (when (and (boundp 'org-emphasis-alist)
  2684. (boundp 'org-emphasis-regexp-components)
  2685. org-emphasis-alist org-emphasis-regexp-components)
  2686. (let* ((e org-emphasis-regexp-components)
  2687. (pre (car e))
  2688. (post (nth 1 e))
  2689. (border (nth 2 e))
  2690. (body (nth 3 e))
  2691. (nl (nth 4 e))
  2692. (stacked (nth 5 e))
  2693. (body1 (concat body "*?"))
  2694. (markers (mapconcat 'car org-emphasis-alist "")))
  2695. ;; make sure special characters appear at the right position in the class
  2696. (if (string-match "\\^" markers)
  2697. (setq markers (concat (replace-match "" t t markers) "^")))
  2698. (if (string-match "-" markers)
  2699. (setq markers (concat (replace-match "" t t markers) "-")))
  2700. (if (> nl 0)
  2701. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2702. (int-to-string nl) "\\}")))
  2703. ;; Make the regexp
  2704. (setq org-emph-re
  2705. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2706. "\\("
  2707. "\\([" markers "]\\)"
  2708. "\\("
  2709. "[^" border (if (and nil stacked) markers) "]"
  2710. body1
  2711. "[^" border (if (and nil stacked) markers) "]"
  2712. "\\)"
  2713. "\\3\\)"
  2714. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2715. (defcustom org-emphasis-regexp-components
  2716. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2717. "Components used to build the reqular expression for emphasis.
  2718. This is a list with 6 entries. Terminology: In an emphasis string
  2719. like \" *strong word* \", we call the initial space PREMATCH, the final
  2720. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2721. and \"trong wor\" is the body. The different components in this variable
  2722. specify what is allowed/forbidden in each part:
  2723. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2724. post Chars allowed as postmatch. End of line will be allowed too.
  2725. border The chars *forbidden* as border characters.
  2726. body-regexp A regexp like \".\" to match a body character. Don't use
  2727. non-shy groups here, and don't allow newline here.
  2728. newline The maximum number of newlines allowed in an emphasis exp.
  2729. stacked Non-nil means, allow stacked styles. This works only in HTML
  2730. export. When this is set, all marker characters (as given in
  2731. `org-emphasis-alist') will be allowed as pre/post, aiding
  2732. inside-out matching.
  2733. Use customize to modify this, or restart Emacs after changing it."
  2734. :group 'org-font-lock
  2735. :set 'org-set-emph-re
  2736. :type '(list
  2737. (sexp :tag "Allowed chars in pre ")
  2738. (sexp :tag "Allowed chars in post ")
  2739. (sexp :tag "Forbidden chars in border ")
  2740. (sexp :tag "Regexp for body ")
  2741. (integer :tag "number of newlines allowed")
  2742. (boolean :tag "Stacking allowed ")))
  2743. (defcustom org-emphasis-alist
  2744. '(("*" bold "<b>" "</b>")
  2745. ("/" italic "<i>" "</i>")
  2746. ("_" underline "<u>" "</u>")
  2747. ("=" shadow "<code>" "</code>")
  2748. ("+" (:strike-through t) "<del>" "</del>")
  2749. )
  2750. "Special syntax for emphasized text.
  2751. Text starting and ending with a special character will be emphasized, for
  2752. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2753. characters, the face to be used by font-lock for highlighting in Org-mode
  2754. Emacs buffers, and the HTML tags to be used for this.
  2755. Use customize to modify this, or restart Emacs after changing it."
  2756. :group 'org-font-lock
  2757. :set 'org-set-emph-re
  2758. :type '(repeat
  2759. (list
  2760. (string :tag "Marker character")
  2761. (choice
  2762. (face :tag "Font-lock-face")
  2763. (plist :tag "Face property list"))
  2764. (string :tag "HTML start tag")
  2765. (string :tag "HTML end tag"))))
  2766. ;;; The faces
  2767. (defgroup org-faces nil
  2768. "Faces in Org-mode."
  2769. :tag "Org Faces"
  2770. :group 'org-font-lock)
  2771. ;; FIXME: convert that into a macro? Not critical, because this
  2772. ;; is only executed a few times at load time.
  2773. (defun org-compatible-face (specs)
  2774. "Make a compatible face specification.
  2775. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2776. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2777. to the top of the list. The `min-colors' attribute will be removed from
  2778. any other entries, and any resulting duplicates will be removed entirely."
  2779. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2780. (let (r e a)
  2781. (while (setq e (pop specs))
  2782. (cond
  2783. ((memq (car e) '(t default)) (push e r))
  2784. ((setq a (member '(min-colors 8) (car e)))
  2785. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2786. (cdr e)))))
  2787. ((setq a (assq 'min-colors (car e)))
  2788. (setq e (cons (delq a (car e)) (cdr e)))
  2789. (or (assoc (car e) r) (push e r)))
  2790. (t (or (assoc (car e) r) (push e r)))))
  2791. (nreverse r))
  2792. specs))
  2793. (defface org-hide
  2794. '((((background light)) (:foreground "white"))
  2795. (((background dark)) (:foreground "black")))
  2796. "Face used to hide leading stars in headlines.
  2797. The forground color of this face should be equal to the background
  2798. color of the frame."
  2799. :group 'org-faces)
  2800. (defface org-level-1 ;; font-lock-function-name-face
  2801. (org-compatible-face
  2802. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2803. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2804. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2805. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2806. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2807. (t (:bold t))))
  2808. "Face used for level 1 headlines."
  2809. :group 'org-faces)
  2810. (defface org-level-2 ;; font-lock-variable-name-face
  2811. (org-compatible-face
  2812. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2813. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2814. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2815. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2816. (t (:bold t))))
  2817. "Face used for level 2 headlines."
  2818. :group 'org-faces)
  2819. (defface org-level-3 ;; font-lock-keyword-face
  2820. (org-compatible-face
  2821. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2822. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2823. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2824. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2825. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2826. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2827. (t (:bold t))))
  2828. "Face used for level 3 headlines."
  2829. :group 'org-faces)
  2830. (defface org-level-4 ;; font-lock-comment-face
  2831. (org-compatible-face
  2832. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2833. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2834. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2835. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2836. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2837. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2838. (t (:bold t))))
  2839. "Face used for level 4 headlines."
  2840. :group 'org-faces)
  2841. (defface org-level-5 ;; font-lock-type-face
  2842. (org-compatible-face
  2843. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2844. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2845. (((class color) (min-colors 8)) (:foreground "green"))))
  2846. "Face used for level 5 headlines."
  2847. :group 'org-faces)
  2848. (defface org-level-6 ;; font-lock-constant-face
  2849. (org-compatible-face
  2850. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2851. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2852. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2853. "Face used for level 6 headlines."
  2854. :group 'org-faces)
  2855. (defface org-level-7 ;; font-lock-builtin-face
  2856. (org-compatible-face
  2857. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2858. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2859. (((class color) (min-colors 8)) (:foreground "blue"))))
  2860. "Face used for level 7 headlines."
  2861. :group 'org-faces)
  2862. (defface org-level-8 ;; font-lock-string-face
  2863. (org-compatible-face
  2864. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2865. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2866. (((class color) (min-colors 8)) (:foreground "green"))))
  2867. "Face used for level 8 headlines."
  2868. :group 'org-faces)
  2869. (defface org-special-keyword ;; font-lock-string-face
  2870. (org-compatible-face
  2871. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2872. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2873. (t (:italic t))))
  2874. "Face used for special keywords."
  2875. :group 'org-faces)
  2876. (defface org-drawer ;; font-lock-function-name-face
  2877. (org-compatible-face
  2878. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2879. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2880. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2881. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2882. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2883. (t (:bold t))))
  2884. "Face used for drawers."
  2885. :group 'org-faces)
  2886. (defface org-property-value nil
  2887. "Face used for the value of a property."
  2888. :group 'org-faces)
  2889. (defface org-column
  2890. (org-compatible-face
  2891. '((((class color) (min-colors 16) (background light))
  2892. (:background "grey90"))
  2893. (((class color) (min-colors 16) (background dark))
  2894. (:background "grey30"))
  2895. (((class color) (min-colors 8))
  2896. (:background "cyan" :foreground "black"))
  2897. (t (:inverse-video t))))
  2898. "Face for column display of entry properties."
  2899. :group 'org-faces)
  2900. (when (fboundp 'set-face-attribute)
  2901. ;; Make sure that a fixed-width face is used when we have a column table.
  2902. (set-face-attribute 'org-column nil
  2903. :height (face-attribute 'default :height)
  2904. :family (face-attribute 'default :family)))
  2905. (defface org-warning ;; font-lock-warning-face
  2906. (org-compatible-face
  2907. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2908. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2909. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2910. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2911. (t (:bold t))))
  2912. "Face for deadlines and TODO keywords."
  2913. :group 'org-faces)
  2914. (defface org-archived ; similar to shadow
  2915. (org-compatible-face
  2916. '((((class color grayscale) (min-colors 88) (background light))
  2917. (:foreground "grey50"))
  2918. (((class color grayscale) (min-colors 88) (background dark))
  2919. (:foreground "grey70"))
  2920. (((class color) (min-colors 8) (background light))
  2921. (:foreground "green"))
  2922. (((class color) (min-colors 8) (background dark))
  2923. (:foreground "yellow"))))
  2924. "Face for headline with the ARCHIVE tag."
  2925. :group 'org-faces)
  2926. (defface org-link
  2927. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2928. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2929. (t (:underline t)))
  2930. "Face for links."
  2931. :group 'org-faces)
  2932. (defface org-target
  2933. '((((class color) (background light)) (:underline t))
  2934. (((class color) (background dark)) (:underline t))
  2935. (t (:underline t)))
  2936. "Face for links."
  2937. :group 'org-faces)
  2938. (defface org-date
  2939. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2940. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2941. (t (:underline t)))
  2942. "Face for links."
  2943. :group 'org-faces)
  2944. (defface org-sexp-date
  2945. '((((class color) (background light)) (:foreground "Purple"))
  2946. (((class color) (background dark)) (:foreground "Cyan"))
  2947. (t (:underline t)))
  2948. "Face for links."
  2949. :group 'org-faces)
  2950. (defface org-tag
  2951. '((t (:bold t)))
  2952. "Face for tags."
  2953. :group 'org-faces)
  2954. (defface org-todo ;; font-lock-warning-face
  2955. (org-compatible-face
  2956. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2957. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2958. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2959. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2960. (t (:inverse-video t :bold t))))
  2961. "Face for TODO keywords."
  2962. :group 'org-faces)
  2963. (defface org-done ;; font-lock-type-face
  2964. (org-compatible-face
  2965. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2966. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2967. (((class color) (min-colors 8)) (:foreground "green"))
  2968. (t (:bold t))))
  2969. "Face used for todo keywords that indicate DONE items."
  2970. :group 'org-faces)
  2971. (defface org-headline-done ;; font-lock-string-face
  2972. (org-compatible-face
  2973. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2974. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2975. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2976. "Face used to indicate that a headline is DONE.
  2977. This face is only used if `org-fontify-done-headline' is set. If applies
  2978. to the part of the headline after the DONE keyword."
  2979. :group 'org-faces)
  2980. (defface org-table ;; font-lock-function-name-face
  2981. (org-compatible-face
  2982. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2983. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2984. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2985. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2986. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2987. (((class color) (min-colors 8) (background dark)))))
  2988. "Face used for tables."
  2989. :group 'org-faces)
  2990. (defface org-formula
  2991. (org-compatible-face
  2992. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2993. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2994. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2995. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2996. (t (:bold t :italic t))))
  2997. "Face for formulas."
  2998. :group 'org-faces)
  2999. (defface org-agenda-structure ;; font-lock-function-name-face
  3000. (org-compatible-face
  3001. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3002. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3003. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3004. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3005. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3006. (t (:bold t))))
  3007. "Face used in agenda for captions and dates."
  3008. :group 'org-faces)
  3009. (defface org-scheduled-today
  3010. (org-compatible-face
  3011. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3012. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3013. (((class color) (min-colors 8)) (:foreground "green"))
  3014. (t (:bold t :italic t))))
  3015. "Face for items scheduled for a certain day."
  3016. :group 'org-faces)
  3017. (defface org-scheduled-previously
  3018. (org-compatible-face
  3019. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3020. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3021. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3022. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3023. (t (:bold t))))
  3024. "Face for items scheduled previously, and not yet done."
  3025. :group 'org-faces)
  3026. (defface org-upcoming-deadline
  3027. (org-compatible-face
  3028. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3029. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3030. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3031. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3032. (t (:bold t))))
  3033. "Face for items scheduled previously, and not yet done."
  3034. :group 'org-faces)
  3035. (defcustom org-agenda-deadline-faces
  3036. '((1.0 . org-warning)
  3037. (0.5 . org-upcoming-deadline)
  3038. (0.0 . default))
  3039. "Faces for showing deadlines in the agenda.
  3040. This is a list of cons cells. The cdr of each cess is a face to be used,
  3041. and it can also just be a like like '(:foreground \"yellow\").
  3042. Each car is a fraction of the head-warning time that must have passed for
  3043. this the face in the cdr to be used for display. The numbers must be
  3044. given in descending order. The head-warning time is normally taken
  3045. from `org-deadline-warning-days', but can also be specified in the deadline
  3046. timestamp itself, like this:
  3047. DEADLINE: <2007-08-13 Mon -8d>
  3048. You may use d for days, w for weeks, m for months and y for years. Months
  3049. and years will only be treated in an approximate fashion (30.4 days for a
  3050. month and 365.24 days for a year)."
  3051. :group 'org-faces
  3052. :group 'org-agenda-daily/weekly
  3053. :type '(repeat
  3054. (cons
  3055. (number :tag "Fraction of head-warning time passed")
  3056. (sexp :tag "Face"))))
  3057. (defface org-time-grid ;; font-lock-variable-name-face
  3058. (org-compatible-face
  3059. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3060. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3061. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3062. "Face used for time grids."
  3063. :group 'org-faces)
  3064. (defconst org-level-faces
  3065. '(org-level-1 org-level-2 org-level-3 org-level-4
  3066. org-level-5 org-level-6 org-level-7 org-level-8
  3067. ))
  3068. (defcustom org-n-level-faces (length org-level-faces)
  3069. "The number different faces to be used for headlines.
  3070. Org-mode defines 8 different headline faces, so this can be at most 8.
  3071. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3072. :type 'number
  3073. :group 'org-faces)
  3074. ;;; Variables for pre-computed regular expressions, all buffer local
  3075. (defvar org-drawer-regexp nil
  3076. "Matches first line of a hidden block.")
  3077. (make-variable-buffer-local 'org-drawer-regexp)
  3078. (defvar org-todo-regexp nil
  3079. "Matches any of the TODO state keywords.")
  3080. (make-variable-buffer-local 'org-todo-regexp)
  3081. (defvar org-not-done-regexp nil
  3082. "Matches any of the TODO state keywords except the last one.")
  3083. (make-variable-buffer-local 'org-not-done-regexp)
  3084. (defvar org-todo-line-regexp nil
  3085. "Matches a headline and puts TODO state into group 2 if present.")
  3086. (make-variable-buffer-local 'org-todo-line-regexp)
  3087. (defvar org-todo-line-tags-regexp nil
  3088. "Matches a headline and puts TODO state into group 2 if present.
  3089. Also put tags into group 4 if tags are present.")
  3090. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3091. (defvar org-nl-done-regexp nil
  3092. "Matches newline followed by a headline with the DONE keyword.")
  3093. (make-variable-buffer-local 'org-nl-done-regexp)
  3094. (defvar org-looking-at-done-regexp nil
  3095. "Matches the DONE keyword a point.")
  3096. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3097. (defvar org-ds-keyword-length 12
  3098. "Maximum length of the Deadline and SCHEDULED keywords.")
  3099. (make-variable-buffer-local 'org-ds-keyword-length)
  3100. (defvar org-deadline-regexp nil
  3101. "Matches the DEADLINE keyword.")
  3102. (make-variable-buffer-local 'org-deadline-regexp)
  3103. (defvar org-deadline-time-regexp nil
  3104. "Matches the DEADLINE keyword together with a time stamp.")
  3105. (make-variable-buffer-local 'org-deadline-time-regexp)
  3106. (defvar org-deadline-line-regexp nil
  3107. "Matches the DEADLINE keyword and the rest of the line.")
  3108. (make-variable-buffer-local 'org-deadline-line-regexp)
  3109. (defvar org-scheduled-regexp nil
  3110. "Matches the SCHEDULED keyword.")
  3111. (make-variable-buffer-local 'org-scheduled-regexp)
  3112. (defvar org-scheduled-time-regexp nil
  3113. "Matches the SCHEDULED keyword together with a time stamp.")
  3114. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3115. (defvar org-closed-time-regexp nil
  3116. "Matches the CLOSED keyword together with a time stamp.")
  3117. (make-variable-buffer-local 'org-closed-time-regexp)
  3118. (defvar org-keyword-time-regexp nil
  3119. "Matches any of the 4 keywords, together with the time stamp.")
  3120. (make-variable-buffer-local 'org-keyword-time-regexp)
  3121. (defvar org-keyword-time-not-clock-regexp nil
  3122. "Matches any of the 3 keywords, together with the time stamp.")
  3123. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3124. (defvar org-maybe-keyword-time-regexp nil
  3125. "Matches a timestamp, possibly preceeded by a keyword.")
  3126. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3127. (defvar org-planning-or-clock-line-re nil
  3128. "Matches a line with planning or clock info.")
  3129. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3130. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3131. rear-nonsticky t mouse-map t fontified t)
  3132. "Properties to remove when a string without properties is wanted.")
  3133. (defsubst org-match-string-no-properties (num &optional string)
  3134. (if (featurep 'xemacs)
  3135. (let ((s (match-string num string)))
  3136. (remove-text-properties 0 (length s) org-rm-props s)
  3137. s)
  3138. (match-string-no-properties num string)))
  3139. (defsubst org-no-properties (s)
  3140. (if (fboundp 'set-text-properties)
  3141. (set-text-properties 0 (length s) nil s)
  3142. (remove-text-properties 0 (length s) org-rm-props s))
  3143. s)
  3144. (defsubst org-get-alist-option (option key)
  3145. (cond ((eq key t) t)
  3146. ((eq option t) t)
  3147. ((assoc key option) (cdr (assoc key option)))
  3148. (t (cdr (assq 'default option)))))
  3149. (defsubst org-inhibit-invisibility ()
  3150. "Modified `buffer-invisibility-spec' for Emacs 21.
  3151. Some ops with invisible text do not work correctly on Emacs 21. For these
  3152. we turn off invisibility temporarily. Use this in a `let' form."
  3153. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3154. (defsubst org-set-local (var value)
  3155. "Make VAR local in current buffer and set it to VALUE."
  3156. (set (make-variable-buffer-local var) value))
  3157. (defsubst org-mode-p ()
  3158. "Check if the current buffer is in Org-mode."
  3159. (eq major-mode 'org-mode))
  3160. (defsubst org-last (list)
  3161. "Return the last element of LIST."
  3162. (car (last list)))
  3163. (defun org-let (list &rest body)
  3164. (eval (cons 'let (cons list body))))
  3165. (put 'org-let 'lisp-indent-function 1)
  3166. (defun org-let2 (list1 list2 &rest body)
  3167. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3168. (put 'org-let2 'lisp-indent-function 2)
  3169. (defconst org-startup-options
  3170. '(("fold" org-startup-folded t)
  3171. ("overview" org-startup-folded t)
  3172. ("nofold" org-startup-folded nil)
  3173. ("showall" org-startup-folded nil)
  3174. ("content" org-startup-folded content)
  3175. ("hidestars" org-hide-leading-stars t)
  3176. ("showstars" org-hide-leading-stars nil)
  3177. ("odd" org-odd-levels-only t)
  3178. ("oddeven" org-odd-levels-only nil)
  3179. ("align" org-startup-align-all-tables t)
  3180. ("noalign" org-startup-align-all-tables nil)
  3181. ("customtime" org-display-custom-times t)
  3182. ("logging" org-log-done t)
  3183. ("logdone" org-log-done t)
  3184. ("nologging" org-log-done nil)
  3185. ("lognotedone" org-log-done done push)
  3186. ("lognotestate" org-log-done state push)
  3187. ("lognoteclock-out" org-log-done clock-out push)
  3188. ("logrepeat" org-log-repeat t)
  3189. ("nologrepeat" org-log-repeat nil)
  3190. ("constcgs" constants-unit-system cgs)
  3191. ("constSI" constants-unit-system SI))
  3192. "Variable associated with STARTUP options for org-mode.
  3193. Each element is a list of three items: The startup options as written
  3194. in the #+STARTUP line, the corresponding variable, and the value to
  3195. set this variable to if the option is found. An optional forth element PUSH
  3196. means to push this value onto the list in the variable.")
  3197. (defun org-set-regexps-and-options ()
  3198. "Precompute regular expressions for current buffer."
  3199. (when (org-mode-p)
  3200. (org-set-local 'org-todo-kwd-alist nil)
  3201. (org-set-local 'org-todo-keywords-1 nil)
  3202. (org-set-local 'org-done-keywords nil)
  3203. (org-set-local 'org-todo-heads nil)
  3204. (org-set-local 'org-todo-sets nil)
  3205. (let ((re (org-make-options-regexp
  3206. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO" "COLUMNS"
  3207. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3208. "CONSTANTS" "PROPERTY")))
  3209. (splitre "[ \t]+")
  3210. kwds key value cat arch tags const links hw dws tail sep kws1 prio
  3211. props)
  3212. (save-excursion
  3213. (save-restriction
  3214. (widen)
  3215. (goto-char (point-min))
  3216. (while (re-search-forward re nil t)
  3217. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3218. (cond
  3219. ((equal key "CATEGORY")
  3220. (if (string-match "[ \t]+$" value)
  3221. (setq value (replace-match "" t t value)))
  3222. (setq cat (intern value)))
  3223. ((equal key "SEQ_TODO")
  3224. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3225. ((equal key "TYP_TODO")
  3226. (push (cons 'type (org-split-string value splitre)) kwds))
  3227. ((equal key "TAGS")
  3228. (setq tags (append tags (org-split-string value splitre))))
  3229. ((equal key "COLUMNS")
  3230. (org-set-local 'org-columns-default-format value))
  3231. ((equal key "LINK")
  3232. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3233. (push (cons (match-string 1 value)
  3234. (org-trim (match-string 2 value)))
  3235. links)))
  3236. ((equal key "PRIORITIES")
  3237. (setq prio (org-split-string value " +")))
  3238. ((equal key "PROPERTY")
  3239. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3240. (push (cons (match-string 1 value) (match-string 2 value))
  3241. props)))
  3242. ((equal key "CONSTANTS")
  3243. (setq const (append const (org-split-string value splitre))))
  3244. ((equal key "STARTUP")
  3245. (let ((opts (org-split-string value splitre))
  3246. l var val)
  3247. (while (setq l (pop opts))
  3248. (when (setq l (assoc l org-startup-options))
  3249. (setq var (nth 1 l) val (nth 2 l))
  3250. (if (not (nth 3 l))
  3251. (set (make-local-variable var) val)
  3252. (if (not (listp (symbol-value var)))
  3253. (set (make-local-variable var) nil))
  3254. (set (make-local-variable var) (symbol-value var))
  3255. (add-to-list var val))))))
  3256. ((equal key "ARCHIVE")
  3257. (string-match " *$" value)
  3258. (setq arch (replace-match "" t t value))
  3259. (remove-text-properties 0 (length arch)
  3260. '(face t fontified t) arch)))
  3261. )))
  3262. (and cat (org-set-local 'org-category cat))
  3263. (when prio
  3264. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3265. (setq prio (mapcar 'string-to-char prio))
  3266. (org-set-local 'org-highest-priority (nth 0 prio))
  3267. (org-set-local 'org-lowest-priority (nth 1 prio))
  3268. (org-set-local 'org-default-priority (nth 2 prio)))
  3269. (and props (org-set-local 'org-local-properties (nreverse props)))
  3270. (and arch (org-set-local 'org-archive-location arch))
  3271. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3272. ;; Process the TODO keywords
  3273. (unless kwds
  3274. ;; Use the global values as if they had been given locally.
  3275. (setq kwds (default-value 'org-todo-keywords))
  3276. (if (stringp (car kwds))
  3277. (setq kwds (list (cons org-todo-interpretation
  3278. (default-value 'org-todo-keywords)))))
  3279. (setq kwds (reverse kwds)))
  3280. (setq kwds (nreverse kwds))
  3281. (let (inter kws)
  3282. (while (setq kws (pop kwds))
  3283. (setq inter (pop kws) sep (member "|" kws)
  3284. kws1 (delete "|" (copy-sequence kws))
  3285. hw (car kws1)
  3286. dws (if sep (cdr sep) (last kws1))
  3287. tail (list inter hw (car dws) (org-last dws)))
  3288. (add-to-list 'org-todo-heads hw 'append)
  3289. (push kws1 org-todo-sets)
  3290. (setq org-done-keywords (append org-done-keywords dws nil))
  3291. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3292. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3293. (setq org-todo-sets (nreverse org-todo-sets)
  3294. org-todo-kwd-alist (nreverse org-todo-kwd-alist)))
  3295. ;; Process the constants
  3296. (when const
  3297. (let (e cst)
  3298. (while (setq e (pop const))
  3299. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3300. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3301. (setq org-table-formula-constants-local cst)))
  3302. ;; Process the tags.
  3303. (when tags
  3304. (let (e tgs)
  3305. (while (setq e (pop tags))
  3306. (cond
  3307. ((equal e "{") (push '(:startgroup) tgs))
  3308. ((equal e "}") (push '(:endgroup) tgs))
  3309. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3310. (push (cons (match-string 1 e)
  3311. (string-to-char (match-string 2 e)))
  3312. tgs))
  3313. (t (push (list e) tgs))))
  3314. (org-set-local 'org-tag-alist nil)
  3315. (while (setq e (pop tgs))
  3316. (or (and (stringp (car e))
  3317. (assoc (car e) org-tag-alist))
  3318. (push e org-tag-alist))))))
  3319. ;; Compute the regular expressions and other local variables
  3320. (if (not org-done-keywords)
  3321. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3322. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3323. (length org-scheduled-string)))
  3324. org-drawer-regexp
  3325. (concat "^[ \t]*:\\("
  3326. (mapconcat 'regexp-quote org-drawers "\\|")
  3327. "\\):[ \t]*$")
  3328. org-not-done-keywords
  3329. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3330. org-todo-regexp
  3331. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3332. "\\|") "\\)\\>")
  3333. org-not-done-regexp
  3334. (concat "\\<\\("
  3335. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3336. "\\)\\>")
  3337. org-todo-line-regexp
  3338. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3339. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3340. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3341. org-nl-done-regexp
  3342. (concat "\n\\*+[ \t]+"
  3343. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3344. "\\)" "\\>")
  3345. org-todo-line-tags-regexp
  3346. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3347. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3348. (org-re
  3349. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3350. org-looking-at-done-regexp
  3351. (concat "^" "\\(?:"
  3352. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3353. "\\>")
  3354. org-deadline-regexp (concat "\\<" org-deadline-string)
  3355. org-deadline-time-regexp
  3356. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3357. org-deadline-line-regexp
  3358. (concat "\\<\\(" org-deadline-string "\\).*")
  3359. org-scheduled-regexp
  3360. (concat "\\<" org-scheduled-string)
  3361. org-scheduled-time-regexp
  3362. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3363. org-closed-time-regexp
  3364. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3365. org-keyword-time-regexp
  3366. (concat "\\<\\(" org-scheduled-string
  3367. "\\|" org-deadline-string
  3368. "\\|" org-closed-string
  3369. "\\|" org-archived-string
  3370. "\\|" org-clock-string "\\)"
  3371. " *[[<]\\([^]>]+\\)[]>]")
  3372. org-keyword-time-not-clock-regexp
  3373. (concat "\\<\\(" org-scheduled-string
  3374. "\\|" org-deadline-string
  3375. "\\|" org-closed-string
  3376. "\\|" org-archived-string
  3377. "\\)"
  3378. " *[[<]\\([^]>]+\\)[]>]")
  3379. org-maybe-keyword-time-regexp
  3380. (concat "\\(\\<\\(" org-scheduled-string
  3381. "\\|" org-deadline-string
  3382. "\\|" org-closed-string
  3383. "\\|" org-archived-string
  3384. "\\|" org-clock-string "\\)\\)?"
  3385. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3386. org-planning-or-clock-line-re
  3387. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3388. "\\|" org-deadline-string
  3389. "\\|" org-closed-string "\\|" org-clock-string
  3390. "\\|" org-archived-string "\\)\\>\\)")
  3391. )
  3392. (org-set-font-lock-defaults)))
  3393. ;;; Some variables ujsed in various places
  3394. (defvar org-window-configuration nil
  3395. "Used in various places to store a window configuration.")
  3396. (defvar org-finish-function nil
  3397. "Function to be called when `C-c C-c' is used.
  3398. This is for getting out of special buffers like remember.")
  3399. ;;; Foreign variables, to inform the compiler
  3400. ;; XEmacs only
  3401. (defvar outline-mode-menu-heading)
  3402. (defvar outline-mode-menu-show)
  3403. (defvar outline-mode-menu-hide)
  3404. (defvar zmacs-regions) ; XEmacs regions
  3405. ;; Emacs only
  3406. (defvar mark-active)
  3407. ;; Packages that org-mode interacts with
  3408. (defvar calc-embedded-close-formula)
  3409. (defvar calc-embedded-open-formula)
  3410. (defvar font-lock-unfontify-region-function)
  3411. (defvar org-goto-start-pos)
  3412. (defvar vm-message-pointer)
  3413. (defvar vm-folder-directory)
  3414. (defvar wl-summary-buffer-elmo-folder)
  3415. (defvar wl-summary-buffer-folder-name)
  3416. (defvar gnus-other-frame-object)
  3417. (defvar gnus-group-name)
  3418. (defvar gnus-article-current)
  3419. (defvar w3m-current-url)
  3420. (defvar w3m-current-title)
  3421. (defvar mh-progs)
  3422. (defvar mh-current-folder)
  3423. (defvar mh-show-folder-buffer)
  3424. (defvar mh-index-folder)
  3425. (defvar mh-searcher)
  3426. (defvar calendar-mode-map)
  3427. (defvar Info-current-file)
  3428. (defvar Info-current-node)
  3429. (defvar texmathp-why)
  3430. (defvar remember-save-after-remembering)
  3431. (defvar remember-data-file)
  3432. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3433. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3434. (defvar org-latex-regexps)
  3435. (defvar constants-unit-system)
  3436. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3437. ;; FIXME: Occasionally check by commenting these, to make sure
  3438. ;; no other functions uses these, forgetting to let-bind them.
  3439. (defvar entry)
  3440. (defvar state)
  3441. (defvar last-state)
  3442. (defvar date)
  3443. (defvar description)
  3444. ;; Defined somewhere in this file, but used before definition.
  3445. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3446. (defvar org-agenda-undo-list)
  3447. (defvar org-agenda-pending-undo-list)
  3448. (defvar org-agenda-overriding-header)
  3449. (defvar orgtbl-mode)
  3450. (defvar org-html-entities)
  3451. (defvar org-struct-menu)
  3452. (defvar org-org-menu)
  3453. (defvar org-tbl-menu)
  3454. (defvar org-agenda-keymap)
  3455. (defvar org-category-table)
  3456. ;;;; Emacs/XEmacs compatibility
  3457. ;; Overlay compatibility functions
  3458. (defun org-make-overlay (beg end &optional buffer)
  3459. (if (featurep 'xemacs)
  3460. (make-extent beg end buffer)
  3461. (make-overlay beg end buffer)))
  3462. (defun org-delete-overlay (ovl)
  3463. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3464. (defun org-detach-overlay (ovl)
  3465. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3466. (defun org-move-overlay (ovl beg end &optional buffer)
  3467. (if (featurep 'xemacs)
  3468. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3469. (move-overlay ovl beg end buffer)))
  3470. (defun org-overlay-put (ovl prop value)
  3471. (if (featurep 'xemacs)
  3472. (set-extent-property ovl prop value)
  3473. (overlay-put ovl prop value)))
  3474. (defun org-overlay-display (ovl text &optional face evap)
  3475. "Make overlay OVL display TEXT with face FACE."
  3476. (if (featurep 'xemacs)
  3477. (let ((gl (make-glyph text)))
  3478. (and face (set-glyph-face gl face))
  3479. (set-extent-property ovl 'invisible t)
  3480. (set-extent-property ovl 'end-glyph gl))
  3481. (overlay-put ovl 'display text)
  3482. (if face (overlay-put ovl 'face face))
  3483. (if evap (overlay-put ovl 'evaporate t))))
  3484. (defun org-overlay-before-string (ovl text &optional face evap)
  3485. "Make overlay OVL display TEXT with face FACE."
  3486. (if (featurep 'xemacs)
  3487. (let ((gl (make-glyph text)))
  3488. (and face (set-glyph-face gl face))
  3489. (set-extent-property ovl 'begin-glyph gl))
  3490. (if face (org-add-props text nil 'face face))
  3491. (overlay-put ovl 'before-string text)
  3492. (if evap (overlay-put ovl 'evaporate t))))
  3493. (defun org-overlay-get (ovl prop)
  3494. (if (featurep 'xemacs)
  3495. (extent-property ovl prop)
  3496. (overlay-get ovl prop)))
  3497. (defun org-overlays-at (pos)
  3498. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3499. ;; FIXME: this is currently not used
  3500. (defun org-overlays-in (&optional start end)
  3501. (if (featurep 'xemacs)
  3502. (extent-list nil start end)
  3503. (overlays-in start end)))
  3504. (defun org-overlay-start (o)
  3505. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3506. (defun org-overlay-end (o)
  3507. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3508. ;; FIXME: this is currently not used
  3509. (defun org-find-overlays (prop &optional pos delete)
  3510. "Find all overlays specifying PROP at POS or point.
  3511. If DELETE is non-nil, delete all those overlays."
  3512. (let ((overlays (org-overlays-at (or pos (point))))
  3513. ov found)
  3514. (while (setq ov (pop overlays))
  3515. (if (org-overlay-get ov prop)
  3516. (if delete (org-delete-overlay ov) (push ov found))))
  3517. found))
  3518. ;; Region compatibility
  3519. (defun org-add-hook (hook function &optional append local)
  3520. "Add-hook, compatible with both Emacsen."
  3521. (if (and local (featurep 'xemacs))
  3522. (add-local-hook hook function append)
  3523. (add-hook hook function append local)))
  3524. (defvar org-ignore-region nil
  3525. "To temporarily disable the active region.")
  3526. (defun org-region-active-p ()
  3527. "Is `transient-mark-mode' on and the region active?
  3528. Works on both Emacs and XEmacs."
  3529. (if org-ignore-region
  3530. nil
  3531. (if (featurep 'xemacs)
  3532. (and zmacs-regions (region-active-p))
  3533. (and transient-mark-mode mark-active))))
  3534. ;; Invisibility compatibility
  3535. (defun org-add-to-invisibility-spec (arg)
  3536. "Add elements to `buffer-invisibility-spec'.
  3537. See documentation for `buffer-invisibility-spec' for the kind of elements
  3538. that can be added."
  3539. (cond
  3540. ((fboundp 'add-to-invisibility-spec)
  3541. (add-to-invisibility-spec arg))
  3542. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3543. (setq buffer-invisibility-spec (list arg)))
  3544. (t
  3545. (setq buffer-invisibility-spec
  3546. (cons arg buffer-invisibility-spec)))))
  3547. (defun org-remove-from-invisibility-spec (arg)
  3548. "Remove elements from `buffer-invisibility-spec'."
  3549. (if (fboundp 'remove-from-invisibility-spec)
  3550. (remove-from-invisibility-spec arg)
  3551. (if (consp buffer-invisibility-spec)
  3552. (setq buffer-invisibility-spec
  3553. (delete arg buffer-invisibility-spec)))))
  3554. ;; FIXME: this is currently not used
  3555. (defun org-in-invisibility-spec-p (arg)
  3556. "Is ARG a member of `buffer-invisibility-spec'?"
  3557. (if (consp buffer-invisibility-spec)
  3558. (member arg buffer-invisibility-spec)
  3559. nil))
  3560. ;;;; Define the Org-mode
  3561. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3562. (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."))
  3563. ;; We use a before-change function to check if a table might need
  3564. ;; an update.
  3565. (defvar org-table-may-need-update t
  3566. "Indicates that a table might need an update.
  3567. This variable is set by `org-before-change-function'.
  3568. `org-table-align' sets it back to nil.")
  3569. (defvar org-mode-map)
  3570. (defvar org-mode-hook nil)
  3571. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3572. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3573. (defvar org-table-buffer-is-an nil)
  3574. ;;;###autoload
  3575. (define-derived-mode org-mode outline-mode "Org"
  3576. "Outline-based notes management and organizer, alias
  3577. \"Carsten's outline-mode for keeping track of everything.\"
  3578. Org-mode develops organizational tasks around a NOTES file which
  3579. contains information about projects as plain text. Org-mode is
  3580. implemented on top of outline-mode, which is ideal to keep the content
  3581. of large files well structured. It supports ToDo items, deadlines and
  3582. time stamps, which magically appear in the diary listing of the Emacs
  3583. calendar. Tables are easily created with a built-in table editor.
  3584. Plain text URL-like links connect to websites, emails (VM), Usenet
  3585. messages (Gnus), BBDB entries, and any files related to the project.
  3586. For printing and sharing of notes, an Org-mode file (or a part of it)
  3587. can be exported as a structured ASCII or HTML file.
  3588. The following commands are available:
  3589. \\{org-mode-map}"
  3590. ;; Get rid of Outline menus, they are not needed
  3591. ;; Need to do this here because define-derived-mode sets up
  3592. ;; the keymap so late. Still, it is a waste to call this each time
  3593. ;; we switch another buffer into org-mode.
  3594. (if (featurep 'xemacs)
  3595. (when (boundp 'outline-mode-menu-heading)
  3596. ;; Assume this is Greg's port, it used easymenu
  3597. (easy-menu-remove outline-mode-menu-heading)
  3598. (easy-menu-remove outline-mode-menu-show)
  3599. (easy-menu-remove outline-mode-menu-hide))
  3600. (define-key org-mode-map [menu-bar headings] 'undefined)
  3601. (define-key org-mode-map [menu-bar hide] 'undefined)
  3602. (define-key org-mode-map [menu-bar show] 'undefined))
  3603. (easy-menu-add org-org-menu)
  3604. (easy-menu-add org-tbl-menu)
  3605. (org-install-agenda-files-menu)
  3606. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3607. (org-add-to-invisibility-spec '(org-cwidth))
  3608. (when (featurep 'xemacs)
  3609. (org-set-local 'line-move-ignore-invisible t))
  3610. (org-set-local 'outline-regexp "\\*+ ")
  3611. (setq outline-level 'org-outline-level)
  3612. (when (and org-ellipsis (stringp org-ellipsis)
  3613. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table))
  3614. (unless org-display-table
  3615. (setq org-display-table (make-display-table)))
  3616. (set-display-table-slot org-display-table
  3617. 4 (string-to-vector org-ellipsis))
  3618. (setq buffer-display-table org-display-table))
  3619. (org-set-regexps-and-options)
  3620. ;; Calc embedded
  3621. (org-set-local 'calc-embedded-open-mode "# ")
  3622. (modify-syntax-entry ?# "<")
  3623. (modify-syntax-entry ?@ "w")
  3624. (if org-startup-truncated (setq truncate-lines t))
  3625. (org-set-local 'font-lock-unfontify-region-function
  3626. 'org-unfontify-region)
  3627. ;; Activate before-change-function
  3628. (org-set-local 'org-table-may-need-update t)
  3629. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3630. 'local)
  3631. ;; Check for running clock before killing a buffer
  3632. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3633. ;; Paragraphs and auto-filling
  3634. (org-set-autofill-regexps)
  3635. (setq indent-line-function 'org-indent-line-function)
  3636. (org-update-radio-target-regexp)
  3637. ;; Comment characters
  3638. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3639. (org-set-local 'comment-padding " ")
  3640. ;; Make isearch reveal context
  3641. (if (or (featurep 'xemacs)
  3642. (not (boundp 'outline-isearch-open-invisible-function)))
  3643. ;; Emacs 21 and XEmacs make use of the hook
  3644. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3645. ;; Emacs 22 deals with this through a special variable
  3646. (org-set-local 'outline-isearch-open-invisible-function
  3647. (lambda (&rest ignore) (org-show-context 'isearch))))
  3648. ;; If empty file that did not turn on org-mode automatically, make it to.
  3649. (if (and org-insert-mode-line-in-empty-file
  3650. (interactive-p)
  3651. (= (point-min) (point-max)))
  3652. (insert "# -*- mode: org -*-\n\n"))
  3653. (unless org-inhibit-startup
  3654. (when org-startup-align-all-tables
  3655. (let ((bmp (buffer-modified-p)))
  3656. (org-table-map-tables 'org-table-align)
  3657. (set-buffer-modified-p bmp)))
  3658. (cond
  3659. ((eq org-startup-folded t)
  3660. (org-cycle '(4)))
  3661. ((eq org-startup-folded 'content)
  3662. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3663. (org-cycle '(4)) (org-cycle '(4)))))))
  3664. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3665. (defsubst org-call-with-arg (command arg)
  3666. "Call COMMAND interactively, but pretend prefix are was ARG."
  3667. (let ((current-prefix-arg arg)) (call-interactively command)))
  3668. (defsubst org-current-line (&optional pos)
  3669. (save-excursion
  3670. (and pos (goto-char pos))
  3671. ;; works also in narrowed buffer, because we start at 1, not point-min
  3672. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3673. (defun org-current-time ()
  3674. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3675. (if (> org-time-stamp-rounding-minutes 0)
  3676. (let ((r org-time-stamp-rounding-minutes)
  3677. (time (decode-time)))
  3678. (apply 'encode-time
  3679. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3680. (nthcdr 2 time))))
  3681. (current-time)))
  3682. (defun org-add-props (string plist &rest props)
  3683. "Add text properties to entire string, from beginning to end.
  3684. PLIST may be a list of properties, PROPS are individual properties and values
  3685. that will be added to PLIST. Returns the string that was modified."
  3686. (add-text-properties
  3687. 0 (length string) (if props (append plist props) plist) string)
  3688. string)
  3689. (put 'org-add-props 'lisp-indent-function 2)
  3690. ;;;; Font-Lock stuff, including the activators
  3691. (defvar org-mouse-map (make-sparse-keymap))
  3692. (org-defkey org-mouse-map
  3693. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3694. (org-defkey org-mouse-map
  3695. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3696. (when org-mouse-1-follows-link
  3697. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3698. (when org-tab-follows-link
  3699. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3700. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3701. (when org-return-follows-link
  3702. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3703. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3704. (require 'font-lock)
  3705. (defconst org-non-link-chars "]\t\n\r<>")
  3706. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3707. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3708. (defvar org-link-re-with-space nil
  3709. "Matches a link with spaces, optional angular brackets around it.")
  3710. (defvar org-link-re-with-space2 nil
  3711. "Matches a link with spaces, optional angular brackets around it.")
  3712. (defvar org-angle-link-re nil
  3713. "Matches link with angular brackets, spaces are allowed.")
  3714. (defvar org-plain-link-re nil
  3715. "Matches plain link, without spaces.")
  3716. (defvar org-bracket-link-regexp nil
  3717. "Matches a link in double brackets.")
  3718. (defvar org-bracket-link-analytic-regexp nil
  3719. "Regular expression used to analyze links.
  3720. Here is what the match groups contain after a match:
  3721. 1: http:
  3722. 2: http
  3723. 3: path
  3724. 4: [desc]
  3725. 5: desc")
  3726. (defvar org-any-link-re nil
  3727. "Regular expression matching any link.")
  3728. (defun org-make-link-regexps ()
  3729. "Update the link regular expressions.
  3730. This should be called after the variable `org-link-types' has changed."
  3731. (setq org-link-re-with-space
  3732. (concat
  3733. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3734. "\\([^" org-non-link-chars " ]"
  3735. "[^" org-non-link-chars "]*"
  3736. "[^" org-non-link-chars " ]\\)>?")
  3737. org-link-re-with-space2
  3738. (concat
  3739. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3740. "\\([^" org-non-link-chars " ]"
  3741. "[^]\t\n\r]*"
  3742. "[^" org-non-link-chars " ]\\)>?")
  3743. org-angle-link-re
  3744. (concat
  3745. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3746. "\\([^" org-non-link-chars " ]"
  3747. "[^" org-non-link-chars "]*"
  3748. "\\)>")
  3749. org-plain-link-re
  3750. (concat
  3751. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3752. "\\([^]\t\n\r<>,;() ]+\\)")
  3753. org-bracket-link-regexp
  3754. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3755. org-bracket-link-analytic-regexp
  3756. (concat
  3757. "\\[\\["
  3758. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3759. "\\([^]]+\\)"
  3760. "\\]"
  3761. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3762. "\\]")
  3763. org-any-link-re
  3764. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3765. org-angle-link-re "\\)\\|\\("
  3766. org-plain-link-re "\\)")))
  3767. (org-make-link-regexps)
  3768. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3769. "Regular expression for fast time stamp matching.")
  3770. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3771. "Regular expression for fast time stamp matching.")
  3772. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3773. "Regular expression matching time strings for analysis.
  3774. This one does not require the space after the date.")
  3775. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3776. "Regular expression matching time strings for analysis.")
  3777. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,11\\}>")
  3778. "Regular expression matching time stamps, with groups.")
  3779. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,11\\}[]>]")
  3780. "Regular expression matching time stamps (also [..]), with groups.")
  3781. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3782. "Regular expression matching a time stamp range.")
  3783. (defconst org-tr-regexp-both
  3784. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3785. "Regular expression matching a time stamp range.")
  3786. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3787. org-ts-regexp "\\)?")
  3788. "Regular expression matching a time stamp or time stamp range.")
  3789. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3790. org-ts-regexp-both "\\)?")
  3791. "Regular expression matching a time stamp or time stamp range.
  3792. The time stamps may be either active or inactive.")
  3793. (defvar org-emph-face nil)
  3794. (defun org-do-emphasis-faces (limit)
  3795. "Run through the buffer and add overlays to links."
  3796. (let (rtn)
  3797. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3798. (if (not (= (char-after (match-beginning 3))
  3799. (char-after (match-beginning 4))))
  3800. (progn
  3801. (setq rtn t)
  3802. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3803. 'face
  3804. (nth 1 (assoc (match-string 3)
  3805. org-emphasis-alist)))
  3806. (add-text-properties (match-beginning 2) (match-end 2)
  3807. '(font-lock-multiline t))
  3808. (backward-char 1))))
  3809. rtn))
  3810. (defun org-emphasize (&optional char)
  3811. "Insert or change an emphasis, i.e. a font like bold or italic.
  3812. If there is an active region, change that region to a new emphasis.
  3813. If there is no region, just insert the marker characters and position
  3814. the cursor between them.
  3815. CHAR should be either the marker character, or the first character of the
  3816. HTML tag associated with that emphasis. If CHAR is a space, the means
  3817. to remove the emphasis of the selected region.
  3818. If char is not given (for example in an interactive call) it
  3819. will be prompted for."
  3820. (interactive)
  3821. (let ((eal org-emphasis-alist) e det
  3822. (erc org-emphasis-regexp-components)
  3823. (prompt "")
  3824. (string "") beg end move tag c s)
  3825. (if (org-region-active-p)
  3826. (setq beg (region-beginning) end (region-end)
  3827. string (buffer-substring beg end))
  3828. (setq move t))
  3829. (while (setq e (pop eal))
  3830. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3831. c (aref tag 0))
  3832. (push (cons c (string-to-char (car e))) det)
  3833. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3834. (substring tag 1)))))
  3835. (unless char
  3836. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3837. (setq char (read-char-exclusive)))
  3838. (setq char (or (cdr (assoc char det)) char))
  3839. (if (equal char ?\ )
  3840. (setq s "" move nil)
  3841. (unless (assoc (char-to-string char) org-emphasis-alist)
  3842. (error "No such emphasis marker: \"%c\"" char))
  3843. (setq s (char-to-string char)))
  3844. (while (and (> (length string) 1)
  3845. (equal (substring string 0 1) (substring string -1))
  3846. (assoc (substring string 0 1) org-emphasis-alist))
  3847. (setq string (substring string 1 -1)))
  3848. (setq string (concat s string s))
  3849. (if beg (delete-region beg end))
  3850. (unless (or (bolp)
  3851. (string-match (concat "[" (nth 0 erc) "\n]")
  3852. (char-to-string (char-before (point)))))
  3853. (insert " "))
  3854. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3855. (char-to-string (char-after (point))))
  3856. (insert " ") (backward-char 1))
  3857. (insert string)
  3858. (and move (backward-char 1))))
  3859. (defun org-activate-plain-links (limit)
  3860. "Run through the buffer and add overlays to links."
  3861. (catch 'exit
  3862. (let (f)
  3863. (while (re-search-forward org-plain-link-re limit t)
  3864. (setq f (get-text-property (match-beginning 0) 'face))
  3865. (if (or (eq f 'org-tag)
  3866. (and (listp f) (memq 'org-tag f)))
  3867. nil
  3868. (add-text-properties (match-beginning 0) (match-end 0)
  3869. (list 'mouse-face 'highlight
  3870. 'rear-nonsticky t
  3871. 'keymap org-mouse-map
  3872. ))
  3873. (throw 'exit t))))))
  3874. (defun org-activate-angle-links (limit)
  3875. "Run through the buffer and add overlays to links."
  3876. (if (re-search-forward org-angle-link-re limit t)
  3877. (progn
  3878. (add-text-properties (match-beginning 0) (match-end 0)
  3879. (list 'mouse-face 'highlight
  3880. 'rear-nonsticky t
  3881. 'keymap org-mouse-map
  3882. ))
  3883. t)))
  3884. (defmacro org-maybe-intangible (props)
  3885. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3886. In emacs 21, invisible text is not avoided by the command loop, so the
  3887. intangible property is needed to make sure point skips this text.
  3888. In Emacs 22, this is not necessary. The intangible text property has
  3889. led to problems with flyspell. These problems are fixed in flyspell.el,
  3890. but we still avoid setting the property in Emacs 22 and later.
  3891. We use a macro so that the test can happen at compilation time."
  3892. (if (< emacs-major-version 22)
  3893. `(append '(intangible t) ,props)
  3894. props))
  3895. (defun org-activate-bracket-links (limit)
  3896. "Run through the buffer and add overlays to bracketed links."
  3897. (if (re-search-forward org-bracket-link-regexp limit t)
  3898. (let* ((help (concat "LINK: "
  3899. (org-match-string-no-properties 1)))
  3900. ;; FIXME: above we should remove the escapes.
  3901. ;; but that requires another match, protecting match data,
  3902. ;; a lot of overhead for font-lock.
  3903. (ip (org-maybe-intangible
  3904. (list 'invisible 'org-link 'rear-nonsticky t
  3905. 'keymap org-mouse-map 'mouse-face 'highlight
  3906. 'help-echo help)))
  3907. (vp (list 'rear-nonsticky t
  3908. 'keymap org-mouse-map 'mouse-face 'highlight
  3909. 'help-echo help)))
  3910. ;; We need to remove the invisible property here. Table narrowing
  3911. ;; may have made some of this invisible.
  3912. (remove-text-properties (match-beginning 0) (match-end 0)
  3913. '(invisible nil))
  3914. (if (match-end 3)
  3915. (progn
  3916. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3917. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3918. (add-text-properties (match-end 3) (match-end 0) ip))
  3919. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3920. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3921. (add-text-properties (match-end 1) (match-end 0) ip))
  3922. t)))
  3923. (defun org-activate-dates (limit)
  3924. "Run through the buffer and add overlays to dates."
  3925. (if (re-search-forward org-tsr-regexp-both limit t)
  3926. (progn
  3927. (add-text-properties (match-beginning 0) (match-end 0)
  3928. (list 'mouse-face 'highlight
  3929. 'rear-nonsticky t
  3930. 'keymap org-mouse-map))
  3931. (when org-display-custom-times
  3932. (if (match-end 3)
  3933. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3934. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3935. t)))
  3936. (defvar org-target-link-regexp nil
  3937. "Regular expression matching radio targets in plain text.")
  3938. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3939. "Regular expression matching a link target.")
  3940. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3941. "Regular expression matching a radio target.")
  3942. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3943. "Regular expression matching any target.")
  3944. (defun org-activate-target-links (limit)
  3945. "Run through the buffer and add overlays to target matches."
  3946. (when org-target-link-regexp
  3947. (let ((case-fold-search t))
  3948. (if (re-search-forward org-target-link-regexp limit t)
  3949. (progn
  3950. (add-text-properties (match-beginning 0) (match-end 0)
  3951. (list 'mouse-face 'highlight
  3952. 'rear-nonsticky t
  3953. 'keymap org-mouse-map
  3954. 'help-echo "Radio target link"
  3955. 'org-linked-text t))
  3956. t)))))
  3957. (defun org-update-radio-target-regexp ()
  3958. "Find all radio targets in this file and update the regular expression."
  3959. (interactive)
  3960. (when (memq 'radio org-activate-links)
  3961. (setq org-target-link-regexp
  3962. (org-make-target-link-regexp (org-all-targets 'radio)))
  3963. (org-restart-font-lock)))
  3964. (defun org-hide-wide-columns (limit)
  3965. (let (s e)
  3966. (setq s (text-property-any (point) (or limit (point-max))
  3967. 'org-cwidth t))
  3968. (when s
  3969. (setq e (next-single-property-change s 'org-cwidth))
  3970. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3971. (goto-char e)
  3972. t)))
  3973. (defun org-restart-font-lock ()
  3974. "Restart font-lock-mode, to force refontification."
  3975. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3976. ;; FIXME: Could font-lock-fontify-buffer be enough???
  3977. (font-lock-mode -1)
  3978. (font-lock-mode 1)))
  3979. (defun org-all-targets (&optional radio)
  3980. "Return a list of all targets in this file.
  3981. With optional argument RADIO, only find radio targets."
  3982. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3983. rtn)
  3984. (save-excursion
  3985. (goto-char (point-min))
  3986. (while (re-search-forward re nil t)
  3987. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3988. rtn)))
  3989. (defun org-make-target-link-regexp (targets)
  3990. "Make regular expression matching all strings in TARGETS.
  3991. The regular expression finds the targets also if there is a line break
  3992. between words."
  3993. (and targets
  3994. (concat
  3995. "\\<\\("
  3996. (mapconcat
  3997. (lambda (x)
  3998. (while (string-match " +" x)
  3999. (setq x (replace-match "\\s-+" t t x)))
  4000. x)
  4001. targets
  4002. "\\|")
  4003. "\\)\\>")))
  4004. (defun org-activate-tags (limit)
  4005. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4006. (progn
  4007. (add-text-properties (match-beginning 1) (match-end 1)
  4008. (list 'mouse-face 'highlight
  4009. 'rear-nonsticky t
  4010. 'keymap org-mouse-map))
  4011. t)))
  4012. (defun org-outline-level ()
  4013. (save-excursion
  4014. (looking-at outline-regexp)
  4015. (if (match-beginning 1)
  4016. (+ (org-get-string-indentation (match-string 1)) 1000)
  4017. (1- (- (match-end 0) (match-beginning 0))))))
  4018. (defvar org-font-lock-keywords nil)
  4019. (defconst org-property-re "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(\\S-.*\\)"
  4020. "Regular expression matching a property line.")
  4021. (defun org-set-font-lock-defaults ()
  4022. (let* ((em org-fontify-emphasized-text)
  4023. (lk org-activate-links)
  4024. (org-font-lock-extra-keywords
  4025. ;; Headlines
  4026. (list
  4027. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4028. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4029. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4030. (1 'org-table))
  4031. ;; Links
  4032. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4033. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4034. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4035. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4036. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4037. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4038. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4039. '(org-hide-wide-columns (0 nil append))
  4040. ;; TODO lines
  4041. (list (concat "^\\*+[ \t]+" org-not-done-regexp)
  4042. '(1 'org-todo t))
  4043. ;; Priorities
  4044. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4045. ;; Special keywords
  4046. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4047. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4048. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4049. (list (concat "\\<" org-archived-string) '(0 'org-special-keyword t))
  4050. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4051. ;; Emphasis
  4052. (if em
  4053. (if (featurep 'xemacs)
  4054. '(org-do-emphasis-faces (0 nil append))
  4055. '(org-do-emphasis-faces)))
  4056. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  4057. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4058. 2 'bold prepend)
  4059. (if org-provide-checkbox-statistics
  4060. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4061. (0 (org-get-checkbox-statistics-face) t)))
  4062. ;; COMMENT
  4063. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4064. "\\|" org-quote-string "\\)\\>")
  4065. '(1 'org-special-keyword t))
  4066. '("^#.*" (0 'font-lock-comment-face t))
  4067. ;; DONE
  4068. (if org-fontify-done-headline
  4069. (list (concat "^[*]+ +\\<\\("
  4070. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4071. "\\)\\(.*\\)")
  4072. '(1 'org-done t) '(2 'org-headline-done t))
  4073. (list (concat "^[*]+ +\\<\\("
  4074. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4075. "\\)\\>")
  4076. '(1 'org-done t)))
  4077. ;; Table stuff
  4078. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  4079. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4080. ; '("^[ \t]*| *\\([#!$*_^/]\\) *|" (1 'org-formula t))
  4081. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4082. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4083. ;; Drawers
  4084. ; (list org-drawer-regexp '(0 'org-drawer t))
  4085. ; (list "^[ \t]*:END:" '(0 'org-drawer t))
  4086. (list org-drawer-regexp '(0 'org-special-keyword t))
  4087. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4088. ;; Properties
  4089. (list org-property-re
  4090. '(1 'org-special-keyword t)
  4091. '(3 'org-property-value t))
  4092. (if org-format-transports-properties-p
  4093. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4094. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  4095. )))
  4096. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4097. ;; Now set the full font-lock-keywords
  4098. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4099. (org-set-local 'font-lock-defaults
  4100. '(org-font-lock-keywords t nil nil backward-paragraph))
  4101. (kill-local-variable 'font-lock-keywords) nil))
  4102. (defvar org-m nil)
  4103. (defvar org-l nil)
  4104. (defvar org-f nil)
  4105. (defun org-get-level-face (n)
  4106. "Get the right face for match N in font-lock matching of healdines."
  4107. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4108. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4109. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4110. (cond
  4111. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4112. ((eq n 2) org-f)
  4113. (t (if org-level-color-stars-only nil org-f))))
  4114. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4115. "Remove fontification and activation overlays from links."
  4116. (font-lock-default-unfontify-region beg end)
  4117. (let* ((buffer-undo-list t)
  4118. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4119. (inhibit-modification-hooks t)
  4120. deactivate-mark buffer-file-name buffer-file-truename)
  4121. (remove-text-properties beg end
  4122. '(mouse-face t keymap t org-linked-text t
  4123. rear-nonsticky t
  4124. invisible t intangible t))))
  4125. ;;;; Visibility cycling, including org-goto and indirect buffer
  4126. ;;; Cycling
  4127. (defvar org-cycle-global-status nil)
  4128. (make-variable-buffer-local 'org-cycle-global-status)
  4129. (defvar org-cycle-subtree-status nil)
  4130. (make-variable-buffer-local 'org-cycle-subtree-status)
  4131. ;;;###autoload
  4132. (defun org-cycle (&optional arg)
  4133. "Visibility cycling for Org-mode.
  4134. - When this function is called with a prefix argument, rotate the entire
  4135. buffer through 3 states (global cycling)
  4136. 1. OVERVIEW: Show only top-level headlines.
  4137. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4138. 3. SHOW ALL: Show everything.
  4139. - When point is at the beginning of a headline, rotate the subtree started
  4140. by this line through 3 different states (local cycling)
  4141. 1. FOLDED: Only the main headline is shown.
  4142. 2. CHILDREN: The main headline and the direct children are shown.
  4143. From this state, you can move to one of the children
  4144. and zoom in further.
  4145. 3. SUBTREE: Show the entire subtree, including body text.
  4146. - When there is a numeric prefix, go up to a heading with level ARG, do
  4147. a `show-subtree' and return to the previous cursor position. If ARG
  4148. is negative, go up that many levels.
  4149. - When point is not at the beginning of a headline, execute
  4150. `indent-relative', like TAB normally does. See the option
  4151. `org-cycle-emulate-tab' for details.
  4152. - Special case: if point is at the beginning of the buffer and there is
  4153. no headline in line 1, this function will act as if called with prefix arg."
  4154. (interactive "P")
  4155. (let* ((outline-regexp
  4156. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4157. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4158. outline-regexp))
  4159. (bob-special (and org-cycle-global-at-bob (bobp)
  4160. (not (looking-at outline-regexp))))
  4161. (org-cycle-hook
  4162. (if bob-special
  4163. (delq 'org-optimize-window-after-visibility-change
  4164. (copy-sequence org-cycle-hook))
  4165. org-cycle-hook))
  4166. (pos (point)))
  4167. (if (or bob-special (equal arg '(4)))
  4168. ;; special case: use global cycling
  4169. (setq arg t))
  4170. (cond
  4171. ((org-at-table-p 'any)
  4172. ;; Enter the table or move to the next field in the table
  4173. (or (org-table-recognize-table.el)
  4174. (progn
  4175. (if arg (org-table-edit-field t)
  4176. (org-table-justify-field-maybe)
  4177. (call-interactively 'org-table-next-field)))))
  4178. ((eq arg t) ;; Global cycling
  4179. (cond
  4180. ((and (eq last-command this-command)
  4181. (eq org-cycle-global-status 'overview))
  4182. ;; We just created the overview - now do table of contents
  4183. ;; This can be slow in very large buffers, so indicate action
  4184. (message "CONTENTS...")
  4185. (org-content)
  4186. (message "CONTENTS...done")
  4187. (setq org-cycle-global-status 'contents)
  4188. (run-hook-with-args 'org-cycle-hook 'contents))
  4189. ((and (eq last-command this-command)
  4190. (eq org-cycle-global-status 'contents))
  4191. ;; We just showed the table of contents - now show everything
  4192. (show-all)
  4193. (message "SHOW ALL")
  4194. (setq org-cycle-global-status 'all)
  4195. (run-hook-with-args 'org-cycle-hook 'all))
  4196. (t
  4197. ;; Default action: go to overview
  4198. (org-overview)
  4199. (message "OVERVIEW")
  4200. (setq org-cycle-global-status 'overview)
  4201. (run-hook-with-args 'org-cycle-hook 'overview))))
  4202. ((and org-drawers
  4203. (save-excursion
  4204. (beginning-of-line 1)
  4205. (looking-at org-drawer-regexp)))
  4206. ;; Toggle block visibility
  4207. (org-flag-drawer
  4208. (not (get-char-property (match-end 0) 'invisible))))
  4209. ((integerp arg)
  4210. ;; Show-subtree, ARG levels up from here.
  4211. (save-excursion
  4212. (org-back-to-heading)
  4213. (outline-up-heading (if (< arg 0) (- arg)
  4214. (- (funcall outline-level) arg)))
  4215. (org-show-subtree)))
  4216. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4217. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4218. ;; At a heading: rotate between three different views
  4219. (org-back-to-heading)
  4220. (let ((goal-column 0) eoh eol eos)
  4221. ;; First, some boundaries
  4222. (save-excursion
  4223. (org-back-to-heading)
  4224. (save-excursion
  4225. (beginning-of-line 2)
  4226. (while (and (not (eobp)) ;; this is like `next-line'
  4227. (get-char-property (1- (point)) 'invisible))
  4228. (beginning-of-line 2)) (setq eol (point)))
  4229. (outline-end-of-heading) (setq eoh (point))
  4230. (org-end-of-subtree t)
  4231. (unless (eobp)
  4232. (skip-chars-forward " \t\n")
  4233. (beginning-of-line 1) ; in case this is an item
  4234. )
  4235. (setq eos (1- (point))))
  4236. ;; Find out what to do next and set `this-command'
  4237. (cond
  4238. ((= eos eoh)
  4239. ;; Nothing is hidden behind this heading
  4240. (message "EMPTY ENTRY")
  4241. (setq org-cycle-subtree-status nil)
  4242. (save-excursion
  4243. (goto-char eos)
  4244. (outline-next-heading)
  4245. (if (org-invisible-p) (org-flag-heading nil))))
  4246. ((or (>= eol eos)
  4247. (not (string-match "\\S-" (buffer-substring eol eos))))
  4248. ;; Entire subtree is hidden in one line: open it
  4249. (org-show-entry)
  4250. (show-children)
  4251. (message "CHILDREN")
  4252. (save-excursion
  4253. (goto-char eos)
  4254. (outline-next-heading)
  4255. (if (org-invisible-p) (org-flag-heading nil)))
  4256. (setq org-cycle-subtree-status 'children)
  4257. (run-hook-with-args 'org-cycle-hook 'children))
  4258. ((and (eq last-command this-command)
  4259. (eq org-cycle-subtree-status 'children))
  4260. ;; We just showed the children, now show everything.
  4261. (org-show-subtree)
  4262. (message "SUBTREE")
  4263. (setq org-cycle-subtree-status 'subtree)
  4264. (run-hook-with-args 'org-cycle-hook 'subtree))
  4265. (t
  4266. ;; Default action: hide the subtree.
  4267. (hide-subtree)
  4268. (message "FOLDED")
  4269. (setq org-cycle-subtree-status 'folded)
  4270. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4271. ;; TAB emulation
  4272. (buffer-read-only (org-back-to-heading))
  4273. ((org-try-cdlatex-tab))
  4274. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4275. (or (not (bolp))
  4276. (not (looking-at outline-regexp))))
  4277. (call-interactively (global-key-binding "\t")))
  4278. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4279. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4280. (or (and (eq org-cycle-emulate-tab 'white)
  4281. (= (match-end 0) (point-at-eol)))
  4282. (and (eq org-cycle-emulate-tab 'whitestart)
  4283. (>= (match-end 0) pos))))
  4284. t
  4285. (eq org-cycle-emulate-tab t))
  4286. (if (and (looking-at "[ \n\r\t]")
  4287. (string-match "^[ \t]*$" (buffer-substring
  4288. (point-at-bol) (point))))
  4289. (progn
  4290. (beginning-of-line 1)
  4291. (and (looking-at "[ \t]+") (replace-match ""))))
  4292. (call-interactively (global-key-binding "\t")))
  4293. (t (save-excursion
  4294. (org-back-to-heading)
  4295. (org-cycle))))))
  4296. ;;;###autoload
  4297. (defun org-global-cycle (&optional arg)
  4298. "Cycle the global visibility. For details see `org-cycle'."
  4299. (interactive "P")
  4300. (let ((org-cycle-include-plain-lists
  4301. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4302. (if (integerp arg)
  4303. (progn
  4304. (show-all)
  4305. (hide-sublevels arg)
  4306. (setq org-cycle-global-status 'contents))
  4307. (org-cycle '(4)))))
  4308. (defun org-overview ()
  4309. "Switch to overview mode, shoing only top-level headlines.
  4310. Really, this shows all headlines with level equal or greater than the level
  4311. of the first headline in the buffer. This is important, because if the
  4312. first headline is not level one, then (hide-sublevels 1) gives confusing
  4313. results."
  4314. (interactive)
  4315. (let ((level (save-excursion
  4316. (goto-char (point-min))
  4317. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4318. (progn
  4319. (goto-char (match-beginning 0))
  4320. (funcall outline-level))))))
  4321. (and level (hide-sublevels level))))
  4322. (defun org-content (&optional arg)
  4323. "Show all headlines in the buffer, like a table of contents.
  4324. With numerical argument N, show content up to level N."
  4325. (interactive "P")
  4326. (save-excursion
  4327. ;; Visit all headings and show their offspring
  4328. (and (integerp arg) (org-overview))
  4329. (goto-char (point-max))
  4330. (catch 'exit
  4331. (while (and (progn (condition-case nil
  4332. (outline-previous-visible-heading 1)
  4333. (error (goto-char (point-min))))
  4334. t)
  4335. (looking-at outline-regexp))
  4336. (if (integerp arg)
  4337. (show-children (1- arg))
  4338. (show-branches))
  4339. (if (bobp) (throw 'exit nil))))))
  4340. (defun org-optimize-window-after-visibility-change (state)
  4341. "Adjust the window after a change in outline visibility.
  4342. This function is the default value of the hook `org-cycle-hook'."
  4343. (when (get-buffer-window (current-buffer))
  4344. (cond
  4345. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4346. ; ((eq state 'overview) (org-beginning-of-line))
  4347. ((eq state 'content) nil)
  4348. ((eq state 'all) nil)
  4349. ((eq state 'folded) nil)
  4350. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4351. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4352. (defun org-cycle-show-empty-lines (state)
  4353. "Show empty lines above all visible headlines.
  4354. The region to be covered depends on STATE when called through
  4355. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4356. entire buffer covered. Note that an empty line is only shown if there
  4357. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4358. (when (> org-cycle-separator-lines 0)
  4359. (save-excursion
  4360. (let* ((n org-cycle-separator-lines)
  4361. (re (cond
  4362. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4363. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4364. (t (let ((ns (number-to-string (- n 2))))
  4365. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4366. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4367. beg end)
  4368. (cond
  4369. ((memq state '(overview contents t))
  4370. (setq beg (point-min) end (point-max)))
  4371. ((memq state '(children folded))
  4372. (setq beg (point) end (progn (org-end-of-subtree t t)
  4373. (beginning-of-line 2)
  4374. (point)))))
  4375. (when beg
  4376. (goto-char beg)
  4377. (while (re-search-forward re end t)
  4378. (if (not (get-char-property (match-end 1) 'invisible))
  4379. (outline-flag-region
  4380. (match-beginning 1) (match-end 1) nil)))))))
  4381. ;; Never hide empty lines at the end of the file.
  4382. (save-excursion
  4383. (goto-char (point-max))
  4384. (outline-previous-heading)
  4385. (outline-end-of-heading)
  4386. (if (and (looking-at "[ \t\n]+")
  4387. (= (match-end 0) (point-max)))
  4388. (outline-flag-region (point) (match-end 0) nil))))
  4389. (defun org-subtree-end-visible-p ()
  4390. "Is the end of the current subtree visible?"
  4391. (pos-visible-in-window-p
  4392. (save-excursion (org-end-of-subtree t) (point))))
  4393. (defun org-first-headline-recenter (&optional N)
  4394. "Move cursor to the first headline and recenter the headline.
  4395. Optional argument N means, put the headline into the Nth line of the window."
  4396. (goto-char (point-min))
  4397. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4398. (beginning-of-line)
  4399. (recenter (prefix-numeric-value N))))
  4400. ;;; Org-goto
  4401. (defvar org-goto-window-configuration nil)
  4402. (defvar org-goto-marker nil)
  4403. (defvar org-goto-map
  4404. (let ((map (make-sparse-keymap)))
  4405. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  4406. (while (setq cmd (pop cmds))
  4407. (substitute-key-definition cmd cmd map global-map)))
  4408. (org-defkey map "\C-m" 'org-goto-ret)
  4409. (org-defkey map [(left)] 'org-goto-left)
  4410. (org-defkey map [(right)] 'org-goto-right)
  4411. (org-defkey map [(?q)] 'org-goto-quit)
  4412. (org-defkey map [(control ?g)] 'org-goto-quit)
  4413. (org-defkey map "\C-i" 'org-cycle)
  4414. (org-defkey map [(tab)] 'org-cycle)
  4415. (org-defkey map [(down)] 'outline-next-visible-heading)
  4416. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4417. (org-defkey map "n" 'outline-next-visible-heading)
  4418. (org-defkey map "p" 'outline-previous-visible-heading)
  4419. (org-defkey map "f" 'outline-forward-same-level)
  4420. (org-defkey map "b" 'outline-backward-same-level)
  4421. (org-defkey map "u" 'outline-up-heading)
  4422. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4423. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4424. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4425. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4426. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4427. ;; FIXME: Could we use suppress-keymap?
  4428. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4429. (while l (org-defkey map (int-to-string (pop l)) 'digit-argument)))
  4430. map))
  4431. (defconst org-goto-help
  4432. "Select a location to jump to, press RET
  4433. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  4434. (defun org-goto ()
  4435. "Go to a different location of the document, keeping current visibility.
  4436. When you want to go to a different location in a document, the fastest way
  4437. is often to fold the entire buffer and then dive into the tree. This
  4438. method has the disadvantage, that the previous location will be folded,
  4439. which may not be what you want.
  4440. This command works around this by showing a copy of the current buffer in
  4441. overview mode. You can dive into the tree in that copy, to find the
  4442. location you want to reach. When pressing RET, the command returns to the
  4443. original buffer in which the visibility is still unchanged. It then jumps
  4444. to the new location, making it and the headline hierarchy above it visible."
  4445. (interactive)
  4446. (let* ((org-goto-start-pos (point))
  4447. (selected-point
  4448. (org-get-location (current-buffer) org-goto-help)))
  4449. (if selected-point
  4450. (progn
  4451. (org-mark-ring-push org-goto-start-pos)
  4452. (goto-char selected-point)
  4453. (if (or (org-invisible-p) (org-invisible-p2))
  4454. (org-show-context 'org-goto)))
  4455. (error "Quit"))))
  4456. (defvar org-selected-point nil) ; dynamically scoped parameter
  4457. (defun org-get-location (buf help)
  4458. "Let the user select a location in the Org-mode buffer BUF.
  4459. This function uses a recursive edit. It returns the selected position
  4460. or nil."
  4461. (let (org-selected-point)
  4462. (save-excursion
  4463. (save-window-excursion
  4464. (delete-other-windows)
  4465. (switch-to-buffer (get-buffer-create "*org-goto*"))
  4466. (with-output-to-temp-buffer "*Help*"
  4467. (princ help))
  4468. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4469. (setq buffer-read-only nil)
  4470. (erase-buffer)
  4471. (insert-buffer-substring buf)
  4472. (let ((org-startup-truncated t)
  4473. (org-startup-folded t)
  4474. (org-startup-align-all-tables nil))
  4475. (org-mode))
  4476. (setq buffer-read-only t)
  4477. (if (and (boundp 'org-goto-start-pos)
  4478. (integer-or-marker-p org-goto-start-pos))
  4479. (let ((org-show-hierarchy-above t)
  4480. (org-show-siblings t)
  4481. (org-show-following-heading t))
  4482. (goto-char org-goto-start-pos)
  4483. (and (org-invisible-p) (org-show-context)))
  4484. (goto-char (point-min)))
  4485. (org-beginning-of-line)
  4486. (message "Select location and press RET")
  4487. ;; now we make sure that during selection, ony very few keys work
  4488. ;; and that it is impossible to switch to another window.
  4489. (let ((gm (current-global-map))
  4490. (overriding-local-map org-goto-map))
  4491. (unwind-protect
  4492. (progn
  4493. (use-global-map org-goto-map)
  4494. (recursive-edit))
  4495. (use-global-map gm)))))
  4496. (kill-buffer "*org-goto*")
  4497. org-selected-point))
  4498. (defun org-goto-ret (&optional arg)
  4499. "Finish `org-goto' by going to the new location."
  4500. (interactive "P")
  4501. (setq org-selected-point (point)
  4502. current-prefix-arg arg)
  4503. (throw 'exit nil))
  4504. (defun org-goto-left ()
  4505. "Finish `org-goto' by going to the new location."
  4506. (interactive)
  4507. (if (org-on-heading-p)
  4508. (progn
  4509. (beginning-of-line 1)
  4510. (setq org-selected-point (point)
  4511. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4512. (throw 'exit nil))
  4513. (error "Not on a heading")))
  4514. (defun org-goto-right ()
  4515. "Finish `org-goto' by going to the new location."
  4516. (interactive)
  4517. (if (org-on-heading-p)
  4518. (progn
  4519. (outline-end-of-subtree)
  4520. (or (eobp) (forward-char 1))
  4521. (setq org-selected-point (point)
  4522. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4523. (throw 'exit nil))
  4524. (error "Not on a heading")))
  4525. (defun org-goto-quit ()
  4526. "Finish `org-goto' without cursor motion."
  4527. (interactive)
  4528. (setq org-selected-point nil)
  4529. (throw 'exit nil))
  4530. ;;; Indirect buffer display of subtrees
  4531. (defvar org-indirect-dedicated-frame nil
  4532. "This is the frame being used for indirect tree display.")
  4533. (defvar org-last-indirect-buffer nil)
  4534. (defun org-tree-to-indirect-buffer (&optional arg)
  4535. "Create indirect buffer and narrow it to current subtree.
  4536. With numerical prefix ARG, go up to this level and then take that tree.
  4537. If ARG is negative, go up that many levels.
  4538. Normally this command removes the indirect buffer previously made
  4539. with this command. However, when called with a C-u prefix, the last buffer
  4540. is kept so that you can work with several indirect buffers at the same time.
  4541. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4542. requests that a new frame be made for the new buffer, so that the dedicated
  4543. frame is not changed."
  4544. (interactive "P")
  4545. (let ((cbuf (current-buffer))
  4546. (cwin (selected-window))
  4547. (pos (point))
  4548. beg end level heading ibuf)
  4549. (save-excursion
  4550. (org-back-to-heading t)
  4551. (when (numberp arg)
  4552. (setq level (org-outline-level))
  4553. (if (< arg 0) (setq arg (+ level arg)))
  4554. (while (> (setq level (org-outline-level)) arg)
  4555. (outline-up-heading 1 t)))
  4556. (setq beg (point)
  4557. heading (org-get-heading))
  4558. (org-end-of-subtree t) (setq end (point)))
  4559. (if (and (not arg)
  4560. (buffer-live-p org-last-indirect-buffer))
  4561. (kill-buffer org-last-indirect-buffer))
  4562. (setq ibuf (org-get-indirect-buffer cbuf)
  4563. org-last-indirect-buffer ibuf)
  4564. (cond
  4565. ((or (eq org-indirect-buffer-display 'new-frame)
  4566. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4567. (select-frame (make-frame))
  4568. (delete-other-windows)
  4569. (switch-to-buffer ibuf)
  4570. (org-set-frame-title heading))
  4571. ((eq org-indirect-buffer-display 'dedicated-frame)
  4572. (raise-frame
  4573. (select-frame (or (and org-indirect-dedicated-frame
  4574. (frame-live-p org-indirect-dedicated-frame)
  4575. org-indirect-dedicated-frame)
  4576. (setq org-indirect-dedicated-frame (make-frame)))))
  4577. (delete-other-windows)
  4578. (switch-to-buffer ibuf)
  4579. (org-set-frame-title (concat "Indirect: " heading)))
  4580. ((eq org-indirect-buffer-display 'current-window)
  4581. (switch-to-buffer ibuf))
  4582. ((eq org-indirect-buffer-display 'other-window)
  4583. (pop-to-buffer ibuf))
  4584. (t (error "Invalid value.")))
  4585. (if (featurep 'xemacs)
  4586. (save-excursion (org-mode) (turn-on-font-lock)))
  4587. (narrow-to-region beg end)
  4588. (show-all)
  4589. (goto-char pos)
  4590. (and (window-live-p cwin) (select-window cwin))))
  4591. (defun org-get-indirect-buffer (&optional buffer)
  4592. (setq buffer (or buffer (current-buffer)))
  4593. (let ((n 1) (base (buffer-name buffer)) bname)
  4594. (while (buffer-live-p
  4595. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4596. (setq n (1+ n)))
  4597. (condition-case nil
  4598. (make-indirect-buffer buffer bname 'clone)
  4599. (error (make-indirect-buffer buffer bname)))))
  4600. (defun org-set-frame-title (title)
  4601. "Set the title of the current frame to the string TITLE."
  4602. ;; FIXME: how to name a single frame in XEmacs???
  4603. (unless (featurep 'xemacs)
  4604. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4605. ;;;; Structure editing
  4606. ;;; Inserting headlines
  4607. (defun org-insert-heading (&optional force-heading)
  4608. "Insert a new heading or item with same depth at point.
  4609. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4610. If point is at the beginning of a headline, insert a sibling before the
  4611. current headline. If point is in the middle of a headline, split the headline
  4612. at that position and make the rest of the headline part of the sibling below
  4613. the current headline."
  4614. (interactive "P")
  4615. (if (= (buffer-size) 0)
  4616. (insert "\n* ")
  4617. (when (or force-heading (not (org-insert-item)))
  4618. (let* ((head (save-excursion
  4619. (condition-case nil
  4620. (progn
  4621. (org-back-to-heading)
  4622. (match-string 0))
  4623. (error "*"))))
  4624. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4625. pos)
  4626. (cond
  4627. ((and (org-on-heading-p) (bolp)
  4628. (or (bobp)
  4629. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4630. (open-line (if blank 2 1)))
  4631. ((and (bolp)
  4632. (or (bobp)
  4633. (save-excursion
  4634. (backward-char 1) (not (org-invisible-p)))))
  4635. nil)
  4636. (t (newline (if blank 2 1))))
  4637. (insert head) (just-one-space)
  4638. (setq pos (point))
  4639. (end-of-line 1)
  4640. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4641. (run-hooks 'org-insert-heading-hook)))))
  4642. (defun org-insert-todo-heading (arg)
  4643. "Insert a new heading with the same level and TODO state as current heading.
  4644. If the heading has no TODO state, or if the state is DONE, use the first
  4645. state (TODO by default). Also with prefix arg, force first state."
  4646. (interactive "P")
  4647. (when (not (org-insert-item 'checkbox))
  4648. (org-insert-heading)
  4649. (save-excursion
  4650. (org-back-to-heading)
  4651. (outline-previous-heading)
  4652. (looking-at org-todo-line-regexp))
  4653. (if (or arg
  4654. (not (match-beginning 2))
  4655. (member (match-string 2) org-done-keywords))
  4656. (insert (car org-todo-keywords-1) " ")
  4657. (insert (match-string 2) " "))))
  4658. (defun org-insert-subheading (arg)
  4659. "Insert a new subheading and demote it.
  4660. Works for outline headings and for plain lists alike."
  4661. (interactive "P")
  4662. (org-insert-heading arg)
  4663. (cond
  4664. ((org-on-heading-p) (org-do-demote))
  4665. ((org-at-item-p) (org-indent-item 1))))
  4666. (defun org-insert-todo-subheading (arg)
  4667. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4668. Works for outline headings and for plain lists alike."
  4669. (interactive "P")
  4670. (org-insert-todo-heading arg)
  4671. (cond
  4672. ((org-on-heading-p) (org-do-demote))
  4673. ((org-at-item-p) (org-indent-item 1))))
  4674. ;;; Promotion and Demotion
  4675. (defun org-promote-subtree ()
  4676. "Promote the entire subtree.
  4677. See also `org-promote'."
  4678. (interactive)
  4679. (save-excursion
  4680. (org-map-tree 'org-promote))
  4681. (org-fix-position-after-promote))
  4682. (defun org-demote-subtree ()
  4683. "Demote the entire subtree. See `org-demote'.
  4684. See also `org-promote'."
  4685. (interactive)
  4686. (save-excursion
  4687. (org-map-tree 'org-demote))
  4688. (org-fix-position-after-promote))
  4689. (defun org-do-promote ()
  4690. "Promote the current heading higher up the tree.
  4691. If the region is active in `transient-mark-mode', promote all headings
  4692. in the region."
  4693. (interactive)
  4694. (save-excursion
  4695. (if (org-region-active-p)
  4696. (org-map-region 'org-promote (region-beginning) (region-end))
  4697. (org-promote)))
  4698. (org-fix-position-after-promote))
  4699. (defun org-do-demote ()
  4700. "Demote the current heading lower down the tree.
  4701. If the region is active in `transient-mark-mode', demote all headings
  4702. in the region."
  4703. (interactive)
  4704. (save-excursion
  4705. (if (org-region-active-p)
  4706. (org-map-region 'org-demote (region-beginning) (region-end))
  4707. (org-demote)))
  4708. (org-fix-position-after-promote))
  4709. (defun org-fix-position-after-promote ()
  4710. "Make sure that after pro/demotion cursor position is right."
  4711. (let ((pos (point)))
  4712. (when (save-excursion
  4713. (beginning-of-line 1)
  4714. (looking-at org-todo-line-regexp)
  4715. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4716. (cond ((eobp) (insert " "))
  4717. ((eolp) (insert " "))
  4718. ((equal (char-after) ?\ ) (forward-char 1))))))
  4719. (defun org-reduced-level (l)
  4720. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4721. (defun org-get-legal-level (level &optional change)
  4722. "Rectify a level change under the influence of `org-odd-levels-only'
  4723. LEVEL is a current level, CHANGE is by how much the level should be
  4724. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4725. even level numbers will become the next higher odd number."
  4726. (if org-odd-levels-only
  4727. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4728. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4729. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4730. (max 1 (+ level change))))
  4731. (defun org-promote ()
  4732. "Promote the current heading higher up the tree.
  4733. If the region is active in `transient-mark-mode', promote all headings
  4734. in the region."
  4735. (org-back-to-heading t)
  4736. (let* ((level (save-match-data (funcall outline-level)))
  4737. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  4738. (diff (abs (- level (length up-head) -1))))
  4739. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4740. (replace-match up-head nil t)
  4741. ;; Fixup tag positioning
  4742. (and org-auto-align-tags (org-set-tags nil t))
  4743. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4744. (defun org-demote ()
  4745. "Demote the current heading lower down the tree.
  4746. If the region is active in `transient-mark-mode', demote all headings
  4747. in the region."
  4748. (org-back-to-heading t)
  4749. (let* ((level (save-match-data (funcall outline-level)))
  4750. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  4751. (diff (abs (- level (length down-head) -1))))
  4752. (replace-match down-head nil t)
  4753. ;; Fixup tag positioning
  4754. (and org-auto-align-tags (org-set-tags nil t))
  4755. (if org-adapt-indentation (org-fixup-indentation diff))))
  4756. (defun org-map-tree (fun)
  4757. "Call FUN for every heading underneath the current one."
  4758. (org-back-to-heading)
  4759. (let ((level (funcall outline-level)))
  4760. (save-excursion
  4761. (funcall fun)
  4762. (while (and (progn
  4763. (outline-next-heading)
  4764. (> (funcall outline-level) level))
  4765. (not (eobp)))
  4766. (funcall fun)))))
  4767. (defun org-map-region (fun beg end)
  4768. "Call FUN for every heading between BEG and END."
  4769. (let ((org-ignore-region t))
  4770. (save-excursion
  4771. (setq end (copy-marker end))
  4772. (goto-char beg)
  4773. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4774. (< (point) end))
  4775. (funcall fun))
  4776. (while (and (progn
  4777. (outline-next-heading)
  4778. (< (point) end))
  4779. (not (eobp)))
  4780. (funcall fun)))))
  4781. (defun org-fixup-indentation (diff)
  4782. "Change the indentation in the current entry by DIFF
  4783. However, if any line in the current entry has no indentation, or if it
  4784. would end up with no indentation after the change, nothing at all is done."
  4785. (save-excursion
  4786. (let ((end (save-excursion (outline-next-heading)
  4787. (point-marker)))
  4788. (prohibit (if (> diff 0)
  4789. "^\\S-"
  4790. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4791. col)
  4792. (unless (save-excursion (end-of-line 1)
  4793. (re-search-forward prohibit end t))
  4794. (while (re-search-forward "^[ \t]+" end t)
  4795. (goto-char (match-end 0))
  4796. (setq col (current-column))
  4797. (if (< diff 0) (replace-match ""))
  4798. (indent-to (+ diff col))))
  4799. (move-marker end nil))))
  4800. (defun org-convert-to-odd-levels ()
  4801. "Convert an org-mode file with all levels allowed to one with odd levels.
  4802. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4803. level 5 etc."
  4804. (interactive)
  4805. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4806. (let ((org-odd-levels-only nil) n)
  4807. (save-excursion
  4808. (goto-char (point-min))
  4809. (while (re-search-forward "^\\*\\*+ " nil t)
  4810. (setq n (- (length (match-string 0)) 2))
  4811. (while (>= (setq n (1- n)) 0)
  4812. (org-demote))
  4813. (end-of-line 1))))))
  4814. (defun org-convert-to-oddeven-levels ()
  4815. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4816. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4817. section with an even level, conversion would destroy the structure of the file. An error
  4818. is signaled in this case."
  4819. (interactive)
  4820. (goto-char (point-min))
  4821. ;; First check if there are no even levels
  4822. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4823. (org-show-context t)
  4824. (error "Not all levels are odd in this file. Conversion not possible."))
  4825. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4826. (let ((org-odd-levels-only nil) n)
  4827. (save-excursion
  4828. (goto-char (point-min))
  4829. (while (re-search-forward "^\\*\\*+ " nil t)
  4830. (setq n (/ (length (1- (match-string 0))) 2))
  4831. (while (>= (setq n (1- n)) 0)
  4832. (org-promote))
  4833. (end-of-line 1))))))
  4834. (defun org-tr-level (n)
  4835. "Make N odd if required."
  4836. (if org-odd-levels-only (1+ (/ n 2)) n))
  4837. ;;; Vertical tree motion, cutting and pasting of subtrees
  4838. (defun org-move-subtree-up (&optional arg)
  4839. "Move the current subtree up past ARG headlines of the same level."
  4840. (interactive "p")
  4841. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4842. (defun org-move-subtree-down (&optional arg)
  4843. "Move the current subtree down past ARG headlines of the same level."
  4844. (interactive "p")
  4845. (setq arg (prefix-numeric-value arg))
  4846. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4847. 'outline-get-last-sibling))
  4848. (ins-point (make-marker))
  4849. (cnt (abs arg))
  4850. beg end txt folded)
  4851. ;; Select the tree
  4852. (org-back-to-heading)
  4853. (setq beg (point))
  4854. (save-match-data
  4855. (save-excursion (outline-end-of-heading)
  4856. (setq folded (org-invisible-p)))
  4857. (outline-end-of-subtree))
  4858. (outline-next-heading)
  4859. (setq end (point))
  4860. ;; Find insertion point, with error handling
  4861. (goto-char beg)
  4862. (while (> cnt 0)
  4863. (or (and (funcall movfunc) (looking-at outline-regexp))
  4864. (progn (goto-char beg)
  4865. (error "Cannot move past superior level or buffer limit")))
  4866. (setq cnt (1- cnt)))
  4867. (if (> arg 0)
  4868. ;; Moving forward - still need to move over subtree
  4869. (progn (outline-end-of-subtree)
  4870. (outline-next-heading)
  4871. (if (not (or (looking-at (concat "^" outline-regexp))
  4872. (bolp)))
  4873. (newline))))
  4874. (move-marker ins-point (point))
  4875. (setq txt (buffer-substring beg end))
  4876. (delete-region beg end)
  4877. (insert txt)
  4878. (or (bolp) (insert "\n"))
  4879. (goto-char ins-point)
  4880. (if folded (hide-subtree))
  4881. (move-marker ins-point nil)))
  4882. (defvar org-subtree-clip ""
  4883. "Clipboard for cut and paste of subtrees.
  4884. This is actually only a copy of the kill, because we use the normal kill
  4885. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4886. (defvar org-subtree-clip-folded nil
  4887. "Was the last copied subtree folded?
  4888. This is used to fold the tree back after pasting.")
  4889. (defun org-cut-subtree ()
  4890. "Cut the current subtree into the clipboard.
  4891. This is a short-hand for marking the subtree and then cutting it."
  4892. (interactive)
  4893. (org-copy-subtree 'cut))
  4894. (defun org-copy-subtree (&optional cut)
  4895. "Cut the current subtree into the clipboard.
  4896. This is a short-hand for marking the subtree and then copying it.
  4897. If CUT is non-nil, actually cut the subtree."
  4898. (interactive)
  4899. (let (beg end folded)
  4900. (if (interactive-p)
  4901. (org-back-to-heading nil) ; take what looks like a subtree
  4902. (org-back-to-heading t)) ; take what is really there
  4903. (setq beg (point))
  4904. (save-match-data
  4905. (save-excursion (outline-end-of-heading)
  4906. (setq folded (org-invisible-p)))
  4907. (outline-end-of-subtree))
  4908. (if (equal (char-after) ?\n) (forward-char 1))
  4909. (setq end (point))
  4910. (goto-char beg)
  4911. (when (> end beg)
  4912. (setq org-subtree-clip-folded folded)
  4913. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4914. (setq org-subtree-clip (current-kill 0))
  4915. (message "%s: Subtree with %d characters"
  4916. (if cut "Cut" "Copied")
  4917. (length org-subtree-clip)))))
  4918. (defun org-paste-subtree (&optional level tree)
  4919. "Paste the clipboard as a subtree, with modification of headline level.
  4920. The entire subtree is promoted or demoted in order to match a new headline
  4921. level. By default, the new level is derived from the visible headings
  4922. before and after the insertion point, and taken to be the inferior headline
  4923. level of the two. So if the previous visible heading is level 3 and the
  4924. next is level 4 (or vice versa), level 4 will be used for insertion.
  4925. This makes sure that the subtree remains an independent subtree and does
  4926. not swallow low level entries.
  4927. You can also force a different level, either by using a numeric prefix
  4928. argument, or by inserting the heading marker by hand. For example, if the
  4929. cursor is after \"*****\", then the tree will be shifted to level 5.
  4930. If you want to insert the tree as is, just use \\[yank].
  4931. If optional TREE is given, use this text instead of the kill ring."
  4932. (interactive "P")
  4933. (unless (org-kill-is-subtree-p tree)
  4934. (error
  4935. (substitute-command-keys
  4936. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4937. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4938. (^re (concat "^\\(" outline-regexp "\\)"))
  4939. (re (concat "\\(" outline-regexp "\\)"))
  4940. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  4941. (old-level (if (string-match ^re txt)
  4942. (- (match-end 0) (match-beginning 0) 1)
  4943. -1))
  4944. (force-level (cond (level (prefix-numeric-value level))
  4945. ((string-match
  4946. ^re_ (buffer-substring (point-at-bol) (point)))
  4947. (- (match-end 0) (match-beginning 0)))
  4948. (t nil)))
  4949. (previous-level (save-excursion
  4950. (condition-case nil
  4951. (progn
  4952. (outline-previous-visible-heading 1)
  4953. (if (looking-at re)
  4954. (- (match-end 0) (match-beginning 0))
  4955. 1))
  4956. (error 1))))
  4957. (next-level (save-excursion
  4958. (condition-case nil
  4959. (progn
  4960. (outline-next-visible-heading 1)
  4961. (if (looking-at re)
  4962. (- (match-end 0) (match-beginning 0))
  4963. 1))
  4964. (error 1))))
  4965. (new-level (or force-level (max previous-level next-level)))
  4966. (shift (if (or (= old-level -1)
  4967. (= new-level -1)
  4968. (= old-level new-level))
  4969. 0
  4970. (- new-level old-level)))
  4971. (shift1 shift)
  4972. (delta (if (> shift 0) -1 1))
  4973. (func (if (> shift 0) 'org-demote 'org-promote))
  4974. (org-odd-levels-only nil)
  4975. beg end)
  4976. ;; Remove the forces level indicator
  4977. (if force-level
  4978. (delete-region (point-at-bol) (point)))
  4979. ;; Make sure we start at the beginning of an empty line
  4980. (if (not (bolp)) (insert "\n"))
  4981. (if (not (looking-at "[ \t]*$"))
  4982. (progn (insert "\n") (backward-char 1)))
  4983. ;; Paste
  4984. (setq beg (point))
  4985. (if (string-match "[ \t\r\n]+\\'" txt)
  4986. (setq txt (replace-match "\n" t t txt)))
  4987. (insert txt)
  4988. (setq end (point))
  4989. (if (looking-at "[ \t\r\n]+")
  4990. (replace-match "\n"))
  4991. (goto-char beg)
  4992. ;; Shift if necessary
  4993. (if (= shift 0)
  4994. (message "Pasted at level %d, without shift" new-level)
  4995. (save-restriction
  4996. (narrow-to-region beg end)
  4997. (while (not (= shift 0))
  4998. (org-map-region func (point-min) (point-max))
  4999. (setq shift (+ delta shift)))
  5000. (goto-char (point-min))
  5001. (message "Pasted at level %d, with shift by %d levels"
  5002. new-level shift1)))
  5003. (if (and kill-ring
  5004. (eq org-subtree-clip (current-kill 0))
  5005. org-subtree-clip-folded)
  5006. ;; The tree was folded before it was killed/copied
  5007. (hide-subtree))))
  5008. (defun org-kill-is-subtree-p (&optional txt)
  5009. "Check if the current kill is an outline subtree, or a set of trees.
  5010. Returns nil if kill does not start with a headline, or if the first
  5011. headline level is not the largest headline level in the tree.
  5012. So this will actually accept several entries of equal levels as well,
  5013. which is OK for `org-paste-subtree'.
  5014. If optional TXT is given, check this string instead of the current kill."
  5015. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5016. (start-level (and kill
  5017. (string-match (concat "\\`" outline-regexp) kill)
  5018. (- (match-end 0) (match-beginning 0))))
  5019. (re (concat "^" outline-regexp))
  5020. (start 1))
  5021. (if (not start-level)
  5022. nil ;; does not even start with a heading
  5023. (catch 'exit
  5024. (while (setq start (string-match re kill (1+ start)))
  5025. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  5026. (throw 'exit nil)))
  5027. t))))
  5028. (defun org-narrow-to-subtree ()
  5029. "Narrow buffer to the current subtree."
  5030. (interactive)
  5031. (save-excursion
  5032. (narrow-to-region
  5033. (progn (org-back-to-heading) (point))
  5034. (progn (org-end-of-subtree t t) (point)))))
  5035. ;;; Outline Sorting
  5036. (defun org-sort (with-case)
  5037. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  5038. (interactive "P")
  5039. (if (org-at-table-p)
  5040. (org-call-with-arg 'org-table-sort-lines with-case)
  5041. (org-call-with-arg 'org-sort-entries with-case)))
  5042. (defun org-sort-entries (&optional with-case sorting-type)
  5043. "Sort entries on a certain level of an outline tree.
  5044. If there is an active region, the entries in the region are sorted.
  5045. Else, if the cursor is before the first entry, sort the top-level items.
  5046. Else, the children of the entry at point are sorted.
  5047. Sorting can be alphabetically, numerically, and by date/time as given by
  5048. the first time stamp in the entry. The command prompts for the sorting
  5049. type unless it has been given to the function through the SORTING-TYPE
  5050. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  5051. Comparing entries ignores case by default. However, with an optional argument
  5052. WITH-CASE, the sorting considers case as well. With two prefix arguments
  5053. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  5054. (interactive "P")
  5055. (let ((unique (equal with-case '(16)))
  5056. start beg end entries stars re re2 p nentries (nremoved 0)
  5057. last txt what)
  5058. ;; Find beginning and end of region to sort
  5059. (cond
  5060. ((org-region-active-p)
  5061. ;; we will sort the region
  5062. (setq end (region-end)
  5063. what "region")
  5064. (goto-char (region-beginning))
  5065. (if (not (org-on-heading-p)) (outline-next-heading))
  5066. (setq start (point)))
  5067. ((or (org-on-heading-p)
  5068. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5069. ;; we will sort the children of the current headline
  5070. (org-back-to-heading)
  5071. (setq start (point) end (org-end-of-subtree) what "children")
  5072. (goto-char start)
  5073. (show-subtree)
  5074. (outline-next-heading))
  5075. (t
  5076. ;; we will sort the top-level entries in this file
  5077. (goto-char (point-min))
  5078. (or (org-on-heading-p) (outline-next-heading))
  5079. (setq start (point) end (point-max) what "top-level")
  5080. (goto-char start)
  5081. (show-all)))
  5082. (setq beg (point))
  5083. (if (>= (point) end) (error "Nothing to sort"))
  5084. (looking-at "\\(\\*+\\)")
  5085. (setq stars (match-string 1)
  5086. re (concat "^" (regexp-quote stars) " +")
  5087. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5088. txt (buffer-substring beg end))
  5089. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5090. (if (and (not (equal stars "*")) (string-match re2 txt))
  5091. (error "Region to sort contains a level above the first entry"))
  5092. ;; Make a list that can be sorted.
  5093. ;; The car is the string for comparison, the cdr is the subtree
  5094. (message "Sorting entries...")
  5095. (setq entries
  5096. (mapcar
  5097. (lambda (x)
  5098. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  5099. (cons (match-string 0 x) x))
  5100. (org-split-string txt re)))
  5101. ;; Sort the list
  5102. (save-excursion
  5103. (goto-char start)
  5104. (setq entries (org-do-sort entries what with-case sorting-type)))
  5105. ;; Delete the old stuff
  5106. (goto-char beg)
  5107. (kill-region beg end)
  5108. (setq nentries (length entries))
  5109. ;; Insert the sorted entries, and remove duplicates if this is required
  5110. (while (setq p (pop entries))
  5111. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  5112. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  5113. (insert stars " " (cdr p))))
  5114. (goto-char start)
  5115. (message "Sorting entries...done (%d entries%s)"
  5116. nentries
  5117. (if unique (format ", %d duplicates removed" nremoved) ""))))
  5118. (defun org-do-sort (table what &optional with-case sorting-type)
  5119. "Sort TABLE of WHAT according to SORTING-TYPE.
  5120. The user will be prompted for the SORTING-TYPE if the call to this
  5121. function does not specify it. WHAT is only for the prompt, to indicate
  5122. what is being sorted. The sorting key will be extracted from
  5123. the car of the elements of the table.
  5124. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5125. (unless sorting-type
  5126. (message
  5127. "Sort %s: [a]lphabetically [n]umerically [t]ime. A/N/T means reversed:"
  5128. what)
  5129. (setq sorting-type (read-char-exclusive)))
  5130. (let ((dcst (downcase sorting-type))
  5131. extractfun comparefun)
  5132. ;; Define the appropriate functions
  5133. (cond
  5134. ((= dcst ?n)
  5135. (setq extractfun 'string-to-number
  5136. comparefun (if (= dcst sorting-type) '< '>)))
  5137. ((= dcst ?a)
  5138. (setq extractfun (if with-case 'identity 'downcase)
  5139. comparefun (if (= dcst sorting-type)
  5140. 'string<
  5141. (lambda (a b) (and (not (string< a b))
  5142. (not (string= a b)))))))
  5143. ((= dcst ?t)
  5144. (setq extractfun
  5145. (lambda (x)
  5146. (if (string-match org-ts-regexp x)
  5147. (time-to-seconds
  5148. (org-time-string-to-time (match-string 0 x)))
  5149. 0))
  5150. comparefun (if (= dcst sorting-type) '< '>)))
  5151. (t (error "Invalid sorting type `%c'" sorting-type)))
  5152. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5153. table)
  5154. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5155. ;;;; Plain list items, including checkboxes
  5156. ;;; Plain list items
  5157. (defun org-at-item-p ()
  5158. "Is point in a line starting a hand-formatted item?"
  5159. (let ((llt org-plain-list-ordered-item-terminator))
  5160. (save-excursion
  5161. (goto-char (point-at-bol))
  5162. (looking-at
  5163. (cond
  5164. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5165. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5166. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5167. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5168. (defun org-in-item-p ()
  5169. "It the cursor inside a plain list item.
  5170. Does not have to be the first line."
  5171. (save-excursion
  5172. (condition-case nil
  5173. (progn
  5174. (org-beginning-of-item)
  5175. (org-at-item-p)
  5176. t)
  5177. (error nil))))
  5178. (defun org-insert-item (&optional checkbox)
  5179. "Insert a new item at the current level.
  5180. Return t when things worked, nil when we are not in an item."
  5181. (when (save-excursion
  5182. (condition-case nil
  5183. (progn
  5184. (org-beginning-of-item)
  5185. (org-at-item-p)
  5186. (if (org-invisible-p) (error "Invisible item"))
  5187. t)
  5188. (error nil)))
  5189. (let* ((bul (match-string 0))
  5190. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5191. (match-end 0)))
  5192. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5193. pos)
  5194. (cond
  5195. ((and (org-at-item-p) (<= (point) eow))
  5196. ;; before the bullet
  5197. (beginning-of-line 1)
  5198. (open-line (if blank 2 1)))
  5199. ((<= (point) eow)
  5200. (beginning-of-line 1))
  5201. (t (newline (if blank 2 1))))
  5202. (insert bul (if checkbox "[ ]" ""))
  5203. (just-one-space)
  5204. (setq pos (point))
  5205. (end-of-line 1)
  5206. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5207. (org-maybe-renumber-ordered-list)
  5208. (and checkbox (org-update-checkbox-count-maybe))
  5209. t))
  5210. ;;; Checkboxes
  5211. (defun org-at-item-checkbox-p ()
  5212. "Is point at a line starting a plain-list item with a checklet?"
  5213. (and (org-at-item-p)
  5214. (save-excursion
  5215. (goto-char (match-end 0))
  5216. (skip-chars-forward " \t")
  5217. (looking-at "\\[[- X]\\]"))))
  5218. (defun org-toggle-checkbox (&optional arg)
  5219. "Toggle the checkbox in the current line."
  5220. (interactive "P")
  5221. (catch 'exit
  5222. (let (beg end status (firstnew 'unknown))
  5223. (cond
  5224. ((org-region-active-p)
  5225. (setq beg (region-beginning) end (region-end)))
  5226. ((org-on-heading-p)
  5227. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5228. ((org-at-item-checkbox-p)
  5229. (save-excursion
  5230. (replace-match
  5231. (cond (arg "[-]")
  5232. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5233. (t "[ ]"))
  5234. t t))
  5235. (throw 'exit t))
  5236. (t (error "Not at a checkbox or heading, and no active region")))
  5237. (save-excursion
  5238. (goto-char beg)
  5239. (while (< (point) end)
  5240. (when (org-at-item-checkbox-p)
  5241. (setq status (equal (match-string 0) "[X]"))
  5242. (when (eq firstnew 'unknown)
  5243. (setq firstnew (not status)))
  5244. (replace-match
  5245. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5246. (beginning-of-line 2)))))
  5247. (org-update-checkbox-count-maybe))
  5248. (defun org-update-checkbox-count-maybe ()
  5249. "Update checkbox statistics unless turned off by user."
  5250. (when org-provide-checkbox-statistics
  5251. (org-update-checkbox-count)))
  5252. (defun org-update-checkbox-count (&optional all)
  5253. "Update the checkbox statistics in the current section.
  5254. This will find all statistic cookies like [57%] and [6/12] and update them
  5255. with the current numbers. With optional prefix argument ALL, do this for
  5256. the whole buffer."
  5257. (interactive "P")
  5258. (save-excursion
  5259. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5260. (beg (condition-case nil
  5261. (progn (outline-back-to-heading) (point))
  5262. (error (point-min))))
  5263. (end (move-marker (make-marker)
  5264. (progn (outline-next-heading) (point))))
  5265. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5266. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5267. b1 e1 f1 c-on c-off lim (cstat 0))
  5268. (when all
  5269. (goto-char (point-min))
  5270. (outline-next-heading)
  5271. (setq beg (point) end (point-max)))
  5272. (goto-char beg)
  5273. (while (re-search-forward re end t)
  5274. (setq cstat (1+ cstat)
  5275. b1 (match-beginning 0)
  5276. e1 (match-end 0)
  5277. f1 (match-beginning 1)
  5278. lim (cond
  5279. ((org-on-heading-p) (outline-next-heading) (point))
  5280. ((org-at-item-p) (org-end-of-item) (point))
  5281. (t nil))
  5282. c-on 0 c-off 0)
  5283. (goto-char e1)
  5284. (when lim
  5285. (while (re-search-forward re-box lim t)
  5286. (if (member (match-string 2) '("[ ]" "[-]"))
  5287. (setq c-off (1+ c-off))
  5288. (setq c-on (1+ c-on))))
  5289. ; (delete-region b1 e1)
  5290. (goto-char b1)
  5291. (insert (if f1
  5292. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5293. (format "[%d/%d]" c-on (+ c-on c-off))))
  5294. (and (looking-at "\\[.*?\\]")
  5295. (replace-match ""))))
  5296. (when (interactive-p)
  5297. (message "Checkbox satistics updated %s (%d places)"
  5298. (if all "in entire file" "in current outline entry") cstat)))))
  5299. (defun org-get-checkbox-statistics-face ()
  5300. "Select the face for checkbox statistics.
  5301. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5302. it will be `org-todo'."
  5303. (if (match-end 1)
  5304. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5305. (if (and (> (match-end 2) (match-beginning 2))
  5306. (equal (match-string 2) (match-string 3)))
  5307. 'org-done
  5308. 'org-todo)))
  5309. (defun org-get-indentation (&optional line)
  5310. "Get the indentation of the current line, interpreting tabs.
  5311. When LINE is given, assume it represents a line and compute its indentation."
  5312. (if line
  5313. (if (string-match "^ *" (org-remove-tabs line))
  5314. (match-end 0))
  5315. (save-excursion
  5316. (beginning-of-line 1)
  5317. (skip-chars-forward " \t")
  5318. (current-column))))
  5319. (defun org-remove-tabs (s &optional width)
  5320. "Replace tabulators in S with spaces.
  5321. Assumes that s is a single line, starting in column 0."
  5322. (setq width (or width tab-width))
  5323. (while (string-match "\t" s)
  5324. (setq s (replace-match
  5325. (make-string
  5326. (- (* width (/ (+ (match-beginning 0) width) width))
  5327. (match-beginning 0)) ?\ )
  5328. t t s)))
  5329. s)
  5330. (defun org-fix-indentation (line ind)
  5331. "Fix indentation in LINE.
  5332. IND is a cons cell with target and minimum indentation.
  5333. If the current indenation in LINE is smaller than the minimum,
  5334. leave it alone. If it is larger than ind, set it to the target."
  5335. (let* ((l (org-remove-tabs line))
  5336. (i (org-get-indentation l))
  5337. (i1 (car ind)) (i2 (cdr ind)))
  5338. (if (>= i i2) (setq l (substring line i2)))
  5339. (if (> i1 0)
  5340. (concat (make-string i1 ?\ ) l)
  5341. l)))
  5342. (defcustom org-empty-line-terminates-plain-lists nil
  5343. "Non-nil means, an empty line ends all plain list levels.
  5344. When nil, empty lines are part of the preceeding item."
  5345. :group 'org-plain-lists
  5346. :type 'boolean)
  5347. (defun org-beginning-of-item ()
  5348. "Go to the beginning of the current hand-formatted item.
  5349. If the cursor is not in an item, throw an error."
  5350. (interactive)
  5351. (let ((pos (point))
  5352. (limit (save-excursion
  5353. (condition-case nil
  5354. (progn
  5355. (org-back-to-heading)
  5356. (beginning-of-line 2) (point))
  5357. (error (point-min)))))
  5358. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5359. ind ind1)
  5360. (if (org-at-item-p)
  5361. (beginning-of-line 1)
  5362. (beginning-of-line 1)
  5363. (skip-chars-forward " \t")
  5364. (setq ind (current-column))
  5365. (if (catch 'exit
  5366. (while t
  5367. (beginning-of-line 0)
  5368. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5369. (if (looking-at "[ \t]*$")
  5370. (setq ind1 ind-empty)
  5371. (skip-chars-forward " \t")
  5372. (setq ind1 (current-column)))
  5373. (if (< ind1 ind)
  5374. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5375. nil
  5376. (goto-char pos)
  5377. (error "Not in an item")))))
  5378. (defun org-end-of-item ()
  5379. "Go to the end of the current hand-formatted item.
  5380. If the cursor is not in an item, throw an error."
  5381. (interactive)
  5382. (let* ((pos (point))
  5383. ind1
  5384. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5385. (limit (save-excursion (outline-next-heading) (point)))
  5386. (ind (save-excursion
  5387. (org-beginning-of-item)
  5388. (skip-chars-forward " \t")
  5389. (current-column)))
  5390. (end (catch 'exit
  5391. (while t
  5392. (beginning-of-line 2)
  5393. (if (eobp) (throw 'exit (point)))
  5394. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5395. (if (looking-at "[ \t]*$")
  5396. (setq ind1 ind-empty)
  5397. (skip-chars-forward " \t")
  5398. (setq ind1 (current-column)))
  5399. (if (<= ind1 ind)
  5400. (throw 'exit (point-at-bol)))))))
  5401. (if end
  5402. (goto-char end)
  5403. (goto-char pos)
  5404. (error "Not in an item"))))
  5405. (defun org-next-item ()
  5406. "Move to the beginning of the next item in the current plain list.
  5407. Error if not at a plain list, or if this is the last item in the list."
  5408. (interactive)
  5409. (let (ind ind1 (pos (point)))
  5410. (org-beginning-of-item)
  5411. (setq ind (org-get-indentation))
  5412. (org-end-of-item)
  5413. (setq ind1 (org-get-indentation))
  5414. (unless (and (org-at-item-p) (= ind ind1))
  5415. (goto-char pos)
  5416. (error "On last item"))))
  5417. (defun org-previous-item ()
  5418. "Move to the beginning of the previous item in the current plain list.
  5419. Error if not at a plain list, or if this is the first item in the list."
  5420. (interactive)
  5421. (let (beg ind ind1 (pos (point)))
  5422. (org-beginning-of-item)
  5423. (setq beg (point))
  5424. (setq ind (org-get-indentation))
  5425. (goto-char beg)
  5426. (catch 'exit
  5427. (while t
  5428. (beginning-of-line 0)
  5429. (if (looking-at "[ \t]*$")
  5430. nil
  5431. (if (<= (setq ind1 (org-get-indentation)) ind)
  5432. (throw 'exit t)))))
  5433. (condition-case nil
  5434. (if (or (not (org-at-item-p))
  5435. (< ind1 (1- ind)))
  5436. (error "")
  5437. (org-beginning-of-item))
  5438. (error (goto-char pos)
  5439. (error "On first item")))))
  5440. (defun org-move-item-down ()
  5441. "Move the plain list item at point down, i.e. swap with following item.
  5442. Subitems (items with larger indentation) are considered part of the item,
  5443. so this really moves item trees."
  5444. (interactive)
  5445. (let (beg end ind ind1 (pos (point)) txt)
  5446. (org-beginning-of-item)
  5447. (setq beg (point))
  5448. (setq ind (org-get-indentation))
  5449. (org-end-of-item)
  5450. (setq end (point))
  5451. (setq ind1 (org-get-indentation))
  5452. (if (and (org-at-item-p) (= ind ind1))
  5453. (progn
  5454. (org-end-of-item)
  5455. (setq txt (buffer-substring beg end))
  5456. (save-excursion
  5457. (delete-region beg end))
  5458. (setq pos (point))
  5459. (insert txt)
  5460. (goto-char pos)
  5461. (org-maybe-renumber-ordered-list))
  5462. (goto-char pos)
  5463. (error "Cannot move this item further down"))))
  5464. (defun org-move-item-up (arg)
  5465. "Move the plain list item at point up, i.e. swap with previous item.
  5466. Subitems (items with larger indentation) are considered part of the item,
  5467. so this really moves item trees."
  5468. (interactive "p")
  5469. (let (beg end ind ind1 (pos (point)) txt)
  5470. (org-beginning-of-item)
  5471. (setq beg (point))
  5472. (setq ind (org-get-indentation))
  5473. (org-end-of-item)
  5474. (setq end (point))
  5475. (goto-char beg)
  5476. (catch 'exit
  5477. (while t
  5478. (beginning-of-line 0)
  5479. (if (looking-at "[ \t]*$")
  5480. (if org-empty-line-terminates-plain-lists
  5481. (progn
  5482. (goto-char pos)
  5483. (error "Cannot move this item further up"))
  5484. nil)
  5485. (if (<= (setq ind1 (org-get-indentation)) ind)
  5486. (throw 'exit t)))))
  5487. (condition-case nil
  5488. (org-beginning-of-item)
  5489. (error (goto-char beg)
  5490. (error "Cannot move this item further up")))
  5491. (setq ind1 (org-get-indentation))
  5492. (if (and (org-at-item-p) (= ind ind1))
  5493. (progn
  5494. (setq txt (buffer-substring beg end))
  5495. (save-excursion
  5496. (delete-region beg end))
  5497. (setq pos (point))
  5498. (insert txt)
  5499. (goto-char pos)
  5500. (org-maybe-renumber-ordered-list))
  5501. (goto-char pos)
  5502. (error "Cannot move this item further up"))))
  5503. (defun org-maybe-renumber-ordered-list ()
  5504. "Renumber the ordered list at point if setup allows it.
  5505. This tests the user option `org-auto-renumber-ordered-lists' before
  5506. doing the renumbering."
  5507. (interactive)
  5508. (when (and org-auto-renumber-ordered-lists
  5509. (org-at-item-p))
  5510. (if (match-beginning 3)
  5511. (org-renumber-ordered-list 1)
  5512. (org-fix-bullet-type 1))))
  5513. (defun org-maybe-renumber-ordered-list-safe ()
  5514. (condition-case nil
  5515. (save-excursion
  5516. (org-maybe-renumber-ordered-list))
  5517. (error nil)))
  5518. (defun org-cycle-list-bullet (&optional which)
  5519. "Cycle through the different itemize/enumerate bullets.
  5520. This cycle the entire list level through the sequence:
  5521. `-' -> `+' -> `*' -> `1.' -> `1)'
  5522. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5523. 0 meand `-', 1 means `+' etc."
  5524. (interactive "P")
  5525. (org-preserve-lc
  5526. (org-beginning-of-item-list)
  5527. (org-at-item-p)
  5528. (beginning-of-line 1)
  5529. (let ((current (match-string 0)) new)
  5530. (setq new (cond
  5531. ((and which (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5532. ((string-match "-" current) "+")
  5533. ((string-match "\\+" current)
  5534. (if (looking-at "\\S-") "1." "*"))
  5535. ((string-match "\\*" current) "1.")
  5536. ((string-match "\\." current) "1)")
  5537. ((string-match ")" current) "-")
  5538. (t (error "This should not happen"))))
  5539. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5540. (org-fix-bullet-type 1)
  5541. (org-maybe-renumber-ordered-list))))
  5542. (defun org-get-string-indentation (s)
  5543. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5544. (let ((n -1) (i 0) (w tab-width) c)
  5545. (catch 'exit
  5546. (while (< (setq n (1+ n)) (length s))
  5547. (setq c (aref s n))
  5548. (cond ((= c ?\ ) (setq i (1+ i)))
  5549. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5550. (t (throw 'exit t)))))
  5551. i))
  5552. (defun org-renumber-ordered-list (arg)
  5553. "Renumber an ordered plain list.
  5554. Cursor needs to be in the first line of an item, the line that starts
  5555. with something like \"1.\" or \"2)\"."
  5556. (interactive "p")
  5557. (unless (and (org-at-item-p)
  5558. (match-beginning 3))
  5559. (error "This is not an ordered list"))
  5560. (let ((line (org-current-line))
  5561. (col (current-column))
  5562. (ind (org-get-string-indentation
  5563. (buffer-substring (point-at-bol) (match-beginning 3))))
  5564. ;; (term (substring (match-string 3) -1))
  5565. ind1 (n (1- arg))
  5566. fmt)
  5567. ;; find where this list begins
  5568. (org-beginning-of-item-list)
  5569. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5570. (setq fmt (concat "%d" (match-string 1)))
  5571. (beginning-of-line 0)
  5572. ;; walk forward and replace these numbers
  5573. (catch 'exit
  5574. (while t
  5575. (catch 'next
  5576. (beginning-of-line 2)
  5577. (if (eobp) (throw 'exit nil))
  5578. (if (looking-at "[ \t]*$") (throw 'next nil))
  5579. (skip-chars-forward " \t") (setq ind1 (current-column))
  5580. (if (> ind1 ind) (throw 'next t))
  5581. (if (< ind1 ind) (throw 'exit t))
  5582. (if (not (org-at-item-p)) (throw 'exit nil))
  5583. (delete-region (match-beginning 2) (match-end 2))
  5584. (goto-char (match-beginning 2))
  5585. (insert (format fmt (setq n (1+ n)))))))
  5586. (goto-line line)
  5587. (move-to-column col)))
  5588. (defun org-fix-bullet-type (arg)
  5589. "Make sure all items in this list have the same bullet."
  5590. (interactive "p")
  5591. (unless (org-at-item-p) (error "This is not a list"))
  5592. (let ((line (org-current-line))
  5593. (col (current-column))
  5594. (ind (current-indentation))
  5595. ind1 bullet)
  5596. ;; find where this list begins
  5597. (org-beginning-of-item-list)
  5598. (beginning-of-line 1)
  5599. ;; find out what the bullet type is
  5600. (looking-at "[ \t]*\\(\\S-+\\)")
  5601. (setq bullet (match-string 1))
  5602. ;; walk forward and replace these numbers
  5603. (beginning-of-line 0)
  5604. (catch 'exit
  5605. (while t
  5606. (catch 'next
  5607. (beginning-of-line 2)
  5608. (if (eobp) (throw 'exit nil))
  5609. (if (looking-at "[ \t]*$") (throw 'next nil))
  5610. (skip-chars-forward " \t") (setq ind1 (current-column))
  5611. (if (> ind1 ind) (throw 'next t))
  5612. (if (< ind1 ind) (throw 'exit t))
  5613. (if (not (org-at-item-p)) (throw 'exit nil))
  5614. (skip-chars-forward " \t")
  5615. (looking-at "\\S-+")
  5616. (replace-match bullet))))
  5617. (goto-line line)
  5618. (move-to-column col)
  5619. (if (string-match "[0-9]" bullet)
  5620. (org-renumber-ordered-list 1))))
  5621. (defun org-beginning-of-item-list ()
  5622. "Go to the beginning of the current item list.
  5623. I.e. to the first item in this list."
  5624. (interactive)
  5625. (org-beginning-of-item)
  5626. (let ((pos (point-at-bol))
  5627. (ind (org-get-indentation))
  5628. ind1)
  5629. ;; find where this list begins
  5630. (catch 'exit
  5631. (while t
  5632. (catch 'next
  5633. (beginning-of-line 0)
  5634. (if (looking-at "[ \t]*$")
  5635. (throw (if (bobp) 'exit 'next) t))
  5636. (skip-chars-forward " \t") (setq ind1 (current-column))
  5637. (if (or (< ind1 ind)
  5638. (and (= ind1 ind)
  5639. (not (org-at-item-p)))
  5640. (bobp))
  5641. (throw 'exit t)
  5642. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5643. (goto-char pos)))
  5644. (defvar org-last-indent-begin-marker (make-marker))
  5645. (defvar org-last-indent-end-marker (make-marker))
  5646. (defun org-outdent-item (arg)
  5647. "Outdent a local list item."
  5648. (interactive "p")
  5649. (org-indent-item (- arg)))
  5650. (defun org-indent-item (arg)
  5651. "Indent a local list item."
  5652. (interactive "p")
  5653. (unless (org-at-item-p)
  5654. (error "Not on an item"))
  5655. (save-excursion
  5656. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5657. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5658. (setq beg org-last-indent-begin-marker
  5659. end org-last-indent-end-marker)
  5660. (org-beginning-of-item)
  5661. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5662. (org-end-of-item)
  5663. (setq end (move-marker org-last-indent-end-marker (point))))
  5664. (goto-char beg)
  5665. (setq tmp (org-item-indent-positions)
  5666. ind (car tmp)
  5667. ind-down (nth 2 tmp)
  5668. ind-up (nth 1 tmp)
  5669. delta (if (> arg 0)
  5670. (if ind-down (- ind-down ind) 2)
  5671. (if ind-up (- ind-up ind) -2)))
  5672. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5673. (while (< (point) end)
  5674. (beginning-of-line 1)
  5675. (skip-chars-forward " \t") (setq ind1 (current-column))
  5676. (delete-region (point-at-bol) (point))
  5677. (or (eolp) (indent-to-column (+ ind1 delta)))
  5678. (beginning-of-line 2))))
  5679. (org-maybe-renumber-ordered-list-safe)
  5680. (save-excursion
  5681. (beginning-of-line 0)
  5682. (condition-case nil (org-beginning-of-item) (error nil))
  5683. (org-maybe-renumber-ordered-list-safe)))
  5684. (defun org-item-indent-positions ()
  5685. "Assumes cursor in item line. FIXME"
  5686. (let* ((bolpos (point-at-bol))
  5687. (ind (org-get-indentation))
  5688. ind-down ind-up pos)
  5689. (save-excursion
  5690. (org-beginning-of-item-list)
  5691. (skip-chars-backward "\n\r \t")
  5692. (when (org-in-item-p)
  5693. (org-beginning-of-item)
  5694. (setq ind-up (org-get-indentation))))
  5695. (setq pos (point))
  5696. (save-excursion
  5697. (cond
  5698. ((and (condition-case nil (progn (org-previous-item) t)
  5699. (error nil))
  5700. (or (forward-char 1) t)
  5701. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5702. (setq ind-down (org-get-indentation)))
  5703. ((and (goto-char pos)
  5704. (org-at-item-p))
  5705. (goto-char (match-end 0))
  5706. (skip-chars-forward " \t")
  5707. (setq ind-down (current-column)))))
  5708. (list ind ind-up ind-down)))
  5709. ;;; The orgstruct minor mode
  5710. ;; Define a minor mode which can be used in other modes in order to
  5711. ;; integrate the org-mode structure editing commands.
  5712. ;; This is really a hack, because the org-mode structure commands use
  5713. ;; keys which normally belong to the major mode. Here is how it
  5714. ;; works: The minor mode defines all the keys necessary to operate the
  5715. ;; structure commands, but wraps the commands into a function which
  5716. ;; tests if the cursor is currently at a headline or a plain list
  5717. ;; item. If that is the case, the structure command is used,
  5718. ;; temporarily setting many Org-mode variables like regular
  5719. ;; expressions for filling etc. However, when any of those keys is
  5720. ;; used at a different location, function uses `key-binding' to look
  5721. ;; up if the key has an associated command in another currently active
  5722. ;; keymap (minor modes, major mode, global), and executes that
  5723. ;; command. There might be problems if any of the keys is otherwise
  5724. ;; used as a prefix key.
  5725. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5726. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5727. ;; addresses this by checking explicitly for both bindings.
  5728. (defvar orgstruct-mode-map (make-sparse-keymap)
  5729. "Keymap for the minor `orgstruct-mode'.")
  5730. ;;;###autoload
  5731. (define-minor-mode orgstruct-mode
  5732. "Toggle the minor more `orgstruct-mode'.
  5733. This mode is for using Org-mode structure commands in other modes.
  5734. The following key behave as if Org-mode was active, if the cursor
  5735. is on a headline, or on a plain list item (both in the definition
  5736. of Org-mode).
  5737. M-up Move entry/item up
  5738. M-down Move entry/item down
  5739. M-left Promote
  5740. M-right Demote
  5741. M-S-up Move entry/item up
  5742. M-S-down Move entry/item down
  5743. M-S-left Promote subtree
  5744. M-S-right Demote subtree
  5745. M-q Fill paragraph and items like in Org-mode
  5746. C-c ^ Sort entries
  5747. C-c - Cycle list bullet
  5748. TAB Cycle item visibility
  5749. M-RET Insert new heading/item
  5750. S-M-RET Insert new TODO heading / Chekbox item
  5751. C-c C-c Set tags / toggle checkbox"
  5752. nil " OrgStruct" nil
  5753. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5754. ;;;###autoload
  5755. (defun turn-on-orgstruct ()
  5756. "Unconditionally turn on `orgstruct-mode'."
  5757. (orgstruct-mode 1))
  5758. (defun orgstruct-error ()
  5759. "Error when there is no default binding for a structure key."
  5760. (interactive)
  5761. (error "This key is has no function outside structure elements"))
  5762. (defvar org-local-vars nil
  5763. "List of local variables, for use by `orgstruct-mode'")
  5764. (defun orgstruct-setup ()
  5765. "Setup orgstruct keymaps."
  5766. (let ((nfunc 0)
  5767. (bindings
  5768. (list
  5769. '([(meta up)] org-metaup)
  5770. '([(meta down)] org-metadown)
  5771. '([(meta left)] org-metaleft)
  5772. '([(meta right)] org-metaright)
  5773. '([(meta shift up)] org-shiftmetaup)
  5774. '([(meta shift down)] org-shiftmetadown)
  5775. '([(meta shift left)] org-shiftmetaleft)
  5776. '([(meta shift right)] org-shiftmetaright)
  5777. '([(shift up)] org-shiftup)
  5778. '([(shift down)] org-shiftdown)
  5779. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5780. '("\M-q" fill-paragraph)
  5781. '("\C-c^" org-sort)
  5782. '("\C-c-" org-cycle-list-bullet)))
  5783. elt key fun cmd)
  5784. (while (setq elt (pop bindings))
  5785. (setq nfunc (1+ nfunc))
  5786. (setq key (org-key (car elt))
  5787. fun (nth 1 elt)
  5788. cmd (orgstruct-make-binding fun nfunc key))
  5789. (org-defkey orgstruct-mode-map key cmd))
  5790. ;; Special treatment needed for TAB and RET
  5791. (org-defkey orgstruct-mode-map [(tab)]
  5792. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5793. (org-defkey orgstruct-mode-map "\C-i"
  5794. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5795. (org-defkey orgstruct-mode-map "\M-\C-m"
  5796. (orgstruct-make-binding 'org-insert-heading 105
  5797. "\M-\C-m" [(meta return)]))
  5798. (org-defkey orgstruct-mode-map [(meta return)]
  5799. (orgstruct-make-binding 'org-insert-heading 106
  5800. [(meta return)] "\M-\C-m"))
  5801. (org-defkey orgstruct-mode-map [(shift meta return)]
  5802. (orgstruct-make-binding 'org-insert-todo-heading 107
  5803. [(meta return)] "\M-\C-m"))
  5804. (unless org-local-vars
  5805. (setq org-local-vars (org-get-local-variables)))
  5806. t))
  5807. (defun orgstruct-make-binding (fun n &rest keys)
  5808. "Create a function for binding in the structure minor mode.
  5809. FUN is the command to call inside a table. N is used to create a unique
  5810. command name. KEYS are keys that should be checked in for a command
  5811. to execute outside of tables."
  5812. (eval
  5813. (list 'defun
  5814. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5815. '(arg)
  5816. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5817. "Outside of structure, run the binding of `"
  5818. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5819. "'.")
  5820. '(interactive "p")
  5821. (list 'if
  5822. '(org-context-p 'headline 'item)
  5823. (list 'org-run-like-in-org-mode (list 'quote fun))
  5824. (list 'let '(orgstruct-mode)
  5825. (list 'call-interactively
  5826. (append '(or)
  5827. (mapcar (lambda (k)
  5828. (list 'key-binding k))
  5829. keys)
  5830. '('orgstruct-error))))))))
  5831. (defun org-context-p (&rest contexts)
  5832. "FIXME:"
  5833. (let ((pos (point)))
  5834. (goto-char (point-at-bol))
  5835. (prog1 (or (and (memq 'table contexts)
  5836. (looking-at "[ \t]*|"))
  5837. (and (memq 'headline contexts)
  5838. (looking-at "\\*+"))
  5839. (and (memq 'item contexts)
  5840. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5841. (goto-char pos))))
  5842. (defun org-get-local-variables ()
  5843. "Return a list of all local variables in an org-mode buffer."
  5844. (let (varlist)
  5845. (with-current-buffer (get-buffer-create "*Org tmp*")
  5846. (erase-buffer)
  5847. (org-mode)
  5848. (setq varlist (buffer-local-variables)))
  5849. (kill-buffer "*Org tmp*")
  5850. (delq nil
  5851. (mapcar
  5852. (lambda (x)
  5853. (setq x
  5854. (if (symbolp x)
  5855. (list x)
  5856. (list (car x) (list 'quote (cdr x)))))
  5857. (if (string-match
  5858. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5859. (symbol-name (car x)))
  5860. x nil))
  5861. varlist))))
  5862. ;;;###autoload
  5863. (defun org-run-like-in-org-mode (cmd)
  5864. (unless org-local-vars
  5865. (setq org-local-vars (org-get-local-variables)))
  5866. (eval (list 'let org-local-vars
  5867. (list 'call-interactively (list 'quote cmd)))))
  5868. ;;;; Archiving
  5869. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5870. (defun org-archive-subtree (&optional find-done)
  5871. "Move the current subtree to the archive.
  5872. The archive can be a certain top-level heading in the current file, or in
  5873. a different file. The tree will be moved to that location, the subtree
  5874. heading be marked DONE, and the current time will be added.
  5875. When called with prefix argument FIND-DONE, find whole trees without any
  5876. open TODO items and archive them (after getting confirmation from the user).
  5877. If the cursor is not at a headline when this comand is called, try all level
  5878. 1 trees. If the cursor is on a headline, only try the direct children of
  5879. this heading."
  5880. (interactive "P")
  5881. (if find-done
  5882. (org-archive-all-done)
  5883. ;; Save all relevant TODO keyword-relatex variables
  5884. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5885. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5886. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5887. (tr-org-done-keywords org-done-keywords)
  5888. (tr-org-todo-regexp org-todo-regexp)
  5889. (tr-org-todo-line-regexp org-todo-line-regexp)
  5890. (tr-org-odd-levels-only org-odd-levels-only)
  5891. (this-buffer (current-buffer))
  5892. (org-archive-location org-archive-location)
  5893. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5894. file heading buffer level newfile-p)
  5895. ;; Try to find a local archive location
  5896. (save-excursion
  5897. (save-restriction
  5898. (widen)
  5899. (if (or (re-search-backward re nil t) (re-search-forward re nil t))
  5900. (setq org-archive-location (match-string 1)))))
  5901. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5902. (progn
  5903. (setq file (format (match-string 1 org-archive-location)
  5904. (file-name-nondirectory buffer-file-name))
  5905. heading (match-string 2 org-archive-location)))
  5906. (error "Invalid `org-archive-location'"))
  5907. (if (> (length file) 0)
  5908. (setq newfile-p (not (file-exists-p file))
  5909. buffer (find-file-noselect file))
  5910. (setq buffer (current-buffer)))
  5911. (unless buffer
  5912. (error "Cannot access file \"%s\"" file))
  5913. (if (and (> (length heading) 0)
  5914. (string-match "^\\*+" heading))
  5915. (setq level (match-end 0))
  5916. (setq heading nil level 0))
  5917. (save-excursion
  5918. ;; We first only copy, in case something goes wrong
  5919. ;; we need to protect this-command, to avoid kill-region sets it,
  5920. ;; which would lead to duplication of subtrees
  5921. (let (this-command) (org-copy-subtree))
  5922. (set-buffer buffer)
  5923. ;; Enforce org-mode for the archive buffer
  5924. (if (not (org-mode-p))
  5925. ;; Force the mode for future visits.
  5926. (let ((org-insert-mode-line-in-empty-file t)
  5927. (org-inhibit-startup t))
  5928. (call-interactively 'org-mode)))
  5929. (when newfile-p
  5930. (goto-char (point-max))
  5931. (insert (format "\nArchived entries from file %s\n\n"
  5932. (buffer-file-name this-buffer))))
  5933. ;; Force the TODO keywords of the original buffer
  5934. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5935. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5936. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5937. (org-done-keywords tr-org-done-keywords)
  5938. (org-todo-regexp tr-org-todo-regexp)
  5939. (org-todo-line-regexp tr-org-todo-line-regexp)
  5940. (org-odd-levels-only
  5941. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5942. org-odd-levels-only
  5943. tr-org-odd-levels-only)))
  5944. (goto-char (point-min))
  5945. (if heading
  5946. (progn
  5947. (if (re-search-forward
  5948. (concat "^" (regexp-quote heading)
  5949. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5950. nil t)
  5951. (goto-char (match-end 0))
  5952. ;; Heading not found, just insert it at the end
  5953. (goto-char (point-max))
  5954. (or (bolp) (insert "\n"))
  5955. (insert "\n" heading "\n")
  5956. (end-of-line 0))
  5957. ;; Make the subtree visible
  5958. (show-subtree)
  5959. (org-end-of-subtree t)
  5960. (skip-chars-backward " \t\r\n")
  5961. (and (looking-at "[ \t\r\n]*")
  5962. (replace-match "\n\n")))
  5963. ;; No specific heading, just go to end of file.
  5964. (goto-char (point-max)) (insert "\n"))
  5965. ;; Paste
  5966. (org-paste-subtree (org-get-legal-level level 1))
  5967. ;; Mark the entry as done
  5968. (when (and org-archive-mark-done
  5969. (looking-at org-todo-line-regexp)
  5970. (or (not (match-end 2))
  5971. (not (member (match-string 2) org-done-keywords))))
  5972. (let (org-log-done)
  5973. (org-todo
  5974. (car (or (member org-archive-mark-done org-done-keywords)
  5975. org-done-keywords)))))
  5976. ;; Move cursor to right after the TODO keyword
  5977. (when org-archive-stamp-time
  5978. (org-add-planning-info 'archived (org-current-time)))
  5979. ;; Save the buffer, if it is not the same buffer.
  5980. (if (not (eq this-buffer buffer)) (save-buffer))))
  5981. ;; Here we are back in the original buffer. Everything seems to have
  5982. ;; worked. So now cut the tree and finish up.
  5983. (let (this-command) (org-cut-subtree))
  5984. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5985. (message "Subtree archived %s"
  5986. (if (eq this-buffer buffer)
  5987. (concat "under heading: " heading)
  5988. (concat "in file: " (abbreviate-file-name file)))))))
  5989. (defun org-archive-all-done (&optional tag)
  5990. "Archive sublevels of the current tree without open TODO items.
  5991. If the cursor is not on a headline, try all level 1 trees. If
  5992. it is on a headline, try all direct children.
  5993. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5994. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5995. (rea (concat ".*:" org-archive-tag ":"))
  5996. (begm (make-marker))
  5997. (endm (make-marker))
  5998. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5999. "Move subtree to archive (no open TODO items)? "))
  6000. beg end (cntarch 0))
  6001. (if (org-on-heading-p)
  6002. (progn
  6003. (setq re1 (concat "^" (regexp-quote
  6004. (make-string
  6005. (1+ (- (match-end 0) (match-beginning 0)))
  6006. ?*))
  6007. " "))
  6008. (move-marker begm (point))
  6009. (move-marker endm (org-end-of-subtree t)))
  6010. (setq re1 "^* ")
  6011. (move-marker begm (point-min))
  6012. (move-marker endm (point-max)))
  6013. (save-excursion
  6014. (goto-char begm)
  6015. (while (re-search-forward re1 endm t)
  6016. (setq beg (match-beginning 0)
  6017. end (save-excursion (org-end-of-subtree t) (point)))
  6018. (goto-char beg)
  6019. (if (re-search-forward re end t)
  6020. (goto-char end)
  6021. (goto-char beg)
  6022. (if (and (or (not tag) (not (looking-at rea)))
  6023. (y-or-n-p question))
  6024. (progn
  6025. (if tag
  6026. (org-toggle-tag org-archive-tag 'on)
  6027. (org-archive-subtree))
  6028. (setq cntarch (1+ cntarch)))
  6029. (goto-char end)))))
  6030. (message "%d trees archived" cntarch)))
  6031. (defun org-cycle-hide-drawers (state)
  6032. "Re-hide all drawers after a visibility state change."
  6033. (when (not (memq state '(overview folded)))
  6034. (save-excursion
  6035. (let* ((globalp (memq state '(contents all)))
  6036. (beg (if globalp (point-min) (point)))
  6037. (end (if globalp (point-max) (org-end-of-subtree t))))
  6038. (goto-char beg)
  6039. (while (re-search-forward org-drawer-regexp end t)
  6040. (org-flag-drawer t))))))
  6041. (defun org-flag-drawer (flag)
  6042. (save-excursion
  6043. (beginning-of-line 1)
  6044. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6045. (let ((b (match-end 0)))
  6046. (if (re-search-forward
  6047. "^[ \t]*:END:"
  6048. (save-excursion (outline-next-heading) (point)) t)
  6049. (outline-flag-region b (point-at-eol) flag)
  6050. (error ":END: line missing"))))))
  6051. (defun org-cycle-hide-archived-subtrees (state)
  6052. "Re-hide all archived subtrees after a visibility state change."
  6053. (when (and (not org-cycle-open-archived-trees)
  6054. (not (memq state '(overview folded))))
  6055. (save-excursion
  6056. (let* ((globalp (memq state '(contents all)))
  6057. (beg (if globalp (point-min) (point)))
  6058. (end (if globalp (point-max) (org-end-of-subtree t))))
  6059. (org-hide-archived-subtrees beg end)
  6060. (goto-char beg)
  6061. (if (looking-at (concat ".*:" org-archive-tag ":"))
  6062. (message (substitute-command-keys
  6063. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  6064. (defun org-force-cycle-archived ()
  6065. "Cycle subtree even if it is archived."
  6066. (interactive)
  6067. (setq this-command 'org-cycle)
  6068. (let ((org-cycle-open-archived-trees t))
  6069. (call-interactively 'org-cycle)))
  6070. (defun org-hide-archived-subtrees (beg end)
  6071. "Re-hide all archived subtrees after a visibility state change."
  6072. (save-excursion
  6073. (let* ((re (concat ":" org-archive-tag ":")))
  6074. (goto-char beg)
  6075. (while (re-search-forward re end t)
  6076. (and (org-on-heading-p) (hide-subtree))
  6077. (org-end-of-subtree t)))))
  6078. (defun org-toggle-tag (tag &optional onoff)
  6079. "Toggle the tag TAG for the current line.
  6080. If ONOFF is `on' or `off', don't toggle but set to this state."
  6081. (unless (org-on-heading-p t) (error "Not on headling"))
  6082. (let (res current)
  6083. (save-excursion
  6084. (beginning-of-line)
  6085. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  6086. (point-at-eol) t)
  6087. (progn
  6088. (setq current (match-string 1))
  6089. (replace-match ""))
  6090. (setq current ""))
  6091. (setq current (nreverse (org-split-string current ":")))
  6092. (cond
  6093. ((eq onoff 'on)
  6094. (setq res t)
  6095. (or (member tag current) (push tag current)))
  6096. ((eq onoff 'off)
  6097. (or (not (member tag current)) (setq current (delete tag current))))
  6098. (t (if (member tag current)
  6099. (setq current (delete tag current))
  6100. (setq res t)
  6101. (push tag current))))
  6102. (end-of-line 1)
  6103. (when current
  6104. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  6105. (org-set-tags nil t))
  6106. res))
  6107. (defun org-toggle-archive-tag (&optional arg)
  6108. "Toggle the archive tag for the current headline.
  6109. With prefix ARG, check all children of current headline and offer tagging
  6110. the children that do not contain any open TODO items."
  6111. (interactive "P")
  6112. (if arg
  6113. (org-archive-all-done 'tag)
  6114. (let (set)
  6115. (save-excursion
  6116. (org-back-to-heading t)
  6117. (setq set (org-toggle-tag org-archive-tag))
  6118. (when set (hide-subtree)))
  6119. (and set (beginning-of-line 1))
  6120. (message "Subtree %s" (if set "archived" "unarchived")))))
  6121. ;;;; Tables
  6122. ;;; The table editor
  6123. ;; Watch out: Here we are talking about two different kind of tables.
  6124. ;; Most of the code is for the tables created with the Org-mode table editor.
  6125. ;; Sometimes, we talk about tables created and edited with the table.el
  6126. ;; Emacs package. We call the former org-type tables, and the latter
  6127. ;; table.el-type tables.
  6128. (defun org-before-change-function (beg end)
  6129. "Every change indicates that a table might need an update."
  6130. (setq org-table-may-need-update t))
  6131. (defconst org-table-line-regexp "^[ \t]*|"
  6132. "Detects an org-type table line.")
  6133. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  6134. "Detects an org-type table line.")
  6135. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  6136. "Detects a table line marked for automatic recalculation.")
  6137. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  6138. "Detects a table line marked for automatic recalculation.")
  6139. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  6140. "Detects a table line marked for automatic recalculation.")
  6141. (defconst org-table-hline-regexp "^[ \t]*|-"
  6142. "Detects an org-type table hline.")
  6143. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  6144. "Detects a table-type table hline.")
  6145. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  6146. "Detects an org-type or table-type table.")
  6147. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  6148. "Searching from within a table (any type) this finds the first line
  6149. outside the table.")
  6150. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  6151. "Searching from within a table (any type) this finds the first line
  6152. outside the table.")
  6153. (defvar org-table-last-highlighted-reference nil)
  6154. (defvar org-table-formula-history nil)
  6155. (defvar org-table-column-names nil
  6156. "Alist with column names, derived from the `!' line.")
  6157. (defvar org-table-column-name-regexp nil
  6158. "Regular expression matching the current column names.")
  6159. (defvar org-table-local-parameters nil
  6160. "Alist with parameter names, derived from the `$' line.")
  6161. (defvar org-table-named-field-locations nil
  6162. "Alist with locations of named fields.")
  6163. (defvar org-table-current-line-types nil
  6164. "Table row types, non-nil only for the duration of a comand.")
  6165. (defvar org-table-current-begin-line nil
  6166. "Table begin line, non-nil only for the duration of a comand.")
  6167. (defvar org-table-current-begin-pos nil
  6168. "Table begin position, non-nil only for the duration of a comand.")
  6169. (defvar org-table-dlines nil
  6170. "Vector of data line line numbers in the current table.")
  6171. (defvar org-table-hlines nil
  6172. "Vector of hline line numbers in the current table.")
  6173. (defconst org-table-range-regexp
  6174. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6175. ;; 1 2 3 4 5
  6176. "Regular expression for matching ranges in formulas.")
  6177. (defconst org-table-range-regexp2
  6178. (concat
  6179. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6180. "\\.\\."
  6181. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6182. "Match a range for reference display.")
  6183. (defconst org-table-translate-regexp
  6184. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6185. "Match a reference that needs translation, for reference display.")
  6186. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6187. (defun org-table-create-with-table.el ()
  6188. "Use the table.el package to insert a new table.
  6189. If there is already a table at point, convert between Org-mode tables
  6190. and table.el tables."
  6191. (interactive)
  6192. (require 'table)
  6193. (cond
  6194. ((org-at-table.el-p)
  6195. (if (y-or-n-p "Convert table to Org-mode table? ")
  6196. (org-table-convert)))
  6197. ((org-at-table-p)
  6198. (if (y-or-n-p "Convert table to table.el table? ")
  6199. (org-table-convert)))
  6200. (t (call-interactively 'table-insert))))
  6201. (defun org-table-create-or-convert-from-region (arg)
  6202. "Convert region to table, or create an empty table.
  6203. If there is an active region, convert it to a table, using the function
  6204. `org-table-convert-region'.
  6205. If there is no such region, create an empty table with `org-table-create'."
  6206. (interactive "P")
  6207. (if (org-region-active-p)
  6208. (org-table-convert-region (region-beginning) (region-end) arg)
  6209. (org-table-create arg)))
  6210. (defun org-table-create (&optional size)
  6211. "Query for a size and insert a table skeleton.
  6212. SIZE is a string Columns x Rows like for example \"3x2\"."
  6213. (interactive "P")
  6214. (unless size
  6215. (setq size (read-string
  6216. (concat "Table size Columns x Rows [e.g. "
  6217. org-table-default-size "]: ")
  6218. "" nil org-table-default-size)))
  6219. (let* ((pos (point))
  6220. (indent (make-string (current-column) ?\ ))
  6221. (split (org-split-string size " *x *"))
  6222. (rows (string-to-number (nth 1 split)))
  6223. (columns (string-to-number (car split)))
  6224. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6225. "\n")))
  6226. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6227. (point-at-bol) (point)))
  6228. (beginning-of-line 1)
  6229. (newline))
  6230. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6231. (dotimes (i rows) (insert line))
  6232. (goto-char pos)
  6233. (if (> rows 1)
  6234. ;; Insert a hline after the first row.
  6235. (progn
  6236. (end-of-line 1)
  6237. (insert "\n|-")
  6238. (goto-char pos)))
  6239. (org-table-align)))
  6240. (defun org-table-convert-region (beg0 end0 &optional nspace)
  6241. "Convert region to a table.
  6242. The region goes from BEG0 to END0, but these borders will be moved
  6243. slightly, to make sure a beginning of line in the first line is included.
  6244. When NSPACE is non-nil, it indicates the minimum number of spaces that
  6245. separate columns. By default, the function first checks if every line
  6246. contains at lease one TAB. If yes, it assumes that the material is TAB
  6247. separated. If not, it assumes a single space as separator."
  6248. (interactive "rP")
  6249. (let* ((beg (min beg0 end0))
  6250. (end (max beg0 end0))
  6251. (tabsep t)
  6252. re)
  6253. (goto-char beg)
  6254. (beginning-of-line 1)
  6255. (setq beg (move-marker (make-marker) (point)))
  6256. (goto-char end)
  6257. (if (bolp) (backward-char 1) (end-of-line 1))
  6258. (setq end (move-marker (make-marker) (point)))
  6259. ;; Lets see if this is tab-separated material. If every nonempty line
  6260. ;; contains a tab, we will assume that it is tab-separated material
  6261. (if nspace
  6262. (setq tabsep nil)
  6263. (goto-char beg)
  6264. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  6265. (if nspace (setq tabsep nil))
  6266. (if tabsep
  6267. (setq re "^\\|\t")
  6268. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  6269. (max 1 (prefix-numeric-value nspace)))))
  6270. (goto-char beg)
  6271. (while (re-search-forward re end t)
  6272. (replace-match "| " t t))
  6273. (goto-char beg)
  6274. (insert " ")
  6275. (org-table-align)))
  6276. (defun org-table-import (file arg)
  6277. "Import FILE as a table.
  6278. The file is assumed to be tab-separated. Such files can be produced by most
  6279. spreadsheet and database applications. If no tabs (at least one per line)
  6280. are found, lines will be split on whitespace into fields."
  6281. (interactive "f\nP")
  6282. (or (bolp) (newline))
  6283. (let ((beg (point))
  6284. (pm (point-max)))
  6285. (insert-file-contents file)
  6286. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  6287. (defun org-table-export ()
  6288. "Export table as a tab-separated file.
  6289. Such a file can be imported into a spreadsheet program like Excel."
  6290. (interactive)
  6291. (let* ((beg (org-table-begin))
  6292. (end (org-table-end))
  6293. (table (buffer-substring beg end))
  6294. (file (read-file-name "Export table to: "))
  6295. buf)
  6296. (unless (or (not (file-exists-p file))
  6297. (y-or-n-p (format "Overwrite file %s? " file)))
  6298. (error "Abort"))
  6299. (with-current-buffer (find-file-noselect file)
  6300. (setq buf (current-buffer))
  6301. (erase-buffer)
  6302. (fundamental-mode)
  6303. (insert table)
  6304. (goto-char (point-min))
  6305. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  6306. (replace-match "" t t)
  6307. (end-of-line 1))
  6308. (goto-char (point-min))
  6309. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  6310. (replace-match "" t t)
  6311. (goto-char (min (1+ (point)) (point-max))))
  6312. (goto-char (point-min))
  6313. (while (re-search-forward "^-[-+]*$" nil t)
  6314. (replace-match "")
  6315. (if (looking-at "\n")
  6316. (delete-char 1)))
  6317. (goto-char (point-min))
  6318. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  6319. (replace-match "\t" t t))
  6320. (save-buffer))
  6321. (kill-buffer buf)))
  6322. (defvar org-table-aligned-begin-marker (make-marker)
  6323. "Marker at the beginning of the table last aligned.
  6324. Used to check if cursor still is in that table, to minimize realignment.")
  6325. (defvar org-table-aligned-end-marker (make-marker)
  6326. "Marker at the end of the table last aligned.
  6327. Used to check if cursor still is in that table, to minimize realignment.")
  6328. (defvar org-table-last-alignment nil
  6329. "List of flags for flushright alignment, from the last re-alignment.
  6330. This is being used to correctly align a single field after TAB or RET.")
  6331. (defvar org-table-last-column-widths nil
  6332. "List of max width of fields in each column.
  6333. This is being used to correctly align a single field after TAB or RET.")
  6334. (defvar org-table-overlay-coordinates nil
  6335. "Overlay coordinates after each align of a table.")
  6336. (make-variable-buffer-local 'org-table-overlay-coordinates)
  6337. (defvar org-last-recalc-line nil)
  6338. (defconst org-narrow-column-arrow "=>"
  6339. "Used as display property in narrowed table columns.")
  6340. (defun org-table-align ()
  6341. "Align the table at point by aligning all vertical bars."
  6342. (interactive)
  6343. (let* (
  6344. ;; Limits of table
  6345. (beg (org-table-begin))
  6346. (end (org-table-end))
  6347. ;; Current cursor position
  6348. (linepos (org-current-line))
  6349. (colpos (org-table-current-column))
  6350. (winstart (window-start))
  6351. (winstartline (org-current-line (min winstart (1- (point-max)))))
  6352. lines (new "") lengths l typenums ty fields maxfields i
  6353. column
  6354. (indent "") cnt frac
  6355. rfmt hfmt
  6356. (spaces '(1 . 1))
  6357. (sp1 (car spaces))
  6358. (sp2 (cdr spaces))
  6359. (rfmt1 (concat
  6360. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  6361. (hfmt1 (concat
  6362. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  6363. emptystrings links dates narrow fmax f1 len c e)
  6364. (untabify beg end)
  6365. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  6366. ;; Check if we have links or dates
  6367. (goto-char beg)
  6368. (setq links (re-search-forward org-bracket-link-regexp end t))
  6369. (goto-char beg)
  6370. (setq dates (and org-display-custom-times
  6371. (re-search-forward org-ts-regexp-both end t)))
  6372. ;; Make sure the link properties are right
  6373. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  6374. ;; Make sure the date properties are right
  6375. (when dates (goto-char beg) (while (org-activate-dates end)))
  6376. ;; Check if we are narrowing any columns
  6377. (goto-char beg)
  6378. (setq narrow (and org-format-transports-properties-p
  6379. (re-search-forward "<[0-9]+>" end t)))
  6380. ;; Get the rows
  6381. (setq lines (org-split-string
  6382. (buffer-substring beg end) "\n"))
  6383. ;; Store the indentation of the first line
  6384. (if (string-match "^ *" (car lines))
  6385. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  6386. ;; Mark the hlines by setting the corresponding element to nil
  6387. ;; At the same time, we remove trailing space.
  6388. (setq lines (mapcar (lambda (l)
  6389. (if (string-match "^ *|-" l)
  6390. nil
  6391. (if (string-match "[ \t]+$" l)
  6392. (substring l 0 (match-beginning 0))
  6393. l)))
  6394. lines))
  6395. ;; Get the data fields by splitting the lines.
  6396. (setq fields (mapcar
  6397. (lambda (l)
  6398. (org-split-string l " *| *"))
  6399. (delq nil (copy-sequence lines))))
  6400. ;; How many fields in the longest line?
  6401. (condition-case nil
  6402. (setq maxfields (apply 'max (mapcar 'length fields)))
  6403. (error
  6404. (kill-region beg end)
  6405. (org-table-create org-table-default-size)
  6406. (error "Empty table - created default table")))
  6407. ;; A list of empty strings to fill any short rows on output
  6408. (setq emptystrings (make-list maxfields ""))
  6409. ;; Check for special formatting.
  6410. (setq i -1)
  6411. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  6412. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  6413. ;; Check if there is an explicit width specified
  6414. (when narrow
  6415. (setq c column fmax nil)
  6416. (while c
  6417. (setq e (pop c))
  6418. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  6419. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  6420. ;; Find fields that are wider than fmax, and shorten them
  6421. (when fmax
  6422. (loop for xx in column do
  6423. (when (and (stringp xx)
  6424. (> (org-string-width xx) fmax))
  6425. (org-add-props xx nil
  6426. 'help-echo
  6427. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  6428. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  6429. (unless (> f1 1)
  6430. (error "Cannot narrow field starting with wide link \"%s\""
  6431. (match-string 0 xx)))
  6432. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  6433. (add-text-properties (- f1 2) f1
  6434. (list 'display org-narrow-column-arrow)
  6435. xx)))))
  6436. ;; Get the maximum width for each column
  6437. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  6438. ;; Get the fraction of numbers, to decide about alignment of the column
  6439. (setq cnt 0 frac 0.0)
  6440. (loop for x in column do
  6441. (if (equal x "")
  6442. nil
  6443. (setq frac ( / (+ (* frac cnt)
  6444. (if (string-match org-table-number-regexp x) 1 0))
  6445. (setq cnt (1+ cnt))))))
  6446. (push (>= frac org-table-number-fraction) typenums))
  6447. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  6448. ;; Store the alignment of this table, for later editing of single fields
  6449. (setq org-table-last-alignment typenums
  6450. org-table-last-column-widths lengths)
  6451. ;; With invisible characters, `format' does not get the field width right
  6452. ;; So we need to make these fields wide by hand.
  6453. (when links
  6454. (loop for i from 0 upto (1- maxfields) do
  6455. (setq len (nth i lengths))
  6456. (loop for j from 0 upto (1- (length fields)) do
  6457. (setq c (nthcdr i (car (nthcdr j fields))))
  6458. (if (and (stringp (car c))
  6459. (string-match org-bracket-link-regexp (car c))
  6460. (< (org-string-width (car c)) len))
  6461. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  6462. ;; Compute the formats needed for output of the table
  6463. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  6464. (while (setq l (pop lengths))
  6465. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  6466. (setq rfmt (concat rfmt (format rfmt1 ty l))
  6467. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  6468. (setq rfmt (concat rfmt "\n")
  6469. hfmt (concat (substring hfmt 0 -1) "|\n"))
  6470. (setq new (mapconcat
  6471. (lambda (l)
  6472. (if l (apply 'format rfmt
  6473. (append (pop fields) emptystrings))
  6474. hfmt))
  6475. lines ""))
  6476. ;; Replace the old one
  6477. (delete-region beg end)
  6478. (move-marker end nil)
  6479. (move-marker org-table-aligned-begin-marker (point))
  6480. (insert new)
  6481. (move-marker org-table-aligned-end-marker (point))
  6482. (when (and orgtbl-mode (not (org-mode-p)))
  6483. (goto-char org-table-aligned-begin-marker)
  6484. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  6485. ;; Try to move to the old location
  6486. (goto-line winstartline)
  6487. (setq winstart (point-at-bol))
  6488. (goto-line linepos)
  6489. (set-window-start (selected-window) winstart 'noforce)
  6490. (org-table-goto-column colpos)
  6491. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  6492. (setq org-table-may-need-update nil)
  6493. ))
  6494. (defun org-string-width (s)
  6495. "Compute width of string, ignoring invisible characters.
  6496. This ignores character with invisibility property `org-link', and also
  6497. characters with property `org-cwidth', because these will become invisible
  6498. upon the next fontification round."
  6499. (let (b l)
  6500. (when (or (eq t buffer-invisibility-spec)
  6501. (assq 'org-link buffer-invisibility-spec))
  6502. (while (setq b (text-property-any 0 (length s)
  6503. 'invisible 'org-link s))
  6504. (setq s (concat (substring s 0 b)
  6505. (substring s (or (next-single-property-change
  6506. b 'invisible s) (length s)))))))
  6507. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  6508. (setq s (concat (substring s 0 b)
  6509. (substring s (or (next-single-property-change
  6510. b 'org-cwidth s) (length s))))))
  6511. (setq l (string-width s) b -1)
  6512. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  6513. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  6514. l))
  6515. (defun org-table-begin (&optional table-type)
  6516. "Find the beginning of the table and return its position.
  6517. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  6518. (save-excursion
  6519. (if (not (re-search-backward
  6520. (if table-type org-table-any-border-regexp
  6521. org-table-border-regexp)
  6522. nil t))
  6523. (progn (goto-char (point-min)) (point))
  6524. (goto-char (match-beginning 0))
  6525. (beginning-of-line 2)
  6526. (point))))
  6527. (defun org-table-end (&optional table-type)
  6528. "Find the end of the table and return its position.
  6529. With argument TABLE-TYPE, go to the end of a table.el-type table."
  6530. (save-excursion
  6531. (if (not (re-search-forward
  6532. (if table-type org-table-any-border-regexp
  6533. org-table-border-regexp)
  6534. nil t))
  6535. (goto-char (point-max))
  6536. (goto-char (match-beginning 0)))
  6537. (point-marker)))
  6538. (defun org-table-justify-field-maybe (&optional new)
  6539. "Justify the current field, text to left, number to right.
  6540. Optional argument NEW may specify text to replace the current field content."
  6541. (cond
  6542. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  6543. ((org-at-table-hline-p))
  6544. ((and (not new)
  6545. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  6546. (current-buffer)))
  6547. (< (point) org-table-aligned-begin-marker)
  6548. (>= (point) org-table-aligned-end-marker)))
  6549. ;; This is not the same table, force a full re-align
  6550. (setq org-table-may-need-update t))
  6551. (t ;; realign the current field, based on previous full realign
  6552. (let* ((pos (point)) s
  6553. (col (org-table-current-column))
  6554. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  6555. l f n o e)
  6556. (when (> col 0)
  6557. (skip-chars-backward "^|\n")
  6558. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  6559. (progn
  6560. (setq s (match-string 1)
  6561. o (match-string 0)
  6562. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  6563. e (not (= (match-beginning 2) (match-end 2))))
  6564. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  6565. l (if e "|" (setq org-table-may-need-update t) ""))
  6566. n (format f s))
  6567. (if new
  6568. (if (<= (length new) l) ;; FIXME: length -> str-width?
  6569. (setq n (format f new))
  6570. (setq n (concat new "|") org-table-may-need-update t)))
  6571. (or (equal n o)
  6572. (let (org-table-may-need-update)
  6573. (replace-match n t t))))
  6574. (setq org-table-may-need-update t))
  6575. (goto-char pos))))))
  6576. (defun org-table-next-field ()
  6577. "Go to the next field in the current table, creating new lines as needed.
  6578. Before doing so, re-align the table if necessary."
  6579. (interactive)
  6580. (org-table-maybe-eval-formula)
  6581. (org-table-maybe-recalculate-line)
  6582. (if (and org-table-automatic-realign
  6583. org-table-may-need-update)
  6584. (org-table-align))
  6585. (let ((end (org-table-end)))
  6586. (if (org-at-table-hline-p)
  6587. (end-of-line 1))
  6588. (condition-case nil
  6589. (progn
  6590. (re-search-forward "|" end)
  6591. (if (looking-at "[ \t]*$")
  6592. (re-search-forward "|" end))
  6593. (if (and (looking-at "-")
  6594. org-table-tab-jumps-over-hlines
  6595. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  6596. (goto-char (match-beginning 1)))
  6597. (if (looking-at "-")
  6598. (progn
  6599. (beginning-of-line 0)
  6600. (org-table-insert-row 'below))
  6601. (if (looking-at " ") (forward-char 1))))
  6602. (error
  6603. (org-table-insert-row 'below)))))
  6604. (defun org-table-previous-field ()
  6605. "Go to the previous field in the table.
  6606. Before doing so, re-align the table if necessary."
  6607. (interactive)
  6608. (org-table-justify-field-maybe)
  6609. (org-table-maybe-recalculate-line)
  6610. (if (and org-table-automatic-realign
  6611. org-table-may-need-update)
  6612. (org-table-align))
  6613. (if (org-at-table-hline-p)
  6614. (end-of-line 1))
  6615. (re-search-backward "|" (org-table-begin))
  6616. (re-search-backward "|" (org-table-begin))
  6617. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  6618. (re-search-backward "|" (org-table-begin)))
  6619. (if (looking-at "| ?")
  6620. (goto-char (match-end 0))))
  6621. (defun org-table-next-row ()
  6622. "Go to the next row (same column) in the current table.
  6623. Before doing so, re-align the table if necessary."
  6624. (interactive)
  6625. (org-table-maybe-eval-formula)
  6626. (org-table-maybe-recalculate-line)
  6627. (if (or (looking-at "[ \t]*$")
  6628. (save-excursion (skip-chars-backward " \t") (bolp)))
  6629. (newline)
  6630. (if (and org-table-automatic-realign
  6631. org-table-may-need-update)
  6632. (org-table-align))
  6633. (let ((col (org-table-current-column)))
  6634. (beginning-of-line 2)
  6635. (if (or (not (org-at-table-p))
  6636. (org-at-table-hline-p))
  6637. (progn
  6638. (beginning-of-line 0)
  6639. (org-table-insert-row 'below)))
  6640. (org-table-goto-column col)
  6641. (skip-chars-backward "^|\n\r")
  6642. (if (looking-at " ") (forward-char 1)))))
  6643. (defun org-table-copy-down (n)
  6644. "Copy a field down in the current column.
  6645. If the field at the cursor is empty, copy into it the content of the nearest
  6646. non-empty field above. With argument N, use the Nth non-empty field.
  6647. If the current field is not empty, it is copied down to the next row, and
  6648. the cursor is moved with it. Therefore, repeating this command causes the
  6649. column to be filled row-by-row.
  6650. If the variable `org-table-copy-increment' is non-nil and the field is an
  6651. integer or a timestamp, it will be incremented while copying. In the case of
  6652. a timestamp, if the cursor is on the year, change the year. If it is on the
  6653. month or the day, change that. Point will stay on the current date field
  6654. in order to easily repeat the interval."
  6655. (interactive "p")
  6656. (let* ((colpos (org-table-current-column))
  6657. (col (current-column))
  6658. (field (org-table-get-field))
  6659. (non-empty (string-match "[^ \t]" field))
  6660. (beg (org-table-begin))
  6661. txt)
  6662. (org-table-check-inside-data-field)
  6663. (if non-empty
  6664. (progn
  6665. (setq txt (org-trim field))
  6666. (org-table-next-row)
  6667. (org-table-blank-field))
  6668. (save-excursion
  6669. (setq txt
  6670. (catch 'exit
  6671. (while (progn (beginning-of-line 1)
  6672. (re-search-backward org-table-dataline-regexp
  6673. beg t))
  6674. (org-table-goto-column colpos t)
  6675. (if (and (looking-at
  6676. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  6677. (= (setq n (1- n)) 0))
  6678. (throw 'exit (match-string 1))))))))
  6679. (if txt
  6680. (progn
  6681. (if (and org-table-copy-increment
  6682. (string-match "^[0-9]+$" txt))
  6683. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  6684. (insert txt)
  6685. (move-to-column col)
  6686. (if (and org-table-copy-increment (org-at-timestamp-p t))
  6687. (org-timestamp-up 1)
  6688. (org-table-maybe-recalculate-line))
  6689. (org-table-align)
  6690. (move-to-column col))
  6691. (error "No non-empty field found"))))
  6692. (defun org-table-check-inside-data-field ()
  6693. "Is point inside a table data field?
  6694. I.e. not on a hline or before the first or after the last column?
  6695. This actually throws an error, so it aborts the current command."
  6696. (if (or (not (org-at-table-p))
  6697. (= (org-table-current-column) 0)
  6698. (org-at-table-hline-p)
  6699. (looking-at "[ \t]*$"))
  6700. (error "Not in table data field")))
  6701. (defvar org-table-clip nil
  6702. "Clipboard for table regions.")
  6703. (defun org-table-blank-field ()
  6704. "Blank the current table field or active region."
  6705. (interactive)
  6706. (org-table-check-inside-data-field)
  6707. (if (and (interactive-p) (org-region-active-p))
  6708. (let (org-table-clip)
  6709. (org-table-cut-region (region-beginning) (region-end)))
  6710. (skip-chars-backward "^|")
  6711. (backward-char 1)
  6712. (if (looking-at "|[^|\n]+")
  6713. (let* ((pos (match-beginning 0))
  6714. (match (match-string 0))
  6715. (len (org-string-width match)))
  6716. (replace-match (concat "|" (make-string (1- len) ?\ )))
  6717. (goto-char (+ 2 pos))
  6718. (substring match 1)))))
  6719. (defun org-table-get-field (&optional n replace)
  6720. "Return the value of the field in column N of current row.
  6721. N defaults to current field.
  6722. If REPLACE is a string, replace field with this value. The return value
  6723. is always the old value."
  6724. (and n (org-table-goto-column n))
  6725. (skip-chars-backward "^|\n")
  6726. (backward-char 1)
  6727. (if (looking-at "|[^|\r\n]*")
  6728. (let* ((pos (match-beginning 0))
  6729. (val (buffer-substring (1+ pos) (match-end 0))))
  6730. (if replace
  6731. (replace-match (concat "|" replace) t t))
  6732. (goto-char (min (point-at-eol) (+ 2 pos)))
  6733. val)
  6734. (forward-char 1) ""))
  6735. (defun org-table-field-info (arg)
  6736. "Show info about the current field, and highlight any reference at point."
  6737. (interactive "P")
  6738. (org-table-get-specials)
  6739. (save-excursion
  6740. (let* ((pos (point))
  6741. (col (org-table-current-column))
  6742. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  6743. (name (car (rassoc (list (org-current-line) col)
  6744. org-table-named-field-locations)))
  6745. (eql (org-table-get-stored-formulas))
  6746. (dline (org-table-current-dline))
  6747. (ref (format "@%d$%d" dline col))
  6748. (ref1 (org-table-convert-refs-to-an ref))
  6749. (fequation (or (assoc name eql) (assoc ref eql)))
  6750. (cequation (assoc (int-to-string col) eql))
  6751. (eqn (or fequation cequation)))
  6752. (goto-char pos)
  6753. (condition-case nil
  6754. (org-table-show-reference 'local)
  6755. (error nil))
  6756. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  6757. dline col
  6758. (if cname (concat " or $" cname) "")
  6759. dline col ref1
  6760. (if name (concat " or $" name) "")
  6761. ;; FIXME: formula info not correct if special table line
  6762. (if eqn
  6763. (concat ", formula: "
  6764. (org-table-formula-to-user
  6765. (concat
  6766. (if (string-match "^[$@]"(car eqn)) "" "$")
  6767. (car eqn) "=" (cdr eqn))))
  6768. "")))))
  6769. (defun org-table-current-column ()
  6770. "Find out which column we are in.
  6771. When called interactively, column is also displayed in echo area."
  6772. (interactive)
  6773. (if (interactive-p) (org-table-check-inside-data-field))
  6774. (save-excursion
  6775. (let ((cnt 0) (pos (point)))
  6776. (beginning-of-line 1)
  6777. (while (search-forward "|" pos t)
  6778. (setq cnt (1+ cnt)))
  6779. (if (interactive-p) (message "This is table column %d" cnt))
  6780. cnt)))
  6781. (defun org-table-current-dline ()
  6782. "Find out what table data line we are in.
  6783. Only datalins count for this."
  6784. (interactive)
  6785. (if (interactive-p) (org-table-check-inside-data-field))
  6786. (save-excursion
  6787. (let ((cnt 0) (pos (point)))
  6788. (goto-char (org-table-begin))
  6789. (while (<= (point) pos)
  6790. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  6791. (beginning-of-line 2))
  6792. (if (interactive-p) (message "This is table line %d" cnt))
  6793. cnt)))
  6794. (defun org-table-goto-column (n &optional on-delim force)
  6795. "Move the cursor to the Nth column in the current table line.
  6796. With optional argument ON-DELIM, stop with point before the left delimiter
  6797. of the field.
  6798. If there are less than N fields, just go to after the last delimiter.
  6799. However, when FORCE is non-nil, create new columns if necessary."
  6800. (interactive "p")
  6801. (let ((pos (point-at-eol)))
  6802. (beginning-of-line 1)
  6803. (when (> n 0)
  6804. (while (and (> (setq n (1- n)) -1)
  6805. (or (search-forward "|" pos t)
  6806. (and force
  6807. (progn (end-of-line 1)
  6808. (skip-chars-backward "^|")
  6809. (insert " | "))))))
  6810. ; (backward-char 2) t)))))
  6811. (when (and force (not (looking-at ".*|")))
  6812. (save-excursion (end-of-line 1) (insert " | ")))
  6813. (if on-delim
  6814. (backward-char 1)
  6815. (if (looking-at " ") (forward-char 1))))))
  6816. (defun org-at-table-p (&optional table-type)
  6817. "Return t if the cursor is inside an org-type table.
  6818. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  6819. (if org-enable-table-editor
  6820. (save-excursion
  6821. (beginning-of-line 1)
  6822. (looking-at (if table-type org-table-any-line-regexp
  6823. org-table-line-regexp)))
  6824. nil))
  6825. (defun org-at-table.el-p ()
  6826. "Return t if and only if we are at a table.el table."
  6827. (and (org-at-table-p 'any)
  6828. (save-excursion
  6829. (goto-char (org-table-begin 'any))
  6830. (looking-at org-table1-hline-regexp))))
  6831. (defun org-table-recognize-table.el ()
  6832. "If there is a table.el table nearby, recognize it and move into it."
  6833. (if org-table-tab-recognizes-table.el
  6834. (if (org-at-table.el-p)
  6835. (progn
  6836. (beginning-of-line 1)
  6837. (if (looking-at org-table-dataline-regexp)
  6838. nil
  6839. (if (looking-at org-table1-hline-regexp)
  6840. (progn
  6841. (beginning-of-line 2)
  6842. (if (looking-at org-table-any-border-regexp)
  6843. (beginning-of-line -1)))))
  6844. (if (re-search-forward "|" (org-table-end t) t)
  6845. (progn
  6846. (require 'table)
  6847. (if (table--at-cell-p (point))
  6848. t
  6849. (message "recognizing table.el table...")
  6850. (table-recognize-table)
  6851. (message "recognizing table.el table...done")))
  6852. (error "This should not happen..."))
  6853. t)
  6854. nil)
  6855. nil))
  6856. (defun org-at-table-hline-p ()
  6857. "Return t if the cursor is inside a hline in a table."
  6858. (if org-enable-table-editor
  6859. (save-excursion
  6860. (beginning-of-line 1)
  6861. (looking-at org-table-hline-regexp))
  6862. nil))
  6863. (defun org-table-insert-column ()
  6864. "Insert a new column into the table."
  6865. (interactive)
  6866. (if (not (org-at-table-p))
  6867. (error "Not at a table"))
  6868. (org-table-find-dataline)
  6869. (let* ((col (max 1 (org-table-current-column)))
  6870. (beg (org-table-begin))
  6871. (end (org-table-end))
  6872. ;; Current cursor position
  6873. (linepos (org-current-line))
  6874. (colpos col))
  6875. (goto-char beg)
  6876. (while (< (point) end)
  6877. (if (org-at-table-hline-p)
  6878. nil
  6879. (org-table-goto-column col t)
  6880. (insert "| "))
  6881. (beginning-of-line 2))
  6882. (move-marker end nil)
  6883. (goto-line linepos)
  6884. (org-table-goto-column colpos)
  6885. (org-table-align)
  6886. (org-table-fix-formulas "$" nil (1- col) 1)))
  6887. (defun org-table-find-dataline ()
  6888. "Find a dataline in the current table, which is needed for column commands."
  6889. (if (and (org-at-table-p)
  6890. (not (org-at-table-hline-p)))
  6891. t
  6892. (let ((col (current-column))
  6893. (end (org-table-end)))
  6894. (move-to-column col)
  6895. (while (and (< (point) end)
  6896. (or (not (= (current-column) col))
  6897. (org-at-table-hline-p)))
  6898. (beginning-of-line 2)
  6899. (move-to-column col))
  6900. (if (and (org-at-table-p)
  6901. (not (org-at-table-hline-p)))
  6902. t
  6903. (error
  6904. "Please position cursor in a data line for column operations")))))
  6905. (defun org-table-delete-column ()
  6906. "Delete a column from the table."
  6907. (interactive)
  6908. (if (not (org-at-table-p))
  6909. (error "Not at a table"))
  6910. (org-table-find-dataline)
  6911. (org-table-check-inside-data-field)
  6912. (let* ((col (org-table-current-column))
  6913. (beg (org-table-begin))
  6914. (end (org-table-end))
  6915. ;; Current cursor position
  6916. (linepos (org-current-line))
  6917. (colpos col))
  6918. (goto-char beg)
  6919. (while (< (point) end)
  6920. (if (org-at-table-hline-p)
  6921. nil
  6922. (org-table-goto-column col t)
  6923. (and (looking-at "|[^|\n]+|")
  6924. (replace-match "|")))
  6925. (beginning-of-line 2))
  6926. (move-marker end nil)
  6927. (goto-line linepos)
  6928. (org-table-goto-column colpos)
  6929. (org-table-align)
  6930. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  6931. col -1 col)))
  6932. (defun org-table-move-column-right ()
  6933. "Move column to the right."
  6934. (interactive)
  6935. (org-table-move-column nil))
  6936. (defun org-table-move-column-left ()
  6937. "Move column to the left."
  6938. (interactive)
  6939. (org-table-move-column 'left))
  6940. (defun org-table-move-column (&optional left)
  6941. "Move the current column to the right. With arg LEFT, move to the left."
  6942. (interactive "P")
  6943. (if (not (org-at-table-p))
  6944. (error "Not at a table"))
  6945. (org-table-find-dataline)
  6946. (org-table-check-inside-data-field)
  6947. (let* ((col (org-table-current-column))
  6948. (col1 (if left (1- col) col))
  6949. (beg (org-table-begin))
  6950. (end (org-table-end))
  6951. ;; Current cursor position
  6952. (linepos (org-current-line))
  6953. (colpos (if left (1- col) (1+ col))))
  6954. (if (and left (= col 1))
  6955. (error "Cannot move column further left"))
  6956. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  6957. (error "Cannot move column further right"))
  6958. (goto-char beg)
  6959. (while (< (point) end)
  6960. (if (org-at-table-hline-p)
  6961. nil
  6962. (org-table-goto-column col1 t)
  6963. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  6964. (replace-match "|\\2|\\1|")))
  6965. (beginning-of-line 2))
  6966. (move-marker end nil)
  6967. (goto-line linepos)
  6968. (org-table-goto-column colpos)
  6969. (org-table-align)
  6970. (org-table-fix-formulas
  6971. "$" (list (cons (number-to-string col) (number-to-string colpos))
  6972. (cons (number-to-string colpos) (number-to-string col))))))
  6973. (defun org-table-move-row-down ()
  6974. "Move table row down."
  6975. (interactive)
  6976. (org-table-move-row nil))
  6977. (defun org-table-move-row-up ()
  6978. "Move table row up."
  6979. (interactive)
  6980. (org-table-move-row 'up))
  6981. (defun org-table-move-row (&optional up)
  6982. "Move the current table line down. With arg UP, move it up."
  6983. (interactive "P")
  6984. (let* ((col (current-column))
  6985. (pos (point))
  6986. (hline1p (save-excursion (beginning-of-line 1)
  6987. (looking-at org-table-hline-regexp)))
  6988. (dline1 (org-table-current-dline))
  6989. (dline2 (+ dline1 (if up -1 1)))
  6990. (tonew (if up 0 2))
  6991. txt hline2p)
  6992. (beginning-of-line tonew)
  6993. (unless (org-at-table-p)
  6994. (goto-char pos)
  6995. (error "Cannot move row further"))
  6996. (setq hline2p (looking-at org-table-hline-regexp))
  6997. (goto-char pos)
  6998. (beginning-of-line 1)
  6999. (setq pos (point))
  7000. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  7001. (delete-region (point) (1+ (point-at-eol)))
  7002. (beginning-of-line tonew)
  7003. (insert txt)
  7004. (beginning-of-line 0)
  7005. (move-to-column col)
  7006. (unless (or hline1p hline2p)
  7007. (org-table-fix-formulas
  7008. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  7009. (cons (number-to-string dline2) (number-to-string dline1)))))))
  7010. (defun org-table-insert-row (&optional arg)
  7011. "Insert a new row above the current line into the table.
  7012. With prefix ARG, insert below the current line."
  7013. (interactive "P")
  7014. (if (not (org-at-table-p))
  7015. (error "Not at a table"))
  7016. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  7017. (new (org-table-clean-line line)))
  7018. ;; Fix the first field if necessary
  7019. (if (string-match "^[ \t]*| *[#$] *|" line)
  7020. (setq new (replace-match (match-string 0 line) t t new)))
  7021. (beginning-of-line (if arg 2 1))
  7022. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  7023. (beginning-of-line 0)
  7024. (re-search-forward "| ?" (point-at-eol) t)
  7025. (and (or org-table-may-need-update org-table-overlay-coordinates)
  7026. (org-table-align))
  7027. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  7028. (defun org-table-insert-hline (&optional above)
  7029. "Insert a horizontal-line below the current line into the table.
  7030. With prefix ABOVE, insert above the current line."
  7031. (interactive "P")
  7032. (if (not (org-at-table-p))
  7033. (error "Not at a table"))
  7034. (let ((line (org-table-clean-line
  7035. (buffer-substring (point-at-bol) (point-at-eol))))
  7036. (col (current-column)))
  7037. (while (string-match "|\\( +\\)|" line)
  7038. (setq line (replace-match
  7039. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  7040. ?-) "|") t t line)))
  7041. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  7042. (beginning-of-line (if above 1 2))
  7043. (insert line "\n")
  7044. (beginning-of-line (if above 1 -1))
  7045. (move-to-column col)
  7046. (and org-table-overlay-coordinates (org-table-align))))
  7047. (defun org-table-hline-and-move (&optional same-column)
  7048. "Insert a hline and move to the row below that line."
  7049. (interactive "P")
  7050. (let ((col (org-table-current-column)))
  7051. (org-table-maybe-eval-formula)
  7052. (org-table-maybe-recalculate-line)
  7053. (org-table-insert-hline)
  7054. (end-of-line 2)
  7055. (if (looking-at "\n[ \t]*|-")
  7056. (progn (insert "\n|") (org-table-align))
  7057. (org-table-next-field))
  7058. (if same-column (org-table-goto-column col))))
  7059. (defun org-table-clean-line (s)
  7060. "Convert a table line S into a string with only \"|\" and space.
  7061. In particular, this does handle wide and invisible characters."
  7062. (if (string-match "^[ \t]*|-" s)
  7063. ;; It's a hline, just map the characters
  7064. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  7065. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  7066. (setq s (replace-match
  7067. (concat "|" (make-string (org-string-width (match-string 1 s))
  7068. ?\ ) "|")
  7069. t t s)))
  7070. s))
  7071. (defun org-table-kill-row ()
  7072. "Delete the current row or horizontal line from the table."
  7073. (interactive)
  7074. (if (not (org-at-table-p))
  7075. (error "Not at a table"))
  7076. (let ((col (current-column))
  7077. (dline (org-table-current-dline)))
  7078. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  7079. (if (not (org-at-table-p)) (beginning-of-line 0))
  7080. (move-to-column col)
  7081. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  7082. dline -1 dline)))
  7083. (defun org-table-sort-lines (with-case &optional sorting-type)
  7084. "Sort table lines according to the column at point.
  7085. The position of point indicates the column to be used for
  7086. sorting, and the range of lines is the range between the nearest
  7087. horizontal separator lines, or the entire table of no such lines
  7088. exist. If point is before the first column, you will be prompted
  7089. for the sorting column. If there is an active region, the mark
  7090. specifies the first line and the sorting column, while point
  7091. should be in the last line to be included into the sorting.
  7092. The command then prompts for the sorting type which can be
  7093. alphabetically, numerically, or by time (as given in a time stamp
  7094. in the field). Sorting in reverse order is also possible.
  7095. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  7096. If SORTING-TYPE is specified when this function is called from a Lisp
  7097. program, no prompting will take place. SORTING-TYPE must be a character,
  7098. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  7099. should be done in reverse order."
  7100. (interactive "P")
  7101. (let* ((thisline (org-current-line))
  7102. (thiscol (org-table-current-column))
  7103. beg end bcol ecol tend tbeg column lns pos)
  7104. (when (equal thiscol 0)
  7105. (if (interactive-p)
  7106. (setq thiscol
  7107. (string-to-number
  7108. (read-string "Use column N for sorting: ")))
  7109. (setq thiscol 1))
  7110. (org-table-goto-column thiscol))
  7111. (org-table-check-inside-data-field)
  7112. (if (org-region-active-p)
  7113. (progn
  7114. (setq beg (region-beginning) end (region-end))
  7115. (goto-char beg)
  7116. (setq column (org-table-current-column)
  7117. beg (point-at-bol))
  7118. (goto-char end)
  7119. (setq end (point-at-bol 2)))
  7120. (setq column (org-table-current-column)
  7121. pos (point)
  7122. tbeg (org-table-begin)
  7123. tend (org-table-end))
  7124. (if (re-search-backward org-table-hline-regexp tbeg t)
  7125. (setq beg (point-at-bol 2))
  7126. (goto-char tbeg)
  7127. (setq beg (point-at-bol 1)))
  7128. (goto-char pos)
  7129. (if (re-search-forward org-table-hline-regexp tend t)
  7130. (setq end (point-at-bol 1))
  7131. (goto-char tend)
  7132. (setq end (point-at-bol))))
  7133. (setq beg (move-marker (make-marker) beg)
  7134. end (move-marker (make-marker) end))
  7135. (untabify beg end)
  7136. (goto-char beg)
  7137. (org-table-goto-column column)
  7138. (skip-chars-backward "^|")
  7139. (setq bcol (current-column))
  7140. (org-table-goto-column (1+ column))
  7141. (skip-chars-backward "^|")
  7142. (setq ecol (1- (current-column)))
  7143. (org-table-goto-column column)
  7144. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  7145. (org-split-string (buffer-substring beg end) "\n")))
  7146. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  7147. (delete-region beg end)
  7148. (move-marker beg nil)
  7149. (move-marker end nil)
  7150. (insert (mapconcat 'cdr lns "\n") "\n")
  7151. (goto-line thisline)
  7152. (org-table-goto-column thiscol)
  7153. (message "%d lines sorted, based on column %d" (length lns) column)))
  7154. (defun org-table-cut-region (beg end)
  7155. "Copy region in table to the clipboard and blank all relevant fields."
  7156. (interactive "r")
  7157. (org-table-copy-region beg end 'cut))
  7158. (defun org-table-copy-region (beg end &optional cut)
  7159. "Copy rectangular region in table to clipboard.
  7160. A special clipboard is used which can only be accessed
  7161. with `org-table-paste-rectangle'."
  7162. (interactive "rP")
  7163. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  7164. region cols
  7165. (rpl (if cut " " nil)))
  7166. (goto-char beg)
  7167. (org-table-check-inside-data-field)
  7168. (setq l01 (org-current-line)
  7169. c01 (org-table-current-column))
  7170. (goto-char end)
  7171. (org-table-check-inside-data-field)
  7172. (setq l02 (org-current-line)
  7173. c02 (org-table-current-column))
  7174. (setq l1 (min l01 l02) l2 (max l01 l02)
  7175. c1 (min c01 c02) c2 (max c01 c02))
  7176. (catch 'exit
  7177. (while t
  7178. (catch 'nextline
  7179. (if (> l1 l2) (throw 'exit t))
  7180. (goto-line l1)
  7181. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7182. (setq cols nil ic1 c1 ic2 c2)
  7183. (while (< ic1 (1+ ic2))
  7184. (push (org-table-get-field ic1 rpl) cols)
  7185. (setq ic1 (1+ ic1)))
  7186. (push (nreverse cols) region)
  7187. (setq l1 (1+ l1)))))
  7188. (setq org-table-clip (nreverse region))
  7189. (if cut (org-table-align))
  7190. org-table-clip))
  7191. (defun org-table-paste-rectangle ()
  7192. "Paste a rectangular region into a table.
  7193. The upper right corner ends up in the current field. All involved fields
  7194. will be overwritten. If the rectangle does not fit into the present table,
  7195. the table is enlarged as needed. The process ignores horizontal separator
  7196. lines."
  7197. (interactive)
  7198. (unless (and org-table-clip (listp org-table-clip))
  7199. (error "First cut/copy a region to paste!"))
  7200. (org-table-check-inside-data-field)
  7201. (let* ((clip org-table-clip)
  7202. (line (org-current-line))
  7203. (col (org-table-current-column))
  7204. (org-enable-table-editor t)
  7205. (org-table-automatic-realign nil)
  7206. c cols field)
  7207. (while (setq cols (pop clip))
  7208. (while (org-at-table-hline-p) (beginning-of-line 2))
  7209. (if (not (org-at-table-p))
  7210. (progn (end-of-line 0) (org-table-next-field)))
  7211. (setq c col)
  7212. (while (setq field (pop cols))
  7213. (org-table-goto-column c nil 'force)
  7214. (org-table-get-field nil field)
  7215. (setq c (1+ c)))
  7216. (beginning-of-line 2))
  7217. (goto-line line)
  7218. (org-table-goto-column col)
  7219. (org-table-align)))
  7220. (defun org-table-convert ()
  7221. "Convert from `org-mode' table to table.el and back.
  7222. Obviously, this only works within limits. When an Org-mode table is
  7223. converted to table.el, all horizontal separator lines get lost, because
  7224. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7225. A table.el table can be converted to an Org-mode table only if it does not
  7226. do row or column spanning. Multiline cells will become multiple cells.
  7227. Beware, Org-mode does not test if the table can be successfully converted - it
  7228. blindly applies a recipe that works for simple tables."
  7229. (interactive)
  7230. (require 'table)
  7231. (if (org-at-table.el-p)
  7232. ;; convert to Org-mode table
  7233. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7234. (end (move-marker (make-marker) (org-table-end t))))
  7235. (table-unrecognize-region beg end)
  7236. (goto-char beg)
  7237. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7238. (replace-match ""))
  7239. (goto-char beg))
  7240. (if (org-at-table-p)
  7241. ;; convert to table.el table
  7242. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7243. (end (move-marker (make-marker) (org-table-end))))
  7244. ;; first, get rid of all horizontal lines
  7245. (goto-char beg)
  7246. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7247. (replace-match ""))
  7248. ;; insert a hline before first
  7249. (goto-char beg)
  7250. (org-table-insert-hline 'above)
  7251. (beginning-of-line -1)
  7252. ;; insert a hline after each line
  7253. (while (progn (beginning-of-line 3) (< (point) end))
  7254. (org-table-insert-hline))
  7255. (goto-char beg)
  7256. (setq end (move-marker end (org-table-end)))
  7257. ;; replace "+" at beginning and ending of hlines
  7258. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  7259. (replace-match "\\1+-"))
  7260. (goto-char beg)
  7261. (while (re-search-forward "-|[ \t]*$" end t)
  7262. (replace-match "-+"))
  7263. (goto-char beg)))))
  7264. (defun org-table-wrap-region (arg)
  7265. "Wrap several fields in a column like a paragraph.
  7266. This is useful if you'd like to spread the contents of a field over several
  7267. lines, in order to keep the table compact.
  7268. If there is an active region, and both point and mark are in the same column,
  7269. the text in the column is wrapped to minimum width for the given number of
  7270. lines. Generally, this makes the table more compact. A prefix ARG may be
  7271. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  7272. formats the selected text to two lines. If the region was longer than two
  7273. lines, the remaining lines remain empty. A negative prefix argument reduces
  7274. the current number of lines by that amount. The wrapped text is pasted back
  7275. into the table. If you formatted it to more lines than it was before, fields
  7276. further down in the table get overwritten - so you might need to make space in
  7277. the table first.
  7278. If there is no region, the current field is split at the cursor position and
  7279. the text fragment to the right of the cursor is prepended to the field one
  7280. line down.
  7281. If there is no region, but you specify a prefix ARG, the current field gets
  7282. blank, and the content is appended to the field above."
  7283. (interactive "P")
  7284. (org-table-check-inside-data-field)
  7285. (if (org-region-active-p)
  7286. ;; There is a region: fill as a paragraph
  7287. (let* ((beg (region-beginning))
  7288. (cline (save-excursion (goto-char beg) (org-current-line)))
  7289. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  7290. nlines)
  7291. (org-table-cut-region (region-beginning) (region-end))
  7292. (if (> (length (car org-table-clip)) 1)
  7293. (error "Region must be limited to single column"))
  7294. (setq nlines (if arg
  7295. (if (< arg 1)
  7296. (+ (length org-table-clip) arg)
  7297. arg)
  7298. (length org-table-clip)))
  7299. (setq org-table-clip
  7300. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  7301. nil nlines)))
  7302. (goto-line cline)
  7303. (org-table-goto-column ccol)
  7304. (org-table-paste-rectangle))
  7305. ;; No region, split the current field at point
  7306. (if arg
  7307. ;; combine with field above
  7308. (let ((s (org-table-blank-field))
  7309. (col (org-table-current-column)))
  7310. (beginning-of-line 0)
  7311. (while (org-at-table-hline-p) (beginning-of-line 0))
  7312. (org-table-goto-column col)
  7313. (skip-chars-forward "^|")
  7314. (skip-chars-backward " ")
  7315. (insert " " (org-trim s))
  7316. (org-table-align))
  7317. ;; split field
  7318. (when (looking-at "\\([^|]+\\)+|")
  7319. (let ((s (match-string 1)))
  7320. (replace-match " |")
  7321. (goto-char (match-beginning 0))
  7322. (org-table-next-row)
  7323. (insert (org-trim s) " ")
  7324. (org-table-align))))))
  7325. (defvar org-field-marker nil)
  7326. (defun org-table-edit-field (arg)
  7327. "Edit table field in a different window.
  7328. This is mainly useful for fields that contain hidden parts.
  7329. When called with a \\[universal-argument] prefix, just make the full field visible so that
  7330. it can be edited in place."
  7331. (interactive "P")
  7332. (if arg
  7333. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  7334. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  7335. (remove-text-properties b e '(org-cwidth t invisible t
  7336. display t intangible t))
  7337. (if (and (boundp 'font-lock-mode) font-lock-mode)
  7338. (font-lock-fontify-block)))
  7339. (let ((pos (move-marker (make-marker) (point)))
  7340. (field (org-table-get-field))
  7341. (cw (current-window-configuration))
  7342. p)
  7343. (switch-to-buffer-other-window "*Org tmp*")
  7344. (erase-buffer)
  7345. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  7346. (let ((org-inhibit-startup t)) (org-mode))
  7347. (goto-char (setq p (point-max)))
  7348. (insert (org-trim field))
  7349. (remove-text-properties p (point-max)
  7350. '(invisible t org-cwidth t display t
  7351. intangible t))
  7352. (goto-char p)
  7353. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  7354. (org-set-local 'org-window-configuration cw)
  7355. (org-set-local 'org-field-marker pos)
  7356. (message "Edit and finish with C-c C-c"))))
  7357. (defun org-table-finish-edit-field ()
  7358. "Finish editing a table data field.
  7359. Remove all newline characters, insert the result into the table, realign
  7360. the table and kill the editing buffer."
  7361. (let ((pos org-field-marker)
  7362. (cw org-window-configuration)
  7363. (cb (current-buffer))
  7364. text)
  7365. (goto-char (point-min))
  7366. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  7367. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  7368. (replace-match " "))
  7369. (setq text (org-trim (buffer-string)))
  7370. (set-window-configuration cw)
  7371. (kill-buffer cb)
  7372. (select-window (get-buffer-window (marker-buffer pos)))
  7373. (goto-char pos)
  7374. (move-marker pos nil)
  7375. (org-table-check-inside-data-field)
  7376. (org-table-get-field nil text)
  7377. (org-table-align)
  7378. (message "New field value inserted")))
  7379. (defun org-trim (s)
  7380. "Remove whitespace at beginning and end of string."
  7381. (if (string-match "^[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  7382. (if (string-match "[ \t\n\r]+$" s) (setq s (replace-match "" t t s)))
  7383. s)
  7384. (defun org-wrap (string &optional width lines)
  7385. "Wrap string to either a number of lines, or a width in characters.
  7386. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  7387. that costs. If there is a word longer than WIDTH, the text is actually
  7388. wrapped to the length of that word.
  7389. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  7390. many lines, whatever width that takes.
  7391. The return value is a list of lines, without newlines at the end."
  7392. (let* ((words (org-split-string string "[ \t\n]+"))
  7393. (maxword (apply 'max (mapcar 'org-string-width words)))
  7394. w ll)
  7395. (cond (width
  7396. (org-do-wrap words (max maxword width)))
  7397. (lines
  7398. (setq w maxword)
  7399. (setq ll (org-do-wrap words maxword))
  7400. (if (<= (length ll) lines)
  7401. ll
  7402. (setq ll words)
  7403. (while (> (length ll) lines)
  7404. (setq w (1+ w))
  7405. (setq ll (org-do-wrap words w)))
  7406. ll))
  7407. (t (error "Cannot wrap this")))))
  7408. (defun org-do-wrap (words width)
  7409. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  7410. (let (lines line)
  7411. (while words
  7412. (setq line (pop words))
  7413. (while (and words (< (+ (length line) (length (car words))) width))
  7414. (setq line (concat line " " (pop words))))
  7415. (setq lines (push line lines)))
  7416. (nreverse lines)))
  7417. (defun org-split-string (string &optional separators)
  7418. "Splits STRING into substrings at SEPARATORS.
  7419. No empty strings are returned if there are matches at the beginning
  7420. and end of string."
  7421. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  7422. (start 0)
  7423. notfirst
  7424. (list nil))
  7425. (while (and (string-match rexp string
  7426. (if (and notfirst
  7427. (= start (match-beginning 0))
  7428. (< start (length string)))
  7429. (1+ start) start))
  7430. (< (match-beginning 0) (length string)))
  7431. (setq notfirst t)
  7432. (or (eq (match-beginning 0) 0)
  7433. (and (eq (match-beginning 0) (match-end 0))
  7434. (eq (match-beginning 0) start))
  7435. (setq list
  7436. (cons (substring string start (match-beginning 0))
  7437. list)))
  7438. (setq start (match-end 0)))
  7439. (or (eq start (length string))
  7440. (setq list
  7441. (cons (substring string start)
  7442. list)))
  7443. (nreverse list)))
  7444. (defun org-table-map-tables (function)
  7445. "Apply FUNCTION to the start of all tables in the buffer."
  7446. (save-excursion
  7447. (save-restriction
  7448. (widen)
  7449. (goto-char (point-min))
  7450. (while (re-search-forward org-table-any-line-regexp nil t)
  7451. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  7452. (beginning-of-line 1)
  7453. (if (looking-at org-table-line-regexp)
  7454. (save-excursion (funcall function)))
  7455. (re-search-forward org-table-any-border-regexp nil 1))))
  7456. (message "Mapping tables: done"))
  7457. (defvar org-timecnt) ; dynamically scoped parameter
  7458. (defun org-table-sum (&optional beg end nlast)
  7459. "Sum numbers in region of current table column.
  7460. The result will be displayed in the echo area, and will be available
  7461. as kill to be inserted with \\[yank].
  7462. If there is an active region, it is interpreted as a rectangle and all
  7463. numbers in that rectangle will be summed. If there is no active
  7464. region and point is located in a table column, sum all numbers in that
  7465. column.
  7466. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  7467. numbers are assumed to be times as well (in decimal hours) and the
  7468. numbers are added as such.
  7469. If NLAST is a number, only the NLAST fields will actually be summed."
  7470. (interactive)
  7471. (save-excursion
  7472. (let (col (org-timecnt 0) diff h m s org-table-clip)
  7473. (cond
  7474. ((and beg end)) ; beg and end given explicitly
  7475. ((org-region-active-p)
  7476. (setq beg (region-beginning) end (region-end)))
  7477. (t
  7478. (setq col (org-table-current-column))
  7479. (goto-char (org-table-begin))
  7480. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  7481. (error "No table data"))
  7482. (org-table-goto-column col)
  7483. (setq beg (point))
  7484. (goto-char (org-table-end))
  7485. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  7486. (error "No table data"))
  7487. (org-table-goto-column col)
  7488. (setq end (point))))
  7489. (let* ((items (apply 'append (org-table-copy-region beg end)))
  7490. (items1 (cond ((not nlast) items)
  7491. ((>= nlast (length items)) items)
  7492. (t (setq items (reverse items))
  7493. (setcdr (nthcdr (1- nlast) items) nil)
  7494. (nreverse items))))
  7495. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  7496. items1)))
  7497. (res (apply '+ numbers))
  7498. (sres (if (= org-timecnt 0)
  7499. (format "%g" res)
  7500. (setq diff (* 3600 res)
  7501. h (floor (/ diff 3600)) diff (mod diff 3600)
  7502. m (floor (/ diff 60)) diff (mod diff 60)
  7503. s diff)
  7504. (format "%d:%02d:%02d" h m s))))
  7505. (kill-new sres)
  7506. (if (interactive-p)
  7507. (message "%s"
  7508. (substitute-command-keys
  7509. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  7510. (length numbers) sres))))
  7511. sres))))
  7512. (defun org-table-get-number-for-summing (s)
  7513. (let (n)
  7514. (if (string-match "^ *|? *" s)
  7515. (setq s (replace-match "" nil nil s)))
  7516. (if (string-match " *|? *$" s)
  7517. (setq s (replace-match "" nil nil s)))
  7518. (setq n (string-to-number s))
  7519. (cond
  7520. ((and (string-match "0" s)
  7521. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  7522. ((string-match "\\`[ \t]+\\'" s) nil)
  7523. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  7524. (let ((h (string-to-number (or (match-string 1 s) "0")))
  7525. (m (string-to-number (or (match-string 2 s) "0")))
  7526. (s (string-to-number (or (match-string 4 s) "0"))))
  7527. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  7528. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  7529. ((equal n 0) nil)
  7530. (t n))))
  7531. (defun org-table-current-field-formula (&optional key noerror)
  7532. "Return the formula active for the current field.
  7533. Assumes that specials are in place.
  7534. If KEY is given, return the key to this formula.
  7535. Otherwise return the formula preceeded with \"=\" or \":=\"."
  7536. (let* ((name (car (rassoc (list (org-current-line)
  7537. (org-table-current-column))
  7538. org-table-named-field-locations)))
  7539. (col (org-table-current-column))
  7540. (scol (int-to-string col))
  7541. (ref (format "@%d$%d" (org-table-current-dline) col))
  7542. (stored-list (org-table-get-stored-formulas noerror))
  7543. (ass (or (assoc name stored-list)
  7544. (assoc ref stored-list)
  7545. (assoc scol stored-list))))
  7546. (if key
  7547. (car ass)
  7548. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  7549. (cdr ass))))))
  7550. (defun org-table-get-formula (&optional equation named)
  7551. "Read a formula from the minibuffer, offer stored formula as default.
  7552. When NAMED is non-nil, look for a named equation."
  7553. (let* ((stored-list (org-table-get-stored-formulas))
  7554. (name (car (rassoc (list (org-current-line)
  7555. (org-table-current-column))
  7556. org-table-named-field-locations)))
  7557. (ref (format "@%d$%d" (org-table-current-dline)
  7558. (org-table-current-column)))
  7559. (refass (assoc ref stored-list))
  7560. (scol (if named
  7561. (if name name ref)
  7562. (int-to-string (org-table-current-column))))
  7563. (dummy (and (or name refass) (not named)
  7564. (not (y-or-n-p "Replace field formula with column formula? " ))
  7565. (error "Abort")))
  7566. (name (or name ref))
  7567. (org-table-may-need-update nil)
  7568. (stored (cdr (assoc scol stored-list)))
  7569. (eq (cond
  7570. ((and stored equation (string-match "^ *=? *$" equation))
  7571. stored)
  7572. ((stringp equation)
  7573. equation)
  7574. (t (org-table-formula-from-user
  7575. (read-string
  7576. (org-table-formula-to-user
  7577. (format "%s formula %s%s="
  7578. (if named "Field" "Column")
  7579. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  7580. scol))
  7581. (if stored (org-table-formula-to-user stored) "")
  7582. 'org-table-formula-history
  7583. )))))
  7584. mustsave)
  7585. (when (not (string-match "\\S-" eq))
  7586. ;; remove formula
  7587. (setq stored-list (delq (assoc scol stored-list) stored-list))
  7588. (org-table-store-formulas stored-list)
  7589. (error "Formula removed"))
  7590. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  7591. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  7592. (if (and name (not named))
  7593. ;; We set the column equation, delete the named one.
  7594. (setq stored-list (delq (assoc name stored-list) stored-list)
  7595. mustsave t))
  7596. (if stored
  7597. (setcdr (assoc scol stored-list) eq)
  7598. (setq stored-list (cons (cons scol eq) stored-list)))
  7599. (if (or mustsave (not (equal stored eq)))
  7600. (org-table-store-formulas stored-list))
  7601. eq))
  7602. (defun org-table-store-formulas (alist)
  7603. "Store the list of formulas below the current table."
  7604. (setq alist (sort alist 'org-table-formula-less-p))
  7605. (save-excursion
  7606. (goto-char (org-table-end))
  7607. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  7608. (progn
  7609. ;; don't overwrite TBLFM, we might use text properties to store stuff
  7610. (goto-char (match-beginning 2))
  7611. (delete-region (match-beginning 2) (match-end 0)))
  7612. (insert "#+TBLFM:"))
  7613. (insert " "
  7614. (mapconcat (lambda (x)
  7615. (concat
  7616. (if (equal (string-to-char (car x)) ?@) "" "$")
  7617. (car x) "=" (cdr x)))
  7618. alist "::")
  7619. "\n")))
  7620. (defsubst org-table-formula-make-cmp-string (a)
  7621. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  7622. (concat
  7623. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  7624. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  7625. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  7626. (defun org-table-formula-less-p (a b)
  7627. "Compare two formulas for sorting."
  7628. (let ((as (org-table-formula-make-cmp-string (car a)))
  7629. (bs (org-table-formula-make-cmp-string (car b))))
  7630. (and as bs (string< as bs))))
  7631. (defun org-table-get-stored-formulas (&optional noerror)
  7632. "Return an alist with the stored formulas directly after current table."
  7633. (interactive)
  7634. (let (scol eq eq-alist strings string seen)
  7635. (save-excursion
  7636. (goto-char (org-table-end))
  7637. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  7638. (setq strings (org-split-string (match-string 2) " *:: *"))
  7639. (while (setq string (pop strings))
  7640. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  7641. (setq scol (if (match-end 2)
  7642. (match-string 2 string)
  7643. (match-string 1 string))
  7644. eq (match-string 3 string)
  7645. eq-alist (cons (cons scol eq) eq-alist))
  7646. (if (member scol seen)
  7647. (if noerror
  7648. (progn
  7649. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  7650. (ding)
  7651. (sit-for 2))
  7652. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  7653. (push scol seen))))))
  7654. (nreverse eq-alist)))
  7655. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  7656. "Modify the equations after the table structure has been edited.
  7657. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  7658. For all numbers larger than LIMIT, shift them by DELTA."
  7659. (save-excursion
  7660. (goto-char (org-table-end))
  7661. (when (looking-at "#\\+TBLFM:")
  7662. (let ((re (concat key "\\([0-9]+\\)"))
  7663. (re2
  7664. (when remove
  7665. (if (equal key "$")
  7666. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  7667. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  7668. s n a)
  7669. (when remove
  7670. (while (re-search-forward re2 (point-at-eol) t)
  7671. (replace-match "")))
  7672. (while (re-search-forward re (point-at-eol) t)
  7673. (setq s (match-string 1) n (string-to-number s))
  7674. (cond
  7675. ((setq a (assoc s replace))
  7676. (replace-match (concat key (cdr a)) t t))
  7677. ((and limit (> n limit))
  7678. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  7679. (defun org-table-get-specials ()
  7680. "Get the column names and local parameters for this table."
  7681. (save-excursion
  7682. (let ((beg (org-table-begin)) (end (org-table-end))
  7683. names name fields fields1 field cnt
  7684. c v l line col types dlines hlines)
  7685. (setq org-table-column-names nil
  7686. org-table-local-parameters nil
  7687. org-table-named-field-locations nil
  7688. org-table-current-begin-line nil
  7689. org-table-current-begin-pos nil
  7690. org-table-current-line-types nil)
  7691. (goto-char beg)
  7692. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  7693. (setq names (org-split-string (match-string 1) " *| *")
  7694. cnt 1)
  7695. (while (setq name (pop names))
  7696. (setq cnt (1+ cnt))
  7697. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  7698. (push (cons name (int-to-string cnt)) org-table-column-names))))
  7699. (setq org-table-column-names (nreverse org-table-column-names))
  7700. (setq org-table-column-name-regexp
  7701. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  7702. (goto-char beg)
  7703. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  7704. (setq fields (org-split-string (match-string 1) " *| *"))
  7705. (while (setq field (pop fields))
  7706. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  7707. (push (cons (match-string 1 field) (match-string 2 field))
  7708. org-table-local-parameters))))
  7709. (goto-char beg)
  7710. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  7711. (setq c (match-string 1)
  7712. fields (org-split-string (match-string 2) " *| *"))
  7713. (save-excursion
  7714. (beginning-of-line (if (equal c "_") 2 0))
  7715. (setq line (org-current-line) col 1)
  7716. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  7717. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  7718. (while (and fields1 (setq field (pop fields)))
  7719. (setq v (pop fields1) col (1+ col))
  7720. (when (and (stringp field) (stringp v)
  7721. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  7722. (push (cons field v) org-table-local-parameters)
  7723. (push (list field line col) org-table-named-field-locations))))
  7724. ;; Analyse the line types
  7725. (goto-char beg)
  7726. (setq org-table-current-begin-line (org-current-line)
  7727. org-table-current-begin-pos (point)
  7728. l org-table-current-begin-line)
  7729. (while (looking-at "[ \t]*|\\(-\\)?")
  7730. (push (if (match-end 1) 'hline 'dline) types)
  7731. (if (match-end 1) (push l hlines) (push l dlines))
  7732. (beginning-of-line 2)
  7733. (setq l (1+ l)))
  7734. (setq org-table-current-line-types (apply 'vector (nreverse types))
  7735. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  7736. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  7737. (defun org-table-maybe-eval-formula ()
  7738. "Check if the current field starts with \"=\" or \":=\".
  7739. If yes, store the formula and apply it."
  7740. ;; We already know we are in a table. Get field will only return a formula
  7741. ;; when appropriate. It might return a separator line, but no problem.
  7742. (when org-table-formula-evaluate-inline
  7743. (let* ((field (org-trim (or (org-table-get-field) "")))
  7744. named eq)
  7745. (when (string-match "^:?=\\(.*\\)" field)
  7746. (setq named (equal (string-to-char field) ?:)
  7747. eq (match-string 1 field))
  7748. (if (or (fboundp 'calc-eval)
  7749. (equal (substring eq 0 (min 2 (length eq))) "'("))
  7750. (org-table-eval-formula (if named '(4) nil)
  7751. (org-table-formula-from-user eq))
  7752. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  7753. (defvar org-recalc-commands nil
  7754. "List of commands triggering the recalculation of a line.
  7755. Will be filled automatically during use.")
  7756. (defvar org-recalc-marks
  7757. '((" " . "Unmarked: no special line, no automatic recalculation")
  7758. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  7759. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  7760. ("!" . "Column name definition line. Reference in formula as $name.")
  7761. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  7762. ("_" . "Names for values in row below this one.")
  7763. ("^" . "Names for values in row above this one.")))
  7764. (defun org-table-rotate-recalc-marks (&optional newchar)
  7765. "Rotate the recalculation mark in the first column.
  7766. If in any row, the first field is not consistent with a mark,
  7767. insert a new column for the markers.
  7768. When there is an active region, change all the lines in the region,
  7769. after prompting for the marking character.
  7770. After each change, a message will be displayed indicating the meaning
  7771. of the new mark."
  7772. (interactive)
  7773. (unless (org-at-table-p) (error "Not at a table"))
  7774. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  7775. (beg (org-table-begin))
  7776. (end (org-table-end))
  7777. (l (org-current-line))
  7778. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  7779. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  7780. (have-col
  7781. (save-excursion
  7782. (goto-char beg)
  7783. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  7784. (col (org-table-current-column))
  7785. (forcenew (car (assoc newchar org-recalc-marks)))
  7786. epos new)
  7787. (when l1
  7788. (message "Change region to what mark? Type # * ! $ or SPC: ")
  7789. (setq newchar (char-to-string (read-char-exclusive))
  7790. forcenew (car (assoc newchar org-recalc-marks))))
  7791. (if (and newchar (not forcenew))
  7792. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  7793. newchar))
  7794. (if l1 (goto-line l1))
  7795. (save-excursion
  7796. (beginning-of-line 1)
  7797. (unless (looking-at org-table-dataline-regexp)
  7798. (error "Not at a table data line")))
  7799. (unless have-col
  7800. (org-table-goto-column 1)
  7801. (org-table-insert-column)
  7802. (org-table-goto-column (1+ col)))
  7803. (setq epos (point-at-eol))
  7804. (save-excursion
  7805. (beginning-of-line 1)
  7806. (org-table-get-field
  7807. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  7808. (concat " "
  7809. (setq new (or forcenew
  7810. (cadr (member (match-string 1) marks))))
  7811. " ")
  7812. " # ")))
  7813. (if (and l1 l2)
  7814. (progn
  7815. (goto-line l1)
  7816. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  7817. (and (looking-at org-table-dataline-regexp)
  7818. (org-table-get-field 1 (concat " " new " "))))
  7819. (goto-line l1)))
  7820. (if (not (= epos (point-at-eol))) (org-table-align))
  7821. (goto-line l)
  7822. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  7823. (defun org-table-maybe-recalculate-line ()
  7824. "Recompute the current line if marked for it, and if we haven't just done it."
  7825. (interactive)
  7826. (and org-table-allow-automatic-line-recalculation
  7827. (not (and (memq last-command org-recalc-commands)
  7828. (equal org-last-recalc-line (org-current-line))))
  7829. (save-excursion (beginning-of-line 1)
  7830. (looking-at org-table-auto-recalculate-regexp))
  7831. (org-table-recalculate) t))
  7832. (defvar org-table-formula-debug nil
  7833. "Non-nil means, debug table formulas.
  7834. When nil, simply write \"#ERROR\" in corrupted fields.")
  7835. (make-variable-buffer-local 'org-table-formula-debug)
  7836. (defvar modes)
  7837. (defsubst org-set-calc-mode (var &optional value)
  7838. (if (stringp var)
  7839. (setq var (assoc var '(("D" calc-angle-mode deg)
  7840. ("R" calc-angle-mode rad)
  7841. ("F" calc-prefer-frac t)
  7842. ("S" calc-symbolic-mode t)))
  7843. value (nth 2 var) var (nth 1 var)))
  7844. (if (memq var modes)
  7845. (setcar (cdr (memq var modes)) value)
  7846. (cons var (cons value modes)))
  7847. modes)
  7848. (defun org-table-eval-formula (&optional arg equation
  7849. suppress-align suppress-const
  7850. suppress-store suppress-analysis)
  7851. "Replace the table field value at the cursor by the result of a calculation.
  7852. This function makes use of Dave Gillespie's Calc package, in my view the
  7853. most exciting program ever written for GNU Emacs. So you need to have Calc
  7854. installed in order to use this function.
  7855. In a table, this command replaces the value in the current field with the
  7856. result of a formula. It also installs the formula as the \"current\" column
  7857. formula, by storing it in a special line below the table. When called
  7858. with a `C-u' prefix, the current field must ba a named field, and the
  7859. formula is installed as valid in only this specific field.
  7860. When called with two `C-u' prefixes, insert the active equation
  7861. for the field back into the current field, so that it can be
  7862. edited there. This is useful in order to use \\[org-table-show-reference]
  7863. to check the referenced fields.
  7864. When called, the command first prompts for a formula, which is read in
  7865. the minibuffer. Previously entered formulas are available through the
  7866. history list, and the last used formula is offered as a default.
  7867. These stored formulas are adapted correctly when moving, inserting, or
  7868. deleting columns with the corresponding commands.
  7869. The formula can be any algebraic expression understood by the Calc package.
  7870. For details, see the Org-mode manual.
  7871. This function can also be called from Lisp programs and offers
  7872. additional arguments: EQUATION can be the formula to apply. If this
  7873. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  7874. used to speed-up recursive calls by by-passing unnecessary aligns.
  7875. SUPPRESS-CONST suppresses the interpretation of constants in the
  7876. formula, assuming that this has been done already outside the function.
  7877. SUPPRESS-STORE means the formula should not be stored, either because
  7878. it is already stored, or because it is a modified equation that should
  7879. not overwrite the stored one."
  7880. (interactive "P")
  7881. (org-table-check-inside-data-field)
  7882. (or suppress-analysis (org-table-get-specials))
  7883. (if (equal arg '(16))
  7884. (let ((eq (org-table-current-field-formula)))
  7885. (or eq (error "No equation active for current field"))
  7886. (org-table-get-field nil eq)
  7887. (org-table-align)
  7888. (setq org-table-may-need-update t))
  7889. (let* (fields
  7890. (ndown (if (integerp arg) arg 1))
  7891. (org-table-automatic-realign nil)
  7892. (case-fold-search nil)
  7893. (down (> ndown 1))
  7894. (formula (if (and equation suppress-store)
  7895. equation
  7896. (org-table-get-formula equation (equal arg '(4)))))
  7897. (n0 (org-table-current-column))
  7898. (modes (copy-sequence org-calc-default-modes))
  7899. (numbers nil) ; was a variable, now fixed default
  7900. (keep-empty nil)
  7901. n form form0 bw fmt x ev orig c lispp literal)
  7902. ;; Parse the format string. Since we have a lot of modes, this is
  7903. ;; a lot of work. However, I think calc still uses most of the time.
  7904. (if (string-match ";" formula)
  7905. (let ((tmp (org-split-string formula ";")))
  7906. (setq formula (car tmp)
  7907. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  7908. (nth 1 tmp)))
  7909. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  7910. (setq c (string-to-char (match-string 1 fmt))
  7911. n (string-to-number (match-string 2 fmt)))
  7912. (if (= c ?p)
  7913. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  7914. (setq modes (org-set-calc-mode
  7915. 'calc-float-format
  7916. (list (cdr (assoc c '((?n . float) (?f . fix)
  7917. (?s . sci) (?e . eng))))
  7918. n))))
  7919. (setq fmt (replace-match "" t t fmt)))
  7920. (if (string-match "[NT]" fmt)
  7921. (setq numbers (equal (match-string 0 fmt) "N")
  7922. fmt (replace-match "" t t fmt)))
  7923. (if (string-match "L" fmt)
  7924. (setq literal t
  7925. fmt (replace-match "" t t fmt)))
  7926. (if (string-match "E" fmt)
  7927. (setq keep-empty t
  7928. fmt (replace-match "" t t fmt)))
  7929. (while (string-match "[DRFS]" fmt)
  7930. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  7931. (setq fmt (replace-match "" t t fmt)))
  7932. (unless (string-match "\\S-" fmt)
  7933. (setq fmt nil))))
  7934. (if (and (not suppress-const) org-table-formula-use-constants)
  7935. (setq formula (org-table-formula-substitute-names formula)))
  7936. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  7937. (while (> ndown 0)
  7938. (setq fields (org-split-string
  7939. (org-no-properties
  7940. (buffer-substring (point-at-bol) (point-at-eol)))
  7941. " *| *"))
  7942. (if (eq numbers t)
  7943. (setq fields (mapcar
  7944. (lambda (x) (number-to-string (string-to-number x)))
  7945. fields)))
  7946. (setq ndown (1- ndown))
  7947. (setq form (copy-sequence formula)
  7948. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  7949. (if (and lispp literal) (setq lispp 'literal))
  7950. ;; Check for old vertical references
  7951. (setq form (org-rewrite-old-row-references form))
  7952. ;; Insert complex ranges
  7953. (while (string-match org-table-range-regexp form)
  7954. (setq form
  7955. (replace-match
  7956. (save-match-data
  7957. (org-table-make-reference
  7958. (org-table-get-range (match-string 0 form) nil n0)
  7959. keep-empty numbers lispp))
  7960. t t form)))
  7961. ;; Insert simple ranges
  7962. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  7963. (setq form
  7964. (replace-match
  7965. (save-match-data
  7966. (org-table-make-reference
  7967. (org-sublist
  7968. fields (string-to-number (match-string 1 form))
  7969. (string-to-number (match-string 2 form)))
  7970. keep-empty numbers lispp))
  7971. t t form)))
  7972. (setq form0 form)
  7973. ;; Insert the references to fields in same row
  7974. (while (string-match "\\$\\([0-9]+\\)" form)
  7975. (setq n (string-to-number (match-string 1 form))
  7976. x (nth (1- (if (= n 0) n0 n)) fields))
  7977. (unless x (error "Invalid field specifier \"%s\""
  7978. (match-string 0 form)))
  7979. (setq form (replace-match
  7980. (save-match-data
  7981. (org-table-make-reference x nil numbers lispp))
  7982. t t form)))
  7983. (if lispp
  7984. (setq ev (condition-case nil
  7985. (eval (eval (read form)))
  7986. (error "#ERROR"))
  7987. ev (if (numberp ev) (number-to-string ev) ev))
  7988. (or (fboundp 'calc-eval)
  7989. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  7990. (setq ev (calc-eval (cons form modes)
  7991. (if numbers 'num))))
  7992. (when org-table-formula-debug
  7993. (with-output-to-temp-buffer "*Substitution History*"
  7994. (princ (format "Substitution history of formula
  7995. Orig: %s
  7996. $xyz-> %s
  7997. @r$c-> %s
  7998. $1-> %s\n" orig formula form0 form))
  7999. (if (listp ev)
  8000. (princ (format " %s^\nError: %s"
  8001. (make-string (car ev) ?\-) (nth 1 ev)))
  8002. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  8003. ev (or fmt "NONE")
  8004. (if fmt (format fmt (string-to-number ev)) ev)))))
  8005. (setq bw (get-buffer-window "*Substitution History*"))
  8006. (shrink-window-if-larger-than-buffer bw)
  8007. (unless (and (interactive-p) (not ndown))
  8008. (unless (let (inhibit-redisplay)
  8009. (y-or-n-p "Debugging Formula. Continue to next? "))
  8010. (org-table-align)
  8011. (error "Abort"))
  8012. (delete-window bw)
  8013. (message "")))
  8014. (if (listp ev) (setq fmt nil ev "#ERROR"))
  8015. (org-table-justify-field-maybe
  8016. (if fmt (format fmt (string-to-number ev)) ev))
  8017. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  8018. (call-interactively 'org-return)
  8019. (setq ndown 0)))
  8020. (and down (org-table-maybe-recalculate-line))
  8021. (or suppress-align (and org-table-may-need-update
  8022. (org-table-align))))))
  8023. (defun org-table-put-field-property (prop value)
  8024. (save-excursion
  8025. (put-text-property (progn (skip-chars-backward "^|") (point))
  8026. (progn (skip-chars-forward "^|") (point))
  8027. prop value)))
  8028. (defun org-table-get-range (desc &optional tbeg col highlight)
  8029. "Get a calc vector from a column, accorting to descriptor DESC.
  8030. Optional arguments TBEG and COL can give the beginning of the table and
  8031. the current column, to avoid unnecessary parsing.
  8032. HIGHLIGHT means, just highlight the range."
  8033. (if (not (equal (string-to-char desc) ?@))
  8034. (setq desc (concat "@" desc)))
  8035. (save-excursion
  8036. (or tbeg (setq tbeg (org-table-begin)))
  8037. (or col (setq col (org-table-current-column)))
  8038. (let ((thisline (org-current-line))
  8039. beg end c1 c2 r1 r2 rangep tmp)
  8040. (unless (string-match org-table-range-regexp desc)
  8041. (error "Invalid table range specifier `%s'" desc))
  8042. (setq rangep (match-end 3)
  8043. r1 (and (match-end 1) (match-string 1 desc))
  8044. r2 (and (match-end 4) (match-string 4 desc))
  8045. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  8046. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  8047. (and c1 (setq c1 (+ (string-to-number c1)
  8048. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  8049. (and c2 (setq c2 (+ (string-to-number c2)
  8050. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  8051. (if (equal r1 "") (setq r1 nil))
  8052. (if (equal r2 "") (setq r2 nil))
  8053. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  8054. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  8055. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  8056. (if (not r1) (setq r1 thisline))
  8057. (if (not r2) (setq r2 thisline))
  8058. (if (not c1) (setq c1 col))
  8059. (if (not c2) (setq c2 col))
  8060. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  8061. ;; just one field
  8062. (progn
  8063. (goto-line r1)
  8064. (while (not (looking-at org-table-dataline-regexp))
  8065. (beginning-of-line 2))
  8066. (prog1 (org-trim (org-table-get-field c1))
  8067. (if highlight (org-table-highlight-rectangle (point) (point)))))
  8068. ;; A range, return a vector
  8069. ;; First sort the numbers to get a regular ractangle
  8070. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  8071. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  8072. (goto-line r1)
  8073. (while (not (looking-at org-table-dataline-regexp))
  8074. (beginning-of-line 2))
  8075. (org-table-goto-column c1)
  8076. (setq beg (point))
  8077. (goto-line r2)
  8078. (while (not (looking-at org-table-dataline-regexp))
  8079. (beginning-of-line 0))
  8080. (org-table-goto-column c2)
  8081. (setq end (point))
  8082. (if highlight
  8083. (org-table-highlight-rectangle
  8084. beg (progn (skip-chars-forward "^|\n") (point))))
  8085. ;; return string representation of calc vector
  8086. (mapcar 'org-trim
  8087. (apply 'append (org-table-copy-region beg end)))))))
  8088. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  8089. "Analyze descriptor DESC and retrieve the corresponding line number.
  8090. The cursor is currently in line CLINE, the table begins in line BLINE,
  8091. and TABLE is a vector with line types."
  8092. (if (string-match "^[0-9]+$" desc)
  8093. (aref org-table-dlines (string-to-number desc))
  8094. (setq cline (or cline (org-current-line))
  8095. bline (or bline org-table-current-begin-line)
  8096. table (or table org-table-current-line-types))
  8097. (if (or
  8098. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  8099. ;; 1 2 3 4 5 6
  8100. (and (not (match-end 3)) (not (match-end 6)))
  8101. (and (match-end 3) (match-end 6) (not (match-end 5))))
  8102. (error "invalid row descriptor `%s'" desc))
  8103. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  8104. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  8105. (odir (and (match-end 5) (match-string 5 desc)))
  8106. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  8107. (i (- cline bline))
  8108. (rel (and (match-end 6)
  8109. (or (and (match-end 1) (not (match-end 3)))
  8110. (match-end 5)))))
  8111. (if (and hn (not hdir))
  8112. (progn
  8113. (setq i 0 hdir "+")
  8114. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  8115. (if (and (not hn) on (not odir))
  8116. (error "should never happen");;(aref org-table-dlines on)
  8117. (if (and hn (> hn 0))
  8118. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  8119. (if on
  8120. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  8121. (+ bline i)))))
  8122. (defun org-find-row-type (table i type backwards relative n)
  8123. (let ((l (length table)))
  8124. (while (> n 0)
  8125. (while (and (setq i (+ i (if backwards -1 1)))
  8126. (>= i 0) (< i l)
  8127. (not (eq (aref table i) type))
  8128. (if (and relative (eq (aref table i) 'hline))
  8129. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  8130. t)))
  8131. (setq n (1- n)))
  8132. (if (or (< i 0) (>= i l))
  8133. (error "Row descriptior leads outside table")
  8134. i)))
  8135. (defun org-rewrite-old-row-references (s)
  8136. (if (string-match "&[-+0-9I]" s)
  8137. (error "Formula contains old &row reference, please rewrite using @-syntax")
  8138. s))
  8139. (defun org-table-make-reference (elements keep-empty numbers lispp)
  8140. "Convert list ELEMENTS to something appropriate to insert into formula.
  8141. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  8142. NUMBERS indicates that everything should be converted to numbers.
  8143. LISPP means to return something appropriate for a Lisp list."
  8144. (if (stringp elements) ; just a single val
  8145. (if lispp
  8146. (if (eq lispp 'literal)
  8147. elements
  8148. (prin1-to-string (if numbers (string-to-number elements) elements)))
  8149. (if (equal elements "") (setq elements "0"))
  8150. (if numbers (number-to-string (string-to-number elements)) elements))
  8151. (unless keep-empty
  8152. (setq elements
  8153. (delq nil
  8154. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  8155. elements))))
  8156. (setq elements (or elements '("0")))
  8157. (if lispp
  8158. (mapconcat
  8159. (lambda (x)
  8160. (if (eq lispp 'literal)
  8161. x
  8162. (prin1-to-string (if numbers (string-to-number x) x))))
  8163. elements " ")
  8164. (concat "[" (mapconcat
  8165. (lambda (x)
  8166. (if numbers (number-to-string (string-to-number x)) x))
  8167. elements
  8168. ",") "]"))))
  8169. (defun org-table-recalculate (&optional all noalign)
  8170. "Recalculate the current table line by applying all stored formulas.
  8171. With prefix arg ALL, do this for all lines in the table."
  8172. (interactive "P")
  8173. (or (memq this-command org-recalc-commands)
  8174. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8175. (unless (org-at-table-p) (error "Not at a table"))
  8176. (if (equal all '(16))
  8177. (org-table-iterate)
  8178. (org-table-get-specials)
  8179. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8180. (lambda (a b) (string< (car a) (car b)))))
  8181. (inhibit-redisplay (not debug-on-error))
  8182. (line-re org-table-dataline-regexp)
  8183. (thisline (org-current-line))
  8184. (thiscol (org-table-current-column))
  8185. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8186. ;; Insert constants in all formulas
  8187. (setq eqlist
  8188. (mapcar (lambda (x)
  8189. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8190. x)
  8191. eqlist))
  8192. ;; Split the equation list
  8193. (while (setq eq (pop eqlist))
  8194. (if (<= (string-to-char (car eq)) ?9)
  8195. (push eq eqlnum)
  8196. (push eq eqlname)))
  8197. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8198. (if all
  8199. (progn
  8200. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8201. (goto-char (setq beg (org-table-begin)))
  8202. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8203. ;; This is a table with marked lines, compute selected lines
  8204. (setq line-re org-table-recalculate-regexp)
  8205. ;; Move forward to the first non-header line
  8206. (if (and (re-search-forward org-table-dataline-regexp end t)
  8207. (re-search-forward org-table-hline-regexp end t)
  8208. (re-search-forward org-table-dataline-regexp end t))
  8209. (setq beg (match-beginning 0))
  8210. nil))) ;; just leave beg where it is
  8211. (setq beg (point-at-bol)
  8212. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8213. (goto-char beg)
  8214. (and all (message "Re-applying formulas to full table..."))
  8215. ;; First find the named fields, and mark them untouchanble
  8216. (remove-text-properties beg end '(org-untouchable t))
  8217. (while (setq eq (pop eqlname))
  8218. (setq name (car eq)
  8219. a (assoc name org-table-named-field-locations))
  8220. (and (not a)
  8221. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8222. (setq a (list name
  8223. (aref org-table-dlines
  8224. (string-to-number (match-string 1 name)))
  8225. (string-to-number (match-string 2 name)))))
  8226. (when (and a (or all (equal (nth 1 a) thisline)))
  8227. (message "Re-applying formula to field: %s" name)
  8228. (goto-line (nth 1 a))
  8229. (org-table-goto-column (nth 2 a))
  8230. (push (append a (list (cdr eq))) eqlname1)
  8231. ;; FIXME (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
  8232. ;; FIXME 'nostore 'noanalysis)
  8233. (org-table-put-field-property :org-untouchable t)))
  8234. ;; Now evauluate the column formulas, but skip fields covered by
  8235. ;; field formulas
  8236. (goto-char beg)
  8237. (while (re-search-forward line-re end t)
  8238. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8239. ;; Unprotected line, recalculate
  8240. (and all (message "Re-applying formulas to full table...(line %d)"
  8241. (setq cnt (1+ cnt))))
  8242. (setq org-last-recalc-line (org-current-line))
  8243. (setq eql eqlnum)
  8244. (while (setq entry (pop eql))
  8245. (goto-line org-last-recalc-line)
  8246. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8247. (unless (get-text-property (point) :org-untouchable)
  8248. (org-table-eval-formula nil (cdr entry)
  8249. 'noalign 'nocst 'nostore 'noanalysis)))))
  8250. ;; Now evaluate the field formulas
  8251. (while (setq eq (pop eqlname1))
  8252. (message "Re-applying formula to field: %s" (car eq))
  8253. (goto-line (nth 1 eq))
  8254. (org-table-goto-column (nth 2 eq))
  8255. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  8256. 'nostore 'noanalysis))
  8257. (goto-line thisline)
  8258. (org-table-goto-column thiscol)
  8259. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  8260. (or noalign (and org-table-may-need-update (org-table-align))
  8261. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  8262. ;; back to initial position
  8263. (message "Re-applying formulas...done")
  8264. (goto-line thisline)
  8265. (org-table-goto-column thiscol)
  8266. (or noalign (and org-table-may-need-update (org-table-align))
  8267. (and all (message "Re-applying formulas...done"))))))
  8268. (defun org-table-iterate (&optional arg)
  8269. "Recalculate the table until it does not change anymore."
  8270. (interactive "P")
  8271. (let ((imax (if arg (prefix-numeric-value arg) 10))
  8272. (i 0)
  8273. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  8274. thistbl)
  8275. (catch 'exit
  8276. (while (< i imax)
  8277. (setq i (1+ i))
  8278. (org-table-recalculate 'all)
  8279. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  8280. (if (not (string= lasttbl thistbl))
  8281. (setq lasttbl thistbl)
  8282. (if (> i 1)
  8283. (message "Convergence after %d iterations" i)
  8284. (message "Table was already stable"))
  8285. (throw 'exit t)))
  8286. (error "No convergence after %d iterations" i))))
  8287. (defun org-table-formula-substitute-names (f)
  8288. "Replace $const with values in string F."
  8289. (let ((start 0) a (f1 f))
  8290. ;; First, check for column names
  8291. (while (setq start (string-match org-table-column-name-regexp f start))
  8292. (setq start (1+ start))
  8293. (setq a (assoc (match-string 1 f) org-table-column-names))
  8294. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  8295. ;; Parameters and constants
  8296. (setq start 0)
  8297. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  8298. (setq start (1+ start))
  8299. (if (setq a (save-match-data
  8300. (org-table-get-constant (match-string 1 f))))
  8301. (setq f (replace-match (concat "(" a ")") t t f))))
  8302. (if org-table-formula-debug
  8303. (put-text-property 0 (length f) :orig-formula f1 f))
  8304. f))
  8305. (defun org-table-get-constant (const)
  8306. "Find the value for a parameter or constant in a formula.
  8307. Parameters get priority."
  8308. (or (cdr (assoc const org-table-local-parameters))
  8309. (cdr (assoc const org-table-formula-constants-local))
  8310. (cdr (assoc const org-table-formula-constants))
  8311. (and (fboundp 'constants-get) (constants-get const))
  8312. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  8313. (org-entry-get nil (substring const 5) 'inherit))
  8314. "#UNDEFINED_NAME"))
  8315. (defvar org-table-fedit-map
  8316. (let ((map (make-sparse-keymap)))
  8317. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  8318. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  8319. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  8320. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  8321. (org-defkey map "\C-c?" 'org-table-show-reference)
  8322. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  8323. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  8324. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  8325. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  8326. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  8327. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  8328. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  8329. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  8330. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  8331. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  8332. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  8333. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  8334. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  8335. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  8336. map))
  8337. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  8338. '("Edit-Formulas"
  8339. ["Finish and Install" org-table-fedit-finish t]
  8340. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  8341. ["Abort" org-table-fedit-abort t]
  8342. "--"
  8343. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  8344. ["Complete Lisp Symbol" lisp-complete-symbol t]
  8345. "--"
  8346. "Shift Reference at Point"
  8347. ["Up" org-table-fedit-ref-up t]
  8348. ["Down" org-table-fedit-ref-down t]
  8349. ["Left" org-table-fedit-ref-left t]
  8350. ["Right" org-table-fedit-ref-right t]
  8351. "-"
  8352. "Change Test Row for Column Formulas"
  8353. ["Up" org-table-fedit-line-up t]
  8354. ["Down" org-table-fedit-line-down t]
  8355. "--"
  8356. ["Scroll Table Window" org-table-fedit-scroll t]
  8357. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  8358. ["Show Table Grid" org-table-fedit-toggle-coordinates
  8359. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  8360. org-table-overlay-coordinates)]
  8361. "--"
  8362. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  8363. :style toggle :selected org-table-buffer-is-an]))
  8364. (defvar org-pos)
  8365. (defun org-table-edit-formulas ()
  8366. "Edit the formulas of the current table in a separate buffer."
  8367. (interactive)
  8368. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  8369. (beginning-of-line 0))
  8370. (unless (org-at-table-p) (error "Not at a table"))
  8371. (org-table-get-specials)
  8372. (let ((key (org-table-current-field-formula 'key 'noerror))
  8373. (eql (sort (org-table-get-stored-formulas 'noerror)
  8374. 'org-table-formula-less-p))
  8375. (pos (move-marker (make-marker) (point)))
  8376. (startline 1)
  8377. (wc (current-window-configuration))
  8378. (titles '((column . "# Column Formulas\n")
  8379. (field . "# Field Formulas\n")
  8380. (named . "# Named Field Formulas\n")))
  8381. entry s type title)
  8382. (switch-to-buffer-other-window "*Edit Formulas*")
  8383. (erase-buffer)
  8384. ;; Keep global-font-lock-mode from turning on font-lock-mode
  8385. (let ((font-lock-global-modes '(not fundamental-mode)))
  8386. (fundamental-mode))
  8387. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  8388. (org-set-local 'org-pos pos)
  8389. (org-set-local 'org-window-configuration wc)
  8390. (use-local-map org-table-fedit-map)
  8391. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  8392. (easy-menu-add org-table-fedit-menu)
  8393. (setq startline (org-current-line))
  8394. (while (setq entry (pop eql))
  8395. (setq type (cond
  8396. ((equal (string-to-char (car entry)) ?@) 'field)
  8397. ((string-match "^[0-9]" (car entry)) 'column)
  8398. (t 'named)))
  8399. (when (setq title (assq type titles))
  8400. (or (bobp) (insert "\n"))
  8401. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  8402. (setq titles (delq title titles)))
  8403. (if (equal key (car entry)) (setq startline (org-current-line)))
  8404. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  8405. (car entry) " = " (cdr entry) "\n"))
  8406. (remove-text-properties 0 (length s) '(face nil) s)
  8407. (insert s))
  8408. (if (eq org-table-use-standard-references t)
  8409. (org-table-fedit-toggle-ref-type))
  8410. (goto-line startline)
  8411. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  8412. (defun org-table-fedit-post-command ()
  8413. (when (not (memq this-command '(lisp-complete-symbol)))
  8414. (let ((win (selected-window)))
  8415. (save-excursion
  8416. (condition-case nil
  8417. (org-table-show-reference)
  8418. (error nil))
  8419. (select-window win)))))
  8420. (defun org-table-formula-to-user (s)
  8421. "Convert a formula from internal to user representation."
  8422. (if (eq org-table-use-standard-references t)
  8423. (org-table-convert-refs-to-an s)
  8424. s))
  8425. (defun org-table-formula-from-user (s)
  8426. "Convert a formula from user to internal representation."
  8427. (if org-table-use-standard-references
  8428. (org-table-convert-refs-to-rc s)
  8429. s))
  8430. (defun org-table-convert-refs-to-rc (s)
  8431. "Convert spreadsheet references from AB7 to @7$28.
  8432. Works for single references, but also for entire formulas and even the
  8433. full TBLFM line."
  8434. (let ((start 0))
  8435. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  8436. (cond
  8437. ((match-end 3)
  8438. ;; format match, just advance
  8439. (setq start (match-end 0)))
  8440. ((and (> (match-beginning 0) 0)
  8441. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  8442. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  8443. ;; 3.e5 or something like this. FIXME: is this ok????
  8444. (setq start (match-end 0)))
  8445. (t
  8446. (setq start (match-beginning 0)
  8447. s (replace-match
  8448. (if (equal (match-string 2 s) "&")
  8449. (format "$%d" (org-letters-to-number (match-string 1 s)))
  8450. (format "@%d$%d"
  8451. (string-to-number (match-string 2 s))
  8452. (org-letters-to-number (match-string 1 s))))
  8453. t t s)))))
  8454. s))
  8455. (defun org-table-convert-refs-to-an (s)
  8456. "Convert spreadsheet references from to @7$28 to AB7.
  8457. Works for single references, but also for entire formulas and even the
  8458. full TBLFM line."
  8459. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  8460. (setq s (replace-match
  8461. (format "%s%d"
  8462. (org-number-to-letters
  8463. (string-to-number (match-string 2 s)))
  8464. (string-to-number (match-string 1 s)))
  8465. t t s)))
  8466. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  8467. (setq s (replace-match (concat "\\1"
  8468. (org-number-to-letters
  8469. (string-to-number (match-string 2 s))) "&")
  8470. t nil s)))
  8471. s)
  8472. (defun org-letters-to-number (s)
  8473. "Convert a base 26 number represented by letters into an integer.
  8474. For example: AB -> 28."
  8475. (let ((n 0))
  8476. (setq s (upcase s))
  8477. (while (> (length s) 0)
  8478. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  8479. s (substring s 1)))
  8480. n))
  8481. (defun org-number-to-letters (n)
  8482. "Convert an integer into a base 26 number represented by letters.
  8483. For example: 28 -> AB."
  8484. (let ((s ""))
  8485. (while (> n 0)
  8486. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  8487. n (/ (1- n) 26)))
  8488. s))
  8489. (defun org-table-fedit-convert-buffer (function)
  8490. "Convert all references in this buffer, using FUNTION."
  8491. (let ((line (org-current-line)))
  8492. (goto-char (point-min))
  8493. (while (not (eobp))
  8494. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  8495. (delete-region (point) (point-at-eol))
  8496. (or (eobp) (forward-char 1)))
  8497. (goto-line line)))
  8498. (defun org-table-fedit-toggle-ref-type ()
  8499. "Convert all references in the buffer from B3 to @3$2 and back."
  8500. (interactive)
  8501. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  8502. (org-table-fedit-convert-buffer
  8503. (if org-table-buffer-is-an
  8504. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  8505. (message "Reference type switched to %s"
  8506. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  8507. (defun org-table-fedit-ref-up ()
  8508. "Shift the reference at point one row/hline up."
  8509. (interactive)
  8510. (org-table-fedit-shift-reference 'up))
  8511. (defun org-table-fedit-ref-down ()
  8512. "Shift the reference at point one row/hline down."
  8513. (interactive)
  8514. (org-table-fedit-shift-reference 'down))
  8515. (defun org-table-fedit-ref-left ()
  8516. "Shift the reference at point one field to the left."
  8517. (interactive)
  8518. (org-table-fedit-shift-reference 'left))
  8519. (defun org-table-fedit-ref-right ()
  8520. "Shift the reference at point one field to the right."
  8521. (interactive)
  8522. (org-table-fedit-shift-reference 'right))
  8523. (defun org-table-fedit-shift-reference (dir)
  8524. (cond
  8525. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  8526. (if (memq dir '(left right))
  8527. (org-rematch-and-replace 1 (eq dir 'left))
  8528. (error "Cannot shift reference in this direction")))
  8529. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  8530. ;; A B3-like reference
  8531. (if (memq dir '(up down))
  8532. (org-rematch-and-replace 2 (eq dir 'up))
  8533. (org-rematch-and-replace 1 (eq dir 'left))))
  8534. ((org-at-regexp-p
  8535. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  8536. ;; An internal reference
  8537. (if (memq dir '(up down))
  8538. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  8539. (org-rematch-and-replace 5 (eq dir 'left))))))
  8540. (defun org-rematch-and-replace (n &optional decr hline)
  8541. "Re-match the group N, and replace it with the shifted refrence."
  8542. (or (match-end n) (error "Cannot shift reference in this direction"))
  8543. (goto-char (match-beginning n))
  8544. (and (looking-at (regexp-quote (match-string n)))
  8545. (replace-match (org-shift-refpart (match-string 0) decr hline)
  8546. t t)))
  8547. (defun org-shift-refpart (ref &optional decr hline)
  8548. "Shift a refrence part REF.
  8549. If DECR is set, decrease the references row/column, else increase.
  8550. If HLINE is set, this may be a hline reference, it certainly is not
  8551. a translation reference."
  8552. (save-match-data
  8553. (let* ((sign (string-match "^[-+]" ref)) n)
  8554. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  8555. (cond
  8556. ((and hline (string-match "^I+" ref))
  8557. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  8558. (setq n (+ n (if decr -1 1)))
  8559. (if (= n 0) (setq n (+ n (if decr -1 1))))
  8560. (if sign
  8561. (setq sign (if (< n 0) "-" "+") n (abs n))
  8562. (setq n (max 1 n)))
  8563. (concat sign (make-string n ?I)))
  8564. ((string-match "^[0-9]+" ref)
  8565. (setq n (string-to-number (concat sign ref)))
  8566. (setq n (+ n (if decr -1 1)))
  8567. (if sign
  8568. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  8569. (number-to-string (max 1 n))))
  8570. ((string-match "^[a-zA-Z]+" ref)
  8571. (org-number-to-letters
  8572. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  8573. (t (error "Cannot shift reference"))))))
  8574. (defun org-table-fedit-toggle-coordinates ()
  8575. "Toggle the display of coordinates in the refrenced table."
  8576. (interactive)
  8577. (let ((pos (marker-position org-pos)))
  8578. (with-current-buffer (marker-buffer org-pos)
  8579. (save-excursion
  8580. (goto-char pos)
  8581. (org-table-toggle-coordinate-overlays)))))
  8582. (defun org-table-fedit-finish (&optional arg)
  8583. "Parse the buffer for formula definitions and install them.
  8584. With prefix ARG, apply the new formulas to the table."
  8585. (interactive "P")
  8586. (org-table-remove-rectangle-highlight)
  8587. (if org-table-use-standard-references
  8588. (progn
  8589. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  8590. (setq org-table-buffer-is-an nil)))
  8591. (let ((pos org-pos) eql var form)
  8592. (goto-char (point-min))
  8593. (while (re-search-forward
  8594. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  8595. nil t)
  8596. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  8597. form (match-string 3))
  8598. (setq form (org-trim form))
  8599. (when (not (equal form ""))
  8600. (while (string-match "[ \t]*\n[ \t]*" form)
  8601. (setq form (replace-match " " t t form)))
  8602. (when (assoc var eql)
  8603. (error "Double formulas for %s" var))
  8604. (push (cons var form) eql)))
  8605. (setq org-pos nil)
  8606. (set-window-configuration org-window-configuration)
  8607. (select-window (get-buffer-window (marker-buffer pos)))
  8608. (goto-char pos)
  8609. (unless (org-at-table-p)
  8610. (error "Lost table position - cannot install formulae"))
  8611. (org-table-store-formulas eql)
  8612. (move-marker pos nil)
  8613. (kill-buffer "*Edit Formulas*")
  8614. (if arg
  8615. (org-table-recalculate 'all)
  8616. (message "New formulas installed - press C-u C-c C-c to apply."))))
  8617. (defun org-table-fedit-abort ()
  8618. "Abort editing formulas, without installing the changes."
  8619. (interactive)
  8620. (org-table-remove-rectangle-highlight)
  8621. (let ((pos org-pos))
  8622. (set-window-configuration org-window-configuration)
  8623. (select-window (get-buffer-window (marker-buffer pos)))
  8624. (goto-char pos)
  8625. (move-marker pos nil)
  8626. (message "Formula editing aborted without installing changes")))
  8627. (defun org-table-fedit-lisp-indent ()
  8628. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  8629. (interactive)
  8630. (let ((pos (point)) beg end ind)
  8631. (beginning-of-line 1)
  8632. (cond
  8633. ((looking-at "[ \t]")
  8634. (goto-char pos)
  8635. (call-interactively 'lisp-indent-line))
  8636. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  8637. ((not (fboundp 'pp-buffer))
  8638. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  8639. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  8640. (goto-char (- (match-end 0) 2))
  8641. (setq beg (point))
  8642. (setq ind (make-string (current-column) ?\ ))
  8643. (condition-case nil (forward-sexp 1)
  8644. (error
  8645. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  8646. (setq end (point))
  8647. (save-restriction
  8648. (narrow-to-region beg end)
  8649. (if (eq last-command this-command)
  8650. (progn
  8651. (goto-char (point-min))
  8652. (setq this-command nil)
  8653. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  8654. (replace-match " ")))
  8655. (pp-buffer)
  8656. (untabify (point-min) (point-max))
  8657. (goto-char (1+ (point-min)))
  8658. (while (re-search-forward "^." nil t)
  8659. (beginning-of-line 1)
  8660. (insert ind))
  8661. (goto-char (point-max))
  8662. (backward-delete-char 1)))
  8663. (goto-char beg))
  8664. (t nil))))
  8665. (defvar org-show-positions nil)
  8666. (defun org-table-show-reference (&optional local)
  8667. "Show the location/value of the $ expression at point."
  8668. (interactive)
  8669. (org-table-remove-rectangle-highlight)
  8670. (catch 'exit
  8671. (let ((pos (if local (point) org-pos))
  8672. (face2 'highlight)
  8673. (org-inhibit-highlight-removal t)
  8674. (win (selected-window))
  8675. (org-show-positions nil)
  8676. var name e what match dest)
  8677. (if local (org-table-get-specials))
  8678. (setq what (cond
  8679. ((or (org-at-regexp-p org-table-range-regexp2)
  8680. (org-at-regexp-p org-table-translate-regexp)
  8681. (org-at-regexp-p org-table-range-regexp))
  8682. (setq match
  8683. (save-match-data
  8684. (org-table-convert-refs-to-rc (match-string 0))))
  8685. 'range)
  8686. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  8687. ((org-at-regexp-p "\\$[0-9]+") 'column)
  8688. ((not local) nil)
  8689. (t (error "No reference at point")))
  8690. match (and what (or match (match-string 0))))
  8691. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  8692. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  8693. 'secondary-selection))
  8694. (org-add-hook 'before-change-functions
  8695. 'org-table-remove-rectangle-highlight)
  8696. (if (eq what 'name) (setq var (substring match 1)))
  8697. (when (eq what 'range)
  8698. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  8699. (setq match (org-table-formula-substitute-names match)))
  8700. (unless local
  8701. (save-excursion
  8702. (end-of-line 1)
  8703. (re-search-backward "^\\S-" nil t)
  8704. (beginning-of-line 1)
  8705. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  8706. (setq dest
  8707. (save-match-data
  8708. (org-table-convert-refs-to-rc (match-string 1))))
  8709. (org-table-add-rectangle-overlay
  8710. (match-beginning 1) (match-end 1) face2))))
  8711. (if (and (markerp pos) (marker-buffer pos))
  8712. (if (get-buffer-window (marker-buffer pos))
  8713. (select-window (get-buffer-window (marker-buffer pos)))
  8714. (switch-to-buffer-other-window (get-buffer-window
  8715. (marker-buffer pos)))))
  8716. (goto-char pos)
  8717. (org-table-force-dataline)
  8718. (when dest
  8719. (setq name (substring dest 1))
  8720. (cond
  8721. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  8722. (setq e (assoc name org-table-named-field-locations))
  8723. (goto-line (nth 1 e))
  8724. (org-table-goto-column (nth 2 e)))
  8725. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  8726. (let ((l (string-to-number (match-string 1 dest)))
  8727. (c (string-to-number (match-string 2 dest))))
  8728. (goto-line (aref org-table-dlines l))
  8729. (org-table-goto-column c)))
  8730. (t (org-table-goto-column (string-to-number name))))
  8731. (move-marker pos (point))
  8732. (org-table-highlight-rectangle nil nil face2))
  8733. (cond
  8734. ((equal dest match))
  8735. ((not match))
  8736. ((eq what 'range)
  8737. (condition-case nil
  8738. (save-excursion
  8739. (org-table-get-range match nil nil 'highlight))
  8740. (error nil)))
  8741. ((setq e (assoc var org-table-named-field-locations))
  8742. (goto-line (nth 1 e))
  8743. (org-table-goto-column (nth 2 e))
  8744. (org-table-highlight-rectangle (point) (point))
  8745. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  8746. ((setq e (assoc var org-table-column-names))
  8747. (org-table-goto-column (string-to-number (cdr e)))
  8748. (org-table-highlight-rectangle (point) (point))
  8749. (goto-char (org-table-begin))
  8750. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  8751. (org-table-end) t)
  8752. (progn
  8753. (goto-char (match-beginning 1))
  8754. (org-table-highlight-rectangle)
  8755. (message "Named column (column %s)" (cdr e)))
  8756. (error "Column name not found")))
  8757. ((eq what 'column)
  8758. ;; column number
  8759. (org-table-goto-column (string-to-number (substring match 1)))
  8760. (org-table-highlight-rectangle (point) (point))
  8761. (message "Column %s" (substring match 1)))
  8762. ((setq e (assoc var org-table-local-parameters))
  8763. (goto-char (org-table-begin))
  8764. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  8765. (progn
  8766. (goto-char (match-beginning 1))
  8767. (org-table-highlight-rectangle)
  8768. (message "Local parameter."))
  8769. (error "Parameter not found")))
  8770. (t
  8771. (cond
  8772. ((not var) (error "No reference at point"))
  8773. ((setq e (assoc var org-table-formula-constants-local))
  8774. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  8775. var (cdr e)))
  8776. ((setq e (assoc var org-table-formula-constants))
  8777. (message "Constant: $%s=%s in `org-table-formula-constants'."
  8778. var (cdr e)))
  8779. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  8780. (message "Constant: $%s=%s, from `constants.el'%s."
  8781. var e (format " (%s units)" constants-unit-system)))
  8782. (t (error "Undefined name $%s" var)))))
  8783. (goto-char pos)
  8784. (when (and org-show-positions
  8785. (not (memq this-command '(org-table-fedit-scroll
  8786. org-table-fedit-scroll-down))))
  8787. (push pos org-show-positions)
  8788. (push org-table-current-begin-pos org-show-positions)
  8789. (let ((min (apply 'min org-show-positions))
  8790. (max (apply 'max org-show-positions)))
  8791. (goto-char min) (recenter 0)
  8792. (goto-char max)
  8793. (or (pos-visible-in-window-p max) (recenter -1))))
  8794. (select-window win))))
  8795. (defun org-table-force-dataline ()
  8796. "Make sure the cursor is in a dataline in a table."
  8797. (unless (save-excursion
  8798. (beginning-of-line 1)
  8799. (looking-at org-table-dataline-regexp))
  8800. (let* ((re org-table-dataline-regexp)
  8801. (p1 (save-excursion (re-search-forward re nil 'move)))
  8802. (p2 (save-excursion (re-search-backward re nil 'move))))
  8803. (cond ((and p1 p2)
  8804. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  8805. p1 p2)))
  8806. ((or p1 p2) (goto-char (or p1 p2)))
  8807. (t (error "No table dataline around here"))))))
  8808. (defun org-table-fedit-line-up ()
  8809. "Move cursor one line up in the window showing the table."
  8810. (interactive)
  8811. (org-table-fedit-move 'previous-line))
  8812. (defun org-table-fedit-line-down ()
  8813. "Move cursor one line down in the window showing the table."
  8814. (interactive)
  8815. (org-table-fedit-move 'next-line))
  8816. (defun org-table-fedit-move (command)
  8817. "Move the cursor in the window shoinw the table.
  8818. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  8819. (let ((org-table-allow-automatic-line-recalculation nil)
  8820. (pos org-pos) (win (selected-window)) p)
  8821. (select-window (get-buffer-window (marker-buffer org-pos)))
  8822. (setq p (point))
  8823. (call-interactively command)
  8824. (while (and (org-at-table-p)
  8825. (org-at-table-hline-p))
  8826. (call-interactively command))
  8827. (or (org-at-table-p) (goto-char p))
  8828. (move-marker pos (point))
  8829. (select-window win)))
  8830. (defun org-table-fedit-scroll (N)
  8831. (interactive "p")
  8832. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  8833. (scroll-other-window N)))
  8834. (defun org-table-fedit-scroll-down (N)
  8835. (interactive "p")
  8836. (org-table-fedit-scroll (- N)))
  8837. (defvar org-table-rectangle-overlays nil)
  8838. (defun org-table-add-rectangle-overlay (beg end &optional face)
  8839. "Add a new overlay."
  8840. (let ((ov (org-make-overlay beg end)))
  8841. (org-overlay-put ov 'face (or face 'secondary-selection))
  8842. (push ov org-table-rectangle-overlays)))
  8843. (defun org-table-highlight-rectangle (&optional beg end face)
  8844. "Highlight rectangular region in a table."
  8845. (setq beg (or beg (point)) end (or end (point)))
  8846. (let ((b (min beg end))
  8847. (e (max beg end))
  8848. l1 c1 l2 c2 tmp)
  8849. (and (boundp 'org-show-positions)
  8850. (setq org-show-positions (cons b (cons e org-show-positions))))
  8851. (goto-char (min beg end))
  8852. (setq l1 (org-current-line)
  8853. c1 (org-table-current-column))
  8854. (goto-char (max beg end))
  8855. (setq l2 (org-current-line)
  8856. c2 (org-table-current-column))
  8857. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  8858. (goto-line l1)
  8859. (beginning-of-line 1)
  8860. (loop for line from l1 to l2 do
  8861. (when (looking-at org-table-dataline-regexp)
  8862. (org-table-goto-column c1)
  8863. (skip-chars-backward "^|\n") (setq beg (point))
  8864. (org-table-goto-column c2)
  8865. (skip-chars-forward "^|\n") (setq end (point))
  8866. (org-table-add-rectangle-overlay beg end face))
  8867. (beginning-of-line 2))
  8868. (goto-char b))
  8869. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  8870. (defun org-table-remove-rectangle-highlight (&rest ignore)
  8871. "Remove the rectangle overlays."
  8872. (unless org-inhibit-highlight-removal
  8873. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  8874. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  8875. (setq org-table-rectangle-overlays nil)))
  8876. (defvar org-table-coordinate-overlays nil
  8877. "Collects the cooordinate grid overlays, so that they can be removed.")
  8878. (make-variable-buffer-local 'org-table-coordinate-overlays)
  8879. (defun org-table-overlay-coordinates ()
  8880. "Add overlays to the table at point, to show row/column coordinates."
  8881. (interactive)
  8882. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8883. (setq org-table-coordinate-overlays nil)
  8884. (save-excursion
  8885. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  8886. (goto-char (org-table-begin))
  8887. (while (org-at-table-p)
  8888. (setq eol (point-at-eol))
  8889. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  8890. (push ov org-table-coordinate-overlays)
  8891. (setq hline (looking-at org-table-hline-regexp))
  8892. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  8893. (format "%4d" (setq id (1+ id)))))
  8894. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  8895. (when hline
  8896. (setq ic 0)
  8897. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  8898. (setq beg (1+ (match-beginning 0))
  8899. ic (1+ ic)
  8900. s1 (concat "$" (int-to-string ic))
  8901. s2 (org-number-to-letters ic)
  8902. str (if (eq org-table-use-standard-references t) s2 s1))
  8903. (setq ov (org-make-overlay beg (+ beg (length str))))
  8904. (push ov org-table-coordinate-overlays)
  8905. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  8906. (beginning-of-line 2)))))
  8907. (defun org-table-toggle-coordinate-overlays ()
  8908. "Toggle the display of Row/Column numbers in tables."
  8909. (interactive)
  8910. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  8911. (message "Row/Column number display turned %s"
  8912. (if org-table-overlay-coordinates "on" "off"))
  8913. (if (and (org-at-table-p) org-table-overlay-coordinates)
  8914. (org-table-align))
  8915. (unless org-table-overlay-coordinates
  8916. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8917. (setq org-table-coordinate-overlays nil)))
  8918. (defun org-table-toggle-formula-debugger ()
  8919. "Toggle the formula debugger in tables."
  8920. (interactive)
  8921. (setq org-table-formula-debug (not org-table-formula-debug))
  8922. (message "Formula debugging has been turned %s"
  8923. (if org-table-formula-debug "on" "off")))
  8924. ;;; The orgtbl minor mode
  8925. ;; Define a minor mode which can be used in other modes in order to
  8926. ;; integrate the org-mode table editor.
  8927. ;; This is really a hack, because the org-mode table editor uses several
  8928. ;; keys which normally belong to the major mode, for example the TAB and
  8929. ;; RET keys. Here is how it works: The minor mode defines all the keys
  8930. ;; necessary to operate the table editor, but wraps the commands into a
  8931. ;; function which tests if the cursor is currently inside a table. If that
  8932. ;; is the case, the table editor command is executed. However, when any of
  8933. ;; those keys is used outside a table, the function uses `key-binding' to
  8934. ;; look up if the key has an associated command in another currently active
  8935. ;; keymap (minor modes, major mode, global), and executes that command.
  8936. ;; There might be problems if any of the keys used by the table editor is
  8937. ;; otherwise used as a prefix key.
  8938. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  8939. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  8940. ;; addresses this by checking explicitly for both bindings.
  8941. ;; The optimized version (see variable `orgtbl-optimized') takes over
  8942. ;; all keys which are bound to `self-insert-command' in the *global map*.
  8943. ;; Some modes bind other commands to simple characters, for example
  8944. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  8945. ;; active, this binding is ignored inside tables and replaced with a
  8946. ;; modified self-insert.
  8947. (defvar orgtbl-mode nil
  8948. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  8949. table editor in arbitrary modes.")
  8950. (make-variable-buffer-local 'orgtbl-mode)
  8951. (defvar orgtbl-mode-map (make-keymap)
  8952. "Keymap for `orgtbl-mode'.")
  8953. ;;;###autoload
  8954. (defun turn-on-orgtbl ()
  8955. "Unconditionally turn on `orgtbl-mode'."
  8956. (orgtbl-mode 1))
  8957. (defvar org-old-auto-fill-inhibit-regexp nil
  8958. "Local variable used by `orgtbl-mode'")
  8959. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  8960. "Matches a line belonging to an orgtbl.")
  8961. (defconst orgtbl-extra-font-lock-keywords
  8962. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  8963. 0 (quote 'org-table) 'prepend))
  8964. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  8965. ;;;###autoload
  8966. (defun orgtbl-mode (&optional arg)
  8967. "The `org-mode' table editor as a minor mode for use in other modes."
  8968. (interactive)
  8969. (if (org-mode-p)
  8970. ;; Exit without error, in case some hook functions calls this
  8971. ;; by accident in org-mode.
  8972. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  8973. (setq orgtbl-mode
  8974. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  8975. (if orgtbl-mode
  8976. (progn
  8977. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  8978. ;; Make sure we are first in minor-mode-map-alist
  8979. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  8980. (and c (setq minor-mode-map-alist
  8981. (cons c (delq c minor-mode-map-alist)))))
  8982. (org-set-local (quote org-table-may-need-update) t)
  8983. (org-add-hook 'before-change-functions 'org-before-change-function
  8984. nil 'local)
  8985. (org-set-local 'org-old-auto-fill-inhibit-regexp
  8986. auto-fill-inhibit-regexp)
  8987. (org-set-local 'auto-fill-inhibit-regexp
  8988. (if auto-fill-inhibit-regexp
  8989. (concat orgtbl-line-start-regexp "\\|"
  8990. auto-fill-inhibit-regexp)
  8991. orgtbl-line-start-regexp))
  8992. (org-add-to-invisibility-spec '(org-cwidth))
  8993. (when (fboundp 'font-lock-add-keywords)
  8994. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  8995. (org-restart-font-lock))
  8996. (easy-menu-add orgtbl-mode-menu)
  8997. (run-hooks 'orgtbl-mode-hook))
  8998. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  8999. (org-cleanup-narrow-column-properties)
  9000. (org-remove-from-invisibility-spec '(org-cwidth))
  9001. (remove-hook 'before-change-functions 'org-before-change-function t)
  9002. (when (fboundp 'font-lock-remove-keywords)
  9003. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  9004. (org-restart-font-lock))
  9005. (easy-menu-remove orgtbl-mode-menu)
  9006. (force-mode-line-update 'all))))
  9007. (defun org-cleanup-narrow-column-properties ()
  9008. "Remove all properties related to narrow-column invisibility."
  9009. (let ((s 1))
  9010. (while (setq s (text-property-any s (point-max)
  9011. 'display org-narrow-column-arrow))
  9012. (remove-text-properties s (1+ s) '(display t)))
  9013. (setq s 1)
  9014. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  9015. (remove-text-properties s (1+ s) '(org-cwidth t)))
  9016. (setq s 1)
  9017. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  9018. (remove-text-properties s (1+ s) '(invisible t)))))
  9019. ;; Install it as a minor mode.
  9020. (put 'orgtbl-mode :included t)
  9021. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9022. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9023. (defun orgtbl-make-binding (fun n &rest keys)
  9024. "Create a function for binding in the table minor mode.
  9025. FUN is the command to call inside a table. N is used to create a unique
  9026. command name. KEYS are keys that should be checked in for a command
  9027. to execute outside of tables."
  9028. (eval
  9029. (list 'defun
  9030. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9031. '(arg)
  9032. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9033. "Outside of tables, run the binding of `"
  9034. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9035. "'.")
  9036. '(interactive "p")
  9037. (list 'if
  9038. '(org-at-table-p)
  9039. (list 'call-interactively (list 'quote fun))
  9040. (list 'let '(orgtbl-mode)
  9041. (list 'call-interactively
  9042. (append '(or)
  9043. (mapcar (lambda (k)
  9044. (list 'key-binding k))
  9045. keys)
  9046. '('orgtbl-error))))))))
  9047. (defun orgtbl-error ()
  9048. "Error when there is no default binding for a table key."
  9049. (interactive)
  9050. (error "This key is has no function outside tables"))
  9051. (defun orgtbl-setup ()
  9052. "Setup orgtbl keymaps."
  9053. (let ((nfunc 0)
  9054. (bindings
  9055. (list
  9056. '([(meta shift left)] org-table-delete-column)
  9057. '([(meta left)] org-table-move-column-left)
  9058. '([(meta right)] org-table-move-column-right)
  9059. '([(meta shift right)] org-table-insert-column)
  9060. '([(meta shift up)] org-table-kill-row)
  9061. '([(meta shift down)] org-table-insert-row)
  9062. '([(meta up)] org-table-move-row-up)
  9063. '([(meta down)] org-table-move-row-down)
  9064. '("\C-c\C-w" org-table-cut-region)
  9065. '("\C-c\M-w" org-table-copy-region)
  9066. '("\C-c\C-y" org-table-paste-rectangle)
  9067. '("\C-c-" org-table-insert-hline)
  9068. '("\C-c}" org-table-toggle-coordinate-overlays)
  9069. '("\C-c{" org-table-toggle-formula-debugger)
  9070. '("\C-m" org-table-next-row)
  9071. '([(shift return)] org-table-copy-down)
  9072. '("\C-c\C-q" org-table-wrap-region)
  9073. '("\C-c?" org-table-field-info)
  9074. '("\C-c " org-table-blank-field)
  9075. '("\C-c+" org-table-sum)
  9076. '("\C-c=" org-table-eval-formula)
  9077. '("\C-c'" org-table-edit-formulas)
  9078. '("\C-c`" org-table-edit-field)
  9079. '("\C-c*" org-table-recalculate)
  9080. '("\C-c|" org-table-create-or-convert-from-region)
  9081. '("\C-c^" org-table-sort-lines)
  9082. '([(control ?#)] org-table-rotate-recalc-marks)))
  9083. elt key fun cmd)
  9084. (while (setq elt (pop bindings))
  9085. (setq nfunc (1+ nfunc))
  9086. (setq key (org-key (car elt))
  9087. fun (nth 1 elt)
  9088. cmd (orgtbl-make-binding fun nfunc key))
  9089. (org-defkey orgtbl-mode-map key cmd))
  9090. ;; Special treatment needed for TAB and RET
  9091. (org-defkey orgtbl-mode-map [(return)]
  9092. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9093. (org-defkey orgtbl-mode-map "\C-m"
  9094. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9095. (org-defkey orgtbl-mode-map [(tab)]
  9096. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9097. (org-defkey orgtbl-mode-map "\C-i"
  9098. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  9099. (org-defkey orgtbl-mode-map [(shift tab)]
  9100. (orgtbl-make-binding 'org-table-previous-field 104
  9101. [(shift tab)] [(tab)] "\C-i"))
  9102. (org-defkey orgtbl-mode-map "\M-\C-m"
  9103. (orgtbl-make-binding 'org-table-wrap-region 105
  9104. "\M-\C-m" [(meta return)]))
  9105. (org-defkey orgtbl-mode-map [(meta return)]
  9106. (orgtbl-make-binding 'org-table-wrap-region 106
  9107. [(meta return)] "\M-\C-m"))
  9108. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  9109. (when orgtbl-optimized
  9110. ;; If the user wants maximum table support, we need to hijack
  9111. ;; some standard editing functions
  9112. (org-remap orgtbl-mode-map
  9113. 'self-insert-command 'orgtbl-self-insert-command
  9114. 'delete-char 'org-delete-char
  9115. 'delete-backward-char 'org-delete-backward-char)
  9116. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  9117. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9118. '("OrgTbl"
  9119. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9120. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9121. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9122. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9123. "--"
  9124. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9125. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9126. ["Copy Field from Above"
  9127. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9128. "--"
  9129. ("Column"
  9130. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9131. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9132. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9133. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  9134. ("Row"
  9135. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9136. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9137. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9138. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9139. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9140. "--"
  9141. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9142. ("Rectangle"
  9143. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9144. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9145. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9146. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9147. "--"
  9148. ("Radio tables"
  9149. ["Insert table template" orgtbl-insert-radio-table
  9150. (assq major-mode orgtbl-radio-table-templates)]
  9151. ["Comment/uncomment table" orgtbl-toggle-comment t])
  9152. "--"
  9153. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9154. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9155. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9156. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9157. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9158. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  9159. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9160. ["Sum Column/Rectangle" org-table-sum
  9161. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9162. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9163. ["Debug Formulas"
  9164. org-table-toggle-formula-debugger :active (org-at-table-p)
  9165. :keys "C-c {"
  9166. :style toggle :selected org-table-formula-debug]
  9167. ["Show Col/Row Numbers"
  9168. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9169. :keys "C-c }"
  9170. :style toggle :selected org-table-overlay-coordinates]
  9171. ))
  9172. t))
  9173. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9174. "If the cursor is inside a table, realign the table.
  9175. It it is a table to be sent away to a receiver, do it.
  9176. With prefix arg, also recompute table."
  9177. (interactive "P")
  9178. (let ((pos (point)) action)
  9179. (save-excursion
  9180. (beginning-of-line 1)
  9181. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9182. ((looking-at "[ \t]*|") pos)
  9183. ((looking-at "#\\+TBLFM:") 'recalc))))
  9184. (cond
  9185. ((integerp action)
  9186. (goto-char action)
  9187. (org-table-maybe-eval-formula)
  9188. (if arg
  9189. (call-interactively 'org-table-recalculate)
  9190. (org-table-maybe-recalculate-line))
  9191. (call-interactively 'org-table-align)
  9192. (orgtbl-send-table 'maybe))
  9193. ((eq action 'recalc)
  9194. (save-excursion
  9195. (beginning-of-line 1)
  9196. (skip-chars-backward " \r\n\t")
  9197. (if (org-at-table-p)
  9198. (org-call-with-arg 'org-table-recalculate t))))
  9199. (t (let (orgtbl-mode)
  9200. (call-interactively (key-binding "\C-c\C-c")))))))
  9201. (defun orgtbl-tab (arg)
  9202. "Justification and field motion for `orgtbl-mode'."
  9203. (interactive "P")
  9204. (if arg (org-table-edit-field t)
  9205. (org-table-justify-field-maybe)
  9206. (org-table-next-field)))
  9207. (defun orgtbl-ret ()
  9208. "Justification and field motion for `orgtbl-mode'."
  9209. (interactive)
  9210. (org-table-justify-field-maybe)
  9211. (org-table-next-row))
  9212. (defun orgtbl-self-insert-command (N)
  9213. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9214. If the cursor is in a table looking at whitespace, the whitespace is
  9215. overwritten, and the table is not marked as requiring realignment."
  9216. (interactive "p")
  9217. (if (and (org-at-table-p)
  9218. (or
  9219. (and org-table-auto-blank-field
  9220. (member last-command
  9221. '(orgtbl-hijacker-command-100
  9222. orgtbl-hijacker-command-101
  9223. orgtbl-hijacker-command-102
  9224. orgtbl-hijacker-command-103
  9225. orgtbl-hijacker-command-104
  9226. orgtbl-hijacker-command-105))
  9227. (org-table-blank-field))
  9228. t)
  9229. (eq N 1)
  9230. (looking-at "[^|\n]* +|"))
  9231. (let (org-table-may-need-update)
  9232. (goto-char (1- (match-end 0)))
  9233. (delete-backward-char 1)
  9234. (goto-char (match-beginning 0))
  9235. (self-insert-command N))
  9236. (setq org-table-may-need-update t)
  9237. (let (orgtbl-mode)
  9238. (call-interactively (key-binding (vector last-input-event))))))
  9239. (defun org-force-self-insert (N)
  9240. "Needed to enforce self-insert under remapping."
  9241. (interactive "p")
  9242. (self-insert-command N))
  9243. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9244. "Regula expression matching exponentials as produced by calc.")
  9245. (defvar org-table-clean-did-remove-column nil)
  9246. (defun orgtbl-export (table target)
  9247. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9248. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9249. org-table-last-alignment org-table-last-column-widths
  9250. maxcol column)
  9251. (if (not (fboundp func))
  9252. (error "Cannot export orgtbl table to %s" target))
  9253. (setq lines (org-table-clean-before-export lines))
  9254. (setq table
  9255. (mapcar
  9256. (lambda (x)
  9257. (if (string-match org-table-hline-regexp x)
  9258. 'hline
  9259. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  9260. lines))
  9261. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  9262. table)))
  9263. (loop for i from (1- maxcol) downto 0 do
  9264. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  9265. (setq column (delq nil column))
  9266. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  9267. (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))
  9268. (funcall func table nil)))
  9269. (defun orgtbl-send-table (&optional maybe)
  9270. "Send a tranformed version of this table to the receiver position.
  9271. With argument MAYBE, fail quietly if no transformation is defined for
  9272. this table."
  9273. (interactive)
  9274. (catch 'exit
  9275. (unless (org-at-table-p) (error "Not at a table"))
  9276. ;; when non-interactive, we assume align has just happened.
  9277. (when (interactive-p) (org-table-align))
  9278. (save-excursion
  9279. (goto-char (org-table-begin))
  9280. (beginning-of-line 0)
  9281. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  9282. (if maybe
  9283. (throw 'exit nil)
  9284. (error "Don't know how to transform this table."))))
  9285. (let* ((name (match-string 1))
  9286. beg
  9287. (transform (intern (match-string 2)))
  9288. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  9289. (skip (plist-get params :skip))
  9290. (skipcols (plist-get params :skipcols))
  9291. (txt (buffer-substring-no-properties
  9292. (org-table-begin) (org-table-end)))
  9293. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  9294. (lines (org-table-clean-before-export lines))
  9295. (i0 (if org-table-clean-did-remove-column 2 1))
  9296. (table (mapcar
  9297. (lambda (x)
  9298. (if (string-match org-table-hline-regexp x)
  9299. 'hline
  9300. (org-remove-by-index
  9301. (org-split-string (org-trim x) "\\s-*|\\s-*")
  9302. skipcols i0)))
  9303. lines))
  9304. (fun (if (= i0 2) 'cdr 'identity))
  9305. (org-table-last-alignment
  9306. (org-remove-by-index (funcall fun org-table-last-alignment)
  9307. skipcols i0))
  9308. (org-table-last-column-widths
  9309. (org-remove-by-index (funcall fun org-table-last-column-widths)
  9310. skipcols i0)))
  9311. (unless (fboundp transform)
  9312. (error "No such transformation function %s" transform))
  9313. (setq txt (funcall transform table params))
  9314. ;; Find the insertion place
  9315. (save-excursion
  9316. (goto-char (point-min))
  9317. (unless (re-search-forward
  9318. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  9319. (error "Don't know where to insert translated table"))
  9320. (goto-char (match-beginning 0))
  9321. (beginning-of-line 2)
  9322. (setq beg (point))
  9323. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  9324. (error "Cannot find end of insertion region"))
  9325. (beginning-of-line 1)
  9326. (delete-region beg (point))
  9327. (goto-char beg)
  9328. (insert txt "\n"))
  9329. (message "Table converted and installed at receiver location"))))
  9330. (defun org-remove-by-index (list indices &optional i0)
  9331. "Remove the elements in LIST with indices in INDICES.
  9332. First element has index 0, or I0 if given."
  9333. (if (not indices)
  9334. list
  9335. (if (integerp indices) (setq indices (list indices)))
  9336. (setq i0 (1- (or i0 0)))
  9337. (delq :rm (mapcar (lambda (x)
  9338. (setq i0 (1+ i0))
  9339. (if (memq i0 indices) :rm x))
  9340. list))))
  9341. (defun orgtbl-toggle-comment ()
  9342. "Comment or uncomment the orgtbl at point."
  9343. (interactive)
  9344. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  9345. (re2 (concat "^" orgtbl-line-start-regexp))
  9346. (commented (save-excursion (beginning-of-line 1)
  9347. (cond ((looking-at re1) t)
  9348. ((looking-at re2) nil)
  9349. (t (error "Not at an org table")))))
  9350. (re (if commented re1 re2))
  9351. beg end)
  9352. (save-excursion
  9353. (beginning-of-line 1)
  9354. (while (looking-at re) (beginning-of-line 0))
  9355. (beginning-of-line 2)
  9356. (setq beg (point))
  9357. (while (looking-at re) (beginning-of-line 2))
  9358. (setq end (point)))
  9359. (comment-region beg end (if commented '(4) nil))))
  9360. (defun orgtbl-insert-radio-table ()
  9361. "Insert a radio table template appropriate for this major mode."
  9362. (interactive)
  9363. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  9364. (txt (nth 1 e))
  9365. name pos)
  9366. (unless e (error "No radio table setup defined for %s" major-mode))
  9367. (setq name (read-string "Table name: "))
  9368. (while (string-match "%n" txt)
  9369. (setq txt (replace-match name t t txt)))
  9370. (or (bolp) (insert "\n"))
  9371. (setq pos (point))
  9372. (insert txt)
  9373. (goto-char pos)))
  9374. (defun org-get-param (params header i sym &optional hsym)
  9375. "Get parameter value for symbol SYM.
  9376. If this is a header line, actually get the value for the symbol with an
  9377. additional \"h\" inserted after the colon.
  9378. If the value is a protperty list, get the element for the current column.
  9379. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  9380. (let ((val (plist-get params sym)))
  9381. (and hsym header (setq val (or (plist-get params hsym) val)))
  9382. (if (consp val) (plist-get val i) val)))
  9383. (defun orgtbl-to-generic (table params)
  9384. "Convert the orgtbl-mode TABLE to some other format.
  9385. This generic routine can be used for many standard cases.
  9386. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9387. separator line, or a list of fields for that line.
  9388. PARAMS is a property list of parameters that can influence the conversion.
  9389. For the generic converter, some parameters are obligatory: You need to
  9390. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  9391. :splice, you must have :tstart and :tend.
  9392. Valid parameters are
  9393. :tstart String to start the table. Ignored when :splice is t.
  9394. :tend String to end the table. Ignored when :splice is t.
  9395. :splice When set to t, return only table body lines, don't wrap
  9396. them into :tstart and :tend. Default is nil.
  9397. :hline String to be inserted on horizontal separation lines.
  9398. May be nil to ignore hlines.
  9399. :lstart String to start a new table line.
  9400. :lend String to end a table line
  9401. :sep Separator between two fields
  9402. :lfmt Format for entire line, with enough %s to capture all fields.
  9403. If this is present, :lstart, :lend, and :sep are ignored.
  9404. :fmt A format to be used to wrap the field, should contain
  9405. %s for the original field value. For example, to wrap
  9406. everything in dollars, you could use :fmt \"$%s$\".
  9407. This may also be a property list with column numbers and
  9408. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9409. :hlstart :hlend :hlsep :hlfmt :hfmt
  9410. Same as above, specific for the header lines in the table.
  9411. All lines before the first hline are treated as header.
  9412. If any of these is not present, the data line value is used.
  9413. :efmt Use this format to print numbers with exponentials.
  9414. The format should have %s twice for inserting mantissa
  9415. and exponent, for example \"%s\\\\times10^{%s}\". This
  9416. may also be a property list with column numbers and
  9417. formats. :fmt will still be applied after :efmt.
  9418. In addition to this, the parameters :skip and :skipcols are always handled
  9419. directly by `orgtbl-send-table'. See manual."
  9420. (interactive)
  9421. (let* ((p params)
  9422. (splicep (plist-get p :splice))
  9423. (hline (plist-get p :hline))
  9424. rtn line i fm efm lfmt h)
  9425. ;; Do we have a header?
  9426. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  9427. (setq h t))
  9428. ;; Put header
  9429. (unless splicep
  9430. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  9431. ;; Now loop over all lines
  9432. (while (setq line (pop table))
  9433. (if (eq line 'hline)
  9434. ;; A horizontal separator line
  9435. (progn (if hline (push hline rtn))
  9436. (setq h nil)) ; no longer in header
  9437. ;; A normal line. Convert the fields, push line onto the result list
  9438. (setq i 0)
  9439. (setq line
  9440. (mapcar
  9441. (lambda (f)
  9442. (setq i (1+ i)
  9443. fm (org-get-param p h i :fmt :hfmt)
  9444. efm (org-get-param p h i :efmt))
  9445. (if (and efm (string-match orgtbl-exp-regexp f))
  9446. (setq f (format
  9447. efm (match-string 1 f) (match-string 2 f))))
  9448. (if fm (setq f (format fm f)))
  9449. f)
  9450. line))
  9451. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  9452. (push (apply 'format lfmt line) rtn)
  9453. (push (concat
  9454. (org-get-param p h i :lstart :hlstart)
  9455. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  9456. (org-get-param p h i :lend :hlend))
  9457. rtn))))
  9458. (unless splicep
  9459. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  9460. (mapconcat 'identity (nreverse rtn) "\n")))
  9461. (defun orgtbl-to-latex (table params)
  9462. "Convert the orgtbl-mode TABLE to LaTeX.
  9463. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9464. separator line, or a list of fields for that line.
  9465. PARAMS is a property list of parameters that can influence the conversion.
  9466. Supports all parameters from `orgtbl-to-generic'. Most important for
  9467. LaTeX are:
  9468. :splice When set to t, return only table body lines, don't wrap
  9469. them into a tabular environment. Default is nil.
  9470. :fmt A format to be used to wrap the field, should contain %s for the
  9471. original field value. For example, to wrap everything in dollars,
  9472. use :fmt \"$%s$\". This may also be a property list with column
  9473. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9474. :efmt Format for transforming numbers with exponentials. The format
  9475. should have %s twice for inserting mantissa and exponent, for
  9476. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  9477. This may also be a property list with column numbers and formats.
  9478. The general parameters :skip and :skipcols have already been applied when
  9479. this function is called."
  9480. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  9481. org-table-last-alignment ""))
  9482. (params2
  9483. (list
  9484. :tstart (concat "\\begin{tabular}{" alignment "}")
  9485. :tend "\\end{tabular}"
  9486. :lstart "" :lend " \\\\" :sep " & "
  9487. :efmt "%s\\,(%s)" :hline "\\hline")))
  9488. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9489. (defun orgtbl-to-html (table params)
  9490. "Convert the orgtbl-mode TABLE to LaTeX.
  9491. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9492. separator line, or a list of fields for that line.
  9493. PARAMS is a property list of parameters that can influence the conversion.
  9494. Currently this function recognizes the following parameters:
  9495. :splice When set to t, return only table body lines, don't wrap
  9496. them into a <table> environment. Default is nil.
  9497. The general parameters :skip and :skipcols have already been applied when
  9498. this function is called. The function does *not* use `orgtbl-to-generic',
  9499. so you cannot specify parameters for it."
  9500. (let* ((splicep (plist-get params :splice))
  9501. html)
  9502. ;; Just call the formatter we already have
  9503. ;; We need to make text lines for it, so put the fields back together.
  9504. (setq html (org-format-org-table-html
  9505. (mapcar
  9506. (lambda (x)
  9507. (if (eq x 'hline)
  9508. "|----+----|"
  9509. (concat "| " (mapconcat 'identity x " | ") " |")))
  9510. table)
  9511. splicep))
  9512. (if (string-match "\n+\\'" html)
  9513. (setq html (replace-match "" t t html)))
  9514. html))
  9515. (defun orgtbl-to-texinfo (table params)
  9516. "Convert the orgtbl-mode TABLE to TeXInfo.
  9517. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9518. separator line, or a list of fields for that line.
  9519. PARAMS is a property list of parameters that can influence the conversion.
  9520. Supports all parameters from `orgtbl-to-generic'. Most important for
  9521. TeXInfo are:
  9522. :splice nil/t When set to t, return only table body lines, don't wrap
  9523. them into a multitable environment. Default is nil.
  9524. :fmt fmt A format to be used to wrap the field, should contain
  9525. %s for the original field value. For example, to wrap
  9526. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  9527. This may also be a property list with column numbers and
  9528. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  9529. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  9530. are computed automatically from the width of the columns
  9531. under org-mode.
  9532. The general parameters :skip and :skipcols have already been applied when
  9533. this function is called."
  9534. (let* ((total (float (apply '+ org-table-last-column-widths)))
  9535. (colfrac (or (plist-get params :cf)
  9536. (mapconcat
  9537. (lambda (x) (format "%.3f" (/ (float x) total)))
  9538. org-table-last-column-widths " ")))
  9539. (params2
  9540. (list
  9541. :tstart (concat "@multitable @columnfractions " colfrac)
  9542. :tend "@end multitable"
  9543. :lstart "@item " :lend "" :sep " @tab "
  9544. :hlstart "@headitem ")))
  9545. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9546. ;;;; Link Stuff
  9547. ;;; Link abbreviations
  9548. (defun org-link-expand-abbrev (link)
  9549. "Apply replacements as defined in `org-link-abbrev-alist."
  9550. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  9551. (let* ((key (match-string 1 link))
  9552. (as (or (assoc key org-link-abbrev-alist-local)
  9553. (assoc key org-link-abbrev-alist)))
  9554. (tag (and (match-end 2) (match-string 3 link)))
  9555. rpl)
  9556. (if (not as)
  9557. link
  9558. (setq rpl (cdr as))
  9559. (cond
  9560. ((symbolp rpl) (funcall rpl tag))
  9561. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9562. (t (concat rpl tag)))))
  9563. link))
  9564. ;;; Storing and inserting links
  9565. (defvar org-insert-link-history nil
  9566. "Minibuffer history for links inserted with `org-insert-link'.")
  9567. (defvar org-stored-links nil
  9568. "Contains the links stored with `org-store-link'.")
  9569. (defvar org-store-link-plist nil
  9570. "Plist with info about the most recently link created with `org-store-link'.")
  9571. (defvar org-link-protocols nil
  9572. "Link protocols added to Org-mode using `org-add-link-type'.")
  9573. (defvar org-store-link-functions nil
  9574. "List of functions that are called to create and store a link.
  9575. Each function will be called in turn until one returns a non-nil
  9576. value. Each function should check if it is responsible for creating
  9577. this link (for example by looking at the major mode).
  9578. If not, it must exit and return nil.
  9579. If yes, it should return a non-nil value after a calling
  9580. `org-store-link-properties' with a list of properties and values.
  9581. Special properties are:
  9582. :type The link prefix. like \"http\". This must be given.
  9583. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  9584. This is obligatory as well.
  9585. :description Optional default description for the second pair
  9586. of brackets in an Org-mode link. The user can still change
  9587. this when inserting this link into an Org-mode buffer.
  9588. In addition to these, any additional properties can be specified
  9589. and then used in remember templates.")
  9590. (defun org-add-link-type (type &optional follow publish)
  9591. "Add TYPE to the list of `org-link-types'.
  9592. Re-compute all regular expressions depending on `org-link-types'
  9593. FOLLOW and PUBLISH are two functions. Both take the link path as
  9594. an argument.
  9595. FOLLOW should do whatever is necessary to follow the link, for example
  9596. to find a file or display a mail message.
  9597. PUBLISH takes the path and retuns the string that should be used when
  9598. this document is published."
  9599. (add-to-list 'org-link-types type t)
  9600. (org-make-link-regexps)
  9601. (add-to-list 'org-link-protocols
  9602. (list type follow publish)))
  9603. (defun org-add-agenda-custom-command (entry)
  9604. "Replace or add a command in `org-agenda-custom-commands'.
  9605. This is mostly for hacking and trying a new command - once the command
  9606. works you probably want to add it to `org-agenda-custom-commands' for good."
  9607. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  9608. (if ass
  9609. (setcdr ass (cdr entry))
  9610. (push entry org-agenda-custom-commands))))
  9611. ;;;###autoload
  9612. (defun org-store-link (arg)
  9613. "\\<org-mode-map>Store an org-link to the current location.
  9614. This link can later be inserted into an org-buffer with
  9615. \\[org-insert-link].
  9616. For some link types, a prefix arg is interpreted:
  9617. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9618. For file links, arg negates `org-context-in-file-links'."
  9619. (interactive "P")
  9620. (setq org-store-link-plist nil) ; reset
  9621. (let (link cpltxt desc description search txt)
  9622. (cond
  9623. ((run-hook-with-args-until-success 'org-store-link-functions)
  9624. (setq link (plist-get org-store-link-plist :link)
  9625. desc (or (plist-get org-store-link-plist :description) link)))
  9626. ((eq major-mode 'bbdb-mode)
  9627. (let ((name (bbdb-record-name (bbdb-current-record)))
  9628. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  9629. (setq cpltxt (concat "bbdb:" (or name company))
  9630. link (org-make-link cpltxt))
  9631. (org-store-link-props :type "bbdb" :name name :company company)))
  9632. ((eq major-mode 'Info-mode)
  9633. (setq link (org-make-link "info:"
  9634. (file-name-nondirectory Info-current-file)
  9635. ":" Info-current-node))
  9636. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9637. ":" Info-current-node))
  9638. (org-store-link-props :type "info" :file Info-current-file
  9639. :node Info-current-node))
  9640. ((eq major-mode 'calendar-mode)
  9641. (let ((cd (calendar-cursor-to-date)))
  9642. (setq link
  9643. (format-time-string
  9644. (car org-time-stamp-formats)
  9645. (apply 'encode-time
  9646. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9647. nil nil nil))))
  9648. (org-store-link-props :type "calendar" :date cd)))
  9649. ((or (eq major-mode 'vm-summary-mode)
  9650. (eq major-mode 'vm-presentation-mode))
  9651. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9652. (vm-follow-summary-cursor)
  9653. (save-excursion
  9654. (vm-select-folder-buffer)
  9655. (let* ((message (car vm-message-pointer))
  9656. (folder buffer-file-name)
  9657. (subject (vm-su-subject message))
  9658. (to (vm-get-header-contents message "To"))
  9659. (from (vm-get-header-contents message "From"))
  9660. (message-id (vm-su-message-id message)))
  9661. (org-store-link-props :type "vm" :from from :to to :subject subject
  9662. :message-id message-id)
  9663. (setq message-id (org-remove-angle-brackets message-id))
  9664. (setq folder (abbreviate-file-name folder))
  9665. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9666. folder)
  9667. (setq folder (replace-match "" t t folder)))
  9668. (setq cpltxt (org-email-link-description))
  9669. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9670. ((eq major-mode 'wl-summary-mode)
  9671. (let* ((msgnum (wl-summary-message-number))
  9672. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9673. msgnum 'message-id))
  9674. (wl-message-entity
  9675. (if (fboundp 'elmo-message-entity)
  9676. (elmo-message-entity
  9677. wl-summary-buffer-elmo-folder msgnum)
  9678. (elmo-msgdb-overview-get-entity
  9679. msgnum (wl-summary-buffer-msgdb))))
  9680. (from (wl-summary-line-from))
  9681. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  9682. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  9683. (wl-summary-line-subject))))
  9684. (org-store-link-props :type "wl" :from from :to to
  9685. :subject subject :message-id message-id)
  9686. (setq message-id (org-remove-angle-brackets message-id))
  9687. (setq cpltxt (org-email-link-description))
  9688. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9689. "#" message-id))))
  9690. ((or (equal major-mode 'mh-folder-mode)
  9691. (equal major-mode 'mh-show-mode))
  9692. (let ((from (org-mhe-get-header "From:"))
  9693. (to (org-mhe-get-header "To:"))
  9694. (message-id (org-mhe-get-header "Message-Id:"))
  9695. (subject (org-mhe-get-header "Subject:")))
  9696. (org-store-link-props :type "mh" :from from :to to
  9697. :subject subject :message-id message-id)
  9698. (setq cpltxt (org-email-link-description))
  9699. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9700. (org-remove-angle-brackets message-id)))))
  9701. ((eq major-mode 'rmail-mode)
  9702. (save-excursion
  9703. (save-restriction
  9704. (rmail-narrow-to-non-pruned-header)
  9705. (let ((folder buffer-file-name)
  9706. (message-id (mail-fetch-field "message-id"))
  9707. (from (mail-fetch-field "from"))
  9708. (to (mail-fetch-field "to"))
  9709. (subject (mail-fetch-field "subject")))
  9710. (org-store-link-props
  9711. :type "rmail" :from from :to to
  9712. :subject subject :message-id message-id)
  9713. (setq message-id (org-remove-angle-brackets message-id))
  9714. (setq cpltxt (org-email-link-description))
  9715. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9716. ((eq major-mode 'gnus-group-mode)
  9717. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9718. (gnus-group-group-name)) ; version
  9719. ((fboundp 'gnus-group-name)
  9720. (gnus-group-name))
  9721. (t "???"))))
  9722. (unless group (error "Not on a group"))
  9723. (org-store-link-props :type "gnus" :group group)
  9724. (setq cpltxt (concat
  9725. (if (org-xor arg org-usenet-links-prefer-google)
  9726. "http://groups.google.com/groups?group="
  9727. "gnus:")
  9728. group)
  9729. link (org-make-link cpltxt))))
  9730. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9731. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9732. (let* ((group gnus-newsgroup-name)
  9733. (article (gnus-summary-article-number))
  9734. (header (gnus-summary-article-header article))
  9735. (from (mail-header-from header))
  9736. (message-id (mail-header-id header))
  9737. (date (mail-header-date header))
  9738. (subject (gnus-summary-subject-string)))
  9739. (org-store-link-props :type "gnus" :from from :subject subject
  9740. :message-id message-id :group group)
  9741. (setq cpltxt (org-email-link-description))
  9742. (if (org-xor arg org-usenet-links-prefer-google)
  9743. (setq link
  9744. (concat
  9745. cpltxt "\n "
  9746. (format "http://groups.google.com/groups?as_umsgid=%s"
  9747. (org-fixup-message-id-for-http message-id))))
  9748. (setq link (org-make-link "gnus:" group
  9749. "#" (number-to-string article))))))
  9750. ((eq major-mode 'w3-mode)
  9751. (setq cpltxt (url-view-url t)
  9752. link (org-make-link cpltxt))
  9753. (org-store-link-props :type "w3" :url (url-view-url t)))
  9754. ((eq major-mode 'w3m-mode)
  9755. (setq cpltxt (or w3m-current-title w3m-current-url)
  9756. link (org-make-link w3m-current-url))
  9757. (org-store-link-props :type "w3m" :url (url-view-url t)))
  9758. ((setq search (run-hook-with-args-until-success
  9759. 'org-create-file-search-functions))
  9760. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9761. "::" search))
  9762. (setq cpltxt (or description link)))
  9763. ((eq major-mode 'image-mode)
  9764. (setq cpltxt (concat "file:"
  9765. (abbreviate-file-name buffer-file-name))
  9766. link (org-make-link cpltxt))
  9767. (org-store-link-props :type "image" :file buffer-file-name))
  9768. ((eq major-mode 'dired-mode)
  9769. ;; link to the file in the current line
  9770. (setq cpltxt (concat "file:"
  9771. (abbreviate-file-name
  9772. (expand-file-name
  9773. (dired-get-filename nil t))))
  9774. link (org-make-link cpltxt)))
  9775. ((and buffer-file-name (org-mode-p))
  9776. ;; Just link to current headline
  9777. (setq cpltxt (concat "file:"
  9778. (abbreviate-file-name buffer-file-name)))
  9779. ;; Add a context search string
  9780. (when (org-xor org-context-in-file-links arg)
  9781. ;; Check if we are on a target
  9782. (if (org-in-regexp "<<\\(.*?\\)>>")
  9783. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9784. (setq txt (cond
  9785. ((org-on-heading-p) nil)
  9786. ((org-region-active-p)
  9787. (buffer-substring (region-beginning) (region-end)))
  9788. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9789. (when (or (null txt) (string-match "\\S-" txt))
  9790. (setq cpltxt
  9791. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9792. desc "NONE"))))
  9793. (if (string-match "::\\'" cpltxt)
  9794. (setq cpltxt (substring cpltxt 0 -2)))
  9795. (setq link (org-make-link cpltxt)))
  9796. (buffer-file-name
  9797. ;; Just link to this file here.
  9798. (setq cpltxt (concat "file:"
  9799. (abbreviate-file-name buffer-file-name)))
  9800. ;; Add a context string
  9801. (when (org-xor org-context-in-file-links arg)
  9802. (setq txt (if (org-region-active-p)
  9803. (buffer-substring (region-beginning) (region-end))
  9804. (buffer-substring (point-at-bol) (point-at-eol))))
  9805. ;; Only use search option if there is some text.
  9806. (when (string-match "\\S-" txt)
  9807. (setq cpltxt
  9808. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9809. desc "NONE")))
  9810. (setq link (org-make-link cpltxt)))
  9811. ((interactive-p)
  9812. (error "Cannot link to a buffer which is not visiting a file"))
  9813. (t (setq link nil)))
  9814. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9815. (setq link (or link cpltxt)
  9816. desc (or desc cpltxt))
  9817. (if (equal desc "NONE") (setq desc nil))
  9818. (if (and (interactive-p) link)
  9819. (progn
  9820. (setq org-stored-links
  9821. (cons (list cpltxt link desc) org-stored-links))
  9822. (message "Stored: %s" (or cpltxt link)))
  9823. (org-make-link-string link desc))))
  9824. (defun org-store-link-props (&rest plist)
  9825. "Store link properties, extract names and addresses."
  9826. (let (x adr)
  9827. (when (setq x (plist-get plist :from))
  9828. (setq adr (mail-extract-address-components x))
  9829. (plist-put plist :fromname (car adr))
  9830. (plist-put plist :fromaddress (nth 1 adr)))
  9831. (when (setq x (plist-get plist :to))
  9832. (setq adr (mail-extract-address-components x))
  9833. (plist-put plist :toname (car adr))
  9834. (plist-put plist :toaddress (nth 1 adr))))
  9835. (let ((from (plist-get plist :from))
  9836. (to (plist-get plist :to)))
  9837. (when (and from to org-from-is-user-regexp)
  9838. (plist-put plist :fromto
  9839. (if (string-match org-from-is-user-regexp from)
  9840. (concat "to %t")
  9841. (concat "from %f")))))
  9842. (setq org-store-link-plist plist))
  9843. (defun org-email-link-description (&optional fmt)
  9844. "Return the description part of an email link.
  9845. This takes information from `org-store-link-plist' and formats it
  9846. according to FMT (default from `org-email-link-description-format')."
  9847. (setq fmt (or fmt org-email-link-description-format))
  9848. (let* ((p org-store-link-plist)
  9849. (to (plist-get p :toaddress))
  9850. (from (plist-get p :fromaddress))
  9851. (table
  9852. (list
  9853. (cons "%c" (plist-get p :fromto))
  9854. (cons "%F" (plist-get p :from))
  9855. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  9856. (cons "%T" (plist-get p :to))
  9857. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  9858. (cons "%s" (plist-get p :subject))
  9859. (cons "%m" (plist-get p :message-id)))))
  9860. (when (string-match "%c" fmt)
  9861. ;; Check if the user wrote this message
  9862. (if (and org-from-is-user-regexp from to
  9863. (save-match-data (string-match org-from-is-user-regexp from)))
  9864. (setq fmt (replace-match "to %t" t t fmt))
  9865. (setq fmt (replace-match "from %f" t t fmt))))
  9866. (org-replace-escapes fmt table)))
  9867. (defun org-make-org-heading-search-string (&optional string heading)
  9868. "Make search string for STRING or current headline."
  9869. (interactive)
  9870. (let ((s (or string (org-get-heading))))
  9871. (unless (and string (not heading))
  9872. ;; We are using a headline, clean up garbage in there.
  9873. (if (string-match org-todo-regexp s)
  9874. (setq s (replace-match "" t t s)))
  9875. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  9876. (setq s (replace-match "" t t s)))
  9877. (setq s (org-trim s))
  9878. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9879. org-comment-string "\\)") s)
  9880. (setq s (replace-match "" t t s)))
  9881. (while (string-match org-ts-regexp s)
  9882. (setq s (replace-match "" t t s))))
  9883. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9884. (setq s (replace-match " " t t s)))
  9885. (or string (setq s (concat "*" s))) ; Add * for headlines
  9886. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9887. (defun org-make-link (&rest strings)
  9888. "Concatenate STRINGS."
  9889. (apply 'concat strings))
  9890. (defun org-make-link-string (link &optional description)
  9891. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9892. (when (stringp description)
  9893. ;; Remove brackets from the description, they are fatal.
  9894. (while (string-match "\\[\\|\\]" description)
  9895. (setq description (replace-match "" t t description))))
  9896. (when (equal (org-link-escape link) description)
  9897. ;; No description needed, it is identical
  9898. (setq description nil))
  9899. (when (and (not description)
  9900. (not (equal link (org-link-escape link))))
  9901. (setq description link))
  9902. (concat "[[" (org-link-escape link) "]"
  9903. (if description (concat "[" description "]") "")
  9904. "]"))
  9905. (defconst org-link-escape-chars
  9906. '((" " . "%20") ("\340" . "%E0")
  9907. ("\342" . "%E2") ("\347" . "%E7")
  9908. ("\350" . "%E8") ("\351" . "%E9")
  9909. ("\352" . "%EA") ("\356" . "%EE")
  9910. ("\364" . "%F4") ("\371" . "%F9")
  9911. ("\373" . "%FB") (";" . "%3B")
  9912. ("?" . "%3F") ("=" . "%3D")
  9913. ("+" . "%2B"))
  9914. "Association list of escapes for some characters problematic in links.")
  9915. (defun org-link-escape (text)
  9916. "Escape charaters in TEXT that are problematic for links."
  9917. (when text
  9918. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9919. org-link-escape-chars "\\|")))
  9920. (while (string-match re text)
  9921. (setq text
  9922. (replace-match
  9923. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9924. t t text)))
  9925. text)))
  9926. (defun org-link-unescape (text)
  9927. "Reverse the action of `org-link-escape'."
  9928. (when text
  9929. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9930. org-link-escape-chars "\\|")))
  9931. (while (string-match re text)
  9932. (setq text
  9933. (replace-match
  9934. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9935. t t text)))
  9936. text)))
  9937. (defun org-xor (a b)
  9938. "Exclusive or."
  9939. (if a (not b) b))
  9940. (defun org-get-header (header)
  9941. "Find a header field in the current buffer."
  9942. (save-excursion
  9943. (goto-char (point-min))
  9944. (let ((case-fold-search t) s)
  9945. (cond
  9946. ((eq header 'from)
  9947. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9948. (setq s (match-string 1)))
  9949. (while (string-match "\"" s)
  9950. (setq s (replace-match "" t t s)))
  9951. (if (string-match "[<(].*" s)
  9952. (setq s (replace-match "" t t s))))
  9953. ((eq header 'message-id)
  9954. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9955. (setq s (match-string 1))))
  9956. ((eq header 'subject)
  9957. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9958. (setq s (match-string 1)))))
  9959. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9960. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9961. s)))
  9962. (defun org-fixup-message-id-for-http (s)
  9963. "Replace special characters in a message id, so it can be used in an http query."
  9964. (while (string-match "<" s)
  9965. (setq s (replace-match "%3C" t t s)))
  9966. (while (string-match ">" s)
  9967. (setq s (replace-match "%3E" t t s)))
  9968. (while (string-match "@" s)
  9969. (setq s (replace-match "%40" t t s)))
  9970. s)
  9971. ;;;###autoload
  9972. (defun org-insert-link-global ()
  9973. "Insert a link like Org-mode does.
  9974. This command can be called in any mode to follow a link that has
  9975. Org-mode syntax."
  9976. (interactive)
  9977. (org-run-like-in-org-mode 'org-insert-link))
  9978. (defun org-insert-link (&optional complete-file)
  9979. "Insert a link. At the prompt, enter the link.
  9980. Completion can be used to select a link previously stored with
  9981. `org-store-link'. When the empty string is entered (i.e. if you just
  9982. press RET at the prompt), the link defaults to the most recently
  9983. stored link. As SPC triggers completion in the minibuffer, you need to
  9984. use M-SPC or C-q SPC to force the insertion of a space character.
  9985. You will also be prompted for a description, and if one is given, it will
  9986. be displayed in the buffer instead of the link.
  9987. If there is already a link at point, this command will allow you to edit link
  9988. and description parts.
  9989. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  9990. selected using completion. The path to the file will be relative to
  9991. the current directory if the file is in the current directory or a
  9992. subdirectory. Otherwise, the link will be the absolute path as
  9993. completed in the minibuffer (i.e. normally ~/path/to/file).
  9994. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  9995. is in the current directory or below.
  9996. With three \\[universal-argument] prefixes, negate the meaning of
  9997. `org-keep-stored-link-after-insertion'."
  9998. (interactive "P")
  9999. (let ((wcf (current-window-configuration))
  10000. (region (if (org-region-active-p)
  10001. (prog1 (buffer-substring (region-beginning) (region-end))
  10002. (delete-region (region-beginning) (region-end)))))
  10003. tmphist ; byte-compile incorrectly complains about this
  10004. link desc entry remove file)
  10005. (cond
  10006. ((org-in-regexp org-bracket-link-regexp 1)
  10007. ;; We do have a link at point, and we are going to edit it.
  10008. (setq remove (list (match-beginning 0) (match-end 0)))
  10009. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10010. (setq link (read-string "Link: "
  10011. (org-link-unescape
  10012. (org-match-string-no-properties 1)))))
  10013. ((or (org-in-regexp org-angle-link-re)
  10014. (org-in-regexp org-plain-link-re))
  10015. ;; Convert to bracket link
  10016. (setq remove (list (match-beginning 0) (match-end 0))
  10017. link (read-string "Link: "
  10018. (org-remove-angle-brackets (match-string 0)))))
  10019. ((equal complete-file '(4))
  10020. ;; Completing read for file names.
  10021. (setq file (read-file-name "File: "))
  10022. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10023. (pwd1 (file-name-as-directory (abbreviate-file-name
  10024. (expand-file-name ".")))))
  10025. (cond
  10026. ((equal complete-file '(16))
  10027. (setq link (org-make-link
  10028. "file:"
  10029. (abbreviate-file-name (expand-file-name file)))))
  10030. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10031. (setq link (org-make-link "file:" (match-string 1 file))))
  10032. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10033. (expand-file-name file))
  10034. (setq link (org-make-link
  10035. "file:" (match-string 1 (expand-file-name file)))))
  10036. (t (setq link (org-make-link "file:" file))))))
  10037. (t
  10038. ;; Read link, with completion for stored links.
  10039. (with-output-to-temp-buffer "*Org Links*"
  10040. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  10041. (when org-stored-links
  10042. (princ "\nStored links are available with <up>/<down> (most recent with RET):\n\n")
  10043. (princ (mapconcat 'car (reverse org-stored-links) "\n"))))
  10044. (let ((cw (selected-window)))
  10045. (select-window (get-buffer-window "*Org Links*"))
  10046. (shrink-window-if-larger-than-buffer)
  10047. (setq truncate-lines t)
  10048. (select-window cw))
  10049. ;; Fake a link history, containing the stored links.
  10050. (setq tmphist (append (mapcar 'car org-stored-links)
  10051. org-insert-link-history))
  10052. (unwind-protect
  10053. (setq link (org-completing-read
  10054. "Link: "
  10055. (append
  10056. (mapcar (lambda (x) (list (concat (car x) ":")))
  10057. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  10058. (mapcar (lambda (x) (list (concat x ":")))
  10059. org-link-types))
  10060. nil nil nil
  10061. 'tmphist
  10062. (or (car (car org-stored-links)))))
  10063. (set-window-configuration wcf)
  10064. (kill-buffer "*Org Links*"))
  10065. (setq entry (assoc link org-stored-links))
  10066. (or entry (push link org-insert-link-history))
  10067. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10068. (not org-keep-stored-link-after-insertion))
  10069. (setq org-stored-links (delq (assoc link org-stored-links)
  10070. org-stored-links)))
  10071. (setq link (if entry (nth 1 entry) link)
  10072. desc (or region desc (nth 2 entry)))))
  10073. (if (string-match org-plain-link-re link)
  10074. ;; URL-like link, normalize the use of angular brackets.
  10075. (setq link (org-make-link (org-remove-angle-brackets link))))
  10076. ;; Check if we are linking to the current file with a search option
  10077. ;; If yes, simplify the link by using only the search option.
  10078. (when (and buffer-file-name
  10079. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10080. (let* ((path (match-string 1 link))
  10081. (case-fold-search nil)
  10082. (search (match-string 2 link)))
  10083. (save-match-data
  10084. (if (equal (file-truename buffer-file-name) (file-truename path))
  10085. ;; We are linking to this same file, with a search option
  10086. (setq link search)))))
  10087. ;; Check if we can/should use a relative path. If yes, simplify the link
  10088. (when (string-match "\\<file:\\(.*\\)" link)
  10089. (let* ((path (match-string 1 link))
  10090. (case-fold-search nil))
  10091. (cond
  10092. ((eq org-link-file-path-type 'absolute)
  10093. (setq path (abbreviate-file-name (expand-file-name path))))
  10094. ((eq org-link-file-path-type 'noabbrev)
  10095. (setq path (expand-file-name path)))
  10096. ((eq org-link-file-path-type 'relative)
  10097. (setq path (file-relative-name path)))
  10098. (t
  10099. (save-match-data
  10100. (if (string-match (concat "^" (regexp-quote
  10101. (file-name-as-directory
  10102. (expand-file-name "."))))
  10103. (expand-file-name path))
  10104. ;; We are linking a file with relative path name.
  10105. (setq path (substring (expand-file-name path)
  10106. (match-end 0)))))))
  10107. (setq link (concat "file:" path))))
  10108. (setq desc (read-string "Description: " desc))
  10109. (unless (string-match "\\S-" desc) (setq desc nil))
  10110. (if remove (apply 'delete-region remove))
  10111. (insert (org-make-link-string link desc))))
  10112. (defun org-completing-read (&rest args)
  10113. (let ((minibuffer-local-completion-map
  10114. (copy-keymap minibuffer-local-completion-map)))
  10115. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  10116. (apply 'completing-read args)))
  10117. ;;; Opening/following a link
  10118. (defvar org-link-search-failed nil)
  10119. (defun org-next-link ()
  10120. "Move forward to the next link.
  10121. If the link is in hidden text, expose it."
  10122. (interactive)
  10123. (when (and org-link-search-failed (eq this-command last-command))
  10124. (goto-char (point-min))
  10125. (message "Link search wrapped back to beginning of buffer"))
  10126. (setq org-link-search-failed nil)
  10127. (let* ((pos (point))
  10128. (ct (org-context))
  10129. (a (assoc :link ct)))
  10130. (if a (goto-char (nth 2 a)))
  10131. (if (re-search-forward org-any-link-re nil t)
  10132. (progn
  10133. (goto-char (match-beginning 0))
  10134. (if (org-invisible-p) (org-show-context)))
  10135. (goto-char pos)
  10136. (setq org-link-search-failed t)
  10137. (error "No further link found"))))
  10138. (defun org-previous-link ()
  10139. "Move backward to the previous link.
  10140. If the link is in hidden text, expose it."
  10141. (interactive)
  10142. (when (and org-link-search-failed (eq this-command last-command))
  10143. (goto-char (point-max))
  10144. (message "Link search wrapped back to end of buffer"))
  10145. (setq org-link-search-failed nil)
  10146. (let* ((pos (point))
  10147. (ct (org-context))
  10148. (a (assoc :link ct)))
  10149. (if a (goto-char (nth 1 a)))
  10150. (if (re-search-backward org-any-link-re nil t)
  10151. (progn
  10152. (goto-char (match-beginning 0))
  10153. (if (org-invisible-p) (org-show-context)))
  10154. (goto-char pos)
  10155. (setq org-link-search-failed t)
  10156. (error "No further link found"))))
  10157. (defun org-find-file-at-mouse (ev)
  10158. "Open file link or URL at mouse."
  10159. (interactive "e")
  10160. (mouse-set-point ev)
  10161. (org-open-at-point 'in-emacs))
  10162. (defun org-open-at-mouse (ev)
  10163. "Open file link or URL at mouse."
  10164. (interactive "e")
  10165. (mouse-set-point ev)
  10166. (org-open-at-point))
  10167. (defvar org-window-config-before-follow-link nil
  10168. "The window configuration before following a link.
  10169. This is saved in case the need arises to restore it.")
  10170. (defvar org-open-link-marker (make-marker)
  10171. "Marker pointing to the location where `org-open-at-point; was called.")
  10172. ;;;###autoload
  10173. (defun org-open-at-point-global ()
  10174. "Follow a link like Org-mode does.
  10175. This command can be called in any mode to follow a link that has
  10176. Org-mode syntax."
  10177. (interactive)
  10178. (org-run-like-in-org-mode 'org-open-at-point))
  10179. (defun org-open-at-point (&optional in-emacs)
  10180. "Open link at or after point.
  10181. If there is no link at point, this function will search forward up to
  10182. the end of the current subtree.
  10183. Normally, files will be opened by an appropriate application. If the
  10184. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  10185. (interactive "P")
  10186. (move-marker org-open-link-marker (point))
  10187. (setq org-window-config-before-follow-link (current-window-configuration))
  10188. (org-remove-occur-highlights nil nil t)
  10189. (if (org-at-timestamp-p t)
  10190. (org-follow-timestamp-link)
  10191. (let (type path link line search (pos (point)))
  10192. (catch 'match
  10193. (save-excursion
  10194. (skip-chars-forward "^]\n\r")
  10195. (when (org-in-regexp org-bracket-link-regexp)
  10196. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  10197. (while (string-match " *\n *" link)
  10198. (setq link (replace-match " " t t link)))
  10199. (setq link (org-link-expand-abbrev link))
  10200. (if (string-match org-link-re-with-space2 link)
  10201. (setq type (match-string 1 link) path (match-string 2 link))
  10202. (setq type "thisfile" path link))
  10203. (throw 'match t)))
  10204. (when (get-text-property (point) 'org-linked-text)
  10205. (setq type "thisfile"
  10206. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  10207. (1+ (point)) (point))
  10208. path (buffer-substring
  10209. (previous-single-property-change pos 'org-linked-text)
  10210. (next-single-property-change pos 'org-linked-text)))
  10211. (throw 'match t))
  10212. (save-excursion
  10213. (when (or (org-in-regexp org-angle-link-re)
  10214. (org-in-regexp org-plain-link-re))
  10215. (setq type (match-string 1) path (match-string 2))
  10216. (throw 'match t)))
  10217. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  10218. (setq type "tree-match"
  10219. path (match-string 1))
  10220. (throw 'match t))
  10221. (save-excursion
  10222. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  10223. (setq type "tags"
  10224. path (match-string 1))
  10225. (while (string-match ":" path)
  10226. (setq path (replace-match "+" t t path)))
  10227. (throw 'match t))))
  10228. (unless path
  10229. (error "No link found"))
  10230. ;; Remove any trailing spaces in path
  10231. (if (string-match " +\\'" path)
  10232. (setq path (replace-match "" t t path)))
  10233. (cond
  10234. ((assoc type org-link-protocols)
  10235. (funcall (nth 1 (assoc type org-link-protocols)) path))
  10236. ((equal type "mailto")
  10237. (let ((cmd (car org-link-mailto-program))
  10238. (args (cdr org-link-mailto-program)) args1
  10239. (address path) (subject "") a)
  10240. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  10241. (setq address (match-string 1 path)
  10242. subject (org-link-escape (match-string 2 path))))
  10243. (while args
  10244. (cond
  10245. ((not (stringp (car args))) (push (pop args) args1))
  10246. (t (setq a (pop args))
  10247. (if (string-match "%a" a)
  10248. (setq a (replace-match address t t a)))
  10249. (if (string-match "%s" a)
  10250. (setq a (replace-match subject t t a)))
  10251. (push a args1))))
  10252. (apply cmd (nreverse args1))))
  10253. ((member type '("http" "https" "ftp" "news"))
  10254. (browse-url (concat type ":" path)))
  10255. ((string= type "tags")
  10256. (org-tags-view in-emacs path))
  10257. ((string= type "thisfile")
  10258. (if in-emacs
  10259. (switch-to-buffer-other-window
  10260. (org-get-buffer-for-internal-link (current-buffer)))
  10261. (org-mark-ring-push))
  10262. (let ((cmd `(org-link-search
  10263. ,path
  10264. ,(cond ((equal in-emacs '(4)) 'occur)
  10265. ((equal in-emacs '(16)) 'org-occur)
  10266. (t nil))
  10267. ,pos)))
  10268. (condition-case nil (eval cmd)
  10269. (error (progn (widen) (eval cmd))))))
  10270. ((string= type "tree-match")
  10271. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  10272. ((string= type "file")
  10273. (if (string-match "::\\([0-9]+\\)\\'" path)
  10274. (setq line (string-to-number (match-string 1 path))
  10275. path (substring path 0 (match-beginning 0)))
  10276. (if (string-match "::\\(.+\\)\\'" path)
  10277. (setq search (match-string 1 path)
  10278. path (substring path 0 (match-beginning 0)))))
  10279. (org-open-file path in-emacs line search))
  10280. ((string= type "news")
  10281. (org-follow-gnus-link path))
  10282. ((string= type "bbdb")
  10283. (org-follow-bbdb-link path))
  10284. ((string= type "info")
  10285. (org-follow-info-link path))
  10286. ((string= type "gnus")
  10287. (let (group article)
  10288. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10289. (error "Error in Gnus link"))
  10290. (setq group (match-string 1 path)
  10291. article (match-string 3 path))
  10292. (org-follow-gnus-link group article)))
  10293. ((string= type "vm")
  10294. (let (folder article)
  10295. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10296. (error "Error in VM link"))
  10297. (setq folder (match-string 1 path)
  10298. article (match-string 3 path))
  10299. ;; in-emacs is the prefix arg, will be interpreted as read-only
  10300. (org-follow-vm-link folder article in-emacs)))
  10301. ((string= type "wl")
  10302. (let (folder article)
  10303. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10304. (error "Error in Wanderlust link"))
  10305. (setq folder (match-string 1 path)
  10306. article (match-string 3 path))
  10307. (org-follow-wl-link folder article)))
  10308. ((string= type "mhe")
  10309. (let (folder article)
  10310. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10311. (error "Error in MHE link"))
  10312. (setq folder (match-string 1 path)
  10313. article (match-string 3 path))
  10314. (org-follow-mhe-link folder article)))
  10315. ((string= type "rmail")
  10316. (let (folder article)
  10317. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10318. (error "Error in RMAIL link"))
  10319. (setq folder (match-string 1 path)
  10320. article (match-string 3 path))
  10321. (org-follow-rmail-link folder article)))
  10322. ((string= type "shell")
  10323. (let ((cmd path))
  10324. ;; FIXME: the following is only for backward compatibility
  10325. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  10326. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  10327. (if (or (not org-confirm-shell-link-function)
  10328. (funcall org-confirm-shell-link-function
  10329. (format "Execute \"%s\" in shell? "
  10330. (org-add-props cmd nil
  10331. 'face 'org-warning))))
  10332. (progn
  10333. (message "Executing %s" cmd)
  10334. (shell-command cmd))
  10335. (error "Abort"))))
  10336. ((string= type "elisp")
  10337. (let ((cmd path))
  10338. (if (or (not org-confirm-elisp-link-function)
  10339. (funcall org-confirm-elisp-link-function
  10340. (format "Execute \"%s\" as elisp? "
  10341. (org-add-props cmd nil
  10342. 'face 'org-warning))))
  10343. (message "%s => %s" cmd (eval (read cmd)))
  10344. (error "Abort"))))
  10345. (t
  10346. (browse-url-at-point)))))
  10347. (move-marker org-open-link-marker nil))
  10348. ;;; File search
  10349. (defvar org-create-file-search-functions nil
  10350. "List of functions to construct the right search string for a file link.
  10351. These functions are called in turn with point at the location to
  10352. which the link should point.
  10353. A function in the hook should first test if it would like to
  10354. handle this file type, for example by checking the major-mode or
  10355. the file extension. If it decides not to handle this file, it
  10356. should just return nil to give other functions a chance. If it
  10357. does handle the file, it must return the search string to be used
  10358. when following the link. The search string will be part of the
  10359. file link, given after a double colon, and `org-open-at-point'
  10360. will automatically search for it. If special measures must be
  10361. taken to make the search successful, another function should be
  10362. added to the companion hook `org-execute-file-search-functions',
  10363. which see.
  10364. A function in this hook may also use `setq' to set the variable
  10365. `description' to provide a suggestion for the descriptive text to
  10366. be used for this link when it gets inserted into an Org-mode
  10367. buffer with \\[org-insert-link].")
  10368. (defvar org-execute-file-search-functions nil
  10369. "List of functions to execute a file search triggered by a link.
  10370. Functions added to this hook must accept a single argument, the
  10371. search string that was part of the file link, the part after the
  10372. double colon. The function must first check if it would like to
  10373. handle this search, for example by checking the major-mode or the
  10374. file extension. If it decides not to handle this search, it
  10375. should just return nil to give other functions a chance. If it
  10376. does handle the search, it must return a non-nil value to keep
  10377. other functions from trying.
  10378. Each function can access the current prefix argument through the
  10379. variable `current-prefix-argument'. Note that a single prefix is
  10380. used to force opening a link in Emacs, so it may be good to only
  10381. use a numeric or double prefix to guide the search function.
  10382. In case this is needed, a function in this hook can also restore
  10383. the window configuration before `org-open-at-point' was called using:
  10384. (set-window-configuration org-window-config-before-follow-link)")
  10385. (defun org-link-search (s &optional type avoid-pos)
  10386. "Search for a link search option.
  10387. If S is surrounded by forward slashes, it is interpreted as a
  10388. regular expression. In org-mode files, this will create an `org-occur'
  10389. sparse tree. In ordinary files, `occur' will be used to list matches.
  10390. If the current buffer is in `dired-mode', grep will be used to search
  10391. in all files. If AVOID-POS is given, ignore matches near that position."
  10392. (let ((case-fold-search t)
  10393. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  10394. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  10395. (append '(("") (" ") ("\t") ("\n"))
  10396. org-emphasis-alist)
  10397. "\\|") "\\)"))
  10398. (pos (point))
  10399. (pre "") (post "")
  10400. words re0 re1 re2 re3 re4 re5 re2a reall)
  10401. (cond
  10402. ;; First check if there are any special
  10403. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  10404. ;; Now try the builtin stuff
  10405. ((save-excursion
  10406. (goto-char (point-min))
  10407. (and
  10408. (re-search-forward
  10409. (concat "<<" (regexp-quote s0) ">>") nil t)
  10410. (setq pos (match-beginning 0))))
  10411. ;; There is an exact target for this
  10412. (goto-char pos))
  10413. ((string-match "^/\\(.*\\)/$" s)
  10414. ;; A regular expression
  10415. (cond
  10416. ((org-mode-p)
  10417. (org-occur (match-string 1 s)))
  10418. ;;((eq major-mode 'dired-mode)
  10419. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  10420. (t (org-do-occur (match-string 1 s)))))
  10421. (t
  10422. ;; A normal search strings
  10423. (when (equal (string-to-char s) ?*)
  10424. ;; Anchor on headlines, post may include tags.
  10425. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  10426. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  10427. s (substring s 1)))
  10428. (remove-text-properties
  10429. 0 (length s)
  10430. '(face nil mouse-face nil keymap nil fontified nil) s)
  10431. ;; Make a series of regular expressions to find a match
  10432. (setq words (org-split-string s "[ \n\r\t]+")
  10433. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10434. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10435. "\\)" markers)
  10436. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  10437. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10438. re1 (concat pre re2 post)
  10439. re3 (concat pre re4 post)
  10440. re5 (concat pre ".*" re4)
  10441. re2 (concat pre re2)
  10442. re2a (concat pre re2a)
  10443. re4 (concat pre re4)
  10444. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10445. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10446. re5 "\\)"
  10447. ))
  10448. (cond
  10449. ((eq type 'org-occur) (org-occur reall))
  10450. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10451. (t (goto-char (point-min))
  10452. (if (or (org-search-not-self 1 re0 nil t)
  10453. (org-search-not-self 1 re1 nil t)
  10454. (org-search-not-self 1 re2 nil t)
  10455. (org-search-not-self 1 re2a nil t)
  10456. (org-search-not-self 1 re3 nil t)
  10457. (org-search-not-self 1 re4 nil t)
  10458. (org-search-not-self 1 re5 nil t)
  10459. )
  10460. (goto-char (match-beginning 1))
  10461. (goto-char pos)
  10462. (error "No match")))))
  10463. (t
  10464. ;; Normal string-search
  10465. (goto-char (point-min))
  10466. (if (search-forward s nil t)
  10467. (goto-char (match-beginning 0))
  10468. (error "No match"))))
  10469. (and (org-mode-p) (org-show-context 'link-search))))
  10470. (defun org-search-not-self (group &rest args)
  10471. "Execute `re-search-forward', but only accept matches that do not
  10472. enclose the position of `org-open-link-marker'."
  10473. (let ((m org-open-link-marker))
  10474. (catch 'exit
  10475. (while (apply 're-search-forward args)
  10476. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10477. (goto-char (match-end group))
  10478. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10479. (> (match-beginning 0) (marker-position m))
  10480. (< (match-end 0) (marker-position m)))
  10481. (save-match-data
  10482. (or (not (org-in-regexp
  10483. org-bracket-link-analytic-regexp 1))
  10484. (not (match-end 4)) ; no description
  10485. (and (<= (match-beginning 4) (point))
  10486. (>= (match-end 4) (point))))))
  10487. (throw 'exit (point))))))))
  10488. (defun org-get-buffer-for-internal-link (buffer)
  10489. "Return a buffer to be used for displaying the link target of internal links."
  10490. (cond
  10491. ((not org-display-internal-link-with-indirect-buffer)
  10492. buffer)
  10493. ((string-match "(Clone)$" (buffer-name buffer))
  10494. (message "Buffer is already a clone, not making another one")
  10495. ;; we also do not modify visibility in this case
  10496. buffer)
  10497. (t ; make a new indirect buffer for displaying the link
  10498. (let* ((bn (buffer-name buffer))
  10499. (ibn (concat bn "(Clone)"))
  10500. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10501. (with-current-buffer ib (org-overview))
  10502. ib))))
  10503. (defun org-do-occur (regexp &optional cleanup)
  10504. "Call the Emacs command `occur'.
  10505. If CLEANUP is non-nil, remove the printout of the regular expression
  10506. in the *Occur* buffer. This is useful if the regex is long and not useful
  10507. to read."
  10508. (occur regexp)
  10509. (when cleanup
  10510. (let ((cwin (selected-window)) win beg end)
  10511. (when (setq win (get-buffer-window "*Occur*"))
  10512. (select-window win))
  10513. (goto-char (point-min))
  10514. (when (re-search-forward "match[a-z]+" nil t)
  10515. (setq beg (match-end 0))
  10516. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10517. (setq end (1- (match-beginning 0)))))
  10518. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10519. (goto-char (point-min))
  10520. (select-window cwin))))
  10521. ;;; The mark ring for links jumps
  10522. (defvar org-mark-ring nil
  10523. "Mark ring for positions before jumps in Org-mode.")
  10524. (defvar org-mark-ring-last-goto nil
  10525. "Last position in the mark ring used to go back.")
  10526. ;; Fill and close the ring
  10527. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10528. (loop for i from 1 to org-mark-ring-length do
  10529. (push (make-marker) org-mark-ring))
  10530. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10531. org-mark-ring)
  10532. (defun org-mark-ring-push (&optional pos buffer)
  10533. "Put the current position or POS into the mark ring and rotate it."
  10534. (interactive)
  10535. (setq pos (or pos (point)))
  10536. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10537. (move-marker (car org-mark-ring)
  10538. (or pos (point))
  10539. (or buffer (current-buffer)))
  10540. (message
  10541. (substitute-command-keys
  10542. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10543. (defun org-mark-ring-goto (&optional n)
  10544. "Jump to the previous position in the mark ring.
  10545. With prefix arg N, jump back that many stored positions. When
  10546. called several times in succession, walk through the entire ring.
  10547. Org-mode commands jumping to a different position in the current file,
  10548. or to another Org-mode file, automatically push the old position
  10549. onto the ring."
  10550. (interactive "p")
  10551. (let (p m)
  10552. (if (eq last-command this-command)
  10553. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10554. (setq p org-mark-ring))
  10555. (setq org-mark-ring-last-goto p)
  10556. (setq m (car p))
  10557. (switch-to-buffer (marker-buffer m))
  10558. (goto-char m)
  10559. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10560. (defun org-remove-angle-brackets (s)
  10561. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10562. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10563. s)
  10564. (defun org-add-angle-brackets (s)
  10565. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10566. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10567. s)
  10568. ;;; Following specific links
  10569. (defun org-follow-timestamp-link ()
  10570. (cond
  10571. ((org-at-date-range-p t)
  10572. (let ((org-agenda-start-on-weekday)
  10573. (t1 (match-string 1))
  10574. (t2 (match-string 2)))
  10575. (setq t1 (time-to-days (org-time-string-to-time t1))
  10576. t2 (time-to-days (org-time-string-to-time t2)))
  10577. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  10578. ((org-at-timestamp-p t)
  10579. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10580. (substring (match-string 1) 0 10)))
  10581. 1))
  10582. (t (error "This should not happen"))))
  10583. (defun org-follow-bbdb-link (name)
  10584. "Follow a BBDB link to NAME."
  10585. (require 'bbdb)
  10586. (let ((inhibit-redisplay (not debug-on-error))
  10587. (bbdb-electric-p nil))
  10588. (catch 'exit
  10589. ;; Exact match on name
  10590. (bbdb-name (concat "\\`" name "\\'") nil)
  10591. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10592. ;; Exact match on name
  10593. (bbdb-company (concat "\\`" name "\\'") nil)
  10594. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10595. ;; Partial match on name
  10596. (bbdb-name name nil)
  10597. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10598. ;; Partial match on company
  10599. (bbdb-company name nil)
  10600. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10601. ;; General match including network address and notes
  10602. (bbdb name nil)
  10603. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  10604. (delete-window (get-buffer-window "*BBDB*"))
  10605. (error "No matching BBDB record")))))
  10606. (defun org-follow-info-link (name)
  10607. "Follow an info file & node link to NAME."
  10608. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  10609. (string-match "\\(.*\\)" name))
  10610. (progn
  10611. (require 'info)
  10612. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  10613. (Info-find-node (match-string 1 name) (match-string 2 name))
  10614. (Info-find-node (match-string 1 name) "Top")))
  10615. (message (concat "Could not open: " name))))
  10616. (defun org-follow-gnus-link (&optional group article)
  10617. "Follow a Gnus link to GROUP and ARTICLE."
  10618. (require 'gnus)
  10619. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  10620. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  10621. (cond ((and group article)
  10622. (gnus-group-read-group 1 nil group)
  10623. (gnus-summary-goto-article (string-to-number article) nil t))
  10624. (group (gnus-group-jump-to-group group))))
  10625. (defun org-follow-vm-link (&optional folder article readonly)
  10626. "Follow a VM link to FOLDER and ARTICLE."
  10627. (require 'vm)
  10628. (setq article (org-add-angle-brackets article))
  10629. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  10630. ;; ange-ftp or efs or tramp access
  10631. (let ((user (or (match-string 1 folder) (user-login-name)))
  10632. (host (match-string 2 folder))
  10633. (file (match-string 3 folder)))
  10634. (cond
  10635. ((featurep 'tramp)
  10636. ;; use tramp to access the file
  10637. (if (featurep 'xemacs)
  10638. (setq folder (format "[%s@%s]%s" user host file))
  10639. (setq folder (format "/%s@%s:%s" user host file))))
  10640. (t
  10641. ;; use ange-ftp or efs
  10642. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  10643. (setq folder (format "/%s@%s:%s" user host file))))))
  10644. (when folder
  10645. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  10646. (sit-for 0.1)
  10647. (when article
  10648. (vm-select-folder-buffer)
  10649. (widen)
  10650. (let ((case-fold-search t))
  10651. (goto-char (point-min))
  10652. (if (not (re-search-forward
  10653. (concat "^" "message-id: *" (regexp-quote article))))
  10654. (error "Could not find the specified message in this folder"))
  10655. (vm-isearch-update)
  10656. (vm-isearch-narrow)
  10657. (vm-beginning-of-message)
  10658. (vm-summarize)))))
  10659. (defun org-follow-wl-link (folder article)
  10660. "Follow a Wanderlust link to FOLDER and ARTICLE."
  10661. (if (and (string= folder "%")
  10662. article
  10663. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  10664. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  10665. ;; Thus, we recompose folder and article ids.
  10666. (setq folder (format "%s#%s" folder (match-string 1 article))
  10667. article (match-string 3 article)))
  10668. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  10669. (error "No such folder: %s" folder))
  10670. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  10671. (and article
  10672. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  10673. (wl-summary-redisplay)))
  10674. (defun org-follow-rmail-link (folder article)
  10675. "Follow an RMAIL link to FOLDER and ARTICLE."
  10676. (setq article (org-add-angle-brackets article))
  10677. (let (message-number)
  10678. (save-excursion
  10679. (save-window-excursion
  10680. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10681. (setq message-number
  10682. (save-restriction
  10683. (widen)
  10684. (goto-char (point-max))
  10685. (if (re-search-backward
  10686. (concat "^Message-ID:\\s-+" (regexp-quote
  10687. (or article "")))
  10688. nil t)
  10689. (rmail-what-message))))))
  10690. (if message-number
  10691. (progn
  10692. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10693. (rmail-show-message message-number)
  10694. message-number)
  10695. (error "Message not found"))))
  10696. ;;; mh-e integration based on planner-mode
  10697. (defun org-mhe-get-message-real-folder ()
  10698. "Return the name of the current message real folder, so if you use
  10699. sequences, it will now work."
  10700. (save-excursion
  10701. (let* ((folder
  10702. (if (equal major-mode 'mh-folder-mode)
  10703. mh-current-folder
  10704. ;; Refer to the show buffer
  10705. mh-show-folder-buffer))
  10706. (end-index
  10707. (if (boundp 'mh-index-folder)
  10708. (min (length mh-index-folder) (length folder))))
  10709. )
  10710. ;; a simple test on mh-index-data does not work, because
  10711. ;; mh-index-data is always nil in a show buffer.
  10712. (if (and (boundp 'mh-index-folder)
  10713. (string= mh-index-folder (substring folder 0 end-index)))
  10714. (if (equal major-mode 'mh-show-mode)
  10715. (save-window-excursion
  10716. (when (buffer-live-p (get-buffer folder))
  10717. (progn
  10718. (pop-to-buffer folder)
  10719. (org-mhe-get-message-folder-from-index)
  10720. )
  10721. ))
  10722. (org-mhe-get-message-folder-from-index)
  10723. )
  10724. folder
  10725. )
  10726. )))
  10727. (defun org-mhe-get-message-folder-from-index ()
  10728. "Returns the name of the message folder in a index folder buffer."
  10729. (save-excursion
  10730. (mh-index-previous-folder)
  10731. (re-search-forward "^\\(+.*\\)$" nil t)
  10732. (message (match-string 1))))
  10733. (defun org-mhe-get-message-folder ()
  10734. "Return the name of the current message folder. Be careful if you
  10735. use sequences."
  10736. (save-excursion
  10737. (if (equal major-mode 'mh-folder-mode)
  10738. mh-current-folder
  10739. ;; Refer to the show buffer
  10740. mh-show-folder-buffer)))
  10741. (defun org-mhe-get-message-num ()
  10742. "Return the number of the current message. Be careful if you
  10743. use sequences."
  10744. (save-excursion
  10745. (if (equal major-mode 'mh-folder-mode)
  10746. (mh-get-msg-num nil)
  10747. ;; Refer to the show buffer
  10748. (mh-show-buffer-message-number))))
  10749. (defun org-mhe-get-header (header)
  10750. "Return a header of the message in folder mode. This will create a
  10751. show buffer for the corresponding message. If you have a more clever
  10752. idea..."
  10753. (let* ((folder (org-mhe-get-message-folder))
  10754. (num (org-mhe-get-message-num))
  10755. (buffer (get-buffer-create (concat "show-" folder)))
  10756. (header-field))
  10757. (with-current-buffer buffer
  10758. (mh-display-msg num folder)
  10759. (if (equal major-mode 'mh-folder-mode)
  10760. (mh-header-display)
  10761. (mh-show-header-display))
  10762. (set-buffer buffer)
  10763. (setq header-field (mh-get-header-field header))
  10764. (if (equal major-mode 'mh-folder-mode)
  10765. (mh-show)
  10766. (mh-show-show))
  10767. header-field)))
  10768. (defun org-follow-mhe-link (folder article)
  10769. "Follow an MHE link to FOLDER and ARTICLE.
  10770. If ARTICLE is nil FOLDER is shown. If the configuration variable
  10771. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  10772. ARTICLE is searched in all folders. Indexed searches (swish++,
  10773. namazu, and others supported by MH-E) will always search in all
  10774. folders."
  10775. (require 'mh-e)
  10776. (require 'mh-search)
  10777. (require 'mh-utils)
  10778. (mh-find-path)
  10779. (if (not article)
  10780. (mh-visit-folder (mh-normalize-folder-name folder))
  10781. (setq article (org-add-angle-brackets article))
  10782. (mh-search-choose)
  10783. (if (equal mh-searcher 'pick)
  10784. (progn
  10785. (mh-search folder (list "--message-id" article))
  10786. (when (and org-mhe-search-all-folders
  10787. (not (org-mhe-get-message-real-folder)))
  10788. (kill-this-buffer)
  10789. (mh-search "+" (list "--message-id" article))))
  10790. (mh-search "+" article))
  10791. (if (org-mhe-get-message-real-folder)
  10792. (mh-show-msg 1)
  10793. (kill-this-buffer)
  10794. (error "Message not found"))))
  10795. ;;; BibTeX links
  10796. ;; Use the custom search meachnism to construct and use search strings for
  10797. ;; file links to BibTeX database entries.
  10798. (defun org-create-file-search-in-bibtex ()
  10799. "Create the search string and description for a BibTeX database entry."
  10800. (when (eq major-mode 'bibtex-mode)
  10801. ;; yes, we want to construct this search string.
  10802. ;; Make a good description for this entry, using names, year and the title
  10803. ;; Put it into the `description' variable which is dynamically scoped.
  10804. (let ((bibtex-autokey-names 1)
  10805. (bibtex-autokey-names-stretch 1)
  10806. (bibtex-autokey-name-case-convert-function 'identity)
  10807. (bibtex-autokey-name-separator " & ")
  10808. (bibtex-autokey-additional-names " et al.")
  10809. (bibtex-autokey-year-length 4)
  10810. (bibtex-autokey-name-year-separator " ")
  10811. (bibtex-autokey-titlewords 3)
  10812. (bibtex-autokey-titleword-separator " ")
  10813. (bibtex-autokey-titleword-case-convert-function 'identity)
  10814. (bibtex-autokey-titleword-length 'infty)
  10815. (bibtex-autokey-year-title-separator ": "))
  10816. (setq description (bibtex-generate-autokey)))
  10817. ;; Now parse the entry, get the key and return it.
  10818. (save-excursion
  10819. (bibtex-beginning-of-entry)
  10820. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  10821. (defun org-execute-file-search-in-bibtex (s)
  10822. "Find the link search string S as a key for a database entry."
  10823. (when (eq major-mode 'bibtex-mode)
  10824. ;; Yes, we want to do the search in this file.
  10825. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  10826. (goto-char (point-min))
  10827. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  10828. (regexp-quote s) "[ \t\n]*,") nil t)
  10829. (goto-char (match-beginning 0)))
  10830. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  10831. ;; Use double prefix to indicate that any web link should be browsed
  10832. (let ((b (current-buffer)) (p (point)))
  10833. ;; Restore the window configuration because we just use the web link
  10834. (set-window-configuration org-window-config-before-follow-link)
  10835. (save-excursion (set-buffer b) (goto-char p)
  10836. (bibtex-url)))
  10837. (recenter 0)) ; Move entry start to beginning of window
  10838. ;; return t to indicate that the search is done.
  10839. t))
  10840. ;; Finally add the functions to the right hooks.
  10841. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  10842. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  10843. ;; end of Bibtex link setup
  10844. ;;; Following file links
  10845. (defun org-open-file (path &optional in-emacs line search)
  10846. "Open the file at PATH.
  10847. First, this expands any special file name abbreviations. Then the
  10848. configuration variable `org-file-apps' is checked if it contains an
  10849. entry for this file type, and if yes, the corresponding command is launched.
  10850. If no application is found, Emacs simply visits the file.
  10851. With optional argument IN-EMACS, Emacs will visit the file.
  10852. Optional LINE specifies a line to go to, optional SEARCH a string to
  10853. search for. If LINE or SEARCH is given, the file will always be
  10854. opened in Emacs.
  10855. If the file does not exist, an error is thrown."
  10856. (setq in-emacs (or in-emacs line search))
  10857. (let* ((file (if (equal path "")
  10858. buffer-file-name
  10859. (substitute-in-file-name (expand-file-name path))))
  10860. (apps (append org-file-apps (org-default-apps)))
  10861. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10862. (dirp (if remp nil (file-directory-p file)))
  10863. (dfile (downcase file))
  10864. (old-buffer (current-buffer))
  10865. (old-pos (point))
  10866. (old-mode major-mode)
  10867. ext cmd)
  10868. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10869. (setq ext (match-string 1 dfile))
  10870. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10871. (setq ext (match-string 1 dfile))))
  10872. (if in-emacs
  10873. (setq cmd 'emacs)
  10874. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10875. (and dirp (cdr (assoc 'directory apps)))
  10876. (cdr (assoc ext apps))
  10877. (cdr (assoc t apps)))))
  10878. (when (eq cmd 'mailcap)
  10879. (require 'mailcap)
  10880. (mailcap-parse-mailcaps)
  10881. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10882. (command (mailcap-mime-info mime-type)))
  10883. (if (stringp command)
  10884. (setq cmd command)
  10885. (setq cmd 'emacs))))
  10886. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10887. (not (file-exists-p file))
  10888. (not org-open-non-existing-files))
  10889. (error "No such file: %s" file))
  10890. (cond
  10891. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10892. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10893. (if (string-match "['\"]%s['\"]" cmd)
  10894. (setq cmd (replace-match "%s" t t cmd)))
  10895. (setq cmd (format cmd (shell-quote-argument file)))
  10896. (save-window-excursion
  10897. (shell-command (concat cmd " &"))))
  10898. ((or (stringp cmd)
  10899. (eq cmd 'emacs))
  10900. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10901. (widen)
  10902. (if line (goto-line line)
  10903. (if search (org-link-search search))))
  10904. ((consp cmd)
  10905. (eval cmd))
  10906. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10907. (and (org-mode-p) (eq old-mode 'org-mode)
  10908. (or (not (equal old-buffer (current-buffer)))
  10909. (not (equal old-pos (point))))
  10910. (org-mark-ring-push old-pos old-buffer))))
  10911. (defun org-default-apps ()
  10912. "Return the default applications for this operating system."
  10913. (cond
  10914. ((eq system-type 'darwin)
  10915. org-file-apps-defaults-macosx)
  10916. ((eq system-type 'windows-nt)
  10917. org-file-apps-defaults-windowsnt)
  10918. (t org-file-apps-defaults-gnu)))
  10919. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10920. (defun org-file-remote-p (file)
  10921. "Test whether FILE specifies a location on a remote system.
  10922. Return non-nil if the location is indeed remote.
  10923. For example, the filename \"/user@host:/foo\" specifies a location
  10924. on the system \"/user@host:\"."
  10925. (cond ((fboundp 'file-remote-p)
  10926. (file-remote-p file))
  10927. ((fboundp 'tramp-handle-file-remote-p)
  10928. (tramp-handle-file-remote-p file))
  10929. ((and (boundp 'ange-ftp-name-format)
  10930. (string-match (car ange-ftp-name-format) file))
  10931. t)
  10932. (t nil)))
  10933. ;;;; Hooks for remember.el
  10934. ;;;###autoload
  10935. (defun org-remember-annotation ()
  10936. "Return a link to the current location as an annotation for remember.el.
  10937. If you are using Org-mode files as target for data storage with
  10938. remember.el, then the annotations should include a link compatible with the
  10939. conventions in Org-mode. This function returns such a link."
  10940. (org-store-link nil))
  10941. (defconst org-remember-help
  10942. "Select a destination location for the note.
  10943. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  10944. RET at beg-of-buf -> Append to file as level 2 headline
  10945. RET on headline -> Store as sublevel entry to current headline
  10946. <left>/<right> -> before/after current headline, same headings level")
  10947. ;;;###autoload
  10948. (defun org-remember-apply-template (&optional use-char skip-interactive)
  10949. "Initialize *remember* buffer with template, invoke `org-mode'.
  10950. This function should be placed into `remember-mode-hook' and in fact requires
  10951. to be run from that hook to fucntion properly."
  10952. (if org-remember-templates
  10953. (let* ((char (or use-char
  10954. (if (= (length org-remember-templates) 1)
  10955. (caar org-remember-templates)
  10956. (message "Select template: %s"
  10957. (mapconcat
  10958. (lambda (x) (char-to-string (car x)))
  10959. org-remember-templates " "))
  10960. (read-char-exclusive))))
  10961. (entry (cdr (assoc char org-remember-templates)))
  10962. (tpl (car entry))
  10963. (plist-p (if org-store-link-plist t nil))
  10964. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  10965. (string-match "\\S-" (nth 1 entry)))
  10966. (nth 1 entry)
  10967. org-default-notes-file))
  10968. (headline (nth 2 entry))
  10969. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  10970. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  10971. (v-u (concat "[" (substring v-t 1 -1) "]"))
  10972. (v-U (concat "[" (substring v-T 1 -1) "]"))
  10973. (v-i initial) ; defined in `remember-mode'
  10974. (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
  10975. (v-n user-full-name)
  10976. (org-startup-folded nil)
  10977. org-time-was-given org-end-time-was-given x prompt char time)
  10978. (setq org-store-link-plist
  10979. (append (list :annotation v-a :initial v-i)
  10980. org-store-link-plist))
  10981. (unless tpl (setq tpl "") (message "No template") (ding))
  10982. (erase-buffer)
  10983. (insert (substitute-command-keys
  10984. (format
  10985. "## `C-c C-c' to file interactively, `C-u C-c C-c' to file directly.
  10986. ## Target file \"%s\", headline \"%s\"
  10987. ## To switch templates, use `\\[org-remember]'.\n\n"
  10988. (abbreviate-file-name (or file org-default-notes-file))
  10989. (or headline ""))))
  10990. (insert tpl) (goto-char (point-min))
  10991. ;; Simple %-escapes
  10992. (while (re-search-forward "%\\([tTuUai]\\)" nil t)
  10993. (when (and initial (equal (match-string 0) "%i"))
  10994. (save-match-data
  10995. (let* ((lead (buffer-substring
  10996. (point-at-bol) (match-beginning 0))))
  10997. (setq v-i (mapconcat 'identity
  10998. (org-split-string initial "\n")
  10999. (concat "\n" lead))))))
  11000. (replace-match
  11001. (or (eval (intern (concat "v-" (match-string 1)))) "")
  11002. t t))
  11003. ;; From the property list
  11004. (when plist-p
  11005. (goto-char (point-min))
  11006. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  11007. (and (setq x (plist-get org-store-link-plist
  11008. (intern (match-string 1))))
  11009. (replace-match x t t))))
  11010. ;; Turn on org-mode in the remember buffer, set local variables
  11011. (org-mode)
  11012. (org-set-local 'org-finish-function 'remember-buffer)
  11013. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  11014. (org-set-local 'org-default-notes-file file))
  11015. (if (and headline (stringp headline) (string-match "\\S-" headline))
  11016. (org-set-local 'org-remember-default-headline headline))
  11017. ;; Interactive template entries
  11018. (goto-char (point-min))
  11019. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  11020. (setq char (if (match-end 3) (match-string 3))
  11021. prompt (if (match-end 2) (match-string 2)))
  11022. (goto-char (match-beginning 0))
  11023. (replace-match "")
  11024. (cond
  11025. ((member char '("G" "g"))
  11026. (let* ((org-last-tags-completion-table
  11027. (org-global-tags-completion-table
  11028. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  11029. (org-add-colon-after-tag-completion t)
  11030. (ins (completing-read
  11031. (if prompt (concat prompt ": ") "Tags: ")
  11032. 'org-tags-completion-function nil nil nil
  11033. 'org-tags-history)))
  11034. (setq ins (mapconcat 'identity
  11035. (org-split-string ins (org-re "[^[:alnum:]]+"))
  11036. ":"))
  11037. (when (string-match "\\S-" ins)
  11038. (or (equal (char-before) ?:) (insert ":"))
  11039. (insert ins)
  11040. (or (equal (char-after) ?:) (insert ":")))))
  11041. (char
  11042. (setq org-time-was-given (equal (upcase char) char))
  11043. (setq time (org-read-date (equal (upcase char) "U") t nil
  11044. prompt))
  11045. (org-insert-time-stamp time org-time-was-given
  11046. (member char '("u" "U"))
  11047. nil nil (list org-end-time-was-given)))
  11048. (t
  11049. (insert (read-string
  11050. (if prompt (concat prompt ": ") "Enter string"))))))
  11051. (goto-char (point-min))
  11052. (if (re-search-forward "%\\?" nil t)
  11053. (replace-match "")
  11054. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  11055. (org-mode)
  11056. (org-set-local 'org-finish-function 'remember-buffer)))
  11057. ;;;###autoload
  11058. (defun org-remember ()
  11059. "Call `remember'. If this is already a remember buffer, re-apply template.
  11060. If there is an active region, make sure remember uses it as initial content
  11061. of the remember buffer."
  11062. (interactive)
  11063. (if (eq org-finish-function 'remember-buffer)
  11064. (progn
  11065. (when (< (length org-remember-templates) 2)
  11066. (error "No other template available"))
  11067. (erase-buffer)
  11068. (let ((annotation (plist-get org-store-link-plist :annotation))
  11069. (initial (plist-get org-store-link-plist :initial)))
  11070. (org-remember-apply-template))
  11071. (message "Press C-c C-c to remember data"))
  11072. (if (org-region-active-p)
  11073. (remember (buffer-substring (point) (mark)))
  11074. (call-interactively 'remember))))
  11075. ;;;###autoload
  11076. (defun org-remember-handler ()
  11077. "Store stuff from remember.el into an org file.
  11078. First prompts for an org file. If the user just presses return, the value
  11079. of `org-default-notes-file' is used.
  11080. Then the command offers the headings tree of the selected file in order to
  11081. file the text at a specific location.
  11082. You can either immediately press RET to get the note appended to the
  11083. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  11084. find a better place. Then press RET or <left> or <right> in insert the note.
  11085. Key Cursor position Note gets inserted
  11086. -----------------------------------------------------------------------------
  11087. RET buffer-start as level 2 heading at end of file
  11088. RET on headline as sublevel of the heading at cursor
  11089. RET no heading at cursor position, level taken from context.
  11090. Or use prefix arg to specify level manually.
  11091. <left> on headline as same level, before current heading
  11092. <right> on headline as same level, after current heading
  11093. So the fastest way to store the note is to press RET RET to append it to
  11094. the default file. This way your current train of thought is not
  11095. interrupted, in accordance with the principles of remember.el.
  11096. You can also get the fast execution without prompting by using
  11097. C-u C-c C-c to exit the remember buffer. See also the variable
  11098. `org-remember-store-without-prompt'.
  11099. Before being stored away, the function ensures that the text has a
  11100. headline, i.e. a first line that starts with a \"*\". If not, a headline
  11101. is constructed from the current date and some additional data.
  11102. If the variable `org-adapt-indentation' is non-nil, the entire text is
  11103. also indented so that it starts in the same column as the headline
  11104. \(i.e. after the stars).
  11105. See also the variable `org-reverse-note-order'."
  11106. (goto-char (point-min))
  11107. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  11108. (replace-match ""))
  11109. (catch 'quit
  11110. (let* ((txt (buffer-substring (point-min) (point-max)))
  11111. (fastp (org-xor (equal current-prefix-arg '(4))
  11112. org-remember-store-without-prompt))
  11113. (file (if fastp org-default-notes-file (org-get-org-file)))
  11114. (heading org-remember-default-headline)
  11115. (visiting (org-find-base-buffer-visiting file))
  11116. (org-startup-folded nil)
  11117. (org-startup-align-all-tables nil)
  11118. (org-goto-start-pos 1)
  11119. spos level indent reversed)
  11120. (setq current-prefix-arg nil)
  11121. ;; Modify text so that it becomes a nice subtree which can be inserted
  11122. ;; into an org tree.
  11123. (let* ((lines (split-string txt "\n"))
  11124. first)
  11125. (setq first (car lines) lines (cdr lines))
  11126. (if (string-match "^\\*+ " first)
  11127. ;; Is already a headline
  11128. (setq indent nil)
  11129. ;; We need to add a headline: Use time and first buffer line
  11130. (setq lines (cons first lines)
  11131. first (concat "* " (current-time-string)
  11132. " (" (remember-buffer-desc) ")")
  11133. indent " "))
  11134. (if (and org-adapt-indentation indent)
  11135. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  11136. (setq txt (concat first "\n"
  11137. (mapconcat 'identity lines "\n"))))
  11138. ;; Find the file
  11139. (if (not visiting) (find-file-noselect file))
  11140. (with-current-buffer (or visiting (get-file-buffer file))
  11141. (save-excursion
  11142. (save-restriction
  11143. (widen)
  11144. (and (goto-char (point-min))
  11145. (not (re-search-forward "^\\* " nil t))
  11146. (insert "\n* Notes\n"))
  11147. (setq reversed (org-notes-order-reversed-p))
  11148. ;; Find the default location
  11149. (when (and heading (stringp heading) (string-match "\\S-" heading))
  11150. (goto-char (point-min))
  11151. (if (re-search-forward
  11152. (concat "^\\*+[ \t]+" (regexp-quote heading)
  11153. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  11154. nil t)
  11155. (setq org-goto-start-pos (match-beginning 0))))
  11156. ;; Ask the User for a location
  11157. (setq spos (if fastp
  11158. org-goto-start-pos
  11159. (org-get-location (current-buffer) org-remember-help)))
  11160. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  11161. ; not handle this note
  11162. (goto-char spos)
  11163. (cond ((and (bobp) (not reversed))
  11164. ;; Put it at the end, one level below level 1
  11165. (save-restriction
  11166. (widen)
  11167. (goto-char (point-max))
  11168. (if (not (bolp)) (newline))
  11169. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  11170. ((and (bobp) reversed)
  11171. ;; Put it at the start, as level 1
  11172. (save-restriction
  11173. (widen)
  11174. (goto-char (point-min))
  11175. (re-search-forward "^\\*+ " nil t)
  11176. (beginning-of-line 1)
  11177. (org-paste-subtree 1 txt)))
  11178. ((and (org-on-heading-p t) (not current-prefix-arg))
  11179. ;; Put it below this entry, at the beg/end of the subtree
  11180. (org-back-to-heading t)
  11181. (setq level (funcall outline-level))
  11182. (if reversed
  11183. (outline-next-heading)
  11184. (org-end-of-subtree t))
  11185. (if (not (bolp)) (newline))
  11186. (beginning-of-line 1)
  11187. (org-paste-subtree (org-get-legal-level level 1) txt))
  11188. (t
  11189. ;; Put it right there, with automatic level determined by
  11190. ;; org-paste-subtree or from prefix arg
  11191. (org-paste-subtree
  11192. (if (numberp current-prefix-arg) current-prefix-arg)
  11193. txt)))
  11194. (when remember-save-after-remembering
  11195. (save-buffer)
  11196. (if (not visiting) (kill-buffer (current-buffer)))))))))
  11197. t) ;; return t to indicate that we took care of this note.
  11198. (defun org-get-org-file ()
  11199. "Read a filename, with default directory `org-directory'."
  11200. (let ((default (or org-default-notes-file remember-data-file)))
  11201. (read-file-name (format "File name [%s]: " default)
  11202. (file-name-as-directory org-directory)
  11203. default)))
  11204. (defun org-notes-order-reversed-p ()
  11205. "Check if the current file should receive notes in reversed order."
  11206. (cond
  11207. ((not org-reverse-note-order) nil)
  11208. ((eq t org-reverse-note-order) t)
  11209. ((not (listp org-reverse-note-order)) nil)
  11210. (t (catch 'exit
  11211. (let ((all org-reverse-note-order)
  11212. entry)
  11213. (while (setq entry (pop all))
  11214. (if (string-match (car entry) buffer-file-name)
  11215. (throw 'exit (cdr entry))))
  11216. nil)))))
  11217. ;;;; Dynamic blocks
  11218. (defun org-find-dblock (name)
  11219. "Find the first dynamic block with name NAME in the buffer.
  11220. If not found, stay at current position and return nil."
  11221. (let (pos)
  11222. (save-excursion
  11223. (goto-char (point-min))
  11224. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  11225. nil t)
  11226. (match-beginning 0))))
  11227. (if pos (goto-char pos))
  11228. pos))
  11229. (defconst org-dblock-start-re
  11230. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  11231. "Matches the startline of a dynamic block, with parameters.")
  11232. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  11233. "Matches the end of a dyhamic block.")
  11234. (defun org-create-dblock (plist)
  11235. "Create a dynamic block section, with parameters taken from PLIST.
  11236. PLIST must containe a :name entry which is used as name of the block."
  11237. (unless (bolp) (newline))
  11238. (let ((name (plist-get plist :name)))
  11239. (insert "#+BEGIN: " name)
  11240. (while plist
  11241. (if (eq (car plist) :name)
  11242. (setq plist (cddr plist))
  11243. (insert " " (prin1-to-string (pop plist)))))
  11244. (insert "\n\n#+END:\n")
  11245. (beginning-of-line -2)))
  11246. (defun org-prepare-dblock ()
  11247. "Prepare dynamic block for refresh.
  11248. This empties the block, puts the cursor at the insert position and returns
  11249. the property list including an extra property :name with the block name."
  11250. (unless (looking-at org-dblock-start-re)
  11251. (error "Not at a dynamic block"))
  11252. (let* ((begdel (1+ (match-end 0)))
  11253. (name (match-string 1))
  11254. (params (append (list :name name)
  11255. (read (concat "(" (match-string 3) ")")))))
  11256. (unless (re-search-forward org-dblock-end-re nil t)
  11257. (error "Dynamic block not terminated"))
  11258. (delete-region begdel (match-beginning 0))
  11259. (goto-char begdel)
  11260. (open-line 1)
  11261. params))
  11262. (defun org-map-dblocks (&optional command)
  11263. "Apply COMMAND to all dynamic blocks in the current buffer.
  11264. If COMMAND is not given, use `org-update-dblock'."
  11265. (let ((cmd (or command 'org-update-dblock))
  11266. pos)
  11267. (save-excursion
  11268. (goto-char (point-min))
  11269. (while (re-search-forward org-dblock-start-re nil t)
  11270. (goto-char (setq pos (match-beginning 0)))
  11271. (condition-case nil
  11272. (funcall cmd)
  11273. (error (message "Error during update of dynamic block")))
  11274. (goto-char pos)
  11275. (unless (re-search-forward org-dblock-end-re nil t)
  11276. (error "Dynamic block not terminated"))))))
  11277. (defun org-dblock-update (&optional arg)
  11278. "User command for updating dynamic blocks.
  11279. Update the dynamic block at point. With prefix ARG, update all dynamic
  11280. blocks in the buffer."
  11281. (interactive "P")
  11282. (if arg
  11283. (org-update-all-dblocks)
  11284. (or (looking-at org-dblock-start-re)
  11285. (org-beginning-of-dblock))
  11286. (org-update-dblock)))
  11287. (defun org-update-dblock ()
  11288. "Update the dynamic block at point
  11289. This means to empty the block, parse for parameters and then call
  11290. the correct writing function."
  11291. (let* ((pos (point))
  11292. (params (org-prepare-dblock))
  11293. (name (plist-get params :name))
  11294. (cmd (intern (concat "org-dblock-write:" name))))
  11295. (funcall cmd params)
  11296. (goto-char pos)))
  11297. (defun org-beginning-of-dblock ()
  11298. "Find the beginning of the dynamic block at point.
  11299. Error if there is no scuh block at point."
  11300. (let ((pos (point))
  11301. beg)
  11302. (end-of-line 1)
  11303. (if (and (re-search-backward org-dblock-start-re nil t)
  11304. (setq beg (match-beginning 0))
  11305. (re-search-forward org-dblock-end-re nil t)
  11306. (> (match-end 0) pos))
  11307. (goto-char beg)
  11308. (goto-char pos)
  11309. (error "Not in a dynamic block"))))
  11310. (defun org-update-all-dblocks ()
  11311. "Update all dynamic blocks in the buffer.
  11312. This function can be used in a hook."
  11313. (when (org-mode-p)
  11314. (org-map-dblocks 'org-update-dblock)))
  11315. ;;;; Completion
  11316. (defun org-complete (&optional arg)
  11317. "Perform completion on word at point.
  11318. At the beginning of a headline, this completes TODO keywords as given in
  11319. `org-todo-keywords'.
  11320. If the current word is preceded by a backslash, completes the TeX symbols
  11321. that are supported for HTML support.
  11322. If the current word is preceded by \"#+\", completes special words for
  11323. setting file options.
  11324. In the line after \"#+STARTUP:, complete valid keywords.\"
  11325. At all other locations, this simply calls `ispell-complete-word'."
  11326. (interactive "P")
  11327. (catch 'exit
  11328. (let* ((end (point))
  11329. (beg1 (save-excursion
  11330. (skip-chars-backward (org-re "[:alnum:]_@"))
  11331. (point)))
  11332. (beg (save-excursion
  11333. (skip-chars-backward "a-zA-Z0-9_:$")
  11334. (point)))
  11335. (confirm (lambda (x) (stringp (car x))))
  11336. (searchhead (equal (char-before beg) ?*))
  11337. (tag (and (equal (char-before beg1) ?:)
  11338. (equal (char-after (point-at-bol)) ?*)))
  11339. (prop (and (equal (char-before beg1) ?:)
  11340. (not (equal (char-after (point-at-bol)) ?*))))
  11341. (texp (equal (char-before beg) ?\\))
  11342. (link (equal (char-before beg) ?\[))
  11343. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  11344. beg)
  11345. "#+"))
  11346. (startup (string-match "^#\\+STARTUP:.*"
  11347. (buffer-substring (point-at-bol) (point))))
  11348. (completion-ignore-case opt)
  11349. (type nil)
  11350. (tbl nil)
  11351. (table (cond
  11352. (opt
  11353. (setq type :opt)
  11354. (mapcar (lambda (x)
  11355. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  11356. (cons (match-string 2 x) (match-string 1 x)))
  11357. (org-split-string (org-get-current-options) "\n")))
  11358. (startup
  11359. (setq type :startup)
  11360. org-startup-options)
  11361. (link (append org-link-abbrev-alist-local
  11362. org-link-abbrev-alist))
  11363. (texp
  11364. (setq type :tex)
  11365. org-html-entities)
  11366. ((string-match "\\`\\*+[ \t]+\\'"
  11367. (buffer-substring (point-at-bol) beg))
  11368. (setq type :todo)
  11369. (mapcar 'list org-todo-keywords-1))
  11370. (searchhead
  11371. (setq type :searchhead)
  11372. (save-excursion
  11373. (goto-char (point-min))
  11374. (while (re-search-forward org-todo-line-regexp nil t)
  11375. (push (list
  11376. (org-make-org-heading-search-string
  11377. (match-string 3) t))
  11378. tbl)))
  11379. tbl)
  11380. (tag (setq type :tag beg beg1)
  11381. (or org-tag-alist (org-get-buffer-tags)))
  11382. (prop (setq type :prop beg beg1)
  11383. (mapcar 'list (org-buffer-property-keys)))
  11384. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  11385. (pattern (buffer-substring-no-properties beg end))
  11386. (completion (try-completion pattern table confirm)))
  11387. (cond ((eq completion t)
  11388. (if (equal type :opt)
  11389. (insert (substring (cdr (assoc (upcase pattern) table))
  11390. (length pattern)))
  11391. (if (memq type '(:tag :prop)) (insert ":"))))
  11392. ((null completion)
  11393. (message "Can't find completion for \"%s\"" pattern)
  11394. (ding))
  11395. ((not (string= pattern completion))
  11396. (delete-region beg end)
  11397. (if (string-match " +$" completion)
  11398. (setq completion (replace-match "" t t completion)))
  11399. (insert completion)
  11400. (if (get-buffer-window "*Completions*")
  11401. (delete-window (get-buffer-window "*Completions*")))
  11402. (if (assoc completion table)
  11403. (if (eq type :todo) (insert " ")
  11404. (if (memq type '(:tag :prop)) (insert ":"))))
  11405. (if (and (equal type :opt) (assoc completion table))
  11406. (message "%s" (substitute-command-keys
  11407. "Press \\[org-complete] again to insert example settings"))))
  11408. (t
  11409. (message "Making completion list...")
  11410. (let ((list (sort (all-completions pattern table confirm)
  11411. 'string<)))
  11412. (with-output-to-temp-buffer "*Completions*"
  11413. (condition-case nil
  11414. ;; Protection needed for XEmacs and emacs 21
  11415. (display-completion-list list pattern)
  11416. (error (display-completion-list list)))))
  11417. (message "Making completion list...%s" "done"))))))
  11418. ;;;; TODO, DEADLINE, Comments
  11419. (defun org-toggle-comment ()
  11420. "Change the COMMENT state of an entry."
  11421. (interactive)
  11422. (save-excursion
  11423. (org-back-to-heading)
  11424. (if (looking-at (concat outline-regexp
  11425. "\\( *\\<" org-comment-string "\\>\\)"))
  11426. (replace-match "" t t nil 1)
  11427. (if (looking-at outline-regexp)
  11428. (progn
  11429. (goto-char (match-end 0))
  11430. (insert org-comment-string " "))))))
  11431. (defvar org-last-todo-state-is-todo nil
  11432. "This is non-nil when the last TODO state change led to a TODO state.
  11433. If the last change removed the TODO tag or switched to DONE, then
  11434. this is nil.")
  11435. (defun org-todo (&optional arg)
  11436. "Change the TODO state of an item.
  11437. The state of an item is given by a keyword at the start of the heading,
  11438. like
  11439. *** TODO Write paper
  11440. *** DONE Call mom
  11441. The different keywords are specified in the variable `org-todo-keywords'.
  11442. By default the available states are \"TODO\" and \"DONE\".
  11443. So for this example: when the item starts with TODO, it is changed to DONE.
  11444. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11445. DONE are present, add TODO at the beginning of the heading.
  11446. With C-u prefix arg, use completion to determine the new state.
  11447. With numeric prefix arg, switch to that state.
  11448. For calling through lisp, arg is also interpreted in the following way:
  11449. 'none -> empty state
  11450. \"\"(empty string) -> switch to empty state
  11451. 'done -> switch to DONE
  11452. 'nextset -> switch to the next set of keywords
  11453. 'previousset -> switch to the previous set of keywords
  11454. \"WAITING\" -> switch to the specified keyword, but only if it
  11455. really is a member of `org-todo-keywords'."
  11456. (interactive "P")
  11457. (save-excursion
  11458. (org-back-to-heading)
  11459. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  11460. (or (looking-at (concat " +" org-todo-regexp " *"))
  11461. (looking-at " *"))
  11462. (let* ((this (match-string 1))
  11463. (head (org-get-todo-sequence-head this))
  11464. (ass (assoc head org-todo-kwd-alist))
  11465. (interpret (nth 1 ass))
  11466. (done-word (nth 3 ass))
  11467. (final-done-word (nth 4 ass))
  11468. (last-state (or this ""))
  11469. (completion-ignore-case t)
  11470. (member (member this org-todo-keywords-1))
  11471. (tail (cdr member))
  11472. (state (cond
  11473. ((equal arg '(4))
  11474. ;; Read a state with completion
  11475. (completing-read "State: " (mapcar (lambda(x) (list x))
  11476. org-todo-keywords-1)
  11477. nil t))
  11478. ((eq arg 'right)
  11479. (if this
  11480. (if tail (car tail) nil)
  11481. (car org-todo-keywords-1)))
  11482. ((eq arg 'left)
  11483. (if (equal member org-todo-keywords-1)
  11484. nil
  11485. (if this
  11486. (nth (- (length org-todo-keywords-1) (length tail) 2)
  11487. org-todo-keywords-1)
  11488. (org-last org-todo-keywords-1))))
  11489. (arg
  11490. ;; user or caller requests a specific state
  11491. (cond
  11492. ((equal arg "") nil)
  11493. ((eq arg 'none) nil)
  11494. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11495. ((eq arg 'nextset)
  11496. (or (car (cdr (member head org-todo-heads)))
  11497. (car org-todo-heads)))
  11498. ((eq arg 'previousset)
  11499. (let ((org-todo-heads (reverse org-todo-heads)))
  11500. (or (car (cdr (member head org-todo-heads)))
  11501. (car org-todo-heads))))
  11502. ((car (member arg org-todo-keywords-1)))
  11503. ((nth (1- (prefix-numeric-value arg))
  11504. org-todo-keywords-1))))
  11505. ((null member) (or head (car org-todo-keywords-1)))
  11506. ((equal this final-done-word) nil) ;; -> make empty
  11507. ((null tail) nil) ;; -> first entry
  11508. ((eq interpret 'sequence)
  11509. (car tail))
  11510. ((memq interpret '(type priority))
  11511. (if (eq this-command last-command)
  11512. (car tail)
  11513. (if (> (length tail) 0)
  11514. (or done-word (car org-done-keywords))
  11515. nil)))
  11516. (t nil)))
  11517. (next (if state (concat " " state " ") " "))
  11518. dostates)
  11519. (replace-match next t t)
  11520. (unless head
  11521. (setq head (org-get-todo-sequence-head state)
  11522. ass (assoc head org-todo-kwd-alist)
  11523. interpret (nth 1 ass)
  11524. done-word (nth 3 ass)
  11525. final-done-word (nth 4 ass)))
  11526. (when (memq arg '(nextset previousset))
  11527. (message "Keyword-Set %d/%d: %s"
  11528. (- (length org-todo-sets) -1
  11529. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  11530. (length org-todo-sets)
  11531. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  11532. (setq org-last-todo-state-is-todo
  11533. (not (member state org-done-keywords)))
  11534. (when (and org-log-done (not (memq arg '(nextset previousset))))
  11535. (setq dostates (and (eq interpret 'sequence)
  11536. (listp org-log-done) (memq 'state org-log-done)))
  11537. (cond
  11538. ((and state (not this))
  11539. ;; FIXME: should we remove CLOSED already then state is nil?
  11540. (org-add-planning-info nil nil 'closed)
  11541. (and dostates (org-add-log-maybe 'state state 'findpos)))
  11542. ((and state dostates)
  11543. (org-add-log-maybe 'state state 'findpos))
  11544. ((member state org-done-keywords)
  11545. ;; Planning info calls the note-setting command.
  11546. (org-add-planning-info 'closed (org-current-time)
  11547. (if (org-get-repeat) nil 'scheduled))
  11548. (org-add-log-maybe 'done state 'findpos))))
  11549. ;; Fixup tag positioning
  11550. (and org-auto-align-tags (org-set-tags nil t))
  11551. (run-hooks 'org-after-todo-state-change-hook)
  11552. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  11553. (if (and arg (not (member state org-done-keywords)))
  11554. (setq head (org-get-todo-sequence-head state)))
  11555. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
  11556. ;; Fixup cursor location if close to the keyword
  11557. (if (and (outline-on-heading-p)
  11558. (not (bolp))
  11559. (save-excursion (beginning-of-line 1)
  11560. (looking-at org-todo-line-regexp))
  11561. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11562. (progn
  11563. (goto-char (or (match-end 2) (match-end 1)))
  11564. (just-one-space))))
  11565. (defun org-get-todo-sequence-head (kwd)
  11566. "Return the head of the TODO sequence to which KWD belongs.
  11567. If KWD is not set, check if there is a text property remembering the
  11568. right sequence."
  11569. (let (p)
  11570. (cond
  11571. ((not kwd)
  11572. (or (get-text-property (point-at-bol) 'org-todo-head)
  11573. (progn
  11574. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11575. nil (point-at-eol)))
  11576. (get-text-property p 'org-todo-head))))
  11577. ((not (member kwd org-todo-keywords-1))
  11578. (car org-todo-keywords-1))
  11579. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11580. (defun org-get-repeat ()
  11581. "Check if tere is a deadline/schedule with repeater in this entry."
  11582. (save-match-data
  11583. (save-excursion
  11584. (org-back-to-heading t)
  11585. (if (re-search-forward
  11586. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  11587. (match-string 1)))))
  11588. (defvar org-last-changed-timestamp)
  11589. (defvar org-log-post-message)
  11590. (defun org-auto-repeat-maybe ()
  11591. "Check if the current headline contains a repeated deadline/schedule.
  11592. If yes, set TODO state back to what it was and change the base date
  11593. of repeating deadline/scheduled time stamps to new date.
  11594. This function should be run in the `org-after-todo-state-change-hook'."
  11595. ;; last-state is dynamically scoped into this function
  11596. (let* ((repeat (org-get-repeat))
  11597. (aa (assoc last-state org-todo-kwd-alist))
  11598. (interpret (nth 1 aa))
  11599. (head (nth 2 aa))
  11600. (done-word (nth 3 aa))
  11601. (whata '(("d" . day) ("m" . month) ("y" . year)))
  11602. (msg "Entry repeats: ")
  11603. (org-log-done)
  11604. re type n what ts)
  11605. (when repeat
  11606. (org-todo (if (eq interpret 'type) last-state head))
  11607. (when (and org-log-repeat
  11608. (not (memq 'org-add-log-note
  11609. (default-value 'post-command-hook))))
  11610. ;; Make sure a note is taken
  11611. (let ((org-log-done '(done)))
  11612. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  11613. 'findpos)))
  11614. (org-back-to-heading t)
  11615. (org-add-planning-info nil nil 'closed)
  11616. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11617. org-deadline-time-regexp "\\)"))
  11618. (while (re-search-forward
  11619. re (save-excursion (outline-next-heading) (point)) t)
  11620. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  11621. ts (match-string (if (match-end 2) 2 4)))
  11622. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  11623. (setq n (string-to-number (match-string 1 ts))
  11624. what (match-string 2 ts))
  11625. (if (equal what "w") (setq n (* n 7) what "d"))
  11626. (org-timestamp-change n (cdr (assoc what whata))))
  11627. (setq msg (concat msg type org-last-changed-timestamp " ")))
  11628. (setq org-log-post-message msg)
  11629. (message msg))))
  11630. (defun org-show-todo-tree (arg)
  11631. "Make a compact tree which shows all headlines marked with TODO.
  11632. The tree will show the lines where the regexp matches, and all higher
  11633. headlines above the match.
  11634. With \\[universal-argument] prefix, also show the DONE entries.
  11635. With a numeric prefix N, construct a sparse tree for the Nth element
  11636. of `org-todo-keywords-1'."
  11637. (interactive "P")
  11638. (let ((case-fold-search nil)
  11639. (kwd-re
  11640. (cond ((null arg) org-not-done-regexp)
  11641. ((equal arg '(4))
  11642. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  11643. (mapcar 'list org-todo-keywords-1))))
  11644. (concat "\\("
  11645. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11646. "\\)\\>")))
  11647. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11648. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11649. org-todo-keywords-1)))
  11650. (t (error "Invalid prefix argument: %s" arg)))))
  11651. (message "%d TODO entries found"
  11652. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  11653. (defun org-deadline ()
  11654. "Insert the DEADLINE: string to make a deadline.
  11655. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11656. to modify it to the correct date."
  11657. (interactive)
  11658. (org-add-planning-info 'deadline nil 'closed))
  11659. (defun org-schedule ()
  11660. "Insert the SCHEDULED: string to schedule a TODO item.
  11661. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11662. to modify it to the correct date."
  11663. (interactive)
  11664. (org-add-planning-info 'scheduled nil 'closed))
  11665. (defun org-add-planning-info (what &optional time &rest remove)
  11666. "Insert new timestamp with keyword in the line directly after the headline.
  11667. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  11668. If non is given, the user is prompted for a date.
  11669. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11670. be removed."
  11671. (interactive)
  11672. (let (org-time-was-given org-end-time-was-given)
  11673. (when what (setq time (or time (org-read-date nil 'to-time))))
  11674. (when (and org-insert-labeled-timestamps-at-point
  11675. (member what '(scheduled deadline)))
  11676. (insert
  11677. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11678. (org-insert-time-stamp time org-time-was-given
  11679. nil nil nil (list org-end-time-was-given))
  11680. (setq what nil))
  11681. (save-excursion
  11682. (save-restriction
  11683. (let (col list elt ts buffer-invisibility-spec)
  11684. (org-back-to-heading t)
  11685. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  11686. (goto-char (match-end 1))
  11687. (setq col (current-column))
  11688. (goto-char (match-end 0))
  11689. (if (eobp) (insert "\n"))
  11690. (forward-char 1)
  11691. (when (and (not org-insert-labeled-timestamps-before-properties-drawer)
  11692. (looking-at "[ \t]*:PROPERTIES:[ \t]*$"))
  11693. (goto-char (match-end 0))
  11694. (if (eobp) (insert "\n"))
  11695. (forward-char 1))
  11696. (if (and (not (looking-at outline-regexp))
  11697. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11698. "[^\r\n]*"))
  11699. (not (equal (match-string 1) org-clock-string)))
  11700. (narrow-to-region (match-beginning 0) (match-end 0))
  11701. (insert-before-markers "\n")
  11702. (backward-char 1)
  11703. (narrow-to-region (point) (point))
  11704. (indent-to-column col))
  11705. ;; Check if we have to remove something.
  11706. (setq list (cons what remove))
  11707. (while list
  11708. (setq elt (pop list))
  11709. (goto-char (point-min))
  11710. (when (or (and (eq elt 'scheduled)
  11711. (re-search-forward org-scheduled-time-regexp nil t))
  11712. (and (eq elt 'deadline)
  11713. (re-search-forward org-deadline-time-regexp nil t))
  11714. (and (eq elt 'closed)
  11715. (re-search-forward org-closed-time-regexp nil t)))
  11716. (replace-match "")
  11717. (if (looking-at "--+<[^>]+>") (replace-match ""))
  11718. (if (looking-at " +") (replace-match ""))))
  11719. (goto-char (point-max))
  11720. (when what
  11721. (insert
  11722. (if (not (equal (char-before) ?\ )) " " "")
  11723. (cond ((eq what 'scheduled) org-scheduled-string)
  11724. ((eq what 'deadline) org-deadline-string)
  11725. ((eq what 'closed) org-closed-string)
  11726. ((eq what 'archived) org-archived-string))
  11727. " ")
  11728. (org-insert-time-stamp
  11729. time
  11730. (or org-time-was-given
  11731. (and (eq what 'closed) org-log-done-with-time))
  11732. (eq what 'closed)
  11733. nil nil (list org-end-time-was-given))
  11734. (end-of-line 1))
  11735. (goto-char (point-min))
  11736. (widen)
  11737. (if (looking-at "[ \t]+\r?\n")
  11738. (replace-match ""))
  11739. ts)))))
  11740. (defvar org-log-note-marker (make-marker))
  11741. (defvar org-log-note-purpose nil)
  11742. (defvar org-log-note-state nil)
  11743. (defvar org-log-note-window-configuration nil)
  11744. (defvar org-log-note-return-to (make-marker))
  11745. (defvar org-log-post-message nil
  11746. "Message to be displayed after a log note has been stored.
  11747. The auto-repeater uses this.")
  11748. (defun org-add-log-maybe (&optional purpose state findpos)
  11749. "Set up the post command hook to take a note."
  11750. (save-excursion
  11751. (when (and (listp org-log-done)
  11752. (memq purpose org-log-done))
  11753. (when findpos
  11754. (org-back-to-heading t)
  11755. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  11756. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11757. "[^\r\n]*\\)?"))
  11758. (goto-char (match-end 0))
  11759. (unless org-log-states-order-reversed
  11760. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11761. (while (looking-at "[ \t]*- State")
  11762. (condition-case nil
  11763. (org-next-item)
  11764. (error (org-end-of-item))))
  11765. (skip-chars-backward " \t\n\r")))
  11766. (move-marker org-log-note-marker (point))
  11767. (setq org-log-note-purpose purpose)
  11768. (setq org-log-note-state state)
  11769. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  11770. (defun org-add-log-note (&optional purpose)
  11771. "Pop up a window for taking a note, and add this note later at point."
  11772. (remove-hook 'post-command-hook 'org-add-log-note)
  11773. (setq org-log-note-window-configuration (current-window-configuration))
  11774. (delete-other-windows)
  11775. (move-marker org-log-note-return-to (point))
  11776. (switch-to-buffer (marker-buffer org-log-note-marker))
  11777. (goto-char org-log-note-marker)
  11778. (switch-to-buffer-other-window "*Org Note*")
  11779. (erase-buffer)
  11780. (let ((org-inhibit-startup t)) (org-mode))
  11781. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  11782. (cond
  11783. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11784. ((eq org-log-note-purpose 'done) "closed todo item")
  11785. ((eq org-log-note-purpose 'state) "state change")
  11786. (t (error "This should not happen")))))
  11787. (org-set-local 'org-finish-function 'org-store-log-note))
  11788. (defun org-store-log-note ()
  11789. "Finish taking a log note, and insert it to where it belongs."
  11790. (let ((txt (buffer-string))
  11791. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11792. lines ind)
  11793. (kill-buffer (current-buffer))
  11794. (if (string-match "^#.*\n[ \t\n]*" txt)
  11795. (setq txt (replace-match "" t t txt)))
  11796. (if (string-match "\\s-+\\'" txt)
  11797. (setq txt (replace-match "" t t txt)))
  11798. (setq lines (org-split-string txt "\n"))
  11799. (when (and note (string-match "\\S-" note))
  11800. (setq note
  11801. (org-replace-escapes
  11802. note
  11803. (list (cons "%u" (user-login-name))
  11804. (cons "%U" user-full-name)
  11805. (cons "%t" (format-time-string
  11806. (org-time-stamp-format 'long 'inactive)
  11807. (current-time)))
  11808. (cons "%s" (if org-log-note-state
  11809. (concat "\"" org-log-note-state "\"")
  11810. "")))))
  11811. (if lines (setq note (concat note " \\\\")))
  11812. (push note lines))
  11813. (when lines
  11814. (save-excursion
  11815. (set-buffer (marker-buffer org-log-note-marker))
  11816. (save-excursion
  11817. (goto-char org-log-note-marker)
  11818. (move-marker org-log-note-marker nil)
  11819. (end-of-line 1)
  11820. (if (not (bolp)) (insert "\n")) (indent-relative nil)
  11821. (insert " - " (pop lines))
  11822. (org-indent-line-function)
  11823. (beginning-of-line 1)
  11824. (looking-at "[ \t]*")
  11825. (setq ind (concat (match-string 0) " "))
  11826. (end-of-line 1)
  11827. (while lines (insert "\n" ind (pop lines)))))))
  11828. (set-window-configuration org-log-note-window-configuration)
  11829. (with-current-buffer (marker-buffer org-log-note-return-to)
  11830. (goto-char org-log-note-return-to))
  11831. (move-marker org-log-note-return-to nil)
  11832. (and org-log-post-message (message org-log-post-message)))
  11833. (defvar org-occur-highlights nil)
  11834. (make-variable-buffer-local 'org-occur-highlights)
  11835. (defun org-occur (regexp &optional keep-previous callback)
  11836. "Make a compact tree which shows all matches of REGEXP.
  11837. The tree will show the lines where the regexp matches, and all higher
  11838. headlines above the match. It will also show the heading after the match,
  11839. to make sure editing the matching entry is easy.
  11840. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11841. call to `org-occur' will be kept, to allow stacking of calls to this
  11842. command.
  11843. If CALLBACK is non-nil, it is a function which is called to confirm
  11844. that the match should indeed be shown."
  11845. (interactive "sRegexp: \nP")
  11846. (or keep-previous (org-remove-occur-highlights nil nil t))
  11847. (let ((cnt 0))
  11848. (save-excursion
  11849. (goto-char (point-min))
  11850. (if (or (not keep-previous) ; do not want to keep
  11851. (not org-occur-highlights)) ; no previous matches
  11852. ;; hide everything
  11853. (org-overview))
  11854. (while (re-search-forward regexp nil t)
  11855. (when (or (not callback)
  11856. (save-match-data (funcall callback)))
  11857. (setq cnt (1+ cnt))
  11858. (when org-highlight-sparse-tree-matches
  11859. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11860. (org-show-context 'occur-tree))))
  11861. (when org-remove-highlights-with-change
  11862. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11863. nil 'local))
  11864. (unless org-sparse-tree-open-archived-trees
  11865. (org-hide-archived-subtrees (point-min) (point-max)))
  11866. (run-hooks 'org-occur-hook)
  11867. (if (interactive-p)
  11868. (message "%d match(es) for regexp %s" cnt regexp))
  11869. cnt))
  11870. (defun org-show-context (&optional key)
  11871. "Make sure point and context and visible.
  11872. How much context is shown depends upon the variables
  11873. `org-show-hierarchy-above', `org-show-following-heading'. and
  11874. `org-show-siblings'."
  11875. (let ((heading-p (org-on-heading-p t))
  11876. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11877. (following-p (org-get-alist-option org-show-following-heading key))
  11878. (siblings-p (org-get-alist-option org-show-siblings key)))
  11879. (catch 'exit
  11880. ;; Show heading or entry text
  11881. (if heading-p
  11882. (org-flag-heading nil) ; only show the heading
  11883. (and (or (org-invisible-p) (org-invisible-p2))
  11884. (org-show-hidden-entry))) ; show entire entry
  11885. (when following-p
  11886. ;; Show next sibling, or heading below text
  11887. (save-excursion
  11888. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11889. (org-flag-heading nil))))
  11890. (when siblings-p (org-show-siblings))
  11891. (when hierarchy-p
  11892. ;; show all higher headings, possibly with siblings
  11893. (save-excursion
  11894. (while (and (condition-case nil
  11895. (progn (org-up-heading-all 1) t)
  11896. (error nil))
  11897. (not (bobp)))
  11898. (org-flag-heading nil)
  11899. (when siblings-p (org-show-siblings))))))))
  11900. (defun org-reveal (&optional siblings)
  11901. "Show current entry, hierarchy above it, and the following headline.
  11902. This can be used to show a consistent set of context around locations
  11903. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11904. not t for the search context.
  11905. With optional argument SIBLINGS, on each level of the hierarchy all
  11906. siblings are shown. This repairs the tree structure to what it would
  11907. look like when opened with hierarchical calls to `org-cycle'."
  11908. (interactive "P")
  11909. (let ((org-show-hierarchy-above t)
  11910. (org-show-following-heading t)
  11911. (org-show-siblings (if siblings t org-show-siblings)))
  11912. (org-show-context nil)))
  11913. (defun org-highlight-new-match (beg end)
  11914. "Highlight from BEG to END and mark the highlight is an occur headline."
  11915. (let ((ov (org-make-overlay beg end)))
  11916. (org-overlay-put ov 'face 'secondary-selection)
  11917. (push ov org-occur-highlights)))
  11918. (defun org-remove-occur-highlights (&optional beg end noremove)
  11919. "Remove the occur highlights from the buffer.
  11920. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11921. from the `before-change-functions' in the current buffer."
  11922. (interactive)
  11923. (unless org-inhibit-highlight-removal
  11924. (mapc 'org-delete-overlay org-occur-highlights)
  11925. (setq org-occur-highlights nil)
  11926. (unless noremove
  11927. (remove-hook 'before-change-functions
  11928. 'org-remove-occur-highlights 'local))))
  11929. ;;;; Priorities
  11930. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11931. "Regular expression matching the priority indicator.")
  11932. (defvar org-remove-priority-next-time nil)
  11933. (defun org-priority-up ()
  11934. "Increase the priority of the current item."
  11935. (interactive)
  11936. (org-priority 'up))
  11937. (defun org-priority-down ()
  11938. "Decrease the priority of the current item."
  11939. (interactive)
  11940. (org-priority 'down))
  11941. (defun org-priority (&optional action)
  11942. "Change the priority of an item by ARG.
  11943. ACTION can be `set', `up', `down', or a character."
  11944. (interactive)
  11945. (setq action (or action 'set))
  11946. (let (current new news have remove)
  11947. (save-excursion
  11948. (org-back-to-heading)
  11949. (if (looking-at org-priority-regexp)
  11950. (setq current (string-to-char (match-string 2))
  11951. have t)
  11952. (setq current org-default-priority))
  11953. (cond
  11954. ((or (eq action 'set) (integerp action))
  11955. (if (integerp action)
  11956. (setq new action)
  11957. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  11958. (setq new (read-char-exclusive)))
  11959. (cond ((equal new ?\ ) (setq remove t))
  11960. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11961. (error "Priority must be between `%c' and `%c'"
  11962. org-highest-priority org-lowest-priority))))
  11963. ((eq action 'up)
  11964. (setq new (1- current)))
  11965. ((eq action 'down)
  11966. (setq new (1+ current)))
  11967. (t (error "Invalid action")))
  11968. (setq new (min (max org-highest-priority (upcase new)) org-lowest-priority))
  11969. (setq news (format "%c" new))
  11970. (if have
  11971. (if remove
  11972. (replace-match "" t t nil 1)
  11973. (replace-match news t t nil 2))
  11974. (if remove
  11975. (error "No priority cookie found in line")
  11976. (looking-at org-todo-line-regexp)
  11977. (if (match-end 2)
  11978. (progn
  11979. (goto-char (match-end 2))
  11980. (insert " [#" news "]"))
  11981. (goto-char (match-beginning 3))
  11982. (insert "[#" news "] ")))))
  11983. (org-preserve-lc (org-set-tags nil 'align))
  11984. (if remove
  11985. (message "Priority removed")
  11986. (message "Priority of current item set to %s" news))))
  11987. (defun org-get-priority (s)
  11988. "Find priority cookie and return priority."
  11989. (save-match-data
  11990. (if (not (string-match org-priority-regexp s))
  11991. (* 1000 (- org-lowest-priority org-default-priority))
  11992. (* 1000 (- org-lowest-priority
  11993. (string-to-char (match-string 2 s)))))))
  11994. ;;;; Tags
  11995. (defun org-scan-tags (action matcher &optional todo-only)
  11996. "Scan headline tags with inheritance and produce output ACTION.
  11997. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  11998. evaluated, testing if a given set of tags qualifies a headline for
  11999. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  12000. are included in the output."
  12001. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  12002. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12003. (org-re
  12004. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  12005. (props (list 'face nil
  12006. 'done-face 'org-done
  12007. 'undone-face nil
  12008. 'mouse-face 'highlight
  12009. 'org-not-done-regexp org-not-done-regexp
  12010. 'org-todo-regexp org-todo-regexp
  12011. 'keymap org-agenda-keymap
  12012. 'help-echo
  12013. (format "mouse-2 or RET jump to org file %s"
  12014. (abbreviate-file-name buffer-file-name))))
  12015. (case-fold-search nil)
  12016. lspos
  12017. tags tags-list tags-alist (llast 0) rtn level category i txt
  12018. todo marker entry priority)
  12019. (save-excursion
  12020. (goto-char (point-min))
  12021. (when (eq action 'sparse-tree) (org-overview))
  12022. (while (re-search-forward re nil t)
  12023. (catch :skip
  12024. (setq todo (if (match-end 1) (match-string 2))
  12025. tags (if (match-end 4) (match-string 4)))
  12026. (goto-char (setq lspos (1+ (match-beginning 0))))
  12027. (setq level (org-reduced-level (funcall outline-level))
  12028. category (org-get-category))
  12029. (setq i llast llast level)
  12030. ;; remove tag lists from same and sublevels
  12031. (while (>= i level)
  12032. (when (setq entry (assoc i tags-alist))
  12033. (setq tags-alist (delete entry tags-alist)))
  12034. (setq i (1- i)))
  12035. ;; add the nex tags
  12036. (when tags
  12037. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  12038. tags-alist
  12039. (cons (cons level tags) tags-alist)))
  12040. ;; compile tags for current headline
  12041. (setq tags-list
  12042. (if org-use-tag-inheritance
  12043. (apply 'append (mapcar 'cdr tags-alist))
  12044. tags))
  12045. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  12046. (eval matcher)
  12047. (or (not org-agenda-skip-archived-trees)
  12048. (not (member org-archive-tag tags-list))))
  12049. (and (eq action 'agenda) (org-agenda-skip))
  12050. ;; list this headline
  12051. (if (eq action 'sparse-tree)
  12052. (progn
  12053. (org-show-context 'tags-tree))
  12054. (setq txt (org-format-agenda-item
  12055. ""
  12056. (concat
  12057. (if org-tags-match-list-sublevels
  12058. (make-string (1- level) ?.) "")
  12059. (org-get-heading))
  12060. category tags-list)
  12061. priority (org-get-priority txt))
  12062. (goto-char lspos)
  12063. (setq marker (org-agenda-new-marker))
  12064. (org-add-props txt props
  12065. 'org-marker marker 'org-hd-marker marker 'org-category category
  12066. 'priority priority 'type "tagsmatch")
  12067. (push txt rtn))
  12068. ;; if we are to skip sublevels, jump to end of subtree
  12069. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  12070. (when (and (eq action 'sparse-tree)
  12071. (not org-sparse-tree-open-archived-trees))
  12072. (org-hide-archived-subtrees (point-min) (point-max)))
  12073. (nreverse rtn)))
  12074. (defvar todo-only) ;; dynamically scoped
  12075. (defun org-tags-sparse-tree (&optional todo-only match)
  12076. "Create a sparse tree according to tags string MATCH.
  12077. MATCH can contain positive and negative selection of tags, like
  12078. \"+WORK+URGENT-WITHBOSS\".
  12079. If optional argument TODO_ONLY is non-nil, only select lines that are
  12080. also TODO lines."
  12081. (interactive "P")
  12082. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12083. (defvar org-cached-props nil)
  12084. (defun org-cached-entry-get (pom property)
  12085. (cdr (assoc property (or org-cached-props
  12086. (setq org-cached-props
  12087. (org-entry-properties pom))))))
  12088. (defun org-global-tags-completion-table (&optional files)
  12089. "Return the list of all tags in all agenda buffer/files."
  12090. (save-excursion
  12091. (org-uniquify
  12092. (apply 'append
  12093. (mapcar
  12094. (lambda (file)
  12095. (set-buffer (find-file-noselect file))
  12096. (org-get-buffer-tags))
  12097. (if (and files (car files))
  12098. files
  12099. (org-agenda-files)))))))
  12100. (defun org-make-tags-matcher (match)
  12101. "Create the TAGS//TODO matcher form for the selection string MATCH."
  12102. ;; todo-only is scoped dynamically into this function, and the function
  12103. ;; may change it it the matcher asksk for it.
  12104. (unless match
  12105. ;; Get a new match request, with completion
  12106. (let ((org-last-tags-completion-table
  12107. (org-global-tags-completion-table)))
  12108. (setq match (completing-read
  12109. "Match: " 'org-tags-completion-function nil nil nil
  12110. 'org-tags-history))))
  12111. ;; Parse the string and create a lisp form
  12112. (let ((match0 match)
  12113. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  12114. minus tag mm
  12115. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12116. orterms term orlist re-p level-p prop-p pn pv)
  12117. (if (string-match "/+" match)
  12118. ;; match contains also a todo-matching request
  12119. (progn
  12120. (setq tagsmatch (substring match 0 (match-beginning 0))
  12121. todomatch (substring match (match-end 0)))
  12122. (if (string-match "^!" todomatch)
  12123. (setq todo-only t todomatch (substring todomatch 1)))
  12124. (if (string-match "^\\s-*$" todomatch)
  12125. (setq todomatch nil)))
  12126. ;; only matching tags
  12127. (setq tagsmatch match todomatch nil))
  12128. ;; Make the tags matcher
  12129. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12130. (setq tagsmatcher t)
  12131. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12132. (while (setq term (pop orterms))
  12133. (while (and (equal (substring term -1) "\\") orterms)
  12134. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12135. (while (string-match re term)
  12136. (setq minus (and (match-end 1)
  12137. (equal (match-string 1 term) "-"))
  12138. tag (match-string 2 term)
  12139. re-p (equal (string-to-char tag) ?{)
  12140. level-p (match-end 3)
  12141. prop-p (match-end 4)
  12142. mm (cond
  12143. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12144. (level-p `(= level ,(string-to-number
  12145. (match-string 3 term))))
  12146. (prop-p
  12147. (setq pn (match-string 4 term)
  12148. pv (match-string 5 term)
  12149. re-p (equal (string-to-char pv) ?{)
  12150. pv (substring pv 1 -1))
  12151. (if re-p
  12152. `(string-match ,pv (org-cached-entry-get nil ,pn))
  12153. `(equal ,pv (org-cached-entry-get nil ,pn))))
  12154. (t `(member ,(downcase tag) tags-list)))
  12155. mm (if minus (list 'not mm) mm)
  12156. term (substring term (match-end 0)))
  12157. (push mm tagsmatcher))
  12158. (push (if (> (length tagsmatcher) 1)
  12159. (cons 'and tagsmatcher)
  12160. (car tagsmatcher))
  12161. orlist)
  12162. (setq tagsmatcher nil))
  12163. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12164. (setq tagsmatcher
  12165. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12166. ;; Make the todo matcher
  12167. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12168. (setq todomatcher t)
  12169. (setq orterms (org-split-string todomatch "|") orlist nil)
  12170. (while (setq term (pop orterms))
  12171. (while (string-match re term)
  12172. (setq minus (and (match-end 1)
  12173. (equal (match-string 1 term) "-"))
  12174. kwd (match-string 2 term)
  12175. re-p (equal (string-to-char kwd) ?{)
  12176. term (substring term (match-end 0))
  12177. mm (if re-p
  12178. `(string-match ,(substring kwd 1 -1) todo)
  12179. (list 'equal 'todo kwd))
  12180. mm (if minus (list 'not mm) mm))
  12181. (push mm todomatcher))
  12182. (push (if (> (length todomatcher) 1)
  12183. (cons 'and todomatcher)
  12184. (car todomatcher))
  12185. orlist)
  12186. (setq todomatcher nil))
  12187. (setq todomatcher (if (> (length orlist) 1)
  12188. (cons 'or orlist) (car orlist))))
  12189. ;; Return the string and lisp forms of the matcher
  12190. (setq matcher (if todomatcher
  12191. (list 'and tagsmatcher todomatcher)
  12192. tagsmatcher))
  12193. (cons match0 matcher)))
  12194. (defun org-match-any-p (re list)
  12195. "Does re match any element of list?"
  12196. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12197. (delq nil list))
  12198. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  12199. (defvar org-tags-overlay (org-make-overlay 1 1))
  12200. (org-detach-overlay org-tags-overlay)
  12201. (defun org-align-tags-here (to-col)
  12202. ;; Assumes that this is a headline
  12203. (let ((pos (point)) (col (current-column)) tags)
  12204. (beginning-of-line 1)
  12205. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12206. (< pos (match-beginning 2)))
  12207. (progn
  12208. (setq tags (match-string 2))
  12209. (goto-char (match-beginning 1))
  12210. (insert " ")
  12211. (delete-region (point) (1+ (match-end 0)))
  12212. (backward-char 1)
  12213. (move-to-column
  12214. (max (1+ (current-column))
  12215. (1+ col)
  12216. (if (> to-col 0)
  12217. to-col
  12218. (- (abs to-col) (length tags))))
  12219. t)
  12220. (insert tags)
  12221. (move-to-column (min (current-column) col) t))
  12222. (goto-char pos))))
  12223. (defun org-set-tags (&optional arg just-align)
  12224. "Set the tags for the current headline.
  12225. With prefix ARG, realign all tags in headings in the current buffer."
  12226. (interactive "P")
  12227. (let* ((re (concat "^" outline-regexp))
  12228. (current (org-get-tags))
  12229. table current-tags inherited-tags ; computed below when needed
  12230. tags p0 c0 c1 rpl)
  12231. (if arg
  12232. (save-excursion
  12233. (goto-char (point-min))
  12234. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12235. (while (re-search-forward re nil t)
  12236. (org-set-tags nil t)
  12237. (end-of-line 1)))
  12238. (message "All tags realigned to column %d" org-tags-column))
  12239. (if just-align
  12240. (setq tags current)
  12241. ;; Get a new set of tags from the user
  12242. (save-excursion
  12243. (setq table (or org-tag-alist (org-get-buffer-tags))
  12244. org-last-tags-completion-table table
  12245. current-tags (org-split-string current ":")
  12246. inherited-tags (nreverse
  12247. (nthcdr (length current-tags)
  12248. (nreverse (org-get-tags-at))))
  12249. tags
  12250. (if (or (eq t org-use-fast-tag-selection)
  12251. (and org-use-fast-tag-selection
  12252. (delq nil (mapcar 'cdr table))))
  12253. (org-fast-tag-selection current-tags inherited-tags table)
  12254. (let ((org-add-colon-after-tag-completion t))
  12255. (org-trim
  12256. (completing-read "Tags: " 'org-tags-completion-function
  12257. nil nil current 'org-tags-history))))))
  12258. (while (string-match "[-+&]+" tags)
  12259. ;; No boolean logic, just a list
  12260. (setq tags (replace-match ":" t t tags))))
  12261. (if (string-match "\\`[\t ]*\\'" tags)
  12262. (setq tags "")
  12263. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12264. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12265. ;; Insert new tags at the correct column
  12266. (beginning-of-line 1)
  12267. (cond
  12268. ((and (equal current "") (equal tags "")))
  12269. ((re-search-forward
  12270. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12271. (point-at-eol) t)
  12272. (if (equal tags "")
  12273. (setq rpl "")
  12274. (goto-char (match-beginning 0))
  12275. (setq c0 (current-column) p0 (point)
  12276. c1 (max (1+ c0) (if (> org-tags-column 0)
  12277. org-tags-column
  12278. (- (- org-tags-column) (length tags))))
  12279. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12280. (replace-match rpl t t)
  12281. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  12282. tags)
  12283. (t (error "Tags alignment failed"))))))
  12284. (defun org-tags-completion-function (string predicate &optional flag)
  12285. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12286. (confirm (lambda (x) (stringp (car x)))))
  12287. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  12288. (setq s1 (match-string 1 string)
  12289. s2 (match-string 2 string))
  12290. (setq s1 "" s2 string))
  12291. (cond
  12292. ((eq flag nil)
  12293. ;; try completion
  12294. (setq rtn (try-completion s2 ctable confirm))
  12295. (if (stringp rtn)
  12296. (setq rtn
  12297. (concat s1 s2 (substring rtn (length s2))
  12298. (if (and org-add-colon-after-tag-completion
  12299. (assoc rtn ctable))
  12300. ":" ""))))
  12301. rtn)
  12302. ((eq flag t)
  12303. ;; all-completions
  12304. (all-completions s2 ctable confirm)
  12305. )
  12306. ((eq flag 'lambda)
  12307. ;; exact match?
  12308. (assoc s2 ctable)))
  12309. ))
  12310. (defun org-fast-tag-insert (kwd tags face &optional end)
  12311. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  12312. (insert (format "%-12s" (concat kwd ":"))
  12313. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12314. (or end "")))
  12315. (defun org-fast-tag-show-exit (flag)
  12316. (save-excursion
  12317. (goto-line 3)
  12318. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12319. (replace-match ""))
  12320. (when flag
  12321. (end-of-line 1)
  12322. (move-to-column (- (window-width) 19) t)
  12323. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12324. (defun org-set-current-tags-overlay (current prefix)
  12325. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12326. (if (featurep 'xemacs)
  12327. (org-overlay-display org-tags-overlay (concat prefix s)
  12328. 'secondary-selection)
  12329. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12330. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12331. (defun org-fast-tag-selection (current inherited table)
  12332. "Fast tag selection with single keys.
  12333. CURRENT is the current list of tags in the headline, INHERITED is the
  12334. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12335. possibly with grouping information.
  12336. If the keys are nil, a-z are automatically assigned.
  12337. Returns the new tags string, or nil to not change the current settings."
  12338. (let* ((maxlen (apply 'max (mapcar
  12339. (lambda (x)
  12340. (if (stringp (car x)) (string-width (car x)) 0))
  12341. table)))
  12342. (buf (current-buffer))
  12343. (expert (eq org-fast-tag-selection-single-key 'expert))
  12344. (buffer-tags nil)
  12345. (fwidth (+ maxlen 3 1 3))
  12346. (ncol (/ (- (window-width) 4) fwidth))
  12347. (i-face 'org-done)
  12348. (c-face 'org-todo)
  12349. tg cnt e c char c1 c2 ntable tbl rtn
  12350. ov-start ov-end ov-prefix
  12351. (exit-after-next org-fast-tag-selection-single-key)
  12352. groups ingroup)
  12353. (save-excursion
  12354. (beginning-of-line 1)
  12355. (if (looking-at
  12356. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12357. (setq ov-start (match-beginning 1)
  12358. ov-end (match-end 1)
  12359. ov-prefix "")
  12360. (setq ov-start (1- (point-at-eol))
  12361. ov-end (1+ ov-start))
  12362. (skip-chars-forward "^\n\r")
  12363. (setq ov-prefix
  12364. (concat
  12365. (buffer-substring (1- (point)) (point))
  12366. (if (> (current-column) org-tags-column)
  12367. " "
  12368. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12369. (org-move-overlay org-tags-overlay ov-start ov-end)
  12370. (save-window-excursion
  12371. (if expert
  12372. (set-buffer (get-buffer-create " *Org tags*"))
  12373. (delete-other-windows)
  12374. (split-window-vertically)
  12375. (switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12376. (erase-buffer)
  12377. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12378. (org-fast-tag-insert "Current" current c-face "\n\n")
  12379. (org-fast-tag-show-exit exit-after-next)
  12380. (org-set-current-tags-overlay current ov-prefix)
  12381. (setq tbl table char ?a cnt 0)
  12382. (while (setq e (pop tbl))
  12383. (cond
  12384. ((equal e '(:startgroup))
  12385. (push '() groups) (setq ingroup t)
  12386. (when (not (= cnt 0))
  12387. (setq cnt 0)
  12388. (insert "\n"))
  12389. (insert "{ "))
  12390. ((equal e '(:endgroup))
  12391. (setq ingroup nil cnt 0)
  12392. (insert "}\n"))
  12393. (t
  12394. (setq tg (car e) c2 nil)
  12395. (if (cdr e)
  12396. (setq c (cdr e))
  12397. ;; automatically assign a character.
  12398. (setq c1 (string-to-char
  12399. (downcase (substring
  12400. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12401. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12402. (while (or (rassoc char ntable) (rassoc char table))
  12403. (setq char (1+ char)))
  12404. (setq c2 c1))
  12405. (setq c (or c2 char)))
  12406. (if ingroup (push tg (car groups)))
  12407. (setq tg (org-add-props tg nil 'face
  12408. (cond
  12409. ((member tg current) c-face)
  12410. ((member tg inherited) i-face)
  12411. (t nil))))
  12412. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12413. (insert "[" c "] " tg (make-string
  12414. (- fwidth 4 (length tg)) ?\ ))
  12415. (push (cons tg c) ntable)
  12416. (when (= (setq cnt (1+ cnt)) ncol)
  12417. (insert "\n")
  12418. (if ingroup (insert " "))
  12419. (setq cnt 0)))))
  12420. (setq ntable (nreverse ntable))
  12421. (insert "\n")
  12422. (goto-char (point-min))
  12423. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12424. (fit-window-to-buffer))
  12425. (setq rtn
  12426. (catch 'exit
  12427. (while t
  12428. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  12429. (if groups " [!] no groups" " [!]groups")
  12430. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12431. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12432. (cond
  12433. ((= c ?\r) (throw 'exit t))
  12434. ((= c ?!)
  12435. (setq groups (not groups))
  12436. (goto-char (point-min))
  12437. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12438. ((= c ?\C-c)
  12439. (if (not expert)
  12440. (org-fast-tag-show-exit
  12441. (setq exit-after-next (not exit-after-next)))
  12442. (setq expert nil)
  12443. (delete-other-windows)
  12444. (split-window-vertically)
  12445. (switch-to-buffer-other-window " *Org tags*")
  12446. (and (fboundp 'fit-window-to-buffer)
  12447. (fit-window-to-buffer))))
  12448. ((or (= c ?\C-g)
  12449. (and (= c ?q) (not (rassoc c ntable))))
  12450. (org-detach-overlay org-tags-overlay)
  12451. (setq quit-flag t))
  12452. ((= c ?\ )
  12453. (setq current nil)
  12454. (if exit-after-next (setq exit-after-next 'now)))
  12455. ((= c ?\t)
  12456. (condition-case nil
  12457. (setq tg (completing-read
  12458. "Tag: "
  12459. (or buffer-tags
  12460. (with-current-buffer buf
  12461. (org-get-buffer-tags)))))
  12462. (quit (setq tg "")))
  12463. (when (string-match "\\S-" tg)
  12464. (add-to-list 'buffer-tags (list tg))
  12465. (if (member tg current)
  12466. (setq current (delete tg current))
  12467. (push tg current)))
  12468. (if exit-after-next (setq exit-after-next 'now)))
  12469. ((setq e (rassoc c ntable) tg (car e))
  12470. (if (member tg current)
  12471. (setq current (delete tg current))
  12472. (loop for g in groups do
  12473. (if (member tg g)
  12474. (mapcar (lambda (x)
  12475. (setq current (delete x current)))
  12476. g)))
  12477. (push tg current))
  12478. (if exit-after-next (setq exit-after-next 'now))))
  12479. ;; Create a sorted list
  12480. (setq current
  12481. (sort current
  12482. (lambda (a b)
  12483. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12484. (if (eq exit-after-next 'now) (throw 'exit t))
  12485. (goto-char (point-min))
  12486. (beginning-of-line 2)
  12487. (delete-region (point) (point-at-eol))
  12488. (org-fast-tag-insert "Current" current c-face)
  12489. (org-set-current-tags-overlay current ov-prefix)
  12490. (while (re-search-forward
  12491. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  12492. (setq tg (match-string 1))
  12493. (add-text-properties (match-beginning 1) (match-end 1)
  12494. (list 'face
  12495. (cond
  12496. ((member tg current) c-face)
  12497. ((member tg inherited) i-face)
  12498. (t nil)))))
  12499. (goto-char (point-min)))))
  12500. (org-detach-overlay org-tags-overlay)
  12501. (if rtn
  12502. (mapconcat 'identity current ":")
  12503. nil))))
  12504. (defun org-get-tags ()
  12505. "Get the TAGS string in the current headline."
  12506. (unless (org-on-heading-p t)
  12507. (error "Not on a heading"))
  12508. (save-excursion
  12509. (beginning-of-line 1)
  12510. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12511. (org-match-string-no-properties 1)
  12512. "")))
  12513. (defun org-get-buffer-tags ()
  12514. "Get a table of all tags used in the buffer, for completion."
  12515. (let (tags)
  12516. (save-excursion
  12517. (goto-char (point-min))
  12518. (while (re-search-forward
  12519. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  12520. (when (equal (char-after (point-at-bol 0)) ?*)
  12521. (mapc (lambda (x) (add-to-list 'tags x))
  12522. (org-split-string (org-match-string-no-properties 1) ":")))))
  12523. (mapcar 'list tags)))
  12524. ;;;; Properties
  12525. ;;; Setting and retrieving properties
  12526. (defconst org-special-properties
  12527. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED"
  12528. "CLOCK" "PRIORITY")
  12529. "The special properties valid in Org-mode.
  12530. These are properties that are not defined in the property drawer,
  12531. but in some other way.")
  12532. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12533. "Regular expression matching the first line of a property drawer.")
  12534. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12535. "Regular expression matching the first line of a property drawer.")
  12536. (defun org-property-action ()
  12537. "Do an action on properties."
  12538. (interactive)
  12539. (let (c prop)
  12540. (org-at-property-p)
  12541. (setq prop (match-string 2))
  12542. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12543. (setq c (read-char-exclusive))
  12544. (cond
  12545. ((equal c ?s)
  12546. (call-interactively 'org-set-property))
  12547. ((equal c ?d)
  12548. (call-interactively 'org-delete-property))
  12549. ((equal c ?D)
  12550. (call-interactively 'org-delete-property-globally))
  12551. ((equal c ?c)
  12552. (call-interactively 'org-compute-property-at-point))
  12553. (t (error "No such property action %c" c)))))
  12554. (defun org-at-property-p ()
  12555. "Is the cursor in a property line?"
  12556. ;; FIXME: Does not check if we are actually in the drawer.
  12557. ;; FIXME: also returns true on any drawers.....
  12558. ;; This is used by C-c C-c for property action.
  12559. (save-excursion
  12560. (beginning-of-line 1)
  12561. (looking-at "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(.*\\)")))
  12562. (defmacro org-with-point-at (pom &rest body)
  12563. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  12564. (declare (indent 1) (debug t))
  12565. `(save-excursion
  12566. (if (markerp pom) (set-buffer (marker-buffer pom)))
  12567. (save-excursion
  12568. (goto-char (or pom (point)))
  12569. ,@body)))
  12570. (defun org-get-property-block (&optional beg end force)
  12571. "Return the (beg . end) range of the body of the property drawer.
  12572. BEG and END can be beginning and end of subtree, if not given
  12573. they will be found.
  12574. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12575. (catch 'exit
  12576. (save-excursion
  12577. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12578. (end (or end (progn (outline-next-heading) (point)))))
  12579. (goto-char beg)
  12580. (if (re-search-forward org-property-start-re end t)
  12581. (setq beg (1+ (match-end 0)))
  12582. (if force
  12583. (save-excursion
  12584. (org-insert-property-drawer)
  12585. (setq end (progn (outline-next-heading) (point))))
  12586. (throw 'exit nil))
  12587. (goto-char beg)
  12588. (if (re-search-forward org-property-start-re end t)
  12589. (setq beg (1+ (match-end 0)))))
  12590. (if (re-search-forward org-property-end-re end t)
  12591. (setq end (match-beginning 0))
  12592. (or force (throw 'exit nil))
  12593. (goto-char beg)
  12594. (setq end beg)
  12595. (org-indent-line-function)
  12596. (insert ":END:\n"))
  12597. (cons beg end)))))
  12598. (defun org-entry-properties (&optional pom which)
  12599. "Get all properties of the entry at point-or-marker POM.
  12600. This includes the TODO keyword, the tags, time strings for deadline,
  12601. scheduled, and clocking, and any additional properties defined in the
  12602. entry. The return value is an alist, keys may occur multiple times
  12603. if the property key was used several times.
  12604. POM may also be nil, in which case the current entry is used.
  12605. If WHICH is nil or `all', get all properties. If WHICH is
  12606. `special' or `standard', only get that subclass."
  12607. (setq which (or which 'all))
  12608. (org-with-point-at pom
  12609. (let ((clockstr (substring org-clock-string 0 -1))
  12610. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  12611. beg end range props sum-props key value)
  12612. (save-excursion
  12613. (when (condition-case nil (org-back-to-heading t) (error nil))
  12614. (setq beg (point))
  12615. (setq sum-props (get-text-property (point) 'org-summaries))
  12616. (outline-next-heading)
  12617. (setq end (point))
  12618. (when (memq which '(all special))
  12619. ;; Get the special properties, like TODO and tags
  12620. (goto-char beg)
  12621. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  12622. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12623. (when (looking-at org-priority-regexp)
  12624. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12625. (when (and (setq value (org-get-tags)) (string-match "\\S-" value))
  12626. (push (cons "TAGS" value) props))
  12627. (when (setq value (org-get-tags-at))
  12628. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  12629. props))
  12630. (while (re-search-forward org-keyword-time-regexp end t)
  12631. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  12632. (unless (member key excluded) (push key excluded))
  12633. (push (cons key
  12634. (if (equal key clockstr)
  12635. (org-no-properties
  12636. (org-trim
  12637. (buffer-substring
  12638. (match-beginning 2) (point-at-eol))))
  12639. (org-match-string-no-properties 2)))
  12640. props)))
  12641. (when (memq which '(all standard))
  12642. ;; Get the standard properties, like :PORP: ...
  12643. (setq range (org-get-property-block beg end))
  12644. (when range
  12645. (goto-char (car range))
  12646. (while (re-search-forward
  12647. "^[ \t]*:\\([a-zA-Z][a-zA-Z_0-9]*\\):[ \t]*\\(\\S-.*\\)?"
  12648. (cdr range) t)
  12649. (setq key (org-match-string-no-properties 1)
  12650. value (org-trim (or (org-match-string-no-properties 2) "")))
  12651. (unless (member key excluded)
  12652. (push (cons key (or value "")) props)))))
  12653. (append sum-props (nreverse props)))))))
  12654. (defun org-entry-get (pom property &optional inherit)
  12655. "Get value of PROPERTY for entry at point-or-marker POM.
  12656. If INHERIT is non-nil and the entry does not have the property,
  12657. then also check higher levels of the hierarchy.
  12658. If the property is present but empty, the return value is the empty string.
  12659. If the property is not present at all, nil is returned."
  12660. (org-with-point-at pom
  12661. (if inherit
  12662. (org-entry-get-with-inheritance property)
  12663. (if (member property org-special-properties)
  12664. ;; We need a special property. Use brute force, get all properties.
  12665. (cdr (assoc property (org-entry-properties nil 'special)))
  12666. (let ((range (org-get-property-block)))
  12667. (if (and range
  12668. (goto-char (car range))
  12669. (re-search-forward
  12670. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  12671. (cdr range) t))
  12672. ;; Found the property, return it.
  12673. (if (match-end 1)
  12674. (org-match-string-no-properties 1)
  12675. "")))))))
  12676. (defun org-entry-delete (pom property)
  12677. "Delete the property PROPERTY from entry at point-or-marker POM."
  12678. (org-with-point-at pom
  12679. (if (member property org-special-properties)
  12680. nil ; cannot delete these properties.
  12681. (let ((range (org-get-property-block)))
  12682. (if (and range
  12683. (goto-char (car range))
  12684. (re-search-forward
  12685. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  12686. (cdr range) t))
  12687. (progn
  12688. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12689. t)
  12690. nil)))))
  12691. (defvar org-entry-property-inherited-from (make-marker))
  12692. (defun org-entry-get-with-inheritance (property)
  12693. "Get entry property, and search higher levels if not present."
  12694. (let (tmp)
  12695. (save-excursion
  12696. (catch 'ex
  12697. (while t
  12698. (when (setq tmp (org-entry-get nil property))
  12699. (org-back-to-heading t)
  12700. (move-marker org-entry-property-inherited-from (point))
  12701. (throw 'ex tmp))
  12702. (condition-case nil
  12703. (org-up-heading-all 1)
  12704. (error (throw 'ex nil))))))
  12705. (or tmp (cdr (assoc property org-local-properties))
  12706. (cdr (assoc property org-global-properties)))))
  12707. (defun org-entry-put (pom property value)
  12708. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12709. (org-with-point-at pom
  12710. (org-back-to-heading t)
  12711. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12712. range)
  12713. (cond
  12714. ((equal property "TODO")
  12715. (when (and (stringp value) (string-match "\\S-" value)
  12716. (not (member value org-todo-keywords-1)))
  12717. (error "\"%s\" is not a valid TODO state" value))
  12718. (if (or (not value)
  12719. (not (string-match "\\S-" value)))
  12720. (setq value 'none))
  12721. (org-todo value)
  12722. (org-set-tags nil 'align))
  12723. ((equal property "PRIORITY")
  12724. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12725. (string-to-char value) ?\ ))
  12726. (org-set-tags nil 'align))
  12727. ((equal property "SCHEDULED")
  12728. (if (re-search-forward org-scheduled-time-regexp end t)
  12729. (cond
  12730. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12731. ((eq value 'later) (org-timestamp-change 1 'day))
  12732. (t (call-interactively 'org-schedule)))
  12733. (call-interactively 'org-schedule)))
  12734. ((equal property "DEADLINE")
  12735. (if (re-search-forward org-deadline-time-regexp end t)
  12736. (cond
  12737. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12738. ((eq value 'later) (org-timestamp-change 1 'day))
  12739. (t (call-interactively 'org-deadline)))
  12740. (call-interactively 'org-deadline)))
  12741. ((member property org-special-properties)
  12742. (error "The %s property can not yet be set with `org-entry-put'"
  12743. property))
  12744. (t ; a non-special property
  12745. (setq range (org-get-property-block beg end 'force))
  12746. (goto-char (car range))
  12747. (if (re-search-forward
  12748. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12749. (progn
  12750. (delete-region (match-beginning 1) (match-end 1))
  12751. (goto-char (match-beginning 1)))
  12752. (goto-char (cdr range))
  12753. (insert "\n")
  12754. (backward-char 1)
  12755. (org-indent-line-function)
  12756. (insert ":" property ":"))
  12757. (and value (insert " " value))
  12758. (org-indent-line-function))))))
  12759. (defun org-buffer-property-keys (&optional include-specials)
  12760. "Get all property keys in the current buffer."
  12761. (let (rtn range)
  12762. (save-excursion
  12763. (save-restriction
  12764. (widen)
  12765. (goto-char (point-min))
  12766. (while (re-search-forward org-property-start-re nil t)
  12767. (setq range (org-get-property-block))
  12768. (goto-char (car range))
  12769. (while (re-search-forward "^[ \t]*:\\([a-zA-Z0-9]+\\):" (cdr range) t)
  12770. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12771. (outline-next-heading))))
  12772. (when include-specials
  12773. (setq rtn (append org-special-properties rtn)))
  12774. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12775. (defun org-insert-property-drawer ()
  12776. "Insert a property drawer into the current entry."
  12777. (interactive)
  12778. (org-back-to-heading t)
  12779. (let ((beg (point))
  12780. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12781. end hiddenp)
  12782. (outline-next-heading)
  12783. (setq end (point))
  12784. (goto-char beg)
  12785. (while (re-search-forward re end t))
  12786. (setq hiddenp (org-invisible-p))
  12787. (end-of-line 1)
  12788. (insert "\n:PROPERTIES:\n:END:")
  12789. (beginning-of-line 0)
  12790. (org-indent-line-function)
  12791. (beginning-of-line 2)
  12792. (org-indent-line-function)
  12793. (beginning-of-line 0)
  12794. (if hiddenp
  12795. (save-excursion
  12796. (org-back-to-heading t)
  12797. (hide-entry))
  12798. (org-flag-drawer t))))
  12799. (defun org-set-property (property value)
  12800. "In the current entry, set PROPERTY to VALUE."
  12801. (interactive
  12802. (let* ((prop (completing-read "Property: "
  12803. (mapcar 'list (org-buffer-property-keys))))
  12804. (cur (org-entry-get nil prop))
  12805. (allowed (org-property-get-allowed-values nil prop 'table))
  12806. (val (if allowed
  12807. (completing-read "Value: " allowed nil 'req-match)
  12808. (read-string
  12809. (concat "Value" (if (and cur (string-match "\\S-" cur))
  12810. (concat "[" cur "]") "")
  12811. ": ")
  12812. "" cur))))
  12813. (list prop (if (equal val "") cur val))))
  12814. (unless (equal (org-entry-get nil property) value)
  12815. (org-entry-put nil property value)))
  12816. (defun org-delete-property (property)
  12817. "In the current entry, delete PROPERTY."
  12818. (interactive
  12819. (let* ((prop (completing-read
  12820. "Property: " (org-entry-properties nil 'standard))))
  12821. (list prop)))
  12822. (message (concat "Property " property
  12823. (if (org-entry-delete nil property)
  12824. " deleted"
  12825. " was not present in the entry"))))
  12826. (defun org-delete-property-globally (property)
  12827. "Remove PROPERTY globally, from all entries."
  12828. (interactive
  12829. (let* ((prop (completing-read
  12830. "Globally remove property: "
  12831. (mapcar 'list (org-buffer-property-keys)))))
  12832. (list prop)))
  12833. (save-excursion
  12834. (save-restriction
  12835. (widen)
  12836. (goto-char (point-min))
  12837. (let ((cnt 0))
  12838. (while (re-search-forward
  12839. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12840. nil t)
  12841. (setq cnt (1+ cnt))
  12842. (replace-match ""))
  12843. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12844. (defvar org-columns-current-fmt-compiled) ; defined below
  12845. (defun org-compute-property-at-point ()
  12846. "FIXME:"
  12847. (interactive)
  12848. (unless (org-at-property-p)
  12849. (error "Not at a property"))
  12850. (let ((prop (org-match-string-no-properties 2)))
  12851. (org-columns-get-format-and-top-level)
  12852. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12853. (error "No operator defined for property %s" prop))
  12854. (org-columns-compute prop)))
  12855. (defun org-property-get-allowed-values (pom property &optional table)
  12856. "Get allowed values for the property PROPERTY.
  12857. When TABLE is non-nil, return an alist that can directly be used for
  12858. completion."
  12859. (let (vals)
  12860. (cond
  12861. ((equal property "TODO")
  12862. (setq vals (org-with-point-at pom
  12863. (append org-todo-keywords-1 '("")))))
  12864. ((equal property "PRIORITY")
  12865. (let ((n org-lowest-priority))
  12866. (while (>= n org-highest-priority)
  12867. (push (char-to-string n) vals)
  12868. (setq n (1- n)))))
  12869. ((member property org-special-properties))
  12870. (t
  12871. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12872. (when (and vals (string-match "\\S-" vals))
  12873. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12874. (setq vals (mapcar (lambda (x)
  12875. (cond ((stringp x) x)
  12876. ((numberp x) (number-to-string x))
  12877. ((symbolp x) (symbol-name x))
  12878. (t "???")))
  12879. vals)))))
  12880. (if table (mapcar 'list vals) vals)))
  12881. (defun org-property-previous-allowed-value (&optional previous)
  12882. "Switch to the next allowed value for this property."
  12883. (interactive)
  12884. (org-property-next-allowed-value t))
  12885. (defun org-property-next-allowed-value (&optional previous)
  12886. "Switch to the next allowed value for this property."
  12887. (interactive)
  12888. (unless (org-at-property-p)
  12889. (error "Not at a property"))
  12890. (let* ((key (match-string 2))
  12891. (value (match-string 3))
  12892. (allowed (or (org-property-get-allowed-values (point) key)
  12893. (and (member value '("[ ]" "[-]" "[X]"))
  12894. '("[ ]" "[X]"))))
  12895. nval)
  12896. (unless allowed
  12897. (error "Allowed values for this property have not been defined"))
  12898. (if previous (setq allowed (reverse allowed)))
  12899. (if (member value allowed)
  12900. (setq nval (car (cdr (member value allowed)))))
  12901. (setq nval (or nval (car allowed)))
  12902. (if (equal nval value)
  12903. (error "Only one allowed value for this property"))
  12904. (org-at-property-p)
  12905. (replace-match (concat " :" key ": " nval) t t)
  12906. (org-indent-line-function)
  12907. (beginning-of-line 1)
  12908. (skip-chars-forward " \t")))
  12909. ;;; Column View
  12910. (defvar org-columns-overlays nil
  12911. "Holds the list of current column overlays.")
  12912. (defvar org-columns-current-fmt nil
  12913. "Local variable, holds the currently active column format.")
  12914. (defvar org-columns-current-fmt-compiled nil
  12915. "Local variable, holds the currently active column format.
  12916. This is the compiled version of the format.")
  12917. (defvar org-columns-current-maxwidths nil
  12918. "Loval variable, holds the currently active maximum column widths.")
  12919. (defvar org-columns-begin-marker (make-marker)
  12920. "Points to the position where last a column creation command was called.")
  12921. (defvar org-columns-top-level-marker (make-marker)
  12922. "Points to the position where current columns region starts.")
  12923. (defvar org-columns-map (make-sparse-keymap)
  12924. "The keymap valid in column display.")
  12925. (defun org-columns-content ()
  12926. "Switch to contents view while in columns view."
  12927. (interactive)
  12928. (org-overview)
  12929. (org-content))
  12930. (org-defkey org-columns-map "c" 'org-columns-content)
  12931. (org-defkey org-columns-map "o" 'org-overview)
  12932. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  12933. (org-defkey org-columns-map "v" 'org-columns-show-value)
  12934. (org-defkey org-columns-map "q" 'org-columns-quit)
  12935. (org-defkey org-columns-map "r" 'org-columns-redo)
  12936. (org-defkey org-columns-map [left] 'backward-char)
  12937. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  12938. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  12939. (org-defkey org-columns-map [right] 'forward-char)
  12940. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  12941. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  12942. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-next-allowed-value)
  12943. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  12944. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  12945. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  12946. (org-defkey org-columns-map "<" 'org-columns-narrow)
  12947. (org-defkey org-columns-map ">" 'org-columns-widen)
  12948. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  12949. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  12950. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  12951. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  12952. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  12953. '("Column"
  12954. ["Edit property" org-columns-edit-value t]
  12955. ["Next allowed value" org-columns-next-allowed-value t]
  12956. ["Previous allowed value" org-columns-previous-allowed-value t]
  12957. ["Show full value" org-columns-show-value t]
  12958. ["Edit allowed" org-columns-edit-allowed t]
  12959. "--"
  12960. ["Edit column attributes" org-columns-edit-attributes t]
  12961. ["Increase column width" org-columns-widen t]
  12962. ["Decrease column width" org-columns-narrow t]
  12963. "--"
  12964. ["Move column right" org-columns-move-right t]
  12965. ["Move column left" org-columns-move-left t]
  12966. ["Add column" org-columns-new t]
  12967. ["Delete column" org-columns-delete t]
  12968. "--"
  12969. ["CONTENTS" org-columns-content t]
  12970. ["OVERVIEW" org-overview t]
  12971. ["Refresh columns display" org-columns-redo t]
  12972. "--"
  12973. ["Quit" org-columns-quit t]))
  12974. (defun org-columns-new-overlay (beg end &optional string face)
  12975. "Create a new column overlay and add it to the list."
  12976. (let ((ov (org-make-overlay beg end)))
  12977. (org-overlay-put ov 'face (or face 'secondary-selection))
  12978. (org-overlay-display ov string face)
  12979. (push ov org-columns-overlays)
  12980. ov))
  12981. (defun org-columns-display-here (&optional props)
  12982. "Overlay the current line with column display."
  12983. (interactive)
  12984. (let* ((fmt org-columns-current-fmt-compiled)
  12985. (beg (point-at-bol))
  12986. (level-face (save-excursion
  12987. (beginning-of-line 1)
  12988. (looking-at "\\(\\**\\)\\(\\* \\)")
  12989. (org-get-level-face 2)))
  12990. (color (list :foreground
  12991. (face-attribute (or level-face 'default) :foreground)))
  12992. props pom property ass width f string ov column)
  12993. ;; Check if the entry is in another buffer.
  12994. (unless props
  12995. (if (eq major-mode 'org-agenda-mode)
  12996. (setq pom (or (get-text-property (point) 'org-hd-marker)
  12997. (get-text-property (point) 'org-marker))
  12998. props (if pom (org-entry-properties pom) nil))
  12999. (setq props (org-entry-properties nil))))
  13000. ;; Walk the format
  13001. (while (setq column (pop fmt))
  13002. (setq property (car column)
  13003. ass (if (equal property "ITEM")
  13004. (cons "ITEM"
  13005. (save-match-data
  13006. (org-no-properties
  13007. (org-remove-tabs
  13008. (buffer-substring-no-properties
  13009. (point-at-bol) (point-at-eol))))))
  13010. (assoc property props))
  13011. width (or (cdr (assoc property org-columns-current-maxwidths))
  13012. (nth 2 column))
  13013. f (format "%%-%d.%ds | " width width)
  13014. string (format f (or (cdr ass) "")))
  13015. ;; Create the overlay
  13016. (org-unmodified
  13017. (setq ov (org-columns-new-overlay
  13018. beg (setq beg (1+ beg)) string
  13019. (list color 'org-column)))
  13020. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  13021. (org-overlay-put ov 'keymap org-columns-map)
  13022. (org-overlay-put ov 'org-columns-key property)
  13023. (org-overlay-put ov 'org-columns-value (cdr ass))
  13024. (org-overlay-put ov 'org-columns-pom pom)
  13025. (org-overlay-put ov 'org-columns-format f))
  13026. (if (or (not (char-after beg))
  13027. (equal (char-after beg) ?\n))
  13028. (let ((inhibit-read-only t))
  13029. (save-excursion
  13030. (goto-char beg)
  13031. (insert " ")))))
  13032. ;; Make the rest of the line disappear.
  13033. (org-unmodified
  13034. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  13035. (org-overlay-put ov 'invisible t)
  13036. (org-overlay-put ov 'keymap org-columns-map)
  13037. (org-overlay-put ov 'intangible t)
  13038. (push ov org-columns-overlays)
  13039. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  13040. (org-overlay-put ov 'keymap org-columns-map)
  13041. (push ov org-columns-overlays)
  13042. (let ((inhibit-read-only t))
  13043. (put-text-property (max (point-min) (1- (point-at-bol)))
  13044. (min (point-max) (1+ (point-at-eol)))
  13045. 'read-only "Type `e' to edit property")))))
  13046. (defvar org-previous-header-line-format nil
  13047. "The header line format before column view was turned on.")
  13048. (defvar org-columns-inhibit-recalculation nil
  13049. "Inhibit recomputing of columns on column view startup.")
  13050. (defvar header-line-format)
  13051. (defun org-columns-display-here-title ()
  13052. "Overlay the newline before the current line with the table title."
  13053. (interactive)
  13054. (let ((fmt org-columns-current-fmt-compiled)
  13055. string (title "")
  13056. property width f column str)
  13057. (while (setq column (pop fmt))
  13058. (setq property (car column)
  13059. str (or (nth 1 column) property)
  13060. width (or (cdr (assoc property org-columns-current-maxwidths))
  13061. (nth 2 column))
  13062. f (format "%%-%d.%ds | " width width)
  13063. string (format f str)
  13064. title (concat title string)))
  13065. (setq title (concat
  13066. (org-add-props " " nil 'display '(space :align-to 0))
  13067. (org-add-props title nil 'face '(:weight bold :underline t))))
  13068. (org-set-local 'org-previous-header-line-format header-line-format)
  13069. (setq header-line-format title)))
  13070. (defun org-columns-remove-overlays ()
  13071. "Remove all currently active column overlays."
  13072. (interactive)
  13073. (when (marker-buffer org-columns-begin-marker)
  13074. (with-current-buffer (marker-buffer org-columns-begin-marker)
  13075. (when (local-variable-p 'org-previous-header-line-format)
  13076. (setq header-line-format org-previous-header-line-format)
  13077. (kill-local-variable 'org-previous-header-line-format))
  13078. (move-marker org-columns-begin-marker nil)
  13079. (move-marker org-columns-top-level-marker nil)
  13080. (org-unmodified
  13081. (mapc 'org-delete-overlay org-columns-overlays)
  13082. (setq org-columns-overlays nil)
  13083. (let ((inhibit-read-only t))
  13084. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  13085. (defun org-columns-show-value ()
  13086. "Show the full value of the property."
  13087. (interactive)
  13088. (let ((value (get-char-property (point) 'org-columns-value)))
  13089. (message "Value is: %s" (or value ""))))
  13090. (defun org-columns-quit ()
  13091. "Remove the column overlays and in this way exit column editing."
  13092. (interactive)
  13093. (org-unmodified
  13094. (org-columns-remove-overlays)
  13095. (let ((inhibit-read-only t))
  13096. ;; FIXME: is this safe???
  13097. ;; or are there other reasons why there may be a read-only property????
  13098. (remove-text-properties (point-min) (point-max) '(read-only t))))
  13099. (when (eq major-mode 'org-agenda-mode)
  13100. (message "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  13101. (defun org-columns-edit-value ()
  13102. "Edit the value of the property at point in column view.
  13103. Where possible, use the standard interface for changing this line."
  13104. (interactive)
  13105. (let* ((col (current-column))
  13106. (key (get-char-property (point) 'org-columns-key))
  13107. (value (get-char-property (point) 'org-columns-value))
  13108. (bol (point-at-bol)) (eol (point-at-eol))
  13109. (pom (or (get-text-property bol 'org-hd-marker)
  13110. (point))) ; keep despite of compiler waring
  13111. (line-overlays
  13112. (delq nil (mapcar (lambda (x)
  13113. (and (eq (overlay-buffer x) (current-buffer))
  13114. (>= (overlay-start x) bol)
  13115. (<= (overlay-start x) eol)
  13116. x))
  13117. org-columns-overlays)))
  13118. nval eval allowed)
  13119. (when (equal key "ITEM")
  13120. (error "Cannot edit item headline from here"))
  13121. (cond
  13122. ((equal key "TODO")
  13123. (setq eval '(org-with-point-at pom
  13124. (let ((current-prefix-arg '(4))) (org-todo '(4))))))
  13125. ((equal key "PRIORITY")
  13126. (setq eval '(org-with-point-at pom
  13127. (call-interactively 'org-priority))))
  13128. ((equal key "TAGS")
  13129. (setq eval '(org-with-point-at pom
  13130. (let ((org-fast-tag-selection-single-key
  13131. (if (eq org-fast-tag-selection-single-key 'expert)
  13132. t org-fast-tag-selection-single-key)))
  13133. (call-interactively 'org-set-tags)))))
  13134. ((equal key "DEADLINE")
  13135. (setq eval '(org-with-point-at pom
  13136. (call-interactively 'org-deadline))))
  13137. ((equal key "SCHEDULED")
  13138. (setq eval '(org-with-point-at pom
  13139. (call-interactively 'org-schedule))))
  13140. (t
  13141. (setq allowed (org-property-get-allowed-values pom key 'table))
  13142. (if allowed
  13143. (setq nval (completing-read "Value: " allowed nil t))
  13144. (setq nval (read-string "Edit: " value)))
  13145. (setq nval (org-trim nval))
  13146. (when (not (equal nval value))
  13147. (setq eval '(org-entry-put pom key nval)))))
  13148. (when eval
  13149. (let ((inhibit-read-only t))
  13150. (remove-text-properties (1- bol) eol '(read-only t))
  13151. (unwind-protect
  13152. (progn
  13153. (setq org-columns-overlays
  13154. (org-delete-all line-overlays org-columns-overlays))
  13155. (mapc 'org-delete-overlay line-overlays)
  13156. (org-columns-eval eval))
  13157. (org-columns-display-here))))
  13158. (move-to-column col)
  13159. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13160. (org-columns-update key))))
  13161. (defun org-columns-edit-allowed ()
  13162. "Edit the list of allowed values for the current property."
  13163. (interactive)
  13164. (let* ((col (current-column))
  13165. (key (get-char-property (point) 'org-columns-key))
  13166. (key1 (concat key "_ALL"))
  13167. (value (get-char-property (point) 'org-columns-value))
  13168. (allowed (org-entry-get (point) key1 t))
  13169. nval)
  13170. (setq nval (read-string "Allowed: " allowed))
  13171. (org-entry-put
  13172. (cond ((marker-position org-entry-property-inherited-from)
  13173. org-entry-property-inherited-from)
  13174. ((marker-position org-columns-top-level-marker)
  13175. org-columns-top-level-marker))
  13176. key1 nval)))
  13177. (defun org-columns-eval (form)
  13178. (let (hidep)
  13179. (save-excursion
  13180. (beginning-of-line 1)
  13181. (next-line 1)
  13182. (setq hidep (org-on-heading-p 1)))
  13183. (eval form)
  13184. (and hidep (hide-entry))))
  13185. (defun org-columns-previous-allowed-value ()
  13186. "Switch to the previous allowed value for this column."
  13187. (interactive)
  13188. (org-columns-next-allowed-value t))
  13189. (defun org-columns-next-allowed-value (&optional previous)
  13190. "Switch to the next allowed value for this column."
  13191. (interactive)
  13192. (let* ((col (current-column))
  13193. (key (get-char-property (point) 'org-columns-key))
  13194. (value (get-char-property (point) 'org-columns-value))
  13195. (bol (point-at-bol)) (eol (point-at-eol))
  13196. (pom (or (get-text-property bol 'org-hd-marker)
  13197. (point))) ; keep despite of compiler waring
  13198. (line-overlays
  13199. (delq nil (mapcar (lambda (x)
  13200. (and (eq (overlay-buffer x) (current-buffer))
  13201. (>= (overlay-start x) bol)
  13202. (<= (overlay-start x) eol)
  13203. x))
  13204. org-columns-overlays)))
  13205. (allowed (or (org-property-get-allowed-values pom key)
  13206. (and (equal
  13207. (nth 4 (assoc key org-columns-current-fmt-compiled))
  13208. 'checkbox) '("[ ]" "[X]"))))
  13209. nval)
  13210. (when (equal key "ITEM")
  13211. (error "Cannot edit item headline from here"))
  13212. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  13213. (error "Allowed values for this property have not been defined"))
  13214. (if (member key '("SCHEDULED" "DEADLINE"))
  13215. (setq nval (if previous 'earlier 'later))
  13216. (if previous (setq allowed (reverse allowed)))
  13217. (if (member value allowed)
  13218. (setq nval (car (cdr (member value allowed)))))
  13219. (setq nval (or nval (car allowed)))
  13220. (if (equal nval value)
  13221. (error "Only one allowed value for this property")))
  13222. (let ((inhibit-read-only t))
  13223. (remove-text-properties (1- bol) eol '(read-only t))
  13224. (unwind-protect
  13225. (progn
  13226. (setq org-columns-overlays
  13227. (org-delete-all line-overlays org-columns-overlays))
  13228. (mapc 'org-delete-overlay line-overlays)
  13229. (org-columns-eval '(org-entry-put pom key nval)))
  13230. (org-columns-display-here)))
  13231. (move-to-column col)
  13232. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13233. (org-columns-update key))))
  13234. (defun org-verify-version (task)
  13235. (cond
  13236. ((eq task 'columns)
  13237. (if (or (featurep 'xemacs)
  13238. (< emacs-major-version 22))
  13239. (error "Emacs 22 is required for the columns feature")))))
  13240. (defun org-columns-get-format-and-top-level ()
  13241. (let (fmt)
  13242. (when (condition-case nil (org-back-to-heading) (error nil))
  13243. (move-marker org-entry-property-inherited-from nil)
  13244. (setq fmt (org-entry-get nil "COLUMNS" t)))
  13245. (setq fmt (or fmt org-columns-default-format))
  13246. (org-set-local 'org-columns-current-fmt fmt)
  13247. (org-columns-compile-format fmt)
  13248. (if (marker-position org-entry-property-inherited-from)
  13249. (move-marker org-columns-top-level-marker
  13250. org-entry-property-inherited-from)
  13251. (move-marker org-columns-top-level-marker (point)))
  13252. fmt))
  13253. (defun org-columns ()
  13254. "Turn on column view on an org-mode file."
  13255. (interactive)
  13256. (org-verify-version 'columns)
  13257. (org-columns-remove-overlays)
  13258. (move-marker org-columns-begin-marker (point))
  13259. (let (beg end fmt cache maxwidths)
  13260. (setq fmt (org-columns-get-format-and-top-level))
  13261. (save-excursion
  13262. (goto-char org-columns-top-level-marker)
  13263. (setq beg (point))
  13264. (unless org-columns-inhibit-recalculation
  13265. (org-columns-compute-all))
  13266. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  13267. (point-max)))
  13268. (goto-char beg)
  13269. ;; Get and cache the properties
  13270. (while (re-search-forward (concat "^" outline-regexp) end t)
  13271. (push (cons (org-current-line) (org-entry-properties)) cache))
  13272. (when cache
  13273. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13274. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13275. (org-columns-display-here-title)
  13276. (mapc (lambda (x)
  13277. (goto-line (car x))
  13278. (org-columns-display-here (cdr x)))
  13279. cache)))))
  13280. (defun org-columns-new (&optional prop title width op fmt)
  13281. "Insert a new column, to the leeft o the current column."
  13282. (interactive)
  13283. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  13284. cell)
  13285. (setq prop (completing-read
  13286. "Property: " (mapcar 'list (org-buffer-property-keys t))
  13287. nil nil prop))
  13288. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  13289. (setq width (read-string "Column width: " (if width (number-to-string width))))
  13290. (if (string-match "\\S-" width)
  13291. (setq width (string-to-number width))
  13292. (setq width nil))
  13293. (setq fmt (completing-read "Summary [none]: "
  13294. '(("none") ("add_numbers") ("add_times") ("checkbox"))
  13295. nil t))
  13296. (if (string-match "\\S-" fmt)
  13297. (setq fmt (intern fmt))
  13298. (setq fmt nil))
  13299. (if (eq fmt 'none) (setq fmt nil))
  13300. (if editp
  13301. (progn
  13302. (setcar editp prop)
  13303. (setcdr editp (list title width nil fmt)))
  13304. (setq cell (nthcdr (1- (current-column))
  13305. org-columns-current-fmt-compiled))
  13306. (setcdr cell (cons (list prop title width nil fmt)
  13307. (cdr cell))))
  13308. (org-columns-store-format)
  13309. (org-columns-redo)))
  13310. (defun org-columns-delete ()
  13311. "Delete the column at point from columns view."
  13312. (interactive)
  13313. (let* ((n (current-column))
  13314. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  13315. (when (y-or-n-p
  13316. (format "Are you sure you want to remove column \"%s\"? " title))
  13317. (setq org-columns-current-fmt-compiled
  13318. (delq (nth n org-columns-current-fmt-compiled)
  13319. org-columns-current-fmt-compiled))
  13320. (org-columns-store-format)
  13321. (org-columns-redo)
  13322. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  13323. (backward-char 1)))))
  13324. (defun org-columns-edit-attributes ()
  13325. "Edit the attributes of the current column."
  13326. (interactive)
  13327. (let* ((n (current-column))
  13328. (info (nth n org-columns-current-fmt-compiled)))
  13329. (apply 'org-columns-new info)))
  13330. (defun org-columns-widen (arg)
  13331. "Make the column wider by ARG characters."
  13332. (interactive "p")
  13333. (let* ((n (current-column))
  13334. (entry (nth n org-columns-current-fmt-compiled))
  13335. (width (or (nth 2 entry)
  13336. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  13337. (setq width (max 1 (+ width arg)))
  13338. (setcar (nthcdr 2 entry) width)
  13339. (org-columns-store-format)
  13340. (org-columns-redo)))
  13341. (defun org-columns-narrow (arg)
  13342. "Make the column nrrower by ARG characters."
  13343. (interactive "p")
  13344. (org-columns-widen (- arg)))
  13345. (defun org-columns-move-right ()
  13346. "Swap this column with the one to the right."
  13347. (interactive)
  13348. (let* ((n (current-column))
  13349. (cell (nthcdr n org-columns-current-fmt-compiled))
  13350. e)
  13351. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  13352. (error "Cannot shift this column further to the right"))
  13353. (setq e (car cell))
  13354. (setcar cell (car (cdr cell)))
  13355. (setcdr cell (cons e (cdr (cdr cell))))
  13356. (org-columns-store-format)
  13357. (org-columns-redo)
  13358. (forward-char 1)))
  13359. (defun org-columns-move-left ()
  13360. "Swap this column with the one to the left."
  13361. (interactive)
  13362. (let* ((n (current-column)))
  13363. (when (= n 0)
  13364. (error "Cannot shift this column further to the left"))
  13365. (backward-char 1)
  13366. (org-columns-move-right)
  13367. (backward-char 1)))
  13368. (defun org-columns-store-format ()
  13369. "Store the text version of the current columns format in appropriate place.
  13370. This is either in the COLUMNS property of the node starting the current column
  13371. display, or in the #+COLUMNS line of the current buffer."
  13372. (let (fmt)
  13373. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  13374. (if (marker-position org-columns-top-level-marker)
  13375. (save-excursion
  13376. (goto-char org-columns-top-level-marker)
  13377. (if (org-entry-get nil "COLUMNS")
  13378. (org-entry-put nil "COLUMNS" fmt)
  13379. (goto-char (point-min))
  13380. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  13381. (replace-match (concat "#+COLUMNS: " fmt t t)))))
  13382. (setq org-columns-current-fmt fmt))))
  13383. (defvar org-overriding-columns-format nil
  13384. "When set, overrides any other definition.")
  13385. (defvar org-agenda-view-columns-initially nil
  13386. "When set, switch to columns view immediately after creating the agenda.")
  13387. (defun org-agenda-columns ()
  13388. "Turn on column view in the agenda."
  13389. (interactive)
  13390. (org-verify-version 'columns)
  13391. (org-columns-remove-overlays)
  13392. (move-marker org-columns-begin-marker (point))
  13393. (let (fmt cache maxwidths m)
  13394. (cond
  13395. ((and (local-variable-p 'org-overriding-columns-format)
  13396. org-overriding-columns-format)
  13397. (setq fmt org-overriding-columns-format))
  13398. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  13399. (setq fmt (org-entry-get m "COLUMNS" t)))
  13400. ((and (boundp 'org-columns-current-fmt)
  13401. (local-variable-p 'org-columns-current-fmt)
  13402. org-columns-current-fmt)
  13403. (setq fmt org-columns-current-fmt))
  13404. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  13405. (setq m (get-text-property m 'org-hd-marker))
  13406. (setq fmt (org-entry-get m "COLUMNS" t))))
  13407. (setq fmt (or fmt org-columns-default-format))
  13408. (org-set-local 'org-columns-current-fmt fmt)
  13409. (org-columns-compile-format fmt)
  13410. (save-excursion
  13411. ;; Get and cache the properties
  13412. (goto-char (point-min))
  13413. (while (not (eobp))
  13414. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  13415. (get-text-property (point) 'org-marker)))
  13416. (push (cons (org-current-line) (org-entry-properties m)) cache))
  13417. (beginning-of-line 2))
  13418. (when cache
  13419. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13420. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13421. (org-columns-display-here-title)
  13422. (mapc (lambda (x)
  13423. (goto-line (car x))
  13424. (org-columns-display-here (cdr x)))
  13425. cache)))))
  13426. (defun org-columns-get-autowidth-alist (s cache)
  13427. "Derive the maximum column widths from the format and the cache."
  13428. (let ((start 0) rtn)
  13429. (while (string-match "%\\([a-zA-Z]\\S-*\\)" s start)
  13430. (push (cons (match-string 1 s) 1) rtn)
  13431. (setq start (match-end 0)))
  13432. (mapc (lambda (x)
  13433. (setcdr x (apply 'max
  13434. (mapcar
  13435. (lambda (y)
  13436. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  13437. cache))))
  13438. rtn)
  13439. rtn))
  13440. (defun org-columns-compute-all ()
  13441. "Compute all columns that have operators defined."
  13442. (org-unmodified
  13443. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  13444. (let ((columns org-columns-current-fmt-compiled) col)
  13445. (while (setq col (pop columns))
  13446. (when (nth 3 col)
  13447. (save-excursion
  13448. (org-columns-compute (car col)))))))
  13449. (defun org-columns-update (property)
  13450. "Recompute PROPERTY, and update the columns display for it."
  13451. (org-columns-compute property)
  13452. (let (fmt val pos)
  13453. (save-excursion
  13454. (mapc (lambda (ov)
  13455. (when (equal (org-overlay-get ov 'org-columns-key) property)
  13456. (setq pos (org-overlay-start ov))
  13457. (goto-char pos)
  13458. (when (setq val (cdr (assoc property
  13459. (get-text-property (point-at-bol) 'org-summaries))))
  13460. (setq fmt (org-overlay-get ov 'org-columns-format))
  13461. (org-overlay-put ov 'display (format fmt val)))))
  13462. org-columns-overlays))))
  13463. (defun org-columns-compute (property)
  13464. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  13465. (interactive)
  13466. (let* ((re (concat "^" outline-regexp))
  13467. (lmax 30) ; Does anyone use deeper levels???
  13468. (lsum (make-vector lmax 0))
  13469. (level 0)
  13470. (ass (assoc property org-columns-current-fmt-compiled))
  13471. (format (nth 4 ass))
  13472. (beg org-columns-top-level-marker)
  13473. last-level val end sumpos sum-alist sum str)
  13474. (save-excursion
  13475. ;; Find the region to compute
  13476. (goto-char beg)
  13477. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  13478. (goto-char end)
  13479. ;; Walk the tree from the back and do the computations
  13480. (while (re-search-backward re beg t)
  13481. (setq sumpos (match-beginning 0)
  13482. last-level level
  13483. level (org-outline-level)
  13484. val (org-entry-get nil property))
  13485. (cond
  13486. ((< level last-level)
  13487. ;; put the sum of lower levels here as a property
  13488. (setq sum (aref lsum last-level)
  13489. str (org-column-number-to-string sum format)
  13490. sum-alist (get-text-property sumpos 'org-summaries))
  13491. (if (assoc property sum-alist)
  13492. (setcdr (assoc property sum-alist) str)
  13493. (push (cons property str) sum-alist)
  13494. (org-unmodified
  13495. (add-text-properties sumpos (1+ sumpos)
  13496. (list 'org-summaries sum-alist))))
  13497. (when val ;?????????????????????????????????? and force?????
  13498. (org-entry-put nil property str))
  13499. ;; add current to current level accumulator
  13500. (aset lsum level (+ (aref lsum level) sum))
  13501. ;; clear accumulators for deeper levels
  13502. (loop for l from (1+ level) to (1- lmax) do (aset lsum l 0)))
  13503. ((>= level last-level)
  13504. ;; add what we have here to the accumulator for this level
  13505. (aset lsum level (+ (aref lsum level)
  13506. (org-column-string-to-number (or val "0") format))))
  13507. (t (error "This should not happen")))))))
  13508. (defun org-columns-redo ()
  13509. "Construct the column display again."
  13510. (interactive)
  13511. (message "Recomputing columns...")
  13512. (save-excursion
  13513. (if (marker-position org-columns-begin-marker)
  13514. (goto-char org-columns-begin-marker))
  13515. (org-columns-remove-overlays)
  13516. (if (org-mode-p)
  13517. (call-interactively 'org-columns)
  13518. (call-interactively 'org-agenda-columns)))
  13519. (message "Recomputing columns...done"))
  13520. (defun org-columns-not-in-agenda ()
  13521. (if (eq major-mode 'org-agenda-mode)
  13522. (error "This command is only allowed in Org-mode buffers")))
  13523. (defun org-string-to-number (s)
  13524. "Convert string to number, and interpret hh:mm:ss."
  13525. (if (not (string-match ":" s))
  13526. (string-to-number s)
  13527. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  13528. (while l
  13529. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  13530. sum)))
  13531. (defun org-column-number-to-string (n fmt)
  13532. "Convert a computed column number to a string value, according to FMT."
  13533. (cond
  13534. ((eq fmt 'add_times)
  13535. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  13536. (format "%d:%02d" h m)))
  13537. ((eq fmt 'checkbox)
  13538. (cond ((= n (floor n)) "[X]")
  13539. ((> n 1.) "[-]")
  13540. (t "[ ]")))
  13541. (t (number-to-string n))))
  13542. (defun org-column-string-to-number (s fmt)
  13543. "Convert a column value to a number that can be used for column computing."
  13544. (cond
  13545. ((string-match ":" s)
  13546. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  13547. (while l
  13548. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  13549. sum))
  13550. ((eq fmt 'checkbox)
  13551. (if (equal s "[X]") 1. 0.000001))
  13552. (t (string-to-number s))))
  13553. (defun org-columns-uncompile-format (cfmt)
  13554. "Turn the compiled columns format back into a string representation."
  13555. (let ((rtn "") e s prop title op width fmt)
  13556. (while (setq e (pop cfmt))
  13557. (setq prop (car e)
  13558. title (nth 1 e)
  13559. width (nth 2 e)
  13560. op (nth 3 e)
  13561. fmt (nth 4 e))
  13562. (cond
  13563. ((eq fmt 'add_times) (setq op ":"))
  13564. ((eq fmt 'checkbox) (setq op "X"))
  13565. ((eq fmt 'add_numbers) (setq op "+")))
  13566. (if (equal title prop) (setq title nil))
  13567. (setq s (concat "%" (if width (number-to-string width))
  13568. prop
  13569. (if title (concat "(" title ")"))
  13570. (if op (concat "{" op "}"))))
  13571. (setq rtn (concat rtn " " s)))
  13572. (org-trim rtn)))
  13573. (defun org-columns-compile-format (fmt)
  13574. "FIXME"
  13575. (let ((start 0) width prop title op f)
  13576. (setq org-columns-current-fmt-compiled nil)
  13577. (while (string-match "%\\([0-9]+\\)?\\([a-zA-Z_0-9]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*"
  13578. fmt start)
  13579. (setq start (match-end 0)
  13580. width (match-string 1 fmt)
  13581. prop (match-string 2 fmt)
  13582. title (or (match-string 3 fmt) prop)
  13583. op (match-string 4 fmt)
  13584. f nil)
  13585. (if width (setq width (string-to-number width)))
  13586. (cond
  13587. ((equal op "+") (setq f 'add_numbers))
  13588. ((equal op ":") (setq f 'add_times))
  13589. ((equal op "X") (setq f 'checkbox)))
  13590. (push (list prop title width op f) org-columns-current-fmt-compiled))
  13591. (setq org-columns-current-fmt-compiled
  13592. (nreverse org-columns-current-fmt-compiled))))
  13593. ;;;; Timestamps
  13594. (defvar org-last-changed-timestamp nil)
  13595. (defvar org-time-was-given) ; dynamically scoped parameter
  13596. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13597. (defvar org-ts-what) ; dynamically scoped parameter
  13598. (defun org-time-stamp (arg)
  13599. "Prompt for a date/time and insert a time stamp.
  13600. If the user specifies a time like HH:MM, or if this command is called
  13601. with a prefix argument, the time stamp will contain date and time.
  13602. Otherwise, only the date will be included. All parts of a date not
  13603. specified by the user will be filled in from the current date/time.
  13604. So if you press just return without typing anything, the time stamp
  13605. will represent the current date/time. If there is already a timestamp
  13606. at the cursor, it will be modified."
  13607. (interactive "P")
  13608. (let (org-time-was-given org-end-time-was-given time)
  13609. (cond
  13610. ((and (org-at-timestamp-p)
  13611. (eq last-command 'org-time-stamp)
  13612. (eq this-command 'org-time-stamp))
  13613. (insert "--")
  13614. (setq time (let ((this-command this-command))
  13615. (org-read-date arg 'totime)))
  13616. (org-insert-time-stamp time (or org-time-was-given arg)))
  13617. ((org-at-timestamp-p)
  13618. (setq time (let ((this-command this-command))
  13619. (org-read-date arg 'totime)))
  13620. (when (org-at-timestamp-p) ; just to get the match data
  13621. (replace-match "")
  13622. (setq org-last-changed-timestamp
  13623. (org-insert-time-stamp
  13624. time (or org-time-was-given arg)
  13625. nil nil nil (list org-end-time-was-given))))
  13626. (message "Timestamp updated"))
  13627. (t
  13628. (setq time (let ((this-command this-command))
  13629. (org-read-date arg 'totime)))
  13630. (org-insert-time-stamp time (or org-time-was-given arg)
  13631. nil nil nil (list org-end-time-was-given))))))
  13632. (defun org-time-stamp-inactive (&optional arg)
  13633. "Insert an inactive time stamp.
  13634. An inactive time stamp is enclosed in square brackets instead of angle
  13635. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13636. does not link to the calendar and cannot be changed with the S-cursor keys.
  13637. So these are more for recording a certain time/date."
  13638. (interactive "P")
  13639. (let (org-time-was-given org-end-time-was-given time)
  13640. (setq time (org-read-date arg 'totime))
  13641. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  13642. nil nil (list org-end-time-was-given))))
  13643. (defvar org-date-ovl (org-make-overlay 1 1))
  13644. (org-overlay-put org-date-ovl 'face 'org-warning)
  13645. (org-detach-overlay org-date-ovl)
  13646. (defvar org-ans1) ; dynamically scoped parameter
  13647. (defvar org-ans2) ; dynamically scoped parameter
  13648. (defvar org-plain-time-of-day-regexp) ; defined below
  13649. (defun org-read-date (&optional with-time to-time from-string prompt)
  13650. "Read a date and make things smooth for the user.
  13651. The prompt will suggest to enter an ISO date, but you can also enter anything
  13652. which will at least partially be understood by `parse-time-string'.
  13653. Unrecognized parts of the date will default to the current day, month, year,
  13654. hour and minute. For example,
  13655. 3-2-5 --> 2003-02-05
  13656. feb 15 --> currentyear-02-15
  13657. sep 12 9 --> 2009-09-12
  13658. 12:45 --> today 12:45
  13659. 22 sept 0:34 --> currentyear-09-22 0:34
  13660. 12 --> currentyear-currentmonth-12
  13661. Fri --> nearest Friday (today or later)
  13662. +4 --> four days from today (only if +N is the only thing given)
  13663. etc.
  13664. The function understands only English month and weekday abbreviations,
  13665. but this can be configured with the variables `parse-time-months' and
  13666. `parse-time-weekdays'.
  13667. While prompting, a calendar is popped up - you can also select the
  13668. date with the mouse (button 1). The calendar shows a period of three
  13669. months. To scroll it to other months, use the keys `>' and `<'.
  13670. If you don't like the calendar, turn it off with
  13671. \(setq org-popup-calendar-for-date-prompt nil)
  13672. With optional argument TO-TIME, the date will immediately be converted
  13673. to an internal time.
  13674. With an optional argument WITH-TIME, the prompt will suggest to also
  13675. insert a time. Note that when WITH-TIME is not set, you can still
  13676. enter a time, and this function will inform the calling routine about
  13677. this change. The calling routine may then choose to change the format
  13678. used to insert the time stamp into the buffer to include the time."
  13679. (require 'parse-time)
  13680. (let* ((org-time-stamp-rounding-minutes
  13681. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  13682. (ct (org-current-time))
  13683. (default-time
  13684. ;; Default time is either today, or, when entering a range,
  13685. ;; the range start.
  13686. (if (save-excursion
  13687. (re-search-backward
  13688. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13689. (- (point) 20) t))
  13690. (apply
  13691. 'encode-time
  13692. (mapcar (lambda(x) (or x 0))
  13693. (parse-time-string (match-string 1))))
  13694. ct))
  13695. (calendar-move-hook nil)
  13696. (view-diary-entries-initially nil)
  13697. (view-calendar-holidays-initially nil)
  13698. (timestr (format-time-string
  13699. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  13700. (prompt (concat (if prompt (concat prompt " ") "")
  13701. (format "Date and/or time (default [%s]): " timestr)))
  13702. ans (org-ans0 "") org-ans1 org-ans2 (deltadays 0)
  13703. second minute hour day month year tl wday wday1 pm)
  13704. (cond
  13705. (from-string (setq ans from-string))
  13706. (org-popup-calendar-for-date-prompt
  13707. (save-excursion
  13708. (save-window-excursion
  13709. (calendar)
  13710. (calendar-forward-day (- (time-to-days default-time)
  13711. (calendar-absolute-from-gregorian
  13712. (calendar-current-date))))
  13713. (org-eval-in-calendar nil t)
  13714. (let* ((old-map (current-local-map))
  13715. (map (copy-keymap calendar-mode-map))
  13716. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13717. (org-defkey map (kbd "RET") 'org-calendar-select)
  13718. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  13719. 'org-calendar-select-mouse)
  13720. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  13721. 'org-calendar-select-mouse)
  13722. (org-defkey minibuffer-local-map [(meta shift left)]
  13723. (lambda () (interactive)
  13724. (org-eval-in-calendar '(calendar-backward-month 1))))
  13725. (org-defkey minibuffer-local-map [(meta shift right)]
  13726. (lambda () (interactive)
  13727. (org-eval-in-calendar '(calendar-forward-month 1))))
  13728. (org-defkey minibuffer-local-map [(shift up)]
  13729. (lambda () (interactive)
  13730. (org-eval-in-calendar '(calendar-backward-week 1))))
  13731. (org-defkey minibuffer-local-map [(shift down)]
  13732. (lambda () (interactive)
  13733. (org-eval-in-calendar '(calendar-forward-week 1))))
  13734. (org-defkey minibuffer-local-map [(shift left)]
  13735. (lambda () (interactive)
  13736. (org-eval-in-calendar '(calendar-backward-day 1))))
  13737. (org-defkey minibuffer-local-map [(shift right)]
  13738. (lambda () (interactive)
  13739. (org-eval-in-calendar '(calendar-forward-day 1))))
  13740. (org-defkey minibuffer-local-map ">"
  13741. (lambda () (interactive)
  13742. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13743. (org-defkey minibuffer-local-map "<"
  13744. (lambda () (interactive)
  13745. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13746. (unwind-protect
  13747. (progn
  13748. (use-local-map map)
  13749. (setq org-ans0 (read-string prompt "" nil nil))
  13750. ;; org-ans0: from prompt
  13751. ;; org-ans1: from mouse click
  13752. ;; org-ans2: from calendar motion
  13753. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13754. (use-local-map old-map))))))
  13755. (t ; Naked prompt only
  13756. (setq ans (read-string prompt "" nil timestr))))
  13757. (org-detach-overlay org-date-ovl)
  13758. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" org-ans0)
  13759. (setq deltadays (string-to-number ans) ans ""))
  13760. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  13761. (when (string-match
  13762. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13763. (setq year (if (match-end 2)
  13764. (string-to-number (match-string 2 ans))
  13765. (string-to-number (format-time-string "%Y")))
  13766. month (string-to-number (match-string 3 ans))
  13767. day (string-to-number (match-string 4 ans)))
  13768. (if (< year 100) (setq year (+ 2000 year)))
  13769. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13770. t nil ans)))
  13771. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13772. ;; If there is a time with am/pm, and *no* time without it, we convert
  13773. ;; so that matching will be successful.
  13774. ;; FIXME: make this replace twice, so that we catch the end time.
  13775. (when (and (not (string-match "[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13776. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13777. (setq hour (string-to-number (match-string 1 ans))
  13778. minute (if (match-end 3) (string-to-number (match-string 3 ans)) 0)
  13779. pm (equal ?p (string-to-char (downcase (match-string 4 ans)))))
  13780. (if (and (= hour 12) (not pm))
  13781. (setq hour 0)
  13782. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13783. (setq ans (replace-match (format "%02d:%02d" hour minute) t t ans)))
  13784. ;; Check if there is a time range
  13785. (when (and (boundp 'org-end-time-was-given)
  13786. (string-match org-plain-time-of-day-regexp ans)
  13787. (match-end 8))
  13788. (setq org-end-time-was-given (match-string 8 ans))
  13789. (setq ans (concat (substring ans 0 (match-beginning 7))
  13790. (substring ans (match-end 7)))))
  13791. (setq tl (parse-time-string ans)
  13792. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  13793. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  13794. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  13795. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  13796. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  13797. second (or (nth 0 tl) 0)
  13798. wday (nth 6 tl))
  13799. (setq day (+ day deltadays))
  13800. (when (and wday (not (nth 3 tl)))
  13801. ;; Weekday was given, but no day, so pick that day in the week
  13802. ;; on or after the derived date.
  13803. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13804. (unless (equal wday wday1)
  13805. (setq day (+ day (% (- wday wday1 -7) 7)))))
  13806. (if (and (boundp 'org-time-was-given)
  13807. (nth 2 tl))
  13808. (setq org-time-was-given t))
  13809. (if (< year 100) (setq year (+ 2000 year)))
  13810. (if to-time
  13811. (encode-time second minute hour day month year)
  13812. (if (or (nth 1 tl) (nth 2 tl))
  13813. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  13814. (format "%04d-%02d-%02d" year month day)))))
  13815. (defun org-eval-in-calendar (form &optional keepdate)
  13816. "Eval FORM in the calendar window and return to current window.
  13817. Also, store the cursor date in variable org-ans2."
  13818. (let ((sw (selected-window)))
  13819. (select-window (get-buffer-window "*Calendar*"))
  13820. (eval form)
  13821. (when (and (not keepdate) (calendar-cursor-to-date))
  13822. (let* ((date (calendar-cursor-to-date))
  13823. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13824. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13825. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13826. (select-window sw)
  13827. ;; Update the prompt to show new default date
  13828. (save-excursion
  13829. (goto-char (point-min))
  13830. (when (and org-ans2
  13831. (re-search-forward "\\[[-0-9]+\\]" nil t)
  13832. (get-text-property (match-end 0) 'field))
  13833. (let ((inhibit-read-only t))
  13834. (replace-match (concat "[" org-ans2 "]") t t)
  13835. (add-text-properties (point-min) (1+ (match-end 0))
  13836. (text-properties-at (1+ (point-min)))))))))
  13837. (defun org-calendar-select ()
  13838. "Return to `org-read-date' with the date currently selected.
  13839. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13840. (interactive)
  13841. (when (calendar-cursor-to-date)
  13842. (let* ((date (calendar-cursor-to-date))
  13843. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13844. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13845. (if (active-minibuffer-window) (exit-minibuffer))))
  13846. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13847. "Insert a date stamp for the date given by the internal TIME.
  13848. WITH-HM means, use the stamp format that includes the time of the day.
  13849. INACTIVE means use square brackets instead of angular ones, so that the
  13850. stamp will not contribute to the agenda.
  13851. PRE and POST are optional strings to be inserted before and after the
  13852. stamp.
  13853. The command returns the inserted time stamp."
  13854. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13855. stamp)
  13856. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13857. (insert (or pre ""))
  13858. (insert (setq stamp (format-time-string fmt time)))
  13859. (when (listp extra)
  13860. (setq extra (car extra))
  13861. (if (and (stringp extra)
  13862. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13863. (setq extra (format "-%02d:%02d"
  13864. (string-to-number (match-string 1 extra))
  13865. (string-to-number (match-string 2 extra))))
  13866. (setq extra nil)))
  13867. (when extra
  13868. (backward-char 1)
  13869. (insert extra)
  13870. (forward-char 1))
  13871. (insert (or post ""))
  13872. stamp))
  13873. (defun org-toggle-time-stamp-overlays ()
  13874. "Toggle the use of custom time stamp formats."
  13875. (interactive)
  13876. (setq org-display-custom-times (not org-display-custom-times))
  13877. (unless org-display-custom-times
  13878. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13879. (while (setq p (next-single-property-change p 'display))
  13880. (if (and (get-text-property p 'display)
  13881. (eq (get-text-property p 'face) 'org-date))
  13882. (remove-text-properties
  13883. p (setq p (next-single-property-change p 'display))
  13884. '(display t))))
  13885. (set-buffer-modified-p bmp)))
  13886. (if (featurep 'xemacs)
  13887. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13888. (org-restart-font-lock)
  13889. (setq org-table-may-need-update t)
  13890. (if org-display-custom-times
  13891. (message "Time stamps are overlayed with custom format")
  13892. (message "Time stamp overlays removed")))
  13893. (defun org-display-custom-time (beg end)
  13894. "Overlay modified time stamp format over timestamp between BED and END."
  13895. (let* ((ts (buffer-substring beg end))
  13896. t1 w1 with-hm tf time str w2 (off 0))
  13897. (save-match-data
  13898. (setq t1 (org-parse-time-string ts t))
  13899. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  13900. (setq off (- (match-end 0) (match-beginning 0)))))
  13901. (setq end (- end off))
  13902. (setq w1 (- end beg)
  13903. with-hm (and (nth 1 t1) (nth 2 t1))
  13904. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13905. time (org-fix-decoded-time t1)
  13906. str (org-add-props
  13907. (format-time-string
  13908. (substring tf 1 -1) (apply 'encode-time time))
  13909. nil 'mouse-face 'highlight)
  13910. w2 (length str))
  13911. (if (not (= w2 w1))
  13912. (add-text-properties (1+ beg) (+ 2 beg)
  13913. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13914. (if (featurep 'xemacs)
  13915. (progn
  13916. (put-text-property beg end 'invisible t)
  13917. (put-text-property beg end 'end-glyph (make-glyph str)))
  13918. (put-text-property beg end 'display str))))
  13919. (defun org-translate-time (string)
  13920. "Translate all timestamps in STRING to custom format.
  13921. But do this only if the variable `org-display-custom-times' is set."
  13922. (when org-display-custom-times
  13923. (save-match-data
  13924. (let* ((start 0)
  13925. (re org-ts-regexp-both)
  13926. t1 with-hm inactive tf time str beg end)
  13927. (while (setq start (string-match re string start))
  13928. (setq beg (match-beginning 0)
  13929. end (match-end 0)
  13930. t1 (save-match-data
  13931. (org-parse-time-string (substring string beg end) t))
  13932. with-hm (and (nth 1 t1) (nth 2 t1))
  13933. inactive (equal (substring string beg (1+ beg)) "[")
  13934. tf (funcall (if with-hm 'cdr 'car)
  13935. org-time-stamp-custom-formats)
  13936. time (org-fix-decoded-time t1)
  13937. str (format-time-string
  13938. (concat
  13939. (if inactive "[" "<") (substring tf 1 -1)
  13940. (if inactive "]" ">"))
  13941. (apply 'encode-time time))
  13942. string (replace-match str t t string)
  13943. start (+ start (length str)))))))
  13944. string)
  13945. (defun org-fix-decoded-time (time)
  13946. "Set 0 instead of nil for the first 6 elements of time.
  13947. Don't touch the rest."
  13948. (let ((n 0))
  13949. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13950. (defun org-days-to-time (timestamp-string)
  13951. "Difference between TIMESTAMP-STRING and now in days."
  13952. (- (time-to-days (org-time-string-to-time timestamp-string))
  13953. (time-to-days (current-time))))
  13954. (defun org-deadline-close (timestamp-string &optional ndays)
  13955. "Is the time in TIMESTAMP-STRING close to the current date?"
  13956. (and (< (org-days-to-time timestamp-string)
  13957. (or ndays org-deadline-warning-days))
  13958. (not (org-entry-is-done-p))))
  13959. (defun org-calendar-select-mouse (ev)
  13960. "Return to `org-read-date' with the date currently selected.
  13961. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13962. (interactive "e")
  13963. (mouse-set-point ev)
  13964. (when (calendar-cursor-to-date)
  13965. (let* ((date (calendar-cursor-to-date))
  13966. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13967. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13968. (if (active-minibuffer-window) (exit-minibuffer))))
  13969. (defun org-check-deadlines (ndays)
  13970. "Check if there are any deadlines due or past due.
  13971. A deadline is considered due if it happens within `org-deadline-warning-days'
  13972. days from today's date. If the deadline appears in an entry marked DONE,
  13973. it is not shown. The prefix arg NDAYS can be used to test that many
  13974. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13975. (interactive "P")
  13976. (let* ((org-warn-days
  13977. (cond
  13978. ((equal ndays '(4)) 100000)
  13979. (ndays (prefix-numeric-value ndays))
  13980. (t org-deadline-warning-days)))
  13981. (case-fold-search nil)
  13982. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13983. (callback
  13984. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13985. (message "%d deadlines past-due or due within %d days"
  13986. (org-occur regexp nil callback)
  13987. org-warn-days)))
  13988. (defun org-evaluate-time-range (&optional to-buffer)
  13989. "Evaluate a time range by computing the difference between start and end.
  13990. Normally the result is just printed in the echo area, but with prefix arg
  13991. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13992. If the time range is actually in a table, the result is inserted into the
  13993. next column.
  13994. For time difference computation, a year is assumed to be exactly 365
  13995. days in order to avoid rounding problems."
  13996. (interactive "P")
  13997. (or
  13998. (org-clock-update-time-maybe)
  13999. (save-excursion
  14000. (unless (org-at-date-range-p)
  14001. (goto-char (point-at-bol))
  14002. (re-search-forward org-tr-regexp (point-at-eol) t))
  14003. (if (not (org-at-date-range-p))
  14004. (error "Not at a time-stamp range, and none found in current line")))
  14005. (let* ((ts1 (match-string 1))
  14006. (ts2 (match-string 2))
  14007. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14008. (match-end (match-end 0))
  14009. (time1 (org-time-string-to-time ts1))
  14010. (time2 (org-time-string-to-time ts2))
  14011. (t1 (time-to-seconds time1))
  14012. (t2 (time-to-seconds time2))
  14013. (diff (abs (- t2 t1)))
  14014. (negative (< (- t2 t1) 0))
  14015. ;; (ys (floor (* 365 24 60 60)))
  14016. (ds (* 24 60 60))
  14017. (hs (* 60 60))
  14018. (fy "%dy %dd %02d:%02d")
  14019. (fy1 "%dy %dd")
  14020. (fd "%dd %02d:%02d")
  14021. (fd1 "%dd")
  14022. (fh "%02d:%02d")
  14023. y d h m align)
  14024. (if havetime
  14025. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14026. y 0
  14027. d (floor (/ diff ds)) diff (mod diff ds)
  14028. h (floor (/ diff hs)) diff (mod diff hs)
  14029. m (floor (/ diff 60)))
  14030. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14031. y 0
  14032. d (floor (+ (/ diff ds) 0.5))
  14033. h 0 m 0))
  14034. (if (not to-buffer)
  14035. (message (org-make-tdiff-string y d h m))
  14036. (when (org-at-table-p)
  14037. (goto-char match-end)
  14038. (setq align t)
  14039. (and (looking-at " *|") (goto-char (match-end 0))))
  14040. (if (looking-at
  14041. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14042. (replace-match ""))
  14043. (if negative (insert " -"))
  14044. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14045. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14046. (insert " " (format fh h m))))
  14047. (if align (org-table-align))
  14048. (message "Time difference inserted")))))
  14049. (defun org-make-tdiff-string (y d h m)
  14050. (let ((fmt "")
  14051. (l nil))
  14052. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14053. l (push y l)))
  14054. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14055. l (push d l)))
  14056. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14057. l (push h l)))
  14058. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14059. l (push m l)))
  14060. (apply 'format fmt (nreverse l))))
  14061. (defun org-time-string-to-time (s)
  14062. (apply 'encode-time (org-parse-time-string s)))
  14063. (defun org-time-string-to-absolute (s &optional daynr)
  14064. "Convert a time stamp to an absolute day number.
  14065. If there is a specifyer for a cyclic time stamp, get the closest date to
  14066. DAYNR."
  14067. (cond
  14068. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14069. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14070. daynr
  14071. (+ daynr 1000)))
  14072. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14073. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14074. (time-to-days (current-time))) (match-string 0 s)))
  14075. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  14076. (defun org-calendar-holiday ()
  14077. "List of holidays, for Diary display in Org-mode."
  14078. (let ((hl (check-calendar-holidays date)))
  14079. (if hl (mapconcat 'identity hl "; "))))
  14080. (defun org-diary-sexp-entry (sexp entry date)
  14081. "Process a SEXP diary ENTRY for DATE."
  14082. (require 'diary-lib)
  14083. (let ((result (if calendar-debug-sexp
  14084. (let ((stack-trace-on-error t))
  14085. (eval (car (read-from-string sexp))))
  14086. (condition-case nil
  14087. (eval (car (read-from-string sexp)))
  14088. (error
  14089. (beep)
  14090. (message "Bad sexp at line %d in %s: %s"
  14091. (org-current-line)
  14092. (buffer-file-name) sexp)
  14093. (sleep-for 2))))))
  14094. (cond ((stringp result) result)
  14095. ((and (consp result)
  14096. (stringp (cdr result))) (cdr result))
  14097. (result entry)
  14098. (t nil))))
  14099. (defun org-diary-to-ical-string (frombuf)
  14100. "FIXME"
  14101. (let* ((tmpdir (if (featurep 'xemacs)
  14102. (temp-directory)
  14103. temporary-file-directory))
  14104. (tmpfile (make-temp-name
  14105. (expand-file-name "orgics" tmpdir)))
  14106. buf rtn b e)
  14107. (save-excursion
  14108. (set-buffer frombuf)
  14109. (icalendar-export-region (point-min) (point-max) tmpfile)
  14110. (setq buf (find-buffer-visiting tmpfile))
  14111. (set-buffer buf)
  14112. (goto-char (point-min))
  14113. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14114. (setq b (match-beginning 0)))
  14115. (goto-char (point-max))
  14116. (if (re-search-backward "^END:VEVENT" nil t)
  14117. (setq e (match-end 0)))
  14118. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14119. (kill-buffer buf)
  14120. (kill-buffer frombuf)
  14121. (delete-file tmpfile)
  14122. rtn))
  14123. (defun org-closest-date (start current change)
  14124. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  14125. ;; Make the proper lists from the dates
  14126. (catch 'exit
  14127. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14128. dn dw sday cday n1 n2
  14129. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14130. (setq start (org-date-to-gregorian start)
  14131. current (org-date-to-gregorian
  14132. (if org-agenda-repeating-timestamp-show-all
  14133. current
  14134. (time-to-days (current-time))))
  14135. sday (calendar-absolute-from-gregorian start)
  14136. cday (calendar-absolute-from-gregorian current))
  14137. (if (<= cday sday) (throw 'exit sday))
  14138. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14139. (setq dn (string-to-number (match-string 1 change))
  14140. dw (cdr (assoc (match-string 2 change) a1)))
  14141. (error "Invalid change specifyer: %s" change))
  14142. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14143. (cond
  14144. ((eq dw 'day)
  14145. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14146. n2 (+ n1 dn)))
  14147. ((eq dw 'year)
  14148. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14149. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14150. (setq date1 (list m d y1)
  14151. n1 (calendar-absolute-from-gregorian date1)
  14152. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14153. n2 (calendar-absolute-from-gregorian date2)))
  14154. ((eq dw 'month)
  14155. ;; approx number of month between the tow dates
  14156. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14157. ;; How often does dn fit in there?
  14158. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14159. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14160. m (+ m nm)
  14161. ny (floor (/ m 12))
  14162. y (+ y ny)
  14163. m (- m (* ny 12)))
  14164. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14165. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14166. (setq m2 (+ m dn) y2 y)
  14167. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14168. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14169. (while (< n2 cday)
  14170. (setq n1 n2 m m2 y y2)
  14171. (setq m2 (+ m dn) y2 y)
  14172. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14173. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14174. (if org-agenda-repeating-timestamp-show-all
  14175. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  14176. (if (= cday n1) n1 n2)))))
  14177. (defun org-date-to-gregorian (date)
  14178. "Turn any specification of DATE into a gregorian date for the calendar."
  14179. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14180. ((and (listp date) (= (length date) 3)) date)
  14181. ((stringp date)
  14182. (setq date (org-parse-time-string date))
  14183. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14184. ((listp date)
  14185. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14186. (defun org-parse-time-string (s &optional nodefault)
  14187. "Parse the standard Org-mode time string.
  14188. This should be a lot faster than the normal `parse-time-string'.
  14189. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14190. hour and minute fields will be nil if not given."
  14191. (if (string-match org-ts-regexp0 s)
  14192. (list 0
  14193. (if (or (match-beginning 8) (not nodefault))
  14194. (string-to-number (or (match-string 8 s) "0")))
  14195. (if (or (match-beginning 7) (not nodefault))
  14196. (string-to-number (or (match-string 7 s) "0")))
  14197. (string-to-number (match-string 4 s))
  14198. (string-to-number (match-string 3 s))
  14199. (string-to-number (match-string 2 s))
  14200. nil nil nil)
  14201. (make-list 9 0)))
  14202. (defun org-timestamp-up (&optional arg)
  14203. "Increase the date item at the cursor by one.
  14204. If the cursor is on the year, change the year. If it is on the month or
  14205. the day, change that.
  14206. With prefix ARG, change by that many units."
  14207. (interactive "p")
  14208. (org-timestamp-change (prefix-numeric-value arg)))
  14209. (defun org-timestamp-down (&optional arg)
  14210. "Decrease the date item at the cursor by one.
  14211. If the cursor is on the year, change the year. If it is on the month or
  14212. the day, change that.
  14213. With prefix ARG, change by that many units."
  14214. (interactive "p")
  14215. (org-timestamp-change (- (prefix-numeric-value arg))))
  14216. (defun org-timestamp-up-day (&optional arg)
  14217. "Increase the date in the time stamp by one day.
  14218. With prefix ARG, change that many days."
  14219. (interactive "p")
  14220. (if (and (not (org-at-timestamp-p t))
  14221. (org-on-heading-p))
  14222. (org-todo 'up)
  14223. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  14224. (defun org-timestamp-down-day (&optional arg)
  14225. "Decrease the date in the time stamp by one day.
  14226. With prefix ARG, change that many days."
  14227. (interactive "p")
  14228. (if (and (not (org-at-timestamp-p t))
  14229. (org-on-heading-p))
  14230. (org-todo 'down)
  14231. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  14232. (defsubst org-pos-in-match-range (pos n)
  14233. (and (match-beginning n)
  14234. (<= (match-beginning n) pos)
  14235. (>= (match-end n) pos)))
  14236. (defun org-at-timestamp-p (&optional inactive-ok)
  14237. "Determine if the cursor is in or at a timestamp."
  14238. (interactive)
  14239. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14240. (pos (point))
  14241. (ans (or (looking-at tsr)
  14242. (save-excursion
  14243. (skip-chars-backward "^[<\n\r\t")
  14244. (if (> (point) 1) (backward-char 1))
  14245. (and (looking-at tsr)
  14246. (> (- (match-end 0) pos) -1))))))
  14247. (and (boundp 'org-ts-what)
  14248. (setq org-ts-what
  14249. (cond
  14250. ((org-pos-in-match-range pos 2) 'year)
  14251. ((org-pos-in-match-range pos 3) 'month)
  14252. ((org-pos-in-match-range pos 7) 'hour)
  14253. ((org-pos-in-match-range pos 8) 'minute)
  14254. ((or (org-pos-in-match-range pos 4)
  14255. (org-pos-in-match-range pos 5)) 'day)
  14256. ((and (> pos (or (match-end 8) (match-end 5)))
  14257. (< pos (match-end 0)))
  14258. (- pos (or (match-end 8) (match-end 5))))
  14259. (t 'day))))
  14260. ans))
  14261. (defun org-timestamp-change (n &optional what)
  14262. "Change the date in the time stamp at point.
  14263. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14264. `year', `minute', `second'. If WHAT is not given, the cursor position
  14265. in the timestamp determines what will be changed."
  14266. (let ((pos (point))
  14267. with-hm inactive
  14268. org-ts-what
  14269. extra
  14270. ts time time0)
  14271. (if (not (org-at-timestamp-p t))
  14272. (error "Not at a timestamp"))
  14273. (if (and (not what) (not (eq org-ts-what 'day))
  14274. org-display-custom-times
  14275. (get-text-property (point) 'display)
  14276. (not (get-text-property (1- (point)) 'display)))
  14277. (setq org-ts-what 'day))
  14278. (setq org-ts-what (or what org-ts-what)
  14279. inactive (= (char-after (match-beginning 0)) ?\[)
  14280. ts (match-string 0))
  14281. (replace-match "")
  14282. (if (string-match
  14283. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  14284. ts)
  14285. (setq extra (match-string 1 ts)))
  14286. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14287. (setq with-hm t))
  14288. (setq time0 (org-parse-time-string ts))
  14289. (setq time
  14290. (apply 'encode-time
  14291. (append
  14292. (list (or (car time0) 0))
  14293. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  14294. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  14295. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  14296. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  14297. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  14298. (nthcdr 6 time0))))
  14299. (when (integerp org-ts-what)
  14300. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  14301. (if (eq what 'calendar)
  14302. (let ((cal-date
  14303. (save-excursion
  14304. (save-match-data
  14305. (set-buffer "*Calendar*")
  14306. (calendar-cursor-to-date)))))
  14307. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14308. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14309. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14310. (setcar time0 (or (car time0) 0))
  14311. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14312. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  14313. (setq time (apply 'encode-time time0))))
  14314. (setq org-last-changed-timestamp
  14315. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14316. (org-clock-update-time-maybe)
  14317. (goto-char pos)
  14318. ;; Try to recenter the calendar window, if any
  14319. (if (and org-calendar-follow-timestamp-change
  14320. (get-buffer-window "*Calendar*" t)
  14321. (memq org-ts-what '(day month year)))
  14322. (org-recenter-calendar (time-to-days time)))))
  14323. (defun org-modify-ts-extra (s pos n)
  14324. "FIXME"
  14325. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14326. ng h m new)
  14327. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14328. (cond
  14329. ((or (org-pos-in-match-range pos 2)
  14330. (org-pos-in-match-range pos 3))
  14331. (setq m (string-to-number (match-string 3 s))
  14332. h (string-to-number (match-string 2 s)))
  14333. (if (org-pos-in-match-range pos 2)
  14334. (setq h (+ h n))
  14335. (setq m (+ m n)))
  14336. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14337. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14338. (setq h (min 24 (max 0 h)))
  14339. (setq ng 1 new (format "-%02d:%02d" h m)))
  14340. ((org-pos-in-match-range pos 6)
  14341. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14342. ((org-pos-in-match-range pos 5)
  14343. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  14344. (when ng
  14345. (setq s (concat
  14346. (substring s 0 (match-beginning ng))
  14347. new
  14348. (substring s (match-end ng))))))
  14349. s))
  14350. (defun org-recenter-calendar (date)
  14351. "If the calendar is visible, recenter it to DATE."
  14352. (let* ((win (selected-window))
  14353. (cwin (get-buffer-window "*Calendar*" t))
  14354. (calendar-move-hook nil))
  14355. (when cwin
  14356. (select-window cwin)
  14357. (calendar-goto-date (if (listp date) date
  14358. (calendar-gregorian-from-absolute date)))
  14359. (select-window win))))
  14360. (defun org-goto-calendar (&optional arg)
  14361. "Go to the Emacs calendar at the current date.
  14362. If there is a time stamp in the current line, go to that date.
  14363. A prefix ARG can be used to force the current date."
  14364. (interactive "P")
  14365. (let ((tsr org-ts-regexp) diff
  14366. (calendar-move-hook nil)
  14367. (view-calendar-holidays-initially nil)
  14368. (view-diary-entries-initially nil))
  14369. (if (or (org-at-timestamp-p)
  14370. (save-excursion
  14371. (beginning-of-line 1)
  14372. (looking-at (concat ".*" tsr))))
  14373. (let ((d1 (time-to-days (current-time)))
  14374. (d2 (time-to-days
  14375. (org-time-string-to-time (match-string 1)))))
  14376. (setq diff (- d2 d1))))
  14377. (calendar)
  14378. (calendar-goto-today)
  14379. (if (and diff (not arg)) (calendar-forward-day diff))))
  14380. (defun org-date-from-calendar ()
  14381. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14382. If there is already a time stamp at the cursor position, update it."
  14383. (interactive)
  14384. (org-timestamp-change 0 'calendar))
  14385. ;;; The clock for measuring work time.
  14386. (defvar org-mode-line-string "")
  14387. (put 'org-mode-line-string 'risky-local-variable t)
  14388. (defvar org-mode-line-timer nil)
  14389. (defvar org-clock-heading "")
  14390. (defvar org-clock-start-time "")
  14391. (defun org-update-mode-line ()
  14392. (let* ((delta (- (time-to-seconds (current-time))
  14393. (time-to-seconds org-clock-start-time)))
  14394. (h (floor delta 3600))
  14395. (m (floor (- delta (* 3600 h)) 60)))
  14396. (setq org-mode-line-string
  14397. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  14398. 'help-echo "Org-mode clock is running"))
  14399. (force-mode-line-update)))
  14400. (defvar org-clock-marker (make-marker)
  14401. "Marker recording the last clock-in.")
  14402. (defvar org-clock-mode-line-entry nil
  14403. "Information for the modeline about the running clock.")
  14404. (defun org-clock-in ()
  14405. "Start the clock on the current item.
  14406. If necessary, clock-out of the currently active clock."
  14407. (interactive)
  14408. (org-clock-out t)
  14409. (let (ts)
  14410. (save-excursion
  14411. (org-back-to-heading t)
  14412. (if (looking-at org-todo-line-regexp)
  14413. (setq org-clock-heading (match-string 3))
  14414. (setq org-clock-heading "???"))
  14415. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  14416. (beginning-of-line 2)
  14417. (while
  14418. (or (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  14419. (not (equal (match-string 1) org-clock-string)))
  14420. (and (looking-at "[ \t]*:PROPERTIES:")
  14421. (not org-insert-labeled-timestamps-before-properties-drawer)))
  14422. ;; Scheduling info, or properties drawer, move one line further
  14423. (beginning-of-line 2)
  14424. (or (bolp) (newline)))
  14425. (insert "\n") (backward-char 1)
  14426. (indent-relative)
  14427. (insert org-clock-string " ")
  14428. (setq org-clock-start-time (current-time))
  14429. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  14430. (move-marker org-clock-marker (point) (buffer-base-buffer))
  14431. (or global-mode-string (setq global-mode-string '("")))
  14432. (or (memq 'org-mode-line-string global-mode-string)
  14433. (setq global-mode-string
  14434. (append global-mode-string '(org-mode-line-string))))
  14435. (org-update-mode-line)
  14436. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  14437. (message "Clock started at %s" ts))))
  14438. (defun org-clock-out (&optional fail-quietly)
  14439. "Stop the currently running clock.
  14440. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  14441. (interactive)
  14442. (catch 'exit
  14443. (if (not (marker-buffer org-clock-marker))
  14444. (if fail-quietly (throw 'exit t) (error "No active clock")))
  14445. (let (ts te s h m)
  14446. (save-excursion
  14447. (set-buffer (marker-buffer org-clock-marker))
  14448. (goto-char org-clock-marker)
  14449. (beginning-of-line 1)
  14450. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  14451. (equal (match-string 1) org-clock-string))
  14452. (setq ts (match-string 2))
  14453. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  14454. (goto-char (match-end 0))
  14455. (delete-region (point) (point-at-eol))
  14456. (insert "--")
  14457. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  14458. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  14459. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  14460. h (floor (/ s 3600))
  14461. s (- s (* 3600 h))
  14462. m (floor (/ s 60))
  14463. s (- s (* 60 s)))
  14464. (insert " => " (format "%2d:%02d" h m))
  14465. (move-marker org-clock-marker nil)
  14466. (org-add-log-maybe 'clock-out)
  14467. (when org-mode-line-timer
  14468. (cancel-timer org-mode-line-timer)
  14469. (setq org-mode-line-timer nil))
  14470. (setq global-mode-string
  14471. (delq 'org-mode-line-string global-mode-string))
  14472. (force-mode-line-update)
  14473. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  14474. (defun org-clock-cancel ()
  14475. "Cancel the running clock be removing the start timestamp."
  14476. (interactive)
  14477. (if (not (marker-buffer org-clock-marker))
  14478. (error "No active clock"))
  14479. (save-excursion
  14480. (set-buffer (marker-buffer org-clock-marker))
  14481. (goto-char org-clock-marker)
  14482. (delete-region (1- (point-at-bol)) (point-at-eol)))
  14483. (message "Clock canceled"))
  14484. (defvar org-clock-file-total-minutes nil
  14485. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  14486. (make-variable-buffer-local 'org-clock-file-total-minutes)
  14487. (defun org-clock-sum (&optional tstart tend)
  14488. "Sum the times for each subtree.
  14489. Puts the resulting times in minutes as a text property on each headline."
  14490. (interactive)
  14491. (let* ((bmp (buffer-modified-p))
  14492. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  14493. org-clock-string
  14494. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  14495. (lmax 30)
  14496. (ltimes (make-vector lmax 0))
  14497. (t1 0)
  14498. (level 0)
  14499. ts te dt
  14500. time)
  14501. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  14502. (save-excursion
  14503. (goto-char (point-max))
  14504. (while (re-search-backward re nil t)
  14505. (cond
  14506. ((match-end 2)
  14507. ;; Two time stamps
  14508. (setq ts (match-string 2)
  14509. te (match-string 3)
  14510. ts (time-to-seconds
  14511. (apply 'encode-time (org-parse-time-string ts)))
  14512. te (time-to-seconds
  14513. (apply 'encode-time (org-parse-time-string te)))
  14514. ts (if tstart (max ts tstart) ts)
  14515. te (if tend (min te tend) te)
  14516. dt (- te ts)
  14517. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  14518. ((match-end 4)
  14519. ;; A naket time
  14520. (setq t1 (+ t1 (string-to-number (match-string 5))
  14521. (* 60 (string-to-number (match-string 4))))))
  14522. (t ;; A headline
  14523. (setq level (- (match-end 1) (match-beginning 1)))
  14524. (when (or (> t1 0) (> (aref ltimes level) 0))
  14525. (loop for l from 0 to level do
  14526. (aset ltimes l (+ (aref ltimes l) t1)))
  14527. (setq t1 0 time (aref ltimes level))
  14528. (loop for l from level to (1- lmax) do
  14529. (aset ltimes l 0))
  14530. (goto-char (match-beginning 0))
  14531. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  14532. (setq org-clock-file-total-minutes (aref ltimes 0)))
  14533. (set-buffer-modified-p bmp)))
  14534. (defun org-clock-display (&optional total-only)
  14535. "Show subtree times in the entire buffer.
  14536. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  14537. in the echo area."
  14538. (interactive)
  14539. (org-remove-clock-overlays)
  14540. (let (time h m p)
  14541. (org-clock-sum)
  14542. (unless total-only
  14543. (save-excursion
  14544. (goto-char (point-min))
  14545. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  14546. (goto-char p)
  14547. (when (setq time (get-text-property p :org-clock-minutes))
  14548. (org-put-clock-overlay time (funcall outline-level))))
  14549. (setq h (/ org-clock-file-total-minutes 60)
  14550. m (- org-clock-file-total-minutes (* 60 h)))
  14551. ;; Arrange to remove the overlays upon next change.
  14552. (when org-remove-highlights-with-change
  14553. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  14554. nil 'local))))
  14555. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  14556. (defvar org-clock-overlays nil)
  14557. (make-variable-buffer-local 'org-clock-overlays)
  14558. (defun org-put-clock-overlay (time &optional level)
  14559. "Put an overlays on the current line, displaying TIME.
  14560. If LEVEL is given, prefix time with a corresponding number of stars.
  14561. This creates a new overlay and stores it in `org-clock-overlays', so that it
  14562. will be easy to remove."
  14563. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  14564. (l (if level (org-get-legal-level level 0) 0))
  14565. (off 0)
  14566. ov tx)
  14567. (move-to-column c)
  14568. (unless (eolp) (skip-chars-backward "^ \t"))
  14569. (skip-chars-backward " \t")
  14570. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  14571. tx (concat (buffer-substring (1- (point)) (point))
  14572. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  14573. (org-add-props (format "%s %2d:%02d%s"
  14574. (make-string l ?*) h m
  14575. (make-string (- 10 l) ?\ ))
  14576. '(face secondary-selection))
  14577. ""))
  14578. (if (not (featurep 'xemacs))
  14579. (org-overlay-put ov 'display tx)
  14580. (org-overlay-put ov 'invisible t)
  14581. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  14582. (push ov org-clock-overlays)))
  14583. (defun org-remove-clock-overlays (&optional beg end noremove)
  14584. "Remove the occur highlights from the buffer.
  14585. BEG and END are ignored. If NOREMOVE is nil, remove this function
  14586. from the `before-change-functions' in the current buffer."
  14587. (interactive)
  14588. (unless org-inhibit-highlight-removal
  14589. (mapc 'org-delete-overlay org-clock-overlays)
  14590. (setq org-clock-overlays nil)
  14591. (unless noremove
  14592. (remove-hook 'before-change-functions
  14593. 'org-remove-clock-overlays 'local))))
  14594. (defun org-clock-out-if-current ()
  14595. "Clock out if the current entry contains the running clock.
  14596. This is used to stop the clock after a TODO entry is marked DONE,
  14597. and is only done if the variable `org-clock-out-when-done' is not nil."
  14598. (when (and org-clock-out-when-done
  14599. (member state org-done-keywords)
  14600. (equal (marker-buffer org-clock-marker) (current-buffer))
  14601. (< (point) org-clock-marker)
  14602. (> (save-excursion (outline-next-heading) (point))
  14603. org-clock-marker))
  14604. ;; Clock out, but don't accept a logging message for this.
  14605. (let ((org-log-done (if (and (listp org-log-done)
  14606. (member 'clock-out org-log-done))
  14607. '(done)
  14608. org-log-done)))
  14609. (org-clock-out))))
  14610. (add-hook 'org-after-todo-state-change-hook
  14611. 'org-clock-out-if-current)
  14612. (defun org-check-running-clock ()
  14613. "Check if the current buffer contains the running clock.
  14614. If yes, offer to stop it and to save the buffer with the changes."
  14615. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  14616. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  14617. (buffer-name))))
  14618. (org-clock-out)
  14619. (when (y-or-n-p "Save changed buffer?")
  14620. (save-buffer))))
  14621. (defun org-clock-report ()
  14622. "Create a table containing a report about clocked time.
  14623. If the buffer contains lines
  14624. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  14625. #+END: clocktable
  14626. then the table will be inserted between these lines, replacing whatever
  14627. is was there before. If these lines are not in the buffer, the table
  14628. is inserted at point, surrounded by the special lines.
  14629. The BEGIN line can contain parameters. Allowed are:
  14630. :maxlevel The maximum level to be included in the table. Default is 3.
  14631. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  14632. (interactive)
  14633. (org-remove-clock-overlays)
  14634. (unless (org-find-dblock "clocktable")
  14635. (org-create-dblock (list :name "clocktable"
  14636. :maxlevel 2 :emphasize nil)))
  14637. (org-update-dblock))
  14638. (defun org-clock-update-time-maybe ()
  14639. "If this is a CLOCK line, update it and return t.
  14640. Otherwise, return nil."
  14641. (interactive)
  14642. (save-excursion
  14643. (beginning-of-line 1)
  14644. (skip-chars-forward " \t")
  14645. (when (looking-at org-clock-string)
  14646. (let ((re (concat "[ \t]*" org-clock-string
  14647. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  14648. "\\([ \t]*=>.*\\)?"))
  14649. ts te h m s)
  14650. (if (not (looking-at re))
  14651. nil
  14652. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  14653. (end-of-line 1)
  14654. (setq ts (match-string 1)
  14655. te (match-string 2))
  14656. (setq s (- (time-to-seconds
  14657. (apply 'encode-time (org-parse-time-string te)))
  14658. (time-to-seconds
  14659. (apply 'encode-time (org-parse-time-string ts))))
  14660. h (floor (/ s 3600))
  14661. s (- s (* 3600 h))
  14662. m (floor (/ s 60))
  14663. s (- s (* 60 s)))
  14664. (insert " => " (format "%2d:%02d" h m))
  14665. t)))))
  14666. (defun org-clock-special-range (key &optional time as-strings)
  14667. "Return two times bordering a special time range.
  14668. Key is a symbol specifying the range and can be one of `today', `yesterday',
  14669. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  14670. A week starts Monday 0:00 and ends Sunday 24:00.
  14671. The range is determined relative to TIME. TIME defaults to the current time.
  14672. The return value is a cons cell with two internal times like the ones
  14673. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  14674. the returned times will be formatted strings."
  14675. (let* ((tm (decode-time (or time (current-time))))
  14676. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  14677. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  14678. (dow (nth 6 tm))
  14679. s1 m1 h1 d1 month1 y1 diff ts te fm)
  14680. (cond
  14681. ((eq key 'today)
  14682. (setq h 0 m 0 h1 24 m1 0))
  14683. ((eq key 'yesterday)
  14684. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  14685. ((eq key 'thisweek)
  14686. (setq diff (if (= dow 0) 6 (1- dow))
  14687. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  14688. ((eq key 'lastweek)
  14689. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  14690. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  14691. ((eq key 'thismonth)
  14692. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  14693. ((eq key 'lastmonth)
  14694. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  14695. ((eq key 'thisyear)
  14696. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  14697. ((eq key 'lastyear)
  14698. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  14699. (t (error "No such time block %s" key)))
  14700. (setq ts (encode-time s m h d month y)
  14701. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  14702. (or d1 d) (or month1 month) (or y1 y)))
  14703. (setq fm (cdr org-time-stamp-formats))
  14704. (if as-strings
  14705. (cons (format-time-string fm ts) (format-time-string fm te))
  14706. (cons ts te))))
  14707. (defun org-dblock-write:clocktable (params)
  14708. "Write the standard clocktable."
  14709. (let ((hlchars '((1 . "*") (2 . ?/)))
  14710. (emph nil)
  14711. (ins (make-marker))
  14712. ipos time h m p level hlc hdl maxlevel
  14713. ts te cc block)
  14714. (setq maxlevel (or (plist-get params :maxlevel) 3)
  14715. emph (plist-get params :emphasize)
  14716. ts (plist-get params :tstart)
  14717. te (plist-get params :tend)
  14718. block (plist-get params :block))
  14719. (when block
  14720. (setq cc (org-clock-special-range block nil t)
  14721. ts (car cc) te (cdr cc)))
  14722. (if ts (setq ts (time-to-seconds
  14723. (apply 'encode-time (org-parse-time-string ts)))))
  14724. (if te (setq te (time-to-seconds
  14725. (apply 'encode-time (org-parse-time-string te)))))
  14726. (move-marker ins (point))
  14727. (setq ipos (point))
  14728. (insert-before-markers "Clock summary at ["
  14729. (substring
  14730. (format-time-string (cdr org-time-stamp-formats))
  14731. 1 -1)
  14732. "]."
  14733. (if block
  14734. (format " Considered range is /%s/." block)
  14735. "")
  14736. "\n\n|L|Headline|Time|\n")
  14737. (org-clock-sum ts te)
  14738. (setq h (/ org-clock-file-total-minutes 60)
  14739. m (- org-clock-file-total-minutes (* 60 h)))
  14740. (insert-before-markers "|-\n|0|" "*Total file time*| "
  14741. (format "*%d:%02d*" h m)
  14742. "|\n")
  14743. (goto-char (point-min))
  14744. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  14745. (goto-char p)
  14746. (when (setq time (get-text-property p :org-clock-minutes))
  14747. (save-excursion
  14748. (beginning-of-line 1)
  14749. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  14750. (setq level (- (match-end 1) (match-beginning 1)))
  14751. (<= level maxlevel))
  14752. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  14753. hdl (match-string 2)
  14754. h (/ time 60)
  14755. m (- time (* 60 h)))
  14756. (goto-char ins)
  14757. (if (= level 1) (insert-before-markers "|-\n"))
  14758. (insert-before-markers
  14759. "| " (int-to-string level) "|" hlc hdl hlc " |"
  14760. (make-string (1- level) ?|)
  14761. hlc
  14762. (format "%d:%02d" h m)
  14763. hlc
  14764. " |\n")))))
  14765. (goto-char ins)
  14766. (backward-delete-char 1)
  14767. (goto-char ipos)
  14768. (skip-chars-forward "^|")
  14769. (org-table-align)))
  14770. ;; FIXME: I don't think anybody uses this, ask David
  14771. (defun org-collect-clock-time-entries ()
  14772. "Return an internal list with clocking information.
  14773. This list has one entry for each CLOCK interval.
  14774. FIXME: describe the elements."
  14775. (interactive)
  14776. (let ((re (concat "^[ \t]*" org-clock-string
  14777. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  14778. rtn beg end next cont level title total closedp leafp
  14779. clockpos titlepos h m donep)
  14780. (save-excursion
  14781. (org-clock-sum)
  14782. (goto-char (point-min))
  14783. (while (re-search-forward re nil t)
  14784. (setq clockpos (match-beginning 0)
  14785. beg (match-string 1) end (match-string 2)
  14786. cont (match-end 0))
  14787. (setq beg (apply 'encode-time (org-parse-time-string beg))
  14788. end (apply 'encode-time (org-parse-time-string end)))
  14789. (org-back-to-heading t)
  14790. (setq donep (org-entry-is-done-p))
  14791. (setq titlepos (point)
  14792. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  14793. h (/ total 60) m (- total (* 60 h))
  14794. total (cons h m))
  14795. (looking-at "\\(\\*+\\) +\\(.*\\)")
  14796. (setq level (- (match-end 1) (match-beginning 1))
  14797. title (org-match-string-no-properties 2))
  14798. (save-excursion (outline-next-heading) (setq next (point)))
  14799. (setq closedp (re-search-forward org-closed-time-regexp next t))
  14800. (goto-char next)
  14801. (setq leafp (and (looking-at "^\\*+ ")
  14802. (<= (- (match-end 0) (point)) level)))
  14803. (push (list beg end clockpos closedp donep
  14804. total title titlepos level leafp)
  14805. rtn)
  14806. (goto-char cont)))
  14807. (nreverse rtn)))
  14808. ;;;; Agenda, and Diary Integration
  14809. ;;; Define the Org-agenda-mode
  14810. (defvar org-agenda-mode-map (make-sparse-keymap)
  14811. "Keymap for `org-agenda-mode'.")
  14812. (defvar org-agenda-menu) ; defined later in this file.
  14813. (defvar org-agenda-follow-mode nil)
  14814. (defvar org-agenda-show-log nil)
  14815. (defvar org-agenda-redo-command nil)
  14816. (defvar org-agenda-mode-hook nil)
  14817. (defvar org-agenda-type nil)
  14818. (defvar org-agenda-force-single-file nil)
  14819. (defun org-agenda-mode ()
  14820. "Mode for time-sorted view on action items in Org-mode files.
  14821. The following commands are available:
  14822. \\{org-agenda-mode-map}"
  14823. (interactive)
  14824. (kill-all-local-variables)
  14825. (setq org-agenda-undo-list nil
  14826. org-agenda-pending-undo-list nil)
  14827. (setq major-mode 'org-agenda-mode)
  14828. ;; Keep global-font-lock-mode from turning on font-lock-mode
  14829. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  14830. (setq mode-name "Org-Agenda")
  14831. (use-local-map org-agenda-mode-map)
  14832. (easy-menu-add org-agenda-menu)
  14833. (if org-startup-truncated (setq truncate-lines t))
  14834. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  14835. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  14836. ;; Make sure properties are removed when copying text
  14837. (when (boundp 'buffer-substring-filters)
  14838. (org-set-local 'buffer-substring-filters
  14839. (cons (lambda (x)
  14840. (set-text-properties 0 (length x) nil x) x)
  14841. buffer-substring-filters)))
  14842. (unless org-agenda-keep-modes
  14843. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  14844. org-agenda-show-log nil))
  14845. (easy-menu-change
  14846. '("Agenda") "Agenda Files"
  14847. (append
  14848. (list
  14849. (vector
  14850. (if (get 'org-agenda-files 'org-restrict)
  14851. "Restricted to single file"
  14852. "Edit File List")
  14853. '(org-edit-agenda-file-list)
  14854. (not (get 'org-agenda-files 'org-restrict)))
  14855. "--")
  14856. (mapcar 'org-file-menu-entry (org-agenda-files))))
  14857. (org-agenda-set-mode-name)
  14858. (apply
  14859. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  14860. (list 'org-agenda-mode-hook)))
  14861. (substitute-key-definition 'undo 'org-agenda-undo
  14862. org-agenda-mode-map global-map)
  14863. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  14864. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  14865. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  14866. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  14867. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  14868. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  14869. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  14870. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  14871. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  14872. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  14873. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  14874. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  14875. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  14876. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  14877. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  14878. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  14879. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  14880. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  14881. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  14882. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  14883. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  14884. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  14885. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  14886. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  14887. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  14888. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  14889. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  14890. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  14891. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  14892. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  14893. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  14894. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  14895. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  14896. (while l (org-defkey org-agenda-mode-map
  14897. (int-to-string (pop l)) 'digit-argument)))
  14898. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  14899. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  14900. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  14901. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  14902. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  14903. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  14904. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  14905. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  14906. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  14907. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  14908. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  14909. (org-defkey org-agenda-mode-map "n" 'next-line)
  14910. (org-defkey org-agenda-mode-map "p" 'previous-line)
  14911. (org-defkey org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  14912. (org-defkey org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  14913. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  14914. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  14915. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  14916. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  14917. (eval-after-load "calendar"
  14918. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  14919. 'org-calendar-goto-agenda))
  14920. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  14921. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  14922. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  14923. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  14924. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  14925. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  14926. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  14927. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  14928. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  14929. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  14930. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  14931. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  14932. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  14933. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  14934. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  14935. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  14936. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  14937. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  14938. "Local keymap for agenda entries from Org-mode.")
  14939. (org-defkey org-agenda-keymap
  14940. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  14941. (org-defkey org-agenda-keymap
  14942. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  14943. (when org-agenda-mouse-1-follows-link
  14944. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  14945. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  14946. '("Agenda"
  14947. ("Agenda Files")
  14948. "--"
  14949. ["Show" org-agenda-show t]
  14950. ["Go To (other window)" org-agenda-goto t]
  14951. ["Go To (this window)" org-agenda-switch-to t]
  14952. ["Follow Mode" org-agenda-follow-mode
  14953. :style toggle :selected org-agenda-follow-mode :active t]
  14954. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  14955. "--"
  14956. ["Cycle TODO" org-agenda-todo t]
  14957. ["Archive subtree" org-agenda-archive t]
  14958. ["Delete subtree" org-agenda-kill t]
  14959. "--"
  14960. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  14961. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  14962. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  14963. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  14964. "--"
  14965. ("Tags and Properties"
  14966. ["Show all Tags" org-agenda-show-tags t]
  14967. ["Set Tags" org-agenda-set-tags t]
  14968. "--"
  14969. ["Column View" org-columns t])
  14970. ("Date/Schedule"
  14971. ["Schedule" org-agenda-schedule t]
  14972. ["Set Deadline" org-agenda-deadline t]
  14973. "--"
  14974. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  14975. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  14976. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  14977. ("Priority"
  14978. ["Set Priority" org-agenda-priority t]
  14979. ["Increase Priority" org-agenda-priority-up t]
  14980. ["Decrease Priority" org-agenda-priority-down t]
  14981. ["Show Priority" org-agenda-show-priority t])
  14982. ("Calendar/Diary"
  14983. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  14984. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  14985. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  14986. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  14987. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  14988. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  14989. "--"
  14990. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  14991. "--"
  14992. ("View"
  14993. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  14994. :style radio :selected (equal org-agenda-ndays 1)]
  14995. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  14996. :style radio :selected (equal org-agenda-ndays 7)]
  14997. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  14998. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  14999. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  15000. :style radio :selected (member org-agenda-ndays '(365 366))]
  15001. "--"
  15002. ["Show Logbook entries" org-agenda-log-mode
  15003. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  15004. ["Include Diary" org-agenda-toggle-diary
  15005. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  15006. ["Use Time Grid" org-agenda-toggle-time-grid
  15007. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  15008. ["Write view to file" org-write-agenda t]
  15009. ["Rebuild buffer" org-agenda-redo t]
  15010. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  15011. "--"
  15012. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  15013. "--"
  15014. ["Quit" org-agenda-quit t]
  15015. ["Exit and Release Buffers" org-agenda-exit t]
  15016. ))
  15017. ;;; Agenda undo
  15018. (defvar org-agenda-allow-remote-undo t
  15019. "Non-nil means, allow remote undo from the agenda buffer.")
  15020. (defvar org-agenda-undo-list nil
  15021. "List of undoable operations in the agenda since last refresh.")
  15022. (defvar org-agenda-undo-has-started-in nil
  15023. "Buffers that have already seen `undo-start' in the current undo sequence.")
  15024. (defvar org-agenda-pending-undo-list nil
  15025. "In a series of undo commands, this is the list of remaning undo items.")
  15026. (defmacro org-if-unprotected (&rest body)
  15027. "Execute BODY if there is no `org-protected' text property at point."
  15028. (declare (debug t))
  15029. `(unless (get-text-property (point) 'org-protected)
  15030. ,@body))
  15031. (defmacro org-with-remote-undo (_buffer &rest _body)
  15032. "Execute BODY while recording undo information in two buffers."
  15033. (declare (indent 1) (debug t))
  15034. `(let ((_cline (org-current-line))
  15035. (_cmd this-command)
  15036. (_buf1 (current-buffer))
  15037. (_buf2 ,_buffer)
  15038. (_undo1 buffer-undo-list)
  15039. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  15040. _c1 _c2)
  15041. ,@_body
  15042. (when org-agenda-allow-remote-undo
  15043. (setq _c1 (org-verify-change-for-undo
  15044. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  15045. _c2 (org-verify-change-for-undo
  15046. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  15047. (when (or _c1 _c2)
  15048. ;; make sure there are undo boundaries
  15049. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  15050. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  15051. ;; remember which buffer to undo
  15052. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  15053. org-agenda-undo-list)))))
  15054. (defun org-agenda-undo ()
  15055. "Undo a remote editing step in the agenda.
  15056. This undoes changes both in the agenda buffer and in the remote buffer
  15057. that have been changed along."
  15058. (interactive)
  15059. (or org-agenda-allow-remote-undo
  15060. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  15061. (if (not (eq this-command last-command))
  15062. (setq org-agenda-undo-has-started-in nil
  15063. org-agenda-pending-undo-list org-agenda-undo-list))
  15064. (if (not org-agenda-pending-undo-list)
  15065. (error "No further undo information"))
  15066. (let* ((entry (pop org-agenda-pending-undo-list))
  15067. buf line cmd rembuf)
  15068. (setq cmd (pop entry) line (pop entry))
  15069. (setq rembuf (nth 2 entry))
  15070. (org-with-remote-undo rembuf
  15071. (while (bufferp (setq buf (pop entry)))
  15072. (if (pop entry)
  15073. (with-current-buffer buf
  15074. (let ((last-undo-buffer buf)
  15075. (inhibit-read-only t))
  15076. (unless (memq buf org-agenda-undo-has-started-in)
  15077. (push buf org-agenda-undo-has-started-in)
  15078. (make-local-variable 'pending-undo-list)
  15079. (undo-start))
  15080. (while (and pending-undo-list
  15081. (listp pending-undo-list)
  15082. (not (car pending-undo-list)))
  15083. (pop pending-undo-list))
  15084. (undo-more 1))))))
  15085. (goto-line line)
  15086. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  15087. (defun org-verify-change-for-undo (l1 l2)
  15088. "Verify that a real change occurred between the undo lists L1 and L2."
  15089. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  15090. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  15091. (not (eq l1 l2)))
  15092. ;;; Agenda dispatch
  15093. (defvar org-agenda-restrict nil)
  15094. (defvar org-agenda-restrict-begin (make-marker))
  15095. (defvar org-agenda-restrict-end (make-marker))
  15096. (defvar org-agenda-last-dispatch-buffer nil)
  15097. ;;;###autoload
  15098. (defun org-agenda (arg)
  15099. "Dispatch agenda commands to collect entries to the agenda buffer.
  15100. Prompts for a character to select a command. Any prefix arg will be passed
  15101. on to the selected command. The default selections are:
  15102. g
  15103. a Call `org-agenda-list' to display the agenda for current day or week.
  15104. t Call `org-todo-list' to display the global todo list.
  15105. T Call `org-todo-list' to display the global todo list, select only
  15106. entries with a specific TODO keyword (the user gets a prompt).
  15107. m Call `org-tags-view' to display headlines with tags matching
  15108. a condition (the user is prompted for the condition).
  15109. M Like `m', but select only TODO entries, no ordinary headlines.
  15110. L Create a timeline for the current buffer.
  15111. e Export views to associated files.
  15112. More commands can be added by configuring the variable
  15113. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  15114. searches can be pre-defined in this way.
  15115. If the current buffer is in Org-mode and visiting a file, you can also
  15116. first press `1' to indicate that the agenda should be temporarily (until the
  15117. next use of \\[org-agenda]) restricted to the current file."
  15118. (interactive "P")
  15119. (catch 'exit
  15120. (let* ((buf (current-buffer))
  15121. (bfn (buffer-file-name (buffer-base-buffer)))
  15122. (restrict-ok (and bfn (org-mode-p)))
  15123. (custom org-agenda-custom-commands)
  15124. c entry key type match lprops)
  15125. ;; Turn off restriction
  15126. (put 'org-agenda-files 'org-restrict nil)
  15127. (setq org-agenda-restrict nil)
  15128. (move-marker org-agenda-restrict-begin nil)
  15129. (move-marker org-agenda-restrict-end nil)
  15130. ;; Delete old local properties
  15131. (put 'org-agenda-redo-command 'org-lprops nil)
  15132. ;; Remember where this call originated
  15133. (setq org-agenda-last-dispatch-buffer (current-buffer))
  15134. (save-window-excursion
  15135. (delete-other-windows)
  15136. (switch-to-buffer-other-window " *Agenda Commands*")
  15137. (erase-buffer)
  15138. (insert (eval-when-compile
  15139. (let ((header
  15140. "Press key for an agenda command:
  15141. -------------------------------- C Configure custom agenda commands
  15142. a Agenda for current week or day e Export agenda views
  15143. t List of all TODO entries T Entries with special TODO kwd
  15144. m Match a TAGS query M Like m, but only TODO entries
  15145. L Timeline for current buffer # List stuck projects (!=configure)
  15146. ")
  15147. (start 0))
  15148. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  15149. (setq start (match-end 0))
  15150. (add-text-properties (match-beginning 2) (match-end 2)
  15151. '(face bold) header))
  15152. header)))
  15153. (while (setq entry (pop custom))
  15154. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  15155. (insert (format "\n%-4s%-14s: %s"
  15156. (org-add-props (copy-sequence key)
  15157. '(face bold))
  15158. (cond
  15159. ((stringp type) type)
  15160. ((eq type 'agenda) "Agenda for current week or day")
  15161. ((eq type 'alltodo) "List of all TODO entries")
  15162. ((eq type 'stuck) "List of stuck projects")
  15163. ((eq type 'todo) "TODO keyword")
  15164. ((eq type 'tags) "Tags query")
  15165. ((eq type 'tags-todo) "Tags (TODO)")
  15166. ((eq type 'tags-tree) "Tags tree")
  15167. ((eq type 'todo-tree) "TODO kwd tree")
  15168. ((eq type 'occur-tree) "Occur tree")
  15169. ((functionp type) (symbol-name type))
  15170. (t "???"))
  15171. (if (stringp match)
  15172. (org-add-props match nil 'face 'org-warning)
  15173. (format "set of %d commands" (length match))))))
  15174. (if restrict-ok
  15175. (insert "\n"
  15176. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  15177. (goto-char (point-min))
  15178. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  15179. (message "Press key for agenda command%s"
  15180. (if restrict-ok ", or [1] or [0] to restrict" ""))
  15181. (setq c (read-char-exclusive))
  15182. (message "")
  15183. (when (memq c '(?L ?1 ?0))
  15184. (if restrict-ok
  15185. (put 'org-agenda-files 'org-restrict (list bfn))
  15186. (error "Cannot restrict agenda to current buffer"))
  15187. (with-current-buffer " *Agenda Commands*"
  15188. (goto-char (point-max))
  15189. (delete-region (point-at-bol) (point))
  15190. (goto-char (point-min)))
  15191. (when (eq c ?0)
  15192. (setq org-agenda-restrict t)
  15193. (with-current-buffer buf
  15194. (if (org-region-active-p)
  15195. (progn
  15196. (move-marker org-agenda-restrict-begin (region-beginning))
  15197. (move-marker org-agenda-restrict-end (region-end)))
  15198. (save-excursion
  15199. (org-back-to-heading t)
  15200. (move-marker org-agenda-restrict-begin (point))
  15201. (move-marker org-agenda-restrict-end
  15202. (progn (org-end-of-subtree t)))))))
  15203. (unless (eq c ?L)
  15204. (message "Press key for agenda command%s"
  15205. (if restrict-ok " (restricted to current file)" ""))
  15206. (setq c (read-char-exclusive)))
  15207. (message "")))
  15208. (require 'calendar) ; FIXME: can we avoid this for some commands?
  15209. ;; For example the todo list should not need it (but does...)
  15210. (cond
  15211. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  15212. (if (symbolp (nth 1 entry))
  15213. (progn
  15214. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  15215. lprops (nth 3 entry))
  15216. (put 'org-agenda-redo-command 'org-lprops lprops)
  15217. (cond
  15218. ((eq type 'agenda)
  15219. (org-let lprops '(org-agenda-list current-prefix-arg)))
  15220. ((eq type 'alltodo)
  15221. (org-let lprops '(org-todo-list current-prefix-arg)))
  15222. ((eq type 'stuck)
  15223. (org-let lprops '(org-agenda-list-stuck-projects
  15224. current-prefix-arg)))
  15225. ((eq type 'tags)
  15226. (org-let lprops '(org-tags-view current-prefix-arg match)))
  15227. ((eq type 'tags-todo)
  15228. (org-let lprops '(org-tags-view '(4) match)))
  15229. ((eq type 'todo)
  15230. (org-let lprops '(org-todo-list match)))
  15231. ((eq type 'tags-tree)
  15232. (org-check-for-org-mode)
  15233. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  15234. ((eq type 'todo-tree)
  15235. (org-check-for-org-mode)
  15236. (org-let lprops
  15237. '(org-occur (concat "^" outline-regexp "[ \t]*"
  15238. (regexp-quote match) "\\>"))))
  15239. ((eq type 'occur-tree)
  15240. (org-check-for-org-mode)
  15241. (org-let lprops '(org-occur match)))
  15242. ((fboundp type)
  15243. (org-let lprops '(funcall type match)))
  15244. (t (error "Invalid custom agenda command type %s" type))))
  15245. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  15246. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  15247. ((equal c ?a) (call-interactively 'org-agenda-list))
  15248. ((equal c ?t) (call-interactively 'org-todo-list))
  15249. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  15250. ((equal c ?m) (call-interactively 'org-tags-view))
  15251. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  15252. ((equal c ?e) (call-interactively 'org-store-agenda-views))
  15253. ((equal c ?L)
  15254. (unless restrict-ok
  15255. (error "This is not an Org-mode file"))
  15256. (org-call-with-arg 'org-timeline arg))
  15257. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  15258. ((equal c ?!) (customize-variable 'org-stuck-projects))
  15259. (t (error "Invalid key"))))))
  15260. (defun org-run-agenda-series (name series)
  15261. (org-prepare-agenda name)
  15262. (let* ((org-agenda-multi t)
  15263. (redo (list 'org-run-agenda-series name (list 'quote series)))
  15264. (cmds (car series))
  15265. (gprops (nth 1 series))
  15266. match ;; The byte compiler incorrectly complains about this. Keep it!
  15267. cmd type lprops)
  15268. (while (setq cmd (pop cmds))
  15269. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  15270. (cond
  15271. ((eq type 'agenda)
  15272. (org-let2 gprops lprops
  15273. '(call-interactively 'org-agenda-list)))
  15274. ((eq type 'alltodo)
  15275. (org-let2 gprops lprops
  15276. '(call-interactively 'org-todo-list)))
  15277. ((eq type 'stuck)
  15278. (org-let2 gprops lprops
  15279. '(call-interactively 'org-agenda-list-stuck-projects)))
  15280. ((eq type 'tags)
  15281. (org-let2 gprops lprops
  15282. '(org-tags-view current-prefix-arg match)))
  15283. ((eq type 'tags-todo)
  15284. (org-let2 gprops lprops
  15285. '(org-tags-view '(4) match)))
  15286. ((eq type 'todo)
  15287. (org-let2 gprops lprops
  15288. '(org-todo-list match)))
  15289. ((fboundp type)
  15290. (org-let2 gprops lprops
  15291. '(funcall type match)))
  15292. (t (error "Invalid type in command series"))))
  15293. (widen)
  15294. (setq org-agenda-redo-command redo)
  15295. (goto-char (point-min)))
  15296. (org-finalize-agenda))
  15297. ;;;###autoload
  15298. (defmacro org-batch-agenda (cmd-key &rest parameters)
  15299. "Run an agenda command in batch mode and send the result to STDOUT.
  15300. If CMD-KEY is a string of length 1, it is used as a key in
  15301. `org-agenda-custom-commands' and triggers this command. If it is a
  15302. longer string is is used as a tags/todo match string.
  15303. Paramters are alternating variable names and values that will be bound
  15304. before running the agenda command."
  15305. (let (pars)
  15306. (while parameters
  15307. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15308. (if (> (length cmd-key) 1)
  15309. (eval (list 'let (nreverse pars)
  15310. (list 'org-tags-view nil cmd-key)))
  15311. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  15312. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  15313. (set-buffer "*Org Agenda*")
  15314. (princ (org-encode-for-stdout (buffer-string)))))
  15315. (defun org-encode-for-stdout (string)
  15316. (if (fboundp 'encode-coding-string)
  15317. (encode-coding-string string buffer-file-coding-system)
  15318. string))
  15319. (defvar org-agenda-info nil)
  15320. ;;;###autoload
  15321. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  15322. "Run an agenda command in batch mode and send the result to STDOUT.
  15323. If CMD-KEY is a string of length 1, it is used as a key in
  15324. `org-agenda-custom-commands' and triggers this command. If it is a
  15325. longer string is is used as a tags/todo match string.
  15326. Paramters are alternating variable names and values that will be bound
  15327. before running the agenda command.
  15328. The output gives a line for each selected agenda item. Each
  15329. item is a list of comma-separated values, like this:
  15330. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  15331. category The category of the item
  15332. head The headline, without TODO kwd, TAGS and PRIORITY
  15333. type The type of the agenda entry, can be
  15334. todo selected in TODO match
  15335. tagsmatch selected in tags match
  15336. diary imported from diary
  15337. deadline a deadline on given date
  15338. scheduled scheduled on given date
  15339. timestamp entry has timestamp on given date
  15340. closed entry was closed on given date
  15341. upcoming-deadline warning about deadline
  15342. past-scheduled forwarded scheduled item
  15343. block entry has date block including g. date
  15344. todo The todo keyword, if any
  15345. tags All tags including inherited ones, separated by colons
  15346. date The relevant date, like 2007-2-14
  15347. time The time, like 15:00-16:50
  15348. extra Sting with extra planning info
  15349. priority-l The priority letter if any was given
  15350. priority-n The computed numerical priority
  15351. agenda-day The day in the agenda where this is listed"
  15352. (let (pars)
  15353. (while parameters
  15354. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15355. (push (list 'org-agenda-remove-tags t) pars)
  15356. (if (> (length cmd-key) 1)
  15357. (eval (list 'let (nreverse pars)
  15358. (list 'org-tags-view nil cmd-key)))
  15359. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  15360. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  15361. (set-buffer "*Org Agenda*")
  15362. (let* ((lines (org-split-string (buffer-string) "\n"))
  15363. line)
  15364. (while (setq line (pop lines))
  15365. (catch 'next
  15366. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  15367. (setq org-agenda-info
  15368. (org-fix-agenda-info (text-properties-at 0 line)))
  15369. (princ
  15370. (org-encode-for-stdout
  15371. (mapconcat 'org-agenda-export-csv-mapper
  15372. '(org-category txt type todo tags date time-of-day extra
  15373. priority-letter priority agenda-day)
  15374. ",")))
  15375. (princ "\n"))))))
  15376. (defun org-fix-agenda-info (props)
  15377. "FIXME"
  15378. (let (tmp re)
  15379. (when (setq tmp (plist-get props 'tags))
  15380. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  15381. (when (setq tmp (plist-get props 'date))
  15382. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  15383. (let ((calendar-date-display-form '(year "-" month "-" day)))
  15384. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  15385. (setq tmp (calendar-date-string tmp)))
  15386. (setq props (plist-put props 'date tmp)))
  15387. (when (setq tmp (plist-get props 'day))
  15388. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  15389. (let ((calendar-date-display-form '(year "-" month "-" day)))
  15390. (setq tmp (calendar-date-string tmp)))
  15391. (setq props (plist-put props 'day tmp))
  15392. (setq props (plist-put props 'agenda-day tmp)))
  15393. (when (setq tmp (plist-get props 'txt))
  15394. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  15395. (plist-put props 'priority-letter (match-string 1 tmp))
  15396. (setq tmp (replace-match "" t t tmp)))
  15397. (when (and (setq re (plist-get props 'org-todo-regexp))
  15398. (setq re (concat "\\`\\.*" re " ?"))
  15399. (string-match re tmp))
  15400. (plist-put props 'todo (match-string 1 tmp))
  15401. (setq tmp (replace-match "" t t tmp)))
  15402. (plist-put props 'txt tmp)))
  15403. props)
  15404. (defun org-agenda-export-csv-mapper (prop)
  15405. (let ((res (plist-get org-agenda-info prop)))
  15406. (setq res
  15407. (cond
  15408. ((not res) "")
  15409. ((stringp res) res)
  15410. (t (prin1-to-string res))))
  15411. (while (string-match "," res)
  15412. (setq res (replace-match ";" t t res)))
  15413. (org-trim res)))
  15414. ;;;###autoload
  15415. (defun org-store-agenda-views (&rest parameters)
  15416. (interactive)
  15417. (eval (list 'org-batch-store-agenda-views)))
  15418. (defvar org-agenda-buffer-name)
  15419. ;; FIXME, why is this a macro?????
  15420. ;;;###autoload
  15421. (defmacro org-batch-store-agenda-views (&rest parameters)
  15422. "Run all custom agenda commands that have a file argument."
  15423. (let ((cmds org-agenda-custom-commands)
  15424. (dir default-directory)
  15425. pars cmd thiscmdkey files opts)
  15426. (while parameters
  15427. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15428. (setq pars (reverse pars))
  15429. (save-window-excursion
  15430. (while cmds
  15431. (setq cmd (pop cmds)
  15432. thiscmdkey (car cmd)
  15433. opts (nth 3 cmd)
  15434. files (org-last cmd))
  15435. (if (stringp files) (setq files (list files)))
  15436. (when files
  15437. (flet ((read-char-exclusive () (string-to-char thiscmdkey)))
  15438. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  15439. '(org-agenda nil))))
  15440. (set-buffer "*Org Agenda*")
  15441. (while files
  15442. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  15443. (list 'org-write-agenda
  15444. (expand-file-name (pop files) dir) t)))))
  15445. (kill-buffer org-agenda-buffer-name)))))
  15446. (defun org-write-agenda (file &optional nosettings)
  15447. "Write the current buffer (an agenda view) as a file.
  15448. Depending on the extension of the file name, plain text (.txt),
  15449. HTML (.html or .htm) or Postscript (.ps) is produced.
  15450. If NOSETTINGS is given, do not scope the settings of
  15451. `org-agenda-exporter-settings' into the export commands. This is used when
  15452. the settings have already been scoped and we do not wish to overrule other,
  15453. higher priority settings."
  15454. (interactive "FWrite agenda to file: ")
  15455. (if (not (file-writable-p file))
  15456. (error "Cannot write agenda to file %s" file))
  15457. (cond
  15458. ((string-match "\\.html?\\'" file) (require 'htmlize))
  15459. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  15460. (org-let (if nosettings nil org-agenda-exporter-settings)
  15461. '(save-excursion
  15462. (save-window-excursion
  15463. (cond
  15464. ((string-match "\\.html?\\'" file)
  15465. (set-buffer (htmlize-buffer (current-buffer)))
  15466. (when (and org-agenda-export-html-style
  15467. (string-match "<style>" org-agenda-export-html-style))
  15468. ;; replace <style> section with org-agenda-export-html-style
  15469. (goto-char (point-min))
  15470. (kill-region (- (search-forward "<style") 6)
  15471. (search-forward "</style>"))
  15472. (insert org-agenda-export-html-style))
  15473. (write-file file)
  15474. (kill-buffer (current-buffer))
  15475. (message "HTML written to %s" file))
  15476. ((string-match "\\.ps\\'" file)
  15477. (ps-print-buffer-with-faces file)
  15478. (message "Postscript written to %s" file))
  15479. (t
  15480. (let ((bs (buffer-string)))
  15481. (find-file file)
  15482. (insert bs)
  15483. (save-buffer 0)
  15484. (kill-buffer (current-buffer))
  15485. (message "Plain text written to %s" file))))))
  15486. (set-buffer org-agenda-buffer-name)))
  15487. (defmacro org-no-read-only (&rest body)
  15488. "Inhibit read-only for BODY."
  15489. `(let ((inhibit-read-only t)) ,@body))
  15490. (defun org-check-for-org-mode ()
  15491. "Make sure current buffer is in org-mode. Error if not."
  15492. (or (org-mode-p)
  15493. (error "Cannot execute org-mode agenda command on buffer in %s."
  15494. major-mode)))
  15495. (defun org-fit-agenda-window ()
  15496. "Fit the window to the buffer size."
  15497. (and (memq org-agenda-window-setup '(reorganize-frame))
  15498. (fboundp 'fit-window-to-buffer)
  15499. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  15500. (/ (frame-height) 2))))
  15501. ;;; Agenda file list
  15502. (defun org-agenda-files (&optional unrestricted)
  15503. "Get the list of agenda files.
  15504. Optional UNRESTRICTED means return the full list even if a restriction
  15505. is currently in place."
  15506. (cond
  15507. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15508. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15509. ((listp org-agenda-files) org-agenda-files)
  15510. (t (error "Invalid value of `org-agenda-files'"))))
  15511. (defun org-edit-agenda-file-list ()
  15512. "Edit the list of agenda files.
  15513. Depending on setup, this either uses customize to edit the variable
  15514. `org-agenda-files', or it visits the file that is holding the list. In the
  15515. latter case, the buffer is set up in a way that saving it automatically kills
  15516. the buffer and restores the previous window configuration."
  15517. (interactive)
  15518. (if (stringp org-agenda-files)
  15519. (let ((cw (current-window-configuration)))
  15520. (find-file org-agenda-files)
  15521. (org-set-local 'org-window-configuration cw)
  15522. (org-add-hook 'after-save-hook
  15523. (lambda ()
  15524. (set-window-configuration
  15525. (prog1 org-window-configuration
  15526. (kill-buffer (current-buffer))))
  15527. (org-install-agenda-files-menu)
  15528. (message "New agenda file list installed"))
  15529. nil 'local)
  15530. (message (substitute-command-keys
  15531. "Edit list and finish with \\[save-buffer]")))
  15532. (customize-variable 'org-agenda-files)))
  15533. (defun org-store-new-agenda-file-list (list)
  15534. "Set new value for the agenda file list and save it correcly."
  15535. (if (stringp org-agenda-files)
  15536. (let ((f org-agenda-files) b)
  15537. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  15538. (with-temp-file f
  15539. (insert (mapconcat 'identity list "\n") "\n")))
  15540. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  15541. (setq org-agenda-files list)
  15542. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15543. (defun org-read-agenda-file-list ()
  15544. "Read the list of agenda files from a file."
  15545. (when (stringp org-agenda-files)
  15546. (with-temp-buffer
  15547. (insert-file-contents org-agenda-files)
  15548. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  15549. ;;;###autoload
  15550. (defun org-cycle-agenda-files ()
  15551. "Cycle through the files in `org-agenda-files'.
  15552. If the current buffer visits an agenda file, find the next one in the list.
  15553. If the current buffer does not, find the first agenda file."
  15554. (interactive)
  15555. (let* ((fs (org-agenda-files t))
  15556. (files (append fs (list (car fs))))
  15557. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15558. file)
  15559. (unless files (error "No agenda files"))
  15560. (catch 'exit
  15561. (while (setq file (pop files))
  15562. (if (equal (file-truename file) tcf)
  15563. (when (car files)
  15564. (find-file (car files))
  15565. (throw 'exit t))))
  15566. (find-file (car fs)))
  15567. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  15568. (defun org-agenda-file-to-front (&optional to-end)
  15569. "Move/add the current file to the top of the agenda file list.
  15570. If the file is not present in the list, it is added to the front. If it is
  15571. present, it is moved there. With optional argument TO-END, add/move to the
  15572. end of the list."
  15573. (interactive "P")
  15574. (let ((file-alist (mapcar (lambda (x)
  15575. (cons (file-truename x) x))
  15576. (org-agenda-files t)))
  15577. (ctf (file-truename buffer-file-name))
  15578. x had)
  15579. (setq x (assoc ctf file-alist) had x)
  15580. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15581. (if to-end
  15582. (setq file-alist (append (delq x file-alist) (list x)))
  15583. (setq file-alist (cons x (delq x file-alist))))
  15584. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15585. (org-install-agenda-files-menu)
  15586. (message "File %s to %s of agenda file list"
  15587. (if had "moved" "added") (if to-end "end" "front"))))
  15588. (defun org-remove-file (&optional file)
  15589. "Remove current file from the list of files in variable `org-agenda-files'.
  15590. These are the files which are being checked for agenda entries.
  15591. Optional argument FILE means, use this file instead of the current."
  15592. (interactive)
  15593. (let* ((file (or file buffer-file-name))
  15594. (true-file (file-truename file))
  15595. (afile (abbreviate-file-name file))
  15596. (files (delq nil (mapcar
  15597. (lambda (x)
  15598. (if (equal true-file
  15599. (file-truename x))
  15600. nil x))
  15601. (org-agenda-files t)))))
  15602. (if (not (= (length files) (length (org-agenda-files t))))
  15603. (progn
  15604. (org-store-new-agenda-file-list files)
  15605. (org-install-agenda-files-menu)
  15606. (message "Removed file: %s" afile))
  15607. (message "File was not in list: %s" afile))))
  15608. (defun org-file-menu-entry (file)
  15609. (vector file (list 'find-file file) t))
  15610. (defun org-check-agenda-file (file)
  15611. "Make sure FILE exists. If not, ask user what to do."
  15612. (when (not (file-exists-p file))
  15613. (message "non-existent file %s. [R]emove from list or [A]bort?"
  15614. (abbreviate-file-name file))
  15615. (let ((r (downcase (read-char-exclusive))))
  15616. (cond
  15617. ((equal r ?r)
  15618. (org-remove-file file)
  15619. (throw 'nextfile t))
  15620. (t (error "Abort"))))))
  15621. ;;; Agenda prepare and finalize
  15622. (defvar org-agenda-multi nil) ; dynammically scoped
  15623. (defvar org-agenda-buffer-name "*Org Agenda*")
  15624. (defvar org-pre-agenda-window-conf nil)
  15625. (defvar org-agenda-name nil)
  15626. (defun org-prepare-agenda (&optional name)
  15627. (setq org-todo-keywords-for-agenda nil)
  15628. (setq org-done-keywords-for-agenda nil)
  15629. (if org-agenda-multi
  15630. (progn
  15631. (setq buffer-read-only nil)
  15632. (goto-char (point-max))
  15633. (unless (bobp)
  15634. (insert "\n" (make-string (window-width) ?=) "\n"))
  15635. (narrow-to-region (point) (point-max)))
  15636. (org-agenda-maybe-reset-markers 'force)
  15637. (org-prepare-agenda-buffers (org-agenda-files))
  15638. (setq org-todo-keywords-for-agenda
  15639. (org-uniquify org-todo-keywords-for-agenda))
  15640. (setq org-done-keywords-for-agenda
  15641. (org-uniquify org-done-keywords-for-agenda))
  15642. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  15643. (awin (get-buffer-window abuf)))
  15644. (cond
  15645. ((equal (current-buffer) abuf) nil)
  15646. (awin (select-window awin))
  15647. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  15648. ((equal org-agenda-window-setup 'current-window)
  15649. (switch-to-buffer abuf))
  15650. ((equal org-agenda-window-setup 'other-window)
  15651. (switch-to-buffer-other-window abuf))
  15652. ((equal org-agenda-window-setup 'other-frame)
  15653. (switch-to-buffer-other-frame abuf))
  15654. ((equal org-agenda-window-setup 'reorganize-frame)
  15655. (delete-other-windows)
  15656. (switch-to-buffer-other-window abuf))))
  15657. (setq buffer-read-only nil)
  15658. (erase-buffer)
  15659. (org-agenda-mode)
  15660. (and name (not org-agenda-name)
  15661. (org-set-local 'org-agenda-name name)))
  15662. (setq buffer-read-only nil))
  15663. (defun org-finalize-agenda ()
  15664. "Finishing touch for the agenda buffer, called just before displaying it."
  15665. (unless org-agenda-multi
  15666. (save-excursion
  15667. (let ((inhibit-read-only t))
  15668. (goto-char (point-min))
  15669. (while (org-activate-bracket-links (point-max))
  15670. (add-text-properties (match-beginning 0) (match-end 0)
  15671. '(face org-link)))
  15672. (org-agenda-align-tags)
  15673. (unless org-agenda-with-colors
  15674. (remove-text-properties (point-min) (point-max) '(face nil))))
  15675. (if (and (boundp 'org-overriding-columns-format)
  15676. org-overriding-columns-format)
  15677. (org-set-local 'org-overriding-columns-format
  15678. org-overriding-columns-format))
  15679. (if (and (boundp 'org-agenda-view-columns-initially)
  15680. org-agenda-view-columns-initially)
  15681. (org-agenda-columns))
  15682. (run-hooks 'org-finalize-agenda-hook))))
  15683. (defun org-prepare-agenda-buffers (files)
  15684. "Create buffers for all agenda files, protect archived trees and comments."
  15685. (interactive)
  15686. (let ((pa '(:org-archived t))
  15687. (pc '(:org-comment t))
  15688. (pall '(:org-archived t :org-comment t))
  15689. (inhibit-read-only t)
  15690. (rea (concat ":" org-archive-tag ":"))
  15691. bmp file re)
  15692. (save-excursion
  15693. (save-restriction
  15694. (while (setq file (pop files))
  15695. (org-check-agenda-file file)
  15696. (set-buffer (org-get-agenda-file-buffer file))
  15697. (widen)
  15698. (setq bmp (buffer-modified-p))
  15699. (setq org-todo-keywords-for-agenda
  15700. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15701. (setq org-done-keywords-for-agenda
  15702. (append org-done-keywords-for-agenda org-done-keywords))
  15703. (save-excursion
  15704. (remove-text-properties (point-min) (point-max) pall)
  15705. (when org-agenda-skip-archived-trees
  15706. (goto-char (point-min))
  15707. (while (re-search-forward rea nil t)
  15708. (if (org-on-heading-p t)
  15709. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15710. (goto-char (point-min))
  15711. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  15712. (while (re-search-forward re nil t)
  15713. (add-text-properties
  15714. (match-beginning 0) (org-end-of-subtree t) pc)))
  15715. (set-buffer-modified-p bmp))))))
  15716. (defvar org-agenda-skip-function nil
  15717. "Function to be called at each match during agenda construction.
  15718. If this function returns nil, the current match should not be skipped.
  15719. Otherwise, the function must return a position from where the search
  15720. should be continued.
  15721. This may also be a Lisp form, it will be evaluated.
  15722. Never set this variable using `setq' or so, because then it will apply
  15723. to all future agenda commands. Instead, bind it with `let' to scope
  15724. it dynamically into the agenda-constructing command. A good way to set
  15725. it is through options in org-agenda-custom-commands.")
  15726. (defun org-agenda-skip ()
  15727. "Throw to `:skip' in places that should be skipped.
  15728. Also moves point to the end of the skipped region, so that search can
  15729. continue from there."
  15730. (let ((p (point-at-bol)) to fp)
  15731. (and org-agenda-skip-archived-trees
  15732. (get-text-property p :org-archived)
  15733. (org-end-of-subtree t)
  15734. (throw :skip t))
  15735. (and (get-text-property p :org-comment)
  15736. (org-end-of-subtree t)
  15737. (throw :skip t))
  15738. (if (equal (char-after p) ?#) (throw :skip t))
  15739. (when (and (or (setq fp (functionp org-agenda-skip-function))
  15740. (consp org-agenda-skip-function))
  15741. (setq to (save-excursion
  15742. (save-match-data
  15743. (if fp
  15744. (funcall org-agenda-skip-function)
  15745. (eval org-agenda-skip-function))))))
  15746. (goto-char to)
  15747. (throw :skip t))))
  15748. (defvar org-agenda-markers nil
  15749. "List of all currently active markers created by `org-agenda'.")
  15750. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  15751. "Creation time of the last agenda marker.")
  15752. (defun org-agenda-new-marker (&optional pos)
  15753. "Return a new agenda marker.
  15754. Org-mode keeps a list of these markers and resets them when they are
  15755. no longer in use."
  15756. (let ((m (copy-marker (or pos (point)))))
  15757. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  15758. (push m org-agenda-markers)
  15759. m))
  15760. (defun org-agenda-maybe-reset-markers (&optional force)
  15761. "Reset markers created by `org-agenda'. But only if they are old enough."
  15762. (if (or (and force (not org-agenda-multi))
  15763. (> (- (time-to-seconds (current-time))
  15764. org-agenda-last-marker-time)
  15765. 5))
  15766. (while org-agenda-markers
  15767. (move-marker (pop org-agenda-markers) nil))))
  15768. (defvar org-agenda-new-buffers nil
  15769. "Buffers created to visit agenda files.")
  15770. (defun org-get-agenda-file-buffer (file)
  15771. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15772. it to the list of buffers which might be released later."
  15773. (let ((buf (org-find-base-buffer-visiting file)))
  15774. (if buf
  15775. buf ; just return it
  15776. ;; Make a new buffer and remember it
  15777. (setq buf (find-file-noselect file))
  15778. (if buf (push buf org-agenda-new-buffers))
  15779. buf)))
  15780. (defun org-release-buffers (blist)
  15781. "Release all buffers in list, asking the user for confirmation when needed.
  15782. When a buffer is unmodified, it is just killed. When modified, it is saved
  15783. \(if the user agrees) and then killed."
  15784. (let (buf file)
  15785. (while (setq buf (pop blist))
  15786. (setq file (buffer-file-name buf))
  15787. (when (and (buffer-modified-p buf)
  15788. file
  15789. (y-or-n-p (format "Save file %s? " file)))
  15790. (with-current-buffer buf (save-buffer)))
  15791. (kill-buffer buf))))
  15792. (defvar org-category-table nil)
  15793. (defun org-get-category-table ()
  15794. "Get the table of categories and positions in current buffer."
  15795. (let (tbl)
  15796. (save-excursion
  15797. (save-restriction
  15798. (widen)
  15799. (goto-char (point-min))
  15800. (while (re-search-forward "^#\\+CATEGORY:[ \t]*\\(.*\\)"
  15801. nil t)
  15802. (push (cons (match-beginning 1)
  15803. (org-trim (match-string 1))) tbl))))
  15804. tbl))
  15805. (defun org-get-category (&optional pos)
  15806. "Get the category applying to position POS."
  15807. (if (not org-category-table)
  15808. (cond
  15809. ((null org-category)
  15810. (setq org-category
  15811. (if buffer-file-name
  15812. (file-name-sans-extension
  15813. (file-name-nondirectory buffer-file-name))
  15814. "???")))
  15815. ((symbolp org-category) (symbol-name org-category))
  15816. (t org-category))
  15817. (let ((tbl org-category-table)
  15818. (pos (or pos (point))))
  15819. (while (and tbl (> (caar tbl) pos))
  15820. (pop tbl))
  15821. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  15822. org-category-table))))))
  15823. ;;; Agenda timeline
  15824. (defun org-timeline (&optional include-all)
  15825. "Show a time-sorted view of the entries in the current org file.
  15826. Only entries with a time stamp of today or later will be listed. With
  15827. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  15828. under the current date.
  15829. If the buffer contains an active region, only check the region for
  15830. dates."
  15831. (interactive "P")
  15832. (require 'calendar)
  15833. (org-compile-prefix-format 'timeline)
  15834. (org-set-sorting-strategy 'timeline)
  15835. (let* ((dopast t)
  15836. (dotodo include-all)
  15837. (doclosed org-agenda-show-log)
  15838. (entry buffer-file-name)
  15839. (date (calendar-current-date))
  15840. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  15841. (end (if (org-region-active-p) (region-end) (point-max)))
  15842. (day-numbers (org-get-all-dates beg end 'no-ranges
  15843. t doclosed ; always include today
  15844. org-timeline-show-empty-dates))
  15845. (today (time-to-days (current-time)))
  15846. (past t)
  15847. args
  15848. s e rtn d emptyp)
  15849. (setq org-agenda-redo-command
  15850. (list 'progn
  15851. (list 'switch-to-buffer-other-window (current-buffer))
  15852. (list 'org-timeline (list 'quote include-all))))
  15853. (if (not dopast)
  15854. ;; Remove past dates from the list of dates.
  15855. (setq day-numbers (delq nil (mapcar (lambda(x)
  15856. (if (>= x today) x nil))
  15857. day-numbers))))
  15858. (org-prepare-agenda (concat "Timeline "
  15859. (file-name-nondirectory buffer-file-name)))
  15860. (if doclosed (push :closed args))
  15861. (push :timestamp args)
  15862. (push :sexp args)
  15863. (if dotodo (push :todo args))
  15864. (while (setq d (pop day-numbers))
  15865. (if (and (listp d) (eq (car d) :omitted))
  15866. (progn
  15867. (setq s (point))
  15868. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  15869. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  15870. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  15871. (if (and (>= d today)
  15872. dopast
  15873. past)
  15874. (progn
  15875. (setq past nil)
  15876. (insert (make-string 79 ?-) "\n")))
  15877. (setq date (calendar-gregorian-from-absolute d))
  15878. (setq s (point))
  15879. (setq rtn (and (not emptyp)
  15880. (apply 'org-agenda-get-day-entries
  15881. entry date args)))
  15882. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  15883. (progn
  15884. (insert (calendar-day-name date) " "
  15885. (number-to-string (extract-calendar-day date)) " "
  15886. (calendar-month-name (extract-calendar-month date)) " "
  15887. (number-to-string (extract-calendar-year date)) "\n")
  15888. ; FIXME: this gives a timezone problem
  15889. ; (insert (format-time-string org-agenda-date-format
  15890. ; (calendar-time-from-absolute d 0))
  15891. ; "\n")
  15892. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  15893. (put-text-property s (1- (point)) 'org-date-line t)
  15894. (if (equal d today)
  15895. (put-text-property s (1- (point)) 'org-today t))
  15896. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  15897. (put-text-property s (1- (point)) 'day d)))))
  15898. (goto-char (point-min))
  15899. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  15900. (point-min)))
  15901. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  15902. (org-finalize-agenda)
  15903. (setq buffer-read-only t)))
  15904. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  15905. "Return a list of all relevant day numbers from BEG to END buffer positions.
  15906. If NO-RANGES is non-nil, include only the start and end dates of a range,
  15907. not every single day in the range. If FORCE-TODAY is non-nil, make
  15908. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  15909. inactive time stamps (those in square brackets) are included.
  15910. When EMPTY is non-nil, also include days without any entries."
  15911. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  15912. dates dates1 date day day1 day2 ts1 ts2)
  15913. (if force-today
  15914. (setq dates (list (time-to-days (current-time)))))
  15915. (save-excursion
  15916. (goto-char beg)
  15917. (while (re-search-forward re end t)
  15918. (setq day (time-to-days (org-time-string-to-time
  15919. (substring (match-string 1) 0 10))))
  15920. (or (memq day dates) (push day dates)))
  15921. (unless no-ranges
  15922. (goto-char beg)
  15923. (while (re-search-forward org-tr-regexp end t)
  15924. (setq ts1 (substring (match-string 1) 0 10)
  15925. ts2 (substring (match-string 2) 0 10)
  15926. day1 (time-to-days (org-time-string-to-time ts1))
  15927. day2 (time-to-days (org-time-string-to-time ts2)))
  15928. (while (< (setq day1 (1+ day1)) day2)
  15929. (or (memq day1 dates) (push day1 dates)))))
  15930. (setq dates (sort dates '<))
  15931. (when empty
  15932. (while (setq day (pop dates))
  15933. (setq day2 (car dates))
  15934. (push day dates1)
  15935. (when (and day2 empty)
  15936. (if (or (eq empty t)
  15937. (and (numberp empty) (<= (- day2 day) empty)))
  15938. (while (< (setq day (1+ day)) day2)
  15939. (push (list day) dates1))
  15940. (push (cons :omitted (- day2 day)) dates1))))
  15941. (setq dates (nreverse dates1)))
  15942. dates)))
  15943. ;;; Agenda Daily/Weekly
  15944. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  15945. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  15946. (defvar org-agenda-last-arguments nil
  15947. "The arguments of the previous call to org-agenda")
  15948. (defvar org-starting-day nil) ; local variable in the agenda buffer
  15949. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  15950. (defvar org-include-all-loc nil) ; local variable
  15951. ;;;###autoload
  15952. (defun org-agenda-list (&optional include-all start-day ndays)
  15953. "Produce a weekly view from all files in variable `org-agenda-files'.
  15954. The view will be for the current week, but from the overview buffer you
  15955. will be able to go to other weeks.
  15956. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  15957. also be shown, under the current date.
  15958. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  15959. on the days are also shown. See the variable `org-log-done' for how
  15960. to turn on logging.
  15961. START-DAY defaults to TODAY, or to the most recent match for the weekday
  15962. given in `org-agenda-start-on-weekday'.
  15963. NDAYS defaults to `org-agenda-ndays'."
  15964. (interactive "P")
  15965. (setq ndays (or ndays org-agenda-ndays)
  15966. start-day (or start-day org-agenda-start-day))
  15967. (if org-agenda-overriding-arguments
  15968. (setq include-all (car org-agenda-overriding-arguments)
  15969. start-day (nth 1 org-agenda-overriding-arguments)
  15970. ndays (nth 2 org-agenda-overriding-arguments)))
  15971. (if (stringp start-day)
  15972. ;; Convert to an absolute day number
  15973. (setq start-day (time-to-days (org-read-date nil t start-day))))
  15974. (setq org-agenda-last-arguments (list include-all start-day ndays))
  15975. (org-compile-prefix-format 'agenda)
  15976. (org-set-sorting-strategy 'agenda)
  15977. (require 'calendar)
  15978. (let* ((org-agenda-start-on-weekday
  15979. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  15980. org-agenda-start-on-weekday nil))
  15981. (thefiles (org-agenda-files))
  15982. (files thefiles)
  15983. (today (time-to-days (current-time)))
  15984. (sd (or start-day today))
  15985. (start (if (or (null org-agenda-start-on-weekday)
  15986. (< org-agenda-ndays 7))
  15987. sd
  15988. (let* ((nt (calendar-day-of-week
  15989. (calendar-gregorian-from-absolute sd)))
  15990. (n1 org-agenda-start-on-weekday)
  15991. (d (- nt n1)))
  15992. (- sd (+ (if (< d 0) 7 0) d)))))
  15993. (day-numbers (list start))
  15994. (inhibit-redisplay (not debug-on-error))
  15995. s e rtn rtnall file date d start-pos end-pos todayp nd)
  15996. (setq org-agenda-redo-command
  15997. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  15998. ;; Make the list of days
  15999. (setq ndays (or ndays org-agenda-ndays)
  16000. nd ndays)
  16001. (while (> ndays 1)
  16002. (push (1+ (car day-numbers)) day-numbers)
  16003. (setq ndays (1- ndays)))
  16004. (setq day-numbers (nreverse day-numbers))
  16005. (org-prepare-agenda "Day/Week")
  16006. (org-set-local 'org-starting-day (car day-numbers))
  16007. (org-set-local 'org-include-all-loc include-all)
  16008. (org-set-local 'org-agenda-span
  16009. (org-agenda-ndays-to-span nd))
  16010. (when (and (or include-all org-agenda-include-all-todo)
  16011. (member today day-numbers))
  16012. (setq files thefiles
  16013. rtnall nil)
  16014. (while (setq file (pop files))
  16015. (catch 'nextfile
  16016. (org-check-agenda-file file)
  16017. (setq date (calendar-gregorian-from-absolute today)
  16018. rtn (org-agenda-get-day-entries
  16019. file date :todo))
  16020. (setq rtnall (append rtnall rtn))))
  16021. (when rtnall
  16022. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  16023. (add-text-properties (point-min) (1- (point))
  16024. (list 'face 'org-agenda-structure))
  16025. (insert (org-finalize-agenda-entries rtnall) "\n")))
  16026. (setq s (point))
  16027. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  16028. "-agenda:\n")
  16029. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  16030. 'org-date-line t))
  16031. (while (setq d (pop day-numbers))
  16032. (setq date (calendar-gregorian-from-absolute d)
  16033. s (point))
  16034. (if (or (setq todayp (= d today))
  16035. (and (not start-pos) (= d sd)))
  16036. (setq start-pos (point))
  16037. (if (and start-pos (not end-pos))
  16038. (setq end-pos (point))))
  16039. (setq files thefiles
  16040. rtnall nil)
  16041. (while (setq file (pop files))
  16042. (catch 'nextfile
  16043. (org-check-agenda-file file)
  16044. (if org-agenda-show-log
  16045. (setq rtn (org-agenda-get-day-entries
  16046. file date
  16047. :deadline :scheduled :timestamp :sexp :closed))
  16048. (setq rtn (org-agenda-get-day-entries
  16049. file date
  16050. :deadline :scheduled :sexp :timestamp)))
  16051. (setq rtnall (append rtnall rtn))))
  16052. (if org-agenda-include-diary
  16053. (progn
  16054. (require 'diary-lib)
  16055. (setq rtn (org-get-entries-from-diary date))
  16056. (setq rtnall (append rtnall rtn))))
  16057. (if (or rtnall org-agenda-show-all-dates)
  16058. (progn
  16059. (insert (format "%-9s %2d %s %4d\n"
  16060. (calendar-day-name date)
  16061. (extract-calendar-day date)
  16062. (calendar-month-name (extract-calendar-month date))
  16063. (extract-calendar-year date)))
  16064. ; FIXME: this gives a timezone problem
  16065. ; (insert (format-time-string org-agenda-date-format
  16066. ; (calendar-time-from-absolute d 0)) "\n")
  16067. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  16068. (put-text-property s (1- (point)) 'org-date-line t)
  16069. (if todayp (put-text-property s (1- (point)) 'org-today t))
  16070. (if rtnall (insert
  16071. (org-finalize-agenda-entries
  16072. (org-agenda-add-time-grid-maybe
  16073. rtnall nd todayp))
  16074. "\n"))
  16075. (put-text-property s (1- (point)) 'day d))))
  16076. (goto-char (point-min))
  16077. (org-fit-agenda-window)
  16078. (unless (and (pos-visible-in-window-p (point-min))
  16079. (pos-visible-in-window-p (point-max)))
  16080. (goto-char (1- (point-max)))
  16081. (recenter -1)
  16082. (if (not (pos-visible-in-window-p (or start-pos 1)))
  16083. (progn
  16084. (goto-char (or start-pos 1))
  16085. (recenter 1))))
  16086. (goto-char (or start-pos 1))
  16087. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  16088. (org-finalize-agenda)
  16089. (setq buffer-read-only t)
  16090. (message "")))
  16091. (defun org-agenda-ndays-to-span (n)
  16092. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  16093. ;;; Agenda TODO list
  16094. (defvar org-select-this-todo-keyword nil)
  16095. (defvar org-last-arg nil)
  16096. ;;;###autoload
  16097. (defun org-todo-list (arg)
  16098. "Show all TODO entries from all agenda file in a single list.
  16099. The prefix arg can be used to select a specific TODO keyword and limit
  16100. the list to these. When using \\[universal-argument], you will be prompted
  16101. for a keyword. A numeric prefix directly selects the Nth keyword in
  16102. `org-todo-keywords-1'."
  16103. (interactive "P")
  16104. (require 'calendar)
  16105. (org-compile-prefix-format 'todo)
  16106. (org-set-sorting-strategy 'todo)
  16107. (org-prepare-agenda "TODO")
  16108. (let* ((today (time-to-days (current-time)))
  16109. (date (calendar-gregorian-from-absolute today))
  16110. (kwds org-todo-keywords-for-agenda)
  16111. (completion-ignore-case t)
  16112. (org-select-this-todo-keyword
  16113. (if (stringp arg) arg
  16114. (and arg (integerp arg) (> arg 0)
  16115. (nth (1- arg) kwds))))
  16116. rtn rtnall files file pos)
  16117. (when (equal arg '(4))
  16118. (setq org-select-this-todo-keyword
  16119. (completing-read "Keyword (or KWD1|K2D2|...): "
  16120. (mapcar 'list kwds) nil nil)))
  16121. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  16122. (org-set-local 'org-last-arg arg)
  16123. (setq org-agenda-redo-command
  16124. '(org-todo-list (or current-prefix-arg org-last-arg)))
  16125. (setq files (org-agenda-files)
  16126. rtnall nil)
  16127. (while (setq file (pop files))
  16128. (catch 'nextfile
  16129. (org-check-agenda-file file)
  16130. (setq rtn (org-agenda-get-day-entries file date :todo))
  16131. (setq rtnall (append rtnall rtn))))
  16132. (if org-agenda-overriding-header
  16133. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  16134. nil 'face 'org-agenda-structure) "\n")
  16135. (insert "Global list of TODO items of type: ")
  16136. (add-text-properties (point-min) (1- (point))
  16137. (list 'face 'org-agenda-structure))
  16138. (setq pos (point))
  16139. (insert (or org-select-this-todo-keyword "ALL") "\n")
  16140. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  16141. (setq pos (point))
  16142. (unless org-agenda-multi
  16143. (insert "Available with `N r': (0)ALL")
  16144. (let ((n 0) s)
  16145. (mapc (lambda (x)
  16146. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  16147. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  16148. (insert "\n "))
  16149. (insert " " s))
  16150. kwds))
  16151. (insert "\n"))
  16152. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  16153. (when rtnall
  16154. (insert (org-finalize-agenda-entries rtnall) "\n"))
  16155. (goto-char (point-min))
  16156. (org-fit-agenda-window)
  16157. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  16158. (org-finalize-agenda)
  16159. (setq buffer-read-only t)))
  16160. ;;; Agenda tags match
  16161. ;;;###autoload
  16162. (defun org-tags-view (&optional todo-only match)
  16163. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  16164. The prefix arg TODO-ONLY limits the search to TODO entries."
  16165. (interactive "P")
  16166. (org-compile-prefix-format 'tags)
  16167. (org-set-sorting-strategy 'tags)
  16168. (let* ((org-tags-match-list-sublevels
  16169. (if todo-only t org-tags-match-list-sublevels))
  16170. (completion-ignore-case t)
  16171. rtn rtnall files file pos matcher
  16172. buffer)
  16173. (setq matcher (org-make-tags-matcher match)
  16174. match (car matcher) matcher (cdr matcher))
  16175. (org-prepare-agenda (concat "TAGS " match))
  16176. (setq org-agenda-redo-command
  16177. (list 'org-tags-view (list 'quote todo-only)
  16178. (list 'if 'current-prefix-arg nil match)))
  16179. (setq files (org-agenda-files)
  16180. rtnall nil)
  16181. (while (setq file (pop files))
  16182. (catch 'nextfile
  16183. (org-check-agenda-file file)
  16184. (setq buffer (if (file-exists-p file)
  16185. (org-get-agenda-file-buffer file)
  16186. (error "No such file %s" file)))
  16187. (if (not buffer)
  16188. ;; If file does not exist, merror message to agenda
  16189. (setq rtn (list
  16190. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  16191. rtnall (append rtnall rtn))
  16192. (with-current-buffer buffer
  16193. (unless (org-mode-p)
  16194. (error "Agenda file %s is not in `org-mode'" file))
  16195. (setq org-category-table (org-get-category-table))
  16196. (save-excursion
  16197. (save-restriction
  16198. (if org-agenda-restrict
  16199. (narrow-to-region org-agenda-restrict-begin
  16200. org-agenda-restrict-end)
  16201. (widen))
  16202. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  16203. (setq rtnall (append rtnall rtn))))))))
  16204. (if org-agenda-overriding-header
  16205. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  16206. nil 'face 'org-agenda-structure) "\n")
  16207. (insert "Headlines with TAGS match: ")
  16208. (add-text-properties (point-min) (1- (point))
  16209. (list 'face 'org-agenda-structure))
  16210. (setq pos (point))
  16211. (insert match "\n")
  16212. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  16213. (setq pos (point))
  16214. (unless org-agenda-multi
  16215. (insert "Press `C-u r' to search again with new search string\n"))
  16216. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  16217. (when rtnall
  16218. (insert (org-finalize-agenda-entries rtnall) "\n"))
  16219. (goto-char (point-min))
  16220. (org-fit-agenda-window)
  16221. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  16222. (org-finalize-agenda)
  16223. (setq buffer-read-only t)))
  16224. ;;; Agenda Finding stuck projects
  16225. (defvar org-agenda-skip-regexp nil
  16226. "Regular expression used in skipping subtrees for the agenda.
  16227. This is basically a temporary global variable that can be set and then
  16228. used by user-defined selections using `org-agenda-skip-function'.")
  16229. (defvar org-agenda-overriding-header nil
  16230. "When this is set during todo and tags searches, will replace header.")
  16231. (defun org-agenda-skip-subtree-when-regexp-matches ()
  16232. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  16233. If yes, it returns the end position of this tree, causing agenda commands
  16234. to skip this subtree. This is a function that can be put into
  16235. `org-agenda-skip-function' for the duration of a command."
  16236. (let ((end (save-excursion (org-end-of-subtree t)))
  16237. skip)
  16238. (save-excursion
  16239. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  16240. (and skip end)))
  16241. (defun org-agenda-skip-entry-if (&rest conditions)
  16242. "Skip entry is any of CONDITIONS is true.
  16243. See `org-agenda-skip-if for details."
  16244. (org-agenda-skip-if nil conditions))
  16245. (defun org-agenda-skip-subtree-if (&rest conditions)
  16246. "Skip entry is any of CONDITIONS is true.
  16247. See `org-agenda-skip-if for details."
  16248. (org-agenda-skip-if t conditions))
  16249. (defun org-agenda-skip-if (subtree conditions)
  16250. "Checks current entity for CONDITIONS.
  16251. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  16252. the entry, i.e. the text before the next heading is checked.
  16253. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  16254. from different tests. Valid conditions are:
  16255. scheduled Check if there is a scheduled cookie
  16256. notscheduled Check if there is no scheduled cookie
  16257. deadline Check if there is a deadline
  16258. notdeadline Check if there is no deadline
  16259. regexp Check if regexp matches
  16260. notregexp Check if regexp does not match.
  16261. The regexp is taken from the conditions list, it must com right after the
  16262. `regexp' of `notregexp' element.
  16263. If any of these conditions is met, this function returns the end point of
  16264. the entity, causing the search to continue from there. This is a function
  16265. that can be put into `org-agenda-skip-function' for the duration of a command."
  16266. (let (beg end m r)
  16267. (org-back-to-heading t)
  16268. (setq beg (point)
  16269. end (if subtree
  16270. (progn (org-end-of-subtree t) (point))
  16271. (progn (outline-next-heading) (1- (point)))))
  16272. (goto-char beg)
  16273. (and
  16274. (or
  16275. (and (memq 'scheduled conditions)
  16276. (re-search-forward org-scheduled-time-regexp end t))
  16277. (and (memq 'notscheduled conditions)
  16278. (not (re-search-forward org-scheduled-time-regexp end t)))
  16279. (and (memq 'deadline conditions)
  16280. (re-search-forward org-deadline-time-regexp end t))
  16281. (and (memq 'notdeadline conditions)
  16282. (not (re-search-forward org-deadline-time-regexp end t)))
  16283. (and (setq m (memq 'regexp conditions))
  16284. (stringp (setq r (nth 1 m)))
  16285. (re-search-forward m end t))
  16286. (and (setq m (memq 'notregexp conditions))
  16287. (stringp (setq r (nth 1 m)))
  16288. (not (re-search-forward m end t))))
  16289. end)))
  16290. (defun org-agenda-list-stuck-projects (&rest ignore)
  16291. "Create agenda view for projects that are stuck.
  16292. Stuck projects are project that have no next actions. For the definitions
  16293. of what a project is and how to check if it stuck, customize the variable
  16294. `org-stuck-projects'.
  16295. MATCH is being ignored."
  16296. (interactive)
  16297. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  16298. ;; FIXME: we could have used org-agenda-skip-if here.
  16299. (org-agenda-overriding-header "List of stuck projects: ")
  16300. (matcher (nth 0 org-stuck-projects))
  16301. (todo (nth 1 org-stuck-projects))
  16302. (todo-wds (if (member "*" todo)
  16303. (progn
  16304. (org-prepare-agenda-buffers (org-agenda-files))
  16305. (org-delete-all
  16306. org-done-keywords-for-agenda
  16307. (copy-sequence org-todo-keywords-for-agenda)))
  16308. todo))
  16309. (todo-re (concat "^\\*+[ \t]+\\("
  16310. (mapconcat 'identity todo-wds "\\|")
  16311. "\\)\\>"))
  16312. (tags (nth 2 org-stuck-projects))
  16313. (tags-re (if (member "*" tags)
  16314. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  16315. (concat "^\\*+ .*:\\("
  16316. (mapconcat 'identity tags "\\|")
  16317. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  16318. (gen-re (nth 3 org-stuck-projects))
  16319. (re-list
  16320. (delq nil
  16321. (list
  16322. (if todo todo-re)
  16323. (if tags tags-re)
  16324. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  16325. gen-re)))))
  16326. (setq org-agenda-skip-regexp
  16327. (if re-list
  16328. (mapconcat 'identity re-list "\\|")
  16329. (error "No information how to identify unstuck projects")))
  16330. (org-tags-view nil matcher)
  16331. (with-current-buffer org-agenda-buffer-name
  16332. (setq org-agenda-redo-command
  16333. '(org-agenda-list-stuck-projects
  16334. (or current-prefix-arg org-last-arg))))))
  16335. ;;; Diary integration
  16336. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  16337. (defun org-get-entries-from-diary (date)
  16338. "Get the (Emacs Calendar) diary entries for DATE."
  16339. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  16340. (diary-display-hook '(fancy-diary-display))
  16341. (list-diary-entries-hook
  16342. (cons 'org-diary-default-entry list-diary-entries-hook))
  16343. (diary-file-name-prefix-function nil) ; turn this feature off
  16344. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  16345. entries
  16346. (org-disable-agenda-to-diary t))
  16347. (save-excursion
  16348. (save-window-excursion
  16349. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  16350. (if (not (get-buffer fancy-diary-buffer))
  16351. (setq entries nil)
  16352. (with-current-buffer fancy-diary-buffer
  16353. (setq buffer-read-only nil)
  16354. (if (zerop (buffer-size))
  16355. ;; No entries
  16356. (setq entries nil)
  16357. ;; Omit the date and other unnecessary stuff
  16358. (org-agenda-cleanup-fancy-diary)
  16359. ;; Add prefix to each line and extend the text properties
  16360. (if (zerop (buffer-size))
  16361. (setq entries nil)
  16362. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  16363. (set-buffer-modified-p nil)
  16364. (kill-buffer fancy-diary-buffer)))
  16365. (when entries
  16366. (setq entries (org-split-string entries "\n"))
  16367. (setq entries
  16368. (mapcar
  16369. (lambda (x)
  16370. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  16371. ;; Extend the text properties to the beginning of the line
  16372. (org-add-props x (text-properties-at (1- (length x)) x)
  16373. 'type "diary" 'date date))
  16374. entries)))))
  16375. (defun org-agenda-cleanup-fancy-diary ()
  16376. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  16377. This gets rid of the date, the underline under the date, and
  16378. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  16379. date. It also removes lines that contain only whitespace."
  16380. (goto-char (point-min))
  16381. (if (looking-at ".*?:[ \t]*")
  16382. (progn
  16383. (replace-match "")
  16384. (re-search-forward "\n=+$" nil t)
  16385. (replace-match "")
  16386. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  16387. (re-search-forward "\n=+$" nil t)
  16388. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  16389. (goto-char (point-min))
  16390. (while (re-search-forward "^ +\n" nil t)
  16391. (replace-match ""))
  16392. (goto-char (point-min))
  16393. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  16394. (replace-match "")))
  16395. ;; Make sure entries from the diary have the right text properties.
  16396. (eval-after-load "diary-lib"
  16397. '(if (boundp 'diary-modify-entry-list-string-function)
  16398. ;; We can rely on the hook, nothing to do
  16399. nil
  16400. ;; Hook not avaiable, must use advice to make this work
  16401. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  16402. "Make the position visible."
  16403. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  16404. (stringp string)
  16405. buffer-file-name)
  16406. (setq string (org-modify-diary-entry-string string))))))
  16407. (defun org-modify-diary-entry-string (string)
  16408. "Add text properties to string, allowing org-mode to act on it."
  16409. (org-add-props string nil
  16410. 'mouse-face 'highlight
  16411. 'keymap org-agenda-keymap
  16412. 'help-echo (if buffer-file-name
  16413. (format "mouse-2 or RET jump to diary file %s"
  16414. (abbreviate-file-name buffer-file-name))
  16415. "")
  16416. 'org-agenda-diary-link t
  16417. 'org-marker (org-agenda-new-marker (point-at-bol))))
  16418. (defun org-diary-default-entry ()
  16419. "Add a dummy entry to the diary.
  16420. Needed to avoid empty dates which mess up holiday display."
  16421. ;; Catch the error if dealing with the new add-to-diary-alist
  16422. (when org-disable-agenda-to-diary
  16423. (condition-case nil
  16424. (add-to-diary-list original-date "Org-mode dummy" "")
  16425. (error
  16426. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  16427. ;;;###autoload
  16428. (defun org-diary (&rest args)
  16429. "Return diary information from org-files.
  16430. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  16431. It accesses org files and extracts information from those files to be
  16432. listed in the diary. The function accepts arguments specifying what
  16433. items should be listed. The following arguments are allowed:
  16434. :timestamp List the headlines of items containing a date stamp or
  16435. date range matching the selected date. Deadlines will
  16436. also be listed, on the expiration day.
  16437. :sexp FIXME
  16438. :deadline List any deadlines past due, or due within
  16439. `org-deadline-warning-days'. The listing occurs only
  16440. in the diary for *today*, not at any other date. If
  16441. an entry is marked DONE, it is no longer listed.
  16442. :scheduled List all items which are scheduled for the given date.
  16443. The diary for *today* also contains items which were
  16444. scheduled earlier and are not yet marked DONE.
  16445. :todo List all TODO items from the org-file. This may be a
  16446. long list - so this is not turned on by default.
  16447. Like deadlines, these entries only show up in the
  16448. diary for *today*, not at any other date.
  16449. The call in the diary file should look like this:
  16450. &%%(org-diary) ~/path/to/some/orgfile.org
  16451. Use a separate line for each org file to check. Or, if you omit the file name,
  16452. all files listed in `org-agenda-files' will be checked automatically:
  16453. &%%(org-diary)
  16454. If you don't give any arguments (as in the example above), the default
  16455. arguments (:deadline :scheduled :timestamp :sexp) are used.
  16456. So the example above may also be written as
  16457. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  16458. The function expects the lisp variables `entry' and `date' to be provided
  16459. by the caller, because this is how the calendar works. Don't use this
  16460. function from a program - use `org-agenda-get-day-entries' instead."
  16461. (org-agenda-maybe-reset-markers)
  16462. (org-compile-prefix-format 'agenda)
  16463. (org-set-sorting-strategy 'agenda)
  16464. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  16465. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  16466. (list entry)
  16467. (org-agenda-files t)))
  16468. file rtn results)
  16469. (org-prepare-agenda-buffers files)
  16470. ;; If this is called during org-agenda, don't return any entries to
  16471. ;; the calendar. Org Agenda will list these entries itself.
  16472. (if org-disable-agenda-to-diary (setq files nil))
  16473. (while (setq file (pop files))
  16474. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  16475. (setq results (append results rtn)))
  16476. (if results
  16477. (concat (org-finalize-agenda-entries results) "\n"))))
  16478. ;;; Agenda entry finders
  16479. (defun org-agenda-get-day-entries (file date &rest args)
  16480. "Does the work for `org-diary' and `org-agenda'.
  16481. FILE is the path to a file to be checked for entries. DATE is date like
  16482. the one returned by `calendar-current-date'. ARGS are symbols indicating
  16483. which kind of entries should be extracted. For details about these, see
  16484. the documentation of `org-diary'."
  16485. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  16486. (let* ((org-startup-folded nil)
  16487. (org-startup-align-all-tables nil)
  16488. (buffer (if (file-exists-p file)
  16489. (org-get-agenda-file-buffer file)
  16490. (error "No such file %s" file)))
  16491. arg results rtn)
  16492. (if (not buffer)
  16493. ;; If file does not exist, make sure an error message ends up in diary
  16494. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  16495. (with-current-buffer buffer
  16496. (unless (org-mode-p)
  16497. (error "Agenda file %s is not in `org-mode'" file))
  16498. (setq org-category-table (org-get-category-table))
  16499. (let ((case-fold-search nil))
  16500. (save-excursion
  16501. (save-restriction
  16502. (if org-agenda-restrict
  16503. (narrow-to-region org-agenda-restrict-begin
  16504. org-agenda-restrict-end)
  16505. (widen))
  16506. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  16507. (while (setq arg (pop args))
  16508. (cond
  16509. ((and (eq arg :todo)
  16510. (equal date (calendar-current-date)))
  16511. (setq rtn (org-agenda-get-todos))
  16512. (setq results (append results rtn)))
  16513. ((eq arg :timestamp)
  16514. (setq rtn (org-agenda-get-blocks))
  16515. (setq results (append results rtn))
  16516. (setq rtn (org-agenda-get-timestamps))
  16517. (setq results (append results rtn)))
  16518. ((eq arg :sexp)
  16519. (setq rtn (org-agenda-get-sexps))
  16520. (setq results (append results rtn)))
  16521. ((eq arg :scheduled)
  16522. (setq rtn (org-agenda-get-scheduled))
  16523. (setq results (append results rtn)))
  16524. ((eq arg :closed)
  16525. (setq rtn (org-agenda-get-closed))
  16526. (setq results (append results rtn)))
  16527. ((eq arg :deadline)
  16528. (setq rtn (org-agenda-get-deadlines))
  16529. (setq results (append results rtn))))))))
  16530. results))))
  16531. ;; FIXME: this works only if the cursor is not at the
  16532. ;; beginning of the entry
  16533. (defun org-entry-is-done-p ()
  16534. "Is the current entry marked DONE?"
  16535. (save-excursion
  16536. (and (re-search-backward "[\r\n]\\*+ " nil t)
  16537. (looking-at org-nl-done-regexp))))
  16538. (defun org-at-date-range-p (&optional inactive-ok)
  16539. "Is the cursor inside a date range?"
  16540. (interactive)
  16541. (save-excursion
  16542. (catch 'exit
  16543. (let ((pos (point)))
  16544. (skip-chars-backward "^[<\r\n")
  16545. (skip-chars-backward "<[")
  16546. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  16547. (>= (match-end 0) pos)
  16548. (throw 'exit t))
  16549. (skip-chars-backward "^<[\r\n")
  16550. (skip-chars-backward "<[")
  16551. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  16552. (>= (match-end 0) pos)
  16553. (throw 'exit t)))
  16554. nil)))
  16555. (defun org-agenda-get-todos ()
  16556. "Return the TODO information for agenda display."
  16557. (let* ((props (list 'face nil
  16558. 'done-face 'org-done
  16559. 'org-not-done-regexp org-not-done-regexp
  16560. 'org-todo-regexp org-todo-regexp
  16561. 'mouse-face 'highlight
  16562. 'keymap org-agenda-keymap
  16563. 'help-echo
  16564. (format "mouse-2 or RET jump to org file %s"
  16565. (abbreviate-file-name buffer-file-name))))
  16566. ;; FIXME: get rid of the \n at some point but watch out
  16567. (regexp (concat "^\\*+[ \t]+\\("
  16568. (if org-select-this-todo-keyword
  16569. (if (equal org-select-this-todo-keyword "*")
  16570. org-todo-regexp
  16571. (concat "\\<\\("
  16572. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  16573. "\\)\\>"))
  16574. org-not-done-regexp)
  16575. "[^\n\r]*\\)"))
  16576. marker priority category tags
  16577. ee txt beg end)
  16578. (goto-char (point-min))
  16579. (while (re-search-forward regexp nil t)
  16580. (catch :skip
  16581. (save-match-data
  16582. (beginning-of-line)
  16583. (setq beg (point) end (progn (outline-next-heading) (point)))
  16584. (when (or (and org-agenda-todo-ignore-scheduled (goto-char beg)
  16585. (re-search-forward org-scheduled-time-regexp end t))
  16586. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  16587. (re-search-forward org-deadline-time-regexp end t)
  16588. (org-deadline-close (match-string 1))))
  16589. (goto-char beg)
  16590. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  16591. (throw :skip nil)))
  16592. (goto-char beg)
  16593. (org-agenda-skip)
  16594. (goto-char (match-beginning 1))
  16595. (setq marker (org-agenda-new-marker (match-beginning 0))
  16596. category (org-get-category)
  16597. tags (org-get-tags-at (point))
  16598. txt (org-format-agenda-item "" (match-string 1) category tags)
  16599. priority (1+ (org-get-priority txt)))
  16600. (org-add-props txt props
  16601. 'org-marker marker 'org-hd-marker marker
  16602. 'priority priority 'org-category category
  16603. 'type "todo")
  16604. (push txt ee)
  16605. (if org-agenda-todo-list-sublevels
  16606. (goto-char (match-end 1))
  16607. (org-end-of-subtree 'invisible))))
  16608. (nreverse ee)))
  16609. (defconst org-agenda-no-heading-message
  16610. "No heading for this item in buffer or region.")
  16611. (defun org-agenda-get-timestamps ()
  16612. "Return the date stamp information for agenda display."
  16613. (let* ((props (list 'face nil
  16614. 'org-not-done-regexp org-not-done-regexp
  16615. 'org-todo-regexp org-todo-regexp
  16616. 'mouse-face 'highlight
  16617. 'keymap org-agenda-keymap
  16618. 'help-echo
  16619. (format "mouse-2 or RET jump to org file %s"
  16620. (abbreviate-file-name buffer-file-name))))
  16621. (d1 (calendar-absolute-from-gregorian date))
  16622. (regexp
  16623. (concat
  16624. (regexp-quote
  16625. (substring
  16626. (format-time-string
  16627. (car org-time-stamp-formats)
  16628. (apply 'encode-time ; DATE bound by calendar
  16629. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16630. 0 11))
  16631. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  16632. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  16633. marker hdmarker deadlinep scheduledp donep tmp priority category
  16634. ee txt timestr tags b0 b3 e3)
  16635. (goto-char (point-min))
  16636. (while (re-search-forward regexp nil t)
  16637. (setq b0 (match-beginning 0)
  16638. b3 (match-beginning 3) e3 (match-end 3))
  16639. (catch :skip
  16640. (and (org-at-date-range-p) (throw :skip nil))
  16641. (org-agenda-skip)
  16642. (if (and (match-end 1)
  16643. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  16644. (throw :skip nil))
  16645. (if (and e3
  16646. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  16647. (throw :skip nil))
  16648. (setq marker (org-agenda-new-marker b0)
  16649. category (org-get-category b0)
  16650. tmp (buffer-substring (max (point-min)
  16651. (- b0 org-ds-keyword-length))
  16652. b0)
  16653. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  16654. deadlinep (string-match org-deadline-regexp tmp)
  16655. scheduledp (string-match org-scheduled-regexp tmp)
  16656. donep (org-entry-is-done-p))
  16657. (if (or scheduledp deadlinep) (throw :skip t))
  16658. (if (string-match ">" timestr)
  16659. ;; substring should only run to end of time stamp
  16660. (setq timestr (substring timestr 0 (match-end 0))))
  16661. (save-excursion
  16662. (if (re-search-backward "^\\*+ " nil t)
  16663. (progn
  16664. (goto-char (match-beginning 0))
  16665. (setq hdmarker (org-agenda-new-marker)
  16666. tags (org-get-tags-at))
  16667. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16668. (setq txt (org-format-agenda-item
  16669. nil (match-string 1) category tags timestr)))
  16670. (setq txt org-agenda-no-heading-message))
  16671. (setq priority (org-get-priority txt))
  16672. (org-add-props txt props
  16673. 'org-marker marker 'org-hd-marker hdmarker)
  16674. (org-add-props txt nil 'priority priority
  16675. 'org-category category 'date date
  16676. 'type "timestamp")
  16677. (push txt ee))
  16678. (outline-next-heading)))
  16679. (nreverse ee)))
  16680. (defun org-agenda-get-sexps ()
  16681. "Return the sexp information for agenda display."
  16682. (require 'diary-lib)
  16683. (let* ((props (list 'face nil
  16684. 'mouse-face 'highlight
  16685. 'keymap org-agenda-keymap
  16686. 'help-echo
  16687. (format "mouse-2 or RET jump to org file %s"
  16688. (abbreviate-file-name buffer-file-name))))
  16689. (regexp "^&?%%(")
  16690. marker category ee txt tags entry result beg b sexp sexp-entry)
  16691. (goto-char (point-min))
  16692. (while (re-search-forward regexp nil t)
  16693. (catch :skip
  16694. (org-agenda-skip)
  16695. (setq beg (match-beginning 0))
  16696. (goto-char (1- (match-end 0)))
  16697. (setq b (point))
  16698. (forward-sexp 1)
  16699. (setq sexp (buffer-substring b (point)))
  16700. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  16701. (org-trim (match-string 1))
  16702. ""))
  16703. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  16704. (when result
  16705. (setq marker (org-agenda-new-marker beg)
  16706. category (org-get-category beg))
  16707. (if (string-match "\\S-" result)
  16708. (setq txt result)
  16709. (setq txt "SEXP entry returned empty string"))
  16710. (setq txt (org-format-agenda-item
  16711. "" txt category tags 'time))
  16712. (org-add-props txt props 'org-marker marker)
  16713. (org-add-props txt nil
  16714. 'org-category category 'date date
  16715. 'type "sexp")
  16716. (push txt ee))))
  16717. (nreverse ee)))
  16718. (defun org-agenda-get-closed ()
  16719. "Return the logged TODO entries for agenda display."
  16720. (let* ((props (list 'mouse-face 'highlight
  16721. 'org-not-done-regexp org-not-done-regexp
  16722. 'org-todo-regexp org-todo-regexp
  16723. 'keymap org-agenda-keymap
  16724. 'help-echo
  16725. (format "mouse-2 or RET jump to org file %s"
  16726. (abbreviate-file-name buffer-file-name))))
  16727. (regexp (concat
  16728. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  16729. (regexp-quote
  16730. (substring
  16731. (format-time-string
  16732. (car org-time-stamp-formats)
  16733. (apply 'encode-time ; DATE bound by calendar
  16734. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16735. 1 11))))
  16736. marker hdmarker priority category tags closedp
  16737. ee txt timestr)
  16738. (goto-char (point-min))
  16739. (while (re-search-forward regexp nil t)
  16740. (catch :skip
  16741. (org-agenda-skip)
  16742. (setq marker (org-agenda-new-marker (match-beginning 0))
  16743. closedp (equal (match-string 1) org-closed-string)
  16744. category (org-get-category (match-beginning 0))
  16745. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  16746. ;; donep (org-entry-is-done-p)
  16747. )
  16748. (if (string-match "\\]" timestr)
  16749. ;; substring should only run to end of time stamp
  16750. (setq timestr (substring timestr 0 (match-end 0))))
  16751. (save-excursion
  16752. (if (re-search-backward "^\\*+ " nil t)
  16753. (progn
  16754. (goto-char (match-beginning 0))
  16755. (setq hdmarker (org-agenda-new-marker)
  16756. tags (org-get-tags-at))
  16757. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16758. (setq txt (org-format-agenda-item
  16759. (if closedp "Closed: " "Clocked: ")
  16760. (match-string 1) category tags timestr)))
  16761. (setq txt org-agenda-no-heading-message))
  16762. (setq priority 100000)
  16763. (org-add-props txt props
  16764. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  16765. 'priority priority 'org-category category
  16766. 'type "closed" 'date date
  16767. 'undone-face 'org-warning 'done-face 'org-done)
  16768. (push txt ee))
  16769. (outline-next-heading)))
  16770. (nreverse ee)))
  16771. (defun org-agenda-get-deadlines ()
  16772. "Return the deadline information for agenda display."
  16773. (let* ((props (list 'mouse-face 'highlight
  16774. 'org-not-done-regexp org-not-done-regexp
  16775. 'org-todo-regexp org-todo-regexp
  16776. 'keymap org-agenda-keymap
  16777. 'help-echo
  16778. (format "mouse-2 or RET jump to org file %s"
  16779. (abbreviate-file-name buffer-file-name))))
  16780. (regexp org-deadline-time-regexp)
  16781. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  16782. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  16783. d2 diff dfrac wdays pos pos1 category tags
  16784. ee txt head face s upcomingp)
  16785. (goto-char (point-min))
  16786. (while (re-search-forward regexp nil t)
  16787. (catch :skip
  16788. (org-agenda-skip)
  16789. (setq s (match-string 1)
  16790. pos (1- (match-beginning 1))
  16791. d2 (org-time-string-to-absolute (match-string 1) d1)
  16792. diff (- d2 d1))
  16793. (if (string-match "-\\([0-9]+\\)\\([dwmy]\\)\\'" s)
  16794. (setq wdays
  16795. (floor
  16796. (* (string-to-number (match-string 1 s))
  16797. (cdr (assoc (match-string 2 s)
  16798. '(("d" . 1) ("w" . 7)
  16799. ("m" . 30.4) ("y" . 365.25)))))))
  16800. (setq wdays org-deadline-warning-days))
  16801. (setq dfrac (/ (* 1.0 (- wdays diff)) wdays))
  16802. (setq upcomingp (and todayp (> diff 0)))
  16803. ;; When to show a deadline in the calendar:
  16804. ;; If the expiration is within wdays warning time.
  16805. ;; Past-due deadlines are only shown on the current date
  16806. (if (or (and (<= diff wdays) todayp)
  16807. (= diff 0))
  16808. (save-excursion
  16809. (setq category (org-get-category))
  16810. (if (re-search-backward "^\\*+[ \t]+" nil t)
  16811. (progn
  16812. (goto-char (match-end 0))
  16813. (setq pos1 (match-beginning 0))
  16814. (setq tags (org-get-tags-at pos1))
  16815. (setq head (buffer-substring-no-properties
  16816. (point)
  16817. (progn (skip-chars-forward "^\r\n")
  16818. (point))))
  16819. (if (and org-agenda-skip-deadline-if-done
  16820. (string-match org-looking-at-done-regexp head))
  16821. (setq txt nil)
  16822. (setq txt (org-format-agenda-item
  16823. (if (= diff 0)
  16824. "Deadline: "
  16825. (format "In %3d d.: " diff))
  16826. head category tags))))
  16827. (setq txt org-agenda-no-heading-message))
  16828. (when txt
  16829. (setq face (org-agenda-deadline-face dfrac))
  16830. (org-add-props txt props
  16831. 'org-marker (org-agenda-new-marker pos)
  16832. 'org-hd-marker (org-agenda-new-marker pos1)
  16833. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  16834. (org-get-priority txt))
  16835. 'org-category category
  16836. 'type (if upcomingp "upcoming-deadline" "deadline")
  16837. 'date (if upcomingp date d2)
  16838. 'face face 'undone-face face 'done-face 'org-done)
  16839. (push txt ee))))))
  16840. ee))
  16841. (defun org-agenda-deadline-face (fraction)
  16842. "Return the face to displaying a deadline item.
  16843. FRACTION is what fraction of the head-warning time has passed."
  16844. (let ((faces org-agenda-deadline-faces) f)
  16845. (catch 'exit
  16846. (while (setq f (pop faces))
  16847. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  16848. (defun org-agenda-get-scheduled ()
  16849. "Return the scheduled information for agenda display."
  16850. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  16851. 'org-todo-regexp org-todo-regexp
  16852. 'done-face 'org-done
  16853. 'mouse-face 'highlight
  16854. 'keymap org-agenda-keymap
  16855. 'help-echo
  16856. (format "mouse-2 or RET jump to org file %s"
  16857. (abbreviate-file-name buffer-file-name))))
  16858. (regexp org-scheduled-time-regexp)
  16859. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  16860. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  16861. d2 diff pos pos1 category tags
  16862. ee txt head pastduep donep face)
  16863. (goto-char (point-min))
  16864. (while (re-search-forward regexp nil t)
  16865. (catch :skip
  16866. (org-agenda-skip)
  16867. (setq pos (1- (match-beginning 1))
  16868. d2 (org-time-string-to-absolute (match-string 1) d1)
  16869. diff (- d2 d1))
  16870. (setq pastduep (and todayp (< diff 0)))
  16871. ;; When to show a scheduled item in the calendar:
  16872. ;; If it is on or past the date.
  16873. (if (or (and (< diff 0) todayp)
  16874. (= diff 0))
  16875. (save-excursion
  16876. (setq category (org-get-category))
  16877. (if (re-search-backward "^\\*+[ \t]+" nil t)
  16878. (progn
  16879. (goto-char (match-end 0))
  16880. (setq pos1 (match-beginning 0))
  16881. (setq tags (org-get-tags-at))
  16882. (setq head (buffer-substring-no-properties
  16883. (point)
  16884. (progn (skip-chars-forward "^\r\n") (point))))
  16885. (setq donep (string-match org-looking-at-done-regexp head))
  16886. (if (and org-agenda-skip-scheduled-if-done donep)
  16887. (setq txt nil)
  16888. (setq txt (org-format-agenda-item
  16889. (if (= diff 0)
  16890. "Scheduled: "
  16891. (format "Sched.%2dx: " (- 1 diff)))
  16892. head category tags))))
  16893. (setq txt org-agenda-no-heading-message))
  16894. (when txt
  16895. (setq face (if pastduep
  16896. 'org-scheduled-previously
  16897. 'org-scheduled-today))
  16898. (org-add-props txt props
  16899. 'undone-face face
  16900. 'face (if donep 'org-done face)
  16901. 'org-marker (org-agenda-new-marker pos)
  16902. 'org-hd-marker (org-agenda-new-marker pos1)
  16903. 'type (if pastduep "past-scheduled" "scheduled")
  16904. 'date (if pastduep d2 date)
  16905. 'priority (+ (- 5 diff) (org-get-priority txt))
  16906. 'org-category category)
  16907. (push txt ee))))))
  16908. ee))
  16909. (defun org-agenda-get-blocks ()
  16910. "Return the date-range information for agenda display."
  16911. (let* ((props (list 'face nil
  16912. 'org-not-done-regexp org-not-done-regexp
  16913. 'org-todo-regexp org-todo-regexp
  16914. 'mouse-face 'highlight
  16915. 'keymap org-agenda-keymap
  16916. 'help-echo
  16917. (format "mouse-2 or RET jump to org file %s"
  16918. (abbreviate-file-name buffer-file-name))))
  16919. (regexp org-tr-regexp)
  16920. (d0 (calendar-absolute-from-gregorian date))
  16921. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  16922. (goto-char (point-min))
  16923. (while (re-search-forward regexp nil t)
  16924. (catch :skip
  16925. (org-agenda-skip)
  16926. (setq pos (point))
  16927. (setq timestr (match-string 0)
  16928. s1 (match-string 1)
  16929. s2 (match-string 2)
  16930. d1 (time-to-days (org-time-string-to-time s1))
  16931. d2 (time-to-days (org-time-string-to-time s2)))
  16932. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  16933. ;; Only allow days between the limits, because the normal
  16934. ;; date stamps will catch the limits.
  16935. (save-excursion
  16936. (setq marker (org-agenda-new-marker (point)))
  16937. (setq category (org-get-category))
  16938. (if (re-search-backward "^\\*+ " nil t)
  16939. (progn
  16940. (goto-char (match-beginning 0))
  16941. (setq hdmarker (org-agenda-new-marker (point)))
  16942. (setq tags (org-get-tags-at))
  16943. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16944. (setq txt (org-format-agenda-item
  16945. (format (if (= d1 d2) "" "(%d/%d): ")
  16946. (1+ (- d0 d1)) (1+ (- d2 d1)))
  16947. (match-string 1) category tags
  16948. (if (= d0 d1) timestr))))
  16949. (setq txt org-agenda-no-heading-message))
  16950. (org-add-props txt props
  16951. 'org-marker marker 'org-hd-marker hdmarker
  16952. 'type "block" 'date date
  16953. 'priority (org-get-priority txt) 'org-category category)
  16954. (push txt ee)))
  16955. (goto-char pos)))
  16956. ;; Sort the entries by expiration date.
  16957. (nreverse ee)))
  16958. ;;; Agenda presentation and sorting
  16959. (defconst org-plain-time-of-day-regexp
  16960. (concat
  16961. "\\(\\<[012]?[0-9]"
  16962. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  16963. "\\(--?"
  16964. "\\(\\<[012]?[0-9]"
  16965. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  16966. "\\)?")
  16967. "Regular expression to match a plain time or time range.
  16968. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  16969. groups carry important information:
  16970. 0 the full match
  16971. 1 the first time, range or not
  16972. 8 the second time, if it is a range.")
  16973. (defconst org-stamp-time-of-day-regexp
  16974. (concat
  16975. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  16976. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  16977. "\\(--?"
  16978. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  16979. "Regular expression to match a timestamp time or time range.
  16980. After a match, the following groups carry important information:
  16981. 0 the full match
  16982. 1 date plus weekday, for backreferencing to make sure both times on same day
  16983. 2 the first time, range or not
  16984. 4 the second time, if it is a range.")
  16985. (defvar org-prefix-has-time nil
  16986. "A flag, set by `org-compile-prefix-format'.
  16987. The flag is set if the currently compiled format contains a `%t'.")
  16988. (defvar org-prefix-has-tag nil
  16989. "A flag, set by `org-compile-prefix-format'.
  16990. The flag is set if the currently compiled format contains a `%T'.")
  16991. (defun org-format-agenda-item (extra txt &optional category tags dotime
  16992. noprefix)
  16993. "Format TXT to be inserted into the agenda buffer.
  16994. In particular, it adds the prefix and corresponding text properties. EXTRA
  16995. must be a string and replaces the `%s' specifier in the prefix format.
  16996. CATEGORY (string, symbol or nil) may be used to overrule the default
  16997. category taken from local variable or file name. It will replace the `%c'
  16998. specifier in the format. DOTIME, when non-nil, indicates that a
  16999. time-of-day should be extracted from TXT for sorting of this entry, and for
  17000. the `%t' specifier in the format. When DOTIME is a string, this string is
  17001. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  17002. only the correctly processes TXT should be returned - this is used by
  17003. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  17004. (save-match-data
  17005. ;; Diary entries sometimes have extra whitespace at the beginning
  17006. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  17007. (let* ((category (or category
  17008. org-category
  17009. (if buffer-file-name
  17010. (file-name-sans-extension
  17011. (file-name-nondirectory buffer-file-name))
  17012. "")))
  17013. (tag (if tags (nth (1- (length tags)) tags) ""))
  17014. time ; time and tag are needed for the eval of the prefix format
  17015. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  17016. (time-of-day (and dotime (org-get-time-of-day ts)))
  17017. stamp plain s0 s1 s2 rtn srp)
  17018. (when (and dotime time-of-day org-prefix-has-time)
  17019. ;; Extract starting and ending time and move them to prefix
  17020. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  17021. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  17022. (setq s0 (match-string 0 ts)
  17023. srp (and stamp (match-end 3))
  17024. s1 (match-string (if plain 1 2) ts)
  17025. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  17026. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  17027. ;; them, we might want to remove them there to avoid duplication.
  17028. ;; The user can turn this off with a variable.
  17029. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  17030. (string-match (concat (regexp-quote s0) " *") txt)
  17031. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  17032. (= (match-beginning 0) 0)
  17033. t))
  17034. (setq txt (replace-match "" nil nil txt))))
  17035. ;; Normalize the time(s) to 24 hour
  17036. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  17037. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  17038. (when (and s1 (not s2) org-agenda-default-appointment-duration
  17039. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  17040. (let ((m (+ (string-to-number (match-string 2 s1))
  17041. (* 60 (string-to-number (match-string 1 s1)))
  17042. org-agenda-default-appointment-duration))
  17043. h)
  17044. (setq h (/ m 60) m (- m (* h 60)))
  17045. (setq s2 (format "%02d:%02d" h m))))
  17046. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  17047. txt)
  17048. ;; Tags are in the string
  17049. (if (or (eq org-agenda-remove-tags t)
  17050. (and org-agenda-remove-tags
  17051. org-prefix-has-tag))
  17052. (setq txt (replace-match "" t t txt))
  17053. (setq txt (replace-match
  17054. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  17055. (match-string 2 txt))
  17056. t t txt))))
  17057. ;; Create the final string
  17058. (if noprefix
  17059. (setq rtn txt)
  17060. ;; Prepare the variables needed in the eval of the compiled format
  17061. (setq time (cond (s2 (concat s1 "-" s2))
  17062. (s1 (concat s1 "......"))
  17063. (t ""))
  17064. extra (or extra "")
  17065. category (if (symbolp category) (symbol-name category) category))
  17066. ;; Evaluate the compiled format
  17067. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  17068. ;; And finally add the text properties
  17069. (org-add-props rtn nil
  17070. 'org-category (downcase category) 'tags tags
  17071. 'prefix-length (- (length rtn) (length txt))
  17072. 'time-of-day time-of-day
  17073. 'txt txt
  17074. 'time time
  17075. 'extra extra
  17076. 'dotime dotime))))
  17077. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  17078. (defvar org-agenda-sorting-strategy-selected nil)
  17079. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  17080. (catch 'exit
  17081. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  17082. ((and todayp (member 'today (car org-agenda-time-grid))))
  17083. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  17084. ((member 'weekly (car org-agenda-time-grid)))
  17085. (t (throw 'exit list)))
  17086. (let* ((have (delq nil (mapcar
  17087. (lambda (x) (get-text-property 1 'time-of-day x))
  17088. list)))
  17089. (string (nth 1 org-agenda-time-grid))
  17090. (gridtimes (nth 2 org-agenda-time-grid))
  17091. (req (car org-agenda-time-grid))
  17092. (remove (member 'remove-match req))
  17093. new time)
  17094. (if (and (member 'require-timed req) (not have))
  17095. ;; don't show empty grid
  17096. (throw 'exit list))
  17097. (while (setq time (pop gridtimes))
  17098. (unless (and remove (member time have))
  17099. (setq time (int-to-string time))
  17100. (push (org-format-agenda-item
  17101. nil string "" nil
  17102. (concat (substring time 0 -2) ":" (substring time -2)))
  17103. new)
  17104. (put-text-property
  17105. 1 (length (car new)) 'face 'org-time-grid (car new))))
  17106. (if (member 'time-up org-agenda-sorting-strategy-selected)
  17107. (append new list)
  17108. (append list new)))))
  17109. (defun org-compile-prefix-format (key)
  17110. "Compile the prefix format into a Lisp form that can be evaluated.
  17111. The resulting form is returned and stored in the variable
  17112. `org-prefix-format-compiled'."
  17113. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  17114. (let ((s (cond
  17115. ((stringp org-agenda-prefix-format)
  17116. org-agenda-prefix-format)
  17117. ((assq key org-agenda-prefix-format)
  17118. (cdr (assq key org-agenda-prefix-format)))
  17119. (t " %-12:c%?-12t% s")))
  17120. (start 0)
  17121. varform vars var e c f opt)
  17122. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  17123. s start)
  17124. (setq var (cdr (assoc (match-string 4 s)
  17125. '(("c" . category) ("t" . time) ("s" . extra)
  17126. ("T" . tag))))
  17127. c (or (match-string 3 s) "")
  17128. opt (match-beginning 1)
  17129. start (1+ (match-beginning 0)))
  17130. (if (equal var 'time) (setq org-prefix-has-time t))
  17131. (if (equal var 'tag) (setq org-prefix-has-tag t))
  17132. (setq f (concat "%" (match-string 2 s) "s"))
  17133. (if opt
  17134. (setq varform
  17135. `(if (equal "" ,var)
  17136. ""
  17137. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  17138. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  17139. (setq s (replace-match "%s" t nil s))
  17140. (push varform vars))
  17141. (setq vars (nreverse vars))
  17142. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  17143. (defun org-set-sorting-strategy (key)
  17144. (if (symbolp (car org-agenda-sorting-strategy))
  17145. ;; the old format
  17146. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  17147. (setq org-agenda-sorting-strategy-selected
  17148. (or (cdr (assq key org-agenda-sorting-strategy))
  17149. (cdr (assq 'agenda org-agenda-sorting-strategy))
  17150. '(time-up category-keep priority-down)))))
  17151. (defun org-get-time-of-day (s &optional string mod24)
  17152. "Check string S for a time of day.
  17153. If found, return it as a military time number between 0 and 2400.
  17154. If not found, return nil.
  17155. The optional STRING argument forces conversion into a 5 character wide string
  17156. HH:MM."
  17157. (save-match-data
  17158. (when
  17159. (or
  17160. (string-match
  17161. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  17162. (string-match
  17163. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  17164. (let* ((h (string-to-number (match-string 1 s)))
  17165. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  17166. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  17167. (am-p (equal ampm "am"))
  17168. (h1 (cond ((not ampm) h)
  17169. ((= h 12) (if am-p 0 12))
  17170. (t (+ h (if am-p 0 12)))))
  17171. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  17172. (mod h1 24) h1))
  17173. (t0 (+ (* 100 h2) m))
  17174. (t1 (concat (if (>= h1 24) "+" " ")
  17175. (if (< t0 100) "0" "")
  17176. (if (< t0 10) "0" "")
  17177. (int-to-string t0))))
  17178. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  17179. (defun org-finalize-agenda-entries (list &optional nosort)
  17180. "Sort and concatenate the agenda items."
  17181. (setq list (mapcar 'org-agenda-highlight-todo list))
  17182. (if nosort
  17183. list
  17184. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  17185. (defun org-agenda-highlight-todo (x)
  17186. (let (re pl)
  17187. (if (eq x 'line)
  17188. (save-excursion
  17189. (beginning-of-line 1)
  17190. (setq re (get-text-property (point) 'org-not-done-regexp))
  17191. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  17192. (and (looking-at (concat "[ \t]*\\.*" re))
  17193. (add-text-properties (match-beginning 0) (match-end 0)
  17194. '(face org-todo))))
  17195. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  17196. pl (get-text-property 0 'prefix-length x))
  17197. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  17198. (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
  17199. '(face org-todo) x))
  17200. x)))
  17201. (defsubst org-cmp-priority (a b)
  17202. "Compare the priorities of string A and B."
  17203. (let ((pa (or (get-text-property 1 'priority a) 0))
  17204. (pb (or (get-text-property 1 'priority b) 0)))
  17205. (cond ((> pa pb) +1)
  17206. ((< pa pb) -1)
  17207. (t nil))))
  17208. (defsubst org-cmp-category (a b)
  17209. "Compare the string values of categories of strings A and B."
  17210. (let ((ca (or (get-text-property 1 'org-category a) ""))
  17211. (cb (or (get-text-property 1 'org-category b) "")))
  17212. (cond ((string-lessp ca cb) -1)
  17213. ((string-lessp cb ca) +1)
  17214. (t nil))))
  17215. (defsubst org-cmp-tag (a b)
  17216. "Compare the string values of categories of strings A and B."
  17217. (let ((ta (car (last (get-text-property 1 'tags a))))
  17218. (tb (car (last (get-text-property 1 'tags b)))))
  17219. (cond ((not ta) +1)
  17220. ((not tb) -1)
  17221. ((string-lessp ta tb) -1)
  17222. ((string-lessp tb ta) +1)
  17223. (t nil))))
  17224. (defsubst org-cmp-time (a b)
  17225. "Compare the time-of-day values of strings A and B."
  17226. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  17227. (ta (or (get-text-property 1 'time-of-day a) def))
  17228. (tb (or (get-text-property 1 'time-of-day b) def)))
  17229. (cond ((< ta tb) -1)
  17230. ((< tb ta) +1)
  17231. (t nil))))
  17232. (defun org-entries-lessp (a b)
  17233. "Predicate for sorting agenda entries."
  17234. ;; The following variables will be used when the form is evaluated.
  17235. ;; So even though the compiler complains, keep them.
  17236. (let* ((time-up (org-cmp-time a b))
  17237. (time-down (if time-up (- time-up) nil))
  17238. (priority-up (org-cmp-priority a b))
  17239. (priority-down (if priority-up (- priority-up) nil))
  17240. (category-up (org-cmp-category a b))
  17241. (category-down (if category-up (- category-up) nil))
  17242. (category-keep (if category-up +1 nil))
  17243. (tag-up (org-cmp-tag a b))
  17244. (tag-down (if tag-up (- tag-up) nil)))
  17245. (cdr (assoc
  17246. (eval (cons 'or org-agenda-sorting-strategy-selected))
  17247. '((-1 . t) (1 . nil) (nil . nil))))))
  17248. ;;; Agenda commands
  17249. (defun org-agenda-check-type (error &rest types)
  17250. "Check if agenda buffer is of allowed type.
  17251. If ERROR is non-nil, throw an error, otherwise just return nil."
  17252. (if (memq org-agenda-type types)
  17253. t
  17254. (if error
  17255. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  17256. nil)))
  17257. (defun org-agenda-quit ()
  17258. "Exit agenda by removing the window or the buffer."
  17259. (interactive)
  17260. (let ((buf (current-buffer)))
  17261. (if (not (one-window-p)) (delete-window))
  17262. (kill-buffer buf)
  17263. (org-agenda-maybe-reset-markers 'force)
  17264. (org-columns-remove-overlays))
  17265. ;; Maybe restore the pre-agenda window configuration.
  17266. (and org-agenda-restore-windows-after-quit
  17267. (not (eq org-agenda-window-setup 'other-frame))
  17268. org-pre-agenda-window-conf
  17269. (set-window-configuration org-pre-agenda-window-conf)))
  17270. (defun org-agenda-exit ()
  17271. "Exit agenda by removing the window or the buffer.
  17272. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  17273. Org-mode buffers visited directly by the user will not be touched."
  17274. (interactive)
  17275. (org-release-buffers org-agenda-new-buffers)
  17276. (setq org-agenda-new-buffers nil)
  17277. (org-agenda-quit))
  17278. (defun org-save-all-org-buffers ()
  17279. "Save all Org-mode buffers without user confirmation."
  17280. (interactive)
  17281. (message "Saving all Org-mode buffers...")
  17282. (save-some-buffers t 'org-mode-p)
  17283. (message "Saving all Org-mode buffers... done"))
  17284. (defun org-agenda-redo ()
  17285. "Rebuild Agenda.
  17286. When this is the global TODO list, a prefix argument will be interpreted."
  17287. (interactive)
  17288. (let* ((org-agenda-keep-modes t)
  17289. (line (org-current-line))
  17290. (window-line (- line (org-current-line (window-start))))
  17291. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  17292. (message "Rebuilding agenda buffer...")
  17293. (org-let lprops '(eval org-agenda-redo-command))
  17294. (setq org-agenda-undo-list nil
  17295. org-agenda-pending-undo-list nil)
  17296. (message "Rebuilding agenda buffer...done")
  17297. (goto-line line)
  17298. (recenter window-line)))
  17299. (defun org-agenda-goto-date (date)
  17300. "Jump to DATE in agenda."
  17301. (interactive (list (org-read-date)))
  17302. (org-agenda-list nil date))
  17303. (defun org-agenda-goto-today ()
  17304. "Go to today."
  17305. (interactive)
  17306. (org-agenda-check-type t 'timeline 'agenda)
  17307. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  17308. (cond
  17309. (tdpos (goto-char tdpos))
  17310. ((eq org-agenda-type 'agenda)
  17311. (let* ((sd (time-to-days (current-time)))
  17312. (comp (org-agenda-compute-time-span sd org-agenda-span))
  17313. (org-agenda-overriding-arguments org-agenda-last-arguments))
  17314. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  17315. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  17316. (org-agenda-redo)
  17317. (org-agenda-find-today-or-agenda)))
  17318. (t (error "Cannot find today")))))
  17319. (defun org-agenda-find-today-or-agenda ()
  17320. (goto-char
  17321. (or (text-property-any (point-min) (point-max) 'org-today t)
  17322. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  17323. (point-min))))
  17324. (defun org-agenda-later (arg)
  17325. "Go forward in time by thee current span.
  17326. With prefix ARG, go forward that many times the current span."
  17327. (interactive "p")
  17328. (org-agenda-check-type t 'agenda)
  17329. (let* ((span org-agenda-span)
  17330. (sd org-starting-day)
  17331. (greg (calendar-gregorian-from-absolute sd))
  17332. greg2 nd)
  17333. (cond
  17334. ((eq span 'day)
  17335. (setq sd (+ arg sd) nd 1))
  17336. ((eq span 'week)
  17337. (setq sd (+ (* 7 arg) sd) nd 7))
  17338. ((eq span 'month)
  17339. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  17340. sd (calendar-absolute-from-gregorian greg2))
  17341. (setcar greg2 (1+ (car greg2)))
  17342. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  17343. ((eq span 'year)
  17344. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  17345. sd (calendar-absolute-from-gregorian greg2))
  17346. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  17347. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  17348. (let ((org-agenda-overriding-arguments
  17349. (list (car org-agenda-last-arguments) sd nd t)))
  17350. (org-agenda-redo)
  17351. (org-agenda-find-today-or-agenda))))
  17352. (defun org-agenda-earlier (arg)
  17353. "Go backward in time by the current span.
  17354. With prefix ARG, go backward that many times the current span."
  17355. (interactive "p")
  17356. (org-agenda-later (- arg)))
  17357. (defun org-agenda-day-view ()
  17358. "Switch to daily view for agenda."
  17359. (interactive)
  17360. (setq org-agenda-ndays 1)
  17361. (org-agenda-change-time-span 'day))
  17362. (defun org-agenda-week-view ()
  17363. "Switch to daily view for agenda."
  17364. (interactive)
  17365. (setq org-agenda-ndays 7)
  17366. (org-agenda-change-time-span 'week))
  17367. (defun org-agenda-month-view ()
  17368. "Switch to daily view for agenda."
  17369. (interactive)
  17370. (org-agenda-change-time-span 'month))
  17371. (defun org-agenda-year-view ()
  17372. "Switch to daily view for agenda."
  17373. (interactive)
  17374. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  17375. (org-agenda-change-time-span 'year)
  17376. (error "Abort")))
  17377. (defun org-agenda-change-time-span (span)
  17378. "Change the agenda view to SPAN.
  17379. SPAN may be `day', `week', `month', `year'."
  17380. (org-agenda-check-type t 'agenda)
  17381. (if (equal org-agenda-span span)
  17382. (error "Viewing span is already \"%s\"" span))
  17383. (let* ((sd (or (get-text-property (point) 'day)
  17384. org-starting-day))
  17385. (computed (org-agenda-compute-time-span sd span))
  17386. (org-agenda-overriding-arguments
  17387. (list (car org-agenda-last-arguments)
  17388. (car computed) (cdr computed) t)))
  17389. (org-agenda-redo)
  17390. (org-agenda-find-today-or-agenda))
  17391. (org-agenda-set-mode-name)
  17392. (message "Switched to %s view" span))
  17393. (defun org-agenda-compute-time-span (sd span)
  17394. "Compute starting date and number of days for agenda.
  17395. SPAN may be `day', `week', `month', `year'. The return value
  17396. is a cons cell with the starting date and the number of days,
  17397. so that the date SD will be in that range."
  17398. (let* ((greg (calendar-gregorian-from-absolute sd))
  17399. nd)
  17400. (cond
  17401. ((eq span 'day)
  17402. (setq nd 1))
  17403. ((eq span 'week)
  17404. (let* ((nt (calendar-day-of-week
  17405. (calendar-gregorian-from-absolute sd)))
  17406. (d (if org-agenda-start-on-weekday
  17407. (- nt org-agenda-start-on-weekday)
  17408. 0)))
  17409. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  17410. (setq nd 7)))
  17411. ((eq span 'month)
  17412. (setq sd (calendar-absolute-from-gregorian
  17413. (list (car greg) 1 (nth 2 greg)))
  17414. nd (- (calendar-absolute-from-gregorian
  17415. (list (1+ (car greg)) 1 (nth 2 greg)))
  17416. sd)))
  17417. ((eq span 'year)
  17418. (setq sd (calendar-absolute-from-gregorian
  17419. (list 1 1 (nth 2 greg)))
  17420. nd (- (calendar-absolute-from-gregorian
  17421. (list 1 1 (1+ (nth 2 greg))))
  17422. sd))))
  17423. (cons sd nd)))
  17424. ;; FIXME: this no longer works if user make date format that starts with a blank
  17425. (defun org-agenda-next-date-line (&optional arg)
  17426. "Jump to the next line indicating a date in agenda buffer."
  17427. (interactive "p")
  17428. (org-agenda-check-type t 'agenda 'timeline)
  17429. (beginning-of-line 1)
  17430. (if (looking-at "^\\S-") (forward-char 1))
  17431. (if (not (re-search-forward "^\\S-" nil t arg))
  17432. (progn
  17433. (backward-char 1)
  17434. (error "No next date after this line in this buffer")))
  17435. (goto-char (match-beginning 0)))
  17436. (defun org-agenda-previous-date-line (&optional arg)
  17437. "Jump to the previous line indicating a date in agenda buffer."
  17438. (interactive "p")
  17439. (org-agenda-check-type t 'agenda 'timeline)
  17440. (beginning-of-line 1)
  17441. (if (not (re-search-backward "^\\S-" nil t arg))
  17442. (error "No previous date before this line in this buffer")))
  17443. ;; Initialize the highlight
  17444. (defvar org-hl (org-make-overlay 1 1))
  17445. (org-overlay-put org-hl 'face 'highlight)
  17446. (defun org-highlight (begin end &optional buffer)
  17447. "Highlight a region with overlay."
  17448. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  17449. org-hl begin end (or buffer (current-buffer))))
  17450. (defun org-unhighlight ()
  17451. "Detach overlay INDEX."
  17452. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  17453. ;; FIXME this is currently not used.
  17454. (defun org-highlight-until-next-command (beg end &optional buffer)
  17455. (org-highlight beg end buffer)
  17456. (add-hook 'pre-command-hook 'org-unhighlight-once))
  17457. (defun org-unhighlight-once ()
  17458. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  17459. (org-unhighlight))
  17460. (defun org-agenda-follow-mode ()
  17461. "Toggle follow mode in an agenda buffer."
  17462. (interactive)
  17463. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  17464. (org-agenda-set-mode-name)
  17465. (message "Follow mode is %s"
  17466. (if org-agenda-follow-mode "on" "off")))
  17467. (defun org-agenda-log-mode ()
  17468. "Toggle log mode in an agenda buffer."
  17469. (interactive)
  17470. (org-agenda-check-type t 'agenda 'timeline)
  17471. (setq org-agenda-show-log (not org-agenda-show-log))
  17472. (org-agenda-set-mode-name)
  17473. (org-agenda-redo)
  17474. (message "Log mode is %s"
  17475. (if org-agenda-show-log "on" "off")))
  17476. (defun org-agenda-toggle-diary ()
  17477. "Toggle diary inclusion in an agenda buffer."
  17478. (interactive)
  17479. (org-agenda-check-type t 'agenda)
  17480. (setq org-agenda-include-diary (not org-agenda-include-diary))
  17481. (org-agenda-redo)
  17482. (org-agenda-set-mode-name)
  17483. (message "Diary inclusion turned %s"
  17484. (if org-agenda-include-diary "on" "off")))
  17485. (defun org-agenda-toggle-time-grid ()
  17486. "Toggle time grid in an agenda buffer."
  17487. (interactive)
  17488. (org-agenda-check-type t 'agenda)
  17489. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  17490. (org-agenda-redo)
  17491. (org-agenda-set-mode-name)
  17492. (message "Time-grid turned %s"
  17493. (if org-agenda-use-time-grid "on" "off")))
  17494. (defun org-agenda-set-mode-name ()
  17495. "Set the mode name to indicate all the small mode settings."
  17496. (setq mode-name
  17497. (concat "Org-Agenda"
  17498. (if (equal org-agenda-ndays 1) " Day" "")
  17499. (if (equal org-agenda-ndays 7) " Week" "")
  17500. (if org-agenda-follow-mode " Follow" "")
  17501. (if org-agenda-include-diary " Diary" "")
  17502. (if org-agenda-use-time-grid " Grid" "")
  17503. (if org-agenda-show-log " Log" "")))
  17504. (force-mode-line-update))
  17505. (defun org-agenda-post-command-hook ()
  17506. (and (eolp) (not (bolp)) (backward-char 1))
  17507. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  17508. (if (and org-agenda-follow-mode
  17509. (get-text-property (point) 'org-marker))
  17510. (org-agenda-show)))
  17511. (defun org-agenda-show-priority ()
  17512. "Show the priority of the current item.
  17513. This priority is composed of the main priority given with the [#A] cookies,
  17514. and by additional input from the age of a schedules or deadline entry."
  17515. (interactive)
  17516. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  17517. (message "Priority is %d" (if pri pri -1000))))
  17518. (defun org-agenda-show-tags ()
  17519. "Show the tags applicable to the current item."
  17520. (interactive)
  17521. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  17522. (if tags
  17523. (message "Tags are :%s:"
  17524. (org-no-properties (mapconcat 'identity tags ":")))
  17525. (message "No tags associated with this line"))))
  17526. (defun org-agenda-goto (&optional highlight)
  17527. "Go to the Org-mode file which contains the item at point."
  17528. (interactive)
  17529. (let* ((marker (or (get-text-property (point) 'org-marker)
  17530. (org-agenda-error)))
  17531. (buffer (marker-buffer marker))
  17532. (pos (marker-position marker)))
  17533. (switch-to-buffer-other-window buffer)
  17534. (widen)
  17535. (goto-char pos)
  17536. (when (org-mode-p)
  17537. (org-show-context 'agenda)
  17538. (save-excursion
  17539. (and (outline-next-heading)
  17540. (org-flag-heading nil)))) ; show the next heading
  17541. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  17542. (defun org-agenda-kill ()
  17543. "Kill the entry or subtree belonging to the current agenda entry."
  17544. (interactive)
  17545. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  17546. (let* ((marker (or (get-text-property (point) 'org-marker)
  17547. (org-agenda-error)))
  17548. (buffer (marker-buffer marker))
  17549. (pos (marker-position marker))
  17550. (type (get-text-property (point) 'type))
  17551. dbeg dend (n 0) conf)
  17552. (org-with-remote-undo buffer
  17553. (with-current-buffer buffer
  17554. (save-excursion
  17555. (goto-char pos)
  17556. (if (and (org-mode-p) (not (member type '("sexp"))))
  17557. (setq dbeg (progn (org-back-to-heading t) (point))
  17558. dend (org-end-of-subtree t))
  17559. (setq dbeg (point-at-bol)
  17560. dend (min (point-max) (1+ (point-at-eol)))))
  17561. (goto-char dbeg)
  17562. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  17563. (setq conf (or (eq t org-agenda-confirm-kill)
  17564. (and (numberp org-agenda-confirm-kill)
  17565. (> n org-agenda-confirm-kill))))
  17566. (and conf
  17567. (not (y-or-n-p
  17568. (format "Delete entry with %d lines in buffer \"%s\"? "
  17569. n (buffer-name buffer))))
  17570. (error "Abort"))
  17571. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  17572. (with-current-buffer buffer (delete-region dbeg dend))
  17573. (message "Agenda item and source killed"))))
  17574. (defun org-agenda-archive ()
  17575. "Kill the entry or subtree belonging to the current agenda entry."
  17576. (interactive)
  17577. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  17578. (let* ((marker (or (get-text-property (point) 'org-marker)
  17579. (org-agenda-error)))
  17580. (buffer (marker-buffer marker))
  17581. (pos (marker-position marker)))
  17582. (org-with-remote-undo buffer
  17583. (with-current-buffer buffer
  17584. (if (org-mode-p)
  17585. (save-excursion
  17586. (goto-char pos)
  17587. (org-remove-subtree-entries-from-agenda)
  17588. (org-back-to-heading t)
  17589. (org-archive-subtree))
  17590. (error "Archiving works only in Org-mode files"))))))
  17591. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  17592. "Remove all lines in the agenda that correspond to a given subtree.
  17593. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  17594. If this information is not given, the function uses the tree at point."
  17595. (let ((buf (or buf (current-buffer))) m p)
  17596. (save-excursion
  17597. (unless (and beg end)
  17598. (org-back-to-heading t)
  17599. (setq beg (point))
  17600. (org-end-of-subtree t)
  17601. (setq end (point)))
  17602. (set-buffer (get-buffer org-agenda-buffer-name))
  17603. (save-excursion
  17604. (goto-char (point-max))
  17605. (beginning-of-line 1)
  17606. (while (not (bobp))
  17607. (when (and (setq m (get-text-property (point) 'org-marker))
  17608. (equal buf (marker-buffer m))
  17609. (setq p (marker-position m))
  17610. (>= p beg)
  17611. (<= p end))
  17612. (let ((inhibit-read-only t))
  17613. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  17614. (beginning-of-line 0))))))
  17615. (defun org-agenda-open-link ()
  17616. "Follow the link in the current line, if any."
  17617. (interactive)
  17618. (let ((eol (point-at-eol)))
  17619. (save-excursion
  17620. (if (or (re-search-forward org-bracket-link-regexp eol t)
  17621. (re-search-forward org-angle-link-re eol t)
  17622. (re-search-forward org-plain-link-re eol t))
  17623. (call-interactively 'org-open-at-point)
  17624. (error "No link in current line")))))
  17625. (defun org-agenda-switch-to (&optional delete-other-windows)
  17626. "Go to the Org-mode file which contains the item at point."
  17627. (interactive)
  17628. (let* ((marker (or (get-text-property (point) 'org-marker)
  17629. (org-agenda-error)))
  17630. (buffer (marker-buffer marker))
  17631. (pos (marker-position marker)))
  17632. (switch-to-buffer buffer)
  17633. (and delete-other-windows (delete-other-windows))
  17634. (widen)
  17635. (goto-char pos)
  17636. (when (org-mode-p)
  17637. (org-show-context 'agenda)
  17638. (save-excursion
  17639. (and (outline-next-heading)
  17640. (org-flag-heading nil)))))) ; show the next heading
  17641. (defun org-agenda-goto-mouse (ev)
  17642. "Go to the Org-mode file which contains the item at the mouse click."
  17643. (interactive "e")
  17644. (mouse-set-point ev)
  17645. (org-agenda-goto))
  17646. (defun org-agenda-show ()
  17647. "Display the Org-mode file which contains the item at point."
  17648. (interactive)
  17649. (let ((win (selected-window)))
  17650. (org-agenda-goto t)
  17651. (select-window win)))
  17652. (defun org-agenda-recenter (arg)
  17653. "Display the Org-mode file which contains the item at point and recenter."
  17654. (interactive "P")
  17655. (let ((win (selected-window)))
  17656. (org-agenda-goto t)
  17657. (recenter arg)
  17658. (select-window win)))
  17659. (defun org-agenda-show-mouse (ev)
  17660. "Display the Org-mode file which contains the item at the mouse click."
  17661. (interactive "e")
  17662. (mouse-set-point ev)
  17663. (org-agenda-show))
  17664. (defun org-agenda-check-no-diary ()
  17665. "Check if the entry is a diary link and abort if yes."
  17666. (if (get-text-property (point) 'org-agenda-diary-link)
  17667. (org-agenda-error)))
  17668. (defun org-agenda-error ()
  17669. (error "Command not allowed in this line"))
  17670. (defun org-agenda-tree-to-indirect-buffer ()
  17671. "Show the subtree corresponding to the current entry in an indirect buffer.
  17672. This calls the command `org-tree-to-indirect-buffer' from the original
  17673. Org-mode buffer.
  17674. With numerical prefix arg ARG, go up to this level and then take that tree.
  17675. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  17676. dedicated frame)."
  17677. (interactive)
  17678. (org-agenda-check-no-diary)
  17679. (let* ((marker (or (get-text-property (point) 'org-marker)
  17680. (org-agenda-error)))
  17681. (buffer (marker-buffer marker))
  17682. (pos (marker-position marker)))
  17683. (with-current-buffer buffer
  17684. (save-excursion
  17685. (goto-char pos)
  17686. (call-interactively 'org-tree-to-indirect-buffer)))))
  17687. (defvar org-last-heading-marker (make-marker)
  17688. "Marker pointing to the headline that last changed its TODO state
  17689. by a remote command from the agenda.")
  17690. (defun org-agenda-todo-nextset ()
  17691. "Switch TODO entry to next sequence."
  17692. (interactive)
  17693. (org-agenda-todo 'nextset))
  17694. (defun org-agenda-todo-previousset ()
  17695. "Switch TODO entry to previous sequence."
  17696. (interactive)
  17697. (org-agenda-todo 'previousset))
  17698. (defun org-agenda-todo (&optional arg)
  17699. "Cycle TODO state of line at point, also in Org-mode file.
  17700. This changes the line at point, all other lines in the agenda referring to
  17701. the same tree node, and the headline of the tree node in the Org-mode file."
  17702. (interactive "P")
  17703. (org-agenda-check-no-diary)
  17704. (let* ((col (current-column))
  17705. (marker (or (get-text-property (point) 'org-marker)
  17706. (org-agenda-error)))
  17707. (buffer (marker-buffer marker))
  17708. (pos (marker-position marker))
  17709. (hdmarker (get-text-property (point) 'org-hd-marker))
  17710. (inhibit-read-only t)
  17711. newhead)
  17712. (org-with-remote-undo buffer
  17713. (with-current-buffer buffer
  17714. (widen)
  17715. (goto-char pos)
  17716. (org-show-context 'agenda)
  17717. (save-excursion
  17718. (and (outline-next-heading)
  17719. (org-flag-heading nil))) ; show the next heading
  17720. (org-todo arg)
  17721. (and (bolp) (forward-char 1))
  17722. (setq newhead (org-get-heading))
  17723. (save-excursion
  17724. (org-back-to-heading)
  17725. (move-marker org-last-heading-marker (point))))
  17726. (beginning-of-line 1)
  17727. (save-excursion
  17728. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  17729. (move-to-column col))))
  17730. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  17731. "Change all lines in the agenda buffer which match HDMARKER.
  17732. The new content of the line will be NEWHEAD (as modified by
  17733. `org-format-agenda-item'). HDMARKER is checked with
  17734. `equal' against all `org-hd-marker' text properties in the file.
  17735. If FIXFACE is non-nil, the face of each item is modified acording to
  17736. the new TODO state."
  17737. (let* ((inhibit-read-only t)
  17738. props m pl undone-face done-face finish new dotime cat tags)
  17739. (save-excursion
  17740. (goto-char (point-max))
  17741. (beginning-of-line 1)
  17742. (while (not finish)
  17743. (setq finish (bobp))
  17744. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  17745. (equal m hdmarker))
  17746. (setq props (text-properties-at (point))
  17747. dotime (get-text-property (point) 'dotime)
  17748. cat (get-text-property (point) 'org-category)
  17749. tags (get-text-property (point) 'tags)
  17750. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  17751. pl (get-text-property (point) 'prefix-length)
  17752. undone-face (get-text-property (point) 'undone-face)
  17753. done-face (get-text-property (point) 'done-face))
  17754. (move-to-column pl)
  17755. (cond
  17756. ((equal new "")
  17757. (beginning-of-line 1)
  17758. (and (looking-at ".*\n?") (replace-match "")))
  17759. ((looking-at ".*")
  17760. (replace-match new t t)
  17761. (beginning-of-line 1)
  17762. (add-text-properties (point-at-bol) (point-at-eol) props)
  17763. (when fixface
  17764. (add-text-properties
  17765. (point-at-bol) (point-at-eol)
  17766. (list 'face
  17767. (if org-last-todo-state-is-todo
  17768. undone-face done-face))))
  17769. (org-agenda-highlight-todo 'line)
  17770. (beginning-of-line 1))
  17771. (t (error "Line update did not work"))))
  17772. (beginning-of-line 0)))
  17773. (org-finalize-agenda)))
  17774. ;; FIXME: allow negative value for org-agenda-align-tags-to-column
  17775. ;; See the code in set-tags for the way to do this.
  17776. (defun org-agenda-align-tags (&optional line)
  17777. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  17778. (let ((inhibit-read-only t))
  17779. (save-excursion
  17780. (goto-char (if line (point-at-bol) (point-min)))
  17781. (while (re-search-forward (org-re "\\([ \t]+\\):[[:alnum:]_@:]+:[ \t]*$")
  17782. (if line (point-at-eol) nil) t)
  17783. (delete-region (match-beginning 1) (match-end 1))
  17784. (goto-char (match-beginning 1))
  17785. (insert (org-add-props
  17786. (make-string (max 1 (- org-agenda-align-tags-to-column
  17787. (current-column))) ?\ )
  17788. (text-properties-at (point))))))))
  17789. (defun org-agenda-priority-up ()
  17790. "Increase the priority of line at point, also in Org-mode file."
  17791. (interactive)
  17792. (org-agenda-priority 'up))
  17793. (defun org-agenda-priority-down ()
  17794. "Decrease the priority of line at point, also in Org-mode file."
  17795. (interactive)
  17796. (org-agenda-priority 'down))
  17797. (defun org-agenda-priority (&optional force-direction)
  17798. "Set the priority of line at point, also in Org-mode file.
  17799. This changes the line at point, all other lines in the agenda referring to
  17800. the same tree node, and the headline of the tree node in the Org-mode file."
  17801. (interactive)
  17802. (org-agenda-check-no-diary)
  17803. (let* ((marker (or (get-text-property (point) 'org-marker)
  17804. (org-agenda-error)))
  17805. (hdmarker (get-text-property (point) 'org-hd-marker))
  17806. (buffer (marker-buffer hdmarker))
  17807. (pos (marker-position hdmarker))
  17808. (inhibit-read-only t)
  17809. newhead)
  17810. (org-with-remote-undo buffer
  17811. (with-current-buffer buffer
  17812. (widen)
  17813. (goto-char pos)
  17814. (org-show-context 'agenda)
  17815. (save-excursion
  17816. (and (outline-next-heading)
  17817. (org-flag-heading nil))) ; show the next heading
  17818. (funcall 'org-priority force-direction)
  17819. (end-of-line 1)
  17820. (setq newhead (org-get-heading)))
  17821. (org-agenda-change-all-lines newhead hdmarker)
  17822. (beginning-of-line 1))))
  17823. (defun org-get-tags-at (&optional pos)
  17824. "Get a list of all headline tags applicable at POS.
  17825. POS defaults to point. If tags are inherited, the list contains
  17826. the targets in the same sequence as the headlines appear, i.e.
  17827. the tags of the current headline come last."
  17828. (interactive)
  17829. (let (tags)
  17830. (save-excursion
  17831. (save-restriction
  17832. (widen)
  17833. (goto-char (or pos (point)))
  17834. (save-match-data
  17835. (org-back-to-heading t)
  17836. (condition-case nil
  17837. (while t
  17838. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  17839. (setq tags (append (org-split-string
  17840. (org-match-string-no-properties 1) ":")
  17841. tags)))
  17842. (or org-use-tag-inheritance (error ""))
  17843. (org-up-heading-all 1))
  17844. (error nil))))
  17845. tags)))
  17846. ;; FIXME: should fix the tags property of the agenda line.
  17847. (defun org-agenda-set-tags ()
  17848. "Set tags for the current headline."
  17849. (interactive)
  17850. (org-agenda-check-no-diary)
  17851. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  17852. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  17853. (org-agenda-error)))
  17854. (buffer (marker-buffer hdmarker))
  17855. (pos (marker-position hdmarker))
  17856. (inhibit-read-only t)
  17857. newhead)
  17858. (org-with-remote-undo buffer
  17859. (with-current-buffer buffer
  17860. (widen)
  17861. (goto-char pos)
  17862. (save-excursion
  17863. (org-show-context 'agenda))
  17864. (save-excursion
  17865. (and (outline-next-heading)
  17866. (org-flag-heading nil))) ; show the next heading
  17867. (goto-char pos)
  17868. (call-interactively 'org-set-tags)
  17869. (end-of-line 1)
  17870. (setq newhead (org-get-heading)))
  17871. (org-agenda-change-all-lines newhead hdmarker)
  17872. (beginning-of-line 1))))
  17873. (defun org-agenda-toggle-archive-tag ()
  17874. "Toggle the archive tag for the current entry."
  17875. (interactive)
  17876. (org-agenda-check-no-diary)
  17877. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  17878. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  17879. (org-agenda-error)))
  17880. (buffer (marker-buffer hdmarker))
  17881. (pos (marker-position hdmarker))
  17882. (inhibit-read-only t)
  17883. newhead)
  17884. (org-with-remote-undo buffer
  17885. (with-current-buffer buffer
  17886. (widen)
  17887. (goto-char pos)
  17888. (org-show-context 'agenda)
  17889. (save-excursion
  17890. (and (outline-next-heading)
  17891. (org-flag-heading nil))) ; show the next heading
  17892. (call-interactively 'org-toggle-archive-tag)
  17893. (end-of-line 1)
  17894. (setq newhead (org-get-heading)))
  17895. (org-agenda-change-all-lines newhead hdmarker)
  17896. (beginning-of-line 1))))
  17897. (defun org-agenda-date-later (arg &optional what)
  17898. "Change the date of this item to one day later."
  17899. (interactive "p")
  17900. (org-agenda-check-type t 'agenda 'timeline)
  17901. (org-agenda-check-no-diary)
  17902. (let* ((marker (or (get-text-property (point) 'org-marker)
  17903. (org-agenda-error)))
  17904. (buffer (marker-buffer marker))
  17905. (pos (marker-position marker)))
  17906. (org-with-remote-undo buffer
  17907. (with-current-buffer buffer
  17908. (widen)
  17909. (goto-char pos)
  17910. (if (not (org-at-timestamp-p))
  17911. (error "Cannot find time stamp"))
  17912. (org-timestamp-change arg (or what 'day)))
  17913. (org-agenda-show-new-time marker org-last-changed-timestamp))
  17914. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  17915. (defun org-agenda-date-earlier (arg &optional what)
  17916. "Change the date of this item to one day earlier."
  17917. (interactive "p")
  17918. (org-agenda-date-later (- arg) what))
  17919. (defun org-agenda-show-new-time (marker stamp)
  17920. "Show new date stamp via text properties."
  17921. ;; We use text properties to make this undoable
  17922. (let ((inhibit-read-only t))
  17923. (setq stamp (concat " => " stamp))
  17924. (save-excursion
  17925. (goto-char (point-max))
  17926. (while (not (bobp))
  17927. (when (equal marker (get-text-property (point) 'org-marker))
  17928. (move-to-column (- (window-width) (length stamp)) t)
  17929. (if (featurep 'xemacs)
  17930. ;; Use `duplicable' property to trigger undo recording
  17931. (let ((ex (make-extent nil nil))
  17932. (gl (make-glyph stamp)))
  17933. (set-glyph-face gl 'secondary-selection)
  17934. (set-extent-properties
  17935. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  17936. (insert-extent ex (1- (point)) (point-at-eol)))
  17937. (add-text-properties
  17938. (1- (point)) (point-at-eol)
  17939. (list 'display (org-add-props stamp nil
  17940. 'face 'secondary-selection))))
  17941. (beginning-of-line 1))
  17942. (beginning-of-line 0)))))
  17943. (defun org-agenda-date-prompt (arg)
  17944. "Change the date of this item. Date is prompted for, with default today.
  17945. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  17946. be used to request time specification in the time stamp."
  17947. (interactive "P")
  17948. (org-agenda-check-type t 'agenda 'timeline)
  17949. (org-agenda-check-no-diary)
  17950. (let* ((marker (or (get-text-property (point) 'org-marker)
  17951. (org-agenda-error)))
  17952. (buffer (marker-buffer marker))
  17953. (pos (marker-position marker)))
  17954. (org-with-remote-undo buffer
  17955. (with-current-buffer buffer
  17956. (widen)
  17957. (goto-char pos)
  17958. (if (not (org-at-timestamp-p))
  17959. (error "Cannot find time stamp"))
  17960. (org-time-stamp arg)
  17961. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  17962. (defun org-agenda-schedule (arg)
  17963. "Schedule the item at point."
  17964. (interactive "P")
  17965. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  17966. (org-agenda-check-no-diary)
  17967. (let* ((marker (or (get-text-property (point) 'org-marker)
  17968. (org-agenda-error)))
  17969. (buffer (marker-buffer marker))
  17970. (pos (marker-position marker))
  17971. (org-insert-labeled-timestamps-at-point nil)
  17972. ts)
  17973. (org-with-remote-undo buffer
  17974. (with-current-buffer buffer
  17975. (widen)
  17976. (goto-char pos)
  17977. (setq ts (org-schedule))
  17978. (message "Item scheduled for %s" ts)))))
  17979. (defun org-agenda-deadline (arg)
  17980. "Schedule the item at point."
  17981. (interactive "P")
  17982. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  17983. (org-agenda-check-no-diary)
  17984. (let* ((marker (or (get-text-property (point) 'org-marker)
  17985. (org-agenda-error)))
  17986. (buffer (marker-buffer marker))
  17987. (pos (marker-position marker))
  17988. (org-insert-labeled-timestamps-at-point nil)
  17989. ts)
  17990. (org-with-remote-undo buffer
  17991. (with-current-buffer buffer
  17992. (widen)
  17993. (goto-char pos)
  17994. (setq ts (org-deadline))
  17995. (message "Deadline for this item set to %s" ts)))))
  17996. (defun org-get-heading ()
  17997. "Return the heading of the current entry, without the stars."
  17998. (save-excursion
  17999. (org-back-to-heading t)
  18000. (if (looking-at "\\*+[ \t]+\\([^\r\n]*\\)") (match-string 1) "")))
  18001. (defun org-agenda-clock-in (&optional arg)
  18002. "Start the clock on the currently selected item."
  18003. (interactive "P")
  18004. (org-agenda-check-no-diary)
  18005. (let* ((marker (or (get-text-property (point) 'org-marker)
  18006. (org-agenda-error)))
  18007. (pos (marker-position marker)))
  18008. (org-with-remote-undo (marker-buffer marker)
  18009. (with-current-buffer (marker-buffer marker)
  18010. (widen)
  18011. (goto-char pos)
  18012. (org-clock-in)))))
  18013. (defun org-agenda-clock-out (&optional arg)
  18014. "Stop the currently running clock."
  18015. (interactive "P")
  18016. (unless (marker-buffer org-clock-marker)
  18017. (error "No running clock"))
  18018. (org-with-remote-undo (marker-buffer org-clock-marker)
  18019. (org-clock-out)))
  18020. (defun org-agenda-clock-cancel (&optional arg)
  18021. "Cancel the currently running clock."
  18022. (interactive "P")
  18023. (unless (marker-buffer org-clock-marker)
  18024. (error "No running clock"))
  18025. (org-with-remote-undo (marker-buffer org-clock-marker)
  18026. (org-clock-cancel)))
  18027. (defun org-agenda-diary-entry ()
  18028. "Make a diary entry, like the `i' command from the calendar.
  18029. All the standard commands work: block, weekly etc."
  18030. (interactive)
  18031. (org-agenda-check-type t 'agenda 'timeline)
  18032. (require 'diary-lib)
  18033. (let* ((char (progn
  18034. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  18035. (read-char-exclusive)))
  18036. (cmd (cdr (assoc char
  18037. '((?d . insert-diary-entry)
  18038. (?w . insert-weekly-diary-entry)
  18039. (?m . insert-monthly-diary-entry)
  18040. (?y . insert-yearly-diary-entry)
  18041. (?a . insert-anniversary-diary-entry)
  18042. (?b . insert-block-diary-entry)
  18043. (?c . insert-cyclic-diary-entry)))))
  18044. (oldf (symbol-function 'calendar-cursor-to-date))
  18045. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  18046. (point (point))
  18047. (mark (or (mark t) (point))))
  18048. (unless cmd
  18049. (error "No command associated with <%c>" char))
  18050. (unless (and (get-text-property point 'day)
  18051. (or (not (equal ?b char))
  18052. (get-text-property mark 'day)))
  18053. (error "Don't know which date to use for diary entry"))
  18054. ;; We implement this by hacking the `calendar-cursor-to-date' function
  18055. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  18056. (let ((calendar-mark-ring
  18057. (list (calendar-gregorian-from-absolute
  18058. (or (get-text-property mark 'day)
  18059. (get-text-property point 'day))))))
  18060. (unwind-protect
  18061. (progn
  18062. (fset 'calendar-cursor-to-date
  18063. (lambda (&optional error)
  18064. (calendar-gregorian-from-absolute
  18065. (get-text-property point 'day))))
  18066. (call-interactively cmd))
  18067. (fset 'calendar-cursor-to-date oldf)))))
  18068. (defun org-agenda-execute-calendar-command (cmd)
  18069. "Execute a calendar command from the agenda, with the date associated to
  18070. the cursor position."
  18071. (org-agenda-check-type t 'agenda 'timeline)
  18072. (require 'diary-lib)
  18073. (unless (get-text-property (point) 'day)
  18074. (error "Don't know which date to use for calendar command"))
  18075. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  18076. (point (point))
  18077. (date (calendar-gregorian-from-absolute
  18078. (get-text-property point 'day)))
  18079. ;; the following 3 vars are needed in the calendar
  18080. (displayed-day (extract-calendar-day date))
  18081. (displayed-month (extract-calendar-month date))
  18082. (displayed-year (extract-calendar-year date)))
  18083. (unwind-protect
  18084. (progn
  18085. (fset 'calendar-cursor-to-date
  18086. (lambda (&optional error)
  18087. (calendar-gregorian-from-absolute
  18088. (get-text-property point 'day))))
  18089. (call-interactively cmd))
  18090. (fset 'calendar-cursor-to-date oldf))))
  18091. (defun org-agenda-phases-of-moon ()
  18092. "Display the phases of the moon for the 3 months around the cursor date."
  18093. (interactive)
  18094. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  18095. (defun org-agenda-holidays ()
  18096. "Display the holidays for the 3 months around the cursor date."
  18097. (interactive)
  18098. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  18099. (defun org-agenda-sunrise-sunset (arg)
  18100. "Display sunrise and sunset for the cursor date.
  18101. Latitude and longitude can be specified with the variables
  18102. `calendar-latitude' and `calendar-longitude'. When called with prefix
  18103. argument, latitude and longitude will be prompted for."
  18104. (interactive "P")
  18105. (let ((calendar-longitude (if arg nil calendar-longitude))
  18106. (calendar-latitude (if arg nil calendar-latitude))
  18107. (calendar-location-name
  18108. (if arg "the given coordinates" calendar-location-name)))
  18109. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  18110. (defun org-agenda-goto-calendar ()
  18111. "Open the Emacs calendar with the date at the cursor."
  18112. (interactive)
  18113. (org-agenda-check-type t 'agenda 'timeline)
  18114. (let* ((day (or (get-text-property (point) 'day)
  18115. (error "Don't know which date to open in calendar")))
  18116. (date (calendar-gregorian-from-absolute day))
  18117. (calendar-move-hook nil)
  18118. (view-calendar-holidays-initially nil)
  18119. (view-diary-entries-initially nil))
  18120. (calendar)
  18121. (calendar-goto-date date)))
  18122. (defun org-calendar-goto-agenda ()
  18123. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  18124. This is a command that has to be installed in `calendar-mode-map'."
  18125. (interactive)
  18126. (org-agenda-list nil (calendar-absolute-from-gregorian
  18127. (calendar-cursor-to-date))
  18128. nil))
  18129. (defun org-agenda-convert-date ()
  18130. (interactive)
  18131. (org-agenda-check-type t 'agenda 'timeline)
  18132. (let ((day (get-text-property (point) 'day))
  18133. date s)
  18134. (unless day
  18135. (error "Don't know which date to convert"))
  18136. (setq date (calendar-gregorian-from-absolute day))
  18137. (setq s (concat
  18138. "Gregorian: " (calendar-date-string date) "\n"
  18139. "ISO: " (calendar-iso-date-string date) "\n"
  18140. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  18141. "Julian: " (calendar-julian-date-string date) "\n"
  18142. "Astron. JD: " (calendar-astro-date-string date)
  18143. " (Julian date number at noon UTC)\n"
  18144. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  18145. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  18146. "French: " (calendar-french-date-string date) "\n"
  18147. "Mayan: " (calendar-mayan-date-string date) "\n"
  18148. "Coptic: " (calendar-coptic-date-string date) "\n"
  18149. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  18150. "Persian: " (calendar-persian-date-string date) "\n"
  18151. "Chinese: " (calendar-chinese-date-string date) "\n"))
  18152. (with-output-to-temp-buffer "*Dates*"
  18153. (princ s))
  18154. (if (fboundp 'fit-window-to-buffer)
  18155. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  18156. ;;;; Embedded LaTeX
  18157. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  18158. "Keymap for the minor `org-cdlatex-mode'.")
  18159. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  18160. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  18161. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  18162. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  18163. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  18164. (defvar org-cdlatex-texmathp-advice-is-done nil
  18165. "Flag remembering if we have applied the advice to texmathp already.")
  18166. (define-minor-mode org-cdlatex-mode
  18167. "Toggle the minor `org-cdlatex-mode'.
  18168. This mode supports entering LaTeX environment and math in LaTeX fragments
  18169. in Org-mode.
  18170. \\{org-cdlatex-mode-map}"
  18171. nil " OCDL" nil
  18172. (when org-cdlatex-mode (require 'cdlatex))
  18173. (unless org-cdlatex-texmathp-advice-is-done
  18174. (setq org-cdlatex-texmathp-advice-is-done t)
  18175. (defadvice texmathp (around org-math-always-on activate)
  18176. "Always return t in org-mode buffers.
  18177. This is because we want to insert math symbols without dollars even outside
  18178. the LaTeX math segments. If Orgmode thinks that point is actually inside
  18179. en embedded LaTeX fragement, let texmathp do its job.
  18180. \\[org-cdlatex-mode-map]"
  18181. (interactive)
  18182. (let (p)
  18183. (cond
  18184. ((not (org-mode-p)) ad-do-it)
  18185. ((eq this-command 'cdlatex-math-symbol)
  18186. (setq ad-return-value t
  18187. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  18188. (t
  18189. (let ((p (org-inside-LaTeX-fragment-p)))
  18190. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  18191. (setq ad-return-value t
  18192. texmathp-why '("Org-mode embedded math" . 0))
  18193. (if p ad-do-it)))))))))
  18194. (defun turn-on-org-cdlatex ()
  18195. "Unconditionally turn on `org-cdlatex-mode'."
  18196. (org-cdlatex-mode 1))
  18197. (defun org-inside-LaTeX-fragment-p ()
  18198. "Test if point is inside a LaTeX fragment.
  18199. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  18200. sequence appearing also before point.
  18201. Even though the matchers for math are configurable, this function assumes
  18202. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  18203. delimiters are skipped when they have been removed by customization.
  18204. The return value is nil, or a cons cell with the delimiter and
  18205. and the position of this delimiter.
  18206. This function does a reasonably good job, but can locally be fooled by
  18207. for example currency specifications. For example it will assume being in
  18208. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  18209. fragments that are properly closed, but during editing, we have to live
  18210. with the uncertainty caused by missing closing delimiters. This function
  18211. looks only before point, not after."
  18212. (catch 'exit
  18213. (let ((pos (point))
  18214. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  18215. (lim (progn
  18216. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  18217. (point)))
  18218. dd-on str (start 0) m re)
  18219. (goto-char pos)
  18220. (when dodollar
  18221. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  18222. re (nth 1 (assoc "$" org-latex-regexps)))
  18223. (while (string-match re str start)
  18224. (cond
  18225. ((= (match-end 0) (length str))
  18226. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  18227. ((= (match-end 0) (- (length str) 5))
  18228. (throw 'exit nil))
  18229. (t (setq start (match-end 0))))))
  18230. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  18231. (goto-char pos)
  18232. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  18233. (and (match-beginning 2) (throw 'exit nil))
  18234. ;; count $$
  18235. (while (re-search-backward "\\$\\$" lim t)
  18236. (setq dd-on (not dd-on)))
  18237. (goto-char pos)
  18238. (if dd-on (cons "$$" m))))))
  18239. (defun org-try-cdlatex-tab ()
  18240. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  18241. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  18242. - inside a LaTeX fragment, or
  18243. - after the first word in a line, where an abbreviation expansion could
  18244. insert a LaTeX environment."
  18245. (when org-cdlatex-mode
  18246. (cond
  18247. ((save-excursion
  18248. (skip-chars-backward "a-zA-Z0-9*")
  18249. (skip-chars-backward " \t")
  18250. (bolp))
  18251. (cdlatex-tab) t)
  18252. ((org-inside-LaTeX-fragment-p)
  18253. (cdlatex-tab) t)
  18254. (t nil))))
  18255. (defun org-cdlatex-underscore-caret (&optional arg)
  18256. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  18257. Revert to the normal definition outside of these fragments."
  18258. (interactive "P")
  18259. (if (org-inside-LaTeX-fragment-p)
  18260. (call-interactively 'cdlatex-sub-superscript)
  18261. (let (org-cdlatex-mode)
  18262. (call-interactively (key-binding (vector last-input-event))))))
  18263. (defun org-cdlatex-math-modify (&optional arg)
  18264. "Execute `cdlatex-math-modify' in LaTeX fragments.
  18265. Revert to the normal definition outside of these fragments."
  18266. (interactive "P")
  18267. (if (org-inside-LaTeX-fragment-p)
  18268. (call-interactively 'cdlatex-math-modify)
  18269. (let (org-cdlatex-mode)
  18270. (call-interactively (key-binding (vector last-input-event))))))
  18271. (defvar org-latex-fragment-image-overlays nil
  18272. "List of overlays carrying the images of latex fragments.")
  18273. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  18274. (defun org-remove-latex-fragment-image-overlays ()
  18275. "Remove all overlays with LaTeX fragment images in current buffer."
  18276. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  18277. (setq org-latex-fragment-image-overlays nil))
  18278. (defun org-preview-latex-fragment (&optional subtree)
  18279. "Preview the LaTeX fragment at point, or all locally or globally.
  18280. If the cursor is in a LaTeX fragment, create the image and overlay
  18281. it over the source code. If there is no fragment at point, display
  18282. all fragments in the current text, from one headline to the next. With
  18283. prefix SUBTREE, display all fragments in the current subtree. With a
  18284. double prefix `C-u C-u', or when the cursor is before the first headline,
  18285. display all fragments in the buffer.
  18286. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  18287. (interactive "P")
  18288. (org-remove-latex-fragment-image-overlays)
  18289. (save-excursion
  18290. (save-restriction
  18291. (let (beg end at msg)
  18292. (cond
  18293. ((or (equal subtree '(16))
  18294. (not (save-excursion
  18295. (re-search-backward (concat "^" outline-regexp) nil t))))
  18296. (setq beg (point-min) end (point-max)
  18297. msg "Creating images for buffer...%s"))
  18298. ((equal subtree '(4))
  18299. (org-back-to-heading)
  18300. (setq beg (point) end (org-end-of-subtree t)
  18301. msg "Creating images for subtree...%s"))
  18302. (t
  18303. (if (setq at (org-inside-LaTeX-fragment-p))
  18304. (goto-char (max (point-min) (- (cdr at) 2)))
  18305. (org-back-to-heading))
  18306. (setq beg (point) end (progn (outline-next-heading) (point))
  18307. msg (if at "Creating image...%s"
  18308. "Creating images for entry...%s"))))
  18309. (message msg "")
  18310. (narrow-to-region beg end)
  18311. (goto-char beg)
  18312. (org-format-latex
  18313. (concat "ltxpng/" (file-name-sans-extension
  18314. (file-name-nondirectory
  18315. buffer-file-name)))
  18316. default-directory 'overlays msg at 'forbuffer)
  18317. (message msg "done. Use `C-c C-c' to remove images.")))))
  18318. (defvar org-latex-regexps
  18319. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  18320. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  18321. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  18322. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  18323. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  18324. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  18325. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  18326. "Regular expressions for matching embedded LaTeX.")
  18327. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  18328. "Replace LaTeX fragments with links to an image, and produce images."
  18329. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  18330. (let* ((prefixnodir (file-name-nondirectory prefix))
  18331. (absprefix (expand-file-name prefix dir))
  18332. (todir (file-name-directory absprefix))
  18333. (opt org-format-latex-options)
  18334. (matchers (plist-get opt :matchers))
  18335. (re-list org-latex-regexps)
  18336. (cnt 0) txt link beg end re e checkdir
  18337. m n block linkfile movefile ov)
  18338. ;; Check if there are old images files with this prefix, and remove them
  18339. (when (file-directory-p todir)
  18340. (mapc 'delete-file
  18341. (directory-files
  18342. todir 'full
  18343. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  18344. ;; Check the different regular expressions
  18345. (while (setq e (pop re-list))
  18346. (setq m (car e) re (nth 1 e) n (nth 2 e)
  18347. block (if (nth 3 e) "\n\n" ""))
  18348. (when (member m matchers)
  18349. (goto-char (point-min))
  18350. (while (re-search-forward re nil t)
  18351. (when (or (not at) (equal (cdr at) (match-beginning n)))
  18352. (setq txt (match-string n)
  18353. beg (match-beginning n) end (match-end n)
  18354. cnt (1+ cnt)
  18355. linkfile (format "%s_%04d.png" prefix cnt)
  18356. movefile (format "%s_%04d.png" absprefix cnt)
  18357. link (concat block "[[file:" linkfile "]]" block))
  18358. (if msg (message msg cnt))
  18359. (goto-char beg)
  18360. (unless checkdir ; make sure the directory exists
  18361. (setq checkdir t)
  18362. (or (file-directory-p todir) (make-directory todir)))
  18363. (org-create-formula-image
  18364. txt movefile opt forbuffer)
  18365. (if overlays
  18366. (progn
  18367. (setq ov (org-make-overlay beg end))
  18368. (if (featurep 'xemacs)
  18369. (progn
  18370. (org-overlay-put ov 'invisible t)
  18371. (org-overlay-put
  18372. ov 'end-glyph
  18373. (make-glyph (vector 'png :file movefile))))
  18374. (org-overlay-put
  18375. ov 'display
  18376. (list 'image :type 'png :file movefile :ascent 'center)))
  18377. (push ov org-latex-fragment-image-overlays)
  18378. (goto-char end))
  18379. (delete-region beg end)
  18380. (insert link))))))))
  18381. ;; This function borrows from Ganesh Swami's latex2png.el
  18382. (defun org-create-formula-image (string tofile options buffer)
  18383. (let* ((tmpdir (if (featurep 'xemacs)
  18384. (temp-directory)
  18385. temporary-file-directory))
  18386. (texfilebase (make-temp-name
  18387. (expand-file-name "orgtex" tmpdir)))
  18388. (texfile (concat texfilebase ".tex"))
  18389. (dvifile (concat texfilebase ".dvi"))
  18390. (pngfile (concat texfilebase ".png"))
  18391. (fnh (face-attribute 'default :height nil))
  18392. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  18393. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  18394. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  18395. "Black"))
  18396. (bg (or (plist-get options (if buffer :background :html-background))
  18397. "Transparent")))
  18398. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  18399. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  18400. (with-temp-file texfile
  18401. (insert org-format-latex-header
  18402. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  18403. (let ((dir default-directory))
  18404. (condition-case nil
  18405. (progn
  18406. (cd tmpdir)
  18407. (call-process "latex" nil nil nil texfile))
  18408. (error nil))
  18409. (cd dir))
  18410. (if (not (file-exists-p dvifile))
  18411. (progn (message "Failed to create dvi file from %s" texfile) nil)
  18412. (call-process "dvipng" nil nil nil
  18413. "-E" "-fg" fg "-bg" bg
  18414. "-D" dpi
  18415. ;;"-x" scale "-y" scale
  18416. "-T" "tight"
  18417. "-o" pngfile
  18418. dvifile)
  18419. (if (not (file-exists-p pngfile))
  18420. (progn (message "Failed to create png file from %s" texfile) nil)
  18421. ;; Use the requested file name and clean up
  18422. (copy-file pngfile tofile 'replace)
  18423. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  18424. (delete-file (concat texfilebase e)))
  18425. pngfile))))
  18426. (defun org-dvipng-color (attr)
  18427. "Return an rgb color specification for dvipng."
  18428. (apply 'format "rgb %s %s %s"
  18429. (mapcar 'org-normalize-color
  18430. (color-values (face-attribute 'default attr nil)))))
  18431. (defun org-normalize-color (value)
  18432. "Return string to be used as color value for an RGB component."
  18433. (format "%g" (/ value 65535.0)))
  18434. ;;;; Exporting
  18435. ;;; Variables, constants, and parameter plists
  18436. (defconst org-level-max 20)
  18437. (defvar org-export-html-preamble nil
  18438. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  18439. (defvar org-export-html-postamble nil
  18440. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  18441. (defvar org-export-html-auto-preamble t
  18442. "Should default preamble be inserted? Set by publishing functions.")
  18443. (defvar org-export-html-auto-postamble t
  18444. "Should default postamble be inserted? Set by publishing functions.")
  18445. (defvar org-current-export-file nil) ; dynamically scoped parameter
  18446. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  18447. (defconst org-export-plist-vars
  18448. '((:language . org-export-default-language)
  18449. (:customtime . org-display-custom-times)
  18450. (:headline-levels . org-export-headline-levels)
  18451. (:section-numbers . org-export-with-section-numbers)
  18452. (:table-of-contents . org-export-with-toc)
  18453. (:preserve-breaks . org-export-preserve-breaks)
  18454. (:archived-trees . org-export-with-archived-trees)
  18455. (:emphasize . org-export-with-emphasize)
  18456. (:sub-superscript . org-export-with-sub-superscripts)
  18457. (:footnotes . org-export-with-footnotes)
  18458. (:property-drawer . org-export-with-property-drawer)
  18459. (:TeX-macros . org-export-with-TeX-macros)
  18460. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  18461. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  18462. (:fixed-width . org-export-with-fixed-width)
  18463. (:timestamps . org-export-with-timestamps)
  18464. (:author-info . org-export-author-info)
  18465. (:time-stamp-file . org-export-time-stamp-file)
  18466. (:tables . org-export-with-tables)
  18467. (:table-auto-headline . org-export-highlight-first-table-line)
  18468. (:style . org-export-html-style)
  18469. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  18470. (:convert-org-links . org-export-html-link-org-files-as-html)
  18471. (:inline-images . org-export-html-inline-images)
  18472. (:expand-quoted-html . org-export-html-expand)
  18473. (:timestamp . org-export-html-with-timestamp)
  18474. (:publishing-directory . org-export-publishing-directory)
  18475. (:preamble . org-export-html-preamble)
  18476. (:postamble . org-export-html-postamble)
  18477. (:auto-preamble . org-export-html-auto-preamble)
  18478. (:auto-postamble . org-export-html-auto-postamble)
  18479. (:author . user-full-name)
  18480. (:email . user-mail-address)))
  18481. (defun org-default-export-plist ()
  18482. "Return the property list with default settings for the export variables."
  18483. (let ((l org-export-plist-vars) rtn e)
  18484. (while (setq e (pop l))
  18485. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  18486. rtn))
  18487. (defun org-infile-export-plist ()
  18488. "Return the property list with file-local settings for export."
  18489. (save-excursion
  18490. (goto-char 0)
  18491. (let ((re (org-make-options-regexp
  18492. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  18493. p key val text options)
  18494. (while (re-search-forward re nil t)
  18495. (setq key (org-match-string-no-properties 1)
  18496. val (org-match-string-no-properties 2))
  18497. (cond
  18498. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  18499. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  18500. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  18501. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  18502. ((string-equal key "TEXT")
  18503. (setq text (if text (concat text "\n" val) val)))
  18504. ((string-equal key "OPTIONS") (setq options val))))
  18505. (setq p (plist-put p :text text))
  18506. (when options
  18507. (let ((op '(("H" . :headline-levels)
  18508. ("num" . :section-numbers)
  18509. ("toc" . :table-of-contents)
  18510. ("\\n" . :preserve-breaks)
  18511. ("@" . :expand-quoted-html)
  18512. (":" . :fixed-width)
  18513. ("|" . :tables)
  18514. ("^" . :sub-superscript)
  18515. ("f" . :footnotes)
  18516. ("p" . :property-drawer)
  18517. ("*" . :emphasize)
  18518. ("TeX" . :TeX-macros)
  18519. ("LaTeX" . :LaTeX-fragments)
  18520. ("skip" . :skip-before-1st-heading)
  18521. ("author" . :author-info)
  18522. ("timestamp" . :time-stamp-file)))
  18523. o)
  18524. (while (setq o (pop op))
  18525. (if (string-match (concat (regexp-quote (car o))
  18526. ":\\([^ \t\n\r;,.]*\\)")
  18527. options)
  18528. (setq p (plist-put p (cdr o)
  18529. (car (read-from-string
  18530. (match-string 1 options)))))))))
  18531. p)))
  18532. (defun org-export-directory (type plist)
  18533. (let* ((val (plist-get plist :publishing-directory))
  18534. (dir (if (listp val)
  18535. (or (cdr (assoc type val)) ".")
  18536. val)))
  18537. dir))
  18538. (defun org-skip-comments (lines)
  18539. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  18540. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  18541. (re2 "^\\(\\*+\\)[ \t\n\r]")
  18542. (case-fold-search nil)
  18543. rtn line level)
  18544. (while (setq line (pop lines))
  18545. (cond
  18546. ((and (string-match re1 line)
  18547. (setq level (- (match-end 1) (match-beginning 1))))
  18548. ;; Beginning of a COMMENT subtree. Skip it.
  18549. (while (and (setq line (pop lines))
  18550. (or (not (string-match re2 line))
  18551. (> (- (match-end 1) (match-beginning 1)) level))))
  18552. (setq lines (cons line lines)))
  18553. ((string-match "^#" line)
  18554. ;; an ordinary comment line
  18555. )
  18556. ((and org-export-table-remove-special-lines
  18557. (string-match "^[ \t]*|" line)
  18558. (or (string-match "^[ \t]*| *[!_^] *|" line)
  18559. (and (string-match "| *<[0-9]+> *|" line)
  18560. (not (string-match "| *[^ <|]" line)))))
  18561. ;; a special table line that should be removed
  18562. )
  18563. (t (setq rtn (cons line rtn)))))
  18564. (nreverse rtn)))
  18565. (defun org-export (&optional arg)
  18566. (interactive)
  18567. (let ((help "[t] insert the export option template
  18568. \[v] limit export to visible part of outline tree
  18569. \[a] export as ASCII
  18570. \[h] export as HTML
  18571. \[H] export as HTML to temporary buffer
  18572. \[R] export region as HTML
  18573. \[b] export as HTML and browse immediately
  18574. \[x] export as XOXO
  18575. \[l] export as LaTeX
  18576. \[L] export as LaTeX to temporary buffer
  18577. \[i] export current file as iCalendar file
  18578. \[I] export all agenda files as iCalendar files
  18579. \[c] export agenda files into combined iCalendar file
  18580. \[F] publish current file
  18581. \[P] publish current project
  18582. \[X] publish... (project will be prompted for)
  18583. \[A] publish all projects")
  18584. (cmds
  18585. '((?t . org-insert-export-options-template)
  18586. (?v . org-export-visible)
  18587. (?a . org-export-as-ascii)
  18588. (?h . org-export-as-html)
  18589. (?b . org-export-as-html-and-open)
  18590. (?H . org-export-as-html-to-buffer)
  18591. (?R . org-export-region-as-html)
  18592. (?x . org-export-as-xoxo)
  18593. (?l . org-export-as-latex)
  18594. (?L . org-export-as-latex-to-buffer)
  18595. (?i . org-export-icalendar-this-file)
  18596. (?I . org-export-icalendar-all-agenda-files)
  18597. (?c . org-export-icalendar-combine-agenda-files)
  18598. (?F . org-publish-current-file)
  18599. (?P . org-publish-current-project)
  18600. (?X . org-publish)
  18601. (?A . org-publish-all)))
  18602. r1 r2 ass)
  18603. (save-window-excursion
  18604. (delete-other-windows)
  18605. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  18606. (princ help))
  18607. (message "Select command: ")
  18608. (setq r1 (read-char-exclusive)))
  18609. (setq r2 (if (< r1 27) (+ r1 96) r1))
  18610. (if (setq ass (assq r2 cmds))
  18611. (call-interactively (cdr ass))
  18612. (error "No command associated with key %c" r1))))
  18613. (defconst org-html-entities
  18614. '(("nbsp")
  18615. ("iexcl")
  18616. ("cent")
  18617. ("pound")
  18618. ("curren")
  18619. ("yen")
  18620. ("brvbar")
  18621. ("vert" . "&#124;")
  18622. ("sect")
  18623. ("uml")
  18624. ("copy")
  18625. ("ordf")
  18626. ("laquo")
  18627. ("not")
  18628. ("shy")
  18629. ("reg")
  18630. ("macr")
  18631. ("deg")
  18632. ("plusmn")
  18633. ("sup2")
  18634. ("sup3")
  18635. ("acute")
  18636. ("micro")
  18637. ("para")
  18638. ("middot")
  18639. ("odot"."o")
  18640. ("star"."*")
  18641. ("cedil")
  18642. ("sup1")
  18643. ("ordm")
  18644. ("raquo")
  18645. ("frac14")
  18646. ("frac12")
  18647. ("frac34")
  18648. ("iquest")
  18649. ("Agrave")
  18650. ("Aacute")
  18651. ("Acirc")
  18652. ("Atilde")
  18653. ("Auml")
  18654. ("Aring") ("AA"."&Aring;")
  18655. ("AElig")
  18656. ("Ccedil")
  18657. ("Egrave")
  18658. ("Eacute")
  18659. ("Ecirc")
  18660. ("Euml")
  18661. ("Igrave")
  18662. ("Iacute")
  18663. ("Icirc")
  18664. ("Iuml")
  18665. ("ETH")
  18666. ("Ntilde")
  18667. ("Ograve")
  18668. ("Oacute")
  18669. ("Ocirc")
  18670. ("Otilde")
  18671. ("Ouml")
  18672. ("times")
  18673. ("Oslash")
  18674. ("Ugrave")
  18675. ("Uacute")
  18676. ("Ucirc")
  18677. ("Uuml")
  18678. ("Yacute")
  18679. ("THORN")
  18680. ("szlig")
  18681. ("agrave")
  18682. ("aacute")
  18683. ("acirc")
  18684. ("atilde")
  18685. ("auml")
  18686. ("aring")
  18687. ("aelig")
  18688. ("ccedil")
  18689. ("egrave")
  18690. ("eacute")
  18691. ("ecirc")
  18692. ("euml")
  18693. ("igrave")
  18694. ("iacute")
  18695. ("icirc")
  18696. ("iuml")
  18697. ("eth")
  18698. ("ntilde")
  18699. ("ograve")
  18700. ("oacute")
  18701. ("ocirc")
  18702. ("otilde")
  18703. ("ouml")
  18704. ("divide")
  18705. ("oslash")
  18706. ("ugrave")
  18707. ("uacute")
  18708. ("ucirc")
  18709. ("uuml")
  18710. ("yacute")
  18711. ("thorn")
  18712. ("yuml")
  18713. ("fnof")
  18714. ("Alpha")
  18715. ("Beta")
  18716. ("Gamma")
  18717. ("Delta")
  18718. ("Epsilon")
  18719. ("Zeta")
  18720. ("Eta")
  18721. ("Theta")
  18722. ("Iota")
  18723. ("Kappa")
  18724. ("Lambda")
  18725. ("Mu")
  18726. ("Nu")
  18727. ("Xi")
  18728. ("Omicron")
  18729. ("Pi")
  18730. ("Rho")
  18731. ("Sigma")
  18732. ("Tau")
  18733. ("Upsilon")
  18734. ("Phi")
  18735. ("Chi")
  18736. ("Psi")
  18737. ("Omega")
  18738. ("alpha")
  18739. ("beta")
  18740. ("gamma")
  18741. ("delta")
  18742. ("epsilon")
  18743. ("varepsilon"."&epsilon;")
  18744. ("zeta")
  18745. ("eta")
  18746. ("theta")
  18747. ("iota")
  18748. ("kappa")
  18749. ("lambda")
  18750. ("mu")
  18751. ("nu")
  18752. ("xi")
  18753. ("omicron")
  18754. ("pi")
  18755. ("rho")
  18756. ("sigmaf") ("varsigma"."&sigmaf;")
  18757. ("sigma")
  18758. ("tau")
  18759. ("upsilon")
  18760. ("phi")
  18761. ("chi")
  18762. ("psi")
  18763. ("omega")
  18764. ("thetasym") ("vartheta"."&thetasym;")
  18765. ("upsih")
  18766. ("piv")
  18767. ("bull") ("bullet"."&bull;")
  18768. ("hellip") ("dots"."&hellip;")
  18769. ("prime")
  18770. ("Prime")
  18771. ("oline")
  18772. ("frasl")
  18773. ("weierp")
  18774. ("image")
  18775. ("real")
  18776. ("trade")
  18777. ("alefsym")
  18778. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  18779. ("uarr") ("uparrow"."&uarr;")
  18780. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  18781. ("darr")("downarrow"."&darr;")
  18782. ("harr") ("leftrightarrow"."&harr;")
  18783. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  18784. ("lArr") ("Leftarrow"."&lArr;")
  18785. ("uArr") ("Uparrow"."&uArr;")
  18786. ("rArr") ("Rightarrow"."&rArr;")
  18787. ("dArr") ("Downarrow"."&dArr;")
  18788. ("hArr") ("Leftrightarrow"."&hArr;")
  18789. ("forall")
  18790. ("part") ("partial"."&part;")
  18791. ("exist") ("exists"."&exist;")
  18792. ("empty") ("emptyset"."&empty;")
  18793. ("nabla")
  18794. ("isin") ("in"."&isin;")
  18795. ("notin")
  18796. ("ni")
  18797. ("prod")
  18798. ("sum")
  18799. ("minus")
  18800. ("lowast") ("ast"."&lowast;")
  18801. ("radic")
  18802. ("prop") ("proptp"."&prop;")
  18803. ("infin") ("infty"."&infin;")
  18804. ("ang") ("angle"."&ang;")
  18805. ("and") ("vee"."&and;")
  18806. ("or") ("wedge"."&or;")
  18807. ("cap")
  18808. ("cup")
  18809. ("int")
  18810. ("there4")
  18811. ("sim")
  18812. ("cong") ("simeq"."&cong;")
  18813. ("asymp")("approx"."&asymp;")
  18814. ("ne") ("neq"."&ne;")
  18815. ("equiv")
  18816. ("le")
  18817. ("ge")
  18818. ("sub") ("subset"."&sub;")
  18819. ("sup") ("supset"."&sup;")
  18820. ("nsub")
  18821. ("sube")
  18822. ("supe")
  18823. ("oplus")
  18824. ("otimes")
  18825. ("perp")
  18826. ("sdot") ("cdot"."&sdot;")
  18827. ("lceil")
  18828. ("rceil")
  18829. ("lfloor")
  18830. ("rfloor")
  18831. ("lang")
  18832. ("rang")
  18833. ("loz") ("Diamond"."&loz;")
  18834. ("spades") ("spadesuit"."&spades;")
  18835. ("clubs") ("clubsuit"."&clubs;")
  18836. ("hearts") ("diamondsuit"."&hearts;")
  18837. ("diams") ("diamondsuit"."&diams;")
  18838. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  18839. ("quot")
  18840. ("amp")
  18841. ("lt")
  18842. ("gt")
  18843. ("OElig")
  18844. ("oelig")
  18845. ("Scaron")
  18846. ("scaron")
  18847. ("Yuml")
  18848. ("circ")
  18849. ("tilde")
  18850. ("ensp")
  18851. ("emsp")
  18852. ("thinsp")
  18853. ("zwnj")
  18854. ("zwj")
  18855. ("lrm")
  18856. ("rlm")
  18857. ("ndash")
  18858. ("mdash")
  18859. ("lsquo")
  18860. ("rsquo")
  18861. ("sbquo")
  18862. ("ldquo")
  18863. ("rdquo")
  18864. ("bdquo")
  18865. ("dagger")
  18866. ("Dagger")
  18867. ("permil")
  18868. ("lsaquo")
  18869. ("rsaquo")
  18870. ("euro")
  18871. ("arccos"."arccos")
  18872. ("arcsin"."arcsin")
  18873. ("arctan"."arctan")
  18874. ("arg"."arg")
  18875. ("cos"."cos")
  18876. ("cosh"."cosh")
  18877. ("cot"."cot")
  18878. ("coth"."coth")
  18879. ("csc"."csc")
  18880. ("deg"."deg")
  18881. ("det"."det")
  18882. ("dim"."dim")
  18883. ("exp"."exp")
  18884. ("gcd"."gcd")
  18885. ("hom"."hom")
  18886. ("inf"."inf")
  18887. ("ker"."ker")
  18888. ("lg"."lg")
  18889. ("lim"."lim")
  18890. ("liminf"."liminf")
  18891. ("limsup"."limsup")
  18892. ("ln"."ln")
  18893. ("log"."log")
  18894. ("max"."max")
  18895. ("min"."min")
  18896. ("Pr"."Pr")
  18897. ("sec"."sec")
  18898. ("sin"."sin")
  18899. ("sinh"."sinh")
  18900. ("sup"."sup")
  18901. ("tan"."tan")
  18902. ("tanh"."tanh")
  18903. )
  18904. "Entities for TeX->HTML translation.
  18905. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  18906. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  18907. In that case, \"\\ent\" will be translated to \"&other;\".
  18908. The list contains HTML entities for Latin-1, Greek and other symbols.
  18909. It is supplemented by a number of commonly used TeX macros with appropriate
  18910. translations. There is currently no way for users to extend this.")
  18911. ;;; General functions for all backends
  18912. (defun org-cleaned-string-for-export (string &rest parameters)
  18913. "Cleanup a buffer STRING so that links can be created safely."
  18914. (interactive)
  18915. (let* ((re-radio (and org-target-link-regexp
  18916. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  18917. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  18918. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  18919. (re-archive (concat ":" org-archive-tag ":"))
  18920. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  18921. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  18922. (htmlp (plist-get parameters :for-html))
  18923. (asciip (plist-get parameters :for-ascii))
  18924. (latexp (plist-get parameters :for-LaTeX))
  18925. (commentsp (plist-get parameters :comments))
  18926. (inhibit-read-only t)
  18927. (outline-regexp "\\*+ ")
  18928. a b xx
  18929. rtn p)
  18930. (with-current-buffer (get-buffer-create " org-mode-tmp")
  18931. (erase-buffer)
  18932. (insert string)
  18933. ;; Remove license-to-kill stuff
  18934. (while (setq p (text-property-any (point-min) (point-max)
  18935. :org-license-to-kill t))
  18936. (delete-region p (next-single-property-change p :org-license-to-kill)))
  18937. (let ((org-inhibit-startup t)) (org-mode))
  18938. (untabify (point-min) (point-max))
  18939. ;; Get the correct stuff before the first headline
  18940. (when (plist-get parameters :skip-before-1st-heading)
  18941. (goto-char (point-min))
  18942. (when (re-search-forward "^\\*+[ \t]" nil t)
  18943. (delete-region (point-min) (match-beginning 0))
  18944. (goto-char (point-min))
  18945. (insert "\n")))
  18946. (when (plist-get parameters :add-text)
  18947. (goto-char (point-min))
  18948. (insert (plist-get parameters :add-text) "\n"))
  18949. ;; Get rid of archived trees
  18950. (when (not (eq org-export-with-archived-trees t))
  18951. (goto-char (point-min))
  18952. (while (re-search-forward re-archive nil t)
  18953. (if (not (org-on-heading-p t))
  18954. (org-end-of-subtree t)
  18955. (beginning-of-line 1)
  18956. (setq a (if org-export-with-archived-trees
  18957. (1+ (point-at-eol)) (point))
  18958. b (org-end-of-subtree t))
  18959. (if (> b a) (delete-region a b)))))
  18960. ;; Get rid of property drawers
  18961. (unless org-export-with-property-drawer
  18962. (goto-char (point-min))
  18963. (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t)
  18964. (replace-match "")))
  18965. ;; Find targets in comments and move them out of comments,
  18966. ;; but mark them as targets that should be invisible
  18967. (goto-char (point-min))
  18968. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  18969. (replace-match "\\1(INVISIBLE)"))
  18970. ;; Protect backend specific stuff, throw away the others.
  18971. (goto-char (point-min))
  18972. (let ((formatters
  18973. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  18974. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  18975. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  18976. fmt)
  18977. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  18978. (add-text-properties (match-beginning 0) (match-end 0)
  18979. '(org-protected t)))
  18980. (while formatters
  18981. (setq fmt (pop formatters))
  18982. (when (car fmt)
  18983. (goto-char (point-min))
  18984. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  18985. ":[ \t]*\\(.*\\)") nil t)
  18986. (replace-match "\\1" t)
  18987. (add-text-properties
  18988. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  18989. '(org-protected t))))
  18990. (goto-char (point-min))
  18991. (while (re-search-forward
  18992. (concat "^#\\+"
  18993. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  18994. (cadddr fmt) "\\>.*\n?") nil t)
  18995. (if (car fmt)
  18996. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  18997. '(org-protected t))
  18998. (delete-region (match-beginning 0) (match-end 0))))))
  18999. ;; Protect quoted subtreedes
  19000. (goto-char (point-min))
  19001. (while (re-search-forward re-quote nil t)
  19002. (goto-char (match-beginning 0))
  19003. (end-of-line 1)
  19004. (add-text-properties (point) (org-end-of-subtree t)
  19005. '(org-protected t)))
  19006. ;; Remove subtrees that are commented
  19007. (goto-char (point-min))
  19008. (while (re-search-forward re-commented nil t)
  19009. (goto-char (match-beginning 0))
  19010. (delete-region (point) (org-end-of-subtree t)))
  19011. ;; Remove special table lines
  19012. (when org-export-table-remove-special-lines
  19013. (goto-char (point-min))
  19014. (while (re-search-forward "^[ \t]*|" nil t)
  19015. (beginning-of-line 1)
  19016. (if (or (looking-at "[ \t]*| *[!_^] *|")
  19017. (and (looking-at ".*?| *<[0-9]+> *|")
  19018. (not (looking-at ".*?| *[^ <|]"))))
  19019. (delete-region (max (point-min) (1- (point-at-bol)))
  19020. (point-at-eol))
  19021. (end-of-line 1))))
  19022. ;; Specific LaTeX cleaning
  19023. (when latexp
  19024. (require 'org-export-latex nil t)
  19025. (org-export-latex-cleaned-string))
  19026. ;; Remove or replace comments
  19027. ;; If :comments is set, use this char for commenting out comments and
  19028. ;; protect them. otherwise delete them
  19029. (goto-char (point-min))
  19030. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  19031. (if commentsp
  19032. (progn (add-text-properties
  19033. (match-beginning 0) (match-end 0) '(org-protected t))
  19034. (replace-match (format commentsp (match-string 1)) t t))
  19035. (replace-match "")))
  19036. ;; Find matches for radio targets and turn them into internal links
  19037. (goto-char (point-min))
  19038. (when re-radio
  19039. (while (re-search-forward re-radio nil t)
  19040. (org-if-unprotected
  19041. (replace-match "\\1[[\\2]]"))))
  19042. ;; Find all links that contain a newline and put them into a single line
  19043. (goto-char (point-min))
  19044. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  19045. (org-if-unprotected
  19046. (replace-match "\\1 \\3")
  19047. (goto-char (match-beginning 0))))
  19048. ;; Convert LaTeX fragments to images
  19049. (when (plist-get parameters :LaTeX-fragments)
  19050. (org-format-latex
  19051. (concat "ltxpng/" (file-name-sans-extension
  19052. (file-name-nondirectory
  19053. org-current-export-file)))
  19054. org-current-export-dir nil "Creating LaTeX image %s"))
  19055. (message "Exporting...")
  19056. ;; Normalize links: Convert angle and plain links into bracket links
  19057. ;; Expand link abbreviations
  19058. (goto-char (point-min))
  19059. (while (re-search-forward re-plain-link nil t)
  19060. (goto-char (1- (match-end 0)))
  19061. (org-if-unprotected
  19062. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  19063. ":" (match-string 3) "]]")))
  19064. ;; added 'org-link face to links
  19065. (put-text-property 0 (length s) 'face 'org-link s)
  19066. (replace-match s t t))))
  19067. (goto-char (point-min))
  19068. (while (re-search-forward re-angle-link nil t)
  19069. (goto-char (1- (match-end 0)))
  19070. (org-if-unprotected
  19071. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  19072. ":" (match-string 3) "]]")))
  19073. (put-text-property 0 (length s) 'face 'org-link s)
  19074. (replace-match s t t))))
  19075. (goto-char (point-min))
  19076. (while (re-search-forward org-bracket-link-regexp nil t)
  19077. (org-if-unprotected
  19078. (let* ((s (concat "[[" (setq xx (save-match-data
  19079. (org-link-expand-abbrev (match-string 1))))
  19080. "]"
  19081. (if (match-end 3)
  19082. (match-string 2)
  19083. (concat "[" xx "]"))
  19084. "]")))
  19085. (put-text-property 0 (length s) 'face 'org-link s)
  19086. (replace-match s t t))))
  19087. ;; Find multiline emphasis and put them into single line
  19088. (when (plist-get parameters :emph-multiline)
  19089. (goto-char (point-min))
  19090. (while (re-search-forward org-emph-re nil t)
  19091. (if (not (= (char-after (match-beginning 3))
  19092. (char-after (match-beginning 4))))
  19093. (org-if-unprotected
  19094. (subst-char-in-region (match-beginning 0) (match-end 0)
  19095. ?\n ?\ t)
  19096. (goto-char (1- (match-end 0))))
  19097. (goto-char (1+ (match-beginning 0))))))
  19098. (setq rtn (buffer-string)))
  19099. (kill-buffer " org-mode-tmp")
  19100. rtn))
  19101. (defun org-export-grab-title-from-buffer ()
  19102. "Get a title for the current document, from looking at the buffer."
  19103. (let ((inhibit-read-only t))
  19104. (save-excursion
  19105. (goto-char (point-min))
  19106. (let ((end (save-excursion (outline-next-heading) (point))))
  19107. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  19108. ;; Mark the line so that it will not be exported as normal text.
  19109. (org-unmodified
  19110. (add-text-properties (match-beginning 0) (match-end 0)
  19111. (list :org-license-to-kill t)))
  19112. ;; Return the title string
  19113. (org-trim (match-string 0)))))))
  19114. (defun org-solidify-link-text (s &optional alist)
  19115. "Take link text and make a safe target out of it."
  19116. (save-match-data
  19117. (let* ((rtn
  19118. (mapconcat
  19119. 'identity
  19120. (org-split-string s "[ \t\r\n]+") "--"))
  19121. (a (assoc rtn alist)))
  19122. (or (cdr a) rtn))))
  19123. ;; Variable holding the vector with section numbers
  19124. (defvar org-section-numbers (make-vector org-level-max 0))
  19125. (defun org-init-section-numbers ()
  19126. "Initialize the vector for the section numbers."
  19127. (let* ((level -1)
  19128. (numbers (nreverse (org-split-string "" "\\.")))
  19129. (depth (1- (length org-section-numbers)))
  19130. (i depth) number-string)
  19131. (while (>= i 0)
  19132. (if (> i level)
  19133. (aset org-section-numbers i 0)
  19134. (setq number-string (or (car numbers) "0"))
  19135. (if (string-match "\\`[A-Z]\\'" number-string)
  19136. (aset org-section-numbers i
  19137. (- (string-to-char number-string) ?A -1))
  19138. (aset org-section-numbers i (string-to-number number-string)))
  19139. (pop numbers))
  19140. (setq i (1- i)))))
  19141. (defun org-section-number (&optional level)
  19142. "Return a string with the current section number.
  19143. When LEVEL is non-nil, increase section numbers on that level."
  19144. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  19145. (when level
  19146. (when (> level -1)
  19147. (aset org-section-numbers
  19148. level (1+ (aref org-section-numbers level))))
  19149. (setq idx (1+ level))
  19150. (while (<= idx depth)
  19151. (if (not (= idx 1))
  19152. (aset org-section-numbers idx 0))
  19153. (setq idx (1+ idx))))
  19154. (setq idx 0)
  19155. (while (<= idx depth)
  19156. (setq n (aref org-section-numbers idx))
  19157. (setq string (concat string (if (not (string= string "")) "." "")
  19158. (int-to-string n)))
  19159. (setq idx (1+ idx)))
  19160. (save-match-data
  19161. (if (string-match "\\`\\([@0]\\.\\)+" string)
  19162. (setq string (replace-match "" t nil string)))
  19163. (if (string-match "\\(\\.0\\)+\\'" string)
  19164. (setq string (replace-match "" t nil string))))
  19165. string))
  19166. ;;; ASCII export
  19167. (defvar org-last-level nil) ; dynamically scoped variable
  19168. (defvar org-levels-open nil) ; dynamically scoped parameter
  19169. (defvar org-ascii-current-indentation nil) ; For communication
  19170. (defun org-export-as-ascii (arg)
  19171. "Export the outline as a pretty ASCII file.
  19172. If there is an active region, export only the region.
  19173. The prefix ARG specifies how many levels of the outline should become
  19174. underlined headlines. The default is 3."
  19175. (interactive "P")
  19176. (setq-default org-todo-line-regexp org-todo-line-regexp)
  19177. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19178. (org-infile-export-plist)))
  19179. (custom-times org-display-custom-times)
  19180. (org-ascii-current-indentation '(0 . 0))
  19181. (level 0) line txt
  19182. (umax nil)
  19183. (umax-toc nil)
  19184. (case-fold-search nil)
  19185. (filename (concat (file-name-as-directory
  19186. (org-export-directory :ascii opt-plist))
  19187. (file-name-sans-extension
  19188. (file-name-nondirectory buffer-file-name))
  19189. ".txt"))
  19190. (filename (if (equal (file-truename filename)
  19191. (file-truename buffer-file-name))
  19192. (concat filename ".txt")
  19193. filename))
  19194. (buffer (find-file-noselect filename))
  19195. (org-levels-open (make-vector org-level-max nil))
  19196. (odd org-odd-levels-only)
  19197. (date (format-time-string "%Y/%m/%d" (current-time)))
  19198. (time (format-time-string "%X" (org-current-time)))
  19199. (author (plist-get opt-plist :author))
  19200. (title (or (plist-get opt-plist :title)
  19201. (and (not
  19202. (plist-get opt-plist :skip-before-1st-heading))
  19203. (org-export-grab-title-from-buffer))
  19204. (file-name-sans-extension
  19205. (file-name-nondirectory buffer-file-name))))
  19206. (email (plist-get opt-plist :email))
  19207. (language (plist-get opt-plist :language))
  19208. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  19209. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  19210. (todo nil)
  19211. (lang-words nil)
  19212. (region
  19213. (buffer-substring
  19214. (if (org-region-active-p) (region-beginning) (point-min))
  19215. (if (org-region-active-p) (region-end) (point-max))))
  19216. (lines (org-split-string
  19217. (org-cleaned-string-for-export
  19218. region
  19219. :for-ascii t
  19220. :skip-before-1st-heading
  19221. (plist-get opt-plist :skip-before-1st-heading)
  19222. :add-text (plist-get opt-plist :text))
  19223. "[\r\n]")) ;; FIXME: why \r here???/
  19224. thetoc have-headings first-heading-pos
  19225. table-open table-buffer)
  19226. (let ((inhibit-read-only t))
  19227. (org-unmodified
  19228. (remove-text-properties (point-min) (point-max)
  19229. '(:org-license-to-kill t))))
  19230. (setq org-last-level 1)
  19231. (org-init-section-numbers)
  19232. (find-file-noselect filename)
  19233. (setq lang-words (or (assoc language org-export-language-setup)
  19234. (assoc "en" org-export-language-setup)))
  19235. (switch-to-buffer-other-window buffer)
  19236. (erase-buffer)
  19237. (fundamental-mode)
  19238. ;; create local variables for all options, to make sure all called
  19239. ;; functions get the correct information
  19240. (mapcar (lambda (x)
  19241. (set (make-local-variable (cdr x))
  19242. (plist-get opt-plist (car x))))
  19243. org-export-plist-vars)
  19244. (org-set-local 'org-odd-levels-only odd)
  19245. (setq umax (if arg (prefix-numeric-value arg)
  19246. org-export-headline-levels))
  19247. (setq umax-toc (if (integerp org-export-with-toc)
  19248. (min org-export-with-toc umax)
  19249. umax))
  19250. ;; File header
  19251. (if title (org-insert-centered title ?=))
  19252. (insert "\n")
  19253. (if (and (or author email)
  19254. org-export-author-info)
  19255. (insert (concat (nth 1 lang-words) ": " (or author "")
  19256. (if email (concat " <" email ">") "")
  19257. "\n")))
  19258. (if (and date time org-export-time-stamp-file)
  19259. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  19260. (insert "\n\n")
  19261. (if org-export-with-toc
  19262. (progn
  19263. (push (concat (nth 3 lang-words) "\n") thetoc)
  19264. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  19265. (mapcar '(lambda (line)
  19266. (if (string-match org-todo-line-regexp
  19267. line)
  19268. ;; This is a headline
  19269. (progn
  19270. (setq have-headings t)
  19271. (setq level (- (match-end 1) (match-beginning 1))
  19272. level (org-tr-level level)
  19273. txt (match-string 3 line)
  19274. todo
  19275. (or (and org-export-mark-todo-in-toc
  19276. (match-beginning 2)
  19277. (not (member (match-string 2 line)
  19278. org-done-keywords)))
  19279. ; TODO, not DONE
  19280. (and org-export-mark-todo-in-toc
  19281. (= level umax-toc)
  19282. (org-search-todo-below
  19283. line lines level))))
  19284. (setq txt (org-html-expand-for-ascii txt))
  19285. (if (and (memq org-export-with-tags '(not-in-toc nil))
  19286. (string-match
  19287. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  19288. txt))
  19289. (setq txt (replace-match "" t t txt)))
  19290. (if (string-match quote-re0 txt)
  19291. (setq txt (replace-match "" t t txt)))
  19292. (if org-export-with-section-numbers
  19293. (setq txt (concat (org-section-number level)
  19294. " " txt)))
  19295. (if (<= level umax-toc)
  19296. (progn
  19297. (push
  19298. (concat
  19299. (make-string (* (1- level) 4) ?\ )
  19300. (format (if todo "%s (*)\n" "%s\n") txt))
  19301. thetoc)
  19302. (setq org-last-level level))
  19303. ))))
  19304. lines)
  19305. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  19306. (org-init-section-numbers)
  19307. (while (setq line (pop lines))
  19308. ;; Remove the quoted HTML tags.
  19309. (setq line (org-html-expand-for-ascii line))
  19310. ;; Remove targets
  19311. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  19312. (setq line (replace-match "" t t line)))
  19313. ;; Replace internal links
  19314. (while (string-match org-bracket-link-regexp line)
  19315. (setq line (replace-match
  19316. (if (match-end 3) "[\\3]" "[\\1]")
  19317. t nil line)))
  19318. (when custom-times
  19319. (setq line (org-translate-time line)))
  19320. (cond
  19321. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  19322. ;; a Headline
  19323. (setq first-heading-pos (or first-heading-pos (point)))
  19324. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  19325. txt (match-string 2 line))
  19326. (org-ascii-level-start level txt umax lines))
  19327. ((and org-export-with-tables
  19328. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  19329. (if (not table-open)
  19330. ;; New table starts
  19331. (setq table-open t table-buffer nil))
  19332. ;; Accumulate lines
  19333. (setq table-buffer (cons line table-buffer))
  19334. (when (or (not lines)
  19335. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  19336. (car lines))))
  19337. (setq table-open nil
  19338. table-buffer (nreverse table-buffer))
  19339. (insert (mapconcat
  19340. (lambda (x)
  19341. (org-fix-indentation x org-ascii-current-indentation))
  19342. (org-format-table-ascii table-buffer)
  19343. "\n") "\n")))
  19344. (t
  19345. (setq line (org-fix-indentation line org-ascii-current-indentation))
  19346. (if (and org-export-with-fixed-width
  19347. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  19348. (setq line (replace-match "\\1" nil nil line)))
  19349. (insert line "\n"))))
  19350. (normal-mode)
  19351. ;; insert the table of contents
  19352. (when thetoc
  19353. (goto-char (point-min))
  19354. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  19355. (progn
  19356. (goto-char (match-beginning 0))
  19357. (replace-match ""))
  19358. (goto-char first-heading-pos))
  19359. (mapc 'insert thetoc)
  19360. (or (looking-at "[ \t]*\n[ \t]*\n")
  19361. (insert "\n\n")))
  19362. (save-buffer)
  19363. ;; remove display and invisible chars
  19364. (let (beg end)
  19365. (goto-char (point-min))
  19366. (while (setq beg (next-single-property-change (point) 'display))
  19367. (setq end (next-single-property-change beg 'display))
  19368. (delete-region beg end)
  19369. (goto-char beg)
  19370. (insert "=>"))
  19371. (goto-char (point-min))
  19372. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  19373. (setq end (next-single-property-change beg 'org-cwidth))
  19374. (delete-region beg end)
  19375. (goto-char beg)))
  19376. (goto-char (point-min))))
  19377. (defun org-search-todo-below (line lines level)
  19378. "Search the subtree below LINE for any TODO entries."
  19379. (let ((rest (cdr (memq line lines)))
  19380. (re org-todo-line-regexp)
  19381. line lv todo)
  19382. (catch 'exit
  19383. (while (setq line (pop rest))
  19384. (if (string-match re line)
  19385. (progn
  19386. (setq lv (- (match-end 1) (match-beginning 1))
  19387. todo (and (match-beginning 2)
  19388. (not (member (match-string 2 line)
  19389. org-done-keywords))))
  19390. ; TODO, not DONE
  19391. (if (<= lv level) (throw 'exit nil))
  19392. (if todo (throw 'exit t))))))))
  19393. (defun org-html-expand-for-ascii (line)
  19394. "Handle quoted HTML for ASCII export."
  19395. (if org-export-html-expand
  19396. (while (string-match "@<[^<>\n]*>" line)
  19397. ;; We just remove the tags for now.
  19398. (setq line (replace-match "" nil nil line))))
  19399. line)
  19400. (defun org-insert-centered (s &optional underline)
  19401. "Insert the string S centered and underline it with character UNDERLINE."
  19402. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  19403. (insert (make-string ind ?\ ) s "\n")
  19404. (if underline
  19405. (insert (make-string ind ?\ )
  19406. (make-string (string-width s) underline)
  19407. "\n"))))
  19408. (defun org-ascii-level-start (level title umax &optional lines)
  19409. "Insert a new level in ASCII export."
  19410. (let (char (n (- level umax 1)) (ind 0))
  19411. (if (> level umax)
  19412. (progn
  19413. (insert (make-string (* 2 n) ?\ )
  19414. (char-to-string (nth (% n (length org-export-ascii-bullets))
  19415. org-export-ascii-bullets))
  19416. " " title "\n")
  19417. ;; find the indentation of the next non-empty line
  19418. (catch 'stop
  19419. (while lines
  19420. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  19421. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  19422. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  19423. (pop lines)))
  19424. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  19425. (if (or (not (equal (char-before) ?\n))
  19426. (not (equal (char-before (1- (point))) ?\n)))
  19427. (insert "\n"))
  19428. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  19429. (unless org-export-with-tags
  19430. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  19431. (setq title (replace-match "" t t title))))
  19432. (if org-export-with-section-numbers
  19433. (setq title (concat (org-section-number level) " " title)))
  19434. (insert title "\n" (make-string (string-width title) char) "\n")
  19435. (setq org-ascii-current-indentation '(0 . 0)))))
  19436. (defun org-export-visible (type arg)
  19437. "Create a copy of the visible part of the current buffer, and export it.
  19438. The copy is created in a temporary buffer and removed after use.
  19439. TYPE is the final key (as a string) that also select the export command in
  19440. the `C-c C-e' export dispatcher.
  19441. As a special case, if the you type SPC at the prompt, the temporary
  19442. org-mode file will not be removed but presented to you so that you can
  19443. continue to use it. The prefix arg ARG is passed through to the exporting
  19444. command."
  19445. (interactive
  19446. (list (progn
  19447. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  19448. (read-char-exclusive))
  19449. current-prefix-arg))
  19450. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  19451. (error "Invalid export key"))
  19452. (let* ((binding (cdr (assoc type
  19453. '((?a . org-export-as-ascii)
  19454. (?\C-a . org-export-as-ascii)
  19455. (?b . org-export-as-html-and-open)
  19456. (?\C-b . org-export-as-html-and-open)
  19457. (?h . org-export-as-html)
  19458. (?H . org-export-as-html-to-buffer)
  19459. (?R . org-export-region-as-html)
  19460. (?x . org-export-as-xoxo)))))
  19461. (keepp (equal type ?\ ))
  19462. (file buffer-file-name)
  19463. (buffer (get-buffer-create "*Org Export Visible*"))
  19464. s e)
  19465. (with-current-buffer buffer (erase-buffer))
  19466. (save-excursion
  19467. (setq s (goto-char (point-min)))
  19468. (while (not (= (point) (point-max)))
  19469. (goto-char (org-find-invisible))
  19470. (append-to-buffer buffer s (point))
  19471. (setq s (goto-char (org-find-visible))))
  19472. (goto-char (point-min))
  19473. (unless keepp
  19474. ;; Copy all comment lines to the end, to make sure #+ settings are
  19475. ;; still available for the second export step. Kind of a hack, but
  19476. ;; does do the trick.
  19477. (if (looking-at "#[^\r\n]*")
  19478. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  19479. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  19480. (append-to-buffer buffer (1+ (match-beginning 0))
  19481. (min (point-max) (1+ (match-end 0))))))
  19482. (set-buffer buffer)
  19483. (let ((buffer-file-name file)
  19484. (org-inhibit-startup t))
  19485. (org-mode)
  19486. (show-all)
  19487. (unless keepp (funcall binding arg))))
  19488. (if (not keepp)
  19489. (kill-buffer buffer)
  19490. (switch-to-buffer-other-window buffer)
  19491. (goto-char (point-min)))))
  19492. (defun org-find-visible ()
  19493. (let ((s (point)))
  19494. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19495. (get-char-property s 'invisible)))
  19496. s))
  19497. (defun org-find-invisible ()
  19498. (let ((s (point)))
  19499. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19500. (not (get-char-property s 'invisible))))
  19501. s))
  19502. ;;; HTML export
  19503. (defun org-get-current-options ()
  19504. "Return a string with current options as keyword options.
  19505. Does include HTML export options as well as TODO and CATEGORY stuff."
  19506. (format
  19507. "#+TITLE: %s
  19508. #+AUTHOR: %s
  19509. #+EMAIL: %s
  19510. #+LANGUAGE: %s
  19511. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  19512. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s p:%s
  19513. #+CATEGORY: %s
  19514. #+SEQ_TODO: %s
  19515. #+TYP_TODO: %s
  19516. #+PRIORITIES: %c %c %c
  19517. #+STARTUP: %s %s %s %s %s
  19518. #+TAGS: %s
  19519. #+ARCHIVE: %s
  19520. #+LINK: %s
  19521. "
  19522. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  19523. org-export-headline-levels
  19524. org-export-with-section-numbers
  19525. org-export-with-toc
  19526. org-export-preserve-breaks
  19527. org-export-html-expand
  19528. org-export-with-fixed-width
  19529. org-export-with-tables
  19530. org-export-with-sub-superscripts
  19531. org-export-with-footnotes
  19532. org-export-with-emphasize
  19533. org-export-with-TeX-macros
  19534. org-export-with-LaTeX-fragments
  19535. org-export-skip-text-before-1st-heading
  19536. org-export-with-property-drawer
  19537. (file-name-nondirectory buffer-file-name)
  19538. "TODO FEEDBACK VERIFY DONE"
  19539. "Me Jason Marie DONE"
  19540. org-highest-priority org-lowest-priority org-default-priority
  19541. (cdr (assoc org-startup-folded
  19542. '((nil . "showall") (t . "overview") (content . "content"))))
  19543. (if org-odd-levels-only "odd" "oddeven")
  19544. (if org-hide-leading-stars "hidestars" "showstars")
  19545. (if org-startup-align-all-tables "align" "noalign")
  19546. (cond ((eq t org-log-done) "logdone")
  19547. ((not org-log-done) "nologging")
  19548. ((listp org-log-done)
  19549. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  19550. org-log-done " ")))
  19551. (or (mapconcat (lambda (x)
  19552. (cond
  19553. ((equal '(:startgroup) x) "{")
  19554. ((equal '(:endgroup) x) "}")
  19555. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  19556. (t (car x))))
  19557. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  19558. org-archive-location
  19559. "org file:~/org/%s.org"
  19560. ))
  19561. (defun org-insert-export-options-template ()
  19562. "Insert into the buffer a template with information for exporting."
  19563. (interactive)
  19564. (if (not (bolp)) (newline))
  19565. (let ((s (org-get-current-options)))
  19566. (and (string-match "#\\+CATEGORY" s)
  19567. (setq s (substring s 0 (match-beginning 0))))
  19568. (insert s)))
  19569. (defun org-toggle-fixed-width-section (arg)
  19570. "Toggle the fixed-width export.
  19571. If there is no active region, the QUOTE keyword at the current headline is
  19572. inserted or removed. When present, it causes the text between this headline
  19573. and the next to be exported as fixed-width text, and unmodified.
  19574. If there is an active region, this command adds or removes a colon as the
  19575. first character of this line. If the first character of a line is a colon,
  19576. this line is also exported in fixed-width font."
  19577. (interactive "P")
  19578. (let* ((cc 0)
  19579. (regionp (org-region-active-p))
  19580. (beg (if regionp (region-beginning) (point)))
  19581. (end (if regionp (region-end)))
  19582. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19583. (re "[ \t]*\\(:\\)")
  19584. off)
  19585. (if regionp
  19586. (save-excursion
  19587. (goto-char beg)
  19588. (setq cc (current-column))
  19589. (beginning-of-line 1)
  19590. (setq off (looking-at re))
  19591. (while (> nlines 0)
  19592. (setq nlines (1- nlines))
  19593. (beginning-of-line 1)
  19594. (cond
  19595. (arg
  19596. (move-to-column cc t)
  19597. (insert ":\n")
  19598. (forward-line -1))
  19599. ((and off (looking-at re))
  19600. (replace-match "" t t nil 1))
  19601. ((not off) (move-to-column cc t) (insert ":")))
  19602. (forward-line 1)))
  19603. (save-excursion
  19604. (org-back-to-heading)
  19605. (if (looking-at (concat outline-regexp
  19606. "\\( *\\<" org-quote-string "\\>\\)"))
  19607. (replace-match "" t t nil 1)
  19608. (if (looking-at outline-regexp)
  19609. (progn
  19610. (goto-char (match-end 0))
  19611. (insert org-quote-string " "))))))))
  19612. (defun org-export-as-html-and-open (arg)
  19613. "Export the outline as HTML and immediately open it with a browser.
  19614. If there is an active region, export only the region.
  19615. The prefix ARG specifies how many levels of the outline should become
  19616. headlines. The default is 3. Lower levels will become bulleted lists."
  19617. (interactive "P")
  19618. (org-export-as-html arg 'hidden)
  19619. (org-open-file buffer-file-name))
  19620. (defun org-export-as-html-batch ()
  19621. "Call `org-export-as-html', may be used in batch processing as
  19622. emacs --batch
  19623. --load=$HOME/lib/emacs/org.el
  19624. --eval \"(setq org-export-headline-levels 2)\"
  19625. --visit=MyFile --funcall org-export-as-html-batch"
  19626. (org-export-as-html org-export-headline-levels 'hidden))
  19627. (defun org-export-as-html-to-buffer (arg)
  19628. "Call `org-exort-as-html` with output to a temporary buffer.
  19629. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  19630. (interactive "P")
  19631. (org-export-as-html arg nil nil "*Org HTML Export*")
  19632. (switch-to-buffer-other-window "*Org HTML Export*"))
  19633. (defun org-replace-region-by-html (beg end)
  19634. "Assume the current region has org-mode syntax, and convert it to HTML.
  19635. This can be used in any buffer. For example, you could write an
  19636. itemized list in org-mode syntax in an HTML buffer and then use this
  19637. command to convert it."
  19638. (interactive "r")
  19639. (let (reg html buf)
  19640. (save-window-excursion
  19641. (if (org-mode-p)
  19642. (setq html (org-export-region-as-html
  19643. beg end t 'string))
  19644. (setq reg (buffer-substring beg end)
  19645. buf (get-buffer-create "*Org tmp*"))
  19646. (with-current-buffer buf
  19647. (erase-buffer)
  19648. (insert reg)
  19649. (org-mode)
  19650. (setq html (org-export-region-as-html
  19651. (point-min) (point-max) t 'string)))
  19652. (kill-buffer buf)))
  19653. (delete-region beg end)
  19654. (insert html)))
  19655. (defun org-export-region-as-html (beg end &optional body-only buffer)
  19656. "Convert region from BEG to END in org-mode buffer to HTML.
  19657. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  19658. contents, and only produce the region of converted text, useful for
  19659. cut-and-paste operations.
  19660. If BUFFER is a buffer or a string, use/create that buffer as a target
  19661. of the converted HTML. If BUFFER is the symbol `string', return the
  19662. produced HTML as a string and leave not buffer behind. For example,
  19663. a Lisp program could call this function in the following way:
  19664. (setq html (org-export-region-as-html beg end t 'string))
  19665. When called interactively, the output buffer is selected, and shown
  19666. in a window. A non-interactive call will only retunr the buffer."
  19667. (interactive "r\nP")
  19668. (when (interactive-p)
  19669. (setq buffer "*Org HTML Export*"))
  19670. (let ((transient-mark-mode t) (zmacs-regions t)
  19671. rtn)
  19672. (goto-char end)
  19673. (set-mark (point)) ;; to activate the region
  19674. (goto-char beg)
  19675. (setq rtn (org-export-as-html
  19676. nil nil nil
  19677. buffer body-only))
  19678. (if (fboundp 'deactivate-mark) (deactivate-mark))
  19679. (if (and (interactive-p) (bufferp rtn))
  19680. (switch-to-buffer-other-window rtn)
  19681. rtn)))
  19682. (defun org-export-as-html (arg &optional hidden ext-plist
  19683. to-buffer body-only)
  19684. "Export the outline as a pretty HTML file.
  19685. If there is an active region, export only the region. The prefix
  19686. ARG specifies how many levels of the outline should become
  19687. headlines. The default is 3. Lower levels will become bulleted
  19688. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  19689. EXT-PLIST is a property list with external parameters overriding
  19690. org-mode's default settings, but still inferior to file-local
  19691. settings. When TO-BUFFER is non-nil, create a buffer with that
  19692. name and export to that buffer. If TO-BUFFER is the symbol `string',
  19693. don't leave any buffer behind but just return the resulting HTML as
  19694. a string. When BODY-ONLY is set, don't produce the file header and footer,
  19695. simply return the content of <body>...</body>, without even
  19696. the body tags themselves."
  19697. (interactive "P")
  19698. ;; Make sure we have a file name when we need it.
  19699. (when (and (not (or to-buffer body-only))
  19700. (not buffer-file-name))
  19701. (if (buffer-base-buffer)
  19702. (org-set-local 'buffer-file-name
  19703. (with-current-buffer (buffer-base-buffer)
  19704. buffer-file-name))
  19705. (error "Need a file name to be able to export.")))
  19706. (message "Exporting...")
  19707. (setq-default org-todo-line-regexp org-todo-line-regexp)
  19708. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  19709. (setq-default org-done-keywords org-done-keywords)
  19710. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  19711. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19712. ext-plist
  19713. (org-infile-export-plist)))
  19714. (style (plist-get opt-plist :style))
  19715. (link-validate (plist-get opt-plist :link-validation-function))
  19716. valid thetoc have-headings first-heading-pos
  19717. (odd org-odd-levels-only)
  19718. (region-p (org-region-active-p))
  19719. ;; The following two are dynamically scoped into other
  19720. ;; routines below.
  19721. (org-current-export-dir (org-export-directory :html opt-plist))
  19722. (org-current-export-file buffer-file-name)
  19723. (level 0) (line "") (origline "") txt todo
  19724. (umax nil)
  19725. (umax-toc nil)
  19726. (filename (if to-buffer nil
  19727. (concat (file-name-as-directory
  19728. (org-export-directory :html opt-plist))
  19729. (file-name-sans-extension
  19730. (file-name-nondirectory buffer-file-name))
  19731. ".html")))
  19732. (current-dir (if buffer-file-name
  19733. (file-name-directory buffer-file-name)
  19734. default-directory))
  19735. (buffer (if to-buffer
  19736. (cond
  19737. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  19738. (t (get-buffer-create to-buffer)))
  19739. (find-file-noselect filename)))
  19740. (org-levels-open (make-vector org-level-max nil))
  19741. (date (format-time-string "%Y/%m/%d" (current-time)))
  19742. (time (format-time-string "%X" (org-current-time)))
  19743. (author (plist-get opt-plist :author))
  19744. (title (or (plist-get opt-plist :title)
  19745. (and (not
  19746. (plist-get opt-plist :skip-before-1st-heading))
  19747. (org-export-grab-title-from-buffer))
  19748. (and buffer-file-name
  19749. (file-name-sans-extension
  19750. (file-name-nondirectory buffer-file-name)))
  19751. "UNTITLED"))
  19752. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  19753. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  19754. (inquote nil)
  19755. (infixed nil)
  19756. (in-local-list nil)
  19757. (local-list-num nil)
  19758. (local-list-indent nil)
  19759. (llt org-plain-list-ordered-item-terminator)
  19760. (email (plist-get opt-plist :email))
  19761. (language (plist-get opt-plist :language))
  19762. (lang-words nil)
  19763. (target-alist nil) tg
  19764. (head-count 0) cnt
  19765. (start 0)
  19766. (coding-system (and (boundp 'buffer-file-coding-system)
  19767. buffer-file-coding-system))
  19768. (coding-system-for-write (or org-export-html-coding-system
  19769. coding-system))
  19770. (save-buffer-coding-system (or org-export-html-coding-system
  19771. coding-system))
  19772. (charset (and coding-system-for-write
  19773. (fboundp 'coding-system-get)
  19774. (coding-system-get coding-system-for-write
  19775. 'mime-charset)))
  19776. (region
  19777. (buffer-substring
  19778. (if region-p (region-beginning) (point-min))
  19779. (if region-p (region-end) (point-max))))
  19780. (lines
  19781. (org-split-string
  19782. (org-cleaned-string-for-export
  19783. region
  19784. :emph-multiline t
  19785. :for-html t
  19786. :skip-before-1st-heading
  19787. (plist-get opt-plist :skip-before-1st-heading)
  19788. :add-text
  19789. (plist-get opt-plist :text)
  19790. :LaTeX-fragments
  19791. (plist-get opt-plist :LaTeX-fragments))
  19792. "[\r\n]"))
  19793. table-open type
  19794. table-buffer table-orig-buffer
  19795. ind start-is-num starter didclose
  19796. rpl path desc descp desc1 desc2 link
  19797. )
  19798. (let ((inhibit-read-only t))
  19799. (org-unmodified
  19800. (remove-text-properties (point-min) (point-max)
  19801. '(:org-license-to-kill t))))
  19802. (message "Exporting...")
  19803. (setq org-last-level 1)
  19804. (org-init-section-numbers)
  19805. ;; Get the language-dependent settings
  19806. (setq lang-words (or (assoc language org-export-language-setup)
  19807. (assoc "en" org-export-language-setup)))
  19808. ;; Switch to the output buffer
  19809. (set-buffer buffer)
  19810. (erase-buffer)
  19811. (fundamental-mode)
  19812. (and (fboundp 'set-buffer-file-coding-system)
  19813. (set-buffer-file-coding-system coding-system-for-write))
  19814. (let ((case-fold-search nil)
  19815. (org-odd-levels-only odd))
  19816. ;; create local variables for all options, to make sure all called
  19817. ;; functions get the correct information
  19818. (mapcar (lambda (x)
  19819. (set (make-local-variable (cdr x))
  19820. (plist-get opt-plist (car x))))
  19821. org-export-plist-vars)
  19822. (setq umax (if arg (prefix-numeric-value arg)
  19823. org-export-headline-levels))
  19824. (setq umax-toc (if (integerp org-export-with-toc)
  19825. (min org-export-with-toc umax)
  19826. umax))
  19827. (unless body-only
  19828. ;; File header
  19829. (insert (format
  19830. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  19831. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  19832. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  19833. lang=\"%s\" xml:lang=\"%s\">
  19834. <head>
  19835. <title>%s</title>
  19836. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  19837. <meta name=\"generator\" content=\"Org-mode\"/>
  19838. <meta name=\"generated\" content=\"%s %s\"/>
  19839. <meta name=\"author\" content=\"%s\"/>
  19840. %s
  19841. </head><body>
  19842. "
  19843. language language (org-html-expand title)
  19844. (or charset "iso-8859-1") date time author style))
  19845. (insert (or (plist-get opt-plist :preamble) ""))
  19846. (when (plist-get opt-plist :auto-preamble)
  19847. (if title (insert (format org-export-html-title-format
  19848. (org-html-expand title))))))
  19849. (if (and org-export-with-toc (not body-only))
  19850. (progn
  19851. (push (format "<h%d>%s</h%d>\n"
  19852. org-export-html-toplevel-hlevel
  19853. (nth 3 lang-words)
  19854. org-export-html-toplevel-hlevel)
  19855. thetoc)
  19856. (push "<ul>\n<li>" thetoc)
  19857. (setq lines
  19858. (mapcar '(lambda (line)
  19859. (if (string-match org-todo-line-regexp line)
  19860. ;; This is a headline
  19861. (progn
  19862. (setq have-headings t)
  19863. (setq level (- (match-end 1) (match-beginning 1))
  19864. level (org-tr-level level)
  19865. txt (save-match-data
  19866. (org-html-expand
  19867. (org-export-cleanup-toc-line
  19868. (match-string 3 line))))
  19869. todo
  19870. (or (and org-export-mark-todo-in-toc
  19871. (match-beginning 2)
  19872. (not (member (match-string 2 line)
  19873. org-done-keywords)))
  19874. ; TODO, not DONE
  19875. (and org-export-mark-todo-in-toc
  19876. (= level umax-toc)
  19877. (org-search-todo-below
  19878. line lines level))))
  19879. (if (and (memq org-export-with-tags '(not-in-toc nil))
  19880. (string-match
  19881. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  19882. txt))
  19883. (setq txt (replace-match "" t t txt)))
  19884. (if (string-match quote-re0 txt)
  19885. (setq txt (replace-match "" t t txt)))
  19886. (if org-export-with-section-numbers
  19887. (setq txt (concat (org-section-number level)
  19888. " " txt)))
  19889. (if (<= level (max umax umax-toc))
  19890. (setq head-count (+ head-count 1)))
  19891. (if (<= level umax-toc)
  19892. (progn
  19893. (if (> level org-last-level)
  19894. (progn
  19895. (setq cnt (- level org-last-level))
  19896. (while (>= (setq cnt (1- cnt)) 0)
  19897. (push "\n<ul>\n<li>" thetoc))
  19898. (push "\n" thetoc)))
  19899. (if (< level org-last-level)
  19900. (progn
  19901. (setq cnt (- org-last-level level))
  19902. (while (>= (setq cnt (1- cnt)) 0)
  19903. (push "</li>\n</ul>" thetoc))
  19904. (push "\n" thetoc)))
  19905. ;; Check for targets
  19906. (while (string-match org-target-regexp line)
  19907. (setq tg (match-string 1 line)
  19908. line (replace-match
  19909. (concat "@<span class=\"target\">" tg "@</span> ")
  19910. t t line))
  19911. (push (cons (org-solidify-link-text tg)
  19912. (format "sec-%d" head-count))
  19913. target-alist))
  19914. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  19915. (setq txt (replace-match "" t t txt)))
  19916. (push
  19917. (format
  19918. (if todo
  19919. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  19920. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  19921. head-count txt) thetoc)
  19922. (setq org-last-level level))
  19923. )))
  19924. line)
  19925. lines))
  19926. (while (> org-last-level 0)
  19927. (setq org-last-level (1- org-last-level))
  19928. (push "</li>\n</ul>\n" thetoc))
  19929. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  19930. (setq head-count 0)
  19931. (org-init-section-numbers)
  19932. (while (setq line (pop lines) origline line)
  19933. (catch 'nextline
  19934. ;; end of quote section?
  19935. (when (and inquote (string-match "^\\*+ " line))
  19936. (insert "</pre>\n")
  19937. (setq inquote nil))
  19938. ;; inside a quote section?
  19939. (when inquote
  19940. (insert (org-html-protect line) "\n")
  19941. (throw 'nextline nil))
  19942. ;; verbatim lines
  19943. (when (and org-export-with-fixed-width
  19944. (string-match "^[ \t]*:\\(.*\\)" line))
  19945. (when (not infixed)
  19946. (setq infixed t)
  19947. (insert "<pre>\n"))
  19948. (insert (org-html-protect (match-string 1 line)) "\n")
  19949. (when (and lines
  19950. (not (string-match "^[ \t]*\\(:.*\\)"
  19951. (car lines))))
  19952. (setq infixed nil)
  19953. (insert "</pre>\n"))
  19954. (throw 'nextline nil))
  19955. ;; Protected HTML
  19956. (when (get-text-property 0 'org-protected line)
  19957. (let (par)
  19958. (when (re-search-backward
  19959. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  19960. (setq par (match-string 1))
  19961. (replace-match "\\2\n"))
  19962. (insert line "\n")
  19963. (while (and lines
  19964. (get-text-property 0 'org-protected (car lines)))
  19965. (insert (pop lines) "\n"))
  19966. (and par (insert "<p>\n")))
  19967. (throw 'nextline nil))
  19968. ;; Horizontal line
  19969. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  19970. (insert "\n<hr/>\n")
  19971. (throw 'nextline nil))
  19972. ;; make targets to anchors
  19973. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  19974. (cond
  19975. ((match-end 2)
  19976. (setq line (replace-match
  19977. (concat "@<a name=\""
  19978. (org-solidify-link-text (match-string 1 line))
  19979. "\">\\nbsp@</a>")
  19980. t t line)))
  19981. ((and org-export-with-toc (equal (string-to-char line) ?*))
  19982. (setq line (replace-match
  19983. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  19984. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  19985. t t line)))
  19986. (t
  19987. (setq line (replace-match
  19988. (concat "@<a name=\""
  19989. (org-solidify-link-text (match-string 1 line))
  19990. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  19991. t t line)))))
  19992. (setq line (org-html-handle-time-stamps line))
  19993. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  19994. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  19995. ;; Also handle sub_superscripts and checkboxes
  19996. (setq line (org-html-expand line))
  19997. ;; Format the links
  19998. (setq start 0)
  19999. (while (string-match org-bracket-link-analytic-regexp line start)
  20000. (setq start (match-beginning 0))
  20001. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  20002. (setq path (match-string 3 line))
  20003. (setq desc1 (if (match-end 5) (match-string 5 line))
  20004. desc2 (if (match-end 2) (concat type ":" path) path)
  20005. descp (and desc1 (not (equal desc1 desc2)))
  20006. desc (or desc1 desc2))
  20007. ;; Make an image out of the description if that is so wanted
  20008. (when (and descp (org-file-image-p desc))
  20009. (save-match-data
  20010. (if (string-match "^file:" desc)
  20011. (setq desc (substring desc (match-end 0)))))
  20012. (setq desc (concat "<img src=\"" desc "\"/>")))
  20013. ;; FIXME: do we need to unescape here somewhere?
  20014. (cond
  20015. ((equal type "internal")
  20016. (setq rpl
  20017. (concat
  20018. "<a href=\"#"
  20019. (org-solidify-link-text
  20020. (save-match-data (org-link-unescape path)) target-alist)
  20021. "\">" desc "</a>")))
  20022. ((member type '("http" "https")) ; FIXME: need to test this.
  20023. ;; standard URL, just check if we need to inline an image
  20024. (if (and (or (eq t org-export-html-inline-images)
  20025. (and org-export-html-inline-images (not descp)))
  20026. (org-file-image-p path))
  20027. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  20028. (setq link (concat type ":" path))
  20029. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  20030. ((member type '("ftp" "mailto" "news"))
  20031. ;; standard URL
  20032. (setq link (concat type ":" path))
  20033. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  20034. ((string= type "file")
  20035. ;; FILE link
  20036. (let* ((filename path)
  20037. (abs-p (file-name-absolute-p filename))
  20038. thefile file-is-image-p search)
  20039. (save-match-data
  20040. (if (string-match "::\\(.*\\)" filename)
  20041. (setq search (match-string 1 filename)
  20042. filename (replace-match "" t nil filename)))
  20043. (setq valid
  20044. (if (functionp link-validate)
  20045. (funcall link-validate filename current-dir)
  20046. t))
  20047. (setq file-is-image-p (org-file-image-p filename))
  20048. (setq thefile (if abs-p (expand-file-name filename) filename))
  20049. (when (and org-export-html-link-org-files-as-html
  20050. (string-match "\\.org$" thefile))
  20051. (setq thefile (concat (substring thefile 0
  20052. (match-beginning 0))
  20053. ".html"))
  20054. (if (and search
  20055. ;; make sure this is can be used as target search
  20056. (not (string-match "^[0-9]*$" search))
  20057. (not (string-match "^\\*" search))
  20058. (not (string-match "^/.*/$" search)))
  20059. (setq thefile (concat thefile "#"
  20060. (org-solidify-link-text
  20061. (org-link-unescape search)))))
  20062. (when (string-match "^file:" desc)
  20063. (setq desc (replace-match "" t t desc))
  20064. (if (string-match "\\.org$" desc)
  20065. (setq desc (replace-match "" t t desc))))))
  20066. (setq rpl (if (and file-is-image-p
  20067. (or (eq t org-export-html-inline-images)
  20068. (and org-export-html-inline-images
  20069. (not descp))))
  20070. (concat "<img src=\"" thefile "\"/>")
  20071. (concat "<a href=\"" thefile "\">" desc "</a>")))
  20072. (if (not valid) (setq rpl desc))))
  20073. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  20074. (setq rpl (concat "<i>&lt;" type ":"
  20075. (save-match-data (org-link-unescape path))
  20076. "&gt;</i>"))))
  20077. (setq line (replace-match rpl t t line)
  20078. start (+ start (length rpl))))
  20079. ;; TODO items
  20080. (if (and (string-match org-todo-line-regexp line)
  20081. (match-beginning 2))
  20082. (setq line
  20083. (concat (substring line 0 (match-beginning 2))
  20084. "<span class=\""
  20085. (if (member (match-string 2 line)
  20086. org-done-keywords)
  20087. "done" "todo")
  20088. "\">" (match-string 2 line)
  20089. "</span>" (substring line (match-end 2)))))
  20090. ;; Does this contain a reference to a footnote?
  20091. (when org-export-with-footnotes
  20092. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  20093. (let ((n (match-string 2 line)))
  20094. (setq line
  20095. (replace-match
  20096. (format
  20097. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  20098. (match-string 1 line) n n n)
  20099. t t line)))))
  20100. (cond
  20101. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  20102. ;; This is a headline
  20103. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  20104. txt (match-string 2 line))
  20105. (if (string-match quote-re0 txt)
  20106. (setq txt (replace-match "" t t txt)))
  20107. (if (<= level (max umax umax-toc))
  20108. (setq head-count (+ head-count 1)))
  20109. (when in-local-list
  20110. ;; Close any local lists before inserting a new header line
  20111. (while local-list-num
  20112. (org-close-li)
  20113. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  20114. (pop local-list-num))
  20115. (setq local-list-indent nil
  20116. in-local-list nil))
  20117. (setq first-heading-pos (or first-heading-pos (point)))
  20118. (org-html-level-start level txt umax
  20119. (and org-export-with-toc (<= level umax))
  20120. head-count)
  20121. ;; QUOTES
  20122. (when (string-match quote-re line)
  20123. (insert "<pre>")
  20124. (setq inquote t)))
  20125. ((and org-export-with-tables
  20126. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  20127. (if (not table-open)
  20128. ;; New table starts
  20129. (setq table-open t table-buffer nil table-orig-buffer nil))
  20130. ;; Accumulate lines
  20131. (setq table-buffer (cons line table-buffer)
  20132. table-orig-buffer (cons origline table-orig-buffer))
  20133. (when (or (not lines)
  20134. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  20135. (car lines))))
  20136. (setq table-open nil
  20137. table-buffer (nreverse table-buffer)
  20138. table-orig-buffer (nreverse table-orig-buffer))
  20139. (org-close-par-maybe)
  20140. (insert (org-format-table-html table-buffer table-orig-buffer))))
  20141. (t
  20142. ;; Normal lines
  20143. (when (string-match
  20144. (cond
  20145. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20146. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20147. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20148. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  20149. line)
  20150. (setq ind (org-get-string-indentation line)
  20151. start-is-num (match-beginning 4)
  20152. starter (if (match-beginning 2)
  20153. (substring (match-string 2 line) 0 -1))
  20154. line (substring line (match-beginning 5)))
  20155. (unless (string-match "[^ \t]" line)
  20156. ;; empty line. Pretend indentation is large.
  20157. (setq ind (if org-empty-line-terminates-plain-lists
  20158. 0
  20159. (1+ (or (car local-list-indent) 1)))))
  20160. (setq didclose nil)
  20161. (while (and in-local-list
  20162. (or (and (= ind (car local-list-indent))
  20163. (not starter))
  20164. (< ind (car local-list-indent))))
  20165. (setq didclose t)
  20166. (org-close-li)
  20167. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  20168. (pop local-list-num) (pop local-list-indent)
  20169. (setq in-local-list local-list-indent))
  20170. (cond
  20171. ((and starter
  20172. (or (not in-local-list)
  20173. (> ind (car local-list-indent))))
  20174. ;; Start new (level of) list
  20175. (org-close-par-maybe)
  20176. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  20177. (push start-is-num local-list-num)
  20178. (push ind local-list-indent)
  20179. (setq in-local-list t))
  20180. (starter
  20181. ;; continue current list
  20182. (org-close-li)
  20183. (insert "<li>\n"))
  20184. (didclose
  20185. ;; we did close a list, normal text follows: need <p>
  20186. (org-open-par)))
  20187. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  20188. (setq line
  20189. (replace-match
  20190. (if (equal (match-string 1 line) "X")
  20191. "<b>[X]</b>"
  20192. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  20193. t t line))))
  20194. ;; Empty lines start a new paragraph. If hand-formatted lists
  20195. ;; are not fully interpreted, lines starting with "-", "+", "*"
  20196. ;; also start a new paragraph.
  20197. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  20198. ;; Is this the start of a footnote?
  20199. (when org-export-with-footnotes
  20200. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  20201. (org-close-par-maybe)
  20202. (let ((n (match-string 1 line)))
  20203. (setq line (replace-match
  20204. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  20205. ;; Check if the line break needs to be conserved
  20206. (cond
  20207. ((string-match "\\\\\\\\[ \t]*$" line)
  20208. (setq line (replace-match "<br/>" t t line)))
  20209. (org-export-preserve-breaks
  20210. (setq line (concat line "<br/>"))))
  20211. (insert line "\n")))))
  20212. ;; Properly close all local lists and other lists
  20213. (when inquote (insert "</pre>\n"))
  20214. (when in-local-list
  20215. ;; Close any local lists before inserting a new header line
  20216. (while local-list-num
  20217. (org-close-li)
  20218. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  20219. (pop local-list-num))
  20220. (setq local-list-indent nil
  20221. in-local-list nil))
  20222. (org-html-level-start 1 nil umax
  20223. (and org-export-with-toc (<= level umax))
  20224. head-count)
  20225. (unless body-only
  20226. (when (plist-get opt-plist :auto-postamble)
  20227. (when (and org-export-author-info author)
  20228. (insert "<p class=\"author\"> "
  20229. (nth 1 lang-words) ": " author "\n")
  20230. (when email
  20231. (insert "<a href=\"mailto:" email "\">&lt;"
  20232. email "&gt;</a>\n"))
  20233. (insert "</p>\n"))
  20234. (when (and date time org-export-time-stamp-file)
  20235. (insert "<p class=\"date\"> "
  20236. (nth 2 lang-words) ": "
  20237. date " " time "</p>\n")))
  20238. (if org-export-html-with-timestamp
  20239. (insert org-export-html-html-helper-timestamp))
  20240. (insert (or (plist-get opt-plist :postamble) ""))
  20241. (insert "</body>\n</html>\n"))
  20242. (normal-mode)
  20243. (if (eq major-mode default-major-mode) (html-mode))
  20244. ;; insert the table of contents
  20245. (goto-char (point-min))
  20246. (when thetoc
  20247. (if (or (re-search-forward
  20248. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  20249. (re-search-forward
  20250. "\\[TABLE-OF-CONTENTS\\]" nil t))
  20251. (progn
  20252. (goto-char (match-beginning 0))
  20253. (replace-match ""))
  20254. (goto-char first-heading-pos)
  20255. (when (looking-at "\\s-*</p>")
  20256. (goto-char (match-end 0))
  20257. (insert "\n")))
  20258. (mapc 'insert thetoc))
  20259. ;; remove empty paragraphs and lists
  20260. (goto-char (point-min))
  20261. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  20262. (replace-match ""))
  20263. (goto-char (point-min))
  20264. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  20265. (replace-match ""))
  20266. (or to-buffer (save-buffer))
  20267. (goto-char (point-min))
  20268. (message "Exporting... done")
  20269. (if (eq to-buffer 'string)
  20270. (prog1 (buffer-substring (point-min) (point-max))
  20271. (kill-buffer (current-buffer)))
  20272. (current-buffer)))))
  20273. (defvar org-table-colgroup-info nil) ;; FIXME: mode to a better place
  20274. (defun org-format-table-ascii (lines)
  20275. "Format a table for ascii export."
  20276. (if (stringp lines)
  20277. (setq lines (org-split-string lines "\n")))
  20278. (if (not (string-match "^[ \t]*|" (car lines)))
  20279. ;; Table made by table.el - test for spanning
  20280. lines
  20281. ;; A normal org table
  20282. ;; Get rid of hlines at beginning and end
  20283. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20284. (setq lines (nreverse lines))
  20285. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20286. (setq lines (nreverse lines))
  20287. (when org-export-table-remove-special-lines
  20288. ;; Check if the table has a marking column. If yes remove the
  20289. ;; column and the special lines
  20290. (setq lines (org-table-clean-before-export lines)))
  20291. ;; Get rid of the vertical lines except for grouping
  20292. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  20293. rtn line vl1 start)
  20294. (while (setq line (pop lines))
  20295. (if (string-match org-table-hline-regexp line)
  20296. (and (string-match "|\\(.*\\)|" line)
  20297. (setq line (replace-match " \\1" t nil line)))
  20298. (setq start 0 vl1 vl)
  20299. (while (string-match "|" line start)
  20300. (setq start (match-end 0))
  20301. (or (pop vl1) (setq line (replace-match " " t t line)))))
  20302. (push line rtn))
  20303. (nreverse rtn))))
  20304. (defun org-colgroup-info-to-vline-list (info)
  20305. (let (vl new last)
  20306. (while info
  20307. (setq last new new (pop info))
  20308. (if (or (memq last '(:end :startend))
  20309. (memq new '(:start :startend)))
  20310. (push t vl)
  20311. (push nil vl)))
  20312. (setq vl (cons nil (nreverse vl)))))
  20313. (defun org-format-table-html (lines olines)
  20314. "Find out which HTML converter to use and return the HTML code."
  20315. (if (stringp lines)
  20316. (setq lines (org-split-string lines "\n")))
  20317. (if (string-match "^[ \t]*|" (car lines))
  20318. ;; A normal org table
  20319. (org-format-org-table-html lines)
  20320. ;; Table made by table.el - test for spanning
  20321. (let* ((hlines (delq nil (mapcar
  20322. (lambda (x)
  20323. (if (string-match "^[ \t]*\\+-" x) x
  20324. nil))
  20325. lines)))
  20326. (first (car hlines))
  20327. (ll (and (string-match "\\S-+" first)
  20328. (match-string 0 first)))
  20329. (re (concat "^[ \t]*" (regexp-quote ll)))
  20330. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  20331. hlines))))
  20332. (if (and (not spanning)
  20333. (not org-export-prefer-native-exporter-for-tables))
  20334. ;; We can use my own converter with HTML conversions
  20335. (org-format-table-table-html lines)
  20336. ;; Need to use the code generator in table.el, with the original text.
  20337. (org-format-table-table-html-using-table-generate-source olines)))))
  20338. (defun org-format-org-table-html (lines &optional splice)
  20339. "Format a table into HTML."
  20340. ;; Get rid of hlines at beginning and end
  20341. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20342. (setq lines (nreverse lines))
  20343. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20344. (setq lines (nreverse lines))
  20345. (when org-export-table-remove-special-lines
  20346. ;; Check if the table has a marking column. If yes remove the
  20347. ;; column and the special lines
  20348. (setq lines (org-table-clean-before-export lines)))
  20349. (let ((head (and org-export-highlight-first-table-line
  20350. (delq nil (mapcar
  20351. (lambda (x) (string-match "^[ \t]*|-" x))
  20352. (cdr lines)))))
  20353. (nlines 0) fnum i
  20354. tbopen line fields html gr colgropen)
  20355. (if splice (setq head nil))
  20356. (unless splice (push (if head "<thead>" "<tbody>") html))
  20357. (setq tbopen t)
  20358. (while (setq line (pop lines))
  20359. (catch 'next-line
  20360. (if (string-match "^[ \t]*|-" line)
  20361. (progn
  20362. (unless splice
  20363. (push (if head "</thead>" "</tbody>") html)
  20364. (if lines (push "<tbody>" html) (setq tbopen nil)))
  20365. (setq head nil) ;; head ends here, first time around
  20366. ;; ignore this line
  20367. (throw 'next-line t)))
  20368. ;; Break the line into fields
  20369. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  20370. (unless fnum (setq fnum (make-vector (length fields) 0)))
  20371. (setq nlines (1+ nlines) i -1)
  20372. (push (concat "<tr>"
  20373. (mapconcat
  20374. (lambda (x)
  20375. (setq i (1+ i))
  20376. (if (and (< i nlines)
  20377. (string-match org-table-number-regexp x))
  20378. (incf (aref fnum i)))
  20379. (if head
  20380. (concat (car org-export-table-header-tags) x
  20381. (cdr org-export-table-header-tags))
  20382. (concat (car org-export-table-data-tags) x
  20383. (cdr org-export-table-data-tags))))
  20384. fields "")
  20385. "</tr>")
  20386. html)))
  20387. (unless splice (if tbopen (push "</tbody>" html)))
  20388. (unless splice (push "</table>\n" html))
  20389. (setq html (nreverse html))
  20390. (unless splice
  20391. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  20392. (push (mapconcat
  20393. (lambda (x)
  20394. (setq gr (pop org-table-colgroup-info))
  20395. (format "%s<COL align=\"%s\"></COL>%s"
  20396. (if (memq gr '(:start :startend))
  20397. (prog1
  20398. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  20399. (setq colgropen t))
  20400. "")
  20401. (if (> (/ (float x) nlines) org-table-number-fraction)
  20402. "right" "left")
  20403. (if (memq gr '(:end :startend))
  20404. (progn (setq colgropen nil) "</colgroup>")
  20405. "")))
  20406. fnum "")
  20407. html)
  20408. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  20409. (push org-export-html-table-tag html))
  20410. (concat (mapconcat 'identity html "\n") "\n")))
  20411. (defun org-table-clean-before-export (lines)
  20412. "Check if the table has a marking column.
  20413. If yes remove the column and the special lines."
  20414. (setq org-table-colgroup-info nil)
  20415. (if (memq nil
  20416. (mapcar
  20417. (lambda (x) (or (string-match "^[ \t]*|-" x)
  20418. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  20419. lines))
  20420. (progn
  20421. (setq org-table-clean-did-remove-column nil)
  20422. (delq nil
  20423. (mapcar
  20424. (lambda (x)
  20425. (cond
  20426. ((string-match "^[ \t]*| */ *|" x)
  20427. (setq org-table-colgroup-info
  20428. (mapcar (lambda (x)
  20429. (cond ((member x '("<" "&lt;")) :start)
  20430. ((member x '(">" "&gt;")) :end)
  20431. ((member x '("<>" "&lt;&gt;")) :startend)
  20432. (t nil)))
  20433. (org-split-string x "[ \t]*|[ \t]*")))
  20434. nil)
  20435. (t x)))
  20436. lines)))
  20437. (setq org-table-clean-did-remove-column t)
  20438. (delq nil
  20439. (mapcar
  20440. (lambda (x)
  20441. (cond
  20442. ((string-match "^[ \t]*| */ *|" x)
  20443. (setq org-table-colgroup-info
  20444. (mapcar (lambda (x)
  20445. (cond ((member x '("<" "&lt;")) :start)
  20446. ((member x '(">" "&gt;")) :end)
  20447. ((member x '("<>" "&lt;&gt;")) :startend)
  20448. (t nil)))
  20449. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  20450. nil)
  20451. ((string-match "^[ \t]*| *[!_^/] *|" x)
  20452. nil) ; ignore this line
  20453. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  20454. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  20455. ;; remove the first column
  20456. (replace-match "\\1|" t nil x))
  20457. (t (error "This should not happen"))))
  20458. lines))))
  20459. (defun org-format-table-table-html (lines)
  20460. "Format a table generated by table.el into HTML.
  20461. This conversion does *not* use `table-generate-source' from table.el.
  20462. This has the advantage that Org-mode's HTML conversions can be used.
  20463. But it has the disadvantage, that no cell- or row-spanning is allowed."
  20464. (let (line field-buffer
  20465. (head org-export-highlight-first-table-line)
  20466. fields html empty)
  20467. (setq html (concat org-export-html-table-tag "\n"))
  20468. (while (setq line (pop lines))
  20469. (setq empty "&nbsp;")
  20470. (catch 'next-line
  20471. (if (string-match "^[ \t]*\\+-" line)
  20472. (progn
  20473. (if field-buffer
  20474. (progn
  20475. (setq
  20476. html
  20477. (concat
  20478. html
  20479. "<tr>"
  20480. (mapconcat
  20481. (lambda (x)
  20482. (if (equal x "") (setq x empty))
  20483. (if head
  20484. (concat (car org-export-table-header-tags) x
  20485. (cdr org-export-table-header-tags))
  20486. (concat (car org-export-table-data-tags) x
  20487. (cdr org-export-table-data-tags))))
  20488. field-buffer "\n")
  20489. "</tr>\n"))
  20490. (setq head nil)
  20491. (setq field-buffer nil)))
  20492. ;; Ignore this line
  20493. (throw 'next-line t)))
  20494. ;; Break the line into fields and store the fields
  20495. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  20496. (if field-buffer
  20497. (setq field-buffer (mapcar
  20498. (lambda (x)
  20499. (concat x "<br/>" (pop fields)))
  20500. field-buffer))
  20501. (setq field-buffer fields))))
  20502. (setq html (concat html "</table>\n"))
  20503. html))
  20504. (defun org-format-table-table-html-using-table-generate-source (lines)
  20505. "Format a table into html, using `table-generate-source' from table.el.
  20506. This has the advantage that cell- or row-spanning is allowed.
  20507. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  20508. (require 'table)
  20509. (with-current-buffer (get-buffer-create " org-tmp1 ")
  20510. (erase-buffer)
  20511. (insert (mapconcat 'identity lines "\n"))
  20512. (goto-char (point-min))
  20513. (if (not (re-search-forward "|[^+]" nil t))
  20514. (error "Error processing table"))
  20515. (table-recognize-table)
  20516. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  20517. (table-generate-source 'html " org-tmp2 ")
  20518. (set-buffer " org-tmp2 ")
  20519. (buffer-substring (point-min) (point-max))))
  20520. (defun org-html-handle-time-stamps (s)
  20521. "Format time stamps in string S, or remove them."
  20522. (catch 'exit
  20523. (let (r b)
  20524. (while (string-match org-maybe-keyword-time-regexp s)
  20525. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  20526. ;; never export CLOCK
  20527. (throw 'exit ""))
  20528. (or b (setq b (substring s 0 (match-beginning 0))))
  20529. (if (not org-export-with-timestamps)
  20530. (setq r (concat r (substring s 0 (match-beginning 0)))
  20531. s (substring s (match-end 0)))
  20532. (setq r (concat
  20533. r (substring s 0 (match-beginning 0))
  20534. (if (match-end 1)
  20535. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  20536. (match-string 1 s)))
  20537. (format " @<span class=\"timestamp\">%s@</span>"
  20538. (substring
  20539. (org-translate-time (match-string 3 s)) 1 -1)))
  20540. s (substring s (match-end 0)))))
  20541. ;; Line break if line started and ended with time stamp stuff
  20542. (if (not r)
  20543. s
  20544. (setq r (concat r s))
  20545. (unless (string-match "\\S-" (concat b s))
  20546. (setq r (concat r "@<br/>")))
  20547. r))))
  20548. (defun org-html-protect (s)
  20549. ;; convert & to &amp;, < to &lt; and > to &gt;
  20550. (let ((start 0))
  20551. (while (string-match "&" s start)
  20552. (setq s (replace-match "&amp;" t t s)
  20553. start (1+ (match-beginning 0))))
  20554. (while (string-match "<" s)
  20555. (setq s (replace-match "&lt;" t t s)))
  20556. (while (string-match ">" s)
  20557. (setq s (replace-match "&gt;" t t s))))
  20558. s)
  20559. (defun org-export-cleanup-toc-line (s)
  20560. "Remove tags and time staps from lines going into the toc."
  20561. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  20562. (setq s (replace-match "" t t s)))
  20563. (when org-export-remove-timestamps-from-toc
  20564. (while (string-match org-maybe-keyword-time-regexp s)
  20565. (setq s (replace-match "" t t s))))
  20566. (while (string-match org-bracket-link-regexp s)
  20567. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  20568. t t s)))
  20569. s)
  20570. (defun org-html-expand (string)
  20571. "Prepare STRING for HTML export. Applies all active conversions.
  20572. If there are links in the string, don't modify these."
  20573. (let* (m s l res)
  20574. (while (setq m (string-match org-bracket-link-regexp string))
  20575. (setq s (substring string 0 m)
  20576. l (match-string 0 string)
  20577. string (substring string (match-end 0)))
  20578. (push (org-html-do-expand s) res)
  20579. (push l res))
  20580. (push (org-html-do-expand string) res)
  20581. (apply 'concat (nreverse res))))
  20582. (defun org-html-do-expand (s)
  20583. "Apply all active conversions to translate special ASCII to HTML."
  20584. (setq s (org-html-protect s))
  20585. (if org-export-html-expand
  20586. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  20587. (setq s (replace-match "<\\1>" t nil s))))
  20588. (if org-export-with-emphasize
  20589. (setq s (org-export-html-convert-emphasize s)))
  20590. (if org-export-with-sub-superscripts
  20591. (setq s (org-export-html-convert-sub-super s)))
  20592. (if org-export-with-TeX-macros
  20593. (let ((start 0) wd ass)
  20594. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  20595. (setq wd (match-string 1 s))
  20596. (if (setq ass (assoc wd org-html-entities))
  20597. (setq s (replace-match (or (cdr ass)
  20598. (concat "&" (car ass) ";"))
  20599. t t s))
  20600. (setq start (+ start (length wd)))))))
  20601. s)
  20602. (defun org-create-multibrace-regexp (left right n)
  20603. "Create a regular expression which will match a balanced sexp.
  20604. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  20605. as single character strings.
  20606. The regexp returned will match the entire expression including the
  20607. delimiters. It will also define a single group which contains the
  20608. match except for the outermost delimiters. The maximum depth of
  20609. stacked delimiters is N. Escaping delimiters is not possible."
  20610. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  20611. (or "\\|")
  20612. (re nothing)
  20613. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  20614. (while (> n 1)
  20615. (setq n (1- n)
  20616. re (concat re or next)
  20617. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  20618. (concat left "\\(" re "\\)" right)))
  20619. (defvar org-match-substring-regexp
  20620. (concat
  20621. "\\([^\\]\\)\\([_^]\\)\\("
  20622. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  20623. "\\|"
  20624. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  20625. "\\|"
  20626. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  20627. "The regular expression matching a sub- or superscript.")
  20628. ;(let ((s "a\\_b"))
  20629. ; (and (string-match org-match-substring-regexp s)
  20630. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  20631. (defun org-export-html-convert-sub-super (string)
  20632. "Convert sub- and superscripts in STRING to HTML."
  20633. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  20634. (while (string-match org-match-substring-regexp string s)
  20635. (if (and requireb (match-end 8))
  20636. (setq s (match-end 2))
  20637. (setq s (match-end 1)
  20638. key (if (string= (match-string 2 string) "_") "sub" "sup")
  20639. c (or (match-string 8 string)
  20640. (match-string 6 string)
  20641. (match-string 5 string))
  20642. string (replace-match
  20643. (concat (match-string 1 string)
  20644. "<" key ">" c "</" key ">")
  20645. t t string))))
  20646. (while (string-match "\\\\\\([_^]\\)" string)
  20647. (setq string (replace-match (match-string 1 string) t t string)))
  20648. string))
  20649. (defun org-export-html-convert-emphasize (string)
  20650. "Apply emphasis."
  20651. (let ((s 0))
  20652. (while (string-match org-emph-re string s)
  20653. (if (not (equal
  20654. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  20655. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  20656. (setq string (replace-match
  20657. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  20658. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  20659. "\\5") t nil string))
  20660. (setq s (1+ s))))
  20661. string))
  20662. (defvar org-par-open nil)
  20663. (defun org-open-par ()
  20664. "Insert <p>, but first close previous paragraph if any."
  20665. (org-close-par-maybe)
  20666. (insert "\n<p>")
  20667. (setq org-par-open t))
  20668. (defun org-close-par-maybe ()
  20669. "Close paragraph if there is one open."
  20670. (when org-par-open
  20671. (insert "</p>")
  20672. (setq org-par-open nil)))
  20673. (defun org-close-li ()
  20674. "Close <li> if necessary."
  20675. (org-close-par-maybe)
  20676. (insert "</li>\n"))
  20677. (defvar body-only) ; dynamically scoped into this.
  20678. (defun org-html-level-start (level title umax with-toc head-count)
  20679. "Insert a new level in HTML export.
  20680. When TITLE is nil, just close all open levels."
  20681. (org-close-par-maybe)
  20682. (let ((l (1+ (max level umax))))
  20683. (while (<= l org-level-max)
  20684. (if (aref org-levels-open (1- l))
  20685. (progn
  20686. (org-html-level-close l)
  20687. (aset org-levels-open (1- l) nil)))
  20688. (setq l (1+ l)))
  20689. (when title
  20690. ;; If title is nil, this means this function is called to close
  20691. ;; all levels, so the rest is done only if title is given
  20692. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  20693. (setq title (replace-match
  20694. (if org-export-with-tags
  20695. (save-match-data
  20696. (concat
  20697. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  20698. (mapconcat 'identity (org-split-string
  20699. (match-string 1 title) ":")
  20700. "&nbsp;")
  20701. "</span>"))
  20702. "")
  20703. t t title)))
  20704. (if (> level umax)
  20705. (progn
  20706. (if (aref org-levels-open (1- level))
  20707. (progn
  20708. (org-close-li)
  20709. (insert "<li>" title "<br/>\n"))
  20710. (aset org-levels-open (1- level) t)
  20711. (org-close-par-maybe)
  20712. (insert "<ul>\n<li>" title "<br/>\n")))
  20713. (if (and org-export-with-section-numbers (not body-only))
  20714. (setq title (concat (org-section-number level) " " title)))
  20715. (setq level (+ level org-export-html-toplevel-hlevel -1))
  20716. (if with-toc
  20717. (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
  20718. level head-count title level))
  20719. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  20720. (org-open-par)))))
  20721. (defun org-html-level-close (&rest args)
  20722. "Terminate one level in HTML export."
  20723. (org-close-li)
  20724. (insert "</ul>\n"))
  20725. ;;; iCalendar export
  20726. ;;;###autoload
  20727. (defun org-export-icalendar-this-file ()
  20728. "Export current file as an iCalendar file.
  20729. The iCalendar file will be located in the same directory as the Org-mode
  20730. file, but with extension `.ics'."
  20731. (interactive)
  20732. (org-export-icalendar nil buffer-file-name))
  20733. ;;;###autoload
  20734. (defun org-export-icalendar-all-agenda-files ()
  20735. "Export all files in `org-agenda-files' to iCalendar .ics files.
  20736. Each iCalendar file will be located in the same directory as the Org-mode
  20737. file, but with extension `.ics'."
  20738. (interactive)
  20739. (apply 'org-export-icalendar nil (org-agenda-files t)))
  20740. ;;;###autoload
  20741. (defun org-export-icalendar-combine-agenda-files ()
  20742. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  20743. The file is stored under the name `org-combined-agenda-icalendar-file'."
  20744. (interactive)
  20745. (apply 'org-export-icalendar t (org-agenda-files t)))
  20746. (defun org-export-icalendar (combine &rest files)
  20747. "Create iCalendar files for all elements of FILES.
  20748. If COMBINE is non-nil, combine all calendar entries into a single large
  20749. file and store it under the name `org-combined-agenda-icalendar-file'."
  20750. (save-excursion
  20751. (org-prepare-agenda-buffers files)
  20752. (let* ((dir (org-export-directory
  20753. :ical (list :publishing-directory
  20754. org-export-publishing-directory)))
  20755. file ical-file ical-buffer category started org-agenda-new-buffers)
  20756. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  20757. (when combine
  20758. (setq ical-file
  20759. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  20760. org-combined-agenda-icalendar-file
  20761. (expand-file-name org-combined-agenda-icalendar-file dir))
  20762. ical-buffer (org-get-agenda-file-buffer ical-file))
  20763. (set-buffer ical-buffer) (erase-buffer))
  20764. (while (setq file (pop files))
  20765. (catch 'nextfile
  20766. (org-check-agenda-file file)
  20767. (set-buffer (org-get-agenda-file-buffer file))
  20768. (unless combine
  20769. (setq ical-file (concat (file-name-as-directory dir)
  20770. (file-name-sans-extension
  20771. (file-name-nondirectory buffer-file-name))
  20772. ".ics"))
  20773. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  20774. (with-current-buffer ical-buffer (erase-buffer)))
  20775. (setq category (or org-category
  20776. (file-name-sans-extension
  20777. (file-name-nondirectory buffer-file-name))))
  20778. (if (symbolp category) (setq category (symbol-name category)))
  20779. (let ((standard-output ical-buffer))
  20780. (if combine
  20781. (and (not started) (setq started t)
  20782. (org-start-icalendar-file org-icalendar-combined-name))
  20783. (org-start-icalendar-file category))
  20784. (org-print-icalendar-entries combine)
  20785. (when (or (and combine (not files)) (not combine))
  20786. (org-finish-icalendar-file)
  20787. (set-buffer ical-buffer)
  20788. (save-buffer)
  20789. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  20790. (org-release-buffers org-agenda-new-buffers))))
  20791. (defvar org-after-save-iCalendar-file-hook nil
  20792. "Hook run after an iCalendar file has been saved.
  20793. The iCalendar buffer is still current when this hook is run.
  20794. A good way to use this is to tell a desktop calenndar application to re-read
  20795. the iCalendar file.")
  20796. (defun org-print-icalendar-entries (&optional combine)
  20797. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  20798. When COMBINE is non nil, add the category to each line."
  20799. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  20800. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  20801. (org-category-table (org-get-category-table))
  20802. (dts (org-ical-ts-to-string
  20803. (format-time-string (cdr org-time-stamp-formats) (current-time))
  20804. "DTSTART"))
  20805. hd ts ts2 state status (inc t) pos b sexp rrule
  20806. scheduledp deadlinep tmp pri category
  20807. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  20808. (save-excursion
  20809. (goto-char (point-min))
  20810. (while (re-search-forward re1 nil t)
  20811. (catch :skip
  20812. (org-agenda-skip)
  20813. (setq pos (match-beginning 0)
  20814. ts (match-string 0)
  20815. inc t
  20816. hd (org-get-heading)
  20817. category (org-get-category))
  20818. (if (looking-at re2)
  20819. (progn
  20820. (goto-char (match-end 0))
  20821. (setq ts2 (match-string 1) inc nil))
  20822. (setq tmp (buffer-substring (max (point-min)
  20823. (- pos org-ds-keyword-length))
  20824. pos)
  20825. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  20826. (progn
  20827. (setq inc nil)
  20828. (replace-match "\\1" t nil ts))
  20829. ts)
  20830. deadlinep (string-match org-deadline-regexp tmp)
  20831. scheduledp (string-match org-scheduled-regexp tmp)
  20832. ;; donep (org-entry-is-done-p)
  20833. ))
  20834. (if (or (string-match org-tr-regexp hd)
  20835. (string-match org-ts-regexp hd))
  20836. (setq hd (replace-match "" t t hd)))
  20837. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  20838. (setq rrule
  20839. (concat "\nRRULE:FREQ="
  20840. (cdr (assoc
  20841. (match-string 2 ts)
  20842. '(("d" . "DAILY")("w" . "WEEKLY")
  20843. ("m" . "MONTHLY")("y" . "YEARLY"))))
  20844. ";INTERVAL=" (match-string 1 ts)))
  20845. (setq rrule ""))
  20846. (if (string-match org-bracket-link-regexp hd)
  20847. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  20848. (match-string 1 hd))
  20849. t t hd)))
  20850. (if deadlinep (setq hd (concat "DL: " hd)))
  20851. (if scheduledp (setq hd (concat "S: " hd)))
  20852. (if (string-match "\\`<%%" ts)
  20853. (with-current-buffer sexp-buffer
  20854. (insert (substring ts 1 -1) " " hd "\n"))
  20855. (princ (format "BEGIN:VEVENT
  20856. %s
  20857. %s%s
  20858. SUMMARY:%s
  20859. CATEGORIES:%s
  20860. END:VEVENT\n"
  20861. (org-ical-ts-to-string ts "DTSTART")
  20862. (org-ical-ts-to-string ts2 "DTEND" inc)
  20863. rrule hd category)))))
  20864. (when (and org-icalendar-include-sexps
  20865. (condition-case nil (require 'icalendar) (error nil))
  20866. (fboundp 'icalendar-export-region))
  20867. ;; Get all the literal sexps
  20868. (goto-char (point-min))
  20869. (while (re-search-forward "^&?%%(" nil t)
  20870. (catch :skip
  20871. (org-agenda-skip)
  20872. (setq b (match-beginning 0))
  20873. (goto-char (1- (match-end 0)))
  20874. (forward-sexp 1)
  20875. (end-of-line 1)
  20876. (setq sexp (buffer-substring b (point)))
  20877. (with-current-buffer sexp-buffer
  20878. (insert sexp "\n"))
  20879. (princ (org-diary-to-ical-string sexp-buffer)))))
  20880. (when org-icalendar-include-todo
  20881. (goto-char (point-min))
  20882. (while (re-search-forward org-todo-line-regexp nil t)
  20883. (catch :skip
  20884. (org-agenda-skip)
  20885. (setq state (match-string 2))
  20886. (setq status (if (member state org-done-keywords)
  20887. "COMPLETED" "NEEDS-ACTION"))
  20888. (when (and state
  20889. (or (not (member state org-done-keywords))
  20890. (eq org-icalendar-include-todo 'all))
  20891. (not (member org-archive-tag (org-get-tags-at)))
  20892. )
  20893. (setq hd (match-string 3))
  20894. (if (string-match org-bracket-link-regexp hd)
  20895. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  20896. (match-string 1 hd))
  20897. t t hd)))
  20898. (if (string-match org-priority-regexp hd)
  20899. (setq pri (string-to-char (match-string 2 hd))
  20900. hd (concat (substring hd 0 (match-beginning 1))
  20901. (substring hd (match-end 1))))
  20902. (setq pri org-default-priority))
  20903. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  20904. (- org-lowest-priority org-highest-priority))))))
  20905. (princ (format "BEGIN:VTODO
  20906. %s
  20907. SUMMARY:%s
  20908. CATEGORIES:%s
  20909. SEQUENCE:1
  20910. PRIORITY:%d
  20911. STATUS:%s
  20912. END:VTODO\n"
  20913. dts hd category pri status)))))))))
  20914. (defun org-start-icalendar-file (name)
  20915. "Start an iCalendar file by inserting the header."
  20916. (let ((user user-full-name)
  20917. (name (or name "unknown"))
  20918. (timezone (cadr (current-time-zone))))
  20919. (princ
  20920. (format "BEGIN:VCALENDAR
  20921. VERSION:2.0
  20922. X-WR-CALNAME:%s
  20923. PRODID:-//%s//Emacs with Org-mode//EN
  20924. X-WR-TIMEZONE:%s
  20925. CALSCALE:GREGORIAN\n" name user timezone))))
  20926. (defun org-finish-icalendar-file ()
  20927. "Finish an iCalendar file by inserting the END statement."
  20928. (princ "END:VCALENDAR\n"))
  20929. (defun org-ical-ts-to-string (s keyword &optional inc)
  20930. "Take a time string S and convert it to iCalendar format.
  20931. KEYWORD is added in front, to make a complete line like DTSTART....
  20932. When INC is non-nil, increase the hour by two (if time string contains
  20933. a time), or the day by one (if it does not contain a time)."
  20934. (let ((t1 (org-parse-time-string s 'nodefault))
  20935. t2 fmt have-time time)
  20936. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  20937. (setq t2 t1 have-time t)
  20938. (setq t2 (org-parse-time-string s)))
  20939. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  20940. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  20941. (when inc
  20942. (if have-time
  20943. (if org-agenda-default-appointment-duration
  20944. (setq mi (+ org-agenda-default-appointment-duration mi))
  20945. (setq h (+ 2 h)))
  20946. (setq d (1+ d))))
  20947. (setq time (encode-time s mi h d m y)))
  20948. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  20949. (concat keyword (format-time-string fmt time))))
  20950. ;;; XOXO export
  20951. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  20952. (with-current-buffer buffer
  20953. (apply 'insert output)))
  20954. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  20955. (defun org-export-as-xoxo (&optional buffer)
  20956. "Export the org buffer as XOXO.
  20957. The XOXO buffer is named *xoxo-<source buffer name>*"
  20958. (interactive (list (current-buffer)))
  20959. ;; A quickie abstraction
  20960. ;; Output everything as XOXO
  20961. (with-current-buffer (get-buffer buffer)
  20962. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  20963. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  20964. (org-infile-export-plist)))
  20965. (filename (concat (file-name-as-directory
  20966. (org-export-directory :xoxo opt-plist))
  20967. (file-name-sans-extension
  20968. (file-name-nondirectory buffer-file-name))
  20969. ".html"))
  20970. (out (find-file-noselect filename))
  20971. (last-level 1)
  20972. (hanging-li nil))
  20973. ;; Check the output buffer is empty.
  20974. (with-current-buffer out (erase-buffer))
  20975. ;; Kick off the output
  20976. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  20977. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  20978. (let* ((hd (match-string-no-properties 1))
  20979. (level (length hd))
  20980. (text (concat
  20981. (match-string-no-properties 2)
  20982. (save-excursion
  20983. (goto-char (match-end 0))
  20984. (let ((str ""))
  20985. (catch 'loop
  20986. (while 't
  20987. (forward-line)
  20988. (if (looking-at "^[ \t]\\(.*\\)")
  20989. (setq str (concat str (match-string-no-properties 1)))
  20990. (throw 'loop str)))))))))
  20991. ;; Handle level rendering
  20992. (cond
  20993. ((> level last-level)
  20994. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  20995. ((< level last-level)
  20996. (dotimes (- (- last-level level) 1)
  20997. (if hanging-li
  20998. (org-export-as-xoxo-insert-into out "</li>\n"))
  20999. (org-export-as-xoxo-insert-into out "</ol>\n"))
  21000. (when hanging-li
  21001. (org-export-as-xoxo-insert-into out "</li>\n")
  21002. (setq hanging-li nil)))
  21003. ((equal level last-level)
  21004. (if hanging-li
  21005. (org-export-as-xoxo-insert-into out "</li>\n")))
  21006. )
  21007. (setq last-level level)
  21008. ;; And output the new li
  21009. (setq hanging-li 't)
  21010. (if (equal ?+ (elt text 0))
  21011. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  21012. (org-export-as-xoxo-insert-into out "<li>" text))))
  21013. ;; Finally finish off the ol
  21014. (dotimes (- last-level 1)
  21015. (if hanging-li
  21016. (org-export-as-xoxo-insert-into out "</li>\n"))
  21017. (org-export-as-xoxo-insert-into out "</ol>\n"))
  21018. ;; Finish the buffer off and clean it up.
  21019. (switch-to-buffer-other-window out)
  21020. (indent-region (point-min) (point-max) nil)
  21021. (save-buffer)
  21022. (goto-char (point-min))
  21023. )))
  21024. ;;;; Key bindings
  21025. ;; Make `C-c C-x' a prefix key
  21026. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  21027. ;; TAB key with modifiers
  21028. (org-defkey org-mode-map "\C-i" 'org-cycle)
  21029. (org-defkey org-mode-map [(tab)] 'org-cycle)
  21030. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  21031. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  21032. (org-defkey org-mode-map "\M-\t" 'org-complete)
  21033. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  21034. ;; The following line is necessary under Suse GNU/Linux
  21035. (unless (featurep 'xemacs)
  21036. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  21037. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  21038. (define-key org-mode-map (kbd "<backtab>") 'org-shifttab)
  21039. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  21040. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  21041. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  21042. ;; Cursor keys with modifiers
  21043. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  21044. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  21045. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  21046. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  21047. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  21048. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  21049. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  21050. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  21051. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  21052. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  21053. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  21054. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  21055. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  21056. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  21057. ;;; Extra keys for tty access.
  21058. ;; We only set them when really needed because otherwise the
  21059. ;; menus don't show the simple keys
  21060. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  21061. (not window-system))
  21062. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  21063. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  21064. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  21065. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  21066. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  21067. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  21068. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  21069. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  21070. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  21071. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  21072. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  21073. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  21074. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  21075. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  21076. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  21077. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  21078. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  21079. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  21080. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  21081. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  21082. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  21083. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  21084. ;; All the other keys
  21085. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  21086. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  21087. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  21088. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  21089. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  21090. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  21091. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  21092. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  21093. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  21094. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  21095. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  21096. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  21097. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  21098. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  21099. (org-defkey org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  21100. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  21101. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  21102. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  21103. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  21104. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  21105. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  21106. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  21107. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  21108. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  21109. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  21110. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  21111. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  21112. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  21113. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  21114. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  21115. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  21116. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  21117. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  21118. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  21119. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  21120. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  21121. (org-defkey org-mode-map "\C-c^" 'org-sort)
  21122. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  21123. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  21124. (org-defkey org-mode-map "\C-m" 'org-return)
  21125. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  21126. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  21127. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  21128. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  21129. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  21130. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  21131. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  21132. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  21133. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  21134. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  21135. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  21136. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  21137. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  21138. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  21139. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  21140. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  21141. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  21142. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  21143. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  21144. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  21145. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  21146. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  21147. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  21148. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  21149. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  21150. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  21151. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  21152. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  21153. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  21154. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  21155. (when (featurep 'xemacs)
  21156. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  21157. (defsubst org-table-p () (org-at-table-p))
  21158. (defun org-self-insert-command (N)
  21159. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  21160. If the cursor is in a table looking at whitespace, the whitespace is
  21161. overwritten, and the table is not marked as requiring realignment."
  21162. (interactive "p")
  21163. (if (and (org-table-p)
  21164. (progn
  21165. ;; check if we blank the field, and if that triggers align
  21166. (and org-table-auto-blank-field
  21167. (member last-command
  21168. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  21169. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  21170. ;; got extra space, this field does not determine column width
  21171. (let (org-table-may-need-update) (org-table-blank-field))
  21172. ;; no extra space, this field may determine column width
  21173. (org-table-blank-field)))
  21174. t)
  21175. (eq N 1)
  21176. (looking-at "[^|\n]* |"))
  21177. (let (org-table-may-need-update)
  21178. (goto-char (1- (match-end 0)))
  21179. (delete-backward-char 1)
  21180. (goto-char (match-beginning 0))
  21181. (self-insert-command N))
  21182. (setq org-table-may-need-update t)
  21183. (self-insert-command N)
  21184. (org-fix-tags-on-the-fly)))
  21185. (defun org-fix-tags-on-the-fly ()
  21186. (when (and (equal (char-after (point-at-bol)) ?*)
  21187. (org-on-heading-p))
  21188. (org-align-tags-here org-tags-column)))
  21189. (defun org-delete-backward-char (N)
  21190. "Like `delete-backward-char', insert whitespace at field end in tables.
  21191. When deleting backwards, in tables this function will insert whitespace in
  21192. front of the next \"|\" separator, to keep the table aligned. The table will
  21193. still be marked for re-alignment if the field did fill the entire column,
  21194. because, in this case the deletion might narrow the column."
  21195. (interactive "p")
  21196. (if (and (org-table-p)
  21197. (eq N 1)
  21198. (string-match "|" (buffer-substring (point-at-bol) (point)))
  21199. (looking-at ".*?|"))
  21200. (let ((pos (point))
  21201. (noalign (looking-at "[^|\n\r]* |"))
  21202. (c org-table-may-need-update))
  21203. (backward-delete-char N)
  21204. (skip-chars-forward "^|")
  21205. (insert " ")
  21206. (goto-char (1- pos))
  21207. ;; noalign: if there were two spaces at the end, this field
  21208. ;; does not determine the width of the column.
  21209. (if noalign (setq org-table-may-need-update c)))
  21210. (backward-delete-char N)
  21211. (org-fix-tags-on-the-fly)))
  21212. (defun org-delete-char (N)
  21213. "Like `delete-char', but insert whitespace at field end in tables.
  21214. When deleting characters, in tables this function will insert whitespace in
  21215. front of the next \"|\" separator, to keep the table aligned. The table will
  21216. still be marked for re-alignment if the field did fill the entire column,
  21217. because, in this case the deletion might narrow the column."
  21218. (interactive "p")
  21219. (if (and (org-table-p)
  21220. (not (bolp))
  21221. (not (= (char-after) ?|))
  21222. (eq N 1))
  21223. (if (looking-at ".*?|")
  21224. (let ((pos (point))
  21225. (noalign (looking-at "[^|\n\r]* |"))
  21226. (c org-table-may-need-update))
  21227. (replace-match (concat
  21228. (substring (match-string 0) 1 -1)
  21229. " |"))
  21230. (goto-char pos)
  21231. ;; noalign: if there were two spaces at the end, this field
  21232. ;; does not determine the width of the column.
  21233. (if noalign (setq org-table-may-need-update c)))
  21234. (delete-char N))
  21235. (delete-char N)
  21236. (org-fix-tags-on-the-fly)))
  21237. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  21238. (put 'org-self-insert-command 'delete-selection t)
  21239. (put 'orgtbl-self-insert-command 'delete-selection t)
  21240. (put 'org-delete-char 'delete-selection 'supersede)
  21241. (put 'org-delete-backward-char 'delete-selection 'supersede)
  21242. ;; Make `flyspell-mode' delay after some commands
  21243. (put 'org-self-insert-command 'flyspell-delayed t)
  21244. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  21245. (put 'org-delete-char 'flyspell-delayed t)
  21246. (put 'org-delete-backward-char 'flyspell-delayed t)
  21247. ;; How to do this: Measure non-white length of current string
  21248. ;; If equal to column width, we should realign.
  21249. (defun org-remap (map &rest commands)
  21250. "In MAP, remap the functions given in COMMANDS.
  21251. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  21252. (let (new old)
  21253. (while commands
  21254. (setq old (pop commands) new (pop commands))
  21255. (if (fboundp 'command-remapping)
  21256. (org-defkey map (vector 'remap old) new)
  21257. (substitute-key-definition old new map global-map)))))
  21258. (when (eq org-enable-table-editor 'optimized)
  21259. ;; If the user wants maximum table support, we need to hijack
  21260. ;; some standard editing functions
  21261. (org-remap org-mode-map
  21262. 'self-insert-command 'org-self-insert-command
  21263. 'delete-char 'org-delete-char
  21264. 'delete-backward-char 'org-delete-backward-char)
  21265. (org-defkey org-mode-map "|" 'org-force-self-insert))
  21266. (defun org-shiftcursor-error ()
  21267. "Throw an error because Shift-Cursor command was applied in wrong context."
  21268. (error "This command is active in special context like tables, headlines or timestamps"))
  21269. (defun org-shifttab (&optional arg)
  21270. "Global visibility cycling or move to previous table field.
  21271. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  21272. on context.
  21273. See the individual commands for more information."
  21274. (interactive "P")
  21275. (cond
  21276. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  21277. (arg (message "Content view to level: ")
  21278. (org-content (prefix-numeric-value arg))
  21279. (setq org-cycle-global-status 'overview))
  21280. (t (call-interactively 'org-global-cycle))))
  21281. (defun org-shiftmetaleft ()
  21282. "Promote subtree or delete table column.
  21283. Calls `org-promote-subtree', `org-outdent-item',
  21284. or `org-table-delete-column', depending on context.
  21285. See the individual commands for more information."
  21286. (interactive)
  21287. (cond
  21288. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  21289. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  21290. ((org-at-item-p) (call-interactively 'org-outdent-item))
  21291. (t (org-shiftcursor-error))))
  21292. (defun org-shiftmetaright ()
  21293. "Demote subtree or insert table column.
  21294. Calls `org-demote-subtree', `org-indent-item',
  21295. or `org-table-insert-column', depending on context.
  21296. See the individual commands for more information."
  21297. (interactive)
  21298. (cond
  21299. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  21300. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  21301. ((org-at-item-p) (call-interactively 'org-indent-item))
  21302. (t (org-shiftcursor-error))))
  21303. (defun org-shiftmetaup (&optional arg)
  21304. "Move subtree up or kill table row.
  21305. Calls `org-move-subtree-up' or `org-table-kill-row' or
  21306. `org-move-item-up' depending on context. See the individual commands
  21307. for more information."
  21308. (interactive "P")
  21309. (cond
  21310. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  21311. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  21312. ((org-at-item-p) (call-interactively 'org-move-item-up))
  21313. (t (org-shiftcursor-error))))
  21314. (defun org-shiftmetadown (&optional arg)
  21315. "Move subtree down or insert table row.
  21316. Calls `org-move-subtree-down' or `org-table-insert-row' or
  21317. `org-move-item-down', depending on context. See the individual
  21318. commands for more information."
  21319. (interactive "P")
  21320. (cond
  21321. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  21322. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  21323. ((org-at-item-p) (call-interactively 'org-move-item-down))
  21324. (t (org-shiftcursor-error))))
  21325. (defun org-metaleft (&optional arg)
  21326. "Promote heading or move table column to left.
  21327. Calls `org-do-promote' or `org-table-move-column', depending on context.
  21328. With no specific context, calls the Emacs default `backward-word'.
  21329. See the individual commands for more information."
  21330. (interactive "P")
  21331. (cond
  21332. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  21333. ((or (org-on-heading-p) (org-region-active-p))
  21334. (call-interactively 'org-do-promote))
  21335. ((org-at-item-p) (call-interactively 'org-outdent-item))
  21336. (t (call-interactively 'backward-word))))
  21337. (defun org-metaright (&optional arg)
  21338. "Demote subtree or move table column to right.
  21339. Calls `org-do-demote' or `org-table-move-column', depending on context.
  21340. With no specific context, calls the Emacs default `forward-word'.
  21341. See the individual commands for more information."
  21342. (interactive "P")
  21343. (cond
  21344. ((org-at-table-p) (call-interactively 'org-table-move-column))
  21345. ((or (org-on-heading-p) (org-region-active-p))
  21346. (call-interactively 'org-do-demote))
  21347. ((org-at-item-p) (call-interactively 'org-indent-item))
  21348. (t (call-interactively 'forward-word))))
  21349. (defun org-metaup (&optional arg)
  21350. "Move subtree up or move table row up.
  21351. Calls `org-move-subtree-up' or `org-table-move-row' or
  21352. `org-move-item-up', depending on context. See the individual commands
  21353. for more information."
  21354. (interactive "P")
  21355. (cond
  21356. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  21357. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  21358. ((org-at-item-p) (call-interactively 'org-move-item-up))
  21359. (t (org-shiftcursor-error))))
  21360. (defun org-metadown (&optional arg)
  21361. "Move subtree down or move table row down.
  21362. Calls `org-move-subtree-down' or `org-table-move-row' or
  21363. `org-move-item-down', depending on context. See the individual
  21364. commands for more information."
  21365. (interactive "P")
  21366. (cond
  21367. ((org-at-table-p) (call-interactively 'org-table-move-row))
  21368. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  21369. ((org-at-item-p) (call-interactively 'org-move-item-down))
  21370. (t (org-shiftcursor-error))))
  21371. (defun org-shiftup (&optional arg)
  21372. "Increase item in timestamp or increase priority of current headline.
  21373. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  21374. depending on context. See the individual commands for more information."
  21375. (interactive "P")
  21376. (cond
  21377. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  21378. ((org-on-heading-p) (call-interactively 'org-priority-up))
  21379. ((org-at-item-p) (call-interactively 'org-previous-item))
  21380. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  21381. (defun org-shiftdown (&optional arg)
  21382. "Decrease item in timestamp or decrease priority of current headline.
  21383. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  21384. depending on context. See the individual commands for more information."
  21385. (interactive "P")
  21386. (cond
  21387. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  21388. ((org-on-heading-p) (call-interactively 'org-priority-down))
  21389. (t (call-interactively 'org-next-item))))
  21390. (defun org-shiftright ()
  21391. "Next TODO keyword or timestamp one day later, depending on context."
  21392. (interactive)
  21393. (cond
  21394. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  21395. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  21396. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  21397. (t (org-shiftcursor-error))))
  21398. (defun org-shiftleft ()
  21399. "Previous TODO keyword or timestamp one day earlier, depending on context."
  21400. (interactive)
  21401. (cond
  21402. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  21403. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  21404. ((org-at-property-p)
  21405. (call-interactively 'org-property-previous-allowed-value))
  21406. (t (org-shiftcursor-error))))
  21407. (defun org-shiftcontrolright ()
  21408. "Switch to next TODO set."
  21409. (interactive)
  21410. (cond
  21411. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  21412. (t (org-shiftcursor-error))))
  21413. (defun org-shiftcontrolleft ()
  21414. "Switch to previous TODO set."
  21415. (interactive)
  21416. (cond
  21417. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  21418. (t (org-shiftcursor-error))))
  21419. (defun org-ctrl-c-ret ()
  21420. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  21421. (interactive)
  21422. (cond
  21423. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  21424. (t (call-interactively 'org-insert-heading))))
  21425. (defun org-copy-special ()
  21426. "Copy region in table or copy current subtree.
  21427. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  21428. See the individual commands for more information."
  21429. (interactive)
  21430. (call-interactively
  21431. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  21432. (defun org-cut-special ()
  21433. "Cut region in table or cut current subtree.
  21434. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  21435. See the individual commands for more information."
  21436. (interactive)
  21437. (call-interactively
  21438. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  21439. (defun org-paste-special (arg)
  21440. "Paste rectangular region into table, or past subtree relative to level.
  21441. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  21442. See the individual commands for more information."
  21443. (interactive "P")
  21444. (if (org-at-table-p)
  21445. (org-table-paste-rectangle)
  21446. (org-paste-subtree arg)))
  21447. (defun org-ctrl-c-ctrl-c (&optional arg)
  21448. "Set tags in headline, or update according to changed information at point.
  21449. This command does many different things, depending on context:
  21450. - If the cursor is in a headline, prompt for tags and insert them
  21451. into the current line, aligned to `org-tags-column'. When called
  21452. with prefix arg, realign all tags in the current buffer.
  21453. - If the cursor is in one of the special #+KEYWORD lines, this
  21454. triggers scanning the buffer for these lines and updating the
  21455. information.
  21456. - If the cursor is inside a table, realign the table. This command
  21457. works even if the automatic table editor has been turned off.
  21458. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  21459. the entire table.
  21460. - If the cursor is inside a table created by the table.el package,
  21461. activate that table.
  21462. - If the current buffer is a remember buffer, close note and file it.
  21463. with a prefix argument, file it without further interaction to the default
  21464. location.
  21465. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  21466. links in this buffer.
  21467. - If the cursor is on a numbered item in a plain list, renumber the
  21468. ordered list."
  21469. (interactive "P")
  21470. (let ((org-enable-table-editor t))
  21471. (cond
  21472. ((or org-clock-overlays
  21473. org-occur-highlights
  21474. org-latex-fragment-image-overlays)
  21475. (org-remove-clock-overlays)
  21476. (org-remove-occur-highlights)
  21477. (org-remove-latex-fragment-image-overlays)
  21478. (message "Temporary highlights/overlays removed from current buffer"))
  21479. ((and (local-variable-p 'org-finish-function (current-buffer))
  21480. (fboundp org-finish-function))
  21481. (funcall org-finish-function))
  21482. ((org-at-property-p)
  21483. (call-interactively 'org-property-action))
  21484. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  21485. ((org-on-heading-p) (call-interactively 'org-set-tags))
  21486. ((org-at-table.el-p)
  21487. (require 'table)
  21488. (beginning-of-line 1)
  21489. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  21490. (call-interactively 'table-recognize-table))
  21491. ((org-at-table-p)
  21492. (org-table-maybe-eval-formula)
  21493. (if arg
  21494. (call-interactively 'org-table-recalculate)
  21495. (org-table-maybe-recalculate-line))
  21496. (call-interactively 'org-table-align))
  21497. ((org-at-item-checkbox-p)
  21498. (call-interactively 'org-toggle-checkbox))
  21499. ((org-at-item-p)
  21500. (call-interactively 'org-maybe-renumber-ordered-list))
  21501. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  21502. (cond
  21503. ((equal (match-string 1) "TBLFM")
  21504. ;; Recalculate the table before this line
  21505. (save-excursion
  21506. (beginning-of-line 1)
  21507. (skip-chars-backward " \r\n\t")
  21508. (if (org-at-table-p)
  21509. (org-call-with-arg 'org-table-recalculate t))))
  21510. (t
  21511. (call-interactively 'org-mode-restart))))
  21512. (t (error "C-c C-c can do nothing useful at this location.")))))
  21513. (defun org-mode-restart ()
  21514. "Restart Org-mode, to scan again for special lines.
  21515. Also updates the keyword regular expressions."
  21516. (interactive)
  21517. (let ((org-inhibit-startup t)) (org-mode))
  21518. (message "Org-mode restarted to refresh keyword and special line setup"))
  21519. (defun org-return ()
  21520. "Goto next table row or insert a newline.
  21521. Calls `org-table-next-row' or `newline', depending on context.
  21522. See the individual commands for more information."
  21523. (interactive)
  21524. (cond
  21525. ((bobp) (newline))
  21526. ((org-at-table-p)
  21527. (org-table-justify-field-maybe)
  21528. (call-interactively 'org-table-next-row))
  21529. (t (newline))))
  21530. (defun org-ctrl-c-minus ()
  21531. "Insert separator line in table or modify bullet type in list.
  21532. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  21533. depending on context."
  21534. (interactive)
  21535. (cond
  21536. ((org-at-table-p)
  21537. (call-interactively 'org-table-insert-hline))
  21538. ((org-in-item-p)
  21539. (call-interactively 'org-cycle-list-bullet))
  21540. (t (error "`C-c -' does have no function here."))))
  21541. (defun org-meta-return (&optional arg)
  21542. "Insert a new heading or wrap a region in a table.
  21543. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  21544. See the individual commands for more information."
  21545. (interactive "P")
  21546. (cond
  21547. ((org-at-table-p)
  21548. (call-interactively 'org-table-wrap-region))
  21549. (t (call-interactively 'org-insert-heading))))
  21550. ;;; Menu entries
  21551. ;; Define the Org-mode menus
  21552. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  21553. '("Tbl"
  21554. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  21555. ["Next Field" org-cycle (org-at-table-p)]
  21556. ["Previous Field" org-shifttab (org-at-table-p)]
  21557. ["Next Row" org-return (org-at-table-p)]
  21558. "--"
  21559. ["Blank Field" org-table-blank-field (org-at-table-p)]
  21560. ["Edit Field" org-table-edit-field (org-at-table-p)]
  21561. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  21562. "--"
  21563. ("Column"
  21564. ["Move Column Left" org-metaleft (org-at-table-p)]
  21565. ["Move Column Right" org-metaright (org-at-table-p)]
  21566. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  21567. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  21568. ("Row"
  21569. ["Move Row Up" org-metaup (org-at-table-p)]
  21570. ["Move Row Down" org-metadown (org-at-table-p)]
  21571. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  21572. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  21573. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  21574. "--"
  21575. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  21576. ("Rectangle"
  21577. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  21578. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  21579. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  21580. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  21581. "--"
  21582. ("Calculate"
  21583. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  21584. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  21585. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  21586. "--"
  21587. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  21588. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  21589. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  21590. "--"
  21591. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  21592. "--"
  21593. ["Sum Column/Rectangle" org-table-sum
  21594. (or (org-at-table-p) (org-region-active-p))]
  21595. ["Which Column?" org-table-current-column (org-at-table-p)])
  21596. ["Debug Formulas"
  21597. org-table-toggle-formula-debugger
  21598. :style toggle :selected org-table-formula-debug]
  21599. ["Show Col/Row Numbers"
  21600. org-table-toggle-coordinate-overlays
  21601. :style toggle :selected org-table-overlay-coordinates]
  21602. "--"
  21603. ["Create" org-table-create (and (not (org-at-table-p))
  21604. org-enable-table-editor)]
  21605. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  21606. ["Import from File" org-table-import (not (org-at-table-p))]
  21607. ["Export to File" org-table-export (org-at-table-p)]
  21608. "--"
  21609. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  21610. (easy-menu-define org-org-menu org-mode-map "Org menu"
  21611. '("Org"
  21612. ("Show/Hide"
  21613. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  21614. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  21615. ["Sparse Tree" org-occur t]
  21616. ["Reveal Context" org-reveal t]
  21617. ["Show All" show-all t]
  21618. "--"
  21619. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  21620. "--"
  21621. ["New Heading" org-insert-heading t]
  21622. ("Navigate Headings"
  21623. ["Up" outline-up-heading t]
  21624. ["Next" outline-next-visible-heading t]
  21625. ["Previous" outline-previous-visible-heading t]
  21626. ["Next Same Level" outline-forward-same-level t]
  21627. ["Previous Same Level" outline-backward-same-level t]
  21628. "--"
  21629. ["Jump" org-goto t]
  21630. "--"
  21631. ["C-a/e find headline/item start/end"
  21632. (setq org-special-ctrl-a/e (not org-special-ctrl-a/e))
  21633. :style toggle :selected org-special-ctrl-a/e])
  21634. ("Edit Structure"
  21635. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  21636. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  21637. "--"
  21638. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  21639. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  21640. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  21641. "--"
  21642. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  21643. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  21644. ["Demote Heading" org-metaright (not (org-at-table-p))]
  21645. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  21646. "--"
  21647. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  21648. "--"
  21649. ["Convert to odd levels" org-convert-to-odd-levels t]
  21650. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  21651. ("Editing"
  21652. ["Emphasis..." org-emphasize t])
  21653. ("Archive"
  21654. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  21655. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  21656. ; :active t :keys "C-u C-c C-x C-a"]
  21657. ["Sparse trees open ARCHIVE trees"
  21658. (setq org-sparse-tree-open-archived-trees
  21659. (not org-sparse-tree-open-archived-trees))
  21660. :style toggle :selected org-sparse-tree-open-archived-trees]
  21661. ["Cycling opens ARCHIVE trees"
  21662. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  21663. :style toggle :selected org-cycle-open-archived-trees]
  21664. ["Agenda includes ARCHIVE trees"
  21665. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  21666. :style toggle :selected (not org-agenda-skip-archived-trees)]
  21667. "--"
  21668. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  21669. ; ["Check and Move Children" (org-archive-subtree '(4))
  21670. ; :active t :keys "C-u C-c C-x C-s"]
  21671. )
  21672. "--"
  21673. ("TODO Lists"
  21674. ["TODO/DONE/-" org-todo t]
  21675. ("Select keyword"
  21676. ["Next keyword" org-shiftright (org-on-heading-p)]
  21677. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  21678. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  21679. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  21680. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  21681. ["Show TODO Tree" org-show-todo-tree t]
  21682. ["Global TODO list" org-todo-list t]
  21683. "--"
  21684. ["Set Priority" org-priority t]
  21685. ["Priority Up" org-shiftup t]
  21686. ["Priority Down" org-shiftdown t])
  21687. ("TAGS and Properties"
  21688. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  21689. ["Column view of properties" org-columns t])
  21690. ("Dates and Scheduling"
  21691. ["Timestamp" org-time-stamp t]
  21692. ["Timestamp (inactive)" org-time-stamp-inactive t]
  21693. ("Change Date"
  21694. ["1 Day Later" org-shiftright t]
  21695. ["1 Day Earlier" org-shiftleft t]
  21696. ["1 ... Later" org-shiftup t]
  21697. ["1 ... Earlier" org-shiftdown t])
  21698. ["Compute Time Range" org-evaluate-time-range t]
  21699. ["Schedule Item" org-schedule t]
  21700. ["Deadline" org-deadline t]
  21701. "--"
  21702. ["Custom time format" org-toggle-time-stamp-overlays
  21703. :style radio :selected org-display-custom-times]
  21704. "--"
  21705. ["Goto Calendar" org-goto-calendar t]
  21706. ["Date from Calendar" org-date-from-calendar t])
  21707. ("Logging work"
  21708. ["Clock in" org-clock-in t]
  21709. ["Clock out" org-clock-out t]
  21710. ["Clock cancel" org-clock-cancel t]
  21711. ["Display times" org-clock-display t]
  21712. ["Create clock table" org-clock-report t]
  21713. "--"
  21714. ["Record DONE time"
  21715. (progn (setq org-log-done (not org-log-done))
  21716. (message "Switching to %s will %s record a timestamp"
  21717. (car org-done-keywords)
  21718. (if org-log-done "automatically" "not")))
  21719. :style toggle :selected org-log-done])
  21720. "--"
  21721. ["Agenda Command..." org-agenda t]
  21722. ("File List for Agenda")
  21723. ("Special views current file"
  21724. ["TODO Tree" org-show-todo-tree t]
  21725. ["Check Deadlines" org-check-deadlines t]
  21726. ["Timeline" org-timeline t]
  21727. ["Tags Tree" org-tags-sparse-tree t])
  21728. "--"
  21729. ("Hyperlinks"
  21730. ["Store Link (Global)" org-store-link t]
  21731. ["Insert Link" org-insert-link t]
  21732. ["Follow Link" org-open-at-point t]
  21733. "--"
  21734. ["Next link" org-next-link t]
  21735. ["Previous link" org-previous-link t]
  21736. "--"
  21737. ["Descriptive Links"
  21738. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  21739. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  21740. ["Literal Links"
  21741. (progn
  21742. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  21743. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  21744. "--"
  21745. ["Export/Publish..." org-export t]
  21746. ("LaTeX"
  21747. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  21748. :selected org-cdlatex-mode]
  21749. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  21750. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  21751. ["Modify math symbol" org-cdlatex-math-modify
  21752. (org-inside-LaTeX-fragment-p)]
  21753. ["Export LaTeX fragments as images"
  21754. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  21755. :style toggle :selected org-export-with-LaTeX-fragments])
  21756. "--"
  21757. ("Documentation"
  21758. ["Show Version" org-version t]
  21759. ["Info Documentation" org-info t])
  21760. ("Customize"
  21761. ["Browse Org Group" org-customize t]
  21762. "--"
  21763. ["Expand This Menu" org-create-customize-menu
  21764. (fboundp 'customize-menu-create)])
  21765. "--"
  21766. ["Refresh setup" org-mode-restart t]
  21767. ))
  21768. (defun org-info (&optional node)
  21769. "Read documentation for Org-mode in the info system.
  21770. With optional NODE, go directly to that node."
  21771. (interactive)
  21772. (require 'info)
  21773. (Info-goto-node (format "(org)%s" (or node ""))))
  21774. (defun org-install-agenda-files-menu ()
  21775. (let ((bl (buffer-list)))
  21776. (save-excursion
  21777. (while bl
  21778. (set-buffer (pop bl))
  21779. (if (org-mode-p) (setq bl nil)))
  21780. (when (org-mode-p)
  21781. (easy-menu-change
  21782. '("Org") "File List for Agenda"
  21783. (append
  21784. (list
  21785. ["Edit File List" (org-edit-agenda-file-list) t]
  21786. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  21787. ["Remove Current File from List" org-remove-file t]
  21788. ["Cycle through agenda files" org-cycle-agenda-files t]
  21789. "--")
  21790. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  21791. ;;;; Documentation
  21792. (defun org-customize ()
  21793. "Call the customize function with org as argument."
  21794. (interactive)
  21795. (customize-browse 'org))
  21796. (defun org-create-customize-menu ()
  21797. "Create a full customization menu for Org-mode, insert it into the menu."
  21798. (interactive)
  21799. (if (fboundp 'customize-menu-create)
  21800. (progn
  21801. (easy-menu-change
  21802. '("Org") "Customize"
  21803. `(["Browse Org group" org-customize t]
  21804. "--"
  21805. ,(customize-menu-create 'org)
  21806. ["Set" Custom-set t]
  21807. ["Save" Custom-save t]
  21808. ["Reset to Current" Custom-reset-current t]
  21809. ["Reset to Saved" Custom-reset-saved t]
  21810. ["Reset to Standard Settings" Custom-reset-standard t]))
  21811. (message "\"Org\"-menu now contains full customization menu"))
  21812. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  21813. ;;;; Miscellaneous stuff
  21814. ;;; Generally useful functions
  21815. (defun org-context ()
  21816. "Return a list of contexts of the current cursor position.
  21817. If several contexts apply, all are returned.
  21818. Each context entry is a list with a symbol naming the context, and
  21819. two positions indicating start and end of the context. Possible
  21820. contexts are:
  21821. :headline anywhere in a headline
  21822. :headline-stars on the leading stars in a headline
  21823. :todo-keyword on a TODO keyword (including DONE) in a headline
  21824. :tags on the TAGS in a headline
  21825. :priority on the priority cookie in a headline
  21826. :item on the first line of a plain list item
  21827. :item-bullet on the bullet/number of a plain list item
  21828. :checkbox on the checkbox in a plain list item
  21829. :table in an org-mode table
  21830. :table-special on a special filed in a table
  21831. :table-table in a table.el table
  21832. :link on a hyperlink
  21833. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  21834. :target on a <<target>>
  21835. :radio-target on a <<<radio-target>>>
  21836. :latex-fragment on a LaTeX fragment
  21837. :latex-preview on a LaTeX fragment with overlayed preview image
  21838. This function expects the position to be visible because it uses font-lock
  21839. faces as a help to recognize the following contexts: :table-special, :link,
  21840. and :keyword."
  21841. (let* ((f (get-text-property (point) 'face))
  21842. (faces (if (listp f) f (list f)))
  21843. (p (point)) clist o)
  21844. ;; First the large context
  21845. (cond
  21846. ((org-on-heading-p t)
  21847. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  21848. (when (progn
  21849. (beginning-of-line 1)
  21850. (looking-at org-todo-line-tags-regexp))
  21851. (push (org-point-in-group p 1 :headline-stars) clist)
  21852. (push (org-point-in-group p 2 :todo-keyword) clist)
  21853. (push (org-point-in-group p 4 :tags) clist))
  21854. (goto-char p)
  21855. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  21856. (if (looking-at "\\[#[A-Z0-9]\\]")
  21857. (push (org-point-in-group p 0 :priority) clist)))
  21858. ((org-at-item-p)
  21859. (push (org-point-in-group p 2 :item-bullet) clist)
  21860. (push (list :item (point-at-bol)
  21861. (save-excursion (org-end-of-item) (point)))
  21862. clist)
  21863. (and (org-at-item-checkbox-p)
  21864. (push (org-point-in-group p 0 :checkbox) clist)))
  21865. ((org-at-table-p)
  21866. (push (list :table (org-table-begin) (org-table-end)) clist)
  21867. (if (memq 'org-formula faces)
  21868. (push (list :table-special
  21869. (previous-single-property-change p 'face)
  21870. (next-single-property-change p 'face)) clist)))
  21871. ((org-at-table-p 'any)
  21872. (push (list :table-table) clist)))
  21873. (goto-char p)
  21874. ;; Now the small context
  21875. (cond
  21876. ((org-at-timestamp-p)
  21877. (push (org-point-in-group p 0 :timestamp) clist))
  21878. ((memq 'org-link faces)
  21879. (push (list :link
  21880. (previous-single-property-change p 'face)
  21881. (next-single-property-change p 'face)) clist))
  21882. ((memq 'org-special-keyword faces)
  21883. (push (list :keyword
  21884. (previous-single-property-change p 'face)
  21885. (next-single-property-change p 'face)) clist))
  21886. ((org-on-target-p)
  21887. (push (org-point-in-group p 0 :target) clist)
  21888. (goto-char (1- (match-beginning 0)))
  21889. (if (looking-at org-radio-target-regexp)
  21890. (push (org-point-in-group p 0 :radio-target) clist))
  21891. (goto-char p))
  21892. ((setq o (car (delq nil
  21893. (mapcar
  21894. (lambda (x)
  21895. (if (memq x org-latex-fragment-image-overlays) x))
  21896. (org-overlays-at (point))))))
  21897. (push (list :latex-fragment
  21898. (org-overlay-start o) (org-overlay-end o)) clist)
  21899. (push (list :latex-preview
  21900. (org-overlay-start o) (org-overlay-end o)) clist))
  21901. ((org-inside-LaTeX-fragment-p)
  21902. ;; FIXME: positions wrong.
  21903. (push (list :latex-fragment (point) (point)) clist)))
  21904. (setq clist (nreverse (delq nil clist)))
  21905. clist))
  21906. ;; FIXME Compare with at-regexp-p
  21907. (defun org-in-regexp (re &optional nlines visually)
  21908. "Check if point is inside a match of regexp.
  21909. Normally only the current line is checked, but you can include NLINES extra
  21910. lines both before and after point into the search.
  21911. If VISUALLY is set, require that the cursor is not after the match but
  21912. really on, so that the block visually is on the match."
  21913. (catch 'exit
  21914. (let ((pos (point))
  21915. (eol (point-at-eol (+ 1 (or nlines 0))))
  21916. (inc (if visually 1 0)))
  21917. (save-excursion
  21918. (beginning-of-line (- 1 (or nlines 0)))
  21919. (while (re-search-forward re eol t)
  21920. (if (and (<= (match-beginning 0) pos)
  21921. (>= (+ inc (match-end 0)) pos))
  21922. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  21923. (defun org-at-regexp-p (regexp)
  21924. "Is point inside a match of REGEXP in the current line?"
  21925. (catch 'exit
  21926. (save-excursion
  21927. (let ((pos (point)) (end (point-at-eol)))
  21928. (beginning-of-line 1)
  21929. (while (re-search-forward regexp end t)
  21930. (if (and (<= (match-beginning 0) pos)
  21931. (>= (match-end 0) pos))
  21932. (throw 'exit t)))
  21933. nil))))
  21934. (defun org-uniquify (list)
  21935. "Remove duplicate elements from LIST."
  21936. (let (res)
  21937. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  21938. res))
  21939. (defun org-delete-all (elts list)
  21940. "Remove all elements in ELTS from LIST."
  21941. (while elts
  21942. (setq list (delete (pop elts) list)))
  21943. list)
  21944. (defun org-point-in-group (point group &optional context)
  21945. "Check if POINT is in match-group GROUP.
  21946. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  21947. match. If the match group does ot exist or point is not inside it,
  21948. return nil."
  21949. (and (match-beginning group)
  21950. (>= point (match-beginning group))
  21951. (<= point (match-end group))
  21952. (if context
  21953. (list context (match-beginning group) (match-end group))
  21954. t)))
  21955. (defun org-combine-plists (&rest plists)
  21956. "Create a single property list from all plists in PLISTS.
  21957. The process starts by copying the first list, and then setting properties
  21958. from the other lists. Settings in the last list are the most significant
  21959. ones and overrule settings in the other lists."
  21960. (let ((rtn (copy-sequence (pop plists)))
  21961. p v ls)
  21962. (while plists
  21963. (setq ls (pop plists))
  21964. (while ls
  21965. (setq p (pop ls) v (pop ls))
  21966. (setq rtn (plist-put rtn p v))))
  21967. rtn))
  21968. (defun org-move-line-down (arg)
  21969. "Move the current line down. With prefix argument, move it past ARG lines."
  21970. (interactive "p")
  21971. (let ((col (current-column))
  21972. beg end pos)
  21973. (beginning-of-line 1) (setq beg (point))
  21974. (beginning-of-line 2) (setq end (point))
  21975. (beginning-of-line (+ 1 arg))
  21976. (setq pos (move-marker (make-marker) (point)))
  21977. (insert (delete-and-extract-region beg end))
  21978. (goto-char pos)
  21979. (move-to-column col)))
  21980. (defun org-move-line-up (arg)
  21981. "Move the current line up. With prefix argument, move it past ARG lines."
  21982. (interactive "p")
  21983. (let ((col (current-column))
  21984. beg end pos)
  21985. (beginning-of-line 1) (setq beg (point))
  21986. (beginning-of-line 2) (setq end (point))
  21987. (beginning-of-line (- arg))
  21988. (setq pos (move-marker (make-marker) (point)))
  21989. (insert (delete-and-extract-region beg end))
  21990. (goto-char pos)
  21991. (move-to-column col)))
  21992. (defun org-replace-escapes (string table)
  21993. "Replace %-escapes in STRING with values in TABLE.
  21994. TABLE is an association list with keys line \"%a\" and string values.
  21995. The sequences in STRING may contain normal field width and padding information,
  21996. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  21997. so values can contain further %-escapes if they are define later in TABLE."
  21998. (let ((case-fold-search nil)
  21999. e re rpl)
  22000. (while (setq e (pop table))
  22001. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  22002. (while (string-match re string)
  22003. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  22004. (cdr e)))
  22005. (setq string (replace-match rpl t t string))))
  22006. string))
  22007. (defun org-sublist (list start end)
  22008. "Return a section of LIST, from START to END.
  22009. Counting starts at 1."
  22010. (let (rtn (c start))
  22011. (setq list (nthcdr (1- start) list))
  22012. (while (and list (<= c end))
  22013. (push (pop list) rtn)
  22014. (setq c (1+ c)))
  22015. (nreverse rtn)))
  22016. (defun org-find-base-buffer-visiting (file)
  22017. "Like `find-buffer-visiting' but alway return the base buffer and
  22018. not an indirect buffer"
  22019. (let ((buf (find-buffer-visiting file)))
  22020. (or (buffer-base-buffer buf) buf)))
  22021. (defun org-image-file-name-regexp ()
  22022. "Return regexp matching the file names of images."
  22023. (if (fboundp 'image-file-name-regexp)
  22024. (image-file-name-regexp)
  22025. (let ((image-file-name-extensions
  22026. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  22027. "xbm" "xpm" "pbm" "pgm" "ppm")))
  22028. (concat "\\."
  22029. (regexp-opt (nconc (mapcar 'upcase
  22030. image-file-name-extensions)
  22031. image-file-name-extensions)
  22032. t)
  22033. "\\'"))))
  22034. (defun org-file-image-p (file)
  22035. "Return non-nil if FILE is an image."
  22036. (save-match-data
  22037. (string-match (org-image-file-name-regexp) file)))
  22038. ;;; Paragraph filling stuff.
  22039. ;; We want this to be just right, so use the full arsenal.
  22040. (defun org-indent-line-function ()
  22041. "Indent line like previous, but further if previous was headline or item."
  22042. (interactive)
  22043. (let* ((pos (point))
  22044. (itemp (org-at-item-p))
  22045. column bpos bcol tpos tcol bullet btype bullet-type)
  22046. ;; Find the previous relevant line
  22047. (beginning-of-line 1)
  22048. (cond
  22049. ((looking-at "#") (setq column 0))
  22050. ((looking-at "\\*+ ") (setq column 0))
  22051. (t
  22052. (beginning-of-line 0)
  22053. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  22054. (beginning-of-line 0))
  22055. (cond
  22056. ((looking-at "\\*+[ \t]+")
  22057. (goto-char (match-end 0))
  22058. (setq column (current-column)))
  22059. ((org-in-item-p)
  22060. (org-beginning-of-item)
  22061. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  22062. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  22063. (setq bpos (match-beginning 1) tpos (match-end 0)
  22064. bcol (progn (goto-char bpos) (current-column))
  22065. tcol (progn (goto-char tpos) (current-column))
  22066. bullet (match-string 1)
  22067. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  22068. (if (not itemp)
  22069. (setq column tcol)
  22070. (goto-char pos)
  22071. (beginning-of-line 1)
  22072. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  22073. (setq bullet (match-string 1)
  22074. btype (if (string-match "[0-9]" bullet) "n" bullet))
  22075. (setq column (if (equal btype bullet-type) bcol tcol))))
  22076. (t (setq column (org-get-indentation))))))
  22077. (goto-char pos)
  22078. (if (<= (current-column) (current-indentation))
  22079. (indent-line-to column)
  22080. (save-excursion (indent-line-to column)))
  22081. (setq column (current-column))
  22082. (beginning-of-line 1)
  22083. (if (looking-at
  22084. "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  22085. (replace-match (concat "\\1" (format org-property-format
  22086. (match-string 2) (match-string 3)))
  22087. t nil))
  22088. (move-to-column column)))
  22089. (defun org-set-autofill-regexps ()
  22090. (interactive)
  22091. ;; In the paragraph separator we include headlines, because filling
  22092. ;; text in a line directly attached to a headline would otherwise
  22093. ;; fill the headline as well.
  22094. (org-set-local 'comment-start-skip "^#+[ \t]*")
  22095. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  22096. ;; FIXME!!!!!!! (org-set-local 'paragraph-separate "\f\\|[ ]*$")
  22097. ;; The paragraph starter includes hand-formatted lists.
  22098. (org-set-local 'paragraph-start
  22099. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  22100. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  22101. ;; But only if the user has not turned off tables or fixed-width regions
  22102. (org-set-local
  22103. 'auto-fill-inhibit-regexp
  22104. (concat "\\*+ \\|#\\+"
  22105. "\\|[ \t]*" org-keyword-time-regexp
  22106. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  22107. (concat
  22108. "\\|[ \t]*["
  22109. (if org-enable-table-editor "|" "")
  22110. (if org-enable-fixed-width-editor ":" "")
  22111. "]"))))
  22112. ;; We use our own fill-paragraph function, to make sure that tables
  22113. ;; and fixed-width regions are not wrapped. That function will pass
  22114. ;; through to `fill-paragraph' when appropriate.
  22115. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  22116. ; Adaptive filling: To get full control, first make sure that
  22117. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  22118. (org-set-local 'adaptive-fill-regexp "\000")
  22119. (org-set-local 'adaptive-fill-function
  22120. 'org-adaptive-fill-function))
  22121. (defun org-fill-paragraph (&optional justify)
  22122. "Re-align a table, pass through to fill-paragraph if no table."
  22123. (let ((table-p (org-at-table-p))
  22124. (table.el-p (org-at-table.el-p)))
  22125. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  22126. (table.el-p t) ; skip table.el tables
  22127. (table-p (org-table-align) t) ; align org-mode tables
  22128. (t nil)))) ; call paragraph-fill
  22129. ;; For reference, this is the default value of adaptive-fill-regexp
  22130. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  22131. (defun org-adaptive-fill-function ()
  22132. "Return a fill prefix for org-mode files.
  22133. In particular, this makes sure hanging paragraphs for hand-formatted lists
  22134. work correctly."
  22135. (cond ((looking-at "#[ \t]+")
  22136. (match-string 0))
  22137. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  22138. (save-excursion
  22139. (goto-char (match-end 0))
  22140. (make-string (current-column) ?\ )))
  22141. (t nil)))
  22142. ;;;; Functions extending outline functionality
  22143. (defun org-beginning-of-line (&optional arg)
  22144. "Go to the beginning of the current line. If that is invisible, continue
  22145. to a visible line beginning. This makes the function of C-a more intuitive.
  22146. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  22147. first attempt, and only move to after the tags when the cursor is already
  22148. beyond the end of the headline."
  22149. (interactive "P")
  22150. (let ((pos (point)))
  22151. (beginning-of-line 1)
  22152. (if (bobp)
  22153. nil
  22154. (backward-char 1)
  22155. (if (org-invisible-p)
  22156. (while (and (not (bobp)) (org-invisible-p))
  22157. (backward-char 1)
  22158. (beginning-of-line 1))
  22159. (forward-char 1)))
  22160. (when org-special-ctrl-a/e
  22161. (cond
  22162. ((and (looking-at org-todo-line-regexp)
  22163. (= (char-after (match-end 1)) ?\ ))
  22164. (goto-char
  22165. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  22166. ((= pos (point)) (match-beginning 3))
  22167. (t (point)))))
  22168. ((org-at-item-p)
  22169. (goto-char
  22170. (cond ((> pos (match-end 4)) (match-end 4))
  22171. ((= pos (point)) (match-end 4))
  22172. (t (point)))))))))
  22173. (defun org-end-of-line (&optional arg)
  22174. "Go to the end of the line.
  22175. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  22176. first attempt, and only move to after the tags when the cursor is already
  22177. beyond the end of the headline."
  22178. (interactive "P")
  22179. (if (or (not org-special-ctrl-a/e)
  22180. (not (org-on-heading-p)))
  22181. (end-of-line arg)
  22182. (let ((pos (point)))
  22183. (beginning-of-line 1)
  22184. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  22185. (if (or (< pos (match-beginning 1))
  22186. (= pos (match-end 0)))
  22187. (goto-char (match-beginning 1))
  22188. (goto-char (match-end 0)))
  22189. (end-of-line arg)))))
  22190. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22191. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22192. (defun org-invisible-p ()
  22193. "Check if point is at a character currently not visible."
  22194. ;; Early versions of noutline don't have `outline-invisible-p'.
  22195. (if (fboundp 'outline-invisible-p)
  22196. (outline-invisible-p)
  22197. (get-char-property (point) 'invisible)))
  22198. (defun org-invisible-p2 ()
  22199. "Check if point is at a character currently not visible."
  22200. (save-excursion
  22201. (if (and (eolp) (not (bobp))) (backward-char 1))
  22202. ;; Early versions of noutline don't have `outline-invisible-p'.
  22203. (if (fboundp 'outline-invisible-p)
  22204. (outline-invisible-p)
  22205. (get-char-property (point) 'invisible))))
  22206. (defalias 'org-back-to-heading 'outline-back-to-heading)
  22207. (defalias 'org-on-heading-p 'outline-on-heading-p)
  22208. (defalias 'org-at-heading-p 'outline-on-heading-p)
  22209. (defun org-at-heading-or-item-p ()
  22210. (or (org-on-heading-p) (org-at-item-p)))
  22211. (defun org-on-target-p ()
  22212. (or (org-in-regexp org-radio-target-regexp)
  22213. (org-in-regexp org-target-regexp)))
  22214. (defun org-up-heading-all (arg)
  22215. "Move to the heading line of which the present line is a subheading.
  22216. This function considers both visible and invisible heading lines.
  22217. With argument, move up ARG levels."
  22218. (if (fboundp 'outline-up-heading-all)
  22219. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22220. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22221. (defun org-goto-sibling (&optional previous)
  22222. "Goto the next sibling, even if it is invisible.
  22223. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22224. when a sibling was found. When none is found, return nil and don't
  22225. move point."
  22226. (let ((fun (if previous 're-search-backward 're-search-forward))
  22227. (pos (point))
  22228. (re (concat "^" outline-regexp))
  22229. level l)
  22230. (when (condition-case nil (org-back-to-heading t) (error nil))
  22231. (setq level (funcall outline-level))
  22232. (catch 'exit
  22233. (or previous (forward-char 1))
  22234. (while (funcall fun re nil t)
  22235. (setq l (funcall outline-level))
  22236. (when (< l level) (goto-char pos) (throw 'exit nil))
  22237. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22238. (goto-char pos)
  22239. nil))))
  22240. (defun org-show-siblings ()
  22241. "Show all siblings of the current headline."
  22242. (save-excursion
  22243. (while (org-goto-sibling) (org-flag-heading nil)))
  22244. (save-excursion
  22245. (while (org-goto-sibling 'previous)
  22246. (org-flag-heading nil))))
  22247. (defun org-show-hidden-entry ()
  22248. "Show an entry where even the heading is hidden."
  22249. (save-excursion
  22250. (org-show-entry)))
  22251. (defun org-flag-heading (flag &optional entry)
  22252. "Flag the current heading. FLAG non-nil means make invisible.
  22253. When ENTRY is non-nil, show the entire entry."
  22254. (save-excursion
  22255. (org-back-to-heading t)
  22256. ;; Check if we should show the entire entry
  22257. (if entry
  22258. (progn
  22259. (org-show-entry)
  22260. (save-excursion
  22261. (and (outline-next-heading)
  22262. (org-flag-heading nil))))
  22263. (outline-flag-region (max (point-min) (1- (point)))
  22264. (save-excursion (outline-end-of-heading) (point))
  22265. flag))))
  22266. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  22267. ;; This is an exact copy of the original function, but it uses
  22268. ;; `org-back-to-heading', to make it work also in invisible
  22269. ;; trees. And is uses an invisible-OK argument.
  22270. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22271. (org-back-to-heading invisible-OK)
  22272. (let ((first t)
  22273. (level (funcall outline-level)))
  22274. (while (and (not (eobp))
  22275. (or first (> (funcall outline-level) level)))
  22276. (setq first nil)
  22277. (outline-next-heading))
  22278. (unless to-heading
  22279. (if (memq (preceding-char) '(?\n ?\^M))
  22280. (progn
  22281. ;; Go to end of line before heading
  22282. (forward-char -1)
  22283. (if (memq (preceding-char) '(?\n ?\^M))
  22284. ;; leave blank line before heading
  22285. (forward-char -1))))))
  22286. (point))
  22287. (defun org-show-subtree ()
  22288. "Show everything after this heading at deeper levels."
  22289. (outline-flag-region
  22290. (point)
  22291. (save-excursion
  22292. (outline-end-of-subtree) (outline-next-heading) (point))
  22293. nil))
  22294. (defun org-show-entry ()
  22295. "Show the body directly following this heading.
  22296. Show the heading too, if it is currently invisible."
  22297. (interactive)
  22298. (save-excursion
  22299. (org-back-to-heading t)
  22300. (outline-flag-region
  22301. (max (point-min) (1- (point)))
  22302. (save-excursion
  22303. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  22304. (or (match-beginning 1) (point-max)))
  22305. nil)))
  22306. (defun org-make-options-regexp (kwds)
  22307. "Make a regular expression for keyword lines."
  22308. (concat
  22309. "^"
  22310. "#?[ \t]*\\+\\("
  22311. (mapconcat 'regexp-quote kwds "\\|")
  22312. "\\):[ \t]*"
  22313. "\\(.+\\)"))
  22314. ;; Make isearch reveal the necessary context
  22315. (defun org-isearch-end ()
  22316. "Reveal context after isearch exits."
  22317. (when isearch-success ; only if search was successful
  22318. (if (featurep 'xemacs)
  22319. ;; Under XEmacs, the hook is run in the correct place,
  22320. ;; we directly show the context.
  22321. (org-show-context 'isearch)
  22322. ;; In Emacs the hook runs *before* restoring the overlays.
  22323. ;; So we have to use a one-time post-command-hook to do this.
  22324. ;; (Emacs 22 has a special variable, see function `org-mode')
  22325. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22326. isearch-mode-end-hook-quit)
  22327. ;; Only when the isearch was not quitted.
  22328. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22329. 'append 'local)))))
  22330. (defun org-isearch-post-command ()
  22331. "Remove self from hook, and show context."
  22332. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22333. (org-show-context 'isearch))
  22334. ;;;; Address problems with some other packages
  22335. ;; Make flyspell not check words in links, to not mess up our keymap
  22336. (defun org-mode-flyspell-verify ()
  22337. "Don't let flyspell put overlays at active buttons."
  22338. (not (get-text-property (point) 'keymap)))
  22339. ;; Make `bookmark-jump' show the jump location if it was hidden.
  22340. (eval-after-load "bookmark"
  22341. '(if (boundp 'bookmark-after-jump-hook)
  22342. ;; We can use the hook
  22343. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22344. ;; Hook not available, use advice
  22345. (defadvice bookmark-jump (after org-make-visible activate)
  22346. "Make the position visible."
  22347. (org-bookmark-jump-unhide))))
  22348. (defun org-bookmark-jump-unhide ()
  22349. "Unhide the current position, to show the bookmark location."
  22350. (and (org-mode-p)
  22351. (or (org-invisible-p)
  22352. (save-excursion (goto-char (max (point-min) (1- (point))))
  22353. (org-invisible-p)))
  22354. (org-show-context 'bookmark-jump)))
  22355. ;; Make session.el ignore our circular variable
  22356. (eval-after-load "session"
  22357. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22358. ;;;; Experimental code
  22359. ;; Make appt aware of appointments from the agenda
  22360. (defun org-agenda-to-appt ()
  22361. "Activate appointments found in `org-agenda-files'."
  22362. (interactive)
  22363. (require 'org)
  22364. (let* ((today (org-date-to-gregorian
  22365. (time-to-days (current-time))))
  22366. (files org-agenda-files) entries file)
  22367. (while (setq file (pop files))
  22368. (setq entries (append entries (org-agenda-get-day-entries
  22369. file today :timestamp))))
  22370. (setq entries (delq nil entries))
  22371. (mapc (lambda(x)
  22372. (let* ((event (org-trim (get-text-property 1 'txt x)))
  22373. (time-of-day (get-text-property 1 'time-of-day x)) tod)
  22374. (when time-of-day
  22375. (setq tod (number-to-string time-of-day)
  22376. tod (when (string-match
  22377. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  22378. (concat (match-string 1 tod) ":"
  22379. (match-string 2 tod))))
  22380. (if tod (appt-add tod event))))) entries)))
  22381. (defun org-closed-in-range ()
  22382. "Sparse tree of items closed in a certain time range.
  22383. Still experimental, may disappear in the furture."
  22384. (interactive)
  22385. ;; Get the time interval from the user.
  22386. (let* ((time1 (time-to-seconds
  22387. (org-read-date nil 'to-time nil "Starting date: ")))
  22388. (time2 (time-to-seconds
  22389. (org-read-date nil 'to-time nil "End date:")))
  22390. ;; callback function
  22391. (callback (lambda ()
  22392. (let ((time
  22393. (time-to-seconds
  22394. (apply 'encode-time
  22395. (org-parse-time-string
  22396. (match-string 1))))))
  22397. ;; check if time in interval
  22398. (and (>= time time1) (<= time time2))))))
  22399. ;; make tree, check each match with the callback
  22400. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  22401. (defun org-fill-paragraph-experimental (&optional justify)
  22402. "Re-align a table, pass through to fill-paragraph if no table."
  22403. (let ((table-p (org-at-table-p))
  22404. (table.el-p (org-at-table.el-p)))
  22405. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  22406. (table.el-p t) ; skip table.el tables
  22407. (table-p (org-table-align) t) ; align org-mode tables
  22408. ((save-excursion
  22409. (let ((pos (1+ (point-at-eol))))
  22410. (backward-paragraph 1)
  22411. (re-search-forward "\\\\\\\\[ \t]*$" pos t)))
  22412. (save-excursion
  22413. (save-restriction
  22414. (narrow-to-region (1+ (match-end 0)) (point-max))
  22415. (fill-paragraph nil)
  22416. t)))
  22417. (t nil)))) ; call paragraph-fill
  22418. ;;;; Finish up
  22419. (provide 'org)
  22420. (run-hooks 'org-load-hook)
  22421. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  22422. ;;; org.el ends here