org.el 850 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112
  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.01
  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.01"
  80. "The version number of the file org.el.")
  81. (defun org-version ()
  82. (interactive)
  83. (message "Org-mode version %s" org-version))
  84. ;;; Compatibility constants
  85. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  86. (defconst org-format-transports-properties-p
  87. (let ((x "a"))
  88. (add-text-properties 0 1 '(test t) x)
  89. (get-text-property 0 'test (format "%s" x)))
  90. "Does format transport text properties?")
  91. (defmacro org-unmodified (&rest body)
  92. "Execute body without changing buffer-modified-p."
  93. `(set-buffer-modified-p
  94. (prog1 (buffer-modified-p) ,@body)))
  95. (defmacro org-re (s)
  96. "Replace posix classes in regular expression."
  97. (if (featurep 'xemacs)
  98. (let ((ss s))
  99. (save-match-data
  100. (while (string-match "\\[:alnum:\\]" ss)
  101. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  102. ss))
  103. s))
  104. (defmacro org-preserve-lc (&rest body)
  105. `(let ((_line (org-current-line))
  106. (_col (current-column)))
  107. (unwind-protect
  108. (progn ,@body)
  109. (goto-line _line)
  110. (move-to-column _col))))
  111. ;;; The custom variables
  112. (defgroup org nil
  113. "Outline-based notes management and organizer."
  114. :tag "Org"
  115. :group 'outlines
  116. :group 'hypermedia
  117. :group 'calendar)
  118. (defgroup org-startup nil
  119. "Options concerning startup of Org-mode."
  120. :tag "Org Startup"
  121. :group 'org)
  122. (defcustom org-startup-folded t
  123. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  124. This can also be configured on a per-file basis by adding one of
  125. the following lines anywhere in the buffer:
  126. #+STARTUP: fold
  127. #+STARTUP: nofold
  128. #+STARTUP: content"
  129. :group 'org-startup
  130. :type '(choice
  131. (const :tag "nofold: show all" nil)
  132. (const :tag "fold: overview" t)
  133. (const :tag "content: all headlines" content)))
  134. (defcustom org-startup-truncated t
  135. "Non-nil means, entering Org-mode will set `truncate-lines'.
  136. This is useful since some lines containing links can be very long and
  137. uninteresting. Also tables look terrible when wrapped."
  138. :group 'org-startup
  139. :type 'boolean)
  140. (defcustom org-startup-align-all-tables nil
  141. "Non-nil means, align all tables when visiting a file.
  142. This is useful when the column width in tables is forced with <N> cookies
  143. in table fields. Such tables will look correct only after the first re-align.
  144. This can also be configured on a per-file basis by adding one of
  145. the following lines anywhere in the buffer:
  146. #+STARTUP: align
  147. #+STARTUP: noalign"
  148. :group 'org-startup
  149. :type 'boolean)
  150. (defcustom org-insert-mode-line-in-empty-file nil
  151. "Non-nil means insert the first line setting Org-mode in empty files.
  152. When the function `org-mode' is called interactively in an empty file, this
  153. normally means that the file name does not automatically trigger Org-mode.
  154. To ensure that the file will always be in Org-mode in the future, a
  155. line enforcing Org-mode will be inserted into the buffer, if this option
  156. has been set."
  157. :group 'org-startup
  158. :type 'boolean)
  159. (defcustom org-replace-disputed-keys nil
  160. "Non-nil means use alternative key bindings for some keys.
  161. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  162. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  163. If you want to use Org-mode together with one of these other modes,
  164. or more generally if you would like to move some Org-mode commands to
  165. other keys, set this variable and configure the keys with the variable
  166. `org-disputed-keys'.
  167. This option is only relevant at load-time of Org-mode, and must be set
  168. *before* org.el is loaded. Changing it requires a restart of Emacs to
  169. become effective."
  170. :group 'org-startup
  171. :type 'boolean)
  172. (if (fboundp 'defvaralias)
  173. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  174. (defcustom org-disputed-keys
  175. '(([(shift up)] . [(meta p)])
  176. ([(shift down)] . [(meta n)])
  177. ([(shift left)] . [(meta -)])
  178. ([(shift right)] . [(meta +)])
  179. ([(control shift right)] . [(meta shift +)])
  180. ([(control shift left)] . [(meta shift -)]))
  181. "Keys for which Org-mode and other modes compete.
  182. This is an alist, cars are the default keys, second element specifies
  183. the alternative to use when `org-replace-disputed-keys' is t.
  184. Keys can be specified in any syntax supported by `define-key'.
  185. The value of this option takes effect only at Org-mode's startup,
  186. therefore you'll have to restart Emacs to apply it after changing."
  187. :group 'org-startup
  188. :type 'alist)
  189. (defun org-key (key)
  190. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  191. Or return the original if not disputed."
  192. (if org-replace-disputed-keys
  193. (let* ((nkey (key-description key))
  194. (x (org-find-if (lambda (x)
  195. (equal (key-description (car x)) nkey))
  196. org-disputed-keys)))
  197. (if x (cdr x) key))
  198. key))
  199. (defun org-find-if (predicate seq)
  200. (catch 'exit
  201. (while seq
  202. (if (funcall predicate (car seq))
  203. (throw 'exit (car seq))
  204. (pop seq)))))
  205. (defun org-defkey (keymap key def)
  206. "Define a key, possibly translated, as returned by `org-key'."
  207. (define-key keymap (org-key key) def))
  208. (defcustom org-ellipsis nil
  209. "The ellipsis to use in the Org-mode outline.
  210. When nil, just use the standard three dots. When a string, use that instead,
  211. and just in Org-mode (which will then use its own display table).
  212. Changing this requires executing `M-x org-mode' in a buffer to become
  213. effective."
  214. :group 'org-startup
  215. :type '(choice (const :tag "Default" nil)
  216. (string :tag "String" :value "...#")))
  217. (defvar org-display-table nil
  218. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  219. (defgroup org-keywords nil
  220. "Keywords in Org-mode."
  221. :tag "Org Keywords"
  222. :group 'org)
  223. (defcustom org-deadline-string "DEADLINE:"
  224. "String to mark deadline entries.
  225. A deadline is this string, followed by a time stamp. Should be a word,
  226. terminated by a colon. You can insert a schedule keyword and
  227. a timestamp with \\[org-deadline].
  228. Changes become only effective after restarting Emacs."
  229. :group 'org-keywords
  230. :type 'string)
  231. (defcustom org-scheduled-string "SCHEDULED:"
  232. "String to mark scheduled TODO entries.
  233. A schedule is this string, followed by a time stamp. Should be a word,
  234. terminated by a colon. You can insert a schedule keyword and
  235. a timestamp with \\[org-schedule].
  236. Changes become only effective after restarting Emacs."
  237. :group 'org-keywords
  238. :type 'string)
  239. (defcustom org-closed-string "CLOSED:"
  240. "String used as the prefix for timestamps logging closing a TODO entry."
  241. :group 'org-keywords
  242. :type 'string)
  243. (defcustom org-archived-string "ARCHIVED:"
  244. "String used as the prefix for timestamps logging archiving a TODO entry."
  245. :group 'org-keywords
  246. :type 'string)
  247. (defcustom org-clock-string "CLOCK:"
  248. "String used as prefix for timestamps clocking work hours on an item."
  249. :group 'org-keywords
  250. :type 'string)
  251. (defcustom org-comment-string "COMMENT"
  252. "Entries starting with this keyword will never be exported.
  253. An entry can be toggled between COMMENT and normal with
  254. \\[org-toggle-comment].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-quote-string "QUOTE"
  259. "Entries starting with this keyword will be exported in fixed-width font.
  260. Quoting applies only to the text in the entry following the headline, and does
  261. not extend beyond the next headline, even if that is lower level.
  262. An entry can be toggled between QUOTE and normal with
  263. \\[org-toggle-fixed-width-section]."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defconst org-repeat-re
  267. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  268. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  269. "Regular expression for specifying repeated events.
  270. After a match, group 1 contains the repeat expression.")
  271. (defgroup org-structure nil
  272. "Options concerning the general structure of Org-mode files."
  273. :tag "Org Structure"
  274. :group 'org)
  275. (defgroup org-reveal-location nil
  276. "Options about how to make context of a location visible."
  277. :tag "Org Reveal Location"
  278. :group 'org-structure)
  279. (defcustom org-show-hierarchy-above '((default . t))
  280. "Non-nil means, show full hierarchy when revealing a location.
  281. Org-mode often shows locations in an org-mode file which might have
  282. been invisible before. When this is set, the hierarchy of headings
  283. above the exposed location is shown.
  284. Turning this off for example for sparse trees makes them very compact.
  285. Instead of t, this can also be an alist specifying this option for different
  286. contexts. Valid contexts are
  287. agenda when exposing an entry from the agenda
  288. org-goto when using the command `org-goto' on key C-c C-j
  289. occur-tree when using the command `org-occur' on key C-c /
  290. tags-tree when constructing a sparse tree based on tags matches
  291. link-search when exposing search matches associated with a link
  292. mark-goto when exposing the jump goal of a mark
  293. bookmark-jump when exposing a bookmark location
  294. isearch when exiting from an incremental search
  295. default default for all contexts not set explicitly"
  296. :group 'org-reveal-location
  297. :type '(choice
  298. (const :tag "Always" t)
  299. (const :tag "Never" nil)
  300. (repeat :greedy t :tag "Individual contexts"
  301. (cons
  302. (choice :tag "Context"
  303. (const agenda)
  304. (const org-goto)
  305. (const occur-tree)
  306. (const tags-tree)
  307. (const link-search)
  308. (const mark-goto)
  309. (const bookmark-jump)
  310. (const isearch)
  311. (const default))
  312. (boolean)))))
  313. (defcustom org-show-following-heading '((default . nil))
  314. "Non-nil means, show following heading when revealing a location.
  315. Org-mode often shows locations in an org-mode file which might have
  316. been invisible before. When this is set, the heading following the
  317. match is shown.
  318. Turning this off for example for sparse trees makes them very compact,
  319. but makes it harder to edit the location of the match. In such a case,
  320. use the command \\[org-reveal] to show more context.
  321. Instead of t, this can also be an alist specifying this option for different
  322. contexts. See `org-show-hierarchy-above' for valid contexts."
  323. :group 'org-reveal-location
  324. :type '(choice
  325. (const :tag "Always" t)
  326. (const :tag "Never" nil)
  327. (repeat :greedy t :tag "Individual contexts"
  328. (cons
  329. (choice :tag "Context"
  330. (const agenda)
  331. (const org-goto)
  332. (const occur-tree)
  333. (const tags-tree)
  334. (const link-search)
  335. (const mark-goto)
  336. (const bookmark-jump)
  337. (const isearch)
  338. (const default))
  339. (boolean)))))
  340. (defcustom org-show-siblings '((default . nil) (isearch t))
  341. "Non-nil means, show all sibling heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the sibling of the current entry
  344. heading are all made visible. If `org-show-hierarchy-above' is t,
  345. the same happens on each level of the hierarchy above the current entry.
  346. By default this is on for the isearch context, off for all other contexts.
  347. Turning this off for example for sparse trees makes them very compact,
  348. but makes it harder to edit the location of the match. In such a case,
  349. use the command \\[org-reveal] to show more context.
  350. Instead of t, this can also be an alist specifying this option for different
  351. contexts. See `org-show-hierarchy-above' for valid contexts."
  352. :group 'org-reveal-location
  353. :type '(choice
  354. (const :tag "Always" t)
  355. (const :tag "Never" nil)
  356. (repeat :greedy t :tag "Individual contexts"
  357. (cons
  358. (choice :tag "Context"
  359. (const agenda)
  360. (const org-goto)
  361. (const occur-tree)
  362. (const tags-tree)
  363. (const link-search)
  364. (const mark-goto)
  365. (const bookmark-jump)
  366. (const isearch)
  367. (const default))
  368. (boolean)))))
  369. (defgroup org-cycle nil
  370. "Options concerning visibility cycling in Org-mode."
  371. :tag "Org Cycle"
  372. :group 'org-structure)
  373. (defcustom org-drawers '("PROPERTIES")
  374. "Names of drawers. Drawers are not opened by cycling on the headline above.
  375. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  376. this:
  377. :DRAWERNAME:
  378. .....
  379. :END:
  380. The drawer \"PROPERTIES\" is special for capturing properties through
  381. the property API."
  382. :group 'org-structure
  383. :type '(repeat (string :tag "Drawer Name")))
  384. (defcustom org-cycle-global-at-bob t
  385. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  386. This makes it possible to do global cycling without having to use S-TAB or
  387. C-u TAB. For this special case to work, the first line of the buffer
  388. must not be a headline - it may be empty ot some other text. When used in
  389. this way, `org-cycle-hook' is disables temporarily, to make sure the
  390. cursor stays at the beginning of the buffer.
  391. When this option is nil, don't do anything special at the beginning
  392. of the buffer."
  393. :group 'org-cycle
  394. :type 'boolean)
  395. (defcustom org-cycle-emulate-tab t
  396. "Where should `org-cycle' emulate TAB.
  397. nil Never
  398. white Only in completely white lines
  399. whitestart Only at the beginning of lines, before the first non-white char.
  400. t Everywhere except in headlines
  401. exc-hl-bol Everywhere except at the start of a headline
  402. If TAB is used in a place where it does not emulate TAB, the current subtree
  403. visibility is cycled."
  404. :group 'org-cycle
  405. :type '(choice (const :tag "Never" nil)
  406. (const :tag "Only in completely white lines" white)
  407. (const :tag "Before first char in a line" whitestart)
  408. (const :tag "Everywhere except in headlines" t)
  409. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  410. ))
  411. (defcustom org-cycle-separator-lines 2
  412. "Number of empty lines needed to keep an empty line between collapsed trees.
  413. If you leave an empty line between the end of a subtree and the following
  414. headline, this empty line is hidden when the subtree is folded.
  415. Org-mode will leave (exactly) one empty line visible if the number of
  416. empty lines is equal or larger to the number given in this variable.
  417. So the default 2 means, at least 2 empty lines after the end of a subtree
  418. are needed to produce free space between a collapsed subtree and the
  419. following headline.
  420. Special case: when 0, never leave empty lines in collapsed view."
  421. :group 'org-cycle
  422. :type 'integer)
  423. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  424. org-cycle-hide-drawers
  425. org-cycle-show-empty-lines
  426. org-optimize-window-after-visibility-change)
  427. "Hook that is run after `org-cycle' has changed the buffer visibility.
  428. The function(s) in this hook must accept a single argument which indicates
  429. the new state that was set by the most recent `org-cycle' command. The
  430. argument is a symbol. After a global state change, it can have the values
  431. `overview', `content', or `all'. After a local state change, it can have
  432. the values `folded', `children', or `subtree'."
  433. :group 'org-cycle
  434. :type 'hook)
  435. (defgroup org-edit-structure nil
  436. "Options concerning structure editing in Org-mode."
  437. :tag "Org Edit Structure"
  438. :group 'org-structure)
  439. (defcustom org-special-ctrl-a nil
  440. "Non-nil means `C-a' behaves specially in headlines.
  441. When set, `C-a' will bring back the cursor to the beginning of the
  442. headline text, i.e. after the stars and after a possible TODO keyword.
  443. When the cursor is already at that position, another `C-a' will bring
  444. it to the beginning of the line."
  445. :group 'org-edit-structure
  446. :type 'boolean)
  447. (defcustom org-odd-levels-only nil
  448. "Non-nil means, skip even levels and only use odd levels for the outline.
  449. This has the effect that two stars are being added/taken away in
  450. promotion/demotion commands. It also influences how levels are
  451. handled by the exporters.
  452. Changing it requires restart of `font-lock-mode' to become effective
  453. for fontification also in regions already fontified.
  454. You may also set this on a per-file basis by adding one of the following
  455. lines to the buffer:
  456. #+STARTUP: odd
  457. #+STARTUP: oddeven"
  458. :group 'org-edit-structure
  459. :group 'org-font-lock
  460. :type 'boolean)
  461. (defcustom org-adapt-indentation t
  462. "Non-nil means, adapt indentation when promoting and demoting.
  463. When this is set and the *entire* text in an entry is indented, the
  464. indentation is increased by one space in a demotion command, and
  465. decreased by one in a promotion command. If any line in the entry
  466. body starts at column 0, indentation is not changed at all."
  467. :group 'org-edit-structure
  468. :type 'boolean)
  469. (defcustom org-blank-before-new-entry '((heading . nil)
  470. (plain-list-item . nil))
  471. "Should `org-insert-heading' leave a blank line before new heading/item?
  472. The value is an alist, with `heading' and `plain-list-item' as car,
  473. and a boolean flag as cdr."
  474. :group 'org-edit-structure
  475. :type '(list
  476. (cons (const heading) (boolean))
  477. (cons (const plain-list-item) (boolean))))
  478. (defcustom org-insert-heading-hook nil
  479. "Hook being run after inserting a new heading."
  480. :group 'org-edit-structure
  481. :type 'boolean)
  482. (defcustom org-enable-fixed-width-editor t
  483. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  484. This currently only means, they are never auto-wrapped.
  485. When nil, such lines will be treated like ordinary lines.
  486. See also the QUOTE keyword."
  487. :group 'org-edit-structure
  488. :type 'boolean)
  489. (defgroup org-sparse-trees nil
  490. "Options concerning sparse trees in Org-mode."
  491. :tag "Org Sparse Trees"
  492. :group 'org-structure)
  493. (defcustom org-highlight-sparse-tree-matches t
  494. "Non-nil means, highlight all matches that define a sparse tree.
  495. The highlights will automatically disappear the next time the buffer is
  496. changed by an edit command."
  497. :group 'org-sparse-trees
  498. :type 'boolean)
  499. (defcustom org-remove-highlights-with-change t
  500. "Non-nil means, any change to the buffer will remove temporary highlights.
  501. Such highlights are created by `org-occur' and `org-clock-display'.
  502. When nil, `C-c C-c needs to be used to get rid of the highlights.
  503. The highlights created by `org-preview-latex-fragment' always need
  504. `C-c C-c' to be removed."
  505. :group 'org-sparse-trees
  506. :group 'org-time
  507. :type 'boolean)
  508. (defcustom org-occur-hook '(org-first-headline-recenter)
  509. "Hook that is run after `org-occur' has constructed a sparse tree.
  510. This can be used to recenter the window to show as much of the structure
  511. as possible."
  512. :group 'org-sparse-trees
  513. :type 'hook)
  514. (defgroup org-plain-lists nil
  515. "Options concerning plain lists in Org-mode."
  516. :tag "Org Plain lists"
  517. :group 'org-structure)
  518. (defcustom org-cycle-include-plain-lists nil
  519. "Non-nil means, include plain lists into visibility cycling.
  520. This means that during cycling, plain list items will *temporarily* be
  521. interpreted as outline headlines with a level given by 1000+i where i is the
  522. indentation of the bullet. In all other operations, plain list items are
  523. not seen as headlines. For example, you cannot assign a TODO keyword to
  524. such an item."
  525. :group 'org-plain-lists
  526. :type 'boolean)
  527. (defcustom org-plain-list-ordered-item-terminator t
  528. "The character that makes a line with leading number an ordered list item.
  529. Valid values are ?. and ?\). To get both terminators, use t. While
  530. ?. may look nicer, it creates the danger that a line with leading
  531. number may be incorrectly interpreted as an item. ?\) therefore is
  532. the safe choice."
  533. :group 'org-plain-lists
  534. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  535. (const :tag "paren like in \"2)\"" ?\))
  536. (const :tab "both" t)))
  537. (defcustom org-auto-renumber-ordered-lists t
  538. "Non-nil means, automatically renumber ordered plain lists.
  539. Renumbering happens when the sequence have been changed with
  540. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  541. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  542. :group 'org-plain-lists
  543. :type 'boolean)
  544. (defcustom org-provide-checkbox-statistics t
  545. "Non-nil means, update checkbox statistics after insert and toggle.
  546. When this is set, checkbox statistics is updated each time you either insert
  547. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  548. with \\[org-ctrl-c-ctrl-c\\]."
  549. :group 'org-plain-lists
  550. :type 'boolean)
  551. (defgroup org-archive nil
  552. "Options concerning archiving in Org-mode."
  553. :tag "Org Archive"
  554. :group 'org-structure)
  555. (defcustom org-archive-tag "ARCHIVE"
  556. "The tag that marks a subtree as archived.
  557. An archived subtree does not open during visibility cycling, and does
  558. not contribute to the agenda listings."
  559. :group 'org-archive
  560. :group 'org-keywords
  561. :type 'string)
  562. (defcustom org-agenda-skip-archived-trees t
  563. "Non-nil means, the agenda will skip any items located in archived trees.
  564. An archived tree is a tree marked with the tag ARCHIVE."
  565. :group 'org-archive
  566. :group 'org-agenda-skip
  567. :type 'boolean)
  568. (defcustom org-cycle-open-archived-trees nil
  569. "Non-nil means, `org-cycle' will open archived trees.
  570. An archived tree is a tree marked with the tag ARCHIVE.
  571. When nil, archived trees will stay folded. You can still open them with
  572. normal outline commands like `show-all', but not with the cycling commands."
  573. :group 'org-archive
  574. :group 'org-cycle
  575. :type 'boolean)
  576. (defcustom org-sparse-tree-open-archived-trees nil
  577. "Non-nil means sparse tree construction shows matches in archived trees.
  578. When nil, matches in these trees are highlighted, but the trees are kept in
  579. collapsed state."
  580. :group 'org-archive
  581. :group 'org-sparse-trees
  582. :type 'boolean)
  583. (defcustom org-archive-location "%s_archive::"
  584. "The location where subtrees should be archived.
  585. This string consists of two parts, separated by a double-colon.
  586. The first part is a file name - when omitted, archiving happens in the same
  587. file. %s will be replaced by the current file name (without directory part).
  588. Archiving to a different file is useful to keep archived entries from
  589. contributing to the Org-mode Agenda.
  590. The part after the double colon is a headline. The archived entries will be
  591. filed under that headline. When omitted, the subtrees are simply filed away
  592. at the end of the file, as top-level entries.
  593. Here are a few examples:
  594. \"%s_archive::\"
  595. If the current file is Projects.org, archive in file
  596. Projects.org_archive, as top-level trees. This is the default.
  597. \"::* Archived Tasks\"
  598. Archive in the current file, under the top-level headline
  599. \"* Archived Tasks\".
  600. \"~/org/archive.org::\"
  601. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  602. \"basement::** Finished Tasks\"
  603. Archive in file ./basement (relative path), as level 3 trees
  604. below the level 2 heading \"** Finished Tasks\".
  605. You may set this option on a per-file basis by adding to the buffer a
  606. line like
  607. #+ARCHIVE: basement::** Finished Tasks"
  608. :group 'org-archive
  609. :type 'string)
  610. (defcustom org-archive-mark-done t
  611. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  612. :group 'org-archive
  613. :type 'boolean)
  614. (defcustom org-archive-stamp-time t
  615. "Non-nil means, add a time stamp to entries moved to an archive file."
  616. :group 'org-archive
  617. :type 'boolean)
  618. (defgroup org-table nil
  619. "Options concerning tables in Org-mode."
  620. :tag "Org Table"
  621. :group 'org)
  622. (defcustom org-enable-table-editor 'optimized
  623. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  624. When nil, such lines will be treated like ordinary lines.
  625. When equal to the symbol `optimized', the table editor will be optimized to
  626. do the following:
  627. - Automatic overwrite mode in front of whitespace in table fields.
  628. This makes the structure of the table stay in tact as long as the edited
  629. field does not exceed the column width.
  630. - Minimize the number of realigns. Normally, the table is aligned each time
  631. TAB or RET are pressed to move to another field. With optimization this
  632. happens only if changes to a field might have changed the column width.
  633. Optimization requires replacing the functions `self-insert-command',
  634. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  635. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  636. very good at guessing when a re-align will be necessary, but you can always
  637. force one with \\[org-ctrl-c-ctrl-c].
  638. If you would like to use the optimized version in Org-mode, but the
  639. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  640. This variable can be used to turn on and off the table editor during a session,
  641. but in order to toggle optimization, a restart is required.
  642. See also the variable `org-table-auto-blank-field'."
  643. :group 'org-table
  644. :type '(choice
  645. (const :tag "off" nil)
  646. (const :tag "on" t)
  647. (const :tag "on, optimized" optimized)))
  648. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  649. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  650. In the optimized version, the table editor takes over all simple keys that
  651. normally just insert a character. In tables, the characters are inserted
  652. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  653. for empty fields). Outside tables, the correct binding of the keys is
  654. restored.
  655. The default for this option is t if the optimized version is also used in
  656. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  657. this variable requires a restart of Emacs to become effective."
  658. :group 'org-table
  659. :type 'boolean)
  660. (defcustom orgtbl-radio-table-templates
  661. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  662. % END RECEIVE ORGTBL %n
  663. \\begin{comment}
  664. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  665. | | |
  666. \\end{comment}\n")
  667. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  668. @c END RECEIVE ORGTBL %n
  669. @ignore
  670. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  671. | | |
  672. @end ignore\n")
  673. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  674. <!-- END RECEIVE ORGTBL %n -->
  675. <!--
  676. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  677. | | |
  678. -->\n"))
  679. "Templates for radio tables in different major modes.
  680. All occurrences of %n in a template will be replaced with the name of the
  681. table, obtained by prompting the user."
  682. :group 'org-table
  683. :type '(repeat
  684. (list (symbol :tag "Major mode")
  685. (string :tag "Format"))))
  686. (defgroup org-table-settings nil
  687. "Settings for tables in Org-mode."
  688. :tag "Org Table Settings"
  689. :group 'org-table)
  690. (defcustom org-table-default-size "5x2"
  691. "The default size for newly created tables, Columns x Rows."
  692. :group 'org-table-settings
  693. :type 'string)
  694. (defcustom org-table-number-regexp
  695. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\)$"
  696. "Regular expression for recognizing numbers in table columns.
  697. If a table column contains mostly numbers, it will be aligned to the
  698. right. If not, it will be aligned to the left.
  699. The default value of this option is a regular expression which allows
  700. anything which looks remotely like a number as used in scientific
  701. context. For example, all of the following will be considered a
  702. number:
  703. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  704. Other options offered by the customize interface are more restrictive."
  705. :group 'org-table-settings
  706. :type '(choice
  707. (const :tag "Positive Integers"
  708. "^[0-9]+$")
  709. (const :tag "Integers"
  710. "^[-+]?[0-9]+$")
  711. (const :tag "Floating Point Numbers"
  712. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  713. (const :tag "Floating Point Number or Integer"
  714. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  715. (const :tag "Exponential, Floating point, Integer"
  716. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  717. (const :tag "Very General Number-Like, including hex"
  718. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\)$")
  719. (string :tag "Regexp:")))
  720. (defcustom org-table-number-fraction 0.5
  721. "Fraction of numbers in a column required to make the column align right.
  722. In a column all non-white fields are considered. If at least this
  723. fraction of fields is matched by `org-table-number-fraction',
  724. alignment to the right border applies."
  725. :group 'org-table-settings
  726. :type 'number)
  727. (defgroup org-table-editing nil
  728. "Bahavior of tables during editing in Org-mode."
  729. :tag "Org Table Editing"
  730. :group 'org-table)
  731. (defcustom org-table-automatic-realign t
  732. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  733. When nil, aligning is only done with \\[org-table-align], or after column
  734. removal/insertion."
  735. :group 'org-table-editing
  736. :type 'boolean)
  737. (defcustom org-table-auto-blank-field t
  738. "Non-nil means, automatically blank table field when starting to type into it.
  739. This only happens when typing immediately after a field motion
  740. command (TAB, S-TAB or RET).
  741. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  742. :group 'org-table-editing
  743. :type 'boolean)
  744. (defcustom org-table-tab-jumps-over-hlines t
  745. "Non-nil means, tab in the last column of a table with jump over a hline.
  746. If a horizontal separator line is following the current line,
  747. `org-table-next-field' can either create a new row before that line, or jump
  748. over the line. When this option is nil, a new line will be created before
  749. this line."
  750. :group 'org-table-editing
  751. :type 'boolean)
  752. (defcustom org-table-tab-recognizes-table.el t
  753. "Non-nil means, TAB will automatically notice a table.el table.
  754. When it sees such a table, it moves point into it and - if necessary -
  755. calls `table-recognize-table'."
  756. :group 'org-table-editing
  757. :type 'boolean)
  758. (defgroup org-table-calculation nil
  759. "Options concerning tables in Org-mode."
  760. :tag "Org Table Calculation"
  761. :group 'org-table)
  762. (defcustom org-table-use-standard-references t
  763. "Should org-mode work with table refrences like B3 instead of @3$2?
  764. Possible values are:
  765. nil never use them
  766. from accept as input, do not present for editing
  767. t: accept as input and present for editing"
  768. :group 'org-table-calculation
  769. :type '(choice
  770. (const :tag "Never, don't even check unser input for them" nil)
  771. (const :tag "Always, both as user input, and when editing" t)
  772. (const :tag "Convert user input, don't offer during editing" 'from)))
  773. (defcustom org-table-copy-increment t
  774. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  775. :group 'org-table-calculation
  776. :type 'boolean)
  777. (defcustom org-calc-default-modes
  778. '(calc-internal-prec 12
  779. calc-float-format (float 5)
  780. calc-angle-mode deg
  781. calc-prefer-frac nil
  782. calc-symbolic-mode nil
  783. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  784. calc-display-working-message t
  785. )
  786. "List with Calc mode settings for use in calc-eval for table formulas.
  787. The list must contain alternating symbols (Calc modes variables and values).
  788. Don't remove any of the default settings, just change the values. Org-mode
  789. relies on the variables to be present in the list."
  790. :group 'org-table-calculation
  791. :type 'plist)
  792. (defcustom org-table-formula-evaluate-inline t
  793. "Non-nil means, TAB and RET evaluate a formula in current table field.
  794. If the current field starts with an equal sign, it is assumed to be a formula
  795. which should be evaluated as described in the manual and in the documentation
  796. string of the command `org-table-eval-formula'. This feature requires the
  797. Emacs calc package.
  798. When this variable is nil, formula calculation is only available through
  799. the command \\[org-table-eval-formula]."
  800. :group 'org-table-calculation
  801. :type 'boolean)
  802. (defcustom org-table-formula-use-constants t
  803. "Non-nil means, interpret constants in formulas in tables.
  804. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  805. by the value given in `org-table-formula-constants', or by a value obtained
  806. from the `constants.el' package."
  807. :group 'org-table-calculation
  808. :type 'boolean)
  809. (defcustom org-table-formula-constants nil
  810. "Alist with constant names and values, for use in table formulas.
  811. The car of each element is a name of a constant, without the `$' before it.
  812. The cdr is the value as a string. For example, if you'd like to use the
  813. speed of light in a formula, you would configure
  814. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  815. and then use it in an equation like `$1*$c'.
  816. Constants can also be defined on a per-file basis using a line like
  817. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  818. :group 'org-table-calculation
  819. :type '(repeat
  820. (cons (string :tag "name")
  821. (string :tag "value"))))
  822. (defvar org-table-formula-constants-local nil
  823. "Local version of `org-table-formula-constants'.")
  824. (make-variable-buffer-local 'org-table-formula-constants-local)
  825. (defcustom org-table-allow-automatic-line-recalculation t
  826. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  827. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  828. :group 'org-table-calculation
  829. :type 'boolean)
  830. (defgroup org-link nil
  831. "Options concerning links in Org-mode."
  832. :tag "Org Link"
  833. :group 'org)
  834. (defvar org-link-abbrev-alist-local nil
  835. "Buffer-local version of `org-link-abbrev-alist', which see.
  836. The value of this is taken from the #+LINK lines.")
  837. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  838. (defcustom org-link-abbrev-alist nil
  839. "Alist of link abbreviations.
  840. The car of each element is a string, to be replaced at the start of a link.
  841. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  842. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  843. [[linkkey:tag][description]]
  844. If REPLACE is a string, the tag will simply be appended to create the link.
  845. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  846. also be a function that will be called with the tag as the only argument to
  847. create the link. See the manual for examples."
  848. :group 'org-link
  849. :type 'alist)
  850. (defcustom org-descriptive-links t
  851. "Non-nil means, hide link part and only show description of bracket links.
  852. Bracket links are like [[link][descritpion]]. This variable sets the initial
  853. state in new org-mode buffers. The setting can then be toggled on a
  854. per-buffer basis from the Org->Hyperlinks menu."
  855. :group 'org-link
  856. :type 'boolean)
  857. (defcustom org-link-file-path-type 'adaptive
  858. "How the path name in file links should be stored.
  859. Valid values are:
  860. relative relative to the current directory, i.e. the directory of the file
  861. into which the link is being inserted.
  862. absolute absolute path, if possible with ~ for home directory.
  863. noabbrev absolute path, no abbreviation of home directory.
  864. adaptive Use relative path for files in the current directory and sub-
  865. directories of it. For other files, use an absolute path."
  866. :group 'org-link
  867. :type '(choice
  868. (const relative)
  869. (const absolute)
  870. (const noabbrev)
  871. (const adaptive)))
  872. (defcustom org-activate-links '(bracket angle plain radio tag date)
  873. "Types of links that should be activated in Org-mode files.
  874. This is a list of symbols, each leading to the activation of a certain link
  875. type. In principle, it does not hurt to turn on most link types - there may
  876. be a small gain when turning off unused link types. The types are:
  877. bracket The recommended [[link][description]] or [[link]] links with hiding.
  878. angular Links in angular brackes that may contain whitespace like
  879. <bbdb:Carsten Dominik>.
  880. plain Plain links in normal text, no whitespace, like http://google.com.
  881. radio Text that is matched by a radio target, see manual for details.
  882. tag Tag settings in a headline (link to tag search).
  883. date Time stamps (link to calendar).
  884. Changing this variable requires a restart of Emacs to become effective."
  885. :group 'org-link
  886. :type '(set (const :tag "Double bracket links (new style)" bracket)
  887. (const :tag "Angular bracket links (old style)" angular)
  888. (const :tag "plain text links" plain)
  889. (const :tag "Radio target matches" radio)
  890. (const :tag "Tags" tag)
  891. (const :tag "Tags" target)
  892. (const :tag "Timestamps" date)))
  893. (defgroup org-link-store nil
  894. "Options concerning storing links in Org-mode"
  895. :tag "Org Store Link"
  896. :group 'org-link)
  897. (defcustom org-email-link-description-format "Email %c: %.30s"
  898. "Format of the description part of a link to an email or usenet message.
  899. The following %-excapes will be replaced by corresponding information:
  900. %F full \"From\" field
  901. %f name, taken from \"From\" field, address if no name
  902. %T full \"To\" field
  903. %t first name in \"To\" field, address if no name
  904. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  905. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  906. %s subject
  907. %m message-id.
  908. You may use normal field width specification between the % and the letter.
  909. This is for example useful to limit the length of the subject.
  910. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  911. :group 'org-link-store
  912. :type 'string)
  913. (defcustom org-from-is-user-regexp
  914. (let (r1 r2)
  915. (when (and user-mail-address (not (string= user-mail-address "")))
  916. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  917. (when (and user-full-name (not (string= user-full-name "")))
  918. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  919. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  920. "Regexp mached against the \"From:\" header of an email or usenet message.
  921. It should match if the message is from the user him/herself."
  922. :group 'org-link-store
  923. :type 'regexp)
  924. (defcustom org-context-in-file-links t
  925. "Non-nil means, file links from `org-store-link' contain context.
  926. A search string will be added to the file name with :: as separator and
  927. used to find the context when the link is activated by the command
  928. `org-open-at-point'.
  929. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  930. negates this setting for the duration of the command."
  931. :group 'org-link-store
  932. :type 'boolean)
  933. (defcustom org-keep-stored-link-after-insertion nil
  934. "Non-nil means, keep link in list for entire session.
  935. The command `org-store-link' adds a link pointing to the current
  936. location to an internal list. These links accumulate during a session.
  937. The command `org-insert-link' can be used to insert links into any
  938. Org-mode file (offering completion for all stored links). When this
  939. option is nil, every link which has been inserted once using \\[org-insert-link]
  940. will be removed from the list, to make completing the unused links
  941. more efficient."
  942. :group 'org-link-store
  943. :type 'boolean)
  944. (defcustom org-usenet-links-prefer-google nil
  945. "Non-nil means, `org-store-link' will create web links to Google groups.
  946. When nil, Gnus will be used for such links.
  947. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  948. negates this setting for the duration of the command."
  949. :group 'org-link-store
  950. :type 'boolean)
  951. (defgroup org-link-follow nil
  952. "Options concerning following links in Org-mode"
  953. :tag "Org Follow Link"
  954. :group 'org-link)
  955. (defcustom org-tab-follows-link nil
  956. "Non-nil means, on links TAB will follow the link.
  957. Needs to be set before org.el is loaded."
  958. :group 'org-link-follow
  959. :type 'boolean)
  960. (defcustom org-return-follows-link nil
  961. "Non-nil means, on links RET will follow the link.
  962. Needs to be set before org.el is loaded."
  963. :group 'org-link-follow
  964. :type 'boolean)
  965. (defcustom org-mouse-1-follows-link t
  966. "Non-nil means, mouse-1 on a link will follow the link.
  967. A longer mouse click will still set point. Does not wortk on XEmacs.
  968. Needs to be set before org.el is loaded."
  969. :group 'org-link-follow
  970. :type 'boolean)
  971. (defcustom org-mark-ring-length 4
  972. "Number of different positions to be recorded in the ring
  973. Changing this requires a restart of Emacs to work correctly."
  974. :group 'org-link-follow
  975. :type 'interger)
  976. (defcustom org-link-frame-setup
  977. '((vm . vm-visit-folder-other-frame)
  978. (gnus . gnus-other-frame)
  979. (file . find-file-other-window))
  980. "Setup the frame configuration for following links.
  981. When following a link with Emacs, it may often be useful to display
  982. this link in another window or frame. This variable can be used to
  983. set this up for the different types of links.
  984. For VM, use any of
  985. `vm-visit-folder'
  986. `vm-visit-folder-other-frame'
  987. For Gnus, use any of
  988. `gnus'
  989. `gnus-other-frame'
  990. For FILE, use any of
  991. `find-file'
  992. `find-file-other-window'
  993. `find-file-other-frame'
  994. For the calendar, use the variable `calendar-setup'.
  995. For BBDB, it is currently only possible to display the matches in
  996. another window."
  997. :group 'org-link-follow
  998. :type '(list
  999. (cons (const vm)
  1000. (choice
  1001. (const vm-visit-folder)
  1002. (const vm-visit-folder-other-window)
  1003. (const vm-visit-folder-other-frame)))
  1004. (cons (const gnus)
  1005. (choice
  1006. (const gnus)
  1007. (const gnus-other-frame)))
  1008. (cons (const file)
  1009. (choice
  1010. (const find-file)
  1011. (const find-file-other-window)
  1012. (const find-file-other-frame)))))
  1013. (defcustom org-display-internal-link-with-indirect-buffer nil
  1014. "Non-nil means, use indirect buffer to display infile links.
  1015. Activating internal links (from one location in a file to another location
  1016. in the same file) normally just jumps to the location. When the link is
  1017. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1018. another window. When this option is set, the other window actually displays
  1019. an indirect buffer clone of the current buffer, to avoid any visibility
  1020. changes to the current buffer."
  1021. :group 'org-link-follow
  1022. :type 'boolean)
  1023. (defcustom org-open-non-existing-files nil
  1024. "Non-nil means, `org-open-file' will open non-existing files.
  1025. When nil, an error will be generated."
  1026. :group 'org-link-follow
  1027. :type 'boolean)
  1028. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1029. "Function and arguments to call for following mailto links.
  1030. This is a list with the first element being a lisp function, and the
  1031. remaining elements being arguments to the function. In string arguments,
  1032. %a will be replaced by the address, and %s will be replaced by the subject
  1033. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1034. :group 'org-link-follow
  1035. :type '(choice
  1036. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1037. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1038. (const :tag "message-mail" (message-mail "%a" "%s"))
  1039. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1040. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1041. "Non-nil means, ask for confirmation before executing shell links.
  1042. Shell links can be dangerous, just thing about a link
  1043. [[shell:rm -rf ~/*][Google Search]]
  1044. This link would show up in your Org-mode document as \"Google Search\"
  1045. but really it would remove your entire home directory.
  1046. Therefore I *definitely* advise against setting this variable to nil.
  1047. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1048. rather than having to type \"yes\"."
  1049. :group 'org-link-follow
  1050. :type '(choice
  1051. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1052. (const :tag "with y-or-n (faster)" y-or-n-p)
  1053. (const :tag "no confirmation (dangerous)" nil)))
  1054. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1055. "Non-nil means, ask for confirmation before executing elisp links.
  1056. Elisp links can be dangerous, just think about a link
  1057. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1058. This link would show up in your Org-mode document as \"Google Search\"
  1059. but really it would remove your entire home directory.
  1060. Therefore I *definitely* advise against setting this variable to nil.
  1061. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1062. rather than having to type \"yes\"."
  1063. :group 'org-link-follow
  1064. :type '(choice
  1065. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1066. (const :tag "with y-or-n (faster)" y-or-n-p)
  1067. (const :tag "no confirmation (dangerous)" nil)))
  1068. (defconst org-file-apps-defaults-gnu
  1069. '((remote . emacs)
  1070. (t . mailcap))
  1071. "Default file applications on a UNIX or GNU/Linux system.
  1072. See `org-file-apps'.")
  1073. (defconst org-file-apps-defaults-macosx
  1074. '((remote . emacs)
  1075. (t . "open %s")
  1076. ("ps" . "gv %s")
  1077. ("ps.gz" . "gv %s")
  1078. ("eps" . "gv %s")
  1079. ("eps.gz" . "gv %s")
  1080. ("dvi" . "xdvi %s")
  1081. ("fig" . "xfig %s"))
  1082. "Default file applications on a MacOS X system.
  1083. The system \"open\" is known as a default, but we use X11 applications
  1084. for some files for which the OS does not have a good default.
  1085. See `org-file-apps'.")
  1086. (defconst org-file-apps-defaults-windowsnt
  1087. (list
  1088. '(remote . emacs)
  1089. (cons t
  1090. (list (if (featurep 'xemacs)
  1091. 'mswindows-shell-execute
  1092. 'w32-shell-execute)
  1093. "open" 'file)))
  1094. "Default file applications on a Windows NT system.
  1095. The system \"open\" is used for most files.
  1096. See `org-file-apps'.")
  1097. (defcustom org-file-apps
  1098. '(
  1099. ("txt" . emacs)
  1100. ("tex" . emacs)
  1101. ("ltx" . emacs)
  1102. ("org" . emacs)
  1103. ("el" . emacs)
  1104. ("bib" . emacs)
  1105. )
  1106. "External applications for opening `file:path' items in a document.
  1107. Org-mode uses system defaults for different file types, but
  1108. you can use this variable to set the application for a given file
  1109. extension. The entries in this list are cons cells where the car identifies
  1110. files and the cdr the corresponding command. Possible values for the
  1111. file identifier are
  1112. \"ext\" A string identifying an extension
  1113. `directory' Matches a directory
  1114. `remote' Matches a remote file, accessible through tramp or efs.
  1115. Remote files most likely should be visited through Emacs
  1116. because external applications cannot handle such paths.
  1117. t Default for all remaining files
  1118. Possible values for the command are:
  1119. `emacs' The file will be visited by the current Emacs process.
  1120. `default' Use the default application for this file type.
  1121. string A command to be executed by a shell; %s will be replaced
  1122. by the path to the file.
  1123. sexp A Lisp form which will be evaluated. The file path will
  1124. be available in the Lisp variable `file'.
  1125. For more examples, see the system specific constants
  1126. `org-file-apps-defaults-macosx'
  1127. `org-file-apps-defaults-windowsnt'
  1128. `org-file-apps-defaults-gnu'."
  1129. :group 'org-link-follow
  1130. :type '(repeat
  1131. (cons (choice :value ""
  1132. (string :tag "Extension")
  1133. (const :tag "Default for unrecognized files" t)
  1134. (const :tag "Remote file" remote)
  1135. (const :tag "Links to a directory" directory))
  1136. (choice :value ""
  1137. (const :tag "Visit with Emacs" emacs)
  1138. (const :tag "Use system default" default)
  1139. (string :tag "Command")
  1140. (sexp :tag "Lisp form")))))
  1141. (defcustom org-mhe-search-all-folders nil
  1142. "Non-nil means, that the search for the mh-message will be extended to
  1143. all folders if the message cannot be found in the folder given in the link.
  1144. Searching all folders is very efficient with one of the search engines
  1145. supported by MH-E, but will be slow with pick."
  1146. :group 'org-link-follow
  1147. :type 'boolean)
  1148. (defgroup org-remember nil
  1149. "Options concerning interaction with remember.el."
  1150. :tag "Org Remember"
  1151. :group 'org)
  1152. (defcustom org-directory "~/org"
  1153. "Directory with org files.
  1154. This directory will be used as default to prompt for org files.
  1155. Used by the hooks for remember.el."
  1156. :group 'org-remember
  1157. :type 'directory)
  1158. (defcustom org-default-notes-file "~/.notes"
  1159. "Default target for storing notes.
  1160. Used by the hooks for remember.el. This can be a string, or nil to mean
  1161. the value of `remember-data-file'.
  1162. You can set this on a per-template basis with the variable
  1163. `org-remember-templates'."
  1164. :group 'org-remember
  1165. :type '(choice
  1166. (const :tag "Default from remember-data-file" nil)
  1167. file))
  1168. (defcustom org-remember-default-headline ""
  1169. "The headline that should be the default location in the notes file.
  1170. When filing remember notes, the cursor will start at that position.
  1171. You can set this on a per-template basis with the variable
  1172. `org-remember-templates'."
  1173. :group 'org-remember
  1174. :type 'string)
  1175. (defcustom org-remember-templates nil
  1176. "Templates for the creation of remember buffers.
  1177. When nil, just let remember make the buffer.
  1178. When not nil, this is a list of 4-element lists. In each entry, the first
  1179. element is a character, a unique key to select this template.
  1180. The second element is the template. The third element is optional and can
  1181. specify a destination file for remember items created with this template.
  1182. The default file is given by `org-default-notes-file'. An optional forth
  1183. element can specify the headline in that file that should be offered
  1184. first when the user is asked to file the entry. The default headline is
  1185. given in the variable `org-remember-default-headline'.
  1186. The template specifies the structure of the remember buffer. It should have
  1187. a first line starting with a star, to act as the org-mode headline.
  1188. Furthermore, the following %-escapes will be replaced with content:
  1189. %^{prompt} prompt the user for a string and replace this sequence with it.
  1190. %t time stamp, date only
  1191. %T time stamp with date and time
  1192. %u, %U like the above, but inactive time stamps
  1193. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1194. You may define a prompt like %^{Please specify birthday}t
  1195. %n user name (taken from `user-full-name')
  1196. %a annotation, normally the link created with org-store-link
  1197. %i initial content, the region when remember is called with C-u.
  1198. If %i is indented, the entire inserted text will be indented
  1199. as well.
  1200. %? After completing the template, position cursor here.
  1201. Apart from these general escapes, you can access information specific to the
  1202. link type that is created. For example, calling `remember' in emails or gnus
  1203. will record the author and the subject of the message, which you can access
  1204. with %:author and %:subject, respectively. Here is a complete list of what
  1205. is recorded for each link type.
  1206. Link type | Available information
  1207. -------------------+------------------------------------------------------
  1208. bbdb | %:type %:name %:company
  1209. vm, wl, mh, rmail | %:type %:subject %:message-id
  1210. | %:from %:fromname %:fromaddress
  1211. | %:to %:toname %:toaddress
  1212. | %:fromto (either \"to NAME\" or \"from NAME\")
  1213. gnus | %:group, for messages also all email fields
  1214. w3, w3m | %:type %:url
  1215. info | %:type %:file %:node
  1216. calendar | %:type %:date"
  1217. :group 'org-remember
  1218. :get (lambda (var) ; Make sure all entries have 4 elements
  1219. (mapcar (lambda (x)
  1220. (cond ((= (length x) 3) (append x '("")))
  1221. ((= (length x) 2) (append x '("" "")))
  1222. (t x)))
  1223. (default-value var)))
  1224. :type '(repeat
  1225. :tag "enabled"
  1226. (list :value (?a "\n" nil nil)
  1227. (character :tag "Selection Key")
  1228. (string :tag "Template")
  1229. (file :tag "Destination file (optional)")
  1230. (string :tag "Destination headline (optional)"))))
  1231. (defcustom org-reverse-note-order nil
  1232. "Non-nil means, store new notes at the beginning of a file or entry.
  1233. When nil, new notes will be filed to the end of a file or entry."
  1234. :group 'org-remember
  1235. :type '(choice
  1236. (const :tag "Reverse always" t)
  1237. (const :tag "Reverse never" nil)
  1238. (repeat :tag "By file name regexp"
  1239. (cons regexp boolean))))
  1240. (defgroup org-todo nil
  1241. "Options concerning TODO items in Org-mode."
  1242. :tag "Org TODO"
  1243. :group 'org)
  1244. (defgroup org-progress nil
  1245. "Options concerning Progress logging in Org-mode."
  1246. :tag "Org Progress"
  1247. :group 'org-time)
  1248. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1249. "List of TODO entry keyword sequences and their interpretation.
  1250. \\<org-mode-map>This is a list of sequences.
  1251. Each sequence starts with a symbol, either `sequence' or `type',
  1252. indicating if the keywords should be interpreted as a sequence of
  1253. action steps, or as different types of TODO items. The first
  1254. keywords are states requiring action - these states will select a headline
  1255. for inclusion into the global TODO list Org-mode produces. If one of
  1256. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1257. signify that no further action is necessary. If \"|\" is not found,
  1258. the last keyword is treated as the only DONE state of the sequence.
  1259. The command \\[org-todo] cycles an entry through these states, and one
  1260. additional state where no keyword is present. For details about this
  1261. cycling, see the manual.
  1262. TODO keywords and interpretation can also be set on a per-file basis with
  1263. the special #+SEQ_TODO and #+TYP_TODO lines.
  1264. For backward compatibility, this variable may also be just a list
  1265. of keywords - in this case the interptetation (sequence or type) will be
  1266. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1267. :group 'org-todo
  1268. :group 'org-keywords
  1269. :type '(choice
  1270. (repeat :tag "Old syntax, just keywords"
  1271. (string :tag "Keyword"))
  1272. (repeat :tag "New syntax"
  1273. (cons
  1274. (choice
  1275. :tag "Interpretation"
  1276. (const :tag "Sequence (cycling hits every state)" sequence)
  1277. (const :tag "Type (cycling directly to DONE)" type))
  1278. (repeat
  1279. (string :tag "Keyword"))))))
  1280. (defvar org-todo-keywords-1 nil)
  1281. (make-variable-buffer-local 'org-todo-keywords-1)
  1282. (defvar org-todo-keywords-for-agenda nil)
  1283. (defvar org-done-keywords-for-agenda nil)
  1284. (defvar org-not-done-keywords nil)
  1285. (make-variable-buffer-local 'org-not-done-keywords)
  1286. (defvar org-done-keywords nil)
  1287. (make-variable-buffer-local 'org-done-keywords)
  1288. (defvar org-todo-heads nil)
  1289. (make-variable-buffer-local 'org-todo-heads)
  1290. (defvar org-todo-sets nil)
  1291. (make-variable-buffer-local 'org-todo-sets)
  1292. (defvar org-todo-kwd-alist nil)
  1293. (make-variable-buffer-local 'org-todo-kwd-alist)
  1294. (defcustom org-todo-interpretation 'sequence
  1295. "Controls how TODO keywords are interpreted.
  1296. This variable is in principle obsolete and is only used for
  1297. backward compatibility, if the interpretation of todo keywords is
  1298. not given already in `org-todo-keywords'. See that variable for
  1299. more information."
  1300. :group 'org-todo
  1301. :group 'org-keywords
  1302. :type '(choice (const sequence)
  1303. (const type)))
  1304. (defcustom org-after-todo-state-change-hook nil
  1305. "Hook which is run after the state of a TODO item was changed.
  1306. The new state (a string with a TODO keyword, or nil) is available in the
  1307. Lisp variable `state'."
  1308. :group 'org-todo
  1309. :type 'hook)
  1310. (defcustom org-log-done nil
  1311. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1312. When the state of an entry is changed from nothing to TODO, remove a previous
  1313. closing date.
  1314. This can also be a list of symbols indicating under which conditions
  1315. the time stamp recording the action should be annotated with a short note.
  1316. Valid members of this list are
  1317. done Offer to record a note when marking entries done
  1318. state Offer to record a note whenever changing the TODO state
  1319. of an item. This is only relevant if TODO keywords are
  1320. interpreted as sequence, see variable `org-todo-interpretation'.
  1321. When `state' is set, this includes tracking `done'.
  1322. clock-out Offer to record a note when clocking out of an item.
  1323. A separate window will then pop up and allow you to type a note.
  1324. After finishing with C-c C-c, the note will be added directly after the
  1325. timestamp, as a plain list item. See also the variable
  1326. `org-log-note-headings'.
  1327. Logging can also be configured on a per-file basis by adding one of
  1328. the following lines anywhere in the buffer:
  1329. #+STARTUP: logdone
  1330. #+STARTUP: nologging
  1331. #+STARTUP: lognotedone
  1332. #+STARTUP: lognotestate
  1333. #+STARTUP: lognoteclock-out"
  1334. :group 'org-todo
  1335. :group 'org-progress
  1336. :type '(choice
  1337. (const :tag "off" nil)
  1338. (const :tag "on" t)
  1339. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1340. (const :tag "when item is marked DONE" done)
  1341. (const :tag "when TODO state changes" state)
  1342. (const :tag "when clocking out" clock-out))))
  1343. (defcustom org-log-done-with-time t
  1344. "Non-nil means, the CLOSED time stamp will contain date and time.
  1345. When nil, only the date will be recorded."
  1346. :group 'org-progress
  1347. :type 'boolean)
  1348. (defcustom org-log-note-headings
  1349. '((done . "CLOSING NOTE %t")
  1350. (state . "State %-12s %t")
  1351. (clock-out . ""))
  1352. "Headings for notes added when clocking out or closing TODO items.
  1353. The value is an alist, with the car being a sympol indicating the note
  1354. context, and the cdr is the heading to be used. The heading may also be the
  1355. empty string.
  1356. %t in the heading will be replaced by a time stamp.
  1357. %s will be replaced by the new TODO state, in double quotes.
  1358. %u will be replaced by the user name.
  1359. %U will be replaced by the full user name."
  1360. :group 'org-todo
  1361. :group 'org-progress
  1362. :type '(list :greedy t
  1363. (cons (const :tag "Heading when closing an item" done) string)
  1364. (cons (const :tag
  1365. "Heading when changing todo state (todo sequence only)"
  1366. state) string)
  1367. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1368. (defcustom org-log-repeat t
  1369. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1370. When nil, no note will be taken."
  1371. :group 'org-todo
  1372. :group 'org-progress
  1373. :type 'boolean)
  1374. (defgroup org-priorities nil
  1375. "Priorities in Org-mode."
  1376. :tag "Org Priorities"
  1377. :group 'org-todo)
  1378. (defcustom org-highest-priority ?A
  1379. "The highest priority of TODO items. A character like ?A, ?B etc.
  1380. Must have a smaller ASCII number than `org-lowest-priority'."
  1381. :group 'org-priorities
  1382. :type 'character)
  1383. (defcustom org-lowest-priority ?C
  1384. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1385. Must have a larger ASCII number than `org-highest-priority'."
  1386. :group 'org-priorities
  1387. :type 'character)
  1388. (defcustom org-default-priority ?B
  1389. "The default priority of TODO items.
  1390. This is the priority an item get if no explicit priority is given."
  1391. :group 'org-priorities
  1392. :type 'character)
  1393. (defgroup org-time nil
  1394. "Options concerning time stamps and deadlines in Org-mode."
  1395. :tag "Org Time"
  1396. :group 'org)
  1397. (defcustom org-insert-labeled-timestamps-at-point nil
  1398. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1399. When nil, these labeled time stamps are forces into the second line of an
  1400. entry, just after the headline. When scheduling from the global TODO list,
  1401. the time stamp will always be forced into the second line."
  1402. :group 'org-time
  1403. :type 'boolean)
  1404. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1405. "Formats for `format-time-string' which are used for time stamps.
  1406. It is not recommended to change this constant.")
  1407. (defcustom org-time-stamp-rounding-minutes 0
  1408. "Number of minutes to round time stamps to upon insertion.
  1409. When zero, insert the time unmodified. Useful rounding numbers
  1410. should be factors of 60, so for example 5, 10, 15.
  1411. When this is not zero, you can still force an exact time-stamp by using
  1412. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1413. :group 'org-time
  1414. :type 'integer)
  1415. (defcustom org-display-custom-times nil
  1416. "Non-nil means, overlay custom formats over all time stamps.
  1417. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1418. To turn this on on a per-file basis, insert anywhere in the file:
  1419. #+STARTUP: customtime"
  1420. :group 'org-time
  1421. :set 'set-default
  1422. :type 'sexp)
  1423. (make-variable-buffer-local 'org-display-custom-times)
  1424. (defcustom org-time-stamp-custom-formats
  1425. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1426. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1427. These are overlayed over the default ISO format if the variable
  1428. `org-display-custom-times' is set. Time like %H:%M should be at the
  1429. end of the second format."
  1430. :group 'org-time
  1431. :type 'sexp)
  1432. (defun org-time-stamp-format (&optional long inactive)
  1433. "Get the right format for a time string."
  1434. (let ((f (if long (cdr org-time-stamp-formats)
  1435. (car org-time-stamp-formats))))
  1436. (if inactive
  1437. (concat "[" (substring f 1 -1) "]")
  1438. f)))
  1439. (defcustom org-deadline-warning-days 30
  1440. "No. of days before expiration during which a deadline becomes active.
  1441. This variable governs the display in sparse trees and in the agenda."
  1442. :group 'org-time
  1443. :type 'number)
  1444. (defcustom org-popup-calendar-for-date-prompt t
  1445. "Non-nil means, pop up a calendar when prompting for a date.
  1446. In the calendar, the date can be selected with mouse-1. However, the
  1447. minibuffer will also be active, and you can simply enter the date as well.
  1448. When nil, only the minibuffer will be available."
  1449. :group 'org-time
  1450. :type 'boolean)
  1451. (defcustom org-calendar-follow-timestamp-change t
  1452. "Non-nil means, make the calendar window follow timestamp changes.
  1453. When a timestamp is modified and the calendar window is visible, it will be
  1454. moved to the new date."
  1455. :group 'org-time
  1456. :type 'boolean)
  1457. (defgroup org-tags nil
  1458. "Options concerning tags in Org-mode."
  1459. :tag "Org Tags"
  1460. :group 'org)
  1461. (defcustom org-tag-alist nil
  1462. "List of tags allowed in Org-mode files.
  1463. When this list is nil, Org-mode will base TAG input on what is already in the
  1464. buffer.
  1465. The value of this variable is an alist, the car may be (and should) be a
  1466. character that is used to select that tag through the fast-tag-selection
  1467. interface. See the manual for details."
  1468. :group 'org-tags
  1469. :type '(repeat
  1470. (choice
  1471. (cons (string :tag "Tag name")
  1472. (character :tag "Access char"))
  1473. (const :tag "Start radio group" (:startgroup))
  1474. (const :tag "End radio group" (:endgroup)))))
  1475. (defcustom org-use-fast-tag-selection 'auto
  1476. "Non-nil means, use fast tag selection scheme.
  1477. This is a special interface to select and deselect tags with single keys.
  1478. When nil, fast selection is never used.
  1479. When the symbol `auto', fast selection is used if and only if selection
  1480. characters for tags have been configured, either through the variable
  1481. `org-tag-alist' or through a #+TAGS line in the buffer.
  1482. When t, fast selection is always used and selection keys are assigned
  1483. automatically if necessary."
  1484. :group 'org-tags
  1485. :type '(choice
  1486. (const :tag "Always" t)
  1487. (const :tag "Never" nil)
  1488. (const :tag "When selection characters are configured" 'auto)))
  1489. (defcustom org-fast-tag-selection-single-key nil
  1490. "Non-nil means, fast tag selection exits after first change.
  1491. When nil, you have to press RET to exit it.
  1492. During fast tag selection, you can toggle this flag with `C-c'.
  1493. This variable can also have the value `expert'. In this case, the window
  1494. displaying the tags menu is not even shown, until you press C-c again."
  1495. :group 'org-tags
  1496. :type '(choice
  1497. (const :tag "No" nil)
  1498. (const :tag "Yes" t)
  1499. (const :tag "Expert" expert)))
  1500. (defcustom org-tags-column 48
  1501. "The column to which tags should be indented in a headline.
  1502. If this number is positive, it specifies the column. If it is negative,
  1503. it means that the tags should be flushright to that column. For example,
  1504. -79 works well for a normal 80 character screen."
  1505. :group 'org-tags
  1506. :type 'integer)
  1507. (defcustom org-auto-align-tags t
  1508. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1509. These operations change the length of a headline and therefore shift
  1510. the tags around. With this options turned on, after each such operation
  1511. the tags are again aligned to `org-tags-column'."
  1512. :group 'org-tags
  1513. :type 'boolean)
  1514. (defcustom org-use-tag-inheritance t
  1515. "Non-nil means, tags in levels apply also for sublevels.
  1516. When nil, only the tags directly given in a specific line apply there.
  1517. If you turn off this option, you very likely want to turn on the
  1518. companion option `org-tags-match-list-sublevels'."
  1519. :group 'org-tags
  1520. :type 'boolean)
  1521. (defcustom org-tags-match-list-sublevels nil
  1522. "Non-nil means list also sublevels of headlines matching tag search.
  1523. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1524. the sublevels of a headline matching a tag search often also match
  1525. the same search. Listing all of them can create very long lists.
  1526. Setting this variable to nil causes subtrees of a match to be skipped.
  1527. This option is off by default, because inheritance in on. If you turn
  1528. inheritance off, you very likely want to turn this option on.
  1529. As a special case, if the tag search is restricted to TODO items, the
  1530. value of this variable is ignored and sublevels are always checked, to
  1531. make sure all corresponding TODO items find their way into the list."
  1532. :group 'org-tags
  1533. :type 'boolean)
  1534. (defvar org-tags-history nil
  1535. "History of minibuffer reads for tags.")
  1536. (defvar org-last-tags-completion-table nil
  1537. "The last used completion table for tags.")
  1538. (defgroup org-properties nil
  1539. "Options concerning properties in Org-mode."
  1540. :tag "Org Properties"
  1541. :group 'org)
  1542. (defcustom org-property-format "%-10s %s"
  1543. "How property key/value pairs should be formatted by `indent-line'.
  1544. When `indent-line' hits a property definition, it will format the line
  1545. according to this format, mainly to make sure that the values are
  1546. lined-up with respect to each other."
  1547. :group 'org-properties
  1548. :type 'string)
  1549. (defcustom org-default-columns-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1550. "The default column format, if no other format has been defined.
  1551. This variable can be set on the per-file basis by inserting a line
  1552. #+COLUMNS: %25ITEM ....."
  1553. :group 'org-properties
  1554. :type 'string)
  1555. (defgroup org-agenda nil
  1556. "Options concerning agenda views in Org-mode."
  1557. :tag "Org Agenda"
  1558. :group 'org)
  1559. (defvar org-category nil
  1560. "Variable used by org files to set a category for agenda display.
  1561. Such files should use a file variable to set it, for example
  1562. # -*- mode: org; org-category: \"ELisp\"
  1563. or contain a special line
  1564. #+CATEGORY: ELisp
  1565. If the file does not specify a category, then file's base name
  1566. is used instead.")
  1567. (make-variable-buffer-local 'org-category)
  1568. (defcustom org-agenda-files nil
  1569. "The files to be used for agenda display.
  1570. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1571. \\[org-remove-file]. You can also use customize to edit the list.
  1572. If the value of the variable is not a list but a single file name, then
  1573. the list of agenda files is actually stored and maintained in that file, one
  1574. agenda file per line."
  1575. :group 'org-agenda
  1576. :type '(choice
  1577. (repeat :tag "List of files" file)
  1578. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1579. (defcustom org-agenda-confirm-kill 1
  1580. "When set, remote killing from the agenda buffer needs confirmation.
  1581. When t, a confirmation is always needed. When a number N, confirmation is
  1582. only needed when the text to be killed contains more than N non-white lines."
  1583. :group 'org-agenda
  1584. :type '(choice
  1585. (const :tag "Never" nil)
  1586. (const :tag "Always" t)
  1587. (number :tag "When more than N lines")))
  1588. (defcustom org-calendar-to-agenda-key [?c]
  1589. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1590. The command `org-calendar-goto-agenda' will be bound to this key. The
  1591. default is the character `c' because then `c' can be used to switch back and
  1592. forth between agenda and calendar."
  1593. :group 'org-agenda
  1594. :type 'sexp)
  1595. (defgroup org-agenda-export nil
  1596. "Options concerning exporting agenda views in Org-mode."
  1597. :tag "Org Agenda Export"
  1598. :group 'org-agenda)
  1599. (defcustom org-agenda-with-colors t
  1600. "Non-nil means, use colors in agenda views."
  1601. :group 'org-agenda-export
  1602. :type 'boolean)
  1603. (defcustom org-agenda-exporter-settings nil
  1604. "Alist of variable/value pairs that should be active during agenda export.
  1605. This is a good place to set uptions for ps-print and for htmlize."
  1606. :group 'org-agenda-export
  1607. :type '(repeat
  1608. (list
  1609. (variable)
  1610. (sexp :tag "Value"))))
  1611. (defcustom org-agenda-export-html-style ""
  1612. "The style specification for exported HTML Agenda files.
  1613. If this variable contains a string, it will replace the default <style>
  1614. section as produced by `htmlize'.
  1615. Since there are different ways of setting style information, this variable
  1616. needs to contain the full HTML structure to provide a style, including the
  1617. surrounding HTML tags. The style specifications should include definitions
  1618. the fonts used by the agenda, here is an example:
  1619. <style type=\"text/css\">
  1620. p { font-weight: normal; color: gray; }
  1621. .org-agenda-structure {
  1622. font-size: 110%;
  1623. color: #003399;
  1624. font-weight: 600;
  1625. }
  1626. .org-todo {
  1627. color: #cc6666;Week-agenda:
  1628. font-weight: bold;
  1629. }
  1630. .org-done {
  1631. color: #339933;
  1632. }
  1633. .title { text-align: center; }
  1634. .todo, .deadline { color: red; }
  1635. .done { color: green; }
  1636. </style>
  1637. or, if you want to keep the style in a file,
  1638. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1639. As the value of this option simply gets inserted into the HTML <head> header,
  1640. you can \"misuse\" it to also add other text to the header. However,
  1641. <style>...</style> is required, if not present the variable will be ignored."
  1642. :group 'org-agenda-export
  1643. :group 'org-export-html
  1644. :type 'string)
  1645. (defgroup org-agenda-custom-commands nil
  1646. "Options concerning agenda views in Org-mode."
  1647. :tag "Org Agenda Custom Commands"
  1648. :group 'org-agenda)
  1649. (defcustom org-agenda-custom-commands nil
  1650. "Custom commands for the agenda.
  1651. These commands will be offered on the splash screen displayed by the
  1652. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1653. (key type match options files)
  1654. key The key (a single char as a string) to be associated with the command.
  1655. type The command type, any of the following symbols:
  1656. todo Entries with a specific TODO keyword, in all agenda files.
  1657. tags Tags match in all agenda files.
  1658. tags-todo Tags match in all agenda files, TODO entries only.
  1659. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1660. tags-tree Sparse tree with all tags matches in *current* file.
  1661. occur-tree Occur sparse tree for *current* file.
  1662. match What to search for:
  1663. - a single keyword for TODO keyword searches
  1664. - a tags match expression for tags searches
  1665. - a regular expression for occur searches
  1666. options A list of option setttings, similar to that in a let form, so like
  1667. this: ((opt1 val1) (opt2 val2) ...)
  1668. files A list of files file to write the produced agenda buffer to
  1669. with the command `org-store-agenda-views'.
  1670. If a file name ends in \".html\", an HTML version of the buffer
  1671. is written out. If it ends in \".ps\", a postscript version is
  1672. produced. Otherwide, only the plain text is written to the file.
  1673. You can also define a set of commands, to create a composite agenda buffer.
  1674. In this case, an entry looks like this:
  1675. (key desc (cmd1 cmd2 ...) general-options file)
  1676. where
  1677. desc A description string to be displayed in the dispatcher menu.
  1678. cmd An agenda command, similar to the above. However, tree commands
  1679. are no allowed, but instead you can get agenda and global todo list.
  1680. So valid commands for a set are:
  1681. (agenda)
  1682. (alltodo)
  1683. (stuck)
  1684. (todo \"match\" options files)
  1685. (tags \"match\" options files)
  1686. (tags-todo \"match\" options files)
  1687. Each command can carry a list of options, and another set of options can be
  1688. given for the whole set of commands. Individual command options take
  1689. precedence over the general options."
  1690. :group 'org-agenda-custom-commands
  1691. :type '(repeat
  1692. (choice :value ("a" tags "" nil)
  1693. (list :tag "Single command"
  1694. (string :tag "Key")
  1695. (choice
  1696. (const :tag "Agenda" agenda)
  1697. (const :tag "TODO list" alltodo)
  1698. (const :tag "Stuck projects" stuck)
  1699. (const :tag "Tags search (all agenda files)" tags)
  1700. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1701. (const :tag "TODO keyword search (all agenda files)" todo)
  1702. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1703. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1704. (const :tag "Occur tree (current buffer)" occur-tree)
  1705. (symbol :tag "Other, user-defined function"))
  1706. (string :tag "Match")
  1707. (repeat :tag "Local options"
  1708. (list (variable :tag "Option") (sexp :tag "Value")))
  1709. (option (repeat :tag "Export" (file :tag "Export to"))))
  1710. (list :tag "Command series, all agenda files"
  1711. (string :tag "Key")
  1712. (string :tag "Description")
  1713. (repeat
  1714. (choice
  1715. (const :tag "Agenda" (agenda))
  1716. (const :tag "TODO list" (alltodo))
  1717. (const :tag "Stuck projects" (stuck))
  1718. (list :tag "Tags search"
  1719. (const :format "" tags)
  1720. (string :tag "Match")
  1721. (repeat :tag "Local options"
  1722. (list (variable :tag "Option")
  1723. (sexp :tag "Value"))))
  1724. (list :tag "Tags search, TODO entries only"
  1725. (const :format "" tags-todo)
  1726. (string :tag "Match")
  1727. (repeat :tag "Local options"
  1728. (list (variable :tag "Option")
  1729. (sexp :tag "Value"))))
  1730. (list :tag "TODO keyword search"
  1731. (const :format "" todo)
  1732. (string :tag "Match")
  1733. (repeat :tag "Local options"
  1734. (list (variable :tag "Option")
  1735. (sexp :tag "Value"))))
  1736. (list :tag "Other, user-defined function"
  1737. (symbol :tag "function")
  1738. (string :tag "Match")
  1739. (repeat :tag "Local options"
  1740. (list (variable :tag "Option")
  1741. (sexp :tag "Value"))))))
  1742. (repeat :tag "General options"
  1743. (list (variable :tag "Option")
  1744. (sexp :tag "Value")))
  1745. (option (repeat :tag "Export" (file :tag "Export to")))))))
  1746. (defcustom org-stuck-projects
  1747. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1748. "How to identify stuck projects.
  1749. This is a list of four items:
  1750. 1. A tags/todo matcher string that is used to identify a project.
  1751. The entire tree below a headline matched by this is considered one project.
  1752. 2. A list of TODO keywords identifying non-stuck projects.
  1753. If the project subtree contains any headline with one of these todo
  1754. keywords, the project is considered to be not stuck. If you specify
  1755. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1756. 3. A list of tags identifying non-stuck projects.
  1757. If the project subtree contains any headline with one of these tags,
  1758. the project is considered to be not stuck. If you specify \"*\" as
  1759. a tag, any tag will mark the project unstuck.
  1760. 4. An arbitrary regular expression matching non-stuck projects.
  1761. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1762. or `C-c a #' to produce the list."
  1763. :group 'org-agenda-custom-commands
  1764. :type '(list
  1765. (string :tag "Tags/TODO match to identify a project")
  1766. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1767. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1768. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1769. (defgroup org-agenda-skip nil
  1770. "Options concerning skipping parts of agenda files."
  1771. :tag "Org Agenda Skip"
  1772. :group 'org-agenda)
  1773. (defcustom org-agenda-todo-list-sublevels t
  1774. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1775. When nil, the sublevels of a TODO entry are not checked, resulting in
  1776. potentially much shorter TODO lists."
  1777. :group 'org-agenda-skip
  1778. :group 'org-todo
  1779. :type 'boolean)
  1780. (defcustom org-agenda-todo-ignore-scheduled nil
  1781. "Non-nil means, don't show scheduled entries in the global todo list.
  1782. The idea behind this is that by scheduling it, you have already taken care
  1783. of this item."
  1784. :group 'org-agenda-skip
  1785. :group 'org-todo
  1786. :type 'boolean)
  1787. (defcustom org-agenda-todo-ignore-deadlines nil
  1788. "Non-nil means, don't show near deadline entries in the global todo list.
  1789. Near means closer than `org-deadline-warning-days' days.
  1790. The idea behind this is that such items will appear in the agenda anyway."
  1791. :group 'org-agenda-skip
  1792. :group 'org-todo
  1793. :type 'boolean)
  1794. (defcustom org-agenda-skip-scheduled-if-done nil
  1795. "Non-nil means don't show scheduled items in agenda when they are done.
  1796. This is relevant for the daily/weekly agenda, not for the TODO list."
  1797. :group 'org-agenda-skip
  1798. :type 'boolean)
  1799. (defcustom org-agenda-skip-deadline-if-done nil
  1800. "Non-nil means don't show deadines when the corresponding item is done.
  1801. When nil, the deadline is still shown and should give you a happy feeling.
  1802. This is relevant for the daily/weekly agenda."
  1803. :group 'org-agenda-skip
  1804. :type 'boolean)
  1805. (defcustom org-timeline-show-empty-dates 3
  1806. "Non-nil means, `org-timeline' also shows dates without an entry.
  1807. When nil, only the days which actually have entries are shown.
  1808. When t, all days between the first and the last date are shown.
  1809. When an integer, show also empty dates, but if there is a gap of more than
  1810. N days, just insert a special line indicating the size of the gap."
  1811. :group 'org-agenda-skip
  1812. :type '(choice
  1813. (const :tag "None" nil)
  1814. (const :tag "All" t)
  1815. (number :tag "at most")))
  1816. (defgroup org-agenda-startup nil
  1817. "Options concerning initial settings in the Agenda in Org Mode."
  1818. :tag "Org Agenda Startup"
  1819. :group 'org-agenda)
  1820. (defcustom org-finalize-agenda-hook nil
  1821. "Hook run just before displaying an agenda buffer."
  1822. :group 'org-agenda-startup
  1823. :type 'hook)
  1824. (defcustom org-agenda-mouse-1-follows-link nil
  1825. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1826. A longer mouse click will still set point. Does not wortk on XEmacs.
  1827. Needs to be set before org.el is loaded."
  1828. :group 'org-agenda-startup
  1829. :type 'boolean)
  1830. (defcustom org-agenda-start-with-follow-mode nil
  1831. "The initial value of follow-mode in a newly created agenda window."
  1832. :group 'org-agenda-startup
  1833. :type 'boolean)
  1834. (defgroup org-agenda-windows nil
  1835. "Options concerning the windows used by the Agenda in Org Mode."
  1836. :tag "Org Agenda Windows"
  1837. :group 'org-agenda)
  1838. (defcustom org-agenda-window-setup 'reorganize-frame
  1839. "How the agenda buffer should be displayed.
  1840. Possible values for this option are:
  1841. current-window Show agenda in the current window, keeping all other windows.
  1842. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1843. other-window Use `switch-to-buffer-other-window' to display agenda.
  1844. reorganize-frame Show only two windows on the current frame, the current
  1845. window and the agenda.
  1846. See also the variable `org-agenda-restore-windows-after-quit'."
  1847. :group 'org-agenda-windows
  1848. :type '(choice
  1849. (const current-window)
  1850. (const other-frame)
  1851. (const other-window)
  1852. (const reorganize-frame)))
  1853. (defcustom org-agenda-restore-windows-after-quit nil
  1854. "Non-nil means, restore window configuration open exiting agenda.
  1855. Before the window configuration is changed for displaying the agenda,
  1856. the current status is recorded. When the agenda is exited with
  1857. `q' or `x' and this option is set, the old state is restored. If
  1858. `org-agenda-window-setup' is `other-frame', the value of this
  1859. option will be ignored.."
  1860. :group 'org-agenda-windows
  1861. :type 'boolean)
  1862. (defcustom org-indirect-buffer-display 'other-window
  1863. "How should indirect tree buffers be displayed?
  1864. This applies to indirect buffers created with the commands
  1865. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1866. Valid values are:
  1867. current-window Display in the current window
  1868. other-window Just display in another window.
  1869. dedicated-frame Create one new frame, and re-use it each time.
  1870. new-frame Make a new frame each time."
  1871. :group 'org-structure
  1872. :group 'org-agenda-windows
  1873. :type '(choice
  1874. (const :tag "In current window" current-window)
  1875. (const :tag "In current frame, other window" other-window)
  1876. (const :tag "Each time a new frame" new-frame)
  1877. (const :tag "One dedicated frame" dedicated-frame)))
  1878. (defgroup org-agenda-daily/weekly nil
  1879. "Options concerning the daily/weekly agenda."
  1880. :tag "Org Agenda Daily/Weekly"
  1881. :group 'org-agenda)
  1882. (defcustom org-agenda-ndays 7
  1883. "Number of days to include in overview display.
  1884. Should be 1 or 7."
  1885. :group 'org-agenda-daily/weekly
  1886. :type 'number)
  1887. (defcustom org-agenda-start-on-weekday 1
  1888. "Non-nil means, start the overview always on the specified weekday.
  1889. 0 denotes Sunday, 1 denotes Monday etc.
  1890. When nil, always start on the current day."
  1891. :group 'org-agenda-daily/weekly
  1892. :type '(choice (const :tag "Today" nil)
  1893. (number :tag "Weekday No.")))
  1894. (defcustom org-agenda-show-all-dates t
  1895. "Non-nil means, `org-agenda' shows every day in the selected range.
  1896. When nil, only the days which actually have entries are shown."
  1897. :group 'org-agenda-daily/weekly
  1898. :type 'boolean)
  1899. (defcustom org-agenda-date-format "%A %d %B %Y"
  1900. "Format string for displaying dates in the agenda.
  1901. Used by the daily/weekly agenda and by the timeline. This should be
  1902. a format string understood by `format-time-string'.
  1903. FIXME: Not used currently, because of timezone problem."
  1904. :group 'org-agenda-daily/weekly
  1905. :type 'string)
  1906. (defcustom org-agenda-include-diary nil
  1907. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1908. :group 'org-agenda-daily/weekly
  1909. :type 'boolean)
  1910. (defcustom org-agenda-include-all-todo nil
  1911. "Set means weekly/daily agenda will always contain all TODO entries.
  1912. The TODO entries will be listed at the top of the agenda, before
  1913. the entries for specific days."
  1914. :group 'org-agenda-daily/weekly
  1915. :type 'boolean)
  1916. (defgroup org-agenda-time-grid nil
  1917. "Options concerning the time grid in the Org-mode Agenda."
  1918. :tag "Org Agenda Time Grid"
  1919. :group 'org-agenda)
  1920. (defcustom org-agenda-use-time-grid t
  1921. "Non-nil means, show a time grid in the agenda schedule.
  1922. A time grid is a set of lines for specific times (like every two hours between
  1923. 8:00 and 20:00). The items scheduled for a day at specific times are
  1924. sorted in between these lines.
  1925. For details about when the grid will be shown, and what it will look like, see
  1926. the variable `org-agenda-time-grid'."
  1927. :group 'org-agenda-time-grid
  1928. :type 'boolean)
  1929. (defcustom org-agenda-time-grid
  1930. '((daily today require-timed)
  1931. "----------------"
  1932. (800 1000 1200 1400 1600 1800 2000))
  1933. "The settings for time grid for agenda display.
  1934. This is a list of three items. The first item is again a list. It contains
  1935. symbols specifying conditions when the grid should be displayed:
  1936. daily if the agenda shows a single day
  1937. weekly if the agenda shows an entire week
  1938. today show grid on current date, independent of daily/weekly display
  1939. require-timed show grid only if at least one item has a time specification
  1940. The second item is a string which will be places behing the grid time.
  1941. The third item is a list of integers, indicating the times that should have
  1942. a grid line."
  1943. :group 'org-agenda-time-grid
  1944. :type
  1945. '(list
  1946. (set :greedy t :tag "Grid Display Options"
  1947. (const :tag "Show grid in single day agenda display" daily)
  1948. (const :tag "Show grid in weekly agenda display" weekly)
  1949. (const :tag "Always show grid for today" today)
  1950. (const :tag "Show grid only if any timed entries are present"
  1951. require-timed)
  1952. (const :tag "Skip grid times already present in an entry"
  1953. remove-match))
  1954. (string :tag "Grid String")
  1955. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1956. (defgroup org-agenda-sorting nil
  1957. "Options concerning sorting in the Org-mode Agenda."
  1958. :tag "Org Agenda Sorting"
  1959. :group 'org-agenda)
  1960. (let ((sorting-choice
  1961. '(choice
  1962. (const time-up) (const time-down)
  1963. (const category-keep) (const category-up) (const category-down)
  1964. (const tag-down) (const tag-up)
  1965. (const priority-up) (const priority-down))))
  1966. (defcustom org-agenda-sorting-strategy
  1967. '((agenda time-up category-keep priority-down)
  1968. (todo category-keep priority-down)
  1969. (tags category-keep priority-down))
  1970. "Sorting structure for the agenda items of a single day.
  1971. This is a list of symbols which will be used in sequence to determine
  1972. if an entry should be listed before another entry. The following
  1973. symbols are recognized:
  1974. time-up Put entries with time-of-day indications first, early first
  1975. time-down Put entries with time-of-day indications first, late first
  1976. category-keep Keep the default order of categories, corresponding to the
  1977. sequence in `org-agenda-files'.
  1978. category-up Sort alphabetically by category, A-Z.
  1979. category-down Sort alphabetically by category, Z-A.
  1980. tag-up Sort alphabetically by last tag, A-Z.
  1981. tag-down Sort alphabetically by last tag, Z-A.
  1982. priority-up Sort numerically by priority, high priority last.
  1983. priority-down Sort numerically by priority, high priority first.
  1984. The different possibilities will be tried in sequence, and testing stops
  1985. if one comparison returns a \"not-equal\". For example, the default
  1986. '(time-up category-keep priority-down)
  1987. means: Pull out all entries having a specified time of day and sort them,
  1988. in order to make a time schedule for the current day the first thing in the
  1989. agenda listing for the day. Of the entries without a time indication, keep
  1990. the grouped in categories, don't sort the categories, but keep them in
  1991. the sequence given in `org-agenda-files'. Within each category sort by
  1992. priority.
  1993. Leaving out `category-keep' would mean that items will be sorted across
  1994. categories by priority."
  1995. :group 'org-agenda-sorting
  1996. :type `(choice
  1997. (repeat :tag "General" ,sorting-choice)
  1998. (list :tag "Individually"
  1999. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2000. (repeat ,sorting-choice))
  2001. (cons (const :tag "Strategy for TODO lists" todo)
  2002. (repeat ,sorting-choice))
  2003. (cons (const :tag "Strategy for Tags matches" tags)
  2004. (repeat ,sorting-choice))))))
  2005. (defcustom org-sort-agenda-notime-is-late t
  2006. "Non-nil means, items without time are considered late.
  2007. This is only relevant for sorting. When t, items which have no explicit
  2008. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2009. do have a time. When nil, the default time is before 0:00. You can use this
  2010. option to decide if the schedule for today should come before or after timeless
  2011. agenda entries."
  2012. :group 'org-agenda-sorting
  2013. :type 'boolean)
  2014. (defgroup org-agenda-prefix nil
  2015. "Options concerning the entry prefix in the Org-mode agenda display."
  2016. :tag "Org Agenda Prefix"
  2017. :group 'org-agenda)
  2018. (defcustom org-agenda-prefix-format
  2019. '((agenda . " %-12:c%?-12t% s")
  2020. (timeline . " % s")
  2021. (todo . " %-12:c")
  2022. (tags . " %-12:c"))
  2023. "Format specifications for the prefix of items in the agenda views.
  2024. An alist with four entries, for the different agenda types. The keys to the
  2025. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2026. are format strings.
  2027. This format works similar to a printf format, with the following meaning:
  2028. %c the category of the item, \"Diary\" for entries from the diary, or
  2029. as given by the CATEGORY keyword or derived from the file name.
  2030. %T the *last* tag of the item. Last because inherited tags come
  2031. first in the list.
  2032. %t the time-of-day specification if one applies to the entry, in the
  2033. format HH:MM
  2034. %s Scheduling/Deadline information, a short string
  2035. All specifiers work basically like the standard `%s' of printf, but may
  2036. contain two additional characters: A question mark just after the `%' and
  2037. a whitespace/punctuation character just before the final letter.
  2038. If the first character after `%' is a question mark, the entire field
  2039. will only be included if the corresponding value applies to the
  2040. current entry. This is useful for fields which should have fixed
  2041. width when present, but zero width when absent. For example,
  2042. \"%?-12t\" will result in a 12 character time field if a time of the
  2043. day is specified, but will completely disappear in entries which do
  2044. not contain a time.
  2045. If there is punctuation or whitespace character just before the final
  2046. format letter, this character will be appended to the field value if
  2047. the value is not empty. For example, the format \"%-12:c\" leads to
  2048. \"Diary: \" if the category is \"Diary\". If the category were be
  2049. empty, no additional colon would be interted.
  2050. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2051. - Indent the line with two space characters
  2052. - Give the category in a 12 chars wide field, padded with whitespace on
  2053. the right (because of `-'). Append a colon if there is a category
  2054. (because of `:').
  2055. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2056. time, don't put in an empty field, just skip it (because of '?').
  2057. - Finally, put the scheduling information and append a whitespace.
  2058. As another example, if you don't want the time-of-day of entries in
  2059. the prefix, you could use:
  2060. (setq org-agenda-prefix-format \" %-11:c% s\")
  2061. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2062. `org-agenda-remove-tags'."
  2063. :type '(choice
  2064. (string :tag "General format")
  2065. (list :greedy t :tag "View dependent"
  2066. (cons (const agenda) (string :tag "Format"))
  2067. (cons (const timeline) (string :tag "Format"))
  2068. (cons (const todo) (string :tag "Format"))
  2069. (cons (const tags) (string :tag "Format"))))
  2070. :group 'org-agenda-prefix)
  2071. (defvar org-prefix-format-compiled nil
  2072. "The compiled version of the most recently used prefix format.
  2073. See the variable `org-agenda-prefix-format'.")
  2074. (defcustom org-agenda-remove-times-when-in-prefix t
  2075. "Non-nil means, remove duplicate time specifications in agenda items.
  2076. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2077. time-of-day specification in a headline or diary entry is extracted and
  2078. placed into the prefix. If this option is non-nil, the original specification
  2079. \(a timestamp or -range, or just a plain time(range) specification like
  2080. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2081. cluttered.
  2082. The option can be t or nil. It may also be the symbol `beg', indicating
  2083. that the time should only be removed what it is located at the beginning of
  2084. the headline/diary entry."
  2085. :group 'org-agenda-prefix
  2086. :type '(choice
  2087. (const :tag "Always" t)
  2088. (const :tag "Never" nil)
  2089. (const :tag "When at beginning of entry" beg)))
  2090. (defcustom org-agenda-default-appointment-duration nil
  2091. "Default duration for appointments that only have a starting time.
  2092. When nil, no duration is specified in such cases.
  2093. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2094. :group 'org-agenda-prefix
  2095. :type '(choice
  2096. (integer :tag "Minutes")
  2097. (const :tag "No default duration")))
  2098. (defcustom org-agenda-remove-tags nil
  2099. "Non-nil means, remove the tags from the headline copy in the agenda.
  2100. When this is the symbol `prefix', only remove tags when
  2101. `org-agenda-prefix-format' contains a `%T' specifier."
  2102. :group 'org-agenda-prefix
  2103. :type '(choice
  2104. (const :tag "Always" t)
  2105. (const :tag "Never" nil)
  2106. (const :tag "When prefix format contains %T" prefix)))
  2107. (if (fboundp 'defvaralias)
  2108. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2109. 'org-agenda-remove-tags))
  2110. (defcustom org-agenda-align-tags-to-column 65
  2111. "Shift tags in agenda items to this column."
  2112. :group 'org-agenda-prefix
  2113. :type 'integer)
  2114. (defgroup org-latex nil
  2115. "Options for embedding LaTeX code into Org-mode"
  2116. :tag "Org LaTeX"
  2117. :group 'org)
  2118. (defcustom org-format-latex-options
  2119. '(:foreground default :background default :scale 1.0
  2120. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2121. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2122. "Options for creating images from LaTeX fragments.
  2123. This is a property list with the following properties:
  2124. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2125. `default' means use the forground of the default face.
  2126. :background the background color, or \"Transparent\".
  2127. `default' means use the background of the default face.
  2128. :scale a scaling factor for the size of the images
  2129. :html-foreground, :html-background, :html-scale
  2130. The same numbers for HTML export.
  2131. :matchers a list indicating which matchers should be used to
  2132. find LaTeX fragments. Valid members of this list are:
  2133. \"begin\" find environments
  2134. \"$\" find math expressions surrounded by $...$
  2135. \"$$\" find math expressions surrounded by $$....$$
  2136. \"\\(\" find math expressions surrounded by \\(...\\)
  2137. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2138. :group 'org-latex
  2139. :type 'plist)
  2140. (defcustom org-format-latex-header "\\documentclass{article}
  2141. \\usepackage{fullpage} % do not remove
  2142. \\usepackage{amssymb}
  2143. \\usepackage[usenames]{color}
  2144. \\usepackage{amsmath}
  2145. \\usepackage{latexsym}
  2146. \\usepackage[mathscr]{eucal}
  2147. \\pagestyle{empty} % do not remove"
  2148. "The document header used for processing LaTeX fragments."
  2149. :group 'org-latex
  2150. :type 'string)
  2151. (defgroup org-export nil
  2152. "Options for exporting org-listings."
  2153. :tag "Org Export"
  2154. :group 'org)
  2155. (defgroup org-export-general nil
  2156. "General options for exporting Org-mode files."
  2157. :tag "Org Export General"
  2158. :group 'org-export)
  2159. (defcustom org-export-publishing-directory "."
  2160. "Path to the location where exported files should be located.
  2161. This path may be relative to the directory where the Org-mode file lives.
  2162. The default is to put them into the same directory as the Org-mode file.
  2163. The variable may also be an alist with export types `:html', `:ascii',
  2164. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  2165. is relative, it is interpreted relative to the directory where the exported
  2166. Org-mode files lives."
  2167. :group 'org-export-general
  2168. :type '(choice
  2169. (directory)
  2170. (repeat
  2171. (cons
  2172. (choice :tag "Type"
  2173. (const :html) (const :ascii) (const :ical) (const :xoxo))
  2174. (directory)))))
  2175. (defcustom org-export-language-setup
  2176. '(("en" "Author" "Date" "Table of Contents")
  2177. ("cs" "Autor" "Datum" "Obsah")
  2178. ("da" "Ophavsmand" "Dato" "Indhold")
  2179. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2180. ("es" "Autor" "Fecha" "\xccndice")
  2181. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  2182. ("it" "Autore" "Data" "Indice")
  2183. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2184. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2185. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2186. "Terms used in export text, translated to different languages.
  2187. Use the variable `org-export-default-language' to set the language,
  2188. or use the +OPTION lines for a per-file setting."
  2189. :group 'org-export-general
  2190. :type '(repeat
  2191. (list
  2192. (string :tag "HTML language tag")
  2193. (string :tag "Author")
  2194. (string :tag "Date")
  2195. (string :tag "Table of Contents"))))
  2196. (defcustom org-export-default-language "en"
  2197. "The default language of HTML export, as a string.
  2198. This should have an association in `org-export-language-setup'."
  2199. :group 'org-export-general
  2200. :type 'string)
  2201. (defcustom org-export-skip-text-before-1st-heading t
  2202. "Non-nil means, skip all text before the first headline when exporting.
  2203. When nil, that text is exported as well."
  2204. :group 'org-export-general
  2205. :type 'boolean)
  2206. (defcustom org-export-headline-levels 3
  2207. "The last level which is still exported as a headline.
  2208. Inferior levels will produce itemize lists when exported.
  2209. Note that a numeric prefix argument to an exporter function overrides
  2210. this setting.
  2211. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2212. :group 'org-export-general
  2213. :type 'number)
  2214. (defcustom org-export-with-section-numbers t
  2215. "Non-nil means, add section numbers to headlines when exporting.
  2216. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2217. :group 'org-export-general
  2218. :type 'boolean)
  2219. (defcustom org-export-with-toc t
  2220. "Non-nil means, create a table of contents in exported files.
  2221. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2222. When an integer, include levels up to N in the toc, this may then be
  2223. different from `org-export-headline-levels', but it will not be allowed
  2224. to be larger than the number of headline levels.
  2225. When nil, no table of contents is made.
  2226. Headlines which contain any TODO items will be marked with \"(*)\" in
  2227. ASCII export, and with red color in HTML output, if the option
  2228. `org-export-mark-todo-in-toc' is set.
  2229. In HTML output, the TOC will be clickable.
  2230. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2231. or \"toc:3\"."
  2232. :group 'org-export-general
  2233. :type '(choice
  2234. (const :tag "No Table of Contents" nil)
  2235. (const :tag "Full Table of Contents" t)
  2236. (integer :tag "TOC to level")))
  2237. (defcustom org-export-mark-todo-in-toc nil
  2238. "Non-nil means, mark TOC lines that contain any open TODO items."
  2239. :group 'org-export-general
  2240. :type 'boolean)
  2241. (defcustom org-export-preserve-breaks nil
  2242. "Non-nil means, preserve all line breaks when exporting.
  2243. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2244. export, line breaks will always be preserved, regardless of this variable.
  2245. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2246. :group 'org-export-general
  2247. :type 'boolean)
  2248. (defcustom org-export-with-archived-trees 'headline
  2249. "Whether subtrees with the ARCHIVE tag should be exported.
  2250. This can have three different values
  2251. nil Do not export, pretend this tree is not present
  2252. t Do export the entire tree
  2253. headline Only export the headline, but skip the tree below it."
  2254. :group 'org-export-general
  2255. :group 'org-archive
  2256. :type '(choice
  2257. (const :tag "not at all" nil)
  2258. (const :tag "headline only" 'headline)
  2259. (const :tag "entirely" t)))
  2260. (defcustom org-export-with-timestamps t
  2261. "If nil, do not export time stamps and associated keywords."
  2262. :group 'org-export-general
  2263. :type 'boolean)
  2264. (defcustom org-export-remove-timestamps-from-toc t
  2265. "If nil, remove timestamps from the table of contents entries."
  2266. :group 'org-export-general
  2267. :type 'boolean)
  2268. (defcustom org-export-with-tags 'not-in-toc
  2269. "If nil, do not export tags, just remove them from headlines.
  2270. If this is the symbol `not-in-toc', tags will be removed from table of
  2271. contents entries, but still be shown in the headlines of the document."
  2272. :group 'org-export-general
  2273. :type '(choice
  2274. (const :tag "Off" nil)
  2275. (const :tag "Not in TOC" not-in-toc)
  2276. (const :tag "On" t)))
  2277. (defcustom org-export-with-property-drawer nil
  2278. "Non-nil means, export property drawers.
  2279. When nil, these drawers are removed before export.
  2280. This option can also be set with the +OPTIONS line, e.g. \"p:t\"."
  2281. :group 'org-export-general
  2282. :type 'boolean)
  2283. (defgroup org-export-translation nil
  2284. "Options for translating special ascii sequences for the export backends."
  2285. :tag "Org Export Translation"
  2286. :group 'org-export)
  2287. (defcustom org-export-with-emphasize t
  2288. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2289. If the export target supports emphasizing text, the word will be
  2290. typeset in bold, italic, or underlined, respectively. Works only for
  2291. single words, but you can say: I *really* *mean* *this*.
  2292. Not all export backends support this.
  2293. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2294. :group 'org-export-translation
  2295. :type 'boolean)
  2296. (defcustom org-export-with-footnotes t
  2297. "If nil, export [1] as a footnote marker.
  2298. Lines starting with [1] will be formatted as footnotes.
  2299. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2300. :group 'org-export-translation
  2301. :type 'boolean)
  2302. (defcustom org-export-with-sub-superscripts t
  2303. "Non-nil means, interpret \"_\" and \"^\" for export.
  2304. When this option is turned on, you can use TeX-like syntax for sub- and
  2305. superscripts. Several characters after \"_\" or \"^\" will be
  2306. considered as a single item - so grouping with {} is normally not
  2307. needed. For example, the following things will be parsed as single
  2308. sub- or superscripts.
  2309. 10^24 or 10^tau several digits will be considered 1 item.
  2310. 10^-12 or 10^-tau a leading sign with digits or a word
  2311. x^2-y^3 will be read as x^2 - y^3, because items are
  2312. terminated by almost any nonword/nondigit char.
  2313. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2314. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2315. sub/superscript. If you set this variable to the symbol `{}',
  2316. the braces are *required* in order to trigger interpretations as
  2317. sub/superscript. This can be helpful in documents that need \"_\"
  2318. frequently in plain text.
  2319. Not all export backends support this, but HTML does.
  2320. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2321. :group 'org-export-translation
  2322. :type '(choice
  2323. (const :tag "Always interpret" t)
  2324. (const :tag "Only with braces" {})
  2325. (const :tag "Never interpret" nil)))
  2326. (defcustom org-export-with-TeX-macros t
  2327. "Non-nil means, interpret simple TeX-like macros when exporting.
  2328. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2329. No only real TeX macros will work here, but the standard HTML entities
  2330. for math can be used as macro names as well. For a list of supported
  2331. names in HTML export, see the constant `org-html-entities'.
  2332. Not all export backends support this.
  2333. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2334. :group 'org-export-translation
  2335. :group 'org-latex
  2336. :type 'boolean)
  2337. (defcustom org-export-with-LaTeX-fragments nil
  2338. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2339. When set, the exporter will find LaTeX environments if the \\begin line is
  2340. the first non-white thing on a line. It will also find the math delimiters
  2341. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2342. display math.
  2343. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2344. :group 'org-export-translation
  2345. :group 'org-latex
  2346. :type 'boolean)
  2347. (defcustom org-export-with-fixed-width t
  2348. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2349. This can be used to have pre-formatted text, fragments of code etc. For
  2350. example:
  2351. : ;; Some Lisp examples
  2352. : (while (defc cnt)
  2353. : (ding))
  2354. will be looking just like this in also HTML. See also the QUOTE keyword.
  2355. Not all export backends support this.
  2356. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2357. :group 'org-export-translation
  2358. :type 'boolean)
  2359. (defcustom org-match-sexp-depth 3
  2360. "Number of stacked braces for sub/superscript matching.
  2361. This has to be set before loading org.el to be effective."
  2362. :group 'org-export-translation
  2363. :type 'integer)
  2364. (defgroup org-export-tables nil
  2365. "Options for exporting tables in Org-mode."
  2366. :tag "Org Export Tables"
  2367. :group 'org-export)
  2368. (defcustom org-export-with-tables t
  2369. "If non-nil, lines starting with \"|\" define a table.
  2370. For example:
  2371. | Name | Address | Birthday |
  2372. |-------------+----------+-----------|
  2373. | Arthur Dent | England | 29.2.2100 |
  2374. Not all export backends support this.
  2375. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2376. :group 'org-export-tables
  2377. :type 'boolean)
  2378. (defcustom org-export-highlight-first-table-line t
  2379. "Non-nil means, highlight the first table line.
  2380. In HTML export, this means use <th> instead of <td>.
  2381. In tables created with table.el, this applies to the first table line.
  2382. In Org-mode tables, all lines before the first horizontal separator
  2383. line will be formatted with <th> tags."
  2384. :group 'org-export-tables
  2385. :type 'boolean)
  2386. (defcustom org-export-table-remove-special-lines t
  2387. "Remove special lines and marking characters in calculating tables.
  2388. This removes the special marking character column from tables that are set
  2389. up for spreadsheet calculations. It also removes the entire lines
  2390. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2391. the values of constants may be useful to have."
  2392. :group 'org-export-tables
  2393. :type 'boolean)
  2394. (defcustom org-export-prefer-native-exporter-for-tables nil
  2395. "Non-nil means, always export tables created with table.el natively.
  2396. Natively means, use the HTML code generator in table.el.
  2397. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2398. the table does not use row- or column-spanning). This has the
  2399. advantage, that the automatic HTML conversions for math symbols and
  2400. sub/superscripts can be applied. Org-mode's HTML generator is also
  2401. much faster."
  2402. :group 'org-export-tables
  2403. :type 'boolean)
  2404. (defgroup org-export-ascii nil
  2405. "Options specific for ASCII export of Org-mode files."
  2406. :tag "Org Export ASCII"
  2407. :group 'org-export)
  2408. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2409. "Characters for underlining headings in ASCII export.
  2410. In the given sequence, these characters will be used for level 1, 2, ..."
  2411. :group 'org-export-ascii
  2412. :type '(repeat character))
  2413. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2414. "Bullet characters for headlines converted to lists in ASCII export.
  2415. The first character is is used for the first lest level generated in this
  2416. way, and so on. If there are more levels than characters given here,
  2417. the list will be repeated.
  2418. Note that plain lists will keep the same bullets as the have in the
  2419. Org-mode file."
  2420. :group 'org-export-ascii
  2421. :type '(repeat character))
  2422. (defgroup org-export-xml nil
  2423. "Options specific for XML export of Org-mode files."
  2424. :tag "Org Export XML"
  2425. :group 'org-export)
  2426. (defgroup org-export-html nil
  2427. "Options specific for HTML export of Org-mode files."
  2428. :tag "Org Export HTML"
  2429. :group 'org-export)
  2430. (defcustom org-export-html-coding-system nil
  2431. ""
  2432. :group 'org-export-html
  2433. :type 'coding-system)
  2434. (defcustom org-export-html-style
  2435. "<style type=\"text/css\">
  2436. html {
  2437. font-family: Times, serif;
  2438. font-size: 12pt;
  2439. }
  2440. .title { text-align: center; }
  2441. .todo { color: red; }
  2442. .done { color: green; }
  2443. .timestamp { color: grey }
  2444. .timestamp-kwd { color: CadetBlue }
  2445. .tag { background-color:lightblue; font-weight:normal }
  2446. .target { background-color: lavender; }
  2447. pre {
  2448. border: 1pt solid #AEBDCC;
  2449. background-color: #F3F5F7;
  2450. padding: 5pt;
  2451. font-family: courier, monospace;
  2452. }
  2453. table { border-collapse: collapse; }
  2454. td, th {
  2455. vertical-align: top;
  2456. <!--border: 1pt solid #ADB9CC;-->
  2457. }
  2458. </style>"
  2459. "The default style specification for exported HTML files.
  2460. Since there are different ways of setting style information, this variable
  2461. needs to contain the full HTML structure to provide a style, including the
  2462. surrounding HTML tags. The style specifications should include definitions
  2463. for new classes todo, done, title, and deadline. For example, legal values
  2464. would be:
  2465. <style type=\"text/css\">
  2466. p { font-weight: normal; color: gray; }
  2467. h1 { color: black; }
  2468. .title { text-align: center; }
  2469. .todo, .deadline { color: red; }
  2470. .done { color: green; }
  2471. </style>
  2472. or, if you want to keep the style in a file,
  2473. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2474. As the value of this option simply gets inserted into the HTML <head> header,
  2475. you can \"misuse\" it to add arbitrary text to the header."
  2476. :group 'org-export-html
  2477. :type 'string)
  2478. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2479. "Format for typesetting the document title in HTML export."
  2480. :group 'org-export-html
  2481. :type 'string)
  2482. (defcustom org-export-html-toplevel-hlevel 2
  2483. "The <H> level for level 1 headings in HTML export."
  2484. :group 'org-export-html
  2485. :type 'string)
  2486. (defcustom org-export-html-link-org-files-as-html t
  2487. "Non-nil means, make file links to `file.org' point to `file.html'.
  2488. When org-mode is exporting an org-mode file to HTML, links to
  2489. non-html files are directly put into a href tag in HTML.
  2490. However, links to other Org-mode files (recognized by the
  2491. extension `.org.) should become links to the corresponding html
  2492. file, assuming that the linked org-mode file will also be
  2493. converted to HTML.
  2494. When nil, the links still point to the plain `.org' file."
  2495. :group 'org-export-html
  2496. :type 'boolean)
  2497. (defcustom org-export-html-inline-images 'maybe
  2498. "Non-nil means, inline images into exported HTML pages.
  2499. This is done using an <img> tag. When nil, an anchor with href is used to
  2500. link to the image. If this option is `maybe', then images in links with
  2501. an empty description will be inlined, while images with a description will
  2502. be linked only."
  2503. :group 'org-export-html
  2504. :type '(choice (const :tag "Never" nil)
  2505. (const :tag "Always" t)
  2506. (const :tag "When there is no description" maybe)))
  2507. ;; FIXME: rename
  2508. (defcustom org-export-html-expand t
  2509. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2510. When nil, these tags will be exported as plain text and therefore
  2511. not be interpreted by a browser.
  2512. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2513. :group 'org-export-html
  2514. :type 'boolean)
  2515. (defcustom org-export-html-table-tag
  2516. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2517. "The HTML tag that is used to start a table.
  2518. This must be a <table> tag, but you may change the options like
  2519. borders and spacing."
  2520. :group 'org-export-html
  2521. :type 'string)
  2522. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2523. "The opening tag for table header fields.
  2524. This is customizable so that alignment options can be specified."
  2525. :group 'org-export-tables
  2526. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2527. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2528. "The opening tag for table data fields.
  2529. This is customizable so that alignment options can be specified."
  2530. :group 'org-export-tables
  2531. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2532. (defcustom org-export-html-with-timestamp nil
  2533. "If non-nil, write `org-export-html-html-helper-timestamp'
  2534. into the exported HTML text. Otherwise, the buffer will just be saved
  2535. to a file."
  2536. :group 'org-export-html
  2537. :type 'boolean)
  2538. (defcustom org-export-html-html-helper-timestamp
  2539. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2540. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2541. :group 'org-export-html
  2542. :type 'string)
  2543. (defgroup org-export-icalendar nil
  2544. "Options specific for iCalendar export of Org-mode files."
  2545. :tag "Org Export iCalendar"
  2546. :group 'org-export)
  2547. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2548. "The file name for the iCalendar file covering all agenda files.
  2549. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2550. The file name should be absolute."
  2551. :group 'org-export-icalendar
  2552. :type 'file)
  2553. (defcustom org-icalendar-include-todo nil
  2554. "Non-nil means, export to iCalendar files should also cover TODO items."
  2555. :group 'org-export-icalendar
  2556. :type '(choice
  2557. (const :tag "None" nil)
  2558. (const :tag "Unfinished" t)
  2559. (const :tag "All" all)))
  2560. (defcustom org-icalendar-include-sexps t
  2561. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2562. These are entries like in the diary, but directly in an Org-mode file."
  2563. :group 'org-export-icalendar
  2564. :type 'boolean)
  2565. (defcustom org-icalendar-combined-name "OrgMode"
  2566. "Calendar name for the combined iCalendar representing all agenda files."
  2567. :group 'org-export-icalendar
  2568. :type 'string)
  2569. (defgroup org-font-lock nil
  2570. "Font-lock settings for highlighting in Org-mode."
  2571. :tag "Org Font Lock"
  2572. :group 'org)
  2573. (defcustom org-level-color-stars-only nil
  2574. "Non-nil means fontify only the stars in each headline.
  2575. When nil, the entire headline is fontified.
  2576. Changing it requires restart of `font-lock-mode' to become effective
  2577. also in regions already fontified."
  2578. :group 'org-font-lock
  2579. :type 'boolean)
  2580. (defcustom org-hide-leading-stars nil
  2581. "Non-nil means, hide the first N-1 stars in a headline.
  2582. This works by using the face `org-hide' for these stars. This
  2583. face is white for a light background, and black for a dark
  2584. background. You may have to customize the face `org-hide' to
  2585. make this work.
  2586. Changing it requires restart of `font-lock-mode' to become effective
  2587. also in regions already fontified.
  2588. You may also set this on a per-file basis by adding one of the following
  2589. lines to the buffer:
  2590. #+STARTUP: hidestars
  2591. #+STARTUP: showstars"
  2592. :group 'org-font-lock
  2593. :type 'boolean)
  2594. (defcustom org-fontify-done-headline nil
  2595. "Non-nil means, change the face of a headline if it is marked DONE.
  2596. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2597. When this is non-nil, the headline after the keyword is set to the
  2598. `org-headline-done' as an additional indication."
  2599. :group 'org-font-lock
  2600. :type 'boolean)
  2601. (defcustom org-fontify-emphasized-text t
  2602. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2603. Changing this variable requires a restart of Emacs to take effect."
  2604. :group 'org-font-lock
  2605. :type 'boolean)
  2606. (defvar org-emph-re nil
  2607. "Regular expression for matching emphasis.")
  2608. (defvar org-emphasis-regexp-components) ; defined just below
  2609. (defvar org-emphasis-alist) ; defined just below
  2610. (defun org-set-emph-re (var val)
  2611. "Set variable and compute the emphasis regular expression."
  2612. (set var val)
  2613. (when (and (boundp 'org-emphasis-alist)
  2614. (boundp 'org-emphasis-regexp-components)
  2615. org-emphasis-alist org-emphasis-regexp-components)
  2616. (let* ((e org-emphasis-regexp-components)
  2617. (pre (car e))
  2618. (post (nth 1 e))
  2619. (border (nth 2 e))
  2620. (body (nth 3 e))
  2621. (nl (nth 4 e))
  2622. (stacked (nth 5 e))
  2623. (body1 (concat body "*?"))
  2624. (markers (mapconcat 'car org-emphasis-alist "")))
  2625. ;; make sure special characters appear at the right position in the class
  2626. (if (string-match "\\^" markers)
  2627. (setq markers (concat (replace-match "" t t markers) "^")))
  2628. (if (string-match "-" markers)
  2629. (setq markers (concat (replace-match "" t t markers) "-")))
  2630. (if (> nl 0)
  2631. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2632. (int-to-string nl) "\\}")))
  2633. ;; Make the regexp
  2634. (setq org-emph-re
  2635. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2636. "\\("
  2637. "\\([" markers "]\\)"
  2638. "\\("
  2639. "[^" border (if (and nil stacked) markers) "]"
  2640. body1
  2641. "[^" border (if (and nil stacked) markers) "]"
  2642. "\\)"
  2643. "\\3\\)"
  2644. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2645. (defcustom org-emphasis-regexp-components
  2646. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2647. "Components used to build the reqular expression for emphasis.
  2648. This is a list with 6 entries. Terminology: In an emphasis string
  2649. like \" *strong word* \", we call the initial space PREMATCH, the final
  2650. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2651. and \"trong wor\" is the body. The different components in this variable
  2652. specify what is allowed/forbidden in each part:
  2653. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2654. post Chars allowed as postmatch. End of line will be allowed too.
  2655. border The chars *forbidden* as border characters.
  2656. body-regexp A regexp like \".\" to match a body character. Don't use
  2657. non-shy groups here, and don't allow newline here.
  2658. newline The maximum number of newlines allowed in an emphasis exp.
  2659. stacked Non-nil means, allow stacked styles. This works only in HTML
  2660. export. When this is set, all marker characters (as given in
  2661. `org-emphasis-alist') will be allowed as pre/post, aiding
  2662. inside-out matching.
  2663. Use customize to modify this, or restart Emacs after changing it."
  2664. :group 'org-font-lock
  2665. :set 'org-set-emph-re
  2666. :type '(list
  2667. (sexp :tag "Allowed chars in pre ")
  2668. (sexp :tag "Allowed chars in post ")
  2669. (sexp :tag "Forbidden chars in border ")
  2670. (sexp :tag "Regexp for body ")
  2671. (integer :tag "number of newlines allowed")
  2672. (boolean :tag "Stacking allowed ")))
  2673. (defcustom org-emphasis-alist
  2674. '(("*" bold "<b>" "</b>")
  2675. ("/" italic "<i>" "</i>")
  2676. ("_" underline "<u>" "</u>")
  2677. ("=" shadow "<code>" "</code>")
  2678. ("+" (:strike-through t) "<del>" "</del>")
  2679. )
  2680. "Special syntax for emphasized text.
  2681. Text starting and ending with a special character will be emphasized, for
  2682. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2683. characters, the face to be used by font-lock for highlighting in Org-mode
  2684. Emacs buffers, and the HTML tags to be used for this.
  2685. Use customize to modify this, or restart Emacs after changing it."
  2686. :group 'org-font-lock
  2687. :set 'org-set-emph-re
  2688. :type '(repeat
  2689. (list
  2690. (string :tag "Marker character")
  2691. (choice
  2692. (face :tag "Font-lock-face")
  2693. (plist :tag "Face property list"))
  2694. (string :tag "HTML start tag")
  2695. (string :tag "HTML end tag"))))
  2696. ;;; The faces
  2697. (defgroup org-faces nil
  2698. "Faces in Org-mode."
  2699. :tag "Org Faces"
  2700. :group 'org-font-lock)
  2701. ;; FIXME: convert that into a macro? Not critical, because this
  2702. ;; is only executed a few times at load time.
  2703. (defun org-compatible-face (specs)
  2704. "Make a compatible face specification.
  2705. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2706. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2707. to the top of the list. The `min-colors' attribute will be removed from
  2708. any other entries, and any resulting duplicates will be removed entirely."
  2709. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2710. (let (r e a)
  2711. (while (setq e (pop specs))
  2712. (cond
  2713. ((memq (car e) '(t default)) (push e r))
  2714. ((setq a (member '(min-colors 8) (car e)))
  2715. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2716. (cdr e)))))
  2717. ((setq a (assq 'min-colors (car e)))
  2718. (setq e (cons (delq a (car e)) (cdr e)))
  2719. (or (assoc (car e) r) (push e r)))
  2720. (t (or (assoc (car e) r) (push e r)))))
  2721. (nreverse r))
  2722. specs))
  2723. (defface org-hide
  2724. '((((background light)) (:foreground "white"))
  2725. (((background dark)) (:foreground "black")))
  2726. "Face used to hide leading stars in headlines.
  2727. The forground color of this face should be equal to the background
  2728. color of the frame."
  2729. :group 'org-faces)
  2730. (defface org-level-1 ;; font-lock-function-name-face
  2731. (org-compatible-face
  2732. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2733. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2734. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2735. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2736. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2737. (t (:bold t))))
  2738. "Face used for level 1 headlines."
  2739. :group 'org-faces)
  2740. (defface org-level-2 ;; font-lock-variable-name-face
  2741. (org-compatible-face
  2742. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2743. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2744. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2745. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2746. (t (:bold t))))
  2747. "Face used for level 2 headlines."
  2748. :group 'org-faces)
  2749. (defface org-level-3 ;; font-lock-keyword-face
  2750. (org-compatible-face
  2751. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2752. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2753. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2754. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2755. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2756. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2757. (t (:bold t))))
  2758. "Face used for level 3 headlines."
  2759. :group 'org-faces)
  2760. (defface org-level-4 ;; font-lock-comment-face
  2761. (org-compatible-face
  2762. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2763. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2764. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2765. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2766. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2767. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2768. (t (:bold t))))
  2769. "Face used for level 4 headlines."
  2770. :group 'org-faces)
  2771. (defface org-level-5 ;; font-lock-type-face
  2772. (org-compatible-face
  2773. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2774. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2775. (((class color) (min-colors 8)) (:foreground "green"))))
  2776. "Face used for level 5 headlines."
  2777. :group 'org-faces)
  2778. (defface org-level-6 ;; font-lock-constant-face
  2779. (org-compatible-face
  2780. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2781. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2782. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2783. "Face used for level 6 headlines."
  2784. :group 'org-faces)
  2785. (defface org-level-7 ;; font-lock-builtin-face
  2786. (org-compatible-face
  2787. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2788. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2789. (((class color) (min-colors 8)) (:foreground "blue"))))
  2790. "Face used for level 7 headlines."
  2791. :group 'org-faces)
  2792. (defface org-level-8 ;; font-lock-string-face
  2793. (org-compatible-face
  2794. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2795. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2796. (((class color) (min-colors 8)) (:foreground "green"))))
  2797. "Face used for level 8 headlines."
  2798. :group 'org-faces)
  2799. (defface org-special-keyword ;; font-lock-string-face
  2800. (org-compatible-face
  2801. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2802. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2803. (t (:italic t))))
  2804. "Face used for special keywords."
  2805. :group 'org-faces)
  2806. (defface org-drawer ;; font-lock-function-name-face
  2807. (org-compatible-face
  2808. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2809. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2810. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2811. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2812. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2813. (t (:bold t))))
  2814. "Face used for drawers."
  2815. :group 'org-faces)
  2816. (defface org-property-value nil
  2817. "Face used for the value of a property."
  2818. :group 'org-faces)
  2819. (defface org-column
  2820. (org-compatible-face
  2821. '((((class color) (min-colors 16) (background light))
  2822. (:background "grey90"))
  2823. (((class color) (min-colors 16) (background dark))
  2824. (:background "grey30"))
  2825. (((class color) (min-colors 8))
  2826. (:background "cyan" :foreground "black"))
  2827. (t (:inverse-video t))))
  2828. "Face for column display of entry properties."
  2829. :group 'org-faces)
  2830. (defface org-warning ;; font-lock-warning-face
  2831. (org-compatible-face
  2832. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2833. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2834. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2835. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2836. (t (:bold t))))
  2837. "Face for deadlines and TODO keywords."
  2838. :group 'org-faces)
  2839. (defface org-archived ; similar to shadow
  2840. (org-compatible-face
  2841. '((((class color grayscale) (min-colors 88) (background light))
  2842. (:foreground "grey50"))
  2843. (((class color grayscale) (min-colors 88) (background dark))
  2844. (:foreground "grey70"))
  2845. (((class color) (min-colors 8) (background light))
  2846. (:foreground "green"))
  2847. (((class color) (min-colors 8) (background dark))
  2848. (:foreground "yellow"))))
  2849. "Face for headline with the ARCHIVE tag."
  2850. :group 'org-faces)
  2851. (defface org-link
  2852. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2853. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2854. (t (:underline t)))
  2855. "Face for links."
  2856. :group 'org-faces)
  2857. (defface org-target
  2858. '((((class color) (background light)) (:underline t))
  2859. (((class color) (background dark)) (:underline t))
  2860. (t (:underline t)))
  2861. "Face for links."
  2862. :group 'org-faces)
  2863. (defface org-date
  2864. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2865. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2866. (t (:underline t)))
  2867. "Face for links."
  2868. :group 'org-faces)
  2869. (defface org-sexp-date
  2870. '((((class color) (background light)) (:foreground "Purple"))
  2871. (((class color) (background dark)) (:foreground "Cyan"))
  2872. (t (:underline t)))
  2873. "Face for links."
  2874. :group 'org-faces)
  2875. (defface org-tag
  2876. '((t (:bold t)))
  2877. "Face for tags."
  2878. :group 'org-faces)
  2879. (defface org-todo ;; font-lock-warning-face
  2880. (org-compatible-face
  2881. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2882. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2883. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2884. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2885. (t (:inverse-video t :bold t))))
  2886. "Face for TODO keywords."
  2887. :group 'org-faces)
  2888. (defface org-done ;; font-lock-type-face
  2889. (org-compatible-face
  2890. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2891. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2892. (((class color) (min-colors 8)) (:foreground "green"))
  2893. (t (:bold t))))
  2894. "Face used for todo keywords that indicate DONE items."
  2895. :group 'org-faces)
  2896. (defface org-headline-done ;; font-lock-string-face
  2897. (org-compatible-face
  2898. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2899. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2900. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2901. "Face used to indicate that a headline is DONE.
  2902. This face is only used if `org-fontify-done-headline' is set. If applies
  2903. to the part of the headline after the DONE keyword."
  2904. :group 'org-faces)
  2905. (defface org-table ;; font-lock-function-name-face
  2906. (org-compatible-face
  2907. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2908. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2909. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2910. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2911. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2912. (((class color) (min-colors 8) (background dark)))))
  2913. "Face used for tables."
  2914. :group 'org-faces)
  2915. (defface org-formula
  2916. (org-compatible-face
  2917. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2918. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2919. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2920. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2921. (t (:bold t :italic t))))
  2922. "Face for formulas."
  2923. :group 'org-faces)
  2924. (defface org-agenda-structure ;; font-lock-function-name-face
  2925. (org-compatible-face
  2926. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2927. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2928. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2929. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2930. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2931. (t (:bold t))))
  2932. "Face used in agenda for captions and dates."
  2933. :group 'org-faces)
  2934. (defface org-scheduled-today
  2935. (org-compatible-face
  2936. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2937. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2938. (((class color) (min-colors 8)) (:foreground "green"))
  2939. (t (:bold t :italic t))))
  2940. "Face for items scheduled for a certain day."
  2941. :group 'org-faces)
  2942. (defface org-scheduled-previously
  2943. (org-compatible-face
  2944. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2945. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2946. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2947. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2948. (t (:bold t))))
  2949. "Face for items scheduled previously, and not yet done."
  2950. :group 'org-faces)
  2951. (defface org-upcoming-deadline
  2952. (org-compatible-face
  2953. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2954. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2955. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2956. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2957. (t (:bold t))))
  2958. "Face for items scheduled previously, and not yet done."
  2959. :group 'org-faces)
  2960. (defface org-time-grid ;; font-lock-variable-name-face
  2961. (org-compatible-face
  2962. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2963. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2964. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2965. "Face used for time grids."
  2966. :group 'org-faces)
  2967. (defconst org-level-faces
  2968. '(org-level-1 org-level-2 org-level-3 org-level-4
  2969. org-level-5 org-level-6 org-level-7 org-level-8
  2970. ))
  2971. (defconst org-n-levels (length org-level-faces))
  2972. ;;; Variables for pre-computed regular expressions, all buffer local
  2973. (defvar org-drawer-regexp nil
  2974. "Matches first line of a hidden block.")
  2975. (make-variable-buffer-local 'org-drawer-regexp)
  2976. (defvar org-todo-regexp nil
  2977. "Matches any of the TODO state keywords.")
  2978. (make-variable-buffer-local 'org-todo-regexp)
  2979. (defvar org-not-done-regexp nil
  2980. "Matches any of the TODO state keywords except the last one.")
  2981. (make-variable-buffer-local 'org-not-done-regexp)
  2982. (defvar org-todo-line-regexp nil
  2983. "Matches a headline and puts TODO state into group 2 if present.")
  2984. (make-variable-buffer-local 'org-todo-line-regexp)
  2985. (defvar org-todo-line-tags-regexp nil
  2986. "Matches a headline and puts TODO state into group 2 if present.
  2987. Also put tags into group 4 if tags are present.")
  2988. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2989. (defvar org-nl-done-regexp nil
  2990. "Matches newline followed by a headline with the DONE keyword.")
  2991. (make-variable-buffer-local 'org-nl-done-regexp)
  2992. (defvar org-looking-at-done-regexp nil
  2993. "Matches the DONE keyword a point.")
  2994. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2995. (defvar org-ds-keyword-length 12
  2996. "Maximum length of the Deadline and SCHEDULED keywords.")
  2997. (make-variable-buffer-local 'org-ds-keyword-length)
  2998. (defvar org-deadline-regexp nil
  2999. "Matches the DEADLINE keyword.")
  3000. (make-variable-buffer-local 'org-deadline-regexp)
  3001. (defvar org-deadline-time-regexp nil
  3002. "Matches the DEADLINE keyword together with a time stamp.")
  3003. (make-variable-buffer-local 'org-deadline-time-regexp)
  3004. (defvar org-deadline-line-regexp nil
  3005. "Matches the DEADLINE keyword and the rest of the line.")
  3006. (make-variable-buffer-local 'org-deadline-line-regexp)
  3007. (defvar org-scheduled-regexp nil
  3008. "Matches the SCHEDULED keyword.")
  3009. (make-variable-buffer-local 'org-scheduled-regexp)
  3010. (defvar org-scheduled-time-regexp nil
  3011. "Matches the SCHEDULED keyword together with a time stamp.")
  3012. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3013. (defvar org-closed-time-regexp nil
  3014. "Matches the CLOSED keyword together with a time stamp.")
  3015. (make-variable-buffer-local 'org-closed-time-regexp)
  3016. (defvar org-keyword-time-regexp nil
  3017. "Matches any of the 4 keywords, together with the time stamp.")
  3018. (make-variable-buffer-local 'org-keyword-time-regexp)
  3019. (defvar org-keyword-time-not-clock-regexp nil
  3020. "Matches any of the 3 keywords, together with the time stamp.")
  3021. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3022. (defvar org-maybe-keyword-time-regexp nil
  3023. "Matches a timestamp, possibly preceeded by a keyword.")
  3024. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3025. (defvar org-planning-or-clock-line-re nil
  3026. "Matches a line with planning or clock info.")
  3027. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3028. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3029. rear-nonsticky t mouse-map t fontified t)
  3030. "Properties to remove when a string without properties is wanted.")
  3031. (defsubst org-match-string-no-properties (num &optional string)
  3032. (if (featurep 'xemacs)
  3033. (let ((s (match-string num string)))
  3034. (remove-text-properties 0 (length s) org-rm-props s)
  3035. s)
  3036. (match-string-no-properties num string)))
  3037. (defsubst org-no-properties (s)
  3038. (if (fboundp 'set-text-properties)
  3039. (set-text-properties 0 (length s) nil s)
  3040. (remove-text-properties 0 (length s) org-rm-props s))
  3041. s)
  3042. (defsubst org-get-alist-option (option key)
  3043. (cond ((eq key t) t)
  3044. ((eq option t) t)
  3045. ((assoc key option) (cdr (assoc key option)))
  3046. (t (cdr (assq 'default option)))))
  3047. (defsubst org-inhibit-invisibility ()
  3048. "Modified `buffer-invisibility-spec' for Emacs 21.
  3049. Some ops with invisible text do not work correctly on Emacs 21. For these
  3050. we turn off invisibility temporarily. Use this in a `let' form."
  3051. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3052. (defsubst org-set-local (var value)
  3053. "Make VAR local in current buffer and set it to VALUE."
  3054. (set (make-variable-buffer-local var) value))
  3055. (defsubst org-mode-p ()
  3056. "Check if the current buffer is in Org-mode."
  3057. (eq major-mode 'org-mode))
  3058. (defsubst org-last (list)
  3059. "Return the last element of LIST."
  3060. (car (last list)))
  3061. (defun org-let (list &rest body)
  3062. (eval (cons 'let (cons list body))))
  3063. (put 'org-let 'lisp-indent-function 1)
  3064. (defun org-let2 (list1 list2 &rest body)
  3065. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3066. (put 'org-let2 'lisp-indent-function 2)
  3067. (defconst org-startup-options
  3068. '(("fold" org-startup-folded t)
  3069. ("overview" org-startup-folded t)
  3070. ("nofold" org-startup-folded nil)
  3071. ("showall" org-startup-folded nil)
  3072. ("content" org-startup-folded content)
  3073. ("hidestars" org-hide-leading-stars t)
  3074. ("showstars" org-hide-leading-stars nil)
  3075. ("odd" org-odd-levels-only t)
  3076. ("oddeven" org-odd-levels-only nil)
  3077. ("align" org-startup-align-all-tables t)
  3078. ("noalign" org-startup-align-all-tables nil)
  3079. ("customtime" org-display-custom-times t)
  3080. ("logging" org-log-done t)
  3081. ("logdone" org-log-done t)
  3082. ("nologging" org-log-done nil)
  3083. ("lognotedone" org-log-done done push)
  3084. ("lognotestate" org-log-done state push)
  3085. ("lognoteclock-out" org-log-done clock-out push)
  3086. ("logrepeat" org-log-repeat t)
  3087. ("nologrepeat" org-log-repeat nil)
  3088. ("constcgs" constants-unit-system cgs)
  3089. ("constSI" constants-unit-system SI))
  3090. "Variable associated with STARTUP options for org-mode.
  3091. Each element is a list of three items: The startup options as written
  3092. in the #+STARTUP line, the corresponding variable, and the value to
  3093. set this variable to if the option is found. An optional forth element PUSH
  3094. means to push this value onto the list in the variable.")
  3095. (defun org-set-regexps-and-options ()
  3096. "Precompute regular expressions for current buffer."
  3097. (when (org-mode-p)
  3098. (org-set-local 'org-todo-kwd-alist nil)
  3099. (org-set-local 'org-todo-keywords-1 nil)
  3100. (org-set-local 'org-done-keywords nil)
  3101. (org-set-local 'org-todo-heads nil)
  3102. (org-set-local 'org-todo-sets nil)
  3103. (let ((re (org-make-options-regexp
  3104. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO" "COLUMNS"
  3105. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3106. "CONSTANTS")))
  3107. (splitre "[ \t]+")
  3108. kwds key value cat arch tags const links hw dws tail sep kws1 prio)
  3109. (save-excursion
  3110. (save-restriction
  3111. (widen)
  3112. (goto-char (point-min))
  3113. (while (re-search-forward re nil t)
  3114. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3115. (cond
  3116. ((equal key "CATEGORY")
  3117. (if (string-match "[ \t]+$" value)
  3118. (setq value (replace-match "" t t value)))
  3119. (setq cat (intern value)))
  3120. ((equal key "SEQ_TODO")
  3121. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3122. ((equal key "TYP_TODO")
  3123. (push (cons 'type (org-split-string value splitre)) kwds))
  3124. ((equal key "TAGS")
  3125. (setq tags (append tags (org-split-string value splitre))))
  3126. ((equal key "COLUMNS")
  3127. (org-set-local 'org-default-columns-format value))
  3128. ((equal key "LINK")
  3129. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3130. (push (cons (match-string 1 value)
  3131. (org-trim (match-string 2 value)))
  3132. links)))
  3133. ((equal key "PRIORITIES")
  3134. (setq prio (org-split-string value " +")))
  3135. ((equal key "CONSTANTS")
  3136. (setq const (append const (org-split-string value splitre))))
  3137. ((equal key "STARTUP")
  3138. (let ((opts (org-split-string value splitre))
  3139. l var val)
  3140. (while (setq l (pop opts))
  3141. (when (setq l (assoc l org-startup-options))
  3142. (setq var (nth 1 l) val (nth 2 l))
  3143. (if (not (nth 3 l))
  3144. (set (make-local-variable var) val)
  3145. (if (not (listp (symbol-value var)))
  3146. (set (make-local-variable var) nil))
  3147. (set (make-local-variable var) (symbol-value var))
  3148. (add-to-list var val))))))
  3149. ((equal key "ARCHIVE")
  3150. (string-match " *$" value)
  3151. (setq arch (replace-match "" t t value))
  3152. (remove-text-properties 0 (length arch)
  3153. '(face t fontified t) arch)))
  3154. )))
  3155. (and cat (org-set-local 'org-category cat))
  3156. (when prio
  3157. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3158. (setq prio (mapcar 'string-to-char prio))
  3159. (org-set-local 'org-highest-priority (nth 0 prio))
  3160. (org-set-local 'org-lowest-priority (nth 1 prio))
  3161. (org-set-local 'org-default-priority (nth 2 prio)))
  3162. (and arch (org-set-local 'org-archive-location arch))
  3163. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3164. ;; Process the TODO keywords
  3165. (unless kwds
  3166. ;; Use the global values as if they had been given locally.
  3167. (setq kwds (default-value 'org-todo-keywords))
  3168. (if (stringp (car kwds))
  3169. (setq kwds (list (cons org-todo-interpretation
  3170. (default-value 'org-todo-keywords)))))
  3171. (setq kwds (reverse kwds)))
  3172. (setq kwds (nreverse kwds))
  3173. (let (inter kws)
  3174. (while (setq kws (pop kwds))
  3175. (setq inter (pop kws) sep (member "|" kws)
  3176. kws1 (delete "|" (copy-sequence kws))
  3177. hw (car kws1)
  3178. dws (if sep (cdr sep) (last kws1))
  3179. tail (list inter hw (car dws) (org-last dws)))
  3180. (add-to-list 'org-todo-heads hw 'append)
  3181. (push kws1 org-todo-sets)
  3182. (setq org-done-keywords (append org-done-keywords dws nil))
  3183. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3184. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3185. (setq org-todo-sets (nreverse org-todo-sets)
  3186. org-todo-kwd-alist (nreverse org-todo-kwd-alist)))
  3187. ;; Process the constants
  3188. (when const
  3189. (let (e cst)
  3190. (while (setq e (pop const))
  3191. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3192. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3193. (setq org-table-formula-constants-local cst)))
  3194. ;; Process the tags.
  3195. (when tags
  3196. (let (e tgs)
  3197. (while (setq e (pop tags))
  3198. (cond
  3199. ((equal e "{") (push '(:startgroup) tgs))
  3200. ((equal e "}") (push '(:endgroup) tgs))
  3201. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3202. (push (cons (match-string 1 e)
  3203. (string-to-char (match-string 2 e)))
  3204. tgs))
  3205. (t (push (list e) tgs))))
  3206. (org-set-local 'org-tag-alist nil)
  3207. (while (setq e (pop tgs))
  3208. (or (and (stringp (car e))
  3209. (assoc (car e) org-tag-alist))
  3210. (push e org-tag-alist))))))
  3211. ;; Compute the regular expressions and other local variables
  3212. (if (not org-done-keywords)
  3213. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3214. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3215. (length org-scheduled-string)))
  3216. org-drawer-regexp
  3217. (concat "^[ \t]*:\\("
  3218. (mapconcat 'regexp-quote org-drawers "\\|")
  3219. "\\):[ \t]*$")
  3220. org-not-done-keywords
  3221. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3222. org-todo-regexp
  3223. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3224. "\\|") "\\)\\>")
  3225. org-not-done-regexp
  3226. (concat "\\<\\("
  3227. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3228. "\\)\\>")
  3229. org-todo-line-regexp
  3230. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  3231. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3232. "\\)\\>\\)? *\\(.*\\)")
  3233. org-nl-done-regexp
  3234. (concat "[\r\n]\\*+[ \t]+"
  3235. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3236. "\\)" "\\>")
  3237. org-todo-line-tags-regexp
  3238. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  3239. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3240. (org-re
  3241. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3242. org-looking-at-done-regexp
  3243. (concat "^" "\\(?:"
  3244. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3245. "\\>")
  3246. org-deadline-regexp (concat "\\<" org-deadline-string)
  3247. org-deadline-time-regexp
  3248. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3249. org-deadline-line-regexp
  3250. (concat "\\<\\(" org-deadline-string "\\).*")
  3251. org-scheduled-regexp
  3252. (concat "\\<" org-scheduled-string)
  3253. org-scheduled-time-regexp
  3254. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3255. org-closed-time-regexp
  3256. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3257. org-keyword-time-regexp
  3258. (concat "\\<\\(" org-scheduled-string
  3259. "\\|" org-deadline-string
  3260. "\\|" org-closed-string
  3261. "\\|" org-archived-string
  3262. "\\|" org-clock-string "\\)"
  3263. " *[[<]\\([^]>]+\\)[]>]")
  3264. org-keyword-time-not-clock-regexp
  3265. (concat "\\<\\(" org-scheduled-string
  3266. "\\|" org-deadline-string
  3267. "\\|" org-closed-string
  3268. "\\|" org-archived-string
  3269. "\\)"
  3270. " *[[<]\\([^]>]+\\)[]>]")
  3271. org-maybe-keyword-time-regexp
  3272. (concat "\\(\\<\\(" org-scheduled-string
  3273. "\\|" org-deadline-string
  3274. "\\|" org-closed-string
  3275. "\\|" org-archived-string
  3276. "\\|" org-clock-string "\\)\\)?"
  3277. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3278. org-planning-or-clock-line-re
  3279. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3280. "\\|" org-deadline-string
  3281. "\\|" org-closed-string "\\|" org-clock-string
  3282. "\\|" org-archived-string "\\)\\>\\)")
  3283. )
  3284. (org-set-font-lock-defaults)))
  3285. ;;; Some variables ujsed in various places
  3286. (defvar org-window-configuration nil
  3287. "Used in various places to store a window configuration.")
  3288. (defvar org-finish-function nil
  3289. "Function to be called when `C-c C-c' is used.
  3290. This is for getting out of special buffers like remember.")
  3291. ;;; Foreign variables, to inform the compiler
  3292. ;; XEmacs only
  3293. (defvar outline-mode-menu-heading)
  3294. (defvar outline-mode-menu-show)
  3295. (defvar outline-mode-menu-hide)
  3296. (defvar zmacs-regions) ; XEmacs regions
  3297. ;; Emacs only
  3298. (defvar mark-active)
  3299. ;; Packages that org-mode interacts with
  3300. (defvar calc-embedded-close-formula)
  3301. (defvar calc-embedded-open-formula)
  3302. (defvar font-lock-unfontify-region-function)
  3303. (defvar org-goto-start-pos)
  3304. (defvar vm-message-pointer)
  3305. (defvar vm-folder-directory)
  3306. (defvar wl-summary-buffer-elmo-folder)
  3307. (defvar wl-summary-buffer-folder-name)
  3308. (defvar gnus-other-frame-object)
  3309. (defvar gnus-group-name)
  3310. (defvar gnus-article-current)
  3311. (defvar w3m-current-url)
  3312. (defvar w3m-current-title)
  3313. (defvar mh-progs)
  3314. (defvar mh-current-folder)
  3315. (defvar mh-show-folder-buffer)
  3316. (defvar mh-index-folder)
  3317. (defvar mh-searcher)
  3318. (defvar calendar-mode-map)
  3319. (defvar Info-current-file)
  3320. (defvar Info-current-node)
  3321. (defvar texmathp-why)
  3322. (defvar remember-save-after-remembering)
  3323. (defvar remember-data-file)
  3324. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3325. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3326. (defvar org-latex-regexps)
  3327. (defvar constants-unit-system)
  3328. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3329. ;; FIXME: Occasionally check by commenting these, to make sure
  3330. ;; no other functions uses these, forgetting to let-bind them.
  3331. (defvar entry)
  3332. (defvar state)
  3333. (defvar last-state)
  3334. (defvar date)
  3335. (defvar description)
  3336. ;; Defined somewhere in this file, but used before definition.
  3337. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3338. (defvar org-agenda-undo-list)
  3339. (defvar org-agenda-pending-undo-list)
  3340. (defvar org-agenda-overriding-header)
  3341. (defvar orgtbl-mode)
  3342. (defvar org-html-entities)
  3343. (defvar org-struct-menu)
  3344. (defvar org-org-menu)
  3345. (defvar org-tbl-menu)
  3346. (defvar org-agenda-keymap)
  3347. (defvar org-category-table)
  3348. ;;;; Emacs/XEmacs compatibility
  3349. ;; Overlay compatibility functions
  3350. (defun org-make-overlay (beg end &optional buffer)
  3351. (if (featurep 'xemacs)
  3352. (make-extent beg end buffer)
  3353. (make-overlay beg end buffer)))
  3354. (defun org-delete-overlay (ovl)
  3355. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3356. (defun org-detach-overlay (ovl)
  3357. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3358. (defun org-move-overlay (ovl beg end &optional buffer)
  3359. (if (featurep 'xemacs)
  3360. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3361. (move-overlay ovl beg end buffer)))
  3362. (defun org-overlay-put (ovl prop value)
  3363. (if (featurep 'xemacs)
  3364. (set-extent-property ovl prop value)
  3365. (overlay-put ovl prop value)))
  3366. (defun org-overlay-display (ovl text &optional face evap)
  3367. "Make overlay OVL display TEXT with face FACE."
  3368. (if (featurep 'xemacs)
  3369. (let ((gl (make-glyph text)))
  3370. (and face (set-glyph-face gl face))
  3371. (set-extent-property ovl 'invisible t)
  3372. (set-extent-property ovl 'end-glyph gl))
  3373. (overlay-put ovl 'display text)
  3374. (if face (overlay-put ovl 'face face))
  3375. (if evap (overlay-put ovl 'evaporate t))))
  3376. (defun org-overlay-before-string (ovl text &optional face evap)
  3377. "Make overlay OVL display TEXT with face FACE."
  3378. (if (featurep 'xemacs)
  3379. (let ((gl (make-glyph text)))
  3380. (and face (set-glyph-face gl face))
  3381. (set-extent-property ovl 'begin-glyph gl))
  3382. (if face (org-add-props text nil 'face face))
  3383. (overlay-put ovl 'before-string text)
  3384. (if evap (overlay-put ovl 'evaporate t))))
  3385. (defun org-overlay-get (ovl prop)
  3386. (if (featurep 'xemacs)
  3387. (extent-property ovl prop)
  3388. (overlay-get ovl prop)))
  3389. (defun org-overlays-at (pos)
  3390. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3391. ;; FIXME: this is currently not used
  3392. (defun org-overlays-in (&optional start end)
  3393. (if (featurep 'xemacs)
  3394. (extent-list nil start end)
  3395. (overlays-in start end)))
  3396. (defun org-overlay-start (o)
  3397. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3398. (defun org-overlay-end (o)
  3399. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3400. ;; FIXME: this is currently not used
  3401. (defun org-find-overlays (prop &optional pos delete)
  3402. "Find all overlays specifying PROP at POS or point.
  3403. If DELETE is non-nil, delete all those overlays."
  3404. (let ((overlays (org-overlays-at (or pos (point))))
  3405. ov found)
  3406. (while (setq ov (pop overlays))
  3407. (if (org-overlay-get ov prop)
  3408. (if delete (org-delete-overlay ov) (push ov found))))
  3409. found))
  3410. ;; Region compatibility
  3411. (defun org-add-hook (hook function &optional append local)
  3412. "Add-hook, compatible with both Emacsen."
  3413. (if (and local (featurep 'xemacs))
  3414. (add-local-hook hook function append)
  3415. (add-hook hook function append local)))
  3416. (defvar org-ignore-region nil
  3417. "To temporarily disable the active region.")
  3418. (defun org-region-active-p ()
  3419. "Is `transient-mark-mode' on and the region active?
  3420. Works on both Emacs and XEmacs."
  3421. (if org-ignore-region
  3422. nil
  3423. (if (featurep 'xemacs)
  3424. (and zmacs-regions (region-active-p))
  3425. (and transient-mark-mode mark-active))))
  3426. ;; Invisibility compatibility
  3427. (defun org-add-to-invisibility-spec (arg)
  3428. "Add elements to `buffer-invisibility-spec'.
  3429. See documentation for `buffer-invisibility-spec' for the kind of elements
  3430. that can be added."
  3431. (cond
  3432. ((fboundp 'add-to-invisibility-spec)
  3433. (add-to-invisibility-spec arg))
  3434. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3435. (setq buffer-invisibility-spec (list arg)))
  3436. (t
  3437. (setq buffer-invisibility-spec
  3438. (cons arg buffer-invisibility-spec)))))
  3439. (defun org-remove-from-invisibility-spec (arg)
  3440. "Remove elements from `buffer-invisibility-spec'."
  3441. (if (fboundp 'remove-from-invisibility-spec)
  3442. (remove-from-invisibility-spec arg)
  3443. (if (consp buffer-invisibility-spec)
  3444. (setq buffer-invisibility-spec
  3445. (delete arg buffer-invisibility-spec)))))
  3446. ;; FIXME: this is currently not used
  3447. (defun org-in-invisibility-spec-p (arg)
  3448. "Is ARG a member of `buffer-invisibility-spec'?"
  3449. (if (consp buffer-invisibility-spec)
  3450. (member arg buffer-invisibility-spec)
  3451. nil))
  3452. ;;;; Define the Org-mode
  3453. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3454. (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."))
  3455. ;; We use a before-change function to check if a table might need
  3456. ;; an update.
  3457. (defvar org-table-may-need-update t
  3458. "Indicates that a table might need an update.
  3459. This variable is set by `org-before-change-function'.
  3460. `org-table-align' sets it back to nil.")
  3461. (defvar org-mode-map)
  3462. (defvar org-mode-hook nil)
  3463. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3464. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3465. (defvar org-table-buffer-is-an nil)
  3466. ;;;###autoload
  3467. (define-derived-mode org-mode outline-mode "Org"
  3468. "Outline-based notes management and organizer, alias
  3469. \"Carsten's outline-mode for keeping track of everything.\"
  3470. Org-mode develops organizational tasks around a NOTES file which
  3471. contains information about projects as plain text. Org-mode is
  3472. implemented on top of outline-mode, which is ideal to keep the content
  3473. of large files well structured. It supports ToDo items, deadlines and
  3474. time stamps, which magically appear in the diary listing of the Emacs
  3475. calendar. Tables are easily created with a built-in table editor.
  3476. Plain text URL-like links connect to websites, emails (VM), Usenet
  3477. messages (Gnus), BBDB entries, and any files related to the project.
  3478. For printing and sharing of notes, an Org-mode file (or a part of it)
  3479. can be exported as a structured ASCII or HTML file.
  3480. The following commands are available:
  3481. \\{org-mode-map}"
  3482. ;; Get rid of Outline menus, they are not needed
  3483. ;; Need to do this here because define-derived-mode sets up
  3484. ;; the keymap so late. Still, it is a waste to call this each time
  3485. ;; we switch another buffer into org-mode.
  3486. (if (featurep 'xemacs)
  3487. (when (boundp 'outline-mode-menu-heading)
  3488. ;; Assume this is Greg's port, it used easymenu
  3489. (easy-menu-remove outline-mode-menu-heading)
  3490. (easy-menu-remove outline-mode-menu-show)
  3491. (easy-menu-remove outline-mode-menu-hide))
  3492. (define-key org-mode-map [menu-bar headings] 'undefined)
  3493. (define-key org-mode-map [menu-bar hide] 'undefined)
  3494. (define-key org-mode-map [menu-bar show] 'undefined))
  3495. (easy-menu-add org-org-menu)
  3496. (easy-menu-add org-tbl-menu)
  3497. (org-install-agenda-files-menu)
  3498. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3499. (org-add-to-invisibility-spec '(org-cwidth))
  3500. (when (featurep 'xemacs)
  3501. (org-set-local 'line-move-ignore-invisible t))
  3502. (setq outline-regexp "\\*+")
  3503. (setq outline-level 'org-outline-level)
  3504. (when (and org-ellipsis (stringp org-ellipsis)
  3505. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table))
  3506. (unless org-display-table
  3507. (setq org-display-table (make-display-table)))
  3508. (set-display-table-slot org-display-table
  3509. 4 (string-to-vector org-ellipsis))
  3510. (setq buffer-display-table org-display-table))
  3511. (org-set-regexps-and-options)
  3512. ;; Calc embedded
  3513. (org-set-local 'calc-embedded-open-mode "# ")
  3514. (modify-syntax-entry ?# "<")
  3515. (modify-syntax-entry ?@ "w")
  3516. (if org-startup-truncated (setq truncate-lines t))
  3517. (org-set-local 'font-lock-unfontify-region-function
  3518. 'org-unfontify-region)
  3519. ;; Activate before-change-function
  3520. (org-set-local 'org-table-may-need-update t)
  3521. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3522. 'local)
  3523. ;; Check for running clock before killing a buffer
  3524. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3525. ;; Paragraphs and auto-filling
  3526. (org-set-autofill-regexps)
  3527. (setq indent-line-function 'org-indent-line-function)
  3528. (org-update-radio-target-regexp)
  3529. ;; Comment characters
  3530. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3531. (org-set-local 'comment-padding " ")
  3532. ;; Make isearch reveal context
  3533. (if (or (featurep 'xemacs)
  3534. (not (boundp 'outline-isearch-open-invisible-function)))
  3535. ;; Emacs 21 and XEmacs make use of the hook
  3536. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3537. ;; Emacs 22 deals with this through a special variable
  3538. (org-set-local 'outline-isearch-open-invisible-function
  3539. (lambda (&rest ignore) (org-show-context 'isearch))))
  3540. ;; If empty file that did not turn on org-mode automatically, make it to.
  3541. (if (and org-insert-mode-line-in-empty-file
  3542. (interactive-p)
  3543. (= (point-min) (point-max)))
  3544. (insert "# -*- mode: org -*-\n\n"))
  3545. (unless org-inhibit-startup
  3546. (when org-startup-align-all-tables
  3547. (let ((bmp (buffer-modified-p)))
  3548. (org-table-map-tables 'org-table-align)
  3549. (set-buffer-modified-p bmp)))
  3550. (cond
  3551. ((eq org-startup-folded t)
  3552. (org-cycle '(4)))
  3553. ((eq org-startup-folded 'content)
  3554. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3555. (org-cycle '(4)) (org-cycle '(4)))))))
  3556. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3557. (defsubst org-call-with-arg (command arg)
  3558. "Call COMMAND interactively, but pretend prefix are was ARG."
  3559. (let ((current-prefix-arg arg)) (call-interactively command)))
  3560. (defsubst org-current-line (&optional pos)
  3561. (save-excursion
  3562. (and pos (goto-char pos))
  3563. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3564. (defun org-current-time ()
  3565. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3566. (if (> org-time-stamp-rounding-minutes 0)
  3567. (let ((r org-time-stamp-rounding-minutes)
  3568. (time (decode-time)))
  3569. (apply 'encode-time
  3570. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3571. (nthcdr 2 time))))
  3572. (current-time)))
  3573. (defun org-add-props (string plist &rest props)
  3574. "Add text properties to entire string, from beginning to end.
  3575. PLIST may be a list of properties, PROPS are individual properties and values
  3576. that will be added to PLIST. Returns the string that was modified."
  3577. (add-text-properties
  3578. 0 (length string) (if props (append plist props) plist) string)
  3579. string)
  3580. (put 'org-add-props 'lisp-indent-function 2)
  3581. ;;;; Font-Lock stuff, including the activators
  3582. (defvar org-mouse-map (make-sparse-keymap))
  3583. (org-defkey org-mouse-map
  3584. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3585. (org-defkey org-mouse-map
  3586. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3587. (when org-mouse-1-follows-link
  3588. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3589. (when org-tab-follows-link
  3590. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3591. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3592. (when org-return-follows-link
  3593. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3594. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3595. (require 'font-lock)
  3596. (defconst org-non-link-chars "]\t\n\r<>")
  3597. (defconst org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3598. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3599. (defconst org-link-re-with-space
  3600. (concat
  3601. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3602. "\\([^" org-non-link-chars " ]"
  3603. "[^" org-non-link-chars "]*"
  3604. "[^" org-non-link-chars " ]\\)>?")
  3605. "Matches a link with spaces, optional angular brackets around it.")
  3606. (defconst org-link-re-with-space2
  3607. (concat
  3608. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3609. "\\([^" org-non-link-chars " ]"
  3610. "[^]\t\n\r]*"
  3611. "[^" org-non-link-chars " ]\\)>?")
  3612. "Matches a link with spaces, optional angular brackets around it.")
  3613. (defconst org-angle-link-re
  3614. (concat
  3615. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3616. "\\([^" org-non-link-chars " ]"
  3617. "[^" org-non-link-chars "]*"
  3618. "\\)>")
  3619. "Matches link with angular brackets, spaces are allowed.")
  3620. (defconst org-plain-link-re
  3621. (concat
  3622. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3623. "\\([^]\t\n\r<>,;() ]+\\)")
  3624. "Matches plain link, without spaces.")
  3625. (defconst org-bracket-link-regexp
  3626. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3627. "Matches a link in double brackets.")
  3628. (defconst org-bracket-link-analytic-regexp
  3629. (concat
  3630. "\\[\\["
  3631. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3632. "\\([^]]+\\)"
  3633. "\\]"
  3634. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3635. "\\]"))
  3636. ; 1: http:
  3637. ; 2: http
  3638. ; 3: path
  3639. ; 4: [desc]
  3640. ; 5: desc
  3641. (defconst org-any-link-re
  3642. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3643. org-angle-link-re "\\)\\|\\("
  3644. org-plain-link-re "\\)")
  3645. "Regular expression matching any link.")
  3646. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3647. "Regular expression for fast time stamp matching.")
  3648. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3649. "Regular expression for fast time stamp matching.")
  3650. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3651. "Regular expression matching time strings for analysis.
  3652. This one does not require the space after the date.")
  3653. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3654. "Regular expression matching time strings for analysis.")
  3655. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,11\\}>")
  3656. "Regular expression matching time stamps, with groups.")
  3657. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,11\\}[]>]")
  3658. "Regular expression matching time stamps (also [..]), with groups.")
  3659. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3660. "Regular expression matching a time stamp range.")
  3661. (defconst org-tr-regexp-both
  3662. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3663. "Regular expression matching a time stamp range.")
  3664. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3665. org-ts-regexp "\\)?")
  3666. "Regular expression matching a time stamp or time stamp range.")
  3667. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3668. org-ts-regexp-both "\\)?")
  3669. "Regular expression matching a time stamp or time stamp range.
  3670. The time stamps may be either active or inactive.")
  3671. (defvar org-emph-face nil)
  3672. (defun org-do-emphasis-faces (limit)
  3673. "Run through the buffer and add overlays to links."
  3674. (let (rtn)
  3675. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3676. (if (not (= (char-after (match-beginning 3))
  3677. (char-after (match-beginning 4))))
  3678. (progn
  3679. (setq rtn t)
  3680. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3681. 'face
  3682. (nth 1 (assoc (match-string 3)
  3683. org-emphasis-alist)))
  3684. (add-text-properties (match-beginning 2) (match-end 2)
  3685. '(font-lock-multiline t))
  3686. (backward-char 1))))
  3687. rtn))
  3688. (defun org-emphasize (&optional char)
  3689. "Insert or change an emphasis, i.e. a font like bold or italic.
  3690. If there is an active region, change that region to a new emphasis.
  3691. If there is no region, just insert the marker characters and position
  3692. the cursor between them.
  3693. CHAR should be either the marker character, or the first character of the
  3694. HTML tag associated with that emphasis. If CHAR is a space, the means
  3695. to remove the emphasis of the selected region.
  3696. If char is not given (for example in an interactive call) it
  3697. will be prompted for."
  3698. (interactive)
  3699. (let ((eal org-emphasis-alist) e det
  3700. (erc org-emphasis-regexp-components)
  3701. (prompt "")
  3702. (string "") beg end move tag c s)
  3703. (if (org-region-active-p)
  3704. (setq beg (region-beginning) end (region-end)
  3705. string (buffer-substring beg end))
  3706. (setq move t))
  3707. (while (setq e (pop eal))
  3708. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3709. c (aref tag 0))
  3710. (push (cons c (string-to-char (car e))) det)
  3711. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3712. (substring tag 1)))))
  3713. (unless char
  3714. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3715. (setq char (read-char-exclusive)))
  3716. (setq char (or (cdr (assoc char det)) char))
  3717. (if (equal char ?\ )
  3718. (setq s "" move nil)
  3719. (unless (assoc (char-to-string char) org-emphasis-alist)
  3720. (error "No such emphasis marker: \"%c\"" char))
  3721. (setq s (char-to-string char)))
  3722. (while (and (> (length string) 1)
  3723. (equal (substring string 0 1) (substring string -1))
  3724. (assoc (substring string 0 1) org-emphasis-alist))
  3725. (setq string (substring string 1 -1)))
  3726. (setq string (concat s string s))
  3727. (if beg (delete-region beg end))
  3728. (unless (or (bolp)
  3729. (string-match (concat "[" (nth 0 erc) "\n]")
  3730. (char-to-string (char-before (point)))))
  3731. (insert " "))
  3732. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3733. (char-to-string (char-after (point))))
  3734. (insert " ") (backward-char 1))
  3735. (insert string)
  3736. (and move (backward-char 1))))
  3737. (defun org-activate-plain-links (limit)
  3738. "Run through the buffer and add overlays to links."
  3739. (catch 'exit
  3740. (let (f)
  3741. (while (re-search-forward org-plain-link-re limit t)
  3742. (setq f (get-text-property (match-beginning 0) 'face))
  3743. (if (or (eq f 'org-tag)
  3744. (and (listp f) (memq 'org-tag f)))
  3745. nil
  3746. (add-text-properties (match-beginning 0) (match-end 0)
  3747. (list 'mouse-face 'highlight
  3748. 'rear-nonsticky t
  3749. 'keymap org-mouse-map
  3750. ))
  3751. (throw 'exit t))))))
  3752. (defun org-activate-angle-links (limit)
  3753. "Run through the buffer and add overlays to links."
  3754. (if (re-search-forward org-angle-link-re limit t)
  3755. (progn
  3756. (add-text-properties (match-beginning 0) (match-end 0)
  3757. (list 'mouse-face 'highlight
  3758. 'rear-nonsticky t
  3759. 'keymap org-mouse-map
  3760. ))
  3761. t)))
  3762. (defmacro org-maybe-intangible (props)
  3763. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3764. In emacs 21, invisible text is not avoided by the command loop, so the
  3765. intangible property is needed to make sure point skips this text.
  3766. In Emacs 22, this is not necessary. The intangible text property has
  3767. led to problems with flyspell. These problems are fixed in flyspell.el,
  3768. but we still avoid setting the property in Emacs 22 and later.
  3769. We use a macro so that the test can happen at compilation time."
  3770. (if (< emacs-major-version 22)
  3771. `(append '(intangible t) ,props)
  3772. props))
  3773. (defun org-activate-bracket-links (limit)
  3774. "Run through the buffer and add overlays to bracketed links."
  3775. (if (re-search-forward org-bracket-link-regexp limit t)
  3776. (let* ((help (concat "LINK: "
  3777. (org-match-string-no-properties 1)))
  3778. ;; FIXME: above we should remove the escapes.
  3779. ;; but that requires another match, protecting match data,
  3780. ;; a lot of overhead for font-lock.
  3781. (ip (org-maybe-intangible
  3782. (list 'invisible 'org-link 'rear-nonsticky t
  3783. 'keymap org-mouse-map 'mouse-face 'highlight
  3784. 'help-echo help)))
  3785. (vp (list 'rear-nonsticky t
  3786. 'keymap org-mouse-map 'mouse-face 'highlight
  3787. 'help-echo help)))
  3788. ;; We need to remove the invisible property here. Table narrowing
  3789. ;; may have made some of this invisible.
  3790. (remove-text-properties (match-beginning 0) (match-end 0)
  3791. '(invisible nil))
  3792. (if (match-end 3)
  3793. (progn
  3794. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3795. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3796. (add-text-properties (match-end 3) (match-end 0) ip))
  3797. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3798. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3799. (add-text-properties (match-end 1) (match-end 0) ip))
  3800. t)))
  3801. (defun org-activate-dates (limit)
  3802. "Run through the buffer and add overlays to dates."
  3803. (if (re-search-forward org-tsr-regexp-both limit t)
  3804. (progn
  3805. (add-text-properties (match-beginning 0) (match-end 0)
  3806. (list 'mouse-face 'highlight
  3807. 'rear-nonsticky t
  3808. 'keymap org-mouse-map))
  3809. (when org-display-custom-times
  3810. (if (match-end 3)
  3811. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3812. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3813. t)))
  3814. (defvar org-target-link-regexp nil
  3815. "Regular expression matching radio targets in plain text.")
  3816. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3817. "Regular expression matching a link target.")
  3818. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3819. "Regular expression matching a radio target.")
  3820. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3821. "Regular expression matching any target.")
  3822. (defun org-activate-target-links (limit)
  3823. "Run through the buffer and add overlays to target matches."
  3824. (when org-target-link-regexp
  3825. (let ((case-fold-search t))
  3826. (if (re-search-forward org-target-link-regexp limit t)
  3827. (progn
  3828. (add-text-properties (match-beginning 0) (match-end 0)
  3829. (list 'mouse-face 'highlight
  3830. 'rear-nonsticky t
  3831. 'keymap org-mouse-map
  3832. 'help-echo "Radio target link"
  3833. 'org-linked-text t))
  3834. t)))))
  3835. (defun org-update-radio-target-regexp ()
  3836. "Find all radio targets in this file and update the regular expression."
  3837. (interactive)
  3838. (when (memq 'radio org-activate-links)
  3839. (setq org-target-link-regexp
  3840. (org-make-target-link-regexp (org-all-targets 'radio)))
  3841. (org-restart-font-lock)))
  3842. (defun org-hide-wide-columns (limit)
  3843. (let (s e)
  3844. (setq s (text-property-any (point) (or limit (point-max))
  3845. 'org-cwidth t))
  3846. (when s
  3847. (setq e (next-single-property-change s 'org-cwidth))
  3848. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3849. (goto-char e)
  3850. t)))
  3851. (defun org-restart-font-lock ()
  3852. "Restart font-lock-mode, to force refontification."
  3853. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3854. (font-lock-mode -1)
  3855. (font-lock-mode 1)))
  3856. (defun org-all-targets (&optional radio)
  3857. "Return a list of all targets in this file.
  3858. With optional argument RADIO, only find radio targets."
  3859. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3860. rtn)
  3861. (save-excursion
  3862. (goto-char (point-min))
  3863. (while (re-search-forward re nil t)
  3864. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3865. rtn)))
  3866. (defun org-make-target-link-regexp (targets)
  3867. "Make regular expression matching all strings in TARGETS.
  3868. The regular expression finds the targets also if there is a line break
  3869. between words."
  3870. (and targets
  3871. (concat
  3872. "\\<\\("
  3873. (mapconcat
  3874. (lambda (x)
  3875. (while (string-match " +" x)
  3876. (setq x (replace-match "\\s-+" t t x)))
  3877. x)
  3878. targets
  3879. "\\|")
  3880. "\\)\\>")))
  3881. (defun org-activate-tags (limit)
  3882. (if (re-search-forward (org-re "[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3883. (progn
  3884. (add-text-properties (match-beginning 1) (match-end 1)
  3885. (list 'mouse-face 'highlight
  3886. 'rear-nonsticky t
  3887. 'keymap org-mouse-map))
  3888. t)))
  3889. (defun org-outline-level ()
  3890. (save-excursion
  3891. (looking-at outline-regexp)
  3892. (if (match-beginning 1)
  3893. (+ (org-get-string-indentation (match-string 1)) 1000)
  3894. (- (match-end 0) (match-beginning 0)))))
  3895. (defvar org-font-lock-keywords nil)
  3896. (defun org-set-font-lock-defaults ()
  3897. (let* ((em org-fontify-emphasized-text)
  3898. (lk org-activate-links)
  3899. (org-font-lock-extra-keywords
  3900. ;; Headlines
  3901. (list
  3902. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  3903. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3904. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3905. (1 'org-table))
  3906. ;; Links
  3907. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3908. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3909. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3910. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3911. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3912. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3913. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3914. '(org-hide-wide-columns (0 nil append))
  3915. ;; TODO lines
  3916. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  3917. '(1 'org-todo t))
  3918. ;; Priorities
  3919. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3920. ;; Special keywords
  3921. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3922. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3923. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3924. (list (concat "\\<" org-archived-string) '(0 'org-special-keyword t))
  3925. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3926. ;; Emphasis
  3927. (if em
  3928. (if (featurep 'xemacs)
  3929. '(org-do-emphasis-faces (0 nil append))
  3930. '(org-do-emphasis-faces)))
  3931. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  3932. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  3933. 2 'bold prepend)
  3934. (if org-provide-checkbox-statistics
  3935. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3936. (0 (org-get-checkbox-statistics-face) t)))
  3937. ;; COMMENT
  3938. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  3939. "\\|" org-quote-string "\\)\\>")
  3940. '(1 'org-special-keyword t))
  3941. '("^#.*" (0 'font-lock-comment-face t))
  3942. ;; DONE
  3943. (if org-fontify-done-headline
  3944. (list (concat "^[*]+ +\\<\\("
  3945. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3946. "\\)\\(.*\\)")
  3947. '(1 'org-done t) '(2 'org-headline-done t))
  3948. (list (concat "^[*]+ +\\<\\("
  3949. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3950. "\\)\\>")
  3951. '(1 'org-done t)))
  3952. ;; Table stuff
  3953. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  3954. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3955. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  3956. ;; Drawers
  3957. (list org-drawer-regexp '(0 'org-drawer t))
  3958. (list "^[ \t]*:END:" '(0 'org-drawer t))
  3959. ;; Properties
  3960. '("^[ \t]*\\(:[a-zA-Z0-9]+:\\)[ \t]*\\(\\S-.*\\)"
  3961. (1 'org-special-keyword t) (2 'org-property-value t))
  3962. ;FIXME (1 'org-tag t) (2 'org-property-value t))
  3963. (if org-format-transports-properties-p
  3964. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3965. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  3966. )))
  3967. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3968. ;; Now set the full font-lock-keywords
  3969. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3970. (org-set-local 'font-lock-defaults
  3971. '(org-font-lock-keywords t nil nil backward-paragraph))
  3972. (kill-local-variable 'font-lock-keywords) nil))
  3973. (defvar org-m nil)
  3974. (defvar org-l nil)
  3975. (defvar org-f nil)
  3976. (defun org-get-level-face (n)
  3977. "Get the right face for match N in font-lock matching of healdines."
  3978. (setq org-l (- (match-end 2) (match-beginning 1)))
  3979. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3980. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  3981. (cond
  3982. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3983. ((eq n 2) org-f)
  3984. (t (if org-level-color-stars-only nil org-f))))
  3985. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3986. "Remove fontification and activation overlays from links."
  3987. (font-lock-default-unfontify-region beg end)
  3988. (let* ((buffer-undo-list t)
  3989. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3990. (inhibit-modification-hooks t)
  3991. deactivate-mark buffer-file-name buffer-file-truename)
  3992. (remove-text-properties beg end
  3993. '(mouse-face t keymap t org-linked-text t
  3994. rear-nonsticky t
  3995. invisible t intangible t))))
  3996. ;;;; Visibility cycling, including org-goto and indirect buffer
  3997. ;;; Cycling
  3998. (defvar org-cycle-global-status nil)
  3999. (make-variable-buffer-local 'org-cycle-global-status)
  4000. (defvar org-cycle-subtree-status nil)
  4001. (make-variable-buffer-local 'org-cycle-subtree-status)
  4002. ;;;###autoload
  4003. (defun org-cycle (&optional arg)
  4004. "Visibility cycling for Org-mode.
  4005. - When this function is called with a prefix argument, rotate the entire
  4006. buffer through 3 states (global cycling)
  4007. 1. OVERVIEW: Show only top-level headlines.
  4008. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4009. 3. SHOW ALL: Show everything.
  4010. - When point is at the beginning of a headline, rotate the subtree started
  4011. by this line through 3 different states (local cycling)
  4012. 1. FOLDED: Only the main headline is shown.
  4013. 2. CHILDREN: The main headline and the direct children are shown.
  4014. From this state, you can move to one of the children
  4015. and zoom in further.
  4016. 3. SUBTREE: Show the entire subtree, including body text.
  4017. - When there is a numeric prefix, go up to a heading with level ARG, do
  4018. a `show-subtree' and return to the previous cursor position. If ARG
  4019. is negative, go up that many levels.
  4020. - When point is not at the beginning of a headline, execute
  4021. `indent-relative', like TAB normally does. See the option
  4022. `org-cycle-emulate-tab' for details.
  4023. - Special case: if point is at the beginning of the buffer and there is
  4024. no headline in line 1, this function will act as if called with prefix arg."
  4025. (interactive "P")
  4026. (let* ((outline-regexp
  4027. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4028. "\\(?:\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4029. outline-regexp))
  4030. (bob-special (and org-cycle-global-at-bob (bobp)
  4031. (not (looking-at outline-regexp))))
  4032. (org-cycle-hook
  4033. (if bob-special
  4034. (delq 'org-optimize-window-after-visibility-change
  4035. (copy-sequence org-cycle-hook))
  4036. org-cycle-hook))
  4037. (pos (point)))
  4038. (if (or bob-special (equal arg '(4)))
  4039. ;; special case: use global cycling
  4040. (setq arg t))
  4041. (cond
  4042. ((org-at-table-p 'any)
  4043. ;; Enter the table or move to the next field in the table
  4044. (or (org-table-recognize-table.el)
  4045. (progn
  4046. (if arg (org-table-edit-field t)
  4047. (org-table-justify-field-maybe)
  4048. (call-interactively 'org-table-next-field)))))
  4049. ((eq arg t) ;; Global cycling
  4050. (cond
  4051. ((and (eq last-command this-command)
  4052. (eq org-cycle-global-status 'overview))
  4053. ;; We just created the overview - now do table of contents
  4054. ;; This can be slow in very large buffers, so indicate action
  4055. (message "CONTENTS...")
  4056. (org-content)
  4057. (message "CONTENTS...done")
  4058. (setq org-cycle-global-status 'contents)
  4059. (run-hook-with-args 'org-cycle-hook 'contents))
  4060. ((and (eq last-command this-command)
  4061. (eq org-cycle-global-status 'contents))
  4062. ;; We just showed the table of contents - now show everything
  4063. (show-all)
  4064. (message "SHOW ALL")
  4065. (setq org-cycle-global-status 'all)
  4066. (run-hook-with-args 'org-cycle-hook 'all))
  4067. (t
  4068. ;; Default action: go to overview
  4069. (org-overview)
  4070. (message "OVERVIEW")
  4071. (setq org-cycle-global-status 'overview)
  4072. (run-hook-with-args 'org-cycle-hook 'overview))))
  4073. ((and org-drawers
  4074. (save-excursion
  4075. (beginning-of-line 1)
  4076. (looking-at org-drawer-regexp)))
  4077. ;; Toggle block visibility
  4078. (org-flag-drawer
  4079. (not (get-char-property (match-end 0) 'invisible))))
  4080. ((integerp arg)
  4081. ;; Show-subtree, ARG levels up from here.
  4082. (save-excursion
  4083. (org-back-to-heading)
  4084. (outline-up-heading (if (< arg 0) (- arg)
  4085. (- (funcall outline-level) arg)))
  4086. (org-show-subtree)))
  4087. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4088. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4089. ;; At a heading: rotate between three different views
  4090. (org-back-to-heading)
  4091. (let ((goal-column 0) eoh eol eos)
  4092. ;; First, some boundaries
  4093. (save-excursion
  4094. (org-back-to-heading)
  4095. (save-excursion
  4096. (beginning-of-line 2)
  4097. (while (and (not (eobp)) ;; this is like `next-line'
  4098. (get-char-property (1- (point)) 'invisible))
  4099. (beginning-of-line 2)) (setq eol (point)))
  4100. (outline-end-of-heading) (setq eoh (point))
  4101. (org-end-of-subtree t)
  4102. (unless (eobp)
  4103. (skip-chars-forward " \t\n")
  4104. (beginning-of-line 1) ; in case this is an item
  4105. )
  4106. (setq eos (1- (point))))
  4107. ;; Find out what to do next and set `this-command'
  4108. (cond
  4109. ((= eos eoh)
  4110. ;; Nothing is hidden behind this heading
  4111. (message "EMPTY ENTRY")
  4112. (setq org-cycle-subtree-status nil)
  4113. (save-excursion
  4114. (goto-char eos)
  4115. (outline-next-heading)
  4116. (if (org-invisible-p) (org-flag-heading nil))))
  4117. ((>= eol eos)
  4118. ;; Entire subtree is hidden in one line: open it
  4119. (org-show-entry)
  4120. (show-children)
  4121. (message "CHILDREN")
  4122. (save-excursion
  4123. (goto-char eos)
  4124. (outline-next-heading)
  4125. (if (org-invisible-p) (org-flag-heading nil)))
  4126. (setq org-cycle-subtree-status 'children)
  4127. (run-hook-with-args 'org-cycle-hook 'children))
  4128. ((and (eq last-command this-command)
  4129. (eq org-cycle-subtree-status 'children))
  4130. ;; We just showed the children, now show everything.
  4131. (org-show-subtree)
  4132. (message "SUBTREE")
  4133. (setq org-cycle-subtree-status 'subtree)
  4134. (run-hook-with-args 'org-cycle-hook 'subtree))
  4135. (t
  4136. ;; Default action: hide the subtree.
  4137. (hide-subtree)
  4138. (message "FOLDED")
  4139. (setq org-cycle-subtree-status 'folded)
  4140. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4141. ;; TAB emulation
  4142. (buffer-read-only (org-back-to-heading))
  4143. ((org-try-cdlatex-tab))
  4144. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4145. (or (not (bolp))
  4146. (not (looking-at outline-regexp))))
  4147. (call-interactively (global-key-binding "\t")))
  4148. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4149. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4150. (or (and (eq org-cycle-emulate-tab 'white)
  4151. (= (match-end 0) (point-at-eol)))
  4152. (and (eq org-cycle-emulate-tab 'whitestart)
  4153. (>= (match-end 0) pos))))
  4154. t
  4155. (eq org-cycle-emulate-tab t))
  4156. (if (and (looking-at "[ \n\r\t]")
  4157. (string-match "^[ \t]*$" (buffer-substring
  4158. (point-at-bol) (point))))
  4159. (progn
  4160. (beginning-of-line 1)
  4161. (and (looking-at "[ \t]+") (replace-match ""))))
  4162. (call-interactively (global-key-binding "\t")))
  4163. (t (save-excursion
  4164. (org-back-to-heading)
  4165. (org-cycle))))))
  4166. ;;;###autoload
  4167. (defun org-global-cycle (&optional arg)
  4168. "Cycle the global visibility. For details see `org-cycle'."
  4169. (interactive "P")
  4170. (let ((org-cycle-include-plain-lists
  4171. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4172. (if (integerp arg)
  4173. (progn
  4174. (show-all)
  4175. (hide-sublevels arg)
  4176. (setq org-cycle-global-status 'contents))
  4177. (org-cycle '(4)))))
  4178. (defun org-overview ()
  4179. "Switch to overview mode, shoing only top-level headlines.
  4180. Really, this shows all headlines with level equal or greater than the level
  4181. of the first headline in the buffer. This is important, because if the
  4182. first headline is not level one, then (hide-sublevels 1) gives confusing
  4183. results."
  4184. (interactive)
  4185. (let ((level (save-excursion
  4186. (goto-char (point-min))
  4187. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4188. (progn
  4189. (goto-char (match-beginning 0))
  4190. (funcall outline-level))))))
  4191. (and level (hide-sublevels level))))
  4192. (defun org-content (&optional arg)
  4193. "Show all headlines in the buffer, like a table of contents.
  4194. With numerical argument N, show content up to level N."
  4195. (interactive "P")
  4196. (save-excursion
  4197. ;; Visit all headings and show their offspring
  4198. (and (integerp arg) (org-overview))
  4199. (goto-char (point-max))
  4200. (catch 'exit
  4201. (while (and (progn (condition-case nil
  4202. (outline-previous-visible-heading 1)
  4203. (error (goto-char (point-min))))
  4204. t)
  4205. (looking-at outline-regexp))
  4206. (if (integerp arg)
  4207. (show-children (1- arg))
  4208. (show-branches))
  4209. (if (bobp) (throw 'exit nil))))))
  4210. (defun org-optimize-window-after-visibility-change (state)
  4211. "Adjust the window after a change in outline visibility.
  4212. This function is the default value of the hook `org-cycle-hook'."
  4213. (when (get-buffer-window (current-buffer))
  4214. (cond
  4215. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4216. ; ((eq state 'overview) (org-beginning-of-line))
  4217. ((eq state 'content) nil)
  4218. ((eq state 'all) nil)
  4219. ((eq state 'folded) nil)
  4220. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4221. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4222. (defun org-cycle-show-empty-lines (state)
  4223. "Show empty lines above all visible headlines.
  4224. The region to be covered depends on STATE when called through
  4225. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4226. entire buffer covered. Note that an empty line is only shown if there
  4227. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4228. (when (> org-cycle-separator-lines 0)
  4229. (save-excursion
  4230. (let* ((n org-cycle-separator-lines)
  4231. (re (cond
  4232. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4233. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4234. (t (let ((ns (number-to-string (- n 2))))
  4235. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4236. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4237. beg end)
  4238. (cond
  4239. ((memq state '(overview contents t))
  4240. (setq beg (point-min) end (point-max)))
  4241. ((memq state '(children folded))
  4242. (setq beg (point) end (progn (org-end-of-subtree t t)
  4243. (beginning-of-line 2)
  4244. (point)))))
  4245. (when beg
  4246. (goto-char beg)
  4247. (while (re-search-forward re end t)
  4248. (if (not (get-char-property (match-end 1) 'invisible))
  4249. (outline-flag-region
  4250. (match-beginning 1) (match-end 1) nil)))))))
  4251. ;; Never hide empty lines at the end of the file.
  4252. (save-excursion
  4253. (goto-char (point-max))
  4254. (outline-previous-heading)
  4255. (outline-end-of-heading)
  4256. (if (and (looking-at "[ \t\n]+")
  4257. (= (match-end 0) (point-max)))
  4258. (outline-flag-region (point) (match-end 0) nil))))
  4259. (defun org-subtree-end-visible-p ()
  4260. "Is the end of the current subtree visible?"
  4261. (pos-visible-in-window-p
  4262. (save-excursion (org-end-of-subtree t) (point))))
  4263. (defun org-first-headline-recenter (&optional N)
  4264. "Move cursor to the first headline and recenter the headline.
  4265. Optional argument N means, put the headline into the Nth line of the window."
  4266. (goto-char (point-min))
  4267. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4268. (beginning-of-line)
  4269. (recenter (prefix-numeric-value N))))
  4270. ;;; Org-goto
  4271. (defvar org-goto-window-configuration nil)
  4272. (defvar org-goto-marker nil)
  4273. (defvar org-goto-map (make-sparse-keymap))
  4274. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  4275. (while (setq cmd (pop cmds))
  4276. (substitute-key-definition cmd cmd org-goto-map global-map)))
  4277. (org-defkey org-goto-map "\C-m" 'org-goto-ret)
  4278. (org-defkey org-goto-map [(left)] 'org-goto-left)
  4279. (org-defkey org-goto-map [(right)] 'org-goto-right)
  4280. (org-defkey org-goto-map [(?q)] 'org-goto-quit)
  4281. (org-defkey org-goto-map [(control ?g)] 'org-goto-quit)
  4282. (org-defkey org-goto-map "\C-i" 'org-cycle)
  4283. (org-defkey org-goto-map [(tab)] 'org-cycle)
  4284. (org-defkey org-goto-map [(down)] 'outline-next-visible-heading)
  4285. (org-defkey org-goto-map [(up)] 'outline-previous-visible-heading)
  4286. (org-defkey org-goto-map "n" 'outline-next-visible-heading)
  4287. (org-defkey org-goto-map "p" 'outline-previous-visible-heading)
  4288. (org-defkey org-goto-map "f" 'outline-forward-same-level)
  4289. (org-defkey org-goto-map "b" 'outline-backward-same-level)
  4290. (org-defkey org-goto-map "u" 'outline-up-heading)
  4291. (org-defkey org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  4292. (org-defkey org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  4293. (org-defkey org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  4294. (org-defkey org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  4295. (org-defkey org-goto-map "\C-c\C-u" 'outline-up-heading)
  4296. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4297. (while l (org-defkey org-goto-map (int-to-string (pop l)) 'digit-argument)))
  4298. (defconst org-goto-help
  4299. "Select a location to jump to, press RET
  4300. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  4301. (defun org-goto ()
  4302. "Go to a different location of the document, keeping current visibility.
  4303. When you want to go to a different location in a document, the fastest way
  4304. is often to fold the entire buffer and then dive into the tree. This
  4305. method has the disadvantage, that the previous location will be folded,
  4306. which may not be what you want.
  4307. This command works around this by showing a copy of the current buffer in
  4308. overview mode. You can dive into the tree in that copy, to find the
  4309. location you want to reach. When pressing RET, the command returns to the
  4310. original buffer in which the visibility is still unchanged. It then jumps
  4311. to the new location, making it and the headline hierarchy above it visible."
  4312. (interactive)
  4313. (let* ((org-goto-start-pos (point))
  4314. (selected-point
  4315. (org-get-location (current-buffer) org-goto-help)))
  4316. (if selected-point
  4317. (progn
  4318. (org-mark-ring-push org-goto-start-pos)
  4319. (goto-char selected-point)
  4320. (if (or (org-invisible-p) (org-invisible-p2))
  4321. (org-show-context 'org-goto)))
  4322. (error "Quit"))))
  4323. (defvar org-selected-point nil) ; dynamically scoped parameter
  4324. (defun org-get-location (buf help)
  4325. "Let the user select a location in the Org-mode buffer BUF.
  4326. This function uses a recursive edit. It returns the selected position
  4327. or nil."
  4328. (let (org-selected-point)
  4329. (save-excursion
  4330. (save-window-excursion
  4331. (delete-other-windows)
  4332. (switch-to-buffer (get-buffer-create "*org-goto*"))
  4333. (with-output-to-temp-buffer "*Help*"
  4334. (princ help))
  4335. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4336. (setq buffer-read-only nil)
  4337. (erase-buffer)
  4338. (insert-buffer-substring buf)
  4339. (let ((org-startup-truncated t)
  4340. (org-startup-folded t)
  4341. (org-startup-align-all-tables nil))
  4342. (org-mode))
  4343. (setq buffer-read-only t)
  4344. (if (and (boundp 'org-goto-start-pos)
  4345. (integer-or-marker-p org-goto-start-pos))
  4346. (let ((org-show-hierarchy-above t)
  4347. (org-show-siblings t)
  4348. (org-show-following-heading t))
  4349. (goto-char org-goto-start-pos)
  4350. (and (org-invisible-p) (org-show-context)))
  4351. (goto-char (point-min)))
  4352. (org-beginning-of-line)
  4353. (message "Select location and press RET")
  4354. ;; now we make sure that during selection, ony very few keys work
  4355. ;; and that it is impossible to switch to another window.
  4356. (let ((gm (current-global-map))
  4357. (overriding-local-map org-goto-map))
  4358. (unwind-protect
  4359. (progn
  4360. (use-global-map org-goto-map)
  4361. (recursive-edit))
  4362. (use-global-map gm)))))
  4363. (kill-buffer "*org-goto*")
  4364. org-selected-point))
  4365. (defun org-goto-ret (&optional arg)
  4366. "Finish `org-goto' by going to the new location."
  4367. (interactive "P")
  4368. (setq org-selected-point (point)
  4369. current-prefix-arg arg)
  4370. (throw 'exit nil))
  4371. (defun org-goto-left ()
  4372. "Finish `org-goto' by going to the new location."
  4373. (interactive)
  4374. (if (org-on-heading-p)
  4375. (progn
  4376. (beginning-of-line 1)
  4377. (setq org-selected-point (point)
  4378. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4379. (throw 'exit nil))
  4380. (error "Not on a heading")))
  4381. (defun org-goto-right ()
  4382. "Finish `org-goto' by going to the new location."
  4383. (interactive)
  4384. (if (org-on-heading-p)
  4385. (progn
  4386. (outline-end-of-subtree)
  4387. (or (eobp) (forward-char 1))
  4388. (setq org-selected-point (point)
  4389. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4390. (throw 'exit nil))
  4391. (error "Not on a heading")))
  4392. (defun org-goto-quit ()
  4393. "Finish `org-goto' without cursor motion."
  4394. (interactive)
  4395. (setq org-selected-point nil)
  4396. (throw 'exit nil))
  4397. ;;; Indirect buffer display of subtrees
  4398. (defvar org-indirect-dedicated-frame nil
  4399. "This is the frame being used for indirect tree display.")
  4400. (defvar org-last-indirect-buffer nil)
  4401. (defun org-tree-to-indirect-buffer (&optional arg)
  4402. "Create indirect buffer and narrow it to current subtree.
  4403. With numerical prefix ARG, go up to this level and then take that tree.
  4404. If ARG is negative, go up that many levels.
  4405. Normally this command removes the indirect buffer previously made
  4406. with this command. However, when called with a C-u prefix, the last buffer
  4407. is kept so that you can work with several indirect buffers at the same time.
  4408. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4409. requests that a new frame be made for the new buffer, so that the dedicated
  4410. frame is not changed."
  4411. (interactive "P")
  4412. (let ((cbuf (current-buffer))
  4413. (cwin (selected-window))
  4414. (pos (point))
  4415. beg end level heading ibuf)
  4416. (save-excursion
  4417. (org-back-to-heading t)
  4418. (when (numberp arg)
  4419. (setq level (org-outline-level))
  4420. (if (< arg 0) (setq arg (+ level arg)))
  4421. (while (> (setq level (org-outline-level)) arg)
  4422. (outline-up-heading 1 t)))
  4423. (setq beg (point)
  4424. heading (org-get-heading))
  4425. (org-end-of-subtree t) (setq end (point)))
  4426. (if (and (not arg)
  4427. (buffer-live-p org-last-indirect-buffer))
  4428. (kill-buffer org-last-indirect-buffer))
  4429. (setq ibuf (org-get-indirect-buffer cbuf)
  4430. org-last-indirect-buffer ibuf)
  4431. (cond
  4432. ((or (eq org-indirect-buffer-display 'new-frame)
  4433. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4434. (select-frame (make-frame))
  4435. (delete-other-windows)
  4436. (switch-to-buffer ibuf)
  4437. (org-set-frame-title heading))
  4438. ((eq org-indirect-buffer-display 'dedicated-frame)
  4439. (raise-frame
  4440. (select-frame (or (and org-indirect-dedicated-frame
  4441. (frame-live-p org-indirect-dedicated-frame)
  4442. org-indirect-dedicated-frame)
  4443. (setq org-indirect-dedicated-frame (make-frame)))))
  4444. (delete-other-windows)
  4445. (switch-to-buffer ibuf)
  4446. (org-set-frame-title (concat "Indirect: " heading)))
  4447. ((eq org-indirect-buffer-display 'current-window)
  4448. (switch-to-buffer ibuf))
  4449. ((eq org-indirect-buffer-display 'other-window)
  4450. (pop-to-buffer ibuf))
  4451. (t (error "Invalid value.")))
  4452. (if (featurep 'xemacs)
  4453. (save-excursion (org-mode) (turn-on-font-lock)))
  4454. (narrow-to-region beg end)
  4455. (show-all)
  4456. (goto-char pos)
  4457. (and (window-live-p cwin) (select-window cwin))))
  4458. (defun org-get-indirect-buffer (&optional buffer)
  4459. (setq buffer (or buffer (current-buffer)))
  4460. (let ((n 1) (base (buffer-name buffer)) bname)
  4461. (while (buffer-live-p
  4462. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4463. (setq n (1+ n)))
  4464. (condition-case nil
  4465. (make-indirect-buffer buffer bname 'clone)
  4466. (error (make-indirect-buffer buffer bname)))))
  4467. (defun org-set-frame-title (title)
  4468. "Set the title of the current frame to the string TITLE."
  4469. ;; FIXME: how to name a single frame in XEmacs???
  4470. (unless (featurep 'xemacs)
  4471. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4472. ;;;; Structure editing
  4473. ;;; Inserting headlines
  4474. (defun org-insert-heading (&optional force-heading)
  4475. "Insert a new heading or item with same depth at point.
  4476. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4477. If point is at the beginning of a headline, insert a sibling before the
  4478. current headline. If point is in the middle of a headline, split the headline
  4479. at that position and make the rest of the headline part of the sibling below
  4480. the current headline."
  4481. (interactive "P")
  4482. (if (= (buffer-size) 0)
  4483. (insert "\n* ")
  4484. (when (or force-heading (not (org-insert-item)))
  4485. (let* ((head (save-excursion
  4486. (condition-case nil
  4487. (progn
  4488. (org-back-to-heading)
  4489. (match-string 0))
  4490. (error "*"))))
  4491. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4492. pos)
  4493. (cond
  4494. ((and (org-on-heading-p) (bolp)
  4495. (or (bobp)
  4496. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4497. (open-line (if blank 2 1)))
  4498. ((and (bolp)
  4499. (or (bobp)
  4500. (save-excursion
  4501. (backward-char 1) (not (org-invisible-p)))))
  4502. nil)
  4503. (t (newline (if blank 2 1))))
  4504. (insert head) (just-one-space)
  4505. (setq pos (point))
  4506. (end-of-line 1)
  4507. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4508. (run-hooks 'org-insert-heading-hook)))))
  4509. (defun org-insert-todo-heading (arg)
  4510. "Insert a new heading with the same level and TODO state as current heading.
  4511. If the heading has no TODO state, or if the state is DONE, use the first
  4512. state (TODO by default). Also with prefix arg, force first state."
  4513. (interactive "P")
  4514. (when (not (org-insert-item 'checkbox))
  4515. (org-insert-heading)
  4516. (save-excursion
  4517. (org-back-to-heading)
  4518. (outline-previous-heading)
  4519. (looking-at org-todo-line-regexp))
  4520. (if (or arg
  4521. (not (match-beginning 2))
  4522. (member (match-string 2) org-done-keywords))
  4523. (insert (car org-todo-keywords-1) " ")
  4524. (insert (match-string 2) " "))))
  4525. ;;; Promotion and Demotion
  4526. (defun org-promote-subtree ()
  4527. "Promote the entire subtree.
  4528. See also `org-promote'."
  4529. (interactive)
  4530. (save-excursion
  4531. (org-map-tree 'org-promote))
  4532. (org-fix-position-after-promote))
  4533. (defun org-demote-subtree ()
  4534. "Demote the entire subtree. See `org-demote'.
  4535. See also `org-promote'."
  4536. (interactive)
  4537. (save-excursion
  4538. (org-map-tree 'org-demote))
  4539. (org-fix-position-after-promote))
  4540. (defun org-do-promote ()
  4541. "Promote the current heading higher up the tree.
  4542. If the region is active in `transient-mark-mode', promote all headings
  4543. in the region."
  4544. (interactive)
  4545. (save-excursion
  4546. (if (org-region-active-p)
  4547. (org-map-region 'org-promote (region-beginning) (region-end))
  4548. (org-promote)))
  4549. (org-fix-position-after-promote))
  4550. (defun org-do-demote ()
  4551. "Demote the current heading lower down the tree.
  4552. If the region is active in `transient-mark-mode', demote all headings
  4553. in the region."
  4554. (interactive)
  4555. (save-excursion
  4556. (if (org-region-active-p)
  4557. (org-map-region 'org-demote (region-beginning) (region-end))
  4558. (org-demote)))
  4559. (org-fix-position-after-promote))
  4560. (defun org-fix-position-after-promote ()
  4561. "Make sure that after pro/demotion cursor position is right."
  4562. (let ((pos (point)))
  4563. (when (save-excursion
  4564. (beginning-of-line 1)
  4565. (looking-at org-todo-line-regexp)
  4566. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4567. (cond ((eobp) (insert " "))
  4568. ((eolp) (insert " "))
  4569. ((equal (char-after) ?\ ) (forward-char 1))))))
  4570. (defun org-reduced-level (l)
  4571. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4572. (defun org-get-legal-level (level &optional change)
  4573. "Rectify a level change under the influence of `org-odd-levels-only'
  4574. LEVEL is a current level, CHANGE is by how much the level should be
  4575. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4576. even level numbers will become the next higher odd number."
  4577. (if org-odd-levels-only
  4578. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4579. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4580. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4581. (max 1 (+ level change))))
  4582. (defun org-promote ()
  4583. "Promote the current heading higher up the tree.
  4584. If the region is active in `transient-mark-mode', promote all headings
  4585. in the region."
  4586. (org-back-to-heading t)
  4587. (let* ((level (save-match-data (funcall outline-level)))
  4588. (up-head (make-string (org-get-legal-level level -1) ?*))
  4589. (diff (abs (- level (length up-head)))))
  4590. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4591. (replace-match up-head nil t)
  4592. ;; Fixup tag positioning
  4593. (and org-auto-align-tags (org-set-tags nil t))
  4594. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4595. (defun org-demote ()
  4596. "Demote the current heading lower down the tree.
  4597. If the region is active in `transient-mark-mode', demote all headings
  4598. in the region."
  4599. (org-back-to-heading t)
  4600. (let* ((level (save-match-data (funcall outline-level)))
  4601. (down-head (make-string (org-get-legal-level level 1) ?*))
  4602. (diff (abs (- level (length down-head)))))
  4603. (replace-match down-head nil t)
  4604. ;; Fixup tag positioning
  4605. (and org-auto-align-tags (org-set-tags nil t))
  4606. (if org-adapt-indentation (org-fixup-indentation diff))))
  4607. (defun org-map-tree (fun)
  4608. "Call FUN for every heading underneath the current one."
  4609. (org-back-to-heading)
  4610. (let ((level (funcall outline-level)))
  4611. (save-excursion
  4612. (funcall fun)
  4613. (while (and (progn
  4614. (outline-next-heading)
  4615. (> (funcall outline-level) level))
  4616. (not (eobp)))
  4617. (funcall fun)))))
  4618. (defun org-map-region (fun beg end)
  4619. "Call FUN for every heading between BEG and END."
  4620. (let ((org-ignore-region t))
  4621. (save-excursion
  4622. (setq end (copy-marker end))
  4623. (goto-char beg)
  4624. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4625. (< (point) end))
  4626. (funcall fun))
  4627. (while (and (progn
  4628. (outline-next-heading)
  4629. (< (point) end))
  4630. (not (eobp)))
  4631. (funcall fun)))))
  4632. (defun org-fixup-indentation (diff)
  4633. "Change the indentation in the current entry by DIFF
  4634. However, if any line in the current entry has no indentation, or if it
  4635. would end up with no indentation after the change, nothing at all is done."
  4636. (save-excursion
  4637. (let ((end (save-excursion (outline-next-heading)
  4638. (point-marker)))
  4639. (prohibit (if (> diff 0)
  4640. "^\\S-"
  4641. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4642. col)
  4643. (unless (save-excursion (re-search-forward prohibit end t))
  4644. (while (re-search-forward "^[ \t]+" end t)
  4645. (goto-char (match-end 0))
  4646. (setq col (current-column))
  4647. (if (< diff 0) (replace-match ""))
  4648. (indent-to (+ diff col))))
  4649. (move-marker end nil))))
  4650. (defun org-convert-to-odd-levels ()
  4651. "Convert an org-mode file with all levels allowed to one with odd levels.
  4652. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4653. level 5 etc."
  4654. (interactive)
  4655. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4656. (let ((org-odd-levels-only nil) n)
  4657. (save-excursion
  4658. (goto-char (point-min))
  4659. (while (re-search-forward "^\\*\\*+" nil t)
  4660. (setq n (1- (length (match-string 0))))
  4661. (while (>= (setq n (1- n)) 0)
  4662. (org-demote))
  4663. (end-of-line 1))))))
  4664. (defun org-convert-to-oddeven-levels ()
  4665. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4666. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4667. section with an even level, conversion would destroy the structure of the file. An error
  4668. is signaled in this case."
  4669. (interactive)
  4670. (goto-char (point-min))
  4671. ;; First check if there are no even levels
  4672. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  4673. (org-show-context t)
  4674. (error "Not all levels are odd in this file. Conversion not possible."))
  4675. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4676. (let ((org-odd-levels-only nil) n)
  4677. (save-excursion
  4678. (goto-char (point-min))
  4679. (while (re-search-forward "^\\*\\*+" nil t)
  4680. (setq n (/ (length (match-string 0)) 2))
  4681. (while (>= (setq n (1- n)) 0)
  4682. (org-promote))
  4683. (end-of-line 1))))))
  4684. (defun org-tr-level (n)
  4685. "Make N odd if required."
  4686. (if org-odd-levels-only (1+ (/ n 2)) n))
  4687. ;;; Vertical tree motion, cutting and pasting of subtrees
  4688. (defun org-move-subtree-up (&optional arg)
  4689. "Move the current subtree up past ARG headlines of the same level."
  4690. (interactive "p")
  4691. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4692. (defun org-move-subtree-down (&optional arg)
  4693. "Move the current subtree down past ARG headlines of the same level."
  4694. (interactive "p")
  4695. (setq arg (prefix-numeric-value arg))
  4696. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4697. 'outline-get-last-sibling))
  4698. (ins-point (make-marker))
  4699. (cnt (abs arg))
  4700. beg end txt folded)
  4701. ;; Select the tree
  4702. (org-back-to-heading)
  4703. (setq beg (point))
  4704. (save-match-data
  4705. (save-excursion (outline-end-of-heading)
  4706. (setq folded (org-invisible-p)))
  4707. (outline-end-of-subtree))
  4708. (outline-next-heading)
  4709. (setq end (point))
  4710. ;; Find insertion point, with error handling
  4711. (goto-char beg)
  4712. (while (> cnt 0)
  4713. (or (and (funcall movfunc) (looking-at outline-regexp))
  4714. (progn (goto-char beg)
  4715. (error "Cannot move past superior level or buffer limit")))
  4716. (setq cnt (1- cnt)))
  4717. (if (> arg 0)
  4718. ;; Moving forward - still need to move over subtree
  4719. (progn (outline-end-of-subtree)
  4720. (outline-next-heading)
  4721. (if (not (or (looking-at (concat "^" outline-regexp))
  4722. (bolp)))
  4723. (newline))))
  4724. (move-marker ins-point (point))
  4725. (setq txt (buffer-substring beg end))
  4726. (delete-region beg end)
  4727. (insert txt)
  4728. (or (bolp) (insert "\n"))
  4729. (goto-char ins-point)
  4730. (if folded (hide-subtree))
  4731. (move-marker ins-point nil)))
  4732. (defvar org-subtree-clip ""
  4733. "Clipboard for cut and paste of subtrees.
  4734. This is actually only a copy of the kill, because we use the normal kill
  4735. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4736. (defvar org-subtree-clip-folded nil
  4737. "Was the last copied subtree folded?
  4738. This is used to fold the tree back after pasting.")
  4739. (defun org-cut-subtree ()
  4740. "Cut the current subtree into the clipboard.
  4741. This is a short-hand for marking the subtree and then cutting it."
  4742. (interactive)
  4743. (org-copy-subtree 'cut))
  4744. (defun org-copy-subtree (&optional cut)
  4745. "Cut the current subtree into the clipboard.
  4746. This is a short-hand for marking the subtree and then copying it.
  4747. If CUT is non-nil, actually cut the subtree."
  4748. (interactive)
  4749. (let (beg end folded)
  4750. (if (interactive-p)
  4751. (org-back-to-heading nil) ; take what looks like a subtree
  4752. (org-back-to-heading t)) ; take what is really there
  4753. (setq beg (point))
  4754. (save-match-data
  4755. (save-excursion (outline-end-of-heading)
  4756. (setq folded (org-invisible-p)))
  4757. (outline-end-of-subtree))
  4758. (if (equal (char-after) ?\n) (forward-char 1))
  4759. (setq end (point))
  4760. (goto-char beg)
  4761. (when (> end beg)
  4762. (setq org-subtree-clip-folded folded)
  4763. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4764. (setq org-subtree-clip (current-kill 0))
  4765. (message "%s: Subtree with %d characters"
  4766. (if cut "Cut" "Copied")
  4767. (length org-subtree-clip)))))
  4768. (defun org-paste-subtree (&optional level tree)
  4769. "Paste the clipboard as a subtree, with modification of headline level.
  4770. The entire subtree is promoted or demoted in order to match a new headline
  4771. level. By default, the new level is derived from the visible headings
  4772. before and after the insertion point, and taken to be the inferior headline
  4773. level of the two. So if the previous visible heading is level 3 and the
  4774. next is level 4 (or vice versa), level 4 will be used for insertion.
  4775. This makes sure that the subtree remains an independent subtree and does
  4776. not swallow low level entries.
  4777. You can also force a different level, either by using a numeric prefix
  4778. argument, or by inserting the heading marker by hand. For example, if the
  4779. cursor is after \"*****\", then the tree will be shifted to level 5.
  4780. If you want to insert the tree as is, just use \\[yank].
  4781. If optional TREE is given, use this text instead of the kill ring."
  4782. (interactive "P")
  4783. (unless (org-kill-is-subtree-p tree)
  4784. (error
  4785. (substitute-command-keys
  4786. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4787. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4788. (^re (concat "^\\(" outline-regexp "\\)"))
  4789. (re (concat "\\(" outline-regexp "\\)"))
  4790. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  4791. (old-level (if (string-match ^re txt)
  4792. (- (match-end 0) (match-beginning 0))
  4793. -1))
  4794. (force-level (cond (level (prefix-numeric-value level))
  4795. ((string-match
  4796. ^re_ (buffer-substring (point-at-bol) (point)))
  4797. (- (match-end 0) (match-beginning 0)))
  4798. (t nil)))
  4799. (previous-level (save-excursion
  4800. (condition-case nil
  4801. (progn
  4802. (outline-previous-visible-heading 1)
  4803. (if (looking-at re)
  4804. (- (match-end 0) (match-beginning 0))
  4805. 1))
  4806. (error 1))))
  4807. (next-level (save-excursion
  4808. (condition-case nil
  4809. (progn
  4810. (outline-next-visible-heading 1)
  4811. (if (looking-at re)
  4812. (- (match-end 0) (match-beginning 0))
  4813. 1))
  4814. (error 1))))
  4815. (new-level (or force-level (max previous-level next-level)))
  4816. (shift (if (or (= old-level -1)
  4817. (= new-level -1)
  4818. (= old-level new-level))
  4819. 0
  4820. (- new-level old-level)))
  4821. (shift1 shift)
  4822. (delta (if (> shift 0) -1 1))
  4823. (func (if (> shift 0) 'org-demote 'org-promote))
  4824. (org-odd-levels-only nil)
  4825. beg end)
  4826. ;; Remove the forces level indicator
  4827. (if force-level
  4828. (delete-region (point-at-bol) (point)))
  4829. ;; Make sure we start at the beginning of an empty line
  4830. (if (not (bolp)) (insert "\n"))
  4831. (if (not (looking-at "[ \t]*$"))
  4832. (progn (insert "\n") (backward-char 1)))
  4833. ;; Paste
  4834. (setq beg (point))
  4835. (if (string-match "[ \t\r\n]+\\'" txt)
  4836. (setq txt (replace-match "\n" t t txt)))
  4837. (insert txt)
  4838. (setq end (point))
  4839. (if (looking-at "[ \t\r\n]+")
  4840. (replace-match "\n"))
  4841. (goto-char beg)
  4842. ;; Shift if necessary
  4843. (if (= shift 0)
  4844. (message "Pasted at level %d, without shift" new-level)
  4845. (save-restriction
  4846. (narrow-to-region beg end)
  4847. (while (not (= shift 0))
  4848. (org-map-region func (point-min) (point-max))
  4849. (setq shift (+ delta shift)))
  4850. (goto-char (point-min))
  4851. (message "Pasted at level %d, with shift by %d levels"
  4852. new-level shift1)))
  4853. (if (and kill-ring
  4854. (eq org-subtree-clip (current-kill 0))
  4855. org-subtree-clip-folded)
  4856. ;; The tree was folded before it was killed/copied
  4857. (hide-subtree))))
  4858. (defun org-kill-is-subtree-p (&optional txt)
  4859. "Check if the current kill is an outline subtree, or a set of trees.
  4860. Returns nil if kill does not start with a headline, or if the first
  4861. headline level is not the largest headline level in the tree.
  4862. So this will actually accept several entries of equal levels as well,
  4863. which is OK for `org-paste-subtree'.
  4864. If optional TXT is given, check this string instead of the current kill."
  4865. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4866. (start-level (and kill
  4867. (string-match (concat "\\`" outline-regexp) kill)
  4868. (- (match-end 0) (match-beginning 0))))
  4869. (re (concat "^" outline-regexp))
  4870. (start 1))
  4871. (if (not start-level)
  4872. nil ;; does not even start with a heading
  4873. (catch 'exit
  4874. (while (setq start (string-match re kill (1+ start)))
  4875. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  4876. (throw 'exit nil)))
  4877. t))))
  4878. (defun org-narrow-to-subtree ()
  4879. "Narrow buffer to the current subtree."
  4880. (interactive)
  4881. (save-excursion
  4882. (narrow-to-region
  4883. (progn (org-back-to-heading) (point))
  4884. (progn (org-end-of-subtree t t) (point)))))
  4885. ;;; Outline Sorting
  4886. (defun org-sort (with-case)
  4887. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  4888. (interactive "P")
  4889. (if (org-at-table-p)
  4890. (org-call-with-arg 'org-table-sort-lines with-case)
  4891. (org-call-with-arg 'org-sort-entries with-case)))
  4892. (defun org-sort-entries (&optional with-case sorting-type)
  4893. "Sort entries on a certain level of an outline tree.
  4894. If there is an active region, the entries in the region are sorted.
  4895. Else, if the cursor is before the first entry, sort the top-level items.
  4896. Else, the children of the entry at point are sorted.
  4897. Sorting can be alphabetically, numerically, and by date/time as given by
  4898. the first time stamp in the entry. The command prompts for the sorting
  4899. type unless it has been given to the function through the SORTING-TYPE
  4900. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  4901. Comparing entries ignores case by default. However, with an optional argument
  4902. WITH-CASE, the sorting considers case as well. With two prefix arguments
  4903. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  4904. (interactive "P")
  4905. (let ((unique (equal with-case '(16)))
  4906. start beg end entries stars re re2 p nentries (nremoved 0)
  4907. last txt what)
  4908. ;; Find beginning and end of region to sort
  4909. (cond
  4910. ((org-region-active-p)
  4911. ;; we will sort the region
  4912. (setq end (region-end)
  4913. what "region")
  4914. (goto-char (region-beginning))
  4915. (if (not (org-on-heading-p)) (outline-next-heading))
  4916. (setq start (point)))
  4917. ((or (org-on-heading-p)
  4918. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4919. ;; we will sort the children of the current headline
  4920. (org-back-to-heading)
  4921. (setq start (point) end (org-end-of-subtree) what "children")
  4922. (goto-char start)
  4923. (show-subtree)
  4924. (outline-next-heading))
  4925. (t
  4926. ;; we will sort the top-level entries in this file
  4927. (goto-char (point-min))
  4928. (or (org-on-heading-p) (outline-next-heading))
  4929. (setq start (point) end (point-max) what "top-level")
  4930. (goto-char start)
  4931. (show-all)))
  4932. (setq beg (point))
  4933. (if (>= (point) end) (error "Nothing to sort"))
  4934. (looking-at "\\(\\*+\\)")
  4935. (setq stars (match-string 1)
  4936. re (concat "^" (regexp-quote stars) " +")
  4937. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4938. txt (buffer-substring beg end))
  4939. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4940. (if (and (not (equal stars "*")) (string-match re2 txt))
  4941. (error "Region to sort contains a level above the first entry"))
  4942. ;; Make a list that can be sorted.
  4943. ;; The car is the string for comparison, the cdr is the subtree
  4944. (message "Sorting entries...")
  4945. (setq entries
  4946. (mapcar
  4947. (lambda (x)
  4948. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  4949. (cons (match-string 0 x) x))
  4950. (org-split-string txt re)))
  4951. ;; Sort the list
  4952. (save-excursion
  4953. (goto-char start)
  4954. (setq entries (org-do-sort entries what with-case sorting-type)))
  4955. ;; Delete the old stuff
  4956. (goto-char beg)
  4957. (kill-region beg end)
  4958. (setq nentries (length entries))
  4959. ;; Insert the sorted entries, and remove duplicates if this is required
  4960. (while (setq p (pop entries))
  4961. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  4962. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  4963. (insert stars " " (cdr p))))
  4964. (goto-char start)
  4965. (message "Sorting entries...done (%d entries%s)"
  4966. nentries
  4967. (if unique (format ", %d duplicates removed" nremoved) ""))))
  4968. (defun org-do-sort (table what &optional with-case sorting-type)
  4969. "Sort TABLE of WHAT according to SORTING-TYPE.
  4970. The user will be prompted for the SORTING-TYPE if the call to this
  4971. function does not specify it. WHAT is only for the prompt, to indicate
  4972. what is being sorted. The sorting key will be extracted from
  4973. the car of the elements of the table.
  4974. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4975. (unless sorting-type
  4976. (message
  4977. "Sort %s: [a]lphabetically [n]umerically [t]ime. A/N/T means reversed:"
  4978. what)
  4979. (setq sorting-type (read-char-exclusive)))
  4980. (let ((dcst (downcase sorting-type))
  4981. extractfun comparefun)
  4982. ;; Define the appropriate functions
  4983. (cond
  4984. ((= dcst ?n)
  4985. (setq extractfun 'string-to-number
  4986. comparefun (if (= dcst sorting-type) '< '>)))
  4987. ((= dcst ?a)
  4988. (setq extractfun (if with-case 'identity 'downcase)
  4989. comparefun (if (= dcst sorting-type)
  4990. 'string<
  4991. (lambda (a b) (and (not (string< a b))
  4992. (not (string= a b)))))))
  4993. ((= dcst ?t)
  4994. (setq extractfun
  4995. (lambda (x)
  4996. (if (string-match org-ts-regexp x)
  4997. (time-to-seconds
  4998. (org-time-string-to-time (match-string 0 x)))
  4999. 0))
  5000. comparefun (if (= dcst sorting-type) '< '>)))
  5001. (t (error "Invalid sorting type `%c'" sorting-type)))
  5002. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5003. table)
  5004. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5005. ;;;; Plain list items, including checkboxes
  5006. ;;; Plain list items
  5007. (defun org-at-item-p ()
  5008. "Is point in a line starting a hand-formatted item?"
  5009. (let ((llt org-plain-list-ordered-item-terminator))
  5010. (save-excursion
  5011. (goto-char (point-at-bol))
  5012. (looking-at
  5013. (cond
  5014. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5015. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5016. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5017. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5018. (defun org-in-item-p ()
  5019. "It the cursor inside a plain list item.
  5020. Does not have to be the first line."
  5021. (save-excursion
  5022. (condition-case nil
  5023. (progn
  5024. (org-beginning-of-item)
  5025. (org-at-item-p)
  5026. t)
  5027. (error nil))))
  5028. (defun org-insert-item (&optional checkbox)
  5029. "Insert a new item at the current level.
  5030. Return t when things worked, nil when we are not in an item."
  5031. (when (save-excursion
  5032. (condition-case nil
  5033. (progn
  5034. (org-beginning-of-item)
  5035. (org-at-item-p)
  5036. (if (org-invisible-p) (error "Invisible item"))
  5037. t)
  5038. (error nil)))
  5039. (let* ((bul (match-string 0))
  5040. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5041. (match-end 0)))
  5042. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5043. pos)
  5044. (cond
  5045. ((and (org-at-item-p) (<= (point) eow))
  5046. ;; before the bullet
  5047. (beginning-of-line 1)
  5048. (open-line (if blank 2 1)))
  5049. ((<= (point) eow)
  5050. (beginning-of-line 1))
  5051. (t (newline (if blank 2 1))))
  5052. (insert bul (if checkbox "[ ]" ""))
  5053. (just-one-space)
  5054. (setq pos (point))
  5055. (end-of-line 1)
  5056. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5057. (org-maybe-renumber-ordered-list)
  5058. (and checkbox (org-update-checkbox-count-maybe))
  5059. t))
  5060. ;;; Checkboxes
  5061. (defun org-at-item-checkbox-p ()
  5062. "Is point at a line starting a plain-list item with a checklet?"
  5063. (and (org-at-item-p)
  5064. (save-excursion
  5065. (goto-char (match-end 0))
  5066. (skip-chars-forward " \t")
  5067. (looking-at "\\[[ X]\\]"))))
  5068. (defun org-toggle-checkbox (&optional arg)
  5069. "Toggle the checkbox in the current line."
  5070. (interactive "P")
  5071. (catch 'exit
  5072. (let (beg end status (firstnew 'unknown))
  5073. (cond
  5074. ((org-region-active-p)
  5075. (setq beg (region-beginning) end (region-end)))
  5076. ((org-on-heading-p)
  5077. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5078. ((org-at-item-checkbox-p)
  5079. (save-excursion
  5080. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  5081. (throw 'exit t))
  5082. (t (error "Not at a checkbox or heading, and no active region")))
  5083. (save-excursion
  5084. (goto-char beg)
  5085. (while (< (point) end)
  5086. (when (org-at-item-checkbox-p)
  5087. (setq status (equal (match-string 0) "[X]"))
  5088. (when (eq firstnew 'unknown)
  5089. (setq firstnew (not status)))
  5090. (replace-match
  5091. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5092. (beginning-of-line 2)))))
  5093. (org-update-checkbox-count-maybe))
  5094. (defun org-update-checkbox-count-maybe ()
  5095. "Update checkbox statistics unless turned off by user."
  5096. (when org-provide-checkbox-statistics
  5097. (org-update-checkbox-count)))
  5098. (defun org-update-checkbox-count (&optional all)
  5099. "Update the checkbox statistics in the current section.
  5100. This will find all statistic cookies like [57%] and [6/12] and update them
  5101. with the current numbers. With optional prefix argument ALL, do this for
  5102. the whole buffer."
  5103. (interactive "P")
  5104. (save-excursion
  5105. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5106. (beg (condition-case nil
  5107. (progn (outline-back-to-heading) (point))
  5108. (error (point-min))))
  5109. (end (move-marker (make-marker)
  5110. (progn (outline-next-heading) (point))))
  5111. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5112. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  5113. b1 e1 f1 c-on c-off lim (cstat 0))
  5114. (when all
  5115. (goto-char (point-min))
  5116. (outline-next-heading)
  5117. (setq beg (point) end (point-max)))
  5118. (goto-char beg)
  5119. (while (re-search-forward re end t)
  5120. (setq cstat (1+ cstat)
  5121. b1 (match-beginning 0)
  5122. e1 (match-end 0)
  5123. f1 (match-beginning 1)
  5124. lim (cond
  5125. ((org-on-heading-p) (outline-next-heading) (point))
  5126. ((org-at-item-p) (org-end-of-item) (point))
  5127. (t nil))
  5128. c-on 0 c-off 0)
  5129. (goto-char e1)
  5130. (when lim
  5131. (while (re-search-forward re-box lim t)
  5132. (if (equal (match-string 2) "[ ]")
  5133. (setq c-off (1+ c-off))
  5134. (setq c-on (1+ c-on))))
  5135. (delete-region b1 e1)
  5136. (goto-char b1)
  5137. (insert (if f1
  5138. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5139. (format "[%d/%d]" c-on (+ c-on c-off))))))
  5140. (when (interactive-p)
  5141. (message "Checkbox satistics updated %s (%d places)"
  5142. (if all "in entire file" "in current outline entry") cstat)))))
  5143. (defun org-get-checkbox-statistics-face ()
  5144. "Select the face for checkbox statistics.
  5145. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5146. it will be `org-todo'."
  5147. (if (match-end 1)
  5148. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5149. (if (and (> (match-end 2) (match-beginning 2))
  5150. (equal (match-string 2) (match-string 3)))
  5151. 'org-done
  5152. 'org-todo)))
  5153. (defun org-get-indentation (&optional line)
  5154. "Get the indentation of the current line, interpreting tabs.
  5155. When LINE is given, assume it represents a line and compute its indentation."
  5156. (if line
  5157. (if (string-match "^ *" (org-remove-tabs line))
  5158. (match-end 0))
  5159. (save-excursion
  5160. (beginning-of-line 1)
  5161. (skip-chars-forward " \t")
  5162. (current-column))))
  5163. (defun org-remove-tabs (s &optional width)
  5164. "Replace tabulators in S with spaces.
  5165. Assumes that s is a single line, starting in column 0."
  5166. (setq width (or width tab-width))
  5167. (while (string-match "\t" s)
  5168. (setq s (replace-match
  5169. (make-string
  5170. (- (* width (/ (+ (match-beginning 0) width) width))
  5171. (match-beginning 0)) ?\ )
  5172. t t s)))
  5173. s)
  5174. (defun org-fix-indentation (line ind)
  5175. "Fix indentation in LINE.
  5176. IND is a cons cell with target and minimum indentation.
  5177. If the current indenation in LINE is smaller than the minimum,
  5178. leave it alone. If it is larger than ind, set it to the target."
  5179. (let* ((l (org-remove-tabs line))
  5180. (i (org-get-indentation l))
  5181. (i1 (car ind)) (i2 (cdr ind)))
  5182. (if (>= i i2) (setq l (substring line i2)))
  5183. (if (> i1 0)
  5184. (concat (make-string i1 ?\ ) l)
  5185. l)))
  5186. (defcustom org-empty-line-terminates-plain-lists nil
  5187. "Non-nil means, an empty line ends all plain list levels.
  5188. When nil, empty lines are part of the preceeding item."
  5189. :group 'org-plain-lists
  5190. :type 'boolean)
  5191. (defun org-beginning-of-item ()
  5192. "Go to the beginning of the current hand-formatted item.
  5193. If the cursor is not in an item, throw an error."
  5194. (interactive)
  5195. (let ((pos (point))
  5196. (limit (save-excursion
  5197. (condition-case nil
  5198. (progn
  5199. (org-back-to-heading)
  5200. (beginning-of-line 2) (point))
  5201. (error (point-min)))))
  5202. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5203. ind ind1)
  5204. (if (org-at-item-p)
  5205. (beginning-of-line 1)
  5206. (beginning-of-line 1)
  5207. (skip-chars-forward " \t")
  5208. (setq ind (current-column))
  5209. (if (catch 'exit
  5210. (while t
  5211. (beginning-of-line 0)
  5212. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5213. (if (looking-at "[ \t]*$")
  5214. (setq ind1 ind-empty)
  5215. (skip-chars-forward " \t")
  5216. (setq ind1 (current-column)))
  5217. (if (< ind1 ind)
  5218. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5219. nil
  5220. (goto-char pos)
  5221. (error "Not in an item")))))
  5222. (defun org-end-of-item ()
  5223. "Go to the end of the current hand-formatted item.
  5224. If the cursor is not in an item, throw an error."
  5225. (interactive)
  5226. (let* ((pos (point))
  5227. ind1
  5228. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5229. (limit (save-excursion (outline-next-heading) (point)))
  5230. (ind (save-excursion
  5231. (org-beginning-of-item)
  5232. (skip-chars-forward " \t")
  5233. (current-column)))
  5234. (end (catch 'exit
  5235. (while t
  5236. (beginning-of-line 2)
  5237. (if (eobp) (throw 'exit (point)))
  5238. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5239. (if (looking-at "[ \t]*$")
  5240. (setq ind1 ind-empty)
  5241. (skip-chars-forward " \t")
  5242. (setq ind1 (current-column)))
  5243. (if (<= ind1 ind)
  5244. (throw 'exit (point-at-bol)))))))
  5245. (if end
  5246. (goto-char end)
  5247. (goto-char pos)
  5248. (error "Not in an item"))))
  5249. (defun org-next-item ()
  5250. "Move to the beginning of the next item in the current plain list.
  5251. Error if not at a plain list, or if this is the last item in the list."
  5252. (interactive)
  5253. (let (ind ind1 (pos (point)))
  5254. (org-beginning-of-item)
  5255. (setq ind (org-get-indentation))
  5256. (org-end-of-item)
  5257. (setq ind1 (org-get-indentation))
  5258. (unless (and (org-at-item-p) (= ind ind1))
  5259. (goto-char pos)
  5260. (error "On last item"))))
  5261. (defun org-previous-item ()
  5262. "Move to the beginning of the previous item in the current plain list.
  5263. Error if not at a plain list, or if this is the first item in the list."
  5264. (interactive)
  5265. (let (beg ind ind1 (pos (point)))
  5266. (org-beginning-of-item)
  5267. (setq beg (point))
  5268. (setq ind (org-get-indentation))
  5269. (goto-char beg)
  5270. (catch 'exit
  5271. (while t
  5272. (beginning-of-line 0)
  5273. (if (looking-at "[ \t]*$")
  5274. nil
  5275. (if (<= (setq ind1 (org-get-indentation)) ind)
  5276. (throw 'exit t)))))
  5277. (condition-case nil
  5278. (if (or (not (org-at-item-p))
  5279. (< ind1 (1- ind)))
  5280. (error "")
  5281. (org-beginning-of-item))
  5282. (error (goto-char pos)
  5283. (error "On first item")))))
  5284. (defun org-move-item-down ()
  5285. "Move the plain list item at point down, i.e. swap with following item.
  5286. Subitems (items with larger indentation) are considered part of the item,
  5287. so this really moves item trees."
  5288. (interactive)
  5289. (let (beg end ind ind1 (pos (point)) txt)
  5290. (org-beginning-of-item)
  5291. (setq beg (point))
  5292. (setq ind (org-get-indentation))
  5293. (org-end-of-item)
  5294. (setq end (point))
  5295. (setq ind1 (org-get-indentation))
  5296. (if (and (org-at-item-p) (= ind ind1))
  5297. (progn
  5298. (org-end-of-item)
  5299. (setq txt (buffer-substring beg end))
  5300. (save-excursion
  5301. (delete-region beg end))
  5302. (setq pos (point))
  5303. (insert txt)
  5304. (goto-char pos)
  5305. (org-maybe-renumber-ordered-list))
  5306. (goto-char pos)
  5307. (error "Cannot move this item further down"))))
  5308. (defun org-move-item-up (arg)
  5309. "Move the plain list item at point up, i.e. swap with previous item.
  5310. Subitems (items with larger indentation) are considered part of the item,
  5311. so this really moves item trees."
  5312. (interactive "p")
  5313. (let (beg end ind ind1 (pos (point)) txt)
  5314. (org-beginning-of-item)
  5315. (setq beg (point))
  5316. (setq ind (org-get-indentation))
  5317. (org-end-of-item)
  5318. (setq end (point))
  5319. (goto-char beg)
  5320. (catch 'exit
  5321. (while t
  5322. (beginning-of-line 0)
  5323. (if (looking-at "[ \t]*$")
  5324. (if org-empty-line-terminates-plain-lists
  5325. (progn
  5326. (goto-char pos)
  5327. (error "Cannot move this item further up"))
  5328. nil)
  5329. (if (<= (setq ind1 (org-get-indentation)) ind)
  5330. (throw 'exit t)))))
  5331. (condition-case nil
  5332. (org-beginning-of-item)
  5333. (error (goto-char beg)
  5334. (error "Cannot move this item further up")))
  5335. (setq ind1 (org-get-indentation))
  5336. (if (and (org-at-item-p) (= ind ind1))
  5337. (progn
  5338. (setq txt (buffer-substring beg end))
  5339. (save-excursion
  5340. (delete-region beg end))
  5341. (setq pos (point))
  5342. (insert txt)
  5343. (goto-char pos)
  5344. (org-maybe-renumber-ordered-list))
  5345. (goto-char pos)
  5346. (error "Cannot move this item further up"))))
  5347. (defun org-maybe-renumber-ordered-list ()
  5348. "Renumber the ordered list at point if setup allows it.
  5349. This tests the user option `org-auto-renumber-ordered-lists' before
  5350. doing the renumbering."
  5351. (interactive)
  5352. (when (and org-auto-renumber-ordered-lists
  5353. (org-at-item-p))
  5354. (if (match-beginning 3)
  5355. (org-renumber-ordered-list 1)
  5356. (org-fix-bullet-type 1))))
  5357. (defun org-maybe-renumber-ordered-list-safe ()
  5358. (condition-case nil
  5359. (save-excursion
  5360. (org-maybe-renumber-ordered-list))
  5361. (error nil)))
  5362. (defun org-cycle-list-bullet (&optional which)
  5363. "Cycle through the different itemize/enumerate bullets.
  5364. This cycle the entire list level through the sequence:
  5365. `-' -> `+' -> `*' -> `1.' -> `1)'
  5366. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5367. 0 meand `-', 1 means `+' etc."
  5368. (interactive "P")
  5369. (org-preserve-lc
  5370. (org-beginning-of-item-list)
  5371. (org-at-item-p)
  5372. (beginning-of-line 1)
  5373. (let ((current (match-string 0)) new)
  5374. (setq new (cond
  5375. ((and which (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5376. ((string-match "-" current) "+")
  5377. ((string-match "\\+" current)
  5378. (if (looking-at "\\S-") "1." "*"))
  5379. ((string-match "\\*" current) "1.")
  5380. ((string-match "\\." current) "1)")
  5381. ((string-match ")" current) "-")
  5382. (t (error "This should not happen"))))
  5383. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5384. (org-fix-bullet-type 1)
  5385. (org-maybe-renumber-ordered-list))))
  5386. (defun org-get-string-indentation (s)
  5387. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5388. (let ((n -1) (i 0) (w tab-width) c)
  5389. (catch 'exit
  5390. (while (< (setq n (1+ n)) (length s))
  5391. (setq c (aref s n))
  5392. (cond ((= c ?\ ) (setq i (1+ i)))
  5393. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5394. (t (throw 'exit t)))))
  5395. i))
  5396. (defun org-renumber-ordered-list (arg)
  5397. "Renumber an ordered plain list.
  5398. Cursor needs to be in the first line of an item, the line that starts
  5399. with something like \"1.\" or \"2)\"."
  5400. (interactive "p")
  5401. (unless (and (org-at-item-p)
  5402. (match-beginning 3))
  5403. (error "This is not an ordered list"))
  5404. (let ((line (org-current-line))
  5405. (col (current-column))
  5406. (ind (org-get-string-indentation
  5407. (buffer-substring (point-at-bol) (match-beginning 3))))
  5408. ;; (term (substring (match-string 3) -1))
  5409. ind1 (n (1- arg))
  5410. fmt)
  5411. ;; find where this list begins
  5412. (org-beginning-of-item-list)
  5413. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5414. (setq fmt (concat "%d" (match-string 1)))
  5415. (beginning-of-line 0)
  5416. ;; walk forward and replace these numbers
  5417. (catch 'exit
  5418. (while t
  5419. (catch 'next
  5420. (beginning-of-line 2)
  5421. (if (eobp) (throw 'exit nil))
  5422. (if (looking-at "[ \t]*$") (throw 'next nil))
  5423. (skip-chars-forward " \t") (setq ind1 (current-column))
  5424. (if (> ind1 ind) (throw 'next t))
  5425. (if (< ind1 ind) (throw 'exit t))
  5426. (if (not (org-at-item-p)) (throw 'exit nil))
  5427. (delete-region (match-beginning 2) (match-end 2))
  5428. (goto-char (match-beginning 2))
  5429. (insert (format fmt (setq n (1+ n)))))))
  5430. (goto-line line)
  5431. (move-to-column col)))
  5432. (defun org-fix-bullet-type (arg)
  5433. "Make sure all items in this list have the same bullet."
  5434. (interactive "p")
  5435. (unless (org-at-item-p) (error "This is not a list"))
  5436. (let ((line (org-current-line))
  5437. (col (current-column))
  5438. (ind (current-indentation))
  5439. ind1 bullet)
  5440. ;; find where this list begins
  5441. (org-beginning-of-item-list)
  5442. (beginning-of-line 1)
  5443. ;; find out what the bullet type is
  5444. (looking-at "[ \t]*\\(\\S-+\\)")
  5445. (setq bullet (match-string 1))
  5446. ;; walk forward and replace these numbers
  5447. (beginning-of-line 0)
  5448. (catch 'exit
  5449. (while t
  5450. (catch 'next
  5451. (beginning-of-line 2)
  5452. (if (eobp) (throw 'exit nil))
  5453. (if (looking-at "[ \t]*$") (throw 'next nil))
  5454. (skip-chars-forward " \t") (setq ind1 (current-column))
  5455. (if (> ind1 ind) (throw 'next t))
  5456. (if (< ind1 ind) (throw 'exit t))
  5457. (if (not (org-at-item-p)) (throw 'exit nil))
  5458. (skip-chars-forward " \t")
  5459. (looking-at "\\S-+")
  5460. (replace-match bullet))))
  5461. (goto-line line)
  5462. (move-to-column col)
  5463. (if (string-match "[0-9]" bullet)
  5464. (org-renumber-ordered-list 1))))
  5465. (defun org-beginning-of-item-list ()
  5466. "Go to the beginning of the current item list.
  5467. I.e. to the first item in this list."
  5468. (interactive)
  5469. (org-beginning-of-item)
  5470. (let ((pos (point-at-bol))
  5471. (ind (org-get-indentation))
  5472. ind1)
  5473. ;; find where this list begins
  5474. (catch 'exit
  5475. (while t
  5476. (catch 'next
  5477. (beginning-of-line 0)
  5478. (if (looking-at "[ \t]*$") (throw 'next t))
  5479. (skip-chars-forward " \t") (setq ind1 (current-column))
  5480. (if (or (< ind1 ind)
  5481. (and (= ind1 ind)
  5482. (not (org-at-item-p))))
  5483. (throw 'exit t)
  5484. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5485. (goto-char pos)))
  5486. (defvar org-last-indent-begin-marker (make-marker))
  5487. (defvar org-last-indent-end-marker (make-marker))
  5488. (defun org-outdent-item (arg)
  5489. "Outdent a local list item."
  5490. (interactive "p")
  5491. (org-indent-item (- arg)))
  5492. (defun org-indent-item (arg)
  5493. "Indent a local list item."
  5494. (interactive "p")
  5495. (unless (org-at-item-p)
  5496. (error "Not on an item"))
  5497. (save-excursion
  5498. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5499. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5500. (setq beg org-last-indent-begin-marker
  5501. end org-last-indent-end-marker)
  5502. (org-beginning-of-item)
  5503. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5504. (org-end-of-item)
  5505. (setq end (move-marker org-last-indent-end-marker (point))))
  5506. (goto-char beg)
  5507. (setq tmp (org-item-indent-positions)
  5508. ind (car tmp)
  5509. ind-down (nth 2 tmp)
  5510. ind-up (nth 1 tmp)
  5511. delta (if (> arg 0)
  5512. (if ind-down (- ind-down ind) (+ 2 ind))
  5513. (if ind-up (- ind-up ind) (- ind 2))))
  5514. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5515. (while (< (point) end)
  5516. (beginning-of-line 1)
  5517. (skip-chars-forward " \t") (setq ind1 (current-column))
  5518. (delete-region (point-at-bol) (point))
  5519. (or (eolp) (indent-to-column (+ ind1 delta)))
  5520. (beginning-of-line 2))))
  5521. (org-maybe-renumber-ordered-list-safe)
  5522. (save-excursion
  5523. (beginning-of-line 0)
  5524. (condition-case nil (org-beginning-of-item) (error nil))
  5525. (org-maybe-renumber-ordered-list-safe)))
  5526. (defun org-item-indent-positions ()
  5527. "Assumes cursor in item line. FIXME"
  5528. (let* ((bolpos (point-at-bol))
  5529. (ind (org-get-indentation))
  5530. ind-down ind-up pos)
  5531. (save-excursion
  5532. (org-beginning-of-item-list)
  5533. (skip-chars-backward "\n\r \t")
  5534. (when (org-in-item-p)
  5535. (org-beginning-of-item)
  5536. (setq ind-up (org-get-indentation))))
  5537. (setq pos (point))
  5538. (save-excursion
  5539. (cond
  5540. ((and (condition-case nil (progn (org-previous-item) t)
  5541. (error nil))
  5542. (or (forward-char 1) t)
  5543. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5544. (setq ind-down (org-get-indentation)))
  5545. ((and (goto-char pos)
  5546. (org-at-item-p))
  5547. (goto-char (match-end 0))
  5548. (skip-chars-forward " \t")
  5549. (setq ind-down (current-column)))))
  5550. (list ind ind-up ind-down)))
  5551. ;;; The orgstruct minor mode
  5552. ;; Define a minor mode which can be used in other modes in order to
  5553. ;; integrate the org-mode structure editing commands.
  5554. ;; This is really a hack, because the org-mode structure commands use
  5555. ;; keys which normally belong to the major mode. Here is how it
  5556. ;; works: The minor mode defines all the keys necessary to operate the
  5557. ;; structure commands, but wraps the commands into a function which
  5558. ;; tests if the cursor is currently at a headline or a plain list
  5559. ;; item. If that is the case, the structure command is used,
  5560. ;; temporarily setting many Org-mode variables like regular
  5561. ;; expressions for filling etc. However, when any of those keys is
  5562. ;; used at a different location, function uses `key-binding' to look
  5563. ;; up if the key has an associated command in another currently active
  5564. ;; keymap (minor modes, major mode, global), and executes that
  5565. ;; command. There might be problems if any of the keys is otherwise
  5566. ;; used as a prefix key.
  5567. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5568. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5569. ;; addresses this by checking explicitly for both bindings.
  5570. (defvar orgstruct-mode-map (make-sparse-keymap)
  5571. "Keymap for the minor `org-cdlatex-mode'.")
  5572. ;;;###autoload
  5573. (define-minor-mode orgstruct-mode
  5574. "Toggle the minor more `orgstruct-mode'.
  5575. This mode is for using Org-mode structure commands in other modes.
  5576. The following key behave as if Org-mode was active, if the cursor
  5577. is on a headline, or on a plain list item (both in the definition
  5578. of Org-mode).
  5579. M-up Move entry/item up
  5580. M-down Move entry/item down
  5581. M-left Promote
  5582. M-right Demote
  5583. M-S-up Move entry/item up
  5584. M-S-down Move entry/item down
  5585. M-S-left Promote subtree
  5586. M-S-right Demote subtree
  5587. M-q Fill paragraph and items like in Org-mode
  5588. C-c ^ Sort entries
  5589. C-c - Cycle list bullet
  5590. TAB Cycle item visibility
  5591. M-RET Insert new heading/item
  5592. S-M-RET Insert new TODO heading / Chekbox item
  5593. C-c C-c Set tags / toggle checkbox"
  5594. nil " OrgStruct" nil
  5595. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5596. ;;;###autoload
  5597. (defun turn-on-orgstruct ()
  5598. "Unconditionally turn on `orgstruct-mode'."
  5599. (orgstruct-mode 1))
  5600. (defun orgstruct-error ()
  5601. "Error when there is no default binding for a structure key."
  5602. (interactive)
  5603. (error "This key is has no function outside structure elements"))
  5604. (defvar org-local-vars nil
  5605. "List of local variables, for use by `orgstruct-mode'")
  5606. (defun orgstruct-setup ()
  5607. "Setup orgstruct keymaps."
  5608. (let ((nfunc 0)
  5609. (bindings
  5610. (list
  5611. '([(meta up)] org-metaup)
  5612. '([(meta down)] org-metadown)
  5613. '([(meta left)] org-metaleft)
  5614. '([(meta right)] org-metaright)
  5615. '([(meta shift up)] org-shiftmetaup)
  5616. '([(meta shift down)] org-shiftmetadown)
  5617. '([(meta shift left)] org-shiftmetaleft)
  5618. '([(meta shift right)] org-shiftmetaright)
  5619. '("\M-q" fill-paragraph)
  5620. '("\C-c^" org-sort)
  5621. '("\C-c-" org-cycle-list-bullet)))
  5622. elt key fun cmd)
  5623. (while (setq elt (pop bindings))
  5624. (setq nfunc (1+ nfunc))
  5625. (setq key (org-key (car elt))
  5626. fun (nth 1 elt)
  5627. cmd (orgstruct-make-binding fun nfunc key))
  5628. (org-defkey orgstruct-mode-map key cmd))
  5629. ;; Special treatment needed for TAB and RET
  5630. (org-defkey orgstruct-mode-map [(tab)]
  5631. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5632. (org-defkey orgstruct-mode-map "\C-i"
  5633. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5634. (org-defkey orgstruct-mode-map "\M-\C-m"
  5635. (orgstruct-make-binding 'org-insert-heading 105
  5636. "\M-\C-m" [(meta return)]))
  5637. (org-defkey orgstruct-mode-map [(meta return)]
  5638. (orgstruct-make-binding 'org-insert-heading 106
  5639. [(meta return)] "\M-\C-m"))
  5640. (org-defkey orgstruct-mode-map [(shift meta return)]
  5641. (orgstruct-make-binding 'org-insert-todo-heading 107
  5642. [(meta return)] "\M-\C-m"))
  5643. (org-defkey orgstruct-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  5644. (setq org-local-vars (org-get-local-variables))
  5645. t))
  5646. (defun orgstruct-make-binding (fun n &rest keys)
  5647. "Create a function for binding in the structure minor mode.
  5648. FUN is the command to call inside a table. N is used to create a unique
  5649. command name. KEYS are keys that should be checked in for a command
  5650. to execute outside of tables."
  5651. (eval
  5652. (list 'defun
  5653. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5654. '(arg)
  5655. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5656. "Outside of structure, run the binding of `"
  5657. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5658. "'.")
  5659. '(interactive "p")
  5660. (list 'if
  5661. '(org-context-p 'headline 'item)
  5662. (list 'org-run-like-in-org-mode (list 'quote fun))
  5663. (list 'let '(orgstruct-mode)
  5664. (list 'call-interactively
  5665. (append '(or)
  5666. (mapcar (lambda (k)
  5667. (list 'key-binding k))
  5668. keys)
  5669. '('orgstruct-error))))))))
  5670. (defun org-context-p (&rest contexts)
  5671. "FIXME:"
  5672. (let ((pos (point)))
  5673. (goto-char (point-at-bol))
  5674. (prog1 (or (and (memq 'table contexts)
  5675. (looking-at "[ \t]*|"))
  5676. (and (memq 'headline contexts)
  5677. (looking-at "\\*+"))
  5678. (and (memq 'item contexts)
  5679. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5680. (goto-char pos))))
  5681. (defun org-get-local-variables ()
  5682. "Return a list of all local variables in an org-mode buffer."
  5683. (let (varlist)
  5684. (with-current-buffer (get-buffer-create "*Org tmp*")
  5685. (erase-buffer)
  5686. (org-mode)
  5687. (setq varlist (buffer-local-variables)))
  5688. (kill-buffer "*Org tmp*")
  5689. (delq nil
  5690. (mapcar
  5691. (lambda (x)
  5692. (setq x
  5693. (if (symbolp x)
  5694. (list x)
  5695. (list (car x) (list 'quote (cdr x)))))
  5696. (if (string-match
  5697. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5698. (symbol-name (car x)))
  5699. x nil))
  5700. varlist))))
  5701. (defun org-run-like-in-org-mode (cmd)
  5702. (eval (list 'let org-local-vars
  5703. (list 'call-interactively (list 'quote cmd)))))
  5704. ;;;; Archiving
  5705. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5706. (defun org-archive-subtree (&optional find-done)
  5707. "Move the current subtree to the archive.
  5708. The archive can be a certain top-level heading in the current file, or in
  5709. a different file. The tree will be moved to that location, the subtree
  5710. heading be marked DONE, and the current time will be added.
  5711. When called with prefix argument FIND-DONE, find whole trees without any
  5712. open TODO items and archive them (after getting confirmation from the user).
  5713. If the cursor is not at a headline when this comand is called, try all level
  5714. 1 trees. If the cursor is on a headline, only try the direct children of
  5715. this heading."
  5716. (interactive "P")
  5717. (if find-done
  5718. (org-archive-all-done)
  5719. ;; Save all relevant TODO keyword-relatex variables
  5720. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5721. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5722. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5723. (tr-org-done-keywords org-done-keywords)
  5724. (tr-org-todo-regexp org-todo-regexp)
  5725. (tr-org-todo-line-regexp org-todo-line-regexp)
  5726. (tr-org-odd-levels-only org-odd-levels-only)
  5727. (this-buffer (current-buffer))
  5728. (org-archive-location org-archive-location)
  5729. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5730. file heading buffer level newfile-p)
  5731. ;; Try to find a local archive location
  5732. (save-excursion
  5733. (save-restriction
  5734. (widen)
  5735. (if (or (re-search-backward re nil t) (re-search-forward re nil t))
  5736. (setq org-archive-location (match-string 1)))))
  5737. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5738. (progn
  5739. (setq file (format (match-string 1 org-archive-location)
  5740. (file-name-nondirectory buffer-file-name))
  5741. heading (match-string 2 org-archive-location)))
  5742. (error "Invalid `org-archive-location'"))
  5743. (if (> (length file) 0)
  5744. (setq newfile-p (not (file-exists-p file))
  5745. buffer (find-file-noselect file))
  5746. (setq buffer (current-buffer)))
  5747. (unless buffer
  5748. (error "Cannot access file \"%s\"" file))
  5749. (if (and (> (length heading) 0)
  5750. (string-match "^\\*+" heading))
  5751. (setq level (match-end 0))
  5752. (setq heading nil level 0))
  5753. (save-excursion
  5754. ;; We first only copy, in case something goes wrong
  5755. ;; we need to protect this-command, to avoid kill-region sets it,
  5756. ;; which would lead to duplication of subtrees
  5757. (let (this-command) (org-copy-subtree))
  5758. (set-buffer buffer)
  5759. ;; Enforce org-mode for the archive buffer
  5760. (if (not (org-mode-p))
  5761. ;; Force the mode for future visits.
  5762. (let ((org-insert-mode-line-in-empty-file t)
  5763. (org-inhibit-startup t))
  5764. (call-interactively 'org-mode)))
  5765. (when newfile-p
  5766. (goto-char (point-max))
  5767. (insert (format "\nArchived entries from file %s\n\n"
  5768. (buffer-file-name this-buffer))))
  5769. ;; Force the TODO keywords of the original buffer
  5770. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5771. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5772. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5773. (org-done-keywords tr-org-done-keywords)
  5774. (org-todo-regexp tr-org-todo-regexp)
  5775. (org-todo-line-regexp tr-org-todo-line-regexp)
  5776. (org-odd-levels-only
  5777. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5778. org-odd-levels-only
  5779. tr-org-odd-levels-only)))
  5780. (goto-char (point-min))
  5781. (if heading
  5782. (progn
  5783. (if (re-search-forward
  5784. (concat "\\(^\\|\r\\)"
  5785. (regexp-quote heading)
  5786. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5787. nil t)
  5788. (goto-char (match-end 0))
  5789. ;; Heading not found, just insert it at the end
  5790. (goto-char (point-max))
  5791. (or (bolp) (insert "\n"))
  5792. (insert "\n" heading "\n")
  5793. (end-of-line 0))
  5794. ;; Make the subtree visible
  5795. (show-subtree)
  5796. (org-end-of-subtree t)
  5797. (skip-chars-backward " \t\r\n")
  5798. (and (looking-at "[ \t\r\n]*")
  5799. (replace-match "\n\n")))
  5800. ;; No specific heading, just go to end of file.
  5801. (goto-char (point-max)) (insert "\n"))
  5802. ;; Paste
  5803. (org-paste-subtree (org-get-legal-level level 1))
  5804. ;; Mark the entry as done, i.e. set to last word in org-todo-keywords-1 FIXME: not right anymore!!!!!!!
  5805. (when (and org-archive-mark-done
  5806. (looking-at org-todo-line-regexp)
  5807. (or (not (match-end 3))
  5808. (not (member (match-string 3) org-done-keywords))))
  5809. (let (org-log-done)
  5810. (org-todo (car org-done-keywords))))
  5811. ;; Move cursor to right after the TODO keyword
  5812. (when org-archive-stamp-time
  5813. (org-add-planning-info 'archived (org-current-time)))
  5814. ;; Save the buffer, if it is not the same buffer.
  5815. (if (not (eq this-buffer buffer)) (save-buffer))))
  5816. ;; Here we are back in the original buffer. Everything seems to have
  5817. ;; worked. So now cut the tree and finish up.
  5818. (let (this-command) (org-cut-subtree))
  5819. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5820. (message "Subtree archived %s"
  5821. (if (eq this-buffer buffer)
  5822. (concat "under heading: " heading)
  5823. (concat "in file: " (abbreviate-file-name file)))))))
  5824. (defun org-archive-all-done (&optional tag)
  5825. "Archive sublevels of the current tree without open TODO items.
  5826. If the cursor is not on a headline, try all level 1 trees. If
  5827. it is on a headline, try all direct children.
  5828. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5829. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5830. (rea (concat ".*:" org-archive-tag ":"))
  5831. (begm (make-marker))
  5832. (endm (make-marker))
  5833. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5834. "Move subtree to archive (no open TODO items)? "))
  5835. beg end (cntarch 0))
  5836. (if (org-on-heading-p)
  5837. (progn
  5838. (setq re1 (concat "^" (regexp-quote
  5839. (make-string
  5840. (1+ (- (match-end 0) (match-beginning 0)))
  5841. ?*))
  5842. " "))
  5843. (move-marker begm (point))
  5844. (move-marker endm (org-end-of-subtree t)))
  5845. (setq re1 "^* ")
  5846. (move-marker begm (point-min))
  5847. (move-marker endm (point-max)))
  5848. (save-excursion
  5849. (goto-char begm)
  5850. (while (re-search-forward re1 endm t)
  5851. (setq beg (match-beginning 0)
  5852. end (save-excursion (org-end-of-subtree t) (point)))
  5853. (goto-char beg)
  5854. (if (re-search-forward re end t)
  5855. (goto-char end)
  5856. (goto-char beg)
  5857. (if (and (or (not tag) (not (looking-at rea)))
  5858. (y-or-n-p question))
  5859. (progn
  5860. (if tag
  5861. (org-toggle-tag org-archive-tag 'on)
  5862. (org-archive-subtree))
  5863. (setq cntarch (1+ cntarch)))
  5864. (goto-char end)))))
  5865. (message "%d trees archived" cntarch)))
  5866. (defun org-cycle-hide-drawers (state)
  5867. "Re-hide all archived subtrees after a visibility state change."
  5868. (when (not (memq state '(overview folded)))
  5869. (save-excursion
  5870. (let* ((globalp (memq state '(contents all)))
  5871. (beg (if globalp (point-min) (point)))
  5872. (end (if globalp (point-max) (org-end-of-subtree t))))
  5873. (goto-char beg)
  5874. (while (re-search-forward org-drawer-regexp end t)
  5875. (org-flag-drawer t))))))
  5876. (defun org-flag-drawer (flag)
  5877. (save-excursion
  5878. (beginning-of-line 1)
  5879. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5880. (let ((b (match-end 0)))
  5881. (if (re-search-forward
  5882. "^[ \t]*:END:"
  5883. (save-excursion (outline-next-heading) (point)) t)
  5884. (outline-flag-region b (point-at-eol) flag)
  5885. (error ":END: line missing"))))))
  5886. (defun org-cycle-hide-archived-subtrees (state)
  5887. "Re-hide all archived subtrees after a visibility state change."
  5888. (when (and (not org-cycle-open-archived-trees)
  5889. (not (memq state '(overview folded))))
  5890. (save-excursion
  5891. (let* ((globalp (memq state '(contents all)))
  5892. (beg (if globalp (point-min) (point)))
  5893. (end (if globalp (point-max) (org-end-of-subtree t))))
  5894. (org-hide-archived-subtrees beg end)
  5895. (goto-char beg)
  5896. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5897. (message (substitute-command-keys
  5898. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5899. (defun org-force-cycle-archived ()
  5900. "Cycle subtree even if it is archived."
  5901. (interactive)
  5902. (setq this-command 'org-cycle)
  5903. (let ((org-cycle-open-archived-trees t))
  5904. (call-interactively 'org-cycle)))
  5905. (defun org-hide-archived-subtrees (beg end)
  5906. "Re-hide all archived subtrees after a visibility state change."
  5907. (save-excursion
  5908. (let* ((re (concat ":" org-archive-tag ":")))
  5909. (goto-char beg)
  5910. (while (re-search-forward re end t)
  5911. (and (org-on-heading-p) (hide-subtree))
  5912. (org-end-of-subtree t)))))
  5913. (defun org-toggle-tag (tag &optional onoff)
  5914. "Toggle the tag TAG for the current line.
  5915. If ONOFF is `on' or `off', don't toggle but set to this state."
  5916. (unless (org-on-heading-p t) (error "Not on headling"))
  5917. (let (res current)
  5918. (save-excursion
  5919. (beginning-of-line)
  5920. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  5921. (point-at-eol) t)
  5922. (progn
  5923. (setq current (match-string 1))
  5924. (replace-match ""))
  5925. (setq current ""))
  5926. (setq current (nreverse (org-split-string current ":")))
  5927. (cond
  5928. ((eq onoff 'on)
  5929. (setq res t)
  5930. (or (member tag current) (push tag current)))
  5931. ((eq onoff 'off)
  5932. (or (not (member tag current)) (setq current (delete tag current))))
  5933. (t (if (member tag current)
  5934. (setq current (delete tag current))
  5935. (setq res t)
  5936. (push tag current))))
  5937. (end-of-line 1)
  5938. (when current
  5939. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  5940. (org-set-tags nil t))
  5941. res))
  5942. (defun org-toggle-archive-tag (&optional arg)
  5943. "Toggle the archive tag for the current headline.
  5944. With prefix ARG, check all children of current headline and offer tagging
  5945. the children that do not contain any open TODO items."
  5946. (interactive "P")
  5947. (if arg
  5948. (org-archive-all-done 'tag)
  5949. (let (set)
  5950. (save-excursion
  5951. (org-back-to-heading t)
  5952. (setq set (org-toggle-tag org-archive-tag))
  5953. (when set (hide-subtree)))
  5954. (and set (beginning-of-line 1))
  5955. (message "Subtree %s" (if set "archived" "unarchived")))))
  5956. ;;;; Tables
  5957. ;;; The table editor
  5958. ;; Watch out: Here we are talking about two different kind of tables.
  5959. ;; Most of the code is for the tables created with the Org-mode table editor.
  5960. ;; Sometimes, we talk about tables created and edited with the table.el
  5961. ;; Emacs package. We call the former org-type tables, and the latter
  5962. ;; table.el-type tables.
  5963. (defun org-before-change-function (beg end)
  5964. "Every change indicates that a table might need an update."
  5965. (setq org-table-may-need-update t))
  5966. (defconst org-table-line-regexp "^[ \t]*|"
  5967. "Detects an org-type table line.")
  5968. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  5969. "Detects an org-type table line.")
  5970. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  5971. "Detects a table line marked for automatic recalculation.")
  5972. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  5973. "Detects a table line marked for automatic recalculation.")
  5974. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  5975. "Detects a table line marked for automatic recalculation.")
  5976. (defconst org-table-hline-regexp "^[ \t]*|-"
  5977. "Detects an org-type table hline.")
  5978. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  5979. "Detects a table-type table hline.")
  5980. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  5981. "Detects an org-type or table-type table.")
  5982. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  5983. "Searching from within a table (any type) this finds the first line
  5984. outside the table.")
  5985. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  5986. "Searching from within a table (any type) this finds the first line
  5987. outside the table.")
  5988. (defvar org-table-last-highlighted-reference nil)
  5989. (defvar org-table-formula-history nil)
  5990. (defvar org-table-column-names nil
  5991. "Alist with column names, derived from the `!' line.")
  5992. (defvar org-table-column-name-regexp nil
  5993. "Regular expression matching the current column names.")
  5994. (defvar org-table-local-parameters nil
  5995. "Alist with parameter names, derived from the `$' line.")
  5996. (defvar org-table-named-field-locations nil
  5997. "Alist with locations of named fields.")
  5998. (defvar org-table-current-line-types nil
  5999. "Table row types, non-nil only for the duration of a comand.")
  6000. (defvar org-table-current-begin-line nil
  6001. "Table begin line, non-nil only for the duration of a comand.")
  6002. (defvar org-table-current-begin-pos nil
  6003. "Table begin position, non-nil only for the duration of a comand.")
  6004. (defvar org-table-dlines nil
  6005. "Vector of data line line numbers in the current table.")
  6006. (defvar org-table-hlines nil
  6007. "Vector of hline line numbers in the current table.")
  6008. (defconst org-table-range-regexp
  6009. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6010. ;; 1 2 3 4 5
  6011. "Regular expression for matching ranges in formulas.")
  6012. (defconst org-table-range-regexp2
  6013. (concat
  6014. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6015. "\\.\\."
  6016. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6017. "Match a range for reference display.")
  6018. (defconst org-table-translate-regexp
  6019. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6020. "Match a reference that needs translation, for reference display.")
  6021. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6022. (defun org-table-create-with-table.el ()
  6023. "Use the table.el package to insert a new table.
  6024. If there is already a table at point, convert between Org-mode tables
  6025. and table.el tables."
  6026. (interactive)
  6027. (require 'table)
  6028. (cond
  6029. ((org-at-table.el-p)
  6030. (if (y-or-n-p "Convert table to Org-mode table? ")
  6031. (org-table-convert)))
  6032. ((org-at-table-p)
  6033. (if (y-or-n-p "Convert table to table.el table? ")
  6034. (org-table-convert)))
  6035. (t (call-interactively 'table-insert))))
  6036. (defun org-table-create-or-convert-from-region (arg)
  6037. "Convert region to table, or create an empty table.
  6038. If there is an active region, convert it to a table, using the function
  6039. `org-table-convert-region'.
  6040. If there is no such region, create an empty table with `org-table-create'."
  6041. (interactive "P")
  6042. (if (org-region-active-p)
  6043. (org-table-convert-region (region-beginning) (region-end) arg)
  6044. (org-table-create arg)))
  6045. (defun org-table-create (&optional size)
  6046. "Query for a size and insert a table skeleton.
  6047. SIZE is a string Columns x Rows like for example \"3x2\"."
  6048. (interactive "P")
  6049. (unless size
  6050. (setq size (read-string
  6051. (concat "Table size Columns x Rows [e.g. "
  6052. org-table-default-size "]: ")
  6053. "" nil org-table-default-size)))
  6054. (let* ((pos (point))
  6055. (indent (make-string (current-column) ?\ ))
  6056. (split (org-split-string size " *x *"))
  6057. (rows (string-to-number (nth 1 split)))
  6058. (columns (string-to-number (car split)))
  6059. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6060. "\n")))
  6061. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6062. (point-at-bol) (point)))
  6063. (beginning-of-line 1)
  6064. (newline))
  6065. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6066. (dotimes (i rows) (insert line))
  6067. (goto-char pos)
  6068. (if (> rows 1)
  6069. ;; Insert a hline after the first row.
  6070. (progn
  6071. (end-of-line 1)
  6072. (insert "\n|-")
  6073. (goto-char pos)))
  6074. (org-table-align)))
  6075. (defun org-table-convert-region (beg0 end0 &optional nspace)
  6076. "Convert region to a table.
  6077. The region goes from BEG0 to END0, but these borders will be moved
  6078. slightly, to make sure a beginning of line in the first line is included.
  6079. When NSPACE is non-nil, it indicates the minimum number of spaces that
  6080. separate columns. By default, the function first checks if every line
  6081. contains at lease one TAB. If yes, it assumes that the material is TAB
  6082. separated. If not, it assumes a single space as separator."
  6083. (interactive "rP")
  6084. (let* ((beg (min beg0 end0))
  6085. (end (max beg0 end0))
  6086. (tabsep t)
  6087. re)
  6088. (goto-char beg)
  6089. (beginning-of-line 1)
  6090. (setq beg (move-marker (make-marker) (point)))
  6091. (goto-char end)
  6092. (if (bolp) (backward-char 1) (end-of-line 1))
  6093. (setq end (move-marker (make-marker) (point)))
  6094. ;; Lets see if this is tab-separated material. If every nonempty line
  6095. ;; contains a tab, we will assume that it is tab-separated material
  6096. (if nspace
  6097. (setq tabsep nil)
  6098. (goto-char beg)
  6099. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  6100. (if nspace (setq tabsep nil))
  6101. (if tabsep
  6102. (setq re "^\\|\t")
  6103. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  6104. (max 1 (prefix-numeric-value nspace)))))
  6105. (goto-char beg)
  6106. (while (re-search-forward re end t)
  6107. (replace-match "| " t t))
  6108. (goto-char beg)
  6109. (insert " ")
  6110. (org-table-align)))
  6111. (defun org-table-import (file arg)
  6112. "Import FILE as a table.
  6113. The file is assumed to be tab-separated. Such files can be produced by most
  6114. spreadsheet and database applications. If no tabs (at least one per line)
  6115. are found, lines will be split on whitespace into fields."
  6116. (interactive "f\nP")
  6117. (or (bolp) (newline))
  6118. (let ((beg (point))
  6119. (pm (point-max)))
  6120. (insert-file-contents file)
  6121. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  6122. (defun org-table-export ()
  6123. "Export table as a tab-separated file.
  6124. Such a file can be imported into a spreadsheet program like Excel."
  6125. (interactive)
  6126. (let* ((beg (org-table-begin))
  6127. (end (org-table-end))
  6128. (table (buffer-substring beg end))
  6129. (file (read-file-name "Export table to: "))
  6130. buf)
  6131. (unless (or (not (file-exists-p file))
  6132. (y-or-n-p (format "Overwrite file %s? " file)))
  6133. (error "Abort"))
  6134. (with-current-buffer (find-file-noselect file)
  6135. (setq buf (current-buffer))
  6136. (erase-buffer)
  6137. (fundamental-mode)
  6138. (insert table)
  6139. (goto-char (point-min))
  6140. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  6141. (replace-match "" t t)
  6142. (end-of-line 1))
  6143. (goto-char (point-min))
  6144. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  6145. (replace-match "" t t)
  6146. (goto-char (min (1+ (point)) (point-max))))
  6147. (goto-char (point-min))
  6148. (while (re-search-forward "^-[-+]*$" nil t)
  6149. (replace-match "")
  6150. (if (looking-at "\n")
  6151. (delete-char 1)))
  6152. (goto-char (point-min))
  6153. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  6154. (replace-match "\t" t t))
  6155. (save-buffer))
  6156. (kill-buffer buf)))
  6157. (defvar org-table-aligned-begin-marker (make-marker)
  6158. "Marker at the beginning of the table last aligned.
  6159. Used to check if cursor still is in that table, to minimize realignment.")
  6160. (defvar org-table-aligned-end-marker (make-marker)
  6161. "Marker at the end of the table last aligned.
  6162. Used to check if cursor still is in that table, to minimize realignment.")
  6163. (defvar org-table-last-alignment nil
  6164. "List of flags for flushright alignment, from the last re-alignment.
  6165. This is being used to correctly align a single field after TAB or RET.")
  6166. (defvar org-table-last-column-widths nil
  6167. "List of max width of fields in each column.
  6168. This is being used to correctly align a single field after TAB or RET.")
  6169. (defvar org-table-overlay-coordinates nil
  6170. "Overlay coordinates after each align of a table.")
  6171. (make-variable-buffer-local 'org-table-overlay-coordinates)
  6172. (defvar org-last-recalc-line nil)
  6173. (defconst org-narrow-column-arrow "=>"
  6174. "Used as display property in narrowed table columns.")
  6175. (defun org-table-align ()
  6176. "Align the table at point by aligning all vertical bars."
  6177. (interactive)
  6178. (let* (
  6179. ;; Limits of table
  6180. (beg (org-table-begin))
  6181. (end (org-table-end))
  6182. ;; Current cursor position
  6183. (linepos (org-current-line))
  6184. (colpos (org-table-current-column))
  6185. (winstart (window-start))
  6186. (winstartline (org-current-line (min winstart (1- (point-max)))))
  6187. lines (new "") lengths l typenums ty fields maxfields i
  6188. column
  6189. (indent "") cnt frac
  6190. rfmt hfmt
  6191. (spaces '(1 . 1))
  6192. (sp1 (car spaces))
  6193. (sp2 (cdr spaces))
  6194. (rfmt1 (concat
  6195. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  6196. (hfmt1 (concat
  6197. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  6198. emptystrings links dates narrow fmax f1 len c e)
  6199. (untabify beg end)
  6200. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  6201. ;; Check if we have links or dates
  6202. (goto-char beg)
  6203. (setq links (re-search-forward org-bracket-link-regexp end t))
  6204. (goto-char beg)
  6205. (setq dates (and org-display-custom-times
  6206. (re-search-forward org-ts-regexp-both end t)))
  6207. ;; Make sure the link properties are right
  6208. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  6209. ;; Make sure the date properties are right
  6210. (when dates (goto-char beg) (while (org-activate-dates end)))
  6211. ;; Check if we are narrowing any columns
  6212. (goto-char beg)
  6213. (setq narrow (and org-format-transports-properties-p
  6214. (re-search-forward "<[0-9]+>" end t)))
  6215. ;; Get the rows
  6216. (setq lines (org-split-string
  6217. (buffer-substring beg end) "\n"))
  6218. ;; Store the indentation of the first line
  6219. (if (string-match "^ *" (car lines))
  6220. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  6221. ;; Mark the hlines by setting the corresponding element to nil
  6222. ;; At the same time, we remove trailing space.
  6223. (setq lines (mapcar (lambda (l)
  6224. (if (string-match "^ *|-" l)
  6225. nil
  6226. (if (string-match "[ \t]+$" l)
  6227. (substring l 0 (match-beginning 0))
  6228. l)))
  6229. lines))
  6230. ;; Get the data fields by splitting the lines.
  6231. (setq fields (mapcar
  6232. (lambda (l)
  6233. (org-split-string l " *| *"))
  6234. (delq nil (copy-sequence lines))))
  6235. ;; How many fields in the longest line?
  6236. (condition-case nil
  6237. (setq maxfields (apply 'max (mapcar 'length fields)))
  6238. (error
  6239. (kill-region beg end)
  6240. (org-table-create org-table-default-size)
  6241. (error "Empty table - created default table")))
  6242. ;; A list of empty strings to fill any short rows on output
  6243. (setq emptystrings (make-list maxfields ""))
  6244. ;; Check for special formatting.
  6245. (setq i -1)
  6246. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  6247. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  6248. ;; Check if there is an explicit width specified
  6249. (when narrow
  6250. (setq c column fmax nil)
  6251. (while c
  6252. (setq e (pop c))
  6253. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  6254. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  6255. ;; Find fields that are wider than fmax, and shorten them
  6256. (when fmax
  6257. (loop for xx in column do
  6258. (when (and (stringp xx)
  6259. (> (org-string-width xx) fmax))
  6260. (org-add-props xx nil
  6261. 'help-echo
  6262. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  6263. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  6264. (unless (> f1 1)
  6265. (error "Cannot narrow field starting with wide link \"%s\""
  6266. (match-string 0 xx)))
  6267. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  6268. (add-text-properties (- f1 2) f1
  6269. (list 'display org-narrow-column-arrow)
  6270. xx)))))
  6271. ;; Get the maximum width for each column
  6272. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  6273. ;; Get the fraction of numbers, to decide about alignment of the column
  6274. (setq cnt 0 frac 0.0)
  6275. (loop for x in column do
  6276. (if (equal x "")
  6277. nil
  6278. (setq frac ( / (+ (* frac cnt)
  6279. (if (string-match org-table-number-regexp x) 1 0))
  6280. (setq cnt (1+ cnt))))))
  6281. (push (>= frac org-table-number-fraction) typenums))
  6282. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  6283. ;; Store the alignment of this table, for later editing of single fields
  6284. (setq org-table-last-alignment typenums
  6285. org-table-last-column-widths lengths)
  6286. ;; With invisible characters, `format' does not get the field width right
  6287. ;; So we need to make these fields wide by hand.
  6288. (when links
  6289. (loop for i from 0 upto (1- maxfields) do
  6290. (setq len (nth i lengths))
  6291. (loop for j from 0 upto (1- (length fields)) do
  6292. (setq c (nthcdr i (car (nthcdr j fields))))
  6293. (if (and (stringp (car c))
  6294. (string-match org-bracket-link-regexp (car c))
  6295. (< (org-string-width (car c)) len))
  6296. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  6297. ;; Compute the formats needed for output of the table
  6298. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  6299. (while (setq l (pop lengths))
  6300. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  6301. (setq rfmt (concat rfmt (format rfmt1 ty l))
  6302. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  6303. (setq rfmt (concat rfmt "\n")
  6304. hfmt (concat (substring hfmt 0 -1) "|\n"))
  6305. (setq new (mapconcat
  6306. (lambda (l)
  6307. (if l (apply 'format rfmt
  6308. (append (pop fields) emptystrings))
  6309. hfmt))
  6310. lines ""))
  6311. ;; Replace the old one
  6312. (delete-region beg end)
  6313. (move-marker end nil)
  6314. (move-marker org-table-aligned-begin-marker (point))
  6315. (insert new)
  6316. (move-marker org-table-aligned-end-marker (point))
  6317. (when (and orgtbl-mode (not (org-mode-p)))
  6318. (goto-char org-table-aligned-begin-marker)
  6319. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  6320. ;; Try to move to the old location
  6321. (goto-line winstartline)
  6322. (setq winstart (point-at-bol))
  6323. (goto-line linepos)
  6324. (set-window-start (selected-window) winstart 'noforce)
  6325. (org-table-goto-column colpos)
  6326. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  6327. (setq org-table-may-need-update nil)
  6328. ))
  6329. (defun org-string-width (s)
  6330. "Compute width of string, ignoring invisible characters.
  6331. This ignores character with invisibility property `org-link', and also
  6332. characters with property `org-cwidth', because these will become invisible
  6333. upon the next fontification round."
  6334. (let (b l)
  6335. (when (or (eq t buffer-invisibility-spec)
  6336. (assq 'org-link buffer-invisibility-spec))
  6337. (while (setq b (text-property-any 0 (length s)
  6338. 'invisible 'org-link s))
  6339. (setq s (concat (substring s 0 b)
  6340. (substring s (or (next-single-property-change
  6341. b 'invisible s) (length s)))))))
  6342. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  6343. (setq s (concat (substring s 0 b)
  6344. (substring s (or (next-single-property-change
  6345. b 'org-cwidth s) (length s))))))
  6346. (setq l (string-width s) b -1)
  6347. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  6348. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  6349. l))
  6350. (defun org-table-begin (&optional table-type)
  6351. "Find the beginning of the table and return its position.
  6352. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  6353. (save-excursion
  6354. (if (not (re-search-backward
  6355. (if table-type org-table-any-border-regexp
  6356. org-table-border-regexp)
  6357. nil t))
  6358. (progn (goto-char (point-min)) (point))
  6359. (goto-char (match-beginning 0))
  6360. (beginning-of-line 2)
  6361. (point))))
  6362. (defun org-table-end (&optional table-type)
  6363. "Find the end of the table and return its position.
  6364. With argument TABLE-TYPE, go to the end of a table.el-type table."
  6365. (save-excursion
  6366. (if (not (re-search-forward
  6367. (if table-type org-table-any-border-regexp
  6368. org-table-border-regexp)
  6369. nil t))
  6370. (goto-char (point-max))
  6371. (goto-char (match-beginning 0)))
  6372. (point-marker)))
  6373. (defun org-table-justify-field-maybe (&optional new)
  6374. "Justify the current field, text to left, number to right.
  6375. Optional argument NEW may specify text to replace the current field content."
  6376. (cond
  6377. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  6378. ((org-at-table-hline-p))
  6379. ((and (not new)
  6380. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  6381. (current-buffer)))
  6382. (< (point) org-table-aligned-begin-marker)
  6383. (>= (point) org-table-aligned-end-marker)))
  6384. ;; This is not the same table, force a full re-align
  6385. (setq org-table-may-need-update t))
  6386. (t ;; realign the current field, based on previous full realign
  6387. (let* ((pos (point)) s
  6388. (col (org-table-current-column))
  6389. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  6390. l f n o e)
  6391. (when (> col 0)
  6392. (skip-chars-backward "^|\n")
  6393. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  6394. (progn
  6395. (setq s (match-string 1)
  6396. o (match-string 0)
  6397. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  6398. e (not (= (match-beginning 2) (match-end 2))))
  6399. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  6400. l (if e "|" (setq org-table-may-need-update t) ""))
  6401. n (format f s))
  6402. (if new
  6403. (if (<= (length new) l) ;; FIXME: length -> str-width?
  6404. (setq n (format f new))
  6405. (setq n (concat new "|") org-table-may-need-update t)))
  6406. (or (equal n o)
  6407. (let (org-table-may-need-update)
  6408. (replace-match n))))
  6409. (setq org-table-may-need-update t))
  6410. (goto-char pos))))))
  6411. (defun org-table-next-field ()
  6412. "Go to the next field in the current table, creating new lines as needed.
  6413. Before doing so, re-align the table if necessary."
  6414. (interactive)
  6415. (org-table-maybe-eval-formula)
  6416. (org-table-maybe-recalculate-line)
  6417. (if (and org-table-automatic-realign
  6418. org-table-may-need-update)
  6419. (org-table-align))
  6420. (let ((end (org-table-end)))
  6421. (if (org-at-table-hline-p)
  6422. (end-of-line 1))
  6423. (condition-case nil
  6424. (progn
  6425. (re-search-forward "|" end)
  6426. (if (looking-at "[ \t]*$")
  6427. (re-search-forward "|" end))
  6428. (if (and (looking-at "-")
  6429. org-table-tab-jumps-over-hlines
  6430. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  6431. (goto-char (match-beginning 1)))
  6432. (if (looking-at "-")
  6433. (progn
  6434. (beginning-of-line 0)
  6435. (org-table-insert-row 'below))
  6436. (if (looking-at " ") (forward-char 1))))
  6437. (error
  6438. (org-table-insert-row 'below)))))
  6439. (defun org-table-previous-field ()
  6440. "Go to the previous field in the table.
  6441. Before doing so, re-align the table if necessary."
  6442. (interactive)
  6443. (org-table-justify-field-maybe)
  6444. (org-table-maybe-recalculate-line)
  6445. (if (and org-table-automatic-realign
  6446. org-table-may-need-update)
  6447. (org-table-align))
  6448. (if (org-at-table-hline-p)
  6449. (end-of-line 1))
  6450. (re-search-backward "|" (org-table-begin))
  6451. (re-search-backward "|" (org-table-begin))
  6452. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  6453. (re-search-backward "|" (org-table-begin)))
  6454. (if (looking-at "| ?")
  6455. (goto-char (match-end 0))))
  6456. (defun org-table-next-row ()
  6457. "Go to the next row (same column) in the current table.
  6458. Before doing so, re-align the table if necessary."
  6459. (interactive)
  6460. (org-table-maybe-eval-formula)
  6461. (org-table-maybe-recalculate-line)
  6462. (if (or (looking-at "[ \t]*$")
  6463. (save-excursion (skip-chars-backward " \t") (bolp)))
  6464. (newline)
  6465. (if (and org-table-automatic-realign
  6466. org-table-may-need-update)
  6467. (org-table-align))
  6468. (let ((col (org-table-current-column)))
  6469. (beginning-of-line 2)
  6470. (if (or (not (org-at-table-p))
  6471. (org-at-table-hline-p))
  6472. (progn
  6473. (beginning-of-line 0)
  6474. (org-table-insert-row 'below)))
  6475. (org-table-goto-column col)
  6476. (skip-chars-backward "^|\n\r")
  6477. (if (looking-at " ") (forward-char 1)))))
  6478. (defun org-table-copy-down (n)
  6479. "Copy a field down in the current column.
  6480. If the field at the cursor is empty, copy into it the content of the nearest
  6481. non-empty field above. With argument N, use the Nth non-empty field.
  6482. If the current field is not empty, it is copied down to the next row, and
  6483. the cursor is moved with it. Therefore, repeating this command causes the
  6484. column to be filled row-by-row.
  6485. If the variable `org-table-copy-increment' is non-nil and the field is an
  6486. integer or a timestamp, it will be incremented while copying. In the case of
  6487. a timestamp, if the cursor is on the year, change the year. If it is on the
  6488. month or the day, change that. Point will stay on the current date field
  6489. in order to easily repeat the interval."
  6490. (interactive "p")
  6491. (let* ((colpos (org-table-current-column))
  6492. (col (current-column))
  6493. (field (org-table-get-field))
  6494. (non-empty (string-match "[^ \t]" field))
  6495. (beg (org-table-begin))
  6496. txt)
  6497. (org-table-check-inside-data-field)
  6498. (if non-empty
  6499. (progn
  6500. (setq txt (org-trim field))
  6501. (org-table-next-row)
  6502. (org-table-blank-field))
  6503. (save-excursion
  6504. (setq txt
  6505. (catch 'exit
  6506. (while (progn (beginning-of-line 1)
  6507. (re-search-backward org-table-dataline-regexp
  6508. beg t))
  6509. (org-table-goto-column colpos t)
  6510. (if (and (looking-at
  6511. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  6512. (= (setq n (1- n)) 0))
  6513. (throw 'exit (match-string 1))))))))
  6514. (if txt
  6515. (progn
  6516. (if (and org-table-copy-increment
  6517. (string-match "^[0-9]+$" txt))
  6518. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  6519. (insert txt)
  6520. (move-to-column col)
  6521. (if (and org-table-copy-increment (org-at-timestamp-p t))
  6522. (org-timestamp-up 1)
  6523. (org-table-maybe-recalculate-line))
  6524. (org-table-align)
  6525. (move-to-column col))
  6526. (error "No non-empty field found"))))
  6527. (defun org-table-check-inside-data-field ()
  6528. "Is point inside a table data field?
  6529. I.e. not on a hline or before the first or after the last column?
  6530. This actually throws an error, so it aborts the current command."
  6531. (if (or (not (org-at-table-p))
  6532. (= (org-table-current-column) 0)
  6533. (org-at-table-hline-p)
  6534. (looking-at "[ \t]*$"))
  6535. (error "Not in table data field")))
  6536. (defvar org-table-clip nil
  6537. "Clipboard for table regions.")
  6538. (defun org-table-blank-field ()
  6539. "Blank the current table field or active region."
  6540. (interactive)
  6541. (org-table-check-inside-data-field)
  6542. (if (and (interactive-p) (org-region-active-p))
  6543. (let (org-table-clip)
  6544. (org-table-cut-region (region-beginning) (region-end)))
  6545. (skip-chars-backward "^|")
  6546. (backward-char 1)
  6547. (if (looking-at "|[^|\n]+")
  6548. (let* ((pos (match-beginning 0))
  6549. (match (match-string 0))
  6550. (len (org-string-width match)))
  6551. (replace-match (concat "|" (make-string (1- len) ?\ )))
  6552. (goto-char (+ 2 pos))
  6553. (substring match 1)))))
  6554. (defun org-table-get-field (&optional n replace)
  6555. "Return the value of the field in column N of current row.
  6556. N defaults to current field.
  6557. If REPLACE is a string, replace field with this value. The return value
  6558. is always the old value."
  6559. (and n (org-table-goto-column n))
  6560. (skip-chars-backward "^|\n")
  6561. (backward-char 1)
  6562. (if (looking-at "|[^|\r\n]*")
  6563. (let* ((pos (match-beginning 0))
  6564. (val (buffer-substring (1+ pos) (match-end 0))))
  6565. (if replace
  6566. (replace-match (concat "|" replace) t t))
  6567. (goto-char (min (point-at-eol) (+ 2 pos)))
  6568. val)
  6569. (forward-char 1) ""))
  6570. (defun org-table-field-info (arg)
  6571. "Show info about the current field, and highlight any reference at point."
  6572. (interactive "P")
  6573. (org-table-get-specials)
  6574. (save-excursion
  6575. (let* ((pos (point))
  6576. (col (org-table-current-column))
  6577. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  6578. (name (car (rassoc (list (org-current-line) col)
  6579. org-table-named-field-locations)))
  6580. (eql (org-table-get-stored-formulas))
  6581. (dline (org-table-current-dline))
  6582. (ref (format "@%d$%d" dline col))
  6583. (ref1 (org-table-convert-refs-to-an ref))
  6584. (fequation (or (assoc name eql) (assoc ref eql)))
  6585. (cequation (assoc (int-to-string col) eql))
  6586. (eqn (or fequation cequation)))
  6587. (goto-char pos)
  6588. (condition-case nil
  6589. (org-table-show-reference 'local)
  6590. (error nil))
  6591. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  6592. dline col
  6593. (if cname (concat " or $" cname) "")
  6594. dline col ref1
  6595. (if name (concat " or $" name) "")
  6596. ;; FIXME: formula info not correct if special table line
  6597. (if eqn
  6598. (concat ", formula: "
  6599. (org-table-formula-to-user
  6600. (concat
  6601. (if (string-match "^[$@]"(car eqn)) "" "$")
  6602. (car eqn) "=" (cdr eqn))))
  6603. "")))))
  6604. (defun org-table-current-column ()
  6605. "Find out which column we are in.
  6606. When called interactively, column is also displayed in echo area."
  6607. (interactive)
  6608. (if (interactive-p) (org-table-check-inside-data-field))
  6609. (save-excursion
  6610. (let ((cnt 0) (pos (point)))
  6611. (beginning-of-line 1)
  6612. (while (search-forward "|" pos t)
  6613. (setq cnt (1+ cnt)))
  6614. (if (interactive-p) (message "This is table column %d" cnt))
  6615. cnt)))
  6616. (defun org-table-current-dline ()
  6617. "Find out what table data line we are in.
  6618. Only datalins count for this."
  6619. (interactive)
  6620. (if (interactive-p) (org-table-check-inside-data-field))
  6621. (save-excursion
  6622. (let ((cnt 0) (pos (point)))
  6623. (goto-char (org-table-begin))
  6624. (while (<= (point) pos)
  6625. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  6626. (beginning-of-line 2))
  6627. (if (interactive-p) (message "This is table line %d" cnt))
  6628. cnt)))
  6629. (defun org-table-goto-column (n &optional on-delim force)
  6630. "Move the cursor to the Nth column in the current table line.
  6631. With optional argument ON-DELIM, stop with point before the left delimiter
  6632. of the field.
  6633. If there are less than N fields, just go to after the last delimiter.
  6634. However, when FORCE is non-nil, create new columns if necessary."
  6635. (interactive "p")
  6636. (let ((pos (point-at-eol)))
  6637. (beginning-of-line 1)
  6638. (when (> n 0)
  6639. (while (and (> (setq n (1- n)) -1)
  6640. (or (search-forward "|" pos t)
  6641. (and force
  6642. (progn (end-of-line 1)
  6643. (skip-chars-backward "^|")
  6644. (insert " | "))))))
  6645. ; (backward-char 2) t)))))
  6646. (when (and force (not (looking-at ".*|")))
  6647. (save-excursion (end-of-line 1) (insert " | ")))
  6648. (if on-delim
  6649. (backward-char 1)
  6650. (if (looking-at " ") (forward-char 1))))))
  6651. (defun org-at-table-p (&optional table-type)
  6652. "Return t if the cursor is inside an org-type table.
  6653. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  6654. (if org-enable-table-editor
  6655. (save-excursion
  6656. (beginning-of-line 1)
  6657. (looking-at (if table-type org-table-any-line-regexp
  6658. org-table-line-regexp)))
  6659. nil))
  6660. (defun org-at-table.el-p ()
  6661. "Return t if and only if we are at a table.el table."
  6662. (and (org-at-table-p 'any)
  6663. (save-excursion
  6664. (goto-char (org-table-begin 'any))
  6665. (looking-at org-table1-hline-regexp))))
  6666. (defun org-table-recognize-table.el ()
  6667. "If there is a table.el table nearby, recognize it and move into it."
  6668. (if org-table-tab-recognizes-table.el
  6669. (if (org-at-table.el-p)
  6670. (progn
  6671. (beginning-of-line 1)
  6672. (if (looking-at org-table-dataline-regexp)
  6673. nil
  6674. (if (looking-at org-table1-hline-regexp)
  6675. (progn
  6676. (beginning-of-line 2)
  6677. (if (looking-at org-table-any-border-regexp)
  6678. (beginning-of-line -1)))))
  6679. (if (re-search-forward "|" (org-table-end t) t)
  6680. (progn
  6681. (require 'table)
  6682. (if (table--at-cell-p (point))
  6683. t
  6684. (message "recognizing table.el table...")
  6685. (table-recognize-table)
  6686. (message "recognizing table.el table...done")))
  6687. (error "This should not happen..."))
  6688. t)
  6689. nil)
  6690. nil))
  6691. (defun org-at-table-hline-p ()
  6692. "Return t if the cursor is inside a hline in a table."
  6693. (if org-enable-table-editor
  6694. (save-excursion
  6695. (beginning-of-line 1)
  6696. (looking-at org-table-hline-regexp))
  6697. nil))
  6698. (defun org-table-insert-column ()
  6699. "Insert a new column into the table."
  6700. (interactive)
  6701. (if (not (org-at-table-p))
  6702. (error "Not at a table"))
  6703. (org-table-find-dataline)
  6704. (let* ((col (max 1 (org-table-current-column)))
  6705. (beg (org-table-begin))
  6706. (end (org-table-end))
  6707. ;; Current cursor position
  6708. (linepos (org-current-line))
  6709. (colpos col))
  6710. (goto-char beg)
  6711. (while (< (point) end)
  6712. (if (org-at-table-hline-p)
  6713. nil
  6714. (org-table-goto-column col t)
  6715. (insert "| "))
  6716. (beginning-of-line 2))
  6717. (move-marker end nil)
  6718. (goto-line linepos)
  6719. (org-table-goto-column colpos)
  6720. (org-table-align)
  6721. (org-table-fix-formulas "$" nil (1- col) 1)))
  6722. (defun org-table-find-dataline ()
  6723. "Find a dataline in the current table, which is needed for column commands."
  6724. (if (and (org-at-table-p)
  6725. (not (org-at-table-hline-p)))
  6726. t
  6727. (let ((col (current-column))
  6728. (end (org-table-end)))
  6729. (move-to-column col)
  6730. (while (and (< (point) end)
  6731. (or (not (= (current-column) col))
  6732. (org-at-table-hline-p)))
  6733. (beginning-of-line 2)
  6734. (move-to-column col))
  6735. (if (and (org-at-table-p)
  6736. (not (org-at-table-hline-p)))
  6737. t
  6738. (error
  6739. "Please position cursor in a data line for column operations")))))
  6740. (defun org-table-delete-column ()
  6741. "Delete a column from the table."
  6742. (interactive)
  6743. (if (not (org-at-table-p))
  6744. (error "Not at a table"))
  6745. (org-table-find-dataline)
  6746. (org-table-check-inside-data-field)
  6747. (let* ((col (org-table-current-column))
  6748. (beg (org-table-begin))
  6749. (end (org-table-end))
  6750. ;; Current cursor position
  6751. (linepos (org-current-line))
  6752. (colpos col))
  6753. (goto-char beg)
  6754. (while (< (point) end)
  6755. (if (org-at-table-hline-p)
  6756. nil
  6757. (org-table-goto-column col t)
  6758. (and (looking-at "|[^|\n]+|")
  6759. (replace-match "|")))
  6760. (beginning-of-line 2))
  6761. (move-marker end nil)
  6762. (goto-line linepos)
  6763. (org-table-goto-column colpos)
  6764. (org-table-align)
  6765. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  6766. col -1 col)))
  6767. (defun org-table-move-column-right ()
  6768. "Move column to the right."
  6769. (interactive)
  6770. (org-table-move-column nil))
  6771. (defun org-table-move-column-left ()
  6772. "Move column to the left."
  6773. (interactive)
  6774. (org-table-move-column 'left))
  6775. (defun org-table-move-column (&optional left)
  6776. "Move the current column to the right. With arg LEFT, move to the left."
  6777. (interactive "P")
  6778. (if (not (org-at-table-p))
  6779. (error "Not at a table"))
  6780. (org-table-find-dataline)
  6781. (org-table-check-inside-data-field)
  6782. (let* ((col (org-table-current-column))
  6783. (col1 (if left (1- col) col))
  6784. (beg (org-table-begin))
  6785. (end (org-table-end))
  6786. ;; Current cursor position
  6787. (linepos (org-current-line))
  6788. (colpos (if left (1- col) (1+ col))))
  6789. (if (and left (= col 1))
  6790. (error "Cannot move column further left"))
  6791. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  6792. (error "Cannot move column further right"))
  6793. (goto-char beg)
  6794. (while (< (point) end)
  6795. (if (org-at-table-hline-p)
  6796. nil
  6797. (org-table-goto-column col1 t)
  6798. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  6799. (replace-match "|\\2|\\1|")))
  6800. (beginning-of-line 2))
  6801. (move-marker end nil)
  6802. (goto-line linepos)
  6803. (org-table-goto-column colpos)
  6804. (org-table-align)
  6805. (org-table-fix-formulas
  6806. "$" (list (cons (number-to-string col) (number-to-string colpos))
  6807. (cons (number-to-string colpos) (number-to-string col))))))
  6808. (defun org-table-move-row-down ()
  6809. "Move table row down."
  6810. (interactive)
  6811. (org-table-move-row nil))
  6812. (defun org-table-move-row-up ()
  6813. "Move table row up."
  6814. (interactive)
  6815. (org-table-move-row 'up))
  6816. (defun org-table-move-row (&optional up)
  6817. "Move the current table line down. With arg UP, move it up."
  6818. (interactive "P")
  6819. (let* ((col (current-column))
  6820. (pos (point))
  6821. (hline1p (save-excursion (beginning-of-line 1)
  6822. (looking-at org-table-hline-regexp)))
  6823. (dline1 (org-table-current-dline))
  6824. (dline2 (+ dline1 (if up -1 1)))
  6825. (tonew (if up 0 2))
  6826. txt hline2p)
  6827. (beginning-of-line tonew)
  6828. (unless (org-at-table-p)
  6829. (goto-char pos)
  6830. (error "Cannot move row further"))
  6831. (setq hline2p (looking-at org-table-hline-regexp))
  6832. (goto-char pos)
  6833. (beginning-of-line 1)
  6834. (setq pos (point))
  6835. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  6836. (delete-region (point) (1+ (point-at-eol)))
  6837. (beginning-of-line tonew)
  6838. (insert txt)
  6839. (beginning-of-line 0)
  6840. (move-to-column col)
  6841. (unless (or hline1p hline2p)
  6842. (org-table-fix-formulas
  6843. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  6844. (cons (number-to-string dline2) (number-to-string dline1)))))))
  6845. (defun org-table-insert-row (&optional arg)
  6846. "Insert a new row above the current line into the table.
  6847. With prefix ARG, insert below the current line."
  6848. (interactive "P")
  6849. (if (not (org-at-table-p))
  6850. (error "Not at a table"))
  6851. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  6852. (new (org-table-clean-line line)))
  6853. ;; Fix the first field if necessary
  6854. (if (string-match "^[ \t]*| *[#$] *|" line)
  6855. (setq new (replace-match (match-string 0 line) t t new)))
  6856. (beginning-of-line (if arg 2 1))
  6857. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  6858. (beginning-of-line 0)
  6859. (re-search-forward "| ?" (point-at-eol) t)
  6860. (and (or org-table-may-need-update org-table-overlay-coordinates)
  6861. (org-table-align))
  6862. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  6863. (defun org-table-insert-hline (&optional above)
  6864. "Insert a horizontal-line below the current line into the table.
  6865. With prefix ABOVE, insert above the current line."
  6866. (interactive "P")
  6867. (if (not (org-at-table-p))
  6868. (error "Not at a table"))
  6869. (let ((line (org-table-clean-line
  6870. (buffer-substring (point-at-bol) (point-at-eol))))
  6871. (col (current-column)))
  6872. (while (string-match "|\\( +\\)|" line)
  6873. (setq line (replace-match
  6874. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  6875. ?-) "|") t t line)))
  6876. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  6877. (beginning-of-line (if above 1 2))
  6878. (insert line "\n")
  6879. (beginning-of-line (if above 1 -1))
  6880. (move-to-column col)
  6881. (and org-table-overlay-coordinates (org-table-align))))
  6882. (defun org-table-hline-and-move (&optional same-column)
  6883. "Insert a hline and move to the row below that line."
  6884. (interactive "P")
  6885. (let ((col (org-table-current-column)))
  6886. (org-table-maybe-eval-formula)
  6887. (org-table-maybe-recalculate-line)
  6888. (org-table-insert-hline)
  6889. (end-of-line 2)
  6890. (if (looking-at "\n[ \t]*|-")
  6891. (progn (insert "\n|") (org-table-align))
  6892. (org-table-next-field))
  6893. (if same-column (org-table-goto-column col))))
  6894. (defun org-table-clean-line (s)
  6895. "Convert a table line S into a string with only \"|\" and space.
  6896. In particular, this does handle wide and invisible characters."
  6897. (if (string-match "^[ \t]*|-" s)
  6898. ;; It's a hline, just map the characters
  6899. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  6900. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  6901. (setq s (replace-match
  6902. (concat "|" (make-string (org-string-width (match-string 1 s))
  6903. ?\ ) "|")
  6904. t t s)))
  6905. s))
  6906. (defun org-table-kill-row ()
  6907. "Delete the current row or horizontal line from the table."
  6908. (interactive)
  6909. (if (not (org-at-table-p))
  6910. (error "Not at a table"))
  6911. (let ((col (current-column))
  6912. (dline (org-table-current-dline)))
  6913. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  6914. (if (not (org-at-table-p)) (beginning-of-line 0))
  6915. (move-to-column col)
  6916. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  6917. dline -1 dline)))
  6918. (defun org-table-sort-lines (with-case &optional sorting-type)
  6919. "Sort table lines according to the column at point.
  6920. The position of point indicates the column to be used for
  6921. sorting, and the range of lines is the range between the nearest
  6922. horizontal separator lines, or the entire table of no such lines
  6923. exist. If point is before the first column, you will be prompted
  6924. for the sorting column. If there is an active region, the mark
  6925. specifies the first line and the sorting column, while point
  6926. should be in the last line to be included into the sorting.
  6927. The command then prompts for the sorting type which can be
  6928. alphabetically, numerically, or by time (as given in a time stamp
  6929. in the field). Sorting in reverse order is also possible.
  6930. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  6931. If SORTING-TYPE is specified when this function is called from a Lisp
  6932. program, no prompting will take place. SORTING-TYPE must be a character,
  6933. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  6934. should be done in reverse order."
  6935. (interactive "P")
  6936. (let* ((thisline (org-current-line))
  6937. (thiscol (org-table-current-column))
  6938. beg end bcol ecol tend tbeg column lns pos)
  6939. (when (equal thiscol 0)
  6940. (if (interactive-p)
  6941. (setq thiscol
  6942. (string-to-number
  6943. (read-string "Use column N for sorting: ")))
  6944. (setq thiscol 1))
  6945. (org-table-goto-column thiscol))
  6946. (org-table-check-inside-data-field)
  6947. (if (org-region-active-p)
  6948. (progn
  6949. (setq beg (region-beginning) end (region-end))
  6950. (goto-char beg)
  6951. (setq column (org-table-current-column)
  6952. beg (point-at-bol))
  6953. (goto-char end)
  6954. (setq end (point-at-bol 2)))
  6955. (setq column (org-table-current-column)
  6956. pos (point)
  6957. tbeg (org-table-begin)
  6958. tend (org-table-end))
  6959. (if (re-search-backward org-table-hline-regexp tbeg t)
  6960. (setq beg (point-at-bol 2))
  6961. (goto-char tbeg)
  6962. (setq beg (point-at-bol 1)))
  6963. (goto-char pos)
  6964. (if (re-search-forward org-table-hline-regexp tend t)
  6965. (setq end (point-at-bol 0))
  6966. (goto-char tend)
  6967. (setq end (point-at-bol))))
  6968. (setq beg (move-marker (make-marker) beg)
  6969. end (move-marker (make-marker) end))
  6970. (untabify beg end)
  6971. (goto-char beg)
  6972. (org-table-goto-column column)
  6973. (skip-chars-backward "^|")
  6974. (setq bcol (current-column))
  6975. (org-table-goto-column (1+ column))
  6976. (skip-chars-backward "^|")
  6977. (setq ecol (1- (current-column)))
  6978. (org-table-goto-column column)
  6979. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  6980. (org-split-string (buffer-substring beg end) "\n")))
  6981. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  6982. (delete-region beg end)
  6983. (move-marker beg nil)
  6984. (move-marker end nil)
  6985. (insert (mapconcat 'cdr lns "\n") "\n")
  6986. (goto-line thisline)
  6987. (org-table-goto-column thiscol)
  6988. (message "%d lines sorted, based on column %d" (length lns) column)))
  6989. (defun org-table-cut-region (beg end)
  6990. "Copy region in table to the clipboard and blank all relevant fields."
  6991. (interactive "r")
  6992. (org-table-copy-region beg end 'cut))
  6993. (defun org-table-copy-region (beg end &optional cut)
  6994. "Copy rectangular region in table to clipboard.
  6995. A special clipboard is used which can only be accessed
  6996. with `org-table-paste-rectangle'."
  6997. (interactive "rP")
  6998. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  6999. region cols
  7000. (rpl (if cut " " nil)))
  7001. (goto-char beg)
  7002. (org-table-check-inside-data-field)
  7003. (setq l01 (org-current-line)
  7004. c01 (org-table-current-column))
  7005. (goto-char end)
  7006. (org-table-check-inside-data-field)
  7007. (setq l02 (org-current-line)
  7008. c02 (org-table-current-column))
  7009. (setq l1 (min l01 l02) l2 (max l01 l02)
  7010. c1 (min c01 c02) c2 (max c01 c02))
  7011. (catch 'exit
  7012. (while t
  7013. (catch 'nextline
  7014. (if (> l1 l2) (throw 'exit t))
  7015. (goto-line l1)
  7016. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7017. (setq cols nil ic1 c1 ic2 c2)
  7018. (while (< ic1 (1+ ic2))
  7019. (push (org-table-get-field ic1 rpl) cols)
  7020. (setq ic1 (1+ ic1)))
  7021. (push (nreverse cols) region)
  7022. (setq l1 (1+ l1)))))
  7023. (setq org-table-clip (nreverse region))
  7024. (if cut (org-table-align))
  7025. org-table-clip))
  7026. (defun org-table-paste-rectangle ()
  7027. "Paste a rectangular region into a table.
  7028. The upper right corner ends up in the current field. All involved fields
  7029. will be overwritten. If the rectangle does not fit into the present table,
  7030. the table is enlarged as needed. The process ignores horizontal separator
  7031. lines."
  7032. (interactive)
  7033. (unless (and org-table-clip (listp org-table-clip))
  7034. (error "First cut/copy a region to paste!"))
  7035. (org-table-check-inside-data-field)
  7036. (let* ((clip org-table-clip)
  7037. (line (org-current-line))
  7038. (col (org-table-current-column))
  7039. (org-enable-table-editor t)
  7040. (org-table-automatic-realign nil)
  7041. c cols field)
  7042. (while (setq cols (pop clip))
  7043. (while (org-at-table-hline-p) (beginning-of-line 2))
  7044. (if (not (org-at-table-p))
  7045. (progn (end-of-line 0) (org-table-next-field)))
  7046. (setq c col)
  7047. (while (setq field (pop cols))
  7048. (org-table-goto-column c nil 'force)
  7049. (org-table-get-field nil field)
  7050. (setq c (1+ c)))
  7051. (beginning-of-line 2))
  7052. (goto-line line)
  7053. (org-table-goto-column col)
  7054. (org-table-align)))
  7055. (defun org-table-convert ()
  7056. "Convert from `org-mode' table to table.el and back.
  7057. Obviously, this only works within limits. When an Org-mode table is
  7058. converted to table.el, all horizontal separator lines get lost, because
  7059. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7060. A table.el table can be converted to an Org-mode table only if it does not
  7061. do row or column spanning. Multiline cells will become multiple cells.
  7062. Beware, Org-mode does not test if the table can be successfully converted - it
  7063. blindly applies a recipe that works for simple tables."
  7064. (interactive)
  7065. (require 'table)
  7066. (if (org-at-table.el-p)
  7067. ;; convert to Org-mode table
  7068. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7069. (end (move-marker (make-marker) (org-table-end t))))
  7070. (table-unrecognize-region beg end)
  7071. (goto-char beg)
  7072. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7073. (replace-match ""))
  7074. (goto-char beg))
  7075. (if (org-at-table-p)
  7076. ;; convert to table.el table
  7077. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7078. (end (move-marker (make-marker) (org-table-end))))
  7079. ;; first, get rid of all horizontal lines
  7080. (goto-char beg)
  7081. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7082. (replace-match ""))
  7083. ;; insert a hline before first
  7084. (goto-char beg)
  7085. (org-table-insert-hline 'above)
  7086. (beginning-of-line -1)
  7087. ;; insert a hline after each line
  7088. (while (progn (beginning-of-line 3) (< (point) end))
  7089. (org-table-insert-hline))
  7090. (goto-char beg)
  7091. (setq end (move-marker end (org-table-end)))
  7092. ;; replace "+" at beginning and ending of hlines
  7093. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  7094. (replace-match "\\1+-"))
  7095. (goto-char beg)
  7096. (while (re-search-forward "-|[ \t]*$" end t)
  7097. (replace-match "-+"))
  7098. (goto-char beg)))))
  7099. (defun org-table-wrap-region (arg)
  7100. "Wrap several fields in a column like a paragraph.
  7101. This is useful if you'd like to spread the contents of a field over several
  7102. lines, in order to keep the table compact.
  7103. If there is an active region, and both point and mark are in the same column,
  7104. the text in the column is wrapped to minimum width for the given number of
  7105. lines. Generally, this makes the table more compact. A prefix ARG may be
  7106. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  7107. formats the selected text to two lines. If the region was longer than two
  7108. lines, the remaining lines remain empty. A negative prefix argument reduces
  7109. the current number of lines by that amount. The wrapped text is pasted back
  7110. into the table. If you formatted it to more lines than it was before, fields
  7111. further down in the table get overwritten - so you might need to make space in
  7112. the table first.
  7113. If there is no region, the current field is split at the cursor position and
  7114. the text fragment to the right of the cursor is prepended to the field one
  7115. line down.
  7116. If there is no region, but you specify a prefix ARG, the current field gets
  7117. blank, and the content is appended to the field above."
  7118. (interactive "P")
  7119. (org-table-check-inside-data-field)
  7120. (if (org-region-active-p)
  7121. ;; There is a region: fill as a paragraph
  7122. (let* ((beg (region-beginning))
  7123. (cline (save-excursion (goto-char beg) (org-current-line)))
  7124. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  7125. nlines)
  7126. (org-table-cut-region (region-beginning) (region-end))
  7127. (if (> (length (car org-table-clip)) 1)
  7128. (error "Region must be limited to single column"))
  7129. (setq nlines (if arg
  7130. (if (< arg 1)
  7131. (+ (length org-table-clip) arg)
  7132. arg)
  7133. (length org-table-clip)))
  7134. (setq org-table-clip
  7135. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  7136. nil nlines)))
  7137. (goto-line cline)
  7138. (org-table-goto-column ccol)
  7139. (org-table-paste-rectangle))
  7140. ;; No region, split the current field at point
  7141. (if arg
  7142. ;; combine with field above
  7143. (let ((s (org-table-blank-field))
  7144. (col (org-table-current-column)))
  7145. (beginning-of-line 0)
  7146. (while (org-at-table-hline-p) (beginning-of-line 0))
  7147. (org-table-goto-column col)
  7148. (skip-chars-forward "^|")
  7149. (skip-chars-backward " ")
  7150. (insert " " (org-trim s))
  7151. (org-table-align))
  7152. ;; split field
  7153. (when (looking-at "\\([^|]+\\)+|")
  7154. (let ((s (match-string 1)))
  7155. (replace-match " |")
  7156. (goto-char (match-beginning 0))
  7157. (org-table-next-row)
  7158. (insert (org-trim s) " ")
  7159. (org-table-align))))))
  7160. (defvar org-field-marker nil)
  7161. (defun org-table-edit-field (arg)
  7162. "Edit table field in a different window.
  7163. This is mainly useful for fields that contain hidden parts.
  7164. When called with a \\[universal-argument] prefix, just make the full field visible so that
  7165. it can be edited in place."
  7166. (interactive "P")
  7167. (if arg
  7168. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  7169. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  7170. (remove-text-properties b e '(org-cwidth t invisible t
  7171. display t intangible t))
  7172. (if (and (boundp 'font-lock-mode) font-lock-mode)
  7173. (font-lock-fontify-block)))
  7174. (let ((pos (move-marker (make-marker) (point)))
  7175. (field (org-table-get-field))
  7176. (cw (current-window-configuration))
  7177. p)
  7178. (switch-to-buffer-other-window "*Org tmp*")
  7179. (erase-buffer)
  7180. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  7181. (let ((org-inhibit-startup t)) (org-mode))
  7182. (goto-char (setq p (point-max)))
  7183. (insert (org-trim field))
  7184. (remove-text-properties p (point-max)
  7185. '(invisible t org-cwidth t display t
  7186. intangible t))
  7187. (goto-char p)
  7188. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  7189. (org-set-local 'org-window-configuration cw)
  7190. (org-set-local 'org-field-marker pos)
  7191. (message "Edit and finish with C-c C-c"))))
  7192. (defun org-table-finish-edit-field ()
  7193. "Finish editing a table data field.
  7194. Remove all newline characters, insert the result into the table, realign
  7195. the table and kill the editing buffer."
  7196. (let ((pos org-field-marker)
  7197. (cw org-window-configuration)
  7198. (cb (current-buffer))
  7199. text)
  7200. (goto-char (point-min))
  7201. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  7202. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  7203. (replace-match " "))
  7204. (setq text (org-trim (buffer-string)))
  7205. (set-window-configuration cw)
  7206. (kill-buffer cb)
  7207. (select-window (get-buffer-window (marker-buffer pos)))
  7208. (goto-char pos)
  7209. (move-marker pos nil)
  7210. (org-table-check-inside-data-field)
  7211. (org-table-get-field nil text)
  7212. (org-table-align)
  7213. (message "New field value inserted")))
  7214. (defun org-trim (s)
  7215. "Remove whitespace at beginning and end of string."
  7216. (if (string-match "^[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  7217. (if (string-match "[ \t\n\r]+$" s) (setq s (replace-match "" t t s)))
  7218. s)
  7219. (defun org-wrap (string &optional width lines)
  7220. "Wrap string to either a number of lines, or a width in characters.
  7221. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  7222. that costs. If there is a word longer than WIDTH, the text is actually
  7223. wrapped to the length of that word.
  7224. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  7225. many lines, whatever width that takes.
  7226. The return value is a list of lines, without newlines at the end."
  7227. (let* ((words (org-split-string string "[ \t\n]+"))
  7228. (maxword (apply 'max (mapcar 'org-string-width words)))
  7229. w ll)
  7230. (cond (width
  7231. (org-do-wrap words (max maxword width)))
  7232. (lines
  7233. (setq w maxword)
  7234. (setq ll (org-do-wrap words maxword))
  7235. (if (<= (length ll) lines)
  7236. ll
  7237. (setq ll words)
  7238. (while (> (length ll) lines)
  7239. (setq w (1+ w))
  7240. (setq ll (org-do-wrap words w)))
  7241. ll))
  7242. (t (error "Cannot wrap this")))))
  7243. (defun org-do-wrap (words width)
  7244. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  7245. (let (lines line)
  7246. (while words
  7247. (setq line (pop words))
  7248. (while (and words (< (+ (length line) (length (car words))) width))
  7249. (setq line (concat line " " (pop words))))
  7250. (setq lines (push line lines)))
  7251. (nreverse lines)))
  7252. (defun org-split-string (string &optional separators)
  7253. "Splits STRING into substrings at SEPARATORS.
  7254. No empty strings are returned if there are matches at the beginning
  7255. and end of string."
  7256. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  7257. (start 0)
  7258. notfirst
  7259. (list nil))
  7260. (while (and (string-match rexp string
  7261. (if (and notfirst
  7262. (= start (match-beginning 0))
  7263. (< start (length string)))
  7264. (1+ start) start))
  7265. (< (match-beginning 0) (length string)))
  7266. (setq notfirst t)
  7267. (or (eq (match-beginning 0) 0)
  7268. (and (eq (match-beginning 0) (match-end 0))
  7269. (eq (match-beginning 0) start))
  7270. (setq list
  7271. (cons (substring string start (match-beginning 0))
  7272. list)))
  7273. (setq start (match-end 0)))
  7274. (or (eq start (length string))
  7275. (setq list
  7276. (cons (substring string start)
  7277. list)))
  7278. (nreverse list)))
  7279. (defun org-table-map-tables (function)
  7280. "Apply FUNCTION to the start of all tables in the buffer."
  7281. (save-excursion
  7282. (save-restriction
  7283. (widen)
  7284. (goto-char (point-min))
  7285. (while (re-search-forward org-table-any-line-regexp nil t)
  7286. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  7287. (beginning-of-line 1)
  7288. (if (looking-at org-table-line-regexp)
  7289. (save-excursion (funcall function)))
  7290. (re-search-forward org-table-any-border-regexp nil 1))))
  7291. (message "Mapping tables: done"))
  7292. (defvar org-timecnt) ; dynamically scoped parameter
  7293. (defun org-table-sum (&optional beg end nlast)
  7294. "Sum numbers in region of current table column.
  7295. The result will be displayed in the echo area, and will be available
  7296. as kill to be inserted with \\[yank].
  7297. If there is an active region, it is interpreted as a rectangle and all
  7298. numbers in that rectangle will be summed. If there is no active
  7299. region and point is located in a table column, sum all numbers in that
  7300. column.
  7301. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  7302. numbers are assumed to be times as well (in decimal hours) and the
  7303. numbers are added as such.
  7304. If NLAST is a number, only the NLAST fields will actually be summed."
  7305. (interactive)
  7306. (save-excursion
  7307. (let (col (org-timecnt 0) diff h m s org-table-clip)
  7308. (cond
  7309. ((and beg end)) ; beg and end given explicitly
  7310. ((org-region-active-p)
  7311. (setq beg (region-beginning) end (region-end)))
  7312. (t
  7313. (setq col (org-table-current-column))
  7314. (goto-char (org-table-begin))
  7315. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  7316. (error "No table data"))
  7317. (org-table-goto-column col)
  7318. (setq beg (point))
  7319. (goto-char (org-table-end))
  7320. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  7321. (error "No table data"))
  7322. (org-table-goto-column col)
  7323. (setq end (point))))
  7324. (let* ((items (apply 'append (org-table-copy-region beg end)))
  7325. (items1 (cond ((not nlast) items)
  7326. ((>= nlast (length items)) items)
  7327. (t (setq items (reverse items))
  7328. (setcdr (nthcdr (1- nlast) items) nil)
  7329. (nreverse items))))
  7330. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  7331. items1)))
  7332. (res (apply '+ numbers))
  7333. (sres (if (= org-timecnt 0)
  7334. (format "%g" res)
  7335. (setq diff (* 3600 res)
  7336. h (floor (/ diff 3600)) diff (mod diff 3600)
  7337. m (floor (/ diff 60)) diff (mod diff 60)
  7338. s diff)
  7339. (format "%d:%02d:%02d" h m s))))
  7340. (kill-new sres)
  7341. (if (interactive-p)
  7342. (message "%s"
  7343. (substitute-command-keys
  7344. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  7345. (length numbers) sres))))
  7346. sres))))
  7347. (defun org-table-get-number-for-summing (s)
  7348. (let (n)
  7349. (if (string-match "^ *|? *" s)
  7350. (setq s (replace-match "" nil nil s)))
  7351. (if (string-match " *|? *$" s)
  7352. (setq s (replace-match "" nil nil s)))
  7353. (setq n (string-to-number s))
  7354. (cond
  7355. ((and (string-match "0" s)
  7356. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  7357. ((string-match "\\`[ \t]+\\'" s) nil)
  7358. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  7359. (let ((h (string-to-number (or (match-string 1 s) "0")))
  7360. (m (string-to-number (or (match-string 2 s) "0")))
  7361. (s (string-to-number (or (match-string 4 s) "0"))))
  7362. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  7363. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  7364. ((equal n 0) nil)
  7365. (t n))))
  7366. (defun org-table-current-field-formula (&optional key noerror)
  7367. "Return the formula active for the current field.
  7368. Assumes that specials are in place.
  7369. If KEY is given, return the key to this formula.
  7370. Otherwise return the formula preceeded with \"=\" or \":=\"."
  7371. (let* ((name (car (rassoc (list (org-current-line)
  7372. (org-table-current-column))
  7373. org-table-named-field-locations)))
  7374. (col (org-table-current-column))
  7375. (scol (int-to-string col))
  7376. (ref (format "@%d$%d" (org-table-current-dline) col))
  7377. (stored-list (org-table-get-stored-formulas noerror))
  7378. (ass (or (assoc name stored-list)
  7379. (assoc ref stored-list)
  7380. (assoc scol stored-list))))
  7381. (if key
  7382. (car ass)
  7383. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  7384. (cdr ass))))))
  7385. (defun org-table-get-formula (&optional equation named)
  7386. "Read a formula from the minibuffer, offer stored formula as default.
  7387. When NAMED is non-nil, look for a named equation."
  7388. (let* ((stored-list (org-table-get-stored-formulas))
  7389. (name (car (rassoc (list (org-current-line)
  7390. (org-table-current-column))
  7391. org-table-named-field-locations)))
  7392. (ref (format "@%d$%d" (org-table-current-dline)
  7393. (org-table-current-column)))
  7394. (refass (assoc ref stored-list))
  7395. (scol (if named
  7396. (if name name ref)
  7397. (int-to-string (org-table-current-column))))
  7398. (dummy (and (or name refass) (not named)
  7399. (not (y-or-n-p "Replace field formula with column formula? " ))
  7400. (error "Abort")))
  7401. (name (or name ref))
  7402. (org-table-may-need-update nil)
  7403. (stored (cdr (assoc scol stored-list)))
  7404. (eq (cond
  7405. ((and stored equation (string-match "^ *=? *$" equation))
  7406. stored)
  7407. ((stringp equation)
  7408. equation)
  7409. (t (org-table-formula-from-user
  7410. (read-string
  7411. (org-table-formula-to-user
  7412. (format "%s formula %s%s="
  7413. (if named "Field" "Column")
  7414. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  7415. scol))
  7416. (if stored (org-table-formula-to-user stored) "")
  7417. 'org-table-formula-history
  7418. )))))
  7419. mustsave)
  7420. (when (not (string-match "\\S-" eq))
  7421. ;; remove formula
  7422. (setq stored-list (delq (assoc scol stored-list) stored-list))
  7423. (org-table-store-formulas stored-list)
  7424. (error "Formula removed"))
  7425. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  7426. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  7427. (if (and name (not named))
  7428. ;; We set the column equation, delete the named one.
  7429. (setq stored-list (delq (assoc name stored-list) stored-list)
  7430. mustsave t))
  7431. (if stored
  7432. (setcdr (assoc scol stored-list) eq)
  7433. (setq stored-list (cons (cons scol eq) stored-list)))
  7434. (if (or mustsave (not (equal stored eq)))
  7435. (org-table-store-formulas stored-list))
  7436. eq))
  7437. (defun org-table-store-formulas (alist)
  7438. "Store the list of formulas below the current table."
  7439. (setq alist (sort alist 'org-table-formula-less-p))
  7440. (save-excursion
  7441. (goto-char (org-table-end))
  7442. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  7443. (progn
  7444. ;; don't overwrite TBLFM, we might use text properties to store stuff
  7445. (goto-char (match-beginning 2))
  7446. (delete-region (match-beginning 2) (match-end 0)))
  7447. (insert "#+TBLFM:"))
  7448. (insert " "
  7449. (mapconcat (lambda (x)
  7450. (concat
  7451. (if (equal (string-to-char (car x)) ?@) "" "$")
  7452. (car x) "=" (cdr x)))
  7453. alist "::")
  7454. "\n")))
  7455. (defsubst org-table-formula-make-cmp-string (a)
  7456. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  7457. (concat
  7458. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  7459. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  7460. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  7461. (defun org-table-formula-less-p (a b)
  7462. "Compare two formulas for sorting."
  7463. (let ((as (org-table-formula-make-cmp-string (car a)))
  7464. (bs (org-table-formula-make-cmp-string (car b))))
  7465. (and as bs (string< as bs))))
  7466. (defun org-table-get-stored-formulas (&optional noerror)
  7467. "Return an alist with the stored formulas directly after current table."
  7468. (interactive)
  7469. (let (scol eq eq-alist strings string seen)
  7470. (save-excursion
  7471. (goto-char (org-table-end))
  7472. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  7473. (setq strings (org-split-string (match-string 2) " *:: *"))
  7474. (while (setq string (pop strings))
  7475. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  7476. (setq scol (if (match-end 2)
  7477. (match-string 2 string)
  7478. (match-string 1 string))
  7479. eq (match-string 3 string)
  7480. eq-alist (cons (cons scol eq) eq-alist))
  7481. (if (member scol seen)
  7482. (if noerror
  7483. (progn
  7484. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  7485. (ding)
  7486. (sit-for 2))
  7487. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  7488. (push scol seen))))))
  7489. (nreverse eq-alist)))
  7490. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  7491. "Modify the equations after the table structure has been edited.
  7492. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  7493. For all numbers larger than LIMIT, shift them by DELTA."
  7494. (save-excursion
  7495. (goto-char (org-table-end))
  7496. (when (looking-at "#\\+TBLFM:")
  7497. (let ((re (concat key "\\([0-9]+\\)"))
  7498. (re2
  7499. (when remove
  7500. (if (equal key "$")
  7501. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  7502. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  7503. s n a)
  7504. (when remove
  7505. (while (re-search-forward re2 (point-at-eol) t)
  7506. (replace-match "")))
  7507. (while (re-search-forward re (point-at-eol) t)
  7508. (setq s (match-string 1) n (string-to-number s))
  7509. (cond
  7510. ((setq a (assoc s replace))
  7511. (replace-match (concat key (cdr a)) t t))
  7512. ((and limit (> n limit))
  7513. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  7514. (defun org-table-get-specials ()
  7515. "Get the column names and local parameters for this table."
  7516. (save-excursion
  7517. (let ((beg (org-table-begin)) (end (org-table-end))
  7518. names name fields fields1 field cnt
  7519. c v l line col types dlines hlines)
  7520. (setq org-table-column-names nil
  7521. org-table-local-parameters nil
  7522. org-table-named-field-locations nil
  7523. org-table-current-begin-line nil
  7524. org-table-current-begin-pos nil
  7525. org-table-current-line-types nil)
  7526. (goto-char beg)
  7527. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  7528. (setq names (org-split-string (match-string 1) " *| *")
  7529. cnt 1)
  7530. (while (setq name (pop names))
  7531. (setq cnt (1+ cnt))
  7532. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  7533. (push (cons name (int-to-string cnt)) org-table-column-names))))
  7534. (setq org-table-column-names (nreverse org-table-column-names))
  7535. (setq org-table-column-name-regexp
  7536. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  7537. (goto-char beg)
  7538. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  7539. (setq fields (org-split-string (match-string 1) " *| *"))
  7540. (while (setq field (pop fields))
  7541. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  7542. (push (cons (match-string 1 field) (match-string 2 field))
  7543. org-table-local-parameters))))
  7544. (goto-char beg)
  7545. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  7546. (setq c (match-string 1)
  7547. fields (org-split-string (match-string 2) " *| *"))
  7548. (save-excursion
  7549. (beginning-of-line (if (equal c "_") 2 0))
  7550. (setq line (org-current-line) col 1)
  7551. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  7552. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  7553. (while (and fields1 (setq field (pop fields)))
  7554. (setq v (pop fields1) col (1+ col))
  7555. (when (and (stringp field) (stringp v)
  7556. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  7557. (push (cons field v) org-table-local-parameters)
  7558. (push (list field line col) org-table-named-field-locations))))
  7559. ;; Analyse the line types
  7560. (goto-char beg)
  7561. (setq org-table-current-begin-line (org-current-line)
  7562. org-table-current-begin-pos (point)
  7563. l org-table-current-begin-line)
  7564. (while (looking-at "[ \t]*|\\(-\\)?")
  7565. (push (if (match-end 1) 'hline 'dline) types)
  7566. (if (match-end 1) (push l hlines) (push l dlines))
  7567. (beginning-of-line 2)
  7568. (setq l (1+ l)))
  7569. (setq org-table-current-line-types (apply 'vector (nreverse types))
  7570. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  7571. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  7572. (defun org-table-maybe-eval-formula ()
  7573. "Check if the current field starts with \"=\" or \":=\".
  7574. If yes, store the formula and apply it."
  7575. ;; We already know we are in a table. Get field will only return a formula
  7576. ;; when appropriate. It might return a separator line, but no problem.
  7577. (when org-table-formula-evaluate-inline
  7578. (let* ((field (org-trim (or (org-table-get-field) "")))
  7579. named eq)
  7580. (when (string-match "^:?=\\(.*\\)" field)
  7581. (setq named (equal (string-to-char field) ?:)
  7582. eq (match-string 1 field))
  7583. (if (or (fboundp 'calc-eval)
  7584. (equal (substring eq 0 (min 2 (length eq))) "'("))
  7585. (org-table-eval-formula (if named '(4) nil)
  7586. (org-table-formula-from-user eq))
  7587. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  7588. (defvar org-recalc-commands nil
  7589. "List of commands triggering the recalculation of a line.
  7590. Will be filled automatically during use.")
  7591. (defvar org-recalc-marks
  7592. '((" " . "Unmarked: no special line, no automatic recalculation")
  7593. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  7594. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  7595. ("!" . "Column name definition line. Reference in formula as $name.")
  7596. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  7597. ("_" . "Names for values in row below this one.")
  7598. ("^" . "Names for values in row above this one.")))
  7599. (defun org-table-rotate-recalc-marks (&optional newchar)
  7600. "Rotate the recalculation mark in the first column.
  7601. If in any row, the first field is not consistent with a mark,
  7602. insert a new column for the markers.
  7603. When there is an active region, change all the lines in the region,
  7604. after prompting for the marking character.
  7605. After each change, a message will be displayed indicating the meaning
  7606. of the new mark."
  7607. (interactive)
  7608. (unless (org-at-table-p) (error "Not at a table"))
  7609. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  7610. (beg (org-table-begin))
  7611. (end (org-table-end))
  7612. (l (org-current-line))
  7613. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  7614. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  7615. (have-col
  7616. (save-excursion
  7617. (goto-char beg)
  7618. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  7619. (col (org-table-current-column))
  7620. (forcenew (car (assoc newchar org-recalc-marks)))
  7621. epos new)
  7622. (when l1
  7623. (message "Change region to what mark? Type # * ! $ or SPC: ")
  7624. (setq newchar (char-to-string (read-char-exclusive))
  7625. forcenew (car (assoc newchar org-recalc-marks))))
  7626. (if (and newchar (not forcenew))
  7627. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  7628. newchar))
  7629. (if l1 (goto-line l1))
  7630. (save-excursion
  7631. (beginning-of-line 1)
  7632. (unless (looking-at org-table-dataline-regexp)
  7633. (error "Not at a table data line")))
  7634. (unless have-col
  7635. (org-table-goto-column 1)
  7636. (org-table-insert-column)
  7637. (org-table-goto-column (1+ col)))
  7638. (setq epos (point-at-eol))
  7639. (save-excursion
  7640. (beginning-of-line 1)
  7641. (org-table-get-field
  7642. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  7643. (concat " "
  7644. (setq new (or forcenew
  7645. (cadr (member (match-string 1) marks))))
  7646. " ")
  7647. " # ")))
  7648. (if (and l1 l2)
  7649. (progn
  7650. (goto-line l1)
  7651. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  7652. (and (looking-at org-table-dataline-regexp)
  7653. (org-table-get-field 1 (concat " " new " "))))
  7654. (goto-line l1)))
  7655. (if (not (= epos (point-at-eol))) (org-table-align))
  7656. (goto-line l)
  7657. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  7658. (defun org-table-maybe-recalculate-line ()
  7659. "Recompute the current line if marked for it, and if we haven't just done it."
  7660. (interactive)
  7661. (and org-table-allow-automatic-line-recalculation
  7662. (not (and (memq last-command org-recalc-commands)
  7663. (equal org-last-recalc-line (org-current-line))))
  7664. (save-excursion (beginning-of-line 1)
  7665. (looking-at org-table-auto-recalculate-regexp))
  7666. (org-table-recalculate) t))
  7667. (defvar org-table-formula-debug nil
  7668. "Non-nil means, debug table formulas.
  7669. When nil, simply write \"#ERROR\" in corrupted fields.")
  7670. (make-variable-buffer-local 'org-table-formula-debug)
  7671. (defvar modes)
  7672. (defsubst org-set-calc-mode (var &optional value)
  7673. (if (stringp var)
  7674. (setq var (assoc var '(("D" calc-angle-mode deg)
  7675. ("R" calc-angle-mode rad)
  7676. ("F" calc-prefer-frac t)
  7677. ("S" calc-symbolic-mode t)))
  7678. value (nth 2 var) var (nth 1 var)))
  7679. (if (memq var modes)
  7680. (setcar (cdr (memq var modes)) value)
  7681. (cons var (cons value modes)))
  7682. modes)
  7683. (defun org-table-eval-formula (&optional arg equation
  7684. suppress-align suppress-const
  7685. suppress-store suppress-analysis)
  7686. "Replace the table field value at the cursor by the result of a calculation.
  7687. This function makes use of Dave Gillespie's Calc package, in my view the
  7688. most exciting program ever written for GNU Emacs. So you need to have Calc
  7689. installed in order to use this function.
  7690. In a table, this command replaces the value in the current field with the
  7691. result of a formula. It also installs the formula as the \"current\" column
  7692. formula, by storing it in a special line below the table. When called
  7693. with a `C-u' prefix, the current field must ba a named field, and the
  7694. formula is installed as valid in only this specific field.
  7695. When called with two `C-u' prefixes, insert the active equation
  7696. for the field back into the current field, so that it can be
  7697. edited there. This is useful in order to use \\[org-table-show-reference]
  7698. to check the referenced fields.
  7699. When called, the command first prompts for a formula, which is read in
  7700. the minibuffer. Previously entered formulas are available through the
  7701. history list, and the last used formula is offered as a default.
  7702. These stored formulas are adapted correctly when moving, inserting, or
  7703. deleting columns with the corresponding commands.
  7704. The formula can be any algebraic expression understood by the Calc package.
  7705. For details, see the Org-mode manual.
  7706. This function can also be called from Lisp programs and offers
  7707. additional arguments: EQUATION can be the formula to apply. If this
  7708. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  7709. used to speed-up recursive calls by by-passing unnecessary aligns.
  7710. SUPPRESS-CONST suppresses the interpretation of constants in the
  7711. formula, assuming that this has been done already outside the function.
  7712. SUPPRESS-STORE means the formula should not be stored, either because
  7713. it is already stored, or because it is a modified equation that should
  7714. not overwrite the stored one."
  7715. (interactive "P")
  7716. (org-table-check-inside-data-field)
  7717. (or suppress-analysis (org-table-get-specials))
  7718. (if (equal arg '(16))
  7719. (let ((eq (org-table-current-field-formula)))
  7720. (or eq (error "No equation active for current field"))
  7721. (org-table-get-field nil eq)
  7722. (org-table-align)
  7723. (setq org-table-may-need-update t))
  7724. (let* (fields
  7725. (ndown (if (integerp arg) arg 1))
  7726. (org-table-automatic-realign nil)
  7727. (case-fold-search nil)
  7728. (down (> ndown 1))
  7729. (formula (if (and equation suppress-store)
  7730. equation
  7731. (org-table-get-formula equation (equal arg '(4)))))
  7732. (n0 (org-table-current-column))
  7733. (modes (copy-sequence org-calc-default-modes))
  7734. (numbers nil) ; was a variable, now fixed default
  7735. (keep-empty nil)
  7736. n form form0 bw fmt x ev orig c lispp literal)
  7737. ;; Parse the format string. Since we have a lot of modes, this is
  7738. ;; a lot of work. However, I think calc still uses most of the time.
  7739. (if (string-match ";" formula)
  7740. (let ((tmp (org-split-string formula ";")))
  7741. (setq formula (car tmp)
  7742. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  7743. (nth 1 tmp)))
  7744. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  7745. (setq c (string-to-char (match-string 1 fmt))
  7746. n (string-to-number (match-string 2 fmt)))
  7747. (if (= c ?p)
  7748. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  7749. (setq modes (org-set-calc-mode
  7750. 'calc-float-format
  7751. (list (cdr (assoc c '((?n . float) (?f . fix)
  7752. (?s . sci) (?e . eng))))
  7753. n))))
  7754. (setq fmt (replace-match "" t t fmt)))
  7755. (if (string-match "[NT]" fmt)
  7756. (setq numbers (equal (match-string 0 fmt) "N")
  7757. fmt (replace-match "" t t fmt)))
  7758. (if (string-match "L" fmt)
  7759. (setq literal t
  7760. fmt (replace-match "" t t fmt)))
  7761. (if (string-match "E" fmt)
  7762. (setq keep-empty t
  7763. fmt (replace-match "" t t fmt)))
  7764. (while (string-match "[DRFS]" fmt)
  7765. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  7766. (setq fmt (replace-match "" t t fmt)))
  7767. (unless (string-match "\\S-" fmt)
  7768. (setq fmt nil))))
  7769. (if (and (not suppress-const) org-table-formula-use-constants)
  7770. (setq formula (org-table-formula-substitute-names formula)))
  7771. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  7772. (while (> ndown 0)
  7773. (setq fields (org-split-string
  7774. (org-no-properties
  7775. (buffer-substring (point-at-bol) (point-at-eol)))
  7776. " *| *"))
  7777. (if (eq numbers t)
  7778. (setq fields (mapcar
  7779. (lambda (x) (number-to-string (string-to-number x)))
  7780. fields)))
  7781. (setq ndown (1- ndown))
  7782. (setq form (copy-sequence formula)
  7783. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  7784. (if (and lispp literal) (setq lispp 'literal))
  7785. ;; Check for old vertical references
  7786. (setq form (org-rewrite-old-row-references form))
  7787. ;; Insert complex ranges
  7788. (while (string-match org-table-range-regexp form)
  7789. (setq form
  7790. (replace-match
  7791. (save-match-data
  7792. (org-table-make-reference
  7793. (org-table-get-range (match-string 0 form) nil n0)
  7794. keep-empty numbers lispp))
  7795. t t form)))
  7796. ;; Insert simple ranges
  7797. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  7798. (setq form
  7799. (replace-match
  7800. (save-match-data
  7801. (org-table-make-reference
  7802. (org-sublist
  7803. fields (string-to-number (match-string 1 form))
  7804. (string-to-number (match-string 2 form)))
  7805. keep-empty numbers lispp))
  7806. t t form)))
  7807. (setq form0 form)
  7808. ;; Insert the references to fields in same row
  7809. (while (string-match "\\$\\([0-9]+\\)" form)
  7810. (setq n (string-to-number (match-string 1 form))
  7811. x (nth (1- (if (= n 0) n0 n)) fields))
  7812. (unless x (error "Invalid field specifier \"%s\""
  7813. (match-string 0 form)))
  7814. (setq form (replace-match
  7815. (save-match-data
  7816. (org-table-make-reference x nil numbers lispp))
  7817. t t form)))
  7818. (if lispp
  7819. (setq ev (condition-case nil
  7820. (eval (eval (read form)))
  7821. (error "#ERROR"))
  7822. ev (if (numberp ev) (number-to-string ev) ev))
  7823. (or (fboundp 'calc-eval)
  7824. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  7825. (setq ev (calc-eval (cons form modes)
  7826. (if numbers 'num))))
  7827. (when org-table-formula-debug
  7828. (with-output-to-temp-buffer "*Substitution History*"
  7829. (princ (format "Substitution history of formula
  7830. Orig: %s
  7831. $xyz-> %s
  7832. @r$c-> %s
  7833. $1-> %s\n" orig formula form0 form))
  7834. (if (listp ev)
  7835. (princ (format " %s^\nError: %s"
  7836. (make-string (car ev) ?\-) (nth 1 ev)))
  7837. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  7838. ev (or fmt "NONE")
  7839. (if fmt (format fmt (string-to-number ev)) ev)))))
  7840. (setq bw (get-buffer-window "*Substitution History*"))
  7841. (shrink-window-if-larger-than-buffer bw)
  7842. (unless (and (interactive-p) (not ndown))
  7843. (unless (let (inhibit-redisplay)
  7844. (y-or-n-p "Debugging Formula. Continue to next? "))
  7845. (org-table-align)
  7846. (error "Abort"))
  7847. (delete-window bw)
  7848. (message "")))
  7849. (if (listp ev) (setq fmt nil ev "#ERROR"))
  7850. (org-table-justify-field-maybe
  7851. (if fmt (format fmt (string-to-number ev)) ev))
  7852. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  7853. (call-interactively 'org-return)
  7854. (setq ndown 0)))
  7855. (and down (org-table-maybe-recalculate-line))
  7856. (or suppress-align (and org-table-may-need-update
  7857. (org-table-align))))))
  7858. (defun org-table-put-field-property (prop value)
  7859. (save-excursion
  7860. (put-text-property (progn (skip-chars-backward "^|") (point))
  7861. (progn (skip-chars-forward "^|") (point))
  7862. prop value)))
  7863. (defun org-table-get-range (desc &optional tbeg col highlight)
  7864. "Get a calc vector from a column, accorting to descriptor DESC.
  7865. Optional arguments TBEG and COL can give the beginning of the table and
  7866. the current column, to avoid unnecessary parsing.
  7867. HIGHLIGHT means, just highlight the range."
  7868. (if (not (equal (string-to-char desc) ?@))
  7869. (setq desc (concat "@" desc)))
  7870. (save-excursion
  7871. (or tbeg (setq tbeg (org-table-begin)))
  7872. (or col (setq col (org-table-current-column)))
  7873. (let ((thisline (org-current-line))
  7874. beg end c1 c2 r1 r2 rangep tmp)
  7875. (unless (string-match org-table-range-regexp desc)
  7876. (error "Invalid table range specifier `%s'" desc))
  7877. (setq rangep (match-end 3)
  7878. r1 (and (match-end 1) (match-string 1 desc))
  7879. r2 (and (match-end 4) (match-string 4 desc))
  7880. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  7881. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  7882. (and c1 (setq c1 (+ (string-to-number c1)
  7883. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  7884. (and c2 (setq c2 (+ (string-to-number c2)
  7885. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  7886. (if (equal r1 "") (setq r1 nil))
  7887. (if (equal r2 "") (setq r2 nil))
  7888. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  7889. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  7890. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  7891. (if (not r1) (setq r1 thisline))
  7892. (if (not r2) (setq r2 thisline))
  7893. (if (not c1) (setq c1 col))
  7894. (if (not c2) (setq c2 col))
  7895. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  7896. ;; just one field
  7897. (progn
  7898. (goto-line r1)
  7899. (while (not (looking-at org-table-dataline-regexp))
  7900. (beginning-of-line 2))
  7901. (prog1 (org-table-get-field c1)
  7902. (if highlight (org-table-highlight-rectangle (point) (point)))))
  7903. ;; A range, return a vector
  7904. ;; First sort the numbers to get a regular ractangle
  7905. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  7906. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  7907. (goto-line r1)
  7908. (while (not (looking-at org-table-dataline-regexp))
  7909. (beginning-of-line 2))
  7910. (org-table-goto-column c1)
  7911. (setq beg (point))
  7912. (goto-line r2)
  7913. (while (not (looking-at org-table-dataline-regexp))
  7914. (beginning-of-line 0))
  7915. (org-table-goto-column c2)
  7916. (setq end (point))
  7917. (if highlight
  7918. (org-table-highlight-rectangle
  7919. beg (progn (skip-chars-forward "^|\n") (point))))
  7920. ;; return string representation of calc vector
  7921. (apply 'append (org-table-copy-region beg end))))))
  7922. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  7923. "Analyze descriptor DESC and retrieve the corresponding line number.
  7924. The cursor is currently in line CLINE, the table begins in line BLINE,
  7925. and TABLE is a vector with line types."
  7926. (if (string-match "^[0-9]+$" desc)
  7927. (aref org-table-dlines (string-to-number desc))
  7928. (setq cline (or cline (org-current-line))
  7929. bline (or bline org-table-current-begin-line)
  7930. table (or table org-table-current-line-types))
  7931. (if (or
  7932. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  7933. ;; 1 2 3 4 5 6
  7934. (and (not (match-end 3)) (not (match-end 6)))
  7935. (and (match-end 3) (match-end 6) (not (match-end 5))))
  7936. (error "invalid row descriptor `%s'" desc))
  7937. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  7938. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  7939. (odir (and (match-end 5) (match-string 5 desc)))
  7940. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  7941. (i (- cline bline))
  7942. (rel (and (match-end 6)
  7943. (or (and (match-end 1) (not (match-end 3)))
  7944. (match-end 5)))))
  7945. (if (and hn (not hdir))
  7946. (progn
  7947. (setq i 0 hdir "+")
  7948. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  7949. (if (and (not hn) on (not odir))
  7950. (error "should never happen");;(aref org-table-dlines on)
  7951. (if (and hn (> hn 0))
  7952. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  7953. (if on
  7954. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  7955. (+ bline i)))))
  7956. (defun org-find-row-type (table i type backwards relative n)
  7957. (let ((l (length table)))
  7958. (while (> n 0)
  7959. (while (and (setq i (+ i (if backwards -1 1)))
  7960. (>= i 0) (< i l)
  7961. (not (eq (aref table i) type))
  7962. (if (and relative (eq (aref table i) 'hline))
  7963. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  7964. t)))
  7965. (setq n (1- n)))
  7966. (if (or (< i 0) (>= i l))
  7967. (error "Row descriptior leads outside table")
  7968. i)))
  7969. (defun org-rewrite-old-row-references (s)
  7970. (if (string-match "&[-+0-9I]" s)
  7971. (error "Formula contains old &row reference, please rewrite using @-syntax")
  7972. s))
  7973. (defun org-table-make-reference (elements keep-empty numbers lispp)
  7974. "Convert list ELEMENTS to something appropriate to insert into formula.
  7975. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  7976. NUMBERS indicates that everything should be converted to numbers.
  7977. LISPP means to return something appropriate for a Lisp list."
  7978. (if (stringp elements) ; just a single val
  7979. (if lispp
  7980. (if (eq lispp 'literal)
  7981. elements
  7982. (prin1-to-string (if numbers (string-to-number elements) elements)))
  7983. (if (equal elements "") (setq elements "0"))
  7984. (if numbers (number-to-string (string-to-number elements)) elements))
  7985. (unless keep-empty
  7986. (setq elements
  7987. (delq nil
  7988. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  7989. elements))))
  7990. (setq elements (or elements '("0")))
  7991. (if lispp
  7992. (mapconcat
  7993. (lambda (x)
  7994. (if (eq lispp 'literal)
  7995. x
  7996. (prin1-to-string (if numbers (string-to-number x) x))))
  7997. " ")
  7998. (concat "[" (mapconcat
  7999. (lambda (x)
  8000. (if numbers (number-to-string (string-to-number x)) x))
  8001. elements
  8002. ",") "]"))))
  8003. (defun org-table-recalculate (&optional all noalign)
  8004. "Recalculate the current table line by applying all stored formulas.
  8005. With prefix arg ALL, do this for all lines in the table."
  8006. (interactive "P")
  8007. (or (memq this-command org-recalc-commands)
  8008. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8009. (unless (org-at-table-p) (error "Not at a table"))
  8010. (if (equal all '(16))
  8011. (org-table-iterate)
  8012. (org-table-get-specials)
  8013. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8014. (lambda (a b) (string< (car a) (car b)))))
  8015. (inhibit-redisplay (not debug-on-error))
  8016. (line-re org-table-dataline-regexp)
  8017. (thisline (org-current-line))
  8018. (thiscol (org-table-current-column))
  8019. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8020. ;; Insert constants in all formulas
  8021. (setq eqlist
  8022. (mapcar (lambda (x)
  8023. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8024. x)
  8025. eqlist))
  8026. ;; Split the equation list
  8027. (while (setq eq (pop eqlist))
  8028. (if (<= (string-to-char (car eq)) ?9)
  8029. (push eq eqlnum)
  8030. (push eq eqlname)))
  8031. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8032. (if all
  8033. (progn
  8034. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8035. (goto-char (setq beg (org-table-begin)))
  8036. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8037. ;; This is a table with marked lines, compute selected lines
  8038. (setq line-re org-table-recalculate-regexp)
  8039. ;; Move forward to the first non-header line
  8040. (if (and (re-search-forward org-table-dataline-regexp end t)
  8041. (re-search-forward org-table-hline-regexp end t)
  8042. (re-search-forward org-table-dataline-regexp end t))
  8043. (setq beg (match-beginning 0))
  8044. nil))) ;; just leave beg where it is
  8045. (setq beg (point-at-bol)
  8046. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8047. (goto-char beg)
  8048. (and all (message "Re-applying formulas to full table..."))
  8049. ;; First find the named fields, and mark them untouchanble
  8050. (remove-text-properties beg end '(org-untouchable t))
  8051. (while (setq eq (pop eqlname))
  8052. (setq name (car eq)
  8053. a (assoc name org-table-named-field-locations))
  8054. (and (not a)
  8055. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8056. (setq a (list name
  8057. (aref org-table-dlines
  8058. (string-to-number (match-string 1 name)))
  8059. (string-to-number (match-string 2 name)))))
  8060. (when (and a (or all (equal (nth 1 a) thisline)))
  8061. (message "Re-applying formula to field: %s" name)
  8062. (goto-line (nth 1 a))
  8063. (org-table-goto-column (nth 2 a))
  8064. (push (append a (list (cdr eq))) eqlname1)
  8065. ;; FIXME (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
  8066. ;; FIXME 'nostore 'noanalysis)
  8067. (org-table-put-field-property :org-untouchable t)))
  8068. ;; Now evauluate the column formulas, but skip fields covered by
  8069. ;; field formulas
  8070. (goto-char beg)
  8071. (while (re-search-forward line-re end t)
  8072. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8073. ;; Unprotected line, recalculate
  8074. (and all (message "Re-applying formulas to full table...(line %d)"
  8075. (setq cnt (1+ cnt))))
  8076. (setq org-last-recalc-line (org-current-line))
  8077. (setq eql eqlnum)
  8078. (while (setq entry (pop eql))
  8079. (goto-line org-last-recalc-line)
  8080. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8081. (unless (get-text-property (point) :org-untouchable)
  8082. (org-table-eval-formula nil (cdr entry)
  8083. 'noalign 'nocst 'nostore 'noanalysis)))))
  8084. ;; Now evaluate the field formulas
  8085. (while (setq eq (pop eqlname1))
  8086. (message "Re-applying formula to field: %s" (car eq))
  8087. (goto-line (nth 1 eq))
  8088. (org-table-goto-column (nth 2 eq))
  8089. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  8090. 'nostore 'noanalysis))
  8091. (goto-line thisline)
  8092. (org-table-goto-column thiscol)
  8093. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  8094. (or noalign (and org-table-may-need-update (org-table-align))
  8095. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  8096. ;; back to initial position
  8097. (message "Re-applying formulas...done")
  8098. (goto-line thisline)
  8099. (org-table-goto-column thiscol)
  8100. (or noalign (and org-table-may-need-update (org-table-align))
  8101. (and all (message "Re-applying formulas...done"))))))
  8102. (defun org-table-iterate (&optional arg)
  8103. "Recalculate the table until it does not change anymore."
  8104. (interactive "P")
  8105. (let ((imax (if arg (prefix-numeric-value arg) 10))
  8106. (i 0)
  8107. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  8108. thistbl)
  8109. (catch 'exit
  8110. (while (< i imax)
  8111. (setq i (1+ i))
  8112. (org-table-recalculate 'all)
  8113. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  8114. (if (not (string= lasttbl thistbl))
  8115. (setq lasttbl thistbl)
  8116. (if (> i 1)
  8117. (message "Convergence after %d iterations" i)
  8118. (message "Table was already stable"))
  8119. (throw 'exit t)))
  8120. (error "No convergence after %d iterations" i))))
  8121. (defun org-table-formula-substitute-names (f)
  8122. "Replace $const with values in string F."
  8123. (let ((start 0) a (f1 f))
  8124. ;; First, check for column names
  8125. (while (setq start (string-match org-table-column-name-regexp f start))
  8126. (setq start (1+ start))
  8127. (setq a (assoc (match-string 1 f) org-table-column-names))
  8128. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  8129. ;; Parameters and constants
  8130. (setq start 0)
  8131. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  8132. (setq start (1+ start))
  8133. (if (setq a (save-match-data
  8134. (org-table-get-constant (match-string 1 f))))
  8135. (setq f (replace-match (concat "(" a ")") t t f))))
  8136. (if org-table-formula-debug
  8137. (put-text-property 0 (length f) :orig-formula f1 f))
  8138. f))
  8139. (defun org-table-get-constant (const)
  8140. "Find the value for a parameter or constant in a formula.
  8141. Parameters get priority."
  8142. (or (cdr (assoc const org-table-local-parameters))
  8143. (cdr (assoc const org-table-formula-constants-local))
  8144. (cdr (assoc const org-table-formula-constants))
  8145. (and (fboundp 'constants-get) (constants-get const))
  8146. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  8147. (org-entry-get nil (substring const 5) 'inherit))
  8148. "#UNDEFINED_NAME"))
  8149. (defvar org-table-fedit-map (make-sparse-keymap))
  8150. (org-defkey org-table-fedit-map "\C-x\C-s" 'org-table-fedit-finish)
  8151. (org-defkey org-table-fedit-map "\C-c\C-s" 'org-table-fedit-finish)
  8152. (org-defkey org-table-fedit-map "\C-c\C-c" 'org-table-fedit-finish)
  8153. (org-defkey org-table-fedit-map "\C-c\C-q" 'org-table-fedit-abort)
  8154. (org-defkey org-table-fedit-map "\C-c?" 'org-table-show-reference)
  8155. (org-defkey org-table-fedit-map [(meta shift up)] 'org-table-fedit-line-up)
  8156. (org-defkey org-table-fedit-map [(meta shift down)] 'org-table-fedit-line-down)
  8157. (org-defkey org-table-fedit-map [(shift up)] 'org-table-fedit-ref-up)
  8158. (org-defkey org-table-fedit-map [(shift down)] 'org-table-fedit-ref-down)
  8159. (org-defkey org-table-fedit-map [(shift left)] 'org-table-fedit-ref-left)
  8160. (org-defkey org-table-fedit-map [(shift right)] 'org-table-fedit-ref-right)
  8161. (org-defkey org-table-fedit-map [(meta up)] 'org-table-fedit-scroll-down)
  8162. (org-defkey org-table-fedit-map [(meta down)] 'org-table-fedit-scroll)
  8163. (org-defkey org-table-fedit-map [(meta tab)] 'lisp-complete-symbol)
  8164. (org-defkey org-table-fedit-map "\M-\C-i" 'lisp-complete-symbol)
  8165. (org-defkey org-table-fedit-map [(tab)] 'org-table-fedit-lisp-indent)
  8166. (org-defkey org-table-fedit-map "\C-i" 'org-table-fedit-lisp-indent)
  8167. (org-defkey org-table-fedit-map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  8168. (org-defkey org-table-fedit-map "\C-c}" 'org-table-fedit-toggle-coordinates)
  8169. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  8170. '("Edit-Formulas"
  8171. ["Finish and Install" org-table-fedit-finish t]
  8172. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  8173. ["Abort" org-table-fedit-abort t]
  8174. "--"
  8175. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  8176. ["Complete Lisp Symbol" lisp-complete-symbol t]
  8177. "--"
  8178. "Shift Reference at Point"
  8179. ["Up" org-table-fedit-ref-up t]
  8180. ["Down" org-table-fedit-ref-down t]
  8181. ["Left" org-table-fedit-ref-left t]
  8182. ["Right" org-table-fedit-ref-right t]
  8183. "-"
  8184. "Change Test Row for Column Formulas"
  8185. ["Up" org-table-fedit-line-up t]
  8186. ["Down" org-table-fedit-line-down t]
  8187. "--"
  8188. ["Scroll Table Window" org-table-fedit-scroll t]
  8189. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  8190. ["Show Table Grid" org-table-fedit-toggle-coordinates
  8191. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  8192. org-table-overlay-coordinates)]
  8193. "--"
  8194. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  8195. :style toggle :selected org-table-buffer-is-an]))
  8196. (defvar org-pos)
  8197. (defun org-table-edit-formulas ()
  8198. "Edit the formulas of the current table in a separate buffer."
  8199. (interactive)
  8200. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  8201. (beginning-of-line 0))
  8202. (unless (org-at-table-p) (error "Not at a table"))
  8203. (org-table-get-specials)
  8204. (let ((key (org-table-current-field-formula 'key 'noerror))
  8205. (eql (sort (org-table-get-stored-formulas 'noerror)
  8206. 'org-table-formula-less-p))
  8207. (pos (move-marker (make-marker) (point)))
  8208. (startline 1)
  8209. (wc (current-window-configuration))
  8210. (titles '((column . "# Column Formulas\n")
  8211. (field . "# Field Formulas\n")
  8212. (named . "# Named Field Formulas\n")))
  8213. entry s type title)
  8214. (switch-to-buffer-other-window "*Edit Formulas*")
  8215. (erase-buffer)
  8216. ;; Keep global-font-lock-mode from turning on font-lock-mode
  8217. (let ((font-lock-global-modes '(not fundamental-mode)))
  8218. (fundamental-mode))
  8219. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  8220. (org-set-local 'org-pos pos)
  8221. (org-set-local 'org-window-configuration wc)
  8222. (use-local-map org-table-fedit-map)
  8223. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  8224. (easy-menu-add org-table-fedit-menu)
  8225. (setq startline (org-current-line))
  8226. (while (setq entry (pop eql))
  8227. (setq type (cond
  8228. ((equal (string-to-char (car entry)) ?@) 'field)
  8229. ((string-match "^[0-9]" (car entry)) 'column)
  8230. (t 'named)))
  8231. (when (setq title (assq type titles))
  8232. (or (bobp) (insert "\n"))
  8233. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  8234. (setq titles (delq title titles)))
  8235. (if (equal key (car entry)) (setq startline (org-current-line)))
  8236. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  8237. (car entry) " = " (cdr entry) "\n"))
  8238. (remove-text-properties 0 (length s) '(face nil) s)
  8239. (insert s))
  8240. (if (eq org-table-use-standard-references t)
  8241. (org-table-fedit-toggle-ref-type))
  8242. (goto-line startline)
  8243. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  8244. (defun org-table-fedit-post-command ()
  8245. (when (not (memq this-command '(lisp-complete-symbol)))
  8246. (let ((win (selected-window)))
  8247. (save-excursion
  8248. (condition-case nil
  8249. (org-table-show-reference)
  8250. (error nil))
  8251. (select-window win)))))
  8252. (defun org-table-formula-to-user (s)
  8253. "Convert a formula from internal to user representation."
  8254. (if (eq org-table-use-standard-references t)
  8255. (org-table-convert-refs-to-an s)
  8256. s))
  8257. (defun org-table-formula-from-user (s)
  8258. "Convert a formula from user to internal representation."
  8259. (if org-table-use-standard-references
  8260. (org-table-convert-refs-to-rc s)
  8261. s))
  8262. (defun org-table-convert-refs-to-rc (s)
  8263. "Convert spreadsheet references from AB7 to @7$28.
  8264. Works for single references, but also for entire formulas and even the
  8265. full TBLFM line."
  8266. (let ((start 0))
  8267. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  8268. (cond
  8269. ((match-end 3)
  8270. ;; format match, just advance
  8271. (setq start (match-end 0)))
  8272. ((and (> (match-beginning 0) 0)
  8273. (equal ?. (aref s (max (1- (match-beginning 0)) 0))))
  8274. ;; 3.e5 or something like this. FIXME: is this ok????
  8275. (setq start (match-end 0)))
  8276. (t
  8277. (setq start (match-beginning 0)
  8278. s (replace-match
  8279. (if (equal (match-string 2 s) "&")
  8280. (format "$%d" (org-letters-to-number (match-string 1 s)))
  8281. (format "@%d$%d"
  8282. (string-to-number (match-string 2 s))
  8283. (org-letters-to-number (match-string 1 s))))
  8284. t t s)))))
  8285. s))
  8286. (defun org-table-convert-refs-to-an (s)
  8287. "Convert spreadsheet references from to @7$28 to AB7.
  8288. Works for single references, but also for entire formulas and even the
  8289. full TBLFM line."
  8290. (while (string-match "@\\([0-9]+\\)$\\([0-9]+\\)" s)
  8291. (setq s (replace-match
  8292. (format "%s%d"
  8293. (org-number-to-letters
  8294. (string-to-number (match-string 2 s)))
  8295. (string-to-number (match-string 1 s)))
  8296. t t s)))
  8297. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  8298. (setq s (replace-match (concat "\\1"
  8299. (org-number-to-letters
  8300. (string-to-number (match-string 2 s))) "&")
  8301. t nil s)))
  8302. s)
  8303. (defun org-letters-to-number (s)
  8304. "Convert a base 26 number represented by letters into an integer.
  8305. For example: AB -> 28."
  8306. (let ((n 0))
  8307. (setq s (upcase s))
  8308. (while (> (length s) 0)
  8309. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  8310. s (substring s 1)))
  8311. n))
  8312. (defun org-number-to-letters (n)
  8313. "Convert an integer into a base 26 number represented by letters.
  8314. For example: 28 -> AB."
  8315. (let ((s ""))
  8316. (while (> n 0)
  8317. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  8318. n (/ (1- n) 26)))
  8319. s))
  8320. (defun org-table-fedit-convert-buffer (function)
  8321. "Convert all references in this buffer, using FUNTION."
  8322. (let ((line (org-current-line)))
  8323. (goto-char (point-min))
  8324. (while (not (eobp))
  8325. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  8326. (delete-region (point) (point-at-eol))
  8327. (or (eobp) (forward-char 1)))
  8328. (goto-line line)))
  8329. (defun org-table-fedit-toggle-ref-type ()
  8330. "Convert all references in the buffer from B3 to @3$2 and back."
  8331. (interactive)
  8332. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  8333. (org-table-fedit-convert-buffer
  8334. (if org-table-buffer-is-an
  8335. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  8336. (message "Reference type switched to %s"
  8337. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  8338. (defun org-table-fedit-ref-up ()
  8339. "Shift the reference at point one row/hline up."
  8340. (interactive)
  8341. (org-table-fedit-shift-reference 'up))
  8342. (defun org-table-fedit-ref-down ()
  8343. "Shift the reference at point one row/hline down."
  8344. (interactive)
  8345. (org-table-fedit-shift-reference 'down))
  8346. (defun org-table-fedit-ref-left ()
  8347. "Shift the reference at point one field to the left."
  8348. (interactive)
  8349. (org-table-fedit-shift-reference 'left))
  8350. (defun org-table-fedit-ref-right ()
  8351. "Shift the reference at point one field to the right."
  8352. (interactive)
  8353. (org-table-fedit-shift-reference 'right))
  8354. (defun org-table-fedit-shift-reference (dir)
  8355. (cond
  8356. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  8357. (if (memq dir '(left right))
  8358. (org-rematch-and-replace 1 (eq dir 'left))
  8359. (error "Cannot shift reference in this direction")))
  8360. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  8361. ;; A B3-like reference
  8362. (if (memq dir '(up down))
  8363. (org-rematch-and-replace 2 (eq dir 'up))
  8364. (org-rematch-and-replace 1 (eq dir 'left))))
  8365. ((org-at-regexp-p
  8366. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  8367. ;; An internal reference
  8368. (if (memq dir '(up down))
  8369. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  8370. (org-rematch-and-replace 5 (eq dir 'left))))))
  8371. (defun org-rematch-and-replace (n &optional decr hline)
  8372. "Re-match the group N, and replace it with the shifted refrence."
  8373. (or (match-end n) (error "Cannot shift reference in this direction"))
  8374. (goto-char (match-beginning n))
  8375. (and (looking-at (regexp-quote (match-string n)))
  8376. (replace-match (org-shift-refpart (match-string 0) decr hline)
  8377. t t)))
  8378. (defun org-shift-refpart (ref &optional decr hline)
  8379. "Shift a refrence part REF.
  8380. If DECR is set, decrease the references row/column, else increase.
  8381. If HLINE is set, this may be a hline reference, it certainly is not
  8382. a translation reference."
  8383. (save-match-data
  8384. (let* ((sign (string-match "^[-+]" ref)) n)
  8385. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  8386. (cond
  8387. ((and hline (string-match "^I+" ref))
  8388. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  8389. (setq n (+ n (if decr -1 1)))
  8390. (if (= n 0) (setq n (+ n (if decr -1 1))))
  8391. (if sign
  8392. (setq sign (if (< n 0) "-" "+") n (abs n))
  8393. (setq n (max 1 n)))
  8394. (concat sign (make-string n ?I)))
  8395. ((string-match "^[0-9]+" ref)
  8396. (setq n (string-to-number (concat sign ref)))
  8397. (setq n (+ n (if decr -1 1)))
  8398. (if sign
  8399. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  8400. (number-to-string (max 1 n))))
  8401. ((string-match "^[a-zA-Z]+" ref)
  8402. (org-number-to-letters
  8403. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  8404. (t (error "Cannot shift reference"))))))
  8405. (defun org-table-fedit-toggle-coordinates ()
  8406. "Toggle the display of coordinates in the refrenced table."
  8407. (interactive)
  8408. (let ((pos (marker-position org-pos)))
  8409. (with-current-buffer (marker-buffer org-pos)
  8410. (save-excursion
  8411. (goto-char pos)
  8412. (org-table-toggle-coordinate-overlays)))))
  8413. (defun org-table-fedit-finish (&optional arg)
  8414. "Parse the buffer for formula definitions and install them.
  8415. With prefix ARG, apply the new formulas to the table."
  8416. (interactive "P")
  8417. (org-table-remove-rectangle-highlight)
  8418. (if org-table-use-standard-references
  8419. (progn
  8420. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  8421. (setq org-table-buffer-is-an nil)))
  8422. (let ((pos org-pos) eql var form)
  8423. (goto-char (point-min))
  8424. (while (re-search-forward
  8425. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  8426. nil t)
  8427. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  8428. form (match-string 3))
  8429. (setq form (org-trim form))
  8430. (when (not (equal form ""))
  8431. (while (string-match "[ \t]*\n[ \t]*" form)
  8432. (setq form (replace-match " " t t form)))
  8433. (when (assoc var eql)
  8434. (error "Double formulas for %s" var))
  8435. (push (cons var form) eql)))
  8436. (setq org-pos nil)
  8437. (set-window-configuration org-window-configuration)
  8438. (select-window (get-buffer-window (marker-buffer pos)))
  8439. (goto-char pos)
  8440. (unless (org-at-table-p)
  8441. (error "Lost table position - cannot install formulae"))
  8442. (org-table-store-formulas eql)
  8443. (move-marker pos nil)
  8444. (kill-buffer "*Edit Formulas*")
  8445. (if arg
  8446. (org-table-recalculate 'all)
  8447. (message "New formulas installed - press C-u C-c C-c to apply."))))
  8448. (defun org-table-fedit-abort ()
  8449. "Abort editing formulas, without installing the changes."
  8450. (interactive)
  8451. (org-table-remove-rectangle-highlight)
  8452. (let ((pos org-pos))
  8453. (set-window-configuration org-window-configuration)
  8454. (select-window (get-buffer-window (marker-buffer pos)))
  8455. (goto-char pos)
  8456. (move-marker pos nil)
  8457. (message "Formula editing aborted without installing changes")))
  8458. (defun org-table-fedit-lisp-indent ()
  8459. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  8460. (interactive)
  8461. (let ((pos (point)) beg end ind)
  8462. (beginning-of-line 1)
  8463. (cond
  8464. ((looking-at "[ \t]")
  8465. (goto-char pos)
  8466. (call-interactively 'lisp-indent-line))
  8467. ((looking-at "[$@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  8468. ((not (fboundp 'pp-buffer))
  8469. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  8470. ((looking-at "[$@0-9a-zA-Z]+ *= *'(")
  8471. (goto-char (- (match-end 0) 2))
  8472. (setq beg (point))
  8473. (setq ind (make-string (current-column) ?\ ))
  8474. (condition-case nil (forward-sexp 1)
  8475. (error
  8476. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  8477. (setq end (point))
  8478. (save-restriction
  8479. (narrow-to-region beg end)
  8480. (if (eq last-command this-command)
  8481. (progn
  8482. (goto-char (point-min))
  8483. (setq this-command nil)
  8484. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  8485. (replace-match " ")))
  8486. (pp-buffer)
  8487. (untabify (point-min) (point-max))
  8488. (goto-char (1+ (point-min)))
  8489. (while (re-search-forward "^." nil t)
  8490. (beginning-of-line 1)
  8491. (insert ind))
  8492. (goto-char (point-max))
  8493. (backward-delete-char 1)))
  8494. (goto-char beg))
  8495. (t nil))))
  8496. (defvar org-show-positions nil)
  8497. (defun org-table-show-reference (&optional local)
  8498. "Show the location/value of the $ expression at point."
  8499. (interactive)
  8500. (org-table-remove-rectangle-highlight)
  8501. (catch 'exit
  8502. (let ((pos (if local (point) org-pos))
  8503. (face2 'highlight)
  8504. (org-inhibit-highlight-removal t)
  8505. (win (selected-window))
  8506. (org-show-positions nil)
  8507. var name e what match dest)
  8508. (if local (org-table-get-specials))
  8509. (setq what (cond
  8510. ((or (org-at-regexp-p org-table-range-regexp2)
  8511. (org-at-regexp-p org-table-translate-regexp)
  8512. (org-at-regexp-p org-table-range-regexp))
  8513. (setq match
  8514. (save-match-data
  8515. (org-table-convert-refs-to-rc (match-string 0))))
  8516. 'range)
  8517. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  8518. ((org-at-regexp-p "\\$[0-9]+") 'column)
  8519. ((not local) nil)
  8520. (t (error "No reference at point")))
  8521. match (and what (or match (match-string 0))))
  8522. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  8523. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  8524. 'secondary-selection))
  8525. (org-add-hook 'before-change-functions
  8526. 'org-table-remove-rectangle-highlight)
  8527. (if (eq what 'name) (setq var (substring match 1)))
  8528. (when (eq what 'range)
  8529. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  8530. (setq match (org-table-formula-substitute-names match)))
  8531. (unless local
  8532. (save-excursion
  8533. (end-of-line 1)
  8534. (re-search-backward "^\\S-" nil t)
  8535. (beginning-of-line 1)
  8536. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  8537. (setq dest
  8538. (save-match-data
  8539. (org-table-convert-refs-to-rc (match-string 1))))
  8540. (org-table-add-rectangle-overlay
  8541. (match-beginning 1) (match-end 1) face2))))
  8542. (if (and (markerp pos) (marker-buffer pos))
  8543. (if (get-buffer-window (marker-buffer pos))
  8544. (select-window (get-buffer-window (marker-buffer pos)))
  8545. (switch-to-buffer-other-window (get-buffer-window
  8546. (marker-buffer pos)))))
  8547. (goto-char pos)
  8548. (org-table-force-dataline)
  8549. (when dest
  8550. (setq name (substring dest 1))
  8551. (cond
  8552. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  8553. (setq e (assoc name org-table-named-field-locations))
  8554. (goto-line (nth 1 e))
  8555. (org-table-goto-column (nth 2 e)))
  8556. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  8557. (let ((l (string-to-number (match-string 1 dest)))
  8558. (c (string-to-number (match-string 2 dest))))
  8559. (goto-line (aref org-table-dlines l))
  8560. (org-table-goto-column c)))
  8561. (t (org-table-goto-column (string-to-number name))))
  8562. (move-marker pos (point))
  8563. (org-table-highlight-rectangle nil nil face2))
  8564. (cond
  8565. ((equal dest match))
  8566. ((not match))
  8567. ((eq what 'range)
  8568. (condition-case nil
  8569. (save-excursion
  8570. (org-table-get-range match nil nil 'highlight))
  8571. (error nil)))
  8572. ((setq e (assoc var org-table-named-field-locations))
  8573. (goto-line (nth 1 e))
  8574. (org-table-goto-column (nth 2 e))
  8575. (org-table-highlight-rectangle (point) (point))
  8576. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  8577. ((setq e (assoc var org-table-column-names))
  8578. (org-table-goto-column (string-to-number (cdr e)))
  8579. (org-table-highlight-rectangle (point) (point))
  8580. (goto-char (org-table-begin))
  8581. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  8582. (org-table-end) t)
  8583. (progn
  8584. (goto-char (match-beginning 1))
  8585. (org-table-highlight-rectangle)
  8586. (message "Named column (column %s)" (cdr e)))
  8587. (error "Column name not found")))
  8588. ((eq what 'column)
  8589. ;; column number
  8590. (org-table-goto-column (string-to-number (substring match 1)))
  8591. (org-table-highlight-rectangle (point) (point))
  8592. (message "Column %s" (substring match 1)))
  8593. ((setq e (assoc var org-table-local-parameters))
  8594. (goto-char (org-table-begin))
  8595. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  8596. (progn
  8597. (goto-char (match-beginning 1))
  8598. (org-table-highlight-rectangle)
  8599. (message "Local parameter."))
  8600. (error "Parameter not found")))
  8601. (t
  8602. (cond
  8603. ((not var) (error "No reference at point"))
  8604. ((setq e (assoc var org-table-formula-constants-local))
  8605. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  8606. var (cdr e)))
  8607. ((setq e (assoc var org-table-formula-constants))
  8608. (message "Constant: $%s=%s in `org-table-formula-constants'."
  8609. var (cdr e)))
  8610. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  8611. (message "Constant: $%s=%s, from `constants.el'%s."
  8612. var e (format " (%s units)" constants-unit-system)))
  8613. (t (error "Undefined name $%s" var)))))
  8614. (goto-char pos)
  8615. (when (and org-show-positions
  8616. (not (memq this-command '(org-table-fedit-scroll
  8617. org-table-fedit-scroll-down))))
  8618. (push pos org-show-positions)
  8619. (push org-table-current-begin-pos org-show-positions)
  8620. (let ((min (apply 'min org-show-positions))
  8621. (max (apply 'max org-show-positions)))
  8622. (goto-char min) (recenter 0)
  8623. (goto-char max)
  8624. (or (pos-visible-in-window-p max) (recenter -1))))
  8625. (select-window win))))
  8626. (defun org-table-force-dataline ()
  8627. "Make sure the cursor is in a dataline in a table."
  8628. (unless (save-excursion
  8629. (beginning-of-line 1)
  8630. (looking-at org-table-dataline-regexp))
  8631. (let* ((re org-table-dataline-regexp)
  8632. (p1 (save-excursion (re-search-forward re nil 'move)))
  8633. (p2 (save-excursion (re-search-backward re nil 'move))))
  8634. (cond ((and p1 p2)
  8635. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  8636. p1 p2)))
  8637. ((or p1 p2) (goto-char (or p1 p2)))
  8638. (t (error "No table dataline around here"))))))
  8639. (defun org-table-fedit-line-up ()
  8640. "Move cursor one line up in the window showing the table."
  8641. (interactive)
  8642. (org-table-fedit-move 'previous-line))
  8643. (defun org-table-fedit-line-down ()
  8644. "Move cursor one line down in the window showing the table."
  8645. (interactive)
  8646. (org-table-fedit-move 'next-line))
  8647. (defun org-table-fedit-move (command)
  8648. "Move the cursor in the window shoinw the table.
  8649. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  8650. (let ((org-table-allow-automatic-line-recalculation nil)
  8651. (pos org-pos) (win (selected-window)) p)
  8652. (select-window (get-buffer-window (marker-buffer org-pos)))
  8653. (setq p (point))
  8654. (call-interactively command)
  8655. (while (and (org-at-table-p)
  8656. (org-at-table-hline-p))
  8657. (call-interactively command))
  8658. (or (org-at-table-p) (goto-char p))
  8659. (move-marker pos (point))
  8660. (select-window win)))
  8661. (defun org-table-fedit-scroll (N)
  8662. (interactive "p")
  8663. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  8664. (scroll-other-window N)))
  8665. (defun org-table-fedit-scroll-down (N)
  8666. (interactive "p")
  8667. (org-table-fedit-scroll (- N)))
  8668. (defvar org-table-rectangle-overlays nil)
  8669. (defun org-table-add-rectangle-overlay (beg end &optional face)
  8670. "Add a new overlay."
  8671. (let ((ov (org-make-overlay beg end)))
  8672. (org-overlay-put ov 'face (or face 'secondary-selection))
  8673. (push ov org-table-rectangle-overlays)))
  8674. (defun org-table-highlight-rectangle (&optional beg end face)
  8675. "Highlight rectangular region in a table."
  8676. (setq beg (or beg (point)) end (or end (point)))
  8677. (let ((b (min beg end))
  8678. (e (max beg end))
  8679. l1 c1 l2 c2 tmp)
  8680. (and (boundp 'org-show-positions)
  8681. (setq org-show-positions (cons b (cons e org-show-positions))))
  8682. (goto-char (min beg end))
  8683. (setq l1 (org-current-line)
  8684. c1 (org-table-current-column))
  8685. (goto-char (max beg end))
  8686. (setq l2 (org-current-line)
  8687. c2 (org-table-current-column))
  8688. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  8689. (goto-line l1)
  8690. (beginning-of-line 1)
  8691. (loop for line from l1 to l2 do
  8692. (when (looking-at org-table-dataline-regexp)
  8693. (org-table-goto-column c1)
  8694. (skip-chars-backward "^|\n") (setq beg (point))
  8695. (org-table-goto-column c2)
  8696. (skip-chars-forward "^|\n") (setq end (point))
  8697. (org-table-add-rectangle-overlay beg end face))
  8698. (beginning-of-line 2))
  8699. (goto-char b))
  8700. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  8701. (defun org-table-remove-rectangle-highlight (&rest ignore)
  8702. "Remove the rectangle overlays."
  8703. (unless org-inhibit-highlight-removal
  8704. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  8705. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  8706. (setq org-table-rectangle-overlays nil)))
  8707. (defvar org-table-coordinate-overlays nil
  8708. "Collects the cooordinate grid overlays, so that they can be removed.")
  8709. (make-variable-buffer-local 'org-table-coordinate-overlays)
  8710. (defun org-table-overlay-coordinates ()
  8711. "Add overlays to the table at point, to show row/column coordinates."
  8712. (interactive)
  8713. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8714. (setq org-table-coordinate-overlays nil)
  8715. (save-excursion
  8716. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  8717. (goto-char (org-table-begin))
  8718. (while (org-at-table-p)
  8719. (setq eol (point-at-eol))
  8720. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  8721. (push ov org-table-coordinate-overlays)
  8722. (setq hline (looking-at org-table-hline-regexp))
  8723. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  8724. (format "%4d" (setq id (1+ id)))))
  8725. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  8726. (when hline
  8727. (setq ic 0)
  8728. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  8729. (setq beg (1+ (match-beginning 0))
  8730. ic (1+ ic)
  8731. s1 (concat "$" (int-to-string ic))
  8732. s2 (org-number-to-letters ic)
  8733. str (if (eq org-table-use-standard-references t) s2 s1))
  8734. (setq ov (org-make-overlay beg (+ beg (length str))))
  8735. (push ov org-table-coordinate-overlays)
  8736. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  8737. (beginning-of-line 2)))))
  8738. (defun org-table-toggle-coordinate-overlays ()
  8739. "Toggle the display of Row/Column numbers in tables."
  8740. (interactive)
  8741. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  8742. (message "Row/Column number display turned %s"
  8743. (if org-table-overlay-coordinates "on" "off"))
  8744. (if (and (org-at-table-p) org-table-overlay-coordinates)
  8745. (org-table-align))
  8746. (unless org-table-overlay-coordinates
  8747. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8748. (setq org-table-coordinate-overlays nil)))
  8749. (defun org-table-toggle-formula-debugger ()
  8750. "Toggle the formula debugger in tables."
  8751. (interactive)
  8752. (setq org-table-formula-debug (not org-table-formula-debug))
  8753. (message "Formula debugging has been turned %s"
  8754. (if org-table-formula-debug "on" "off")))
  8755. ;;; The orgtbl minor mode
  8756. ;; Define a minor mode which can be used in other modes in order to
  8757. ;; integrate the org-mode table editor.
  8758. ;; This is really a hack, because the org-mode table editor uses several
  8759. ;; keys which normally belong to the major mode, for example the TAB and
  8760. ;; RET keys. Here is how it works: The minor mode defines all the keys
  8761. ;; necessary to operate the table editor, but wraps the commands into a
  8762. ;; function which tests if the cursor is currently inside a table. If that
  8763. ;; is the case, the table editor command is executed. However, when any of
  8764. ;; those keys is used outside a table, the function uses `key-binding' to
  8765. ;; look up if the key has an associated command in another currently active
  8766. ;; keymap (minor modes, major mode, global), and executes that command.
  8767. ;; There might be problems if any of the keys used by the table editor is
  8768. ;; otherwise used as a prefix key.
  8769. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  8770. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  8771. ;; addresses this by checking explicitly for both bindings.
  8772. ;; The optimized version (see variable `orgtbl-optimized') takes over
  8773. ;; all keys which are bound to `self-insert-command' in the *global map*.
  8774. ;; Some modes bind other commands to simple characters, for example
  8775. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  8776. ;; active, this binding is ignored inside tables and replaced with a
  8777. ;; modified self-insert.
  8778. (defvar orgtbl-mode nil
  8779. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  8780. table editor in arbitrary modes.")
  8781. (make-variable-buffer-local 'orgtbl-mode)
  8782. (defvar orgtbl-mode-map (make-keymap)
  8783. "Keymap for `orgtbl-mode'.")
  8784. ;;;###autoload
  8785. (defun turn-on-orgtbl ()
  8786. "Unconditionally turn on `orgtbl-mode'."
  8787. (orgtbl-mode 1))
  8788. (defvar org-old-auto-fill-inhibit-regexp nil
  8789. "Local variable used by `orgtbl-mode'")
  8790. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  8791. "Matches a line belonging to an orgtbl.")
  8792. (defconst orgtbl-extra-font-lock-keywords
  8793. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  8794. 0 (quote 'org-table) 'prepend))
  8795. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  8796. ;;;###autoload
  8797. (defun orgtbl-mode (&optional arg)
  8798. "The `org-mode' table editor as a minor mode for use in other modes."
  8799. (interactive)
  8800. (if (org-mode-p)
  8801. ;; Exit without error, in case some hook functions calls this
  8802. ;; by accident in org-mode.
  8803. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  8804. (setq orgtbl-mode
  8805. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  8806. (if orgtbl-mode
  8807. (progn
  8808. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  8809. ;; Make sure we are first in minor-mode-map-alist
  8810. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  8811. (and c (setq minor-mode-map-alist
  8812. (cons c (delq c minor-mode-map-alist)))))
  8813. (org-set-local (quote org-table-may-need-update) t)
  8814. (org-add-hook 'before-change-functions 'org-before-change-function
  8815. nil 'local)
  8816. (org-set-local 'org-old-auto-fill-inhibit-regexp
  8817. auto-fill-inhibit-regexp)
  8818. (org-set-local 'auto-fill-inhibit-regexp
  8819. (if auto-fill-inhibit-regexp
  8820. (concat orgtbl-line-start-regexp "\\|"
  8821. auto-fill-inhibit-regexp)
  8822. orgtbl-line-start-regexp))
  8823. (org-add-to-invisibility-spec '(org-cwidth))
  8824. (when (fboundp 'font-lock-add-keywords)
  8825. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  8826. (org-restart-font-lock))
  8827. (easy-menu-add orgtbl-mode-menu)
  8828. (run-hooks 'orgtbl-mode-hook))
  8829. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  8830. (org-cleanup-narrow-column-properties)
  8831. (org-remove-from-invisibility-spec '(org-cwidth))
  8832. (remove-hook 'before-change-functions 'org-before-change-function t)
  8833. (when (fboundp 'font-lock-remove-keywords)
  8834. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  8835. (org-restart-font-lock))
  8836. (easy-menu-remove orgtbl-mode-menu)
  8837. (force-mode-line-update 'all))))
  8838. (defun org-cleanup-narrow-column-properties ()
  8839. "Remove all properties related to narrow-column invisibility."
  8840. (let ((s 1))
  8841. (while (setq s (text-property-any s (point-max)
  8842. 'display org-narrow-column-arrow))
  8843. (remove-text-properties s (1+ s) '(display t)))
  8844. (setq s 1)
  8845. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  8846. (remove-text-properties s (1+ s) '(org-cwidth t)))
  8847. (setq s 1)
  8848. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  8849. (remove-text-properties s (1+ s) '(invisible t)))))
  8850. ;; Install it as a minor mode.
  8851. (put 'orgtbl-mode :included t)
  8852. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  8853. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  8854. (defun orgtbl-make-binding (fun n &rest keys)
  8855. "Create a function for binding in the table minor mode.
  8856. FUN is the command to call inside a table. N is used to create a unique
  8857. command name. KEYS are keys that should be checked in for a command
  8858. to execute outside of tables."
  8859. (eval
  8860. (list 'defun
  8861. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  8862. '(arg)
  8863. (concat "In tables, run `" (symbol-name fun) "'.\n"
  8864. "Outside of tables, run the binding of `"
  8865. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  8866. "'.")
  8867. '(interactive "p")
  8868. (list 'if
  8869. '(org-at-table-p)
  8870. (list 'call-interactively (list 'quote fun))
  8871. (list 'let '(orgtbl-mode)
  8872. (list 'call-interactively
  8873. (append '(or)
  8874. (mapcar (lambda (k)
  8875. (list 'key-binding k))
  8876. keys)
  8877. '('orgtbl-error))))))))
  8878. (defun orgtbl-error ()
  8879. "Error when there is no default binding for a table key."
  8880. (interactive)
  8881. (error "This key is has no function outside tables"))
  8882. (defun orgtbl-setup ()
  8883. "Setup orgtbl keymaps."
  8884. (let ((nfunc 0)
  8885. (bindings
  8886. (list
  8887. '([(meta shift left)] org-table-delete-column)
  8888. '([(meta left)] org-table-move-column-left)
  8889. '([(meta right)] org-table-move-column-right)
  8890. '([(meta shift right)] org-table-insert-column)
  8891. '([(meta shift up)] org-table-kill-row)
  8892. '([(meta shift down)] org-table-insert-row)
  8893. '([(meta up)] org-table-move-row-up)
  8894. '([(meta down)] org-table-move-row-down)
  8895. '("\C-c\C-w" org-table-cut-region)
  8896. '("\C-c\M-w" org-table-copy-region)
  8897. '("\C-c\C-y" org-table-paste-rectangle)
  8898. '("\C-c-" org-table-insert-hline)
  8899. '("\C-c}" org-table-toggle-coordinate-overlays)
  8900. '("\C-c{" org-table-toggle-formula-debugger)
  8901. '("\C-m" org-table-next-row)
  8902. '([(shift return)] org-table-copy-down)
  8903. '("\C-c\C-q" org-table-wrap-region)
  8904. '("\C-c?" org-table-field-info)
  8905. '("\C-c " org-table-blank-field)
  8906. '("\C-c+" org-table-sum)
  8907. '("\C-c=" org-table-eval-formula)
  8908. '("\C-c'" org-table-edit-formulas)
  8909. '("\C-c`" org-table-edit-field)
  8910. '("\C-c*" org-table-recalculate)
  8911. '("\C-c|" org-table-create-or-convert-from-region)
  8912. '("\C-c^" org-table-sort-lines)
  8913. '([(control ?#)] org-table-rotate-recalc-marks)))
  8914. elt key fun cmd)
  8915. (while (setq elt (pop bindings))
  8916. (setq nfunc (1+ nfunc))
  8917. (setq key (org-key (car elt))
  8918. fun (nth 1 elt)
  8919. cmd (orgtbl-make-binding fun nfunc key))
  8920. (org-defkey orgtbl-mode-map key cmd))
  8921. ;; Special treatment needed for TAB and RET
  8922. (org-defkey orgtbl-mode-map [(return)]
  8923. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  8924. (org-defkey orgtbl-mode-map "\C-m"
  8925. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  8926. (org-defkey orgtbl-mode-map [(tab)]
  8927. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  8928. (org-defkey orgtbl-mode-map "\C-i"
  8929. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  8930. (org-defkey orgtbl-mode-map [(shift tab)]
  8931. (orgtbl-make-binding 'org-table-previous-field 104
  8932. [(shift tab)] [(tab)] "\C-i"))
  8933. (org-defkey orgtbl-mode-map "\M-\C-m"
  8934. (orgtbl-make-binding 'org-table-wrap-region 105
  8935. "\M-\C-m" [(meta return)]))
  8936. (org-defkey orgtbl-mode-map [(meta return)]
  8937. (orgtbl-make-binding 'org-table-wrap-region 106
  8938. [(meta return)] "\M-\C-m"))
  8939. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  8940. (when orgtbl-optimized
  8941. ;; If the user wants maximum table support, we need to hijack
  8942. ;; some standard editing functions
  8943. (org-remap orgtbl-mode-map
  8944. 'self-insert-command 'orgtbl-self-insert-command
  8945. 'delete-char 'org-delete-char
  8946. 'delete-backward-char 'org-delete-backward-char)
  8947. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  8948. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  8949. '("OrgTbl"
  8950. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  8951. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  8952. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  8953. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  8954. "--"
  8955. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  8956. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  8957. ["Copy Field from Above"
  8958. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  8959. "--"
  8960. ("Column"
  8961. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  8962. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  8963. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  8964. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  8965. ("Row"
  8966. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  8967. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  8968. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  8969. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  8970. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  8971. "--"
  8972. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  8973. ("Rectangle"
  8974. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  8975. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  8976. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  8977. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  8978. "--"
  8979. ("Radio tables"
  8980. ["Insert table template" orgtbl-insert-radio-table
  8981. (assq major-mode orgtbl-radio-table-templates)]
  8982. ["Comment/uncomment table" orgtbl-toggle-comment t])
  8983. "--"
  8984. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  8985. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  8986. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  8987. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  8988. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  8989. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  8990. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  8991. ["Sum Column/Rectangle" org-table-sum
  8992. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  8993. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  8994. ["Debug Formulas"
  8995. org-table-toggle-formula-debugger :active (org-at-table-p)
  8996. :keys "C-c {"
  8997. :style toggle :selected org-table-formula-debug]
  8998. ["Show Col/Row Numbers"
  8999. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9000. :keys "C-c }"
  9001. :style toggle :selected org-table-overlay-coordinates]
  9002. ))
  9003. t))
  9004. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9005. "If the cursor is inside a table, realign the table.
  9006. It it is a table to be sent away to a receiver, do it.
  9007. With prefix arg, also recompute table."
  9008. (interactive "P")
  9009. (let ((pos (point)) action)
  9010. (save-excursion
  9011. (beginning-of-line 1)
  9012. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9013. ((looking-at "[ \t]*|") pos)
  9014. ((looking-at "#\\+TBLFM:") 'recalc))))
  9015. (cond
  9016. ((integerp action)
  9017. (goto-char action)
  9018. (org-table-maybe-eval-formula)
  9019. (if arg
  9020. (call-interactively 'org-table-recalculate)
  9021. (org-table-maybe-recalculate-line))
  9022. (call-interactively 'org-table-align)
  9023. (orgtbl-send-table 'maybe))
  9024. ((eq action 'recalc)
  9025. (save-excursion
  9026. (beginning-of-line 1)
  9027. (skip-chars-backward " \r\n\t")
  9028. (if (org-at-table-p)
  9029. (org-call-with-arg 'org-table-recalculate t))))
  9030. (t (let (orgtbl-mode)
  9031. (call-interactively (key-binding "\C-c\C-c")))))))
  9032. (defun orgtbl-tab (arg)
  9033. "Justification and field motion for `orgtbl-mode'."
  9034. (interactive "P")
  9035. (if arg (org-table-edit-field t)
  9036. (org-table-justify-field-maybe)
  9037. (org-table-next-field)))
  9038. (defun orgtbl-ret ()
  9039. "Justification and field motion for `orgtbl-mode'."
  9040. (interactive)
  9041. (org-table-justify-field-maybe)
  9042. (org-table-next-row))
  9043. (defun orgtbl-self-insert-command (N)
  9044. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9045. If the cursor is in a table looking at whitespace, the whitespace is
  9046. overwritten, and the table is not marked as requiring realignment."
  9047. (interactive "p")
  9048. (if (and (org-at-table-p)
  9049. (or
  9050. (and org-table-auto-blank-field
  9051. (member last-command
  9052. '(orgtbl-hijacker-command-100
  9053. orgtbl-hijacker-command-101
  9054. orgtbl-hijacker-command-102
  9055. orgtbl-hijacker-command-103
  9056. orgtbl-hijacker-command-104
  9057. orgtbl-hijacker-command-105))
  9058. (org-table-blank-field))
  9059. t)
  9060. (eq N 1)
  9061. (looking-at "[^|\n]* +|"))
  9062. (let (org-table-may-need-update)
  9063. (goto-char (1- (match-end 0)))
  9064. (delete-backward-char 1)
  9065. (goto-char (match-beginning 0))
  9066. (self-insert-command N))
  9067. (setq org-table-may-need-update t)
  9068. (let (orgtbl-mode)
  9069. (call-interactively (key-binding (vector last-input-event))))))
  9070. (defun org-force-self-insert (N)
  9071. "Needed to enforce self-insert under remapping."
  9072. (interactive "p")
  9073. (self-insert-command N))
  9074. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9075. "Regula expression matching exponentials as produced by calc.")
  9076. (defvar org-table-clean-did-remove-column nil)
  9077. (defun orgtbl-export (table target)
  9078. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9079. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9080. org-table-last-alignment org-table-last-column-widths
  9081. maxcol column)
  9082. (if (not (fboundp func))
  9083. (error "Cannot export orgtbl table to %s" target))
  9084. (setq lines (org-table-clean-before-export lines))
  9085. (setq table
  9086. (mapcar
  9087. (lambda (x)
  9088. (if (string-match org-table-hline-regexp x)
  9089. 'hline
  9090. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  9091. lines))
  9092. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  9093. table)))
  9094. (loop for i from (1- maxcol) downto 0 do
  9095. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  9096. (setq column (delq nil column))
  9097. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  9098. (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))
  9099. (funcall func table nil)))
  9100. (defun orgtbl-send-table (&optional maybe)
  9101. "Send a tranformed version of this table to the receiver position.
  9102. With argument MAYBE, fail quietly if no transformation is defined for
  9103. this table."
  9104. (interactive)
  9105. (catch 'exit
  9106. (unless (org-at-table-p) (error "Not at a table"))
  9107. ;; when non-interactive, we assume align has just happened.
  9108. (when (interactive-p) (org-table-align))
  9109. (save-excursion
  9110. (goto-char (org-table-begin))
  9111. (beginning-of-line 0)
  9112. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  9113. (if maybe
  9114. (throw 'exit nil)
  9115. (error "Don't know how to transform this table."))))
  9116. (let* ((name (match-string 1))
  9117. beg
  9118. (transform (intern (match-string 2)))
  9119. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  9120. (skip (plist-get params :skip))
  9121. (skipcols (plist-get params :skipcols))
  9122. (txt (buffer-substring-no-properties
  9123. (org-table-begin) (org-table-end)))
  9124. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  9125. (lines (org-table-clean-before-export lines))
  9126. (i0 (if org-table-clean-did-remove-column 2 1))
  9127. (table (mapcar
  9128. (lambda (x)
  9129. (if (string-match org-table-hline-regexp x)
  9130. 'hline
  9131. (org-remove-by-index
  9132. (org-split-string (org-trim x) "\\s-*|\\s-*")
  9133. skipcols i0)))
  9134. lines))
  9135. (fun (if (= i0 2) 'cdr 'identity))
  9136. (org-table-last-alignment
  9137. (org-remove-by-index (funcall fun org-table-last-alignment)
  9138. skipcols i0))
  9139. (org-table-last-column-widths
  9140. (org-remove-by-index (funcall fun org-table-last-column-widths)
  9141. skipcols i0)))
  9142. (unless (fboundp transform)
  9143. (error "No such transformation function %s" transform))
  9144. (setq txt (funcall transform table params))
  9145. ;; Find the insertion place
  9146. (save-excursion
  9147. (goto-char (point-min))
  9148. (unless (re-search-forward
  9149. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  9150. (error "Don't know where to insert translated table"))
  9151. (goto-char (match-beginning 0))
  9152. (beginning-of-line 2)
  9153. (setq beg (point))
  9154. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  9155. (error "Cannot find end of insertion region"))
  9156. (beginning-of-line 1)
  9157. (delete-region beg (point))
  9158. (goto-char beg)
  9159. (insert txt "\n"))
  9160. (message "Table converted and installed at receiver location"))))
  9161. (defun org-remove-by-index (list indices &optional i0)
  9162. "Remove the elements in LIST with indices in INDICES.
  9163. First element has index 0, or I0 if given."
  9164. (if (not indices)
  9165. list
  9166. (if (integerp indices) (setq indices (list indices)))
  9167. (setq i0 (1- (or i0 0)))
  9168. (delq :rm (mapcar (lambda (x)
  9169. (setq i0 (1+ i0))
  9170. (if (memq i0 indices) :rm x))
  9171. list))))
  9172. (defun orgtbl-toggle-comment ()
  9173. "Comment or uncomment the orgtbl at point."
  9174. (interactive)
  9175. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  9176. (re2 (concat "^" orgtbl-line-start-regexp))
  9177. (commented (save-excursion (beginning-of-line 1)
  9178. (cond ((looking-at re1) t)
  9179. ((looking-at re2) nil)
  9180. (t (error "Not at an org table")))))
  9181. (re (if commented re1 re2))
  9182. beg end)
  9183. (save-excursion
  9184. (beginning-of-line 1)
  9185. (while (looking-at re) (beginning-of-line 0))
  9186. (beginning-of-line 2)
  9187. (setq beg (point))
  9188. (while (looking-at re) (beginning-of-line 2))
  9189. (setq end (point)))
  9190. (comment-region beg end (if commented '(4) nil))))
  9191. (defun orgtbl-insert-radio-table ()
  9192. "Insert a radio table template appropriate for this major mode."
  9193. (interactive)
  9194. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  9195. (txt (nth 1 e))
  9196. name pos)
  9197. (unless e (error "No radio table setup defined for %s" major-mode))
  9198. (setq name (read-string "Table name: "))
  9199. (while (string-match "%n" txt)
  9200. (setq txt (replace-match name t t txt)))
  9201. (or (bolp) (insert "\n"))
  9202. (setq pos (point))
  9203. (insert txt)
  9204. (goto-char pos)))
  9205. (defun org-get-param (params header i sym &optional hsym)
  9206. "Get parameter value for symbol SYM.
  9207. If this is a header line, actually get the value for the symbol with an
  9208. additional \"h\" inserted after the colon.
  9209. If the value is a protperty list, get the element for the current column.
  9210. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  9211. (let ((val (plist-get params sym)))
  9212. (and hsym header (setq val (or (plist-get params hsym) val)))
  9213. (if (consp val) (plist-get val i) val)))
  9214. (defun orgtbl-to-generic (table params)
  9215. "Convert the orgtbl-mode TABLE to some other format.
  9216. This generic routine can be used for many standard cases.
  9217. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9218. separator line, or a list of fields for that line.
  9219. PARAMS is a property list of parameters that can influence the conversion.
  9220. For the generic converter, some parameters are obligatory: You need to
  9221. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  9222. :splice, you must have :tstart and :tend.
  9223. Valid parameters are
  9224. :tstart String to start the table. Ignored when :splice is t.
  9225. :tend String to end the table. Ignored when :splice is t.
  9226. :splice When set to t, return only table body lines, don't wrap
  9227. them into :tstart and :tend. Default is nil.
  9228. :hline String to be inserted on horizontal separation lines.
  9229. May be nil to ignore hlines.
  9230. :lstart String to start a new table line.
  9231. :lend String to end a table line
  9232. :sep Separator between two fields
  9233. :lfmt Format for entire line, with enough %s to capture all fields.
  9234. If this is present, :lstart, :lend, and :sep are ignored.
  9235. :fmt A format to be used to wrap the field, should contain
  9236. %s for the original field value. For example, to wrap
  9237. everything in dollars, you could use :fmt \"$%s$\".
  9238. This may also be a property list with column numbers and
  9239. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9240. :hlstart :hlend :hlsep :hlfmt :hfmt
  9241. Same as above, specific for the header lines in the table.
  9242. All lines before the first hline are treated as header.
  9243. If any of these is not present, the data line value is used.
  9244. :efmt Use this format to print numbers with exponentials.
  9245. The format should have %s twice for inserting mantissa
  9246. and exponent, for example \"%s\\\\times10^{%s}\". This
  9247. may also be a property list with column numbers and
  9248. formats. :fmt will still be applied after :efmt.
  9249. In addition to this, the parameters :skip and :skipcols are always handled
  9250. directly by `orgtbl-send-table'. See manual."
  9251. (interactive)
  9252. (let* ((p params)
  9253. (splicep (plist-get p :splice))
  9254. (hline (plist-get p :hline))
  9255. rtn line i fm efm lfmt h)
  9256. ;; Do we have a header?
  9257. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  9258. (setq h t))
  9259. ;; Put header
  9260. (unless splicep
  9261. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  9262. ;; Now loop over all lines
  9263. (while (setq line (pop table))
  9264. (if (eq line 'hline)
  9265. ;; A horizontal separator line
  9266. (progn (if hline (push hline rtn))
  9267. (setq h nil)) ; no longer in header
  9268. ;; A normal line. Convert the fields, push line onto the result list
  9269. (setq i 0)
  9270. (setq line
  9271. (mapcar
  9272. (lambda (f)
  9273. (setq i (1+ i)
  9274. fm (org-get-param p h i :fmt :hfmt)
  9275. efm (org-get-param p h i :efmt))
  9276. (if (and efm (string-match orgtbl-exp-regexp f))
  9277. (setq f (format
  9278. efm (match-string 1 f) (match-string 2 f))))
  9279. (if fm (setq f (format fm f)))
  9280. f)
  9281. line))
  9282. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  9283. (push (apply 'format lfmt line) rtn)
  9284. (push (concat
  9285. (org-get-param p h i :lstart :hlstart)
  9286. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  9287. (org-get-param p h i :lend :hlend))
  9288. rtn))))
  9289. (unless splicep
  9290. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  9291. (mapconcat 'identity (nreverse rtn) "\n")))
  9292. (defun orgtbl-to-latex (table params)
  9293. "Convert the orgtbl-mode TABLE to LaTeX.
  9294. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9295. separator line, or a list of fields for that line.
  9296. PARAMS is a property list of parameters that can influence the conversion.
  9297. Supports all parameters from `orgtbl-to-generic'. Most important for
  9298. LaTeX are:
  9299. :splice When set to t, return only table body lines, don't wrap
  9300. them into a tabular environment. Default is nil.
  9301. :fmt A format to be used to wrap the field, should contain %s for the
  9302. original field value. For example, to wrap everything in dollars,
  9303. use :fmt \"$%s$\". This may also be a property list with column
  9304. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9305. :efmt Format for transforming numbers with exponentials. The format
  9306. should have %s twice for inserting mantissa and exponent, for
  9307. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  9308. This may also be a property list with column numbers and formats.
  9309. The general parameters :skip and :skipcols have already been applied when
  9310. this function is called."
  9311. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  9312. org-table-last-alignment ""))
  9313. (params2
  9314. (list
  9315. :tstart (concat "\\begin{tabular}{" alignment "}")
  9316. :tend "\\end{tabular}"
  9317. :lstart "" :lend " \\\\" :sep " & "
  9318. :efmt "%s\\,(%s)" :hline "\\hline")))
  9319. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9320. (defun orgtbl-to-html (table params)
  9321. "Convert the orgtbl-mode TABLE to LaTeX.
  9322. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9323. separator line, or a list of fields for that line.
  9324. PARAMS is a property list of parameters that can influence the conversion.
  9325. Currently this function recognizes the following parameters:
  9326. :splice When set to t, return only table body lines, don't wrap
  9327. them into a <table> environment. Default is nil.
  9328. The general parameters :skip and :skipcols have already been applied when
  9329. this function is called. The function does *not* use `orgtbl-to-generic',
  9330. so you cannot specify parameters for it."
  9331. (let* ((splicep (plist-get params :splice))
  9332. html)
  9333. ;; Just call the formatter we already have
  9334. ;; We need to make text lines for it, so put the fields back together.
  9335. (setq html (org-format-org-table-html
  9336. (mapcar
  9337. (lambda (x)
  9338. (if (eq x 'hline)
  9339. "|----+----|"
  9340. (concat "| " (mapconcat 'identity x " | ") " |")))
  9341. table)
  9342. splicep))
  9343. (if (string-match "\n+\\'" html)
  9344. (setq html (replace-match "" t t html)))
  9345. html))
  9346. (defun orgtbl-to-texinfo (table params)
  9347. "Convert the orgtbl-mode TABLE to TeXInfo.
  9348. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9349. separator line, or a list of fields for that line.
  9350. PARAMS is a property list of parameters that can influence the conversion.
  9351. Supports all parameters from `orgtbl-to-generic'. Most important for
  9352. TeXInfo are:
  9353. :splice nil/t When set to t, return only table body lines, don't wrap
  9354. them into a multitable environment. Default is nil.
  9355. :fmt fmt A format to be used to wrap the field, should contain
  9356. %s for the original field value. For example, to wrap
  9357. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  9358. This may also be a property list with column numbers and
  9359. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  9360. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  9361. are computed automatically from the width of the columns
  9362. under org-mode.
  9363. The general parameters :skip and :skipcols have already been applied when
  9364. this function is called."
  9365. (let* ((total (float (apply '+ org-table-last-column-widths)))
  9366. (colfrac (or (plist-get params :cf)
  9367. (mapconcat
  9368. (lambda (x) (format "%.3f" (/ (float x) total)))
  9369. org-table-last-column-widths " ")))
  9370. (params2
  9371. (list
  9372. :tstart (concat "@multitable @columnfractions " colfrac)
  9373. :tend "@end multitable"
  9374. :lstart "@item " :lend "" :sep " @tab "
  9375. :hlstart "@headitem ")))
  9376. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9377. ;;;; Link Stuff
  9378. ;;; Link abbreviations
  9379. (defun org-link-expand-abbrev (link)
  9380. "Apply replacements as defined in `org-link-abbrev-alist."
  9381. (if (string-match "^\\([a-zA-Z]+\\)\\(::?\\(.*\\)\\)?$" link)
  9382. (let* ((key (match-string 1 link))
  9383. (as (or (assoc key org-link-abbrev-alist-local)
  9384. (assoc key org-link-abbrev-alist)))
  9385. (tag (and (match-end 2) (match-string 3 link)))
  9386. rpl)
  9387. (if (not as)
  9388. link
  9389. (setq rpl (cdr as))
  9390. (cond
  9391. ((symbolp rpl) (funcall rpl tag))
  9392. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9393. (t (concat rpl tag)))))
  9394. link))
  9395. ;;; Storing and inserting links
  9396. (defvar org-insert-link-history nil
  9397. "Minibuffer history for links inserted with `org-insert-link'.")
  9398. (defvar org-stored-links nil
  9399. "Contains the links stored with `org-store-link'.")
  9400. (defvar org-store-link-plist nil
  9401. "Plist with info about the most recently link created with `org-store-link'.")
  9402. ;;;###autoload
  9403. (defun org-store-link (arg)
  9404. "\\<org-mode-map>Store an org-link to the current location.
  9405. This link can later be inserted into an org-buffer with
  9406. \\[org-insert-link].
  9407. For some link types, a prefix arg is interpreted:
  9408. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9409. For file links, arg negates `org-context-in-file-links'."
  9410. (interactive "P")
  9411. (setq org-store-link-plist nil) ; reset
  9412. (let (link cpltxt desc description search txt)
  9413. (cond
  9414. ((eq major-mode 'bbdb-mode)
  9415. (let ((name (bbdb-record-name (bbdb-current-record)))
  9416. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  9417. (setq cpltxt (concat "bbdb:" (or name company))
  9418. link (org-make-link cpltxt))
  9419. (org-store-link-props :type "bbdb" :name name :company company)))
  9420. ((eq major-mode 'Info-mode)
  9421. (setq link (org-make-link "info:"
  9422. (file-name-nondirectory Info-current-file)
  9423. ":" Info-current-node))
  9424. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9425. ":" Info-current-node))
  9426. (org-store-link-props :type "info" :file Info-current-file
  9427. :node Info-current-node))
  9428. ((eq major-mode 'calendar-mode)
  9429. (let ((cd (calendar-cursor-to-date)))
  9430. (setq link
  9431. (format-time-string
  9432. (car org-time-stamp-formats)
  9433. (apply 'encode-time
  9434. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9435. nil nil nil))))
  9436. (org-store-link-props :type "calendar" :date cd)))
  9437. ((or (eq major-mode 'vm-summary-mode)
  9438. (eq major-mode 'vm-presentation-mode))
  9439. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9440. (vm-follow-summary-cursor)
  9441. (save-excursion
  9442. (vm-select-folder-buffer)
  9443. (let* ((message (car vm-message-pointer))
  9444. (folder buffer-file-name)
  9445. (subject (vm-su-subject message))
  9446. (to (vm-get-header-contents message "To"))
  9447. (from (vm-get-header-contents message "From"))
  9448. (message-id (vm-su-message-id message)))
  9449. (org-store-link-props :type "vm" :from from :to to :subject subject
  9450. :message-id message-id)
  9451. (setq message-id (org-remove-angle-brackets message-id))
  9452. (setq folder (abbreviate-file-name folder))
  9453. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9454. folder)
  9455. (setq folder (replace-match "" t t folder)))
  9456. (setq cpltxt (org-email-link-description))
  9457. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9458. ((eq major-mode 'wl-summary-mode)
  9459. (let* ((msgnum (wl-summary-message-number))
  9460. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9461. msgnum 'message-id))
  9462. (wl-message-entity
  9463. (if (fboundp 'elmo-message-entity)
  9464. (elmo-message-entity
  9465. wl-summary-buffer-elmo-folder msgnum)
  9466. (elmo-msgdb-overview-get-entity
  9467. msgnum (wl-summary-buffer-msgdb))))
  9468. (from (wl-summary-line-from))
  9469. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  9470. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  9471. (wl-summary-line-subject))))
  9472. (org-store-link-props :type "wl" :from from :to to
  9473. :subject subject :message-id message-id)
  9474. (setq message-id (org-remove-angle-brackets message-id))
  9475. (setq cpltxt (org-email-link-description))
  9476. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9477. "#" message-id))))
  9478. ((or (equal major-mode 'mh-folder-mode)
  9479. (equal major-mode 'mh-show-mode))
  9480. (let ((from (org-mhe-get-header "From:"))
  9481. (to (org-mhe-get-header "To:"))
  9482. (message-id (org-mhe-get-header "Message-Id:"))
  9483. (subject (org-mhe-get-header "Subject:")))
  9484. (org-store-link-props :type "mh" :from from :to to
  9485. :subject subject :message-id message-id)
  9486. (setq cpltxt (org-email-link-description))
  9487. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9488. (org-remove-angle-brackets message-id)))))
  9489. ((eq major-mode 'rmail-mode)
  9490. (save-excursion
  9491. (save-restriction
  9492. (rmail-narrow-to-non-pruned-header)
  9493. (let ((folder buffer-file-name)
  9494. (message-id (mail-fetch-field "message-id"))
  9495. (from (mail-fetch-field "from"))
  9496. (to (mail-fetch-field "to"))
  9497. (subject (mail-fetch-field "subject")))
  9498. (org-store-link-props
  9499. :type "rmail" :from from :to to
  9500. :subject subject :message-id message-id)
  9501. (setq message-id (org-remove-angle-brackets message-id))
  9502. (setq cpltxt (org-email-link-description))
  9503. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9504. ((eq major-mode 'gnus-group-mode)
  9505. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9506. (gnus-group-group-name)) ; version
  9507. ((fboundp 'gnus-group-name)
  9508. (gnus-group-name))
  9509. (t "???"))))
  9510. (unless group (error "Not on a group"))
  9511. (org-store-link-props :type "gnus" :group group)
  9512. (setq cpltxt (concat
  9513. (if (org-xor arg org-usenet-links-prefer-google)
  9514. "http://groups.google.com/groups?group="
  9515. "gnus:")
  9516. group)
  9517. link (org-make-link cpltxt))))
  9518. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9519. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9520. (let* ((group gnus-newsgroup-name)
  9521. (article (gnus-summary-article-number))
  9522. (header (gnus-summary-article-header article))
  9523. (from (mail-header-from header))
  9524. (message-id (mail-header-id header))
  9525. (date (mail-header-date header))
  9526. (subject (gnus-summary-subject-string)))
  9527. (org-store-link-props :type "gnus" :from from :subject subject
  9528. :message-id message-id :group group)
  9529. (setq cpltxt (org-email-link-description))
  9530. (if (org-xor arg org-usenet-links-prefer-google)
  9531. (setq link
  9532. (concat
  9533. cpltxt "\n "
  9534. (format "http://groups.google.com/groups?as_umsgid=%s"
  9535. (org-fixup-message-id-for-http message-id))))
  9536. (setq link (org-make-link "gnus:" group
  9537. "#" (number-to-string article))))))
  9538. ((eq major-mode 'w3-mode)
  9539. (setq cpltxt (url-view-url t)
  9540. link (org-make-link cpltxt))
  9541. (org-store-link-props :type "w3" :url (url-view-url t)))
  9542. ((eq major-mode 'w3m-mode)
  9543. (setq cpltxt (or w3m-current-title w3m-current-url)
  9544. link (org-make-link w3m-current-url))
  9545. (org-store-link-props :type "w3m" :url (url-view-url t)))
  9546. ((setq search (run-hook-with-args-until-success
  9547. 'org-create-file-search-functions))
  9548. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9549. "::" search))
  9550. (setq cpltxt (or description link)))
  9551. ((eq major-mode 'image-mode)
  9552. (setq cpltxt (concat "file:"
  9553. (abbreviate-file-name buffer-file-name))
  9554. link (org-make-link cpltxt))
  9555. (org-store-link-props :type "image" :file buffer-file-name))
  9556. ((eq major-mode 'dired-mode)
  9557. ;; link to the file in the current line
  9558. (setq cpltxt (concat "file:"
  9559. (abbreviate-file-name
  9560. (expand-file-name
  9561. (dired-get-filename nil t))))
  9562. link (org-make-link cpltxt)))
  9563. ((and buffer-file-name (org-mode-p))
  9564. ;; Just link to current headline
  9565. (setq cpltxt (concat "file:"
  9566. (abbreviate-file-name buffer-file-name)))
  9567. ;; Add a context search string
  9568. (when (org-xor org-context-in-file-links arg)
  9569. ;; Check if we are on a target
  9570. (if (org-in-regexp "<<\\(.*?\\)>>")
  9571. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9572. (setq txt (cond
  9573. ((org-on-heading-p) nil)
  9574. ((org-region-active-p)
  9575. (buffer-substring (region-beginning) (region-end)))
  9576. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9577. (when (or (null txt) (string-match "\\S-" txt))
  9578. (setq cpltxt
  9579. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9580. desc "NONE"))))
  9581. (if (string-match "::\\'" cpltxt)
  9582. (setq cpltxt (substring cpltxt 0 -2)))
  9583. (setq link (org-make-link cpltxt)))
  9584. (buffer-file-name
  9585. ;; Just link to this file here.
  9586. (setq cpltxt (concat "file:"
  9587. (abbreviate-file-name buffer-file-name)))
  9588. ;; Add a context string
  9589. (when (org-xor org-context-in-file-links arg)
  9590. (setq txt (if (org-region-active-p)
  9591. (buffer-substring (region-beginning) (region-end))
  9592. (buffer-substring (point-at-bol) (point-at-eol))))
  9593. ;; Only use search option if there is some text.
  9594. (when (string-match "\\S-" txt)
  9595. (setq cpltxt
  9596. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9597. desc "NONE")))
  9598. (setq link (org-make-link cpltxt)))
  9599. ((interactive-p)
  9600. (error "Cannot link to a buffer which is not visiting a file"))
  9601. (t (setq link nil)))
  9602. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9603. (setq link (or link cpltxt)
  9604. desc (or desc cpltxt))
  9605. (if (equal desc "NONE") (setq desc nil))
  9606. (if (and (interactive-p) link)
  9607. (progn
  9608. (setq org-stored-links
  9609. (cons (list cpltxt link desc) org-stored-links))
  9610. (message "Stored: %s" (or cpltxt link)))
  9611. (org-make-link-string link desc))))
  9612. (defun org-store-link-props (&rest plist)
  9613. "Store link properties, extract names and addresses."
  9614. (let (x adr)
  9615. (when (setq x (plist-get plist :from))
  9616. (setq adr (mail-extract-address-components x))
  9617. (plist-put plist :fromname (car adr))
  9618. (plist-put plist :fromaddress (nth 1 adr)))
  9619. (when (setq x (plist-get plist :to))
  9620. (setq adr (mail-extract-address-components x))
  9621. (plist-put plist :toname (car adr))
  9622. (plist-put plist :toaddress (nth 1 adr))))
  9623. (let ((from (plist-get plist :from))
  9624. (to (plist-get plist :to)))
  9625. (when (and from to org-from-is-user-regexp)
  9626. (plist-put plist :fromto
  9627. (if (string-match org-from-is-user-regexp from)
  9628. (concat "to %t")
  9629. (concat "from %f")))))
  9630. (setq org-store-link-plist plist))
  9631. (defun org-email-link-description (&optional fmt)
  9632. "Return the description part of an email link.
  9633. This takes information from `org-store-link-plist' and formats it
  9634. according to FMT (default from `org-email-link-description-format')."
  9635. (setq fmt (or fmt org-email-link-description-format))
  9636. (let* ((p org-store-link-plist)
  9637. (to (plist-get p :toaddress))
  9638. (from (plist-get p :fromaddress))
  9639. (table
  9640. (list
  9641. (cons "%c" (plist-get p :fromto))
  9642. (cons "%F" (plist-get p :from))
  9643. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  9644. (cons "%T" (plist-get p :to))
  9645. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  9646. (cons "%s" (plist-get p :subject))
  9647. (cons "%m" (plist-get p :message-id)))))
  9648. (when (string-match "%c" fmt)
  9649. ;; Check if the user wrote this message
  9650. (if (and org-from-is-user-regexp from to
  9651. (save-match-data (string-match org-from-is-user-regexp from)))
  9652. (setq fmt (replace-match "to %t" t t fmt))
  9653. (setq fmt (replace-match "from %f" t t fmt))))
  9654. (org-replace-escapes fmt table)))
  9655. (defun org-make-org-heading-search-string (&optional string heading)
  9656. "Make search string for STRING or current headline."
  9657. (interactive)
  9658. (let ((s (or string (org-get-heading))))
  9659. (unless (and string (not heading))
  9660. ;; We are using a headline, clean up garbage in there.
  9661. (if (string-match org-todo-regexp s)
  9662. (setq s (replace-match "" t t s)))
  9663. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  9664. (setq s (replace-match "" t t s)))
  9665. (setq s (org-trim s))
  9666. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9667. org-comment-string "\\)") s)
  9668. (setq s (replace-match "" t t s)))
  9669. (while (string-match org-ts-regexp s)
  9670. (setq s (replace-match "" t t s))))
  9671. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9672. (setq s (replace-match " " t t s)))
  9673. (or string (setq s (concat "*" s))) ; Add * for headlines
  9674. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9675. (defun org-make-link (&rest strings)
  9676. "Concatenate STRINGS, format resulting string with `org-link-format'."
  9677. (apply 'concat strings))
  9678. (defun org-make-link-string (link &optional description)
  9679. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9680. (when (stringp description)
  9681. ;; Remove brackets from the description, they are fatal.
  9682. (while (string-match "\\[\\|\\]" description)
  9683. (setq description (replace-match "" t t description))))
  9684. (when (equal (org-link-escape link) description)
  9685. ;; No description needed, it is identical
  9686. (setq description nil))
  9687. (when (and (not description)
  9688. (not (equal link (org-link-escape link))))
  9689. (setq description link))
  9690. (concat "[[" (org-link-escape link) "]"
  9691. (if description (concat "[" description "]") "")
  9692. "]"))
  9693. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  9694. "Association list of escapes for some characters problematic in links.")
  9695. (defun org-link-escape (text)
  9696. "Escape charaters in TEXT that are problematic for links."
  9697. (when text
  9698. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9699. org-link-escape-chars "\\|")))
  9700. (while (string-match re text)
  9701. (setq text
  9702. (replace-match
  9703. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9704. t t text)))
  9705. text)))
  9706. (defun org-link-unescape (text)
  9707. "Reverse the action of `org-link-escape'."
  9708. (when text
  9709. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9710. org-link-escape-chars "\\|")))
  9711. (while (string-match re text)
  9712. (setq text
  9713. (replace-match
  9714. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9715. t t text)))
  9716. text)))
  9717. (defun org-xor (a b)
  9718. "Exclusive or."
  9719. (if a (not b) b))
  9720. (defun org-get-header (header)
  9721. "Find a header field in the current buffer."
  9722. (save-excursion
  9723. (goto-char (point-min))
  9724. (let ((case-fold-search t) s)
  9725. (cond
  9726. ((eq header 'from)
  9727. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9728. (setq s (match-string 1)))
  9729. (while (string-match "\"" s)
  9730. (setq s (replace-match "" t t s)))
  9731. (if (string-match "[<(].*" s)
  9732. (setq s (replace-match "" t t s))))
  9733. ((eq header 'message-id)
  9734. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9735. (setq s (match-string 1))))
  9736. ((eq header 'subject)
  9737. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9738. (setq s (match-string 1)))))
  9739. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9740. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9741. s)))
  9742. (defun org-fixup-message-id-for-http (s)
  9743. "Replace special characters in a message id, so it can be used in an http query."
  9744. (while (string-match "<" s)
  9745. (setq s (replace-match "%3C" t t s)))
  9746. (while (string-match ">" s)
  9747. (setq s (replace-match "%3E" t t s)))
  9748. (while (string-match "@" s)
  9749. (setq s (replace-match "%40" t t s)))
  9750. s)
  9751. (defun org-insert-link (&optional complete-file)
  9752. "Insert a link. At the prompt, enter the link.
  9753. Completion can be used to select a link previously stored with
  9754. `org-store-link'. When the empty string is entered (i.e. if you just
  9755. press RET at the prompt), the link defaults to the most recently
  9756. stored link. As SPC triggers completion in the minibuffer, you need to
  9757. use M-SPC or C-q SPC to force the insertion of a space character.
  9758. You will also be prompted for a description, and if one is given, it will
  9759. be displayed in the buffer instead of the link.
  9760. If there is already a link at point, this command will allow you to edit link
  9761. and description parts.
  9762. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  9763. selected using completion. The path to the file will be relative to
  9764. the current directory if the file is in the current directory or a
  9765. subdirectory. Otherwise, the link will be the absolute path as
  9766. completed in the minibuffer (i.e. normally ~/path/to/file).
  9767. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  9768. is in the current directory or below.
  9769. With three \\[universal-argument] prefixes, negate the meaning of
  9770. `org-keep-stored-link-after-insertion'."
  9771. (interactive "P")
  9772. (let ((wcf (current-window-configuration))
  9773. (region (if (org-region-active-p)
  9774. (prog1 (buffer-substring (region-beginning) (region-end))
  9775. (delete-region (region-beginning) (region-end)))))
  9776. tmphist ; byte-compile incorrectly complains about this
  9777. link desc entry remove file)
  9778. (cond
  9779. ((org-in-regexp org-bracket-link-regexp 1)
  9780. ;; We do have a link at point, and we are going to edit it.
  9781. (setq remove (list (match-beginning 0) (match-end 0)))
  9782. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9783. (setq link (read-string "Link: "
  9784. (org-link-unescape
  9785. (org-match-string-no-properties 1)))))
  9786. ((or (org-in-regexp org-angle-link-re)
  9787. (org-in-regexp org-plain-link-re))
  9788. ;; Convert to bracket link
  9789. (setq remove (list (match-beginning 0) (match-end 0))
  9790. link (read-string "Link: "
  9791. (org-remove-angle-brackets (match-string 0)))))
  9792. ((equal complete-file '(4))
  9793. ;; Completing read for file names.
  9794. (setq file (read-file-name "File: "))
  9795. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9796. (pwd1 (file-name-as-directory (abbreviate-file-name
  9797. (expand-file-name ".")))))
  9798. (cond
  9799. ((equal complete-file '(16))
  9800. (setq link (org-make-link
  9801. "file:"
  9802. (abbreviate-file-name (expand-file-name file)))))
  9803. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9804. (setq link (org-make-link "file:" (match-string 1 file))))
  9805. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9806. (expand-file-name file))
  9807. (setq link (org-make-link
  9808. "file:" (match-string 1 (expand-file-name file)))))
  9809. (t (setq link (org-make-link "file:" file))))))
  9810. (t
  9811. ;; Read link, with completion for stored links.
  9812. (with-output-to-temp-buffer "*Org Links*"
  9813. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  9814. (when org-stored-links
  9815. (princ "\nStored links are available with <up>/<down> (most recent with RET):\n\n")
  9816. (princ (mapconcat 'car (reverse org-stored-links) "\n"))))
  9817. (let ((cw (selected-window)))
  9818. (select-window (get-buffer-window "*Org Links*"))
  9819. (shrink-window-if-larger-than-buffer)
  9820. (setq truncate-lines t)
  9821. (select-window cw))
  9822. ;; Fake a link history, containing the stored links.
  9823. (setq tmphist (append (mapcar 'car org-stored-links)
  9824. org-insert-link-history))
  9825. (unwind-protect
  9826. (setq link (org-completing-read
  9827. "Link: "
  9828. (append
  9829. (mapcar (lambda (x) (concat (car x) ":"))
  9830. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  9831. (mapcar (lambda (x) (concat x ":")) org-link-types))
  9832. nil nil nil
  9833. 'tmphist
  9834. (or (car (car org-stored-links)))))
  9835. (set-window-configuration wcf)
  9836. (kill-buffer "*Org Links*"))
  9837. (setq entry (assoc link org-stored-links))
  9838. (or entry (push link org-insert-link-history))
  9839. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9840. (not org-keep-stored-link-after-insertion))
  9841. (setq org-stored-links (delq (assoc link org-stored-links)
  9842. org-stored-links)))
  9843. (setq link (if entry (nth 1 entry) link)
  9844. desc (or region desc (nth 2 entry)))))
  9845. (if (string-match org-plain-link-re link)
  9846. ;; URL-like link, normalize the use of angular brackets.
  9847. (setq link (org-make-link (org-remove-angle-brackets link))))
  9848. ;; Check if we are linking to the current file with a search option
  9849. ;; If yes, simplify the link by using only the search option.
  9850. (when (and buffer-file-name
  9851. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  9852. (let* ((path (match-string 1 link))
  9853. (case-fold-search nil)
  9854. (search (match-string 2 link)))
  9855. (save-match-data
  9856. (if (equal (file-truename buffer-file-name) (file-truename path))
  9857. ;; We are linking to this same file, with a search option
  9858. (setq link search)))))
  9859. ;; Check if we can/should use a relative path. If yes, simplify the link
  9860. (when (string-match "\\<file:\\(.*\\)" link)
  9861. (let* ((path (match-string 1 link))
  9862. (case-fold-search nil))
  9863. (cond
  9864. ((eq org-link-file-path-type 'absolute)
  9865. (setq path (abbreviate-file-name (expand-file-name path))))
  9866. ((eq org-link-file-path-type 'noabbrev)
  9867. (setq path (expand-file-name path)))
  9868. ((eq org-link-file-path-type 'relative)
  9869. (setq path (file-relative-name path)))
  9870. (t
  9871. (save-match-data
  9872. (if (string-match (concat "^" (regexp-quote
  9873. (file-name-as-directory
  9874. (expand-file-name "."))))
  9875. (expand-file-name path))
  9876. ;; We are linking a file with relative path name.
  9877. (setq path (substring (expand-file-name path)
  9878. (match-end 0)))))))
  9879. (setq link (concat "file:" path))))
  9880. (setq desc (read-string "Description: " desc))
  9881. (unless (string-match "\\S-" desc) (setq desc nil))
  9882. (if remove (apply 'delete-region remove))
  9883. (insert (org-make-link-string link desc))))
  9884. (defun org-completing-read (&rest args)
  9885. (let ((minibuffer-local-completion-map
  9886. (copy-keymap minibuffer-local-completion-map)))
  9887. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9888. (apply 'completing-read args)))
  9889. ;;; Opening/following a link
  9890. (defvar org-link-search-failed nil)
  9891. (defun org-next-link ()
  9892. "Move forward to the next link.
  9893. If the link is in hidden text, expose it."
  9894. (interactive)
  9895. (when (and org-link-search-failed (eq this-command last-command))
  9896. (goto-char (point-min))
  9897. (message "Link search wrapped back to beginning of buffer"))
  9898. (setq org-link-search-failed nil)
  9899. (let* ((pos (point))
  9900. (ct (org-context))
  9901. (a (assoc :link ct)))
  9902. (if a (goto-char (nth 2 a)))
  9903. (if (re-search-forward org-any-link-re nil t)
  9904. (progn
  9905. (goto-char (match-beginning 0))
  9906. (if (org-invisible-p) (org-show-context)))
  9907. (goto-char pos)
  9908. (setq org-link-search-failed t)
  9909. (error "No further link found"))))
  9910. (defun org-previous-link ()
  9911. "Move backward to the previous link.
  9912. If the link is in hidden text, expose it."
  9913. (interactive)
  9914. (when (and org-link-search-failed (eq this-command last-command))
  9915. (goto-char (point-max))
  9916. (message "Link search wrapped back to end of buffer"))
  9917. (setq org-link-search-failed nil)
  9918. (let* ((pos (point))
  9919. (ct (org-context))
  9920. (a (assoc :link ct)))
  9921. (if a (goto-char (nth 1 a)))
  9922. (if (re-search-backward org-any-link-re nil t)
  9923. (progn
  9924. (goto-char (match-beginning 0))
  9925. (if (org-invisible-p) (org-show-context)))
  9926. (goto-char pos)
  9927. (setq org-link-search-failed t)
  9928. (error "No further link found"))))
  9929. (defun org-find-file-at-mouse (ev)
  9930. "Open file link or URL at mouse."
  9931. (interactive "e")
  9932. (mouse-set-point ev)
  9933. (org-open-at-point 'in-emacs))
  9934. (defun org-open-at-mouse (ev)
  9935. "Open file link or URL at mouse."
  9936. (interactive "e")
  9937. (mouse-set-point ev)
  9938. (org-open-at-point))
  9939. (defvar org-window-config-before-follow-link nil
  9940. "The window configuration before following a link.
  9941. This is saved in case the need arises to restore it.")
  9942. (defvar org-open-link-marker (make-marker)
  9943. "Marker pointing to the location where `org-open-at-point; was called.")
  9944. (defun org-open-at-point (&optional in-emacs)
  9945. "Open link at or after point.
  9946. If there is no link at point, this function will search forward up to
  9947. the end of the current subtree.
  9948. Normally, files will be opened by an appropriate application. If the
  9949. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  9950. (interactive "P")
  9951. (move-marker org-open-link-marker (point))
  9952. (setq org-window-config-before-follow-link (current-window-configuration))
  9953. (org-remove-occur-highlights nil nil t)
  9954. (if (org-at-timestamp-p t)
  9955. (org-follow-timestamp-link)
  9956. (let (type path link line search (pos (point)))
  9957. (catch 'match
  9958. (save-excursion
  9959. (skip-chars-forward "^]\n\r")
  9960. (when (org-in-regexp org-bracket-link-regexp)
  9961. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  9962. (while (string-match " *\n *" link)
  9963. (setq link (replace-match " " t t link)))
  9964. (setq link (org-link-expand-abbrev link))
  9965. (if (string-match org-link-re-with-space2 link)
  9966. (setq type (match-string 1 link) path (match-string 2 link))
  9967. (setq type "thisfile" path link))
  9968. (throw 'match t)))
  9969. (when (get-text-property (point) 'org-linked-text)
  9970. (setq type "thisfile"
  9971. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9972. (1+ (point)) (point))
  9973. path (buffer-substring
  9974. (previous-single-property-change pos 'org-linked-text)
  9975. (next-single-property-change pos 'org-linked-text)))
  9976. (throw 'match t))
  9977. (save-excursion
  9978. (when (or (org-in-regexp org-angle-link-re)
  9979. (org-in-regexp org-plain-link-re))
  9980. (setq type (match-string 1) path (match-string 2))
  9981. (throw 'match t)))
  9982. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  9983. (setq type "tree-match"
  9984. path (match-string 1))
  9985. (throw 'match t))
  9986. (save-excursion
  9987. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  9988. (setq type "tags"
  9989. path (match-string 1))
  9990. (while (string-match ":" path)
  9991. (setq path (replace-match "+" t t path)))
  9992. (throw 'match t))))
  9993. (unless path
  9994. (error "No link found"))
  9995. ;; Remove any trailing spaces in path
  9996. (if (string-match " +\\'" path)
  9997. (setq path (replace-match "" t t path)))
  9998. (cond
  9999. ((equal type "mailto")
  10000. (let ((cmd (car org-link-mailto-program))
  10001. (args (cdr org-link-mailto-program)) args1
  10002. (address path) (subject "") a)
  10003. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  10004. (setq address (match-string 1 path)
  10005. subject (org-link-escape (match-string 2 path))))
  10006. (while args
  10007. (cond
  10008. ((not (stringp (car args))) (push (pop args) args1))
  10009. (t (setq a (pop args))
  10010. (if (string-match "%a" a)
  10011. (setq a (replace-match address t t a)))
  10012. (if (string-match "%s" a)
  10013. (setq a (replace-match subject t t a)))
  10014. (push a args1))))
  10015. (apply cmd (nreverse args1))))
  10016. ((member type '("http" "https" "ftp" "news"))
  10017. (browse-url (concat type ":" path)))
  10018. ((string= type "tags")
  10019. (org-tags-view in-emacs path))
  10020. ((string= type "thisfile")
  10021. (if in-emacs
  10022. (switch-to-buffer-other-window
  10023. (org-get-buffer-for-internal-link (current-buffer)))
  10024. (org-mark-ring-push))
  10025. (org-link-search
  10026. path
  10027. (cond ((equal in-emacs '(4)) 'occur)
  10028. ((equal in-emacs '(16)) 'org-occur)
  10029. (t nil))
  10030. pos))
  10031. ((string= type "tree-match")
  10032. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  10033. ((string= type "file")
  10034. (if (string-match "::\\([0-9]+\\)\\'" path)
  10035. (setq line (string-to-number (match-string 1 path))
  10036. path (substring path 0 (match-beginning 0)))
  10037. (if (string-match "::\\(.+\\)\\'" path)
  10038. (setq search (match-string 1 path)
  10039. path (substring path 0 (match-beginning 0)))))
  10040. (org-open-file path in-emacs line search))
  10041. ((string= type "news")
  10042. (org-follow-gnus-link path))
  10043. ((string= type "bbdb")
  10044. (org-follow-bbdb-link path))
  10045. ((string= type "info")
  10046. (org-follow-info-link path))
  10047. ((string= type "gnus")
  10048. (let (group article)
  10049. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10050. (error "Error in Gnus link"))
  10051. (setq group (match-string 1 path)
  10052. article (match-string 3 path))
  10053. (org-follow-gnus-link group article)))
  10054. ((string= type "vm")
  10055. (let (folder article)
  10056. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10057. (error "Error in VM link"))
  10058. (setq folder (match-string 1 path)
  10059. article (match-string 3 path))
  10060. ;; in-emacs is the prefix arg, will be interpreted as read-only
  10061. (org-follow-vm-link folder article in-emacs)))
  10062. ((string= type "wl")
  10063. (let (folder article)
  10064. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10065. (error "Error in Wanderlust link"))
  10066. (setq folder (match-string 1 path)
  10067. article (match-string 3 path))
  10068. (org-follow-wl-link folder article)))
  10069. ((string= type "mhe")
  10070. (let (folder article)
  10071. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10072. (error "Error in MHE link"))
  10073. (setq folder (match-string 1 path)
  10074. article (match-string 3 path))
  10075. (org-follow-mhe-link folder article)))
  10076. ((string= type "rmail")
  10077. (let (folder article)
  10078. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10079. (error "Error in RMAIL link"))
  10080. (setq folder (match-string 1 path)
  10081. article (match-string 3 path))
  10082. (org-follow-rmail-link folder article)))
  10083. ((string= type "shell")
  10084. (let ((cmd path))
  10085. ;; FIXME: the following is only for backward compatibility
  10086. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  10087. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  10088. (if (or (not org-confirm-shell-link-function)
  10089. (funcall org-confirm-shell-link-function
  10090. (format "Execute \"%s\" in shell? "
  10091. (org-add-props cmd nil
  10092. 'face 'org-warning))))
  10093. (progn
  10094. (message "Executing %s" cmd)
  10095. (shell-command cmd))
  10096. (error "Abort"))))
  10097. ((string= type "elisp")
  10098. (let ((cmd path))
  10099. (if (or (not org-confirm-elisp-link-function)
  10100. (funcall org-confirm-elisp-link-function
  10101. (format "Execute \"%s\" as elisp? "
  10102. (org-add-props cmd nil
  10103. 'face 'org-warning))))
  10104. (message "%s => %s" cmd (eval (read cmd)))
  10105. (error "Abort"))))
  10106. (t
  10107. (browse-url-at-point)))))
  10108. (move-marker org-open-link-marker nil))
  10109. ;;; File search
  10110. (defvar org-create-file-search-functions nil
  10111. "List of functions to construct the right search string for a file link.
  10112. These functions are called in turn with point at the location to
  10113. which the link should point.
  10114. A function in the hook should first test if it would like to
  10115. handle this file type, for example by checking the major-mode or
  10116. the file extension. If it decides not to handle this file, it
  10117. should just return nil to give other functions a chance. If it
  10118. does handle the file, it must return the search string to be used
  10119. when following the link. The search string will be part of the
  10120. file link, given after a double colon, and `org-open-at-point'
  10121. will automatically search for it. If special measures must be
  10122. taken to make the search successful, another function should be
  10123. added to the companion hook `org-execute-file-search-functions',
  10124. which see.
  10125. A function in this hook may also use `setq' to set the variable
  10126. `description' to provide a suggestion for the descriptive text to
  10127. be used for this link when it gets inserted into an Org-mode
  10128. buffer with \\[org-insert-link].")
  10129. (defvar org-execute-file-search-functions nil
  10130. "List of functions to execute a file search triggered by a link.
  10131. Functions added to this hook must accept a single argument, the
  10132. search string that was part of the file link, the part after the
  10133. double colon. The function must first check if it would like to
  10134. handle this search, for example by checking the major-mode or the
  10135. file extension. If it decides not to handle this search, it
  10136. should just return nil to give other functions a chance. If it
  10137. does handle the search, it must return a non-nil value to keep
  10138. other functions from trying.
  10139. Each function can access the current prefix argument through the
  10140. variable `current-prefix-argument'. Note that a single prefix is
  10141. used to force opening a link in Emacs, so it may be good to only
  10142. use a numeric or double prefix to guide the search function.
  10143. In case this is needed, a function in this hook can also restore
  10144. the window configuration before `org-open-at-point' was called using:
  10145. (set-window-configuration org-window-config-before-follow-link)")
  10146. (defun org-link-search (s &optional type avoid-pos)
  10147. "Search for a link search option.
  10148. If S is surrounded by forward slashes, it is interpreted as a
  10149. regular expression. In org-mode files, this will create an `org-occur'
  10150. sparse tree. In ordinary files, `occur' will be used to list matches.
  10151. If the current buffer is in `dired-mode', grep will be used to search
  10152. in all files. If AVOID-POS is given, ignore matches near that position."
  10153. (let ((case-fold-search t)
  10154. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  10155. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  10156. (append '((" ") ("\t") ("\n"))
  10157. org-emphasis-alist)
  10158. "\\|") "\\)"))
  10159. (pos (point))
  10160. (pre "") (post "")
  10161. words re0 re1 re2 re3 re4 re5 re2a reall)
  10162. (cond
  10163. ;; First check if there are any special
  10164. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  10165. ;; Now try the builtin stuff
  10166. ((save-excursion
  10167. (goto-char (point-min))
  10168. (and
  10169. (re-search-forward
  10170. (concat "<<" (regexp-quote s0) ">>") nil t)
  10171. (setq pos (match-beginning 0))))
  10172. ;; There is an exact target for this
  10173. (goto-char pos))
  10174. ((string-match "^/\\(.*\\)/$" s)
  10175. ;; A regular expression
  10176. (cond
  10177. ((org-mode-p)
  10178. (org-occur (match-string 1 s)))
  10179. ;;((eq major-mode 'dired-mode)
  10180. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  10181. (t (org-do-occur (match-string 1 s)))))
  10182. (t
  10183. ;; A normal search string
  10184. (when (equal (string-to-char s) ?*)
  10185. ;; Anchor on headlines, post may include tags.
  10186. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  10187. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  10188. s (substring s 1)))
  10189. (remove-text-properties
  10190. 0 (length s)
  10191. '(face nil mouse-face nil keymap nil fontified nil) s)
  10192. ;; Make a series of regular expressions to find a match
  10193. (setq words (org-split-string s "[ \n\r\t]+")
  10194. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10195. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10196. "\\)" markers)
  10197. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  10198. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10199. re1 (concat pre re2 post)
  10200. re3 (concat pre re4 post)
  10201. re5 (concat pre ".*" re4)
  10202. re2 (concat pre re2)
  10203. re2a (concat pre re2a)
  10204. re4 (concat pre re4)
  10205. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10206. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10207. re5 "\\)"
  10208. ))
  10209. (cond
  10210. ((eq type 'org-occur) (org-occur reall))
  10211. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10212. (t (goto-char (point-min))
  10213. (if (or (org-search-not-self 1 re0 nil t)
  10214. (org-search-not-self 1 re1 nil t)
  10215. (org-search-not-self 1 re2 nil t)
  10216. (org-search-not-self 1 re2a nil t)
  10217. (org-search-not-self 1 re3 nil t)
  10218. (org-search-not-self 1 re4 nil t)
  10219. (org-search-not-self 1 re5 nil t)
  10220. )
  10221. (goto-char (match-beginning 1))
  10222. (goto-char pos)
  10223. (error "No match")))))
  10224. (t
  10225. ;; Normal string-search
  10226. (goto-char (point-min))
  10227. (if (search-forward s nil t)
  10228. (goto-char (match-beginning 0))
  10229. (error "No match"))))
  10230. (and (org-mode-p) (org-show-context 'link-search))))
  10231. (defun org-search-not-self (group &rest args)
  10232. "Execute `re-search-forward', but only accept matches that do not
  10233. enclose the position of `org-open-link-marker'."
  10234. (let ((m org-open-link-marker))
  10235. (catch 'exit
  10236. (while (apply 're-search-forward args)
  10237. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10238. (goto-char (match-end group))
  10239. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10240. (> (match-beginning 0) (marker-position m))
  10241. (< (match-end 0) (marker-position m)))
  10242. (save-match-data
  10243. (or (not (org-in-regexp
  10244. org-bracket-link-analytic-regexp 1))
  10245. (not (match-end 4)) ; no description
  10246. (and (<= (match-beginning 4) (point))
  10247. (>= (match-end 4) (point))))))
  10248. (throw 'exit (point))))))))
  10249. (defun org-get-buffer-for-internal-link (buffer)
  10250. "Return a buffer to be used for displaying the link target of internal links."
  10251. (cond
  10252. ((not org-display-internal-link-with-indirect-buffer)
  10253. buffer)
  10254. ((string-match "(Clone)$" (buffer-name buffer))
  10255. (message "Buffer is already a clone, not making another one")
  10256. ;; we also do not modify visibility in this case
  10257. buffer)
  10258. (t ; make a new indirect buffer for displaying the link
  10259. (let* ((bn (buffer-name buffer))
  10260. (ibn (concat bn "(Clone)"))
  10261. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10262. (with-current-buffer ib (org-overview))
  10263. ib))))
  10264. (defun org-do-occur (regexp &optional cleanup)
  10265. "Call the Emacs command `occur'.
  10266. If CLEANUP is non-nil, remove the printout of the regular expression
  10267. in the *Occur* buffer. This is useful if the regex is long and not useful
  10268. to read."
  10269. (occur regexp)
  10270. (when cleanup
  10271. (let ((cwin (selected-window)) win beg end)
  10272. (when (setq win (get-buffer-window "*Occur*"))
  10273. (select-window win))
  10274. (goto-char (point-min))
  10275. (when (re-search-forward "match[a-z]+" nil t)
  10276. (setq beg (match-end 0))
  10277. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10278. (setq end (1- (match-beginning 0)))))
  10279. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  10280. (goto-char (point-min))
  10281. (select-window cwin))))
  10282. ;;; The mark ring for links jumps
  10283. (defvar org-mark-ring nil
  10284. "Mark ring for positions before jumps in Org-mode.")
  10285. (defvar org-mark-ring-last-goto nil
  10286. "Last position in the mark ring used to go back.")
  10287. ;; Fill and close the ring
  10288. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10289. (loop for i from 1 to org-mark-ring-length do
  10290. (push (make-marker) org-mark-ring))
  10291. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10292. org-mark-ring)
  10293. (defun org-mark-ring-push (&optional pos buffer)
  10294. "Put the current position or POS into the mark ring and rotate it."
  10295. (interactive)
  10296. (setq pos (or pos (point)))
  10297. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10298. (move-marker (car org-mark-ring)
  10299. (or pos (point))
  10300. (or buffer (current-buffer)))
  10301. (message
  10302. (substitute-command-keys
  10303. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10304. (defun org-mark-ring-goto (&optional n)
  10305. "Jump to the previous position in the mark ring.
  10306. With prefix arg N, jump back that many stored positions. When
  10307. called several times in succession, walk through the entire ring.
  10308. Org-mode commands jumping to a different position in the current file,
  10309. or to another Org-mode file, automatically push the old position
  10310. onto the ring."
  10311. (interactive "p")
  10312. (let (p m)
  10313. (if (eq last-command this-command)
  10314. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10315. (setq p org-mark-ring))
  10316. (setq org-mark-ring-last-goto p)
  10317. (setq m (car p))
  10318. (switch-to-buffer (marker-buffer m))
  10319. (goto-char m)
  10320. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10321. (defun org-remove-angle-brackets (s)
  10322. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10323. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10324. s)
  10325. (defun org-add-angle-brackets (s)
  10326. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10327. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10328. s)
  10329. ;;; Following specific links
  10330. (defun org-follow-timestamp-link ()
  10331. (cond
  10332. ((org-at-date-range-p t)
  10333. (let ((org-agenda-start-on-weekday)
  10334. (t1 (match-string 1))
  10335. (t2 (match-string 2)))
  10336. (setq t1 (time-to-days (org-time-string-to-time t1))
  10337. t2 (time-to-days (org-time-string-to-time t2)))
  10338. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  10339. ((org-at-timestamp-p t)
  10340. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10341. (substring (match-string 1) 0 10)))
  10342. 1))
  10343. (t (error "This should not happen"))))
  10344. (defun org-follow-bbdb-link (name)
  10345. "Follow a BBDB link to NAME."
  10346. (require 'bbdb)
  10347. (let ((inhibit-redisplay (not debug-on-error))
  10348. (bbdb-electric-p nil))
  10349. (catch 'exit
  10350. ;; Exact match on name
  10351. (bbdb-name (concat "\\`" name "\\'") nil)
  10352. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10353. ;; Exact match on name
  10354. (bbdb-company (concat "\\`" name "\\'") nil)
  10355. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10356. ;; Partial match on name
  10357. (bbdb-name name nil)
  10358. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10359. ;; Partial match on company
  10360. (bbdb-company name nil)
  10361. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10362. ;; General match including network address and notes
  10363. (bbdb name nil)
  10364. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  10365. (delete-window (get-buffer-window "*BBDB*"))
  10366. (error "No matching BBDB record")))))
  10367. (defun org-follow-info-link (name)
  10368. "Follow an info file & node link to NAME."
  10369. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  10370. (string-match "\\(.*\\)" name))
  10371. (progn
  10372. (require 'info)
  10373. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  10374. (Info-find-node (match-string 1 name) (match-string 2 name))
  10375. (Info-find-node (match-string 1 name) "Top")))
  10376. (message (concat "Could not open: " name))))
  10377. (defun org-follow-gnus-link (&optional group article)
  10378. "Follow a Gnus link to GROUP and ARTICLE."
  10379. (require 'gnus)
  10380. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  10381. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  10382. (cond ((and group article)
  10383. (gnus-group-read-group 1 nil group)
  10384. (gnus-summary-goto-article (string-to-number article) nil t))
  10385. (group (gnus-group-jump-to-group group))))
  10386. (defun org-follow-vm-link (&optional folder article readonly)
  10387. "Follow a VM link to FOLDER and ARTICLE."
  10388. (require 'vm)
  10389. (setq article (org-add-angle-brackets article))
  10390. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  10391. ;; ange-ftp or efs or tramp access
  10392. (let ((user (or (match-string 1 folder) (user-login-name)))
  10393. (host (match-string 2 folder))
  10394. (file (match-string 3 folder)))
  10395. (cond
  10396. ((featurep 'tramp)
  10397. ;; use tramp to access the file
  10398. (if (featurep 'xemacs)
  10399. (setq folder (format "[%s@%s]%s" user host file))
  10400. (setq folder (format "/%s@%s:%s" user host file))))
  10401. (t
  10402. ;; use ange-ftp or efs
  10403. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  10404. (setq folder (format "/%s@%s:%s" user host file))))))
  10405. (when folder
  10406. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  10407. (sit-for 0.1)
  10408. (when article
  10409. (vm-select-folder-buffer)
  10410. (widen)
  10411. (let ((case-fold-search t))
  10412. (goto-char (point-min))
  10413. (if (not (re-search-forward
  10414. (concat "^" "message-id: *" (regexp-quote article))))
  10415. (error "Could not find the specified message in this folder"))
  10416. (vm-isearch-update)
  10417. (vm-isearch-narrow)
  10418. (vm-beginning-of-message)
  10419. (vm-summarize)))))
  10420. (defun org-follow-wl-link (folder article)
  10421. "Follow a Wanderlust link to FOLDER and ARTICLE."
  10422. (if (and (string= folder "%")
  10423. article
  10424. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  10425. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  10426. ;; Thus, we recompose folder and article ids.
  10427. (setq folder (format "%s#%s" folder (match-string 1 article))
  10428. article (match-string 3 article)))
  10429. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  10430. (error "No such folder: %s" folder))
  10431. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  10432. (and article
  10433. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  10434. (wl-summary-redisplay)))
  10435. (defun org-follow-rmail-link (folder article)
  10436. "Follow an RMAIL link to FOLDER and ARTICLE."
  10437. (setq article (org-add-angle-brackets article))
  10438. (let (message-number)
  10439. (save-excursion
  10440. (save-window-excursion
  10441. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10442. (setq message-number
  10443. (save-restriction
  10444. (widen)
  10445. (goto-char (point-max))
  10446. (if (re-search-backward
  10447. (concat "^Message-ID:\\s-+" (regexp-quote
  10448. (or article "")))
  10449. nil t)
  10450. (rmail-what-message))))))
  10451. (if message-number
  10452. (progn
  10453. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10454. (rmail-show-message message-number)
  10455. message-number)
  10456. (error "Message not found"))))
  10457. ;;; mh-e integration based on planner-mode
  10458. (defun org-mhe-get-message-real-folder ()
  10459. "Return the name of the current message real folder, so if you use
  10460. sequences, it will now work."
  10461. (save-excursion
  10462. (let* ((folder
  10463. (if (equal major-mode 'mh-folder-mode)
  10464. mh-current-folder
  10465. ;; Refer to the show buffer
  10466. mh-show-folder-buffer))
  10467. (end-index
  10468. (if (boundp 'mh-index-folder)
  10469. (min (length mh-index-folder) (length folder))))
  10470. )
  10471. ;; a simple test on mh-index-data does not work, because
  10472. ;; mh-index-data is always nil in a show buffer.
  10473. (if (and (boundp 'mh-index-folder)
  10474. (string= mh-index-folder (substring folder 0 end-index)))
  10475. (if (equal major-mode 'mh-show-mode)
  10476. (save-window-excursion
  10477. (when (buffer-live-p (get-buffer folder))
  10478. (progn
  10479. (pop-to-buffer folder)
  10480. (org-mhe-get-message-folder-from-index)
  10481. )
  10482. ))
  10483. (org-mhe-get-message-folder-from-index)
  10484. )
  10485. folder
  10486. )
  10487. )))
  10488. (defun org-mhe-get-message-folder-from-index ()
  10489. "Returns the name of the message folder in a index folder buffer."
  10490. (save-excursion
  10491. (mh-index-previous-folder)
  10492. (re-search-forward "^\\(+.*\\)$" nil t)
  10493. (message (match-string 1))))
  10494. (defun org-mhe-get-message-folder ()
  10495. "Return the name of the current message folder. Be careful if you
  10496. use sequences."
  10497. (save-excursion
  10498. (if (equal major-mode 'mh-folder-mode)
  10499. mh-current-folder
  10500. ;; Refer to the show buffer
  10501. mh-show-folder-buffer)))
  10502. (defun org-mhe-get-message-num ()
  10503. "Return the number of the current message. Be careful if you
  10504. use sequences."
  10505. (save-excursion
  10506. (if (equal major-mode 'mh-folder-mode)
  10507. (mh-get-msg-num nil)
  10508. ;; Refer to the show buffer
  10509. (mh-show-buffer-message-number))))
  10510. (defun org-mhe-get-header (header)
  10511. "Return a header of the message in folder mode. This will create a
  10512. show buffer for the corresponding message. If you have a more clever
  10513. idea..."
  10514. (let* ((folder (org-mhe-get-message-folder))
  10515. (num (org-mhe-get-message-num))
  10516. (buffer (get-buffer-create (concat "show-" folder)))
  10517. (header-field))
  10518. (with-current-buffer buffer
  10519. (mh-display-msg num folder)
  10520. (if (equal major-mode 'mh-folder-mode)
  10521. (mh-header-display)
  10522. (mh-show-header-display))
  10523. (set-buffer buffer)
  10524. (setq header-field (mh-get-header-field header))
  10525. (if (equal major-mode 'mh-folder-mode)
  10526. (mh-show)
  10527. (mh-show-show))
  10528. header-field)))
  10529. (defun org-follow-mhe-link (folder article)
  10530. "Follow an MHE link to FOLDER and ARTICLE.
  10531. If ARTICLE is nil FOLDER is shown. If the configuration variable
  10532. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  10533. ARTICLE is searched in all folders. Indexed searches (swish++,
  10534. namazu, and others supported by MH-E) will always search in all
  10535. folders."
  10536. (require 'mh-e)
  10537. (require 'mh-search)
  10538. (require 'mh-utils)
  10539. (mh-find-path)
  10540. (if (not article)
  10541. (mh-visit-folder (mh-normalize-folder-name folder))
  10542. (setq article (org-add-angle-brackets article))
  10543. (mh-search-choose)
  10544. (if (equal mh-searcher 'pick)
  10545. (progn
  10546. (mh-search folder (list "--message-id" article))
  10547. (when (and org-mhe-search-all-folders
  10548. (not (org-mhe-get-message-real-folder)))
  10549. (kill-this-buffer)
  10550. (mh-search "+" (list "--message-id" article))))
  10551. (mh-search "+" article))
  10552. (if (org-mhe-get-message-real-folder)
  10553. (mh-show-msg 1)
  10554. (kill-this-buffer)
  10555. (error "Message not found"))))
  10556. ;;; BibTeX links
  10557. ;; Use the custom search meachnism to construct and use search strings for
  10558. ;; file links to BibTeX database entries.
  10559. (defun org-create-file-search-in-bibtex ()
  10560. "Create the search string and description for a BibTeX database entry."
  10561. (when (eq major-mode 'bibtex-mode)
  10562. ;; yes, we want to construct this search string.
  10563. ;; Make a good description for this entry, using names, year and the title
  10564. ;; Put it into the `description' variable which is dynamically scoped.
  10565. (let ((bibtex-autokey-names 1)
  10566. (bibtex-autokey-names-stretch 1)
  10567. (bibtex-autokey-name-case-convert-function 'identity)
  10568. (bibtex-autokey-name-separator " & ")
  10569. (bibtex-autokey-additional-names " et al.")
  10570. (bibtex-autokey-year-length 4)
  10571. (bibtex-autokey-name-year-separator " ")
  10572. (bibtex-autokey-titlewords 3)
  10573. (bibtex-autokey-titleword-separator " ")
  10574. (bibtex-autokey-titleword-case-convert-function 'identity)
  10575. (bibtex-autokey-titleword-length 'infty)
  10576. (bibtex-autokey-year-title-separator ": "))
  10577. (setq description (bibtex-generate-autokey)))
  10578. ;; Now parse the entry, get the key and return it.
  10579. (save-excursion
  10580. (bibtex-beginning-of-entry)
  10581. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  10582. (defun org-execute-file-search-in-bibtex (s)
  10583. "Find the link search string S as a key for a database entry."
  10584. (when (eq major-mode 'bibtex-mode)
  10585. ;; Yes, we want to do the search in this file.
  10586. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  10587. (goto-char (point-min))
  10588. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  10589. (regexp-quote s) "[ \t\n]*,") nil t)
  10590. (goto-char (match-beginning 0)))
  10591. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  10592. ;; Use double prefix to indicate that any web link should be browsed
  10593. (let ((b (current-buffer)) (p (point)))
  10594. ;; Restore the window configuration because we just use the web link
  10595. (set-window-configuration org-window-config-before-follow-link)
  10596. (save-excursion (set-buffer b) (goto-char p)
  10597. (bibtex-url)))
  10598. (recenter 0)) ; Move entry start to beginning of window
  10599. ;; return t to indicate that the search is done.
  10600. t))
  10601. ;; Finally add the functions to the right hooks.
  10602. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  10603. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  10604. ;; end of Bibtex link setup
  10605. ;;; Following file links
  10606. (defun org-open-file (path &optional in-emacs line search)
  10607. "Open the file at PATH.
  10608. First, this expands any special file name abbreviations. Then the
  10609. configuration variable `org-file-apps' is checked if it contains an
  10610. entry for this file type, and if yes, the corresponding command is launched.
  10611. If no application is found, Emacs simply visits the file.
  10612. With optional argument IN-EMACS, Emacs will visit the file.
  10613. Optional LINE specifies a line to go to, optional SEARCH a string to
  10614. search for. If LINE or SEARCH is given, the file will always be
  10615. opened in Emacs.
  10616. If the file does not exist, an error is thrown."
  10617. (setq in-emacs (or in-emacs line search))
  10618. (let* ((file (if (equal path "")
  10619. buffer-file-name
  10620. (substitute-in-file-name (expand-file-name path))))
  10621. (apps (append org-file-apps (org-default-apps)))
  10622. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10623. (dirp (if remp nil (file-directory-p file)))
  10624. (dfile (downcase file))
  10625. (old-buffer (current-buffer))
  10626. (old-pos (point))
  10627. (old-mode major-mode)
  10628. ext cmd)
  10629. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10630. (setq ext (match-string 1 dfile))
  10631. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10632. (setq ext (match-string 1 dfile))))
  10633. (if in-emacs
  10634. (setq cmd 'emacs)
  10635. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10636. (and dirp (cdr (assoc 'directory apps)))
  10637. (cdr (assoc ext apps))
  10638. (cdr (assoc t apps)))))
  10639. (when (eq cmd 'mailcap)
  10640. (require 'mailcap)
  10641. (mailcap-parse-mailcaps)
  10642. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10643. (command (mailcap-mime-info mime-type)))
  10644. (if (stringp command)
  10645. (setq cmd command)
  10646. (setq cmd 'emacs))))
  10647. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10648. (not (file-exists-p file))
  10649. (not org-open-non-existing-files))
  10650. (error "No such file: %s" file))
  10651. (cond
  10652. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10653. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10654. (if (string-match "['\"]%s['\"]" cmd)
  10655. (setq cmd (replace-match "%s" t t cmd)))
  10656. (setq cmd (format cmd (shell-quote-argument file)))
  10657. (save-window-excursion
  10658. (shell-command (concat cmd " &"))))
  10659. ((or (stringp cmd)
  10660. (eq cmd 'emacs))
  10661. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10662. (if line (goto-line line)
  10663. (if search (org-link-search search))))
  10664. ((consp cmd)
  10665. (eval cmd))
  10666. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10667. (and (org-mode-p) (eq old-mode 'org-mode)
  10668. (or (not (equal old-buffer (current-buffer)))
  10669. (not (equal old-pos (point))))
  10670. (org-mark-ring-push old-pos old-buffer))))
  10671. (defun org-default-apps ()
  10672. "Return the default applications for this operating system."
  10673. (cond
  10674. ((eq system-type 'darwin)
  10675. org-file-apps-defaults-macosx)
  10676. ((eq system-type 'windows-nt)
  10677. org-file-apps-defaults-windowsnt)
  10678. (t org-file-apps-defaults-gnu)))
  10679. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10680. (defun org-file-remote-p (file)
  10681. "Test whether FILE specifies a location on a remote system.
  10682. Return non-nil if the location is indeed remote.
  10683. For example, the filename \"/user@host:/foo\" specifies a location
  10684. on the system \"/user@host:\"."
  10685. (cond ((fboundp 'file-remote-p)
  10686. (file-remote-p file))
  10687. ((fboundp 'tramp-handle-file-remote-p)
  10688. (tramp-handle-file-remote-p file))
  10689. ((and (boundp 'ange-ftp-name-format)
  10690. (string-match (car ange-ftp-name-format) file))
  10691. t)
  10692. (t nil)))
  10693. ;;;; Hooks for remember.el
  10694. ;;;###autoload
  10695. (defun org-remember-annotation ()
  10696. "Return a link to the current location as an annotation for remember.el.
  10697. If you are using Org-mode files as target for data storage with
  10698. remember.el, then the annotations should include a link compatible with the
  10699. conventions in Org-mode. This function returns such a link."
  10700. (org-store-link nil))
  10701. (defconst org-remember-help
  10702. "Select a destination location for the note.
  10703. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  10704. RET at beg-of-buf -> Append to file as level 2 headline
  10705. RET on headline -> Store as sublevel entry to current headline
  10706. <left>/<right> -> before/after current headline, same headings level")
  10707. ;;;###autoload
  10708. (defun org-remember-apply-template (&optional use-char skip-interactive)
  10709. "Initialize *remember* buffer with template, invoke `org-mode'.
  10710. This function should be placed into `remember-mode-hook' and in fact requires
  10711. to be run from that hook to fucntion properly."
  10712. (if org-remember-templates
  10713. (let* ((char (or use-char
  10714. (if (= (length org-remember-templates) 1)
  10715. (caar org-remember-templates)
  10716. (message "Select template: %s"
  10717. (mapconcat
  10718. (lambda (x) (char-to-string (car x)))
  10719. org-remember-templates " "))
  10720. (read-char-exclusive))))
  10721. (entry (cdr (assoc char org-remember-templates)))
  10722. (tpl (car entry))
  10723. (plist-p (if org-store-link-plist t nil))
  10724. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  10725. (string-match "\\S-" (nth 1 entry)))
  10726. (nth 1 entry)
  10727. org-default-notes-file))
  10728. (headline (nth 2 entry))
  10729. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  10730. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  10731. (v-u (concat "[" (substring v-t 1 -1) "]"))
  10732. (v-U (concat "[" (substring v-T 1 -1) "]"))
  10733. (v-i initial) ; defined in `remember-mode'
  10734. (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
  10735. (v-n user-full-name)
  10736. (org-startup-folded nil)
  10737. org-time-was-given org-end-time-was-given x prompt char time)
  10738. (setq org-store-link-plist
  10739. (append (list :annotation v-a :initial v-i)))
  10740. (unless tpl (setq tpl "") (message "No template") (ding))
  10741. (erase-buffer)
  10742. (insert (substitute-command-keys
  10743. (format
  10744. "## `C-c C-c' to file interactively, `C-u C-c C-c' to file directly.
  10745. ## Target file \"%s\", headline \"%s\"
  10746. ## To switch templates, use `\\[org-remember]'.\n\n"
  10747. (abbreviate-file-name (or file org-default-notes-file))
  10748. (or headline ""))))
  10749. (insert tpl) (goto-char (point-min))
  10750. ;; Simple %-escapes
  10751. (while (re-search-forward "%\\([tTuUai]\\)" nil t)
  10752. (when (and initial (equal (match-string 0) "%i"))
  10753. (save-match-data
  10754. (let* ((lead (buffer-substring
  10755. (point-at-bol) (match-beginning 0))))
  10756. (setq v-i (mapconcat 'identity
  10757. (org-split-string initial "\n")
  10758. (concat "\n" lead))))))
  10759. (replace-match
  10760. (or (eval (intern (concat "v-" (match-string 1)))) "")
  10761. t t))
  10762. ;; From the property list
  10763. (when plist-p
  10764. (goto-char (point-min))
  10765. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  10766. (and (setq x (plist-get org-store-link-plist
  10767. (intern (match-string 1))))
  10768. (replace-match x t t))))
  10769. ;; Turn on org-mode in the remember buffer, set local variables
  10770. (org-mode)
  10771. (org-set-local 'org-finish-function 'remember-buffer)
  10772. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  10773. (org-set-local 'org-default-notes-file file))
  10774. (if (and headline (stringp headline) (string-match "\\S-" headline))
  10775. (org-set-local 'org-remember-default-headline headline))
  10776. ;; Interactive template entries
  10777. (goto-char (point-min))
  10778. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  10779. (setq char (if (match-end 3) (match-string 3))
  10780. prompt (if (match-end 2) (match-string 2)))
  10781. (goto-char (match-beginning 0))
  10782. (replace-match "")
  10783. (cond
  10784. ((member char '("G" "g"))
  10785. (let* ((org-last-tags-completion-table
  10786. (org-global-tags-completion-table
  10787. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  10788. (ins (completing-read
  10789. (if prompt (concat prompt ": ") "Tags: ")
  10790. 'org-tags-completion-function nil nil nil
  10791. 'org-tags-history)))
  10792. (insert (concat ":" (mapconcat 'identity
  10793. (org-split-string ins (org-re "[^[:alnum:]]+"))
  10794. ":")
  10795. ":"))))
  10796. (char
  10797. (setq org-time-was-given (equal (upcase char) char))
  10798. (setq time (org-read-date (equal (upcase char) "U") t nil
  10799. prompt))
  10800. (org-insert-time-stamp time org-time-was-given
  10801. (member char '("u" "U"))
  10802. nil nil (list org-end-time-was-given)))
  10803. (t
  10804. (insert (read-string
  10805. (if prompt (concat prompt ": ") "Enter string"))))))
  10806. (goto-char (point-min))
  10807. (if (re-search-forward "%\\?" nil t)
  10808. (replace-match "")
  10809. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  10810. (org-mode)
  10811. (org-set-local 'org-finish-function 'remember-buffer)))
  10812. ;;;###autoload
  10813. (defun org-remember ()
  10814. "Call `remember'. If this is already a remember buffer, re-apply template.
  10815. If there is an active region, make sure remember uses it as initial content
  10816. of the remember buffer."
  10817. (interactive)
  10818. (if (eq org-finish-function 'remember-buffer)
  10819. (progn
  10820. (when (< (length org-remember-templates) 2)
  10821. (error "No other template available"))
  10822. (erase-buffer)
  10823. (let ((annotation (plist-get org-store-link-plist :annotation))
  10824. (initial (plist-get org-store-link-plist :initial)))
  10825. (org-remember-apply-template))
  10826. (message "Press C-c C-c to remember data"))
  10827. (if (org-region-active-p)
  10828. (remember (buffer-substring (point) (mark)))
  10829. (call-interactively 'remember))))
  10830. ;;;###autoload
  10831. (defun org-remember-handler ()
  10832. "Store stuff from remember.el into an org file.
  10833. First prompts for an org file. If the user just presses return, the value
  10834. of `org-default-notes-file' is used.
  10835. Then the command offers the headings tree of the selected file in order to
  10836. file the text at a specific location.
  10837. You can either immediately press RET to get the note appended to the
  10838. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  10839. find a better place. Then press RET or <left> or <right> in insert the note.
  10840. Key Cursor position Note gets inserted
  10841. -----------------------------------------------------------------------------
  10842. RET buffer-start as level 2 heading at end of file
  10843. RET on headline as sublevel of the heading at cursor
  10844. RET no heading at cursor position, level taken from context.
  10845. Or use prefix arg to specify level manually.
  10846. <left> on headline as same level, before current heading
  10847. <right> on headline as same level, after current heading
  10848. So the fastest way to store the note is to press RET RET to append it to
  10849. the default file. This way your current train of thought is not
  10850. interrupted, in accordance with the principles of remember.el. But with
  10851. little extra effort, you can push it directly to the correct location.
  10852. Before being stored away, the function ensures that the text has a
  10853. headline, i.e. a first line that starts with a \"*\". If not, a headline
  10854. is constructed from the current date and some additional data.
  10855. If the variable `org-adapt-indentation' is non-nil, the entire text is
  10856. also indented so that it starts in the same column as the headline
  10857. \(i.e. after the stars).
  10858. See also the variable `org-reverse-note-order'."
  10859. (goto-char (point-min))
  10860. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  10861. (replace-match ""))
  10862. (catch 'quit
  10863. (let* ((txt (buffer-substring (point-min) (point-max)))
  10864. (fastp (equal current-prefix-arg '(4)))
  10865. (file (if fastp org-default-notes-file (org-get-org-file)))
  10866. (heading org-remember-default-headline)
  10867. (visiting (org-find-base-buffer-visiting file))
  10868. (org-startup-folded nil)
  10869. (org-startup-align-all-tables nil)
  10870. (org-goto-start-pos 1)
  10871. spos level indent reversed)
  10872. (setq current-prefix-arg nil)
  10873. ;; Modify text so that it becomes a nice subtree which can be inserted
  10874. ;; into an org tree.
  10875. (let* ((lines (split-string txt "\n"))
  10876. first)
  10877. (setq first (car lines) lines (cdr lines))
  10878. (if (string-match "^\\*+" first)
  10879. ;; Is already a headline
  10880. (setq indent nil)
  10881. ;; We need to add a headline: Use time and first buffer line
  10882. (setq lines (cons first lines)
  10883. first (concat "* " (current-time-string)
  10884. " (" (remember-buffer-desc) ")")
  10885. indent " "))
  10886. (if (and org-adapt-indentation indent)
  10887. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  10888. (setq txt (concat first "\n"
  10889. (mapconcat 'identity lines "\n"))))
  10890. ;; Find the file
  10891. (if (not visiting) (find-file-noselect file))
  10892. (with-current-buffer (or visiting (get-file-buffer file))
  10893. (save-excursion
  10894. (save-restriction
  10895. (widen)
  10896. (and (goto-char (point-min))
  10897. (not (re-search-forward "^\\* " nil t))
  10898. (insert "\n* Notes\n"))
  10899. (setq reversed (org-notes-order-reversed-p))
  10900. ;; Find the default location
  10901. (when (and heading (stringp heading) (string-match "\\S-" heading))
  10902. (goto-char (point-min))
  10903. (if (re-search-forward
  10904. (concat "^\\*+[ \t]+" (regexp-quote heading)
  10905. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  10906. nil t)
  10907. (setq org-goto-start-pos (match-beginning 0))))
  10908. ;; Ask the User for a location
  10909. (setq spos (if fastp
  10910. org-goto-start-pos
  10911. (org-get-location (current-buffer) org-remember-help)))
  10912. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  10913. ; not handle this note
  10914. (goto-char spos)
  10915. (cond ((and (bobp) (not reversed))
  10916. ;; Put it at the end, one level below level 1
  10917. (save-restriction
  10918. (widen)
  10919. (goto-char (point-max))
  10920. (if (not (bolp)) (newline))
  10921. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  10922. ((and (bobp) reversed)
  10923. ;; Put it at the start, as level 1
  10924. (save-restriction
  10925. (widen)
  10926. (goto-char (point-min))
  10927. (re-search-forward "^\\*" nil t)
  10928. (beginning-of-line 1)
  10929. (org-paste-subtree 1 txt)))
  10930. ((and (org-on-heading-p t) (not current-prefix-arg))
  10931. ;; Put it below this entry, at the beg/end of the subtree
  10932. (org-back-to-heading t)
  10933. (setq level (funcall outline-level))
  10934. (if reversed
  10935. (outline-next-heading)
  10936. (org-end-of-subtree t))
  10937. (if (not (bolp)) (newline))
  10938. (beginning-of-line 1)
  10939. (org-paste-subtree (org-get-legal-level level 1) txt))
  10940. (t
  10941. ;; Put it right there, with automatic level determined by
  10942. ;; org-paste-subtree or from prefix arg
  10943. (org-paste-subtree
  10944. (if (numberp current-prefix-arg) current-prefix-arg)
  10945. txt)))
  10946. (when remember-save-after-remembering
  10947. (save-buffer)
  10948. (if (not visiting) (kill-buffer (current-buffer)))))))))
  10949. t) ;; return t to indicate that we took care of this note.
  10950. (defun org-get-org-file ()
  10951. "Read a filename, with default directory `org-directory'."
  10952. (let ((default (or org-default-notes-file remember-data-file)))
  10953. (read-file-name (format "File name [%s]: " default)
  10954. (file-name-as-directory org-directory)
  10955. default)))
  10956. (defun org-notes-order-reversed-p ()
  10957. "Check if the current file should receive notes in reversed order."
  10958. (cond
  10959. ((not org-reverse-note-order) nil)
  10960. ((eq t org-reverse-note-order) t)
  10961. ((not (listp org-reverse-note-order)) nil)
  10962. (t (catch 'exit
  10963. (let ((all org-reverse-note-order)
  10964. entry)
  10965. (while (setq entry (pop all))
  10966. (if (string-match (car entry) buffer-file-name)
  10967. (throw 'exit (cdr entry))))
  10968. nil)))))
  10969. ;;;; Dynamic blocks
  10970. (defun org-find-dblock (name)
  10971. "Find the first dynamic block with name NAME in the buffer.
  10972. If not found, stay at current position and return nil."
  10973. (let (pos)
  10974. (save-excursion
  10975. (goto-char (point-min))
  10976. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  10977. nil t)
  10978. (match-beginning 0))))
  10979. (if pos (goto-char pos))
  10980. pos))
  10981. (defconst org-dblock-start-re
  10982. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10983. "Matches the startline of a dynamic block, with parameters.")
  10984. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  10985. "Matches the end of a dyhamic block.")
  10986. (defun org-create-dblock (plist)
  10987. "Create a dynamic block section, with parameters taken from PLIST.
  10988. PLIST must containe a :name entry which is used as name of the block."
  10989. (unless (bolp) (newline))
  10990. (let ((name (plist-get plist :name)))
  10991. (insert "#+BEGIN: " name)
  10992. (while plist
  10993. (if (eq (car plist) :name)
  10994. (setq plist (cddr plist))
  10995. (insert " " (prin1-to-string (pop plist)))))
  10996. (insert "\n\n#+END:\n")
  10997. (beginning-of-line -2)))
  10998. (defun org-prepare-dblock ()
  10999. "Prepare dynamic block for refresh.
  11000. This empties the block, puts the cursor at the insert position and returns
  11001. the property list including an extra property :name with the block name."
  11002. (unless (looking-at org-dblock-start-re)
  11003. (error "Not at a dynamic block"))
  11004. (let* ((begdel (1+ (match-end 0)))
  11005. (name (match-string 1))
  11006. (params (append (list :name name)
  11007. (read (concat "(" (match-string 3) ")")))))
  11008. (unless (re-search-forward org-dblock-end-re nil t)
  11009. (error "Dynamic block not terminated"))
  11010. (delete-region begdel (match-beginning 0))
  11011. (goto-char begdel)
  11012. (open-line 1)
  11013. params))
  11014. (defun org-map-dblocks (&optional command)
  11015. "Apply COMMAND to all dynamic blocks in the current buffer.
  11016. If COMMAND is not given, use `org-update-dblock'."
  11017. (let ((cmd (or command 'org-update-dblock))
  11018. pos)
  11019. (save-excursion
  11020. (goto-char (point-min))
  11021. (while (re-search-forward org-dblock-start-re nil t)
  11022. (goto-char (setq pos (match-beginning 0)))
  11023. (condition-case nil
  11024. (funcall cmd)
  11025. (error (message "Error during update of dynamic block")))
  11026. (goto-char pos)
  11027. (unless (re-search-forward org-dblock-end-re nil t)
  11028. (error "Dynamic block not terminated"))))))
  11029. (defun org-dblock-update (&optional arg)
  11030. "User command for updating dynamic blocks.
  11031. Update the dynamic block at point. With prefix ARG, update all dynamic
  11032. blocks in the buffer."
  11033. (interactive "P")
  11034. (if arg
  11035. (org-update-all-dblocks)
  11036. (or (looking-at org-dblock-start-re)
  11037. (org-beginning-of-dblock))
  11038. (org-update-dblock)))
  11039. (defun org-update-dblock ()
  11040. "Update the dynamic block at point
  11041. This means to empty the block, parse for parameters and then call
  11042. the correct writing function."
  11043. (let* ((pos (point))
  11044. (params (org-prepare-dblock))
  11045. (name (plist-get params :name))
  11046. (cmd (intern (concat "org-dblock-write:" name))))
  11047. (funcall cmd params)
  11048. (goto-char pos)))
  11049. (defun org-beginning-of-dblock ()
  11050. "Find the beginning of the dynamic block at point.
  11051. Error if there is no scuh block at point."
  11052. (let ((pos (point))
  11053. beg)
  11054. (end-of-line 1)
  11055. (if (and (re-search-backward org-dblock-start-re nil t)
  11056. (setq beg (match-beginning 0))
  11057. (re-search-forward org-dblock-end-re nil t)
  11058. (> (match-end 0) pos))
  11059. (goto-char beg)
  11060. (goto-char pos)
  11061. (error "Not in a dynamic block"))))
  11062. (defun org-update-all-dblocks ()
  11063. "Update all dynamic blocks in the buffer.
  11064. This function can be used in a hook."
  11065. (when (org-mode-p)
  11066. (org-map-dblocks 'org-update-dblock)))
  11067. ;;;; Completion
  11068. (defun org-complete (&optional arg)
  11069. "Perform completion on word at point.
  11070. At the beginning of a headline, this completes TODO keywords as given in
  11071. `org-todo-keywords'.
  11072. If the current word is preceded by a backslash, completes the TeX symbols
  11073. that are supported for HTML support.
  11074. If the current word is preceded by \"#+\", completes special words for
  11075. setting file options.
  11076. In the line after \"#+STARTUP:, complete valid keywords.\"
  11077. At all other locations, this simply calls `ispell-complete-word'."
  11078. (interactive "P")
  11079. (catch 'exit
  11080. (let* ((end (point))
  11081. (beg1 (save-excursion
  11082. (skip-chars-backward (org-re "[:alnum:]_@"))
  11083. (point)))
  11084. (beg (save-excursion
  11085. (skip-chars-backward "a-zA-Z0-9_:$")
  11086. (point)))
  11087. (confirm (lambda (x) (stringp (car x))))
  11088. (searchhead (equal (char-before beg) ?*))
  11089. (tag (and (equal (char-before beg1) ?:)
  11090. (equal (char-after (point-at-bol)) ?*)))
  11091. (prop (and (equal (char-before beg1) ?:)
  11092. (not (equal (char-after (point-at-bol)) ?*))))
  11093. (texp (equal (char-before beg) ?\\))
  11094. (link (equal (char-before beg) ?\[))
  11095. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  11096. beg)
  11097. "#+"))
  11098. (startup (string-match "^#\\+STARTUP:.*"
  11099. (buffer-substring (point-at-bol) (point))))
  11100. (completion-ignore-case opt)
  11101. (type nil)
  11102. (tbl nil)
  11103. (table (cond
  11104. (opt
  11105. (setq type :opt)
  11106. (mapcar (lambda (x)
  11107. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  11108. (cons (match-string 2 x) (match-string 1 x)))
  11109. (org-split-string (org-get-current-options) "\n")))
  11110. (startup
  11111. (setq type :startup)
  11112. org-startup-options)
  11113. (link (append org-link-abbrev-alist-local
  11114. org-link-abbrev-alist))
  11115. (texp
  11116. (setq type :tex)
  11117. org-html-entities)
  11118. ((string-match "\\`\\*+[ \t]*\\'"
  11119. (buffer-substring (point-at-bol) beg))
  11120. (setq type :todo)
  11121. (mapcar 'list org-todo-keywords-1))
  11122. (searchhead
  11123. (setq type :searchhead)
  11124. (save-excursion
  11125. (goto-char (point-min))
  11126. (while (re-search-forward org-todo-line-regexp nil t)
  11127. (push (list
  11128. (org-make-org-heading-search-string
  11129. (match-string 3) t))
  11130. tbl)))
  11131. tbl)
  11132. (tag (setq type :tag beg beg1)
  11133. (or org-tag-alist (org-get-buffer-tags)))
  11134. (prop (setq type :prop beg beg1)
  11135. (mapcar 'list (org-buffer-property-keys)))
  11136. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  11137. (pattern (buffer-substring-no-properties beg end))
  11138. (completion (try-completion pattern table confirm)))
  11139. (cond ((eq completion t)
  11140. (if (equal type :opt)
  11141. (insert (substring (cdr (assoc (upcase pattern) table))
  11142. (length pattern)))
  11143. (if (memq type '(:tag :prop)) (insert ":"))))
  11144. ((null completion)
  11145. (message "Can't find completion for \"%s\"" pattern)
  11146. (ding))
  11147. ((not (string= pattern completion))
  11148. (delete-region beg end)
  11149. (if (string-match " +$" completion)
  11150. (setq completion (replace-match "" t t completion)))
  11151. (insert completion)
  11152. (if (get-buffer-window "*Completions*")
  11153. (delete-window (get-buffer-window "*Completions*")))
  11154. (if (assoc completion table)
  11155. (if (eq type :todo) (insert " ")
  11156. (if (memq type '(:tag :prop)) (insert ":"))))
  11157. (if (and (equal type :opt) (assoc completion table))
  11158. (message "%s" (substitute-command-keys
  11159. "Press \\[org-complete] again to insert example settings"))))
  11160. (t
  11161. (message "Making completion list...")
  11162. (let ((list (sort (all-completions pattern table confirm)
  11163. 'string<)))
  11164. (with-output-to-temp-buffer "*Completions*"
  11165. (condition-case nil
  11166. ;; Protection needed for XEmacs and emacs 21
  11167. (display-completion-list list pattern)
  11168. (error (display-completion-list list)))))
  11169. (message "Making completion list...%s" "done"))))))
  11170. ;;;; TODO, DEADLINE, Comments
  11171. (defun org-toggle-comment ()
  11172. "Change the COMMENT state of an entry."
  11173. (interactive)
  11174. (save-excursion
  11175. (org-back-to-heading)
  11176. (if (looking-at (concat outline-regexp
  11177. "\\( +\\<" org-comment-string "\\>\\)"))
  11178. (replace-match "" t t nil 1)
  11179. (if (looking-at outline-regexp)
  11180. (progn
  11181. (goto-char (match-end 0))
  11182. (insert " " org-comment-string))))))
  11183. (defvar org-last-todo-state-is-todo nil
  11184. "This is non-nil when the last TODO state change led to a TODO state.
  11185. If the last change removed the TODO tag or switched to DONE, then
  11186. this is nil.")
  11187. (defun org-todo (&optional arg)
  11188. "Change the TODO state of an item.
  11189. The state of an item is given by a keyword at the start of the heading,
  11190. like
  11191. *** TODO Write paper
  11192. *** DONE Call mom
  11193. The different keywords are specified in the variable `org-todo-keywords'.
  11194. By default the available states are \"TODO\" and \"DONE\".
  11195. So for this example: when the item starts with TODO, it is changed to DONE.
  11196. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11197. DONE are present, add TODO at the beginning of the heading.
  11198. With C-u prefix arg, use completion to determine the new state.
  11199. With numeric prefix arg, switch to that state.
  11200. For calling through lisp, arg is also interpreted in the following way:
  11201. 'none -> empty state
  11202. \"\"(empty string) -> switch to empty state
  11203. 'done -> switch to DONE
  11204. 'nextset -> switch to the next set of keywords
  11205. 'previousset -> switch to the previous set of keywords
  11206. \"WAITING\" -> switch to the specified keyword, but only if it
  11207. really is a member of `org-todo-keywords'."
  11208. (interactive "P")
  11209. (save-excursion
  11210. (org-back-to-heading)
  11211. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  11212. (or (looking-at (concat " +" org-todo-regexp " *"))
  11213. (looking-at " *"))
  11214. (let* ((this (match-string 1))
  11215. (head (org-get-todo-sequence-head this))
  11216. (ass (assoc head org-todo-kwd-alist))
  11217. (interpret (nth 1 ass))
  11218. (done-word (nth 3 ass))
  11219. (final-done-word (nth 4 ass))
  11220. (last-state (or this ""))
  11221. (completion-ignore-case t)
  11222. (member (member this org-todo-keywords-1))
  11223. (tail (cdr member))
  11224. (state (cond
  11225. ((equal arg '(4))
  11226. ;; Read a state with completion
  11227. (completing-read "State: " (mapcar (lambda(x) (list x))
  11228. org-todo-keywords-1)
  11229. nil t))
  11230. ((eq arg 'right)
  11231. (if this
  11232. (if tail (car tail) nil)
  11233. (car org-todo-keywords-1)))
  11234. ((eq arg 'left)
  11235. (if (equal member org-todo-keywords-1)
  11236. nil
  11237. (if this
  11238. (nth (- (length org-todo-keywords-1) (length tail) 2)
  11239. org-todo-keywords-1)
  11240. (org-last org-todo-keywords-1))))
  11241. (arg
  11242. ;; user or caller requests a specific state
  11243. (cond
  11244. ((equal arg "") nil)
  11245. ((eq arg 'none) nil)
  11246. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11247. ((eq arg 'nextset)
  11248. (or (car (cdr (member head org-todo-heads)))
  11249. (car org-todo-heads)))
  11250. ((eq arg 'previousset)
  11251. (let ((org-todo-heads (reverse org-todo-heads)))
  11252. (or (car (cdr (member head org-todo-heads)))
  11253. (car org-todo-heads))))
  11254. ((car (member arg org-todo-keywords-1)))
  11255. ((nth (1- (prefix-numeric-value arg))
  11256. org-todo-keywords-1))))
  11257. ((null member) (or head (car org-todo-keywords-1)))
  11258. ((equal this final-done-word) nil) ;; -> make empty
  11259. ((null tail) nil) ;; -> first entry
  11260. ((eq interpret 'sequence)
  11261. (car tail))
  11262. ((memq interpret '(type priority))
  11263. (if (eq this-command last-command)
  11264. (car tail)
  11265. (if (> (length tail) 0)
  11266. (or done-word (car org-done-keywords))
  11267. nil)))
  11268. (t nil)))
  11269. (next (if state (concat " " state " ") " "))
  11270. dostates)
  11271. (replace-match next t t)
  11272. (unless head
  11273. (setq head (org-get-todo-sequence-head state)
  11274. ass (assoc head org-todo-kwd-alist)
  11275. interpret (nth 1 ass)
  11276. done-word (nth 3 ass)
  11277. final-done-word (nth 4 ass)))
  11278. (when (memq arg '(nextset previousset))
  11279. (message "Keyword set: %s"
  11280. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  11281. (setq org-last-todo-state-is-todo
  11282. (not (member state org-done-keywords)))
  11283. (when (and org-log-done (not (memq arg '(nextset previousset))))
  11284. (setq dostates (and (eq interpret 'sequence)
  11285. (listp org-log-done) (memq 'state org-log-done)))
  11286. (cond
  11287. ((and state (not this))
  11288. (org-add-planning-info nil nil 'closed)
  11289. (and dostates (org-add-log-maybe 'state state 'findpos)))
  11290. ((and state dostates)
  11291. (org-add-log-maybe 'state state 'findpos))
  11292. ((member state org-done-keywords)
  11293. ;; Planning info calls the note-setting command.
  11294. (org-add-planning-info 'closed (org-current-time)
  11295. (if (org-get-repeat) nil 'scheduled))
  11296. (org-add-log-maybe 'done state 'findpos))))
  11297. ;; Fixup tag positioning
  11298. (and org-auto-align-tags (org-set-tags nil t))
  11299. (run-hooks 'org-after-todo-state-change-hook)
  11300. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  11301. (if (and arg (not (member state org-done-keywords)))
  11302. (setq head (org-get-todo-sequence-head state)))
  11303. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
  11304. ;; Fixup cursor location if close to the keyword
  11305. (if (and (outline-on-heading-p)
  11306. (not (bolp))
  11307. (save-excursion (beginning-of-line 1)
  11308. (looking-at org-todo-line-regexp))
  11309. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11310. (progn
  11311. (goto-char (or (match-end 2) (match-end 1)))
  11312. (just-one-space))))
  11313. (defun org-get-todo-sequence-head (kwd)
  11314. "Return the head of the TODO sequence to which KWD belongs.
  11315. If KWD is not set, check if there is a text property remembering the
  11316. right sequence."
  11317. (let (p)
  11318. (cond
  11319. ((not kwd)
  11320. (or (get-text-property (point-at-bol) 'org-todo-head)
  11321. (progn
  11322. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11323. nil (point-at-eol)))
  11324. (get-text-property p 'org-todo-head))))
  11325. ((not (member kwd org-todo-keywords-1))
  11326. (car org-todo-keywords-1))
  11327. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11328. (defun org-get-repeat ()
  11329. "Check if tere is a deadline/schedule with repeater in this entry."
  11330. (save-match-data
  11331. (save-excursion
  11332. (org-back-to-heading t)
  11333. (if (re-search-forward
  11334. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  11335. (match-string 1)))))
  11336. (defvar org-last-changed-timestamp)
  11337. (defvar org-log-post-message)
  11338. (defun org-auto-repeat-maybe ()
  11339. "Check if the current headline contains a repeated deadline/schedule.
  11340. If yes, set TODO state back to what it was and change the base date
  11341. of repeating deadline/scheduled time stamps to new date.
  11342. This function should be run in the `org-after-todo-state-change-hook'."
  11343. ;; last-state is dynamically scoped into this function
  11344. (let* ((repeat (org-get-repeat))
  11345. (aa (assoc last-state org-todo-kwd-alist))
  11346. (interpret (nth 1 aa))
  11347. (head (nth 2 aa))
  11348. (done-word (nth 3 aa))
  11349. (whata '(("d" . day) ("m" . month) ("y" . year)))
  11350. (msg "Entry repeats: ")
  11351. (org-log-done)
  11352. re type n what ts)
  11353. (when repeat
  11354. (org-todo (if (eq interpret 'type) last-state head))
  11355. (when (and org-log-repeat
  11356. (not (memq 'org-add-log-note
  11357. (default-value 'post-command-hook))))
  11358. ;; Make sure a note is taken
  11359. (let ((org-log-done '(done)))
  11360. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  11361. 'findpos)))
  11362. (org-back-to-heading t)
  11363. (org-add-planning-info nil nil 'closed)
  11364. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11365. org-deadline-time-regexp "\\)"))
  11366. (while (re-search-forward
  11367. re (save-excursion (outline-next-heading) (point)) t)
  11368. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  11369. ts (match-string (if (match-end 2) 2 4)))
  11370. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  11371. (setq n (string-to-number (match-string 1 ts))
  11372. what (match-string 2 ts))
  11373. (if (equal what "w") (setq n (* n 7) what "d"))
  11374. (org-timestamp-change n (cdr (assoc what whata))))
  11375. (setq msg (concat msg type org-last-changed-timestamp " ")))
  11376. (setq org-log-post-message msg)
  11377. (message msg))))
  11378. (defun org-show-todo-tree (arg)
  11379. "Make a compact tree which shows all headlines marked with TODO.
  11380. The tree will show the lines where the regexp matches, and all higher
  11381. headlines above the match.
  11382. With \\[universal-argument] prefix, also show the DONE entries.
  11383. With a numeric prefix N, construct a sparse tree for the Nth element
  11384. of `org-todo-keywords-1'."
  11385. (interactive "P")
  11386. (let ((case-fold-search nil)
  11387. (kwd-re
  11388. (cond ((null arg) org-not-done-regexp)
  11389. ((equal arg '(4))
  11390. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  11391. (mapcar 'list org-todo-keywords-1))))
  11392. (concat "\\("
  11393. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11394. "\\)\\>")))
  11395. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11396. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11397. org-todo-keywords-1)))
  11398. (t (error "Invalid prefix argument: %s" arg)))))
  11399. (message "%d TODO entries found"
  11400. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  11401. (defun org-deadline ()
  11402. "Insert the DEADLINE: string to make a deadline.
  11403. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11404. to modify it to the correct date."
  11405. (interactive)
  11406. (org-add-planning-info 'deadline nil 'closed))
  11407. (defun org-schedule ()
  11408. "Insert the SCHEDULED: string to schedule a TODO item.
  11409. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11410. to modify it to the correct date."
  11411. (interactive)
  11412. (org-add-planning-info 'scheduled nil 'closed))
  11413. (defun org-add-planning-info (what &optional time &rest remove)
  11414. "Insert new timestamp with keyword in the line directly after the headline.
  11415. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  11416. If non is given, the user is prompted for a date.
  11417. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11418. be removed."
  11419. (interactive)
  11420. (let (org-time-was-given org-end-time-was-given)
  11421. (when what (setq time (or time (org-read-date nil 'to-time))))
  11422. (when (and org-insert-labeled-timestamps-at-point
  11423. (member what '(scheduled deadline)))
  11424. (insert
  11425. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11426. (org-insert-time-stamp time org-time-was-given
  11427. nil nil nil (list org-end-time-was-given))
  11428. (setq what nil))
  11429. (save-excursion
  11430. (save-restriction
  11431. (let (col list elt ts buffer-invisibility-spec)
  11432. (org-back-to-heading t)
  11433. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  11434. (goto-char (match-end 1))
  11435. (setq col (current-column))
  11436. (goto-char (match-end 0))
  11437. (if (eobp) (insert "\n"))
  11438. (forward-char 1)
  11439. (when (looking-at "[ \t]*:PROPERTIES:[ \t]*$")
  11440. (goto-char (match-end 0))
  11441. (if (eobp) (insert "\n"))
  11442. (forward-char 1))
  11443. (if (and (not (looking-at outline-regexp))
  11444. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11445. "[^\r\n]*"))
  11446. (not (equal (match-string 1) org-clock-string)))
  11447. (narrow-to-region (match-beginning 0) (match-end 0))
  11448. (insert "\n")
  11449. (backward-char 1)
  11450. (narrow-to-region (point) (point))
  11451. (indent-to-column col))
  11452. ;; Check if we have to remove something.
  11453. (setq list (cons what remove))
  11454. (while list
  11455. (setq elt (pop list))
  11456. (goto-char (point-min))
  11457. (when (or (and (eq elt 'scheduled)
  11458. (re-search-forward org-scheduled-time-regexp nil t))
  11459. (and (eq elt 'deadline)
  11460. (re-search-forward org-deadline-time-regexp nil t))
  11461. (and (eq elt 'closed)
  11462. (re-search-forward org-closed-time-regexp nil t)))
  11463. (replace-match "")
  11464. (if (looking-at "--+<[^>]+>") (replace-match ""))
  11465. (if (looking-at " +") (replace-match ""))))
  11466. (goto-char (point-max))
  11467. (when what
  11468. (insert
  11469. (if (not (equal (char-before) ?\ )) " " "")
  11470. (cond ((eq what 'scheduled) org-scheduled-string)
  11471. ((eq what 'deadline) org-deadline-string)
  11472. ((eq what 'closed) org-closed-string)
  11473. ((eq what 'archived) org-archived-string))
  11474. " ")
  11475. (org-insert-time-stamp
  11476. time
  11477. (or org-time-was-given
  11478. (and (eq what 'closed) org-log-done-with-time))
  11479. (eq what 'closed)
  11480. nil nil (list org-end-time-was-given))
  11481. (end-of-line 1))
  11482. (goto-char (point-min))
  11483. (widen)
  11484. (if (looking-at "[ \t]+\r?\n")
  11485. (replace-match ""))
  11486. ts)))))
  11487. (defvar org-log-note-marker (make-marker))
  11488. (defvar org-log-note-purpose nil)
  11489. (defvar org-log-note-state nil)
  11490. (defvar org-log-note-window-configuration nil)
  11491. (defvar org-log-note-return-to (make-marker))
  11492. (defvar org-log-post-message nil
  11493. "Message to be displayed after a log note has been stored.
  11494. The auto-repeater uses this.")
  11495. (defun org-add-log-maybe (&optional purpose state findpos)
  11496. "Set up the post command hook to take a note."
  11497. (save-excursion
  11498. (when (and (listp org-log-done)
  11499. (memq purpose org-log-done))
  11500. (when findpos
  11501. (org-back-to-heading t)
  11502. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  11503. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11504. "[^\r\n]*\\)?"))
  11505. (goto-char (match-end 0)))
  11506. (move-marker org-log-note-marker (point))
  11507. (setq org-log-note-purpose purpose)
  11508. (setq org-log-note-state state)
  11509. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  11510. (defun org-add-log-note (&optional purpose)
  11511. "Pop up a window for taking a note, and add this note later at point."
  11512. (remove-hook 'post-command-hook 'org-add-log-note)
  11513. (setq org-log-note-window-configuration (current-window-configuration))
  11514. (delete-other-windows)
  11515. (move-marker org-log-note-return-to (point))
  11516. (switch-to-buffer (marker-buffer org-log-note-marker))
  11517. (goto-char org-log-note-marker)
  11518. (switch-to-buffer-other-window "*Org Note*")
  11519. (erase-buffer)
  11520. (let ((org-inhibit-startup t)) (org-mode))
  11521. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  11522. (cond
  11523. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11524. ((eq org-log-note-purpose 'done) "closed todo item")
  11525. ((eq org-log-note-purpose 'state) "state change")
  11526. (t (error "This should not happen")))))
  11527. (org-set-local 'org-finish-function 'org-store-log-note))
  11528. (defun org-store-log-note ()
  11529. "Finish taking a log note, and insert it to where it belongs."
  11530. (let ((txt (buffer-string))
  11531. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11532. lines ind)
  11533. (kill-buffer (current-buffer))
  11534. (if (string-match "^#.*\n[ \t\n]*" txt)
  11535. (setq txt (replace-match "" t t txt)))
  11536. (if (string-match "\\s-+\\'" txt)
  11537. (setq txt (replace-match "" t t txt)))
  11538. (setq lines (org-split-string txt "\n"))
  11539. (when (and note (string-match "\\S-" note))
  11540. (setq note
  11541. (org-replace-escapes
  11542. note
  11543. (list (cons "%u" (user-login-name))
  11544. (cons "%U" user-full-name)
  11545. (cons "%t" (format-time-string
  11546. (org-time-stamp-format 'long 'inactive)
  11547. (current-time)))
  11548. (cons "%s" (if org-log-note-state
  11549. (concat "\"" org-log-note-state "\"")
  11550. "")))))
  11551. (if lines (setq note (concat note " \\\\")))
  11552. (push note lines))
  11553. (when lines
  11554. (save-excursion
  11555. (set-buffer (marker-buffer org-log-note-marker))
  11556. (save-excursion
  11557. (goto-char org-log-note-marker)
  11558. (move-marker org-log-note-marker nil)
  11559. (end-of-line 1)
  11560. (if (not (bolp)) (insert "\n")) (indent-relative nil)
  11561. (setq ind (concat (buffer-substring (point-at-bol) (point)) " "))
  11562. (insert " - " (pop lines))
  11563. (while lines
  11564. (insert "\n" ind (pop lines)))))))
  11565. (set-window-configuration org-log-note-window-configuration)
  11566. (with-current-buffer (marker-buffer org-log-note-return-to)
  11567. (goto-char org-log-note-return-to))
  11568. (move-marker org-log-note-return-to nil)
  11569. (and org-log-post-message (message org-log-post-message)))
  11570. (defvar org-occur-highlights nil)
  11571. (make-variable-buffer-local 'org-occur-highlights)
  11572. (defun org-occur (regexp &optional keep-previous callback)
  11573. "Make a compact tree which shows all matches of REGEXP.
  11574. The tree will show the lines where the regexp matches, and all higher
  11575. headlines above the match. It will also show the heading after the match,
  11576. to make sure editing the matching entry is easy.
  11577. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11578. call to `org-occur' will be kept, to allow stacking of calls to this
  11579. command.
  11580. If CALLBACK is non-nil, it is a function which is called to confirm
  11581. that the match should indeed be shown."
  11582. (interactive "sRegexp: \nP")
  11583. (or keep-previous (org-remove-occur-highlights nil nil t))
  11584. (let ((cnt 0))
  11585. (save-excursion
  11586. (goto-char (point-min))
  11587. (if (or (not keep-previous) ; do not want to keep
  11588. (not org-occur-highlights)) ; no previous matches
  11589. ;; hide everything
  11590. (org-overview))
  11591. (while (re-search-forward regexp nil t)
  11592. (when (or (not callback)
  11593. (save-match-data (funcall callback)))
  11594. (setq cnt (1+ cnt))
  11595. (when org-highlight-sparse-tree-matches
  11596. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11597. (org-show-context 'occur-tree))))
  11598. (when org-remove-highlights-with-change
  11599. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11600. nil 'local))
  11601. (unless org-sparse-tree-open-archived-trees
  11602. (org-hide-archived-subtrees (point-min) (point-max)))
  11603. (run-hooks 'org-occur-hook)
  11604. (if (interactive-p)
  11605. (message "%d match(es) for regexp %s" cnt regexp))
  11606. cnt))
  11607. (defun org-show-context (&optional key)
  11608. "Make sure point and context and visible.
  11609. How much context is shown depends upon the variables
  11610. `org-show-hierarchy-above', `org-show-following-heading'. and
  11611. `org-show-siblings'."
  11612. (let ((heading-p (org-on-heading-p t))
  11613. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11614. (following-p (org-get-alist-option org-show-following-heading key))
  11615. (siblings-p (org-get-alist-option org-show-siblings key)))
  11616. (catch 'exit
  11617. ;; Show heading or entry text
  11618. (if heading-p
  11619. (org-flag-heading nil) ; only show the heading
  11620. (and (or (org-invisible-p) (org-invisible-p2))
  11621. (org-show-hidden-entry))) ; show entire entry
  11622. (when following-p
  11623. ;; Show next sibling, or heading below text
  11624. (save-excursion
  11625. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11626. (org-flag-heading nil))))
  11627. (when siblings-p (org-show-siblings))
  11628. (when hierarchy-p
  11629. ;; show all higher headings, possibly with siblings
  11630. (save-excursion
  11631. (while (and (condition-case nil
  11632. (progn (org-up-heading-all 1) t)
  11633. (error nil))
  11634. (not (bobp)))
  11635. (org-flag-heading nil)
  11636. (when siblings-p (org-show-siblings))))))))
  11637. (defun org-reveal (&optional siblings)
  11638. "Show current entry, hierarchy above it, and the following headline.
  11639. This can be used to show a consistent set of context around locations
  11640. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11641. not t for the search context.
  11642. With optional argument SIBLINGS, on each level of the hierarchy all
  11643. siblings are shown. This repairs the tree structure to what it would
  11644. look like when opened with hierarchical calls to `org-cycle'."
  11645. (interactive "P")
  11646. (let ((org-show-hierarchy-above t)
  11647. (org-show-following-heading t)
  11648. (org-show-siblings (if siblings t org-show-siblings)))
  11649. (org-show-context nil)))
  11650. (defun org-highlight-new-match (beg end)
  11651. "Highlight from BEG to END and mark the highlight is an occur headline."
  11652. (let ((ov (org-make-overlay beg end)))
  11653. (org-overlay-put ov 'face 'secondary-selection)
  11654. (push ov org-occur-highlights)))
  11655. (defun org-remove-occur-highlights (&optional beg end noremove)
  11656. "Remove the occur highlights from the buffer.
  11657. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11658. from the `before-change-functions' in the current buffer."
  11659. (interactive)
  11660. (unless org-inhibit-highlight-removal
  11661. (mapc 'org-delete-overlay org-occur-highlights)
  11662. (setq org-occur-highlights nil)
  11663. (unless noremove
  11664. (remove-hook 'before-change-functions
  11665. 'org-remove-occur-highlights 'local))))
  11666. ;;;; Priorities
  11667. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11668. "Regular expression matching the priority indicator.")
  11669. (defvar org-remove-priority-next-time nil)
  11670. (defun org-priority-up ()
  11671. "Increase the priority of the current item."
  11672. (interactive)
  11673. (org-priority 'up))
  11674. (defun org-priority-down ()
  11675. "Decrease the priority of the current item."
  11676. (interactive)
  11677. (org-priority 'down))
  11678. (defun org-priority (&optional action)
  11679. "Change the priority of an item by ARG.
  11680. ACTION can be `set', `up', `down', or a character."
  11681. (interactive)
  11682. (setq action (or action 'set))
  11683. (let (current new news have remove)
  11684. (save-excursion
  11685. (org-back-to-heading)
  11686. (if (looking-at org-priority-regexp)
  11687. (setq current (string-to-char (match-string 2))
  11688. have t)
  11689. (setq current org-default-priority))
  11690. (cond
  11691. ((or (eq action 'set) (integerp action))
  11692. (if (integerp action)
  11693. (setq new action)
  11694. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  11695. (setq new (read-char-exclusive)))
  11696. (cond ((equal new ?\ ) (setq remove t))
  11697. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11698. (error "Priority must be between `%c' and `%c'"
  11699. org-highest-priority org-lowest-priority))))
  11700. ((eq action 'up)
  11701. (setq new (1- current)))
  11702. ((eq action 'down)
  11703. (setq new (1+ current)))
  11704. (t (error "Invalid action")))
  11705. (setq new (min (max org-highest-priority (upcase new)) org-lowest-priority))
  11706. (setq news (format "%c" new))
  11707. (if have
  11708. (if remove
  11709. (replace-match "" t t nil 1)
  11710. (replace-match news t t nil 2))
  11711. (if remove
  11712. (error "No priority cookie found in line")
  11713. (looking-at org-todo-line-regexp)
  11714. (if (match-end 2)
  11715. (progn
  11716. (goto-char (match-end 2))
  11717. (insert " [#" news "]"))
  11718. (goto-char (match-beginning 3))
  11719. (insert "[#" news "] ")))))
  11720. (org-preserve-lc (org-set-tags nil 'align))
  11721. (if remove
  11722. (message "Priority removed")
  11723. (message "Priority of current item set to %s" news))))
  11724. (defun org-get-priority (s)
  11725. "Find priority cookie and return priority."
  11726. (save-match-data
  11727. (if (not (string-match org-priority-regexp s))
  11728. (* 1000 (- org-lowest-priority org-default-priority))
  11729. (* 1000 (- org-lowest-priority
  11730. (string-to-char (match-string 2 s)))))))
  11731. ;;;; Tags
  11732. (defun org-scan-tags (action matcher &optional todo-only)
  11733. "Scan headline tags with inheritance and produce output ACTION.
  11734. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  11735. evaluated, testing if a given set of tags qualifies a headline for
  11736. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  11737. are included in the output."
  11738. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  11739. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11740. (org-re
  11741. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  11742. (props (list 'face nil
  11743. 'done-face 'org-done
  11744. 'undone-face nil
  11745. 'mouse-face 'highlight
  11746. 'org-not-done-regexp org-not-done-regexp
  11747. 'org-todo-regexp org-todo-regexp
  11748. 'keymap org-agenda-keymap
  11749. 'help-echo
  11750. (format "mouse-2 or RET jump to org file %s"
  11751. (abbreviate-file-name buffer-file-name))))
  11752. (case-fold-search nil)
  11753. lspos
  11754. tags tags-list tags-alist (llast 0) rtn level category i txt
  11755. todo marker entry priority)
  11756. (save-excursion
  11757. (goto-char (point-min))
  11758. (when (eq action 'sparse-tree) (org-overview))
  11759. (while (re-search-forward re nil t)
  11760. (catch :skip
  11761. (setq todo (if (match-end 1) (match-string 2))
  11762. tags (if (match-end 4) (match-string 4)))
  11763. (goto-char (setq lspos (1+ (match-beginning 0))))
  11764. (setq level (org-reduced-level (funcall outline-level))
  11765. category (org-get-category))
  11766. (setq i llast llast level)
  11767. ;; remove tag lists from same and sublevels
  11768. (while (>= i level)
  11769. (when (setq entry (assoc i tags-alist))
  11770. (setq tags-alist (delete entry tags-alist)))
  11771. (setq i (1- i)))
  11772. ;; add the nex tags
  11773. (when tags
  11774. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  11775. tags-alist
  11776. (cons (cons level tags) tags-alist)))
  11777. ;; compile tags for current headline
  11778. (setq tags-list
  11779. (if org-use-tag-inheritance
  11780. (apply 'append (mapcar 'cdr tags-alist))
  11781. tags))
  11782. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  11783. (eval matcher)
  11784. (or (not org-agenda-skip-archived-trees)
  11785. (not (member org-archive-tag tags-list))))
  11786. (and (eq action 'agenda) (org-agenda-skip))
  11787. ;; list this headline
  11788. (if (eq action 'sparse-tree)
  11789. (progn
  11790. (org-show-context 'tags-tree))
  11791. (setq txt (org-format-agenda-item
  11792. ""
  11793. (concat
  11794. (if org-tags-match-list-sublevels
  11795. (make-string (1- level) ?.) "")
  11796. (org-get-heading))
  11797. category tags-list)
  11798. priority (org-get-priority txt))
  11799. (goto-char lspos)
  11800. (setq marker (org-agenda-new-marker))
  11801. (org-add-props txt props
  11802. 'org-marker marker 'org-hd-marker marker 'org-category category
  11803. 'priority priority 'type "tagsmatch")
  11804. (push txt rtn))
  11805. ;; if we are to skip sublevels, jump to end of subtree
  11806. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  11807. (when (and (eq action 'sparse-tree)
  11808. (not org-sparse-tree-open-archived-trees))
  11809. (org-hide-archived-subtrees (point-min) (point-max)))
  11810. (nreverse rtn)))
  11811. (defvar todo-only) ;; dynamically scoped
  11812. (defun org-tags-sparse-tree (&optional todo-only match)
  11813. "Create a sparse tree according to tags string MATCH.
  11814. MATCH can contain positive and negative selection of tags, like
  11815. \"+WORK+URGENT-WITHBOSS\".
  11816. If optional argument TODO_ONLY is non-nil, only select lines that are
  11817. also TODO lines."
  11818. (interactive "P")
  11819. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11820. (defvar org-cached-props nil)
  11821. (defun org-cached-entry-get (pom property)
  11822. (cdr (assoc property (or org-cached-props
  11823. (setq org-cached-props
  11824. (org-entry-properties pom))))))
  11825. (defun org-global-tags-completion-table (&optional files)
  11826. "Return the list of all tags in all agenda buffer/files."
  11827. (save-excursion
  11828. (org-uniquify
  11829. (apply 'append
  11830. (mapcar
  11831. (lambda (file)
  11832. (set-buffer (find-file-noselect file))
  11833. (org-get-buffer-tags))
  11834. (if (and files (car files))
  11835. files
  11836. (org-agenda-files)))))))
  11837. (defun org-make-tags-matcher (match)
  11838. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11839. ;; todo-only is scoped dynamically into this function, and the function
  11840. ;; may change it it the matcher asksk for it.
  11841. (unless match
  11842. ;; Get a new match request, with completion
  11843. (let ((org-last-tags-completion-table
  11844. (org-global-tags-completion-table)))
  11845. (setq match (completing-read
  11846. "Match: " 'org-tags-completion-function nil nil nil
  11847. 'org-tags-history))))
  11848. ;; Parse the string and create a lisp form
  11849. (let ((match0 match)
  11850. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  11851. minus tag mm
  11852. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11853. orterms term orlist re-p level-p prop-p pn pv)
  11854. (if (string-match "/+" match)
  11855. ;; match contains also a todo-matching request
  11856. (progn
  11857. (setq tagsmatch (substring match 0 (match-beginning 0))
  11858. todomatch (substring match (match-end 0)))
  11859. (if (string-match "^!" todomatch)
  11860. (setq todo-only t todomatch (substring todomatch 1)))
  11861. (if (string-match "^\\s-*$" todomatch)
  11862. (setq todomatch nil)))
  11863. ;; only matching tags
  11864. (setq tagsmatch match todomatch nil))
  11865. ;; Make the tags matcher
  11866. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11867. (setq tagsmatcher t)
  11868. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11869. (while (setq term (pop orterms))
  11870. (while (and (equal (substring term -1) "\\") orterms)
  11871. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11872. (while (string-match re term)
  11873. (setq minus (and (match-end 1)
  11874. (equal (match-string 1 term) "-"))
  11875. tag (match-string 2 term)
  11876. re-p (equal (string-to-char tag) ?{)
  11877. level-p (match-end 3)
  11878. prop-p (match-end 4)
  11879. mm (cond
  11880. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11881. (level-p `(= level ,(string-to-number
  11882. (match-string 3 term))))
  11883. (prop-p
  11884. (setq pn (match-string 4 term)
  11885. pv (match-string 5 term)
  11886. re-p (equal (string-to-char pv) ?{)
  11887. pv (substring pv 1 -1))
  11888. (if re-p
  11889. `(string-match ,pv (org-cached-entry-get nil ,pn))
  11890. `(equal ,pv (org-cached-entry-get nil ,pn))))
  11891. (t `(member ,(downcase tag) tags-list)))
  11892. mm (if minus (list 'not mm) mm)
  11893. term (substring term (match-end 0)))
  11894. (push mm tagsmatcher))
  11895. (push (if (> (length tagsmatcher) 1)
  11896. (cons 'and tagsmatcher)
  11897. (car tagsmatcher))
  11898. orlist)
  11899. (setq tagsmatcher nil))
  11900. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11901. (setq tagsmatcher
  11902. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11903. ;; Make the todo matcher
  11904. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11905. (setq todomatcher t)
  11906. (setq orterms (org-split-string todomatch "|") orlist nil)
  11907. (while (setq term (pop orterms))
  11908. (while (string-match re term)
  11909. (setq minus (and (match-end 1)
  11910. (equal (match-string 1 term) "-"))
  11911. kwd (match-string 2 term)
  11912. re-p (equal (string-to-char kwd) ?{)
  11913. term (substring term (match-end 0))
  11914. mm (if re-p
  11915. `(string-match ,(substring kwd 1 -1) todo)
  11916. (list 'equal 'todo kwd))
  11917. mm (if minus (list 'not mm) mm))
  11918. (push mm todomatcher))
  11919. (push (if (> (length todomatcher) 1)
  11920. (cons 'and todomatcher)
  11921. (car todomatcher))
  11922. orlist)
  11923. (setq todomatcher nil))
  11924. (setq todomatcher (if (> (length orlist) 1)
  11925. (cons 'or orlist) (car orlist))))
  11926. ;; Return the string and lisp forms of the matcher
  11927. (setq matcher (if todomatcher
  11928. (list 'and tagsmatcher todomatcher)
  11929. tagsmatcher))
  11930. (cons match0 matcher)))
  11931. (defun org-match-any-p (re list)
  11932. "Does re match any element of list?"
  11933. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11934. (delq nil list))
  11935. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  11936. (defvar org-tags-overlay (org-make-overlay 1 1))
  11937. (org-detach-overlay org-tags-overlay)
  11938. (defun org-set-tags (&optional arg just-align)
  11939. "Set the tags for the current headline.
  11940. With prefix ARG, realign all tags in headings in the current buffer."
  11941. (interactive "P")
  11942. (let* ((re (concat "^" outline-regexp))
  11943. (current (org-get-tags))
  11944. table current-tags inherited-tags ; computed below when needed
  11945. tags p0 c0 c1 rpl)
  11946. (if arg
  11947. (save-excursion
  11948. (goto-char (point-min))
  11949. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11950. (while (re-search-forward re nil t)
  11951. (org-set-tags nil t)
  11952. (end-of-line 1)))
  11953. (message "All tags realigned to column %d" org-tags-column))
  11954. (if just-align
  11955. (setq tags current)
  11956. ;; Get a new set of tags from the user
  11957. (save-excursion
  11958. (setq table (or org-tag-alist (org-get-buffer-tags))
  11959. org-last-tags-completion-table table
  11960. current-tags (org-split-string current ":")
  11961. inherited-tags (nreverse
  11962. (nthcdr (length current-tags)
  11963. (nreverse (org-get-tags-at))))
  11964. tags
  11965. (if (or (eq t org-use-fast-tag-selection)
  11966. (and org-use-fast-tag-selection
  11967. (delq nil (mapcar 'cdr table))))
  11968. (org-fast-tag-selection current-tags inherited-tags table)
  11969. (let ((org-add-colon-after-tag-completion t))
  11970. (org-trim
  11971. (completing-read "Tags: " 'org-tags-completion-function
  11972. nil nil current 'org-tags-history))))))
  11973. (while (string-match "[-+&]+" tags)
  11974. ;; No boolean logic, just a list
  11975. (setq tags (replace-match ":" t t tags))))
  11976. (if (string-match "\\`[\t ]*\\'" tags)
  11977. (setq tags "")
  11978. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11979. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11980. ;; Insert new tags at the correct column
  11981. (beginning-of-line 1)
  11982. (if (re-search-forward
  11983. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11984. (point-at-eol) t)
  11985. (progn
  11986. (if (equal tags "")
  11987. (setq rpl "")
  11988. (goto-char (match-beginning 0))
  11989. (setq c0 (current-column) p0 (point)
  11990. c1 (max (1+ c0) (if (> org-tags-column 0)
  11991. org-tags-column
  11992. (- (- org-tags-column) (length tags))))
  11993. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11994. (replace-match rpl t t)
  11995. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  11996. tags)
  11997. (error "Tags alignment failed")))))
  11998. (defun org-tags-completion-function (string predicate &optional flag)
  11999. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12000. (confirm (lambda (x) (stringp (car x)))))
  12001. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  12002. (setq s1 (match-string 1 string)
  12003. s2 (match-string 2 string))
  12004. (setq s1 "" s2 string))
  12005. (cond
  12006. ((eq flag nil)
  12007. ;; try completion
  12008. (setq rtn (try-completion s2 ctable confirm))
  12009. (if (stringp rtn)
  12010. (concat s1 s2 (substring rtn (length s2))
  12011. (if (and org-add-colon-after-tag-completion
  12012. (assoc rtn ctable))
  12013. ":" "")))
  12014. )
  12015. ((eq flag t)
  12016. ;; all-completions
  12017. (all-completions s2 ctable confirm)
  12018. )
  12019. ((eq flag 'lambda)
  12020. ;; exact match?
  12021. (assoc s2 ctable)))
  12022. ))
  12023. (defun org-fast-tag-insert (kwd tags face &optional end)
  12024. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  12025. (insert (format "%-12s" (concat kwd ":"))
  12026. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12027. (or end "")))
  12028. (defun org-fast-tag-show-exit (flag)
  12029. (save-excursion
  12030. (goto-line 3)
  12031. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12032. (replace-match ""))
  12033. (when flag
  12034. (end-of-line 1)
  12035. (move-to-column (- (window-width) 19) t)
  12036. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12037. (defun org-set-current-tags-overlay (current prefix)
  12038. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12039. (if (featurep 'xemacs)
  12040. (org-overlay-display org-tags-overlay (concat prefix s)
  12041. 'secondary-selection)
  12042. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12043. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12044. (defun org-fast-tag-selection (current inherited table)
  12045. "Fast tag selection with single keys.
  12046. CURRENT is the current list of tags in the headline, INHERITED is the
  12047. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12048. possibly with grouping information.
  12049. If the keys are nil, a-z are automatically assigned.
  12050. Returns the new tags string, or nil to not change the current settings."
  12051. (let* ((maxlen (apply 'max (mapcar
  12052. (lambda (x)
  12053. (if (stringp (car x)) (string-width (car x)) 0))
  12054. table)))
  12055. (buf (current-buffer))
  12056. (expert (eq org-fast-tag-selection-single-key 'expert))
  12057. (buffer-tags nil)
  12058. (fwidth (+ maxlen 3 1 3))
  12059. (ncol (/ (- (window-width) 4) fwidth))
  12060. (i-face 'org-done)
  12061. (c-face 'org-todo)
  12062. tg cnt e c char c1 c2 ntable tbl rtn
  12063. ov-start ov-end ov-prefix
  12064. (exit-after-next org-fast-tag-selection-single-key)
  12065. groups ingroup)
  12066. (save-excursion
  12067. (beginning-of-line 1)
  12068. (if (looking-at
  12069. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*\\(\r\\|$\\)"))
  12070. (setq ov-start (match-beginning 1)
  12071. ov-end (match-end 1)
  12072. ov-prefix "")
  12073. (setq ov-start (1- (point-at-eol))
  12074. ov-end (1+ ov-start))
  12075. (skip-chars-forward "^\n\r")
  12076. (setq ov-prefix
  12077. (concat
  12078. (buffer-substring (1- (point)) (point))
  12079. (if (> (current-column) org-tags-column)
  12080. " "
  12081. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12082. (org-move-overlay org-tags-overlay ov-start ov-end)
  12083. (save-window-excursion
  12084. (if expert
  12085. (set-buffer (get-buffer-create " *Org tags*"))
  12086. (delete-other-windows)
  12087. (split-window-vertically)
  12088. (switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12089. (erase-buffer)
  12090. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12091. (org-fast-tag-insert "Current" current c-face "\n\n")
  12092. (org-fast-tag-show-exit exit-after-next)
  12093. (org-set-current-tags-overlay current ov-prefix)
  12094. (setq tbl table char ?a cnt 0)
  12095. (while (setq e (pop tbl))
  12096. (cond
  12097. ((equal e '(:startgroup))
  12098. (push '() groups) (setq ingroup t)
  12099. (when (not (= cnt 0))
  12100. (setq cnt 0)
  12101. (insert "\n"))
  12102. (insert "{ "))
  12103. ((equal e '(:endgroup))
  12104. (setq ingroup nil cnt 0)
  12105. (insert "}\n"))
  12106. (t
  12107. (setq tg (car e) c2 nil)
  12108. (if (cdr e)
  12109. (setq c (cdr e))
  12110. ;; automatically assign a character.
  12111. (setq c1 (string-to-char
  12112. (downcase (substring
  12113. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12114. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12115. (while (or (rassoc char ntable) (rassoc char table))
  12116. (setq char (1+ char)))
  12117. (setq c2 c1))
  12118. (setq c (or c2 char)))
  12119. (if ingroup (push tg (car groups)))
  12120. (setq tg (org-add-props tg nil 'face
  12121. (cond
  12122. ((member tg current) c-face)
  12123. ((member tg inherited) i-face)
  12124. (t nil))))
  12125. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12126. (insert "[" c "] " tg (make-string
  12127. (- fwidth 4 (length tg)) ?\ ))
  12128. (push (cons tg c) ntable)
  12129. (when (= (setq cnt (1+ cnt)) ncol)
  12130. (insert "\n")
  12131. (if ingroup (insert " "))
  12132. (setq cnt 0)))))
  12133. (setq ntable (nreverse ntable))
  12134. (insert "\n")
  12135. (goto-char (point-min))
  12136. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12137. (fit-window-to-buffer))
  12138. (setq rtn
  12139. (catch 'exit
  12140. (while t
  12141. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  12142. (if groups " [!] no groups" " [!]groups")
  12143. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12144. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12145. (cond
  12146. ((= c ?\r) (throw 'exit t))
  12147. ((= c ?!)
  12148. (setq groups (not groups))
  12149. (goto-char (point-min))
  12150. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12151. ((= c ?\C-c)
  12152. (if (not expert)
  12153. (org-fast-tag-show-exit
  12154. (setq exit-after-next (not exit-after-next)))
  12155. (setq expert nil)
  12156. (delete-other-windows)
  12157. (split-window-vertically)
  12158. (switch-to-buffer-other-window " *Org tags*")
  12159. (and (fboundp 'fit-window-to-buffer)
  12160. (fit-window-to-buffer))))
  12161. ((or (= c ?\C-g)
  12162. (and (= c ?q) (not (rassoc c ntable))))
  12163. (org-detach-overlay org-tags-overlay)
  12164. (setq quit-flag t))
  12165. ((= c ?\ )
  12166. (setq current nil)
  12167. (if exit-after-next (setq exit-after-next 'now)))
  12168. ((= c ?\t)
  12169. (condition-case nil
  12170. (setq tg (completing-read
  12171. "Tag: "
  12172. (or buffer-tags
  12173. (with-current-buffer buf
  12174. (org-get-buffer-tags)))))
  12175. (quit (setq tg "")))
  12176. (when (string-match "\\S-" tg)
  12177. (add-to-list 'buffer-tags (list tg))
  12178. (if (member tg current)
  12179. (setq current (delete tg current))
  12180. (push tg current)))
  12181. (if exit-after-next (setq exit-after-next 'now)))
  12182. ((setq e (rassoc c ntable) tg (car e))
  12183. (if (member tg current)
  12184. (setq current (delete tg current))
  12185. (loop for g in groups do
  12186. (if (member tg g)
  12187. (mapcar (lambda (x)
  12188. (setq current (delete x current)))
  12189. g)))
  12190. (push tg current))
  12191. (if exit-after-next (setq exit-after-next 'now))))
  12192. ;; Create a sorted list
  12193. (setq current
  12194. (sort current
  12195. (lambda (a b)
  12196. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12197. (if (eq exit-after-next 'now) (throw 'exit t))
  12198. (goto-char (point-min))
  12199. (beginning-of-line 2)
  12200. (delete-region (point) (point-at-eol))
  12201. (org-fast-tag-insert "Current" current c-face)
  12202. (org-set-current-tags-overlay current ov-prefix)
  12203. (while (re-search-forward
  12204. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  12205. (setq tg (match-string 1))
  12206. (add-text-properties (match-beginning 1) (match-end 1)
  12207. (list 'face
  12208. (cond
  12209. ((member tg current) c-face)
  12210. ((member tg inherited) i-face)
  12211. (t nil)))))
  12212. (goto-char (point-min)))))
  12213. (org-detach-overlay org-tags-overlay)
  12214. (if rtn
  12215. (mapconcat 'identity current ":")
  12216. nil))))
  12217. (defun org-get-tags ()
  12218. "Get the TAGS string in the current headline."
  12219. (unless (org-on-heading-p t)
  12220. (error "Not on a heading"))
  12221. (save-excursion
  12222. (beginning-of-line 1)
  12223. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*\\(\r\\|$\\)"))
  12224. (org-match-string-no-properties 1)
  12225. "")))
  12226. (defun org-get-buffer-tags ()
  12227. "Get a table of all tags used in the buffer, for completion."
  12228. (let (tags)
  12229. (save-excursion
  12230. (goto-char (point-min))
  12231. (while (re-search-forward
  12232. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  12233. (when (equal (char-after (point-at-bol 0)) ?*)
  12234. (mapc (lambda (x) (add-to-list 'tags x))
  12235. (org-split-string (org-match-string-no-properties 1) ":")))))
  12236. (mapcar 'list tags)))
  12237. ;;;; Properties
  12238. ;;; Setting and retrieving properties
  12239. (defconst org-special-properties
  12240. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED"
  12241. "CLOCK" "PRIORITY")
  12242. "The special properties valid in Org-mode.
  12243. These are properties that are not defined in the property drawer,
  12244. but in some other way.")
  12245. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12246. "Regular expression matching the first line of a property drawer.")
  12247. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12248. "Regular expression matching the first line of a property drawer.")
  12249. (defmacro org-with-point-at (pom &rest body)
  12250. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  12251. (declare (indent 1) (debug t))
  12252. `(save-excursion
  12253. (if (markerp pom) (set-buffer (marker-buffer pom)))
  12254. (save-excursion
  12255. (goto-char (or pom (point)))
  12256. ,@body)))
  12257. (defun org-get-property-block (&optional beg end force)
  12258. "Return the (beg . end) range of the body of the property drawer.
  12259. BEG and END can be beginning and end of subtree, if not given
  12260. they will be found.
  12261. If the drawer does not exist and FORCE is non-nil, greater the drawer."
  12262. (catch 'exit
  12263. (save-excursion
  12264. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12265. (end (or end (progn (outline-next-heading) (point)))))
  12266. (goto-char beg)
  12267. (if (re-search-forward org-property-start-re end t)
  12268. (setq beg (1+ (match-end 0)))
  12269. (or force (throw 'exit nil))
  12270. (beginning-of-line 2)
  12271. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  12272. (not (equal (match-string 1) org-clock-string)))
  12273. (beginning-of-line 2))
  12274. (insert ":PROPERTIES:\n:END:\n")
  12275. (beginning-of-line -1)
  12276. (org-indent-line-function)
  12277. (setq beg (1+ (point-at-eol)) end beg)
  12278. (beginning-of-line 2)
  12279. (org-indent-line-function)
  12280. (throw 'exit (cons beg end)))
  12281. (if (re-search-forward org-property-end-re end t)
  12282. (setq end (match-beginning 0))
  12283. (or force (throw 'exit nil))
  12284. (goto-char beg)
  12285. (setq end beg)
  12286. (org-indent-line-function)
  12287. (insert ":END:\n"))
  12288. (cons beg end)))))
  12289. (defun org-entry-properties (&optional pom which)
  12290. "Get all properties of the entry at point-or-marker POM.
  12291. This includes the TODO keyword, the tags, time strings for deadline,
  12292. scheduled, and clocking, and any additional properties defined in the
  12293. entry. The return value is an alist, keys may occur multiple times
  12294. if the property key was used several times.
  12295. POM may also be nil, in which case the current entry is used.
  12296. If WHICH is nil or `all', get all properties. If WHICH is
  12297. `special' or `standard', only get that subclass."
  12298. (setq which (or which 'all))
  12299. (org-with-point-at pom
  12300. (let ((clockstr (substring org-clock-string 0 -1))
  12301. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  12302. beg end range props key value)
  12303. (save-excursion
  12304. (when (condition-case nil (org-back-to-heading t) (error nil))
  12305. (setq beg (point))
  12306. (outline-next-heading)
  12307. (setq end (point))
  12308. (when (memq which '(all special))
  12309. ;; Get the special properties, like TODO and tags
  12310. (goto-char beg)
  12311. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  12312. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12313. (when (looking-at org-priority-regexp)
  12314. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12315. (when (and (setq value (org-get-tags)) (string-match "\\S-" value))
  12316. (push (cons "TAGS" value) props))
  12317. (when (setq value (org-get-tags-at))
  12318. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  12319. props))
  12320. (while (re-search-forward org-keyword-time-regexp end t)
  12321. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  12322. (unless (member key excluded) (push key excluded))
  12323. (push (cons key
  12324. (if (equal key clockstr)
  12325. (org-no-properties
  12326. (org-trim
  12327. (buffer-substring
  12328. (match-beginning 2) (point-at-eol))))
  12329. (org-match-string-no-properties 2)))
  12330. props)))
  12331. (when (memq which '(all standard))
  12332. ;; Get the standard properties, like :PORP: ...
  12333. (setq range (org-get-property-block beg end))
  12334. (when range
  12335. (goto-char (car range))
  12336. (while (re-search-forward
  12337. "^[ \t]*:\\([a-zA-Z][a-zA-Z0-9]*\\):[ \t]*\\(\\S-.*\\S-\\)"
  12338. (cdr range) t)
  12339. (setq key (org-match-string-no-properties 1)
  12340. value (org-match-string-no-properties 2))
  12341. (unless (member key excluded)
  12342. (push (cons key value) props)))))
  12343. (nreverse props))))))
  12344. (defun org-entry-get (pom property &optional inherit)
  12345. "Get value of PROPERTY for entry at point-or-marker POM.
  12346. If INHERIT is non-nil and the entry does not have the property,
  12347. then also check higher levels of the hierarchy."
  12348. (org-with-point-at pom
  12349. (if inherit
  12350. (org-entry-get-with-inheritance property)
  12351. (if (member property org-special-properties)
  12352. ;; We need a special property. Use brute force, get all properties.
  12353. (cdr (assoc property (org-entry-properties nil 'special)))
  12354. (let ((range (org-get-property-block)))
  12355. (if (and range
  12356. (goto-char (car range))
  12357. (re-search-forward
  12358. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  12359. (cdr range) t))
  12360. ;; Found the property, return it.
  12361. (org-match-string-no-properties 1)))))))
  12362. (defun org-entry-delete (pom property)
  12363. "Delete the property PROPERTY from entry at point-or-marker POM."
  12364. (org-with-point-at pom
  12365. (if (member property org-special-properties)
  12366. nil ; cannot delete these properties.
  12367. (let ((range (org-get-property-block)))
  12368. (if (and range
  12369. (goto-char (car range))
  12370. (re-search-forward
  12371. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  12372. (cdr range) t))
  12373. (delete-region (match-beginning 0) (1+ (point-at-eol))))))))
  12374. (defvar org-entry-property-inherited-from (make-marker))
  12375. (defun org-entry-get-with-inheritance (property)
  12376. "Get entry property, and search higher levels if not present."
  12377. (let (tmp)
  12378. (save-excursion
  12379. (catch 'ex
  12380. (while t
  12381. (when (setq tmp (org-entry-get nil property))
  12382. (org-back-to-heading t)
  12383. (move-marker org-entry-property-inherited-from (point))
  12384. (throw 'ex tmp))
  12385. (condition-case nil
  12386. (org-up-heading-all 1)
  12387. (error (throw 'ex nil))))))))
  12388. (defun org-entry-put (pom property value)
  12389. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12390. (org-with-point-at pom
  12391. (org-back-to-heading t)
  12392. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12393. range)
  12394. (cond
  12395. ((equal property "TODO")
  12396. (when (and (stringp value) (string-match "\\S-" value)
  12397. (not (member value org-todo-keywords-1)))
  12398. (error "\"%s\" is not a valid TODO state" value))
  12399. (if (or (not value)
  12400. (not (string-match "\\S-" value)))
  12401. (setq value 'none))
  12402. (org-todo value)
  12403. (org-set-tags nil 'align))
  12404. ((equal property "PRIORITY")
  12405. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12406. (string-to-char value) ?\ ))
  12407. (org-set-tags nil 'align))
  12408. ((member property org-special-properties)
  12409. (error "The %s property can not yet be set with `org-entry-put'"
  12410. property))
  12411. (t ; a non-special property
  12412. (setq range (org-get-property-block beg end 'force))
  12413. (goto-char (car range))
  12414. (if (re-search-forward
  12415. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12416. (progn
  12417. (delete-region (match-beginning 1) (match-end 1))
  12418. (goto-char (match-beginning 1)))
  12419. (goto-char (cdr range))
  12420. (insert "\n")
  12421. (backward-char 1)
  12422. (org-indent-line-function)
  12423. (insert ":" property ":"))
  12424. (and value (insert " " value)))))))
  12425. (defun org-buffer-property-keys (&optional include-specials)
  12426. "Get all property keys in the current buffer."
  12427. (let (rtn range)
  12428. (save-excursion
  12429. (save-restriction
  12430. (widen)
  12431. (goto-char (point-min))
  12432. (while (re-search-forward org-property-start-re nil t)
  12433. (setq range (org-get-property-block))
  12434. (goto-char (car range))
  12435. (while (re-search-forward "^[ \t]*:\\([a-zA-Z0-9]+\\):" (cdr range) t)
  12436. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12437. (outline-next-heading))))
  12438. (when include-specials
  12439. (setq rtn (append org-special-properties rtn)))
  12440. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12441. ;; FIXME: This should automatically find the right place int he entry.
  12442. ;; And then org-entry-put should use it.
  12443. (defun org-insert-property-drawer ()
  12444. "Insert a property drawer at point."
  12445. (interactive)
  12446. (beginning-of-line 1)
  12447. (insert ":PROPERTIES:\n:END:\n")
  12448. (beginning-of-line -1)
  12449. (org-indent-line-function)
  12450. (beginning-of-line 2)
  12451. (org-indent-line-function)
  12452. (end-of-line 0))
  12453. (defvar org-column-overlays nil
  12454. "Holds the list of current column overlays.")
  12455. (defvar org-current-columns-fmt nil
  12456. "Loval variable, holds the currently active column format.")
  12457. (defvar org-current-columns-maxwidths nil
  12458. "Loval variable, holds the currently active maximum column widths.")
  12459. (defvar org-column-map (make-sparse-keymap)
  12460. "The keymap valid in column display.")
  12461. (define-key org-column-map "e" 'org-column-edit)
  12462. (define-key org-column-map "v" 'org-column-show-value)
  12463. (define-key org-column-map "q" 'org-column-quit)
  12464. (define-key org-column-map [left] 'backward-char)
  12465. (define-key org-column-map [right] 'forward-char)
  12466. (easy-menu-define org-column-menu org-column-map "Org Column Menu"
  12467. '("Column"
  12468. ["Edit property" org-column-edit t]
  12469. ["Show full value" org-column-show-value t]
  12470. ["Quit" org-column-quit t]))
  12471. (defun org-new-column-overlay (beg end &optional string face)
  12472. "Create a new column overlay an add it to the list."
  12473. (let ((ov (org-make-overlay beg end)))
  12474. (org-overlay-put ov 'face (or face 'secondary-selection))
  12475. (org-overlay-display ov string face)
  12476. (push ov org-column-overlays)
  12477. ov))
  12478. (defun org-overlay-columns (&optional props)
  12479. "Overlay the current line with column display."
  12480. (interactive)
  12481. (let ((fmt (copy-sequence org-current-columns-fmt))
  12482. (beg (point-at-bol))
  12483. (start 0) props pom property ass width f string ov)
  12484. ;; Check if the entry is in another buffer.
  12485. (unless props
  12486. (if (eq major-mode 'org-agenda-mode)
  12487. (setq pom (or (get-text-property (point) 'org-hd-marker)
  12488. (get-text-property (point) 'org-marker))
  12489. props (if pom (org-entry-properties pom) nil))
  12490. (setq props (org-entry-properties nil))))
  12491. ;; Parse the format
  12492. (while (string-match "%\\([0-9]+\\)?\\([a-zA-Z0-9]+\\)\\-*"
  12493. fmt start)
  12494. (setq start (match-end 0)
  12495. property (match-string 2 fmt)
  12496. ass (if (equal property "ITEM")
  12497. (cons "ITEM"
  12498. (save-match-data
  12499. (org-no-properties
  12500. (org-remove-tabs
  12501. (buffer-substring-no-properties
  12502. (point-at-bol) (point-at-eol))))))
  12503. (assoc property props))
  12504. width (or (cdr (assoc property org-current-columns-maxwidths))
  12505. (string-to-number (or (match-string 1 fmt) "10")))
  12506. f (format "%%-%d.%ds | " width width)
  12507. string (format f (or (cdr ass) "")))
  12508. ;; Create the overlay
  12509. (org-unmodified
  12510. (setq ov (org-new-column-overlay
  12511. beg (setq beg (1+ beg)) string 'org-column))
  12512. (org-overlay-put ov 'keymap org-column-map)
  12513. (org-overlay-put ov 'org-column-key property)
  12514. (org-overlay-put ov 'org-column-value (cdr ass)))
  12515. (if (or (not (char-after beg))
  12516. (equal (char-after beg) ?\n))
  12517. (let ((inhibit-read-only t))
  12518. (save-excursion
  12519. (goto-char beg)
  12520. (insert " ")))))
  12521. ;; Make the rest of the line disappear.
  12522. ;; FIXME: put the keymap also at the end of the line!
  12523. (org-unmodified
  12524. (setq ov (org-new-column-overlay beg (point-at-eol)))
  12525. (org-overlay-put ov 'invisible t)
  12526. (org-overlay-put ov 'keymap 'org-column-map)
  12527. (push ov org-column-overlays)
  12528. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  12529. (org-overlay-put ov 'keymap 'org-column-map)
  12530. (push ov org-column-overlays)
  12531. (let ((inhibit-read-only t))
  12532. (put-text-property (1- (point-at-bol))
  12533. (min (point-max) (1+ (point-at-eol)))
  12534. 'read-only "Type `e' to edit property")))))
  12535. (defun org-overlay-columns-title ()
  12536. "Overlay the newline before the current line with the table title."
  12537. (interactive)
  12538. (let ((fmt (copy-sequence org-current-columns-fmt))
  12539. (start 0)
  12540. string (title "")
  12541. property width f ov)
  12542. (while (string-match "%\\([0-9]+\\)?\\([a-zA-Z0-9]+\\)\\-*"
  12543. fmt start)
  12544. (setq start (match-end 0)
  12545. property (match-string 2 fmt)
  12546. width (or (cdr (assoc property org-current-columns-maxwidths))
  12547. (string-to-number (or (match-string 1 fmt) "10")))
  12548. f (format "%%-%d.%ds | " width width)
  12549. string (format f property)
  12550. title (concat title string)))
  12551. (org-unmodified
  12552. (setq ov (org-new-column-overlay
  12553. (1- (point-at-bol)) (point-at-bol)
  12554. (concat "\n" (make-string (length title) ?-) "\n"
  12555. title "\n" (make-string (length title) ?-) "\n")
  12556. 'bold))
  12557. (org-overlay-put ov 'keymap org-column-map))))
  12558. (defun org-remove-column-overlays ()
  12559. "Remove all currently active column overlays."
  12560. (interactive)
  12561. (org-unmodified
  12562. (mapc 'org-delete-overlay org-column-overlays)
  12563. (setq org-column-overlays nil)
  12564. (let ((inhibit-read-only t))
  12565. (remove-text-properties (point-min) (point-max) '(read-only t)))))
  12566. (defun org-column-show-value ()
  12567. "Show the full value of the property."
  12568. (interactive)
  12569. (let ((value (get-char-property (point) 'org-column-value)))
  12570. (message "Value is: %s" (or value ""))))
  12571. (defun org-column-quit ()
  12572. "Remove the column overlays and in this way exit column editing."
  12573. (interactive)
  12574. (org-unmodified
  12575. (org-remove-column-overlays)
  12576. (let ((inhibit-read-only t))
  12577. ;; FIXME: is this safe???
  12578. ;; or are there other reasons why there may be a read-only property????
  12579. (remove-text-properties (point-min) (point-max) '(read-only t))))
  12580. (when (eq major-mode 'org-agenda-mode)
  12581. (message "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  12582. (defun org-column-edit ()
  12583. "Edit the value of the property at point in column view.
  12584. Where possible, use the standard interface for changing this line."
  12585. (interactive)
  12586. (let* ((col (current-column))
  12587. (key (get-char-property (point) 'org-column-key))
  12588. (value (get-char-property (point) 'org-column-value))
  12589. (bol (point-at-bol)) (eol (point-at-eol))
  12590. (pom (or (get-text-property bol 'org-hd-marker)
  12591. (point))) ; keep despite of compiler waring
  12592. (line-overlays
  12593. (delq nil (mapcar (lambda (x)
  12594. (and (eq (overlay-buffer x) (current-buffer))
  12595. (>= (overlay-start x) bol)
  12596. (<= (overlay-start x) eol)
  12597. x))
  12598. org-column-overlays)))
  12599. nval eval)
  12600. (when (equal key "ITEM")
  12601. (error "Cannot edit item headline from here"))
  12602. (cond
  12603. ((equal key "TODO")
  12604. (setq eval '(org-with-point-at pom
  12605. (let ((current-prefix-arg '(4))) (org-todo '(4))))))
  12606. ((equal key "PRIORITY")
  12607. (setq eval '(org-with-point-at pom
  12608. (call-interactively 'org-priority))))
  12609. ((equal key "TAGS")
  12610. (setq eval '(org-with-point-at pom
  12611. (let ((org-fast-tag-selection-single-key
  12612. (if (eq org-fast-tag-selection-single-key 'expert)
  12613. t org-fast-tag-selection-single-key)))
  12614. (call-interactively 'org-set-tags)))))
  12615. ((equal key "DEADLINE")
  12616. (setq eval '(org-with-point-at pom
  12617. (call-interactively 'org-deadline))))
  12618. ((equal key "SCHEDULED")
  12619. (setq eval '(org-with-point-at pom
  12620. (call-interactively 'org-deadline))))
  12621. (t
  12622. (setq nval (read-string "Edit: " value))
  12623. (setq nval (org-trim nval))
  12624. (when (not (equal nval value))
  12625. (setq eval '(org-entry-put pom key nval)))))
  12626. (when eval
  12627. (let ((inhibit-read-only t))
  12628. (remove-text-properties (1- bol) eol '(read-only t))
  12629. (unwind-protect
  12630. (progn
  12631. (setq org-column-overlays
  12632. (org-delete-all line-overlays org-column-overlays))
  12633. (mapc 'org-delete-overlay line-overlays)
  12634. (eval eval))
  12635. (org-overlay-columns))))
  12636. (move-to-column col)))
  12637. (defun org-columns ()
  12638. "Turn on column view on an org-mode file."
  12639. (interactive)
  12640. (org-remove-column-overlays)
  12641. (let (beg end fmt cache maxwidths)
  12642. (move-marker org-entry-property-inherited-from nil)
  12643. (setq fmt (org-entry-get nil "COLUMNS" t))
  12644. (unless fmt
  12645. (message "No local columns format defined, using default"))
  12646. (org-set-local 'org-current-columns-fmt (or fmt org-default-columns-format))
  12647. (org-back-to-heading)
  12648. (save-excursion
  12649. (if (marker-position org-entry-property-inherited-from)
  12650. (goto-char org-entry-property-inherited-from))
  12651. (setq beg (point)
  12652. end (org-end-of-subtree t t))
  12653. (goto-char beg)
  12654. ;; Get and cache the properties
  12655. (while (re-search-forward (concat "^" outline-regexp) end t)
  12656. (push (cons (org-current-line) (org-entry-properties)) cache))
  12657. (when cache
  12658. (setq maxwidths (org-get-columns-autowidth-alist fmt cache))
  12659. (org-set-local 'org-current-columns-maxwidths maxwidths)
  12660. (goto-line (car (org-last cache)))
  12661. (org-overlay-columns-title)
  12662. (mapc (lambda (x)
  12663. (goto-line (car x))
  12664. (org-overlay-columns (cdr x)))
  12665. cache)))))
  12666. (defvar org-overriding-columns-format nil
  12667. "FIXME:")
  12668. (defvar org-agenda-view-columns-initially nil
  12669. "FIXME:")
  12670. (defun org-agenda-columns ()
  12671. "Turn on column view in the agenda."
  12672. (interactive)
  12673. (let (fmt first-done cache maxwidths m)
  12674. (cond
  12675. ((and (local-variable-p 'org-overriding-columns-format)
  12676. org-overriding-columns-format)
  12677. (setq fmt org-overriding-columns-format))
  12678. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  12679. (setq fmt (org-entry-get m "COLUMNS" t)))
  12680. ((and (boundp 'org-current-columns-fmt)
  12681. (local-variable-p 'org-current-columns-fmt)
  12682. org-current-columns-fmt)
  12683. (setq fmt org-current-columns-fmt))
  12684. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  12685. (setq m (get-text-property m 'org-hd-marker))
  12686. (setq fmt (org-entry-get m "COLUMNS" t))))
  12687. (setq fmt (or fmt org-default-columns-format))
  12688. (org-set-local 'org-current-columns-fmt fmt)
  12689. (save-excursion
  12690. ;; Get and cache the properties
  12691. (goto-char (point-min))
  12692. (while (not (eobp))
  12693. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  12694. (get-text-property (point) 'org-marker)))
  12695. (push (cons (org-current-line) (org-entry-properties m)) cache))
  12696. (beginning-of-line 2))
  12697. (when cache
  12698. (setq maxwidths (org-get-columns-autowidth-alist fmt cache))
  12699. (org-set-local 'org-current-columns-maxwidths maxwidths)
  12700. (goto-line (car (org-last cache)))
  12701. (org-overlay-columns-title)
  12702. (mapc (lambda (x)
  12703. (goto-line (car x))
  12704. (org-overlay-columns (cdr x)))
  12705. cache)))))
  12706. (defun org-get-columns-autowidth-alist (s cache)
  12707. "Derive the maximum column widths from the format and the cache."
  12708. (let ((start 0) rtn)
  12709. (while (string-match "%\\([a-zA-Z]\\S-*\\)" s start)
  12710. (push (cons (match-string 1 s) 1) rtn)
  12711. (setq start (match-end 0)))
  12712. (mapc (lambda (x)
  12713. (setcdr x (apply 'max
  12714. (mapcar
  12715. (lambda (y)
  12716. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  12717. cache))))
  12718. rtn)
  12719. rtn))
  12720. ;;;; Timestamps
  12721. (defvar org-last-changed-timestamp nil)
  12722. (defvar org-time-was-given) ; dynamically scoped parameter
  12723. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12724. (defvar org-ts-what) ; dynamically scoped parameter
  12725. (defun org-time-stamp (arg)
  12726. "Prompt for a date/time and insert a time stamp.
  12727. If the user specifies a time like HH:MM, or if this command is called
  12728. with a prefix argument, the time stamp will contain date and time.
  12729. Otherwise, only the date will be included. All parts of a date not
  12730. specified by the user will be filled in from the current date/time.
  12731. So if you press just return without typing anything, the time stamp
  12732. will represent the current date/time. If there is already a timestamp
  12733. at the cursor, it will be modified."
  12734. (interactive "P")
  12735. (let (org-time-was-given org-end-time-was-given time)
  12736. (cond
  12737. ((and (org-at-timestamp-p)
  12738. (eq last-command 'org-time-stamp)
  12739. (eq this-command 'org-time-stamp))
  12740. (insert "--")
  12741. (setq time (let ((this-command this-command))
  12742. (org-read-date arg 'totime)))
  12743. (org-insert-time-stamp time (or org-time-was-given arg)))
  12744. ((org-at-timestamp-p)
  12745. (setq time (let ((this-command this-command))
  12746. (org-read-date arg 'totime)))
  12747. (when (org-at-timestamp-p) ; just to get the match data
  12748. (replace-match "")
  12749. (setq org-last-changed-timestamp
  12750. (org-insert-time-stamp
  12751. time (or org-time-was-given arg)
  12752. nil nil nil (list org-end-time-was-given))))
  12753. (message "Timestamp updated"))
  12754. (t
  12755. (setq time (let ((this-command this-command))
  12756. (org-read-date arg 'totime)))
  12757. (org-insert-time-stamp time (or org-time-was-given arg)
  12758. nil nil nil (list org-end-time-was-given))))))
  12759. (defun org-time-stamp-inactive (&optional arg)
  12760. "Insert an inactive time stamp.
  12761. An inactive time stamp is enclosed in square brackets instead of angle
  12762. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12763. does not link to the calendar and cannot be changed with the S-cursor keys.
  12764. So these are more for recording a certain time/date."
  12765. (interactive "P")
  12766. (let (org-time-was-given org-end-time-was-given time)
  12767. (setq time (org-read-date arg 'totime))
  12768. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  12769. nil nil (list org-end-time-was-given))))
  12770. (defvar org-date-ovl (org-make-overlay 1 1))
  12771. (org-overlay-put org-date-ovl 'face 'org-warning)
  12772. (org-detach-overlay org-date-ovl)
  12773. (defvar org-ans1) ; dynamically scoped parameter
  12774. (defvar org-ans2) ; dynamically scoped parameter
  12775. (defvar org-plain-time-of-day-regexp) ; defined below
  12776. (defun org-read-date (&optional with-time to-time from-string prompt)
  12777. "Read a date and make things smooth for the user.
  12778. The prompt will suggest to enter an ISO date, but you can also enter anything
  12779. which will at least partially be understood by `parse-time-string'.
  12780. Unrecognized parts of the date will default to the current day, month, year,
  12781. hour and minute. For example,
  12782. 3-2-5 --> 2003-02-05
  12783. feb 15 --> currentyear-02-15
  12784. sep 12 9 --> 2009-09-12
  12785. 12:45 --> today 12:45
  12786. 22 sept 0:34 --> currentyear-09-22 0:34
  12787. 12 --> currentyear-currentmonth-12
  12788. Fri --> nearest Friday (today or later)
  12789. +4 --> four days from today (only if +N is the only thing given)
  12790. etc.
  12791. The function understands only English month and weekday abbreviations,
  12792. but this can be configured with the variables `parse-time-months' and
  12793. `parse-time-weekdays'.
  12794. While prompting, a calendar is popped up - you can also select the
  12795. date with the mouse (button 1). The calendar shows a period of three
  12796. months. To scroll it to other months, use the keys `>' and `<'.
  12797. If you don't like the calendar, turn it off with
  12798. \(setq org-popup-calendar-for-date-prompt nil)
  12799. With optional argument TO-TIME, the date will immediately be converted
  12800. to an internal time.
  12801. With an optional argument WITH-TIME, the prompt will suggest to also
  12802. insert a time. Note that when WITH-TIME is not set, you can still
  12803. enter a time, and this function will inform the calling routine about
  12804. this change. The calling routine may then choose to change the format
  12805. used to insert the time stamp into the buffer to include the time."
  12806. (require 'parse-time)
  12807. (let* ((org-time-stamp-rounding-minutes
  12808. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  12809. (ct (org-current-time))
  12810. (default-time
  12811. ;; Default time is either today, or, when entering a range,
  12812. ;; the range start.
  12813. (if (save-excursion
  12814. (re-search-backward
  12815. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12816. (- (point) 20) t))
  12817. (apply
  12818. 'encode-time
  12819. (mapcar (lambda(x) (or x 0))
  12820. (parse-time-string (match-string 1))))
  12821. ct))
  12822. (calendar-move-hook nil)
  12823. (view-diary-entries-initially nil)
  12824. (view-calendar-holidays-initially nil)
  12825. (timestr (format-time-string
  12826. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  12827. (prompt (concat (if prompt (concat prompt " ") "")
  12828. (format "Date and/or time (default [%s]): " timestr)))
  12829. ans (org-ans0 "") org-ans1 org-ans2 (deltadays 0)
  12830. second minute hour day month year tl wday wday1 pm)
  12831. (cond
  12832. (from-string (setq ans from-string))
  12833. (org-popup-calendar-for-date-prompt
  12834. (save-excursion
  12835. (save-window-excursion
  12836. (calendar)
  12837. (calendar-forward-day (- (time-to-days default-time)
  12838. (calendar-absolute-from-gregorian
  12839. (calendar-current-date))))
  12840. (org-eval-in-calendar nil t)
  12841. (let* ((old-map (current-local-map))
  12842. (map (copy-keymap calendar-mode-map))
  12843. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12844. (org-defkey map (kbd "RET") 'org-calendar-select)
  12845. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12846. 'org-calendar-select-mouse)
  12847. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12848. 'org-calendar-select-mouse)
  12849. (org-defkey minibuffer-local-map [(meta shift left)]
  12850. (lambda () (interactive)
  12851. (org-eval-in-calendar '(calendar-backward-month 1))))
  12852. (org-defkey minibuffer-local-map [(meta shift right)]
  12853. (lambda () (interactive)
  12854. (org-eval-in-calendar '(calendar-forward-month 1))))
  12855. (org-defkey minibuffer-local-map [(shift up)]
  12856. (lambda () (interactive)
  12857. (org-eval-in-calendar '(calendar-backward-week 1))))
  12858. (org-defkey minibuffer-local-map [(shift down)]
  12859. (lambda () (interactive)
  12860. (org-eval-in-calendar '(calendar-forward-week 1))))
  12861. (org-defkey minibuffer-local-map [(shift left)]
  12862. (lambda () (interactive)
  12863. (org-eval-in-calendar '(calendar-backward-day 1))))
  12864. (org-defkey minibuffer-local-map [(shift right)]
  12865. (lambda () (interactive)
  12866. (org-eval-in-calendar '(calendar-forward-day 1))))
  12867. (org-defkey minibuffer-local-map ">"
  12868. (lambda () (interactive)
  12869. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12870. (org-defkey minibuffer-local-map "<"
  12871. (lambda () (interactive)
  12872. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12873. (unwind-protect
  12874. (progn
  12875. (use-local-map map)
  12876. (setq org-ans0 (read-string prompt "" nil nil))
  12877. ;; org-ans0: from prompt
  12878. ;; org-ans1: from mouse click
  12879. ;; org-ans2: from calendar motion
  12880. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12881. (use-local-map old-map))))))
  12882. (t ; Naked prompt only
  12883. (setq ans (read-string prompt "" nil timestr))))
  12884. (org-detach-overlay org-date-ovl)
  12885. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" org-ans0)
  12886. (setq deltadays (string-to-number ans) ans ""))
  12887. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  12888. (when (string-match
  12889. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12890. (setq year (if (match-end 2)
  12891. (string-to-number (match-string 2 ans))
  12892. (string-to-number (format-time-string "%Y")))
  12893. month (string-to-number (match-string 3 ans))
  12894. day (string-to-number (match-string 4 ans)))
  12895. (if (< year 100) (setq year (+ 2000 year)))
  12896. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12897. t nil ans)))
  12898. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12899. ;; If there is a time with am/pm, and *no* time without it, we convert
  12900. ;; so that matching will be successful.
  12901. ;; FIXME: make this replace twoce, so that we catch the end time.
  12902. (when (and (not (string-match "[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12903. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12904. (setq hour (string-to-number (match-string 1 ans))
  12905. minute (if (match-end 3) (string-to-number (match-string 3 ans)) 0)
  12906. pm (equal ?p (string-to-char (downcase (match-string 4 ans)))))
  12907. (if (and (= hour 12) (not pm))
  12908. (setq hour 0)
  12909. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12910. (setq ans (replace-match (format "%02d:%02d" hour minute) t t ans)))
  12911. ;; Check if there is a time range
  12912. (when (and (boundp 'org-end-time-was-given)
  12913. (string-match org-plain-time-of-day-regexp ans)
  12914. (match-end 8))
  12915. (setq org-end-time-was-given (match-string 8 ans))
  12916. (setq ans (concat (substring ans 0 (match-beginning 7))
  12917. (substring ans (match-end 7)))))
  12918. (setq tl (parse-time-string ans)
  12919. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  12920. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  12921. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  12922. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  12923. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  12924. second (or (nth 0 tl) 0)
  12925. wday (nth 6 tl))
  12926. (setq day (+ day deltadays))
  12927. (when (and wday (not (nth 3 tl)))
  12928. ;; Weekday was given, but no day, so pick that day in the week
  12929. ;; on or after the derived date.
  12930. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12931. (unless (equal wday wday1)
  12932. (setq day (+ day (% (- wday wday1 -7) 7)))))
  12933. (if (and (boundp 'org-time-was-given)
  12934. (nth 2 tl))
  12935. (setq org-time-was-given t))
  12936. (if (< year 100) (setq year (+ 2000 year)))
  12937. (if to-time
  12938. (encode-time second minute hour day month year)
  12939. (if (or (nth 1 tl) (nth 2 tl))
  12940. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  12941. (format "%04d-%02d-%02d" year month day)))))
  12942. (defun org-eval-in-calendar (form &optional keepdate)
  12943. "Eval FORM in the calendar window and return to current window.
  12944. Also, store the cursor date in variable org-ans2."
  12945. (let ((sw (selected-window)))
  12946. (select-window (get-buffer-window "*Calendar*"))
  12947. (eval form)
  12948. (when (and (not keepdate) (calendar-cursor-to-date))
  12949. (let* ((date (calendar-cursor-to-date))
  12950. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12951. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12952. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12953. (select-window sw)
  12954. ;; Update the prompt to show new default date
  12955. (save-excursion
  12956. (goto-char (point-min))
  12957. (when (and org-ans2
  12958. (re-search-forward "\\[[-0-9]+\\]" nil t)
  12959. (get-text-property (match-end 0) 'field))
  12960. (let ((inhibit-read-only t))
  12961. (replace-match (concat "[" org-ans2 "]") t t)
  12962. (add-text-properties (point-min) (1+ (match-end 0))
  12963. (text-properties-at (1+ (point-min)))))))))
  12964. (defun org-calendar-select ()
  12965. "Return to `org-read-date' with the date currently selected.
  12966. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12967. (interactive)
  12968. (when (calendar-cursor-to-date)
  12969. (let* ((date (calendar-cursor-to-date))
  12970. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12971. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12972. (if (active-minibuffer-window) (exit-minibuffer))))
  12973. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12974. "Insert a date stamp for the date given by the internal TIME.
  12975. WITH-HM means, use the stamp format that includes the time of the day.
  12976. INACTIVE means use square brackets instead of angular ones, so that the
  12977. stamp will not contribute to the agenda.
  12978. PRE and POST are optional strings to be inserted before and after the
  12979. stamp.
  12980. The command returns the inserted time stamp."
  12981. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12982. stamp)
  12983. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12984. (insert (or pre ""))
  12985. (insert (setq stamp (format-time-string fmt time)))
  12986. (when (listp extra)
  12987. (setq extra (car extra))
  12988. (if (and (stringp extra)
  12989. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12990. (setq extra (format "-%02d:%02d"
  12991. (string-to-number (match-string 1 extra))
  12992. (string-to-number (match-string 2 extra))))
  12993. (setq extra nil)))
  12994. (when extra
  12995. (backward-char 1)
  12996. (insert extra)
  12997. (forward-char 1))
  12998. (insert (or post ""))
  12999. stamp))
  13000. (defun org-toggle-time-stamp-overlays ()
  13001. "Toggle the use of custom time stamp formats."
  13002. (interactive)
  13003. (setq org-display-custom-times (not org-display-custom-times))
  13004. (unless org-display-custom-times
  13005. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13006. (while (setq p (next-single-property-change p 'display))
  13007. (if (and (get-text-property p 'display)
  13008. (eq (get-text-property p 'face) 'org-date))
  13009. (remove-text-properties
  13010. p (setq p (next-single-property-change p 'display))
  13011. '(display t))))
  13012. (set-buffer-modified-p bmp)))
  13013. (if (featurep 'xemacs)
  13014. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13015. (org-restart-font-lock)
  13016. (setq org-table-may-need-update t)
  13017. (if org-display-custom-times
  13018. (message "Time stamps are overlayed with custom format")
  13019. (message "Time stamp overlays removed")))
  13020. (defun org-display-custom-time (beg end)
  13021. "Overlay modified time stamp format over timestamp between BED and END."
  13022. (let* ((ts (buffer-substring beg end))
  13023. t1 w1 with-hm tf time str w2 (off 0))
  13024. (save-match-data
  13025. (setq t1 (org-parse-time-string ts t))
  13026. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  13027. (setq off (- (match-end 0) (match-beginning 0)))))
  13028. (setq end (- end off))
  13029. (setq w1 (- end beg)
  13030. with-hm (and (nth 1 t1) (nth 2 t1))
  13031. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13032. time (org-fix-decoded-time t1)
  13033. str (org-add-props
  13034. (format-time-string
  13035. (substring tf 1 -1) (apply 'encode-time time))
  13036. nil 'mouse-face 'highlight)
  13037. w2 (length str))
  13038. (if (not (= w2 w1))
  13039. (add-text-properties (1+ beg) (+ 2 beg)
  13040. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13041. (if (featurep 'xemacs)
  13042. (progn
  13043. (put-text-property beg end 'invisible t)
  13044. (put-text-property beg end 'end-glyph (make-glyph str)))
  13045. (put-text-property beg end 'display str))))
  13046. (defun org-translate-time (string)
  13047. "Translate all timestamps in STRING to custom format.
  13048. But do this only if the variable `org-display-custom-times' is set."
  13049. (when org-display-custom-times
  13050. (save-match-data
  13051. (let* ((start 0)
  13052. (re org-ts-regexp-both)
  13053. t1 with-hm inactive tf time str beg end)
  13054. (while (setq start (string-match re string start))
  13055. (setq beg (match-beginning 0)
  13056. end (match-end 0)
  13057. t1 (save-match-data
  13058. (org-parse-time-string (substring string beg end) t))
  13059. with-hm (and (nth 1 t1) (nth 2 t1))
  13060. inactive (equal (substring string beg (1+ beg)) "[")
  13061. tf (funcall (if with-hm 'cdr 'car)
  13062. org-time-stamp-custom-formats)
  13063. time (org-fix-decoded-time t1)
  13064. str (format-time-string
  13065. (concat
  13066. (if inactive "[" "<") (substring tf 1 -1)
  13067. (if inactive "]" ">"))
  13068. (apply 'encode-time time))
  13069. string (replace-match str t t string)
  13070. start (+ start (length str)))))))
  13071. string)
  13072. (defun org-fix-decoded-time (time)
  13073. "Set 0 instead of nil for the first 6 elements of time.
  13074. Don't touch the rest."
  13075. (let ((n 0))
  13076. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13077. (defun org-days-to-time (timestamp-string)
  13078. "Difference between TIMESTAMP-STRING and now in days."
  13079. (- (time-to-days (org-time-string-to-time timestamp-string))
  13080. (time-to-days (current-time))))
  13081. (defun org-deadline-close (timestamp-string &optional ndays)
  13082. "Is the time in TIMESTAMP-STRING close to the current date?"
  13083. (and (< (org-days-to-time timestamp-string)
  13084. (or ndays org-deadline-warning-days))
  13085. (not (org-entry-is-done-p))))
  13086. (defun org-calendar-select-mouse (ev)
  13087. "Return to `org-read-date' with the date currently selected.
  13088. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13089. (interactive "e")
  13090. (mouse-set-point ev)
  13091. (when (calendar-cursor-to-date)
  13092. (let* ((date (calendar-cursor-to-date))
  13093. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13094. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13095. (if (active-minibuffer-window) (exit-minibuffer))))
  13096. (defun org-check-deadlines (ndays)
  13097. "Check if there are any deadlines due or past due.
  13098. A deadline is considered due if it happens within `org-deadline-warning-days'
  13099. days from today's date. If the deadline appears in an entry marked DONE,
  13100. it is not shown. The prefix arg NDAYS can be used to test that many
  13101. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13102. (interactive "P")
  13103. (let* ((org-warn-days
  13104. (cond
  13105. ((equal ndays '(4)) 100000)
  13106. (ndays (prefix-numeric-value ndays))
  13107. (t org-deadline-warning-days)))
  13108. (case-fold-search nil)
  13109. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13110. (callback
  13111. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13112. (message "%d deadlines past-due or due within %d days"
  13113. (org-occur regexp nil callback)
  13114. org-warn-days)))
  13115. (defun org-evaluate-time-range (&optional to-buffer)
  13116. "Evaluate a time range by computing the difference between start and end.
  13117. Normally the result is just printed in the echo area, but with prefix arg
  13118. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13119. If the time range is actually in a table, the result is inserted into the
  13120. next column.
  13121. For time difference computation, a year is assumed to be exactly 365
  13122. days in order to avoid rounding problems."
  13123. (interactive "P")
  13124. (or
  13125. (org-clock-update-time-maybe)
  13126. (save-excursion
  13127. (unless (org-at-date-range-p)
  13128. (goto-char (point-at-bol))
  13129. (re-search-forward org-tr-regexp (point-at-eol) t))
  13130. (if (not (org-at-date-range-p))
  13131. (error "Not at a time-stamp range, and none found in current line")))
  13132. (let* ((ts1 (match-string 1))
  13133. (ts2 (match-string 2))
  13134. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13135. (match-end (match-end 0))
  13136. (time1 (org-time-string-to-time ts1))
  13137. (time2 (org-time-string-to-time ts2))
  13138. (t1 (time-to-seconds time1))
  13139. (t2 (time-to-seconds time2))
  13140. (diff (abs (- t2 t1)))
  13141. (negative (< (- t2 t1) 0))
  13142. ;; (ys (floor (* 365 24 60 60)))
  13143. (ds (* 24 60 60))
  13144. (hs (* 60 60))
  13145. (fy "%dy %dd %02d:%02d")
  13146. (fy1 "%dy %dd")
  13147. (fd "%dd %02d:%02d")
  13148. (fd1 "%dd")
  13149. (fh "%02d:%02d")
  13150. y d h m align)
  13151. (if havetime
  13152. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13153. y 0
  13154. d (floor (/ diff ds)) diff (mod diff ds)
  13155. h (floor (/ diff hs)) diff (mod diff hs)
  13156. m (floor (/ diff 60)))
  13157. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13158. y 0
  13159. d (floor (+ (/ diff ds) 0.5))
  13160. h 0 m 0))
  13161. (if (not to-buffer)
  13162. (message (org-make-tdiff-string y d h m))
  13163. (when (org-at-table-p)
  13164. (goto-char match-end)
  13165. (setq align t)
  13166. (and (looking-at " *|") (goto-char (match-end 0))))
  13167. (if (looking-at
  13168. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13169. (replace-match ""))
  13170. (if negative (insert " -"))
  13171. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13172. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13173. (insert " " (format fh h m))))
  13174. (if align (org-table-align))
  13175. (message "Time difference inserted")))))
  13176. (defun org-make-tdiff-string (y d h m)
  13177. (let ((fmt "")
  13178. (l nil))
  13179. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13180. l (push y l)))
  13181. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13182. l (push d l)))
  13183. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13184. l (push h l)))
  13185. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13186. l (push m l)))
  13187. (apply 'format fmt (nreverse l))))
  13188. (defun org-time-string-to-time (s)
  13189. (apply 'encode-time (org-parse-time-string s)))
  13190. (defun org-time-string-to-absolute (s &optional daynr)
  13191. "Convert a time stamp to an absolute day number.
  13192. If there is a specifyer for a cyclic time stamp, get the closest date to
  13193. DATE."
  13194. (cond
  13195. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13196. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13197. daynr
  13198. (+ daynr 1000)))
  13199. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13200. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13201. (time-to-days (current-time))) (match-string 0 s)))
  13202. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13203. (defun org-calendar-holiday ()
  13204. "List of holidays, for Diary display in Org-mode."
  13205. (let ((hl (check-calendar-holidays date)))
  13206. (if hl (mapconcat 'identity hl "; "))))
  13207. (defun org-diary-sexp-entry (sexp entry date)
  13208. "Process a SEXP diary ENTRY for DATE."
  13209. (let ((result (if calendar-debug-sexp
  13210. (let ((stack-trace-on-error t))
  13211. (eval (car (read-from-string sexp))))
  13212. (condition-case nil
  13213. (eval (car (read-from-string sexp)))
  13214. (error
  13215. (beep)
  13216. (message "Bad sexp at line %d in %s: %s"
  13217. (org-current-line)
  13218. (buffer-file-name) sexp)
  13219. (sleep-for 2))))))
  13220. (cond ((stringp result) result)
  13221. ((and (consp result)
  13222. (stringp (cdr result))) (cdr result))
  13223. (result entry)
  13224. (t nil))))
  13225. (defun org-diary-to-ical-string (frombuf)
  13226. "FIXME"
  13227. (let* ((tmpdir (if (featurep 'xemacs)
  13228. (temp-directory)
  13229. temporary-file-directory))
  13230. (tmpfile (make-temp-name
  13231. (expand-file-name "orgics" tmpdir)))
  13232. buf rtn b e)
  13233. (save-excursion
  13234. (set-buffer frombuf)
  13235. (icalendar-export-region (point-min) (point-max) tmpfile)
  13236. (setq buf (find-buffer-visiting tmpfile))
  13237. (set-buffer buf)
  13238. (goto-char (point-min))
  13239. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13240. (setq b (match-beginning 0)))
  13241. (goto-char (point-max))
  13242. (if (re-search-backward "^END:VEVENT" nil t)
  13243. (setq e (match-end 0)))
  13244. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13245. (kill-buffer buf)
  13246. (kill-buffer frombuf)
  13247. (delete-file tmpfile)
  13248. rtn))
  13249. (defun org-closest-date (start current change)
  13250. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  13251. ;; Make the proper lists from the dates
  13252. (catch 'exit
  13253. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13254. dn dw sday cday n1 n2
  13255. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13256. (setq start (org-date-to-gregorian start)
  13257. current (org-date-to-gregorian current)
  13258. sday (calendar-absolute-from-gregorian start)
  13259. cday (calendar-absolute-from-gregorian current))
  13260. (if (<= cday sday) (throw 'exit sday))
  13261. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13262. (setq dn (string-to-number (match-string 1 change))
  13263. dw (cdr (assoc (match-string 2 change) a1)))
  13264. (error "Invalid change specifyer: %s" change))
  13265. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13266. (cond
  13267. ((eq dw 'day)
  13268. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13269. n2 (+ n1 dn)))
  13270. ((eq dw 'year)
  13271. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13272. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13273. (setq date1 (list m d y1)
  13274. n1 (calendar-absolute-from-gregorian date1)
  13275. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13276. n2 (calendar-absolute-from-gregorian date2)))
  13277. ((eq dw 'month)
  13278. ;; approx number of month between the tow dates
  13279. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13280. ;; How often does dn fit in there?
  13281. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13282. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13283. m (+ m nm)
  13284. ny (floor (/ m 12))
  13285. y (+ y ny)
  13286. m (- m (* ny 12)))
  13287. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13288. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13289. (setq m2 (+ m dn) y2 y)
  13290. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13291. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13292. (while (< n2 cday)
  13293. (setq n1 n2 m m2 y y2)
  13294. (setq m2 (+ m dn) y2 y)
  13295. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13296. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13297. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1))))
  13298. (defun org-date-to-gregorian (date)
  13299. "Turn any specification of DATE into a gregorian date for the calendar."
  13300. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13301. ((and (listp date) (= (length date) 3)) date)
  13302. ((stringp date)
  13303. (setq date (org-parse-time-string date))
  13304. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13305. ((listp date)
  13306. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13307. (defun org-parse-time-string (s &optional nodefault)
  13308. "Parse the standard Org-mode time string.
  13309. This should be a lot faster than the normal `parse-time-string'.
  13310. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13311. hour and minute fields will be nil if not given."
  13312. (if (string-match org-ts-regexp0 s)
  13313. (list 0
  13314. (if (or (match-beginning 8) (not nodefault))
  13315. (string-to-number (or (match-string 8 s) "0")))
  13316. (if (or (match-beginning 7) (not nodefault))
  13317. (string-to-number (or (match-string 7 s) "0")))
  13318. (string-to-number (match-string 4 s))
  13319. (string-to-number (match-string 3 s))
  13320. (string-to-number (match-string 2 s))
  13321. nil nil nil)
  13322. (make-list 9 0)))
  13323. (defun org-timestamp-up (&optional arg)
  13324. "Increase the date item at the cursor by one.
  13325. If the cursor is on the year, change the year. If it is on the month or
  13326. the day, change that.
  13327. With prefix ARG, change by that many units."
  13328. (interactive "p")
  13329. (org-timestamp-change (prefix-numeric-value arg)))
  13330. (defun org-timestamp-down (&optional arg)
  13331. "Decrease the date item at the cursor by one.
  13332. If the cursor is on the year, change the year. If it is on the month or
  13333. the day, change that.
  13334. With prefix ARG, change by that many units."
  13335. (interactive "p")
  13336. (org-timestamp-change (- (prefix-numeric-value arg))))
  13337. (defun org-timestamp-up-day (&optional arg)
  13338. "Increase the date in the time stamp by one day.
  13339. With prefix ARG, change that many days."
  13340. (interactive "p")
  13341. (if (and (not (org-at-timestamp-p t))
  13342. (org-on-heading-p))
  13343. (org-todo 'up)
  13344. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13345. (defun org-timestamp-down-day (&optional arg)
  13346. "Decrease the date in the time stamp by one day.
  13347. With prefix ARG, change that many days."
  13348. (interactive "p")
  13349. (if (and (not (org-at-timestamp-p t))
  13350. (org-on-heading-p))
  13351. (org-todo 'down)
  13352. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13353. (defsubst org-pos-in-match-range (pos n)
  13354. (and (match-beginning n)
  13355. (<= (match-beginning n) pos)
  13356. (>= (match-end n) pos)))
  13357. (defun org-at-timestamp-p (&optional inactive-ok)
  13358. "Determine if the cursor is in or at a timestamp."
  13359. (interactive)
  13360. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13361. (pos (point))
  13362. (ans (or (looking-at tsr)
  13363. (save-excursion
  13364. (skip-chars-backward "^[<\n\r\t")
  13365. (if (> (point) 1) (backward-char 1))
  13366. (and (looking-at tsr)
  13367. (> (- (match-end 0) pos) -1))))))
  13368. (and (boundp 'org-ts-what)
  13369. (setq org-ts-what
  13370. (cond
  13371. ((org-pos-in-match-range pos 2) 'year)
  13372. ((org-pos-in-match-range pos 3) 'month)
  13373. ((org-pos-in-match-range pos 7) 'hour)
  13374. ((org-pos-in-match-range pos 8) 'minute)
  13375. ((or (org-pos-in-match-range pos 4)
  13376. (org-pos-in-match-range pos 5)) 'day)
  13377. ((and (> pos (or (match-end 8) (match-end 5)))
  13378. (< pos (match-end 0)))
  13379. (- pos (or (match-end 8) (match-end 5))))
  13380. (t 'day))))
  13381. ans))
  13382. (defun org-timestamp-change (n &optional what)
  13383. "Change the date in the time stamp at point.
  13384. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13385. `year', `minute', `second'. If WHAT is not given, the cursor position
  13386. in the timestamp determines what will be changed."
  13387. (let ((pos (point))
  13388. with-hm inactive
  13389. org-ts-what
  13390. extra
  13391. ts time time0)
  13392. (if (not (org-at-timestamp-p t))
  13393. (error "Not at a timestamp"))
  13394. (if (and (not what) (not (eq org-ts-what 'day))
  13395. org-display-custom-times
  13396. (get-text-property (point) 'display)
  13397. (not (get-text-property (1- (point)) 'display)))
  13398. (setq org-ts-what 'day))
  13399. (setq org-ts-what (or what org-ts-what)
  13400. inactive (= (char-after (match-beginning 0)) ?\[)
  13401. ts (match-string 0))
  13402. (replace-match "")
  13403. (if (string-match
  13404. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( \\+[0-9]+[dwmy]\\)?\\)[]>]"
  13405. ts)
  13406. (setq extra (match-string 1 ts)))
  13407. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13408. (setq with-hm t))
  13409. (setq time0 (org-parse-time-string ts))
  13410. (setq time
  13411. (apply 'encode-time
  13412. (append
  13413. (list (or (car time0) 0))
  13414. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  13415. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  13416. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  13417. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  13418. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  13419. (nthcdr 6 time0))))
  13420. (when (integerp org-ts-what)
  13421. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  13422. (if (eq what 'calendar)
  13423. (let ((cal-date
  13424. (save-excursion
  13425. (save-match-data
  13426. (set-buffer "*Calendar*")
  13427. (calendar-cursor-to-date)))))
  13428. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13429. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13430. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13431. (setcar time0 (or (car time0) 0))
  13432. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13433. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  13434. (setq time (apply 'encode-time time0))))
  13435. (setq org-last-changed-timestamp
  13436. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13437. (org-clock-update-time-maybe)
  13438. (goto-char pos)
  13439. ;; Try to recenter the calendar window, if any
  13440. (if (and org-calendar-follow-timestamp-change
  13441. (get-buffer-window "*Calendar*" t)
  13442. (memq org-ts-what '(day month year)))
  13443. (org-recenter-calendar (time-to-days time)))))
  13444. (defun org-modify-ts-extra (s pos n)
  13445. "FIXME"
  13446. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13447. ng h m new)
  13448. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13449. (cond
  13450. ((or (org-pos-in-match-range pos 2)
  13451. (org-pos-in-match-range pos 3))
  13452. (setq m (string-to-number (match-string 3 s))
  13453. h (string-to-number (match-string 2 s)))
  13454. (if (org-pos-in-match-range pos 2)
  13455. (setq h (+ h n))
  13456. (setq m (+ m n)))
  13457. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13458. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13459. (setq h (min 24 (max 0 h)))
  13460. (setq ng 1 new (format "-%02d:%02d" h m)))
  13461. ((org-pos-in-match-range pos 6)
  13462. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13463. ((org-pos-in-match-range pos 5)
  13464. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  13465. (when ng
  13466. (setq s (concat
  13467. (substring s 0 (match-beginning ng))
  13468. new
  13469. (substring s (match-end ng))))))
  13470. s))
  13471. (defun org-recenter-calendar (date)
  13472. "If the calendar is visible, recenter it to DATE."
  13473. (let* ((win (selected-window))
  13474. (cwin (get-buffer-window "*Calendar*" t))
  13475. (calendar-move-hook nil))
  13476. (when cwin
  13477. (select-window cwin)
  13478. (calendar-goto-date (if (listp date) date
  13479. (calendar-gregorian-from-absolute date)))
  13480. (select-window win))))
  13481. (defun org-goto-calendar (&optional arg)
  13482. "Go to the Emacs calendar at the current date.
  13483. If there is a time stamp in the current line, go to that date.
  13484. A prefix ARG can be used to force the current date."
  13485. (interactive "P")
  13486. (let ((tsr org-ts-regexp) diff
  13487. (calendar-move-hook nil)
  13488. (view-calendar-holidays-initially nil)
  13489. (view-diary-entries-initially nil))
  13490. (if (or (org-at-timestamp-p)
  13491. (save-excursion
  13492. (beginning-of-line 1)
  13493. (looking-at (concat ".*" tsr))))
  13494. (let ((d1 (time-to-days (current-time)))
  13495. (d2 (time-to-days
  13496. (org-time-string-to-time (match-string 1)))))
  13497. (setq diff (- d2 d1))))
  13498. (calendar)
  13499. (calendar-goto-today)
  13500. (if (and diff (not arg)) (calendar-forward-day diff))))
  13501. (defun org-date-from-calendar ()
  13502. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13503. If there is already a time stamp at the cursor position, update it."
  13504. (interactive)
  13505. (org-timestamp-change 0 'calendar))
  13506. ;;; The clock for measuring work time.
  13507. (defvar org-mode-line-string "")
  13508. (put 'org-mode-line-string 'risky-local-variable t)
  13509. (defvar org-mode-line-timer nil)
  13510. (defvar org-clock-heading "")
  13511. (defvar org-clock-start-time "")
  13512. (defun org-update-mode-line ()
  13513. (let* ((delta (- (time-to-seconds (current-time))
  13514. (time-to-seconds org-clock-start-time)))
  13515. (h (floor delta 3600))
  13516. (m (floor (- delta (* 3600 h)) 60)))
  13517. (setq org-mode-line-string
  13518. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  13519. 'help-echo "Org-mode clock is running"))
  13520. (force-mode-line-update)))
  13521. (defvar org-clock-marker (make-marker)
  13522. "Marker recording the last clock-in.")
  13523. (defvar org-clock-mode-line-entry nil
  13524. "Information for the modeline about the running clock.")
  13525. (defun org-clock-in ()
  13526. "Start the clock on the current item.
  13527. If necessary, clock-out of the currently active clock."
  13528. (interactive)
  13529. (org-clock-out t)
  13530. (let (ts)
  13531. (save-excursion
  13532. (org-back-to-heading t)
  13533. (if (looking-at org-todo-line-regexp)
  13534. (setq org-clock-heading (match-string 3))
  13535. (setq org-clock-heading "???"))
  13536. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  13537. (beginning-of-line 2)
  13538. (when (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  13539. (not (equal (match-string 1) org-clock-string)))
  13540. ;; First line hast scheduling info, move one further
  13541. (beginning-of-line 2)
  13542. (or (bolp) (newline)))
  13543. (insert "\n") (backward-char 1)
  13544. (indent-relative)
  13545. (insert org-clock-string " ")
  13546. (setq org-clock-start-time (current-time))
  13547. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  13548. (move-marker org-clock-marker (point) (buffer-base-buffer))
  13549. (or global-mode-string (setq global-mode-string '("")))
  13550. (or (memq 'org-mode-line-string global-mode-string)
  13551. (setq global-mode-string
  13552. (append global-mode-string '(org-mode-line-string))))
  13553. (org-update-mode-line)
  13554. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  13555. (message "Clock started at %s" ts))))
  13556. (defun org-clock-out (&optional fail-quietly)
  13557. "Stop the currently running clock.
  13558. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  13559. (interactive)
  13560. (catch 'exit
  13561. (if (not (marker-buffer org-clock-marker))
  13562. (if fail-quietly (throw 'exit t) (error "No active clock")))
  13563. (let (ts te s h m)
  13564. (save-excursion
  13565. (set-buffer (marker-buffer org-clock-marker))
  13566. (goto-char org-clock-marker)
  13567. (beginning-of-line 1)
  13568. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  13569. (equal (match-string 1) org-clock-string))
  13570. (setq ts (match-string 2))
  13571. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  13572. (goto-char (match-end 0))
  13573. (delete-region (point) (point-at-eol))
  13574. (insert "--")
  13575. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  13576. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  13577. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  13578. h (floor (/ s 3600))
  13579. s (- s (* 3600 h))
  13580. m (floor (/ s 60))
  13581. s (- s (* 60 s)))
  13582. (insert " => " (format "%2d:%02d" h m))
  13583. (move-marker org-clock-marker nil)
  13584. (org-add-log-maybe 'clock-out)
  13585. (when org-mode-line-timer
  13586. (cancel-timer org-mode-line-timer)
  13587. (setq org-mode-line-timer nil))
  13588. (setq global-mode-string
  13589. (delq 'org-mode-line-string global-mode-string))
  13590. (force-mode-line-update)
  13591. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  13592. (defun org-clock-cancel ()
  13593. "Cancel the running clock be removing the start timestamp."
  13594. (interactive)
  13595. (if (not (marker-buffer org-clock-marker))
  13596. (error "No active clock"))
  13597. (save-excursion
  13598. (set-buffer (marker-buffer org-clock-marker))
  13599. (goto-char org-clock-marker)
  13600. (delete-region (1- (point-at-bol)) (point-at-eol)))
  13601. (message "Clock canceled"))
  13602. (defvar org-clock-file-total-minutes nil
  13603. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  13604. (make-variable-buffer-local 'org-clock-file-total-minutes)
  13605. (defun org-clock-sum (&optional tstart tend)
  13606. "Sum the times for each subtree.
  13607. Puts the resulting times in minutes as a text property on each headline."
  13608. (interactive)
  13609. (let* ((bmp (buffer-modified-p))
  13610. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  13611. org-clock-string
  13612. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  13613. (lmax 30)
  13614. (ltimes (make-vector lmax 0))
  13615. (t1 0)
  13616. (level 0)
  13617. ts te dt
  13618. time)
  13619. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  13620. (save-excursion
  13621. (goto-char (point-max))
  13622. (while (re-search-backward re nil t)
  13623. (cond
  13624. ((match-end 2)
  13625. ;; Two time stamps
  13626. (setq ts (match-string 2)
  13627. te (match-string 3)
  13628. ts (time-to-seconds
  13629. (apply 'encode-time (org-parse-time-string ts)))
  13630. te (time-to-seconds
  13631. (apply 'encode-time (org-parse-time-string te)))
  13632. ts (if tstart (max ts tstart) ts)
  13633. te (if tend (min te tend) te)
  13634. dt (- te ts)
  13635. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  13636. ((match-end 4)
  13637. ;; A naket time
  13638. (setq t1 (+ t1 (string-to-number (match-string 5))
  13639. (* 60 (string-to-number (match-string 4))))))
  13640. (t ;; A headline
  13641. (setq level (- (match-end 1) (match-beginning 1)))
  13642. (when (or (> t1 0) (> (aref ltimes level) 0))
  13643. (loop for l from 0 to level do
  13644. (aset ltimes l (+ (aref ltimes l) t1)))
  13645. (setq t1 0 time (aref ltimes level))
  13646. (loop for l from level to (1- lmax) do
  13647. (aset ltimes l 0))
  13648. (goto-char (match-beginning 0))
  13649. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  13650. (setq org-clock-file-total-minutes (aref ltimes 0)))
  13651. (set-buffer-modified-p bmp)))
  13652. (defun org-clock-display (&optional total-only)
  13653. "Show subtree times in the entire buffer.
  13654. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  13655. in the echo area."
  13656. (interactive)
  13657. (org-remove-clock-overlays)
  13658. (let (time h m p)
  13659. (org-clock-sum)
  13660. (unless total-only
  13661. (save-excursion
  13662. (goto-char (point-min))
  13663. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  13664. (goto-char p)
  13665. (when (setq time (get-text-property p :org-clock-minutes))
  13666. (org-put-clock-overlay time (funcall outline-level))))
  13667. (setq h (/ org-clock-file-total-minutes 60)
  13668. m (- org-clock-file-total-minutes (* 60 h)))
  13669. ;; Arrange to remove the overlays upon next change.
  13670. (when org-remove-highlights-with-change
  13671. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  13672. nil 'local))))
  13673. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  13674. (defvar org-clock-overlays nil)
  13675. (make-variable-buffer-local 'org-clock-overlays)
  13676. (defun org-put-clock-overlay (time &optional level)
  13677. "Put an overlays on the current line, displaying TIME.
  13678. If LEVEL is given, prefix time with a corresponding number of stars.
  13679. This creates a new overlay and stores it in `org-clock-overlays', so that it
  13680. will be easy to remove."
  13681. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  13682. (l (if level (org-get-legal-level level 0) 0))
  13683. (off 0)
  13684. ov tx)
  13685. (move-to-column c)
  13686. (unless (eolp) (skip-chars-backward "^ \t"))
  13687. (skip-chars-backward " \t")
  13688. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  13689. tx (concat (buffer-substring (1- (point)) (point))
  13690. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  13691. (org-add-props (format "%s %2d:%02d%s"
  13692. (make-string l ?*) h m
  13693. (make-string (- 10 l) ?\ ))
  13694. '(face secondary-selection))
  13695. ""))
  13696. (if (not (featurep 'xemacs))
  13697. (org-overlay-put ov 'display tx)
  13698. (org-overlay-put ov 'invisible t)
  13699. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  13700. (push ov org-clock-overlays)))
  13701. (defun org-remove-clock-overlays (&optional beg end noremove)
  13702. "Remove the occur highlights from the buffer.
  13703. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13704. from the `before-change-functions' in the current buffer."
  13705. (interactive)
  13706. (unless org-inhibit-highlight-removal
  13707. (mapc 'org-delete-overlay org-clock-overlays)
  13708. (setq org-clock-overlays nil)
  13709. (unless noremove
  13710. (remove-hook 'before-change-functions
  13711. 'org-remove-clock-overlays 'local))))
  13712. (defun org-clock-out-if-current ()
  13713. "Clock out if the current entry contains the running clock.
  13714. This is used to stop the clock after a TODO entry is marked DONE."
  13715. (when (and (member state org-done-keywords)
  13716. (equal (marker-buffer org-clock-marker) (current-buffer))
  13717. (< (point) org-clock-marker)
  13718. (> (save-excursion (outline-next-heading) (point))
  13719. org-clock-marker))
  13720. ;; Clock out, but don't accept a logging message for this.
  13721. (let ((org-log-done (if (and (listp org-log-done)
  13722. (member 'clock-out org-log-done))
  13723. '(done)
  13724. org-log-done)))
  13725. (org-clock-out))))
  13726. (add-hook 'org-after-todo-state-change-hook
  13727. 'org-clock-out-if-current)
  13728. (defun org-check-running-clock ()
  13729. "Check if the current buffer contains the running clock.
  13730. If yes, offer to stop it and to save the buffer with the changes."
  13731. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  13732. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  13733. (buffer-name))))
  13734. (org-clock-out)
  13735. (when (y-or-n-p "Save changed buffer?")
  13736. (save-buffer))))
  13737. (defun org-clock-report ()
  13738. "Create a table containing a report about clocked time.
  13739. If the buffer contains lines
  13740. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  13741. #+END: clocktable
  13742. then the table will be inserted between these lines, replacing whatever
  13743. is was there before. If these lines are not in the buffer, the table
  13744. is inserted at point, surrounded by the special lines.
  13745. The BEGIN line can contain parameters. Allowed are:
  13746. :maxlevel The maximum level to be included in the table. Default is 3.
  13747. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  13748. (interactive)
  13749. (org-remove-clock-overlays)
  13750. (unless (org-find-dblock "clocktable")
  13751. (org-create-dblock (list :name "clocktable"
  13752. :maxlevel 2 :emphasize nil)))
  13753. (org-update-dblock))
  13754. (defun org-clock-update-time-maybe ()
  13755. "If this is a CLOCK line, update it and return t.
  13756. Otherwise, return nil."
  13757. (interactive)
  13758. (save-excursion
  13759. (beginning-of-line 1)
  13760. (skip-chars-forward " \t")
  13761. (when (looking-at org-clock-string)
  13762. (let ((re (concat "[ \t]*" org-clock-string
  13763. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  13764. "\\([ \t]*=>.*\\)?"))
  13765. ts te h m s)
  13766. (if (not (looking-at re))
  13767. nil
  13768. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  13769. (end-of-line 1)
  13770. (setq ts (match-string 1)
  13771. te (match-string 2))
  13772. (setq s (- (time-to-seconds
  13773. (apply 'encode-time (org-parse-time-string te)))
  13774. (time-to-seconds
  13775. (apply 'encode-time (org-parse-time-string ts))))
  13776. h (floor (/ s 3600))
  13777. s (- s (* 3600 h))
  13778. m (floor (/ s 60))
  13779. s (- s (* 60 s)))
  13780. (insert " => " (format "%2d:%02d" h m))
  13781. t)))))
  13782. (defun org-clock-special-range (key &optional time as-strings)
  13783. "Return two times bordering a special time range.
  13784. Key is a symbol specifying the range and can be one of `today', `yesterday',
  13785. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  13786. A week starts Monday 0:00 and ends Sunday 24:00.
  13787. The range is determined relative to TIME. TIME defaults to the current time.
  13788. The return value is a cons cell with two internal times like the ones
  13789. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  13790. the returned times will be formatted strings."
  13791. (let* ((tm (decode-time (or time (current-time))))
  13792. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  13793. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  13794. (dow (nth 6 tm))
  13795. s1 m1 h1 d1 month1 y1 diff ts te fm)
  13796. (cond
  13797. ((eq key 'today)
  13798. (setq h 0 m 0 h1 24 m1 0))
  13799. ((eq key 'yesterday)
  13800. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  13801. ((eq key 'thisweek)
  13802. (setq diff (if (= dow 0) 6 (1- dow))
  13803. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  13804. ((eq key 'lastweek)
  13805. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  13806. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  13807. ((eq key 'thismonth)
  13808. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  13809. ((eq key 'lastmonth)
  13810. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  13811. ((eq key 'thisyear)
  13812. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  13813. ((eq key 'lastyear)
  13814. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  13815. (t (error "No such time block %s" key)))
  13816. (setq ts (encode-time s m h d month y)
  13817. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  13818. (or d1 d) (or month1 month) (or y1 y)))
  13819. (setq fm (cdr org-time-stamp-formats))
  13820. (if as-strings
  13821. (cons (format-time-string fm ts) (format-time-string fm te))
  13822. (cons ts te))))
  13823. (defun org-dblock-write:clocktable (params)
  13824. "Write the standard clocktable."
  13825. (let ((hlchars '((1 . "*") (2 . ?/)))
  13826. (emph nil)
  13827. (ins (make-marker))
  13828. ipos time h m p level hlc hdl maxlevel
  13829. ts te cc block)
  13830. (setq maxlevel (or (plist-get params :maxlevel) 3)
  13831. emph (plist-get params :emphasize)
  13832. ts (plist-get params :tstart)
  13833. te (plist-get params :tend)
  13834. block (plist-get params :block))
  13835. (when block
  13836. (setq cc (org-clock-special-range block nil t)
  13837. ts (car cc) te (cdr cc)))
  13838. (if ts (setq ts (time-to-seconds
  13839. (apply 'encode-time (org-parse-time-string ts)))))
  13840. (if te (setq te (time-to-seconds
  13841. (apply 'encode-time (org-parse-time-string te)))))
  13842. (move-marker ins (point))
  13843. (setq ipos (point))
  13844. (insert-before-markers "Clock summary at ["
  13845. (substring
  13846. (format-time-string (cdr org-time-stamp-formats))
  13847. 1 -1)
  13848. "]."
  13849. (if block
  13850. (format " Considered range is /%s/." block)
  13851. "")
  13852. "\n\n|L|Headline|Time|\n")
  13853. (org-clock-sum ts te)
  13854. (setq h (/ org-clock-file-total-minutes 60)
  13855. m (- org-clock-file-total-minutes (* 60 h)))
  13856. (insert-before-markers "|-\n|0|" "*Total file time*| "
  13857. (format "*%d:%02d*" h m)
  13858. "|\n")
  13859. (goto-char (point-min))
  13860. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  13861. (goto-char p)
  13862. (when (setq time (get-text-property p :org-clock-minutes))
  13863. (save-excursion
  13864. (beginning-of-line 1)
  13865. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  13866. (setq level (- (match-end 1) (match-beginning 1)))
  13867. (<= level maxlevel))
  13868. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  13869. hdl (match-string 2)
  13870. h (/ time 60)
  13871. m (- time (* 60 h)))
  13872. (goto-char ins)
  13873. (if (= level 1) (insert-before-markers "|-\n"))
  13874. (insert-before-markers
  13875. "| " (int-to-string level) "|" hlc hdl hlc " |"
  13876. (make-string (1- level) ?|)
  13877. hlc
  13878. (format "%d:%02d" h m)
  13879. hlc
  13880. " |\n")))))
  13881. (goto-char ins)
  13882. (backward-delete-char 1)
  13883. (goto-char ipos)
  13884. (skip-chars-forward "^|")
  13885. (org-table-align)))
  13886. ;; FIXME: I don't think anybody uses this, ask David
  13887. (defun org-collect-clock-time-entries ()
  13888. "Return an internal list with clocking information.
  13889. This list has one entry for each CLOCK interval.
  13890. FIXME: describe the elements."
  13891. (interactive)
  13892. (let ((re (concat "^[ \t]*" org-clock-string
  13893. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  13894. rtn beg end next cont level title total closedp leafp
  13895. clockpos titlepos h m donep)
  13896. (save-excursion
  13897. (org-clock-sum)
  13898. (goto-char (point-min))
  13899. (while (re-search-forward re nil t)
  13900. (setq clockpos (match-beginning 0)
  13901. beg (match-string 1) end (match-string 2)
  13902. cont (match-end 0))
  13903. (setq beg (apply 'encode-time (org-parse-time-string beg))
  13904. end (apply 'encode-time (org-parse-time-string end)))
  13905. (org-back-to-heading t)
  13906. (setq donep (org-entry-is-done-p))
  13907. (setq titlepos (point)
  13908. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  13909. h (/ total 60) m (- total (* 60 h))
  13910. total (cons h m))
  13911. (looking-at "\\(\\*+\\) +\\(.*\\)")
  13912. (setq level (- (match-end 1) (match-beginning 1))
  13913. title (org-match-string-no-properties 2))
  13914. (save-excursion (outline-next-heading) (setq next (point)))
  13915. (setq closedp (re-search-forward org-closed-time-regexp next t))
  13916. (goto-char next)
  13917. (setq leafp (and (looking-at "^\\*+ ")
  13918. (<= (- (match-end 0) (point)) level)))
  13919. (push (list beg end clockpos closedp donep
  13920. total title titlepos level leafp)
  13921. rtn)
  13922. (goto-char cont)))
  13923. (nreverse rtn)))
  13924. ;;;; Agenda, and Diary Integration
  13925. ;;; Define the Org-agenda-mode
  13926. (defvar org-agenda-mode-map (make-sparse-keymap)
  13927. "Keymap for `org-agenda-mode'.")
  13928. (defvar org-agenda-menu) ; defined later in this file.
  13929. (defvar org-agenda-follow-mode nil)
  13930. (defvar org-agenda-show-log nil)
  13931. (defvar org-agenda-redo-command nil)
  13932. (defvar org-agenda-mode-hook nil)
  13933. (defvar org-agenda-type nil)
  13934. (defvar org-agenda-force-single-file nil)
  13935. (defun org-agenda-mode ()
  13936. "Mode for time-sorted view on action items in Org-mode files.
  13937. The following commands are available:
  13938. \\{org-agenda-mode-map}"
  13939. (interactive)
  13940. (kill-all-local-variables)
  13941. (setq org-agenda-undo-list nil
  13942. org-agenda-pending-undo-list nil)
  13943. (setq major-mode 'org-agenda-mode)
  13944. ;; Keep global-font-lock-mode from turning on font-lock-mode
  13945. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  13946. (setq mode-name "Org-Agenda")
  13947. (use-local-map org-agenda-mode-map)
  13948. (easy-menu-add org-agenda-menu)
  13949. (if org-startup-truncated (setq truncate-lines t))
  13950. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  13951. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  13952. ;; Make sure properties are removed when copying text
  13953. (when (boundp 'buffer-substring-filters)
  13954. (org-set-local 'buffer-substring-filters
  13955. (cons (lambda (x)
  13956. (set-text-properties 0 (length x) nil x) x)
  13957. buffer-substring-filters)))
  13958. (unless org-agenda-keep-modes
  13959. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  13960. org-agenda-show-log nil))
  13961. (easy-menu-change
  13962. '("Agenda") "Agenda Files"
  13963. (append
  13964. (list
  13965. (vector
  13966. (if (get 'org-agenda-files 'org-restrict)
  13967. "Restricted to single file"
  13968. "Edit File List")
  13969. '(org-edit-agenda-file-list)
  13970. (not (get 'org-agenda-files 'org-restrict)))
  13971. "--")
  13972. (mapcar 'org-file-menu-entry (org-agenda-files))))
  13973. (org-agenda-set-mode-name)
  13974. (apply
  13975. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  13976. (list 'org-agenda-mode-hook)))
  13977. (substitute-key-definition 'undo 'org-agenda-undo
  13978. org-agenda-mode-map global-map)
  13979. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  13980. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  13981. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  13982. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  13983. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  13984. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  13985. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  13986. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  13987. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  13988. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  13989. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  13990. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  13991. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  13992. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  13993. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  13994. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  13995. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  13996. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  13997. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  13998. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  13999. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  14000. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  14001. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  14002. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  14003. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  14004. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  14005. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  14006. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  14007. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  14008. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  14009. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  14010. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  14011. (while l (org-defkey org-agenda-mode-map
  14012. (int-to-string (pop l)) 'digit-argument)))
  14013. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  14014. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  14015. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  14016. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  14017. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  14018. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  14019. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  14020. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  14021. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  14022. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  14023. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  14024. (org-defkey org-agenda-mode-map "n" 'next-line)
  14025. (org-defkey org-agenda-mode-map "p" 'previous-line)
  14026. (org-defkey org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  14027. (org-defkey org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  14028. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  14029. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  14030. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  14031. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  14032. (eval-after-load "calendar"
  14033. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  14034. 'org-calendar-goto-agenda))
  14035. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  14036. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  14037. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  14038. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  14039. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  14040. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  14041. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  14042. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  14043. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  14044. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  14045. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  14046. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  14047. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  14048. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  14049. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  14050. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  14051. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  14052. ; FIXME: other key? wtah about the menu???/
  14053. ;(org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  14054. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  14055. "Local keymap for agenda entries from Org-mode.")
  14056. (org-defkey org-agenda-keymap
  14057. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  14058. (org-defkey org-agenda-keymap
  14059. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  14060. (when org-agenda-mouse-1-follows-link
  14061. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  14062. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  14063. '("Agenda"
  14064. ("Agenda Files")
  14065. "--"
  14066. ["Show" org-agenda-show t]
  14067. ["Go To (other window)" org-agenda-goto t]
  14068. ["Go To (this window)" org-agenda-switch-to t]
  14069. ["Follow Mode" org-agenda-follow-mode
  14070. :style toggle :selected org-agenda-follow-mode :active t]
  14071. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  14072. "--"
  14073. ["Cycle TODO" org-agenda-todo t]
  14074. ["Archive subtree" org-agenda-archive t]
  14075. ["Delete subtree" org-agenda-kill t]
  14076. "--"
  14077. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  14078. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  14079. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  14080. "--"
  14081. ("Tags and Properties"
  14082. ["Show all Tags" org-agenda-show-tags t]
  14083. ["Set Tags" org-agenda-set-tags t]
  14084. "--"
  14085. ["Column View" org-columns t])
  14086. ("Date/Schedule"
  14087. ["Schedule" org-agenda-schedule t]
  14088. ["Set Deadline" org-agenda-deadline t]
  14089. "--"
  14090. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  14091. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  14092. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  14093. ("Priority"
  14094. ["Set Priority" org-agenda-priority t]
  14095. ["Increase Priority" org-agenda-priority-up t]
  14096. ["Decrease Priority" org-agenda-priority-down t]
  14097. ["Show Priority" org-agenda-show-priority t])
  14098. ("Calendar/Diary"
  14099. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  14100. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  14101. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  14102. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  14103. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  14104. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  14105. "--"
  14106. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  14107. "--"
  14108. ("View"
  14109. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  14110. :style radio :selected (equal org-agenda-ndays 1)]
  14111. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  14112. :style radio :selected (equal org-agenda-ndays 7)]
  14113. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  14114. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  14115. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  14116. :style radio :selected (member org-agenda-ndays '(365 366))]
  14117. "--"
  14118. ["Show Logbook entries" org-agenda-log-mode
  14119. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  14120. ["Include Diary" org-agenda-toggle-diary
  14121. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  14122. ["Use Time Grid" org-agenda-toggle-time-grid
  14123. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  14124. ["Write view to file" org-write-agenda t]
  14125. ["Rebuild buffer" org-agenda-redo t]
  14126. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  14127. "--"
  14128. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  14129. "--"
  14130. ["Quit" org-agenda-quit t]
  14131. ["Exit and Release Buffers" org-agenda-exit t]
  14132. ))
  14133. ;;; Agenda undo
  14134. (defvar org-agenda-allow-remote-undo t
  14135. "Non-nil means, allow remote undo from the agenda buffer.")
  14136. (defvar org-agenda-undo-list nil
  14137. "List of undoable operations in the agenda since last refresh.")
  14138. (defvar org-agenda-undo-has-started-in nil
  14139. "Buffers that have already seen `undo-start' in the current undo sequence.")
  14140. (defvar org-agenda-pending-undo-list nil
  14141. "In a series of undo commands, this is the list of remaning undo items.")
  14142. (defmacro org-if-unprotected (&rest body)
  14143. "Execute BODY if ther is no `org-protected' text property at point."
  14144. (declare (debug t))
  14145. `(unless (get-text-property (point) 'org-protected)
  14146. ,@body))
  14147. (defmacro org-with-remote-undo (_buffer &rest _body)
  14148. "Execute BODY while recording undo information in two buffers."
  14149. (declare (indent 1) (debug t))
  14150. `(let ((_cline (org-current-line))
  14151. (_cmd this-command)
  14152. (_buf1 (current-buffer))
  14153. (_buf2 ,_buffer)
  14154. (_undo1 buffer-undo-list)
  14155. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  14156. _c1 _c2)
  14157. ,@_body
  14158. (when org-agenda-allow-remote-undo
  14159. (setq _c1 (org-verify-change-for-undo
  14160. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  14161. _c2 (org-verify-change-for-undo
  14162. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  14163. (when (or _c1 _c2)
  14164. ;; make sure there are undo boundaries
  14165. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  14166. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  14167. ;; remember which buffer to undo
  14168. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  14169. org-agenda-undo-list)))))
  14170. (defun org-agenda-undo ()
  14171. "Undo a remote editing step in the agenda.
  14172. This undoes changes both in the agenda buffer and in the remote buffer
  14173. that have been changed along."
  14174. (interactive)
  14175. (or org-agenda-allow-remote-undo
  14176. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  14177. (if (not (eq this-command last-command))
  14178. (setq org-agenda-undo-has-started-in nil
  14179. org-agenda-pending-undo-list org-agenda-undo-list))
  14180. (if (not org-agenda-pending-undo-list)
  14181. (error "No further undo information"))
  14182. (let* ((entry (pop org-agenda-pending-undo-list))
  14183. buf line cmd rembuf)
  14184. (setq cmd (pop entry) line (pop entry))
  14185. (setq rembuf (nth 2 entry))
  14186. (org-with-remote-undo rembuf
  14187. (while (bufferp (setq buf (pop entry)))
  14188. (if (pop entry)
  14189. (with-current-buffer buf
  14190. (let ((last-undo-buffer buf)
  14191. buffer-read-only)
  14192. (unless (memq buf org-agenda-undo-has-started-in)
  14193. (push buf org-agenda-undo-has-started-in)
  14194. (make-local-variable 'pending-undo-list)
  14195. (undo-start))
  14196. (while (and pending-undo-list
  14197. (listp pending-undo-list)
  14198. (not (car pending-undo-list)))
  14199. (pop pending-undo-list))
  14200. (undo-more 1))))))
  14201. (goto-line line)
  14202. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  14203. (defun org-verify-change-for-undo (l1 l2)
  14204. "Verify that a real change occurred between the undo lists L1 and L2."
  14205. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  14206. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  14207. (not (eq l1 l2)))
  14208. ;;; Agenda dispatch
  14209. (defvar org-agenda-restrict nil)
  14210. (defvar org-agenda-restrict-begin (make-marker))
  14211. (defvar org-agenda-restrict-end (make-marker))
  14212. (defvar org-agenda-last-dispatch-buffer nil)
  14213. ;;;###autoload
  14214. (defun org-agenda (arg)
  14215. "Dispatch agenda commands to collect entries to the agenda buffer.
  14216. Prompts for a character to select a command. Any prefix arg will be passed
  14217. on to the selected command. The default selections are:
  14218. g
  14219. a Call `org-agenda-list' to display the agenda for current day or week.
  14220. t Call `org-todo-list' to display the global todo list.
  14221. T Call `org-todo-list' to display the global todo list, select only
  14222. entries with a specific TODO keyword (the user gets a prompt).
  14223. m Call `org-tags-view' to display headlines with tags matching
  14224. a condition (the user is prompted for the condition).
  14225. M Like `m', but select only TODO entries, no ordinary headlines.
  14226. l Create a timeline for the current buffer.
  14227. e Export views to associated files.
  14228. More commands can be added by configuring the variable
  14229. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  14230. searches can be pre-defined in this way.
  14231. If the current buffer is in Org-mode and visiting a file, you can also
  14232. first press `1' to indicate that the agenda should be temporarily (until the
  14233. next use of \\[org-agenda]) restricted to the current file."
  14234. (interactive "P")
  14235. (catch 'exit
  14236. (let* ((buf (current-buffer))
  14237. (bfn (buffer-file-name (buffer-base-buffer)))
  14238. (restrict-ok (and bfn (org-mode-p)))
  14239. (custom org-agenda-custom-commands)
  14240. c entry key type match lprops)
  14241. ;; Turn off restriction
  14242. (put 'org-agenda-files 'org-restrict nil)
  14243. (setq org-agenda-restrict nil)
  14244. (move-marker org-agenda-restrict-begin nil)
  14245. (move-marker org-agenda-restrict-end nil)
  14246. ;; Remember where this call originated
  14247. (setq org-agenda-last-dispatch-buffer (current-buffer))
  14248. (save-window-excursion
  14249. (delete-other-windows)
  14250. (switch-to-buffer-other-window " *Agenda Commands*")
  14251. (erase-buffer)
  14252. (insert (eval-when-compile
  14253. (let ((header
  14254. "Press key for an agenda command:
  14255. -------------------------------- C Configure custom agenda commands
  14256. a Agenda for current week or day e Export agenda views
  14257. t List of all TODO entries T Entries with special TODO kwd
  14258. m Match a TAGS query M Like m, but only TODO entries
  14259. L Timeline for current buffer # List stuck projects (!=configure)
  14260. ")
  14261. (start 0))
  14262. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  14263. (setq start (match-end 0))
  14264. (add-text-properties (match-beginning 2) (match-end 2)
  14265. '(face bold) header))
  14266. header)))
  14267. (while (setq entry (pop custom))
  14268. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  14269. (insert (format "\n%-4s%-14s: %s"
  14270. (org-add-props (copy-sequence key)
  14271. '(face bold))
  14272. (cond
  14273. ((stringp type) type)
  14274. ((eq type 'agenda) "Agenda for current week or day")
  14275. ((eq type 'alltodo) "List of all TODO entries")
  14276. ((eq type 'stuck) "List of stuck projects")
  14277. ((eq type 'todo) "TODO keyword")
  14278. ((eq type 'tags) "Tags query")
  14279. ((eq type 'tags-todo) "Tags (TODO)")
  14280. ((eq type 'tags-tree) "Tags tree")
  14281. ((eq type 'todo-tree) "TODO kwd tree")
  14282. ((eq type 'occur-tree) "Occur tree")
  14283. ((functionp type) (symbol-name type))
  14284. (t "???"))
  14285. (if (stringp match)
  14286. (org-add-props match nil 'face 'org-warning)
  14287. (format "set of %d commands" (length match))))))
  14288. (if restrict-ok
  14289. (insert "\n"
  14290. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  14291. (goto-char (point-min))
  14292. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  14293. (message "Press key for agenda command%s"
  14294. (if restrict-ok ", or [1] or [0] to restrict" ""))
  14295. (setq c (read-char-exclusive))
  14296. (message "")
  14297. (when (memq c '(?L ?1 ?0))
  14298. (if restrict-ok
  14299. (put 'org-agenda-files 'org-restrict (list bfn))
  14300. (error "Cannot restrict agenda to current buffer"))
  14301. (with-current-buffer " *Agenda Commands*"
  14302. (goto-char (point-max))
  14303. (delete-region (point-at-bol) (point))
  14304. (goto-char (point-min)))
  14305. (when (eq c ?0)
  14306. (setq org-agenda-restrict t)
  14307. (with-current-buffer buf
  14308. (if (org-region-active-p)
  14309. (progn
  14310. (move-marker org-agenda-restrict-begin (region-beginning))
  14311. (move-marker org-agenda-restrict-end (region-end)))
  14312. (save-excursion
  14313. (org-back-to-heading t)
  14314. (move-marker org-agenda-restrict-begin (point))
  14315. (move-marker org-agenda-restrict-end
  14316. (progn (org-end-of-subtree t)))))))
  14317. (unless (eq c ?L)
  14318. (message "Press key for agenda command%s"
  14319. (if restrict-ok " (restricted to current file)" ""))
  14320. (setq c (read-char-exclusive)))
  14321. (message "")))
  14322. (require 'calendar) ; FIXME: can we avoid this for some commands?
  14323. ;; For example the todo list should not need it (but does...)
  14324. (cond
  14325. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  14326. (if (symbolp (nth 1 entry))
  14327. (progn
  14328. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  14329. lprops (nth 3 entry))
  14330. (cond
  14331. ((eq type 'agenda)
  14332. (org-let lprops '(org-agenda-list current-prefix-arg)))
  14333. ((eq type 'alltodo)
  14334. (org-let lprops '(org-todo-list current-prefix-arg)))
  14335. ((eq type 'stuck)
  14336. (org-let lprops '(org-agenda-list-stuck-projects
  14337. current-prefix-arg)))
  14338. ((eq type 'tags)
  14339. (org-let lprops '(org-tags-view current-prefix-arg match)))
  14340. ((eq type 'tags-todo)
  14341. (org-let lprops '(org-tags-view '(4) match)))
  14342. ((eq type 'todo)
  14343. (org-let lprops '(org-todo-list match)))
  14344. ((eq type 'tags-tree)
  14345. (org-check-for-org-mode)
  14346. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  14347. ((eq type 'todo-tree)
  14348. (org-check-for-org-mode)
  14349. (org-let lprops
  14350. '(org-occur (concat "^" outline-regexp "[ \t]*"
  14351. (regexp-quote match) "\\>"))))
  14352. ((eq type 'occur-tree)
  14353. (org-check-for-org-mode)
  14354. (org-let lprops '(org-occur match)))
  14355. ((fboundp type)
  14356. (org-let lprops '(funcall type match)))
  14357. (t (error "Invalid custom agenda command type %s" type))))
  14358. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  14359. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  14360. ((equal c ?a) (call-interactively 'org-agenda-list))
  14361. ((equal c ?t) (call-interactively 'org-todo-list))
  14362. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  14363. ((equal c ?m) (call-interactively 'org-tags-view))
  14364. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  14365. ((equal c ?e) (call-interactively 'org-store-agenda-views))
  14366. ((equal c ?L)
  14367. (unless restrict-ok
  14368. (error "This is not an Org-mode file"))
  14369. (org-call-with-arg 'org-timeline arg))
  14370. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  14371. ((equal c ?!) (customize-variable 'org-stuck-projects))
  14372. (t (error "Invalid key"))))))
  14373. (defun org-run-agenda-series (name series)
  14374. (org-prepare-agenda name)
  14375. (let* ((org-agenda-multi t)
  14376. (redo (list 'org-run-agenda-series name (list 'quote series)))
  14377. (cmds (car series))
  14378. (gprops (nth 1 series))
  14379. match ;; The byte compiler incorrectly complains about this. Keep it!
  14380. cmd type lprops)
  14381. (while (setq cmd (pop cmds))
  14382. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  14383. (cond
  14384. ((eq type 'agenda)
  14385. (org-let2 gprops lprops
  14386. '(call-interactively 'org-agenda-list)))
  14387. ((eq type 'alltodo)
  14388. (org-let2 gprops lprops
  14389. '(call-interactively 'org-todo-list)))
  14390. ((eq type 'stuck)
  14391. (org-let2 gprops lprops
  14392. '(call-interactively 'org-agenda-list-stuck-projects)))
  14393. ((eq type 'tags)
  14394. (org-let2 gprops lprops
  14395. '(org-tags-view current-prefix-arg match)))
  14396. ((eq type 'tags-todo)
  14397. (org-let2 gprops lprops
  14398. '(org-tags-view '(4) match)))
  14399. ((eq type 'todo)
  14400. (org-let2 gprops lprops
  14401. '(org-todo-list match)))
  14402. ((fboundp type)
  14403. (org-let2 gprops lprops
  14404. '(funcall type match)))
  14405. (t (error "Invalid type in command series"))))
  14406. (widen)
  14407. (setq org-agenda-redo-command redo)
  14408. (goto-char (point-min)))
  14409. (org-finalize-agenda))
  14410. ;;;###autoload
  14411. (defmacro org-batch-agenda (cmd-key &rest parameters)
  14412. "Run an agenda command in batch mode and send the result to STDOUT.
  14413. If CMD-KEY is a string of length 1, it is used as a key in
  14414. `org-agenda-custom-commands' and triggers this command. If it is a
  14415. longer string is is used as a tags/todo match string.
  14416. Paramters are alternating variable names and values that will be bound
  14417. before running the agenda command."
  14418. (let (pars)
  14419. (while parameters
  14420. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  14421. (if (> (length cmd-key) 1)
  14422. (eval (list 'let (nreverse pars)
  14423. (list 'org-tags-view nil cmd-key)))
  14424. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  14425. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  14426. (set-buffer "*Org Agenda*")
  14427. (princ (org-encode-for-stdout (buffer-string)))))
  14428. (defun org-encode-for-stdout (string)
  14429. (if (fboundp 'encode-coding-string)
  14430. (encode-coding-string string buffer-file-coding-system)
  14431. string))
  14432. (defvar org-agenda-info nil)
  14433. ;;;###autoload
  14434. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  14435. "Run an agenda command in batch mode and send the result to STDOUT.
  14436. If CMD-KEY is a string of length 1, it is used as a key in
  14437. `org-agenda-custom-commands' and triggers this command. If it is a
  14438. longer string is is used as a tags/todo match string.
  14439. Paramters are alternating variable names and values that will be bound
  14440. before running the agenda command.
  14441. The output gives a line for each selected agenda item. Each
  14442. item is a list of comma-separated values, like this:
  14443. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  14444. category The category of the item
  14445. head The headline, without TODO kwd, TAGS and PRIORITY
  14446. type The type of the agenda entry, can be
  14447. todo selected in TODO match
  14448. tagsmatch selected in tags match
  14449. diary imported from diary
  14450. deadline a deadline on given date
  14451. scheduled scheduled on given date
  14452. timestamp entry has timestamp on given date
  14453. closed entry was closed on given date
  14454. upcoming-deadline warning about deadline
  14455. past-scheduled forwarded scheduled item
  14456. block entry has date block including g. date
  14457. todo The todo keyword, if any
  14458. tags All tags including inherited ones, separated by colons
  14459. date The relevant date, like 2007-2-14
  14460. time The time, like 15:00-16:50
  14461. extra Sting with extra planning info
  14462. priority-l The priority letter if any was given
  14463. priority-n The computed numerical priority
  14464. agenda-day The day in the agenda where this is listed"
  14465. (let (pars)
  14466. (while parameters
  14467. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  14468. (push (list 'org-agenda-remove-tags t) pars)
  14469. (if (> (length cmd-key) 1)
  14470. (eval (list 'let (nreverse pars)
  14471. (list 'org-tags-view nil cmd-key)))
  14472. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  14473. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  14474. (set-buffer "*Org Agenda*")
  14475. (let* ((lines (org-split-string (buffer-string) "\n"))
  14476. line)
  14477. (while (setq line (pop lines))
  14478. (catch 'next
  14479. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  14480. (setq org-agenda-info
  14481. (org-fix-agenda-info (text-properties-at 0 line)))
  14482. (princ
  14483. (org-encode-for-stdout
  14484. (mapconcat 'org-agenda-export-csv-mapper
  14485. '(org-category txt type todo tags date time-of-day extra
  14486. priority-letter priority agenda-day)
  14487. ",")))
  14488. (princ "\n"))))))
  14489. (defun org-fix-agenda-info (props)
  14490. "FIXME"
  14491. (let (tmp re)
  14492. (when (setq tmp (plist-get props 'tags))
  14493. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  14494. (when (setq tmp (plist-get props 'date))
  14495. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  14496. (let ((calendar-date-display-form '(year "-" month "-" day)))
  14497. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  14498. (setq tmp (calendar-date-string tmp)))
  14499. (setq props (plist-put props 'date tmp)))
  14500. (when (setq tmp (plist-get props 'day))
  14501. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  14502. (let ((calendar-date-display-form '(year "-" month "-" day)))
  14503. (setq tmp (calendar-date-string tmp)))
  14504. (setq props (plist-put props 'day tmp))
  14505. (setq props (plist-put props 'agenda-day tmp)))
  14506. (when (setq tmp (plist-get props 'txt))
  14507. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  14508. (plist-put props 'priority-letter (match-string 1 tmp))
  14509. (setq tmp (replace-match "" t t tmp)))
  14510. (when (and (setq re (plist-get props 'org-todo-regexp))
  14511. (setq re (concat "\\`\\.*" re " ?"))
  14512. (string-match re tmp))
  14513. (plist-put props 'todo (match-string 1 tmp))
  14514. (setq tmp (replace-match "" t t tmp)))
  14515. (plist-put props 'txt tmp)))
  14516. props)
  14517. (defun org-agenda-export-csv-mapper (prop)
  14518. (let ((res (plist-get org-agenda-info prop)))
  14519. (setq res
  14520. (cond
  14521. ((not res) "")
  14522. ((stringp res) res)
  14523. (t (prin1-to-string res))))
  14524. (while (string-match "," res)
  14525. (setq res (replace-match ";" t t res)))
  14526. (org-trim res)))
  14527. ;;;###autoload
  14528. (defun org-store-agenda-views (&rest parameters)
  14529. (interactive)
  14530. (eval (list 'org-batch-store-agenda-views)))
  14531. (defvar org-agenda-buffer-name)
  14532. ;; FIXME, why is this a macro?????
  14533. ;;;###autoload
  14534. (defmacro org-batch-store-agenda-views (&rest parameters)
  14535. "Run all custom agenda commands that have a file argument."
  14536. (let ((cmds org-agenda-custom-commands)
  14537. (dir (default-directory))
  14538. pars cmd thiscmdkey files opts)
  14539. (while parameters
  14540. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  14541. (setq pars (reverse pars))
  14542. (save-window-excursion
  14543. (while cmds
  14544. (setq cmd (pop cmds)
  14545. thiscmdkey (car cmd)
  14546. opts (nth 3 cmd)
  14547. files (org-last cmd))
  14548. (if (stringp files) (setq files (list files)))
  14549. (when files
  14550. (flet ((read-char-exclusive () (string-to-char thiscmdkey)))
  14551. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  14552. '(org-agenda nil))))
  14553. (set-buffer "*Org Agenda*")
  14554. (while files
  14555. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  14556. (list 'org-write-agenda
  14557. (expand-file-name (pop files) dir) t)))))
  14558. (kill-buffer org-agenda-buffer-name)))))
  14559. (defun org-write-agenda (file &optional nosettings)
  14560. "Write the current buffer (an agenda view) as a file.
  14561. Depending on the extension of the file name, plain text (.txt),
  14562. HTML (.html or .htm) or Postscript (.ps) is produced.
  14563. If NOSETTINGS is given, do not scope the settings of
  14564. `org-agenda-exporter-settings' into the export commands. This is used when
  14565. the settings have already been scoped and we do not wish to overrule other,
  14566. higher priority settings."
  14567. (interactive "FWrite agenda to file: ")
  14568. (if (not (file-writable-p file))
  14569. (error "Cannot write agenda to file %s" file))
  14570. (cond
  14571. ((string-match "\\.html?\\'" file) (require 'htmlize))
  14572. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  14573. (org-let (if nosettings nil org-agenda-exporter-settings)
  14574. '(save-excursion
  14575. (save-window-excursion
  14576. (cond
  14577. ((string-match "\\.html?\\'" file)
  14578. (set-buffer (htmlize-buffer (current-buffer)))
  14579. (when (and org-agenda-export-html-style
  14580. (string-match "<style>" org-agenda-export-html-style))
  14581. ;; replace <style> section with org-agenda-export-html-style
  14582. (goto-char (point-min))
  14583. (kill-region (- (search-forward "<style") 6)
  14584. (search-forward "</style>"))
  14585. (insert org-agenda-export-html-style))
  14586. (write-file file)
  14587. (kill-buffer (current-buffer))
  14588. (message "HTML written to %s" file))
  14589. ((string-match "\\.ps\\'" file)
  14590. (ps-print-buffer-with-faces file)
  14591. (message "Postscript written to %s" file))
  14592. (t
  14593. (let ((bs (buffer-string)))
  14594. (find-file file)
  14595. (insert bs)
  14596. (save-buffer 0)
  14597. (kill-buffer (current-buffer))
  14598. (message "Plain text written to %s" file))))))
  14599. (set-buffer org-agenda-buffer-name)))
  14600. (defmacro org-no-read-only (&rest body)
  14601. "Inhibit read-only for BODY."
  14602. `(let ((inhibit-read-only t)) ,@body))
  14603. (defun org-check-for-org-mode ()
  14604. "Make sure current buffer is in org-mode. Error if not."
  14605. (or (org-mode-p)
  14606. (error "Cannot execute org-mode agenda command on buffer in %s."
  14607. major-mode)))
  14608. (defun org-fit-agenda-window ()
  14609. "Fit the window to the buffer size."
  14610. (and (memq org-agenda-window-setup '(reorganize-frame))
  14611. (fboundp 'fit-window-to-buffer)
  14612. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  14613. (/ (frame-height) 2))))
  14614. ;;; Agenda file list
  14615. (defun org-agenda-files (&optional unrestricted)
  14616. "Get the list of agenda files.
  14617. Optional UNRESTRICTED means return the full list even if a restriction
  14618. is currently in place."
  14619. (cond
  14620. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14621. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14622. ((listp org-agenda-files) org-agenda-files)
  14623. (t (error "Invalid value of `org-agenda-files'"))))
  14624. (defun org-edit-agenda-file-list ()
  14625. "Edit the list of agenda files.
  14626. Depending on setup, this either uses customize to edit the variable
  14627. `org-agenda-files', or it visits the file that is holding the list. In the
  14628. latter case, the buffer is set up in a way that saving it automatically kills
  14629. the buffer and restores the previous window configuration."
  14630. (interactive)
  14631. (if (stringp org-agenda-files)
  14632. (let ((cw (current-window-configuration)))
  14633. (find-file org-agenda-files)
  14634. (org-set-local 'org-window-configuration cw)
  14635. (org-add-hook 'after-save-hook
  14636. (lambda ()
  14637. (set-window-configuration
  14638. (prog1 org-window-configuration
  14639. (kill-buffer (current-buffer))))
  14640. (org-install-agenda-files-menu)
  14641. (message "New agenda file list installed"))
  14642. nil 'local)
  14643. (message (substitute-command-keys
  14644. "Edit list and finish with \\[save-buffer]")))
  14645. (customize-variable 'org-agenda-files)))
  14646. (defun org-store-new-agenda-file-list (list)
  14647. "Set new value for the agenda file list and save it correcly."
  14648. (if (stringp org-agenda-files)
  14649. (let ((f org-agenda-files) b)
  14650. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  14651. (with-temp-file f
  14652. (insert (mapconcat 'identity list "\n") "\n")))
  14653. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  14654. (setq org-agenda-files list)
  14655. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14656. (defun org-read-agenda-file-list ()
  14657. "Read the list of agenda files from a file."
  14658. (when (stringp org-agenda-files)
  14659. (with-temp-buffer
  14660. (insert-file-contents org-agenda-files)
  14661. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  14662. ;;;###autoload
  14663. (defun org-cycle-agenda-files ()
  14664. "Cycle through the files in `org-agenda-files'.
  14665. If the current buffer visits an agenda file, find the next one in the list.
  14666. If the current buffer does not, find the first agenda file."
  14667. (interactive)
  14668. (let* ((fs (org-agenda-files t))
  14669. (files (append fs (list (car fs))))
  14670. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14671. file)
  14672. (unless files (error "No agenda files"))
  14673. (catch 'exit
  14674. (while (setq file (pop files))
  14675. (if (equal (file-truename file) tcf)
  14676. (when (car files)
  14677. (find-file (car files))
  14678. (throw 'exit t))))
  14679. (find-file (car fs)))
  14680. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14681. (defun org-agenda-file-to-front (&optional to-end)
  14682. "Move/add the current file to the top of the agenda file list.
  14683. If the file is not present in the list, it is added to the front. If it is
  14684. present, it is moved there. With optional argument TO-END, add/move to the
  14685. end of the list."
  14686. (interactive "P")
  14687. (let ((file-alist (mapcar (lambda (x)
  14688. (cons (file-truename x) x))
  14689. (org-agenda-files t)))
  14690. (ctf (file-truename buffer-file-name))
  14691. x had)
  14692. (setq x (assoc ctf file-alist) had x)
  14693. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14694. (if to-end
  14695. (setq file-alist (append (delq x file-alist) (list x)))
  14696. (setq file-alist (cons x (delq x file-alist))))
  14697. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14698. (org-install-agenda-files-menu)
  14699. (message "File %s to %s of agenda file list"
  14700. (if had "moved" "added") (if to-end "end" "front"))))
  14701. (defun org-remove-file (&optional file)
  14702. "Remove current file from the list of files in variable `org-agenda-files'.
  14703. These are the files which are being checked for agenda entries.
  14704. Optional argument FILE means, use this file instead of the current."
  14705. (interactive)
  14706. (let* ((file (or file buffer-file-name))
  14707. (true-file (file-truename file))
  14708. (afile (abbreviate-file-name file))
  14709. (files (delq nil (mapcar
  14710. (lambda (x)
  14711. (if (equal true-file
  14712. (file-truename x))
  14713. nil x))
  14714. (org-agenda-files t)))))
  14715. (if (not (= (length files) (length (org-agenda-files t))))
  14716. (progn
  14717. (org-store-new-agenda-file-list files)
  14718. (org-install-agenda-files-menu)
  14719. (message "Removed file: %s" afile))
  14720. (message "File was not in list: %s" afile))))
  14721. (defun org-file-menu-entry (file)
  14722. (vector file (list 'find-file file) t))
  14723. (defun org-check-agenda-file (file)
  14724. "Make sure FILE exists. If not, ask user what to do."
  14725. (when (not (file-exists-p file))
  14726. (message "non-existent file %s. [R]emove from list or [A]bort?"
  14727. (abbreviate-file-name file))
  14728. (let ((r (downcase (read-char-exclusive))))
  14729. (cond
  14730. ((equal r ?r)
  14731. (org-remove-file file)
  14732. (throw 'nextfile t))
  14733. (t (error "Abort"))))))
  14734. ;;; Agenda prepare and finalize
  14735. (defvar org-agenda-multi nil) ; dynammically scoped
  14736. (defvar org-agenda-buffer-name "*Org Agenda*")
  14737. (defvar org-pre-agenda-window-conf nil)
  14738. (defvar org-agenda-name nil)
  14739. (defun org-prepare-agenda (&optional name)
  14740. (setq org-todo-keywords-for-agenda nil)
  14741. (setq org-done-keywords-for-agenda nil)
  14742. (if org-agenda-multi
  14743. (progn
  14744. (setq buffer-read-only nil)
  14745. (goto-char (point-max))
  14746. (unless (= (point) 1)
  14747. (insert "\n" (make-string (window-width) ?=) "\n"))
  14748. (narrow-to-region (point) (point-max)))
  14749. (org-agenda-maybe-reset-markers 'force)
  14750. (org-prepare-agenda-buffers (org-agenda-files))
  14751. (setq org-todo-keywords-for-agenda
  14752. (org-uniquify org-todo-keywords-for-agenda))
  14753. (setq org-done-keywords-for-agenda
  14754. (org-uniquify org-done-keywords-for-agenda))
  14755. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  14756. (awin (get-buffer-window abuf)))
  14757. (cond
  14758. ((equal (current-buffer) abuf) nil)
  14759. (awin (select-window awin))
  14760. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  14761. ((equal org-agenda-window-setup 'current-window)
  14762. (switch-to-buffer abuf))
  14763. ((equal org-agenda-window-setup 'other-window)
  14764. (switch-to-buffer-other-window abuf))
  14765. ((equal org-agenda-window-setup 'other-frame)
  14766. (switch-to-buffer-other-frame abuf))
  14767. ((equal org-agenda-window-setup 'reorganize-frame)
  14768. (delete-other-windows)
  14769. (switch-to-buffer-other-window abuf))))
  14770. (setq buffer-read-only nil)
  14771. (erase-buffer)
  14772. (org-agenda-mode)
  14773. (and name (not org-agenda-name)
  14774. (org-set-local 'org-agenda-name name)))
  14775. (setq buffer-read-only nil))
  14776. (defun org-finalize-agenda ()
  14777. "Finishing touch for the agenda buffer, called just before displaying it."
  14778. (unless org-agenda-multi
  14779. (save-excursion
  14780. (let ((buffer-read-only))
  14781. (goto-char (point-min))
  14782. (while (org-activate-bracket-links (point-max))
  14783. (add-text-properties (match-beginning 0) (match-end 0)
  14784. '(face org-link)))
  14785. (org-agenda-align-tags)
  14786. (unless org-agenda-with-colors
  14787. (remove-text-properties (point-min) (point-max) '(face nil))))
  14788. (if (and (boundp 'org-overriding-columns-format)
  14789. org-overriding-columns-format)
  14790. (org-set-local 'org-overriding-columns-format
  14791. org-overriding-columns-format))
  14792. (if (and (boundp 'org-agenda-view-columns-initially)
  14793. org-agenda-view-columns-initially)
  14794. (org-agenda-columns))
  14795. (run-hooks 'org-finalize-agenda-hook))))
  14796. (defun org-prepare-agenda-buffers (files)
  14797. "Create buffers for all agenda files, protect archived trees and comments."
  14798. (interactive)
  14799. (let ((pa '(:org-archived t))
  14800. (pc '(:org-comment t))
  14801. (pall '(:org-archived t :org-comment t))
  14802. (rea (concat ":" org-archive-tag ":"))
  14803. bmp file re)
  14804. (save-excursion
  14805. (save-restriction
  14806. (while (setq file (pop files))
  14807. (org-check-agenda-file file)
  14808. (set-buffer (org-get-agenda-file-buffer file))
  14809. (widen)
  14810. (setq bmp (buffer-modified-p))
  14811. (setq org-todo-keywords-for-agenda
  14812. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14813. (setq org-done-keywords-for-agenda
  14814. (append org-done-keywords-for-agenda org-done-keywords))
  14815. (save-excursion
  14816. (remove-text-properties (point-min) (point-max) pall)
  14817. (when org-agenda-skip-archived-trees
  14818. (goto-char (point-min))
  14819. (while (re-search-forward rea nil t)
  14820. (if (org-on-heading-p t)
  14821. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14822. (goto-char (point-min))
  14823. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14824. (while (re-search-forward re nil t)
  14825. (add-text-properties
  14826. (match-beginning 0) (org-end-of-subtree t) pc)))
  14827. (set-buffer-modified-p bmp))))))
  14828. (defvar org-agenda-skip-function nil
  14829. "Function to be called at each match during agenda construction.
  14830. If this function return nil, the current match should not be skipped.
  14831. Otherwise, the function must return a position from where the search
  14832. should be continued.
  14833. Never set this variable using `setq' or so, because then it will apply
  14834. to all future agenda commands. Instead, bind it with `let' to scope
  14835. it dynamically into the agenda-constructing command.")
  14836. (defun org-agenda-skip ()
  14837. "Throw to `:skip' in places that should be skipped.
  14838. Also moves point to the end of the skipped region, so that search can
  14839. continue from there."
  14840. (let ((p (point-at-bol)) to)
  14841. (and org-agenda-skip-archived-trees
  14842. (get-text-property p :org-archived)
  14843. (org-end-of-subtree t)
  14844. (throw :skip t))
  14845. (and (get-text-property p :org-comment)
  14846. (org-end-of-subtree t)
  14847. (throw :skip t))
  14848. (if (equal (char-after p) ?#) (throw :skip t))
  14849. (when (and (functionp org-agenda-skip-function)
  14850. (setq to (save-excursion
  14851. (save-match-data
  14852. (funcall org-agenda-skip-function)))))
  14853. (goto-char to)
  14854. (throw :skip t))))
  14855. (defvar org-agenda-markers nil
  14856. "List of all currently active markers created by `org-agenda'.")
  14857. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  14858. "Creation time of the last agenda marker.")
  14859. (defun org-agenda-new-marker (&optional pos)
  14860. "Return a new agenda marker.
  14861. Org-mode keeps a list of these markers and resets them when they are
  14862. no longer in use."
  14863. (let ((m (copy-marker (or pos (point)))))
  14864. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  14865. (push m org-agenda-markers)
  14866. m))
  14867. (defun org-agenda-maybe-reset-markers (&optional force)
  14868. "Reset markers created by `org-agenda'. But only if they are old enough."
  14869. (if (or (and force (not org-agenda-multi))
  14870. (> (- (time-to-seconds (current-time))
  14871. org-agenda-last-marker-time)
  14872. 5))
  14873. (while org-agenda-markers
  14874. (move-marker (pop org-agenda-markers) nil))))
  14875. (defvar org-agenda-new-buffers nil
  14876. "Buffers created to visit agenda files.")
  14877. (defun org-get-agenda-file-buffer (file)
  14878. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14879. it to the list of buffers which might be released later."
  14880. (let ((buf (org-find-base-buffer-visiting file)))
  14881. (if buf
  14882. buf ; just return it
  14883. ;; Make a new buffer and remember it
  14884. (setq buf (find-file-noselect file))
  14885. (if buf (push buf org-agenda-new-buffers))
  14886. buf)))
  14887. (defun org-release-buffers (blist)
  14888. "Release all buffers in list, asking the user for confirmation when needed.
  14889. When a buffer is unmodified, it is just killed. When modified, it is saved
  14890. \(if the user agrees) and then killed."
  14891. (let (buf file)
  14892. (while (setq buf (pop blist))
  14893. (setq file (buffer-file-name buf))
  14894. (when (and (buffer-modified-p buf)
  14895. file
  14896. (y-or-n-p (format "Save file %s? " file)))
  14897. (with-current-buffer buf (save-buffer)))
  14898. (kill-buffer buf))))
  14899. (defvar org-category-table nil)
  14900. (defun org-get-category-table ()
  14901. "Get the table of categories and positions in current buffer."
  14902. (let (tbl)
  14903. (save-excursion
  14904. (save-restriction
  14905. (widen)
  14906. (goto-char (point-min))
  14907. (while (re-search-forward "^#\\+CATEGORY:[ \t]*\\(.*\\)"
  14908. nil t)
  14909. (push (cons (match-beginning 1)
  14910. (org-trim (match-string 1))) tbl))))
  14911. tbl))
  14912. (defun org-get-category (&optional pos)
  14913. "Get the category applying to position POS."
  14914. (if (not org-category-table)
  14915. (cond
  14916. ((null org-category)
  14917. (setq org-category
  14918. (if buffer-file-name
  14919. (file-name-sans-extension
  14920. (file-name-nondirectory buffer-file-name))
  14921. "???")))
  14922. ((symbolp org-category) (symbol-name org-category))
  14923. (t org-category))
  14924. (let ((tbl org-category-table)
  14925. (pos (or pos (point))))
  14926. (while (and tbl (> (caar tbl) pos))
  14927. (pop tbl))
  14928. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  14929. org-category-table))))))
  14930. ;;; Agenda timeline
  14931. (defun org-timeline (&optional include-all)
  14932. "Show a time-sorted view of the entries in the current org file.
  14933. Only entries with a time stamp of today or later will be listed. With
  14934. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  14935. under the current date.
  14936. If the buffer contains an active region, only check the region for
  14937. dates."
  14938. (interactive "P")
  14939. (require 'calendar)
  14940. (org-compile-prefix-format 'timeline)
  14941. (org-set-sorting-strategy 'timeline)
  14942. (let* ((dopast t)
  14943. (dotodo include-all)
  14944. (doclosed org-agenda-show-log)
  14945. (entry buffer-file-name)
  14946. (date (calendar-current-date))
  14947. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  14948. (end (if (org-region-active-p) (region-end) (point-max)))
  14949. (day-numbers (org-get-all-dates beg end 'no-ranges
  14950. t doclosed ; always include today
  14951. org-timeline-show-empty-dates))
  14952. (today (time-to-days (current-time)))
  14953. (past t)
  14954. args
  14955. s e rtn d emptyp)
  14956. (setq org-agenda-redo-command
  14957. (list 'progn
  14958. (list 'switch-to-buffer-other-window (current-buffer))
  14959. (list 'org-timeline (list 'quote include-all))))
  14960. (if (not dopast)
  14961. ;; Remove past dates from the list of dates.
  14962. (setq day-numbers (delq nil (mapcar (lambda(x)
  14963. (if (>= x today) x nil))
  14964. day-numbers))))
  14965. (org-prepare-agenda (concat "Timeline "
  14966. (file-name-nondirectory buffer-file-name)))
  14967. (if doclosed (push :closed args))
  14968. (push :timestamp args)
  14969. (push :sexp args)
  14970. (if dotodo (push :todo args))
  14971. (while (setq d (pop day-numbers))
  14972. (if (and (listp d) (eq (car d) :omitted))
  14973. (progn
  14974. (setq s (point))
  14975. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  14976. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  14977. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  14978. (if (and (>= d today)
  14979. dopast
  14980. past)
  14981. (progn
  14982. (setq past nil)
  14983. (insert (make-string 79 ?-) "\n")))
  14984. (setq date (calendar-gregorian-from-absolute d))
  14985. (setq s (point))
  14986. (setq rtn (and (not emptyp)
  14987. (apply 'org-agenda-get-day-entries
  14988. entry date args)))
  14989. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  14990. (progn
  14991. (insert (calendar-day-name date) " "
  14992. (number-to-string (extract-calendar-day date)) " "
  14993. (calendar-month-name (extract-calendar-month date)) " "
  14994. (number-to-string (extract-calendar-year date)) "\n")
  14995. ; FIXME: this gives a timezone problem
  14996. ; (insert (format-time-string org-agenda-date-format
  14997. ; (calendar-time-from-absolute d 0))
  14998. ; "\n")
  14999. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  15000. (put-text-property s (1- (point)) 'org-date-line t)
  15001. (if (equal d today)
  15002. (put-text-property s (1- (point)) 'org-today t))
  15003. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  15004. (put-text-property s (1- (point)) 'day d)))))
  15005. (goto-char (point-min))
  15006. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  15007. (point-min)))
  15008. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  15009. (org-finalize-agenda)
  15010. (setq buffer-read-only t)))
  15011. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  15012. "Return a list of all relevant day numbers from BEG to END buffer positions.
  15013. If NO-RANGES is non-nil, include only the start and end dates of a range,
  15014. not every single day in the range. If FORCE-TODAY is non-nil, make
  15015. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  15016. inactive time stamps (those in square brackets) are included.
  15017. When EMPTY is non-nil, also include days without any entries."
  15018. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  15019. dates dates1 date day day1 day2 ts1 ts2)
  15020. (if force-today
  15021. (setq dates (list (time-to-days (current-time)))))
  15022. (save-excursion
  15023. (goto-char beg)
  15024. (while (re-search-forward re end t)
  15025. (setq day (time-to-days (org-time-string-to-time
  15026. (substring (match-string 1) 0 10))))
  15027. (or (memq day dates) (push day dates)))
  15028. (unless no-ranges
  15029. (goto-char beg)
  15030. (while (re-search-forward org-tr-regexp end t)
  15031. (setq ts1 (substring (match-string 1) 0 10)
  15032. ts2 (substring (match-string 2) 0 10)
  15033. day1 (time-to-days (org-time-string-to-time ts1))
  15034. day2 (time-to-days (org-time-string-to-time ts2)))
  15035. (while (< (setq day1 (1+ day1)) day2)
  15036. (or (memq day1 dates) (push day1 dates)))))
  15037. (setq dates (sort dates '<))
  15038. (when empty
  15039. (while (setq day (pop dates))
  15040. (setq day2 (car dates))
  15041. (push day dates1)
  15042. (when (and day2 empty)
  15043. (if (or (eq empty t)
  15044. (and (numberp empty) (<= (- day2 day) empty)))
  15045. (while (< (setq day (1+ day)) day2)
  15046. (push (list day) dates1))
  15047. (push (cons :omitted (- day2 day)) dates1))))
  15048. (setq dates (nreverse dates1)))
  15049. dates)))
  15050. ;;; Agenda Daily/Weekly
  15051. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  15052. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  15053. (defvar org-agenda-last-arguments nil
  15054. "The arguments of the previous call to org-agenda")
  15055. (defvar org-starting-day nil) ; local variable in the agenda buffer
  15056. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  15057. (defvar org-include-all-loc nil) ; local variable
  15058. ;;;###autoload
  15059. (defun org-agenda-list (&optional include-all start-day ndays)
  15060. "Produce a weekly view from all files in variable `org-agenda-files'.
  15061. The view will be for the current week, but from the overview buffer you
  15062. will be able to go to other weeks.
  15063. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  15064. also be shown, under the current date.
  15065. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  15066. on the days are also shown. See the variable `org-log-done' for how
  15067. to turn on logging.
  15068. START-DAY defaults to TODAY, or to the most recent match for the weekday
  15069. given in `org-agenda-start-on-weekday'.
  15070. NDAYS defaults to `org-agenda-ndays'."
  15071. (interactive "P")
  15072. (setq ndays (or ndays org-agenda-ndays)
  15073. start-day (or start-day org-agenda-start-day))
  15074. (if org-agenda-overriding-arguments
  15075. (setq include-all (car org-agenda-overriding-arguments)
  15076. start-day (nth 1 org-agenda-overriding-arguments)
  15077. ndays (nth 2 org-agenda-overriding-arguments)))
  15078. (if (stringp start-day)
  15079. ;; Convert to an absolute day number
  15080. (setq start-day (time-to-days (org-read-date nil t start-day))))
  15081. (setq org-agenda-last-arguments (list include-all start-day ndays))
  15082. (org-compile-prefix-format 'agenda)
  15083. (org-set-sorting-strategy 'agenda)
  15084. (require 'calendar)
  15085. (let* ((org-agenda-start-on-weekday
  15086. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  15087. org-agenda-start-on-weekday nil))
  15088. (thefiles (org-agenda-files))
  15089. (files thefiles)
  15090. (today (time-to-days (current-time)))
  15091. (sd (or start-day today))
  15092. (start (if (or (null org-agenda-start-on-weekday)
  15093. (< org-agenda-ndays 7))
  15094. sd
  15095. (let* ((nt (calendar-day-of-week
  15096. (calendar-gregorian-from-absolute sd)))
  15097. (n1 org-agenda-start-on-weekday)
  15098. (d (- nt n1)))
  15099. (- sd (+ (if (< d 0) 7 0) d)))))
  15100. (day-numbers (list start))
  15101. (inhibit-redisplay (not debug-on-error))
  15102. s e rtn rtnall file date d start-pos end-pos todayp nd)
  15103. (setq org-agenda-redo-command
  15104. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  15105. ;; Make the list of days
  15106. (setq ndays (or ndays org-agenda-ndays)
  15107. nd ndays)
  15108. (while (> ndays 1)
  15109. (push (1+ (car day-numbers)) day-numbers)
  15110. (setq ndays (1- ndays)))
  15111. (setq day-numbers (nreverse day-numbers))
  15112. (org-prepare-agenda "Day/Week")
  15113. (org-set-local 'org-starting-day (car day-numbers))
  15114. (org-set-local 'org-include-all-loc include-all)
  15115. (org-set-local 'org-agenda-span
  15116. (org-agenda-ndays-to-span nd))
  15117. (when (and (or include-all org-agenda-include-all-todo)
  15118. (member today day-numbers))
  15119. (setq files thefiles
  15120. rtnall nil)
  15121. (while (setq file (pop files))
  15122. (catch 'nextfile
  15123. (org-check-agenda-file file)
  15124. (setq date (calendar-gregorian-from-absolute today)
  15125. rtn (org-agenda-get-day-entries
  15126. file date :todo))
  15127. (setq rtnall (append rtnall rtn))))
  15128. (when rtnall
  15129. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  15130. (add-text-properties (point-min) (1- (point))
  15131. (list 'face 'org-agenda-structure))
  15132. (insert (org-finalize-agenda-entries rtnall) "\n")))
  15133. (setq s (point))
  15134. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  15135. "-agenda:\n")
  15136. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  15137. 'org-date-line t))
  15138. (while (setq d (pop day-numbers))
  15139. (setq date (calendar-gregorian-from-absolute d)
  15140. s (point))
  15141. (if (or (setq todayp (= d today))
  15142. (and (not start-pos) (= d sd)))
  15143. (setq start-pos (point))
  15144. (if (and start-pos (not end-pos))
  15145. (setq end-pos (point))))
  15146. (setq files thefiles
  15147. rtnall nil)
  15148. (while (setq file (pop files))
  15149. (catch 'nextfile
  15150. (org-check-agenda-file file)
  15151. (if org-agenda-show-log
  15152. (setq rtn (org-agenda-get-day-entries
  15153. file date
  15154. :deadline :scheduled :timestamp :sexp :closed))
  15155. (setq rtn (org-agenda-get-day-entries
  15156. file date
  15157. :deadline :scheduled :sexp :timestamp)))
  15158. (setq rtnall (append rtnall rtn))))
  15159. (if org-agenda-include-diary
  15160. (progn
  15161. (require 'diary-lib)
  15162. (setq rtn (org-get-entries-from-diary date))
  15163. (setq rtnall (append rtnall rtn))))
  15164. (if (or rtnall org-agenda-show-all-dates)
  15165. (progn
  15166. (insert (format "%-9s %2d %s %4d\n"
  15167. (calendar-day-name date)
  15168. (extract-calendar-day date)
  15169. (calendar-month-name (extract-calendar-month date))
  15170. (extract-calendar-year date)))
  15171. ; FIXME: this gives a timezone problem
  15172. ; (insert (format-time-string org-agenda-date-format
  15173. ; (calendar-time-from-absolute d 0)) "\n")
  15174. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  15175. (put-text-property s (1- (point)) 'org-date-line t)
  15176. (if todayp (put-text-property s (1- (point)) 'org-today t))
  15177. (if rtnall (insert
  15178. (org-finalize-agenda-entries
  15179. (org-agenda-add-time-grid-maybe
  15180. rtnall nd todayp))
  15181. "\n"))
  15182. (put-text-property s (1- (point)) 'day d))))
  15183. (goto-char (point-min))
  15184. (org-fit-agenda-window)
  15185. (unless (and (pos-visible-in-window-p (point-min))
  15186. (pos-visible-in-window-p (point-max)))
  15187. (goto-char (1- (point-max)))
  15188. (recenter -1)
  15189. (if (not (pos-visible-in-window-p (or start-pos 1)))
  15190. (progn
  15191. (goto-char (or start-pos 1))
  15192. (recenter 1))))
  15193. (goto-char (or start-pos 1))
  15194. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  15195. (org-finalize-agenda)
  15196. (setq buffer-read-only t)
  15197. (message "")))
  15198. (defun org-agenda-ndays-to-span (n)
  15199. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  15200. ;;; Agenda TODO list
  15201. (defvar org-select-this-todo-keyword nil)
  15202. (defvar org-last-arg nil)
  15203. ;;;###autoload
  15204. (defun org-todo-list (arg)
  15205. "Show all TODO entries from all agenda file in a single list.
  15206. The prefix arg can be used to select a specific TODO keyword and limit
  15207. the list to these. When using \\[universal-argument], you will be prompted
  15208. for a keyword. A numeric prefix directly selects the Nth keyword in
  15209. `org-todo-keywords-1'."
  15210. (interactive "P")
  15211. (require 'calendar)
  15212. (org-compile-prefix-format 'todo)
  15213. (org-set-sorting-strategy 'todo)
  15214. (org-prepare-agenda "TODO")
  15215. (let* ((today (time-to-days (current-time)))
  15216. (date (calendar-gregorian-from-absolute today))
  15217. (kwds org-todo-keywords-for-agenda)
  15218. (completion-ignore-case t)
  15219. (org-select-this-todo-keyword
  15220. (if (stringp arg) arg
  15221. (and arg (integerp arg) (> arg 0)
  15222. (nth (1- arg) kwds))))
  15223. rtn rtnall files file pos)
  15224. (when (equal arg '(4))
  15225. (setq org-select-this-todo-keyword
  15226. (completing-read "Keyword (or KWD1|K2D2|...): "
  15227. (mapcar 'list kwds) nil nil)))
  15228. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  15229. (org-set-local 'org-last-arg arg)
  15230. ;FIXME (org-set-local 'org-todo-keywords-for-agenda kwds)
  15231. (setq org-agenda-redo-command
  15232. '(org-todo-list (or current-prefix-arg org-last-arg)))
  15233. (setq files (org-agenda-files)
  15234. rtnall nil)
  15235. (while (setq file (pop files))
  15236. (catch 'nextfile
  15237. (org-check-agenda-file file)
  15238. (setq rtn (org-agenda-get-day-entries file date :todo))
  15239. (setq rtnall (append rtnall rtn))))
  15240. (if org-agenda-overriding-header
  15241. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  15242. nil 'face 'org-agenda-structure) "\n")
  15243. (insert "Global list of TODO items of type: ")
  15244. (add-text-properties (point-min) (1- (point))
  15245. (list 'face 'org-agenda-structure))
  15246. (setq pos (point))
  15247. (insert (or org-select-this-todo-keyword "ALL") "\n")
  15248. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  15249. (setq pos (point))
  15250. (unless org-agenda-multi
  15251. (insert "Available with `N r': (0)ALL")
  15252. (let ((n 0) s)
  15253. (mapc (lambda (x)
  15254. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  15255. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  15256. (insert "\n "))
  15257. (insert " " s))
  15258. kwds))
  15259. (insert "\n"))
  15260. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  15261. (when rtnall
  15262. (insert (org-finalize-agenda-entries rtnall) "\n"))
  15263. (goto-char (point-min))
  15264. (org-fit-agenda-window)
  15265. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  15266. (org-finalize-agenda)
  15267. (setq buffer-read-only t)))
  15268. ;;; Agenda tags match
  15269. ;;;###autoload
  15270. (defun org-tags-view (&optional todo-only match)
  15271. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  15272. The prefix arg TODO-ONLY limits the search to TODO entries."
  15273. (interactive "P")
  15274. (org-compile-prefix-format 'tags)
  15275. (org-set-sorting-strategy 'tags)
  15276. (let* ((org-tags-match-list-sublevels
  15277. (if todo-only t org-tags-match-list-sublevels))
  15278. (completion-ignore-case t)
  15279. rtn rtnall files file pos matcher
  15280. buffer)
  15281. (setq matcher (org-make-tags-matcher match)
  15282. match (car matcher) matcher (cdr matcher))
  15283. (org-prepare-agenda (concat "TAGS " match))
  15284. (setq org-agenda-redo-command
  15285. (list 'org-tags-view (list 'quote todo-only)
  15286. (list 'if 'current-prefix-arg nil match)))
  15287. (setq files (org-agenda-files)
  15288. rtnall nil)
  15289. (while (setq file (pop files))
  15290. (catch 'nextfile
  15291. (org-check-agenda-file file)
  15292. (setq buffer (if (file-exists-p file)
  15293. (org-get-agenda-file-buffer file)
  15294. (error "No such file %s" file)))
  15295. (if (not buffer)
  15296. ;; If file does not exist, merror message to agenda
  15297. (setq rtn (list
  15298. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  15299. rtnall (append rtnall rtn))
  15300. (with-current-buffer buffer
  15301. (unless (org-mode-p)
  15302. (error "Agenda file %s is not in `org-mode'" file))
  15303. (setq org-category-table (org-get-category-table))
  15304. (save-excursion
  15305. (save-restriction
  15306. (if org-agenda-restrict
  15307. (narrow-to-region org-agenda-restrict-begin
  15308. org-agenda-restrict-end)
  15309. (widen))
  15310. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  15311. (setq rtnall (append rtnall rtn))))))))
  15312. (if org-agenda-overriding-header
  15313. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  15314. nil 'face 'org-agenda-structure) "\n")
  15315. (insert "Headlines with TAGS match: ")
  15316. (add-text-properties (point-min) (1- (point))
  15317. (list 'face 'org-agenda-structure))
  15318. (setq pos (point))
  15319. (insert match "\n")
  15320. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  15321. (setq pos (point))
  15322. (unless org-agenda-multi
  15323. (insert "Press `C-u r' to search again with new search string\n"))
  15324. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  15325. (when rtnall
  15326. (insert (org-finalize-agenda-entries rtnall) "\n"))
  15327. (goto-char (point-min))
  15328. (org-fit-agenda-window)
  15329. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  15330. (org-finalize-agenda)
  15331. (setq buffer-read-only t)))
  15332. ;;; Agenda Finding stuck projects
  15333. (defvar org-agenda-skip-regexp nil
  15334. "Regular expression used in skipping subtrees for the agenda.
  15335. This is basically a temporary global variable that can be set and then
  15336. used by user-defined selections using `org-agenda-skip-function'.")
  15337. (defvar org-agenda-overriding-header nil
  15338. "When this is set during todo and tags searches, will replace header.")
  15339. (defun org-agenda-skip-subtree-when-regexp-matches ()
  15340. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  15341. If yes, it returns the end position of this tree, causing agenda commands
  15342. to skip this subtree. This is a function that can be put into
  15343. `org-agenda-skip-function' for the duration of a command."
  15344. (save-match-data
  15345. (let ((end (save-excursion (org-end-of-subtree t)))
  15346. skip)
  15347. (save-excursion
  15348. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  15349. (and skip end))))
  15350. (defun org-agenda-list-stuck-projects (&rest ignore)
  15351. "Create agenda view for projects that are stuck.
  15352. Stuck projects are project that have no next actions. For the definitions
  15353. of what a project is and how to check if it stuck, customize the variable
  15354. `org-stuck-projects'.
  15355. MATCH is being ignored."
  15356. (interactive)
  15357. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  15358. (org-agenda-overriding-header "List of stuck projects: ")
  15359. (matcher (nth 0 org-stuck-projects))
  15360. (todo (nth 1 org-stuck-projects))
  15361. (todo-wds (if (member "*" todo)
  15362. (progn
  15363. (org-prepare-agenda-buffers (org-agenda-files))
  15364. (org-delete-all
  15365. org-done-keywords-for-agenda
  15366. (copy-sequence org-todo-keywords-for-agenda)))
  15367. todo))
  15368. (todo-re (concat "^\\*+[ \t]+\\("
  15369. (mapconcat 'identity todo-wds "\\|")
  15370. "\\)\\>"))
  15371. (tags (nth 2 org-stuck-projects))
  15372. (tags-re (if (member "*" tags)
  15373. (org-re "^\\*+.*:[[:alnum:]_@]+:[ \t]*$")
  15374. (concat "^\\*+.*:\\("
  15375. (mapconcat 'identity tags "\\|")
  15376. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  15377. (gen-re (nth 3 org-stuck-projects))
  15378. (re-list
  15379. (delq nil
  15380. (list
  15381. (if todo todo-re)
  15382. (if tags tags-re)
  15383. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  15384. gen-re)))))
  15385. (setq org-agenda-skip-regexp
  15386. (if re-list
  15387. (mapconcat 'identity re-list "\\|")
  15388. (error "No information how to identify unstuck projects")))
  15389. (org-tags-view nil matcher)
  15390. (with-current-buffer org-agenda-buffer-name
  15391. (setq org-agenda-redo-command
  15392. '(org-agenda-list-stuck-projects
  15393. (or current-prefix-arg org-last-arg))))))
  15394. ;;; Diary integration
  15395. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  15396. (defun org-get-entries-from-diary (date)
  15397. "Get the (Emacs Calendar) diary entries for DATE."
  15398. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  15399. (diary-display-hook '(fancy-diary-display))
  15400. (list-diary-entries-hook
  15401. (cons 'org-diary-default-entry list-diary-entries-hook))
  15402. (diary-file-name-prefix-function nil) ; turn this feature off
  15403. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  15404. entries
  15405. (org-disable-agenda-to-diary t))
  15406. (save-excursion
  15407. (save-window-excursion
  15408. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  15409. (if (not (get-buffer fancy-diary-buffer))
  15410. (setq entries nil)
  15411. (with-current-buffer fancy-diary-buffer
  15412. (setq buffer-read-only nil)
  15413. (if (= (point-max) 1)
  15414. ;; No entries
  15415. (setq entries nil)
  15416. ;; Omit the date and other unnecessary stuff
  15417. (org-agenda-cleanup-fancy-diary)
  15418. ;; Add prefix to each line and extend the text properties
  15419. (if (= (point-max) 1)
  15420. (setq entries nil)
  15421. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  15422. (set-buffer-modified-p nil)
  15423. (kill-buffer fancy-diary-buffer)))
  15424. (when entries
  15425. (setq entries (org-split-string entries "\n"))
  15426. (setq entries
  15427. (mapcar
  15428. (lambda (x)
  15429. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  15430. ;; Extend the text properties to the beginning of the line
  15431. (org-add-props x (text-properties-at (1- (length x)) x)
  15432. 'type "diary" 'date date))
  15433. entries)))))
  15434. (defun org-agenda-cleanup-fancy-diary ()
  15435. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  15436. This gets rid of the date, the underline under the date, and
  15437. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  15438. date. It also removes lines that contain only whitespace."
  15439. (goto-char (point-min))
  15440. (if (looking-at ".*?:[ \t]*")
  15441. (progn
  15442. (replace-match "")
  15443. (re-search-forward "\n=+$" nil t)
  15444. (replace-match "")
  15445. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  15446. (re-search-forward "\n=+$" nil t)
  15447. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  15448. (goto-char (point-min))
  15449. (while (re-search-forward "^ +\n" nil t)
  15450. (replace-match ""))
  15451. (goto-char (point-min))
  15452. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  15453. (replace-match "")))
  15454. ;; Make sure entries from the diary have the right text properties.
  15455. (eval-after-load "diary-lib"
  15456. '(if (boundp 'diary-modify-entry-list-string-function)
  15457. ;; We can rely on the hook, nothing to do
  15458. nil
  15459. ;; Hook not avaiable, must use advice to make this work
  15460. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  15461. "Make the position visible."
  15462. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  15463. (stringp string)
  15464. buffer-file-name)
  15465. (setq string (org-modify-diary-entry-string string))))))
  15466. (defun org-modify-diary-entry-string (string)
  15467. "Add text properties to string, allowing org-mode to act on it."
  15468. (org-add-props string nil
  15469. 'mouse-face 'highlight
  15470. 'keymap org-agenda-keymap
  15471. 'help-echo (if buffer-file-name
  15472. (format "mouse-2 or RET jump to diary file %s"
  15473. (abbreviate-file-name buffer-file-name))
  15474. "")
  15475. 'org-agenda-diary-link t
  15476. 'org-marker (org-agenda-new-marker (point-at-bol))))
  15477. (defun org-diary-default-entry ()
  15478. "Add a dummy entry to the diary.
  15479. Needed to avoid empty dates which mess up holiday display."
  15480. ;; Catch the error if dealing with the new add-to-diary-alist
  15481. (when org-disable-agenda-to-diary
  15482. (condition-case nil
  15483. (add-to-diary-list original-date "Org-mode dummy" "")
  15484. (error
  15485. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  15486. ;;;###autoload
  15487. (defun org-diary (&rest args)
  15488. "Return diary information from org-files.
  15489. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  15490. It accesses org files and extracts information from those files to be
  15491. listed in the diary. The function accepts arguments specifying what
  15492. items should be listed. The following arguments are allowed:
  15493. :timestamp List the headlines of items containing a date stamp or
  15494. date range matching the selected date. Deadlines will
  15495. also be listed, on the expiration day.
  15496. :sexp FIXME
  15497. :deadline List any deadlines past due, or due within
  15498. `org-deadline-warning-days'. The listing occurs only
  15499. in the diary for *today*, not at any other date. If
  15500. an entry is marked DONE, it is no longer listed.
  15501. :scheduled List all items which are scheduled for the given date.
  15502. The diary for *today* also contains items which were
  15503. scheduled earlier and are not yet marked DONE.
  15504. :todo List all TODO items from the org-file. This may be a
  15505. long list - so this is not turned on by default.
  15506. Like deadlines, these entries only show up in the
  15507. diary for *today*, not at any other date.
  15508. The call in the diary file should look like this:
  15509. &%%(org-diary) ~/path/to/some/orgfile.org
  15510. Use a separate line for each org file to check. Or, if you omit the file name,
  15511. all files listed in `org-agenda-files' will be checked automatically:
  15512. &%%(org-diary)
  15513. If you don't give any arguments (as in the example above), the default
  15514. arguments (:deadline :scheduled :timestamp :sexp) are used.
  15515. So the example above may also be written as
  15516. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  15517. The function expects the lisp variables `entry' and `date' to be provided
  15518. by the caller, because this is how the calendar works. Don't use this
  15519. function from a program - use `org-agenda-get-day-entries' instead."
  15520. (org-agenda-maybe-reset-markers)
  15521. (org-compile-prefix-format 'agenda)
  15522. (org-set-sorting-strategy 'agenda)
  15523. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  15524. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  15525. (list entry)
  15526. (org-agenda-files t)))
  15527. file rtn results)
  15528. (org-prepare-agenda-buffers files)
  15529. ;; If this is called during org-agenda, don't return any entries to
  15530. ;; the calendar. Org Agenda will list these entries itself.
  15531. (if org-disable-agenda-to-diary (setq files nil))
  15532. (while (setq file (pop files))
  15533. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  15534. (setq results (append results rtn)))
  15535. (if results
  15536. (concat (org-finalize-agenda-entries results) "\n"))))
  15537. ;;; Agenda entry finders
  15538. (defun org-agenda-get-day-entries (file date &rest args)
  15539. "Does the work for `org-diary' and `org-agenda'.
  15540. FILE is the path to a file to be checked for entries. DATE is date like
  15541. the one returned by `calendar-current-date'. ARGS are symbols indicating
  15542. which kind of entries should be extracted. For details about these, see
  15543. the documentation of `org-diary'."
  15544. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  15545. (let* ((org-startup-folded nil)
  15546. (org-startup-align-all-tables nil)
  15547. (buffer (if (file-exists-p file)
  15548. (org-get-agenda-file-buffer file)
  15549. (error "No such file %s" file)))
  15550. arg results rtn)
  15551. (if (not buffer)
  15552. ;; If file does not exist, make sure an error message ends up in diary
  15553. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  15554. (with-current-buffer buffer
  15555. (unless (org-mode-p)
  15556. (error "Agenda file %s is not in `org-mode'" file))
  15557. (setq org-category-table (org-get-category-table))
  15558. (let ((case-fold-search nil))
  15559. (save-excursion
  15560. (save-restriction
  15561. (if org-agenda-restrict
  15562. (narrow-to-region org-agenda-restrict-begin
  15563. org-agenda-restrict-end)
  15564. (widen))
  15565. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  15566. (while (setq arg (pop args))
  15567. (cond
  15568. ((and (eq arg :todo)
  15569. (equal date (calendar-current-date)))
  15570. (setq rtn (org-agenda-get-todos))
  15571. (setq results (append results rtn)))
  15572. ((eq arg :timestamp)
  15573. (setq rtn (org-agenda-get-blocks))
  15574. (setq results (append results rtn))
  15575. (setq rtn (org-agenda-get-timestamps))
  15576. (setq results (append results rtn)))
  15577. ((eq arg :sexp)
  15578. (setq rtn (org-agenda-get-sexps))
  15579. (setq results (append results rtn)))
  15580. ((eq arg :scheduled)
  15581. (setq rtn (org-agenda-get-scheduled))
  15582. (setq results (append results rtn)))
  15583. ((eq arg :closed)
  15584. (setq rtn (org-agenda-get-closed))
  15585. (setq results (append results rtn)))
  15586. ((and (eq arg :deadline)
  15587. (equal date (calendar-current-date)))
  15588. (setq rtn (org-agenda-get-deadlines))
  15589. (setq results (append results rtn))))))))
  15590. results))))
  15591. ;; FIXME: this works only if the cursor is not at the
  15592. ;; beginning of the entry
  15593. (defun org-entry-is-done-p ()
  15594. "Is the current entry marked DONE?"
  15595. (save-excursion
  15596. (and (re-search-backward "[\r\n]\\*" nil t)
  15597. (looking-at org-nl-done-regexp))))
  15598. (defun org-at-date-range-p (&optional inactive-ok)
  15599. "Is the cursor inside a date range?"
  15600. (interactive)
  15601. (save-excursion
  15602. (catch 'exit
  15603. (let ((pos (point)))
  15604. (skip-chars-backward "^[<\r\n")
  15605. (skip-chars-backward "<[")
  15606. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  15607. (>= (match-end 0) pos)
  15608. (throw 'exit t))
  15609. (skip-chars-backward "^<[\r\n")
  15610. (skip-chars-backward "<[")
  15611. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  15612. (>= (match-end 0) pos)
  15613. (throw 'exit t)))
  15614. nil)))
  15615. (defun org-agenda-get-todos ()
  15616. "Return the TODO information for agenda display."
  15617. (let* ((props (list 'face nil
  15618. 'done-face 'org-done
  15619. 'org-not-done-regexp org-not-done-regexp
  15620. 'org-todo-regexp org-todo-regexp
  15621. 'mouse-face 'highlight
  15622. 'keymap org-agenda-keymap
  15623. 'help-echo
  15624. (format "mouse-2 or RET jump to org file %s"
  15625. (abbreviate-file-name buffer-file-name))))
  15626. ;; FIXME: get rid of the \n at some point but watch out
  15627. (regexp (concat "[\n\r]\\*+ *\\("
  15628. (if org-select-this-todo-keyword
  15629. (if (equal org-select-this-todo-keyword "*")
  15630. org-todo-regexp
  15631. (concat "\\<\\("
  15632. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  15633. "\\)\\>"))
  15634. org-not-done-regexp)
  15635. "[^\n\r]*\\)"))
  15636. marker priority category tags
  15637. ee txt beg end)
  15638. (goto-char (point-min))
  15639. (while (re-search-forward regexp nil t)
  15640. (catch :skip
  15641. (save-match-data
  15642. (beginning-of-line)
  15643. (setq beg (point) end (progn (outline-next-heading) (point)))
  15644. (when (or (and org-agenda-todo-ignore-scheduled (goto-char beg)
  15645. (re-search-forward org-scheduled-time-regexp end t))
  15646. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  15647. (re-search-forward org-deadline-time-regexp end t)
  15648. (org-deadline-close (match-string 1))))
  15649. (goto-char beg)
  15650. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  15651. (throw :skip nil)))
  15652. (goto-char beg)
  15653. (org-agenda-skip)
  15654. (goto-char (match-beginning 1))
  15655. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  15656. category (org-get-category)
  15657. tags (org-get-tags-at (point))
  15658. txt (org-format-agenda-item "" (match-string 1) category tags)
  15659. priority (1+ (org-get-priority txt)))
  15660. (org-add-props txt props
  15661. 'org-marker marker 'org-hd-marker marker
  15662. 'priority priority 'org-category category
  15663. 'type "todo")
  15664. (push txt ee)
  15665. (if org-agenda-todo-list-sublevels
  15666. (goto-char (match-end 1))
  15667. (org-end-of-subtree 'invisible))))
  15668. (nreverse ee)))
  15669. (defconst org-agenda-no-heading-message
  15670. "No heading for this item in buffer or region.")
  15671. (defun org-agenda-get-timestamps ()
  15672. "Return the date stamp information for agenda display."
  15673. (let* ((props (list 'face nil
  15674. 'org-not-done-regexp org-not-done-regexp
  15675. 'org-todo-regexp org-todo-regexp
  15676. 'mouse-face 'highlight
  15677. 'keymap org-agenda-keymap
  15678. 'help-echo
  15679. (format "mouse-2 or RET jump to org file %s"
  15680. (abbreviate-file-name buffer-file-name))))
  15681. ;???? (regexp (regexp-quote
  15682. ; (substring
  15683. ; (format-time-string
  15684. ; (car org-time-stamp-formats)
  15685. ; (apply 'encode-time ; DATE bound by calendar
  15686. ; (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  15687. ; 0 11)))
  15688. (d1 (calendar-absolute-from-gregorian date))
  15689. (regexp
  15690. (concat
  15691. (regexp-quote
  15692. (substring
  15693. (format-time-string
  15694. (car org-time-stamp-formats)
  15695. (apply 'encode-time ; DATE bound by calendar
  15696. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  15697. 0 11))
  15698. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  15699. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  15700. marker hdmarker deadlinep scheduledp donep tmp priority category
  15701. ee txt timestr tags b0 b3 e3)
  15702. (goto-char (point-min))
  15703. (while (re-search-forward regexp nil t)
  15704. (setq b0 (match-beginning 0)
  15705. b3 (match-beginning 3) e3 (match-end 3))
  15706. (catch :skip
  15707. (and (org-at-date-range-p) (throw :skip nil))
  15708. (org-agenda-skip)
  15709. (if (and (match-end 1)
  15710. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  15711. (throw :skip nil))
  15712. (if (and e3
  15713. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  15714. (throw :skip nil))
  15715. (setq marker (org-agenda-new-marker b0)
  15716. category (org-get-category b0)
  15717. tmp (buffer-substring (max (point-min)
  15718. (- b0 org-ds-keyword-length))
  15719. b0)
  15720. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  15721. deadlinep (string-match org-deadline-regexp tmp)
  15722. scheduledp (string-match org-scheduled-regexp tmp)
  15723. donep (org-entry-is-done-p))
  15724. (and org-agenda-skip-scheduled-if-done
  15725. scheduledp donep
  15726. (throw :skip t))
  15727. (and org-agenda-skip-deadline-if-done
  15728. deadlinep donep
  15729. (throw :skip t))
  15730. (if (string-match ">" timestr)
  15731. ;; substring should only run to end of time stamp
  15732. (setq timestr (substring timestr 0 (match-end 0))))
  15733. (save-excursion
  15734. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  15735. (progn
  15736. (goto-char (match-end 1))
  15737. (setq hdmarker (org-agenda-new-marker)
  15738. tags (org-get-tags-at))
  15739. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  15740. (setq txt (org-format-agenda-item
  15741. (format "%s%s"
  15742. (if deadlinep "Deadline: " "")
  15743. (if scheduledp "Scheduled: " ""))
  15744. (match-string 1) category tags timestr)))
  15745. (setq txt org-agenda-no-heading-message))
  15746. (setq priority (org-get-priority txt))
  15747. (org-add-props txt props
  15748. 'org-marker marker 'org-hd-marker hdmarker)
  15749. (if deadlinep
  15750. (org-add-props txt nil
  15751. 'face (if donep 'org-done 'org-warning)
  15752. 'type "deadline" 'date date
  15753. 'undone-face 'org-warning 'done-face 'org-done
  15754. 'org-category category 'priority (+ 100 priority))
  15755. (if scheduledp
  15756. (org-add-props txt nil
  15757. 'face 'org-scheduled-today
  15758. 'type "scheduled" 'date date
  15759. 'undone-face 'org-scheduled-today 'done-face 'org-done
  15760. 'org-category category 'priority (+ 99 priority))
  15761. (org-add-props txt nil 'priority priority
  15762. 'org-category category 'date date
  15763. 'type "timestamp")))
  15764. (push txt ee))
  15765. (outline-next-heading)))
  15766. (nreverse ee)))
  15767. (defun org-agenda-get-sexps ()
  15768. "Return the sexp information for agenda display."
  15769. (require 'diary-lib)
  15770. (let* ((props (list 'face nil
  15771. 'mouse-face 'highlight
  15772. 'keymap org-agenda-keymap
  15773. 'help-echo
  15774. (format "mouse-2 or RET jump to org file %s"
  15775. (abbreviate-file-name buffer-file-name))))
  15776. (regexp "^&?%%(")
  15777. marker category ee txt tags entry result beg b sexp sexp-entry)
  15778. (goto-char (point-min))
  15779. (while (re-search-forward regexp nil t)
  15780. (catch :skip
  15781. (org-agenda-skip)
  15782. (setq beg (match-beginning 0))
  15783. (goto-char (1- (match-end 0)))
  15784. (setq b (point))
  15785. (forward-sexp 1)
  15786. (setq sexp (buffer-substring b (point)))
  15787. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  15788. (org-trim (match-string 1))
  15789. ""))
  15790. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  15791. (when result
  15792. (setq marker (org-agenda-new-marker beg)
  15793. category (org-get-category beg))
  15794. (if (string-match "\\S-" result)
  15795. (setq txt result)
  15796. (setq txt "SEXP entry returned empty string"))
  15797. (setq txt (org-format-agenda-item
  15798. "" txt category tags 'time))
  15799. (org-add-props txt props 'org-marker marker)
  15800. (org-add-props txt nil
  15801. 'org-category category 'date date
  15802. 'type "sexp")
  15803. (push txt ee))))
  15804. (nreverse ee)))
  15805. (defun org-agenda-get-closed ()
  15806. "Return the logged TODO entries for agenda display."
  15807. (let* ((props (list 'mouse-face 'highlight
  15808. 'org-not-done-regexp org-not-done-regexp
  15809. 'org-todo-regexp org-todo-regexp
  15810. 'keymap org-agenda-keymap
  15811. 'help-echo
  15812. (format "mouse-2 or RET jump to org file %s"
  15813. (abbreviate-file-name buffer-file-name))))
  15814. (regexp (concat
  15815. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  15816. (regexp-quote
  15817. (substring
  15818. (format-time-string
  15819. (car org-time-stamp-formats)
  15820. (apply 'encode-time ; DATE bound by calendar
  15821. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  15822. 1 11))))
  15823. marker hdmarker priority category tags closedp
  15824. ee txt timestr)
  15825. (goto-char (point-min))
  15826. (while (re-search-forward regexp nil t)
  15827. (catch :skip
  15828. (org-agenda-skip)
  15829. (setq marker (org-agenda-new-marker (match-beginning 0))
  15830. closedp (equal (match-string 1) org-closed-string)
  15831. category (org-get-category (match-beginning 0))
  15832. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  15833. ;; donep (org-entry-is-done-p)
  15834. )
  15835. (if (string-match "\\]" timestr)
  15836. ;; substring should only run to end of time stamp
  15837. (setq timestr (substring timestr 0 (match-end 0))))
  15838. (save-excursion
  15839. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  15840. (progn
  15841. (goto-char (match-end 1))
  15842. (setq hdmarker (org-agenda-new-marker)
  15843. tags (org-get-tags-at))
  15844. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  15845. (setq txt (org-format-agenda-item
  15846. (if closedp "Closed: " "Clocked: ")
  15847. (match-string 1) category tags timestr)))
  15848. (setq txt org-agenda-no-heading-message))
  15849. (setq priority 100000)
  15850. (org-add-props txt props
  15851. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  15852. 'priority priority 'org-category category
  15853. 'type "closed" 'date date
  15854. 'undone-face 'org-warning 'done-face 'org-done)
  15855. (push txt ee))
  15856. (outline-next-heading)))
  15857. (nreverse ee)))
  15858. (defun org-agenda-get-deadlines ()
  15859. "Return the deadline information for agenda display."
  15860. (let* ((wdays org-deadline-warning-days)
  15861. (props (list 'mouse-face 'highlight
  15862. 'org-not-done-regexp org-not-done-regexp
  15863. 'org-todo-regexp org-todo-regexp
  15864. 'keymap org-agenda-keymap
  15865. 'help-echo
  15866. (format "mouse-2 or RET jump to org file %s"
  15867. (abbreviate-file-name buffer-file-name))))
  15868. (regexp org-deadline-time-regexp)
  15869. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  15870. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  15871. d2 diff pos pos1 category tags
  15872. ee txt head face)
  15873. (goto-char (point-min))
  15874. (while (re-search-forward regexp nil t)
  15875. (catch :skip
  15876. (org-agenda-skip)
  15877. (setq pos (1- (match-beginning 1))
  15878. ;??? d2 (time-to-days
  15879. ;??? (org-time-string-to-time (match-string 1)))
  15880. d2 (org-time-string-to-absolute (match-string 1) d1)
  15881. diff (- d2 d1))
  15882. ;; When to show a deadline in the calendar:
  15883. ;; If the expiration is within wdays warning time.
  15884. ;; Past-due deadlines are only shown on the current date
  15885. (if (and (< diff wdays) todayp (not (= diff 0)))
  15886. (save-excursion
  15887. (setq category (org-get-category))
  15888. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  15889. (progn
  15890. (goto-char (match-end 0))
  15891. (setq pos1 (match-end 1))
  15892. (setq tags (org-get-tags-at pos1))
  15893. (setq head (buffer-substring-no-properties
  15894. (point)
  15895. (progn (skip-chars-forward "^\r\n")
  15896. (point))))
  15897. (if (string-match org-looking-at-done-regexp head)
  15898. (setq txt nil)
  15899. (setq txt (org-format-agenda-item
  15900. (format "In %3d d.: " diff) head category tags))))
  15901. (setq txt org-agenda-no-heading-message))
  15902. (when txt
  15903. (setq face (cond ((<= diff 0) 'org-warning)
  15904. ((<= diff 5) 'org-upcoming-deadline)
  15905. (t nil)))
  15906. (org-add-props txt props
  15907. 'org-marker (org-agenda-new-marker pos)
  15908. 'org-hd-marker (org-agenda-new-marker pos1)
  15909. 'priority (+ (- 10 diff) (org-get-priority txt))
  15910. 'org-category category
  15911. 'type "upcoming-deadline" 'date d2
  15912. 'face face 'undone-face face 'done-face 'org-done)
  15913. (push txt ee))))))
  15914. ee))
  15915. (defun org-agenda-get-scheduled ()
  15916. "Return the scheduled information for agenda display."
  15917. (let* ((props (list 'face 'org-scheduled-previously
  15918. 'org-not-done-regexp org-not-done-regexp
  15919. 'org-todo-regexp org-todo-regexp
  15920. 'undone-face 'org-scheduled-previously
  15921. 'done-face 'org-done
  15922. 'mouse-face 'highlight
  15923. 'keymap org-agenda-keymap
  15924. 'help-echo
  15925. (format "mouse-2 or RET jump to org file %s"
  15926. (abbreviate-file-name buffer-file-name))))
  15927. (regexp org-scheduled-time-regexp)
  15928. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  15929. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  15930. d2 diff pos pos1 category tags
  15931. ee txt head)
  15932. (goto-char (point-min))
  15933. (while (re-search-forward regexp nil t)
  15934. (catch :skip
  15935. (org-agenda-skip)
  15936. (setq pos (1- (match-beginning 1))
  15937. d2 (org-time-string-to-absolute (match-string 1) d1)
  15938. ;??? d2 (time-to-days
  15939. ;??? (org-time-string-to-time (match-string 1)))
  15940. diff (- d2 d1))
  15941. ;; When to show a scheduled item in the calendar:
  15942. ;; If it is on or past the date.
  15943. (if (and (< diff 0) todayp)
  15944. (save-excursion
  15945. (setq category (org-get-category))
  15946. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  15947. (progn
  15948. (goto-char (match-end 0))
  15949. (setq pos1 (match-end 1))
  15950. (setq tags (org-get-tags-at))
  15951. (setq head (buffer-substring-no-properties
  15952. (point)
  15953. (progn (skip-chars-forward "^\r\n") (point))))
  15954. (if (string-match org-looking-at-done-regexp head)
  15955. (setq txt nil)
  15956. (setq txt (org-format-agenda-item
  15957. (format "Sched.%2dx: " (- 1 diff)) head
  15958. category tags))))
  15959. (setq txt org-agenda-no-heading-message))
  15960. (when txt
  15961. (org-add-props txt props
  15962. 'org-marker (org-agenda-new-marker pos)
  15963. 'org-hd-marker (org-agenda-new-marker pos1)
  15964. 'type "past-scheduled" 'date d2
  15965. 'priority (+ (- 5 diff) (org-get-priority txt))
  15966. 'org-category category)
  15967. (push txt ee))))))
  15968. ee))
  15969. (defun org-agenda-get-blocks ()
  15970. "Return the date-range information for agenda display."
  15971. (let* ((props (list 'face nil
  15972. 'org-not-done-regexp org-not-done-regexp
  15973. 'org-todo-regexp org-todo-regexp
  15974. 'mouse-face 'highlight
  15975. 'keymap org-agenda-keymap
  15976. 'help-echo
  15977. (format "mouse-2 or RET jump to org file %s"
  15978. (abbreviate-file-name buffer-file-name))))
  15979. (regexp org-tr-regexp)
  15980. (d0 (calendar-absolute-from-gregorian date))
  15981. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  15982. (goto-char (point-min))
  15983. (while (re-search-forward regexp nil t)
  15984. (catch :skip
  15985. (org-agenda-skip)
  15986. (setq pos (point))
  15987. (setq timestr (match-string 0)
  15988. s1 (match-string 1)
  15989. s2 (match-string 2)
  15990. d1 (time-to-days (org-time-string-to-time s1))
  15991. d2 (time-to-days (org-time-string-to-time s2)))
  15992. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  15993. ;; Only allow days between the limits, because the normal
  15994. ;; date stamps will catch the limits.
  15995. (save-excursion
  15996. (setq marker (org-agenda-new-marker (point)))
  15997. (setq category (org-get-category))
  15998. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  15999. (progn
  16000. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  16001. (goto-char (match-end 1))
  16002. (setq tags (org-get-tags-at))
  16003. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  16004. (setq txt (org-format-agenda-item
  16005. (format (if (= d1 d2) "" "(%d/%d): ")
  16006. (1+ (- d0 d1)) (1+ (- d2 d1)))
  16007. (match-string 1) category tags
  16008. (if (= d0 d1) timestr))))
  16009. (setq txt org-agenda-no-heading-message))
  16010. (org-add-props txt props
  16011. 'org-marker marker 'org-hd-marker hdmarker
  16012. 'type "block" 'date date
  16013. 'priority (org-get-priority txt) 'org-category category)
  16014. (push txt ee)))
  16015. (goto-char pos)))
  16016. ;; Sort the entries by expiration date.
  16017. (nreverse ee)))
  16018. ;;; Agenda presentation and sorting
  16019. (defconst org-plain-time-of-day-regexp
  16020. (concat
  16021. "\\(\\<[012]?[0-9]"
  16022. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  16023. "\\(--?"
  16024. "\\(\\<[012]?[0-9]"
  16025. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  16026. "\\)?")
  16027. "Regular expression to match a plain time or time range.
  16028. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  16029. groups carry important information:
  16030. 0 the full match
  16031. 1 the first time, range or not
  16032. 8 the second time, if it is a range.")
  16033. (defconst org-stamp-time-of-day-regexp
  16034. (concat
  16035. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  16036. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  16037. "\\(--?"
  16038. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  16039. "Regular expression to match a timestamp time or time range.
  16040. After a match, the following groups carry important information:
  16041. 0 the full match
  16042. 1 date plus weekday, for backreferencing to make sure both times on same day
  16043. 2 the first time, range or not
  16044. 4 the second time, if it is a range.")
  16045. (defvar org-prefix-has-time nil
  16046. "A flag, set by `org-compile-prefix-format'.
  16047. The flag is set if the currently compiled format contains a `%t'.")
  16048. (defvar org-prefix-has-tag nil
  16049. "A flag, set by `org-compile-prefix-format'.
  16050. The flag is set if the currently compiled format contains a `%T'.")
  16051. (defun org-format-agenda-item (extra txt &optional category tags dotime
  16052. noprefix)
  16053. "Format TXT to be inserted into the agenda buffer.
  16054. In particular, it adds the prefix and corresponding text properties. EXTRA
  16055. must be a string and replaces the `%s' specifier in the prefix format.
  16056. CATEGORY (string, symbol or nil) may be used to overrule the default
  16057. category taken from local variable or file name. It will replace the `%c'
  16058. specifier in the format. DOTIME, when non-nil, indicates that a
  16059. time-of-day should be extracted from TXT for sorting of this entry, and for
  16060. the `%t' specifier in the format. When DOTIME is a string, this string is
  16061. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  16062. only the correctly processes TXT should be returned - this is used by
  16063. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  16064. (save-match-data
  16065. ;; Diary entries sometimes have extra whitespace at the beginning
  16066. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  16067. (let* ((category (or category
  16068. org-category
  16069. (if buffer-file-name
  16070. (file-name-sans-extension
  16071. (file-name-nondirectory buffer-file-name))
  16072. "")))
  16073. (tag (if tags (nth (1- (length tags)) tags) ""))
  16074. time ; time and tag are needed for the eval of the prefix format
  16075. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  16076. (time-of-day (and dotime (org-get-time-of-day ts)))
  16077. stamp plain s0 s1 s2 rtn srp)
  16078. (when (and dotime time-of-day org-prefix-has-time)
  16079. ;; Extract starting and ending time and move them to prefix
  16080. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  16081. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  16082. (setq s0 (match-string 0 ts)
  16083. srp (and stamp (match-end 3))
  16084. s1 (match-string (if plain 1 2) ts)
  16085. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  16086. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  16087. ;; them, we might want to remove them there to avoid duplication.
  16088. ;; The user can turn this off with a variable.
  16089. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  16090. (string-match (concat (regexp-quote s0) " *") txt)
  16091. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  16092. (= (match-beginning 0) 0)
  16093. t))
  16094. (setq txt (replace-match "" nil nil txt))))
  16095. ;; Normalize the time(s) to 24 hour
  16096. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  16097. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  16098. (when (and s1 (not s2) org-agenda-default-appointment-duration
  16099. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  16100. (let ((m (+ (string-to-number (match-string 2 s1))
  16101. (* 60 (string-to-number (match-string 1 s1)))
  16102. org-agenda-default-appointment-duration))
  16103. h)
  16104. (setq h (/ m 60) m (- m (* h 60)))
  16105. (setq s2 (format "%02d:%02d" h m))))
  16106. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  16107. txt)
  16108. ;; Tags are in the string
  16109. (if (or (eq org-agenda-remove-tags t)
  16110. (and org-agenda-remove-tags
  16111. org-prefix-has-tag))
  16112. (setq txt (replace-match "" t t txt))
  16113. (setq txt (replace-match
  16114. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  16115. (match-string 2 txt))
  16116. t t txt))))
  16117. ;; Create the final string
  16118. (if noprefix
  16119. (setq rtn txt)
  16120. ;; Prepare the variables needed in the eval of the compiled format
  16121. (setq time (cond (s2 (concat s1 "-" s2))
  16122. (s1 (concat s1 "......"))
  16123. (t ""))
  16124. extra (or extra "")
  16125. category (if (symbolp category) (symbol-name category) category))
  16126. ;; Evaluate the compiled format
  16127. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  16128. ;; And finally add the text properties
  16129. (org-add-props rtn nil
  16130. 'org-category (downcase category) 'tags tags
  16131. 'prefix-length (- (length rtn) (length txt))
  16132. 'time-of-day time-of-day
  16133. 'txt txt
  16134. 'time time
  16135. 'extra extra
  16136. 'dotime dotime))))
  16137. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  16138. (defvar org-agenda-sorting-strategy-selected nil)
  16139. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  16140. (catch 'exit
  16141. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  16142. ((and todayp (member 'today (car org-agenda-time-grid))))
  16143. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  16144. ((member 'weekly (car org-agenda-time-grid)))
  16145. (t (throw 'exit list)))
  16146. (let* ((have (delq nil (mapcar
  16147. (lambda (x) (get-text-property 1 'time-of-day x))
  16148. list)))
  16149. (string (nth 1 org-agenda-time-grid))
  16150. (gridtimes (nth 2 org-agenda-time-grid))
  16151. (req (car org-agenda-time-grid))
  16152. (remove (member 'remove-match req))
  16153. new time)
  16154. (if (and (member 'require-timed req) (not have))
  16155. ;; don't show empty grid
  16156. (throw 'exit list))
  16157. (while (setq time (pop gridtimes))
  16158. (unless (and remove (member time have))
  16159. (setq time (int-to-string time))
  16160. (push (org-format-agenda-item
  16161. nil string "" nil
  16162. (concat (substring time 0 -2) ":" (substring time -2)))
  16163. new)
  16164. (put-text-property
  16165. 1 (length (car new)) 'face 'org-time-grid (car new))))
  16166. (if (member 'time-up org-agenda-sorting-strategy-selected)
  16167. (append new list)
  16168. (append list new)))))
  16169. (defun org-compile-prefix-format (key)
  16170. "Compile the prefix format into a Lisp form that can be evaluated.
  16171. The resulting form is returned and stored in the variable
  16172. `org-prefix-format-compiled'."
  16173. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  16174. (let ((s (cond
  16175. ((stringp org-agenda-prefix-format)
  16176. org-agenda-prefix-format)
  16177. ((assq key org-agenda-prefix-format)
  16178. (cdr (assq key org-agenda-prefix-format)))
  16179. (t " %-12:c%?-12t% s")))
  16180. (start 0)
  16181. varform vars var e c f opt)
  16182. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  16183. s start)
  16184. (setq var (cdr (assoc (match-string 4 s)
  16185. '(("c" . category) ("t" . time) ("s" . extra)
  16186. ("T" . tag))))
  16187. c (or (match-string 3 s) "")
  16188. opt (match-beginning 1)
  16189. start (1+ (match-beginning 0)))
  16190. (if (equal var 'time) (setq org-prefix-has-time t))
  16191. (if (equal var 'tag) (setq org-prefix-has-tag t))
  16192. (setq f (concat "%" (match-string 2 s) "s"))
  16193. (if opt
  16194. (setq varform
  16195. `(if (equal "" ,var)
  16196. ""
  16197. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  16198. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  16199. (setq s (replace-match "%s" t nil s))
  16200. (push varform vars))
  16201. (setq vars (nreverse vars))
  16202. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  16203. (defun org-set-sorting-strategy (key)
  16204. (if (symbolp (car org-agenda-sorting-strategy))
  16205. ;; the old format
  16206. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  16207. (setq org-agenda-sorting-strategy-selected
  16208. (or (cdr (assq key org-agenda-sorting-strategy))
  16209. (cdr (assq 'agenda org-agenda-sorting-strategy))
  16210. '(time-up category-keep priority-down)))))
  16211. (defun org-get-time-of-day (s &optional string mod24)
  16212. "Check string S for a time of day.
  16213. If found, return it as a military time number between 0 and 2400.
  16214. If not found, return nil.
  16215. The optional STRING argument forces conversion into a 5 character wide string
  16216. HH:MM."
  16217. (save-match-data
  16218. (when
  16219. (or
  16220. (string-match
  16221. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  16222. (string-match
  16223. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  16224. (let* ((h (string-to-number (match-string 1 s)))
  16225. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  16226. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  16227. (am-p (equal ampm "am"))
  16228. (h1 (cond ((not ampm) h)
  16229. ((= h 12) (if am-p 0 12))
  16230. (t (+ h (if am-p 0 12)))))
  16231. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  16232. (mod h1 24) h1))
  16233. (t0 (+ (* 100 h2) m))
  16234. (t1 (concat (if (>= h1 24) "+" " ")
  16235. (if (< t0 100) "0" "")
  16236. (if (< t0 10) "0" "")
  16237. (int-to-string t0))))
  16238. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  16239. (defun org-finalize-agenda-entries (list &optional nosort)
  16240. "Sort and concatenate the agenda items."
  16241. (setq list (mapcar 'org-agenda-highlight-todo list))
  16242. (if nosort
  16243. list
  16244. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  16245. (defun org-agenda-highlight-todo (x)
  16246. (let (re pl)
  16247. (if (eq x 'line)
  16248. (save-excursion
  16249. (beginning-of-line 1)
  16250. (setq re (get-text-property (point) 'org-not-done-regexp))
  16251. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  16252. (and (looking-at (concat "[ \t]*\\.*" re))
  16253. (add-text-properties (match-beginning 0) (match-end 0)
  16254. '(face org-todo))))
  16255. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  16256. pl (get-text-property 0 'prefix-length x))
  16257. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  16258. (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
  16259. '(face org-todo) x))
  16260. x)))
  16261. (defsubst org-cmp-priority (a b)
  16262. "Compare the priorities of string A and B."
  16263. (let ((pa (or (get-text-property 1 'priority a) 0))
  16264. (pb (or (get-text-property 1 'priority b) 0)))
  16265. (cond ((> pa pb) +1)
  16266. ((< pa pb) -1)
  16267. (t nil))))
  16268. (defsubst org-cmp-category (a b)
  16269. "Compare the string values of categories of strings A and B."
  16270. (let ((ca (or (get-text-property 1 'category a) ""))
  16271. (cb (or (get-text-property 1 'category b) "")))
  16272. (cond ((string-lessp ca cb) -1)
  16273. ((string-lessp cb ca) +1)
  16274. (t nil))))
  16275. (defsubst org-cmp-tag (a b)
  16276. "Compare the string values of categories of strings A and B."
  16277. (let ((ta (car (last (get-text-property 1 'tags a))))
  16278. (tb (car (last (get-text-property 1 'tags b)))))
  16279. (cond ((not ta) +1)
  16280. ((not tb) -1)
  16281. ((string-lessp ta tb) -1)
  16282. ((string-lessp tb ta) +1)
  16283. (t nil))))
  16284. (defsubst org-cmp-time (a b)
  16285. "Compare the time-of-day values of strings A and B."
  16286. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  16287. (ta (or (get-text-property 1 'time-of-day a) def))
  16288. (tb (or (get-text-property 1 'time-of-day b) def)))
  16289. (cond ((< ta tb) -1)
  16290. ((< tb ta) +1)
  16291. (t nil))))
  16292. (defun org-entries-lessp (a b)
  16293. "Predicate for sorting agenda entries."
  16294. ;; The following variables will be used when the form is evaluated.
  16295. ;; So even though the compiler complains, keep them.
  16296. (let* ((time-up (org-cmp-time a b))
  16297. (time-down (if time-up (- time-up) nil))
  16298. (priority-up (org-cmp-priority a b))
  16299. (priority-down (if priority-up (- priority-up) nil))
  16300. (category-up (org-cmp-category a b))
  16301. (category-down (if category-up (- category-up) nil))
  16302. (category-keep (if category-up +1 nil))
  16303. (tag-up (org-cmp-tag a b))
  16304. (tag-down (if tag-up (- tag-up) nil)))
  16305. (cdr (assoc
  16306. (eval (cons 'or org-agenda-sorting-strategy-selected))
  16307. '((-1 . t) (1 . nil) (nil . nil))))))
  16308. ;;; Agenda commands
  16309. (defun org-agenda-check-type (error &rest types)
  16310. "Check if agenda buffer is of allowed type.
  16311. If ERROR is non-nil, throw an error, otherwise just return nil."
  16312. (if (memq org-agenda-type types)
  16313. t
  16314. (if error
  16315. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  16316. nil)))
  16317. (defun org-agenda-quit ()
  16318. "Exit agenda by removing the window or the buffer."
  16319. (interactive)
  16320. (let ((buf (current-buffer)))
  16321. (if (not (one-window-p)) (delete-window))
  16322. (kill-buffer buf)
  16323. (org-agenda-maybe-reset-markers 'force)
  16324. (org-remove-column-overlays))
  16325. ;; Maybe restore the pre-agenda window configuration.
  16326. (and org-agenda-restore-windows-after-quit
  16327. (not (eq org-agenda-window-setup 'other-frame))
  16328. org-pre-agenda-window-conf
  16329. (set-window-configuration org-pre-agenda-window-conf)))
  16330. (defun org-agenda-exit ()
  16331. "Exit agenda by removing the window or the buffer.
  16332. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  16333. Org-mode buffers visited directly by the user will not be touched."
  16334. (interactive)
  16335. (org-release-buffers org-agenda-new-buffers)
  16336. (setq org-agenda-new-buffers nil)
  16337. (org-agenda-quit))
  16338. (defun org-save-all-org-buffers ()
  16339. "Save all Org-mode buffers without user confirmation."
  16340. (interactive)
  16341. (message "Saving all Org-mode buffers...")
  16342. (save-some-buffers t 'org-mode-p)
  16343. (message "Saving all Org-mode buffers... done"))
  16344. (defun org-agenda-redo ()
  16345. "Rebuild Agenda.
  16346. When this is the global TODO list, a prefix argument will be interpreted."
  16347. (interactive)
  16348. (let* ((org-agenda-keep-modes t)
  16349. (line (org-current-line))
  16350. (window-line (- line (org-current-line (window-start)))))
  16351. (message "Rebuilding agenda buffer...")
  16352. (eval org-agenda-redo-command)
  16353. (setq org-agenda-undo-list nil
  16354. org-agenda-pending-undo-list nil)
  16355. (message "Rebuilding agenda buffer...done")
  16356. (goto-line line)
  16357. (recenter window-line)))
  16358. (defun org-agenda-goto-today ()
  16359. "Go to today."
  16360. (interactive)
  16361. (org-agenda-check-type t 'timeline 'agenda)
  16362. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  16363. (cond
  16364. (tdpos (goto-char tdpos))
  16365. ((eq org-agenda-type 'agenda)
  16366. (let* ((sd (time-to-days (current-time)))
  16367. (comp (org-agenda-compute-time-span sd org-agenda-span))
  16368. (org-agenda-overriding-arguments org-agenda-last-arguments))
  16369. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  16370. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  16371. (org-agenda-redo)
  16372. (org-agenda-find-today-or-agenda)))
  16373. (t (error "Cannot find today")))))
  16374. (defun org-agenda-find-today-or-agenda ()
  16375. (goto-char
  16376. (or (text-property-any (point-min) (point-max) 'org-today t)
  16377. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  16378. (point-min))))
  16379. (defun org-agenda-later (arg)
  16380. "Go forward in time by thee current span.
  16381. With prefix ARG, go forward that many times the current span."
  16382. (interactive "p")
  16383. (org-agenda-check-type t 'agenda)
  16384. (let* ((span org-agenda-span)
  16385. (sd org-starting-day)
  16386. (greg (calendar-gregorian-from-absolute sd))
  16387. greg2 nd)
  16388. (cond
  16389. ((eq span 'day)
  16390. (setq sd (+ arg sd) nd 1))
  16391. ((eq span 'week)
  16392. (setq sd (+ (* 7 arg) sd) nd 7))
  16393. ((eq span 'month)
  16394. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  16395. sd (calendar-absolute-from-gregorian greg2))
  16396. (setcar greg2 (1+ (car greg2)))
  16397. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  16398. ((eq span 'year)
  16399. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  16400. sd (calendar-absolute-from-gregorian greg2))
  16401. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  16402. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  16403. (let ((org-agenda-overriding-arguments
  16404. (list (car org-agenda-last-arguments) sd nd t)))
  16405. (org-agenda-redo)
  16406. (org-agenda-find-today-or-agenda))))
  16407. (defun org-agenda-earlier (arg)
  16408. "Go backward in time by the current span.
  16409. With prefix ARG, go backward that many times the current span."
  16410. (interactive "p")
  16411. (org-agenda-later (- arg)))
  16412. (defun org-agenda-day-view ()
  16413. "Switch to daily view for agenda."
  16414. (interactive)
  16415. (org-agenda-change-time-span 'day))
  16416. (defun org-agenda-week-view ()
  16417. "Switch to daily view for agenda."
  16418. (interactive)
  16419. (org-agenda-change-time-span 'week))
  16420. (defun org-agenda-month-view ()
  16421. "Switch to daily view for agenda."
  16422. (interactive)
  16423. (org-agenda-change-time-span 'month))
  16424. (defun org-agenda-year-view ()
  16425. "Switch to daily view for agenda."
  16426. (interactive)
  16427. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  16428. (org-agenda-change-time-span 'year)
  16429. (error "Abort")))
  16430. (defun org-agenda-change-time-span (span)
  16431. "Change the agenda view to SPAN.
  16432. SPAN may be `day', `week', `month', `year'."
  16433. (org-agenda-check-type t 'agenda)
  16434. (if (equal org-agenda-span span)
  16435. (error "Viewing span is already \"%s\"" span))
  16436. (let* ((sd (or (get-text-property (point) 'day)
  16437. org-starting-day))
  16438. (computed (org-agenda-compute-time-span sd span))
  16439. (org-agenda-overriding-arguments
  16440. (list (car org-agenda-last-arguments)
  16441. (car computed) (cdr computed) t)))
  16442. (org-agenda-redo)
  16443. (org-agenda-find-today-or-agenda))
  16444. (org-agenda-set-mode-name)
  16445. (message "Switched to %s view" span))
  16446. (defun org-agenda-compute-time-span (sd span)
  16447. "Compute starting date and number of days for agenda.
  16448. SPAN may be `day', `week', `month', `year'. The return value
  16449. is a cons cell with the starting date and the number of days,
  16450. so that the date SD will be in that range."
  16451. (let* ((greg (calendar-gregorian-from-absolute sd))
  16452. nd)
  16453. (cond
  16454. ((eq span 'day)
  16455. (setq nd 1))
  16456. ((eq span 'week)
  16457. (let* ((nt (calendar-day-of-week
  16458. (calendar-gregorian-from-absolute sd)))
  16459. (n1 org-agenda-start-on-weekday)
  16460. (d (- nt n1)))
  16461. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  16462. (setq nd 7)))
  16463. ((eq span 'month)
  16464. (setq sd (calendar-absolute-from-gregorian
  16465. (list (car greg) 1 (nth 2 greg)))
  16466. nd (- (calendar-absolute-from-gregorian
  16467. (list (1+ (car greg)) 1 (nth 2 greg)))
  16468. sd)))
  16469. ((eq span 'year)
  16470. (setq sd (calendar-absolute-from-gregorian
  16471. (list 1 1 (nth 2 greg)))
  16472. nd (- (calendar-absolute-from-gregorian
  16473. (list 1 1 (1+ (nth 2 greg))))
  16474. sd))))
  16475. (cons sd nd)))
  16476. ;; FIXME: this no longer works if user make date format that starts with a blank
  16477. (defun org-agenda-next-date-line (&optional arg)
  16478. "Jump to the next line indicating a date in agenda buffer."
  16479. (interactive "p")
  16480. (org-agenda-check-type t 'agenda 'timeline)
  16481. (beginning-of-line 1)
  16482. (if (looking-at "^\\S-") (forward-char 1))
  16483. (if (not (re-search-forward "^\\S-" nil t arg))
  16484. (progn
  16485. (backward-char 1)
  16486. (error "No next date after this line in this buffer")))
  16487. (goto-char (match-beginning 0)))
  16488. (defun org-agenda-previous-date-line (&optional arg)
  16489. "Jump to the previous line indicating a date in agenda buffer."
  16490. (interactive "p")
  16491. (org-agenda-check-type t 'agenda 'timeline)
  16492. (beginning-of-line 1)
  16493. (if (not (re-search-backward "^\\S-" nil t arg))
  16494. (error "No previous date before this line in this buffer")))
  16495. ;; Initialize the highlight
  16496. (defvar org-hl (org-make-overlay 1 1))
  16497. (org-overlay-put org-hl 'face 'highlight)
  16498. (defun org-highlight (begin end &optional buffer)
  16499. "Highlight a region with overlay."
  16500. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  16501. org-hl begin end (or buffer (current-buffer))))
  16502. (defun org-unhighlight ()
  16503. "Detach overlay INDEX."
  16504. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  16505. ;; FIXME this is currently not used.
  16506. (defun org-highlight-until-next-command (beg end &optional buffer)
  16507. (org-highlight beg end buffer)
  16508. (add-hook 'pre-command-hook 'org-unhighlight-once))
  16509. (defun org-unhighlight-once ()
  16510. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  16511. (org-unhighlight))
  16512. (defun org-agenda-follow-mode ()
  16513. "Toggle follow mode in an agenda buffer."
  16514. (interactive)
  16515. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  16516. (org-agenda-set-mode-name)
  16517. (message "Follow mode is %s"
  16518. (if org-agenda-follow-mode "on" "off")))
  16519. (defun org-agenda-log-mode ()
  16520. "Toggle log mode in an agenda buffer."
  16521. (interactive)
  16522. (org-agenda-check-type t 'agenda 'timeline)
  16523. (setq org-agenda-show-log (not org-agenda-show-log))
  16524. (org-agenda-set-mode-name)
  16525. (org-agenda-redo)
  16526. (message "Log mode is %s"
  16527. (if org-agenda-show-log "on" "off")))
  16528. (defun org-agenda-toggle-diary ()
  16529. "Toggle diary inclusion in an agenda buffer."
  16530. (interactive)
  16531. (org-agenda-check-type t 'agenda)
  16532. (setq org-agenda-include-diary (not org-agenda-include-diary))
  16533. (org-agenda-redo)
  16534. (org-agenda-set-mode-name)
  16535. (message "Diary inclusion turned %s"
  16536. (if org-agenda-include-diary "on" "off")))
  16537. (defun org-agenda-toggle-time-grid ()
  16538. "Toggle time grid in an agenda buffer."
  16539. (interactive)
  16540. (org-agenda-check-type t 'agenda)
  16541. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  16542. (org-agenda-redo)
  16543. (org-agenda-set-mode-name)
  16544. (message "Time-grid turned %s"
  16545. (if org-agenda-use-time-grid "on" "off")))
  16546. (defun org-agenda-set-mode-name ()
  16547. "Set the mode name to indicate all the small mode settings."
  16548. (setq mode-name
  16549. (concat "Org-Agenda"
  16550. (if (equal org-agenda-ndays 1) " Day" "")
  16551. (if (equal org-agenda-ndays 7) " Week" "")
  16552. (if org-agenda-follow-mode " Follow" "")
  16553. (if org-agenda-include-diary " Diary" "")
  16554. (if org-agenda-use-time-grid " Grid" "")
  16555. (if org-agenda-show-log " Log" "")))
  16556. (force-mode-line-update))
  16557. (defun org-agenda-post-command-hook ()
  16558. (and (eolp) (not (bolp)) (backward-char 1))
  16559. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  16560. (if (and org-agenda-follow-mode
  16561. (get-text-property (point) 'org-marker))
  16562. (org-agenda-show)))
  16563. (defun org-agenda-show-priority ()
  16564. "Show the priority of the current item.
  16565. This priority is composed of the main priority given with the [#A] cookies,
  16566. and by additional input from the age of a schedules or deadline entry."
  16567. (interactive)
  16568. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  16569. (message "Priority is %d" (if pri pri -1000))))
  16570. (defun org-agenda-show-tags ()
  16571. "Show the tags applicable to the current item."
  16572. (interactive)
  16573. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  16574. (if tags
  16575. (message "Tags are :%s:"
  16576. (org-no-properties (mapconcat 'identity tags ":")))
  16577. (message "No tags associated with this line"))))
  16578. (defun org-agenda-goto (&optional highlight)
  16579. "Go to the Org-mode file which contains the item at point."
  16580. (interactive)
  16581. (let* ((marker (or (get-text-property (point) 'org-marker)
  16582. (org-agenda-error)))
  16583. (buffer (marker-buffer marker))
  16584. (pos (marker-position marker)))
  16585. (switch-to-buffer-other-window buffer)
  16586. (widen)
  16587. (goto-char pos)
  16588. (when (org-mode-p)
  16589. (org-show-context 'agenda)
  16590. (save-excursion
  16591. (and (outline-next-heading)
  16592. (org-flag-heading nil)))) ; show the next heading
  16593. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  16594. (defun org-agenda-kill ()
  16595. "Kill the entry or subtree belonging to the current agenda entry."
  16596. (interactive)
  16597. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  16598. (let* ((marker (or (get-text-property (point) 'org-marker)
  16599. (org-agenda-error)))
  16600. (buffer (marker-buffer marker))
  16601. (pos (marker-position marker))
  16602. (type (get-text-property (point) 'type))
  16603. dbeg dend (n 0) conf)
  16604. (org-with-remote-undo buffer
  16605. (with-current-buffer buffer
  16606. (save-excursion
  16607. (goto-char pos)
  16608. (if (and (org-mode-p) (not (member type '("sexp"))))
  16609. (setq dbeg (progn (org-back-to-heading t) (point))
  16610. dend (org-end-of-subtree t))
  16611. (setq dbeg (point-at-bol)
  16612. dend (min (point-max) (1+ (point-at-eol)))))
  16613. (goto-char dbeg)
  16614. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  16615. (setq conf (or (eq t org-agenda-confirm-kill)
  16616. (and (numberp org-agenda-confirm-kill)
  16617. (> n org-agenda-confirm-kill))))
  16618. (and conf
  16619. (not (y-or-n-p
  16620. (format "Delete entry with %d lines in buffer \"%s\"? "
  16621. n (buffer-name buffer))))
  16622. (error "Abort"))
  16623. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  16624. (with-current-buffer buffer (delete-region dbeg dend))
  16625. (message "Agenda item and source killed"))))
  16626. (defun org-agenda-archive ()
  16627. "Kill the entry or subtree belonging to the current agenda entry."
  16628. (interactive)
  16629. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  16630. (let* ((marker (or (get-text-property (point) 'org-marker)
  16631. (org-agenda-error)))
  16632. (buffer (marker-buffer marker))
  16633. (pos (marker-position marker)))
  16634. (org-with-remote-undo buffer
  16635. (with-current-buffer buffer
  16636. (if (org-mode-p)
  16637. (save-excursion
  16638. (goto-char pos)
  16639. (org-remove-subtree-entries-from-agenda)
  16640. (org-back-to-heading t)
  16641. (org-archive-subtree))
  16642. (error "Archiving works only in Org-mode files"))))))
  16643. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  16644. "Remove all lines in the agenda that correspond to a given subtree.
  16645. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  16646. If this information is not given, the function uses the tree at point."
  16647. (let ((buf (or buf (current-buffer))) m p)
  16648. (save-excursion
  16649. (unless (and beg end)
  16650. (org-back-to-heading t)
  16651. (setq beg (point))
  16652. (org-end-of-subtree t)
  16653. (setq end (point)))
  16654. (set-buffer (get-buffer org-agenda-buffer-name))
  16655. (save-excursion
  16656. (goto-char (point-max))
  16657. (beginning-of-line 1)
  16658. (while (not (bobp))
  16659. (when (and (setq m (get-text-property (point) 'org-marker))
  16660. (equal buf (marker-buffer m))
  16661. (setq p (marker-position m))
  16662. (>= p beg)
  16663. (<= p end))
  16664. (let (buffer-read-only)
  16665. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  16666. (beginning-of-line 0))))))
  16667. (defun org-agenda-open-link ()
  16668. "Follow the link in the current line, if any."
  16669. (interactive)
  16670. (let ((eol (point-at-eol)))
  16671. (save-excursion
  16672. (if (or (re-search-forward org-bracket-link-regexp eol t)
  16673. (re-search-forward org-angle-link-re eol t)
  16674. (re-search-forward org-plain-link-re eol t))
  16675. (call-interactively 'org-open-at-point)
  16676. (error "No link in current line")))))
  16677. (defun org-agenda-switch-to (&optional delete-other-windows)
  16678. "Go to the Org-mode file which contains the item at point."
  16679. (interactive)
  16680. (let* ((marker (or (get-text-property (point) 'org-marker)
  16681. (org-agenda-error)))
  16682. (buffer (marker-buffer marker))
  16683. (pos (marker-position marker)))
  16684. (switch-to-buffer buffer)
  16685. (and delete-other-windows (delete-other-windows))
  16686. (widen)
  16687. (goto-char pos)
  16688. (when (org-mode-p)
  16689. (org-show-context 'agenda)
  16690. (save-excursion
  16691. (and (outline-next-heading)
  16692. (org-flag-heading nil)))))) ; show the next heading
  16693. (defun org-agenda-goto-mouse (ev)
  16694. "Go to the Org-mode file which contains the item at the mouse click."
  16695. (interactive "e")
  16696. (mouse-set-point ev)
  16697. (org-agenda-goto))
  16698. (defun org-agenda-show ()
  16699. "Display the Org-mode file which contains the item at point."
  16700. (interactive)
  16701. (let ((win (selected-window)))
  16702. (org-agenda-goto t)
  16703. (select-window win)))
  16704. (defun org-agenda-recenter (arg)
  16705. "Display the Org-mode file which contains the item at point and recenter."
  16706. (interactive "P")
  16707. (let ((win (selected-window)))
  16708. (org-agenda-goto t)
  16709. (recenter arg)
  16710. (select-window win)))
  16711. (defun org-agenda-show-mouse (ev)
  16712. "Display the Org-mode file which contains the item at the mouse click."
  16713. (interactive "e")
  16714. (mouse-set-point ev)
  16715. (org-agenda-show))
  16716. (defun org-agenda-check-no-diary ()
  16717. "Check if the entry is a diary link and abort if yes."
  16718. (if (get-text-property (point) 'org-agenda-diary-link)
  16719. (org-agenda-error)))
  16720. (defun org-agenda-error ()
  16721. (error "Command not allowed in this line"))
  16722. (defun org-agenda-tree-to-indirect-buffer ()
  16723. "Show the subtree corresponding to the current entry in an indirect buffer.
  16724. This calls the command `org-tree-to-indirect-buffer' from the original
  16725. Org-mode buffer.
  16726. With numerical prefix arg ARG, go up to this level and then take that tree.
  16727. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  16728. dedicated frame)."
  16729. (interactive)
  16730. (org-agenda-check-no-diary)
  16731. (let* ((marker (or (get-text-property (point) 'org-marker)
  16732. (org-agenda-error)))
  16733. (buffer (marker-buffer marker))
  16734. (pos (marker-position marker)))
  16735. (with-current-buffer buffer
  16736. (save-excursion
  16737. (goto-char pos)
  16738. (call-interactively 'org-tree-to-indirect-buffer)))))
  16739. (defvar org-last-heading-marker (make-marker)
  16740. "Marker pointing to the headline that last changed its TODO state
  16741. by a remote command from the agenda.")
  16742. (defun org-agenda-todo-nextset ()
  16743. "Switch TODO entry to next sequence."
  16744. (interactive)
  16745. (org-agenda-todo 'nextset))
  16746. (defun org-agenda-todo-previousset ()
  16747. "Switch TODO entry to previous sequence."
  16748. (interactive)
  16749. (org-agenda-todo 'previousset))
  16750. (defun org-agenda-todo (&optional arg)
  16751. "Cycle TODO state of line at point, also in Org-mode file.
  16752. This changes the line at point, all other lines in the agenda referring to
  16753. the same tree node, and the headline of the tree node in the Org-mode file."
  16754. (interactive "P")
  16755. (org-agenda-check-no-diary)
  16756. (let* ((col (current-column))
  16757. (marker (or (get-text-property (point) 'org-marker)
  16758. (org-agenda-error)))
  16759. (buffer (marker-buffer marker))
  16760. (pos (marker-position marker))
  16761. (hdmarker (get-text-property (point) 'org-hd-marker))
  16762. (buffer-read-only nil)
  16763. newhead)
  16764. (org-with-remote-undo buffer
  16765. (with-current-buffer buffer
  16766. (widen)
  16767. (goto-char pos)
  16768. (org-show-context 'agenda)
  16769. (save-excursion
  16770. (and (outline-next-heading)
  16771. (org-flag-heading nil))) ; show the next heading
  16772. (org-todo arg)
  16773. (and (bolp) (forward-char 1))
  16774. (setq newhead (org-get-heading))
  16775. (save-excursion
  16776. (org-back-to-heading)
  16777. (move-marker org-last-heading-marker (point))))
  16778. (beginning-of-line 1)
  16779. (save-excursion
  16780. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  16781. (move-to-column col))))
  16782. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  16783. "Change all lines in the agenda buffer which match HDMARKER.
  16784. The new content of the line will be NEWHEAD (as modified by
  16785. `org-format-agenda-item'). HDMARKER is checked with
  16786. `equal' against all `org-hd-marker' text properties in the file.
  16787. If FIXFACE is non-nil, the face of each item is modified acording to
  16788. the new TODO state."
  16789. (let* ((buffer-read-only nil)
  16790. props m pl undone-face done-face finish new dotime cat tags)
  16791. (save-excursion
  16792. (goto-char (point-max))
  16793. (beginning-of-line 1)
  16794. (while (not finish)
  16795. (setq finish (bobp))
  16796. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  16797. (equal m hdmarker))
  16798. (setq props (text-properties-at (point))
  16799. dotime (get-text-property (point) 'dotime)
  16800. cat (get-text-property (point) 'org-category)
  16801. tags (get-text-property (point) 'tags)
  16802. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  16803. pl (get-text-property (point) 'prefix-length)
  16804. undone-face (get-text-property (point) 'undone-face)
  16805. done-face (get-text-property (point) 'done-face))
  16806. (move-to-column pl)
  16807. (cond
  16808. ((equal new "")
  16809. (beginning-of-line 1)
  16810. (and (looking-at ".*\n?") (replace-match "")))
  16811. ((looking-at ".*")
  16812. (replace-match new t t)
  16813. (beginning-of-line 1)
  16814. (add-text-properties (point-at-bol) (point-at-eol) props)
  16815. (when fixface
  16816. (add-text-properties
  16817. (point-at-bol) (point-at-eol)
  16818. (list 'face
  16819. (if org-last-todo-state-is-todo
  16820. undone-face done-face))))
  16821. (org-agenda-highlight-todo 'line)
  16822. (beginning-of-line 1))
  16823. (t (error "Line update did not work"))))
  16824. (beginning-of-line 0)))
  16825. (org-finalize-agenda)))
  16826. ;; FIXME: allow negative value for org-agenda-align-tags-to-column
  16827. ;; See the code in set-tags for the way to do this.
  16828. (defun org-agenda-align-tags (&optional line)
  16829. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  16830. (let ((buffer-read-only))
  16831. (save-excursion
  16832. (goto-char (if line (point-at-bol) (point-min)))
  16833. (while (re-search-forward (org-re "\\([ \t]+\\):[[:alnum:]_@:]+:[ \t]*$")
  16834. (if line (point-at-eol) nil) t)
  16835. (delete-region (match-beginning 1) (match-end 1))
  16836. (goto-char (match-beginning 1))
  16837. (insert (org-add-props
  16838. (make-string (max 1 (- org-agenda-align-tags-to-column
  16839. (current-column))) ?\ )
  16840. (text-properties-at (point))))))))
  16841. (defun org-agenda-priority-up ()
  16842. "Increase the priority of line at point, also in Org-mode file."
  16843. (interactive)
  16844. (org-agenda-priority 'up))
  16845. (defun org-agenda-priority-down ()
  16846. "Decrease the priority of line at point, also in Org-mode file."
  16847. (interactive)
  16848. (org-agenda-priority 'down))
  16849. (defun org-agenda-priority (&optional force-direction)
  16850. "Set the priority of line at point, also in Org-mode file.
  16851. This changes the line at point, all other lines in the agenda referring to
  16852. the same tree node, and the headline of the tree node in the Org-mode file."
  16853. (interactive)
  16854. (org-agenda-check-no-diary)
  16855. (let* ((marker (or (get-text-property (point) 'org-marker)
  16856. (org-agenda-error)))
  16857. (buffer (marker-buffer marker))
  16858. (pos (marker-position marker))
  16859. (hdmarker (get-text-property (point) 'org-hd-marker))
  16860. (buffer-read-only nil)
  16861. newhead)
  16862. (org-with-remote-undo buffer
  16863. (with-current-buffer buffer
  16864. (widen)
  16865. (goto-char pos)
  16866. (org-show-context 'agenda)
  16867. (save-excursion
  16868. (and (outline-next-heading)
  16869. (org-flag-heading nil))) ; show the next heading
  16870. (funcall 'org-priority force-direction)
  16871. (end-of-line 1)
  16872. (setq newhead (org-get-heading)))
  16873. (org-agenda-change-all-lines newhead hdmarker)
  16874. (beginning-of-line 1))))
  16875. (defun org-get-tags-at (&optional pos)
  16876. "Get a list of all headline tags applicable at POS.
  16877. POS defaults to point. If tags are inherited, the list contains
  16878. the targets in the same sequence as the headlines appear, i.e.
  16879. the tags of the current headline come last."
  16880. (interactive)
  16881. (let (tags)
  16882. (save-excursion
  16883. (save-restriction
  16884. (widen)
  16885. (goto-char (or pos (point)))
  16886. (save-match-data
  16887. (org-back-to-heading t)
  16888. (condition-case nil
  16889. (while t
  16890. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*\\([\n\r]\\|\\'\\)"))
  16891. (setq tags (append (org-split-string
  16892. (org-match-string-no-properties 1) ":")
  16893. tags)))
  16894. (or org-use-tag-inheritance (error ""))
  16895. (org-up-heading-all 1))
  16896. (error nil))))
  16897. tags)))
  16898. ;; FIXME: should fix the tags property of the agenda line.
  16899. (defun org-agenda-set-tags ()
  16900. "Set tags for the current headline."
  16901. (interactive)
  16902. (org-agenda-check-no-diary)
  16903. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  16904. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  16905. (org-agenda-error)))
  16906. (buffer (marker-buffer hdmarker))
  16907. (pos (marker-position hdmarker))
  16908. (buffer-read-only nil)
  16909. newhead)
  16910. (org-with-remote-undo buffer
  16911. (with-current-buffer buffer
  16912. (widen)
  16913. (goto-char pos)
  16914. (save-excursion
  16915. (org-show-context 'agenda))
  16916. (save-excursion
  16917. (and (outline-next-heading)
  16918. (org-flag-heading nil))) ; show the next heading
  16919. (goto-char pos)
  16920. (call-interactively 'org-set-tags)
  16921. (end-of-line 1)
  16922. (setq newhead (org-get-heading)))
  16923. (org-agenda-change-all-lines newhead hdmarker)
  16924. (beginning-of-line 1))))
  16925. (defun org-agenda-toggle-archive-tag ()
  16926. "Toggle the archive tag for the current entry."
  16927. (interactive)
  16928. (org-agenda-check-no-diary)
  16929. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  16930. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  16931. (org-agenda-error)))
  16932. (buffer (marker-buffer hdmarker))
  16933. (pos (marker-position hdmarker))
  16934. (buffer-read-only nil)
  16935. newhead)
  16936. (org-with-remote-undo buffer
  16937. (with-current-buffer buffer
  16938. (widen)
  16939. (goto-char pos)
  16940. (org-show-context 'agenda)
  16941. (save-excursion
  16942. (and (outline-next-heading)
  16943. (org-flag-heading nil))) ; show the next heading
  16944. (call-interactively 'org-toggle-archive-tag)
  16945. (end-of-line 1)
  16946. (setq newhead (org-get-heading)))
  16947. (org-agenda-change-all-lines newhead hdmarker)
  16948. (beginning-of-line 1))))
  16949. (defun org-agenda-date-later (arg &optional what)
  16950. "Change the date of this item to one day later."
  16951. (interactive "p")
  16952. (org-agenda-check-type t 'agenda 'timeline)
  16953. (org-agenda-check-no-diary)
  16954. (let* ((marker (or (get-text-property (point) 'org-marker)
  16955. (org-agenda-error)))
  16956. (buffer (marker-buffer marker))
  16957. (pos (marker-position marker)))
  16958. (org-with-remote-undo buffer
  16959. (with-current-buffer buffer
  16960. (widen)
  16961. (goto-char pos)
  16962. (if (not (org-at-timestamp-p))
  16963. (error "Cannot find time stamp"))
  16964. (org-timestamp-change arg (or what 'day)))
  16965. (org-agenda-show-new-time marker org-last-changed-timestamp))
  16966. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  16967. (defun org-agenda-date-earlier (arg &optional what)
  16968. "Change the date of this item to one day earlier."
  16969. (interactive "p")
  16970. (org-agenda-date-later (- arg) what))
  16971. (defun org-agenda-show-new-time (marker stamp)
  16972. "Show new date stamp via text properties."
  16973. ;; We use text properties to make this undoable
  16974. (let ((buffer-read-only nil))
  16975. (setq stamp (concat " => " stamp))
  16976. (save-excursion
  16977. (goto-char (point-max))
  16978. (while (not (bobp))
  16979. (when (equal marker (get-text-property (point) 'org-marker))
  16980. (move-to-column (- (window-width) (length stamp)) t)
  16981. (if (featurep 'xemacs)
  16982. ;; Use `duplicable' property to trigger undo recording
  16983. (let ((ex (make-extent nil nil))
  16984. (gl (make-glyph stamp)))
  16985. (set-glyph-face gl 'secondary-selection)
  16986. (set-extent-properties
  16987. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  16988. (insert-extent ex (1- (point)) (point-at-eol)))
  16989. (add-text-properties
  16990. (1- (point)) (point-at-eol)
  16991. (list 'display (org-add-props stamp nil
  16992. 'face 'secondary-selection))))
  16993. (beginning-of-line 1))
  16994. (beginning-of-line 0)))))
  16995. (defun org-agenda-date-prompt (arg)
  16996. "Change the date of this item. Date is prompted for, with default today.
  16997. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  16998. be used to request time specification in the time stamp."
  16999. (interactive "P")
  17000. (org-agenda-check-type t 'agenda 'timeline)
  17001. (org-agenda-check-no-diary)
  17002. (let* ((marker (or (get-text-property (point) 'org-marker)
  17003. (org-agenda-error)))
  17004. (buffer (marker-buffer marker))
  17005. (pos (marker-position marker)))
  17006. (org-with-remote-undo buffer
  17007. (with-current-buffer buffer
  17008. (widen)
  17009. (goto-char pos)
  17010. (if (not (org-at-timestamp-p))
  17011. (error "Cannot find time stamp"))
  17012. (org-time-stamp arg)
  17013. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  17014. (defun org-agenda-schedule (arg)
  17015. "Schedule the item at point."
  17016. (interactive "P")
  17017. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  17018. (org-agenda-check-no-diary)
  17019. (let* ((marker (or (get-text-property (point) 'org-marker)
  17020. (org-agenda-error)))
  17021. (buffer (marker-buffer marker))
  17022. (pos (marker-position marker))
  17023. (org-insert-labeled-timestamps-at-point nil)
  17024. ts)
  17025. (org-with-remote-undo buffer
  17026. (with-current-buffer buffer
  17027. (widen)
  17028. (goto-char pos)
  17029. (setq ts (org-schedule))
  17030. (message "Item scheduled for %s" ts)))))
  17031. (defun org-agenda-deadline (arg)
  17032. "Schedule the item at point."
  17033. (interactive "P")
  17034. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  17035. (org-agenda-check-no-diary)
  17036. (let* ((marker (or (get-text-property (point) 'org-marker)
  17037. (org-agenda-error)))
  17038. (buffer (marker-buffer marker))
  17039. (pos (marker-position marker))
  17040. (org-insert-labeled-timestamps-at-point nil)
  17041. ts)
  17042. (org-with-remote-undo buffer
  17043. (with-current-buffer buffer
  17044. (widen)
  17045. (goto-char pos)
  17046. (setq ts (org-deadline))
  17047. (message "Deadline for this item set to %s" ts)))))
  17048. (defun org-get-heading ()
  17049. "Return the heading of the current entry, without the stars."
  17050. (save-excursion
  17051. (org-back-to-heading t)
  17052. (if (looking-at "\\*+[ \t]+\\([^\r\n]*\\)") (match-string 1) "")))
  17053. (defun org-agenda-clock-in (&optional arg)
  17054. "Start the clock on the currently selected item."
  17055. (interactive "P")
  17056. (org-agenda-check-no-diary)
  17057. (let* ((marker (or (get-text-property (point) 'org-marker)
  17058. (org-agenda-error)))
  17059. (pos (marker-position marker)))
  17060. (org-with-remote-undo (marker-buffer marker)
  17061. (with-current-buffer (marker-buffer marker)
  17062. (widen)
  17063. (goto-char pos)
  17064. (org-clock-in)))))
  17065. (defun org-agenda-clock-out (&optional arg)
  17066. "Stop the currently running clock."
  17067. (interactive "P")
  17068. (unless (marker-buffer org-clock-marker)
  17069. (error "No running clock"))
  17070. (org-with-remote-undo (marker-buffer org-clock-marker)
  17071. (org-clock-out)))
  17072. (defun org-agenda-clock-cancel (&optional arg)
  17073. "Cancel the currently running clock."
  17074. (interactive "P")
  17075. (unless (marker-buffer org-clock-marker)
  17076. (error "No running clock"))
  17077. (org-with-remote-undo (marker-buffer org-clock-marker)
  17078. (org-clock-cancel)))
  17079. (defun org-agenda-diary-entry ()
  17080. "Make a diary entry, like the `i' command from the calendar.
  17081. All the standard commands work: block, weekly etc."
  17082. (interactive)
  17083. (org-agenda-check-type t 'agenda 'timeline)
  17084. (require 'diary-lib)
  17085. (let* ((char (progn
  17086. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  17087. (read-char-exclusive)))
  17088. (cmd (cdr (assoc char
  17089. '((?d . insert-diary-entry)
  17090. (?w . insert-weekly-diary-entry)
  17091. (?m . insert-monthly-diary-entry)
  17092. (?y . insert-yearly-diary-entry)
  17093. (?a . insert-anniversary-diary-entry)
  17094. (?b . insert-block-diary-entry)
  17095. (?c . insert-cyclic-diary-entry)))))
  17096. (oldf (symbol-function 'calendar-cursor-to-date))
  17097. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  17098. (point (point))
  17099. (mark (or (mark t) (point))))
  17100. (unless cmd
  17101. (error "No command associated with <%c>" char))
  17102. (unless (and (get-text-property point 'day)
  17103. (or (not (equal ?b char))
  17104. (get-text-property mark 'day)))
  17105. (error "Don't know which date to use for diary entry"))
  17106. ;; We implement this by hacking the `calendar-cursor-to-date' function
  17107. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  17108. (let ((calendar-mark-ring
  17109. (list (calendar-gregorian-from-absolute
  17110. (or (get-text-property mark 'day)
  17111. (get-text-property point 'day))))))
  17112. (unwind-protect
  17113. (progn
  17114. (fset 'calendar-cursor-to-date
  17115. (lambda (&optional error)
  17116. (calendar-gregorian-from-absolute
  17117. (get-text-property point 'day))))
  17118. (call-interactively cmd))
  17119. (fset 'calendar-cursor-to-date oldf)))))
  17120. (defun org-agenda-execute-calendar-command (cmd)
  17121. "Execute a calendar command from the agenda, with the date associated to
  17122. the cursor position."
  17123. (org-agenda-check-type t 'agenda 'timeline)
  17124. (require 'diary-lib)
  17125. (unless (get-text-property (point) 'day)
  17126. (error "Don't know which date to use for calendar command"))
  17127. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  17128. (point (point))
  17129. (date (calendar-gregorian-from-absolute
  17130. (get-text-property point 'day)))
  17131. ;; the following 3 vars are needed in the calendar
  17132. (displayed-day (extract-calendar-day date))
  17133. (displayed-month (extract-calendar-month date))
  17134. (displayed-year (extract-calendar-year date)))
  17135. (unwind-protect
  17136. (progn
  17137. (fset 'calendar-cursor-to-date
  17138. (lambda (&optional error)
  17139. (calendar-gregorian-from-absolute
  17140. (get-text-property point 'day))))
  17141. (call-interactively cmd))
  17142. (fset 'calendar-cursor-to-date oldf))))
  17143. (defun org-agenda-phases-of-moon ()
  17144. "Display the phases of the moon for the 3 months around the cursor date."
  17145. (interactive)
  17146. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  17147. (defun org-agenda-holidays ()
  17148. "Display the holidays for the 3 months around the cursor date."
  17149. (interactive)
  17150. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  17151. (defun org-agenda-sunrise-sunset (arg)
  17152. "Display sunrise and sunset for the cursor date.
  17153. Latitude and longitude can be specified with the variables
  17154. `calendar-latitude' and `calendar-longitude'. When called with prefix
  17155. argument, latitude and longitude will be prompted for."
  17156. (interactive "P")
  17157. (let ((calendar-longitude (if arg nil calendar-longitude))
  17158. (calendar-latitude (if arg nil calendar-latitude))
  17159. (calendar-location-name
  17160. (if arg "the given coordinates" calendar-location-name)))
  17161. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  17162. (defun org-agenda-goto-calendar ()
  17163. "Open the Emacs calendar with the date at the cursor."
  17164. (interactive)
  17165. (org-agenda-check-type t 'agenda 'timeline)
  17166. (let* ((day (or (get-text-property (point) 'day)
  17167. (error "Don't know which date to open in calendar")))
  17168. (date (calendar-gregorian-from-absolute day))
  17169. (calendar-move-hook nil)
  17170. (view-calendar-holidays-initially nil)
  17171. (view-diary-entries-initially nil))
  17172. (calendar)
  17173. (calendar-goto-date date)))
  17174. (defun org-calendar-goto-agenda ()
  17175. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  17176. This is a command that has to be installed in `calendar-mode-map'."
  17177. (interactive)
  17178. (org-agenda-list nil (calendar-absolute-from-gregorian
  17179. (calendar-cursor-to-date))
  17180. nil))
  17181. (defun org-agenda-convert-date ()
  17182. (interactive)
  17183. (org-agenda-check-type t 'agenda 'timeline)
  17184. (let ((day (get-text-property (point) 'day))
  17185. date s)
  17186. (unless day
  17187. (error "Don't know which date to convert"))
  17188. (setq date (calendar-gregorian-from-absolute day))
  17189. (setq s (concat
  17190. "Gregorian: " (calendar-date-string date) "\n"
  17191. "ISO: " (calendar-iso-date-string date) "\n"
  17192. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  17193. "Julian: " (calendar-julian-date-string date) "\n"
  17194. "Astron. JD: " (calendar-astro-date-string date)
  17195. " (Julian date number at noon UTC)\n"
  17196. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  17197. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  17198. "French: " (calendar-french-date-string date) "\n"
  17199. "Mayan: " (calendar-mayan-date-string date) "\n"
  17200. "Coptic: " (calendar-coptic-date-string date) "\n"
  17201. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  17202. "Persian: " (calendar-persian-date-string date) "\n"
  17203. "Chinese: " (calendar-chinese-date-string date) "\n"))
  17204. (with-output-to-temp-buffer "*Dates*"
  17205. (princ s))
  17206. (if (fboundp 'fit-window-to-buffer)
  17207. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  17208. ;;;; Embedded LaTeX
  17209. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17210. "Keymap for the minor `org-cdlatex-mode'.")
  17211. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17212. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17213. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17214. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17215. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  17216. (defvar org-cdlatex-texmathp-advice-is-done nil
  17217. "Flag remembering if we have applied the advice to texmathp already.")
  17218. (define-minor-mode org-cdlatex-mode
  17219. "Toggle the minor `org-cdlatex-mode'.
  17220. This mode supports entering LaTeX environment and math in LaTeX fragments
  17221. in Org-mode.
  17222. \\{org-cdlatex-mode-map}"
  17223. nil " OCDL" nil
  17224. (when org-cdlatex-mode (require 'cdlatex))
  17225. (unless org-cdlatex-texmathp-advice-is-done
  17226. (setq org-cdlatex-texmathp-advice-is-done t)
  17227. (defadvice texmathp (around org-math-always-on activate)
  17228. "Always return t in org-mode buffers.
  17229. This is because we want to insert math symbols without dollars even outside
  17230. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17231. en embedded LaTeX fragement, let texmathp do its job.
  17232. \\[org-cdlatex-mode-map]"
  17233. (interactive)
  17234. (let (p)
  17235. (cond
  17236. ((not (org-mode-p)) ad-do-it)
  17237. ((eq this-command 'cdlatex-math-symbol)
  17238. (setq ad-return-value t
  17239. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17240. (t
  17241. (let ((p (org-inside-LaTeX-fragment-p)))
  17242. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17243. (setq ad-return-value t
  17244. texmathp-why '("Org-mode embedded math" . 0))
  17245. (if p ad-do-it)))))))))
  17246. (defun turn-on-org-cdlatex ()
  17247. "Unconditionally turn on `org-cdlatex-mode'."
  17248. (org-cdlatex-mode 1))
  17249. (defun org-inside-LaTeX-fragment-p ()
  17250. "Test if point is inside a LaTeX fragment.
  17251. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17252. sequence appearing also before point.
  17253. Even though the matchers for math are configurable, this function assumes
  17254. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17255. delimiters are skipped when they have been removed by customization.
  17256. The return value is nil, or a cons cell with the delimiter and
  17257. and the position of this delimiter.
  17258. This function does a reasonably good job, but can locally be fooled by
  17259. for example currency specifications. For example it will assume being in
  17260. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17261. fragments that are properly closed, but during editing, we have to live
  17262. with the uncertainty caused by missing closing delimiters. This function
  17263. looks only before point, not after."
  17264. (catch 'exit
  17265. (let ((pos (point))
  17266. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17267. (lim (progn
  17268. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17269. (point)))
  17270. dd-on str (start 0) m re)
  17271. (goto-char pos)
  17272. (when dodollar
  17273. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17274. re (nth 1 (assoc "$" org-latex-regexps)))
  17275. (while (string-match re str start)
  17276. (cond
  17277. ((= (match-end 0) (length str))
  17278. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17279. ((= (match-end 0) (- (length str) 5))
  17280. (throw 'exit nil))
  17281. (t (setq start (match-end 0))))))
  17282. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17283. (goto-char pos)
  17284. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17285. (and (match-beginning 2) (throw 'exit nil))
  17286. ;; count $$
  17287. (while (re-search-backward "\\$\\$" lim t)
  17288. (setq dd-on (not dd-on)))
  17289. (goto-char pos)
  17290. (if dd-on (cons "$$" m))))))
  17291. (defun org-try-cdlatex-tab ()
  17292. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17293. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17294. - inside a LaTeX fragment, or
  17295. - after the first word in a line, where an abbreviation expansion could
  17296. insert a LaTeX environment."
  17297. (when org-cdlatex-mode
  17298. (cond
  17299. ((save-excursion
  17300. (skip-chars-backward "a-zA-Z0-9*")
  17301. (skip-chars-backward " \t")
  17302. (bolp))
  17303. (cdlatex-tab) t)
  17304. ((org-inside-LaTeX-fragment-p)
  17305. (cdlatex-tab) t)
  17306. (t nil))))
  17307. (defun org-cdlatex-underscore-caret (&optional arg)
  17308. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17309. Revert to the normal definition outside of these fragments."
  17310. (interactive "P")
  17311. (if (org-inside-LaTeX-fragment-p)
  17312. (call-interactively 'cdlatex-sub-superscript)
  17313. (let (org-cdlatex-mode)
  17314. (call-interactively (key-binding (vector last-input-event))))))
  17315. (defun org-cdlatex-math-modify (&optional arg)
  17316. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17317. Revert to the normal definition outside of these fragments."
  17318. (interactive "P")
  17319. (if (org-inside-LaTeX-fragment-p)
  17320. (call-interactively 'cdlatex-math-modify)
  17321. (let (org-cdlatex-mode)
  17322. (call-interactively (key-binding (vector last-input-event))))))
  17323. (defvar org-latex-fragment-image-overlays nil
  17324. "List of overlays carrying the images of latex fragments.")
  17325. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17326. (defun org-remove-latex-fragment-image-overlays ()
  17327. "Remove all overlays with LaTeX fragment images in current buffer."
  17328. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  17329. (setq org-latex-fragment-image-overlays nil))
  17330. (defun org-preview-latex-fragment (&optional subtree)
  17331. "Preview the LaTeX fragment at point, or all locally or globally.
  17332. If the cursor is in a LaTeX fragment, create the image and overlay
  17333. it over the source code. If there is no fragment at point, display
  17334. all fragments in the current text, from one headline to the next. With
  17335. prefix SUBTREE, display all fragments in the current subtree. With a
  17336. double prefix `C-u C-u', or when the cursor is before the first headline,
  17337. display all fragments in the buffer.
  17338. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  17339. (interactive "P")
  17340. (org-remove-latex-fragment-image-overlays)
  17341. (save-excursion
  17342. (save-restriction
  17343. (let (beg end at msg)
  17344. (cond
  17345. ((or (equal subtree '(16))
  17346. (not (save-excursion
  17347. (re-search-backward (concat "^" outline-regexp) nil t))))
  17348. (setq beg (point-min) end (point-max)
  17349. msg "Creating images for buffer...%s"))
  17350. ((equal subtree '(4))
  17351. (org-back-to-heading)
  17352. (setq beg (point) end (org-end-of-subtree t)
  17353. msg "Creating images for subtree...%s"))
  17354. (t
  17355. (if (setq at (org-inside-LaTeX-fragment-p))
  17356. (goto-char (max (point-min) (- (cdr at) 2)))
  17357. (org-back-to-heading))
  17358. (setq beg (point) end (progn (outline-next-heading) (point))
  17359. msg (if at "Creating image...%s"
  17360. "Creating images for entry...%s"))))
  17361. (message msg "")
  17362. (narrow-to-region beg end)
  17363. (goto-char beg)
  17364. (org-format-latex
  17365. (concat "ltxpng/" (file-name-sans-extension
  17366. (file-name-nondirectory
  17367. buffer-file-name)))
  17368. default-directory 'overlays msg at 'forbuffer)
  17369. (message msg "done. Use `C-c C-c' to remove images.")))))
  17370. (defvar org-latex-regexps
  17371. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  17372. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  17373. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  17374. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  17375. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  17376. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  17377. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  17378. "Regular expressions for matching embedded LaTeX.")
  17379. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  17380. "Replace LaTeX fragments with links to an image, and produce images."
  17381. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17382. (let* ((prefixnodir (file-name-nondirectory prefix))
  17383. (absprefix (expand-file-name prefix dir))
  17384. (todir (file-name-directory absprefix))
  17385. (opt org-format-latex-options)
  17386. (matchers (plist-get opt :matchers))
  17387. (re-list org-latex-regexps)
  17388. (cnt 0) txt link beg end re e checkdir
  17389. m n block linkfile movefile ov)
  17390. ;; Check if there are old images files with this prefix, and remove them
  17391. (when (file-directory-p todir)
  17392. (mapc 'delete-file
  17393. (directory-files
  17394. todir 'full
  17395. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  17396. ;; Check the different regular expressions
  17397. (while (setq e (pop re-list))
  17398. (setq m (car e) re (nth 1 e) n (nth 2 e)
  17399. block (if (nth 3 e) "\n\n" ""))
  17400. (when (member m matchers)
  17401. (goto-char (point-min))
  17402. (while (re-search-forward re nil t)
  17403. (when (or (not at) (equal (cdr at) (match-beginning n)))
  17404. (setq txt (match-string n)
  17405. beg (match-beginning n) end (match-end n)
  17406. cnt (1+ cnt)
  17407. linkfile (format "%s_%04d.png" prefix cnt)
  17408. movefile (format "%s_%04d.png" absprefix cnt)
  17409. link (concat block "[[file:" linkfile "]]" block))
  17410. (if msg (message msg cnt))
  17411. (goto-char beg)
  17412. (unless checkdir ; make sure the directory exists
  17413. (setq checkdir t)
  17414. (or (file-directory-p todir) (make-directory todir)))
  17415. (org-create-formula-image
  17416. txt movefile opt forbuffer)
  17417. (if overlays
  17418. (progn
  17419. (setq ov (org-make-overlay beg end))
  17420. (if (featurep 'xemacs)
  17421. (progn
  17422. (org-overlay-put ov 'invisible t)
  17423. (org-overlay-put
  17424. ov 'end-glyph
  17425. (make-glyph (vector 'png :file movefile))))
  17426. (org-overlay-put
  17427. ov 'display
  17428. (list 'image :type 'png :file movefile :ascent 'center)))
  17429. (push ov org-latex-fragment-image-overlays)
  17430. (goto-char end))
  17431. (delete-region beg end)
  17432. (insert link))))))))
  17433. ;; This function borrows from Ganesh Swami's latex2png.el
  17434. (defun org-create-formula-image (string tofile options buffer)
  17435. (let* ((tmpdir (if (featurep 'xemacs)
  17436. (temp-directory)
  17437. temporary-file-directory))
  17438. (texfilebase (make-temp-name
  17439. (expand-file-name "orgtex" tmpdir)))
  17440. (texfile (concat texfilebase ".tex"))
  17441. (dvifile (concat texfilebase ".dvi"))
  17442. (pngfile (concat texfilebase ".png"))
  17443. (fnh (face-attribute 'default :height nil))
  17444. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17445. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17446. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17447. "Black"))
  17448. (bg (or (plist-get options (if buffer :background :html-background))
  17449. "Transparent")))
  17450. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  17451. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  17452. (with-temp-file texfile
  17453. (insert org-format-latex-header
  17454. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  17455. (let ((dir default-directory))
  17456. (condition-case nil
  17457. (progn
  17458. (cd tmpdir)
  17459. (call-process "latex" nil nil nil texfile))
  17460. (error nil))
  17461. (cd dir))
  17462. (if (not (file-exists-p dvifile))
  17463. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17464. (call-process "dvipng" nil nil nil
  17465. "-E" "-fg" fg "-bg" bg
  17466. "-D" dpi
  17467. ;;"-x" scale "-y" scale
  17468. "-T" "tight"
  17469. "-o" pngfile
  17470. dvifile)
  17471. (if (not (file-exists-p pngfile))
  17472. (progn (message "Failed to create png file from %s" texfile) nil)
  17473. ;; Use the requested file name and clean up
  17474. (copy-file pngfile tofile 'replace)
  17475. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  17476. (delete-file (concat texfilebase e)))
  17477. pngfile))))
  17478. (defun org-dvipng-color (attr)
  17479. "Return an rgb color specification for dvipng."
  17480. (apply 'format "rgb %s %s %s"
  17481. (mapcar 'org-normalize-color
  17482. (color-values (face-attribute 'default attr nil)))))
  17483. (defun org-normalize-color (value)
  17484. "Return string to be used as color value for an RGB component."
  17485. (format "%g" (/ value 65535.0)))
  17486. ;;;; Exporting
  17487. ;;; Variables, constants, and parameter plists
  17488. (defconst org-level-max 20)
  17489. (defvar org-export-html-preamble nil
  17490. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  17491. (defvar org-export-html-postamble nil
  17492. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  17493. (defvar org-export-html-auto-preamble t
  17494. "Should default preamble be inserted? Set by publishing functions.")
  17495. (defvar org-export-html-auto-postamble t
  17496. "Should default postamble be inserted? Set by publishing functions.")
  17497. (defvar org-current-export-file nil) ; dynamically scoped parameter
  17498. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  17499. (defconst org-export-plist-vars
  17500. '((:language . org-export-default-language)
  17501. (:customtime . org-display-custom-times)
  17502. (:headline-levels . org-export-headline-levels)
  17503. (:section-numbers . org-export-with-section-numbers)
  17504. (:table-of-contents . org-export-with-toc)
  17505. (:preserve-breaks . org-export-preserve-breaks)
  17506. (:archived-trees . org-export-with-archived-trees)
  17507. (:emphasize . org-export-with-emphasize)
  17508. (:sub-superscript . org-export-with-sub-superscripts)
  17509. (:footnotes . org-export-with-footnotes)
  17510. (:property-drawer . org-export-with-property-drawer)
  17511. (:TeX-macros . org-export-with-TeX-macros)
  17512. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  17513. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  17514. (:fixed-width . org-export-with-fixed-width)
  17515. (:timestamps . org-export-with-timestamps)
  17516. (:tables . org-export-with-tables)
  17517. (:table-auto-headline . org-export-highlight-first-table-line)
  17518. (:style . org-export-html-style)
  17519. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  17520. (:convert-org-links . org-export-html-link-org-files-as-html)
  17521. (:inline-images . org-export-html-inline-images)
  17522. (:expand-quoted-html . org-export-html-expand)
  17523. (:timestamp . org-export-html-with-timestamp)
  17524. (:publishing-directory . org-export-publishing-directory)
  17525. (:preamble . org-export-html-preamble)
  17526. (:postamble . org-export-html-postamble)
  17527. (:auto-preamble . org-export-html-auto-preamble)
  17528. (:auto-postamble . org-export-html-auto-postamble)
  17529. (:author . user-full-name)
  17530. (:email . user-mail-address)))
  17531. (defun org-default-export-plist ()
  17532. "Return the property list with default settings for the export variables."
  17533. (let ((l org-export-plist-vars) rtn e)
  17534. (while (setq e (pop l))
  17535. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  17536. rtn))
  17537. (defun org-infile-export-plist ()
  17538. "Return the property list with file-local settings for export."
  17539. (save-excursion
  17540. (goto-char 0)
  17541. (let ((re (org-make-options-regexp
  17542. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  17543. p key val text options)
  17544. (while (re-search-forward re nil t)
  17545. (setq key (org-match-string-no-properties 1)
  17546. val (org-match-string-no-properties 2))
  17547. (cond
  17548. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  17549. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  17550. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  17551. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  17552. ((string-equal key "TEXT")
  17553. (setq text (if text (concat text "\n" val) val)))
  17554. ((string-equal key "OPTIONS") (setq options val))))
  17555. (setq p (plist-put p :text text))
  17556. (when options
  17557. (let ((op '(("H" . :headline-levels)
  17558. ("num" . :section-numbers)
  17559. ("toc" . :table-of-contents)
  17560. ("\\n" . :preserve-breaks)
  17561. ("@" . :expand-quoted-html)
  17562. (":" . :fixed-width)
  17563. ("|" . :tables)
  17564. ("^" . :sub-superscript)
  17565. ("f" . :footnotes)
  17566. ("p" . :property-drawer)
  17567. ("*" . :emphasize)
  17568. ("TeX" . :TeX-macros)
  17569. ("LaTeX" . :LaTeX-fragments)
  17570. ("skip" . :skip-before-1st-heading)))
  17571. o)
  17572. (while (setq o (pop op))
  17573. (if (string-match (concat (regexp-quote (car o))
  17574. ":\\([^ \t\n\r;,.]*\\)")
  17575. options)
  17576. (setq p (plist-put p (cdr o)
  17577. (car (read-from-string
  17578. (match-string 1 options)))))))))
  17579. p)))
  17580. (defun org-export-directory (type plist)
  17581. (let* ((val (plist-get plist :publishing-directory))
  17582. (dir (if (listp val)
  17583. (or (cdr (assoc type val)) ".")
  17584. val)))
  17585. dir))
  17586. (defun org-skip-comments (lines)
  17587. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  17588. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  17589. (re2 "^\\(\\*+\\)[ \t\n\r]")
  17590. (case-fold-search nil)
  17591. rtn line level)
  17592. (while (setq line (pop lines))
  17593. (cond
  17594. ((and (string-match re1 line)
  17595. (setq level (- (match-end 1) (match-beginning 1))))
  17596. ;; Beginning of a COMMENT subtree. Skip it.
  17597. (while (and (setq line (pop lines))
  17598. (or (not (string-match re2 line))
  17599. (> (- (match-end 1) (match-beginning 1)) level))))
  17600. (setq lines (cons line lines)))
  17601. ((string-match "^#" line)
  17602. ;; an ordinary comment line
  17603. )
  17604. ((and org-export-table-remove-special-lines
  17605. (string-match "^[ \t]*|" line)
  17606. (or (string-match "^[ \t]*| *[!_^] *|" line)
  17607. (and (string-match "| *<[0-9]+> *|" line)
  17608. (not (string-match "| *[^ <|]" line)))))
  17609. ;; a special table line that should be removed
  17610. )
  17611. (t (setq rtn (cons line rtn)))))
  17612. (nreverse rtn)))
  17613. (defun org-export (&optional arg)
  17614. (interactive)
  17615. (let ((help "[t] insert the export option template
  17616. \[v] limit export to visible part of outline tree
  17617. \[a] export as ASCII
  17618. \[h] export as HTML
  17619. \[H] export as HTML to temporary buffer
  17620. \[b] export as HTML and browse immediately
  17621. \[x] export as XOXO
  17622. \[i] export current file as iCalendar file
  17623. \[I] export all agenda files as iCalendar files
  17624. \[c] export agenda files into combined iCalendar file
  17625. \[F] publish current file
  17626. \[P] publish current project
  17627. \[X] publish... (project will be prompted for)
  17628. \[A] publish all projects")
  17629. (cmds
  17630. '((?t . org-insert-export-options-template)
  17631. (?v . org-export-visible)
  17632. (?a . org-export-as-ascii)
  17633. (?h . org-export-as-html)
  17634. (?b . org-export-as-html-and-open)
  17635. (?H . org-export-as-html-to-buffer)
  17636. (?R . org-export-region-as-html)
  17637. (?x . org-export-as-xoxo)
  17638. (?i . org-export-icalendar-this-file)
  17639. (?I . org-export-icalendar-all-agenda-files)
  17640. (?c . org-export-icalendar-combine-agenda-files)
  17641. (?F . org-publish-current-file)
  17642. (?P . org-publish-current-project)
  17643. (?X . org-publish)
  17644. (?A . org-publish-all)))
  17645. r1 r2 ass)
  17646. (save-window-excursion
  17647. (delete-other-windows)
  17648. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  17649. (princ help))
  17650. (message "Select command: ")
  17651. (setq r1 (read-char-exclusive)))
  17652. (setq r2 (if (< r1 27) (+ r1 96) r1))
  17653. (if (setq ass (assq r2 cmds))
  17654. (call-interactively (cdr ass))
  17655. (error "No command associated with key %c" r1))))
  17656. (defconst org-html-entities
  17657. '(("nbsp")
  17658. ("iexcl")
  17659. ("cent")
  17660. ("pound")
  17661. ("curren")
  17662. ("yen")
  17663. ("brvbar")
  17664. ("vert" . "&#124;")
  17665. ("sect")
  17666. ("uml")
  17667. ("copy")
  17668. ("ordf")
  17669. ("laquo")
  17670. ("not")
  17671. ("shy")
  17672. ("reg")
  17673. ("macr")
  17674. ("deg")
  17675. ("plusmn")
  17676. ("sup2")
  17677. ("sup3")
  17678. ("acute")
  17679. ("micro")
  17680. ("para")
  17681. ("middot")
  17682. ("odot"."o")
  17683. ("star"."*")
  17684. ("cedil")
  17685. ("sup1")
  17686. ("ordm")
  17687. ("raquo")
  17688. ("frac14")
  17689. ("frac12")
  17690. ("frac34")
  17691. ("iquest")
  17692. ("Agrave")
  17693. ("Aacute")
  17694. ("Acirc")
  17695. ("Atilde")
  17696. ("Auml")
  17697. ("Aring") ("AA"."&Aring;")
  17698. ("AElig")
  17699. ("Ccedil")
  17700. ("Egrave")
  17701. ("Eacute")
  17702. ("Ecirc")
  17703. ("Euml")
  17704. ("Igrave")
  17705. ("Iacute")
  17706. ("Icirc")
  17707. ("Iuml")
  17708. ("ETH")
  17709. ("Ntilde")
  17710. ("Ograve")
  17711. ("Oacute")
  17712. ("Ocirc")
  17713. ("Otilde")
  17714. ("Ouml")
  17715. ("times")
  17716. ("Oslash")
  17717. ("Ugrave")
  17718. ("Uacute")
  17719. ("Ucirc")
  17720. ("Uuml")
  17721. ("Yacute")
  17722. ("THORN")
  17723. ("szlig")
  17724. ("agrave")
  17725. ("aacute")
  17726. ("acirc")
  17727. ("atilde")
  17728. ("auml")
  17729. ("aring")
  17730. ("aelig")
  17731. ("ccedil")
  17732. ("egrave")
  17733. ("eacute")
  17734. ("ecirc")
  17735. ("euml")
  17736. ("igrave")
  17737. ("iacute")
  17738. ("icirc")
  17739. ("iuml")
  17740. ("eth")
  17741. ("ntilde")
  17742. ("ograve")
  17743. ("oacute")
  17744. ("ocirc")
  17745. ("otilde")
  17746. ("ouml")
  17747. ("divide")
  17748. ("oslash")
  17749. ("ugrave")
  17750. ("uacute")
  17751. ("ucirc")
  17752. ("uuml")
  17753. ("yacute")
  17754. ("thorn")
  17755. ("yuml")
  17756. ("fnof")
  17757. ("Alpha")
  17758. ("Beta")
  17759. ("Gamma")
  17760. ("Delta")
  17761. ("Epsilon")
  17762. ("Zeta")
  17763. ("Eta")
  17764. ("Theta")
  17765. ("Iota")
  17766. ("Kappa")
  17767. ("Lambda")
  17768. ("Mu")
  17769. ("Nu")
  17770. ("Xi")
  17771. ("Omicron")
  17772. ("Pi")
  17773. ("Rho")
  17774. ("Sigma")
  17775. ("Tau")
  17776. ("Upsilon")
  17777. ("Phi")
  17778. ("Chi")
  17779. ("Psi")
  17780. ("Omega")
  17781. ("alpha")
  17782. ("beta")
  17783. ("gamma")
  17784. ("delta")
  17785. ("epsilon")
  17786. ("varepsilon"."&epsilon;")
  17787. ("zeta")
  17788. ("eta")
  17789. ("theta")
  17790. ("iota")
  17791. ("kappa")
  17792. ("lambda")
  17793. ("mu")
  17794. ("nu")
  17795. ("xi")
  17796. ("omicron")
  17797. ("pi")
  17798. ("rho")
  17799. ("sigmaf") ("varsigma"."&sigmaf;")
  17800. ("sigma")
  17801. ("tau")
  17802. ("upsilon")
  17803. ("phi")
  17804. ("chi")
  17805. ("psi")
  17806. ("omega")
  17807. ("thetasym") ("vartheta"."&thetasym;")
  17808. ("upsih")
  17809. ("piv")
  17810. ("bull") ("bullet"."&bull;")
  17811. ("hellip") ("dots"."&hellip;")
  17812. ("prime")
  17813. ("Prime")
  17814. ("oline")
  17815. ("frasl")
  17816. ("weierp")
  17817. ("image")
  17818. ("real")
  17819. ("trade")
  17820. ("alefsym")
  17821. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  17822. ("uarr") ("uparrow"."&uarr;")
  17823. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  17824. ("darr")("downarrow"."&darr;")
  17825. ("harr") ("leftrightarrow"."&harr;")
  17826. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  17827. ("lArr") ("Leftarrow"."&lArr;")
  17828. ("uArr") ("Uparrow"."&uArr;")
  17829. ("rArr") ("Rightarrow"."&rArr;")
  17830. ("dArr") ("Downarrow"."&dArr;")
  17831. ("hArr") ("Leftrightarrow"."&hArr;")
  17832. ("forall")
  17833. ("part") ("partial"."&part;")
  17834. ("exist") ("exists"."&exist;")
  17835. ("empty") ("emptyset"."&empty;")
  17836. ("nabla")
  17837. ("isin") ("in"."&isin;")
  17838. ("notin")
  17839. ("ni")
  17840. ("prod")
  17841. ("sum")
  17842. ("minus")
  17843. ("lowast") ("ast"."&lowast;")
  17844. ("radic")
  17845. ("prop") ("proptp"."&prop;")
  17846. ("infin") ("infty"."&infin;")
  17847. ("ang") ("angle"."&ang;")
  17848. ("and") ("vee"."&and;")
  17849. ("or") ("wedge"."&or;")
  17850. ("cap")
  17851. ("cup")
  17852. ("int")
  17853. ("there4")
  17854. ("sim")
  17855. ("cong") ("simeq"."&cong;")
  17856. ("asymp")("approx"."&asymp;")
  17857. ("ne") ("neq"."&ne;")
  17858. ("equiv")
  17859. ("le")
  17860. ("ge")
  17861. ("sub") ("subset"."&sub;")
  17862. ("sup") ("supset"."&sup;")
  17863. ("nsub")
  17864. ("sube")
  17865. ("supe")
  17866. ("oplus")
  17867. ("otimes")
  17868. ("perp")
  17869. ("sdot") ("cdot"."&sdot;")
  17870. ("lceil")
  17871. ("rceil")
  17872. ("lfloor")
  17873. ("rfloor")
  17874. ("lang")
  17875. ("rang")
  17876. ("loz") ("Diamond"."&loz;")
  17877. ("spades") ("spadesuit"."&spades;")
  17878. ("clubs") ("clubsuit"."&clubs;")
  17879. ("hearts") ("diamondsuit"."&hearts;")
  17880. ("diams") ("diamondsuit"."&diams;")
  17881. ("quot")
  17882. ("amp")
  17883. ("lt")
  17884. ("gt")
  17885. ("OElig")
  17886. ("oelig")
  17887. ("Scaron")
  17888. ("scaron")
  17889. ("Yuml")
  17890. ("circ")
  17891. ("tilde")
  17892. ("ensp")
  17893. ("emsp")
  17894. ("thinsp")
  17895. ("zwnj")
  17896. ("zwj")
  17897. ("lrm")
  17898. ("rlm")
  17899. ("ndash")
  17900. ("mdash")
  17901. ("lsquo")
  17902. ("rsquo")
  17903. ("sbquo")
  17904. ("ldquo")
  17905. ("rdquo")
  17906. ("bdquo")
  17907. ("dagger")
  17908. ("Dagger")
  17909. ("permil")
  17910. ("lsaquo")
  17911. ("rsaquo")
  17912. ("euro")
  17913. ("arccos"."arccos")
  17914. ("arcsin"."arcsin")
  17915. ("arctan"."arctan")
  17916. ("arg"."arg")
  17917. ("cos"."cos")
  17918. ("cosh"."cosh")
  17919. ("cot"."cot")
  17920. ("coth"."coth")
  17921. ("csc"."csc")
  17922. ("deg"."deg")
  17923. ("det"."det")
  17924. ("dim"."dim")
  17925. ("exp"."exp")
  17926. ("gcd"."gcd")
  17927. ("hom"."hom")
  17928. ("inf"."inf")
  17929. ("ker"."ker")
  17930. ("lg"."lg")
  17931. ("lim"."lim")
  17932. ("liminf"."liminf")
  17933. ("limsup"."limsup")
  17934. ("ln"."ln")
  17935. ("log"."log")
  17936. ("max"."max")
  17937. ("min"."min")
  17938. ("Pr"."Pr")
  17939. ("sec"."sec")
  17940. ("sin"."sin")
  17941. ("sinh"."sinh")
  17942. ("sup"."sup")
  17943. ("tan"."tan")
  17944. ("tanh"."tanh")
  17945. )
  17946. "Entities for TeX->HTML translation.
  17947. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  17948. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  17949. In that case, \"\\ent\" will be translated to \"&other;\".
  17950. The list contains HTML entities for Latin-1, Greek and other symbols.
  17951. It is supplemented by a number of commonly used TeX macros with appropriate
  17952. translations. There is currently no way for users to extend this.")
  17953. ;;; General functions for all backends
  17954. (defun org-cleaned-string-for-export (string &rest parameters)
  17955. "Cleanup a buffer substring so that links can be created safely."
  17956. (interactive)
  17957. (let* ((re-radio (and org-target-link-regexp
  17958. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  17959. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  17960. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  17961. (re-archive (concat ":" org-archive-tag ":"))
  17962. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  17963. (htmlp (plist-get parameters :for-html))
  17964. (outline-regexp "\\*+")
  17965. a b
  17966. rtn p)
  17967. (save-excursion
  17968. (set-buffer (get-buffer-create " org-mode-tmp"))
  17969. (erase-buffer)
  17970. (insert string)
  17971. ;; Remove license-to-kill stuff
  17972. (while (setq p (text-property-any (point-min) (point-max)
  17973. :org-license-to-kill t))
  17974. (delete-region p (next-single-property-change p :org-license-to-kill)))
  17975. (let ((org-inhibit-startup t)) (org-mode))
  17976. (untabify (point-min) (point-max))
  17977. ;; Get the correct stuff before the first headline
  17978. (when (plist-get parameters :skip-before-1st-heading)
  17979. (goto-char (point-min))
  17980. (when (re-search-forward "^\\*+[ \t]" nil t)
  17981. (delete-region (point-min) (match-beginning 0))
  17982. (goto-char (point-min))
  17983. (insert "\n")))
  17984. (when (plist-get parameters :add-text)
  17985. (goto-char (point-min))
  17986. (insert (plist-get parameters :add-text) "\n"))
  17987. ;; Get rid of archived trees
  17988. (when (not (eq org-export-with-archived-trees t))
  17989. (goto-char (point-min))
  17990. (while (re-search-forward re-archive nil t)
  17991. (if (not (org-on-heading-p t))
  17992. (org-end-of-subtree t)
  17993. (beginning-of-line 1)
  17994. (setq a (if org-export-with-archived-trees
  17995. (1+ (point-at-eol)) (point))
  17996. b (org-end-of-subtree t))
  17997. (if (> b a) (delete-region a b)))))
  17998. ;; Get rid of property drawers
  17999. (unless org-export-with-property-drawer
  18000. (goto-char (point-min))
  18001. (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t)
  18002. (replace-match "")))
  18003. ;; Protect stuff from HTML processing
  18004. (goto-char (point-min))
  18005. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  18006. (add-text-properties (match-beginning 0) (match-end 0)
  18007. '(org-protected t)))
  18008. (when htmlp
  18009. (goto-char (point-min))
  18010. (while (re-search-forward "^#\\+HTML:[ \t]*\\(.*\\)" nil t)
  18011. (replace-match "\\1" t)
  18012. (add-text-properties
  18013. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  18014. '(org-protected t))))
  18015. (goto-char (point-min))
  18016. (while (re-search-forward
  18017. "^#\\+BEGIN_HTML\\>.*\\(\\(\n.*\\)*?\n\\)#\\+END_HTML\\>.*\n?" nil t)
  18018. (if htmlp
  18019. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  18020. '(org-protected t))
  18021. (delete-region (match-beginning 0) (match-end 0))))
  18022. (goto-char (point-min))
  18023. (while (re-search-forward re-quote nil t)
  18024. (goto-char (match-beginning 0))
  18025. (end-of-line 1)
  18026. (add-text-properties (point) (org-end-of-subtree t)
  18027. '(org-protected t)))
  18028. ;; Find targets in comments and move them out of comments,
  18029. ;; but mark them as targets that should be invisible
  18030. (goto-char (point-min))
  18031. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  18032. (replace-match "\\1(INVISIBLE)"))
  18033. ;; Remove comments
  18034. (goto-char (point-min))
  18035. (while (re-search-forward "^#.*\n?" nil t)
  18036. (replace-match ""))
  18037. ;; Find matches for radio targets and turn them into internal links
  18038. (goto-char (point-min))
  18039. (when re-radio
  18040. (while (re-search-forward re-radio nil t)
  18041. (org-if-unprotected
  18042. (replace-match "\\1[[\\2]]"))))
  18043. ;; Find all links that contain a newline and put them into a single line
  18044. (goto-char (point-min))
  18045. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  18046. (org-if-unprotected
  18047. (replace-match "\\1 \\3")
  18048. (goto-char (match-beginning 0))))
  18049. ;; Convert LaTeX fragments to images
  18050. (when (plist-get parameters :LaTeX-fragments)
  18051. (org-format-latex
  18052. (concat "ltxpng/" (file-name-sans-extension
  18053. (file-name-nondirectory
  18054. org-current-export-file)))
  18055. org-current-export-dir nil "Creating LaTeX image %s"))
  18056. (message "Exporting...")
  18057. ;; Normalize links: Convert angle and plain links into bracket links
  18058. ;; Expand link abbreviations
  18059. (goto-char (point-min))
  18060. (while (re-search-forward re-plain-link nil t)
  18061. (goto-char (1- (match-end 0)))
  18062. (org-if-unprotected
  18063. (replace-match
  18064. (concat
  18065. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  18066. t t)))
  18067. (goto-char (point-min))
  18068. (while (re-search-forward re-angle-link nil t)
  18069. (goto-char (1- (match-end 0)))
  18070. (org-if-unprotected
  18071. (replace-match
  18072. (concat
  18073. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  18074. t t)))
  18075. (goto-char (point-min))
  18076. (while (re-search-forward org-bracket-link-regexp nil t)
  18077. (org-if-unprotected
  18078. (replace-match
  18079. (concat "[[" (save-match-data
  18080. (org-link-expand-abbrev (match-string 1)))
  18081. "]"
  18082. (if (match-end 3)
  18083. (match-string 2)
  18084. (concat "[" (match-string 1) "]"))
  18085. "]")
  18086. t t)))
  18087. ;; Find multiline emphasis and put them into single line
  18088. (when (plist-get parameters :emph-multiline)
  18089. (goto-char (point-min))
  18090. (while (re-search-forward org-emph-re nil t)
  18091. (if (not (= (char-after (match-beginning 3))
  18092. (char-after (match-beginning 4))))
  18093. (org-if-unprotected
  18094. (subst-char-in-region (match-beginning 0) (match-end 0)
  18095. ?\n ?\ t)
  18096. (goto-char (1- (match-end 0))))
  18097. (goto-char (1+ (match-beginning 0))))))
  18098. (setq rtn (buffer-string)))
  18099. (kill-buffer " org-mode-tmp")
  18100. rtn))
  18101. (defun org-export-grab-title-from-buffer ()
  18102. "Get a title for the current document, from looking at the buffer."
  18103. (let (buffer-read-only)
  18104. (save-excursion
  18105. (goto-char (point-min))
  18106. (let ((end (save-excursion (outline-next-heading) (point))))
  18107. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  18108. ;; Mark the line so that it will not be exported as normal text.
  18109. (org-unmodified
  18110. (add-text-properties (match-beginning 0) (match-end 0)
  18111. (list :org-license-to-kill t)))
  18112. ;; Return the title string
  18113. (org-trim (match-string 0)))))))
  18114. (defun org-solidify-link-text (s &optional alist)
  18115. "Take link text and make a safe target out of it."
  18116. (save-match-data
  18117. (let* ((rtn
  18118. (mapconcat
  18119. 'identity
  18120. (org-split-string s "[ \t\r\n]+") "--"))
  18121. (a (assoc rtn alist)))
  18122. (or (cdr a) rtn))))
  18123. ;; Variable holding the vector with section numbers
  18124. (defvar org-section-numbers (make-vector org-level-max 0))
  18125. (defun org-init-section-numbers ()
  18126. "Initialize the vector for the section numbers."
  18127. (let* ((level -1)
  18128. (numbers (nreverse (org-split-string "" "\\.")))
  18129. (depth (1- (length org-section-numbers)))
  18130. (i depth) number-string)
  18131. (while (>= i 0)
  18132. (if (> i level)
  18133. (aset org-section-numbers i 0)
  18134. (setq number-string (or (car numbers) "0"))
  18135. (if (string-match "\\`[A-Z]\\'" number-string)
  18136. (aset org-section-numbers i
  18137. (- (string-to-char number-string) ?A -1))
  18138. (aset org-section-numbers i (string-to-number number-string)))
  18139. (pop numbers))
  18140. (setq i (1- i)))))
  18141. (defun org-section-number (&optional level)
  18142. "Return a string with the current section number.
  18143. When LEVEL is non-nil, increase section numbers on that level."
  18144. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  18145. (when level
  18146. (when (> level -1)
  18147. (aset org-section-numbers
  18148. level (1+ (aref org-section-numbers level))))
  18149. (setq idx (1+ level))
  18150. (while (<= idx depth)
  18151. (if (not (= idx 1))
  18152. (aset org-section-numbers idx 0))
  18153. (setq idx (1+ idx))))
  18154. (setq idx 0)
  18155. (while (<= idx depth)
  18156. (setq n (aref org-section-numbers idx))
  18157. (setq string (concat string (if (not (string= string "")) "." "")
  18158. (int-to-string n)))
  18159. (setq idx (1+ idx)))
  18160. (save-match-data
  18161. (if (string-match "\\`\\([@0]\\.\\)+" string)
  18162. (setq string (replace-match "" t nil string)))
  18163. (if (string-match "\\(\\.0\\)+\\'" string)
  18164. (setq string (replace-match "" t nil string))))
  18165. string))
  18166. ;;; ASCII export
  18167. (defvar org-last-level nil) ; dynamically scoped variable
  18168. (defvar org-levels-open nil) ; dynamically scoped parameter
  18169. (defvar org-ascii-current-indentation nil) ; For communication
  18170. (defun org-export-as-ascii (arg)
  18171. "Export the outline as a pretty ASCII file.
  18172. If there is an active region, export only the region.
  18173. The prefix ARG specifies how many levels of the outline should become
  18174. underlined headlines. The default is 3."
  18175. (interactive "P")
  18176. (setq-default org-todo-line-regexp org-todo-line-regexp)
  18177. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  18178. (org-infile-export-plist)))
  18179. (custom-times org-display-custom-times)
  18180. (org-ascii-current-indentation '(0 . 0))
  18181. (level 0) line txt
  18182. (umax nil)
  18183. (umax-toc nil)
  18184. (case-fold-search nil)
  18185. (filename (concat (file-name-as-directory
  18186. (org-export-directory :ascii opt-plist))
  18187. (file-name-sans-extension
  18188. (file-name-nondirectory buffer-file-name))
  18189. ".txt"))
  18190. (buffer (find-file-noselect filename))
  18191. (org-levels-open (make-vector org-level-max nil))
  18192. (odd org-odd-levels-only)
  18193. (date (format-time-string "%Y/%m/%d" (current-time)))
  18194. (time (format-time-string "%X" (org-current-time)))
  18195. (author (plist-get opt-plist :author))
  18196. (title (or (plist-get opt-plist :title)
  18197. (and (not
  18198. (plist-get opt-plist :skip-before-1st-heading))
  18199. (org-export-grab-title-from-buffer))
  18200. (file-name-sans-extension
  18201. (file-name-nondirectory buffer-file-name))))
  18202. (email (plist-get opt-plist :email))
  18203. (language (plist-get opt-plist :language))
  18204. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  18205. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  18206. (todo nil)
  18207. (lang-words nil)
  18208. (region
  18209. (buffer-substring
  18210. (if (org-region-active-p) (region-beginning) (point-min))
  18211. (if (org-region-active-p) (region-end) (point-max))))
  18212. (lines (org-skip-comments
  18213. (org-split-string
  18214. (org-cleaned-string-for-export
  18215. region
  18216. :skip-before-1st-heading
  18217. (plist-get opt-plist :skip-before-1st-heading)
  18218. :add-text (plist-get opt-plist :text))
  18219. "[\r\n]")))
  18220. thetoc have-headings first-heading-pos
  18221. table-open table-buffer)
  18222. (let (buffer-read-only)
  18223. (org-unmodified
  18224. (remove-text-properties (point-min) (point-max)
  18225. '(:org-license-to-kill t))))
  18226. (setq org-last-level 1)
  18227. (org-init-section-numbers)
  18228. (find-file-noselect filename)
  18229. (setq lang-words (or (assoc language org-export-language-setup)
  18230. (assoc "en" org-export-language-setup)))
  18231. (switch-to-buffer-other-window buffer)
  18232. (erase-buffer)
  18233. (fundamental-mode)
  18234. ;; create local variables for all options, to make sure all called
  18235. ;; functions get the correct information
  18236. (mapcar (lambda (x)
  18237. (set (make-local-variable (cdr x))
  18238. (plist-get opt-plist (car x))))
  18239. org-export-plist-vars)
  18240. (org-set-local 'org-odd-levels-only odd)
  18241. (setq umax (if arg (prefix-numeric-value arg)
  18242. org-export-headline-levels))
  18243. (setq umax-toc (if (integerp org-export-with-toc)
  18244. (min org-export-with-toc umax)
  18245. umax))
  18246. ;; File header
  18247. (if title (org-insert-centered title ?=))
  18248. (insert "\n")
  18249. (if (or author email)
  18250. (insert (concat (nth 1 lang-words) ": " (or author "")
  18251. (if email (concat " <" email ">") "")
  18252. "\n")))
  18253. (if (and date time)
  18254. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  18255. (insert "\n\n")
  18256. (if org-export-with-toc
  18257. (progn
  18258. (push (concat (nth 3 lang-words) "\n") thetoc)
  18259. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  18260. (mapcar '(lambda (line)
  18261. (if (string-match org-todo-line-regexp
  18262. line)
  18263. ;; This is a headline
  18264. (progn
  18265. (setq have-headings t)
  18266. (setq level (- (match-end 1) (match-beginning 1))
  18267. level (org-tr-level level)
  18268. txt (match-string 3 line)
  18269. todo
  18270. (or (and org-export-mark-todo-in-toc
  18271. (match-beginning 2)
  18272. (not (member (match-string 2 line)
  18273. org-done-keywords)))
  18274. ; TODO, not DONE
  18275. (and org-export-mark-todo-in-toc
  18276. (= level umax-toc)
  18277. (org-search-todo-below
  18278. line lines level))))
  18279. (setq txt (org-html-expand-for-ascii txt))
  18280. (if (and (memq org-export-with-tags '(not-in-toc nil))
  18281. (string-match
  18282. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  18283. txt))
  18284. (setq txt (replace-match "" t t txt)))
  18285. (if (string-match quote-re0 txt)
  18286. (setq txt (replace-match "" t t txt)))
  18287. (if org-export-with-section-numbers
  18288. (setq txt (concat (org-section-number level)
  18289. " " txt)))
  18290. (if (<= level umax-toc)
  18291. (progn
  18292. (push
  18293. (concat
  18294. (make-string (* (1- level) 4) ?\ )
  18295. (format (if todo "%s (*)\n" "%s\n") txt))
  18296. thetoc)
  18297. (setq org-last-level level))
  18298. ))))
  18299. lines)
  18300. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  18301. (org-init-section-numbers)
  18302. (while (setq line (pop lines))
  18303. ;; Remove the quoted HTML tags.
  18304. (setq line (org-html-expand-for-ascii line))
  18305. ;; Remove targets
  18306. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  18307. (setq line (replace-match "" t t line)))
  18308. ;; Replace internal links
  18309. (while (string-match org-bracket-link-regexp line)
  18310. (setq line (replace-match
  18311. (if (match-end 3) "[\\3]" "[\\1]")
  18312. t nil line)))
  18313. (when custom-times
  18314. (setq line (org-translate-time line)))
  18315. (cond
  18316. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  18317. ;; a Headline
  18318. (setq first-heading-pos (or first-heading-pos (point)))
  18319. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  18320. txt (match-string 2 line))
  18321. (org-ascii-level-start level txt umax lines))
  18322. ((and org-export-with-tables
  18323. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  18324. (if (not table-open)
  18325. ;; New table starts
  18326. (setq table-open t table-buffer nil))
  18327. ;; Accumulate lines
  18328. (setq table-buffer (cons line table-buffer))
  18329. (when (or (not lines)
  18330. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  18331. (car lines))))
  18332. (setq table-open nil
  18333. table-buffer (nreverse table-buffer))
  18334. (insert (mapconcat
  18335. (lambda (x)
  18336. (org-fix-indentation x org-ascii-current-indentation))
  18337. (org-format-table-ascii table-buffer)
  18338. "\n") "\n")))
  18339. (t
  18340. (setq line (org-fix-indentation line org-ascii-current-indentation))
  18341. (if (and org-export-with-fixed-width
  18342. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  18343. (setq line (replace-match "\\1" nil nil line)))
  18344. (insert line "\n"))))
  18345. (normal-mode)
  18346. ;; insert the table of contents
  18347. (when thetoc
  18348. (goto-char (point-min))
  18349. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  18350. (progn
  18351. (goto-char (match-beginning 0))
  18352. (replace-match ""))
  18353. (goto-char first-heading-pos))
  18354. (mapc 'insert thetoc)
  18355. (or (looking-at "[ \t]*\n[ \t]*\n")
  18356. (insert "\n\n")))
  18357. (save-buffer)
  18358. ;; remove display and invisible chars
  18359. (let (beg end)
  18360. (goto-char (point-min))
  18361. (while (setq beg (next-single-property-change (point) 'display))
  18362. (setq end (next-single-property-change beg 'display))
  18363. (delete-region beg end)
  18364. (goto-char beg)
  18365. (insert "=>"))
  18366. (goto-char (point-min))
  18367. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  18368. (setq end (next-single-property-change beg 'org-cwidth))
  18369. (delete-region beg end)
  18370. (goto-char beg)))
  18371. (goto-char (point-min))))
  18372. (defun org-search-todo-below (line lines level)
  18373. "Search the subtree below LINE for any TODO entries."
  18374. (let ((rest (cdr (memq line lines)))
  18375. (re org-todo-line-regexp)
  18376. line lv todo)
  18377. (catch 'exit
  18378. (while (setq line (pop rest))
  18379. (if (string-match re line)
  18380. (progn
  18381. (setq lv (- (match-end 1) (match-beginning 1))
  18382. todo (and (match-beginning 2)
  18383. (not (member (match-string 2 line)
  18384. org-done-keywords))))
  18385. ; TODO, not DONE
  18386. (if (<= lv level) (throw 'exit nil))
  18387. (if todo (throw 'exit t))))))))
  18388. (defun org-html-expand-for-ascii (line)
  18389. "Handle quoted HTML for ASCII export."
  18390. (if org-export-html-expand
  18391. (while (string-match "@<[^<>\n]*>" line)
  18392. ;; We just remove the tags for now.
  18393. (setq line (replace-match "" nil nil line))))
  18394. line)
  18395. (defun org-insert-centered (s &optional underline)
  18396. "Insert the string S centered and underline it with character UNDERLINE."
  18397. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  18398. (insert (make-string ind ?\ ) s "\n")
  18399. (if underline
  18400. (insert (make-string ind ?\ )
  18401. (make-string (string-width s) underline)
  18402. "\n"))))
  18403. (defun org-ascii-level-start (level title umax &optional lines)
  18404. "Insert a new level in ASCII export."
  18405. (let (char (n (- level umax 1)) (ind 0))
  18406. (if (> level umax)
  18407. (progn
  18408. (insert (make-string (* 2 n) ?\ )
  18409. (char-to-string (nth (% n (length org-export-ascii-bullets))
  18410. org-export-ascii-bullets))
  18411. " " title "\n")
  18412. ;; find the indentation of the next non-empty line
  18413. (catch 'stop
  18414. (while lines
  18415. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  18416. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  18417. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  18418. (pop lines)))
  18419. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  18420. (if (or (not (equal (char-before) ?\n))
  18421. (not (equal (char-before (1- (point))) ?\n)))
  18422. (insert "\n"))
  18423. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  18424. (unless org-export-with-tags
  18425. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  18426. (setq title (replace-match "" t t title))))
  18427. (if org-export-with-section-numbers
  18428. (setq title (concat (org-section-number level) " " title)))
  18429. (insert title "\n" (make-string (string-width title) char) "\n")
  18430. (setq org-ascii-current-indentation '(0 . 0)))))
  18431. (defun org-export-visible (type arg)
  18432. "Create a copy of the visible part of the current buffer, and export it.
  18433. The copy is created in a temporary buffer and removed after use.
  18434. TYPE is the final key (as a string) that also select the export command in
  18435. the `C-c C-e' export dispatcher.
  18436. As a special case, if the you type SPC at the prompt, the temporary
  18437. org-mode file will not be removed but presented to you so that you can
  18438. continue to use it. The prefix arg ARG is passed through to the exporting
  18439. command."
  18440. (interactive
  18441. (list (progn
  18442. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  18443. (read-char-exclusive))
  18444. current-prefix-arg))
  18445. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  18446. (error "Invalid export key"))
  18447. (let* ((binding (cdr (assoc type
  18448. '((?a . org-export-as-ascii)
  18449. (?\C-a . org-export-as-ascii)
  18450. (?b . org-export-as-html-and-open)
  18451. (?\C-b . org-export-as-html-and-open)
  18452. (?h . org-export-as-html)
  18453. (?H . org-export-as-html-to-buffer)
  18454. (?R . org-export-region-as-html)
  18455. (?x . org-export-as-xoxo)))))
  18456. (keepp (equal type ?\ ))
  18457. (file buffer-file-name)
  18458. (buffer (get-buffer-create "*Org Export Visible*"))
  18459. s e)
  18460. (with-current-buffer buffer (erase-buffer))
  18461. (save-excursion
  18462. (setq s (goto-char (point-min)))
  18463. (while (not (= (point) (point-max)))
  18464. (goto-char (org-find-invisible))
  18465. (append-to-buffer buffer s (point))
  18466. (setq s (goto-char (org-find-visible))))
  18467. (goto-char (point-min))
  18468. (unless keepp
  18469. ;; Copy all comment lines to the end, to make sure #+ settings are
  18470. ;; still available for the second export step. Kind of a hack, but
  18471. ;; does do the trick.
  18472. (if (looking-at "#[^\r\n]*")
  18473. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  18474. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  18475. (append-to-buffer buffer (1+ (match-beginning 0))
  18476. (min (point-max) (1+ (match-end 0))))))
  18477. (set-buffer buffer)
  18478. (let ((buffer-file-name file)
  18479. (org-inhibit-startup t))
  18480. (org-mode)
  18481. (show-all)
  18482. (unless keepp (funcall binding arg))))
  18483. (if (not keepp)
  18484. (kill-buffer buffer)
  18485. (switch-to-buffer-other-window buffer)
  18486. (goto-char (point-min)))))
  18487. (defun org-find-visible ()
  18488. (let ((s (point)))
  18489. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18490. (get-char-property s 'invisible)))
  18491. s))
  18492. (defun org-find-invisible ()
  18493. (let ((s (point)))
  18494. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18495. (not (get-char-property s 'invisible))))
  18496. s))
  18497. ;;; HTML export
  18498. (defun org-get-current-options ()
  18499. "Return a string with current options as keyword options.
  18500. Does include HTML export options as well as TODO and CATEGORY stuff."
  18501. (format
  18502. "#+TITLE: %s
  18503. #+AUTHOR: %s
  18504. #+EMAIL: %s
  18505. #+LANGUAGE: %s
  18506. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  18507. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s p:%s
  18508. #+CATEGORY: %s
  18509. #+SEQ_TODO: %s
  18510. #+TYP_TODO: %s
  18511. #+PRIORITIES: %c %c %c
  18512. #+STARTUP: %s %s %s %s %s
  18513. #+TAGS: %s
  18514. #+ARCHIVE: %s
  18515. #+LINK: %s
  18516. "
  18517. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  18518. org-export-headline-levels
  18519. org-export-with-section-numbers
  18520. org-export-with-toc
  18521. org-export-preserve-breaks
  18522. org-export-html-expand
  18523. org-export-with-fixed-width
  18524. org-export-with-tables
  18525. org-export-with-sub-superscripts
  18526. org-export-with-footnotes
  18527. org-export-with-emphasize
  18528. org-export-with-TeX-macros
  18529. org-export-with-LaTeX-fragments
  18530. org-export-skip-text-before-1st-heading
  18531. org-export-with-property-drawer
  18532. (file-name-nondirectory buffer-file-name)
  18533. "TODO FEEDBACK VERIFY DONE"
  18534. "Me Jason Marie DONE"
  18535. org-highest-priority org-lowest-priority org-default-priority
  18536. (cdr (assoc org-startup-folded
  18537. '((nil . "showall") (t . "overview") (content . "content"))))
  18538. (if org-odd-levels-only "odd" "oddeven")
  18539. (if org-hide-leading-stars "hidestars" "showstars")
  18540. (if org-startup-align-all-tables "align" "noalign")
  18541. (cond ((eq t org-log-done) "logdone")
  18542. ((not org-log-done) "nologging")
  18543. ((listp org-log-done)
  18544. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  18545. org-log-done " ")))
  18546. (or (mapconcat (lambda (x)
  18547. (cond
  18548. ((equal '(:startgroup) x) "{")
  18549. ((equal '(:endgroup) x) "}")
  18550. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  18551. (t (car x))))
  18552. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  18553. org-archive-location
  18554. "org file:~/org/%s.org"
  18555. ))
  18556. (defun org-insert-export-options-template ()
  18557. "Insert into the buffer a template with information for exporting."
  18558. (interactive)
  18559. (if (not (bolp)) (newline))
  18560. (let ((s (org-get-current-options)))
  18561. (and (string-match "#\\+CATEGORY" s)
  18562. (setq s (substring s 0 (match-beginning 0))))
  18563. (insert s)))
  18564. (defun org-toggle-fixed-width-section (arg)
  18565. "Toggle the fixed-width export.
  18566. If there is no active region, the QUOTE keyword at the current headline is
  18567. inserted or removed. When present, it causes the text between this headline
  18568. and the next to be exported as fixed-width text, and unmodified.
  18569. If there is an active region, this command adds or removes a colon as the
  18570. first character of this line. If the first character of a line is a colon,
  18571. this line is also exported in fixed-width font."
  18572. (interactive "P")
  18573. (let* ((cc 0)
  18574. (regionp (org-region-active-p))
  18575. (beg (if regionp (region-beginning) (point)))
  18576. (end (if regionp (region-end)))
  18577. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18578. (re "[ \t]*\\(:\\)")
  18579. off)
  18580. (if regionp
  18581. (save-excursion
  18582. (goto-char beg)
  18583. (setq cc (current-column))
  18584. (beginning-of-line 1)
  18585. (setq off (looking-at re))
  18586. (while (> nlines 0)
  18587. (setq nlines (1- nlines))
  18588. (beginning-of-line 1)
  18589. (cond
  18590. (arg
  18591. (move-to-column cc t)
  18592. (insert ":\n")
  18593. (forward-line -1))
  18594. ((and off (looking-at re))
  18595. (replace-match "" t t nil 1))
  18596. ((not off) (move-to-column cc t) (insert ":")))
  18597. (forward-line 1)))
  18598. (save-excursion
  18599. (org-back-to-heading)
  18600. (if (looking-at (concat outline-regexp
  18601. "\\( +\\<" org-quote-string "\\>\\)"))
  18602. (replace-match "" t t nil 1)
  18603. (if (looking-at outline-regexp)
  18604. (progn
  18605. (goto-char (match-end 0))
  18606. (insert " " org-quote-string))))))))
  18607. (defun org-export-as-html-and-open (arg)
  18608. "Export the outline as HTML and immediately open it with a browser.
  18609. If there is an active region, export only the region.
  18610. The prefix ARG specifies how many levels of the outline should become
  18611. headlines. The default is 3. Lower levels will become bulleted lists."
  18612. (interactive "P")
  18613. (org-export-as-html arg 'hidden)
  18614. (org-open-file buffer-file-name))
  18615. (defun org-export-as-html-batch ()
  18616. "Call `org-export-as-html', may be used in batch processing as
  18617. emacs --batch
  18618. --load=$HOME/lib/emacs/org.el
  18619. --eval \"(setq org-export-headline-levels 2)\"
  18620. --visit=MyFile --funcall org-export-as-html-batch"
  18621. (org-export-as-html org-export-headline-levels 'hidden))
  18622. (defun org-export-as-html-to-buffer (arg)
  18623. "Call `org-exort-as-html` with output to a temporary buffer.
  18624. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  18625. (interactive "P")
  18626. (org-export-as-html arg nil nil "*Org HTML Export*")
  18627. (switch-to-buffer-other-window "*Org HTML Export*"))
  18628. (defun org-replace-region-by-html (beg end)
  18629. "Assume the current region has org-mode syntax, and convert it to HTML.
  18630. This can be used in any buffer. For example, you could write an
  18631. itemized list in org-mode syntax in an HTML buffer and then use this
  18632. command to convert it."
  18633. (interactive "r")
  18634. (let (reg html buf)
  18635. (if (org-mode-p)
  18636. (setq html (org-export-region-as-html
  18637. beg end t 'string))
  18638. (setq reg (buffer-substring beg end)
  18639. buf (get-buffer-create "*Org tmp*"))
  18640. (save-excursion
  18641. (set-buffer buf)
  18642. (erase-buffer)
  18643. (insert reg)
  18644. (org-mode)
  18645. (setq html (org-export-region-as-html
  18646. (point-min) (point-max) t 'string)))
  18647. (kill-buffer buf))
  18648. (delete-region beg end)
  18649. (insert html)))
  18650. (defun org-export-region-as-html (beg end &optional body-only buffer)
  18651. "Convert region from BEG to END in org-mode buffer to HTML.
  18652. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  18653. contents, and only produce the region of converted text, useful for
  18654. cut-and-paste operations.
  18655. If BUFFER is a buffer or a string, use/create that buffer as a target
  18656. of the converted HTML. If BUFFER is the symbol `string', return the
  18657. produced HTML as a string and leave not buffer behind. For example,
  18658. a Lisp program could call this function in the following way:
  18659. (setq html (org-export-region-as-html beg end t 'string))
  18660. When called interactively, the output buffer is selected, and shown
  18661. in a window. A non-interactive call will only retunr the buffer."
  18662. (interactive "r\nP")
  18663. (when (interactive-p)
  18664. (setq buffer "*Org HTML EXPORT*"))
  18665. (let ((transient-mark-mode t) (zmacs-regions t)
  18666. rtn)
  18667. (goto-char end)
  18668. (set-mark (point)) ;; to activate the region
  18669. (goto-char beg)
  18670. (setq rtn (org-export-as-html
  18671. nil nil nil
  18672. buffer body-only))
  18673. (if (fboundp 'deactivate-mark) (deactivate-mark))
  18674. (if (and (interactive-p) (bufferp rtn))
  18675. (switch-to-buffer-other-window rtn)
  18676. rtn)))
  18677. (defun org-export-as-html (arg &optional hidden ext-plist
  18678. to-buffer body-only)
  18679. "Export the outline as a pretty HTML file.
  18680. If there is an active region, export only the region. The prefix
  18681. ARG specifies how many levels of the outline should become
  18682. headlines. The default is 3. Lower levels will become bulleted
  18683. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  18684. EXT-PLIST is a property list with external parameters overriding
  18685. org-mode's default settings, but still inferior to file-local
  18686. settings. When TO-BUFFER is non-nil, create a buffer with that
  18687. name and export to that buffer. If TO-BUFFER is the symbol `string',
  18688. don't leave any buffer behind but just return the resulting HTML as
  18689. a string. When BODY-ONLY is set, don't produce the file header and footer,
  18690. simply return the content of <body>...</body>, without even
  18691. the body tags themselves."
  18692. (interactive "P")
  18693. ;; Make sure we have a file name when we need it.
  18694. (when (and (not (or to-buffer body-only))
  18695. (not buffer-file-name))
  18696. (if (buffer-base-buffer)
  18697. (org-set-local 'buffer-file-name
  18698. (with-current-buffer (buffer-base-buffer)
  18699. buffer-file-name))
  18700. (error "Need a file name to be able to export.")))
  18701. (message "Exporting...")
  18702. (setq-default org-todo-line-regexp org-todo-line-regexp)
  18703. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  18704. (setq-default org-done-keywords org-done-keywords)
  18705. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  18706. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  18707. ext-plist
  18708. (org-infile-export-plist)))
  18709. (style (plist-get opt-plist :style))
  18710. (link-validate (plist-get opt-plist :link-validation-function))
  18711. valid thetoc have-headings first-heading-pos
  18712. (odd org-odd-levels-only)
  18713. (region-p (org-region-active-p))
  18714. ;; The following two are dynamically scoped into other
  18715. ;; routines below.
  18716. (org-current-export-dir (org-export-directory :html opt-plist))
  18717. (org-current-export-file buffer-file-name)
  18718. (level 0) (line "") (origline "") txt todo
  18719. (umax nil)
  18720. (umax-toc nil)
  18721. (filename (if to-buffer nil
  18722. (concat (file-name-as-directory
  18723. (org-export-directory :html opt-plist))
  18724. (file-name-sans-extension
  18725. (file-name-nondirectory buffer-file-name))
  18726. ".html")))
  18727. (current-dir (if buffer-file-name
  18728. (file-name-directory buffer-file-name)
  18729. default-directory))
  18730. (buffer (if to-buffer
  18731. (cond
  18732. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  18733. (t (get-buffer-create to-buffer)))
  18734. (find-file-noselect filename)))
  18735. (org-levels-open (make-vector org-level-max nil))
  18736. (date (format-time-string "%Y/%m/%d" (current-time)))
  18737. (time (format-time-string "%X" (org-current-time)))
  18738. (author (plist-get opt-plist :author))
  18739. (title (or (plist-get opt-plist :title)
  18740. (and (not
  18741. (plist-get opt-plist :skip-before-1st-heading))
  18742. (org-export-grab-title-from-buffer))
  18743. (and buffer-file-name
  18744. (file-name-sans-extension
  18745. (file-name-nondirectory buffer-file-name)))
  18746. "UNTITLED"))
  18747. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  18748. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  18749. (inquote nil)
  18750. (infixed nil)
  18751. (in-local-list nil)
  18752. (local-list-num nil)
  18753. (local-list-indent nil)
  18754. (llt org-plain-list-ordered-item-terminator)
  18755. (email (plist-get opt-plist :email))
  18756. (language (plist-get opt-plist :language))
  18757. (lang-words nil)
  18758. (target-alist nil) tg
  18759. (head-count 0) cnt
  18760. (start 0)
  18761. (coding-system (and (boundp 'buffer-file-coding-system)
  18762. buffer-file-coding-system))
  18763. (coding-system-for-write (or org-export-html-coding-system
  18764. coding-system))
  18765. (save-buffer-coding-system (or org-export-html-coding-system
  18766. coding-system))
  18767. (charset (and coding-system-for-write
  18768. (fboundp 'coding-system-get)
  18769. (coding-system-get coding-system-for-write
  18770. 'mime-charset)))
  18771. (region
  18772. (buffer-substring
  18773. (if region-p (region-beginning) (point-min))
  18774. (if region-p (region-end) (point-max))))
  18775. (lines
  18776. (org-skip-comments (org-split-string
  18777. (org-cleaned-string-for-export
  18778. region
  18779. :emph-multiline t
  18780. :for-html t
  18781. :skip-before-1st-heading
  18782. (plist-get opt-plist :skip-before-1st-heading)
  18783. :add-text
  18784. (plist-get opt-plist :text)
  18785. :LaTeX-fragments
  18786. (plist-get opt-plist :LaTeX-fragments))
  18787. "[\r\n]")))
  18788. table-open type
  18789. table-buffer table-orig-buffer
  18790. ind start-is-num starter didclose
  18791. rpl path desc descp desc1 desc2 link
  18792. )
  18793. (let (buffer-read-only)
  18794. (org-unmodified
  18795. (remove-text-properties (point-min) (point-max)
  18796. '(:org-license-to-kill t))))
  18797. (message "Exporting...")
  18798. (setq org-last-level 1)
  18799. (org-init-section-numbers)
  18800. ;; Get the language-dependent settings
  18801. (setq lang-words (or (assoc language org-export-language-setup)
  18802. (assoc "en" org-export-language-setup)))
  18803. ;; Switch to the output buffer
  18804. (set-buffer buffer)
  18805. (erase-buffer)
  18806. (fundamental-mode)
  18807. (let ((case-fold-search nil)
  18808. (org-odd-levels-only odd))
  18809. ;; create local variables for all options, to make sure all called
  18810. ;; functions get the correct information
  18811. (mapcar (lambda (x)
  18812. (set (make-local-variable (cdr x))
  18813. (plist-get opt-plist (car x))))
  18814. org-export-plist-vars)
  18815. (setq umax (if arg (prefix-numeric-value arg)
  18816. org-export-headline-levels))
  18817. (setq umax-toc (if (integerp org-export-with-toc)
  18818. (min org-export-with-toc umax)
  18819. umax))
  18820. (unless body-only
  18821. ;; File header
  18822. (insert (format
  18823. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  18824. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  18825. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  18826. lang=\"%s\" xml:lang=\"%s\">
  18827. <head>
  18828. <title>%s</title>
  18829. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  18830. <meta name=\"generator\" content=\"Org-mode\"/>
  18831. <meta name=\"generated\" content=\"%s %s\"/>
  18832. <meta name=\"author\" content=\"%s\"/>
  18833. %s
  18834. </head><body>
  18835. "
  18836. language language (org-html-expand title)
  18837. (or charset "iso-8859-1") date time author style))
  18838. (insert (or (plist-get opt-plist :preamble) ""))
  18839. (when (plist-get opt-plist :auto-preamble)
  18840. (if title (insert (format org-export-html-title-format
  18841. (org-html-expand title))))))
  18842. (if (and org-export-with-toc (not body-only))
  18843. (progn
  18844. (push (format "<h%d>%s</h%d>\n"
  18845. org-export-html-toplevel-hlevel
  18846. (nth 3 lang-words)
  18847. org-export-html-toplevel-hlevel)
  18848. thetoc)
  18849. (push "<ul>\n<li>" thetoc)
  18850. (setq lines
  18851. (mapcar '(lambda (line)
  18852. (if (string-match org-todo-line-regexp line)
  18853. ;; This is a headline
  18854. (progn
  18855. (setq have-headings t)
  18856. (setq level (- (match-end 1) (match-beginning 1))
  18857. level (org-tr-level level)
  18858. txt (save-match-data
  18859. (org-html-expand
  18860. (org-export-cleanup-toc-line
  18861. (match-string 3 line))))
  18862. todo
  18863. (or (and org-export-mark-todo-in-toc
  18864. (match-beginning 2)
  18865. (not (member (match-string 2 line)
  18866. org-done-keywords)))
  18867. ; TODO, not DONE
  18868. (and org-export-mark-todo-in-toc
  18869. (= level umax-toc)
  18870. (org-search-todo-below
  18871. line lines level))))
  18872. (if (and (memq org-export-with-tags '(not-in-toc nil))
  18873. (string-match
  18874. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  18875. txt))
  18876. (setq txt (replace-match "" t t txt)))
  18877. (if (string-match quote-re0 txt)
  18878. (setq txt (replace-match "" t t txt)))
  18879. (if org-export-with-section-numbers
  18880. (setq txt (concat (org-section-number level)
  18881. " " txt)))
  18882. (if (<= level (max umax umax-toc))
  18883. (setq head-count (+ head-count 1)))
  18884. (if (<= level umax-toc)
  18885. (progn
  18886. (if (> level org-last-level)
  18887. (progn
  18888. (setq cnt (- level org-last-level))
  18889. (while (>= (setq cnt (1- cnt)) 0)
  18890. (push "\n<ul>\n<li>" thetoc))
  18891. (push "\n" thetoc)))
  18892. (if (< level org-last-level)
  18893. (progn
  18894. (setq cnt (- org-last-level level))
  18895. (while (>= (setq cnt (1- cnt)) 0)
  18896. (push "</li>\n</ul>" thetoc))
  18897. (push "\n" thetoc)))
  18898. ;; Check for targets
  18899. (while (string-match org-target-regexp line)
  18900. (setq tg (match-string 1 line)
  18901. line (replace-match
  18902. (concat "@<span class=\"target\">" tg "@</span> ")
  18903. t t line))
  18904. (push (cons (org-solidify-link-text tg)
  18905. (format "sec-%d" head-count))
  18906. target-alist))
  18907. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  18908. (setq txt (replace-match "" t t txt)))
  18909. (push
  18910. (format
  18911. (if todo
  18912. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  18913. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  18914. head-count txt) thetoc)
  18915. (setq org-last-level level))
  18916. )))
  18917. line)
  18918. lines))
  18919. (while (> org-last-level 0)
  18920. (setq org-last-level (1- org-last-level))
  18921. (push "</li>\n</ul>\n" thetoc))
  18922. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  18923. (setq head-count 0)
  18924. (org-init-section-numbers)
  18925. (while (setq line (pop lines) origline line)
  18926. (catch 'nextline
  18927. ;; end of quote section?
  18928. (when (and inquote (string-match "^\\*+" line))
  18929. (insert "</pre>\n")
  18930. (setq inquote nil))
  18931. ;; inside a quote section?
  18932. (when inquote
  18933. (insert (org-html-protect line) "\n")
  18934. (throw 'nextline nil))
  18935. ;; verbatim lines
  18936. (when (and org-export-with-fixed-width
  18937. (string-match "^[ \t]*:\\(.*\\)" line))
  18938. (when (not infixed)
  18939. (setq infixed t)
  18940. (insert "<pre>\n"))
  18941. (insert (org-html-protect (match-string 1 line)) "\n")
  18942. (when (and lines
  18943. (not (string-match "^[ \t]*\\(:.*\\)"
  18944. (car lines))))
  18945. (setq infixed nil)
  18946. (insert "</pre>\n"))
  18947. (throw 'nextline nil))
  18948. ;; Protected HTML
  18949. (when (get-text-property 0 'org-protected line)
  18950. (let (par)
  18951. (when (re-search-backward
  18952. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  18953. (setq par (match-string 1))
  18954. (replace-match "\\2\n"))
  18955. (insert line "\n")
  18956. (while (and lines
  18957. (get-text-property 0 'org-protected (car lines)))
  18958. (insert (pop lines) "\n"))
  18959. (and par (insert "<p>\n")))
  18960. (throw 'nextline nil))
  18961. ;; Horizontal line
  18962. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  18963. (insert "\n<hr/>\n")
  18964. (throw 'nextline nil))
  18965. ;; make targets to anchors
  18966. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  18967. (cond
  18968. ((match-end 2)
  18969. (setq line (replace-match
  18970. (concat "@<a name=\""
  18971. (org-solidify-link-text (match-string 1 line))
  18972. "\">\\nbsp@</a>")
  18973. t t line)))
  18974. ((and org-export-with-toc (equal (string-to-char line) ?*))
  18975. (setq line (replace-match
  18976. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  18977. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  18978. t t line)))
  18979. (t
  18980. (setq line (replace-match
  18981. (concat "@<a name=\""
  18982. (org-solidify-link-text (match-string 1 line))
  18983. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  18984. t t line)))))
  18985. (setq line (org-html-handle-time-stamps line))
  18986. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  18987. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  18988. ;; Also handle sub_superscripts and checkboxes
  18989. (setq line (org-html-expand line))
  18990. ;; Format the links
  18991. (setq start 0)
  18992. (while (string-match org-bracket-link-analytic-regexp line start)
  18993. (setq start (match-beginning 0))
  18994. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  18995. (setq path (match-string 3 line))
  18996. (setq desc1 (if (match-end 5) (match-string 5 line))
  18997. desc2 (if (match-end 2) (concat type ":" path) path)
  18998. descp (and desc1 (not (equal desc1 desc2)))
  18999. desc (or desc1 desc2))
  19000. ;; Make an image out of the description if that is so wanted
  19001. (when (and descp (org-file-image-p desc))
  19002. (save-match-data
  19003. (if (string-match "^file:" desc)
  19004. (setq desc (substring desc (match-end 0)))))
  19005. (setq desc (concat "<img src=\"" desc "\"/>")))
  19006. ;; FIXME: do we need to unescape here somewhere?
  19007. (cond
  19008. ((equal type "internal")
  19009. (setq rpl
  19010. (concat
  19011. "<a href=\"#"
  19012. (org-solidify-link-text
  19013. (save-match-data (org-link-unescape path)) target-alist)
  19014. "\">" desc "</a>")))
  19015. ((member type '("http" "https")) ; FIXME: need to test this.
  19016. ;; standard URL, just check if we need to inline an image
  19017. (if (and (or (eq t org-export-html-inline-images)
  19018. (and org-export-html-inline-images (not descp)))
  19019. (org-file-image-p path))
  19020. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  19021. (setq link (concat type ":" path))
  19022. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  19023. ((member type '("ftp" "mailto" "news"))
  19024. ;; standard URL
  19025. (setq link (concat type ":" path))
  19026. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  19027. ((string= type "file")
  19028. ;; FILE link
  19029. (let* ((filename path)
  19030. (abs-p (file-name-absolute-p filename))
  19031. thefile file-is-image-p search)
  19032. (save-match-data
  19033. (if (string-match "::\\(.*\\)" filename)
  19034. (setq search (match-string 1 filename)
  19035. filename (replace-match "" t nil filename)))
  19036. (setq valid
  19037. (if (functionp link-validate)
  19038. (funcall link-validate filename current-dir)
  19039. t))
  19040. (setq file-is-image-p (org-file-image-p filename))
  19041. (setq thefile (if abs-p (expand-file-name filename) filename))
  19042. (when (and org-export-html-link-org-files-as-html
  19043. (string-match "\\.org$" thefile))
  19044. (setq thefile (concat (substring thefile 0
  19045. (match-beginning 0))
  19046. ".html"))
  19047. (if (and search
  19048. ;; make sure this is can be used as target search
  19049. (not (string-match "^[0-9]*$" search))
  19050. (not (string-match "^\\*" search))
  19051. (not (string-match "^/.*/$" search)))
  19052. (setq thefile (concat thefile "#"
  19053. (org-solidify-link-text
  19054. (org-link-unescape search)))))
  19055. (when (string-match "^file:" desc)
  19056. (setq desc (replace-match "" t t desc))
  19057. (if (string-match "\\.org$" desc)
  19058. (setq desc (replace-match "" t t desc))))))
  19059. (setq rpl (if (and file-is-image-p
  19060. (or (eq t org-export-html-inline-images)
  19061. (and org-export-html-inline-images
  19062. (not descp))))
  19063. (concat "<img src=\"" thefile "\"/>")
  19064. (concat "<a href=\"" thefile "\">" desc "</a>")))
  19065. (if (not valid) (setq rpl desc))))
  19066. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  19067. (setq rpl (concat "<i>&lt;" type ":"
  19068. (save-match-data (org-link-unescape path))
  19069. "&gt;</i>"))))
  19070. (setq line (replace-match rpl t t line)
  19071. start (+ start (length rpl))))
  19072. ;; TODO items
  19073. (if (and (string-match org-todo-line-regexp line)
  19074. (match-beginning 2))
  19075. (setq line
  19076. (concat (substring line 0 (match-beginning 2))
  19077. "<span class=\""
  19078. (if (member (match-string 2 line)
  19079. org-done-keywords)
  19080. "done" "todo")
  19081. "\">" (match-string 2 line)
  19082. "</span>" (substring line (match-end 2)))))
  19083. ;; Does this contain a reference to a footnote?
  19084. (when org-export-with-footnotes
  19085. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  19086. (let ((n (match-string 2 line)))
  19087. (setq line
  19088. (replace-match
  19089. (format
  19090. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  19091. (match-string 1 line) n n n)
  19092. t t line)))))
  19093. (cond
  19094. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  19095. ;; This is a headline
  19096. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  19097. txt (match-string 2 line))
  19098. (if (string-match quote-re0 txt)
  19099. (setq txt (replace-match "" t t txt)))
  19100. (if (<= level (max umax umax-toc))
  19101. (setq head-count (+ head-count 1)))
  19102. (when in-local-list
  19103. ;; Close any local lists before inserting a new header line
  19104. (while local-list-num
  19105. (org-close-li)
  19106. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  19107. (pop local-list-num))
  19108. (setq local-list-indent nil
  19109. in-local-list nil))
  19110. (setq first-heading-pos (or first-heading-pos (point)))
  19111. (org-html-level-start level txt umax
  19112. (and org-export-with-toc (<= level umax))
  19113. head-count)
  19114. ;; QUOTES
  19115. (when (string-match quote-re line)
  19116. (insert "<pre>")
  19117. (setq inquote t)))
  19118. ((and org-export-with-tables
  19119. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  19120. (if (not table-open)
  19121. ;; New table starts
  19122. (setq table-open t table-buffer nil table-orig-buffer nil))
  19123. ;; Accumulate lines
  19124. (setq table-buffer (cons line table-buffer)
  19125. table-orig-buffer (cons origline table-orig-buffer))
  19126. (when (or (not lines)
  19127. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  19128. (car lines))))
  19129. (setq table-open nil
  19130. table-buffer (nreverse table-buffer)
  19131. table-orig-buffer (nreverse table-orig-buffer))
  19132. (org-close-par-maybe)
  19133. (insert (org-format-table-html table-buffer table-orig-buffer))))
  19134. (t
  19135. ;; Normal lines
  19136. (when (string-match
  19137. (cond
  19138. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  19139. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  19140. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  19141. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  19142. line)
  19143. (setq ind (org-get-string-indentation line)
  19144. start-is-num (match-beginning 4)
  19145. starter (if (match-beginning 2)
  19146. (substring (match-string 2 line) 0 -1))
  19147. line (substring line (match-beginning 5)))
  19148. (unless (string-match "[^ \t]" line)
  19149. ;; empty line. Pretend indentation is large.
  19150. (setq ind (if org-empty-line-terminates-plain-lists
  19151. 0
  19152. (1+ (or (car local-list-indent) 1)))))
  19153. (setq didclose nil)
  19154. (while (and in-local-list
  19155. (or (and (= ind (car local-list-indent))
  19156. (not starter))
  19157. (< ind (car local-list-indent))))
  19158. (setq didclose t)
  19159. (org-close-li)
  19160. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  19161. (pop local-list-num) (pop local-list-indent)
  19162. (setq in-local-list local-list-indent))
  19163. (cond
  19164. ((and starter
  19165. (or (not in-local-list)
  19166. (> ind (car local-list-indent))))
  19167. ;; Start new (level of) list
  19168. (org-close-par-maybe)
  19169. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  19170. (push start-is-num local-list-num)
  19171. (push ind local-list-indent)
  19172. (setq in-local-list t))
  19173. (starter
  19174. ;; continue current list
  19175. (org-close-li)
  19176. (insert "<li>\n"))
  19177. (didclose
  19178. ;; we did close a list, normal text follows: need <p>
  19179. (org-open-par)))
  19180. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  19181. (setq line
  19182. (replace-match
  19183. (if (equal (match-string 1 line) "X")
  19184. "<b>[X]</b>"
  19185. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  19186. t t line))))
  19187. ;; Empty lines start a new paragraph. If hand-formatted lists
  19188. ;; are not fully interpreted, lines starting with "-", "+", "*"
  19189. ;; also start a new paragraph.
  19190. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  19191. ;; Is this the start of a footnote?
  19192. (when org-export-with-footnotes
  19193. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  19194. (org-close-par-maybe)
  19195. (let ((n (match-string 1 line)))
  19196. (setq line (replace-match
  19197. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  19198. ;; Check if the line break needs to be conserved
  19199. (cond
  19200. ((string-match "\\\\\\\\[ \t]*$" line)
  19201. (setq line (replace-match "<br/>" t t line)))
  19202. (org-export-preserve-breaks
  19203. (setq line (concat line "<br/>"))))
  19204. (insert line "\n")))))
  19205. ;; Properly close all local lists and other lists
  19206. (when inquote (insert "</pre>\n"))
  19207. (when in-local-list
  19208. ;; Close any local lists before inserting a new header line
  19209. (while local-list-num
  19210. (org-close-li)
  19211. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  19212. (pop local-list-num))
  19213. (setq local-list-indent nil
  19214. in-local-list nil))
  19215. (org-html-level-start 1 nil umax
  19216. (and org-export-with-toc (<= level umax))
  19217. head-count)
  19218. (unless body-only
  19219. (when (plist-get opt-plist :auto-postamble)
  19220. (when author
  19221. (insert "<p class=\"author\"> "
  19222. (nth 1 lang-words) ": " author "\n")
  19223. (when email
  19224. (insert "<a href=\"mailto:" email "\">&lt;"
  19225. email "&gt;</a>\n"))
  19226. (insert "</p>\n"))
  19227. (when (and date time)
  19228. (insert "<p class=\"date\"> "
  19229. (nth 2 lang-words) ": "
  19230. date " " time "</p>\n")))
  19231. (if org-export-html-with-timestamp
  19232. (insert org-export-html-html-helper-timestamp))
  19233. (insert (or (plist-get opt-plist :postamble) ""))
  19234. (insert "</body>\n</html>\n"))
  19235. (normal-mode)
  19236. (if (eq major-mode default-major-mode) (html-mode))
  19237. ;; insert the table of contents
  19238. (goto-char (point-min))
  19239. (when thetoc
  19240. (if (or (re-search-forward
  19241. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  19242. (re-search-forward
  19243. "\\[TABLE-OF-CONTENTS\\]" nil t))
  19244. (progn
  19245. (goto-char (match-beginning 0))
  19246. (replace-match ""))
  19247. (goto-char first-heading-pos)
  19248. (when (looking-at "\\s-*</p>")
  19249. (goto-char (match-end 0))
  19250. (insert "\n")))
  19251. (mapc 'insert thetoc))
  19252. ;; remove empty paragraphs and lists
  19253. (goto-char (point-min))
  19254. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  19255. (replace-match ""))
  19256. (goto-char (point-min))
  19257. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  19258. (replace-match ""))
  19259. (or to-buffer (save-buffer))
  19260. (goto-char (point-min))
  19261. (message "Exporting... done")
  19262. (if (eq to-buffer 'string)
  19263. (prog1 (buffer-substring (point-min) (point-max))
  19264. (kill-buffer (current-buffer)))
  19265. (current-buffer)))))
  19266. (defvar org-table-colgroup-info nil) ;; FIXME: mode to a better place
  19267. (defun org-format-table-ascii (lines)
  19268. "Format a table for ascii export."
  19269. (if (stringp lines)
  19270. (setq lines (org-split-string lines "\n")))
  19271. (if (not (string-match "^[ \t]*|" (car lines)))
  19272. ;; Table made by table.el - test for spanning
  19273. lines
  19274. ;; A normal org table
  19275. ;; Get rid of hlines at beginning and end
  19276. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  19277. (setq lines (nreverse lines))
  19278. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  19279. (setq lines (nreverse lines))
  19280. (when org-export-table-remove-special-lines
  19281. ;; Check if the table has a marking column. If yes remove the
  19282. ;; column and the special lines
  19283. (setq lines (org-table-clean-before-export lines)))
  19284. ;; Get rid of the vertical lines except for grouping
  19285. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  19286. rtn line vl1 start)
  19287. (while (setq line (pop lines))
  19288. (if (string-match org-table-hline-regexp line)
  19289. (and (string-match "|\\(.*\\)|" line)
  19290. (setq line (replace-match " \\1" t nil line)))
  19291. (setq start 0 vl1 vl)
  19292. (while (string-match "|" line start)
  19293. (setq start (match-end 0))
  19294. (or (pop vl1) (setq line (replace-match " " t t line)))))
  19295. (push line rtn))
  19296. (nreverse rtn))))
  19297. (defun org-colgroup-info-to-vline-list (info)
  19298. (let (vl new last)
  19299. (while info
  19300. (setq last new new (pop info))
  19301. (if (or (memq last '(:end :startend))
  19302. (memq new '(:start :startend)))
  19303. (push t vl)
  19304. (push nil vl)))
  19305. (setq vl (cons nil (nreverse vl)))))
  19306. (defun org-format-table-html (lines olines)
  19307. "Find out which HTML converter to use and return the HTML code."
  19308. (if (stringp lines)
  19309. (setq lines (org-split-string lines "\n")))
  19310. (if (string-match "^[ \t]*|" (car lines))
  19311. ;; A normal org table
  19312. (org-format-org-table-html lines)
  19313. ;; Table made by table.el - test for spanning
  19314. (let* ((hlines (delq nil (mapcar
  19315. (lambda (x)
  19316. (if (string-match "^[ \t]*\\+-" x) x
  19317. nil))
  19318. lines)))
  19319. (first (car hlines))
  19320. (ll (and (string-match "\\S-+" first)
  19321. (match-string 0 first)))
  19322. (re (concat "^[ \t]*" (regexp-quote ll)))
  19323. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  19324. hlines))))
  19325. (if (and (not spanning)
  19326. (not org-export-prefer-native-exporter-for-tables))
  19327. ;; We can use my own converter with HTML conversions
  19328. (org-format-table-table-html lines)
  19329. ;; Need to use the code generator in table.el, with the original text.
  19330. (org-format-table-table-html-using-table-generate-source olines)))))
  19331. (defun org-format-org-table-html (lines &optional splice)
  19332. "Format a table into HTML."
  19333. ;; Get rid of hlines at beginning and end
  19334. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  19335. (setq lines (nreverse lines))
  19336. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  19337. (setq lines (nreverse lines))
  19338. (when org-export-table-remove-special-lines
  19339. ;; Check if the table has a marking column. If yes remove the
  19340. ;; column and the special lines
  19341. (setq lines (org-table-clean-before-export lines)))
  19342. (let ((head (and org-export-highlight-first-table-line
  19343. (delq nil (mapcar
  19344. (lambda (x) (string-match "^[ \t]*|-" x))
  19345. (cdr lines)))))
  19346. (nlines 0) fnum i
  19347. tbopen line fields html gr colgropen)
  19348. (if splice (setq head nil))
  19349. (unless splice (push (if head "<thead>" "<tbody>") html))
  19350. (setq tbopen t)
  19351. (while (setq line (pop lines))
  19352. (catch 'next-line
  19353. (if (string-match "^[ \t]*|-" line)
  19354. (progn
  19355. (unless splice
  19356. (push (if head "</thead>" "</tbody>") html)
  19357. (if lines (push "<tbody>" html) (setq tbopen nil)))
  19358. (setq head nil) ;; head ends here, first time around
  19359. ;; ignore this line
  19360. (throw 'next-line t)))
  19361. ;; Break the line into fields
  19362. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  19363. (unless fnum (setq fnum (make-vector (length fields) 0)))
  19364. (setq nlines (1+ nlines) i -1)
  19365. (push (concat "<tr>"
  19366. (mapconcat
  19367. (lambda (x)
  19368. (setq i (1+ i))
  19369. (if (and (< i nlines)
  19370. (string-match org-table-number-regexp x))
  19371. (incf (aref fnum i)))
  19372. (if head
  19373. (concat (car org-export-table-header-tags) x
  19374. (cdr org-export-table-header-tags))
  19375. (concat (car org-export-table-data-tags) x
  19376. (cdr org-export-table-data-tags))))
  19377. fields "")
  19378. "</tr>")
  19379. html)))
  19380. (unless splice (if tbopen (push "</tbody>" html)))
  19381. (unless splice (push "</table>\n" html))
  19382. (setq html (nreverse html))
  19383. (unless splice
  19384. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  19385. (push (mapconcat
  19386. (lambda (x)
  19387. (setq gr (pop org-table-colgroup-info))
  19388. (format "%s<COL align=\"%s\"></COL>%s"
  19389. (if (memq gr '(:start :startend))
  19390. (prog1
  19391. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  19392. (setq colgropen t))
  19393. "")
  19394. (if (> (/ (float x) nlines) org-table-number-fraction)
  19395. "right" "left")
  19396. (if (memq gr '(:end :startend))
  19397. (progn (setq colgropen nil) "</colgroup>")
  19398. "")))
  19399. fnum "")
  19400. html)
  19401. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  19402. (push org-export-html-table-tag html))
  19403. (concat (mapconcat 'identity html "\n") "\n")))
  19404. (defun org-table-clean-before-export (lines)
  19405. "Check if the table has a marking column.
  19406. If yes remove the column and the special lines."
  19407. (setq org-table-colgroup-info nil)
  19408. (if (memq nil
  19409. (mapcar
  19410. (lambda (x) (or (string-match "^[ \t]*|-" x)
  19411. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  19412. lines))
  19413. (progn
  19414. (setq org-table-clean-did-remove-column nil)
  19415. (delq nil
  19416. (mapcar
  19417. (lambda (x)
  19418. (cond
  19419. ((string-match "^[ \t]*| */ *|" x)
  19420. (setq org-table-colgroup-info
  19421. (mapcar (lambda (x)
  19422. (cond ((member x '("<" "&lt;")) :start)
  19423. ((member x '(">" "&gt;")) :end)
  19424. ((member x '("<>" "&lt;&gt;")) :startend)
  19425. (t nil)))
  19426. (org-split-string x "[ \t]*|[ \t]*")))
  19427. nil)
  19428. (t x)))
  19429. lines)))
  19430. (setq org-table-clean-did-remove-column t)
  19431. (delq nil
  19432. (mapcar
  19433. (lambda (x)
  19434. (cond
  19435. ((string-match "^[ \t]*| */ *|" x)
  19436. (setq org-table-colgroup-info
  19437. (mapcar (lambda (x)
  19438. (cond ((member x '("<" "&lt;")) :start)
  19439. ((member x '(">" "&gt;")) :end)
  19440. ((member x '("<>" "&lt;&gt;")) :startend)
  19441. (t nil)))
  19442. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  19443. nil)
  19444. ((string-match "^[ \t]*| *[!_^/] *|" x)
  19445. nil) ; ignore this line
  19446. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  19447. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  19448. ;; remove the first column
  19449. (replace-match "\\1|" t nil x))
  19450. (t (error "This should not happen"))))
  19451. lines))))
  19452. (defun org-format-table-table-html (lines)
  19453. "Format a table generated by table.el into HTML.
  19454. This conversion does *not* use `table-generate-source' from table.el.
  19455. This has the advantage that Org-mode's HTML conversions can be used.
  19456. But it has the disadvantage, that no cell- or row-spanning is allowed."
  19457. (let (line field-buffer
  19458. (head org-export-highlight-first-table-line)
  19459. fields html empty)
  19460. (setq html (concat org-export-html-table-tag "\n"))
  19461. (while (setq line (pop lines))
  19462. (setq empty "&nbsp;")
  19463. (catch 'next-line
  19464. (if (string-match "^[ \t]*\\+-" line)
  19465. (progn
  19466. (if field-buffer
  19467. (progn
  19468. (setq
  19469. html
  19470. (concat
  19471. html
  19472. "<tr>"
  19473. (mapconcat
  19474. (lambda (x)
  19475. (if (equal x "") (setq x empty))
  19476. (if head
  19477. (concat (car org-export-table-header-tags) x
  19478. (cdr org-export-table-header-tags))
  19479. (concat (car org-export-table-data-tags) x
  19480. (cdr org-export-table-data-tags))))
  19481. field-buffer "\n")
  19482. "</tr>\n"))
  19483. (setq head nil)
  19484. (setq field-buffer nil)))
  19485. ;; Ignore this line
  19486. (throw 'next-line t)))
  19487. ;; Break the line into fields and store the fields
  19488. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  19489. (if field-buffer
  19490. (setq field-buffer (mapcar
  19491. (lambda (x)
  19492. (concat x "<br/>" (pop fields)))
  19493. field-buffer))
  19494. (setq field-buffer fields))))
  19495. (setq html (concat html "</table>\n"))
  19496. html))
  19497. (defun org-format-table-table-html-using-table-generate-source (lines)
  19498. "Format a table into html, using `table-generate-source' from table.el.
  19499. This has the advantage that cell- or row-spanning is allowed.
  19500. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  19501. (require 'table)
  19502. (with-current-buffer (get-buffer-create " org-tmp1 ")
  19503. (erase-buffer)
  19504. (insert (mapconcat 'identity lines "\n"))
  19505. (goto-char (point-min))
  19506. (if (not (re-search-forward "|[^+]" nil t))
  19507. (error "Error processing table"))
  19508. (table-recognize-table)
  19509. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  19510. (table-generate-source 'html " org-tmp2 ")
  19511. (set-buffer " org-tmp2 ")
  19512. (buffer-substring (point-min) (point-max))))
  19513. (defun org-html-handle-time-stamps (s)
  19514. "Format time stamps in string S, or remove them."
  19515. (catch 'exit
  19516. (let (r b)
  19517. (while (string-match org-maybe-keyword-time-regexp s)
  19518. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  19519. ;; never export CLOCK
  19520. (throw 'exit ""))
  19521. (or b (setq b (substring s 0 (match-beginning 0))))
  19522. (if (not org-export-with-timestamps)
  19523. (setq r (concat r (substring s 0 (match-beginning 0)))
  19524. s (substring s (match-end 0)))
  19525. (setq r (concat
  19526. r (substring s 0 (match-beginning 0))
  19527. (if (match-end 1)
  19528. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  19529. (match-string 1 s)))
  19530. (format " @<span class=\"timestamp\">%s@</span>"
  19531. (substring
  19532. (org-translate-time (match-string 3 s)) 1 -1)))
  19533. s (substring s (match-end 0)))))
  19534. ;; Line break if line started and ended with time stamp stuff
  19535. (if (not r)
  19536. s
  19537. (setq r (concat r s))
  19538. (unless (string-match "\\S-" (concat b s))
  19539. (setq r (concat r "@<br/>")))
  19540. r))))
  19541. (defun org-html-protect (s)
  19542. ;; convert & to &amp;, < to &lt; and > to &gt;
  19543. (let ((start 0))
  19544. (while (string-match "&" s start)
  19545. (setq s (replace-match "&amp;" t t s)
  19546. start (1+ (match-beginning 0))))
  19547. (while (string-match "<" s)
  19548. (setq s (replace-match "&lt;" t t s)))
  19549. (while (string-match ">" s)
  19550. (setq s (replace-match "&gt;" t t s))))
  19551. s)
  19552. (defun org-export-cleanup-toc-line (s)
  19553. "Remove tags and time staps from lines going into the toc."
  19554. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  19555. (setq s (replace-match "" t t s)))
  19556. (when org-export-remove-timestamps-from-toc
  19557. (while (string-match org-maybe-keyword-time-regexp s)
  19558. (setq s (replace-match "" t t s))))
  19559. (while (string-match org-bracket-link-regexp s)
  19560. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  19561. t t s)))
  19562. s)
  19563. (defun org-html-expand (string)
  19564. "Prepare STRING for HTML export. Applies all active conversions.
  19565. If there are links in the string, don't modify these."
  19566. (let* (m s l res)
  19567. (while (setq m (string-match org-bracket-link-regexp string))
  19568. (setq s (substring string 0 m)
  19569. l (match-string 0 string)
  19570. string (substring string (match-end 0)))
  19571. (push (org-html-do-expand s) res)
  19572. (push l res))
  19573. (push (org-html-do-expand string) res)
  19574. (apply 'concat (nreverse res))))
  19575. (defun org-html-do-expand (s)
  19576. "Apply all active conversions to translate special ASCII to HTML."
  19577. (setq s (org-html-protect s))
  19578. (if org-export-html-expand
  19579. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  19580. (setq s (replace-match "<\\1>" t nil s))))
  19581. (if org-export-with-emphasize
  19582. (setq s (org-export-html-convert-emphasize s)))
  19583. (if org-export-with-sub-superscripts
  19584. (setq s (org-export-html-convert-sub-super s)))
  19585. (if org-export-with-TeX-macros
  19586. (let ((start 0) wd ass)
  19587. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  19588. (setq wd (match-string 1 s))
  19589. (if (setq ass (assoc wd org-html-entities))
  19590. (setq s (replace-match (or (cdr ass)
  19591. (concat "&" (car ass) ";"))
  19592. t t s))
  19593. (setq start (+ start (length wd)))))))
  19594. s)
  19595. (defun org-create-multibrace-regexp (left right n)
  19596. "Create a regular expression which will match a balanced sexp.
  19597. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  19598. as single character strings.
  19599. The regexp returned will match the entire expression including the
  19600. delimiters. It will also define a single group which contains the
  19601. match except for the outermost delimiters. The maximum depth of
  19602. stacked delimiters is N. Escaping delimiters is not possible."
  19603. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  19604. (or "\\|")
  19605. (re nothing)
  19606. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  19607. (while (> n 1)
  19608. (setq n (1- n)
  19609. re (concat re or next)
  19610. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  19611. (concat left "\\(" re "\\)" right)))
  19612. (defvar org-match-substring-regexp
  19613. (concat
  19614. "\\([^\\]\\)\\([_^]\\)\\("
  19615. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  19616. "\\|"
  19617. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  19618. "\\|"
  19619. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  19620. "The regular expression matching a sub- or superscript.")
  19621. ;(let ((s "a\\_b"))
  19622. ; (and (string-match org-match-substring-regexp s)
  19623. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  19624. (defun org-export-html-convert-sub-super (string)
  19625. "Convert sub- and superscripts in STRING to HTML."
  19626. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  19627. (while (string-match org-match-substring-regexp string s)
  19628. (if (and requireb (match-end 8))
  19629. (setq s (match-end 2))
  19630. (setq s (match-end 1)
  19631. key (if (string= (match-string 2 string) "_") "sub" "sup")
  19632. c (or (match-string 8 string)
  19633. (match-string 6 string)
  19634. (match-string 5 string))
  19635. string (replace-match
  19636. (concat (match-string 1 string)
  19637. "<" key ">" c "</" key ">")
  19638. t t string))))
  19639. (while (string-match "\\\\\\([_^]\\)" string)
  19640. (setq string (replace-match (match-string 1 string) t t string)))
  19641. string))
  19642. (defun org-export-html-convert-emphasize (string)
  19643. "Apply emphasis."
  19644. (let ((s 0))
  19645. (while (string-match org-emph-re string s)
  19646. (if (not (equal
  19647. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  19648. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  19649. (setq string (replace-match
  19650. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  19651. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  19652. "\\5") t nil string))
  19653. (setq s (1+ s))))
  19654. string))
  19655. (defvar org-par-open nil)
  19656. (defun org-open-par ()
  19657. "Insert <p>, but first close previous paragraph if any."
  19658. (org-close-par-maybe)
  19659. (insert "\n<p>")
  19660. (setq org-par-open t))
  19661. (defun org-close-par-maybe ()
  19662. "Close paragraph if there is one open."
  19663. (when org-par-open
  19664. (insert "</p>")
  19665. (setq org-par-open nil)))
  19666. (defun org-close-li ()
  19667. "Close <li> if necessary."
  19668. (org-close-par-maybe)
  19669. (insert "</li>\n"))
  19670. (defvar body-only) ; dynamically scoped into this.
  19671. (defun org-html-level-start (level title umax with-toc head-count)
  19672. "Insert a new level in HTML export.
  19673. When TITLE is nil, just close all open levels."
  19674. (org-close-par-maybe)
  19675. (let ((l (1+ (max level umax))))
  19676. (while (<= l org-level-max)
  19677. (if (aref org-levels-open (1- l))
  19678. (progn
  19679. (org-html-level-close l)
  19680. (aset org-levels-open (1- l) nil)))
  19681. (setq l (1+ l)))
  19682. (when title
  19683. ;; If title is nil, this means this function is called to close
  19684. ;; all levels, so the rest is done only if title is given
  19685. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  19686. (setq title (replace-match
  19687. (if org-export-with-tags
  19688. (save-match-data
  19689. (concat
  19690. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  19691. (mapconcat 'identity (org-split-string
  19692. (match-string 1 title) ":")
  19693. "&nbsp;")
  19694. "</span>"))
  19695. "")
  19696. t t title)))
  19697. (if (> level umax)
  19698. (progn
  19699. (if (aref org-levels-open (1- level))
  19700. (progn
  19701. (org-close-li)
  19702. (insert "<li>" title "<br/>\n"))
  19703. (aset org-levels-open (1- level) t)
  19704. (org-close-par-maybe)
  19705. (insert "<ul>\n<li>" title "<br/>\n")))
  19706. (if (and org-export-with-section-numbers (not body-only))
  19707. (setq title (concat (org-section-number level) " " title)))
  19708. (setq level (+ level org-export-html-toplevel-hlevel -1))
  19709. (if with-toc
  19710. (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
  19711. level head-count title level))
  19712. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  19713. (org-open-par)))))
  19714. (defun org-html-level-close (&rest args)
  19715. "Terminate one level in HTML export."
  19716. (org-close-li)
  19717. (insert "</ul>\n"))
  19718. ;;; iCalendar export
  19719. ;;;###autoload
  19720. (defun org-export-icalendar-this-file ()
  19721. "Export current file as an iCalendar file.
  19722. The iCalendar file will be located in the same directory as the Org-mode
  19723. file, but with extension `.ics'."
  19724. (interactive)
  19725. (org-export-icalendar nil buffer-file-name))
  19726. ;;;###autoload
  19727. (defun org-export-icalendar-all-agenda-files ()
  19728. "Export all files in `org-agenda-files' to iCalendar .ics files.
  19729. Each iCalendar file will be located in the same directory as the Org-mode
  19730. file, but with extension `.ics'."
  19731. (interactive)
  19732. (apply 'org-export-icalendar nil (org-agenda-files t)))
  19733. ;;;###autoload
  19734. (defun org-export-icalendar-combine-agenda-files ()
  19735. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  19736. The file is stored under the name `org-combined-agenda-icalendar-file'."
  19737. (interactive)
  19738. (apply 'org-export-icalendar t (org-agenda-files t)))
  19739. (defun org-export-icalendar (combine &rest files)
  19740. "Create iCalendar files for all elements of FILES.
  19741. If COMBINE is non-nil, combine all calendar entries into a single large
  19742. file and store it under the name `org-combined-agenda-icalendar-file'."
  19743. (save-excursion
  19744. (org-prepare-agenda-buffers files)
  19745. (let* ((dir (org-export-directory
  19746. :ical (list :publishing-directory
  19747. org-export-publishing-directory)))
  19748. file ical-file ical-buffer category started org-agenda-new-buffers)
  19749. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  19750. (when combine
  19751. (setq ical-file
  19752. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  19753. org-combined-agenda-icalendar-file
  19754. (expand-file-name org-combined-agenda-icalendar-file dir))
  19755. ical-buffer (org-get-agenda-file-buffer ical-file))
  19756. (set-buffer ical-buffer) (erase-buffer))
  19757. (while (setq file (pop files))
  19758. (catch 'nextfile
  19759. (org-check-agenda-file file)
  19760. (set-buffer (org-get-agenda-file-buffer file))
  19761. (unless combine
  19762. (setq ical-file (concat (file-name-as-directory dir)
  19763. (file-name-sans-extension
  19764. (file-name-nondirectory buffer-file-name))
  19765. ".ics"))
  19766. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  19767. (with-current-buffer ical-buffer (erase-buffer)))
  19768. (setq category (or org-category
  19769. (file-name-sans-extension
  19770. (file-name-nondirectory buffer-file-name))))
  19771. (if (symbolp category) (setq category (symbol-name category)))
  19772. (let ((standard-output ical-buffer))
  19773. (if combine
  19774. (and (not started) (setq started t)
  19775. (org-start-icalendar-file org-icalendar-combined-name))
  19776. (org-start-icalendar-file category))
  19777. (org-print-icalendar-entries combine)
  19778. (when (or (and combine (not files)) (not combine))
  19779. (org-finish-icalendar-file)
  19780. (set-buffer ical-buffer)
  19781. (save-buffer)
  19782. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  19783. (org-release-buffers org-agenda-new-buffers))))
  19784. (defvar org-after-save-iCalendar-file-hook nil
  19785. "Hook run after an iCalendar file has been saved.
  19786. The iCalendar buffer is still current when this hook is run.
  19787. A good way to use this is to tell a desktop calenndar application to re-read
  19788. the iCalendar file.")
  19789. (defun org-print-icalendar-entries (&optional combine)
  19790. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  19791. When COMBINE is non nil, add the category to each line."
  19792. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  19793. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  19794. (org-category-table (org-get-category-table))
  19795. (dts (org-ical-ts-to-string
  19796. (format-time-string (cdr org-time-stamp-formats) (current-time))
  19797. "DTSTART"))
  19798. hd ts ts2 state status (inc t) pos b sexp rrule
  19799. scheduledp deadlinep tmp pri category
  19800. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  19801. (save-excursion
  19802. (goto-char (point-min))
  19803. (debug)
  19804. (while (re-search-forward re1 nil t)
  19805. (catch :skip
  19806. (org-agenda-skip)
  19807. (setq pos (match-beginning 0)
  19808. ts (match-string 0)
  19809. inc t
  19810. hd (org-get-heading)
  19811. category (org-get-category))
  19812. (if (looking-at re2)
  19813. (progn
  19814. (goto-char (match-end 0))
  19815. (setq ts2 (match-string 1) inc nil))
  19816. (setq tmp (buffer-substring (max (point-min)
  19817. (- pos org-ds-keyword-length))
  19818. pos)
  19819. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  19820. (progn
  19821. (setq inc nil)
  19822. (replace-match "\\1" t nil ts))
  19823. ts)
  19824. deadlinep (string-match org-deadline-regexp tmp)
  19825. scheduledp (string-match org-scheduled-regexp tmp)
  19826. ;; donep (org-entry-is-done-p)
  19827. ))
  19828. (if (or (string-match org-tr-regexp hd)
  19829. (string-match org-ts-regexp hd))
  19830. (setq hd (replace-match "" t t hd)))
  19831. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  19832. (setq rrule
  19833. (concat "\nRRULE:FREQ="
  19834. (cdr (assoc
  19835. (match-string 2 ts)
  19836. '(("d" . "DAILY")("w" . "WEEKLY")
  19837. ("m" . "MONTHLY")("y" . "YEARLY"))))
  19838. ";INTERVAL=" (match-string 1 ts)))
  19839. (setq rrule ""))
  19840. (if (string-match org-bracket-link-regexp hd)
  19841. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  19842. (match-string 1 hd))
  19843. t t hd)))
  19844. (if deadlinep (setq hd (concat "DL: " hd)))
  19845. (if scheduledp (setq hd (concat "S: " hd)))
  19846. (if (string-match "\\`<%%" ts)
  19847. (with-current-buffer sexp-buffer
  19848. (insert (substring ts 1 -1) " " hd "\n"))
  19849. (princ (format "BEGIN:VEVENT
  19850. %s
  19851. %s%s
  19852. SUMMARY:%s
  19853. CATEGORIES:%s
  19854. END:VEVENT\n"
  19855. (org-ical-ts-to-string ts "DTSTART")
  19856. (org-ical-ts-to-string ts2 "DTEND" inc)
  19857. rrule hd category)))))
  19858. (when (and org-icalendar-include-sexps
  19859. (condition-case nil (require 'icalendar) (error nil))
  19860. (fboundp 'icalendar-export-region))
  19861. ;; Get all the literal sexps
  19862. (goto-char (point-min))
  19863. (while (re-search-forward "^&?%%(" nil t)
  19864. (catch :skip
  19865. (org-agenda-skip)
  19866. (setq b (match-beginning 0))
  19867. (goto-char (1- (match-end 0)))
  19868. (forward-sexp 1)
  19869. (end-of-line 1)
  19870. (setq sexp (buffer-substring b (point)))
  19871. (with-current-buffer sexp-buffer
  19872. (insert sexp "\n"))
  19873. (princ (org-diary-to-ical-string sexp-buffer)))))
  19874. (when org-icalendar-include-todo
  19875. (goto-char (point-min))
  19876. (while (re-search-forward org-todo-line-regexp nil t)
  19877. (catch :skip
  19878. (org-agenda-skip)
  19879. (setq state (match-string 2))
  19880. (setq status (if (member state org-done-keywords)
  19881. "COMPLETED" "NEEDS-ACTION"))
  19882. (when (and state
  19883. (or (not (member state org-done-keywords))
  19884. (eq org-icalendar-include-todo 'all))
  19885. (not (member org-archive-tag (org-get-tags-at)))
  19886. )
  19887. (setq hd (match-string 3))
  19888. (if (string-match org-bracket-link-regexp hd)
  19889. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  19890. (match-string 1 hd))
  19891. t t hd)))
  19892. (if (string-match org-priority-regexp hd)
  19893. (setq pri (string-to-char (match-string 2 hd))
  19894. hd (concat (substring hd 0 (match-beginning 1))
  19895. (substring hd (match-end 1))))
  19896. (setq pri org-default-priority))
  19897. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  19898. (- org-lowest-priority org-highest-priority))))))
  19899. (princ (format "BEGIN:VTODO
  19900. %s
  19901. SUMMARY:%s
  19902. CATEGORIES:%s
  19903. SEQUENCE:1
  19904. PRIORITY:%d
  19905. STATUS:%s
  19906. END:VTODO\n"
  19907. dts hd category pri status)))))))))
  19908. (defun org-start-icalendar-file (name)
  19909. "Start an iCalendar file by inserting the header."
  19910. (let ((user user-full-name)
  19911. (name (or name "unknown"))
  19912. (timezone (cadr (current-time-zone))))
  19913. (princ
  19914. (format "BEGIN:VCALENDAR
  19915. VERSION:2.0
  19916. X-WR-CALNAME:%s
  19917. PRODID:-//%s//Emacs with Org-mode//EN
  19918. X-WR-TIMEZONE:%s
  19919. CALSCALE:GREGORIAN\n" name user timezone))))
  19920. (defun org-finish-icalendar-file ()
  19921. "Finish an iCalendar file by inserting the END statement."
  19922. (princ "END:VCALENDAR\n"))
  19923. (defun org-ical-ts-to-string (s keyword &optional inc)
  19924. "Take a time string S and convert it to iCalendar format.
  19925. KEYWORD is added in front, to make a complete line like DTSTART....
  19926. When INC is non-nil, increase the hour by two (if time string contains
  19927. a time), or the day by one (if it does not contain a time)."
  19928. (let ((t1 (org-parse-time-string s 'nodefault))
  19929. t2 fmt have-time time)
  19930. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  19931. (setq t2 t1 have-time t)
  19932. (setq t2 (org-parse-time-string s)))
  19933. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  19934. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  19935. (when inc
  19936. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  19937. (setq time (encode-time s mi h d m y)))
  19938. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  19939. (concat keyword (format-time-string fmt time))))
  19940. ;;; XOXO export
  19941. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  19942. (with-current-buffer buffer
  19943. (apply 'insert output)))
  19944. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  19945. (defun org-export-as-xoxo (&optional buffer)
  19946. "Export the org buffer as XOXO.
  19947. The XOXO buffer is named *xoxo-<source buffer name>*"
  19948. (interactive (list (current-buffer)))
  19949. ;; A quickie abstraction
  19950. ;; Output everything as XOXO
  19951. (with-current-buffer (get-buffer buffer)
  19952. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  19953. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19954. (org-infile-export-plist)))
  19955. (filename (concat (file-name-as-directory
  19956. (org-export-directory :xoxo opt-plist))
  19957. (file-name-sans-extension
  19958. (file-name-nondirectory buffer-file-name))
  19959. ".html"))
  19960. (out (find-file-noselect filename))
  19961. (last-level 1)
  19962. (hanging-li nil))
  19963. ;; Check the output buffer is empty.
  19964. (with-current-buffer out (erase-buffer))
  19965. ;; Kick off the output
  19966. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  19967. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  19968. (let* ((hd (match-string-no-properties 1))
  19969. (level (length hd))
  19970. (text (concat
  19971. (match-string-no-properties 2)
  19972. (save-excursion
  19973. (goto-char (match-end 0))
  19974. (let ((str ""))
  19975. (catch 'loop
  19976. (while 't
  19977. (forward-line)
  19978. (if (looking-at "^[ \t]\\(.*\\)")
  19979. (setq str (concat str (match-string-no-properties 1)))
  19980. (throw 'loop str)))))))))
  19981. ;; Handle level rendering
  19982. (cond
  19983. ((> level last-level)
  19984. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  19985. ((< level last-level)
  19986. (dotimes (- (- last-level level) 1)
  19987. (if hanging-li
  19988. (org-export-as-xoxo-insert-into out "</li>\n"))
  19989. (org-export-as-xoxo-insert-into out "</ol>\n"))
  19990. (when hanging-li
  19991. (org-export-as-xoxo-insert-into out "</li>\n")
  19992. (setq hanging-li nil)))
  19993. ((equal level last-level)
  19994. (if hanging-li
  19995. (org-export-as-xoxo-insert-into out "</li>\n")))
  19996. )
  19997. (setq last-level level)
  19998. ;; And output the new li
  19999. (setq hanging-li 't)
  20000. (if (equal ?+ (elt text 0))
  20001. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  20002. (org-export-as-xoxo-insert-into out "<li>" text))))
  20003. ;; Finally finish off the ol
  20004. (dotimes (- last-level 1)
  20005. (if hanging-li
  20006. (org-export-as-xoxo-insert-into out "</li>\n"))
  20007. (org-export-as-xoxo-insert-into out "</ol>\n"))
  20008. ;; Finish the buffer off and clean it up.
  20009. (switch-to-buffer-other-window out)
  20010. (indent-region (point-min) (point-max) nil)
  20011. (save-buffer)
  20012. (goto-char (point-min))
  20013. )))
  20014. ;;;; Key bindings
  20015. ;; Make `C-c C-x' a prefix key
  20016. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  20017. ;; TAB key with modifiers
  20018. (org-defkey org-mode-map "\C-i" 'org-cycle)
  20019. (org-defkey org-mode-map [(tab)] 'org-cycle)
  20020. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  20021. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  20022. (org-defkey org-mode-map "\M-\t" 'org-complete)
  20023. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  20024. ;; The following line is necessary under Suse GNU/Linux
  20025. (unless (featurep 'xemacs)
  20026. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  20027. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  20028. (define-key org-mode-map (kbd "<backtab>") 'org-shifttab)
  20029. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  20030. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  20031. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  20032. ;; Cursor keys with modifiers
  20033. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  20034. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  20035. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  20036. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  20037. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  20038. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  20039. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  20040. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  20041. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  20042. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  20043. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  20044. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  20045. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  20046. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  20047. ;;; Extra keys for tty access.
  20048. ;; We only set them when really needed because otherwise the
  20049. ;; menus don't show the simple keys
  20050. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  20051. (not window-system))
  20052. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  20053. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  20054. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  20055. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  20056. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  20057. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  20058. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  20059. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  20060. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  20061. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  20062. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  20063. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  20064. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  20065. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  20066. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  20067. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  20068. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  20069. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  20070. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  20071. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  20072. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  20073. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  20074. ;; All the other keys
  20075. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  20076. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  20077. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  20078. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  20079. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  20080. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  20081. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  20082. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  20083. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  20084. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  20085. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  20086. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  20087. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  20088. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  20089. (org-defkey org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  20090. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  20091. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  20092. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  20093. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  20094. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  20095. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  20096. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  20097. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  20098. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  20099. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  20100. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  20101. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  20102. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  20103. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  20104. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  20105. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  20106. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  20107. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  20108. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  20109. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  20110. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  20111. (org-defkey org-mode-map "\C-c^" 'org-sort)
  20112. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  20113. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  20114. (org-defkey org-mode-map "\C-m" 'org-return)
  20115. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  20116. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  20117. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  20118. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  20119. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  20120. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  20121. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  20122. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  20123. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  20124. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  20125. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  20126. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  20127. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  20128. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  20129. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  20130. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  20131. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  20132. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  20133. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  20134. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  20135. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  20136. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  20137. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  20138. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  20139. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  20140. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  20141. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  20142. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  20143. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  20144. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  20145. (when (featurep 'xemacs)
  20146. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  20147. (defsubst org-table-p () (org-at-table-p))
  20148. (defun org-self-insert-command (N)
  20149. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  20150. If the cursor is in a table looking at whitespace, the whitespace is
  20151. overwritten, and the table is not marked as requiring realignment."
  20152. (interactive "p")
  20153. (if (and (org-table-p)
  20154. (progn
  20155. ;; check if we blank the field, and if that triggers align
  20156. (and org-table-auto-blank-field
  20157. (member last-command
  20158. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  20159. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  20160. ;; got extra space, this field does not determine column width
  20161. (let (org-table-may-need-update) (org-table-blank-field))
  20162. ;; no extra space, this field may determine column width
  20163. (org-table-blank-field)))
  20164. t)
  20165. (eq N 1)
  20166. (looking-at "[^|\n]* |"))
  20167. (let (org-table-may-need-update)
  20168. (goto-char (1- (match-end 0)))
  20169. (delete-backward-char 1)
  20170. (goto-char (match-beginning 0))
  20171. (self-insert-command N))
  20172. (setq org-table-may-need-update t)
  20173. (self-insert-command N)))
  20174. (defun org-delete-backward-char (N)
  20175. "Like `delete-backward-char', insert whitespace at field end in tables.
  20176. When deleting backwards, in tables this function will insert whitespace in
  20177. front of the next \"|\" separator, to keep the table aligned. The table will
  20178. still be marked for re-alignment if the field did fill the entire column,
  20179. because, in this case the deletion might narrow the column."
  20180. (interactive "p")
  20181. (if (and (org-table-p)
  20182. (eq N 1)
  20183. (string-match "|" (buffer-substring (point-at-bol) (point)))
  20184. (looking-at ".*?|"))
  20185. (let ((pos (point))
  20186. (noalign (looking-at "[^|\n\r]* |"))
  20187. (c org-table-may-need-update))
  20188. (backward-delete-char N)
  20189. (skip-chars-forward "^|")
  20190. (insert " ")
  20191. (goto-char (1- pos))
  20192. ;; noalign: if there were two spaces at the end, this field
  20193. ;; does not determine the width of the column.
  20194. (if noalign (setq org-table-may-need-update c)))
  20195. (backward-delete-char N)))
  20196. (defun org-delete-char (N)
  20197. "Like `delete-char', but insert whitespace at field end in tables.
  20198. When deleting characters, in tables this function will insert whitespace in
  20199. front of the next \"|\" separator, to keep the table aligned. The table will
  20200. still be marked for re-alignment if the field did fill the entire column,
  20201. because, in this case the deletion might narrow the column."
  20202. (interactive "p")
  20203. (if (and (org-table-p)
  20204. (not (bolp))
  20205. (not (= (char-after) ?|))
  20206. (eq N 1))
  20207. (if (looking-at ".*?|")
  20208. (let ((pos (point))
  20209. (noalign (looking-at "[^|\n\r]* |"))
  20210. (c org-table-may-need-update))
  20211. (replace-match (concat
  20212. (substring (match-string 0) 1 -1)
  20213. " |"))
  20214. (goto-char pos)
  20215. ;; noalign: if there were two spaces at the end, this field
  20216. ;; does not determine the width of the column.
  20217. (if noalign (setq org-table-may-need-update c)))
  20218. (delete-char N))
  20219. (delete-char N)))
  20220. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  20221. (put 'org-self-insert-command 'delete-selection t)
  20222. (put 'orgtbl-self-insert-command 'delete-selection t)
  20223. (put 'org-delete-char 'delete-selection 'supersede)
  20224. (put 'org-delete-backward-char 'delete-selection 'supersede)
  20225. ;; Make `flyspell-mode' delay after some commands
  20226. (put 'org-self-insert-command 'flyspell-delayed t)
  20227. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  20228. (put 'org-delete-char 'flyspell-delayed t)
  20229. (put 'org-delete-backward-char 'flyspell-delayed t)
  20230. ;; How to do this: Measure non-white length of current string
  20231. ;; If equal to column width, we should realign.
  20232. (defun org-remap (map &rest commands)
  20233. "In MAP, remap the functions given in COMMANDS.
  20234. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  20235. (let (new old)
  20236. (while commands
  20237. (setq old (pop commands) new (pop commands))
  20238. (if (fboundp 'command-remapping)
  20239. (org-defkey map (vector 'remap old) new)
  20240. (substitute-key-definition old new map global-map)))))
  20241. (when (eq org-enable-table-editor 'optimized)
  20242. ;; If the user wants maximum table support, we need to hijack
  20243. ;; some standard editing functions
  20244. (org-remap org-mode-map
  20245. 'self-insert-command 'org-self-insert-command
  20246. 'delete-char 'org-delete-char
  20247. 'delete-backward-char 'org-delete-backward-char)
  20248. (org-defkey org-mode-map "|" 'org-force-self-insert))
  20249. (defun org-shiftcursor-error ()
  20250. "Throw an error because Shift-Cursor command was applied in wrong context."
  20251. (error "This command is active in special context like tables, headlines or timestamps"))
  20252. (defun org-shifttab (&optional arg)
  20253. "Global visibility cycling or move to previous table field.
  20254. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  20255. on context.
  20256. See the individual commands for more information."
  20257. (interactive "P")
  20258. (cond
  20259. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  20260. (arg (message "Content view to level: ")
  20261. (org-content (prefix-numeric-value arg))
  20262. (setq org-cycle-global-status 'overview))
  20263. (t (call-interactively 'org-global-cycle))))
  20264. (defun org-shiftmetaleft ()
  20265. "Promote subtree or delete table column.
  20266. Calls `org-promote-subtree', `org-outdent-item',
  20267. or `org-table-delete-column', depending on context.
  20268. See the individual commands for more information."
  20269. (interactive)
  20270. (cond
  20271. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  20272. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  20273. ((org-at-item-p) (call-interactively 'org-outdent-item))
  20274. (t (org-shiftcursor-error))))
  20275. (defun org-shiftmetaright ()
  20276. "Demote subtree or insert table column.
  20277. Calls `org-demote-subtree', `org-indent-item',
  20278. or `org-table-insert-column', depending on context.
  20279. See the individual commands for more information."
  20280. (interactive)
  20281. (cond
  20282. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  20283. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  20284. ((org-at-item-p) (call-interactively 'org-indent-item))
  20285. (t (org-shiftcursor-error))))
  20286. (defun org-shiftmetaup (&optional arg)
  20287. "Move subtree up or kill table row.
  20288. Calls `org-move-subtree-up' or `org-table-kill-row' or
  20289. `org-move-item-up' depending on context. See the individual commands
  20290. for more information."
  20291. (interactive "P")
  20292. (cond
  20293. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  20294. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  20295. ((org-at-item-p) (call-interactively 'org-move-item-up))
  20296. (t (org-shiftcursor-error))))
  20297. (defun org-shiftmetadown (&optional arg)
  20298. "Move subtree down or insert table row.
  20299. Calls `org-move-subtree-down' or `org-table-insert-row' or
  20300. `org-move-item-down', depending on context. See the individual
  20301. commands for more information."
  20302. (interactive "P")
  20303. (cond
  20304. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  20305. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  20306. ((org-at-item-p) (call-interactively 'org-move-item-down))
  20307. (t (org-shiftcursor-error))))
  20308. (defun org-metaleft (&optional arg)
  20309. "Promote heading or move table column to left.
  20310. Calls `org-do-promote' or `org-table-move-column', depending on context.
  20311. With no specific context, calls the Emacs default `backward-word'.
  20312. See the individual commands for more information."
  20313. (interactive "P")
  20314. (cond
  20315. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  20316. ((or (org-on-heading-p) (org-region-active-p))
  20317. (call-interactively 'org-do-promote))
  20318. ((org-at-item-p) (call-interactively 'org-outdent-item))
  20319. (t (call-interactively 'backward-word))))
  20320. (defun org-metaright (&optional arg)
  20321. "Demote subtree or move table column to right.
  20322. Calls `org-do-demote' or `org-table-move-column', depending on context.
  20323. With no specific context, calls the Emacs default `forward-word'.
  20324. See the individual commands for more information."
  20325. (interactive "P")
  20326. (cond
  20327. ((org-at-table-p) (call-interactively 'org-table-move-column))
  20328. ((or (org-on-heading-p) (org-region-active-p))
  20329. (call-interactively 'org-do-demote))
  20330. ((org-at-item-p) (call-interactively 'org-indent-item))
  20331. (t (call-interactively 'forward-word))))
  20332. (defun org-metaup (&optional arg)
  20333. "Move subtree up or move table row up.
  20334. Calls `org-move-subtree-up' or `org-table-move-row' or
  20335. `org-move-item-up', depending on context. See the individual commands
  20336. for more information."
  20337. (interactive "P")
  20338. (cond
  20339. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  20340. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  20341. ((org-at-item-p) (call-interactively 'org-move-item-up))
  20342. (t (org-shiftcursor-error))))
  20343. (defun org-metadown (&optional arg)
  20344. "Move subtree down or move table row down.
  20345. Calls `org-move-subtree-down' or `org-table-move-row' or
  20346. `org-move-item-down', depending on context. See the individual
  20347. commands for more information."
  20348. (interactive "P")
  20349. (cond
  20350. ((org-at-table-p) (call-interactively 'org-table-move-row))
  20351. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  20352. ((org-at-item-p) (call-interactively 'org-move-item-down))
  20353. (t (org-shiftcursor-error))))
  20354. (defun org-shiftup (&optional arg)
  20355. "Increase item in timestamp or increase priority of current headline.
  20356. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  20357. depending on context. See the individual commands for more information."
  20358. (interactive "P")
  20359. (cond
  20360. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  20361. ((org-on-heading-p) (call-interactively 'org-priority-up))
  20362. ((org-at-item-p) (call-interactively 'org-previous-item))
  20363. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  20364. (defun org-shiftdown (&optional arg)
  20365. "Decrease item in timestamp or decrease priority of current headline.
  20366. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  20367. depending on context. See the individual commands for more information."
  20368. (interactive "P")
  20369. (cond
  20370. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  20371. ((org-on-heading-p) (call-interactively 'org-priority-down))
  20372. (t (call-interactively 'org-next-item))))
  20373. (defun org-shiftright ()
  20374. "Next TODO keyword or timestamp one day later, depending on context."
  20375. (interactive)
  20376. (cond
  20377. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  20378. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  20379. (t (org-shiftcursor-error))))
  20380. (defun org-shiftleft ()
  20381. "Previous TODO keyword or timestamp one day earlier, depending on context."
  20382. (interactive)
  20383. (cond
  20384. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  20385. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  20386. (t (org-shiftcursor-error))))
  20387. (defun org-shiftcontrolright ()
  20388. "Switch to next TODO set."
  20389. (interactive)
  20390. (cond
  20391. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  20392. (t (org-shiftcursor-error))))
  20393. (defun org-shiftcontrolleft ()
  20394. "Switch to previous TODO set."
  20395. (interactive)
  20396. (cond
  20397. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  20398. (t (org-shiftcursor-error))))
  20399. (defun org-ctrl-c-ret ()
  20400. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  20401. (interactive)
  20402. (cond
  20403. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  20404. (t (call-interactively 'org-insert-heading))))
  20405. (defun org-copy-special ()
  20406. "Copy region in table or copy current subtree.
  20407. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  20408. See the individual commands for more information."
  20409. (interactive)
  20410. (call-interactively
  20411. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  20412. (defun org-cut-special ()
  20413. "Cut region in table or cut current subtree.
  20414. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  20415. See the individual commands for more information."
  20416. (interactive)
  20417. (call-interactively
  20418. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  20419. (defun org-paste-special (arg)
  20420. "Paste rectangular region into table, or past subtree relative to level.
  20421. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  20422. See the individual commands for more information."
  20423. (interactive "P")
  20424. (if (org-at-table-p)
  20425. (org-table-paste-rectangle)
  20426. (org-paste-subtree arg)))
  20427. (defun org-ctrl-c-ctrl-c (&optional arg)
  20428. "Set tags in headline, or update according to changed information at point.
  20429. This command does many different things, depending on context:
  20430. - If the cursor is in a headline, prompt for tags and insert them
  20431. into the current line, aligned to `org-tags-column'. When called
  20432. with prefix arg, realign all tags in the current buffer.
  20433. - If the cursor is in one of the special #+KEYWORD lines, this
  20434. triggers scanning the buffer for these lines and updating the
  20435. information.
  20436. - If the cursor is inside a table, realign the table. This command
  20437. works even if the automatic table editor has been turned off.
  20438. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  20439. the entire table.
  20440. - If the cursor is inside a table created by the table.el package,
  20441. activate that table.
  20442. - If the current buffer is a remember buffer, close note and file it.
  20443. with a prefix argument, file it without further interaction to the default
  20444. location.
  20445. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  20446. links in this buffer.
  20447. - If the cursor is on a numbered item in a plain list, renumber the
  20448. ordered list."
  20449. (interactive "P")
  20450. (let ((org-enable-table-editor t))
  20451. (cond
  20452. ((or org-clock-overlays
  20453. org-occur-highlights
  20454. org-latex-fragment-image-overlays)
  20455. (org-remove-clock-overlays)
  20456. (org-remove-occur-highlights)
  20457. (org-remove-latex-fragment-image-overlays)
  20458. (message "Temporary highlights/overlays removed from current buffer"))
  20459. ((and (local-variable-p 'org-finish-function (current-buffer))
  20460. (fboundp org-finish-function))
  20461. (funcall org-finish-function))
  20462. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  20463. ((org-on-heading-p) (call-interactively 'org-set-tags))
  20464. ((org-at-table.el-p)
  20465. (require 'table)
  20466. (beginning-of-line 1)
  20467. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  20468. (call-interactively 'table-recognize-table))
  20469. ((org-at-table-p)
  20470. (org-table-maybe-eval-formula)
  20471. (if arg
  20472. (call-interactively 'org-table-recalculate)
  20473. (org-table-maybe-recalculate-line))
  20474. (call-interactively 'org-table-align))
  20475. ((org-at-item-checkbox-p)
  20476. (call-interactively 'org-toggle-checkbox))
  20477. ((org-at-item-p)
  20478. (call-interactively 'org-maybe-renumber-ordered-list))
  20479. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  20480. (cond
  20481. ((equal (match-string 1) "TBLFM")
  20482. ;; Recalculate the table before this line
  20483. (save-excursion
  20484. (beginning-of-line 1)
  20485. (skip-chars-backward " \r\n\t")
  20486. (if (org-at-table-p)
  20487. (org-call-with-arg 'org-table-recalculate t))))
  20488. (t
  20489. (call-interactively 'org-mode-restart))))
  20490. (t (error "C-c C-c can do nothing useful at this location.")))))
  20491. (defun org-mode-restart ()
  20492. "Restart Org-mode, to scan again for special lines.
  20493. Also updates the keyword regular expressions."
  20494. (interactive)
  20495. (let ((org-inhibit-startup t)) (org-mode))
  20496. (message "Org-mode restarted to refresh keyword and special line setup"))
  20497. (defun org-return ()
  20498. "Goto next table row or insert a newline.
  20499. Calls `org-table-next-row' or `newline', depending on context.
  20500. See the individual commands for more information."
  20501. (interactive)
  20502. (cond
  20503. ((bobp) (newline))
  20504. ((org-at-table-p)
  20505. (org-table-justify-field-maybe)
  20506. (call-interactively 'org-table-next-row))
  20507. (t (newline))))
  20508. (defun org-ctrl-c-minus ()
  20509. "Insert separator line in table or modify bullet type in list.
  20510. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  20511. depending on context."
  20512. (interactive)
  20513. (cond
  20514. ((org-at-table-p)
  20515. (call-interactively 'org-table-insert-hline))
  20516. ((org-in-item-p)
  20517. (call-interactively 'org-cycle-list-bullet))
  20518. (t (error "`C-c -' does have no function here."))))
  20519. (defun org-meta-return (&optional arg)
  20520. "Insert a new heading or wrap a region in a table.
  20521. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  20522. See the individual commands for more information."
  20523. (interactive "P")
  20524. (cond
  20525. ((org-at-table-p)
  20526. (call-interactively 'org-table-wrap-region))
  20527. (t (call-interactively 'org-insert-heading))))
  20528. ;;; Menu entries
  20529. ;; Define the Org-mode menus
  20530. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  20531. '("Tbl"
  20532. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  20533. ["Next Field" org-cycle (org-at-table-p)]
  20534. ["Previous Field" org-shifttab (org-at-table-p)]
  20535. ["Next Row" org-return (org-at-table-p)]
  20536. "--"
  20537. ["Blank Field" org-table-blank-field (org-at-table-p)]
  20538. ["Edit Field" org-table-edit-field (org-at-table-p)]
  20539. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  20540. "--"
  20541. ("Column"
  20542. ["Move Column Left" org-metaleft (org-at-table-p)]
  20543. ["Move Column Right" org-metaright (org-at-table-p)]
  20544. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  20545. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  20546. ("Row"
  20547. ["Move Row Up" org-metaup (org-at-table-p)]
  20548. ["Move Row Down" org-metadown (org-at-table-p)]
  20549. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  20550. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  20551. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  20552. "--"
  20553. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  20554. ("Rectangle"
  20555. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  20556. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  20557. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  20558. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  20559. "--"
  20560. ("Calculate"
  20561. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  20562. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  20563. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  20564. "--"
  20565. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  20566. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  20567. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  20568. "--"
  20569. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  20570. "--"
  20571. ["Sum Column/Rectangle" org-table-sum
  20572. (or (org-at-table-p) (org-region-active-p))]
  20573. ["Which Column?" org-table-current-column (org-at-table-p)])
  20574. ["Debug Formulas"
  20575. org-table-toggle-formula-debugger
  20576. :style toggle :selected org-table-formula-debug]
  20577. ["Show Col/Row Numbers"
  20578. org-table-toggle-coordinate-overlays
  20579. :style toggle :selected org-table-overlay-coordinates]
  20580. "--"
  20581. ["Create" org-table-create (and (not (org-at-table-p))
  20582. org-enable-table-editor)]
  20583. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  20584. ["Import from File" org-table-import (not (org-at-table-p))]
  20585. ["Export to File" org-table-export (org-at-table-p)]
  20586. "--"
  20587. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  20588. (easy-menu-define org-org-menu org-mode-map "Org menu"
  20589. '("Org"
  20590. ("Show/Hide"
  20591. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  20592. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  20593. ["Sparse Tree" org-occur t]
  20594. ["Reveal Context" org-reveal t]
  20595. ["Show All" show-all t]
  20596. "--"
  20597. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  20598. "--"
  20599. ["New Heading" org-insert-heading t]
  20600. ("Navigate Headings"
  20601. ["Up" outline-up-heading t]
  20602. ["Next" outline-next-visible-heading t]
  20603. ["Previous" outline-previous-visible-heading t]
  20604. ["Next Same Level" outline-forward-same-level t]
  20605. ["Previous Same Level" outline-backward-same-level t]
  20606. "--"
  20607. ["Jump" org-goto t]
  20608. "--"
  20609. ["C-a finds headline start"
  20610. (setq org-special-ctrl-a (not org-special-ctrl-a))
  20611. :style toggle :selected org-special-ctrl-a])
  20612. ("Edit Structure"
  20613. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  20614. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  20615. "--"
  20616. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  20617. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  20618. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  20619. "--"
  20620. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  20621. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  20622. ["Demote Heading" org-metaright (not (org-at-table-p))]
  20623. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  20624. "--"
  20625. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  20626. "--"
  20627. ["Convert to odd levels" org-convert-to-odd-levels t]
  20628. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  20629. ("Editing"
  20630. ["Emphasis..." org-emphasize t])
  20631. ("Archive"
  20632. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  20633. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  20634. ; :active t :keys "C-u C-c C-x C-a"]
  20635. ["Sparse trees open ARCHIVE trees"
  20636. (setq org-sparse-tree-open-archived-trees
  20637. (not org-sparse-tree-open-archived-trees))
  20638. :style toggle :selected org-sparse-tree-open-archived-trees]
  20639. ["Cycling opens ARCHIVE trees"
  20640. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  20641. :style toggle :selected org-cycle-open-archived-trees]
  20642. ["Agenda includes ARCHIVE trees"
  20643. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  20644. :style toggle :selected (not org-agenda-skip-archived-trees)]
  20645. "--"
  20646. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  20647. ; ["Check and Move Children" (org-archive-subtree '(4))
  20648. ; :active t :keys "C-u C-c C-x C-s"]
  20649. )
  20650. "--"
  20651. ("TODO Lists"
  20652. ["TODO/DONE/-" org-todo t]
  20653. ("Select keyword"
  20654. ["Next keyword" org-shiftright (org-on-heading-p)]
  20655. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  20656. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  20657. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  20658. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  20659. ["Show TODO Tree" org-show-todo-tree t]
  20660. ["Global TODO list" org-todo-list t]
  20661. "--"
  20662. ["Set Priority" org-priority t]
  20663. ["Priority Up" org-shiftup t]
  20664. ["Priority Down" org-shiftdown t]
  20665. "--"
  20666. ;; FIXME: why is this still here????
  20667. ; ["Insert Checkbox" org-insert-todo-heading (org-in-item-p)]
  20668. ; ["Toggle Checkbox" org-ctrl-c-ctrl-c (org-at-item-checkbox-p)]
  20669. ; ["Insert [n/m] cookie" (progn (insert "[/]") (org-update-checkbox-count))
  20670. ; (or (org-on-heading-p) (org-at-item-p))]
  20671. ; ["Insert [%] cookie" (progn (insert "[%]") (org-update-checkbox-count))
  20672. ; (or (org-on-heading-p) (org-at-item-p))]
  20673. ; ["Update Statistics" org-update-checkbox-count t]
  20674. )
  20675. ("TAGS and Properties"
  20676. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  20677. ["Column view of properties" org-columns t])
  20678. ("Dates and Scheduling"
  20679. ["Timestamp" org-time-stamp t]
  20680. ["Timestamp (inactive)" org-time-stamp-inactive t]
  20681. ("Change Date"
  20682. ["1 Day Later" org-shiftright t]
  20683. ["1 Day Earlier" org-shiftleft t]
  20684. ["1 ... Later" org-shiftup t]
  20685. ["1 ... Earlier" org-shiftdown t])
  20686. ["Compute Time Range" org-evaluate-time-range t]
  20687. ["Schedule Item" org-schedule t]
  20688. ["Deadline" org-deadline t]
  20689. "--"
  20690. ["Custom time format" org-toggle-time-stamp-overlays
  20691. :style radio :selected org-display-custom-times]
  20692. "--"
  20693. ["Goto Calendar" org-goto-calendar t]
  20694. ["Date from Calendar" org-date-from-calendar t])
  20695. ("Logging work"
  20696. ["Clock in" org-clock-in t]
  20697. ["Clock out" org-clock-out t]
  20698. ["Clock cancel" org-clock-cancel t]
  20699. ["Display times" org-clock-display t]
  20700. ["Create clock table" org-clock-report t]
  20701. "--"
  20702. ["Record DONE time"
  20703. (progn (setq org-log-done (not org-log-done))
  20704. (message "Switching to %s will %s record a timestamp"
  20705. (car org-done-keywords)
  20706. (if org-log-done "automatically" "not")))
  20707. :style toggle :selected org-log-done])
  20708. "--"
  20709. ["Agenda Command..." org-agenda t]
  20710. ("File List for Agenda")
  20711. ("Special views current file"
  20712. ["TODO Tree" org-show-todo-tree t]
  20713. ["Check Deadlines" org-check-deadlines t]
  20714. ["Timeline" org-timeline t]
  20715. ["Tags Tree" org-tags-sparse-tree t])
  20716. "--"
  20717. ("Hyperlinks"
  20718. ["Store Link (Global)" org-store-link t]
  20719. ["Insert Link" org-insert-link t]
  20720. ["Follow Link" org-open-at-point t]
  20721. "--"
  20722. ["Next link" org-next-link t]
  20723. ["Previous link" org-previous-link t]
  20724. "--"
  20725. ["Descriptive Links"
  20726. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  20727. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  20728. ["Literal Links"
  20729. (progn
  20730. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  20731. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  20732. "--"
  20733. ["Export/Publish..." org-export t]
  20734. ("LaTeX"
  20735. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20736. :selected org-cdlatex-mode]
  20737. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20738. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20739. ["Modify math symbol" org-cdlatex-math-modify
  20740. (org-inside-LaTeX-fragment-p)]
  20741. ["Export LaTeX fragments as images"
  20742. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  20743. :style toggle :selected org-export-with-LaTeX-fragments])
  20744. "--"
  20745. ("Documentation"
  20746. ["Show Version" org-version t]
  20747. ["Info Documentation" org-info t])
  20748. ("Customize"
  20749. ["Browse Org Group" org-customize t]
  20750. "--"
  20751. ["Expand This Menu" org-create-customize-menu
  20752. (fboundp 'customize-menu-create)])
  20753. "--"
  20754. ["Refresh setup" org-mode-restart t]
  20755. ))
  20756. (defun org-info (&optional node)
  20757. "Read documentation for Org-mode in the info system.
  20758. With optional NODE, go directly to that node."
  20759. (interactive)
  20760. (require 'info)
  20761. (Info-goto-node (format "(org)%s" (or node ""))))
  20762. (defun org-install-agenda-files-menu ()
  20763. (let ((bl (buffer-list)))
  20764. (save-excursion
  20765. (while bl
  20766. (set-buffer (pop bl))
  20767. (if (org-mode-p) (setq bl nil)))
  20768. (when (org-mode-p)
  20769. (easy-menu-change
  20770. '("Org") "File List for Agenda"
  20771. (append
  20772. (list
  20773. ["Edit File List" (org-edit-agenda-file-list) t]
  20774. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20775. ["Remove Current File from List" org-remove-file t]
  20776. ["Cycle through agenda files" org-cycle-agenda-files t]
  20777. "--")
  20778. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20779. ;;;; Documentation
  20780. (defun org-customize ()
  20781. "Call the customize function with org as argument."
  20782. (interactive)
  20783. (customize-browse 'org))
  20784. (defun org-create-customize-menu ()
  20785. "Create a full customization menu for Org-mode, insert it into the menu."
  20786. (interactive)
  20787. (if (fboundp 'customize-menu-create)
  20788. (progn
  20789. (easy-menu-change
  20790. '("Org") "Customize"
  20791. `(["Browse Org group" org-customize t]
  20792. "--"
  20793. ,(customize-menu-create 'org)
  20794. ["Set" Custom-set t]
  20795. ["Save" Custom-save t]
  20796. ["Reset to Current" Custom-reset-current t]
  20797. ["Reset to Saved" Custom-reset-saved t]
  20798. ["Reset to Standard Settings" Custom-reset-standard t]))
  20799. (message "\"Org\"-menu now contains full customization menu"))
  20800. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20801. ;;;; Miscellaneous stuff
  20802. ;;; Generally useful functions
  20803. (defun org-context ()
  20804. "Return a list of contexts of the current cursor position.
  20805. If several contexts apply, all are returned.
  20806. Each context entry is a list with a symbol naming the context, and
  20807. two positions indicating start and end of the context. Possible
  20808. contexts are:
  20809. :headline anywhere in a headline
  20810. :headline-stars on the leading stars in a headline
  20811. :todo-keyword on a TODO keyword (including DONE) in a headline
  20812. :tags on the TAGS in a headline
  20813. :priority on the priority cookie in a headline
  20814. :item on the first line of a plain list item
  20815. :item-bullet on the bullet/number of a plain list item
  20816. :checkbox on the checkbox in a plain list item
  20817. :table in an org-mode table
  20818. :table-special on a special filed in a table
  20819. :table-table in a table.el table
  20820. :link on a hyperlink
  20821. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  20822. :target on a <<target>>
  20823. :radio-target on a <<<radio-target>>>
  20824. :latex-fragment on a LaTeX fragment
  20825. :latex-preview on a LaTeX fragment with overlayed preview image
  20826. This function expects the position to be visible because it uses font-lock
  20827. faces as a help to recognize the following contexts: :table-special, :link,
  20828. and :keyword."
  20829. (let* ((f (get-text-property (point) 'face))
  20830. (faces (if (listp f) f (list f)))
  20831. (p (point)) clist o)
  20832. ;; First the large context
  20833. (cond
  20834. ((org-on-heading-p t)
  20835. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20836. (when (progn
  20837. (beginning-of-line 1)
  20838. (looking-at org-todo-line-tags-regexp))
  20839. (push (org-point-in-group p 1 :headline-stars) clist)
  20840. (push (org-point-in-group p 2 :todo-keyword) clist)
  20841. (push (org-point-in-group p 4 :tags) clist))
  20842. (goto-char p)
  20843. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  20844. (if (looking-at "\\[#[A-Z0-9]\\]")
  20845. (push (org-point-in-group p 0 :priority) clist)))
  20846. ((org-at-item-p)
  20847. (push (org-point-in-group p 2 :item-bullet) clist)
  20848. (push (list :item (point-at-bol)
  20849. (save-excursion (org-end-of-item) (point)))
  20850. clist)
  20851. (and (org-at-item-checkbox-p)
  20852. (push (org-point-in-group p 0 :checkbox) clist)))
  20853. ((org-at-table-p)
  20854. (push (list :table (org-table-begin) (org-table-end)) clist)
  20855. (if (memq 'org-formula faces)
  20856. (push (list :table-special
  20857. (previous-single-property-change p 'face)
  20858. (next-single-property-change p 'face)) clist)))
  20859. ((org-at-table-p 'any)
  20860. (push (list :table-table) clist)))
  20861. (goto-char p)
  20862. ;; Now the small context
  20863. (cond
  20864. ((org-at-timestamp-p)
  20865. (push (org-point-in-group p 0 :timestamp) clist))
  20866. ((memq 'org-link faces)
  20867. (push (list :link
  20868. (previous-single-property-change p 'face)
  20869. (next-single-property-change p 'face)) clist))
  20870. ((memq 'org-special-keyword faces)
  20871. (push (list :keyword
  20872. (previous-single-property-change p 'face)
  20873. (next-single-property-change p 'face)) clist))
  20874. ((org-on-target-p)
  20875. (push (org-point-in-group p 0 :target) clist)
  20876. (goto-char (1- (match-beginning 0)))
  20877. (if (looking-at org-radio-target-regexp)
  20878. (push (org-point-in-group p 0 :radio-target) clist))
  20879. (goto-char p))
  20880. ((setq o (car (delq nil
  20881. (mapcar
  20882. (lambda (x)
  20883. (if (memq x org-latex-fragment-image-overlays) x))
  20884. (org-overlays-at (point))))))
  20885. (push (list :latex-fragment
  20886. (org-overlay-start o) (org-overlay-end o)) clist)
  20887. (push (list :latex-preview
  20888. (org-overlay-start o) (org-overlay-end o)) clist))
  20889. ((org-inside-LaTeX-fragment-p)
  20890. ;; FIXME: positions wrong.
  20891. (push (list :latex-fragment (point) (point)) clist)))
  20892. (setq clist (nreverse (delq nil clist)))
  20893. clist))
  20894. ;; FIXME Compare with at-regexp-p
  20895. (defun org-in-regexp (re &optional nlines visually)
  20896. "Check if point is inside a match of regexp.
  20897. Normally only the current line is checked, but you can include NLINES extra
  20898. lines both before and after point into the search.
  20899. If VISUALLY is set, require that the cursor is not after the match but
  20900. really on, so that the block visually is on the match."
  20901. (catch 'exit
  20902. (let ((pos (point))
  20903. (eol (point-at-eol (+ 1 (or nlines 0))))
  20904. (inc (if visually 1 0)))
  20905. (save-excursion
  20906. (beginning-of-line (- 1 (or nlines 0)))
  20907. (while (re-search-forward re eol t)
  20908. (if (and (<= (match-beginning 0) pos)
  20909. (>= (+ inc (match-end 0)) pos))
  20910. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20911. (defun org-at-regexp-p (regexp)
  20912. "Is point inside a match of REGEXP in the current line?"
  20913. (catch 'exit
  20914. (save-excursion
  20915. (let ((pos (point)) (end (point-at-eol)))
  20916. (beginning-of-line 1)
  20917. (while (re-search-forward regexp end t)
  20918. (if (and (<= (match-beginning 0) pos)
  20919. (>= (match-end 0) pos))
  20920. (throw 'exit t)))
  20921. nil))))
  20922. (defun org-uniquify (list)
  20923. "Remove duplicate elements from LIST."
  20924. (let (res)
  20925. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  20926. res))
  20927. (defun org-delete-all (elts list)
  20928. "Remove all elements in ELTS from LIST."
  20929. (while elts
  20930. (setq list (delete (pop elts) list)))
  20931. list)
  20932. (defun org-point-in-group (point group &optional context)
  20933. "Check if POINT is in match-group GROUP.
  20934. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20935. match. If the match group does ot exist or point is not inside it,
  20936. return nil."
  20937. (and (match-beginning group)
  20938. (>= point (match-beginning group))
  20939. (<= point (match-end group))
  20940. (if context
  20941. (list context (match-beginning group) (match-end group))
  20942. t)))
  20943. (defun org-combine-plists (&rest plists)
  20944. "Create a single property list from all plists in PLISTS.
  20945. The process starts by copying the first list, and then setting properties
  20946. from the other lists. Settings in the last list are the most significant
  20947. ones and overrule settings in the other lists."
  20948. (let ((rtn (copy-sequence (pop plists)))
  20949. p v ls)
  20950. (while plists
  20951. (setq ls (pop plists))
  20952. (while ls
  20953. (setq p (pop ls) v (pop ls))
  20954. (setq rtn (plist-put rtn p v))))
  20955. rtn))
  20956. (defun org-move-line-down (arg)
  20957. "Move the current line down. With prefix argument, move it past ARG lines."
  20958. (interactive "p")
  20959. (let ((col (current-column))
  20960. beg end pos)
  20961. (beginning-of-line 1) (setq beg (point))
  20962. (beginning-of-line 2) (setq end (point))
  20963. (beginning-of-line (+ 1 arg))
  20964. (setq pos (move-marker (make-marker) (point)))
  20965. (insert (delete-and-extract-region beg end))
  20966. (goto-char pos)
  20967. (move-to-column col)))
  20968. (defun org-move-line-up (arg)
  20969. "Move the current line up. With prefix argument, move it past ARG lines."
  20970. (interactive "p")
  20971. (let ((col (current-column))
  20972. beg end pos)
  20973. (beginning-of-line 1) (setq beg (point))
  20974. (beginning-of-line 2) (setq end (point))
  20975. (beginning-of-line (- arg))
  20976. (setq pos (move-marker (make-marker) (point)))
  20977. (insert (delete-and-extract-region beg end))
  20978. (goto-char pos)
  20979. (move-to-column col)))
  20980. (defun org-replace-escapes (string table)
  20981. "Replace %-escapes in STRING with values in TABLE.
  20982. TABLE is an association list with keys line \"%a\" and string values.
  20983. The sequences in STRING may contain normal field width and padding information,
  20984. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20985. so values can contain further %-escapes if they are define later in TABLE."
  20986. (let ((case-fold-search nil)
  20987. e re rpl)
  20988. (while (setq e (pop table))
  20989. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20990. (while (string-match re string)
  20991. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20992. (cdr e)))
  20993. (setq string (replace-match rpl t t string))))
  20994. string))
  20995. (defun org-sublist (list start end)
  20996. "Return a section of LIST, from START to END.
  20997. Counting starts at 1."
  20998. (let (rtn (c start))
  20999. (setq list (nthcdr (1- start) list))
  21000. (while (and list (<= c end))
  21001. (push (pop list) rtn)
  21002. (setq c (1+ c)))
  21003. (nreverse rtn)))
  21004. (defun org-find-base-buffer-visiting (file)
  21005. "Like `find-buffer-visiting' but alway return the base buffer and
  21006. not an indirect buffer"
  21007. (let ((buf (find-buffer-visiting file)))
  21008. (or (buffer-base-buffer buf) buf)))
  21009. (defun org-image-file-name-regexp ()
  21010. "Return regexp matching the file names of images."
  21011. (if (fboundp 'image-file-name-regexp)
  21012. (image-file-name-regexp)
  21013. (let ((image-file-name-extensions
  21014. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  21015. "xbm" "xpm" "pbm" "pgm" "ppm")))
  21016. (concat "\\."
  21017. (regexp-opt (nconc (mapcar 'upcase
  21018. image-file-name-extensions)
  21019. image-file-name-extensions)
  21020. t)
  21021. "\\'"))))
  21022. (defun org-file-image-p (file)
  21023. "Return non-nil if FILE is an image."
  21024. (save-match-data
  21025. (string-match (org-image-file-name-regexp) file)))
  21026. ;;; Paragraph filling stuff.
  21027. ;; We want this to be just right, so use the full arsenal.
  21028. (defun org-indent-line-function ()
  21029. "Indent line like previous, but further if previous was headline or item."
  21030. (interactive)
  21031. (let* ((pos (point))
  21032. (itemp (org-at-item-p))
  21033. column bpos bcol tpos tcol bullet btype bullet-type)
  21034. ;; Find the previous relevant line
  21035. (beginning-of-line 1)
  21036. (cond
  21037. ((looking-at "#") (setq column 0))
  21038. ((looking-at "\\*+ ") (setq column 0))
  21039. (t
  21040. (beginning-of-line 0)
  21041. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  21042. (beginning-of-line 0))
  21043. (cond
  21044. ((looking-at "\\*+[ \t]+")
  21045. (goto-char (match-end 0))
  21046. (setq column (current-column)))
  21047. ((org-in-item-p)
  21048. (org-beginning-of-item)
  21049. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  21050. (setq bpos (match-beginning 1) tpos (match-end 0)
  21051. bcol (progn (goto-char bpos) (current-column))
  21052. tcol (progn (goto-char tpos) (current-column))
  21053. bullet (match-string 1)
  21054. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  21055. (if (not itemp)
  21056. (setq column tcol)
  21057. (goto-char pos)
  21058. (beginning-of-line 1)
  21059. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  21060. (setq bullet (match-string 1)
  21061. btype (if (string-match "[0-9]" bullet) "n" bullet))
  21062. (setq column (if (equal btype bullet-type) bcol tcol))))
  21063. (t (setq column (org-get-indentation))))))
  21064. (goto-char pos)
  21065. (if (<= (current-column) (current-indentation))
  21066. (indent-line-to column)
  21067. (save-excursion (indent-line-to column)))
  21068. (setq column (current-column))
  21069. (beginning-of-line 1)
  21070. (if (looking-at
  21071. "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  21072. (replace-match (concat "\\1" (format org-property-format
  21073. (match-string 2) (match-string 3)))
  21074. t nil))
  21075. (move-to-column column)))
  21076. (defun org-set-autofill-regexps ()
  21077. (interactive)
  21078. ;; In the paragraph separator we include headlines, because filling
  21079. ;; text in a line directly attached to a headline would otherwise
  21080. ;; fill the headline as well.
  21081. (org-set-local 'comment-start-skip "^#+[ \t]*")
  21082. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  21083. ;; FIXME!!!!!!! (org-set-local 'paragraph-separate "\f\\|[ ]*$")
  21084. ;; The paragraph starter includes hand-formatted lists.
  21085. (org-set-local 'paragraph-start
  21086. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  21087. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  21088. ;; But only if the user has not turned off tables or fixed-width regions
  21089. (org-set-local
  21090. 'auto-fill-inhibit-regexp
  21091. (concat "\\*\\|#\\+"
  21092. "\\|[ \t]*" org-keyword-time-regexp
  21093. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  21094. (concat
  21095. "\\|[ \t]*["
  21096. (if org-enable-table-editor "|" "")
  21097. (if org-enable-fixed-width-editor ":" "")
  21098. "]"))))
  21099. ;; We use our own fill-paragraph function, to make sure that tables
  21100. ;; and fixed-width regions are not wrapped. That function will pass
  21101. ;; through to `fill-paragraph' when appropriate.
  21102. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21103. ; Adaptive filling: To get full control, first make sure that
  21104. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  21105. (org-set-local 'adaptive-fill-regexp "\000")
  21106. (org-set-local 'adaptive-fill-function
  21107. 'org-adaptive-fill-function))
  21108. (defun org-fill-paragraph (&optional justify)
  21109. "Re-align a table, pass through to fill-paragraph if no table."
  21110. (let ((table-p (org-at-table-p))
  21111. (table.el-p (org-at-table.el-p)))
  21112. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  21113. (table.el-p t) ; skip table.el tables
  21114. (table-p (org-table-align) t) ; align org-mode tables
  21115. (t nil)))) ; call paragraph-fill
  21116. ;; For reference, this is the default value of adaptive-fill-regexp
  21117. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  21118. (defun org-adaptive-fill-function ()
  21119. "Return a fill prefix for org-mode files.
  21120. In particular, this makes sure hanging paragraphs for hand-formatted lists
  21121. work correctly."
  21122. (cond ((looking-at "#[ \t]+")
  21123. (match-string 0))
  21124. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  21125. (save-excursion
  21126. (goto-char (match-end 0))
  21127. (make-string (current-column) ?\ )))
  21128. (t nil)))
  21129. ;;;; Functions extending outline functionality
  21130. ;; C-a should go to the beginning of a *visible* line, also in the
  21131. ;; new outline.el. I guess this should be patched into Emacs?
  21132. (defun org-beginning-of-line ()
  21133. "Go to the beginning of the current line. If that is invisible, continue
  21134. to a visible line beginning. This makes the function of C-a more intuitive."
  21135. (interactive)
  21136. (let ((pos (point)))
  21137. (beginning-of-line 1)
  21138. (if (bobp)
  21139. nil
  21140. (backward-char 1)
  21141. (if (org-invisible-p)
  21142. (while (and (not (bobp)) (org-invisible-p))
  21143. (backward-char 1)
  21144. (beginning-of-line 1))
  21145. (forward-char 1)))
  21146. (when (and org-special-ctrl-a (looking-at org-todo-line-regexp)
  21147. (= (char-after (match-end 1)) ?\ ))
  21148. (goto-char
  21149. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  21150. ((= pos (point)) (match-beginning 3))
  21151. (t (point)))))))
  21152. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21153. (defun org-invisible-p ()
  21154. "Check if point is at a character currently not visible."
  21155. ;; Early versions of noutline don't have `outline-invisible-p'.
  21156. (if (fboundp 'outline-invisible-p)
  21157. (outline-invisible-p)
  21158. (get-char-property (point) 'invisible)))
  21159. (defun org-invisible-p2 ()
  21160. "Check if point is at a character currently not visible."
  21161. (save-excursion
  21162. (if (and (eolp) (not (bobp))) (backward-char 1))
  21163. ;; Early versions of noutline don't have `outline-invisible-p'.
  21164. (if (fboundp 'outline-invisible-p)
  21165. (outline-invisible-p)
  21166. (get-char-property (point) 'invisible))))
  21167. (defalias 'org-back-to-heading 'outline-back-to-heading)
  21168. (defalias 'org-on-heading-p 'outline-on-heading-p)
  21169. (defalias 'org-at-heading-p 'outline-on-heading-p)
  21170. (defun org-at-heading-or-item-p ()
  21171. (or (org-on-heading-p) (org-at-item-p)))
  21172. (defun org-on-target-p ()
  21173. (or (org-in-regexp org-radio-target-regexp)
  21174. (org-in-regexp org-target-regexp)))
  21175. (defun org-up-heading-all (arg)
  21176. "Move to the heading line of which the present line is a subheading.
  21177. This function considers both visible and invisible heading lines.
  21178. With argument, move up ARG levels."
  21179. (if (fboundp 'outline-up-heading-all)
  21180. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21181. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21182. (defun org-goto-sibling (&optional previous)
  21183. "Goto the next sibling, even if it is invisible.
  21184. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21185. when a sibling was found. When none is found, return nil and don't
  21186. move point."
  21187. (let ((fun (if previous 're-search-backward 're-search-forward))
  21188. (pos (point))
  21189. (re (concat "^" outline-regexp))
  21190. level l)
  21191. (when (condition-case nil (org-back-to-heading t) (error nil))
  21192. (setq level (funcall outline-level))
  21193. (catch 'exit
  21194. (or previous (forward-char 1))
  21195. (while (funcall fun re nil t)
  21196. (setq l (funcall outline-level))
  21197. (when (< l level) (goto-char pos) (throw 'exit nil))
  21198. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21199. (goto-char pos)
  21200. nil))))
  21201. (defun org-show-siblings ()
  21202. "Show all siblings of the current headline."
  21203. (save-excursion
  21204. (while (org-goto-sibling) (org-flag-heading nil)))
  21205. (save-excursion
  21206. (while (org-goto-sibling 'previous)
  21207. (org-flag-heading nil))))
  21208. (defun org-show-hidden-entry ()
  21209. "Show an entry where even the heading is hidden."
  21210. (save-excursion
  21211. (org-show-entry)))
  21212. (defun org-flag-heading (flag &optional entry)
  21213. "Flag the current heading. FLAG non-nil means make invisible.
  21214. When ENTRY is non-nil, show the entire entry."
  21215. (save-excursion
  21216. (org-back-to-heading t)
  21217. ;; Check if we should show the entire entry
  21218. (if entry
  21219. (progn
  21220. (org-show-entry)
  21221. (save-excursion
  21222. (and (outline-next-heading)
  21223. (org-flag-heading nil))))
  21224. (outline-flag-region (max 1 (1- (point)))
  21225. (save-excursion (outline-end-of-heading) (point))
  21226. flag))))
  21227. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  21228. ;; This is an exact copy of the original function, but it uses
  21229. ;; `org-back-to-heading', to make it work also in invisible
  21230. ;; trees. And is uses an invisible-OK argument.
  21231. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21232. (org-back-to-heading invisible-OK)
  21233. (let ((first t)
  21234. (level (funcall outline-level)))
  21235. (while (and (not (eobp))
  21236. (or first (> (funcall outline-level) level)))
  21237. (setq first nil)
  21238. (outline-next-heading))
  21239. (unless to-heading
  21240. (if (memq (preceding-char) '(?\n ?\^M))
  21241. (progn
  21242. ;; Go to end of line before heading
  21243. (forward-char -1)
  21244. (if (memq (preceding-char) '(?\n ?\^M))
  21245. ;; leave blank line before heading
  21246. (forward-char -1))))))
  21247. (point))
  21248. (defun org-show-subtree ()
  21249. "Show everything after this heading at deeper levels."
  21250. (outline-flag-region
  21251. (point)
  21252. (save-excursion
  21253. (outline-end-of-subtree) (outline-next-heading) (point))
  21254. nil))
  21255. (defun org-show-entry ()
  21256. "Show the body directly following this heading.
  21257. Show the heading too, if it is currently invisible."
  21258. (interactive)
  21259. (save-excursion
  21260. (org-back-to-heading t)
  21261. (outline-flag-region
  21262. (max 1 (1- (point)))
  21263. (save-excursion
  21264. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  21265. (or (match-beginning 1) (point-max)))
  21266. nil)))
  21267. (defun org-make-options-regexp (kwds)
  21268. "Make a regular expression for keyword lines."
  21269. (concat
  21270. "^"
  21271. "#?[ \t]*\\+\\("
  21272. (mapconcat 'regexp-quote kwds "\\|")
  21273. "\\):[ \t]*"
  21274. "\\(.+\\)"))
  21275. ;; Make isearch reveal the necessary context
  21276. (defun org-isearch-end ()
  21277. "Reveal context after isearch exits."
  21278. (when isearch-success ; only if search was successful
  21279. (if (featurep 'xemacs)
  21280. ;; Under XEmacs, the hook is run in the correct place,
  21281. ;; we directly show the context.
  21282. (org-show-context 'isearch)
  21283. ;; In Emacs the hook runs *before* restoring the overlays.
  21284. ;; So we have to use a one-time post-command-hook to do this.
  21285. ;; (Emacs 22 has a special variable, see function `org-mode')
  21286. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21287. isearch-mode-end-hook-quit)
  21288. ;; Only when the isearch was not quitted.
  21289. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21290. 'append 'local)))))
  21291. (defun org-isearch-post-command ()
  21292. "Remove self from hook, and show context."
  21293. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21294. (org-show-context 'isearch))
  21295. ;;;; Address problems with some other packages
  21296. ;; Make flyspell not check words in links, to not mess up our keymap
  21297. (defun org-mode-flyspell-verify ()
  21298. "Don't let flyspell put overlays at active buttons."
  21299. (not (get-text-property (point) 'keymap)))
  21300. ;; Make `bookmark-jump' show the jump location if it was hidden.
  21301. (eval-after-load "bookmark"
  21302. '(if (boundp 'bookmark-after-jump-hook)
  21303. ;; We can use the hook
  21304. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21305. ;; Hook not available, use advice
  21306. (defadvice bookmark-jump (after org-make-visible activate)
  21307. "Make the position visible."
  21308. (org-bookmark-jump-unhide))))
  21309. (defun org-bookmark-jump-unhide ()
  21310. "Unhide the current position, to show the bookmark location."
  21311. (and (org-mode-p)
  21312. (or (org-invisible-p)
  21313. (save-excursion (goto-char (max (point-min) (1- (point))))
  21314. (org-invisible-p)))
  21315. (org-show-context 'bookmark-jump)))
  21316. ;; Make session.el ignore our circular variable
  21317. (eval-after-load "session"
  21318. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21319. ;;;; Experimental code
  21320. (defun org-closed-in-range ()
  21321. "Sparse tree of items closed in a certain time range.
  21322. Still experimental, may disappear in the furture."
  21323. (interactive)
  21324. ;; Get the time interval from the user.
  21325. (let* ((time1 (time-to-seconds
  21326. (org-read-date nil 'to-time nil "Starting date: ")))
  21327. (time2 (time-to-seconds
  21328. (org-read-date nil 'to-time nil "End date:")))
  21329. ;; callback function
  21330. (callback (lambda ()
  21331. (let ((time
  21332. (time-to-seconds
  21333. (apply 'encode-time
  21334. (org-parse-time-string
  21335. (match-string 1))))))
  21336. ;; check if time in interval
  21337. (and (>= time time1) (<= time time2))))))
  21338. ;; make tree, check each match with the callback
  21339. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21340. ;;;; Finish up
  21341. (provide 'org)
  21342. (run-hooks 'org-load-hook)
  21343. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  21344. ;;; org.el ends here