org.el 972 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476
  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 <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.13e
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (require 'easymenu)
  77. ;;;; Customization variables
  78. ;;; Version
  79. (defconst org-version "5.13e"
  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. (while (string-match "\\[:alpha:\\]" ss)
  103. (setq ss (replace-match "a-zA-Z" t t ss)))
  104. ss))
  105. s))
  106. (defmacro org-preserve-lc (&rest body)
  107. `(let ((_line (org-current-line))
  108. (_col (current-column)))
  109. (unwind-protect
  110. (progn ,@body)
  111. (goto-line _line)
  112. (move-to-column _col))))
  113. (defmacro org-without-partial-completion (&rest body)
  114. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  115. partial-completion-mode)))
  116. (unwind-protect
  117. (progn
  118. (if pc-mode (partial-completion-mode -1))
  119. ,@body)
  120. (if pc-mode (partial-completion-mode 1)))))
  121. ;;; The custom variables
  122. (defgroup org nil
  123. "Outline-based notes management and organizer."
  124. :tag "Org"
  125. :group 'outlines
  126. :group 'hypermedia
  127. :group 'calendar)
  128. ;; FIXME: Needs a separate group...
  129. (defcustom org-completion-fallback-command 'hippie-expand
  130. "The expansion command called by \\[org-complete] in normal context.
  131. Normal means, no org-mode-specific context."
  132. :group 'org
  133. :type 'function)
  134. (defgroup org-startup nil
  135. "Options concerning startup of Org-mode."
  136. :tag "Org Startup"
  137. :group 'org)
  138. (defcustom org-startup-folded t
  139. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  140. This can also be configured on a per-file basis by adding one of
  141. the following lines anywhere in the buffer:
  142. #+STARTUP: fold
  143. #+STARTUP: nofold
  144. #+STARTUP: content"
  145. :group 'org-startup
  146. :type '(choice
  147. (const :tag "nofold: show all" nil)
  148. (const :tag "fold: overview" t)
  149. (const :tag "content: all headlines" content)))
  150. (defcustom org-startup-truncated t
  151. "Non-nil means, entering Org-mode will set `truncate-lines'.
  152. This is useful since some lines containing links can be very long and
  153. uninteresting. Also tables look terrible when wrapped."
  154. :group 'org-startup
  155. :type 'boolean)
  156. (defcustom org-startup-align-all-tables nil
  157. "Non-nil means, align all tables when visiting a file.
  158. This is useful when the column width in tables is forced with <N> cookies
  159. in table fields. Such tables will look correct only after the first re-align.
  160. This can also be configured on a per-file basis by adding one of
  161. the following lines anywhere in the buffer:
  162. #+STARTUP: align
  163. #+STARTUP: noalign"
  164. :group 'org-startup
  165. :type 'boolean)
  166. (defcustom org-insert-mode-line-in-empty-file nil
  167. "Non-nil means insert the first line setting Org-mode in empty files.
  168. When the function `org-mode' is called interactively in an empty file, this
  169. normally means that the file name does not automatically trigger Org-mode.
  170. To ensure that the file will always be in Org-mode in the future, a
  171. line enforcing Org-mode will be inserted into the buffer, if this option
  172. has been set."
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-replace-disputed-keys nil
  176. "Non-nil means use alternative key bindings for some keys.
  177. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  178. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  179. If you want to use Org-mode together with one of these other modes,
  180. or more generally if you would like to move some Org-mode commands to
  181. other keys, set this variable and configure the keys with the variable
  182. `org-disputed-keys'.
  183. This option is only relevant at load-time of Org-mode, and must be set
  184. *before* org.el is loaded. Changing it requires a restart of Emacs to
  185. become effective."
  186. :group 'org-startup
  187. :type 'boolean)
  188. (if (fboundp 'defvaralias)
  189. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  190. (defcustom org-disputed-keys
  191. '(([(shift up)] . [(meta p)])
  192. ([(shift down)] . [(meta n)])
  193. ([(shift left)] . [(meta -)])
  194. ([(shift right)] . [(meta +)])
  195. ([(control shift right)] . [(meta shift +)])
  196. ([(control shift left)] . [(meta shift -)]))
  197. "Keys for which Org-mode and other modes compete.
  198. This is an alist, cars are the default keys, second element specifies
  199. the alternative to use when `org-replace-disputed-keys' is t.
  200. Keys can be specified in any syntax supported by `define-key'.
  201. The value of this option takes effect only at Org-mode's startup,
  202. therefore you'll have to restart Emacs to apply it after changing."
  203. :group 'org-startup
  204. :type 'alist)
  205. (defun org-key (key)
  206. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  207. Or return the original if not disputed."
  208. (if org-replace-disputed-keys
  209. (let* ((nkey (key-description key))
  210. (x (org-find-if (lambda (x)
  211. (equal (key-description (car x)) nkey))
  212. org-disputed-keys)))
  213. (if x (cdr x) key))
  214. key))
  215. (defun org-find-if (predicate seq)
  216. (catch 'exit
  217. (while seq
  218. (if (funcall predicate (car seq))
  219. (throw 'exit (car seq))
  220. (pop seq)))))
  221. (defun org-defkey (keymap key def)
  222. "Define a key, possibly translated, as returned by `org-key'."
  223. (define-key keymap (org-key key) def))
  224. (defcustom org-ellipsis 'org-ellipsis
  225. "The ellipsis to use in the Org-mode outline.
  226. When nil, just use the standard three dots. When a string, use that instead,
  227. When a face, use the standart 3 dots, but with the specified face.
  228. The change affects only Org-mode (which will then use its own display table).
  229. Changing this requires executing `M-x org-mode' in a buffer to become
  230. effective."
  231. :group 'org-startup
  232. :type '(choice (const :tag "Default" nil)
  233. (face :tag "Face" :value org-warning)
  234. (string :tag "String" :value "...#")))
  235. (defvar org-display-table nil
  236. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  237. (defgroup org-keywords nil
  238. "Keywords in Org-mode."
  239. :tag "Org Keywords"
  240. :group 'org)
  241. (defcustom org-deadline-string "DEADLINE:"
  242. "String to mark deadline entries.
  243. A deadline is this string, followed by a time stamp. Should be a word,
  244. terminated by a colon. You can insert a schedule keyword and
  245. a timestamp with \\[org-deadline].
  246. Changes become only effective after restarting Emacs."
  247. :group 'org-keywords
  248. :type 'string)
  249. (defcustom org-scheduled-string "SCHEDULED:"
  250. "String to mark scheduled TODO entries.
  251. A schedule is this string, followed by a time stamp. Should be a word,
  252. terminated by a colon. You can insert a schedule keyword and
  253. a timestamp with \\[org-schedule].
  254. Changes become only effective after restarting Emacs."
  255. :group 'org-keywords
  256. :type 'string)
  257. (defcustom org-closed-string "CLOSED:"
  258. "String used as the prefix for timestamps logging closing a TODO entry."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-clock-string "CLOCK:"
  262. "String used as prefix for timestamps clocking work hours on an item."
  263. :group 'org-keywords
  264. :type 'string)
  265. (defcustom org-comment-string "COMMENT"
  266. "Entries starting with this keyword will never be exported.
  267. An entry can be toggled between COMMENT and normal with
  268. \\[org-toggle-comment].
  269. Changes become only effective after restarting Emacs."
  270. :group 'org-keywords
  271. :type 'string)
  272. (defcustom org-quote-string "QUOTE"
  273. "Entries starting with this keyword will be exported in fixed-width font.
  274. Quoting applies only to the text in the entry following the headline, and does
  275. not extend beyond the next headline, even if that is lower level.
  276. An entry can be toggled between QUOTE and normal with
  277. \\[org-toggle-fixed-width-section]."
  278. :group 'org-keywords
  279. :type 'string)
  280. (defconst org-repeat-re
  281. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  282. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  283. "Regular expression for specifying repeated events.
  284. After a match, group 1 contains the repeat expression.")
  285. (defgroup org-structure nil
  286. "Options concerning the general structure of Org-mode files."
  287. :tag "Org Structure"
  288. :group 'org)
  289. (defgroup org-reveal-location nil
  290. "Options about how to make context of a location visible."
  291. :tag "Org Reveal Location"
  292. :group 'org-structure)
  293. (defcustom org-show-hierarchy-above '((default . t))
  294. "Non-nil means, show full hierarchy when revealing a location.
  295. Org-mode often shows locations in an org-mode file which might have
  296. been invisible before. When this is set, the hierarchy of headings
  297. above the exposed location is shown.
  298. Turning this off for example for sparse trees makes them very compact.
  299. Instead of t, this can also be an alist specifying this option for different
  300. contexts. Valid contexts are
  301. agenda when exposing an entry from the agenda
  302. org-goto when using the command `org-goto' on key C-c C-j
  303. occur-tree when using the command `org-occur' on key C-c /
  304. tags-tree when constructing a sparse tree based on tags matches
  305. link-search when exposing search matches associated with a link
  306. mark-goto when exposing the jump goal of a mark
  307. bookmark-jump when exposing a bookmark location
  308. isearch when exiting from an incremental search
  309. default default for all contexts not set explicitly"
  310. :group 'org-reveal-location
  311. :type '(choice
  312. (const :tag "Always" t)
  313. (const :tag "Never" nil)
  314. (repeat :greedy t :tag "Individual contexts"
  315. (cons
  316. (choice :tag "Context"
  317. (const agenda)
  318. (const org-goto)
  319. (const occur-tree)
  320. (const tags-tree)
  321. (const link-search)
  322. (const mark-goto)
  323. (const bookmark-jump)
  324. (const isearch)
  325. (const default))
  326. (boolean)))))
  327. (defcustom org-show-following-heading '((default . nil))
  328. "Non-nil means, show following heading when revealing a location.
  329. Org-mode often shows locations in an org-mode file which might have
  330. been invisible before. When this is set, the heading following the
  331. match is shown.
  332. Turning this off for example for sparse trees makes them very compact,
  333. but makes it harder to edit the location of the match. In such a case,
  334. use the command \\[org-reveal] to show more context.
  335. Instead of t, this can also be an alist specifying this option for different
  336. contexts. See `org-show-hierarchy-above' for valid contexts."
  337. :group 'org-reveal-location
  338. :type '(choice
  339. (const :tag "Always" t)
  340. (const :tag "Never" nil)
  341. (repeat :greedy t :tag "Individual contexts"
  342. (cons
  343. (choice :tag "Context"
  344. (const agenda)
  345. (const org-goto)
  346. (const occur-tree)
  347. (const tags-tree)
  348. (const link-search)
  349. (const mark-goto)
  350. (const bookmark-jump)
  351. (const isearch)
  352. (const default))
  353. (boolean)))))
  354. (defcustom org-show-siblings '((default . nil) (isearch t))
  355. "Non-nil means, show all sibling heading when revealing a location.
  356. Org-mode often shows locations in an org-mode file which might have
  357. been invisible before. When this is set, the sibling of the current entry
  358. heading are all made visible. If `org-show-hierarchy-above' is t,
  359. the same happens on each level of the hierarchy above the current entry.
  360. By default this is on for the isearch context, off for all other contexts.
  361. Turning this off for example for sparse trees makes them very compact,
  362. but makes it harder to edit the location of the match. In such a case,
  363. use the command \\[org-reveal] to show more context.
  364. Instead of t, this can also be an alist specifying this option for different
  365. contexts. See `org-show-hierarchy-above' for valid contexts."
  366. :group 'org-reveal-location
  367. :type '(choice
  368. (const :tag "Always" t)
  369. (const :tag "Never" nil)
  370. (repeat :greedy t :tag "Individual contexts"
  371. (cons
  372. (choice :tag "Context"
  373. (const agenda)
  374. (const org-goto)
  375. (const occur-tree)
  376. (const tags-tree)
  377. (const link-search)
  378. (const mark-goto)
  379. (const bookmark-jump)
  380. (const isearch)
  381. (const default))
  382. (boolean)))))
  383. (defgroup org-cycle nil
  384. "Options concerning visibility cycling in Org-mode."
  385. :tag "Org Cycle"
  386. :group 'org-structure)
  387. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  388. "Names of drawers. Drawers are not opened by cycling on the headline above.
  389. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  390. this:
  391. :DRAWERNAME:
  392. .....
  393. :END:
  394. The drawer \"PROPERTIES\" is special for capturing properties through
  395. the property API.
  396. Drawers can be defined on the per-file basis with a line like:
  397. #+DRAWERS: HIDDEN STATE PROPERTIES"
  398. :group 'org-structure
  399. :type '(repeat (string :tag "Drawer Name")))
  400. (defcustom org-cycle-global-at-bob nil
  401. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  402. This makes it possible to do global cycling without having to use S-TAB or
  403. C-u TAB. For this special case to work, the first line of the buffer
  404. must not be a headline - it may be empty ot some other text. When used in
  405. this way, `org-cycle-hook' is disables temporarily, to make sure the
  406. cursor stays at the beginning of the buffer.
  407. When this option is nil, don't do anything special at the beginning
  408. of the buffer."
  409. :group 'org-cycle
  410. :type 'boolean)
  411. (defcustom org-cycle-emulate-tab t
  412. "Where should `org-cycle' emulate TAB.
  413. nil Never
  414. white Only in completely white lines
  415. whitestart Only at the beginning of lines, before the first non-white char.
  416. t Everywhere except in headlines
  417. exc-hl-bol Everywhere except at the start of a headline
  418. If TAB is used in a place where it does not emulate TAB, the current subtree
  419. visibility is cycled."
  420. :group 'org-cycle
  421. :type '(choice (const :tag "Never" nil)
  422. (const :tag "Only in completely white lines" white)
  423. (const :tag "Before first char in a line" whitestart)
  424. (const :tag "Everywhere except in headlines" t)
  425. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  426. ))
  427. (defcustom org-cycle-separator-lines 2
  428. "Number of empty lines needed to keep an empty line between collapsed trees.
  429. If you leave an empty line between the end of a subtree and the following
  430. headline, this empty line is hidden when the subtree is folded.
  431. Org-mode will leave (exactly) one empty line visible if the number of
  432. empty lines is equal or larger to the number given in this variable.
  433. So the default 2 means, at least 2 empty lines after the end of a subtree
  434. are needed to produce free space between a collapsed subtree and the
  435. following headline.
  436. Special case: when 0, never leave empty lines in collapsed view."
  437. :group 'org-cycle
  438. :type 'integer)
  439. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  440. org-cycle-hide-drawers
  441. org-cycle-show-empty-lines
  442. org-optimize-window-after-visibility-change)
  443. "Hook that is run after `org-cycle' has changed the buffer visibility.
  444. The function(s) in this hook must accept a single argument which indicates
  445. the new state that was set by the most recent `org-cycle' command. The
  446. argument is a symbol. After a global state change, it can have the values
  447. `overview', `content', or `all'. After a local state change, it can have
  448. the values `folded', `children', or `subtree'."
  449. :group 'org-cycle
  450. :type 'hook)
  451. (defgroup org-edit-structure nil
  452. "Options concerning structure editing in Org-mode."
  453. :tag "Org Edit Structure"
  454. :group 'org-structure)
  455. (defcustom org-special-ctrl-a/e nil
  456. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  457. When t, `C-a' will bring back the cursor to the beginning of the
  458. headline text, i.e. after the stars and after a possible TODO keyword.
  459. In an item, this will be the position after the bullet.
  460. When the cursor is already at that position, another `C-a' will bring
  461. it to the beginning of the line.
  462. `C-e' will jump to the end of the headline, ignoring the presence of tags
  463. in the headline. A second `C-e' will then jump to the true end of the
  464. line, after any tags.
  465. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  466. and only a directly following, identical keypress will bring the cursor
  467. to the special positions."
  468. :group 'org-edit-structure
  469. :type '(choice
  470. (const :tag "off" nil)
  471. (const :tag "after bullet first" t)
  472. (const :tag "border first" reversed)))
  473. (if (fboundp 'defvaralias)
  474. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  475. (defcustom org-odd-levels-only nil
  476. "Non-nil means, skip even levels and only use odd levels for the outline.
  477. This has the effect that two stars are being added/taken away in
  478. promotion/demotion commands. It also influences how levels are
  479. handled by the exporters.
  480. Changing it requires restart of `font-lock-mode' to become effective
  481. for fontification also in regions already fontified.
  482. You may also set this on a per-file basis by adding one of the following
  483. lines to the buffer:
  484. #+STARTUP: odd
  485. #+STARTUP: oddeven"
  486. :group 'org-edit-structure
  487. :group 'org-font-lock
  488. :type 'boolean)
  489. (defcustom org-adapt-indentation t
  490. "Non-nil means, adapt indentation when promoting and demoting.
  491. When this is set and the *entire* text in an entry is indented, the
  492. indentation is increased by one space in a demotion command, and
  493. decreased by one in a promotion command. If any line in the entry
  494. body starts at column 0, indentation is not changed at all."
  495. :group 'org-edit-structure
  496. :type 'boolean)
  497. (defcustom org-blank-before-new-entry '((heading . nil)
  498. (plain-list-item . nil))
  499. "Should `org-insert-heading' leave a blank line before new heading/item?
  500. The value is an alist, with `heading' and `plain-list-item' as car,
  501. and a boolean flag as cdr."
  502. :group 'org-edit-structure
  503. :type '(list
  504. (cons (const heading) (boolean))
  505. (cons (const plain-list-item) (boolean))))
  506. (defcustom org-insert-heading-hook nil
  507. "Hook being run after inserting a new heading."
  508. :group 'org-edit-structure
  509. :type 'boolean)
  510. (defcustom org-enable-fixed-width-editor t
  511. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  512. This currently only means, they are never auto-wrapped.
  513. When nil, such lines will be treated like ordinary lines.
  514. See also the QUOTE keyword."
  515. :group 'org-edit-structure
  516. :type 'boolean)
  517. (defgroup org-sparse-trees nil
  518. "Options concerning sparse trees in Org-mode."
  519. :tag "Org Sparse Trees"
  520. :group 'org-structure)
  521. (defcustom org-highlight-sparse-tree-matches t
  522. "Non-nil means, highlight all matches that define a sparse tree.
  523. The highlights will automatically disappear the next time the buffer is
  524. changed by an edit command."
  525. :group 'org-sparse-trees
  526. :type 'boolean)
  527. (defcustom org-remove-highlights-with-change t
  528. "Non-nil means, any change to the buffer will remove temporary highlights.
  529. Such highlights are created by `org-occur' and `org-clock-display'.
  530. When nil, `C-c C-c needs to be used to get rid of the highlights.
  531. The highlights created by `org-preview-latex-fragment' always need
  532. `C-c C-c' to be removed."
  533. :group 'org-sparse-trees
  534. :group 'org-time
  535. :type 'boolean)
  536. (defcustom org-occur-hook '(org-first-headline-recenter)
  537. "Hook that is run after `org-occur' has constructed a sparse tree.
  538. This can be used to recenter the window to show as much of the structure
  539. as possible."
  540. :group 'org-sparse-trees
  541. :type 'hook)
  542. (defgroup org-plain-lists nil
  543. "Options concerning plain lists in Org-mode."
  544. :tag "Org Plain lists"
  545. :group 'org-structure)
  546. (defcustom org-cycle-include-plain-lists nil
  547. "Non-nil means, include plain lists into visibility cycling.
  548. This means that during cycling, plain list items will *temporarily* be
  549. interpreted as outline headlines with a level given by 1000+i where i is the
  550. indentation of the bullet. In all other operations, plain list items are
  551. not seen as headlines. For example, you cannot assign a TODO keyword to
  552. such an item."
  553. :group 'org-plain-lists
  554. :type 'boolean)
  555. (defcustom org-plain-list-ordered-item-terminator t
  556. "The character that makes a line with leading number an ordered list item.
  557. Valid values are ?. and ?\). To get both terminators, use t. While
  558. ?. may look nicer, it creates the danger that a line with leading
  559. number may be incorrectly interpreted as an item. ?\) therefore is
  560. the safe choice."
  561. :group 'org-plain-lists
  562. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  563. (const :tag "paren like in \"2)\"" ?\))
  564. (const :tab "both" t)))
  565. (defcustom org-auto-renumber-ordered-lists t
  566. "Non-nil means, automatically renumber ordered plain lists.
  567. Renumbering happens when the sequence have been changed with
  568. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  569. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  570. :group 'org-plain-lists
  571. :type 'boolean)
  572. (defcustom org-provide-checkbox-statistics t
  573. "Non-nil means, update checkbox statistics after insert and toggle.
  574. When this is set, checkbox statistics is updated each time you either insert
  575. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  576. with \\[org-ctrl-c-ctrl-c\\]."
  577. :group 'org-plain-lists
  578. :type 'boolean)
  579. (defgroup org-archive nil
  580. "Options concerning archiving in Org-mode."
  581. :tag "Org Archive"
  582. :group 'org-structure)
  583. (defcustom org-archive-tag "ARCHIVE"
  584. "The tag that marks a subtree as archived.
  585. An archived subtree does not open during visibility cycling, and does
  586. not contribute to the agenda listings."
  587. :group 'org-archive
  588. :group 'org-keywords
  589. :type 'string)
  590. (defcustom org-agenda-skip-archived-trees t
  591. "Non-nil means, the agenda will skip any items located in archived trees.
  592. An archived tree is a tree marked with the tag ARCHIVE."
  593. :group 'org-archive
  594. :group 'org-agenda-skip
  595. :type 'boolean)
  596. (defcustom org-cycle-open-archived-trees nil
  597. "Non-nil means, `org-cycle' will open archived trees.
  598. An archived tree is a tree marked with the tag ARCHIVE.
  599. When nil, archived trees will stay folded. You can still open them with
  600. normal outline commands like `show-all', but not with the cycling commands."
  601. :group 'org-archive
  602. :group 'org-cycle
  603. :type 'boolean)
  604. (defcustom org-sparse-tree-open-archived-trees nil
  605. "Non-nil means sparse tree construction shows matches in archived trees.
  606. When nil, matches in these trees are highlighted, but the trees are kept in
  607. collapsed state."
  608. :group 'org-archive
  609. :group 'org-sparse-trees
  610. :type 'boolean)
  611. (defcustom org-archive-location "%s_archive::"
  612. "The location where subtrees should be archived.
  613. This string consists of two parts, separated by a double-colon.
  614. The first part is a file name - when omitted, archiving happens in the same
  615. file. %s will be replaced by the current file name (without directory part).
  616. Archiving to a different file is useful to keep archived entries from
  617. contributing to the Org-mode Agenda.
  618. The part after the double colon is a headline. The archived entries will be
  619. filed under that headline. When omitted, the subtrees are simply filed away
  620. at the end of the file, as top-level entries.
  621. Here are a few examples:
  622. \"%s_archive::\"
  623. If the current file is Projects.org, archive in file
  624. Projects.org_archive, as top-level trees. This is the default.
  625. \"::* Archived Tasks\"
  626. Archive in the current file, under the top-level headline
  627. \"* Archived Tasks\".
  628. \"~/org/archive.org::\"
  629. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  630. \"basement::** Finished Tasks\"
  631. Archive in file ./basement (relative path), as level 3 trees
  632. below the level 2 heading \"** Finished Tasks\".
  633. You may set this option on a per-file basis by adding to the buffer a
  634. line like
  635. #+ARCHIVE: basement::** Finished Tasks"
  636. :group 'org-archive
  637. :type 'string)
  638. (defcustom org-archive-mark-done t
  639. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  640. This can be a string to set the keyword to use. When t, Org-mode will
  641. use the first keyword in its list that means done."
  642. :group 'org-archive
  643. :type '(choice
  644. (const :tag "No" nil)
  645. (const :tag "Yes" t)
  646. (string :tag "Use this keyword")))
  647. (defcustom org-archive-stamp-time t
  648. "Non-nil means, add a time stamp to entries moved to an archive file.
  649. This variable is obsolete and has no effect anymore, instead add ot remove
  650. `time' from the variablle `org-archive-save-context-info'."
  651. :group 'org-archive
  652. :type 'boolean)
  653. (defcustom org-archive-save-context-info '(time file category todo itags)
  654. "Parts of context info that should be stored as properties when archiving.
  655. When a subtree is moved to an archive file, it looses information given by
  656. context, like inherited tags, the category, and possibly also the TODO
  657. state (depending on the variable `org-archive-mark-done').
  658. This variable can be a list of any of the following symbols:
  659. time The time of archiving.
  660. file The file where the entry originates.
  661. itags The local tags, in the headline of the subtree.
  662. ltags The tags the subtree inherits from further up the hierarchy.
  663. todo The pre-archive TODO state.
  664. category The category, taken from file name or #+CATEGORY lines.
  665. For each symbol present in the list, a property will be created in
  666. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  667. information."
  668. :group 'org-archive
  669. :type '(set :greedy t
  670. (const :tag "Time" time)
  671. (const :tag "File" file)
  672. (const :tag "Category" category)
  673. (const :tag "TODO state" todo)
  674. (const :tag "TODO state" priority)
  675. (const :tag "Inherited tags" itags)
  676. (const :tag "Local tags" ltags)))
  677. (defgroup org-table nil
  678. "Options concerning tables in Org-mode."
  679. :tag "Org Table"
  680. :group 'org)
  681. (defcustom org-enable-table-editor 'optimized
  682. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  683. When nil, such lines will be treated like ordinary lines.
  684. When equal to the symbol `optimized', the table editor will be optimized to
  685. do the following:
  686. - Automatic overwrite mode in front of whitespace in table fields.
  687. This makes the structure of the table stay in tact as long as the edited
  688. field does not exceed the column width.
  689. - Minimize the number of realigns. Normally, the table is aligned each time
  690. TAB or RET are pressed to move to another field. With optimization this
  691. happens only if changes to a field might have changed the column width.
  692. Optimization requires replacing the functions `self-insert-command',
  693. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  694. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  695. very good at guessing when a re-align will be necessary, but you can always
  696. force one with \\[org-ctrl-c-ctrl-c].
  697. If you would like to use the optimized version in Org-mode, but the
  698. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  699. This variable can be used to turn on and off the table editor during a session,
  700. but in order to toggle optimization, a restart is required.
  701. See also the variable `org-table-auto-blank-field'."
  702. :group 'org-table
  703. :type '(choice
  704. (const :tag "off" nil)
  705. (const :tag "on" t)
  706. (const :tag "on, optimized" optimized)))
  707. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  708. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  709. In the optimized version, the table editor takes over all simple keys that
  710. normally just insert a character. In tables, the characters are inserted
  711. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  712. for empty fields). Outside tables, the correct binding of the keys is
  713. restored.
  714. The default for this option is t if the optimized version is also used in
  715. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  716. this variable requires a restart of Emacs to become effective."
  717. :group 'org-table
  718. :type 'boolean)
  719. (defcustom orgtbl-radio-table-templates
  720. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  721. % END RECEIVE ORGTBL %n
  722. \\begin{comment}
  723. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  724. | | |
  725. \\end{comment}\n")
  726. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  727. @c END RECEIVE ORGTBL %n
  728. @ignore
  729. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  730. | | |
  731. @end ignore\n")
  732. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  733. <!-- END RECEIVE ORGTBL %n -->
  734. <!--
  735. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  736. | | |
  737. -->\n"))
  738. "Templates for radio tables in different major modes.
  739. All occurrences of %n in a template will be replaced with the name of the
  740. table, obtained by prompting the user."
  741. :group 'org-table
  742. :type '(repeat
  743. (list (symbol :tag "Major mode")
  744. (string :tag "Format"))))
  745. (defgroup org-table-settings nil
  746. "Settings for tables in Org-mode."
  747. :tag "Org Table Settings"
  748. :group 'org-table)
  749. (defcustom org-table-default-size "5x2"
  750. "The default size for newly created tables, Columns x Rows."
  751. :group 'org-table-settings
  752. :type 'string)
  753. (defcustom org-table-number-regexp
  754. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  755. "Regular expression for recognizing numbers in table columns.
  756. If a table column contains mostly numbers, it will be aligned to the
  757. right. If not, it will be aligned to the left.
  758. The default value of this option is a regular expression which allows
  759. anything which looks remotely like a number as used in scientific
  760. context. For example, all of the following will be considered a
  761. number:
  762. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  763. Other options offered by the customize interface are more restrictive."
  764. :group 'org-table-settings
  765. :type '(choice
  766. (const :tag "Positive Integers"
  767. "^[0-9]+$")
  768. (const :tag "Integers"
  769. "^[-+]?[0-9]+$")
  770. (const :tag "Floating Point Numbers"
  771. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  772. (const :tag "Floating Point Number or Integer"
  773. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  774. (const :tag "Exponential, Floating point, Integer"
  775. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  776. (const :tag "Very General Number-Like, including hex"
  777. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  778. (string :tag "Regexp:")))
  779. (defcustom org-table-number-fraction 0.5
  780. "Fraction of numbers in a column required to make the column align right.
  781. In a column all non-white fields are considered. If at least this
  782. fraction of fields is matched by `org-table-number-fraction',
  783. alignment to the right border applies."
  784. :group 'org-table-settings
  785. :type 'number)
  786. (defgroup org-table-editing nil
  787. "Bahavior of tables during editing in Org-mode."
  788. :tag "Org Table Editing"
  789. :group 'org-table)
  790. (defcustom org-table-automatic-realign t
  791. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  792. When nil, aligning is only done with \\[org-table-align], or after column
  793. removal/insertion."
  794. :group 'org-table-editing
  795. :type 'boolean)
  796. (defcustom org-table-auto-blank-field t
  797. "Non-nil means, automatically blank table field when starting to type into it.
  798. This only happens when typing immediately after a field motion
  799. command (TAB, S-TAB or RET).
  800. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  801. :group 'org-table-editing
  802. :type 'boolean)
  803. (defcustom org-table-tab-jumps-over-hlines t
  804. "Non-nil means, tab in the last column of a table with jump over a hline.
  805. If a horizontal separator line is following the current line,
  806. `org-table-next-field' can either create a new row before that line, or jump
  807. over the line. When this option is nil, a new line will be created before
  808. this line."
  809. :group 'org-table-editing
  810. :type 'boolean)
  811. (defcustom org-table-tab-recognizes-table.el t
  812. "Non-nil means, TAB will automatically notice a table.el table.
  813. When it sees such a table, it moves point into it and - if necessary -
  814. calls `table-recognize-table'."
  815. :group 'org-table-editing
  816. :type 'boolean)
  817. (defgroup org-table-calculation nil
  818. "Options concerning tables in Org-mode."
  819. :tag "Org Table Calculation"
  820. :group 'org-table)
  821. (defcustom org-table-use-standard-references t
  822. "Should org-mode work with table refrences like B3 instead of @3$2?
  823. Possible values are:
  824. nil never use them
  825. from accept as input, do not present for editing
  826. t: accept as input and present for editing"
  827. :group 'org-table-calculation
  828. :type '(choice
  829. (const :tag "Never, don't even check unser input for them" nil)
  830. (const :tag "Always, both as user input, and when editing" t)
  831. (const :tag "Convert user input, don't offer during editing" 'from)))
  832. (defcustom org-table-copy-increment t
  833. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  834. :group 'org-table-calculation
  835. :type 'boolean)
  836. (defcustom org-calc-default-modes
  837. '(calc-internal-prec 12
  838. calc-float-format (float 5)
  839. calc-angle-mode deg
  840. calc-prefer-frac nil
  841. calc-symbolic-mode nil
  842. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  843. calc-display-working-message t
  844. )
  845. "List with Calc mode settings for use in calc-eval for table formulas.
  846. The list must contain alternating symbols (Calc modes variables and values).
  847. Don't remove any of the default settings, just change the values. Org-mode
  848. relies on the variables to be present in the list."
  849. :group 'org-table-calculation
  850. :type 'plist)
  851. (defcustom org-table-formula-evaluate-inline t
  852. "Non-nil means, TAB and RET evaluate a formula in current table field.
  853. If the current field starts with an equal sign, it is assumed to be a formula
  854. which should be evaluated as described in the manual and in the documentation
  855. string of the command `org-table-eval-formula'. This feature requires the
  856. Emacs calc package.
  857. When this variable is nil, formula calculation is only available through
  858. the command \\[org-table-eval-formula]."
  859. :group 'org-table-calculation
  860. :type 'boolean)
  861. (defcustom org-table-formula-use-constants t
  862. "Non-nil means, interpret constants in formulas in tables.
  863. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  864. by the value given in `org-table-formula-constants', or by a value obtained
  865. from the `constants.el' package."
  866. :group 'org-table-calculation
  867. :type 'boolean)
  868. (defcustom org-table-formula-constants nil
  869. "Alist with constant names and values, for use in table formulas.
  870. The car of each element is a name of a constant, without the `$' before it.
  871. The cdr is the value as a string. For example, if you'd like to use the
  872. speed of light in a formula, you would configure
  873. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  874. and then use it in an equation like `$1*$c'.
  875. Constants can also be defined on a per-file basis using a line like
  876. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  877. :group 'org-table-calculation
  878. :type '(repeat
  879. (cons (string :tag "name")
  880. (string :tag "value"))))
  881. (defvar org-table-formula-constants-local nil
  882. "Local version of `org-table-formula-constants'.")
  883. (make-variable-buffer-local 'org-table-formula-constants-local)
  884. (defcustom org-table-allow-automatic-line-recalculation t
  885. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  886. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  887. :group 'org-table-calculation
  888. :type 'boolean)
  889. (defgroup org-link nil
  890. "Options concerning links in Org-mode."
  891. :tag "Org Link"
  892. :group 'org)
  893. (defvar org-link-abbrev-alist-local nil
  894. "Buffer-local version of `org-link-abbrev-alist', which see.
  895. The value of this is taken from the #+LINK lines.")
  896. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  897. (defcustom org-link-abbrev-alist nil
  898. "Alist of link abbreviations.
  899. The car of each element is a string, to be replaced at the start of a link.
  900. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  901. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  902. [[linkkey:tag][description]]
  903. If REPLACE is a string, the tag will simply be appended to create the link.
  904. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  905. also be a function that will be called with the tag as the only argument to
  906. create the link. See the manual for examples."
  907. :group 'org-link
  908. :type 'alist)
  909. (defcustom org-descriptive-links t
  910. "Non-nil means, hide link part and only show description of bracket links.
  911. Bracket links are like [[link][descritpion]]. This variable sets the initial
  912. state in new org-mode buffers. The setting can then be toggled on a
  913. per-buffer basis from the Org->Hyperlinks menu."
  914. :group 'org-link
  915. :type 'boolean)
  916. (defcustom org-link-file-path-type 'adaptive
  917. "How the path name in file links should be stored.
  918. Valid values are:
  919. relative relative to the current directory, i.e. the directory of the file
  920. into which the link is being inserted.
  921. absolute absolute path, if possible with ~ for home directory.
  922. noabbrev absolute path, no abbreviation of home directory.
  923. adaptive Use relative path for files in the current directory and sub-
  924. directories of it. For other files, use an absolute path."
  925. :group 'org-link
  926. :type '(choice
  927. (const relative)
  928. (const absolute)
  929. (const noabbrev)
  930. (const adaptive)))
  931. (defcustom org-activate-links '(bracket angle plain radio tag date)
  932. "Types of links that should be activated in Org-mode files.
  933. This is a list of symbols, each leading to the activation of a certain link
  934. type. In principle, it does not hurt to turn on most link types - there may
  935. be a small gain when turning off unused link types. The types are:
  936. bracket The recommended [[link][description]] or [[link]] links with hiding.
  937. angular Links in angular brackes that may contain whitespace like
  938. <bbdb:Carsten Dominik>.
  939. plain Plain links in normal text, no whitespace, like http://google.com.
  940. radio Text that is matched by a radio target, see manual for details.
  941. tag Tag settings in a headline (link to tag search).
  942. date Time stamps (link to calendar).
  943. Changing this variable requires a restart of Emacs to become effective."
  944. :group 'org-link
  945. :type '(set (const :tag "Double bracket links (new style)" bracket)
  946. (const :tag "Angular bracket links (old style)" angular)
  947. (const :tag "plain text links" plain)
  948. (const :tag "Radio target matches" radio)
  949. (const :tag "Tags" tag)
  950. (const :tag "Tags" target)
  951. (const :tag "Timestamps" date)))
  952. (defgroup org-link-store nil
  953. "Options concerning storing links in Org-mode"
  954. :tag "Org Store Link"
  955. :group 'org-link)
  956. (defcustom org-email-link-description-format "Email %c: %.30s"
  957. "Format of the description part of a link to an email or usenet message.
  958. The following %-excapes will be replaced by corresponding information:
  959. %F full \"From\" field
  960. %f name, taken from \"From\" field, address if no name
  961. %T full \"To\" field
  962. %t first name in \"To\" field, address if no name
  963. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  964. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  965. %s subject
  966. %m message-id.
  967. You may use normal field width specification between the % and the letter.
  968. This is for example useful to limit the length of the subject.
  969. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  970. :group 'org-link-store
  971. :type 'string)
  972. (defcustom org-from-is-user-regexp
  973. (let (r1 r2)
  974. (when (and user-mail-address (not (string= user-mail-address "")))
  975. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  976. (when (and user-full-name (not (string= user-full-name "")))
  977. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  978. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  979. "Regexp mached against the \"From:\" header of an email or usenet message.
  980. It should match if the message is from the user him/herself."
  981. :group 'org-link-store
  982. :type 'regexp)
  983. (defcustom org-context-in-file-links t
  984. "Non-nil means, file links from `org-store-link' contain context.
  985. A search string will be added to the file name with :: as separator and
  986. used to find the context when the link is activated by the command
  987. `org-open-at-point'.
  988. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  989. negates this setting for the duration of the command."
  990. :group 'org-link-store
  991. :type 'boolean)
  992. (defcustom org-keep-stored-link-after-insertion nil
  993. "Non-nil means, keep link in list for entire session.
  994. The command `org-store-link' adds a link pointing to the current
  995. location to an internal list. These links accumulate during a session.
  996. The command `org-insert-link' can be used to insert links into any
  997. Org-mode file (offering completion for all stored links). When this
  998. option is nil, every link which has been inserted once using \\[org-insert-link]
  999. will be removed from the list, to make completing the unused links
  1000. more efficient."
  1001. :group 'org-link-store
  1002. :type 'boolean)
  1003. (defcustom org-usenet-links-prefer-google nil
  1004. "Non-nil means, `org-store-link' will create web links to Google groups.
  1005. When nil, Gnus will be used for such links.
  1006. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1007. negates this setting for the duration of the command."
  1008. :group 'org-link-store
  1009. :type 'boolean)
  1010. (defgroup org-link-follow nil
  1011. "Options concerning following links in Org-mode"
  1012. :tag "Org Follow Link"
  1013. :group 'org-link)
  1014. (defcustom org-tab-follows-link nil
  1015. "Non-nil means, on links TAB will follow the link.
  1016. Needs to be set before org.el is loaded."
  1017. :group 'org-link-follow
  1018. :type 'boolean)
  1019. (defcustom org-return-follows-link nil
  1020. "Non-nil means, on links RET will follow the link.
  1021. Needs to be set before org.el is loaded."
  1022. :group 'org-link-follow
  1023. :type 'boolean)
  1024. (defcustom org-mouse-1-follows-link t
  1025. "Non-nil means, mouse-1 on a link will follow the link.
  1026. A longer mouse click will still set point. Does not wortk on XEmacs.
  1027. Needs to be set before org.el is loaded."
  1028. :group 'org-link-follow
  1029. :type 'boolean)
  1030. (defcustom org-mark-ring-length 4
  1031. "Number of different positions to be recorded in the ring
  1032. Changing this requires a restart of Emacs to work correctly."
  1033. :group 'org-link-follow
  1034. :type 'interger)
  1035. (defcustom org-link-frame-setup
  1036. '((vm . vm-visit-folder-other-frame)
  1037. (gnus . gnus-other-frame)
  1038. (file . find-file-other-window))
  1039. "Setup the frame configuration for following links.
  1040. When following a link with Emacs, it may often be useful to display
  1041. this link in another window or frame. This variable can be used to
  1042. set this up for the different types of links.
  1043. For VM, use any of
  1044. `vm-visit-folder'
  1045. `vm-visit-folder-other-frame'
  1046. For Gnus, use any of
  1047. `gnus'
  1048. `gnus-other-frame'
  1049. For FILE, use any of
  1050. `find-file'
  1051. `find-file-other-window'
  1052. `find-file-other-frame'
  1053. For the calendar, use the variable `calendar-setup'.
  1054. For BBDB, it is currently only possible to display the matches in
  1055. another window."
  1056. :group 'org-link-follow
  1057. :type '(list
  1058. (cons (const vm)
  1059. (choice
  1060. (const vm-visit-folder)
  1061. (const vm-visit-folder-other-window)
  1062. (const vm-visit-folder-other-frame)))
  1063. (cons (const gnus)
  1064. (choice
  1065. (const gnus)
  1066. (const gnus-other-frame)))
  1067. (cons (const file)
  1068. (choice
  1069. (const find-file)
  1070. (const find-file-other-window)
  1071. (const find-file-other-frame)))))
  1072. (defcustom org-display-internal-link-with-indirect-buffer nil
  1073. "Non-nil means, use indirect buffer to display infile links.
  1074. Activating internal links (from one location in a file to another location
  1075. in the same file) normally just jumps to the location. When the link is
  1076. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1077. another window. When this option is set, the other window actually displays
  1078. an indirect buffer clone of the current buffer, to avoid any visibility
  1079. changes to the current buffer."
  1080. :group 'org-link-follow
  1081. :type 'boolean)
  1082. (defcustom org-open-non-existing-files nil
  1083. "Non-nil means, `org-open-file' will open non-existing files.
  1084. When nil, an error will be generated."
  1085. :group 'org-link-follow
  1086. :type 'boolean)
  1087. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1088. "Function and arguments to call for following mailto links.
  1089. This is a list with the first element being a lisp function, and the
  1090. remaining elements being arguments to the function. In string arguments,
  1091. %a will be replaced by the address, and %s will be replaced by the subject
  1092. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1093. :group 'org-link-follow
  1094. :type '(choice
  1095. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1096. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1097. (const :tag "message-mail" (message-mail "%a" "%s"))
  1098. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1099. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1100. "Non-nil means, ask for confirmation before executing shell links.
  1101. Shell links can be dangerous: just think about a link
  1102. [[shell:rm -rf ~/*][Google Search]]
  1103. This link would show up in your Org-mode document as \"Google Search\",
  1104. but really it would remove your entire home directory.
  1105. Therefore we advise against setting this variable to nil.
  1106. Just change it to `y-or-n-p' of you want to confirm with a
  1107. single keystroke rather than having to type \"yes\"."
  1108. :group 'org-link-follow
  1109. :type '(choice
  1110. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1111. (const :tag "with y-or-n (faster)" y-or-n-p)
  1112. (const :tag "no confirmation (dangerous)" nil)))
  1113. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1114. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1115. Elisp links can be dangerous: just think about a link
  1116. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1117. This link would show up in your Org-mode document as \"Google Search\",
  1118. but really it would remove your entire home directory.
  1119. Therefore we advise against setting this variable to nil.
  1120. Just change it to `y-or-n-p' of you want to confirm with a
  1121. single keystroke rather than having to type \"yes\"."
  1122. :group 'org-link-follow
  1123. :type '(choice
  1124. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1125. (const :tag "with y-or-n (faster)" y-or-n-p)
  1126. (const :tag "no confirmation (dangerous)" nil)))
  1127. (defconst org-file-apps-defaults-gnu
  1128. '((remote . emacs)
  1129. (t . mailcap))
  1130. "Default file applications on a UNIX or GNU/Linux system.
  1131. See `org-file-apps'.")
  1132. (defconst org-file-apps-defaults-macosx
  1133. '((remote . emacs)
  1134. (t . "open %s")
  1135. ("ps" . "gv %s")
  1136. ("ps.gz" . "gv %s")
  1137. ("eps" . "gv %s")
  1138. ("eps.gz" . "gv %s")
  1139. ("dvi" . "xdvi %s")
  1140. ("fig" . "xfig %s"))
  1141. "Default file applications on a MacOS X system.
  1142. The system \"open\" is known as a default, but we use X11 applications
  1143. for some files for which the OS does not have a good default.
  1144. See `org-file-apps'.")
  1145. (defconst org-file-apps-defaults-windowsnt
  1146. (list
  1147. '(remote . emacs)
  1148. (cons t
  1149. (list (if (featurep 'xemacs)
  1150. 'mswindows-shell-execute
  1151. 'w32-shell-execute)
  1152. "open" 'file)))
  1153. "Default file applications on a Windows NT system.
  1154. The system \"open\" is used for most files.
  1155. See `org-file-apps'.")
  1156. (defcustom org-file-apps
  1157. '(
  1158. ("txt" . emacs)
  1159. ("tex" . emacs)
  1160. ("ltx" . emacs)
  1161. ("org" . emacs)
  1162. ("el" . emacs)
  1163. ("bib" . emacs)
  1164. )
  1165. "External applications for opening `file:path' items in a document.
  1166. Org-mode uses system defaults for different file types, but
  1167. you can use this variable to set the application for a given file
  1168. extension. The entries in this list are cons cells where the car identifies
  1169. files and the cdr the corresponding command. Possible values for the
  1170. file identifier are
  1171. \"ext\" A string identifying an extension
  1172. `directory' Matches a directory
  1173. `remote' Matches a remote file, accessible through tramp or efs.
  1174. Remote files most likely should be visited through Emacs
  1175. because external applications cannot handle such paths.
  1176. t Default for all remaining files
  1177. Possible values for the command are:
  1178. `emacs' The file will be visited by the current Emacs process.
  1179. `default' Use the default application for this file type.
  1180. string A command to be executed by a shell; %s will be replaced
  1181. by the path to the file.
  1182. sexp A Lisp form which will be evaluated. The file path will
  1183. be available in the Lisp variable `file'.
  1184. For more examples, see the system specific constants
  1185. `org-file-apps-defaults-macosx'
  1186. `org-file-apps-defaults-windowsnt'
  1187. `org-file-apps-defaults-gnu'."
  1188. :group 'org-link-follow
  1189. :type '(repeat
  1190. (cons (choice :value ""
  1191. (string :tag "Extension")
  1192. (const :tag "Default for unrecognized files" t)
  1193. (const :tag "Remote file" remote)
  1194. (const :tag "Links to a directory" directory))
  1195. (choice :value ""
  1196. (const :tag "Visit with Emacs" emacs)
  1197. (const :tag "Use system default" default)
  1198. (string :tag "Command")
  1199. (sexp :tag "Lisp form")))))
  1200. (defcustom org-mhe-search-all-folders nil
  1201. "Non-nil means, that the search for the mh-message will be extended to
  1202. all folders if the message cannot be found in the folder given in the link.
  1203. Searching all folders is very efficient with one of the search engines
  1204. supported by MH-E, but will be slow with pick."
  1205. :group 'org-link-follow
  1206. :type 'boolean)
  1207. (defgroup org-remember nil
  1208. "Options concerning interaction with remember.el."
  1209. :tag "Org Remember"
  1210. :group 'org)
  1211. (defcustom org-directory "~/org"
  1212. "Directory with org files.
  1213. This directory will be used as default to prompt for org files.
  1214. Used by the hooks for remember.el."
  1215. :group 'org-remember
  1216. :type 'directory)
  1217. (defcustom org-default-notes-file "~/.notes"
  1218. "Default target for storing notes.
  1219. Used by the hooks for remember.el. This can be a string, or nil to mean
  1220. the value of `remember-data-file'.
  1221. You can set this on a per-template basis with the variable
  1222. `org-remember-templates'."
  1223. :group 'org-remember
  1224. :type '(choice
  1225. (const :tag "Default from remember-data-file" nil)
  1226. file))
  1227. (defcustom org-remember-store-without-prompt t
  1228. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1229. In this case, you need `C-u C-c C-c' to get the prompts for
  1230. note file and headline.
  1231. When this variable is nil, `C-c C-c' give you the prompts, and
  1232. `C-u C-c C-c' trigger the fasttrack."
  1233. :group 'org-remember
  1234. :type 'boolean)
  1235. (defcustom org-remember-default-headline ""
  1236. "The headline that should be the default location in the notes file.
  1237. When filing remember notes, the cursor will start at that position.
  1238. You can set this on a per-template basis with the variable
  1239. `org-remember-templates'."
  1240. :group 'org-remember
  1241. :type 'string)
  1242. (defcustom org-remember-templates nil
  1243. "Templates for the creation of remember buffers.
  1244. When nil, just let remember make the buffer.
  1245. When not nil, this is a list of 5-element lists. In each entry, the first
  1246. element is a the name of the template, It should be a single short word.
  1247. The second element is a character, a unique key to select this template.
  1248. The third element is the template. The forth element is optional and can
  1249. specify a destination file for remember items created with this template.
  1250. The default file is given by `org-default-notes-file'. An optional fifth
  1251. element can specify the headline in that file that should be offered
  1252. first when the user is asked to file the entry. The default headline is
  1253. given in the variable `org-remember-default-headline'.
  1254. The template specifies the structure of the remember buffer. It should have
  1255. a first line starting with a star, to act as the org-mode headline.
  1256. Furthermore, the following %-escapes will be replaced with content:
  1257. %^{prompt} prompt the user for a string and replace this sequence with it.
  1258. %t time stamp, date only
  1259. %T time stamp with date and time
  1260. %u, %U like the above, but inactive time stamps
  1261. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1262. You may define a prompt like %^{Please specify birthday}t
  1263. %n user name (taken from `user-full-name')
  1264. %a annotation, normally the link created with org-store-link
  1265. %i initial content, the region when remember is called with C-u.
  1266. If %i is indented, the entire inserted text will be indented
  1267. as well.
  1268. %? After completing the template, position cursor here.
  1269. Apart from these general escapes, you can access information specific to the
  1270. link type that is created. For example, calling `remember' in emails or gnus
  1271. will record the author and the subject of the message, which you can access
  1272. with %:author and %:subject, respectively. Here is a complete list of what
  1273. is recorded for each link type.
  1274. Link type | Available information
  1275. -------------------+------------------------------------------------------
  1276. bbdb | %:type %:name %:company
  1277. vm, wl, mh, rmail | %:type %:subject %:message-id
  1278. | %:from %:fromname %:fromaddress
  1279. | %:to %:toname %:toaddress
  1280. | %:fromto (either \"to NAME\" or \"from NAME\")
  1281. gnus | %:group, for messages also all email fields
  1282. w3, w3m | %:type %:url
  1283. info | %:type %:file %:node
  1284. calendar | %:type %:date"
  1285. :group 'org-remember
  1286. :get (lambda (var) ; Make sure all entries have 5 elements
  1287. (mapcar (lambda (x)
  1288. (if (not (stringp (car x))) (setq x (cons "" x)))
  1289. (cond ((= (length x) 4) (append x '("")))
  1290. ((= (length x) 3) (append x '("" "")))
  1291. (t x)))
  1292. (default-value var)))
  1293. :type '(repeat
  1294. :tag "enabled"
  1295. (list :value ("" ?a "\n" nil nil)
  1296. (string :tag "Name")
  1297. (character :tag "Selection Key")
  1298. (string :tag "Template")
  1299. (choice
  1300. (file :tag "Destination file")
  1301. (const :tag "Prompt for file" nil))
  1302. (choice
  1303. (string :tag "Destination headline")
  1304. (const :tag "Selection interface for heading")))))
  1305. (defcustom org-reverse-note-order nil
  1306. "Non-nil means, store new notes at the beginning of a file or entry.
  1307. When nil, new notes will be filed to the end of a file or entry."
  1308. :group 'org-remember
  1309. :type '(choice
  1310. (const :tag "Reverse always" t)
  1311. (const :tag "Reverse never" nil)
  1312. (repeat :tag "By file name regexp"
  1313. (cons regexp boolean))))
  1314. (defgroup org-todo nil
  1315. "Options concerning TODO items in Org-mode."
  1316. :tag "Org TODO"
  1317. :group 'org)
  1318. (defgroup org-progress nil
  1319. "Options concerning Progress logging in Org-mode."
  1320. :tag "Org Progress"
  1321. :group 'org-time)
  1322. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1323. "List of TODO entry keyword sequences and their interpretation.
  1324. \\<org-mode-map>This is a list of sequences.
  1325. Each sequence starts with a symbol, either `sequence' or `type',
  1326. indicating if the keywords should be interpreted as a sequence of
  1327. action steps, or as different types of TODO items. The first
  1328. keywords are states requiring action - these states will select a headline
  1329. for inclusion into the global TODO list Org-mode produces. If one of
  1330. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1331. signify that no further action is necessary. If \"|\" is not found,
  1332. the last keyword is treated as the only DONE state of the sequence.
  1333. The command \\[org-todo] cycles an entry through these states, and one
  1334. additional state where no keyword is present. For details about this
  1335. cycling, see the manual.
  1336. TODO keywords and interpretation can also be set on a per-file basis with
  1337. the special #+SEQ_TODO and #+TYP_TODO lines.
  1338. For backward compatibility, this variable may also be just a list
  1339. of keywords - in this case the interptetation (sequence or type) will be
  1340. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1341. :group 'org-todo
  1342. :group 'org-keywords
  1343. :type '(choice
  1344. (repeat :tag "Old syntax, just keywords"
  1345. (string :tag "Keyword"))
  1346. (repeat :tag "New syntax"
  1347. (cons
  1348. (choice
  1349. :tag "Interpretation"
  1350. (const :tag "Sequence (cycling hits every state)" sequence)
  1351. (const :tag "Type (cycling directly to DONE)" type))
  1352. (repeat
  1353. (string :tag "Keyword"))))))
  1354. (defvar org-todo-keywords-1 nil)
  1355. (make-variable-buffer-local 'org-todo-keywords-1)
  1356. (defvar org-todo-keywords-for-agenda nil)
  1357. (defvar org-done-keywords-for-agenda nil)
  1358. (defvar org-not-done-keywords nil)
  1359. (make-variable-buffer-local 'org-not-done-keywords)
  1360. (defvar org-done-keywords nil)
  1361. (make-variable-buffer-local 'org-done-keywords)
  1362. (defvar org-todo-heads nil)
  1363. (make-variable-buffer-local 'org-todo-heads)
  1364. (defvar org-todo-sets nil)
  1365. (make-variable-buffer-local 'org-todo-sets)
  1366. (defvar org-todo-log-states nil)
  1367. (make-variable-buffer-local 'org-todo-log-states)
  1368. (defvar org-todo-kwd-alist nil)
  1369. (make-variable-buffer-local 'org-todo-kwd-alist)
  1370. (defvar org-todo-key-alist nil)
  1371. (make-variable-buffer-local 'org-todo-key-alist)
  1372. (defvar org-todo-key-trigger nil)
  1373. (make-variable-buffer-local 'org-todo-key-trigger)
  1374. (defcustom org-todo-interpretation 'sequence
  1375. "Controls how TODO keywords are interpreted.
  1376. This variable is in principle obsolete and is only used for
  1377. backward compatibility, if the interpretation of todo keywords is
  1378. not given already in `org-todo-keywords'. See that variable for
  1379. more information."
  1380. :group 'org-todo
  1381. :group 'org-keywords
  1382. :type '(choice (const sequence)
  1383. (const type)))
  1384. (defcustom org-use-fast-todo-selection 'prefix
  1385. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1386. This variable describes if and under what circumstances the cycling
  1387. mechanism for TODO keywords will be replaced by a single-key, direct
  1388. selection scheme.
  1389. When nil, fast selection is never used.
  1390. When the symbol `prefix', it will be used when `org-todo' is called with
  1391. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1392. in an agenda buffer.
  1393. When t, fast selection is used by default. In this case, the prefix
  1394. argument forces cycling instead.
  1395. In all cases, the special interface is only used if access keys have actually
  1396. been assigned by the user, i.e. if keywords in the configuration are followed
  1397. by a letter in parenthesis, like TODO(t)."
  1398. :group 'org-todo
  1399. :type '(choice
  1400. (const :tag "Never" nil)
  1401. (const :tag "By default" t)
  1402. (const :tag "Only with C-u C-c C-t" prefix)))
  1403. (defcustom org-after-todo-state-change-hook nil
  1404. "Hook which is run after the state of a TODO item was changed.
  1405. The new state (a string with a TODO keyword, or nil) is available in the
  1406. Lisp variable `state'."
  1407. :group 'org-todo
  1408. :type 'hook)
  1409. (defcustom org-log-done nil
  1410. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1411. When the state of an entry is changed from nothing or a DONE state to
  1412. a not-done TODO state, remove a previous closing date.
  1413. This can also be a list of symbols indicating under which conditions
  1414. the time stamp recording the action should be annotated with a short note.
  1415. Valid members of this list are
  1416. done Offer to record a note when marking entries done
  1417. state Offer to record a note whenever changing the TODO state
  1418. of an item. This is only relevant if TODO keywords are
  1419. interpreted as sequence, see variable `org-todo-interpretation'.
  1420. When `state' is set, this includes tracking `done'.
  1421. clock-out Offer to record a note when clocking out of an item.
  1422. A separate window will then pop up and allow you to type a note.
  1423. After finishing with C-c C-c, the note will be added directly after the
  1424. timestamp, as a plain list item. See also the variable
  1425. `org-log-note-headings'.
  1426. Logging can also be configured on a per-file basis by adding one of
  1427. the following lines anywhere in the buffer:
  1428. #+STARTUP: logdone
  1429. #+STARTUP: nologging
  1430. #+STARTUP: lognotedone
  1431. #+STARTUP: lognotestate
  1432. #+STARTUP: lognoteclock-out
  1433. You can have local logging settings for a subtree by setting the LOGGING
  1434. property to one or more of these keywords."
  1435. :group 'org-todo
  1436. :group 'org-progress
  1437. :type '(choice
  1438. (const :tag "off" nil)
  1439. (const :tag "on" t)
  1440. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1441. (const :tag "when item is marked DONE" done)
  1442. (const :tag "when TODO state changes" state)
  1443. (const :tag "when clocking out" clock-out))))
  1444. (defcustom org-log-done-with-time t
  1445. "Non-nil means, the CLOSED time stamp will contain date and time.
  1446. When nil, only the date will be recorded."
  1447. :group 'org-progress
  1448. :type 'boolean)
  1449. (defcustom org-log-note-headings
  1450. '((done . "CLOSING NOTE %t")
  1451. (state . "State %-12s %t")
  1452. (clock-out . ""))
  1453. "Headings for notes added when clocking out or closing TODO items.
  1454. The value is an alist, with the car being a symbol indicating the note
  1455. context, and the cdr is the heading to be used. The heading may also be the
  1456. empty string.
  1457. %t in the heading will be replaced by a time stamp.
  1458. %s will be replaced by the new TODO state, in double quotes.
  1459. %u will be replaced by the user name.
  1460. %U will be replaced by the full user name."
  1461. :group 'org-todo
  1462. :group 'org-progress
  1463. :type '(list :greedy t
  1464. (cons (const :tag "Heading when closing an item" done) string)
  1465. (cons (const :tag
  1466. "Heading when changing todo state (todo sequence only)"
  1467. state) string)
  1468. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1469. (defcustom org-log-states-order-reversed t
  1470. "Non-nil means, the latest state change note will be directly after heading.
  1471. When nil, the notes will be orderer according to time."
  1472. :group 'org-todo
  1473. :group 'org-progress
  1474. :type 'boolean)
  1475. (defcustom org-log-repeat t
  1476. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1477. When nil, no note will be taken.
  1478. This option can also be set with on a per-file-basis with
  1479. #+STARTUP: logrepeat
  1480. #+STARTUP: nologrepeat
  1481. You can have local logging settings for a subtree by setting the LOGGING
  1482. property to one or more of these keywords."
  1483. :group 'org-todo
  1484. :group 'org-progress
  1485. :type 'boolean)
  1486. (defcustom org-clock-into-drawer 2
  1487. "Should clocking info be wrapped into a drawer?
  1488. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1489. If necessary, the drawer will be created.
  1490. When nil, the drawer will not be created, but used when present.
  1491. When an integer and the number of clocking entries in an item
  1492. reaches or exceeds this number, a drawer will be created."
  1493. :group 'org-todo
  1494. :group 'org-progress
  1495. :type '(choice
  1496. (const :tag "Always" t)
  1497. (const :tag "Only when drawer exists" nil)
  1498. (integer :tag "When at least N clock entries")))
  1499. (defcustom org-clock-out-when-done t
  1500. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1501. Nil means, clock will keep running until stopped explicitly with
  1502. `C-c C-x C-o', or until the clock is started in a different item."
  1503. :group 'org-progress
  1504. :type 'boolean)
  1505. (defgroup org-priorities nil
  1506. "Priorities in Org-mode."
  1507. :tag "Org Priorities"
  1508. :group 'org-todo)
  1509. (defcustom org-highest-priority ?A
  1510. "The highest priority of TODO items. A character like ?A, ?B etc.
  1511. Must have a smaller ASCII number than `org-lowest-priority'."
  1512. :group 'org-priorities
  1513. :type 'character)
  1514. (defcustom org-lowest-priority ?C
  1515. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1516. Must have a larger ASCII number than `org-highest-priority'."
  1517. :group 'org-priorities
  1518. :type 'character)
  1519. (defcustom org-default-priority ?B
  1520. "The default priority of TODO items.
  1521. This is the priority an item get if no explicit priority is given."
  1522. :group 'org-priorities
  1523. :type 'character)
  1524. (defcustom org-priority-start-cycle-with-default t
  1525. "Non-nil means, start with default priority when starting to cycle.
  1526. When this is nil, the first step in the cycle will be (depending on the
  1527. command used) one higher or lower that the default priority."
  1528. :group 'org-priorities
  1529. :type 'boolean)
  1530. (defgroup org-time nil
  1531. "Options concerning time stamps and deadlines in Org-mode."
  1532. :tag "Org Time"
  1533. :group 'org)
  1534. (defcustom org-insert-labeled-timestamps-at-point nil
  1535. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1536. When nil, these labeled time stamps are forces into the second line of an
  1537. entry, just after the headline. When scheduling from the global TODO list,
  1538. the time stamp will always be forced into the second line."
  1539. :group 'org-time
  1540. :type 'boolean)
  1541. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1542. "Formats for `format-time-string' which are used for time stamps.
  1543. It is not recommended to change this constant.")
  1544. (defcustom org-time-stamp-rounding-minutes 0
  1545. "Number of minutes to round time stamps to upon insertion.
  1546. When zero, insert the time unmodified. Useful rounding numbers
  1547. should be factors of 60, so for example 5, 10, 15.
  1548. When this is not zero, you can still force an exact time-stamp by using
  1549. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1550. :group 'org-time
  1551. :type 'integer)
  1552. (defcustom org-display-custom-times nil
  1553. "Non-nil means, overlay custom formats over all time stamps.
  1554. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1555. To turn this on on a per-file basis, insert anywhere in the file:
  1556. #+STARTUP: customtime"
  1557. :group 'org-time
  1558. :set 'set-default
  1559. :type 'sexp)
  1560. (make-variable-buffer-local 'org-display-custom-times)
  1561. (defcustom org-time-stamp-custom-formats
  1562. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1563. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1564. These are overlayed over the default ISO format if the variable
  1565. `org-display-custom-times' is set. Time like %H:%M should be at the
  1566. end of the second format."
  1567. :group 'org-time
  1568. :type 'sexp)
  1569. (defun org-time-stamp-format (&optional long inactive)
  1570. "Get the right format for a time string."
  1571. (let ((f (if long (cdr org-time-stamp-formats)
  1572. (car org-time-stamp-formats))))
  1573. (if inactive
  1574. (concat "[" (substring f 1 -1) "]")
  1575. f)))
  1576. (defcustom org-popup-calendar-for-date-prompt t
  1577. "Non-nil means, pop up a calendar when prompting for a date.
  1578. In the calendar, the date can be selected with mouse-1. However, the
  1579. minibuffer will also be active, and you can simply enter the date as well.
  1580. When nil, only the minibuffer will be available."
  1581. :group 'org-time
  1582. :type 'boolean)
  1583. (defcustom org-edit-timestamp-down-means-later nil
  1584. "Non-nil means, S-down will increase the time in a time stamp.
  1585. When nil, S-up will increase."
  1586. :group 'org-time
  1587. :type 'boolean)
  1588. (defcustom org-calendar-follow-timestamp-change t
  1589. "Non-nil means, make the calendar window follow timestamp changes.
  1590. When a timestamp is modified and the calendar window is visible, it will be
  1591. moved to the new date."
  1592. :group 'org-time
  1593. :type 'boolean)
  1594. (defgroup org-tags nil
  1595. "Options concerning tags in Org-mode."
  1596. :tag "Org Tags"
  1597. :group 'org)
  1598. (defcustom org-tag-alist nil
  1599. "List of tags allowed in Org-mode files.
  1600. When this list is nil, Org-mode will base TAG input on what is already in the
  1601. buffer.
  1602. The value of this variable is an alist, the car of each entry must be a
  1603. keyword as a string, the cdr may be a character that is used to select
  1604. that tag through the fast-tag-selection interface.
  1605. See the manual for details."
  1606. :group 'org-tags
  1607. :type '(repeat
  1608. (choice
  1609. (cons (string :tag "Tag name")
  1610. (character :tag "Access char"))
  1611. (const :tag "Start radio group" (:startgroup))
  1612. (const :tag "End radio group" (:endgroup)))))
  1613. (defcustom org-use-fast-tag-selection 'auto
  1614. "Non-nil means, use fast tag selection scheme.
  1615. This is a special interface to select and deselect tags with single keys.
  1616. When nil, fast selection is never used.
  1617. When the symbol `auto', fast selection is used if and only if selection
  1618. characters for tags have been configured, either through the variable
  1619. `org-tag-alist' or through a #+TAGS line in the buffer.
  1620. When t, fast selection is always used and selection keys are assigned
  1621. automatically if necessary."
  1622. :group 'org-tags
  1623. :type '(choice
  1624. (const :tag "Always" t)
  1625. (const :tag "Never" nil)
  1626. (const :tag "When selection characters are configured" 'auto)))
  1627. (defcustom org-fast-tag-selection-single-key nil
  1628. "Non-nil means, fast tag selection exits after first change.
  1629. When nil, you have to press RET to exit it.
  1630. During fast tag selection, you can toggle this flag with `C-c'.
  1631. This variable can also have the value `expert'. In this case, the window
  1632. displaying the tags menu is not even shown, until you press C-c again."
  1633. :group 'org-tags
  1634. :type '(choice
  1635. (const :tag "No" nil)
  1636. (const :tag "Yes" t)
  1637. (const :tag "Expert" expert)))
  1638. (defvar org-fast-tag-selection-include-todo nil
  1639. "Non-nil means, fast tags selection interface will also offer TODO states.
  1640. This is an undocumented feature, you should not rely on it.")
  1641. (defcustom org-tags-column -80
  1642. "The column to which tags should be indented in a headline.
  1643. If this number is positive, it specifies the column. If it is negative,
  1644. it means that the tags should be flushright to that column. For example,
  1645. -80 works well for a normal 80 character screen."
  1646. :group 'org-tags
  1647. :type 'integer)
  1648. (defcustom org-auto-align-tags t
  1649. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1650. These operations change the length of a headline and therefore shift
  1651. the tags around. With this options turned on, after each such operation
  1652. the tags are again aligned to `org-tags-column'."
  1653. :group 'org-tags
  1654. :type 'boolean)
  1655. (defcustom org-use-tag-inheritance t
  1656. "Non-nil means, tags in levels apply also for sublevels.
  1657. When nil, only the tags directly given in a specific line apply there.
  1658. If you turn off this option, you very likely want to turn on the
  1659. companion option `org-tags-match-list-sublevels'."
  1660. :group 'org-tags
  1661. :type 'boolean)
  1662. (defcustom org-tags-match-list-sublevels nil
  1663. "Non-nil means list also sublevels of headlines matching tag search.
  1664. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1665. the sublevels of a headline matching a tag search often also match
  1666. the same search. Listing all of them can create very long lists.
  1667. Setting this variable to nil causes subtrees of a match to be skipped.
  1668. This option is off by default, because inheritance in on. If you turn
  1669. inheritance off, you very likely want to turn this option on.
  1670. As a special case, if the tag search is restricted to TODO items, the
  1671. value of this variable is ignored and sublevels are always checked, to
  1672. make sure all corresponding TODO items find their way into the list."
  1673. :group 'org-tags
  1674. :type 'boolean)
  1675. (defvar org-tags-history nil
  1676. "History of minibuffer reads for tags.")
  1677. (defvar org-last-tags-completion-table nil
  1678. "The last used completion table for tags.")
  1679. (defvar org-after-tags-change-hook nil
  1680. "Hook that is run after the tags in a line have changed.")
  1681. (defgroup org-properties nil
  1682. "Options concerning properties in Org-mode."
  1683. :tag "Org Properties"
  1684. :group 'org)
  1685. (defcustom org-property-format "%-10s %s"
  1686. "How property key/value pairs should be formatted by `indent-line'.
  1687. When `indent-line' hits a property definition, it will format the line
  1688. according to this format, mainly to make sure that the values are
  1689. lined-up with respect to each other."
  1690. :group 'org-properties
  1691. :type 'string)
  1692. (defcustom org-use-property-inheritance nil
  1693. "Non-nil means, properties apply also for sublevels.
  1694. This can cause significant overhead when doing a search, so this is turned
  1695. off by default.
  1696. When nil, only the properties directly given in the current entry count.
  1697. However, note that some special properties use inheritance under special
  1698. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1699. and the properties ending in \"_ALL\" when they are used as descriptor
  1700. for valid values of a property."
  1701. :group 'org-properties
  1702. :type 'boolean)
  1703. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1704. "The default column format, if no other format has been defined.
  1705. This variable can be set on the per-file basis by inserting a line
  1706. #+COLUMNS: %25ITEM ....."
  1707. :group 'org-properties
  1708. :type 'string)
  1709. (defcustom org-global-properties nil
  1710. "List of property/value pairs that can be inherited by any entry.
  1711. You can set buffer-local values for this by adding lines like
  1712. #+PROPERTY: NAME VALUE"
  1713. :group 'org-properties
  1714. :type '(repeat
  1715. (cons (string :tag "Property")
  1716. (string :tag "Value"))))
  1717. (defvar org-local-properties nil
  1718. "List of property/value pairs that can be inherited by any entry.
  1719. Valid for the current buffer.
  1720. This variable is populated from #+PROPERTY lines.")
  1721. (defgroup org-agenda nil
  1722. "Options concerning agenda views in Org-mode."
  1723. :tag "Org Agenda"
  1724. :group 'org)
  1725. (defvar org-category nil
  1726. "Variable used by org files to set a category for agenda display.
  1727. Such files should use a file variable to set it, for example
  1728. # -*- mode: org; org-category: \"ELisp\"
  1729. or contain a special line
  1730. #+CATEGORY: ELisp
  1731. If the file does not specify a category, then file's base name
  1732. is used instead.")
  1733. (make-variable-buffer-local 'org-category)
  1734. (defcustom org-agenda-files nil
  1735. "The files to be used for agenda display.
  1736. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1737. \\[org-remove-file]. You can also use customize to edit the list.
  1738. If an entry is a directory, all files in that directory that are matched by
  1739. `org-agenda-file-regexp' will be part of the file list.
  1740. If the value of the variable is not a list but a single file name, then
  1741. the list of agenda files is actually stored and maintained in that file, one
  1742. agenda file per line."
  1743. :group 'org-agenda
  1744. :type '(choice
  1745. (repeat :tag "List of files and directories" file)
  1746. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1747. (defcustom org-agenda-file-regexp "\\.org\\'"
  1748. "Regular expression to match files for `org-agenda-files'.
  1749. If any element in the list in that variable contains a directory instead
  1750. of a normal file, all files in that directory that are matched by this
  1751. regular expression will be included."
  1752. :group 'org-agenda
  1753. :type 'regexp)
  1754. (defcustom org-agenda-skip-unavailable-files nil
  1755. "t means to just skip non-reachable files in `org-agenda-files'.
  1756. Nil means to remove them, after a query, from the list."
  1757. :group 'org-agenda
  1758. :type 'boolean)
  1759. (defcustom org-agenda-multi-occur-extra-files nil
  1760. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1761. The files in `org-agenda-files' are always searched."
  1762. :group 'org-agenda
  1763. :type '(repeat file))
  1764. (defcustom org-agenda-confirm-kill 1
  1765. "When set, remote killing from the agenda buffer needs confirmation.
  1766. When t, a confirmation is always needed. When a number N, confirmation is
  1767. only needed when the text to be killed contains more than N non-white lines."
  1768. :group 'org-agenda
  1769. :type '(choice
  1770. (const :tag "Never" nil)
  1771. (const :tag "Always" t)
  1772. (number :tag "When more than N lines")))
  1773. (defcustom org-calendar-to-agenda-key [?c]
  1774. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1775. The command `org-calendar-goto-agenda' will be bound to this key. The
  1776. default is the character `c' because then `c' can be used to switch back and
  1777. forth between agenda and calendar."
  1778. :group 'org-agenda
  1779. :type 'sexp)
  1780. (defcustom org-agenda-compact-blocks nil
  1781. "Non-nil means, make the block agenda more compact.
  1782. This is done by leaving out unnecessary lines."
  1783. :group 'org-agenda
  1784. :type nil)
  1785. (defgroup org-agenda-export nil
  1786. "Options concerning exporting agenda views in Org-mode."
  1787. :tag "Org Agenda Export"
  1788. :group 'org-agenda)
  1789. (defcustom org-agenda-with-colors t
  1790. "Non-nil means, use colors in agenda views."
  1791. :group 'org-agenda-export
  1792. :type 'boolean)
  1793. (defcustom org-agenda-exporter-settings nil
  1794. "Alist of variable/value pairs that should be active during agenda export.
  1795. This is a good place to set uptions for ps-print and for htmlize."
  1796. :group 'org-agenda-export
  1797. :type '(repeat
  1798. (list
  1799. (variable)
  1800. (sexp :tag "Value"))))
  1801. (defcustom org-agenda-export-html-style ""
  1802. "The style specification for exported HTML Agenda files.
  1803. If this variable contains a string, it will replace the default <style>
  1804. section as produced by `htmlize'.
  1805. Since there are different ways of setting style information, this variable
  1806. needs to contain the full HTML structure to provide a style, including the
  1807. surrounding HTML tags. The style specifications should include definitions
  1808. the fonts used by the agenda, here is an example:
  1809. <style type=\"text/css\">
  1810. p { font-weight: normal; color: gray; }
  1811. .org-agenda-structure {
  1812. font-size: 110%;
  1813. color: #003399;
  1814. font-weight: 600;
  1815. }
  1816. .org-todo {
  1817. color: #cc6666;Week-agenda:
  1818. font-weight: bold;
  1819. }
  1820. .org-done {
  1821. color: #339933;
  1822. }
  1823. .title { text-align: center; }
  1824. .todo, .deadline { color: red; }
  1825. .done { color: green; }
  1826. </style>
  1827. or, if you want to keep the style in a file,
  1828. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1829. As the value of this option simply gets inserted into the HTML <head> header,
  1830. you can \"misuse\" it to also add other text to the header. However,
  1831. <style>...</style> is required, if not present the variable will be ignored."
  1832. :group 'org-agenda-export
  1833. :group 'org-export-html
  1834. :type 'string)
  1835. (defgroup org-agenda-custom-commands nil
  1836. "Options concerning agenda views in Org-mode."
  1837. :tag "Org Agenda Custom Commands"
  1838. :group 'org-agenda)
  1839. (defcustom org-agenda-custom-commands nil
  1840. "Custom commands for the agenda.
  1841. These commands will be offered on the splash screen displayed by the
  1842. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1843. (key desc type match options files)
  1844. key The key (one or more characters as a string) to be associated
  1845. with the command.
  1846. desc A description of the commend, when omitted or nil, a default
  1847. description is built using MATCH.
  1848. type The command type, any of the following symbols:
  1849. todo Entries with a specific TODO keyword, in all agenda files.
  1850. tags Tags match in all agenda files.
  1851. tags-todo Tags match in all agenda files, TODO entries only.
  1852. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1853. tags-tree Sparse tree with all tags matches in *current* file.
  1854. occur-tree Occur sparse tree for *current* file.
  1855. ... A user-defined function.
  1856. match What to search for:
  1857. - a single keyword for TODO keyword searches
  1858. - a tags match expression for tags searches
  1859. - a regular expression for occur searches
  1860. options A list of option settings, similar to that in a let form, so like
  1861. this: ((opt1 val1) (opt2 val2) ...)
  1862. files A list of files file to write the produced agenda buffer to
  1863. with the command `org-store-agenda-views'.
  1864. If a file name ends in \".html\", an HTML version of the buffer
  1865. is written out. If it ends in \".ps\", a postscript version is
  1866. produced. Otherwide, only the plain text is written to the file.
  1867. You can also define a set of commands, to create a composite agenda buffer.
  1868. In this case, an entry looks like this:
  1869. (key desc (cmd1 cmd2 ...) general-options file)
  1870. where
  1871. desc A description string to be displayed in the dispatcher menu.
  1872. cmd An agenda command, similar to the above. However, tree commands
  1873. are no allowed, but instead you can get agenda and global todo list.
  1874. So valid commands for a set are:
  1875. (agenda)
  1876. (alltodo)
  1877. (stuck)
  1878. (todo \"match\" options files)
  1879. (tags \"match\" options files)
  1880. (tags-todo \"match\" options files)
  1881. Each command can carry a list of options, and another set of options can be
  1882. given for the whole set of commands. Individual command options take
  1883. precedence over the general options.
  1884. When using several characters as key to a command, the first characters
  1885. are prefix commands. For the dispatcher to display useful information, you
  1886. should provide a description for the prefix, like
  1887. (setq org-agenda-custom-commands
  1888. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  1889. (\"hl\" tags \"+HOME+Lisa\")
  1890. (\"hp\" tags \"+HOME+Peter\")
  1891. (\"hk\" tags \"+HOME+Kim\")))"
  1892. :group 'org-agenda-custom-commands
  1893. :type '(repeat
  1894. (choice :value ("a" "" tags "" nil)
  1895. (list :tag "Single command"
  1896. (string :tag "Access Key(s) ")
  1897. (option (string :tag "Description"))
  1898. (choice
  1899. (const :tag "Agenda" agenda)
  1900. (const :tag "TODO list" alltodo)
  1901. (const :tag "Stuck projects" stuck)
  1902. (const :tag "Tags search (all agenda files)" tags)
  1903. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1904. (const :tag "TODO keyword search (all agenda files)" todo)
  1905. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1906. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1907. (const :tag "Occur tree (current buffer)" occur-tree)
  1908. (sexp :tag "Other, user-defined function"))
  1909. (string :tag "Match")
  1910. (repeat :tag "Local options"
  1911. (list (variable :tag "Option") (sexp :tag "Value")))
  1912. (option (repeat :tag "Export" (file :tag "Export to"))))
  1913. (list :tag "Command series, all agenda files"
  1914. (string :tag "Access Key(s)")
  1915. (string :tag "Description ")
  1916. (repeat
  1917. (choice
  1918. (const :tag "Agenda" (agenda))
  1919. (const :tag "TODO list" (alltodo))
  1920. (const :tag "Stuck projects" (stuck))
  1921. (list :tag "Tags search"
  1922. (const :format "" tags)
  1923. (string :tag "Match")
  1924. (repeat :tag "Local options"
  1925. (list (variable :tag "Option")
  1926. (sexp :tag "Value"))))
  1927. (list :tag "Tags search, TODO entries only"
  1928. (const :format "" tags-todo)
  1929. (string :tag "Match")
  1930. (repeat :tag "Local options"
  1931. (list (variable :tag "Option")
  1932. (sexp :tag "Value"))))
  1933. (list :tag "TODO keyword search"
  1934. (const :format "" todo)
  1935. (string :tag "Match")
  1936. (repeat :tag "Local options"
  1937. (list (variable :tag "Option")
  1938. (sexp :tag "Value"))))
  1939. (list :tag "Other, user-defined function"
  1940. (symbol :tag "function")
  1941. (string :tag "Match")
  1942. (repeat :tag "Local options"
  1943. (list (variable :tag "Option")
  1944. (sexp :tag "Value"))))))
  1945. (repeat :tag "General options"
  1946. (list (variable :tag "Option")
  1947. (sexp :tag "Value")))
  1948. (option (repeat :tag "Export" (file :tag "Export to"))))
  1949. (cons :tag "Prefix key documentation"
  1950. (string :tag "Access Key(s)")
  1951. (string :tag "Description ")))))
  1952. (defcustom org-stuck-projects
  1953. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1954. "How to identify stuck projects.
  1955. This is a list of four items:
  1956. 1. A tags/todo matcher string that is used to identify a project.
  1957. The entire tree below a headline matched by this is considered one project.
  1958. 2. A list of TODO keywords identifying non-stuck projects.
  1959. If the project subtree contains any headline with one of these todo
  1960. keywords, the project is considered to be not stuck. If you specify
  1961. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1962. 3. A list of tags identifying non-stuck projects.
  1963. If the project subtree contains any headline with one of these tags,
  1964. the project is considered to be not stuck. If you specify \"*\" as
  1965. a tag, any tag will mark the project unstuck.
  1966. 4. An arbitrary regular expression matching non-stuck projects.
  1967. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1968. or `C-c a #' to produce the list."
  1969. :group 'org-agenda-custom-commands
  1970. :type '(list
  1971. (string :tag "Tags/TODO match to identify a project")
  1972. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1973. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1974. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1975. (defgroup org-agenda-skip nil
  1976. "Options concerning skipping parts of agenda files."
  1977. :tag "Org Agenda Skip"
  1978. :group 'org-agenda)
  1979. (defcustom org-agenda-todo-list-sublevels t
  1980. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1981. When nil, the sublevels of a TODO entry are not checked, resulting in
  1982. potentially much shorter TODO lists."
  1983. :group 'org-agenda-skip
  1984. :group 'org-todo
  1985. :type 'boolean)
  1986. (defcustom org-agenda-todo-ignore-with-date nil
  1987. "Non-nil means, don't show entries with a date in the global todo list.
  1988. You can use this if you prefer to mark mere appointments with a TODO keyword,
  1989. but don't want them to show up in the TODO list.
  1990. When this is set, it also covers deadlines and scheduled items, the settings
  1991. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  1992. will be ignored."
  1993. :group 'org-agenda-skip
  1994. :group 'org-todo
  1995. :type 'boolean)
  1996. (defcustom org-agenda-todo-ignore-scheduled nil
  1997. "Non-nil means, don't show scheduled entries in the global todo list.
  1998. The idea behind this is that by scheduling it, you have already taken care
  1999. of this item.
  2000. See also `org-agenda-todo-ignore-with-date'."
  2001. :group 'org-agenda-skip
  2002. :group 'org-todo
  2003. :type 'boolean)
  2004. (defcustom org-agenda-todo-ignore-deadlines nil
  2005. "Non-nil means, don't show near deadline entries in the global todo list.
  2006. Near means closer than `org-deadline-warning-days' days.
  2007. The idea behind this is that such items will appear in the agenda anyway.
  2008. See also `org-agenda-todo-ignore-with-date'."
  2009. :group 'org-agenda-skip
  2010. :group 'org-todo
  2011. :type 'boolean)
  2012. (defcustom org-agenda-skip-scheduled-if-done nil
  2013. "Non-nil means don't show scheduled items in agenda when they are done.
  2014. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2015. it applies only to the actual date of the scheduling. Warnings about
  2016. an item with a past scheduling dates are always turned off when the item
  2017. is DONE."
  2018. :group 'org-agenda-skip
  2019. :type 'boolean)
  2020. (defcustom org-agenda-skip-deadline-if-done nil
  2021. "Non-nil means don't show deadines when the corresponding item is done.
  2022. When nil, the deadline is still shown and should give you a happy feeling.
  2023. This is relevant for the daily/weekly agenda. And it applied only to the
  2024. actualy date of the deadline. Warnings about approching and past-due
  2025. deadlines are always turned off when the item is DONE."
  2026. :group 'org-agenda-skip
  2027. :type 'boolean)
  2028. (defcustom org-timeline-show-empty-dates 3
  2029. "Non-nil means, `org-timeline' also shows dates without an entry.
  2030. When nil, only the days which actually have entries are shown.
  2031. When t, all days between the first and the last date are shown.
  2032. When an integer, show also empty dates, but if there is a gap of more than
  2033. N days, just insert a special line indicating the size of the gap."
  2034. :group 'org-agenda-skip
  2035. :type '(choice
  2036. (const :tag "None" nil)
  2037. (const :tag "All" t)
  2038. (number :tag "at most")))
  2039. (defgroup org-agenda-startup nil
  2040. "Options concerning initial settings in the Agenda in Org Mode."
  2041. :tag "Org Agenda Startup"
  2042. :group 'org-agenda)
  2043. (defcustom org-finalize-agenda-hook nil
  2044. "Hook run just before displaying an agenda buffer."
  2045. :group 'org-agenda-startup
  2046. :type 'hook)
  2047. (defcustom org-agenda-mouse-1-follows-link nil
  2048. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2049. A longer mouse click will still set point. Does not wortk on XEmacs.
  2050. Needs to be set before org.el is loaded."
  2051. :group 'org-agenda-startup
  2052. :type 'boolean)
  2053. (defcustom org-agenda-start-with-follow-mode nil
  2054. "The initial value of follow-mode in a newly created agenda window."
  2055. :group 'org-agenda-startup
  2056. :type 'boolean)
  2057. (defgroup org-agenda-windows nil
  2058. "Options concerning the windows used by the Agenda in Org Mode."
  2059. :tag "Org Agenda Windows"
  2060. :group 'org-agenda)
  2061. (defcustom org-agenda-window-setup 'reorganize-frame
  2062. "How the agenda buffer should be displayed.
  2063. Possible values for this option are:
  2064. current-window Show agenda in the current window, keeping all other windows.
  2065. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2066. other-window Use `switch-to-buffer-other-window' to display agenda.
  2067. reorganize-frame Show only two windows on the current frame, the current
  2068. window and the agenda.
  2069. See also the variable `org-agenda-restore-windows-after-quit'."
  2070. :group 'org-agenda-windows
  2071. :type '(choice
  2072. (const current-window)
  2073. (const other-frame)
  2074. (const other-window)
  2075. (const reorganize-frame)))
  2076. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2077. "The min and max height of the agenda window as a fraction of frame height.
  2078. The value of the variable is a cons cell with two numbers between 0 and 1.
  2079. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2080. :group 'org-agenda-windows
  2081. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2082. (defcustom org-agenda-restore-windows-after-quit nil
  2083. "Non-nil means, restore window configuration open exiting agenda.
  2084. Before the window configuration is changed for displaying the agenda,
  2085. the current status is recorded. When the agenda is exited with
  2086. `q' or `x' and this option is set, the old state is restored. If
  2087. `org-agenda-window-setup' is `other-frame', the value of this
  2088. option will be ignored.."
  2089. :group 'org-agenda-windows
  2090. :type 'boolean)
  2091. (defcustom org-indirect-buffer-display 'other-window
  2092. "How should indirect tree buffers be displayed?
  2093. This applies to indirect buffers created with the commands
  2094. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2095. Valid values are:
  2096. current-window Display in the current window
  2097. other-window Just display in another window.
  2098. dedicated-frame Create one new frame, and re-use it each time.
  2099. new-frame Make a new frame each time."
  2100. :group 'org-structure
  2101. :group 'org-agenda-windows
  2102. :type '(choice
  2103. (const :tag "In current window" current-window)
  2104. (const :tag "In current frame, other window" other-window)
  2105. (const :tag "Each time a new frame" new-frame)
  2106. (const :tag "One dedicated frame" dedicated-frame)))
  2107. (defgroup org-agenda-daily/weekly nil
  2108. "Options concerning the daily/weekly agenda."
  2109. :tag "Org Agenda Daily/Weekly"
  2110. :group 'org-agenda)
  2111. (defcustom org-agenda-ndays 7
  2112. "Number of days to include in overview display.
  2113. Should be 1 or 7."
  2114. :group 'org-agenda-daily/weekly
  2115. :type 'number)
  2116. (defcustom org-agenda-start-on-weekday 1
  2117. "Non-nil means, start the overview always on the specified weekday.
  2118. 0 denotes Sunday, 1 denotes Monday etc.
  2119. When nil, always start on the current day."
  2120. :group 'org-agenda-daily/weekly
  2121. :type '(choice (const :tag "Today" nil)
  2122. (number :tag "Weekday No.")))
  2123. (defcustom org-agenda-show-all-dates t
  2124. "Non-nil means, `org-agenda' shows every day in the selected range.
  2125. When nil, only the days which actually have entries are shown."
  2126. :group 'org-agenda-daily/weekly
  2127. :type 'boolean)
  2128. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2129. "Format string for displaying dates in the agenda.
  2130. Used by the daily/weekly agenda and by the timeline. This should be
  2131. a format string understood by `format-time-string', or a function returning
  2132. the formatted date as a string. The function must take a single argument,
  2133. a calendar-style date list like (month day year)."
  2134. :group 'org-agenda-daily/weekly
  2135. :type '(choice
  2136. (string :tag "Format string")
  2137. (function :tag "Function")))
  2138. (defun org-agenda-format-date-aligned (date)
  2139. "Format a date string for display in the daily/weekly agenda, or timeline.
  2140. This function makes sure that dates are aligned for easy reading."
  2141. (format "%-9s %2d %s %4d"
  2142. (calendar-day-name date)
  2143. (extract-calendar-day date)
  2144. (calendar-month-name (extract-calendar-month date))
  2145. (extract-calendar-year date)))
  2146. (defcustom org-agenda-include-diary nil
  2147. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2148. :group 'org-agenda-daily/weekly
  2149. :type 'boolean)
  2150. (defcustom org-agenda-include-all-todo nil
  2151. "Set means weekly/daily agenda will always contain all TODO entries.
  2152. The TODO entries will be listed at the top of the agenda, before
  2153. the entries for specific days."
  2154. :group 'org-agenda-daily/weekly
  2155. :type 'boolean)
  2156. (defcustom org-agenda-repeating-timestamp-show-all t
  2157. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2158. When nil, only one occurence is shown, either today or the
  2159. nearest into the future."
  2160. :group 'org-agenda-daily/weekly
  2161. :type 'boolean)
  2162. (defcustom org-deadline-warning-days 14
  2163. "No. of days before expiration during which a deadline becomes active.
  2164. This variable governs the display in sparse trees and in the agenda.
  2165. When negative, it means use this number (the absolute value of it)
  2166. even if a deadline has a different individual lead time specified."
  2167. :group 'org-time
  2168. :group 'org-agenda-daily/weekly
  2169. :type 'number)
  2170. (defcustom org-scheduled-past-days 10000
  2171. "No. of days to continue listing scheduled items that are not marked DONE.
  2172. When an item is scheduled on a date, it shows up in the agenda on this
  2173. day and will be listed until it is marked done for the number of days
  2174. given here."
  2175. :group 'org-agenda-daily/weekly
  2176. :type 'number)
  2177. (defgroup org-agenda-time-grid nil
  2178. "Options concerning the time grid in the Org-mode Agenda."
  2179. :tag "Org Agenda Time Grid"
  2180. :group 'org-agenda)
  2181. (defcustom org-agenda-use-time-grid t
  2182. "Non-nil means, show a time grid in the agenda schedule.
  2183. A time grid is a set of lines for specific times (like every two hours between
  2184. 8:00 and 20:00). The items scheduled for a day at specific times are
  2185. sorted in between these lines.
  2186. For details about when the grid will be shown, and what it will look like, see
  2187. the variable `org-agenda-time-grid'."
  2188. :group 'org-agenda-time-grid
  2189. :type 'boolean)
  2190. (defcustom org-agenda-time-grid
  2191. '((daily today require-timed)
  2192. "----------------"
  2193. (800 1000 1200 1400 1600 1800 2000))
  2194. "The settings for time grid for agenda display.
  2195. This is a list of three items. The first item is again a list. It contains
  2196. symbols specifying conditions when the grid should be displayed:
  2197. daily if the agenda shows a single day
  2198. weekly if the agenda shows an entire week
  2199. today show grid on current date, independent of daily/weekly display
  2200. require-timed show grid only if at least one item has a time specification
  2201. The second item is a string which will be places behing the grid time.
  2202. The third item is a list of integers, indicating the times that should have
  2203. a grid line."
  2204. :group 'org-agenda-time-grid
  2205. :type
  2206. '(list
  2207. (set :greedy t :tag "Grid Display Options"
  2208. (const :tag "Show grid in single day agenda display" daily)
  2209. (const :tag "Show grid in weekly agenda display" weekly)
  2210. (const :tag "Always show grid for today" today)
  2211. (const :tag "Show grid only if any timed entries are present"
  2212. require-timed)
  2213. (const :tag "Skip grid times already present in an entry"
  2214. remove-match))
  2215. (string :tag "Grid String")
  2216. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2217. (defgroup org-agenda-sorting nil
  2218. "Options concerning sorting in the Org-mode Agenda."
  2219. :tag "Org Agenda Sorting"
  2220. :group 'org-agenda)
  2221. (let ((sorting-choice
  2222. '(choice
  2223. (const time-up) (const time-down)
  2224. (const category-keep) (const category-up) (const category-down)
  2225. (const tag-down) (const tag-up)
  2226. (const priority-up) (const priority-down))))
  2227. (defcustom org-agenda-sorting-strategy
  2228. '((agenda time-up category-keep priority-down)
  2229. (todo category-keep priority-down)
  2230. (tags category-keep priority-down))
  2231. "Sorting structure for the agenda items of a single day.
  2232. This is a list of symbols which will be used in sequence to determine
  2233. if an entry should be listed before another entry. The following
  2234. symbols are recognized:
  2235. time-up Put entries with time-of-day indications first, early first
  2236. time-down Put entries with time-of-day indications first, late first
  2237. category-keep Keep the default order of categories, corresponding to the
  2238. sequence in `org-agenda-files'.
  2239. category-up Sort alphabetically by category, A-Z.
  2240. category-down Sort alphabetically by category, Z-A.
  2241. tag-up Sort alphabetically by last tag, A-Z.
  2242. tag-down Sort alphabetically by last tag, Z-A.
  2243. priority-up Sort numerically by priority, high priority last.
  2244. priority-down Sort numerically by priority, high priority first.
  2245. The different possibilities will be tried in sequence, and testing stops
  2246. if one comparison returns a \"not-equal\". For example, the default
  2247. '(time-up category-keep priority-down)
  2248. means: Pull out all entries having a specified time of day and sort them,
  2249. in order to make a time schedule for the current day the first thing in the
  2250. agenda listing for the day. Of the entries without a time indication, keep
  2251. the grouped in categories, don't sort the categories, but keep them in
  2252. the sequence given in `org-agenda-files'. Within each category sort by
  2253. priority.
  2254. Leaving out `category-keep' would mean that items will be sorted across
  2255. categories by priority."
  2256. :group 'org-agenda-sorting
  2257. :type `(choice
  2258. (repeat :tag "General" ,sorting-choice)
  2259. (list :tag "Individually"
  2260. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2261. (repeat ,sorting-choice))
  2262. (cons (const :tag "Strategy for TODO lists" todo)
  2263. (repeat ,sorting-choice))
  2264. (cons (const :tag "Strategy for Tags matches" tags)
  2265. (repeat ,sorting-choice))))))
  2266. (defcustom org-sort-agenda-notime-is-late t
  2267. "Non-nil means, items without time are considered late.
  2268. This is only relevant for sorting. When t, items which have no explicit
  2269. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2270. do have a time. When nil, the default time is before 0:00. You can use this
  2271. option to decide if the schedule for today should come before or after timeless
  2272. agenda entries."
  2273. :group 'org-agenda-sorting
  2274. :type 'boolean)
  2275. (defgroup org-agenda-line-format nil
  2276. "Options concerning the entry prefix in the Org-mode agenda display."
  2277. :tag "Org Agenda Line Format"
  2278. :group 'org-agenda)
  2279. (defcustom org-agenda-prefix-format
  2280. '((agenda . " %-12:c%?-12t% s")
  2281. (timeline . " % s")
  2282. (todo . " %-12:c")
  2283. (tags . " %-12:c"))
  2284. "Format specifications for the prefix of items in the agenda views.
  2285. An alist with four entries, for the different agenda types. The keys to the
  2286. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2287. are format strings.
  2288. This format works similar to a printf format, with the following meaning:
  2289. %c the category of the item, \"Diary\" for entries from the diary, or
  2290. as given by the CATEGORY keyword or derived from the file name.
  2291. %T the *last* tag of the item. Last because inherited tags come
  2292. first in the list.
  2293. %t the time-of-day specification if one applies to the entry, in the
  2294. format HH:MM
  2295. %s Scheduling/Deadline information, a short string
  2296. All specifiers work basically like the standard `%s' of printf, but may
  2297. contain two additional characters: A question mark just after the `%' and
  2298. a whitespace/punctuation character just before the final letter.
  2299. If the first character after `%' is a question mark, the entire field
  2300. will only be included if the corresponding value applies to the
  2301. current entry. This is useful for fields which should have fixed
  2302. width when present, but zero width when absent. For example,
  2303. \"%?-12t\" will result in a 12 character time field if a time of the
  2304. day is specified, but will completely disappear in entries which do
  2305. not contain a time.
  2306. If there is punctuation or whitespace character just before the final
  2307. format letter, this character will be appended to the field value if
  2308. the value is not empty. For example, the format \"%-12:c\" leads to
  2309. \"Diary: \" if the category is \"Diary\". If the category were be
  2310. empty, no additional colon would be interted.
  2311. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2312. - Indent the line with two space characters
  2313. - Give the category in a 12 chars wide field, padded with whitespace on
  2314. the right (because of `-'). Append a colon if there is a category
  2315. (because of `:').
  2316. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2317. time, don't put in an empty field, just skip it (because of '?').
  2318. - Finally, put the scheduling information and append a whitespace.
  2319. As another example, if you don't want the time-of-day of entries in
  2320. the prefix, you could use:
  2321. (setq org-agenda-prefix-format \" %-11:c% s\")
  2322. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2323. `org-agenda-remove-tags'."
  2324. :type '(choice
  2325. (string :tag "General format")
  2326. (list :greedy t :tag "View dependent"
  2327. (cons (const agenda) (string :tag "Format"))
  2328. (cons (const timeline) (string :tag "Format"))
  2329. (cons (const todo) (string :tag "Format"))
  2330. (cons (const tags) (string :tag "Format"))))
  2331. :group 'org-agenda-line-format)
  2332. (defvar org-prefix-format-compiled nil
  2333. "The compiled version of the most recently used prefix format.
  2334. See the variable `org-agenda-prefix-format'.")
  2335. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2336. "Text preceeding scheduled items in the agenda view.
  2337. THis is a list with two strings. The first applies when the item is
  2338. scheduled on the current day. The second applies when it has been scheduled
  2339. previously, it may contain a %d to capture how many days ago the item was
  2340. scheduled."
  2341. :group 'org-agenda-line-format
  2342. :type '(list
  2343. (string :tag "Scheduled today ")
  2344. (string :tag "Scheduled previously")))
  2345. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2346. "Text preceeding deadline items in the agenda view.
  2347. This is a list with two strings. The first applies when the item has its
  2348. deadline on the current day. The second applies when it is in the past or
  2349. in the future, it may contain %d to capture how many days away the deadline
  2350. is (was)."
  2351. :group 'org-agenda-line-format
  2352. :type '(list
  2353. (string :tag "Deadline today ")
  2354. (string :tag "Deadline relative")))
  2355. (defcustom org-agenda-remove-times-when-in-prefix t
  2356. "Non-nil means, remove duplicate time specifications in agenda items.
  2357. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2358. time-of-day specification in a headline or diary entry is extracted and
  2359. placed into the prefix. If this option is non-nil, the original specification
  2360. \(a timestamp or -range, or just a plain time(range) specification like
  2361. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2362. cluttered.
  2363. The option can be t or nil. It may also be the symbol `beg', indicating
  2364. that the time should only be removed what it is located at the beginning of
  2365. the headline/diary entry."
  2366. :group 'org-agenda-line-format
  2367. :type '(choice
  2368. (const :tag "Always" t)
  2369. (const :tag "Never" nil)
  2370. (const :tag "When at beginning of entry" beg)))
  2371. (defcustom org-agenda-default-appointment-duration nil
  2372. "Default duration for appointments that only have a starting time.
  2373. When nil, no duration is specified in such cases.
  2374. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2375. :group 'org-agenda-line-format
  2376. :type '(choice
  2377. (integer :tag "Minutes")
  2378. (const :tag "No default duration")))
  2379. (defcustom org-agenda-remove-tags nil
  2380. "Non-nil means, remove the tags from the headline copy in the agenda.
  2381. When this is the symbol `prefix', only remove tags when
  2382. `org-agenda-prefix-format' contains a `%T' specifier."
  2383. :group 'org-agenda-line-format
  2384. :type '(choice
  2385. (const :tag "Always" t)
  2386. (const :tag "Never" nil)
  2387. (const :tag "When prefix format contains %T" prefix)))
  2388. (if (fboundp 'defvaralias)
  2389. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2390. 'org-agenda-remove-tags))
  2391. (defcustom org-agenda-tags-column -80
  2392. "Shift tags in agenda items to this column.
  2393. If this number is positive, it specifies the column. If it is negative,
  2394. it means that the tags should be flushright to that column. For example,
  2395. -80 works well for a normal 80 character screen."
  2396. :group 'org-agenda-line-format
  2397. :type 'integer)
  2398. (if (fboundp 'defvaralias)
  2399. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2400. (defcustom org-agenda-fontify-priorities t
  2401. "Non-nil means, highlight low and high priorities in agenda.
  2402. When t, the highest priority entries are bold, lowest priority italic.
  2403. This may also be an association list of priority faces. The face may be
  2404. a names face, or a list like `(:background \"Red\")'."
  2405. :group 'org-agenda-line-format
  2406. :type '(choice
  2407. (const :tag "Never" nil)
  2408. (const :tag "Defaults" t)
  2409. (repeat :tag "Specify"
  2410. (list (character :tag "Priority" :value ?A)
  2411. (sexp :tag "face")))))
  2412. (defgroup org-latex nil
  2413. "Options for embedding LaTeX code into Org-mode"
  2414. :tag "Org LaTeX"
  2415. :group 'org)
  2416. (defcustom org-format-latex-options
  2417. '(:foreground default :background default :scale 1.0
  2418. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2419. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2420. "Options for creating images from LaTeX fragments.
  2421. This is a property list with the following properties:
  2422. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2423. `default' means use the forground of the default face.
  2424. :background the background color, or \"Transparent\".
  2425. `default' means use the background of the default face.
  2426. :scale a scaling factor for the size of the images
  2427. :html-foreground, :html-background, :html-scale
  2428. The same numbers for HTML export.
  2429. :matchers a list indicating which matchers should be used to
  2430. find LaTeX fragments. Valid members of this list are:
  2431. \"begin\" find environments
  2432. \"$\" find math expressions surrounded by $...$
  2433. \"$$\" find math expressions surrounded by $$....$$
  2434. \"\\(\" find math expressions surrounded by \\(...\\)
  2435. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2436. :group 'org-latex
  2437. :type 'plist)
  2438. (defcustom org-format-latex-header "\\documentclass{article}
  2439. \\usepackage{fullpage} % do not remove
  2440. \\usepackage{amssymb}
  2441. \\usepackage[usenames]{color}
  2442. \\usepackage{amsmath}
  2443. \\usepackage{latexsym}
  2444. \\usepackage[mathscr]{eucal}
  2445. \\pagestyle{empty} % do not remove"
  2446. "The document header used for processing LaTeX fragments."
  2447. :group 'org-latex
  2448. :type 'string)
  2449. (defgroup org-export nil
  2450. "Options for exporting org-listings."
  2451. :tag "Org Export"
  2452. :group 'org)
  2453. (defgroup org-export-general nil
  2454. "General options for exporting Org-mode files."
  2455. :tag "Org Export General"
  2456. :group 'org-export)
  2457. (defcustom org-export-publishing-directory "."
  2458. "Path to the location where exported files should be located.
  2459. This path may be relative to the directory where the Org-mode file lives.
  2460. The default is to put them into the same directory as the Org-mode file.
  2461. The variable may also be an alist with export types `:html', `:ascii',
  2462. `:ical', `:LaTeX', or `:xoxo' and the corresponding directories.
  2463. If a directory path is relative, it is interpreted relative to the
  2464. directory where the exported Org-mode files lives."
  2465. :group 'org-export-general
  2466. :type '(choice
  2467. (directory)
  2468. (repeat
  2469. (cons
  2470. (choice :tag "Type"
  2471. (const :html) (const :LaTeX)
  2472. (const :ascii) (const :ical) (const :xoxo))
  2473. (directory)))))
  2474. (defcustom org-export-language-setup
  2475. '(("en" "Author" "Date" "Table of Contents")
  2476. ("cs" "Autor" "Datum" "Obsah")
  2477. ("da" "Ophavsmand" "Dato" "Indhold")
  2478. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2479. ("es" "Autor" "Fecha" "\xcdndice")
  2480. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2481. ("it" "Autore" "Data" "Indice")
  2482. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2483. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2484. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2485. "Terms used in export text, translated to different languages.
  2486. Use the variable `org-export-default-language' to set the language,
  2487. or use the +OPTION lines for a per-file setting."
  2488. :group 'org-export-general
  2489. :type '(repeat
  2490. (list
  2491. (string :tag "HTML language tag")
  2492. (string :tag "Author")
  2493. (string :tag "Date")
  2494. (string :tag "Table of Contents"))))
  2495. (defcustom org-export-default-language "en"
  2496. "The default language of HTML export, as a string.
  2497. This should have an association in `org-export-language-setup'."
  2498. :group 'org-export-general
  2499. :type 'string)
  2500. (defcustom org-export-skip-text-before-1st-heading t
  2501. "Non-nil means, skip all text before the first headline when exporting.
  2502. When nil, that text is exported as well."
  2503. :group 'org-export-general
  2504. :type 'boolean)
  2505. (defcustom org-export-headline-levels 3
  2506. "The last level which is still exported as a headline.
  2507. Inferior levels will produce itemize lists when exported.
  2508. Note that a numeric prefix argument to an exporter function overrides
  2509. this setting.
  2510. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2511. :group 'org-export-general
  2512. :type 'number)
  2513. (defcustom org-export-with-section-numbers t
  2514. "Non-nil means, add section numbers to headlines when exporting.
  2515. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2516. :group 'org-export-general
  2517. :type 'boolean)
  2518. (defcustom org-export-with-toc t
  2519. "Non-nil means, create a table of contents in exported files.
  2520. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2521. When an integer, include levels up to N in the toc, this may then be
  2522. different from `org-export-headline-levels', but it will not be allowed
  2523. to be larger than the number of headline levels.
  2524. When nil, no table of contents is made.
  2525. Headlines which contain any TODO items will be marked with \"(*)\" in
  2526. ASCII export, and with red color in HTML output, if the option
  2527. `org-export-mark-todo-in-toc' is set.
  2528. In HTML output, the TOC will be clickable.
  2529. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2530. or \"toc:3\"."
  2531. :group 'org-export-general
  2532. :type '(choice
  2533. (const :tag "No Table of Contents" nil)
  2534. (const :tag "Full Table of Contents" t)
  2535. (integer :tag "TOC to level")))
  2536. (defcustom org-export-mark-todo-in-toc nil
  2537. "Non-nil means, mark TOC lines that contain any open TODO items."
  2538. :group 'org-export-general
  2539. :type 'boolean)
  2540. (defcustom org-export-preserve-breaks nil
  2541. "Non-nil means, preserve all line breaks when exporting.
  2542. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2543. export, line breaks will always be preserved, regardless of this variable.
  2544. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2545. :group 'org-export-general
  2546. :type 'boolean)
  2547. (defcustom org-export-with-archived-trees 'headline
  2548. "Whether subtrees with the ARCHIVE tag should be exported.
  2549. This can have three different values
  2550. nil Do not export, pretend this tree is not present
  2551. t Do export the entire tree
  2552. headline Only export the headline, but skip the tree below it."
  2553. :group 'org-export-general
  2554. :group 'org-archive
  2555. :type '(choice
  2556. (const :tag "not at all" nil)
  2557. (const :tag "headline only" 'headline)
  2558. (const :tag "entirely" t)))
  2559. (defcustom org-export-author-info t
  2560. "Non-nil means, insert author name and email into the exported file.
  2561. This option can also be set with the +OPTIONS line,
  2562. e.g. \"author-info:nil\"."
  2563. :group 'org-export-general
  2564. :type 'boolean)
  2565. (defcustom org-export-time-stamp-file t
  2566. "Non-nil means, insert a time stamp into the exported file.
  2567. The time stamp shows when the file was created.
  2568. This option can also be set with the +OPTIONS line,
  2569. e.g. \"timestamp:nil\"."
  2570. :group 'org-export-general
  2571. :type 'boolean)
  2572. (defcustom org-export-with-timestamps t
  2573. "If nil, do not export time stamps and associated keywords."
  2574. :group 'org-export-general
  2575. :type 'boolean)
  2576. (defcustom org-export-remove-timestamps-from-toc t
  2577. "If nil, remove timestamps from the table of contents entries."
  2578. :group 'org-export-general
  2579. :type 'boolean)
  2580. (defcustom org-export-with-tags 'not-in-toc
  2581. "If nil, do not export tags, just remove them from headlines.
  2582. If this is the symbol `not-in-toc', tags will be removed from table of
  2583. contents entries, but still be shown in the headlines of the document.
  2584. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2585. :group 'org-export-general
  2586. :type '(choice
  2587. (const :tag "Off" nil)
  2588. (const :tag "Not in TOC" not-in-toc)
  2589. (const :tag "On" t)))
  2590. (defcustom org-export-with-drawers nil
  2591. "Non-nil means, export with drawers like the property drawer.
  2592. When t, all drawers are exported. This may also be a list of
  2593. drawer names to export."
  2594. :group 'org-export-general
  2595. :type '(choice
  2596. (const :tag "All drawers" t)
  2597. (const :tag "None" nil)
  2598. (repeat :tag "Selected drawers"
  2599. (string :tag "Drawer name"))))
  2600. (defgroup org-export-translation nil
  2601. "Options for translating special ascii sequences for the export backends."
  2602. :tag "Org Export Translation"
  2603. :group 'org-export)
  2604. (defcustom org-export-with-emphasize t
  2605. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2606. If the export target supports emphasizing text, the word will be
  2607. typeset in bold, italic, or underlined, respectively. Works only for
  2608. single words, but you can say: I *really* *mean* *this*.
  2609. Not all export backends support this.
  2610. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2611. :group 'org-export-translation
  2612. :type 'boolean)
  2613. (defcustom org-export-with-footnotes t
  2614. "If nil, export [1] as a footnote marker.
  2615. Lines starting with [1] will be formatted as footnotes.
  2616. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2617. :group 'org-export-translation
  2618. :type 'boolean)
  2619. (defcustom org-export-with-sub-superscripts t
  2620. "Non-nil means, interpret \"_\" and \"^\" for export.
  2621. When this option is turned on, you can use TeX-like syntax for sub- and
  2622. superscripts. Several characters after \"_\" or \"^\" will be
  2623. considered as a single item - so grouping with {} is normally not
  2624. needed. For example, the following things will be parsed as single
  2625. sub- or superscripts.
  2626. 10^24 or 10^tau several digits will be considered 1 item.
  2627. 10^-12 or 10^-tau a leading sign with digits or a word
  2628. x^2-y^3 will be read as x^2 - y^3, because items are
  2629. terminated by almost any nonword/nondigit char.
  2630. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2631. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2632. sub/superscript. If you set this variable to the symbol `{}',
  2633. the braces are *required* in order to trigger interpretations as
  2634. sub/superscript. This can be helpful in documents that need \"_\"
  2635. frequently in plain text.
  2636. Not all export backends support this, but HTML does.
  2637. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2638. :group 'org-export-translation
  2639. :type '(choice
  2640. (const :tag "Always interpret" t)
  2641. (const :tag "Only with braces" {})
  2642. (const :tag "Never interpret" nil)))
  2643. (defcustom org-export-with-TeX-macros t
  2644. "Non-nil means, interpret simple TeX-like macros when exporting.
  2645. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2646. No only real TeX macros will work here, but the standard HTML entities
  2647. for math can be used as macro names as well. For a list of supported
  2648. names in HTML export, see the constant `org-html-entities'.
  2649. Not all export backends support this.
  2650. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2651. :group 'org-export-translation
  2652. :group 'org-export-latex
  2653. :type 'boolean)
  2654. (defcustom org-export-with-LaTeX-fragments nil
  2655. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2656. When set, the exporter will find LaTeX environments if the \\begin line is
  2657. the first non-white thing on a line. It will also find the math delimiters
  2658. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2659. display math.
  2660. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2661. :group 'org-export-translation
  2662. :group 'org-export-latex
  2663. :type 'boolean)
  2664. (defcustom org-export-with-fixed-width t
  2665. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2666. This can be used to have pre-formatted text, fragments of code etc. For
  2667. example:
  2668. : ;; Some Lisp examples
  2669. : (while (defc cnt)
  2670. : (ding))
  2671. will be looking just like this in also HTML. See also the QUOTE keyword.
  2672. Not all export backends support this.
  2673. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2674. :group 'org-export-translation
  2675. :type 'boolean)
  2676. (defcustom org-match-sexp-depth 3
  2677. "Number of stacked braces for sub/superscript matching.
  2678. This has to be set before loading org.el to be effective."
  2679. :group 'org-export-translation
  2680. :type 'integer)
  2681. (defgroup org-export-tables nil
  2682. "Options for exporting tables in Org-mode."
  2683. :tag "Org Export Tables"
  2684. :group 'org-export)
  2685. (defcustom org-export-with-tables t
  2686. "If non-nil, lines starting with \"|\" define a table.
  2687. For example:
  2688. | Name | Address | Birthday |
  2689. |-------------+----------+-----------|
  2690. | Arthur Dent | England | 29.2.2100 |
  2691. Not all export backends support this.
  2692. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2693. :group 'org-export-tables
  2694. :type 'boolean)
  2695. (defcustom org-export-highlight-first-table-line t
  2696. "Non-nil means, highlight the first table line.
  2697. In HTML export, this means use <th> instead of <td>.
  2698. In tables created with table.el, this applies to the first table line.
  2699. In Org-mode tables, all lines before the first horizontal separator
  2700. line will be formatted with <th> tags."
  2701. :group 'org-export-tables
  2702. :type 'boolean)
  2703. (defcustom org-export-table-remove-special-lines t
  2704. "Remove special lines and marking characters in calculating tables.
  2705. This removes the special marking character column from tables that are set
  2706. up for spreadsheet calculations. It also removes the entire lines
  2707. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2708. the values of constants may be useful to have."
  2709. :group 'org-export-tables
  2710. :type 'boolean)
  2711. (defcustom org-export-prefer-native-exporter-for-tables nil
  2712. "Non-nil means, always export tables created with table.el natively.
  2713. Natively means, use the HTML code generator in table.el.
  2714. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2715. the table does not use row- or column-spanning). This has the
  2716. advantage, that the automatic HTML conversions for math symbols and
  2717. sub/superscripts can be applied. Org-mode's HTML generator is also
  2718. much faster."
  2719. :group 'org-export-tables
  2720. :type 'boolean)
  2721. (defgroup org-export-ascii nil
  2722. "Options specific for ASCII export of Org-mode files."
  2723. :tag "Org Export ASCII"
  2724. :group 'org-export)
  2725. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2726. "Characters for underlining headings in ASCII export.
  2727. In the given sequence, these characters will be used for level 1, 2, ..."
  2728. :group 'org-export-ascii
  2729. :type '(repeat character))
  2730. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2731. "Bullet characters for headlines converted to lists in ASCII export.
  2732. The first character is is used for the first lest level generated in this
  2733. way, and so on. If there are more levels than characters given here,
  2734. the list will be repeated.
  2735. Note that plain lists will keep the same bullets as the have in the
  2736. Org-mode file."
  2737. :group 'org-export-ascii
  2738. :type '(repeat character))
  2739. (defgroup org-export-xml nil
  2740. "Options specific for XML export of Org-mode files."
  2741. :tag "Org Export XML"
  2742. :group 'org-export)
  2743. (defgroup org-export-html nil
  2744. "Options specific for HTML export of Org-mode files."
  2745. :tag "Org Export HTML"
  2746. :group 'org-export)
  2747. (defcustom org-export-html-coding-system nil
  2748. ""
  2749. :group 'org-export-html
  2750. :type 'coding-system)
  2751. (defcustom org-export-html-extension "html"
  2752. "The extension for exported HTML files."
  2753. :group 'org-export-html
  2754. :type 'string)
  2755. (defcustom org-export-html-style
  2756. "<style type=\"text/css\">
  2757. html {
  2758. font-family: Times, serif;
  2759. font-size: 12pt;
  2760. }
  2761. .title { text-align: center; }
  2762. .todo { color: red; }
  2763. .done { color: green; }
  2764. .timestamp { color: grey }
  2765. .timestamp-kwd { color: CadetBlue }
  2766. .tag { background-color:lightblue; font-weight:normal }
  2767. .target { background-color: lavender; }
  2768. pre {
  2769. border: 1pt solid #AEBDCC;
  2770. background-color: #F3F5F7;
  2771. padding: 5pt;
  2772. font-family: courier, monospace;
  2773. }
  2774. table { border-collapse: collapse; }
  2775. td, th {
  2776. vertical-align: top;
  2777. <!--border: 1pt solid #ADB9CC;-->
  2778. }
  2779. </style>"
  2780. "The default style specification for exported HTML files.
  2781. Since there are different ways of setting style information, this variable
  2782. needs to contain the full HTML structure to provide a style, including the
  2783. surrounding HTML tags. The style specifications should include definitions
  2784. for new classes todo, done, title, and deadline. For example, legal values
  2785. would be:
  2786. <style type=\"text/css\">
  2787. p { font-weight: normal; color: gray; }
  2788. h1 { color: black; }
  2789. .title { text-align: center; }
  2790. .todo, .deadline { color: red; }
  2791. .done { color: green; }
  2792. </style>
  2793. or, if you want to keep the style in a file,
  2794. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2795. As the value of this option simply gets inserted into the HTML <head> header,
  2796. you can \"misuse\" it to add arbitrary text to the header."
  2797. :group 'org-export-html
  2798. :type 'string)
  2799. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2800. "Format for typesetting the document title in HTML export."
  2801. :group 'org-export-html
  2802. :type 'string)
  2803. (defcustom org-export-html-toplevel-hlevel 2
  2804. "The <H> level for level 1 headings in HTML export."
  2805. :group 'org-export-html
  2806. :type 'string)
  2807. (defcustom org-export-html-link-org-files-as-html t
  2808. "Non-nil means, make file links to `file.org' point to `file.html'.
  2809. When org-mode is exporting an org-mode file to HTML, links to
  2810. non-html files are directly put into a href tag in HTML.
  2811. However, links to other Org-mode files (recognized by the
  2812. extension `.org.) should become links to the corresponding html
  2813. file, assuming that the linked org-mode file will also be
  2814. converted to HTML.
  2815. When nil, the links still point to the plain `.org' file."
  2816. :group 'org-export-html
  2817. :type 'boolean)
  2818. (defcustom org-export-html-inline-images 'maybe
  2819. "Non-nil means, inline images into exported HTML pages.
  2820. This is done using an <img> tag. When nil, an anchor with href is used to
  2821. link to the image. If this option is `maybe', then images in links with
  2822. an empty description will be inlined, while images with a description will
  2823. be linked only."
  2824. :group 'org-export-html
  2825. :type '(choice (const :tag "Never" nil)
  2826. (const :tag "Always" t)
  2827. (const :tag "When there is no description" maybe)))
  2828. ;; FIXME: rename
  2829. (defcustom org-export-html-expand t
  2830. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2831. When nil, these tags will be exported as plain text and therefore
  2832. not be interpreted by a browser.
  2833. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2834. :group 'org-export-html
  2835. :type 'boolean)
  2836. (defcustom org-export-html-table-tag
  2837. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2838. "The HTML tag that is used to start a table.
  2839. This must be a <table> tag, but you may change the options like
  2840. borders and spacing."
  2841. :group 'org-export-html
  2842. :type 'string)
  2843. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2844. "The opening tag for table header fields.
  2845. This is customizable so that alignment options can be specified."
  2846. :group 'org-export-tables
  2847. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2848. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2849. "The opening tag for table data fields.
  2850. This is customizable so that alignment options can be specified."
  2851. :group 'org-export-tables
  2852. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2853. (defcustom org-export-html-with-timestamp nil
  2854. "If non-nil, write `org-export-html-html-helper-timestamp'
  2855. into the exported HTML text. Otherwise, the buffer will just be saved
  2856. to a file."
  2857. :group 'org-export-html
  2858. :type 'boolean)
  2859. (defcustom org-export-html-html-helper-timestamp
  2860. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2861. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2862. :group 'org-export-html
  2863. :type 'string)
  2864. (defgroup org-export-icalendar nil
  2865. "Options specific for iCalendar export of Org-mode files."
  2866. :tag "Org Export iCalendar"
  2867. :group 'org-export)
  2868. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2869. "The file name for the iCalendar file covering all agenda files.
  2870. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2871. The file name should be absolute, the file will be overwritten without warning."
  2872. :group 'org-export-icalendar
  2873. :type 'file)
  2874. (defcustom org-icalendar-include-todo nil
  2875. "Non-nil means, export to iCalendar files should also cover TODO items."
  2876. :group 'org-export-icalendar
  2877. :type '(choice
  2878. (const :tag "None" nil)
  2879. (const :tag "Unfinished" t)
  2880. (const :tag "All" all)))
  2881. (defcustom org-icalendar-include-sexps t
  2882. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2883. These are entries like in the diary, but directly in an Org-mode file."
  2884. :group 'org-export-icalendar
  2885. :type 'boolean)
  2886. (defcustom org-icalendar-include-body 100
  2887. "Amount of text below headline to be included in iCalendar export.
  2888. This is a number of characters that should maximally be included.
  2889. Properties, scheduling and clocking lines will always be removed.
  2890. The text will be inserted into the DESCRIPTION field."
  2891. :group 'org-export-icalendar
  2892. :type '(choice
  2893. (const :tag "Nothing" nil)
  2894. (const :tag "Everything" t)
  2895. (integer :tag "Max characters")))
  2896. (defcustom org-icalendar-combined-name "OrgMode"
  2897. "Calendar name for the combined iCalendar representing all agenda files."
  2898. :group 'org-export-icalendar
  2899. :type 'string)
  2900. (defgroup org-font-lock nil
  2901. "Font-lock settings for highlighting in Org-mode."
  2902. :tag "Org Font Lock"
  2903. :group 'org)
  2904. (defcustom org-level-color-stars-only nil
  2905. "Non-nil means fontify only the stars in each headline.
  2906. When nil, the entire headline is fontified.
  2907. Changing it requires restart of `font-lock-mode' to become effective
  2908. also in regions already fontified."
  2909. :group 'org-font-lock
  2910. :type 'boolean)
  2911. (defcustom org-hide-leading-stars nil
  2912. "Non-nil means, hide the first N-1 stars in a headline.
  2913. This works by using the face `org-hide' for these stars. This
  2914. face is white for a light background, and black for a dark
  2915. background. You may have to customize the face `org-hide' to
  2916. make this work.
  2917. Changing it requires restart of `font-lock-mode' to become effective
  2918. also in regions already fontified.
  2919. You may also set this on a per-file basis by adding one of the following
  2920. lines to the buffer:
  2921. #+STARTUP: hidestars
  2922. #+STARTUP: showstars"
  2923. :group 'org-font-lock
  2924. :type 'boolean)
  2925. (defcustom org-fontify-done-headline nil
  2926. "Non-nil means, change the face of a headline if it is marked DONE.
  2927. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2928. When this is non-nil, the headline after the keyword is set to the
  2929. `org-headline-done' as an additional indication."
  2930. :group 'org-font-lock
  2931. :type 'boolean)
  2932. (defcustom org-fontify-emphasized-text t
  2933. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2934. Changing this variable requires a restart of Emacs to take effect."
  2935. :group 'org-font-lock
  2936. :type 'boolean)
  2937. (defvar org-emph-re nil
  2938. "Regular expression for matching emphasis.")
  2939. (defvar org-emphasis-regexp-components) ; defined just below
  2940. (defvar org-emphasis-alist) ; defined just below
  2941. (defun org-set-emph-re (var val)
  2942. "Set variable and compute the emphasis regular expression."
  2943. (set var val)
  2944. (when (and (boundp 'org-emphasis-alist)
  2945. (boundp 'org-emphasis-regexp-components)
  2946. org-emphasis-alist org-emphasis-regexp-components)
  2947. (let* ((e org-emphasis-regexp-components)
  2948. (pre (car e))
  2949. (post (nth 1 e))
  2950. (border (nth 2 e))
  2951. (body (nth 3 e))
  2952. (nl (nth 4 e))
  2953. (stacked (nth 5 e))
  2954. (body1 (concat body "*?"))
  2955. (markers (mapconcat 'car org-emphasis-alist "")))
  2956. ;; make sure special characters appear at the right position in the class
  2957. (if (string-match "\\^" markers)
  2958. (setq markers (concat (replace-match "" t t markers) "^")))
  2959. (if (string-match "-" markers)
  2960. (setq markers (concat (replace-match "" t t markers) "-")))
  2961. (if (> nl 0)
  2962. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2963. (int-to-string nl) "\\}")))
  2964. ;; Make the regexp
  2965. (setq org-emph-re
  2966. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2967. "\\("
  2968. "\\([" markers "]\\)"
  2969. "\\("
  2970. "[^" border (if (and nil stacked) markers) "]"
  2971. body1
  2972. "[^" border (if (and nil stacked) markers) "]"
  2973. "\\)"
  2974. "\\3\\)"
  2975. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2976. (defcustom org-emphasis-regexp-components
  2977. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2978. "Components used to build the reqular expression for emphasis.
  2979. This is a list with 6 entries. Terminology: In an emphasis string
  2980. like \" *strong word* \", we call the initial space PREMATCH, the final
  2981. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2982. and \"trong wor\" is the body. The different components in this variable
  2983. specify what is allowed/forbidden in each part:
  2984. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2985. post Chars allowed as postmatch. End of line will be allowed too.
  2986. border The chars *forbidden* as border characters.
  2987. body-regexp A regexp like \".\" to match a body character. Don't use
  2988. non-shy groups here, and don't allow newline here.
  2989. newline The maximum number of newlines allowed in an emphasis exp.
  2990. stacked Non-nil means, allow stacked styles. This works only in HTML
  2991. export. When this is set, all marker characters (as given in
  2992. `org-emphasis-alist') will be allowed as pre/post, aiding
  2993. inside-out matching.
  2994. Use customize to modify this, or restart Emacs after changing it."
  2995. :group 'org-font-lock
  2996. :set 'org-set-emph-re
  2997. :type '(list
  2998. (sexp :tag "Allowed chars in pre ")
  2999. (sexp :tag "Allowed chars in post ")
  3000. (sexp :tag "Forbidden chars in border ")
  3001. (sexp :tag "Regexp for body ")
  3002. (integer :tag "number of newlines allowed")
  3003. (boolean :tag "Stacking allowed ")))
  3004. (defcustom org-emphasis-alist
  3005. '(("*" bold "<b>" "</b>")
  3006. ("/" italic "<i>" "</i>")
  3007. ("_" underline "<u>" "</u>")
  3008. ("=" org-code "<code>" "</code>")
  3009. ("+" (:strike-through t) "<del>" "</del>")
  3010. )
  3011. "Special syntax for emphasized text.
  3012. Text starting and ending with a special character will be emphasized, for
  3013. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3014. characters, the face to be used by font-lock for highlighting in Org-mode
  3015. Emacs buffers, and the HTML tags to be used for this.
  3016. Use customize to modify this, or restart Emacs after changing it."
  3017. :group 'org-font-lock
  3018. :set 'org-set-emph-re
  3019. :type '(repeat
  3020. (list
  3021. (string :tag "Marker character")
  3022. (choice
  3023. (face :tag "Font-lock-face")
  3024. (plist :tag "Face property list"))
  3025. (string :tag "HTML start tag")
  3026. (string :tag "HTML end tag"))))
  3027. ;;; The faces
  3028. (defgroup org-faces nil
  3029. "Faces in Org-mode."
  3030. :tag "Org Faces"
  3031. :group 'org-font-lock)
  3032. (defun org-compatible-face (inherits specs)
  3033. "Make a compatible face specification.
  3034. If INHERITS is an existing face and if the Emacs version supports it,
  3035. just inherit the face. If not, use SPECS to define the face.
  3036. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3037. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3038. to the top of the list. The `min-colors' attribute will be removed from
  3039. any other entries, and any resulting duplicates will be removed entirely."
  3040. (cond
  3041. ((and inherits (facep inherits)
  3042. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3043. ;; In Emacs 23, we use inheritance where possible.
  3044. ;; We only do this in Emacs 23, because only there the outline
  3045. ;; faces have been changed to the original org-mode-level-faces.
  3046. (list (list t :inherit inherits)))
  3047. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3048. ;; These do not understand the `min-colors' attribute.
  3049. (let (r e a)
  3050. (while (setq e (pop specs))
  3051. (cond
  3052. ((memq (car e) '(t default)) (push e r))
  3053. ((setq a (member '(min-colors 8) (car e)))
  3054. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3055. (cdr e)))))
  3056. ((setq a (assq 'min-colors (car e)))
  3057. (setq e (cons (delq a (car e)) (cdr e)))
  3058. (or (assoc (car e) r) (push e r)))
  3059. (t (or (assoc (car e) r) (push e r)))))
  3060. (nreverse r)))
  3061. (t specs)))
  3062. (defface org-hide
  3063. '((((background light)) (:foreground "white"))
  3064. (((background dark)) (:foreground "black")))
  3065. "Face used to hide leading stars in headlines.
  3066. The forground color of this face should be equal to the background
  3067. color of the frame."
  3068. :group 'org-faces)
  3069. (defface org-level-1 ;; font-lock-function-name-face
  3070. (org-compatible-face
  3071. 'outline-1
  3072. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3073. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3074. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3075. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3076. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3077. (t (:bold t))))
  3078. "Face used for level 1 headlines."
  3079. :group 'org-faces)
  3080. (defface org-level-2 ;; font-lock-variable-name-face
  3081. (org-compatible-face
  3082. 'outline-2
  3083. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3084. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3085. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3086. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3087. (t (:bold t))))
  3088. "Face used for level 2 headlines."
  3089. :group 'org-faces)
  3090. (defface org-level-3 ;; font-lock-keyword-face
  3091. (org-compatible-face
  3092. 'outline-3
  3093. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3094. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3095. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3096. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3097. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3098. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3099. (t (:bold t))))
  3100. "Face used for level 3 headlines."
  3101. :group 'org-faces)
  3102. (defface org-level-4 ;; font-lock-comment-face
  3103. (org-compatible-face
  3104. 'outline-4
  3105. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3106. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3107. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3108. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3109. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3110. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3111. (t (:bold t))))
  3112. "Face used for level 4 headlines."
  3113. :group 'org-faces)
  3114. (defface org-level-5 ;; font-lock-type-face
  3115. (org-compatible-face
  3116. 'outline-5
  3117. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3118. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3119. (((class color) (min-colors 8)) (:foreground "green"))))
  3120. "Face used for level 5 headlines."
  3121. :group 'org-faces)
  3122. (defface org-level-6 ;; font-lock-constant-face
  3123. (org-compatible-face
  3124. 'outline-6
  3125. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3126. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3127. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3128. "Face used for level 6 headlines."
  3129. :group 'org-faces)
  3130. (defface org-level-7 ;; font-lock-builtin-face
  3131. (org-compatible-face
  3132. 'outline-7
  3133. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3134. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3135. (((class color) (min-colors 8)) (:foreground "blue"))))
  3136. "Face used for level 7 headlines."
  3137. :group 'org-faces)
  3138. (defface org-level-8 ;; font-lock-string-face
  3139. (org-compatible-face
  3140. 'outline-8
  3141. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3142. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3143. (((class color) (min-colors 8)) (:foreground "green"))))
  3144. "Face used for level 8 headlines."
  3145. :group 'org-faces)
  3146. (defface org-special-keyword ;; font-lock-string-face
  3147. (org-compatible-face
  3148. nil
  3149. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3150. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3151. (t (:italic t))))
  3152. "Face used for special keywords."
  3153. :group 'org-faces)
  3154. (defface org-drawer ;; font-lock-function-name-face
  3155. (org-compatible-face
  3156. nil
  3157. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3158. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3159. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3160. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3161. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3162. (t (:bold t))))
  3163. "Face used for drawers."
  3164. :group 'org-faces)
  3165. (defface org-property-value nil
  3166. "Face used for the value of a property."
  3167. :group 'org-faces)
  3168. (defface org-column
  3169. (org-compatible-face
  3170. nil
  3171. '((((class color) (min-colors 16) (background light))
  3172. (:background "grey90"))
  3173. (((class color) (min-colors 16) (background dark))
  3174. (:background "grey30"))
  3175. (((class color) (min-colors 8))
  3176. (:background "cyan" :foreground "black"))
  3177. (t (:inverse-video t))))
  3178. "Face for column display of entry properties."
  3179. :group 'org-faces)
  3180. (when (fboundp 'set-face-attribute)
  3181. ;; Make sure that a fixed-width face is used when we have a column table.
  3182. (set-face-attribute 'org-column nil
  3183. :height (face-attribute 'default :height)
  3184. :family (face-attribute 'default :family)))
  3185. (defface org-warning
  3186. (org-compatible-face
  3187. 'font-lock-warning-face
  3188. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3189. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3190. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3191. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3192. (t (:bold t))))
  3193. "Face for deadlines and TODO keywords."
  3194. :group 'org-faces)
  3195. (defface org-archived ; similar to shadow
  3196. (org-compatible-face
  3197. 'shadow
  3198. '((((class color grayscale) (min-colors 88) (background light))
  3199. (:foreground "grey50"))
  3200. (((class color grayscale) (min-colors 88) (background dark))
  3201. (:foreground "grey70"))
  3202. (((class color) (min-colors 8) (background light))
  3203. (:foreground "green"))
  3204. (((class color) (min-colors 8) (background dark))
  3205. (:foreground "yellow"))))
  3206. "Face for headline with the ARCHIVE tag."
  3207. :group 'org-faces)
  3208. (defface org-link
  3209. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3210. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3211. (t (:underline t)))
  3212. "Face for links."
  3213. :group 'org-faces)
  3214. (defface org-ellipsis
  3215. '((((class color) (background light)) (:foreground "DarkGoldenrod" :strike-through t))
  3216. (((class color) (background dark)) (:foreground "LightGoldenrod" :strike-through t))
  3217. (t (:strike-through t)))
  3218. "Face for the ellipsis in folded text."
  3219. :group 'org-faces)
  3220. (defface org-target
  3221. '((((class color) (background light)) (:underline t))
  3222. (((class color) (background dark)) (:underline t))
  3223. (t (:underline t)))
  3224. "Face for links."
  3225. :group 'org-faces)
  3226. (defface org-date
  3227. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3228. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3229. (t (:underline t)))
  3230. "Face for links."
  3231. :group 'org-faces)
  3232. (defface org-sexp-date
  3233. '((((class color) (background light)) (:foreground "Purple"))
  3234. (((class color) (background dark)) (:foreground "Cyan"))
  3235. (t (:underline t)))
  3236. "Face for links."
  3237. :group 'org-faces)
  3238. (defface org-tag
  3239. '((t (:bold t)))
  3240. "Face for tags."
  3241. :group 'org-faces)
  3242. (defface org-todo ; font-lock-warning-face
  3243. (org-compatible-face
  3244. nil
  3245. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3246. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3247. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3248. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3249. (t (:inverse-video t :bold t))))
  3250. "Face for TODO keywords."
  3251. :group 'org-faces)
  3252. (defface org-done ;; font-lock-type-face
  3253. (org-compatible-face
  3254. nil
  3255. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3256. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3257. (((class color) (min-colors 8)) (:foreground "green"))
  3258. (t (:bold t))))
  3259. "Face used for todo keywords that indicate DONE items."
  3260. :group 'org-faces)
  3261. (defface org-headline-done ;; font-lock-string-face
  3262. (org-compatible-face
  3263. nil
  3264. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3265. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3266. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3267. "Face used to indicate that a headline is DONE.
  3268. This face is only used if `org-fontify-done-headline' is set. If applies
  3269. to the part of the headline after the DONE keyword."
  3270. :group 'org-faces)
  3271. (defcustom org-todo-keyword-faces nil
  3272. "Faces for specific TODO keywords.
  3273. This is a list of cons cells, with TODO keywords in the car
  3274. and faces in the cdr. The face can be a symbol, or a property
  3275. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3276. :group 'org-faces
  3277. :group 'org-todo
  3278. :type '(repeat
  3279. (cons
  3280. (string :tag "keyword")
  3281. (sexp :tag "face"))))
  3282. (defface org-table ;; font-lock-function-name-face
  3283. (org-compatible-face
  3284. nil
  3285. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3286. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3287. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3288. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3289. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3290. (((class color) (min-colors 8) (background dark)))))
  3291. "Face used for tables."
  3292. :group 'org-faces)
  3293. (defface org-formula
  3294. (org-compatible-face
  3295. nil
  3296. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3297. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3298. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3299. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3300. (t (:bold t :italic t))))
  3301. "Face for formulas."
  3302. :group 'org-faces)
  3303. (defface org-code
  3304. (org-compatible-face
  3305. nil
  3306. '((((class color grayscale) (min-colors 88) (background light))
  3307. (:foreground "grey50"))
  3308. (((class color grayscale) (min-colors 88) (background dark))
  3309. (:foreground "grey70"))
  3310. (((class color) (min-colors 8) (background light))
  3311. (:foreground "green"))
  3312. (((class color) (min-colors 8) (background dark))
  3313. (:foreground "yellow"))))
  3314. "Face for fixed-with text like code snippets."
  3315. :group 'org-faces
  3316. :version "22.1")
  3317. (defface org-agenda-structure ;; font-lock-function-name-face
  3318. (org-compatible-face
  3319. nil
  3320. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3321. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3322. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3323. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3324. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3325. (t (:bold t))))
  3326. "Face used in agenda for captions and dates."
  3327. :group 'org-faces)
  3328. (defface org-scheduled-today
  3329. (org-compatible-face
  3330. nil
  3331. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3332. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3333. (((class color) (min-colors 8)) (:foreground "green"))
  3334. (t (:bold t :italic t))))
  3335. "Face for items scheduled for a certain day."
  3336. :group 'org-faces)
  3337. (defface org-scheduled-previously
  3338. (org-compatible-face
  3339. nil
  3340. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3341. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3342. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3343. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3344. (t (:bold t))))
  3345. "Face for items scheduled previously, and not yet done."
  3346. :group 'org-faces)
  3347. (defface org-upcoming-deadline
  3348. (org-compatible-face
  3349. nil
  3350. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3351. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3352. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3353. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3354. (t (:bold t))))
  3355. "Face for items scheduled previously, and not yet done."
  3356. :group 'org-faces)
  3357. (defcustom org-agenda-deadline-faces
  3358. '((1.0 . org-warning)
  3359. (0.5 . org-upcoming-deadline)
  3360. (0.0 . default))
  3361. "Faces for showing deadlines in the agenda.
  3362. This is a list of cons cells. The cdr of each cess is a face to be used,
  3363. and it can also just be a like like '(:foreground \"yellow\").
  3364. Each car is a fraction of the head-warning time that must have passed for
  3365. this the face in the cdr to be used for display. The numbers must be
  3366. given in descending order. The head-warning time is normally taken
  3367. from `org-deadline-warning-days', but can also be specified in the deadline
  3368. timestamp itself, like this:
  3369. DEADLINE: <2007-08-13 Mon -8d>
  3370. You may use d for days, w for weeks, m for months and y for years. Months
  3371. and years will only be treated in an approximate fashion (30.4 days for a
  3372. month and 365.24 days for a year)."
  3373. :group 'org-faces
  3374. :group 'org-agenda-daily/weekly
  3375. :type '(repeat
  3376. (cons
  3377. (number :tag "Fraction of head-warning time passed")
  3378. (sexp :tag "Face"))))
  3379. (defface org-time-grid ;; font-lock-variable-name-face
  3380. (org-compatible-face
  3381. nil
  3382. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3383. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3384. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3385. "Face used for time grids."
  3386. :group 'org-faces)
  3387. (defconst org-level-faces
  3388. '(org-level-1 org-level-2 org-level-3 org-level-4
  3389. org-level-5 org-level-6 org-level-7 org-level-8
  3390. ))
  3391. (defcustom org-n-level-faces (length org-level-faces)
  3392. "The number different faces to be used for headlines.
  3393. Org-mode defines 8 different headline faces, so this can be at most 8.
  3394. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3395. :type 'number
  3396. :group 'org-faces)
  3397. ;;; Variables for pre-computed regular expressions, all buffer local
  3398. (defvar org-drawer-regexp nil
  3399. "Matches first line of a hidden block.")
  3400. (make-variable-buffer-local 'org-drawer-regexp)
  3401. (defvar org-todo-regexp nil
  3402. "Matches any of the TODO state keywords.")
  3403. (make-variable-buffer-local 'org-todo-regexp)
  3404. (defvar org-not-done-regexp nil
  3405. "Matches any of the TODO state keywords except the last one.")
  3406. (make-variable-buffer-local 'org-not-done-regexp)
  3407. (defvar org-todo-line-regexp nil
  3408. "Matches a headline and puts TODO state into group 2 if present.")
  3409. (make-variable-buffer-local 'org-todo-line-regexp)
  3410. (defvar org-complex-heading-regexp nil
  3411. "Matches a headline and puts everything into groups:
  3412. group 1: the stars
  3413. group 2: The todo keyword, maybe
  3414. group 3: Priority cookie
  3415. group 4: True headline
  3416. group 5: Tags")
  3417. (make-variable-buffer-local 'org-complex-heading-regexp)
  3418. (defvar org-todo-line-tags-regexp nil
  3419. "Matches a headline and puts TODO state into group 2 if present.
  3420. Also put tags into group 4 if tags are present.")
  3421. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3422. (defvar org-nl-done-regexp nil
  3423. "Matches newline followed by a headline with the DONE keyword.")
  3424. (make-variable-buffer-local 'org-nl-done-regexp)
  3425. (defvar org-looking-at-done-regexp nil
  3426. "Matches the DONE keyword a point.")
  3427. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3428. (defvar org-ds-keyword-length 12
  3429. "Maximum length of the Deadline and SCHEDULED keywords.")
  3430. (make-variable-buffer-local 'org-ds-keyword-length)
  3431. (defvar org-deadline-regexp nil
  3432. "Matches the DEADLINE keyword.")
  3433. (make-variable-buffer-local 'org-deadline-regexp)
  3434. (defvar org-deadline-time-regexp nil
  3435. "Matches the DEADLINE keyword together with a time stamp.")
  3436. (make-variable-buffer-local 'org-deadline-time-regexp)
  3437. (defvar org-deadline-line-regexp nil
  3438. "Matches the DEADLINE keyword and the rest of the line.")
  3439. (make-variable-buffer-local 'org-deadline-line-regexp)
  3440. (defvar org-scheduled-regexp nil
  3441. "Matches the SCHEDULED keyword.")
  3442. (make-variable-buffer-local 'org-scheduled-regexp)
  3443. (defvar org-scheduled-time-regexp nil
  3444. "Matches the SCHEDULED keyword together with a time stamp.")
  3445. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3446. (defvar org-closed-time-regexp nil
  3447. "Matches the CLOSED keyword together with a time stamp.")
  3448. (make-variable-buffer-local 'org-closed-time-regexp)
  3449. (defvar org-keyword-time-regexp nil
  3450. "Matches any of the 4 keywords, together with the time stamp.")
  3451. (make-variable-buffer-local 'org-keyword-time-regexp)
  3452. (defvar org-keyword-time-not-clock-regexp nil
  3453. "Matches any of the 3 keywords, together with the time stamp.")
  3454. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3455. (defvar org-maybe-keyword-time-regexp nil
  3456. "Matches a timestamp, possibly preceeded by a keyword.")
  3457. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3458. (defvar org-planning-or-clock-line-re nil
  3459. "Matches a line with planning or clock info.")
  3460. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3461. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3462. rear-nonsticky t mouse-map t fontified t)
  3463. "Properties to remove when a string without properties is wanted.")
  3464. (defsubst org-match-string-no-properties (num &optional string)
  3465. (if (featurep 'xemacs)
  3466. (let ((s (match-string num string)))
  3467. (remove-text-properties 0 (length s) org-rm-props s)
  3468. s)
  3469. (match-string-no-properties num string)))
  3470. (defsubst org-no-properties (s)
  3471. (if (fboundp 'set-text-properties)
  3472. (set-text-properties 0 (length s) nil s)
  3473. (remove-text-properties 0 (length s) org-rm-props s))
  3474. s)
  3475. (defsubst org-get-alist-option (option key)
  3476. (cond ((eq key t) t)
  3477. ((eq option t) t)
  3478. ((assoc key option) (cdr (assoc key option)))
  3479. (t (cdr (assq 'default option)))))
  3480. (defsubst org-inhibit-invisibility ()
  3481. "Modified `buffer-invisibility-spec' for Emacs 21.
  3482. Some ops with invisible text do not work correctly on Emacs 21. For these
  3483. we turn off invisibility temporarily. Use this in a `let' form."
  3484. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3485. (defsubst org-set-local (var value)
  3486. "Make VAR local in current buffer and set it to VALUE."
  3487. (set (make-variable-buffer-local var) value))
  3488. (defsubst org-mode-p ()
  3489. "Check if the current buffer is in Org-mode."
  3490. (eq major-mode 'org-mode))
  3491. (defsubst org-last (list)
  3492. "Return the last element of LIST."
  3493. (car (last list)))
  3494. (defun org-let (list &rest body)
  3495. (eval (cons 'let (cons list body))))
  3496. (put 'org-let 'lisp-indent-function 1)
  3497. (defun org-let2 (list1 list2 &rest body)
  3498. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3499. (put 'org-let2 'lisp-indent-function 2)
  3500. (defconst org-startup-options
  3501. '(("fold" org-startup-folded t)
  3502. ("overview" org-startup-folded t)
  3503. ("nofold" org-startup-folded nil)
  3504. ("showall" org-startup-folded nil)
  3505. ("content" org-startup-folded content)
  3506. ("hidestars" org-hide-leading-stars t)
  3507. ("showstars" org-hide-leading-stars nil)
  3508. ("odd" org-odd-levels-only t)
  3509. ("oddeven" org-odd-levels-only nil)
  3510. ("align" org-startup-align-all-tables t)
  3511. ("noalign" org-startup-align-all-tables nil)
  3512. ("customtime" org-display-custom-times t)
  3513. ("logging" org-log-done t)
  3514. ("logdone" org-log-done t)
  3515. ("nologging" org-log-done nil)
  3516. ("lognotedone" org-log-done done push)
  3517. ("lognotestate" org-log-done state push)
  3518. ("lognoteclock-out" org-log-done clock-out push)
  3519. ("logrepeat" org-log-repeat t)
  3520. ("nologrepeat" org-log-repeat nil)
  3521. ("constcgs" constants-unit-system cgs)
  3522. ("constSI" constants-unit-system SI))
  3523. "Variable associated with STARTUP options for org-mode.
  3524. Each element is a list of three items: The startup options as written
  3525. in the #+STARTUP line, the corresponding variable, and the value to
  3526. set this variable to if the option is found. An optional forth element PUSH
  3527. means to push this value onto the list in the variable.")
  3528. (defun org-set-regexps-and-options ()
  3529. "Precompute regular expressions for current buffer."
  3530. (when (org-mode-p)
  3531. (org-set-local 'org-todo-kwd-alist nil)
  3532. (org-set-local 'org-todo-key-alist nil)
  3533. (org-set-local 'org-todo-key-trigger nil)
  3534. (org-set-local 'org-todo-keywords-1 nil)
  3535. (org-set-local 'org-done-keywords nil)
  3536. (org-set-local 'org-todo-heads nil)
  3537. (org-set-local 'org-todo-sets nil)
  3538. (org-set-local 'org-todo-log-states nil)
  3539. (let ((re (org-make-options-regexp
  3540. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3541. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3542. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3543. (splitre "[ \t]+")
  3544. kwds kws0 kwsa key value cat arch tags const links hw dws
  3545. tail sep kws1 prio props drawers
  3546. ex log)
  3547. (save-excursion
  3548. (save-restriction
  3549. (widen)
  3550. (goto-char (point-min))
  3551. (while (re-search-forward re nil t)
  3552. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3553. (cond
  3554. ((equal key "CATEGORY")
  3555. (if (string-match "[ \t]+$" value)
  3556. (setq value (replace-match "" t t value)))
  3557. (setq cat (intern value)))
  3558. ((member key '("SEQ_TODO" "TODO"))
  3559. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3560. ((equal key "TYP_TODO")
  3561. (push (cons 'type (org-split-string value splitre)) kwds))
  3562. ((equal key "TAGS")
  3563. (setq tags (append tags (org-split-string value splitre))))
  3564. ((equal key "COLUMNS")
  3565. (org-set-local 'org-columns-default-format value))
  3566. ((equal key "LINK")
  3567. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3568. (push (cons (match-string 1 value)
  3569. (org-trim (match-string 2 value)))
  3570. links)))
  3571. ((equal key "PRIORITIES")
  3572. (setq prio (org-split-string value " +")))
  3573. ((equal key "PROPERTY")
  3574. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3575. (push (cons (match-string 1 value) (match-string 2 value))
  3576. props)))
  3577. ((equal key "DRAWERS")
  3578. (setq drawers (org-split-string value splitre)))
  3579. ((equal key "CONSTANTS")
  3580. (setq const (append const (org-split-string value splitre))))
  3581. ((equal key "STARTUP")
  3582. (let ((opts (org-split-string value splitre))
  3583. l var val)
  3584. (while (setq l (pop opts))
  3585. (when (setq l (assoc l org-startup-options))
  3586. (setq var (nth 1 l) val (nth 2 l))
  3587. (if (not (nth 3 l))
  3588. (set (make-local-variable var) val)
  3589. (if (not (listp (symbol-value var)))
  3590. (set (make-local-variable var) nil))
  3591. (set (make-local-variable var) (symbol-value var))
  3592. (add-to-list var val))))))
  3593. ((equal key "ARCHIVE")
  3594. (string-match " *$" value)
  3595. (setq arch (replace-match "" t t value))
  3596. (remove-text-properties 0 (length arch)
  3597. '(face t fontified t) arch)))
  3598. )))
  3599. (and cat (org-set-local 'org-category cat))
  3600. (when prio
  3601. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3602. (setq prio (mapcar 'string-to-char prio))
  3603. (org-set-local 'org-highest-priority (nth 0 prio))
  3604. (org-set-local 'org-lowest-priority (nth 1 prio))
  3605. (org-set-local 'org-default-priority (nth 2 prio)))
  3606. (and props (org-set-local 'org-local-properties (nreverse props)))
  3607. (and drawers (org-set-local 'org-drawers drawers))
  3608. (and arch (org-set-local 'org-archive-location arch))
  3609. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3610. ;; Process the TODO keywords
  3611. (unless kwds
  3612. ;; Use the global values as if they had been given locally.
  3613. (setq kwds (default-value 'org-todo-keywords))
  3614. (if (stringp (car kwds))
  3615. (setq kwds (list (cons org-todo-interpretation
  3616. (default-value 'org-todo-keywords)))))
  3617. (setq kwds (reverse kwds)))
  3618. (setq kwds (nreverse kwds))
  3619. (let (inter kws kw)
  3620. (while (setq kws (pop kwds))
  3621. (setq inter (pop kws) sep (member "|" kws)
  3622. kws0 (delete "|" (copy-sequence kws))
  3623. kwsa nil
  3624. kws1 (mapcar
  3625. (lambda (x)
  3626. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3627. (progn
  3628. (setq kw (match-string 1 x)
  3629. ex (and (match-end 2) (match-string 2 x))
  3630. log (and ex (string-match "@" ex))
  3631. key (and ex (substring ex 0 1)))
  3632. (if (equal key "@") (setq key nil))
  3633. (push (cons kw (and key (string-to-char key))) kwsa)
  3634. (and log (push kw org-todo-log-states))
  3635. kw)
  3636. (error "Invalid TODO keyword %s" x)))
  3637. kws0)
  3638. kwsa (if kwsa (append '((:startgroup))
  3639. (nreverse kwsa)
  3640. '((:endgroup))))
  3641. hw (car kws1)
  3642. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3643. tail (list inter hw (car dws) (org-last dws)))
  3644. (add-to-list 'org-todo-heads hw 'append)
  3645. (push kws1 org-todo-sets)
  3646. (setq org-done-keywords (append org-done-keywords dws nil))
  3647. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3648. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3649. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3650. (setq org-todo-sets (nreverse org-todo-sets)
  3651. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3652. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3653. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3654. ;; Process the constants
  3655. (when const
  3656. (let (e cst)
  3657. (while (setq e (pop const))
  3658. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3659. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3660. (setq org-table-formula-constants-local cst)))
  3661. ;; Process the tags.
  3662. (when tags
  3663. (let (e tgs)
  3664. (while (setq e (pop tags))
  3665. (cond
  3666. ((equal e "{") (push '(:startgroup) tgs))
  3667. ((equal e "}") (push '(:endgroup) tgs))
  3668. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3669. (push (cons (match-string 1 e)
  3670. (string-to-char (match-string 2 e)))
  3671. tgs))
  3672. (t (push (list e) tgs))))
  3673. (org-set-local 'org-tag-alist nil)
  3674. (while (setq e (pop tgs))
  3675. (or (and (stringp (car e))
  3676. (assoc (car e) org-tag-alist))
  3677. (push e org-tag-alist))))))
  3678. ;; Compute the regular expressions and other local variables
  3679. (if (not org-done-keywords)
  3680. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3681. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3682. (length org-scheduled-string)))
  3683. org-drawer-regexp
  3684. (concat "^[ \t]*:\\("
  3685. (mapconcat 'regexp-quote org-drawers "\\|")
  3686. "\\):[ \t]*$")
  3687. org-not-done-keywords
  3688. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3689. org-todo-regexp
  3690. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3691. "\\|") "\\)\\>")
  3692. org-not-done-regexp
  3693. (concat "\\<\\("
  3694. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3695. "\\)\\>")
  3696. org-todo-line-regexp
  3697. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3698. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3699. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3700. org-complex-heading-regexp
  3701. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  3702. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3703. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3704. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3705. org-nl-done-regexp
  3706. (concat "\n\\*+[ \t]+"
  3707. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3708. "\\)" "\\>")
  3709. org-todo-line-tags-regexp
  3710. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3711. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3712. (org-re
  3713. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3714. org-looking-at-done-regexp
  3715. (concat "^" "\\(?:"
  3716. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3717. "\\>")
  3718. org-deadline-regexp (concat "\\<" org-deadline-string)
  3719. org-deadline-time-regexp
  3720. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3721. org-deadline-line-regexp
  3722. (concat "\\<\\(" org-deadline-string "\\).*")
  3723. org-scheduled-regexp
  3724. (concat "\\<" org-scheduled-string)
  3725. org-scheduled-time-regexp
  3726. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3727. org-closed-time-regexp
  3728. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3729. org-keyword-time-regexp
  3730. (concat "\\<\\(" org-scheduled-string
  3731. "\\|" org-deadline-string
  3732. "\\|" org-closed-string
  3733. "\\|" org-clock-string "\\)"
  3734. " *[[<]\\([^]>]+\\)[]>]")
  3735. org-keyword-time-not-clock-regexp
  3736. (concat "\\<\\(" org-scheduled-string
  3737. "\\|" org-deadline-string
  3738. "\\|" org-closed-string
  3739. "\\)"
  3740. " *[[<]\\([^]>]+\\)[]>]")
  3741. org-maybe-keyword-time-regexp
  3742. (concat "\\(\\<\\(" org-scheduled-string
  3743. "\\|" org-deadline-string
  3744. "\\|" org-closed-string
  3745. "\\|" org-clock-string "\\)\\)?"
  3746. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3747. org-planning-or-clock-line-re
  3748. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3749. "\\|" org-deadline-string
  3750. "\\|" org-closed-string "\\|" org-clock-string
  3751. "\\)\\>\\)")
  3752. )
  3753. (org-set-font-lock-defaults)))
  3754. (defun org-remove-keyword-keys (list)
  3755. (mapcar (lambda (x)
  3756. (if (string-match "(..?)$" x)
  3757. (substring x 0 (match-beginning 0))
  3758. x))
  3759. list))
  3760. ;;; Some variables ujsed in various places
  3761. (defvar org-window-configuration nil
  3762. "Used in various places to store a window configuration.")
  3763. (defvar org-finish-function nil
  3764. "Function to be called when `C-c C-c' is used.
  3765. This is for getting out of special buffers like remember.")
  3766. ;;; Foreign variables, to inform the compiler
  3767. ;; XEmacs only
  3768. (defvar outline-mode-menu-heading)
  3769. (defvar outline-mode-menu-show)
  3770. (defvar outline-mode-menu-hide)
  3771. (defvar zmacs-regions) ; XEmacs regions
  3772. ;; Emacs only
  3773. (defvar mark-active)
  3774. ;; Packages that org-mode interacts with
  3775. (defvar calc-embedded-close-formula)
  3776. (defvar calc-embedded-open-formula)
  3777. (defvar font-lock-unfontify-region-function)
  3778. (defvar org-goto-start-pos)
  3779. (defvar vm-message-pointer)
  3780. (defvar vm-folder-directory)
  3781. (defvar wl-summary-buffer-elmo-folder)
  3782. (defvar wl-summary-buffer-folder-name)
  3783. (defvar gnus-other-frame-object)
  3784. (defvar gnus-group-name)
  3785. (defvar gnus-article-current)
  3786. (defvar w3m-current-url)
  3787. (defvar w3m-current-title)
  3788. (defvar mh-progs)
  3789. (defvar mh-current-folder)
  3790. (defvar mh-show-folder-buffer)
  3791. (defvar mh-index-folder)
  3792. (defvar mh-searcher)
  3793. (defvar calendar-mode-map)
  3794. (defvar Info-current-file)
  3795. (defvar Info-current-node)
  3796. (defvar texmathp-why)
  3797. (defvar remember-save-after-remembering)
  3798. (defvar remember-data-file)
  3799. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3800. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3801. (defvar org-latex-regexps)
  3802. (defvar constants-unit-system)
  3803. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3804. ;; FIXME: Occasionally check by commenting these, to make sure
  3805. ;; no other functions uses these, forgetting to let-bind them.
  3806. (defvar entry)
  3807. (defvar state)
  3808. (defvar last-state)
  3809. (defvar date)
  3810. (defvar description)
  3811. ;; Defined somewhere in this file, but used before definition.
  3812. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3813. (defvar org-agenda-buffer-name)
  3814. (defvar org-agenda-undo-list)
  3815. (defvar org-agenda-pending-undo-list)
  3816. (defvar org-agenda-overriding-header)
  3817. (defvar orgtbl-mode)
  3818. (defvar org-html-entities)
  3819. (defvar org-struct-menu)
  3820. (defvar org-org-menu)
  3821. (defvar org-tbl-menu)
  3822. (defvar org-agenda-keymap)
  3823. ;;;; Emacs/XEmacs compatibility
  3824. ;; Overlay compatibility functions
  3825. (defun org-make-overlay (beg end &optional buffer)
  3826. (if (featurep 'xemacs)
  3827. (make-extent beg end buffer)
  3828. (make-overlay beg end buffer)))
  3829. (defun org-delete-overlay (ovl)
  3830. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3831. (defun org-detach-overlay (ovl)
  3832. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3833. (defun org-move-overlay (ovl beg end &optional buffer)
  3834. (if (featurep 'xemacs)
  3835. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3836. (move-overlay ovl beg end buffer)))
  3837. (defun org-overlay-put (ovl prop value)
  3838. (if (featurep 'xemacs)
  3839. (set-extent-property ovl prop value)
  3840. (overlay-put ovl prop value)))
  3841. (defun org-overlay-display (ovl text &optional face evap)
  3842. "Make overlay OVL display TEXT with face FACE."
  3843. (if (featurep 'xemacs)
  3844. (let ((gl (make-glyph text)))
  3845. (and face (set-glyph-face gl face))
  3846. (set-extent-property ovl 'invisible t)
  3847. (set-extent-property ovl 'end-glyph gl))
  3848. (overlay-put ovl 'display text)
  3849. (if face (overlay-put ovl 'face face))
  3850. (if evap (overlay-put ovl 'evaporate t))))
  3851. (defun org-overlay-before-string (ovl text &optional face evap)
  3852. "Make overlay OVL display TEXT with face FACE."
  3853. (if (featurep 'xemacs)
  3854. (let ((gl (make-glyph text)))
  3855. (and face (set-glyph-face gl face))
  3856. (set-extent-property ovl 'begin-glyph gl))
  3857. (if face (org-add-props text nil 'face face))
  3858. (overlay-put ovl 'before-string text)
  3859. (if evap (overlay-put ovl 'evaporate t))))
  3860. (defun org-overlay-get (ovl prop)
  3861. (if (featurep 'xemacs)
  3862. (extent-property ovl prop)
  3863. (overlay-get ovl prop)))
  3864. (defun org-overlays-at (pos)
  3865. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3866. (defun org-overlays-in (&optional start end)
  3867. (if (featurep 'xemacs)
  3868. (extent-list nil start end)
  3869. (overlays-in start end)))
  3870. (defun org-overlay-start (o)
  3871. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3872. (defun org-overlay-end (o)
  3873. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3874. (defun org-find-overlays (prop &optional pos delete)
  3875. "Find all overlays specifying PROP at POS or point.
  3876. If DELETE is non-nil, delete all those overlays."
  3877. (let ((overlays (org-overlays-at (or pos (point))))
  3878. ov found)
  3879. (while (setq ov (pop overlays))
  3880. (if (org-overlay-get ov prop)
  3881. (if delete (org-delete-overlay ov) (push ov found))))
  3882. found))
  3883. ;; Region compatibility
  3884. (defun org-add-hook (hook function &optional append local)
  3885. "Add-hook, compatible with both Emacsen."
  3886. (if (and local (featurep 'xemacs))
  3887. (add-local-hook hook function append)
  3888. (add-hook hook function append local)))
  3889. (defvar org-ignore-region nil
  3890. "To temporarily disable the active region.")
  3891. (defun org-region-active-p ()
  3892. "Is `transient-mark-mode' on and the region active?
  3893. Works on both Emacs and XEmacs."
  3894. (if org-ignore-region
  3895. nil
  3896. (if (featurep 'xemacs)
  3897. (and zmacs-regions (region-active-p))
  3898. (and transient-mark-mode mark-active))))
  3899. ;; Invisibility compatibility
  3900. (defun org-add-to-invisibility-spec (arg)
  3901. "Add elements to `buffer-invisibility-spec'.
  3902. See documentation for `buffer-invisibility-spec' for the kind of elements
  3903. that can be added."
  3904. (cond
  3905. ((fboundp 'add-to-invisibility-spec)
  3906. (add-to-invisibility-spec arg))
  3907. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3908. (setq buffer-invisibility-spec (list arg)))
  3909. (t
  3910. (setq buffer-invisibility-spec
  3911. (cons arg buffer-invisibility-spec)))))
  3912. (defun org-remove-from-invisibility-spec (arg)
  3913. "Remove elements from `buffer-invisibility-spec'."
  3914. (if (fboundp 'remove-from-invisibility-spec)
  3915. (remove-from-invisibility-spec arg)
  3916. (if (consp buffer-invisibility-spec)
  3917. (setq buffer-invisibility-spec
  3918. (delete arg buffer-invisibility-spec)))))
  3919. (defun org-in-invisibility-spec-p (arg)
  3920. "Is ARG a member of `buffer-invisibility-spec'?"
  3921. (if (consp buffer-invisibility-spec)
  3922. (member arg buffer-invisibility-spec)
  3923. nil))
  3924. ;;;; Define the Org-mode
  3925. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3926. (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."))
  3927. ;; We use a before-change function to check if a table might need
  3928. ;; an update.
  3929. (defvar org-table-may-need-update t
  3930. "Indicates that a table might need an update.
  3931. This variable is set by `org-before-change-function'.
  3932. `org-table-align' sets it back to nil.")
  3933. (defvar org-mode-map)
  3934. (defvar org-mode-hook nil)
  3935. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3936. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3937. (defvar org-table-buffer-is-an nil)
  3938. (defconst org-outline-regexp "\\*+ ")
  3939. ;;;###autoload
  3940. (define-derived-mode org-mode outline-mode "Org"
  3941. "Outline-based notes management and organizer, alias
  3942. \"Carsten's outline-mode for keeping track of everything.\"
  3943. Org-mode develops organizational tasks around a NOTES file which
  3944. contains information about projects as plain text. Org-mode is
  3945. implemented on top of outline-mode, which is ideal to keep the content
  3946. of large files well structured. It supports ToDo items, deadlines and
  3947. time stamps, which magically appear in the diary listing of the Emacs
  3948. calendar. Tables are easily created with a built-in table editor.
  3949. Plain text URL-like links connect to websites, emails (VM), Usenet
  3950. messages (Gnus), BBDB entries, and any files related to the project.
  3951. For printing and sharing of notes, an Org-mode file (or a part of it)
  3952. can be exported as a structured ASCII or HTML file.
  3953. The following commands are available:
  3954. \\{org-mode-map}"
  3955. ;; Get rid of Outline menus, they are not needed
  3956. ;; Need to do this here because define-derived-mode sets up
  3957. ;; the keymap so late. Still, it is a waste to call this each time
  3958. ;; we switch another buffer into org-mode.
  3959. (if (featurep 'xemacs)
  3960. (when (boundp 'outline-mode-menu-heading)
  3961. ;; Assume this is Greg's port, it used easymenu
  3962. (easy-menu-remove outline-mode-menu-heading)
  3963. (easy-menu-remove outline-mode-menu-show)
  3964. (easy-menu-remove outline-mode-menu-hide))
  3965. (define-key org-mode-map [menu-bar headings] 'undefined)
  3966. (define-key org-mode-map [menu-bar hide] 'undefined)
  3967. (define-key org-mode-map [menu-bar show] 'undefined))
  3968. (easy-menu-add org-org-menu)
  3969. (easy-menu-add org-tbl-menu)
  3970. (org-install-agenda-files-menu)
  3971. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3972. (org-add-to-invisibility-spec '(org-cwidth))
  3973. (when (featurep 'xemacs)
  3974. (org-set-local 'line-move-ignore-invisible t))
  3975. (org-set-local 'outline-regexp org-outline-regexp)
  3976. (org-set-local 'outline-level 'org-outline-level)
  3977. (when (and org-ellipsis
  3978. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3979. (fboundp 'make-glyph-code))
  3980. (unless org-display-table
  3981. (setq org-display-table (make-display-table)))
  3982. (set-display-table-slot
  3983. org-display-table 4
  3984. (vconcat (mapcar
  3985. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3986. org-ellipsis)))
  3987. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3988. (setq buffer-display-table org-display-table))
  3989. (org-set-regexps-and-options)
  3990. ;; Calc embedded
  3991. (org-set-local 'calc-embedded-open-mode "# ")
  3992. (modify-syntax-entry ?# "<")
  3993. (modify-syntax-entry ?@ "w")
  3994. (if org-startup-truncated (setq truncate-lines t))
  3995. (org-set-local 'font-lock-unfontify-region-function
  3996. 'org-unfontify-region)
  3997. ;; Activate before-change-function
  3998. (org-set-local 'org-table-may-need-update t)
  3999. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4000. 'local)
  4001. ;; Check for running clock before killing a buffer
  4002. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4003. ;; Paragraphs and auto-filling
  4004. (org-set-autofill-regexps)
  4005. (setq indent-line-function 'org-indent-line-function)
  4006. (org-update-radio-target-regexp)
  4007. ;; Comment characters
  4008. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4009. (org-set-local 'comment-padding " ")
  4010. ;; Make isearch reveal context
  4011. (if (or (featurep 'xemacs)
  4012. (not (boundp 'outline-isearch-open-invisible-function)))
  4013. ;; Emacs 21 and XEmacs make use of the hook
  4014. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4015. ;; Emacs 22 deals with this through a special variable
  4016. (org-set-local 'outline-isearch-open-invisible-function
  4017. (lambda (&rest ignore) (org-show-context 'isearch))))
  4018. ;; If empty file that did not turn on org-mode automatically, make it to.
  4019. (if (and org-insert-mode-line-in-empty-file
  4020. (interactive-p)
  4021. (= (point-min) (point-max)))
  4022. (insert "# -*- mode: org -*-\n\n"))
  4023. (unless org-inhibit-startup
  4024. (when org-startup-align-all-tables
  4025. (let ((bmp (buffer-modified-p)))
  4026. (org-table-map-tables 'org-table-align)
  4027. (set-buffer-modified-p bmp)))
  4028. (org-cycle-hide-drawers 'all)
  4029. (cond
  4030. ((eq org-startup-folded t)
  4031. (org-cycle '(4)))
  4032. ((eq org-startup-folded 'content)
  4033. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4034. (org-cycle '(4)) (org-cycle '(4)))))))
  4035. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4036. (defsubst org-call-with-arg (command arg)
  4037. "Call COMMAND interactively, but pretend prefix are was ARG."
  4038. (let ((current-prefix-arg arg)) (call-interactively command)))
  4039. (defsubst org-current-line (&optional pos)
  4040. (save-excursion
  4041. (and pos (goto-char pos))
  4042. ;; works also in narrowed buffer, because we start at 1, not point-min
  4043. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4044. (defun org-current-time ()
  4045. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4046. (if (> org-time-stamp-rounding-minutes 0)
  4047. (let ((r org-time-stamp-rounding-minutes)
  4048. (time (decode-time)))
  4049. (apply 'encode-time
  4050. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4051. (nthcdr 2 time))))
  4052. (current-time)))
  4053. (defun org-add-props (string plist &rest props)
  4054. "Add text properties to entire string, from beginning to end.
  4055. PLIST may be a list of properties, PROPS are individual properties and values
  4056. that will be added to PLIST. Returns the string that was modified."
  4057. (add-text-properties
  4058. 0 (length string) (if props (append plist props) plist) string)
  4059. string)
  4060. (put 'org-add-props 'lisp-indent-function 2)
  4061. ;;;; Font-Lock stuff, including the activators
  4062. (defvar org-mouse-map (make-sparse-keymap))
  4063. (org-defkey org-mouse-map
  4064. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4065. (org-defkey org-mouse-map
  4066. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4067. (when org-mouse-1-follows-link
  4068. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4069. (when org-tab-follows-link
  4070. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4071. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4072. (when org-return-follows-link
  4073. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4074. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4075. (require 'font-lock)
  4076. (defconst org-non-link-chars "]\t\n\r<>")
  4077. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4078. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  4079. (defvar org-link-re-with-space nil
  4080. "Matches a link with spaces, optional angular brackets around it.")
  4081. (defvar org-link-re-with-space2 nil
  4082. "Matches a link with spaces, optional angular brackets around it.")
  4083. (defvar org-angle-link-re nil
  4084. "Matches link with angular brackets, spaces are allowed.")
  4085. (defvar org-plain-link-re nil
  4086. "Matches plain link, without spaces.")
  4087. (defvar org-bracket-link-regexp nil
  4088. "Matches a link in double brackets.")
  4089. (defvar org-bracket-link-analytic-regexp nil
  4090. "Regular expression used to analyze links.
  4091. Here is what the match groups contain after a match:
  4092. 1: http:
  4093. 2: http
  4094. 3: path
  4095. 4: [desc]
  4096. 5: desc")
  4097. (defvar org-any-link-re nil
  4098. "Regular expression matching any link.")
  4099. (defun org-make-link-regexps ()
  4100. "Update the link regular expressions.
  4101. This should be called after the variable `org-link-types' has changed."
  4102. (setq org-link-re-with-space
  4103. (concat
  4104. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4105. "\\([^" org-non-link-chars " ]"
  4106. "[^" org-non-link-chars "]*"
  4107. "[^" org-non-link-chars " ]\\)>?")
  4108. org-link-re-with-space2
  4109. (concat
  4110. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4111. "\\([^" org-non-link-chars " ]"
  4112. "[^]\t\n\r]*"
  4113. "[^" org-non-link-chars " ]\\)>?")
  4114. org-angle-link-re
  4115. (concat
  4116. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4117. "\\([^" org-non-link-chars " ]"
  4118. "[^" org-non-link-chars "]*"
  4119. "\\)>")
  4120. org-plain-link-re
  4121. (concat
  4122. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4123. "\\([^]\t\n\r<>,;() ]+\\)")
  4124. org-bracket-link-regexp
  4125. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4126. org-bracket-link-analytic-regexp
  4127. (concat
  4128. "\\[\\["
  4129. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4130. "\\([^]]+\\)"
  4131. "\\]"
  4132. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4133. "\\]")
  4134. org-any-link-re
  4135. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4136. org-angle-link-re "\\)\\|\\("
  4137. org-plain-link-re "\\)")))
  4138. (org-make-link-regexps)
  4139. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4140. "Regular expression for fast time stamp matching.")
  4141. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4142. "Regular expression for fast time stamp matching.")
  4143. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4144. "Regular expression matching time strings for analysis.
  4145. This one does not require the space after the date.")
  4146. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4147. "Regular expression matching time strings for analysis.")
  4148. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4149. "Regular expression matching time stamps, with groups.")
  4150. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4151. "Regular expression matching time stamps (also [..]), with groups.")
  4152. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4153. "Regular expression matching a time stamp range.")
  4154. (defconst org-tr-regexp-both
  4155. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4156. "Regular expression matching a time stamp range.")
  4157. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4158. org-ts-regexp "\\)?")
  4159. "Regular expression matching a time stamp or time stamp range.")
  4160. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4161. org-ts-regexp-both "\\)?")
  4162. "Regular expression matching a time stamp or time stamp range.
  4163. The time stamps may be either active or inactive.")
  4164. (defvar org-emph-face nil)
  4165. (defun org-do-emphasis-faces (limit)
  4166. "Run through the buffer and add overlays to links."
  4167. (let (rtn)
  4168. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4169. (if (not (= (char-after (match-beginning 3))
  4170. (char-after (match-beginning 4))))
  4171. (progn
  4172. (setq rtn t)
  4173. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4174. 'face
  4175. (nth 1 (assoc (match-string 3)
  4176. org-emphasis-alist)))
  4177. (add-text-properties (match-beginning 2) (match-end 2)
  4178. '(font-lock-multiline t))
  4179. (backward-char 1))))
  4180. rtn))
  4181. (defun org-emphasize (&optional char)
  4182. "Insert or change an emphasis, i.e. a font like bold or italic.
  4183. If there is an active region, change that region to a new emphasis.
  4184. If there is no region, just insert the marker characters and position
  4185. the cursor between them.
  4186. CHAR should be either the marker character, or the first character of the
  4187. HTML tag associated with that emphasis. If CHAR is a space, the means
  4188. to remove the emphasis of the selected region.
  4189. If char is not given (for example in an interactive call) it
  4190. will be prompted for."
  4191. (interactive)
  4192. (let ((eal org-emphasis-alist) e det
  4193. (erc org-emphasis-regexp-components)
  4194. (prompt "")
  4195. (string "") beg end move tag c s)
  4196. (if (org-region-active-p)
  4197. (setq beg (region-beginning) end (region-end)
  4198. string (buffer-substring beg end))
  4199. (setq move t))
  4200. (while (setq e (pop eal))
  4201. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4202. c (aref tag 0))
  4203. (push (cons c (string-to-char (car e))) det)
  4204. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4205. (substring tag 1)))))
  4206. (unless char
  4207. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4208. (setq char (read-char-exclusive)))
  4209. (setq char (or (cdr (assoc char det)) char))
  4210. (if (equal char ?\ )
  4211. (setq s "" move nil)
  4212. (unless (assoc (char-to-string char) org-emphasis-alist)
  4213. (error "No such emphasis marker: \"%c\"" char))
  4214. (setq s (char-to-string char)))
  4215. (while (and (> (length string) 1)
  4216. (equal (substring string 0 1) (substring string -1))
  4217. (assoc (substring string 0 1) org-emphasis-alist))
  4218. (setq string (substring string 1 -1)))
  4219. (setq string (concat s string s))
  4220. (if beg (delete-region beg end))
  4221. (unless (or (bolp)
  4222. (string-match (concat "[" (nth 0 erc) "\n]")
  4223. (char-to-string (char-before (point)))))
  4224. (insert " "))
  4225. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4226. (char-to-string (char-after (point))))
  4227. (insert " ") (backward-char 1))
  4228. (insert string)
  4229. (and move (backward-char 1))))
  4230. (defconst org-nonsticky-props
  4231. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4232. (defun org-activate-plain-links (limit)
  4233. "Run through the buffer and add overlays to links."
  4234. (catch 'exit
  4235. (let (f)
  4236. (while (re-search-forward org-plain-link-re limit t)
  4237. (setq f (get-text-property (match-beginning 0) 'face))
  4238. (if (or (eq f 'org-tag)
  4239. (and (listp f) (memq 'org-tag f)))
  4240. nil
  4241. (add-text-properties (match-beginning 0) (match-end 0)
  4242. (list 'mouse-face 'highlight
  4243. 'rear-nonsticky org-nonsticky-props
  4244. 'keymap org-mouse-map
  4245. ))
  4246. (throw 'exit t))))))
  4247. (defun org-activate-code (limit)
  4248. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4249. (unless (get-text-property (match-beginning 1) 'face)
  4250. (remove-text-properties (match-beginning 0) (match-end 0)
  4251. '(display t invisible t intangible t))
  4252. t)))
  4253. (defun org-activate-angle-links (limit)
  4254. "Run through the buffer and add overlays to links."
  4255. (if (re-search-forward org-angle-link-re limit t)
  4256. (progn
  4257. (add-text-properties (match-beginning 0) (match-end 0)
  4258. (list 'mouse-face 'highlight
  4259. 'rear-nonsticky org-nonsticky-props
  4260. 'keymap org-mouse-map
  4261. ))
  4262. t)))
  4263. (defmacro org-maybe-intangible (props)
  4264. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4265. In emacs 21, invisible text is not avoided by the command loop, so the
  4266. intangible property is needed to make sure point skips this text.
  4267. In Emacs 22, this is not necessary. The intangible text property has
  4268. led to problems with flyspell. These problems are fixed in flyspell.el,
  4269. but we still avoid setting the property in Emacs 22 and later.
  4270. We use a macro so that the test can happen at compilation time."
  4271. (if (< emacs-major-version 22)
  4272. `(append '(intangible t) ,props)
  4273. props))
  4274. (defun org-activate-bracket-links (limit)
  4275. "Run through the buffer and add overlays to bracketed links."
  4276. (if (re-search-forward org-bracket-link-regexp limit t)
  4277. (let* ((help (concat "LINK: "
  4278. (org-match-string-no-properties 1)))
  4279. ;; FIXME: above we should remove the escapes.
  4280. ;; but that requires another match, protecting match data,
  4281. ;; a lot of overhead for font-lock.
  4282. (ip (org-maybe-intangible
  4283. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4284. 'keymap org-mouse-map 'mouse-face 'highlight
  4285. 'help-echo help)))
  4286. (vp (list 'rear-nonsticky org-nonsticky-props
  4287. 'keymap org-mouse-map 'mouse-face 'highlight
  4288. 'help-echo help)))
  4289. ;; We need to remove the invisible property here. Table narrowing
  4290. ;; may have made some of this invisible.
  4291. (remove-text-properties (match-beginning 0) (match-end 0)
  4292. '(invisible nil))
  4293. (if (match-end 3)
  4294. (progn
  4295. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4296. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4297. (add-text-properties (match-end 3) (match-end 0) ip))
  4298. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4299. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4300. (add-text-properties (match-end 1) (match-end 0) ip))
  4301. t)))
  4302. (defun org-activate-dates (limit)
  4303. "Run through the buffer and add overlays to dates."
  4304. (if (re-search-forward org-tsr-regexp-both limit t)
  4305. (progn
  4306. (add-text-properties (match-beginning 0) (match-end 0)
  4307. (list 'mouse-face 'highlight
  4308. 'rear-nonsticky org-nonsticky-props
  4309. 'keymap org-mouse-map))
  4310. (when org-display-custom-times
  4311. (if (match-end 3)
  4312. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4313. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4314. t)))
  4315. (defvar org-target-link-regexp nil
  4316. "Regular expression matching radio targets in plain text.")
  4317. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4318. "Regular expression matching a link target.")
  4319. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4320. "Regular expression matching a radio target.")
  4321. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4322. "Regular expression matching any target.")
  4323. (defun org-activate-target-links (limit)
  4324. "Run through the buffer and add overlays to target matches."
  4325. (when org-target-link-regexp
  4326. (let ((case-fold-search t))
  4327. (if (re-search-forward org-target-link-regexp limit t)
  4328. (progn
  4329. (add-text-properties (match-beginning 0) (match-end 0)
  4330. (list 'mouse-face 'highlight
  4331. 'rear-nonsticky org-nonsticky-props
  4332. 'keymap org-mouse-map
  4333. 'help-echo "Radio target link"
  4334. 'org-linked-text t))
  4335. t)))))
  4336. (defun org-update-radio-target-regexp ()
  4337. "Find all radio targets in this file and update the regular expression."
  4338. (interactive)
  4339. (when (memq 'radio org-activate-links)
  4340. (setq org-target-link-regexp
  4341. (org-make-target-link-regexp (org-all-targets 'radio)))
  4342. (org-restart-font-lock)))
  4343. (defun org-hide-wide-columns (limit)
  4344. (let (s e)
  4345. (setq s (text-property-any (point) (or limit (point-max))
  4346. 'org-cwidth t))
  4347. (when s
  4348. (setq e (next-single-property-change s 'org-cwidth))
  4349. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4350. (goto-char e)
  4351. t)))
  4352. (defun org-restart-font-lock ()
  4353. "Restart font-lock-mode, to force refontification."
  4354. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4355. (font-lock-mode -1)
  4356. (font-lock-mode 1)))
  4357. (defun org-all-targets (&optional radio)
  4358. "Return a list of all targets in this file.
  4359. With optional argument RADIO, only find radio targets."
  4360. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4361. rtn)
  4362. (save-excursion
  4363. (goto-char (point-min))
  4364. (while (re-search-forward re nil t)
  4365. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4366. rtn)))
  4367. (defun org-make-target-link-regexp (targets)
  4368. "Make regular expression matching all strings in TARGETS.
  4369. The regular expression finds the targets also if there is a line break
  4370. between words."
  4371. (and targets
  4372. (concat
  4373. "\\<\\("
  4374. (mapconcat
  4375. (lambda (x)
  4376. (while (string-match " +" x)
  4377. (setq x (replace-match "\\s-+" t t x)))
  4378. x)
  4379. targets
  4380. "\\|")
  4381. "\\)\\>")))
  4382. (defun org-activate-tags (limit)
  4383. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4384. (progn
  4385. (add-text-properties (match-beginning 1) (match-end 1)
  4386. (list 'mouse-face 'highlight
  4387. 'rear-nonsticky org-nonsticky-props
  4388. 'keymap org-mouse-map))
  4389. t)))
  4390. (defun org-outline-level ()
  4391. (save-excursion
  4392. (looking-at outline-regexp)
  4393. (if (match-beginning 1)
  4394. (+ (org-get-string-indentation (match-string 1)) 1000)
  4395. (1- (- (match-end 0) (match-beginning 0))))))
  4396. (defvar org-font-lock-keywords nil)
  4397. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4398. "Regular expression matching a property line.")
  4399. (defun org-set-font-lock-defaults ()
  4400. (let* ((em org-fontify-emphasized-text)
  4401. (lk org-activate-links)
  4402. (org-font-lock-extra-keywords
  4403. (list
  4404. ;; Headlines
  4405. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4406. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4407. ;; Table lines
  4408. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4409. (1 'org-table t))
  4410. ;; Table internals
  4411. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4412. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4413. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4414. ;; Drawers
  4415. (list org-drawer-regexp '(0 'org-special-keyword t))
  4416. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4417. ;; Properties
  4418. (list org-property-re
  4419. '(1 'org-special-keyword t)
  4420. '(3 'org-property-value t))
  4421. (if org-format-transports-properties-p
  4422. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4423. ;; Links
  4424. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4425. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4426. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4427. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4428. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4429. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4430. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4431. '(org-hide-wide-columns (0 nil append))
  4432. ;; TODO lines
  4433. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4434. '(1 (org-get-todo-face 1) t))
  4435. ;; DONE
  4436. (if org-fontify-done-headline
  4437. (list (concat "^[*]+ +\\<\\("
  4438. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4439. "\\)\\(.*\\)")
  4440. '(2 'org-headline-done t))
  4441. nil)
  4442. ;; Priorities
  4443. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4444. ;; Special keywords
  4445. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4446. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4447. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4448. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4449. ;; Emphasis
  4450. (if em
  4451. (if (featurep 'xemacs)
  4452. '(org-do-emphasis-faces (0 nil append))
  4453. '(org-do-emphasis-faces)))
  4454. ;; Checkboxes
  4455. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4456. 2 'bold prepend)
  4457. (if org-provide-checkbox-statistics
  4458. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4459. (0 (org-get-checkbox-statistics-face) t)))
  4460. ;; COMMENT
  4461. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4462. "\\|" org-quote-string "\\)\\>")
  4463. '(1 'org-special-keyword t))
  4464. '("^#.*" (0 'font-lock-comment-face t))
  4465. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  4466. ;; Code
  4467. '(org-activate-code (1 'org-code t))
  4468. )))
  4469. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4470. ;; Now set the full font-lock-keywords
  4471. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4472. (org-set-local 'font-lock-defaults
  4473. '(org-font-lock-keywords t nil nil backward-paragraph))
  4474. (kill-local-variable 'font-lock-keywords) nil))
  4475. (defvar org-m nil)
  4476. (defvar org-l nil)
  4477. (defvar org-f nil)
  4478. (defun org-get-level-face (n)
  4479. "Get the right face for match N in font-lock matching of healdines."
  4480. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4481. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4482. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4483. (cond
  4484. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4485. ((eq n 2) org-f)
  4486. (t (if org-level-color-stars-only nil org-f))))
  4487. (defun org-get-todo-face (kwd)
  4488. "Get the right face for a TODO keyword KWD.
  4489. If KWD is a number, get the corresponding match group."
  4490. (if (numberp kwd) (setq kwd (match-string kwd)))
  4491. (or (cdr (assoc kwd org-todo-keyword-faces))
  4492. (and (member kwd org-done-keywords) 'org-done)
  4493. 'org-todo))
  4494. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4495. "Remove fontification and activation overlays from links."
  4496. (font-lock-default-unfontify-region beg end)
  4497. (let* ((buffer-undo-list t)
  4498. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4499. (inhibit-modification-hooks t)
  4500. deactivate-mark buffer-file-name buffer-file-truename)
  4501. (remove-text-properties beg end
  4502. '(mouse-face t keymap t org-linked-text t
  4503. invisible t intangible t))))
  4504. ;;;; Visibility cycling, including org-goto and indirect buffer
  4505. ;;; Cycling
  4506. (defvar org-cycle-global-status nil)
  4507. (make-variable-buffer-local 'org-cycle-global-status)
  4508. (defvar org-cycle-subtree-status nil)
  4509. (make-variable-buffer-local 'org-cycle-subtree-status)
  4510. ;;;###autoload
  4511. (defun org-cycle (&optional arg)
  4512. "Visibility cycling for Org-mode.
  4513. - When this function is called with a prefix argument, rotate the entire
  4514. buffer through 3 states (global cycling)
  4515. 1. OVERVIEW: Show only top-level headlines.
  4516. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4517. 3. SHOW ALL: Show everything.
  4518. - When point is at the beginning of a headline, rotate the subtree started
  4519. by this line through 3 different states (local cycling)
  4520. 1. FOLDED: Only the main headline is shown.
  4521. 2. CHILDREN: The main headline and the direct children are shown.
  4522. From this state, you can move to one of the children
  4523. and zoom in further.
  4524. 3. SUBTREE: Show the entire subtree, including body text.
  4525. - When there is a numeric prefix, go up to a heading with level ARG, do
  4526. a `show-subtree' and return to the previous cursor position. If ARG
  4527. is negative, go up that many levels.
  4528. - When point is not at the beginning of a headline, execute
  4529. `indent-relative', like TAB normally does. See the option
  4530. `org-cycle-emulate-tab' for details.
  4531. - Special case: if point is at the beginning of the buffer and there is
  4532. no headline in line 1, this function will act as if called with prefix arg.
  4533. But only if also the variable `org-cycle-global-at-bob' is t."
  4534. (interactive "P")
  4535. (let* ((outline-regexp
  4536. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4537. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4538. outline-regexp))
  4539. (bob-special (and org-cycle-global-at-bob (bobp)
  4540. (not (looking-at outline-regexp))))
  4541. (org-cycle-hook
  4542. (if bob-special
  4543. (delq 'org-optimize-window-after-visibility-change
  4544. (copy-sequence org-cycle-hook))
  4545. org-cycle-hook))
  4546. (pos (point)))
  4547. (if (or bob-special (equal arg '(4)))
  4548. ;; special case: use global cycling
  4549. (setq arg t))
  4550. (cond
  4551. ((org-at-table-p 'any)
  4552. ;; Enter the table or move to the next field in the table
  4553. (or (org-table-recognize-table.el)
  4554. (progn
  4555. (if arg (org-table-edit-field t)
  4556. (org-table-justify-field-maybe)
  4557. (call-interactively 'org-table-next-field)))))
  4558. ((eq arg t) ;; Global cycling
  4559. (cond
  4560. ((and (eq last-command this-command)
  4561. (eq org-cycle-global-status 'overview))
  4562. ;; We just created the overview - now do table of contents
  4563. ;; This can be slow in very large buffers, so indicate action
  4564. (message "CONTENTS...")
  4565. (org-content)
  4566. (message "CONTENTS...done")
  4567. (setq org-cycle-global-status 'contents)
  4568. (run-hook-with-args 'org-cycle-hook 'contents))
  4569. ((and (eq last-command this-command)
  4570. (eq org-cycle-global-status 'contents))
  4571. ;; We just showed the table of contents - now show everything
  4572. (show-all)
  4573. (message "SHOW ALL")
  4574. (setq org-cycle-global-status 'all)
  4575. (run-hook-with-args 'org-cycle-hook 'all))
  4576. (t
  4577. ;; Default action: go to overview
  4578. (org-overview)
  4579. (message "OVERVIEW")
  4580. (setq org-cycle-global-status 'overview)
  4581. (run-hook-with-args 'org-cycle-hook 'overview))))
  4582. ((and org-drawers org-drawer-regexp
  4583. (save-excursion
  4584. (beginning-of-line 1)
  4585. (looking-at org-drawer-regexp)))
  4586. ;; Toggle block visibility
  4587. (org-flag-drawer
  4588. (not (get-char-property (match-end 0) 'invisible))))
  4589. ((integerp arg)
  4590. ;; Show-subtree, ARG levels up from here.
  4591. (save-excursion
  4592. (org-back-to-heading)
  4593. (outline-up-heading (if (< arg 0) (- arg)
  4594. (- (funcall outline-level) arg)))
  4595. (org-show-subtree)))
  4596. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4597. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4598. ;; At a heading: rotate between three different views
  4599. (org-back-to-heading)
  4600. (let ((goal-column 0) eoh eol eos)
  4601. ;; First, some boundaries
  4602. (save-excursion
  4603. (org-back-to-heading)
  4604. (save-excursion
  4605. (beginning-of-line 2)
  4606. (while (and (not (eobp)) ;; this is like `next-line'
  4607. (get-char-property (1- (point)) 'invisible))
  4608. (beginning-of-line 2)) (setq eol (point)))
  4609. (outline-end-of-heading) (setq eoh (point))
  4610. (org-end-of-subtree t)
  4611. (unless (eobp)
  4612. (skip-chars-forward " \t\n")
  4613. (beginning-of-line 1) ; in case this is an item
  4614. )
  4615. (setq eos (1- (point))))
  4616. ;; Find out what to do next and set `this-command'
  4617. (cond
  4618. ((= eos eoh)
  4619. ;; Nothing is hidden behind this heading
  4620. (message "EMPTY ENTRY")
  4621. (setq org-cycle-subtree-status nil)
  4622. (save-excursion
  4623. (goto-char eos)
  4624. (outline-next-heading)
  4625. (if (org-invisible-p) (org-flag-heading nil))))
  4626. ((or (>= eol eos)
  4627. (not (string-match "\\S-" (buffer-substring eol eos))))
  4628. ;; Entire subtree is hidden in one line: open it
  4629. (org-show-entry)
  4630. (show-children)
  4631. (message "CHILDREN")
  4632. (save-excursion
  4633. (goto-char eos)
  4634. (outline-next-heading)
  4635. (if (org-invisible-p) (org-flag-heading nil)))
  4636. (setq org-cycle-subtree-status 'children)
  4637. (run-hook-with-args 'org-cycle-hook 'children))
  4638. ((and (eq last-command this-command)
  4639. (eq org-cycle-subtree-status 'children))
  4640. ;; We just showed the children, now show everything.
  4641. (org-show-subtree)
  4642. (message "SUBTREE")
  4643. (setq org-cycle-subtree-status 'subtree)
  4644. (run-hook-with-args 'org-cycle-hook 'subtree))
  4645. (t
  4646. ;; Default action: hide the subtree.
  4647. (hide-subtree)
  4648. (message "FOLDED")
  4649. (setq org-cycle-subtree-status 'folded)
  4650. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4651. ;; TAB emulation
  4652. (buffer-read-only (org-back-to-heading))
  4653. ((org-try-cdlatex-tab))
  4654. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4655. (or (not (bolp))
  4656. (not (looking-at outline-regexp))))
  4657. (call-interactively (global-key-binding "\t")))
  4658. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4659. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4660. (or (and (eq org-cycle-emulate-tab 'white)
  4661. (= (match-end 0) (point-at-eol)))
  4662. (and (eq org-cycle-emulate-tab 'whitestart)
  4663. (>= (match-end 0) pos))))
  4664. t
  4665. (eq org-cycle-emulate-tab t))
  4666. (if (and (looking-at "[ \n\r\t]")
  4667. (string-match "^[ \t]*$" (buffer-substring
  4668. (point-at-bol) (point))))
  4669. (progn
  4670. (beginning-of-line 1)
  4671. (and (looking-at "[ \t]+") (replace-match ""))))
  4672. (call-interactively (global-key-binding "\t")))
  4673. (t (save-excursion
  4674. (org-back-to-heading)
  4675. (org-cycle))))))
  4676. ;;;###autoload
  4677. (defun org-global-cycle (&optional arg)
  4678. "Cycle the global visibility. For details see `org-cycle'."
  4679. (interactive "P")
  4680. (let ((org-cycle-include-plain-lists
  4681. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4682. (if (integerp arg)
  4683. (progn
  4684. (show-all)
  4685. (hide-sublevels arg)
  4686. (setq org-cycle-global-status 'contents))
  4687. (org-cycle '(4)))))
  4688. (defun org-overview ()
  4689. "Switch to overview mode, shoing only top-level headlines.
  4690. Really, this shows all headlines with level equal or greater than the level
  4691. of the first headline in the buffer. This is important, because if the
  4692. first headline is not level one, then (hide-sublevels 1) gives confusing
  4693. results."
  4694. (interactive)
  4695. (let ((level (save-excursion
  4696. (goto-char (point-min))
  4697. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4698. (progn
  4699. (goto-char (match-beginning 0))
  4700. (funcall outline-level))))))
  4701. (and level (hide-sublevels level))))
  4702. (defun org-content (&optional arg)
  4703. "Show all headlines in the buffer, like a table of contents.
  4704. With numerical argument N, show content up to level N."
  4705. (interactive "P")
  4706. (save-excursion
  4707. ;; Visit all headings and show their offspring
  4708. (and (integerp arg) (org-overview))
  4709. (goto-char (point-max))
  4710. (catch 'exit
  4711. (while (and (progn (condition-case nil
  4712. (outline-previous-visible-heading 1)
  4713. (error (goto-char (point-min))))
  4714. t)
  4715. (looking-at outline-regexp))
  4716. (if (integerp arg)
  4717. (show-children (1- arg))
  4718. (show-branches))
  4719. (if (bobp) (throw 'exit nil))))))
  4720. (defun org-optimize-window-after-visibility-change (state)
  4721. "Adjust the window after a change in outline visibility.
  4722. This function is the default value of the hook `org-cycle-hook'."
  4723. (when (get-buffer-window (current-buffer))
  4724. (cond
  4725. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4726. ; ((eq state 'overview) (org-beginning-of-line))
  4727. ((eq state 'content) nil)
  4728. ((eq state 'all) nil)
  4729. ((eq state 'folded) nil)
  4730. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4731. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4732. (defun org-cycle-show-empty-lines (state)
  4733. "Show empty lines above all visible headlines.
  4734. The region to be covered depends on STATE when called through
  4735. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4736. entire buffer covered. Note that an empty line is only shown if there
  4737. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4738. (when (> org-cycle-separator-lines 0)
  4739. (save-excursion
  4740. (let* ((n org-cycle-separator-lines)
  4741. (re (cond
  4742. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4743. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4744. (t (let ((ns (number-to-string (- n 2))))
  4745. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4746. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4747. beg end)
  4748. (cond
  4749. ((memq state '(overview contents t))
  4750. (setq beg (point-min) end (point-max)))
  4751. ((memq state '(children folded))
  4752. (setq beg (point) end (progn (org-end-of-subtree t t)
  4753. (beginning-of-line 2)
  4754. (point)))))
  4755. (when beg
  4756. (goto-char beg)
  4757. (while (re-search-forward re end t)
  4758. (if (not (get-char-property (match-end 1) 'invisible))
  4759. (outline-flag-region
  4760. (match-beginning 1) (match-end 1) nil)))))))
  4761. ;; Never hide empty lines at the end of the file.
  4762. (save-excursion
  4763. (goto-char (point-max))
  4764. (outline-previous-heading)
  4765. (outline-end-of-heading)
  4766. (if (and (looking-at "[ \t\n]+")
  4767. (= (match-end 0) (point-max)))
  4768. (outline-flag-region (point) (match-end 0) nil))))
  4769. (defun org-subtree-end-visible-p ()
  4770. "Is the end of the current subtree visible?"
  4771. (pos-visible-in-window-p
  4772. (save-excursion (org-end-of-subtree t) (point))))
  4773. (defun org-first-headline-recenter (&optional N)
  4774. "Move cursor to the first headline and recenter the headline.
  4775. Optional argument N means, put the headline into the Nth line of the window."
  4776. (goto-char (point-min))
  4777. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4778. (beginning-of-line)
  4779. (recenter (prefix-numeric-value N))))
  4780. ;;; Org-goto
  4781. (defvar org-goto-window-configuration nil)
  4782. (defvar org-goto-marker nil)
  4783. (defvar org-goto-map
  4784. (let ((map (make-sparse-keymap)))
  4785. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4786. (while (setq cmd (pop cmds))
  4787. (substitute-key-definition cmd cmd map global-map)))
  4788. (suppress-keymap map)
  4789. (org-defkey map "\C-m" 'org-goto-ret)
  4790. (org-defkey map [(left)] 'org-goto-left)
  4791. (org-defkey map [(right)] 'org-goto-right)
  4792. (org-defkey map [(?q)] 'org-goto-quit)
  4793. (org-defkey map [(control ?g)] 'org-goto-quit)
  4794. (org-defkey map "\C-i" 'org-cycle)
  4795. (org-defkey map [(tab)] 'org-cycle)
  4796. (org-defkey map [(down)] 'outline-next-visible-heading)
  4797. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4798. (org-defkey map "n" 'outline-next-visible-heading)
  4799. (org-defkey map "p" 'outline-previous-visible-heading)
  4800. (org-defkey map "f" 'outline-forward-same-level)
  4801. (org-defkey map "b" 'outline-backward-same-level)
  4802. (org-defkey map "u" 'outline-up-heading)
  4803. (org-defkey map "/" 'org-occur)
  4804. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4805. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4806. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4807. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4808. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4809. map))
  4810. (defconst org-goto-help
  4811. "Browse copy of buffer to find location or copy text.
  4812. RET=jump to location [Q]uit and return to previous location
  4813. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur"
  4814. )
  4815. (defun org-goto ()
  4816. "Look up a different location in the current file, keeping current visibility.
  4817. When you want look-up or go to a different location in a document, the
  4818. fastest way is often to fold the entire buffer and then dive into the tree.
  4819. This method has the disadvantage, that the previous location will be folded,
  4820. which may not be what you want.
  4821. This command works around this by showing a copy of the current buffer
  4822. in an indirect buffer, in overview mode. You can dive into the tree in
  4823. that copy, use org-occur and incremental search to find a location.
  4824. When pressing RET or `Q', the command returns to the original buffer in
  4825. which the visibility is still unchanged. After RET is will also jump to
  4826. the location selected in the indirect buffer and expose the
  4827. the headline hierarchy above."
  4828. (interactive)
  4829. (let* ((org-goto-start-pos (point))
  4830. (selected-point
  4831. (car (org-get-location (current-buffer) org-goto-help))))
  4832. (if selected-point
  4833. (progn
  4834. (org-mark-ring-push org-goto-start-pos)
  4835. (goto-char selected-point)
  4836. (if (or (org-invisible-p) (org-invisible-p2))
  4837. (org-show-context 'org-goto)))
  4838. (message "Quit"))))
  4839. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4840. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4841. (defun org-get-location (buf help)
  4842. "Let the user select a location in the Org-mode buffer BUF.
  4843. This function uses a recursive edit. It returns the selected position
  4844. or nil."
  4845. (let (org-goto-selected-point org-goto-exit-command)
  4846. (save-excursion
  4847. (save-window-excursion
  4848. (delete-other-windows)
  4849. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4850. (switch-to-buffer
  4851. (condition-case nil
  4852. (make-indirect-buffer (current-buffer) "*org-goto*")
  4853. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4854. (with-output-to-temp-buffer "*Help*"
  4855. (princ help))
  4856. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4857. (setq buffer-read-only nil)
  4858. (let ((org-startup-truncated t)
  4859. (org-startup-folded nil)
  4860. (org-startup-align-all-tables nil))
  4861. (org-mode)
  4862. (org-overview))
  4863. (setq buffer-read-only t)
  4864. (if (and (boundp 'org-goto-start-pos)
  4865. (integer-or-marker-p org-goto-start-pos))
  4866. (let ((org-show-hierarchy-above t)
  4867. (org-show-siblings t)
  4868. (org-show-following-heading t))
  4869. (goto-char org-goto-start-pos)
  4870. (and (org-invisible-p) (org-show-context)))
  4871. (goto-char (point-min)))
  4872. (org-beginning-of-line)
  4873. (message "Select location and press RET")
  4874. ;; now we make sure that during selection, ony very few keys work
  4875. ;; and that it is impossible to switch to another window.
  4876. ; (let ((gm (current-global-map))
  4877. ; (overriding-local-map org-goto-map))
  4878. ; (unwind-protect
  4879. ; (progn
  4880. ; (use-global-map org-goto-map)
  4881. ; (recursive-edit))
  4882. ; (use-global-map gm)))
  4883. (use-local-map org-goto-map)
  4884. (recursive-edit)
  4885. ))
  4886. (kill-buffer "*org-goto*")
  4887. (cons org-goto-selected-point org-goto-exit-command)))
  4888. (defun org-goto-ret (&optional arg)
  4889. "Finish `org-goto' by going to the new location."
  4890. (interactive "P")
  4891. (setq org-goto-selected-point (point)
  4892. org-goto-exit-command 'return)
  4893. (throw 'exit nil))
  4894. (defun org-goto-left ()
  4895. "Finish `org-goto' by going to the new location."
  4896. (interactive)
  4897. (if (org-on-heading-p)
  4898. (progn
  4899. (beginning-of-line 1)
  4900. (setq org-goto-selected-point (point)
  4901. org-goto-exit-command 'left)
  4902. (throw 'exit nil))
  4903. (error "Not on a heading")))
  4904. (defun org-goto-right ()
  4905. "Finish `org-goto' by going to the new location."
  4906. (interactive)
  4907. (if (org-on-heading-p)
  4908. (progn
  4909. (setq org-goto-selected-point (point)
  4910. org-goto-exit-command 'right)
  4911. (throw 'exit nil))
  4912. (error "Not on a heading")))
  4913. (defun org-goto-quit ()
  4914. "Finish `org-goto' without cursor motion."
  4915. (interactive)
  4916. (setq org-goto-selected-point nil)
  4917. (setq org-goto-exit-command 'quit)
  4918. (throw 'exit nil))
  4919. ;;; Indirect buffer display of subtrees
  4920. (defvar org-indirect-dedicated-frame nil
  4921. "This is the frame being used for indirect tree display.")
  4922. (defvar org-last-indirect-buffer nil)
  4923. (defun org-tree-to-indirect-buffer (&optional arg)
  4924. "Create indirect buffer and narrow it to current subtree.
  4925. With numerical prefix ARG, go up to this level and then take that tree.
  4926. If ARG is negative, go up that many levels.
  4927. Normally this command removes the indirect buffer previously made
  4928. with this command. However, when called with a C-u prefix, the last buffer
  4929. is kept so that you can work with several indirect buffers at the same time.
  4930. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4931. requests that a new frame be made for the new buffer, so that the dedicated
  4932. frame is not changed."
  4933. (interactive "P")
  4934. (let ((cbuf (current-buffer))
  4935. (cwin (selected-window))
  4936. (pos (point))
  4937. beg end level heading ibuf)
  4938. (save-excursion
  4939. (org-back-to-heading t)
  4940. (when (numberp arg)
  4941. (setq level (org-outline-level))
  4942. (if (< arg 0) (setq arg (+ level arg)))
  4943. (while (> (setq level (org-outline-level)) arg)
  4944. (outline-up-heading 1 t)))
  4945. (setq beg (point)
  4946. heading (org-get-heading))
  4947. (org-end-of-subtree t) (setq end (point)))
  4948. (if (and (not arg)
  4949. (buffer-live-p org-last-indirect-buffer))
  4950. (kill-buffer org-last-indirect-buffer))
  4951. (setq ibuf (org-get-indirect-buffer cbuf)
  4952. org-last-indirect-buffer ibuf)
  4953. (cond
  4954. ((or (eq org-indirect-buffer-display 'new-frame)
  4955. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4956. (select-frame (make-frame))
  4957. (delete-other-windows)
  4958. (switch-to-buffer ibuf)
  4959. (org-set-frame-title heading))
  4960. ((eq org-indirect-buffer-display 'dedicated-frame)
  4961. (raise-frame
  4962. (select-frame (or (and org-indirect-dedicated-frame
  4963. (frame-live-p org-indirect-dedicated-frame)
  4964. org-indirect-dedicated-frame)
  4965. (setq org-indirect-dedicated-frame (make-frame)))))
  4966. (delete-other-windows)
  4967. (switch-to-buffer ibuf)
  4968. (org-set-frame-title (concat "Indirect: " heading)))
  4969. ((eq org-indirect-buffer-display 'current-window)
  4970. (switch-to-buffer ibuf))
  4971. ((eq org-indirect-buffer-display 'other-window)
  4972. (pop-to-buffer ibuf))
  4973. (t (error "Invalid value.")))
  4974. (if (featurep 'xemacs)
  4975. (save-excursion (org-mode) (turn-on-font-lock)))
  4976. (narrow-to-region beg end)
  4977. (show-all)
  4978. (goto-char pos)
  4979. (and (window-live-p cwin) (select-window cwin))))
  4980. (defun org-get-indirect-buffer (&optional buffer)
  4981. (setq buffer (or buffer (current-buffer)))
  4982. (let ((n 1) (base (buffer-name buffer)) bname)
  4983. (while (buffer-live-p
  4984. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4985. (setq n (1+ n)))
  4986. (condition-case nil
  4987. (make-indirect-buffer buffer bname 'clone)
  4988. (error (make-indirect-buffer buffer bname)))))
  4989. (defun org-set-frame-title (title)
  4990. "Set the title of the current frame to the string TITLE."
  4991. ;; FIXME: how to name a single frame in XEmacs???
  4992. (unless (featurep 'xemacs)
  4993. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4994. ;;;; Structure editing
  4995. ;;; Inserting headlines
  4996. (defun org-insert-heading (&optional force-heading)
  4997. "Insert a new heading or item with same depth at point.
  4998. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4999. If point is at the beginning of a headline, insert a sibling before the
  5000. current headline. If point is in the middle of a headline, split the headline
  5001. at that position and make the rest of the headline part of the sibling below
  5002. the current headline."
  5003. (interactive "P")
  5004. (if (= (buffer-size) 0)
  5005. (insert "\n* ")
  5006. (when (or force-heading (not (org-insert-item)))
  5007. (let* ((head (save-excursion
  5008. (condition-case nil
  5009. (progn
  5010. (org-back-to-heading)
  5011. (match-string 0))
  5012. (error "*"))))
  5013. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5014. pos)
  5015. (cond
  5016. ((and (org-on-heading-p) (bolp)
  5017. (or (bobp)
  5018. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5019. (open-line (if blank 2 1)))
  5020. ((and (bolp)
  5021. (or (bobp)
  5022. (save-excursion
  5023. (backward-char 1) (not (org-invisible-p)))))
  5024. nil)
  5025. (t (newline (if blank 2 1))))
  5026. (insert head) (just-one-space)
  5027. (setq pos (point))
  5028. (end-of-line 1)
  5029. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5030. (run-hooks 'org-insert-heading-hook)))))
  5031. (defun org-insert-heading-after-current ()
  5032. "Insert a new heading with same level as current, after current subtree."
  5033. (interactive)
  5034. (org-back-to-heading)
  5035. (org-insert-heading)
  5036. (org-move-subtree-down)
  5037. (end-of-line 1))
  5038. (defun org-insert-todo-heading (arg)
  5039. "Insert a new heading with the same level and TODO state as current heading.
  5040. If the heading has no TODO state, or if the state is DONE, use the first
  5041. state (TODO by default). Also with prefix arg, force first state."
  5042. (interactive "P")
  5043. (when (not (org-insert-item 'checkbox))
  5044. (org-insert-heading)
  5045. (save-excursion
  5046. (org-back-to-heading)
  5047. (outline-previous-heading)
  5048. (looking-at org-todo-line-regexp))
  5049. (if (or arg
  5050. (not (match-beginning 2))
  5051. (member (match-string 2) org-done-keywords))
  5052. (insert (car org-todo-keywords-1) " ")
  5053. (insert (match-string 2) " "))))
  5054. (defun org-insert-subheading (arg)
  5055. "Insert a new subheading and demote it.
  5056. Works for outline headings and for plain lists alike."
  5057. (interactive "P")
  5058. (org-insert-heading arg)
  5059. (cond
  5060. ((org-on-heading-p) (org-do-demote))
  5061. ((org-at-item-p) (org-indent-item 1))))
  5062. (defun org-insert-todo-subheading (arg)
  5063. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5064. Works for outline headings and for plain lists alike."
  5065. (interactive "P")
  5066. (org-insert-todo-heading arg)
  5067. (cond
  5068. ((org-on-heading-p) (org-do-demote))
  5069. ((org-at-item-p) (org-indent-item 1))))
  5070. ;;; Promotion and Demotion
  5071. (defun org-promote-subtree ()
  5072. "Promote the entire subtree.
  5073. See also `org-promote'."
  5074. (interactive)
  5075. (save-excursion
  5076. (org-map-tree 'org-promote))
  5077. (org-fix-position-after-promote))
  5078. (defun org-demote-subtree ()
  5079. "Demote the entire subtree. See `org-demote'.
  5080. See also `org-promote'."
  5081. (interactive)
  5082. (save-excursion
  5083. (org-map-tree 'org-demote))
  5084. (org-fix-position-after-promote))
  5085. (defun org-do-promote ()
  5086. "Promote the current heading higher up the tree.
  5087. If the region is active in `transient-mark-mode', promote all headings
  5088. in the region."
  5089. (interactive)
  5090. (save-excursion
  5091. (if (org-region-active-p)
  5092. (org-map-region 'org-promote (region-beginning) (region-end))
  5093. (org-promote)))
  5094. (org-fix-position-after-promote))
  5095. (defun org-do-demote ()
  5096. "Demote the current heading lower down the tree.
  5097. If the region is active in `transient-mark-mode', demote all headings
  5098. in the region."
  5099. (interactive)
  5100. (save-excursion
  5101. (if (org-region-active-p)
  5102. (org-map-region 'org-demote (region-beginning) (region-end))
  5103. (org-demote)))
  5104. (org-fix-position-after-promote))
  5105. (defun org-fix-position-after-promote ()
  5106. "Make sure that after pro/demotion cursor position is right."
  5107. (let ((pos (point)))
  5108. (when (save-excursion
  5109. (beginning-of-line 1)
  5110. (looking-at org-todo-line-regexp)
  5111. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5112. (cond ((eobp) (insert " "))
  5113. ((eolp) (insert " "))
  5114. ((equal (char-after) ?\ ) (forward-char 1))))))
  5115. (defun org-reduced-level (l)
  5116. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5117. (defun org-get-legal-level (level &optional change)
  5118. "Rectify a level change under the influence of `org-odd-levels-only'
  5119. LEVEL is a current level, CHANGE is by how much the level should be
  5120. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5121. even level numbers will become the next higher odd number."
  5122. (if org-odd-levels-only
  5123. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5124. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5125. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5126. (max 1 (+ level change))))
  5127. (defun org-promote ()
  5128. "Promote the current heading higher up the tree.
  5129. If the region is active in `transient-mark-mode', promote all headings
  5130. in the region."
  5131. (org-back-to-heading t)
  5132. (let* ((level (save-match-data (funcall outline-level)))
  5133. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5134. (diff (abs (- level (length up-head) -1))))
  5135. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5136. (replace-match up-head nil t)
  5137. ;; Fixup tag positioning
  5138. (and org-auto-align-tags (org-set-tags nil t))
  5139. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5140. (defun org-demote ()
  5141. "Demote the current heading lower down the tree.
  5142. If the region is active in `transient-mark-mode', demote all headings
  5143. in the region."
  5144. (org-back-to-heading t)
  5145. (let* ((level (save-match-data (funcall outline-level)))
  5146. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5147. (diff (abs (- level (length down-head) -1))))
  5148. (replace-match down-head nil t)
  5149. ;; Fixup tag positioning
  5150. (and org-auto-align-tags (org-set-tags nil t))
  5151. (if org-adapt-indentation (org-fixup-indentation diff))))
  5152. (defun org-map-tree (fun)
  5153. "Call FUN for every heading underneath the current one."
  5154. (org-back-to-heading)
  5155. (let ((level (funcall outline-level)))
  5156. (save-excursion
  5157. (funcall fun)
  5158. (while (and (progn
  5159. (outline-next-heading)
  5160. (> (funcall outline-level) level))
  5161. (not (eobp)))
  5162. (funcall fun)))))
  5163. (defun org-map-region (fun beg end)
  5164. "Call FUN for every heading between BEG and END."
  5165. (let ((org-ignore-region t))
  5166. (save-excursion
  5167. (setq end (copy-marker end))
  5168. (goto-char beg)
  5169. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5170. (< (point) end))
  5171. (funcall fun))
  5172. (while (and (progn
  5173. (outline-next-heading)
  5174. (< (point) end))
  5175. (not (eobp)))
  5176. (funcall fun)))))
  5177. (defun org-fixup-indentation (diff)
  5178. "Change the indentation in the current entry by DIFF
  5179. However, if any line in the current entry has no indentation, or if it
  5180. would end up with no indentation after the change, nothing at all is done."
  5181. (save-excursion
  5182. (let ((end (save-excursion (outline-next-heading)
  5183. (point-marker)))
  5184. (prohibit (if (> diff 0)
  5185. "^\\S-"
  5186. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5187. col)
  5188. (unless (save-excursion (end-of-line 1)
  5189. (re-search-forward prohibit end t))
  5190. (while (re-search-forward "^[ \t]+" end t)
  5191. (goto-char (match-end 0))
  5192. (setq col (current-column))
  5193. (if (< diff 0) (replace-match ""))
  5194. (indent-to (+ diff col))))
  5195. (move-marker end nil))))
  5196. (defun org-convert-to-odd-levels ()
  5197. "Convert an org-mode file with all levels allowed to one with odd levels.
  5198. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5199. level 5 etc."
  5200. (interactive)
  5201. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5202. (let ((org-odd-levels-only nil) n)
  5203. (save-excursion
  5204. (goto-char (point-min))
  5205. (while (re-search-forward "^\\*\\*+ " nil t)
  5206. (setq n (- (length (match-string 0)) 2))
  5207. (while (>= (setq n (1- n)) 0)
  5208. (org-demote))
  5209. (end-of-line 1))))))
  5210. (defun org-convert-to-oddeven-levels ()
  5211. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5212. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5213. section with an even level, conversion would destroy the structure of the file. An error
  5214. is signaled in this case."
  5215. (interactive)
  5216. (goto-char (point-min))
  5217. ;; First check if there are no even levels
  5218. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5219. (org-show-context t)
  5220. (error "Not all levels are odd in this file. Conversion not possible."))
  5221. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5222. (let ((org-odd-levels-only nil) n)
  5223. (save-excursion
  5224. (goto-char (point-min))
  5225. (while (re-search-forward "^\\*\\*+ " nil t)
  5226. (setq n (/ (1- (length (match-string 0))) 2))
  5227. (while (>= (setq n (1- n)) 0)
  5228. (org-promote))
  5229. (end-of-line 1))))))
  5230. (defun org-tr-level (n)
  5231. "Make N odd if required."
  5232. (if org-odd-levels-only (1+ (/ n 2)) n))
  5233. ;;; Vertical tree motion, cutting and pasting of subtrees
  5234. (defun org-move-subtree-up (&optional arg)
  5235. "Move the current subtree up past ARG headlines of the same level."
  5236. (interactive "p")
  5237. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5238. (defun org-move-subtree-down (&optional arg)
  5239. "Move the current subtree down past ARG headlines of the same level."
  5240. (interactive "p")
  5241. (setq arg (prefix-numeric-value arg))
  5242. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5243. 'outline-get-last-sibling))
  5244. (ins-point (make-marker))
  5245. (cnt (abs arg))
  5246. beg end txt folded)
  5247. ;; Select the tree
  5248. (org-back-to-heading)
  5249. (setq beg (point))
  5250. (save-match-data
  5251. (save-excursion (outline-end-of-heading)
  5252. (setq folded (org-invisible-p)))
  5253. (outline-end-of-subtree))
  5254. (outline-next-heading)
  5255. (setq end (point))
  5256. ;; Find insertion point, with error handling
  5257. (goto-char beg)
  5258. (while (> cnt 0)
  5259. (or (and (funcall movfunc) (looking-at outline-regexp))
  5260. (progn (goto-char beg)
  5261. (error "Cannot move past superior level or buffer limit")))
  5262. (setq cnt (1- cnt)))
  5263. (if (> arg 0)
  5264. ;; Moving forward - still need to move over subtree
  5265. (progn (outline-end-of-subtree)
  5266. (outline-next-heading)
  5267. (if (not (or (looking-at (concat "^" outline-regexp))
  5268. (bolp)))
  5269. (newline))))
  5270. (move-marker ins-point (point))
  5271. (setq txt (buffer-substring beg end))
  5272. (delete-region beg end)
  5273. (insert txt)
  5274. (or (bolp) (insert "\n"))
  5275. (goto-char ins-point)
  5276. (if folded (hide-subtree))
  5277. (move-marker ins-point nil)))
  5278. (defvar org-subtree-clip ""
  5279. "Clipboard for cut and paste of subtrees.
  5280. This is actually only a copy of the kill, because we use the normal kill
  5281. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5282. (defvar org-subtree-clip-folded nil
  5283. "Was the last copied subtree folded?
  5284. This is used to fold the tree back after pasting.")
  5285. (defun org-cut-subtree (&optional n)
  5286. "Cut the current subtree into the clipboard.
  5287. With prefix arg N, cut this many sequential subtrees.
  5288. This is a short-hand for marking the subtree and then cutting it."
  5289. (interactive "p")
  5290. (org-copy-subtree n 'cut))
  5291. (defun org-copy-subtree (&optional n cut)
  5292. "Cut the current subtree into the clipboard.
  5293. With prefix arg N, cut this many sequential subtrees.
  5294. This is a short-hand for marking the subtree and then copying it.
  5295. If CUT is non-nil, actually cut the subtree."
  5296. (interactive "p")
  5297. (let (beg end folded)
  5298. (if (interactive-p)
  5299. (org-back-to-heading nil) ; take what looks like a subtree
  5300. (org-back-to-heading t)) ; take what is really there
  5301. (setq beg (point))
  5302. (save-match-data
  5303. (save-excursion (outline-end-of-heading)
  5304. (setq folded (org-invisible-p)))
  5305. (condition-case nil
  5306. (outline-forward-same-level (1- n))
  5307. (error nil))
  5308. (org-end-of-subtree t t))
  5309. (setq end (point))
  5310. (goto-char beg)
  5311. (when (> end beg)
  5312. (setq org-subtree-clip-folded folded)
  5313. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5314. (setq org-subtree-clip (current-kill 0))
  5315. (message "%s: Subtree(s) with %d characters"
  5316. (if cut "Cut" "Copied")
  5317. (length org-subtree-clip)))))
  5318. (defun org-paste-subtree (&optional level tree)
  5319. "Paste the clipboard as a subtree, with modification of headline level.
  5320. The entire subtree is promoted or demoted in order to match a new headline
  5321. level. By default, the new level is derived from the visible headings
  5322. before and after the insertion point, and taken to be the inferior headline
  5323. level of the two. So if the previous visible heading is level 3 and the
  5324. next is level 4 (or vice versa), level 4 will be used for insertion.
  5325. This makes sure that the subtree remains an independent subtree and does
  5326. not swallow low level entries.
  5327. You can also force a different level, either by using a numeric prefix
  5328. argument, or by inserting the heading marker by hand. For example, if the
  5329. cursor is after \"*****\", then the tree will be shifted to level 5.
  5330. If you want to insert the tree as is, just use \\[yank].
  5331. If optional TREE is given, use this text instead of the kill ring."
  5332. (interactive "P")
  5333. (unless (org-kill-is-subtree-p tree)
  5334. (error
  5335. (substitute-command-keys
  5336. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5337. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5338. (^re (concat "^\\(" outline-regexp "\\)"))
  5339. (re (concat "\\(" outline-regexp "\\)"))
  5340. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5341. (old-level (if (string-match ^re txt)
  5342. (- (match-end 0) (match-beginning 0) 1)
  5343. -1))
  5344. (force-level (cond (level (prefix-numeric-value level))
  5345. ((string-match
  5346. ^re_ (buffer-substring (point-at-bol) (point)))
  5347. (- (match-end 1) (match-beginning 1)))
  5348. (t nil)))
  5349. (previous-level (save-excursion
  5350. (condition-case nil
  5351. (progn
  5352. (outline-previous-visible-heading 1)
  5353. (if (looking-at re)
  5354. (- (match-end 0) (match-beginning 0) 1)
  5355. 1))
  5356. (error 1))))
  5357. (next-level (save-excursion
  5358. (condition-case nil
  5359. (progn
  5360. (or (looking-at outline-regexp)
  5361. (outline-next-visible-heading 1))
  5362. (if (looking-at re)
  5363. (- (match-end 0) (match-beginning 0) 1)
  5364. 1))
  5365. (error 1))))
  5366. (new-level (or force-level (max previous-level next-level)))
  5367. (shift (if (or (= old-level -1)
  5368. (= new-level -1)
  5369. (= old-level new-level))
  5370. 0
  5371. (- new-level old-level)))
  5372. (delta (if (> shift 0) -1 1))
  5373. (func (if (> shift 0) 'org-demote 'org-promote))
  5374. (org-odd-levels-only nil)
  5375. beg end)
  5376. ;; Remove the forced level indicator
  5377. (if force-level
  5378. (delete-region (point-at-bol) (point)))
  5379. ;; Paste
  5380. (beginning-of-line 1)
  5381. (setq beg (point))
  5382. (insert txt)
  5383. (unless (string-match "\n[ \t]*\\'" txt) (insert "\n"))
  5384. (setq end (point))
  5385. (goto-char beg)
  5386. ;; Shift if necessary
  5387. (unless (= shift 0)
  5388. (save-restriction
  5389. (narrow-to-region beg end)
  5390. (while (not (= shift 0))
  5391. (org-map-region func (point-min) (point-max))
  5392. (setq shift (+ delta shift)))
  5393. (goto-char (point-min))))
  5394. (when (interactive-p)
  5395. (message "Clipboard pasted as level %d subtree" new-level))
  5396. (if (and kill-ring
  5397. (eq org-subtree-clip (current-kill 0))
  5398. org-subtree-clip-folded)
  5399. ;; The tree was folded before it was killed/copied
  5400. (hide-subtree))))
  5401. (defun org-kill-is-subtree-p (&optional txt)
  5402. "Check if the current kill is an outline subtree, or a set of trees.
  5403. Returns nil if kill does not start with a headline, or if the first
  5404. headline level is not the largest headline level in the tree.
  5405. So this will actually accept several entries of equal levels as well,
  5406. which is OK for `org-paste-subtree'.
  5407. If optional TXT is given, check this string instead of the current kill."
  5408. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5409. (start-level (and kill
  5410. (string-match (concat "\\`" org-outline-regexp) kill)
  5411. (- (match-end 0) (match-beginning 0) 1)))
  5412. (re (concat "^" org-outline-regexp))
  5413. (start 1))
  5414. (if (not start-level)
  5415. (progn
  5416. nil) ;; does not even start with a heading
  5417. (catch 'exit
  5418. (while (setq start (string-match re kill (1+ start)))
  5419. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5420. (throw 'exit nil)))
  5421. t))))
  5422. (defun org-narrow-to-subtree ()
  5423. "Narrow buffer to the current subtree."
  5424. (interactive)
  5425. (save-excursion
  5426. (narrow-to-region
  5427. (progn (org-back-to-heading) (point))
  5428. (progn (org-end-of-subtree t t) (point)))))
  5429. ;;; Outline Sorting
  5430. (defun org-sort (with-case)
  5431. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5432. Optional argument WITH-CASE means sort case-sensitively."
  5433. (interactive "P")
  5434. (if (org-at-table-p)
  5435. (org-call-with-arg 'org-table-sort-lines with-case)
  5436. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5437. (defvar org-priority-regexp) ; defined later in the file
  5438. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5439. "Sort entries on a certain level of an outline tree.
  5440. If there is an active region, the entries in the region are sorted.
  5441. Else, if the cursor is before the first entry, sort the top-level items.
  5442. Else, the children of the entry at point are sorted.
  5443. Sorting can be alphabetically, numerically, and by date/time as given by
  5444. the first time stamp in the entry. The command prompts for the sorting
  5445. type unless it has been given to the function through the SORTING-TYPE
  5446. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5447. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5448. called with point at the beginning of the record. It must return either
  5449. a string or a number that should serve as the sorting key for that record.
  5450. Comparing entries ignores case by default. However, with an optional argument
  5451. WITH-CASE, the sorting considers case as well."
  5452. (interactive "P")
  5453. (let ((case-func (if with-case 'identity 'downcase))
  5454. start beg end stars re re2
  5455. txt what tmp plain-list-p)
  5456. ;; Find beginning and end of region to sort
  5457. (cond
  5458. ((org-region-active-p)
  5459. ;; we will sort the region
  5460. (setq end (region-end)
  5461. what "region")
  5462. (goto-char (region-beginning))
  5463. (if (not (org-on-heading-p)) (outline-next-heading))
  5464. (setq start (point)))
  5465. ((org-at-item-p)
  5466. ;; we will sort this plain list
  5467. (org-beginning-of-item-list) (setq start (point))
  5468. (org-end-of-item-list) (setq end (point))
  5469. (goto-char start)
  5470. (setq plain-list-p t
  5471. what "plain list"))
  5472. ((or (org-on-heading-p)
  5473. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5474. ;; we will sort the children of the current headline
  5475. (org-back-to-heading)
  5476. (setq start (point) end (org-end-of-subtree) what "children")
  5477. (goto-char start)
  5478. (show-subtree)
  5479. (outline-next-heading))
  5480. (t
  5481. ;; we will sort the top-level entries in this file
  5482. (goto-char (point-min))
  5483. (or (org-on-heading-p) (outline-next-heading))
  5484. (setq start (point) end (point-max) what "top-level")
  5485. (goto-char start)
  5486. (show-all)))
  5487. (setq beg (point))
  5488. (if (>= beg end) (error "Nothing to sort"))
  5489. (unless plain-list-p
  5490. (looking-at "\\(\\*+\\)")
  5491. (setq stars (match-string 1)
  5492. re (concat "^" (regexp-quote stars) " +")
  5493. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5494. txt (buffer-substring beg end))
  5495. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5496. (if (and (not (equal stars "*")) (string-match re2 txt))
  5497. (error "Region to sort contains a level above the first entry")))
  5498. (unless sorting-type
  5499. (message
  5500. (if plain-list-p
  5501. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5502. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  5503. what)
  5504. (setq sorting-type (read-char-exclusive))
  5505. (and (= (downcase sorting-type) ?f)
  5506. (setq getkey-func
  5507. (completing-read "Sort using function: "
  5508. obarray 'fboundp t nil nil))
  5509. (setq getkey-func (intern getkey-func)))
  5510. (and (= (downcase sorting-type) ?r)
  5511. (setq property
  5512. (completing-read "Property: "
  5513. (mapcar 'list (org-buffer-property-keys t))
  5514. nil t))))
  5515. (message "Sorting entries...")
  5516. (save-restriction
  5517. (narrow-to-region start end)
  5518. (let ((dcst (downcase sorting-type))
  5519. (now (current-time)))
  5520. (sort-subr
  5521. (/= dcst sorting-type)
  5522. ;; This function moves to the beginning character of the "record" to
  5523. ;; be sorted.
  5524. (if plain-list-p
  5525. (lambda nil
  5526. (if (org-at-item-p) t (goto-char (point-max))))
  5527. (lambda nil
  5528. (if (re-search-forward re nil t)
  5529. (goto-char (match-beginning 0))
  5530. (goto-char (point-max)))))
  5531. ;; This function moves to the last character of the "record" being
  5532. ;; sorted.
  5533. (if plain-list-p
  5534. 'org-end-of-item
  5535. (lambda nil
  5536. (save-match-data
  5537. (condition-case nil
  5538. (outline-forward-same-level 1)
  5539. (error
  5540. (goto-char (point-max)))))))
  5541. ;; This function returns the value that gets sorted against.
  5542. (if plain-list-p
  5543. (lambda nil
  5544. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5545. (cond
  5546. ((= dcst ?n)
  5547. (string-to-number (buffer-substring (match-end 0)
  5548. (line-end-position))))
  5549. ((= dcst ?a)
  5550. (buffer-substring (match-end 0) (line-end-position)))
  5551. ((= dcst ?t)
  5552. (if (re-search-forward org-ts-regexp
  5553. (line-end-position) t)
  5554. (org-time-string-to-time (match-string 0))
  5555. now))
  5556. ((= dcst ?f)
  5557. (if getkey-func
  5558. (progn
  5559. (setq tmp (funcall getkey-func))
  5560. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5561. tmp)
  5562. (error "Invalid key function `%s'" getkey-func)))
  5563. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5564. (lambda nil
  5565. (cond
  5566. ((= dcst ?n)
  5567. (if (looking-at outline-regexp)
  5568. (string-to-number (buffer-substring (match-end 0)
  5569. (line-end-position)))
  5570. nil))
  5571. ((= dcst ?a)
  5572. (funcall case-func (buffer-substring (line-beginning-position)
  5573. (line-end-position))))
  5574. ((= dcst ?t)
  5575. (if (re-search-forward org-ts-regexp
  5576. (save-excursion
  5577. (forward-line 2)
  5578. (point)) t)
  5579. (org-time-string-to-time (match-string 0))
  5580. now))
  5581. ((= dcst ?p)
  5582. (if (re-search-forward org-priority-regexp (line-end-position) t)
  5583. (string-to-char (match-string 2))
  5584. org-default-priority))
  5585. ((= dcst ?r)
  5586. (or (org-entry-get nil property) ""))
  5587. ((= dcst ?f)
  5588. (if getkey-func
  5589. (progn
  5590. (setq tmp (funcall getkey-func))
  5591. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5592. tmp)
  5593. (error "Invalid key function `%s'" getkey-func)))
  5594. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5595. nil
  5596. (cond
  5597. ((= dcst ?a) 'string<)
  5598. ((= dcst ?t) 'time-less-p)
  5599. (t nil)))))
  5600. (message "Sorting entries...done")))
  5601. (defun org-do-sort (table what &optional with-case sorting-type)
  5602. "Sort TABLE of WHAT according to SORTING-TYPE.
  5603. The user will be prompted for the SORTING-TYPE if the call to this
  5604. function does not specify it. WHAT is only for the prompt, to indicate
  5605. what is being sorted. The sorting key will be extracted from
  5606. the car of the elements of the table.
  5607. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5608. (unless sorting-type
  5609. (message
  5610. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5611. what)
  5612. (setq sorting-type (read-char-exclusive)))
  5613. (let ((dcst (downcase sorting-type))
  5614. extractfun comparefun)
  5615. ;; Define the appropriate functions
  5616. (cond
  5617. ((= dcst ?n)
  5618. (setq extractfun 'string-to-number
  5619. comparefun (if (= dcst sorting-type) '< '>)))
  5620. ((= dcst ?a)
  5621. (setq extractfun (if with-case 'identity 'downcase)
  5622. comparefun (if (= dcst sorting-type)
  5623. 'string<
  5624. (lambda (a b) (and (not (string< a b))
  5625. (not (string= a b)))))))
  5626. ((= dcst ?t)
  5627. (setq extractfun
  5628. (lambda (x)
  5629. (if (string-match org-ts-regexp x)
  5630. (time-to-seconds
  5631. (org-time-string-to-time (match-string 0 x)))
  5632. 0))
  5633. comparefun (if (= dcst sorting-type) '< '>)))
  5634. (t (error "Invalid sorting type `%c'" sorting-type)))
  5635. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5636. table)
  5637. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5638. ;;;; Plain list items, including checkboxes
  5639. ;;; Plain list items
  5640. (defun org-at-item-p ()
  5641. "Is point in a line starting a hand-formatted item?"
  5642. (let ((llt org-plain-list-ordered-item-terminator))
  5643. (save-excursion
  5644. (goto-char (point-at-bol))
  5645. (looking-at
  5646. (cond
  5647. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5648. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5649. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5650. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5651. (defun org-in-item-p ()
  5652. "It the cursor inside a plain list item.
  5653. Does not have to be the first line."
  5654. (save-excursion
  5655. (condition-case nil
  5656. (progn
  5657. (org-beginning-of-item)
  5658. (org-at-item-p)
  5659. t)
  5660. (error nil))))
  5661. (defun org-insert-item (&optional checkbox)
  5662. "Insert a new item at the current level.
  5663. Return t when things worked, nil when we are not in an item."
  5664. (when (save-excursion
  5665. (condition-case nil
  5666. (progn
  5667. (org-beginning-of-item)
  5668. (org-at-item-p)
  5669. (if (org-invisible-p) (error "Invisible item"))
  5670. t)
  5671. (error nil)))
  5672. (let* ((bul (match-string 0))
  5673. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5674. (match-end 0)))
  5675. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5676. pos)
  5677. (cond
  5678. ((and (org-at-item-p) (<= (point) eow))
  5679. ;; before the bullet
  5680. (beginning-of-line 1)
  5681. (open-line (if blank 2 1)))
  5682. ((<= (point) eow)
  5683. (beginning-of-line 1))
  5684. (t (newline (if blank 2 1))))
  5685. (insert bul (if checkbox "[ ]" ""))
  5686. (just-one-space)
  5687. (setq pos (point))
  5688. (end-of-line 1)
  5689. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5690. (org-maybe-renumber-ordered-list)
  5691. (and checkbox (org-update-checkbox-count-maybe))
  5692. t))
  5693. ;;; Checkboxes
  5694. (defun org-at-item-checkbox-p ()
  5695. "Is point at a line starting a plain-list item with a checklet?"
  5696. (and (org-at-item-p)
  5697. (save-excursion
  5698. (goto-char (match-end 0))
  5699. (skip-chars-forward " \t")
  5700. (looking-at "\\[[- X]\\]"))))
  5701. (defun org-toggle-checkbox (&optional arg)
  5702. "Toggle the checkbox in the current line."
  5703. (interactive "P")
  5704. (catch 'exit
  5705. (let (beg end status (firstnew 'unknown))
  5706. (cond
  5707. ((org-region-active-p)
  5708. (setq beg (region-beginning) end (region-end)))
  5709. ((org-on-heading-p)
  5710. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5711. ((org-at-item-checkbox-p)
  5712. (save-excursion
  5713. (replace-match
  5714. (cond (arg "[-]")
  5715. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5716. (t "[ ]"))
  5717. t t))
  5718. (throw 'exit t))
  5719. (t (error "Not at a checkbox or heading, and no active region")))
  5720. (save-excursion
  5721. (goto-char beg)
  5722. (while (< (point) end)
  5723. (when (org-at-item-checkbox-p)
  5724. (setq status (equal (match-string 0) "[X]"))
  5725. (when (eq firstnew 'unknown)
  5726. (setq firstnew (not status)))
  5727. (replace-match
  5728. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5729. (beginning-of-line 2)))))
  5730. (org-update-checkbox-count-maybe))
  5731. (defun org-update-checkbox-count-maybe ()
  5732. "Update checkbox statistics unless turned off by user."
  5733. (when org-provide-checkbox-statistics
  5734. (org-update-checkbox-count)))
  5735. (defun org-update-checkbox-count (&optional all)
  5736. "Update the checkbox statistics in the current section.
  5737. This will find all statistic cookies like [57%] and [6/12] and update them
  5738. with the current numbers. With optional prefix argument ALL, do this for
  5739. the whole buffer."
  5740. (interactive "P")
  5741. (save-excursion
  5742. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5743. (beg (condition-case nil
  5744. (progn (outline-back-to-heading) (point))
  5745. (error (point-min))))
  5746. (end (move-marker (make-marker)
  5747. (progn (outline-next-heading) (point))))
  5748. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5749. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5750. b1 e1 f1 c-on c-off lim (cstat 0))
  5751. (when all
  5752. (goto-char (point-min))
  5753. (outline-next-heading)
  5754. (setq beg (point) end (point-max)))
  5755. (goto-char beg)
  5756. (while (re-search-forward re end t)
  5757. (setq cstat (1+ cstat)
  5758. b1 (match-beginning 0)
  5759. e1 (match-end 0)
  5760. f1 (match-beginning 1)
  5761. lim (cond
  5762. ((org-on-heading-p) (outline-next-heading) (point))
  5763. ((org-at-item-p) (org-end-of-item) (point))
  5764. (t nil))
  5765. c-on 0 c-off 0)
  5766. (goto-char e1)
  5767. (when lim
  5768. (while (re-search-forward re-box lim t)
  5769. (if (member (match-string 2) '("[ ]" "[-]"))
  5770. (setq c-off (1+ c-off))
  5771. (setq c-on (1+ c-on))))
  5772. ; (delete-region b1 e1)
  5773. (goto-char b1)
  5774. (insert (if f1
  5775. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5776. (format "[%d/%d]" c-on (+ c-on c-off))))
  5777. (and (looking-at "\\[.*?\\]")
  5778. (replace-match ""))))
  5779. (when (interactive-p)
  5780. (message "Checkbox satistics updated %s (%d places)"
  5781. (if all "in entire file" "in current outline entry") cstat)))))
  5782. (defun org-get-checkbox-statistics-face ()
  5783. "Select the face for checkbox statistics.
  5784. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5785. it will be `org-todo'."
  5786. (if (match-end 1)
  5787. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5788. (if (and (> (match-end 2) (match-beginning 2))
  5789. (equal (match-string 2) (match-string 3)))
  5790. 'org-done
  5791. 'org-todo)))
  5792. (defun org-get-indentation (&optional line)
  5793. "Get the indentation of the current line, interpreting tabs.
  5794. When LINE is given, assume it represents a line and compute its indentation."
  5795. (if line
  5796. (if (string-match "^ *" (org-remove-tabs line))
  5797. (match-end 0))
  5798. (save-excursion
  5799. (beginning-of-line 1)
  5800. (skip-chars-forward " \t")
  5801. (current-column))))
  5802. (defun org-remove-tabs (s &optional width)
  5803. "Replace tabulators in S with spaces.
  5804. Assumes that s is a single line, starting in column 0."
  5805. (setq width (or width tab-width))
  5806. (while (string-match "\t" s)
  5807. (setq s (replace-match
  5808. (make-string
  5809. (- (* width (/ (+ (match-beginning 0) width) width))
  5810. (match-beginning 0)) ?\ )
  5811. t t s)))
  5812. s)
  5813. (defun org-fix-indentation (line ind)
  5814. "Fix indentation in LINE.
  5815. IND is a cons cell with target and minimum indentation.
  5816. If the current indenation in LINE is smaller than the minimum,
  5817. leave it alone. If it is larger than ind, set it to the target."
  5818. (let* ((l (org-remove-tabs line))
  5819. (i (org-get-indentation l))
  5820. (i1 (car ind)) (i2 (cdr ind)))
  5821. (if (>= i i2) (setq l (substring line i2)))
  5822. (if (> i1 0)
  5823. (concat (make-string i1 ?\ ) l)
  5824. l)))
  5825. (defcustom org-empty-line-terminates-plain-lists nil
  5826. "Non-nil means, an empty line ends all plain list levels.
  5827. When nil, empty lines are part of the preceeding item."
  5828. :group 'org-plain-lists
  5829. :type 'boolean)
  5830. (defun org-beginning-of-item ()
  5831. "Go to the beginning of the current hand-formatted item.
  5832. If the cursor is not in an item, throw an error."
  5833. (interactive)
  5834. (let ((pos (point))
  5835. (limit (save-excursion
  5836. (condition-case nil
  5837. (progn
  5838. (org-back-to-heading)
  5839. (beginning-of-line 2) (point))
  5840. (error (point-min)))))
  5841. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5842. ind ind1)
  5843. (if (org-at-item-p)
  5844. (beginning-of-line 1)
  5845. (beginning-of-line 1)
  5846. (skip-chars-forward " \t")
  5847. (setq ind (current-column))
  5848. (if (catch 'exit
  5849. (while t
  5850. (beginning-of-line 0)
  5851. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5852. (if (looking-at "[ \t]*$")
  5853. (setq ind1 ind-empty)
  5854. (skip-chars-forward " \t")
  5855. (setq ind1 (current-column)))
  5856. (if (< ind1 ind)
  5857. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5858. nil
  5859. (goto-char pos)
  5860. (error "Not in an item")))))
  5861. (defun org-end-of-item ()
  5862. "Go to the end of the current hand-formatted item.
  5863. If the cursor is not in an item, throw an error."
  5864. (interactive)
  5865. (let* ((pos (point))
  5866. ind1
  5867. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5868. (limit (save-excursion (outline-next-heading) (point)))
  5869. (ind (save-excursion
  5870. (org-beginning-of-item)
  5871. (skip-chars-forward " \t")
  5872. (current-column)))
  5873. (end (catch 'exit
  5874. (while t
  5875. (beginning-of-line 2)
  5876. (if (eobp) (throw 'exit (point)))
  5877. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5878. (if (looking-at "[ \t]*$")
  5879. (setq ind1 ind-empty)
  5880. (skip-chars-forward " \t")
  5881. (setq ind1 (current-column)))
  5882. (if (<= ind1 ind)
  5883. (throw 'exit (point-at-bol)))))))
  5884. (if end
  5885. (goto-char end)
  5886. (goto-char pos)
  5887. (error "Not in an item"))))
  5888. (defun org-next-item ()
  5889. "Move to the beginning of the next item in the current plain list.
  5890. Error if not at a plain list, or if this is the last item in the list."
  5891. (interactive)
  5892. (let (ind ind1 (pos (point)))
  5893. (org-beginning-of-item)
  5894. (setq ind (org-get-indentation))
  5895. (org-end-of-item)
  5896. (setq ind1 (org-get-indentation))
  5897. (unless (and (org-at-item-p) (= ind ind1))
  5898. (goto-char pos)
  5899. (error "On last item"))))
  5900. (defun org-previous-item ()
  5901. "Move to the beginning of the previous item in the current plain list.
  5902. Error if not at a plain list, or if this is the first item in the list."
  5903. (interactive)
  5904. (let (beg ind ind1 (pos (point)))
  5905. (org-beginning-of-item)
  5906. (setq beg (point))
  5907. (setq ind (org-get-indentation))
  5908. (goto-char beg)
  5909. (catch 'exit
  5910. (while t
  5911. (beginning-of-line 0)
  5912. (if (looking-at "[ \t]*$")
  5913. nil
  5914. (if (<= (setq ind1 (org-get-indentation)) ind)
  5915. (throw 'exit t)))))
  5916. (condition-case nil
  5917. (if (or (not (org-at-item-p))
  5918. (< ind1 (1- ind)))
  5919. (error "")
  5920. (org-beginning-of-item))
  5921. (error (goto-char pos)
  5922. (error "On first item")))))
  5923. (defun org-move-item-down ()
  5924. "Move the plain list item at point down, i.e. swap with following item.
  5925. Subitems (items with larger indentation) are considered part of the item,
  5926. so this really moves item trees."
  5927. (interactive)
  5928. (let (beg end ind ind1 (pos (point)) txt)
  5929. (org-beginning-of-item)
  5930. (setq beg (point))
  5931. (setq ind (org-get-indentation))
  5932. (org-end-of-item)
  5933. (setq end (point))
  5934. (setq ind1 (org-get-indentation))
  5935. (if (and (org-at-item-p) (= ind ind1))
  5936. (progn
  5937. (org-end-of-item)
  5938. (setq txt (buffer-substring beg end))
  5939. (save-excursion
  5940. (delete-region beg end))
  5941. (setq pos (point))
  5942. (insert txt)
  5943. (goto-char pos)
  5944. (org-maybe-renumber-ordered-list))
  5945. (goto-char pos)
  5946. (error "Cannot move this item further down"))))
  5947. (defun org-move-item-up (arg)
  5948. "Move the plain list item at point up, i.e. swap with previous item.
  5949. Subitems (items with larger indentation) are considered part of the item,
  5950. so this really moves item trees."
  5951. (interactive "p")
  5952. (let (beg end ind ind1 (pos (point)) txt)
  5953. (org-beginning-of-item)
  5954. (setq beg (point))
  5955. (setq ind (org-get-indentation))
  5956. (org-end-of-item)
  5957. (setq end (point))
  5958. (goto-char beg)
  5959. (catch 'exit
  5960. (while t
  5961. (beginning-of-line 0)
  5962. (if (looking-at "[ \t]*$")
  5963. (if org-empty-line-terminates-plain-lists
  5964. (progn
  5965. (goto-char pos)
  5966. (error "Cannot move this item further up"))
  5967. nil)
  5968. (if (<= (setq ind1 (org-get-indentation)) ind)
  5969. (throw 'exit t)))))
  5970. (condition-case nil
  5971. (org-beginning-of-item)
  5972. (error (goto-char beg)
  5973. (error "Cannot move this item further up")))
  5974. (setq ind1 (org-get-indentation))
  5975. (if (and (org-at-item-p) (= ind ind1))
  5976. (progn
  5977. (setq txt (buffer-substring beg end))
  5978. (save-excursion
  5979. (delete-region beg end))
  5980. (setq pos (point))
  5981. (insert txt)
  5982. (goto-char pos)
  5983. (org-maybe-renumber-ordered-list))
  5984. (goto-char pos)
  5985. (error "Cannot move this item further up"))))
  5986. (defun org-maybe-renumber-ordered-list ()
  5987. "Renumber the ordered list at point if setup allows it.
  5988. This tests the user option `org-auto-renumber-ordered-lists' before
  5989. doing the renumbering."
  5990. (interactive)
  5991. (when (and org-auto-renumber-ordered-lists
  5992. (org-at-item-p))
  5993. (if (match-beginning 3)
  5994. (org-renumber-ordered-list 1)
  5995. (org-fix-bullet-type))))
  5996. (defun org-maybe-renumber-ordered-list-safe ()
  5997. (condition-case nil
  5998. (save-excursion
  5999. (org-maybe-renumber-ordered-list))
  6000. (error nil)))
  6001. (defun org-cycle-list-bullet (&optional which)
  6002. "Cycle through the different itemize/enumerate bullets.
  6003. This cycle the entire list level through the sequence:
  6004. `-' -> `+' -> `*' -> `1.' -> `1)'
  6005. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6006. 0 meand `-', 1 means `+' etc."
  6007. (interactive "P")
  6008. (org-preserve-lc
  6009. (org-beginning-of-item-list)
  6010. (org-at-item-p)
  6011. (beginning-of-line 1)
  6012. (let ((current (match-string 0))
  6013. (prevp (eq which 'previous))
  6014. new)
  6015. (setq new (cond
  6016. ((and (numberp which)
  6017. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6018. ((string-match "-" current) (if prevp "1)" "+"))
  6019. ((string-match "\\+" current)
  6020. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6021. ((string-match "\\*" current) (if prevp "+" "1."))
  6022. ((string-match "\\." current) (if prevp "*" "1)"))
  6023. ((string-match ")" current) (if prevp "1." "-"))
  6024. (t (error "This should not happen"))))
  6025. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6026. (org-fix-bullet-type)
  6027. (org-maybe-renumber-ordered-list))))
  6028. (defun org-get-string-indentation (s)
  6029. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6030. (let ((n -1) (i 0) (w tab-width) c)
  6031. (catch 'exit
  6032. (while (< (setq n (1+ n)) (length s))
  6033. (setq c (aref s n))
  6034. (cond ((= c ?\ ) (setq i (1+ i)))
  6035. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6036. (t (throw 'exit t)))))
  6037. i))
  6038. (defun org-renumber-ordered-list (arg)
  6039. "Renumber an ordered plain list.
  6040. Cursor needs to be in the first line of an item, the line that starts
  6041. with something like \"1.\" or \"2)\"."
  6042. (interactive "p")
  6043. (unless (and (org-at-item-p)
  6044. (match-beginning 3))
  6045. (error "This is not an ordered list"))
  6046. (let ((line (org-current-line))
  6047. (col (current-column))
  6048. (ind (org-get-string-indentation
  6049. (buffer-substring (point-at-bol) (match-beginning 3))))
  6050. ;; (term (substring (match-string 3) -1))
  6051. ind1 (n (1- arg))
  6052. fmt)
  6053. ;; find where this list begins
  6054. (org-beginning-of-item-list)
  6055. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6056. (setq fmt (concat "%d" (match-string 1)))
  6057. (beginning-of-line 0)
  6058. ;; walk forward and replace these numbers
  6059. (catch 'exit
  6060. (while t
  6061. (catch 'next
  6062. (beginning-of-line 2)
  6063. (if (eobp) (throw 'exit nil))
  6064. (if (looking-at "[ \t]*$") (throw 'next nil))
  6065. (skip-chars-forward " \t") (setq ind1 (current-column))
  6066. (if (> ind1 ind) (throw 'next t))
  6067. (if (< ind1 ind) (throw 'exit t))
  6068. (if (not (org-at-item-p)) (throw 'exit nil))
  6069. (delete-region (match-beginning 2) (match-end 2))
  6070. (goto-char (match-beginning 2))
  6071. (insert (format fmt (setq n (1+ n)))))))
  6072. (goto-line line)
  6073. (move-to-column col)))
  6074. (defun org-fix-bullet-type ()
  6075. "Make sure all items in this list have the same bullet as the firsst item."
  6076. (interactive)
  6077. (unless (org-at-item-p) (error "This is not a list"))
  6078. (let ((line (org-current-line))
  6079. (col (current-column))
  6080. (ind (current-indentation))
  6081. ind1 bullet)
  6082. ;; find where this list begins
  6083. (org-beginning-of-item-list)
  6084. (beginning-of-line 1)
  6085. ;; find out what the bullet type is
  6086. (looking-at "[ \t]*\\(\\S-+\\)")
  6087. (setq bullet (match-string 1))
  6088. ;; walk forward and replace these numbers
  6089. (beginning-of-line 0)
  6090. (catch 'exit
  6091. (while t
  6092. (catch 'next
  6093. (beginning-of-line 2)
  6094. (if (eobp) (throw 'exit nil))
  6095. (if (looking-at "[ \t]*$") (throw 'next nil))
  6096. (skip-chars-forward " \t") (setq ind1 (current-column))
  6097. (if (> ind1 ind) (throw 'next t))
  6098. (if (< ind1 ind) (throw 'exit t))
  6099. (if (not (org-at-item-p)) (throw 'exit nil))
  6100. (skip-chars-forward " \t")
  6101. (looking-at "\\S-+")
  6102. (replace-match bullet))))
  6103. (goto-line line)
  6104. (move-to-column col)
  6105. (if (string-match "[0-9]" bullet)
  6106. (org-renumber-ordered-list 1))))
  6107. (defun org-beginning-of-item-list ()
  6108. "Go to the beginning of the current item list.
  6109. I.e. to the first item in this list."
  6110. (interactive)
  6111. (org-beginning-of-item)
  6112. (let ((pos (point-at-bol))
  6113. (ind (org-get-indentation))
  6114. ind1)
  6115. ;; find where this list begins
  6116. (catch 'exit
  6117. (while t
  6118. (catch 'next
  6119. (beginning-of-line 0)
  6120. (if (looking-at "[ \t]*$")
  6121. (throw (if (bobp) 'exit 'next) t))
  6122. (skip-chars-forward " \t") (setq ind1 (current-column))
  6123. (if (or (< ind1 ind)
  6124. (and (= ind1 ind)
  6125. (not (org-at-item-p)))
  6126. (bobp))
  6127. (throw 'exit t)
  6128. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6129. (goto-char pos)))
  6130. (defun org-end-of-item-list ()
  6131. "Go to the end of the current item list.
  6132. I.e. to the text after the last item."
  6133. (interactive)
  6134. (org-beginning-of-item)
  6135. (let ((pos (point-at-bol))
  6136. (ind (org-get-indentation))
  6137. ind1)
  6138. ;; find where this list begins
  6139. (catch 'exit
  6140. (while t
  6141. (catch 'next
  6142. (beginning-of-line 2)
  6143. (if (looking-at "[ \t]*$")
  6144. (throw (if (eobp) 'exit 'next) t))
  6145. (skip-chars-forward " \t") (setq ind1 (current-column))
  6146. (if (or (< ind1 ind)
  6147. (and (= ind1 ind)
  6148. (not (org-at-item-p)))
  6149. (eobp))
  6150. (progn
  6151. (setq pos (point-at-bol))
  6152. (throw 'exit t))))))
  6153. (goto-char pos)))
  6154. (defvar org-last-indent-begin-marker (make-marker))
  6155. (defvar org-last-indent-end-marker (make-marker))
  6156. (defun org-outdent-item (arg)
  6157. "Outdent a local list item."
  6158. (interactive "p")
  6159. (org-indent-item (- arg)))
  6160. (defun org-indent-item (arg)
  6161. "Indent a local list item."
  6162. (interactive "p")
  6163. (unless (org-at-item-p)
  6164. (error "Not on an item"))
  6165. (save-excursion
  6166. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6167. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6168. (setq beg org-last-indent-begin-marker
  6169. end org-last-indent-end-marker)
  6170. (org-beginning-of-item)
  6171. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6172. (org-end-of-item)
  6173. (setq end (move-marker org-last-indent-end-marker (point))))
  6174. (goto-char beg)
  6175. (setq tmp (org-item-indent-positions)
  6176. ind (car tmp)
  6177. ind-down (nth 2 tmp)
  6178. ind-up (nth 1 tmp)
  6179. delta (if (> arg 0)
  6180. (if ind-down (- ind-down ind) 2)
  6181. (if ind-up (- ind-up ind) -2)))
  6182. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6183. (while (< (point) end)
  6184. (beginning-of-line 1)
  6185. (skip-chars-forward " \t") (setq ind1 (current-column))
  6186. (delete-region (point-at-bol) (point))
  6187. (or (eolp) (indent-to-column (+ ind1 delta)))
  6188. (beginning-of-line 2))))
  6189. (org-fix-bullet-type)
  6190. (org-maybe-renumber-ordered-list-safe)
  6191. (save-excursion
  6192. (beginning-of-line 0)
  6193. (condition-case nil (org-beginning-of-item) (error nil))
  6194. (org-maybe-renumber-ordered-list-safe)))
  6195. (defun org-item-indent-positions ()
  6196. "Return indentation for plain list items.
  6197. This returns a list with three values: The current indentation, the
  6198. parent indentation and the indentation a child should habe.
  6199. Assumes cursor in item line."
  6200. (let* ((bolpos (point-at-bol))
  6201. (ind (org-get-indentation))
  6202. ind-down ind-up pos)
  6203. (save-excursion
  6204. (org-beginning-of-item-list)
  6205. (skip-chars-backward "\n\r \t")
  6206. (when (org-in-item-p)
  6207. (org-beginning-of-item)
  6208. (setq ind-up (org-get-indentation))))
  6209. (setq pos (point))
  6210. (save-excursion
  6211. (cond
  6212. ((and (condition-case nil (progn (org-previous-item) t)
  6213. (error nil))
  6214. (or (forward-char 1) t)
  6215. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6216. (setq ind-down (org-get-indentation)))
  6217. ((and (goto-char pos)
  6218. (org-at-item-p))
  6219. (goto-char (match-end 0))
  6220. (skip-chars-forward " \t")
  6221. (setq ind-down (current-column)))))
  6222. (list ind ind-up ind-down)))
  6223. ;;; The orgstruct minor mode
  6224. ;; Define a minor mode which can be used in other modes in order to
  6225. ;; integrate the org-mode structure editing commands.
  6226. ;; This is really a hack, because the org-mode structure commands use
  6227. ;; keys which normally belong to the major mode. Here is how it
  6228. ;; works: The minor mode defines all the keys necessary to operate the
  6229. ;; structure commands, but wraps the commands into a function which
  6230. ;; tests if the cursor is currently at a headline or a plain list
  6231. ;; item. If that is the case, the structure command is used,
  6232. ;; temporarily setting many Org-mode variables like regular
  6233. ;; expressions for filling etc. However, when any of those keys is
  6234. ;; used at a different location, function uses `key-binding' to look
  6235. ;; up if the key has an associated command in another currently active
  6236. ;; keymap (minor modes, major mode, global), and executes that
  6237. ;; command. There might be problems if any of the keys is otherwise
  6238. ;; used as a prefix key.
  6239. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6240. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6241. ;; addresses this by checking explicitly for both bindings.
  6242. (defvar orgstruct-mode-map (make-sparse-keymap)
  6243. "Keymap for the minor `orgstruct-mode'.")
  6244. (defvar org-local-vars nil
  6245. "List of local variables, for use by `orgstruct-mode'")
  6246. ;;;###autoload
  6247. (define-minor-mode orgstruct-mode
  6248. "Toggle the minor more `orgstruct-mode'.
  6249. This mode is for using Org-mode structure commands in other modes.
  6250. The following key behave as if Org-mode was active, if the cursor
  6251. is on a headline, or on a plain list item (both in the definition
  6252. of Org-mode).
  6253. M-up Move entry/item up
  6254. M-down Move entry/item down
  6255. M-left Promote
  6256. M-right Demote
  6257. M-S-up Move entry/item up
  6258. M-S-down Move entry/item down
  6259. M-S-left Promote subtree
  6260. M-S-right Demote subtree
  6261. M-q Fill paragraph and items like in Org-mode
  6262. C-c ^ Sort entries
  6263. C-c - Cycle list bullet
  6264. TAB Cycle item visibility
  6265. M-RET Insert new heading/item
  6266. S-M-RET Insert new TODO heading / Chekbox item
  6267. C-c C-c Set tags / toggle checkbox"
  6268. nil " OrgStruct" nil
  6269. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6270. ;;;###autoload
  6271. (defun turn-on-orgstruct ()
  6272. "Unconditionally turn on `orgstruct-mode'."
  6273. (orgstruct-mode 1))
  6274. ;;;###autoload
  6275. (defun turn-on-orgstruct++ ()
  6276. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6277. In addition to setting orgstruct-mode, this also exports all indentation and
  6278. autofilling variables from org-mode into the buffer. Note that turning
  6279. off orgstruct-mode will *not* remove these additonal settings."
  6280. (orgstruct-mode 1)
  6281. (let (var val)
  6282. (mapc
  6283. (lambda (x)
  6284. (when (string-match
  6285. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6286. (symbol-name (car x)))
  6287. (setq var (car x) val (nth 1 x))
  6288. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6289. org-local-vars)))
  6290. (defun orgstruct-error ()
  6291. "Error when there is no default binding for a structure key."
  6292. (interactive)
  6293. (error "This key is has no function outside structure elements"))
  6294. (defun orgstruct-setup ()
  6295. "Setup orgstruct keymaps."
  6296. (let ((nfunc 0)
  6297. (bindings
  6298. (list
  6299. '([(meta up)] org-metaup)
  6300. '([(meta down)] org-metadown)
  6301. '([(meta left)] org-metaleft)
  6302. '([(meta right)] org-metaright)
  6303. '([(meta shift up)] org-shiftmetaup)
  6304. '([(meta shift down)] org-shiftmetadown)
  6305. '([(meta shift left)] org-shiftmetaleft)
  6306. '([(meta shift right)] org-shiftmetaright)
  6307. '([(shift up)] org-shiftup)
  6308. '([(shift down)] org-shiftdown)
  6309. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6310. '("\M-q" fill-paragraph)
  6311. '("\C-c^" org-sort)
  6312. '("\C-c-" org-cycle-list-bullet)))
  6313. elt key fun cmd)
  6314. (while (setq elt (pop bindings))
  6315. (setq nfunc (1+ nfunc))
  6316. (setq key (org-key (car elt))
  6317. fun (nth 1 elt)
  6318. cmd (orgstruct-make-binding fun nfunc key))
  6319. (org-defkey orgstruct-mode-map key cmd))
  6320. ;; Special treatment needed for TAB and RET
  6321. (org-defkey orgstruct-mode-map [(tab)]
  6322. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6323. (org-defkey orgstruct-mode-map "\C-i"
  6324. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6325. (org-defkey orgstruct-mode-map "\M-\C-m"
  6326. (orgstruct-make-binding 'org-insert-heading 105
  6327. "\M-\C-m" [(meta return)]))
  6328. (org-defkey orgstruct-mode-map [(meta return)]
  6329. (orgstruct-make-binding 'org-insert-heading 106
  6330. [(meta return)] "\M-\C-m"))
  6331. (org-defkey orgstruct-mode-map [(shift meta return)]
  6332. (orgstruct-make-binding 'org-insert-todo-heading 107
  6333. [(meta return)] "\M-\C-m"))
  6334. (unless org-local-vars
  6335. (setq org-local-vars (org-get-local-variables)))
  6336. t))
  6337. (defun orgstruct-make-binding (fun n &rest keys)
  6338. "Create a function for binding in the structure minor mode.
  6339. FUN is the command to call inside a table. N is used to create a unique
  6340. command name. KEYS are keys that should be checked in for a command
  6341. to execute outside of tables."
  6342. (eval
  6343. (list 'defun
  6344. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6345. '(arg)
  6346. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6347. "Outside of structure, run the binding of `"
  6348. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6349. "'.")
  6350. '(interactive "p")
  6351. (list 'if
  6352. '(org-context-p 'headline 'item)
  6353. (list 'org-run-like-in-org-mode (list 'quote fun))
  6354. (list 'let '(orgstruct-mode)
  6355. (list 'call-interactively
  6356. (append '(or)
  6357. (mapcar (lambda (k)
  6358. (list 'key-binding k))
  6359. keys)
  6360. '('orgstruct-error))))))))
  6361. (defun org-context-p (&rest contexts)
  6362. "Check if local context is and of CONTEXTS.
  6363. Possible values in the list of contexts are `table', `headline', and `item'."
  6364. (let ((pos (point)))
  6365. (goto-char (point-at-bol))
  6366. (prog1 (or (and (memq 'table contexts)
  6367. (looking-at "[ \t]*|"))
  6368. (and (memq 'headline contexts)
  6369. (looking-at "\\*+"))
  6370. (and (memq 'item contexts)
  6371. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6372. (goto-char pos))))
  6373. (defun org-get-local-variables ()
  6374. "Return a list of all local variables in an org-mode buffer."
  6375. (let (varlist)
  6376. (with-current-buffer (get-buffer-create "*Org tmp*")
  6377. (erase-buffer)
  6378. (org-mode)
  6379. (setq varlist (buffer-local-variables)))
  6380. (kill-buffer "*Org tmp*")
  6381. (delq nil
  6382. (mapcar
  6383. (lambda (x)
  6384. (setq x
  6385. (if (symbolp x)
  6386. (list x)
  6387. (list (car x) (list 'quote (cdr x)))))
  6388. (if (string-match
  6389. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6390. (symbol-name (car x)))
  6391. x nil))
  6392. varlist))))
  6393. ;;;###autoload
  6394. (defun org-run-like-in-org-mode (cmd)
  6395. (unless org-local-vars
  6396. (setq org-local-vars (org-get-local-variables)))
  6397. (eval (list 'let org-local-vars
  6398. (list 'call-interactively (list 'quote cmd)))))
  6399. ;;;; Archiving
  6400. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6401. (defun org-archive-subtree (&optional find-done)
  6402. "Move the current subtree to the archive.
  6403. The archive can be a certain top-level heading in the current file, or in
  6404. a different file. The tree will be moved to that location, the subtree
  6405. heading be marked DONE, and the current time will be added.
  6406. When called with prefix argument FIND-DONE, find whole trees without any
  6407. open TODO items and archive them (after getting confirmation from the user).
  6408. If the cursor is not at a headline when this comand is called, try all level
  6409. 1 trees. If the cursor is on a headline, only try the direct children of
  6410. this heading."
  6411. (interactive "P")
  6412. (if find-done
  6413. (org-archive-all-done)
  6414. ;; Save all relevant TODO keyword-relatex variables
  6415. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6416. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6417. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6418. (tr-org-done-keywords org-done-keywords)
  6419. (tr-org-todo-regexp org-todo-regexp)
  6420. (tr-org-todo-line-regexp org-todo-line-regexp)
  6421. (tr-org-odd-levels-only org-odd-levels-only)
  6422. (this-buffer (current-buffer))
  6423. (org-archive-location org-archive-location)
  6424. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6425. ;; start of variables that will be used for savind context
  6426. (file (abbreviate-file-name (buffer-file-name)))
  6427. (time (format-time-string
  6428. (substring (cdr org-time-stamp-formats) 1 -1)
  6429. (current-time)))
  6430. afile heading buffer level newfile-p
  6431. category todo priority
  6432. ;; start of variables that will be used for savind context
  6433. ltags itags prop)
  6434. ;; Try to find a local archive location
  6435. (save-excursion
  6436. (save-restriction
  6437. (widen)
  6438. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6439. (if (and prop (string-match "\\S-" prop))
  6440. (setq org-archive-location prop)
  6441. (if (or (re-search-backward re nil t)
  6442. (re-search-forward re nil t))
  6443. (setq org-archive-location (match-string 1))))))
  6444. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6445. (progn
  6446. (setq afile (format (match-string 1 org-archive-location)
  6447. (file-name-nondirectory buffer-file-name))
  6448. heading (match-string 2 org-archive-location)))
  6449. (error "Invalid `org-archive-location'"))
  6450. (if (> (length afile) 0)
  6451. (setq newfile-p (not (file-exists-p afile))
  6452. buffer (find-file-noselect afile))
  6453. (setq buffer (current-buffer)))
  6454. (unless buffer
  6455. (error "Cannot access file \"%s\"" afile))
  6456. (if (and (> (length heading) 0)
  6457. (string-match "^\\*+" heading))
  6458. (setq level (match-end 0))
  6459. (setq heading nil level 0))
  6460. (save-excursion
  6461. (org-back-to-heading t)
  6462. ;; Get context information that will be lost by moving the tree
  6463. (org-refresh-category-properties)
  6464. (setq category (org-get-category)
  6465. todo (and (looking-at org-todo-line-regexp)
  6466. (match-string 2))
  6467. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  6468. ltags (org-get-tags)
  6469. itags (org-delete-all ltags (org-get-tags-at)))
  6470. (setq ltags (mapconcat 'identity ltags " ")
  6471. itags (mapconcat 'identity itags " "))
  6472. ;; We first only copy, in case something goes wrong
  6473. ;; we need to protect this-command, to avoid kill-region sets it,
  6474. ;; which would lead to duplication of subtrees
  6475. (let (this-command) (org-copy-subtree))
  6476. (set-buffer buffer)
  6477. ;; Enforce org-mode for the archive buffer
  6478. (if (not (org-mode-p))
  6479. ;; Force the mode for future visits.
  6480. (let ((org-insert-mode-line-in-empty-file t)
  6481. (org-inhibit-startup t))
  6482. (call-interactively 'org-mode)))
  6483. (when newfile-p
  6484. (goto-char (point-max))
  6485. (insert (format "\nArchived entries from file %s\n\n"
  6486. (buffer-file-name this-buffer))))
  6487. ;; Force the TODO keywords of the original buffer
  6488. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  6489. (org-todo-keywords-1 tr-org-todo-keywords-1)
  6490. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  6491. (org-done-keywords tr-org-done-keywords)
  6492. (org-todo-regexp tr-org-todo-regexp)
  6493. (org-todo-line-regexp tr-org-todo-line-regexp)
  6494. (org-odd-levels-only
  6495. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  6496. org-odd-levels-only
  6497. tr-org-odd-levels-only)))
  6498. (goto-char (point-min))
  6499. (if heading
  6500. (progn
  6501. (if (re-search-forward
  6502. (concat "^" (regexp-quote heading)
  6503. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  6504. nil t)
  6505. (goto-char (match-end 0))
  6506. ;; Heading not found, just insert it at the end
  6507. (goto-char (point-max))
  6508. (or (bolp) (insert "\n"))
  6509. (insert "\n" heading "\n")
  6510. (end-of-line 0))
  6511. ;; Make the subtree visible
  6512. (show-subtree)
  6513. (org-end-of-subtree t)
  6514. (skip-chars-backward " \t\r\n")
  6515. (and (looking-at "[ \t\r\n]*")
  6516. (replace-match "\n\n")))
  6517. ;; No specific heading, just go to end of file.
  6518. (goto-char (point-max)) (insert "\n"))
  6519. ;; Paste
  6520. (org-paste-subtree (org-get-legal-level level 1))
  6521. ;; Mark the entry as done
  6522. (when (and org-archive-mark-done
  6523. (looking-at org-todo-line-regexp)
  6524. (or (not (match-end 2))
  6525. (not (member (match-string 2) org-done-keywords))))
  6526. (let (org-log-done)
  6527. (org-todo
  6528. (car (or (member org-archive-mark-done org-done-keywords)
  6529. org-done-keywords)))))
  6530. ;; Add the context info
  6531. (when org-archive-save-context-info
  6532. (let ((l org-archive-save-context-info) e n v)
  6533. (while (setq e (pop l))
  6534. (when (and (setq v (symbol-value e))
  6535. (stringp v) (string-match "\\S-" v))
  6536. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  6537. (org-entry-put (point) n v)))))
  6538. ;; Save the buffer, if it is not the same buffer.
  6539. (if (not (eq this-buffer buffer)) (save-buffer))))
  6540. ;; Here we are back in the original buffer. Everything seems to have
  6541. ;; worked. So now cut the tree and finish up.
  6542. (let (this-command) (org-cut-subtree))
  6543. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  6544. (message "Subtree archived %s"
  6545. (if (eq this-buffer buffer)
  6546. (concat "under heading: " heading)
  6547. (concat "in file: " (abbreviate-file-name afile)))))))
  6548. (defun org-refresh-category-properties ()
  6549. "Refresh category text properties in teh buffer."
  6550. (let ((def-cat (cond
  6551. ((null org-category)
  6552. (if buffer-file-name
  6553. (file-name-sans-extension
  6554. (file-name-nondirectory buffer-file-name))
  6555. "???"))
  6556. ((symbolp org-category) (symbol-name org-category))
  6557. (t org-category)))
  6558. beg end cat pos optionp)
  6559. (org-unmodified
  6560. (save-excursion
  6561. (save-restriction
  6562. (widen)
  6563. (goto-char (point-min))
  6564. (put-text-property (point) (point-max) 'org-category def-cat)
  6565. (while (re-search-forward
  6566. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6567. (setq pos (match-end 0)
  6568. optionp (equal (char-after (match-beginning 0)) ?#)
  6569. cat (org-trim (match-string 2)))
  6570. (if optionp
  6571. (setq beg (point-at-bol) end (point-max))
  6572. (org-back-to-heading t)
  6573. (setq beg (point) end (org-end-of-subtree t t)))
  6574. (put-text-property beg end 'org-category cat)
  6575. (goto-char pos)))))))
  6576. (defun org-archive-all-done (&optional tag)
  6577. "Archive sublevels of the current tree without open TODO items.
  6578. If the cursor is not on a headline, try all level 1 trees. If
  6579. it is on a headline, try all direct children.
  6580. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  6581. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  6582. (rea (concat ".*:" org-archive-tag ":"))
  6583. (begm (make-marker))
  6584. (endm (make-marker))
  6585. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  6586. "Move subtree to archive (no open TODO items)? "))
  6587. beg end (cntarch 0))
  6588. (if (org-on-heading-p)
  6589. (progn
  6590. (setq re1 (concat "^" (regexp-quote
  6591. (make-string
  6592. (1+ (- (match-end 0) (match-beginning 0)))
  6593. ?*))
  6594. " "))
  6595. (move-marker begm (point))
  6596. (move-marker endm (org-end-of-subtree t)))
  6597. (setq re1 "^* ")
  6598. (move-marker begm (point-min))
  6599. (move-marker endm (point-max)))
  6600. (save-excursion
  6601. (goto-char begm)
  6602. (while (re-search-forward re1 endm t)
  6603. (setq beg (match-beginning 0)
  6604. end (save-excursion (org-end-of-subtree t) (point)))
  6605. (goto-char beg)
  6606. (if (re-search-forward re end t)
  6607. (goto-char end)
  6608. (goto-char beg)
  6609. (if (and (or (not tag) (not (looking-at rea)))
  6610. (y-or-n-p question))
  6611. (progn
  6612. (if tag
  6613. (org-toggle-tag org-archive-tag 'on)
  6614. (org-archive-subtree))
  6615. (setq cntarch (1+ cntarch)))
  6616. (goto-char end)))))
  6617. (message "%d trees archived" cntarch)))
  6618. (defun org-cycle-hide-drawers (state)
  6619. "Re-hide all drawers after a visibility state change."
  6620. (when (and (org-mode-p)
  6621. (not (memq state '(overview folded))))
  6622. (save-excursion
  6623. (let* ((globalp (memq state '(contents all)))
  6624. (beg (if globalp (point-min) (point)))
  6625. (end (if globalp (point-max) (org-end-of-subtree t))))
  6626. (goto-char beg)
  6627. (while (re-search-forward org-drawer-regexp end t)
  6628. (org-flag-drawer t))))))
  6629. (defun org-flag-drawer (flag)
  6630. (save-excursion
  6631. (beginning-of-line 1)
  6632. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6633. (let ((b (match-end 0)))
  6634. (if (re-search-forward
  6635. "^[ \t]*:END:"
  6636. (save-excursion (outline-next-heading) (point)) t)
  6637. (outline-flag-region b (point-at-eol) flag)
  6638. (error ":END: line missing"))))))
  6639. (defun org-cycle-hide-archived-subtrees (state)
  6640. "Re-hide all archived subtrees after a visibility state change."
  6641. (when (and (not org-cycle-open-archived-trees)
  6642. (not (memq state '(overview folded))))
  6643. (save-excursion
  6644. (let* ((globalp (memq state '(contents all)))
  6645. (beg (if globalp (point-min) (point)))
  6646. (end (if globalp (point-max) (org-end-of-subtree t))))
  6647. (org-hide-archived-subtrees beg end)
  6648. (goto-char beg)
  6649. (if (looking-at (concat ".*:" org-archive-tag ":"))
  6650. (message (substitute-command-keys
  6651. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  6652. (defun org-force-cycle-archived ()
  6653. "Cycle subtree even if it is archived."
  6654. (interactive)
  6655. (setq this-command 'org-cycle)
  6656. (let ((org-cycle-open-archived-trees t))
  6657. (call-interactively 'org-cycle)))
  6658. (defun org-hide-archived-subtrees (beg end)
  6659. "Re-hide all archived subtrees after a visibility state change."
  6660. (save-excursion
  6661. (let* ((re (concat ":" org-archive-tag ":")))
  6662. (goto-char beg)
  6663. (while (re-search-forward re end t)
  6664. (and (org-on-heading-p) (hide-subtree))
  6665. (org-end-of-subtree t)))))
  6666. (defun org-toggle-tag (tag &optional onoff)
  6667. "Toggle the tag TAG for the current line.
  6668. If ONOFF is `on' or `off', don't toggle but set to this state."
  6669. (unless (org-on-heading-p t) (error "Not on headling"))
  6670. (let (res current)
  6671. (save-excursion
  6672. (beginning-of-line)
  6673. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  6674. (point-at-eol) t)
  6675. (progn
  6676. (setq current (match-string 1))
  6677. (replace-match ""))
  6678. (setq current ""))
  6679. (setq current (nreverse (org-split-string current ":")))
  6680. (cond
  6681. ((eq onoff 'on)
  6682. (setq res t)
  6683. (or (member tag current) (push tag current)))
  6684. ((eq onoff 'off)
  6685. (or (not (member tag current)) (setq current (delete tag current))))
  6686. (t (if (member tag current)
  6687. (setq current (delete tag current))
  6688. (setq res t)
  6689. (push tag current))))
  6690. (end-of-line 1)
  6691. (if current
  6692. (progn
  6693. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  6694. (org-set-tags nil t))
  6695. (delete-horizontal-space))
  6696. (run-hooks 'org-after-tags-change-hook))
  6697. res))
  6698. (defun org-toggle-archive-tag (&optional arg)
  6699. "Toggle the archive tag for the current headline.
  6700. With prefix ARG, check all children of current headline and offer tagging
  6701. the children that do not contain any open TODO items."
  6702. (interactive "P")
  6703. (if arg
  6704. (org-archive-all-done 'tag)
  6705. (let (set)
  6706. (save-excursion
  6707. (org-back-to-heading t)
  6708. (setq set (org-toggle-tag org-archive-tag))
  6709. (when set (hide-subtree)))
  6710. (and set (beginning-of-line 1))
  6711. (message "Subtree %s" (if set "archived" "unarchived")))))
  6712. ;;;; Tables
  6713. ;;; The table editor
  6714. ;; Watch out: Here we are talking about two different kind of tables.
  6715. ;; Most of the code is for the tables created with the Org-mode table editor.
  6716. ;; Sometimes, we talk about tables created and edited with the table.el
  6717. ;; Emacs package. We call the former org-type tables, and the latter
  6718. ;; table.el-type tables.
  6719. (defun org-before-change-function (beg end)
  6720. "Every change indicates that a table might need an update."
  6721. (setq org-table-may-need-update t))
  6722. (defconst org-table-line-regexp "^[ \t]*|"
  6723. "Detects an org-type table line.")
  6724. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  6725. "Detects an org-type table line.")
  6726. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  6727. "Detects a table line marked for automatic recalculation.")
  6728. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  6729. "Detects a table line marked for automatic recalculation.")
  6730. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  6731. "Detects a table line marked for automatic recalculation.")
  6732. (defconst org-table-hline-regexp "^[ \t]*|-"
  6733. "Detects an org-type table hline.")
  6734. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  6735. "Detects a table-type table hline.")
  6736. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  6737. "Detects an org-type or table-type table.")
  6738. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  6739. "Searching from within a table (any type) this finds the first line
  6740. outside the table.")
  6741. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  6742. "Searching from within a table (any type) this finds the first line
  6743. outside the table.")
  6744. (defvar org-table-last-highlighted-reference nil)
  6745. (defvar org-table-formula-history nil)
  6746. (defvar org-table-column-names nil
  6747. "Alist with column names, derived from the `!' line.")
  6748. (defvar org-table-column-name-regexp nil
  6749. "Regular expression matching the current column names.")
  6750. (defvar org-table-local-parameters nil
  6751. "Alist with parameter names, derived from the `$' line.")
  6752. (defvar org-table-named-field-locations nil
  6753. "Alist with locations of named fields.")
  6754. (defvar org-table-current-line-types nil
  6755. "Table row types, non-nil only for the duration of a comand.")
  6756. (defvar org-table-current-begin-line nil
  6757. "Table begin line, non-nil only for the duration of a comand.")
  6758. (defvar org-table-current-begin-pos nil
  6759. "Table begin position, non-nil only for the duration of a comand.")
  6760. (defvar org-table-dlines nil
  6761. "Vector of data line line numbers in the current table.")
  6762. (defvar org-table-hlines nil
  6763. "Vector of hline line numbers in the current table.")
  6764. (defconst org-table-range-regexp
  6765. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6766. ;; 1 2 3 4 5
  6767. "Regular expression for matching ranges in formulas.")
  6768. (defconst org-table-range-regexp2
  6769. (concat
  6770. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6771. "\\.\\."
  6772. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6773. "Match a range for reference display.")
  6774. (defconst org-table-translate-regexp
  6775. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6776. "Match a reference that needs translation, for reference display.")
  6777. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6778. (defun org-table-create-with-table.el ()
  6779. "Use the table.el package to insert a new table.
  6780. If there is already a table at point, convert between Org-mode tables
  6781. and table.el tables."
  6782. (interactive)
  6783. (require 'table)
  6784. (cond
  6785. ((org-at-table.el-p)
  6786. (if (y-or-n-p "Convert table to Org-mode table? ")
  6787. (org-table-convert)))
  6788. ((org-at-table-p)
  6789. (if (y-or-n-p "Convert table to table.el table? ")
  6790. (org-table-convert)))
  6791. (t (call-interactively 'table-insert))))
  6792. (defun org-table-create-or-convert-from-region (arg)
  6793. "Convert region to table, or create an empty table.
  6794. If there is an active region, convert it to a table, using the function
  6795. `org-table-convert-region'. See the documentation of that function
  6796. to learn how the prefix argument is interpreted to determine the field
  6797. separator.
  6798. If there is no such region, create an empty table with `org-table-create'."
  6799. (interactive "P")
  6800. (if (org-region-active-p)
  6801. (org-table-convert-region (region-beginning) (region-end) arg)
  6802. (org-table-create arg)))
  6803. (defun org-table-create (&optional size)
  6804. "Query for a size and insert a table skeleton.
  6805. SIZE is a string Columns x Rows like for example \"3x2\"."
  6806. (interactive "P")
  6807. (unless size
  6808. (setq size (read-string
  6809. (concat "Table size Columns x Rows [e.g. "
  6810. org-table-default-size "]: ")
  6811. "" nil org-table-default-size)))
  6812. (let* ((pos (point))
  6813. (indent (make-string (current-column) ?\ ))
  6814. (split (org-split-string size " *x *"))
  6815. (rows (string-to-number (nth 1 split)))
  6816. (columns (string-to-number (car split)))
  6817. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6818. "\n")))
  6819. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6820. (point-at-bol) (point)))
  6821. (beginning-of-line 1)
  6822. (newline))
  6823. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6824. (dotimes (i rows) (insert line))
  6825. (goto-char pos)
  6826. (if (> rows 1)
  6827. ;; Insert a hline after the first row.
  6828. (progn
  6829. (end-of-line 1)
  6830. (insert "\n|-")
  6831. (goto-char pos)))
  6832. (org-table-align)))
  6833. (defun org-table-convert-region (beg0 end0 &optional separator)
  6834. "Convert region to a table.
  6835. The region goes from BEG0 to END0, but these borders will be moved
  6836. slightly, to make sure a beginning of line in the first line is included.
  6837. SEPARATOR specifies the field separator in the lines. It can have the
  6838. following values:
  6839. '(4) Use the comma as a field separator
  6840. '(16) Use a TAB as field separator
  6841. integer When a number, use that many spaces as field separator
  6842. nil When nil, the command tries to be smart and figure out the
  6843. separator in the following way:
  6844. - when each line contains a TAB, assume TAB-separated material
  6845. - when each line contains a comme, assume CSV material
  6846. - else, assume one or more SPACE charcters as separator."
  6847. (interactive "rP")
  6848. (let* ((beg (min beg0 end0))
  6849. (end (max beg0 end0))
  6850. re)
  6851. (goto-char beg)
  6852. (beginning-of-line 1)
  6853. (setq beg (move-marker (make-marker) (point)))
  6854. (goto-char end)
  6855. (if (bolp) (backward-char 1) (end-of-line 1))
  6856. (setq end (move-marker (make-marker) (point)))
  6857. ;; Get the right field separator
  6858. (unless separator
  6859. (goto-char beg)
  6860. (setq separator
  6861. (cond
  6862. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  6863. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  6864. (t 1))))
  6865. (setq re (cond
  6866. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  6867. ((equal separator '(16)) "^\\|\t")
  6868. ((integerp separator)
  6869. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  6870. (t (error "This should not happen"))))
  6871. (goto-char beg)
  6872. (while (re-search-forward re end t)
  6873. (replace-match "| " t t))
  6874. (goto-char beg)
  6875. (insert " ")
  6876. (org-table-align)))
  6877. (defun org-table-import (file arg)
  6878. "Import FILE as a table.
  6879. The file is assumed to be tab-separated. Such files can be produced by most
  6880. spreadsheet and database applications. If no tabs (at least one per line)
  6881. are found, lines will be split on whitespace into fields."
  6882. (interactive "f\nP")
  6883. (or (bolp) (newline))
  6884. (let ((beg (point))
  6885. (pm (point-max)))
  6886. (insert-file-contents file)
  6887. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  6888. (defun org-table-export ()
  6889. "Export table as a tab-separated file.
  6890. Such a file can be imported into a spreadsheet program like Excel."
  6891. (interactive)
  6892. (let* ((beg (org-table-begin))
  6893. (end (org-table-end))
  6894. (table (buffer-substring beg end))
  6895. (file (read-file-name "Export table to: "))
  6896. buf)
  6897. (unless (or (not (file-exists-p file))
  6898. (y-or-n-p (format "Overwrite file %s? " file)))
  6899. (error "Abort"))
  6900. (with-current-buffer (find-file-noselect file)
  6901. (setq buf (current-buffer))
  6902. (erase-buffer)
  6903. (fundamental-mode)
  6904. (insert table)
  6905. (goto-char (point-min))
  6906. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  6907. (replace-match "" t t)
  6908. (end-of-line 1))
  6909. (goto-char (point-min))
  6910. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  6911. (replace-match "" t t)
  6912. (goto-char (min (1+ (point)) (point-max))))
  6913. (goto-char (point-min))
  6914. (while (re-search-forward "^-[-+]*$" nil t)
  6915. (replace-match "")
  6916. (if (looking-at "\n")
  6917. (delete-char 1)))
  6918. (goto-char (point-min))
  6919. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  6920. (replace-match "\t" t t))
  6921. (save-buffer))
  6922. (kill-buffer buf)))
  6923. (defvar org-table-aligned-begin-marker (make-marker)
  6924. "Marker at the beginning of the table last aligned.
  6925. Used to check if cursor still is in that table, to minimize realignment.")
  6926. (defvar org-table-aligned-end-marker (make-marker)
  6927. "Marker at the end of the table last aligned.
  6928. Used to check if cursor still is in that table, to minimize realignment.")
  6929. (defvar org-table-last-alignment nil
  6930. "List of flags for flushright alignment, from the last re-alignment.
  6931. This is being used to correctly align a single field after TAB or RET.")
  6932. (defvar org-table-last-column-widths nil
  6933. "List of max width of fields in each column.
  6934. This is being used to correctly align a single field after TAB or RET.")
  6935. (defvar org-table-overlay-coordinates nil
  6936. "Overlay coordinates after each align of a table.")
  6937. (make-variable-buffer-local 'org-table-overlay-coordinates)
  6938. (defvar org-last-recalc-line nil)
  6939. (defconst org-narrow-column-arrow "=>"
  6940. "Used as display property in narrowed table columns.")
  6941. (defun org-table-align ()
  6942. "Align the table at point by aligning all vertical bars."
  6943. (interactive)
  6944. (let* (
  6945. ;; Limits of table
  6946. (beg (org-table-begin))
  6947. (end (org-table-end))
  6948. ;; Current cursor position
  6949. (linepos (org-current-line))
  6950. (colpos (org-table-current-column))
  6951. (winstart (window-start))
  6952. (winstartline (org-current-line (min winstart (1- (point-max)))))
  6953. lines (new "") lengths l typenums ty fields maxfields i
  6954. column
  6955. (indent "") cnt frac
  6956. rfmt hfmt
  6957. (spaces '(1 . 1))
  6958. (sp1 (car spaces))
  6959. (sp2 (cdr spaces))
  6960. (rfmt1 (concat
  6961. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  6962. (hfmt1 (concat
  6963. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  6964. emptystrings links dates narrow fmax f1 len c e)
  6965. (untabify beg end)
  6966. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  6967. ;; Check if we have links or dates
  6968. (goto-char beg)
  6969. (setq links (re-search-forward org-bracket-link-regexp end t))
  6970. (goto-char beg)
  6971. (setq dates (and org-display-custom-times
  6972. (re-search-forward org-ts-regexp-both end t)))
  6973. ;; Make sure the link properties are right
  6974. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  6975. ;; Make sure the date properties are right
  6976. (when dates (goto-char beg) (while (org-activate-dates end)))
  6977. ;; Check if we are narrowing any columns
  6978. (goto-char beg)
  6979. (setq narrow (and org-format-transports-properties-p
  6980. (re-search-forward "<[0-9]+>" end t)))
  6981. ;; Get the rows
  6982. (setq lines (org-split-string
  6983. (buffer-substring beg end) "\n"))
  6984. ;; Store the indentation of the first line
  6985. (if (string-match "^ *" (car lines))
  6986. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  6987. ;; Mark the hlines by setting the corresponding element to nil
  6988. ;; At the same time, we remove trailing space.
  6989. (setq lines (mapcar (lambda (l)
  6990. (if (string-match "^ *|-" l)
  6991. nil
  6992. (if (string-match "[ \t]+$" l)
  6993. (substring l 0 (match-beginning 0))
  6994. l)))
  6995. lines))
  6996. ;; Get the data fields by splitting the lines.
  6997. (setq fields (mapcar
  6998. (lambda (l)
  6999. (org-split-string l " *| *"))
  7000. (delq nil (copy-sequence lines))))
  7001. ;; How many fields in the longest line?
  7002. (condition-case nil
  7003. (setq maxfields (apply 'max (mapcar 'length fields)))
  7004. (error
  7005. (kill-region beg end)
  7006. (org-table-create org-table-default-size)
  7007. (error "Empty table - created default table")))
  7008. ;; A list of empty strings to fill any short rows on output
  7009. (setq emptystrings (make-list maxfields ""))
  7010. ;; Check for special formatting.
  7011. (setq i -1)
  7012. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7013. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7014. ;; Check if there is an explicit width specified
  7015. (when narrow
  7016. (setq c column fmax nil)
  7017. (while c
  7018. (setq e (pop c))
  7019. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7020. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7021. ;; Find fields that are wider than fmax, and shorten them
  7022. (when fmax
  7023. (loop for xx in column do
  7024. (when (and (stringp xx)
  7025. (> (org-string-width xx) fmax))
  7026. (org-add-props xx nil
  7027. 'help-echo
  7028. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7029. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7030. (unless (> f1 1)
  7031. (error "Cannot narrow field starting with wide link \"%s\""
  7032. (match-string 0 xx)))
  7033. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7034. (add-text-properties (- f1 2) f1
  7035. (list 'display org-narrow-column-arrow)
  7036. xx)))))
  7037. ;; Get the maximum width for each column
  7038. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7039. ;; Get the fraction of numbers, to decide about alignment of the column
  7040. (setq cnt 0 frac 0.0)
  7041. (loop for x in column do
  7042. (if (equal x "")
  7043. nil
  7044. (setq frac ( / (+ (* frac cnt)
  7045. (if (string-match org-table-number-regexp x) 1 0))
  7046. (setq cnt (1+ cnt))))))
  7047. (push (>= frac org-table-number-fraction) typenums))
  7048. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7049. ;; Store the alignment of this table, for later editing of single fields
  7050. (setq org-table-last-alignment typenums
  7051. org-table-last-column-widths lengths)
  7052. ;; With invisible characters, `format' does not get the field width right
  7053. ;; So we need to make these fields wide by hand.
  7054. (when links
  7055. (loop for i from 0 upto (1- maxfields) do
  7056. (setq len (nth i lengths))
  7057. (loop for j from 0 upto (1- (length fields)) do
  7058. (setq c (nthcdr i (car (nthcdr j fields))))
  7059. (if (and (stringp (car c))
  7060. (string-match org-bracket-link-regexp (car c))
  7061. (< (org-string-width (car c)) len))
  7062. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7063. ;; Compute the formats needed for output of the table
  7064. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7065. (while (setq l (pop lengths))
  7066. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7067. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7068. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7069. (setq rfmt (concat rfmt "\n")
  7070. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7071. (setq new (mapconcat
  7072. (lambda (l)
  7073. (if l (apply 'format rfmt
  7074. (append (pop fields) emptystrings))
  7075. hfmt))
  7076. lines ""))
  7077. ;; Replace the old one
  7078. (delete-region beg end)
  7079. (move-marker end nil)
  7080. (move-marker org-table-aligned-begin-marker (point))
  7081. (insert new)
  7082. (move-marker org-table-aligned-end-marker (point))
  7083. (when (and orgtbl-mode (not (org-mode-p)))
  7084. (goto-char org-table-aligned-begin-marker)
  7085. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7086. ;; Try to move to the old location
  7087. (goto-line winstartline)
  7088. (setq winstart (point-at-bol))
  7089. (goto-line linepos)
  7090. (set-window-start (selected-window) winstart 'noforce)
  7091. (org-table-goto-column colpos)
  7092. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7093. (setq org-table-may-need-update nil)
  7094. ))
  7095. (defun org-string-width (s)
  7096. "Compute width of string, ignoring invisible characters.
  7097. This ignores character with invisibility property `org-link', and also
  7098. characters with property `org-cwidth', because these will become invisible
  7099. upon the next fontification round."
  7100. (let (b l)
  7101. (when (or (eq t buffer-invisibility-spec)
  7102. (assq 'org-link buffer-invisibility-spec))
  7103. (while (setq b (text-property-any 0 (length s)
  7104. 'invisible 'org-link s))
  7105. (setq s (concat (substring s 0 b)
  7106. (substring s (or (next-single-property-change
  7107. b 'invisible s) (length s)))))))
  7108. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7109. (setq s (concat (substring s 0 b)
  7110. (substring s (or (next-single-property-change
  7111. b 'org-cwidth s) (length s))))))
  7112. (setq l (string-width s) b -1)
  7113. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7114. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7115. l))
  7116. (defun org-table-begin (&optional table-type)
  7117. "Find the beginning of the table and return its position.
  7118. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7119. (save-excursion
  7120. (if (not (re-search-backward
  7121. (if table-type org-table-any-border-regexp
  7122. org-table-border-regexp)
  7123. nil t))
  7124. (progn (goto-char (point-min)) (point))
  7125. (goto-char (match-beginning 0))
  7126. (beginning-of-line 2)
  7127. (point))))
  7128. (defun org-table-end (&optional table-type)
  7129. "Find the end of the table and return its position.
  7130. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7131. (save-excursion
  7132. (if (not (re-search-forward
  7133. (if table-type org-table-any-border-regexp
  7134. org-table-border-regexp)
  7135. nil t))
  7136. (goto-char (point-max))
  7137. (goto-char (match-beginning 0)))
  7138. (point-marker)))
  7139. (defun org-table-justify-field-maybe (&optional new)
  7140. "Justify the current field, text to left, number to right.
  7141. Optional argument NEW may specify text to replace the current field content."
  7142. (cond
  7143. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7144. ((org-at-table-hline-p))
  7145. ((and (not new)
  7146. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7147. (current-buffer)))
  7148. (< (point) org-table-aligned-begin-marker)
  7149. (>= (point) org-table-aligned-end-marker)))
  7150. ;; This is not the same table, force a full re-align
  7151. (setq org-table-may-need-update t))
  7152. (t ;; realign the current field, based on previous full realign
  7153. (let* ((pos (point)) s
  7154. (col (org-table-current-column))
  7155. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7156. l f n o e)
  7157. (when (> col 0)
  7158. (skip-chars-backward "^|\n")
  7159. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7160. (progn
  7161. (setq s (match-string 1)
  7162. o (match-string 0)
  7163. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7164. e (not (= (match-beginning 2) (match-end 2))))
  7165. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7166. l (if e "|" (setq org-table-may-need-update t) ""))
  7167. n (format f s))
  7168. (if new
  7169. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7170. (setq n (format f new))
  7171. (setq n (concat new "|") org-table-may-need-update t)))
  7172. (or (equal n o)
  7173. (let (org-table-may-need-update)
  7174. (replace-match n t t))))
  7175. (setq org-table-may-need-update t))
  7176. (goto-char pos))))))
  7177. (defun org-table-next-field ()
  7178. "Go to the next field in the current table, creating new lines as needed.
  7179. Before doing so, re-align the table if necessary."
  7180. (interactive)
  7181. (org-table-maybe-eval-formula)
  7182. (org-table-maybe-recalculate-line)
  7183. (if (and org-table-automatic-realign
  7184. org-table-may-need-update)
  7185. (org-table-align))
  7186. (let ((end (org-table-end)))
  7187. (if (org-at-table-hline-p)
  7188. (end-of-line 1))
  7189. (condition-case nil
  7190. (progn
  7191. (re-search-forward "|" end)
  7192. (if (looking-at "[ \t]*$")
  7193. (re-search-forward "|" end))
  7194. (if (and (looking-at "-")
  7195. org-table-tab-jumps-over-hlines
  7196. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7197. (goto-char (match-beginning 1)))
  7198. (if (looking-at "-")
  7199. (progn
  7200. (beginning-of-line 0)
  7201. (org-table-insert-row 'below))
  7202. (if (looking-at " ") (forward-char 1))))
  7203. (error
  7204. (org-table-insert-row 'below)))))
  7205. (defun org-table-previous-field ()
  7206. "Go to the previous field in the table.
  7207. Before doing so, re-align the table if necessary."
  7208. (interactive)
  7209. (org-table-justify-field-maybe)
  7210. (org-table-maybe-recalculate-line)
  7211. (if (and org-table-automatic-realign
  7212. org-table-may-need-update)
  7213. (org-table-align))
  7214. (if (org-at-table-hline-p)
  7215. (end-of-line 1))
  7216. (re-search-backward "|" (org-table-begin))
  7217. (re-search-backward "|" (org-table-begin))
  7218. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7219. (re-search-backward "|" (org-table-begin)))
  7220. (if (looking-at "| ?")
  7221. (goto-char (match-end 0))))
  7222. (defun org-table-next-row ()
  7223. "Go to the next row (same column) in the current table.
  7224. Before doing so, re-align the table if necessary."
  7225. (interactive)
  7226. (org-table-maybe-eval-formula)
  7227. (org-table-maybe-recalculate-line)
  7228. (if (or (looking-at "[ \t]*$")
  7229. (save-excursion (skip-chars-backward " \t") (bolp)))
  7230. (newline)
  7231. (if (and org-table-automatic-realign
  7232. org-table-may-need-update)
  7233. (org-table-align))
  7234. (let ((col (org-table-current-column)))
  7235. (beginning-of-line 2)
  7236. (if (or (not (org-at-table-p))
  7237. (org-at-table-hline-p))
  7238. (progn
  7239. (beginning-of-line 0)
  7240. (org-table-insert-row 'below)))
  7241. (org-table-goto-column col)
  7242. (skip-chars-backward "^|\n\r")
  7243. (if (looking-at " ") (forward-char 1)))))
  7244. (defun org-table-copy-down (n)
  7245. "Copy a field down in the current column.
  7246. If the field at the cursor is empty, copy into it the content of the nearest
  7247. non-empty field above. With argument N, use the Nth non-empty field.
  7248. If the current field is not empty, it is copied down to the next row, and
  7249. the cursor is moved with it. Therefore, repeating this command causes the
  7250. column to be filled row-by-row.
  7251. If the variable `org-table-copy-increment' is non-nil and the field is an
  7252. integer or a timestamp, it will be incremented while copying. In the case of
  7253. a timestamp, if the cursor is on the year, change the year. If it is on the
  7254. month or the day, change that. Point will stay on the current date field
  7255. in order to easily repeat the interval."
  7256. (interactive "p")
  7257. (let* ((colpos (org-table-current-column))
  7258. (col (current-column))
  7259. (field (org-table-get-field))
  7260. (non-empty (string-match "[^ \t]" field))
  7261. (beg (org-table-begin))
  7262. txt)
  7263. (org-table-check-inside-data-field)
  7264. (if non-empty
  7265. (progn
  7266. (setq txt (org-trim field))
  7267. (org-table-next-row)
  7268. (org-table-blank-field))
  7269. (save-excursion
  7270. (setq txt
  7271. (catch 'exit
  7272. (while (progn (beginning-of-line 1)
  7273. (re-search-backward org-table-dataline-regexp
  7274. beg t))
  7275. (org-table-goto-column colpos t)
  7276. (if (and (looking-at
  7277. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7278. (= (setq n (1- n)) 0))
  7279. (throw 'exit (match-string 1))))))))
  7280. (if txt
  7281. (progn
  7282. (if (and org-table-copy-increment
  7283. (string-match "^[0-9]+$" txt))
  7284. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7285. (insert txt)
  7286. (move-to-column col)
  7287. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7288. (org-timestamp-up 1)
  7289. (org-table-maybe-recalculate-line))
  7290. (org-table-align)
  7291. (move-to-column col))
  7292. (error "No non-empty field found"))))
  7293. (defun org-table-check-inside-data-field ()
  7294. "Is point inside a table data field?
  7295. I.e. not on a hline or before the first or after the last column?
  7296. This actually throws an error, so it aborts the current command."
  7297. (if (or (not (org-at-table-p))
  7298. (= (org-table-current-column) 0)
  7299. (org-at-table-hline-p)
  7300. (looking-at "[ \t]*$"))
  7301. (error "Not in table data field")))
  7302. (defvar org-table-clip nil
  7303. "Clipboard for table regions.")
  7304. (defun org-table-blank-field ()
  7305. "Blank the current table field or active region."
  7306. (interactive)
  7307. (org-table-check-inside-data-field)
  7308. (if (and (interactive-p) (org-region-active-p))
  7309. (let (org-table-clip)
  7310. (org-table-cut-region (region-beginning) (region-end)))
  7311. (skip-chars-backward "^|")
  7312. (backward-char 1)
  7313. (if (looking-at "|[^|\n]+")
  7314. (let* ((pos (match-beginning 0))
  7315. (match (match-string 0))
  7316. (len (org-string-width match)))
  7317. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7318. (goto-char (+ 2 pos))
  7319. (substring match 1)))))
  7320. (defun org-table-get-field (&optional n replace)
  7321. "Return the value of the field in column N of current row.
  7322. N defaults to current field.
  7323. If REPLACE is a string, replace field with this value. The return value
  7324. is always the old value."
  7325. (and n (org-table-goto-column n))
  7326. (skip-chars-backward "^|\n")
  7327. (backward-char 1)
  7328. (if (looking-at "|[^|\r\n]*")
  7329. (let* ((pos (match-beginning 0))
  7330. (val (buffer-substring (1+ pos) (match-end 0))))
  7331. (if replace
  7332. (replace-match (concat "|" replace) t t))
  7333. (goto-char (min (point-at-eol) (+ 2 pos)))
  7334. val)
  7335. (forward-char 1) ""))
  7336. (defun org-table-field-info (arg)
  7337. "Show info about the current field, and highlight any reference at point."
  7338. (interactive "P")
  7339. (org-table-get-specials)
  7340. (save-excursion
  7341. (let* ((pos (point))
  7342. (col (org-table-current-column))
  7343. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7344. (name (car (rassoc (list (org-current-line) col)
  7345. org-table-named-field-locations)))
  7346. (eql (org-table-get-stored-formulas))
  7347. (dline (org-table-current-dline))
  7348. (ref (format "@%d$%d" dline col))
  7349. (ref1 (org-table-convert-refs-to-an ref))
  7350. (fequation (or (assoc name eql) (assoc ref eql)))
  7351. (cequation (assoc (int-to-string col) eql))
  7352. (eqn (or fequation cequation)))
  7353. (goto-char pos)
  7354. (condition-case nil
  7355. (org-table-show-reference 'local)
  7356. (error nil))
  7357. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7358. dline col
  7359. (if cname (concat " or $" cname) "")
  7360. dline col ref1
  7361. (if name (concat " or $" name) "")
  7362. ;; FIXME: formula info not correct if special table line
  7363. (if eqn
  7364. (concat ", formula: "
  7365. (org-table-formula-to-user
  7366. (concat
  7367. (if (string-match "^[$@]"(car eqn)) "" "$")
  7368. (car eqn) "=" (cdr eqn))))
  7369. "")))))
  7370. (defun org-table-current-column ()
  7371. "Find out which column we are in.
  7372. When called interactively, column is also displayed in echo area."
  7373. (interactive)
  7374. (if (interactive-p) (org-table-check-inside-data-field))
  7375. (save-excursion
  7376. (let ((cnt 0) (pos (point)))
  7377. (beginning-of-line 1)
  7378. (while (search-forward "|" pos t)
  7379. (setq cnt (1+ cnt)))
  7380. (if (interactive-p) (message "This is table column %d" cnt))
  7381. cnt)))
  7382. (defun org-table-current-dline ()
  7383. "Find out what table data line we are in.
  7384. Only datalins count for this."
  7385. (interactive)
  7386. (if (interactive-p) (org-table-check-inside-data-field))
  7387. (save-excursion
  7388. (let ((cnt 0) (pos (point)))
  7389. (goto-char (org-table-begin))
  7390. (while (<= (point) pos)
  7391. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7392. (beginning-of-line 2))
  7393. (if (interactive-p) (message "This is table line %d" cnt))
  7394. cnt)))
  7395. (defun org-table-goto-column (n &optional on-delim force)
  7396. "Move the cursor to the Nth column in the current table line.
  7397. With optional argument ON-DELIM, stop with point before the left delimiter
  7398. of the field.
  7399. If there are less than N fields, just go to after the last delimiter.
  7400. However, when FORCE is non-nil, create new columns if necessary."
  7401. (interactive "p")
  7402. (let ((pos (point-at-eol)))
  7403. (beginning-of-line 1)
  7404. (when (> n 0)
  7405. (while (and (> (setq n (1- n)) -1)
  7406. (or (search-forward "|" pos t)
  7407. (and force
  7408. (progn (end-of-line 1)
  7409. (skip-chars-backward "^|")
  7410. (insert " | "))))))
  7411. ; (backward-char 2) t)))))
  7412. (when (and force (not (looking-at ".*|")))
  7413. (save-excursion (end-of-line 1) (insert " | ")))
  7414. (if on-delim
  7415. (backward-char 1)
  7416. (if (looking-at " ") (forward-char 1))))))
  7417. (defun org-at-table-p (&optional table-type)
  7418. "Return t if the cursor is inside an org-type table.
  7419. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7420. (if org-enable-table-editor
  7421. (save-excursion
  7422. (beginning-of-line 1)
  7423. (looking-at (if table-type org-table-any-line-regexp
  7424. org-table-line-regexp)))
  7425. nil))
  7426. (defun org-at-table.el-p ()
  7427. "Return t if and only if we are at a table.el table."
  7428. (and (org-at-table-p 'any)
  7429. (save-excursion
  7430. (goto-char (org-table-begin 'any))
  7431. (looking-at org-table1-hline-regexp))))
  7432. (defun org-table-recognize-table.el ()
  7433. "If there is a table.el table nearby, recognize it and move into it."
  7434. (if org-table-tab-recognizes-table.el
  7435. (if (org-at-table.el-p)
  7436. (progn
  7437. (beginning-of-line 1)
  7438. (if (looking-at org-table-dataline-regexp)
  7439. nil
  7440. (if (looking-at org-table1-hline-regexp)
  7441. (progn
  7442. (beginning-of-line 2)
  7443. (if (looking-at org-table-any-border-regexp)
  7444. (beginning-of-line -1)))))
  7445. (if (re-search-forward "|" (org-table-end t) t)
  7446. (progn
  7447. (require 'table)
  7448. (if (table--at-cell-p (point))
  7449. t
  7450. (message "recognizing table.el table...")
  7451. (table-recognize-table)
  7452. (message "recognizing table.el table...done")))
  7453. (error "This should not happen..."))
  7454. t)
  7455. nil)
  7456. nil))
  7457. (defun org-at-table-hline-p ()
  7458. "Return t if the cursor is inside a hline in a table."
  7459. (if org-enable-table-editor
  7460. (save-excursion
  7461. (beginning-of-line 1)
  7462. (looking-at org-table-hline-regexp))
  7463. nil))
  7464. (defun org-table-insert-column ()
  7465. "Insert a new column into the table."
  7466. (interactive)
  7467. (if (not (org-at-table-p))
  7468. (error "Not at a table"))
  7469. (org-table-find-dataline)
  7470. (let* ((col (max 1 (org-table-current-column)))
  7471. (beg (org-table-begin))
  7472. (end (org-table-end))
  7473. ;; Current cursor position
  7474. (linepos (org-current-line))
  7475. (colpos col))
  7476. (goto-char beg)
  7477. (while (< (point) end)
  7478. (if (org-at-table-hline-p)
  7479. nil
  7480. (org-table-goto-column col t)
  7481. (insert "| "))
  7482. (beginning-of-line 2))
  7483. (move-marker end nil)
  7484. (goto-line linepos)
  7485. (org-table-goto-column colpos)
  7486. (org-table-align)
  7487. (org-table-fix-formulas "$" nil (1- col) 1)))
  7488. (defun org-table-find-dataline ()
  7489. "Find a dataline in the current table, which is needed for column commands."
  7490. (if (and (org-at-table-p)
  7491. (not (org-at-table-hline-p)))
  7492. t
  7493. (let ((col (current-column))
  7494. (end (org-table-end)))
  7495. (move-to-column col)
  7496. (while (and (< (point) end)
  7497. (or (not (= (current-column) col))
  7498. (org-at-table-hline-p)))
  7499. (beginning-of-line 2)
  7500. (move-to-column col))
  7501. (if (and (org-at-table-p)
  7502. (not (org-at-table-hline-p)))
  7503. t
  7504. (error
  7505. "Please position cursor in a data line for column operations")))))
  7506. (defun org-table-delete-column ()
  7507. "Delete a column from the table."
  7508. (interactive)
  7509. (if (not (org-at-table-p))
  7510. (error "Not at a table"))
  7511. (org-table-find-dataline)
  7512. (org-table-check-inside-data-field)
  7513. (let* ((col (org-table-current-column))
  7514. (beg (org-table-begin))
  7515. (end (org-table-end))
  7516. ;; Current cursor position
  7517. (linepos (org-current-line))
  7518. (colpos col))
  7519. (goto-char beg)
  7520. (while (< (point) end)
  7521. (if (org-at-table-hline-p)
  7522. nil
  7523. (org-table-goto-column col t)
  7524. (and (looking-at "|[^|\n]+|")
  7525. (replace-match "|")))
  7526. (beginning-of-line 2))
  7527. (move-marker end nil)
  7528. (goto-line linepos)
  7529. (org-table-goto-column colpos)
  7530. (org-table-align)
  7531. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  7532. col -1 col)))
  7533. (defun org-table-move-column-right ()
  7534. "Move column to the right."
  7535. (interactive)
  7536. (org-table-move-column nil))
  7537. (defun org-table-move-column-left ()
  7538. "Move column to the left."
  7539. (interactive)
  7540. (org-table-move-column 'left))
  7541. (defun org-table-move-column (&optional left)
  7542. "Move the current column to the right. With arg LEFT, move to the left."
  7543. (interactive "P")
  7544. (if (not (org-at-table-p))
  7545. (error "Not at a table"))
  7546. (org-table-find-dataline)
  7547. (org-table-check-inside-data-field)
  7548. (let* ((col (org-table-current-column))
  7549. (col1 (if left (1- col) col))
  7550. (beg (org-table-begin))
  7551. (end (org-table-end))
  7552. ;; Current cursor position
  7553. (linepos (org-current-line))
  7554. (colpos (if left (1- col) (1+ col))))
  7555. (if (and left (= col 1))
  7556. (error "Cannot move column further left"))
  7557. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  7558. (error "Cannot move column further right"))
  7559. (goto-char beg)
  7560. (while (< (point) end)
  7561. (if (org-at-table-hline-p)
  7562. nil
  7563. (org-table-goto-column col1 t)
  7564. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  7565. (replace-match "|\\2|\\1|")))
  7566. (beginning-of-line 2))
  7567. (move-marker end nil)
  7568. (goto-line linepos)
  7569. (org-table-goto-column colpos)
  7570. (org-table-align)
  7571. (org-table-fix-formulas
  7572. "$" (list (cons (number-to-string col) (number-to-string colpos))
  7573. (cons (number-to-string colpos) (number-to-string col))))))
  7574. (defun org-table-move-row-down ()
  7575. "Move table row down."
  7576. (interactive)
  7577. (org-table-move-row nil))
  7578. (defun org-table-move-row-up ()
  7579. "Move table row up."
  7580. (interactive)
  7581. (org-table-move-row 'up))
  7582. (defun org-table-move-row (&optional up)
  7583. "Move the current table line down. With arg UP, move it up."
  7584. (interactive "P")
  7585. (let* ((col (current-column))
  7586. (pos (point))
  7587. (hline1p (save-excursion (beginning-of-line 1)
  7588. (looking-at org-table-hline-regexp)))
  7589. (dline1 (org-table-current-dline))
  7590. (dline2 (+ dline1 (if up -1 1)))
  7591. (tonew (if up 0 2))
  7592. txt hline2p)
  7593. (beginning-of-line tonew)
  7594. (unless (org-at-table-p)
  7595. (goto-char pos)
  7596. (error "Cannot move row further"))
  7597. (setq hline2p (looking-at org-table-hline-regexp))
  7598. (goto-char pos)
  7599. (beginning-of-line 1)
  7600. (setq pos (point))
  7601. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  7602. (delete-region (point) (1+ (point-at-eol)))
  7603. (beginning-of-line tonew)
  7604. (insert txt)
  7605. (beginning-of-line 0)
  7606. (move-to-column col)
  7607. (unless (or hline1p hline2p)
  7608. (org-table-fix-formulas
  7609. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  7610. (cons (number-to-string dline2) (number-to-string dline1)))))))
  7611. (defun org-table-insert-row (&optional arg)
  7612. "Insert a new row above the current line into the table.
  7613. With prefix ARG, insert below the current line."
  7614. (interactive "P")
  7615. (if (not (org-at-table-p))
  7616. (error "Not at a table"))
  7617. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  7618. (new (org-table-clean-line line)))
  7619. ;; Fix the first field if necessary
  7620. (if (string-match "^[ \t]*| *[#$] *|" line)
  7621. (setq new (replace-match (match-string 0 line) t t new)))
  7622. (beginning-of-line (if arg 2 1))
  7623. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  7624. (beginning-of-line 0)
  7625. (re-search-forward "| ?" (point-at-eol) t)
  7626. (and (or org-table-may-need-update org-table-overlay-coordinates)
  7627. (org-table-align))
  7628. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  7629. (defun org-table-insert-hline (&optional above)
  7630. "Insert a horizontal-line below the current line into the table.
  7631. With prefix ABOVE, insert above the current line."
  7632. (interactive "P")
  7633. (if (not (org-at-table-p))
  7634. (error "Not at a table"))
  7635. (let ((line (org-table-clean-line
  7636. (buffer-substring (point-at-bol) (point-at-eol))))
  7637. (col (current-column)))
  7638. (while (string-match "|\\( +\\)|" line)
  7639. (setq line (replace-match
  7640. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  7641. ?-) "|") t t line)))
  7642. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  7643. (beginning-of-line (if above 1 2))
  7644. (insert line "\n")
  7645. (beginning-of-line (if above 1 -1))
  7646. (move-to-column col)
  7647. (and org-table-overlay-coordinates (org-table-align))))
  7648. (defun org-table-hline-and-move (&optional same-column)
  7649. "Insert a hline and move to the row below that line."
  7650. (interactive "P")
  7651. (let ((col (org-table-current-column)))
  7652. (org-table-maybe-eval-formula)
  7653. (org-table-maybe-recalculate-line)
  7654. (org-table-insert-hline)
  7655. (end-of-line 2)
  7656. (if (looking-at "\n[ \t]*|-")
  7657. (progn (insert "\n|") (org-table-align))
  7658. (org-table-next-field))
  7659. (if same-column (org-table-goto-column col))))
  7660. (defun org-table-clean-line (s)
  7661. "Convert a table line S into a string with only \"|\" and space.
  7662. In particular, this does handle wide and invisible characters."
  7663. (if (string-match "^[ \t]*|-" s)
  7664. ;; It's a hline, just map the characters
  7665. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  7666. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  7667. (setq s (replace-match
  7668. (concat "|" (make-string (org-string-width (match-string 1 s))
  7669. ?\ ) "|")
  7670. t t s)))
  7671. s))
  7672. (defun org-table-kill-row ()
  7673. "Delete the current row or horizontal line from the table."
  7674. (interactive)
  7675. (if (not (org-at-table-p))
  7676. (error "Not at a table"))
  7677. (let ((col (current-column))
  7678. (dline (org-table-current-dline)))
  7679. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  7680. (if (not (org-at-table-p)) (beginning-of-line 0))
  7681. (move-to-column col)
  7682. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  7683. dline -1 dline)))
  7684. (defun org-table-sort-lines (with-case &optional sorting-type)
  7685. "Sort table lines according to the column at point.
  7686. The position of point indicates the column to be used for
  7687. sorting, and the range of lines is the range between the nearest
  7688. horizontal separator lines, or the entire table of no such lines
  7689. exist. If point is before the first column, you will be prompted
  7690. for the sorting column. If there is an active region, the mark
  7691. specifies the first line and the sorting column, while point
  7692. should be in the last line to be included into the sorting.
  7693. The command then prompts for the sorting type which can be
  7694. alphabetically, numerically, or by time (as given in a time stamp
  7695. in the field). Sorting in reverse order is also possible.
  7696. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  7697. If SORTING-TYPE is specified when this function is called from a Lisp
  7698. program, no prompting will take place. SORTING-TYPE must be a character,
  7699. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  7700. should be done in reverse order."
  7701. (interactive "P")
  7702. (let* ((thisline (org-current-line))
  7703. (thiscol (org-table-current-column))
  7704. beg end bcol ecol tend tbeg column lns pos)
  7705. (when (equal thiscol 0)
  7706. (if (interactive-p)
  7707. (setq thiscol
  7708. (string-to-number
  7709. (read-string "Use column N for sorting: ")))
  7710. (setq thiscol 1))
  7711. (org-table-goto-column thiscol))
  7712. (org-table-check-inside-data-field)
  7713. (if (org-region-active-p)
  7714. (progn
  7715. (setq beg (region-beginning) end (region-end))
  7716. (goto-char beg)
  7717. (setq column (org-table-current-column)
  7718. beg (point-at-bol))
  7719. (goto-char end)
  7720. (setq end (point-at-bol 2)))
  7721. (setq column (org-table-current-column)
  7722. pos (point)
  7723. tbeg (org-table-begin)
  7724. tend (org-table-end))
  7725. (if (re-search-backward org-table-hline-regexp tbeg t)
  7726. (setq beg (point-at-bol 2))
  7727. (goto-char tbeg)
  7728. (setq beg (point-at-bol 1)))
  7729. (goto-char pos)
  7730. (if (re-search-forward org-table-hline-regexp tend t)
  7731. (setq end (point-at-bol 1))
  7732. (goto-char tend)
  7733. (setq end (point-at-bol))))
  7734. (setq beg (move-marker (make-marker) beg)
  7735. end (move-marker (make-marker) end))
  7736. (untabify beg end)
  7737. (goto-char beg)
  7738. (org-table-goto-column column)
  7739. (skip-chars-backward "^|")
  7740. (setq bcol (current-column))
  7741. (org-table-goto-column (1+ column))
  7742. (skip-chars-backward "^|")
  7743. (setq ecol (1- (current-column)))
  7744. (org-table-goto-column column)
  7745. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  7746. (org-split-string (buffer-substring beg end) "\n")))
  7747. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  7748. (delete-region beg end)
  7749. (move-marker beg nil)
  7750. (move-marker end nil)
  7751. (insert (mapconcat 'cdr lns "\n") "\n")
  7752. (goto-line thisline)
  7753. (org-table-goto-column thiscol)
  7754. (message "%d lines sorted, based on column %d" (length lns) column)))
  7755. (defun org-table-cut-region (beg end)
  7756. "Copy region in table to the clipboard and blank all relevant fields."
  7757. (interactive "r")
  7758. (org-table-copy-region beg end 'cut))
  7759. (defun org-table-copy-region (beg end &optional cut)
  7760. "Copy rectangular region in table to clipboard.
  7761. A special clipboard is used which can only be accessed
  7762. with `org-table-paste-rectangle'."
  7763. (interactive "rP")
  7764. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  7765. region cols
  7766. (rpl (if cut " " nil)))
  7767. (goto-char beg)
  7768. (org-table-check-inside-data-field)
  7769. (setq l01 (org-current-line)
  7770. c01 (org-table-current-column))
  7771. (goto-char end)
  7772. (org-table-check-inside-data-field)
  7773. (setq l02 (org-current-line)
  7774. c02 (org-table-current-column))
  7775. (setq l1 (min l01 l02) l2 (max l01 l02)
  7776. c1 (min c01 c02) c2 (max c01 c02))
  7777. (catch 'exit
  7778. (while t
  7779. (catch 'nextline
  7780. (if (> l1 l2) (throw 'exit t))
  7781. (goto-line l1)
  7782. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7783. (setq cols nil ic1 c1 ic2 c2)
  7784. (while (< ic1 (1+ ic2))
  7785. (push (org-table-get-field ic1 rpl) cols)
  7786. (setq ic1 (1+ ic1)))
  7787. (push (nreverse cols) region)
  7788. (setq l1 (1+ l1)))))
  7789. (setq org-table-clip (nreverse region))
  7790. (if cut (org-table-align))
  7791. org-table-clip))
  7792. (defun org-table-paste-rectangle ()
  7793. "Paste a rectangular region into a table.
  7794. The upper right corner ends up in the current field. All involved fields
  7795. will be overwritten. If the rectangle does not fit into the present table,
  7796. the table is enlarged as needed. The process ignores horizontal separator
  7797. lines."
  7798. (interactive)
  7799. (unless (and org-table-clip (listp org-table-clip))
  7800. (error "First cut/copy a region to paste!"))
  7801. (org-table-check-inside-data-field)
  7802. (let* ((clip org-table-clip)
  7803. (line (org-current-line))
  7804. (col (org-table-current-column))
  7805. (org-enable-table-editor t)
  7806. (org-table-automatic-realign nil)
  7807. c cols field)
  7808. (while (setq cols (pop clip))
  7809. (while (org-at-table-hline-p) (beginning-of-line 2))
  7810. (if (not (org-at-table-p))
  7811. (progn (end-of-line 0) (org-table-next-field)))
  7812. (setq c col)
  7813. (while (setq field (pop cols))
  7814. (org-table-goto-column c nil 'force)
  7815. (org-table-get-field nil field)
  7816. (setq c (1+ c)))
  7817. (beginning-of-line 2))
  7818. (goto-line line)
  7819. (org-table-goto-column col)
  7820. (org-table-align)))
  7821. (defun org-table-convert ()
  7822. "Convert from `org-mode' table to table.el and back.
  7823. Obviously, this only works within limits. When an Org-mode table is
  7824. converted to table.el, all horizontal separator lines get lost, because
  7825. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7826. A table.el table can be converted to an Org-mode table only if it does not
  7827. do row or column spanning. Multiline cells will become multiple cells.
  7828. Beware, Org-mode does not test if the table can be successfully converted - it
  7829. blindly applies a recipe that works for simple tables."
  7830. (interactive)
  7831. (require 'table)
  7832. (if (org-at-table.el-p)
  7833. ;; convert to Org-mode table
  7834. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7835. (end (move-marker (make-marker) (org-table-end t))))
  7836. (table-unrecognize-region beg end)
  7837. (goto-char beg)
  7838. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7839. (replace-match ""))
  7840. (goto-char beg))
  7841. (if (org-at-table-p)
  7842. ;; convert to table.el table
  7843. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7844. (end (move-marker (make-marker) (org-table-end))))
  7845. ;; first, get rid of all horizontal lines
  7846. (goto-char beg)
  7847. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7848. (replace-match ""))
  7849. ;; insert a hline before first
  7850. (goto-char beg)
  7851. (org-table-insert-hline 'above)
  7852. (beginning-of-line -1)
  7853. ;; insert a hline after each line
  7854. (while (progn (beginning-of-line 3) (< (point) end))
  7855. (org-table-insert-hline))
  7856. (goto-char beg)
  7857. (setq end (move-marker end (org-table-end)))
  7858. ;; replace "+" at beginning and ending of hlines
  7859. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  7860. (replace-match "\\1+-"))
  7861. (goto-char beg)
  7862. (while (re-search-forward "-|[ \t]*$" end t)
  7863. (replace-match "-+"))
  7864. (goto-char beg)))))
  7865. (defun org-table-wrap-region (arg)
  7866. "Wrap several fields in a column like a paragraph.
  7867. This is useful if you'd like to spread the contents of a field over several
  7868. lines, in order to keep the table compact.
  7869. If there is an active region, and both point and mark are in the same column,
  7870. the text in the column is wrapped to minimum width for the given number of
  7871. lines. Generally, this makes the table more compact. A prefix ARG may be
  7872. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  7873. formats the selected text to two lines. If the region was longer than two
  7874. lines, the remaining lines remain empty. A negative prefix argument reduces
  7875. the current number of lines by that amount. The wrapped text is pasted back
  7876. into the table. If you formatted it to more lines than it was before, fields
  7877. further down in the table get overwritten - so you might need to make space in
  7878. the table first.
  7879. If there is no region, the current field is split at the cursor position and
  7880. the text fragment to the right of the cursor is prepended to the field one
  7881. line down.
  7882. If there is no region, but you specify a prefix ARG, the current field gets
  7883. blank, and the content is appended to the field above."
  7884. (interactive "P")
  7885. (org-table-check-inside-data-field)
  7886. (if (org-region-active-p)
  7887. ;; There is a region: fill as a paragraph
  7888. (let* ((beg (region-beginning))
  7889. (cline (save-excursion (goto-char beg) (org-current-line)))
  7890. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  7891. nlines)
  7892. (org-table-cut-region (region-beginning) (region-end))
  7893. (if (> (length (car org-table-clip)) 1)
  7894. (error "Region must be limited to single column"))
  7895. (setq nlines (if arg
  7896. (if (< arg 1)
  7897. (+ (length org-table-clip) arg)
  7898. arg)
  7899. (length org-table-clip)))
  7900. (setq org-table-clip
  7901. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  7902. nil nlines)))
  7903. (goto-line cline)
  7904. (org-table-goto-column ccol)
  7905. (org-table-paste-rectangle))
  7906. ;; No region, split the current field at point
  7907. (if arg
  7908. ;; combine with field above
  7909. (let ((s (org-table-blank-field))
  7910. (col (org-table-current-column)))
  7911. (beginning-of-line 0)
  7912. (while (org-at-table-hline-p) (beginning-of-line 0))
  7913. (org-table-goto-column col)
  7914. (skip-chars-forward "^|")
  7915. (skip-chars-backward " ")
  7916. (insert " " (org-trim s))
  7917. (org-table-align))
  7918. ;; split field
  7919. (when (looking-at "\\([^|]+\\)+|")
  7920. (let ((s (match-string 1)))
  7921. (replace-match " |")
  7922. (goto-char (match-beginning 0))
  7923. (org-table-next-row)
  7924. (insert (org-trim s) " ")
  7925. (org-table-align))))))
  7926. (defvar org-field-marker nil)
  7927. (defun org-table-edit-field (arg)
  7928. "Edit table field in a different window.
  7929. This is mainly useful for fields that contain hidden parts.
  7930. When called with a \\[universal-argument] prefix, just make the full field visible so that
  7931. it can be edited in place."
  7932. (interactive "P")
  7933. (if arg
  7934. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  7935. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  7936. (remove-text-properties b e '(org-cwidth t invisible t
  7937. display t intangible t))
  7938. (if (and (boundp 'font-lock-mode) font-lock-mode)
  7939. (font-lock-fontify-block)))
  7940. (let ((pos (move-marker (make-marker) (point)))
  7941. (field (org-table-get-field))
  7942. (cw (current-window-configuration))
  7943. p)
  7944. (org-switch-to-buffer-other-window "*Org tmp*")
  7945. (erase-buffer)
  7946. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  7947. (let ((org-inhibit-startup t)) (org-mode))
  7948. (goto-char (setq p (point-max)))
  7949. (insert (org-trim field))
  7950. (remove-text-properties p (point-max)
  7951. '(invisible t org-cwidth t display t
  7952. intangible t))
  7953. (goto-char p)
  7954. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  7955. (org-set-local 'org-window-configuration cw)
  7956. (org-set-local 'org-field-marker pos)
  7957. (message "Edit and finish with C-c C-c"))))
  7958. (defun org-table-finish-edit-field ()
  7959. "Finish editing a table data field.
  7960. Remove all newline characters, insert the result into the table, realign
  7961. the table and kill the editing buffer."
  7962. (let ((pos org-field-marker)
  7963. (cw org-window-configuration)
  7964. (cb (current-buffer))
  7965. text)
  7966. (goto-char (point-min))
  7967. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  7968. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  7969. (replace-match " "))
  7970. (setq text (org-trim (buffer-string)))
  7971. (set-window-configuration cw)
  7972. (kill-buffer cb)
  7973. (select-window (get-buffer-window (marker-buffer pos)))
  7974. (goto-char pos)
  7975. (move-marker pos nil)
  7976. (org-table-check-inside-data-field)
  7977. (org-table-get-field nil text)
  7978. (org-table-align)
  7979. (message "New field value inserted")))
  7980. (defun org-trim (s)
  7981. "Remove whitespace at beginning and end of string."
  7982. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  7983. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  7984. s)
  7985. (defun org-wrap (string &optional width lines)
  7986. "Wrap string to either a number of lines, or a width in characters.
  7987. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  7988. that costs. If there is a word longer than WIDTH, the text is actually
  7989. wrapped to the length of that word.
  7990. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  7991. many lines, whatever width that takes.
  7992. The return value is a list of lines, without newlines at the end."
  7993. (let* ((words (org-split-string string "[ \t\n]+"))
  7994. (maxword (apply 'max (mapcar 'org-string-width words)))
  7995. w ll)
  7996. (cond (width
  7997. (org-do-wrap words (max maxword width)))
  7998. (lines
  7999. (setq w maxword)
  8000. (setq ll (org-do-wrap words maxword))
  8001. (if (<= (length ll) lines)
  8002. ll
  8003. (setq ll words)
  8004. (while (> (length ll) lines)
  8005. (setq w (1+ w))
  8006. (setq ll (org-do-wrap words w)))
  8007. ll))
  8008. (t (error "Cannot wrap this")))))
  8009. (defun org-do-wrap (words width)
  8010. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8011. (let (lines line)
  8012. (while words
  8013. (setq line (pop words))
  8014. (while (and words (< (+ (length line) (length (car words))) width))
  8015. (setq line (concat line " " (pop words))))
  8016. (setq lines (push line lines)))
  8017. (nreverse lines)))
  8018. (defun org-split-string (string &optional separators)
  8019. "Splits STRING into substrings at SEPARATORS.
  8020. No empty strings are returned if there are matches at the beginning
  8021. and end of string."
  8022. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8023. (start 0)
  8024. notfirst
  8025. (list nil))
  8026. (while (and (string-match rexp string
  8027. (if (and notfirst
  8028. (= start (match-beginning 0))
  8029. (< start (length string)))
  8030. (1+ start) start))
  8031. (< (match-beginning 0) (length string)))
  8032. (setq notfirst t)
  8033. (or (eq (match-beginning 0) 0)
  8034. (and (eq (match-beginning 0) (match-end 0))
  8035. (eq (match-beginning 0) start))
  8036. (setq list
  8037. (cons (substring string start (match-beginning 0))
  8038. list)))
  8039. (setq start (match-end 0)))
  8040. (or (eq start (length string))
  8041. (setq list
  8042. (cons (substring string start)
  8043. list)))
  8044. (nreverse list)))
  8045. (defun org-table-map-tables (function)
  8046. "Apply FUNCTION to the start of all tables in the buffer."
  8047. (save-excursion
  8048. (save-restriction
  8049. (widen)
  8050. (goto-char (point-min))
  8051. (while (re-search-forward org-table-any-line-regexp nil t)
  8052. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8053. (beginning-of-line 1)
  8054. (if (looking-at org-table-line-regexp)
  8055. (save-excursion (funcall function)))
  8056. (re-search-forward org-table-any-border-regexp nil 1))))
  8057. (message "Mapping tables: done"))
  8058. (defvar org-timecnt) ; dynamically scoped parameter
  8059. (defun org-table-sum (&optional beg end nlast)
  8060. "Sum numbers in region of current table column.
  8061. The result will be displayed in the echo area, and will be available
  8062. as kill to be inserted with \\[yank].
  8063. If there is an active region, it is interpreted as a rectangle and all
  8064. numbers in that rectangle will be summed. If there is no active
  8065. region and point is located in a table column, sum all numbers in that
  8066. column.
  8067. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8068. numbers are assumed to be times as well (in decimal hours) and the
  8069. numbers are added as such.
  8070. If NLAST is a number, only the NLAST fields will actually be summed."
  8071. (interactive)
  8072. (save-excursion
  8073. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8074. (cond
  8075. ((and beg end)) ; beg and end given explicitly
  8076. ((org-region-active-p)
  8077. (setq beg (region-beginning) end (region-end)))
  8078. (t
  8079. (setq col (org-table-current-column))
  8080. (goto-char (org-table-begin))
  8081. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8082. (error "No table data"))
  8083. (org-table-goto-column col)
  8084. (setq beg (point))
  8085. (goto-char (org-table-end))
  8086. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8087. (error "No table data"))
  8088. (org-table-goto-column col)
  8089. (setq end (point))))
  8090. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8091. (items1 (cond ((not nlast) items)
  8092. ((>= nlast (length items)) items)
  8093. (t (setq items (reverse items))
  8094. (setcdr (nthcdr (1- nlast) items) nil)
  8095. (nreverse items))))
  8096. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8097. items1)))
  8098. (res (apply '+ numbers))
  8099. (sres (if (= org-timecnt 0)
  8100. (format "%g" res)
  8101. (setq diff (* 3600 res)
  8102. h (floor (/ diff 3600)) diff (mod diff 3600)
  8103. m (floor (/ diff 60)) diff (mod diff 60)
  8104. s diff)
  8105. (format "%d:%02d:%02d" h m s))))
  8106. (kill-new sres)
  8107. (if (interactive-p)
  8108. (message "%s"
  8109. (substitute-command-keys
  8110. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8111. (length numbers) sres))))
  8112. sres))))
  8113. (defun org-table-get-number-for-summing (s)
  8114. (let (n)
  8115. (if (string-match "^ *|? *" s)
  8116. (setq s (replace-match "" nil nil s)))
  8117. (if (string-match " *|? *$" s)
  8118. (setq s (replace-match "" nil nil s)))
  8119. (setq n (string-to-number s))
  8120. (cond
  8121. ((and (string-match "0" s)
  8122. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8123. ((string-match "\\`[ \t]+\\'" s) nil)
  8124. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8125. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8126. (m (string-to-number (or (match-string 2 s) "0")))
  8127. (s (string-to-number (or (match-string 4 s) "0"))))
  8128. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8129. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8130. ((equal n 0) nil)
  8131. (t n))))
  8132. (defun org-table-current-field-formula (&optional key noerror)
  8133. "Return the formula active for the current field.
  8134. Assumes that specials are in place.
  8135. If KEY is given, return the key to this formula.
  8136. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8137. (let* ((name (car (rassoc (list (org-current-line)
  8138. (org-table-current-column))
  8139. org-table-named-field-locations)))
  8140. (col (org-table-current-column))
  8141. (scol (int-to-string col))
  8142. (ref (format "@%d$%d" (org-table-current-dline) col))
  8143. (stored-list (org-table-get-stored-formulas noerror))
  8144. (ass (or (assoc name stored-list)
  8145. (assoc ref stored-list)
  8146. (assoc scol stored-list))))
  8147. (if key
  8148. (car ass)
  8149. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8150. (cdr ass))))))
  8151. (defun org-table-get-formula (&optional equation named)
  8152. "Read a formula from the minibuffer, offer stored formula as default.
  8153. When NAMED is non-nil, look for a named equation."
  8154. (let* ((stored-list (org-table-get-stored-formulas))
  8155. (name (car (rassoc (list (org-current-line)
  8156. (org-table-current-column))
  8157. org-table-named-field-locations)))
  8158. (ref (format "@%d$%d" (org-table-current-dline)
  8159. (org-table-current-column)))
  8160. (refass (assoc ref stored-list))
  8161. (scol (if named
  8162. (if name name ref)
  8163. (int-to-string (org-table-current-column))))
  8164. (dummy (and (or name refass) (not named)
  8165. (not (y-or-n-p "Replace field formula with column formula? " ))
  8166. (error "Abort")))
  8167. (name (or name ref))
  8168. (org-table-may-need-update nil)
  8169. (stored (cdr (assoc scol stored-list)))
  8170. (eq (cond
  8171. ((and stored equation (string-match "^ *=? *$" equation))
  8172. stored)
  8173. ((stringp equation)
  8174. equation)
  8175. (t (org-table-formula-from-user
  8176. (read-string
  8177. (org-table-formula-to-user
  8178. (format "%s formula %s%s="
  8179. (if named "Field" "Column")
  8180. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8181. scol))
  8182. (if stored (org-table-formula-to-user stored) "")
  8183. 'org-table-formula-history
  8184. )))))
  8185. mustsave)
  8186. (when (not (string-match "\\S-" eq))
  8187. ;; remove formula
  8188. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8189. (org-table-store-formulas stored-list)
  8190. (error "Formula removed"))
  8191. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8192. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8193. (if (and name (not named))
  8194. ;; We set the column equation, delete the named one.
  8195. (setq stored-list (delq (assoc name stored-list) stored-list)
  8196. mustsave t))
  8197. (if stored
  8198. (setcdr (assoc scol stored-list) eq)
  8199. (setq stored-list (cons (cons scol eq) stored-list)))
  8200. (if (or mustsave (not (equal stored eq)))
  8201. (org-table-store-formulas stored-list))
  8202. eq))
  8203. (defun org-table-store-formulas (alist)
  8204. "Store the list of formulas below the current table."
  8205. (setq alist (sort alist 'org-table-formula-less-p))
  8206. (save-excursion
  8207. (goto-char (org-table-end))
  8208. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8209. (progn
  8210. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8211. (goto-char (match-beginning 2))
  8212. (delete-region (match-beginning 2) (match-end 0)))
  8213. (insert "#+TBLFM:"))
  8214. (insert " "
  8215. (mapconcat (lambda (x)
  8216. (concat
  8217. (if (equal (string-to-char (car x)) ?@) "" "$")
  8218. (car x) "=" (cdr x)))
  8219. alist "::")
  8220. "\n")))
  8221. (defsubst org-table-formula-make-cmp-string (a)
  8222. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8223. (concat
  8224. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8225. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8226. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8227. (defun org-table-formula-less-p (a b)
  8228. "Compare two formulas for sorting."
  8229. (let ((as (org-table-formula-make-cmp-string (car a)))
  8230. (bs (org-table-formula-make-cmp-string (car b))))
  8231. (and as bs (string< as bs))))
  8232. (defun org-table-get-stored-formulas (&optional noerror)
  8233. "Return an alist with the stored formulas directly after current table."
  8234. (interactive)
  8235. (let (scol eq eq-alist strings string seen)
  8236. (save-excursion
  8237. (goto-char (org-table-end))
  8238. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8239. (setq strings (org-split-string (match-string 2) " *:: *"))
  8240. (while (setq string (pop strings))
  8241. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8242. (setq scol (if (match-end 2)
  8243. (match-string 2 string)
  8244. (match-string 1 string))
  8245. eq (match-string 3 string)
  8246. eq-alist (cons (cons scol eq) eq-alist))
  8247. (if (member scol seen)
  8248. (if noerror
  8249. (progn
  8250. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8251. (ding)
  8252. (sit-for 2))
  8253. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8254. (push scol seen))))))
  8255. (nreverse eq-alist)))
  8256. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8257. "Modify the equations after the table structure has been edited.
  8258. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8259. For all numbers larger than LIMIT, shift them by DELTA."
  8260. (save-excursion
  8261. (goto-char (org-table-end))
  8262. (when (looking-at "#\\+TBLFM:")
  8263. (let ((re (concat key "\\([0-9]+\\)"))
  8264. (re2
  8265. (when remove
  8266. (if (equal key "$")
  8267. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8268. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8269. s n a)
  8270. (when remove
  8271. (while (re-search-forward re2 (point-at-eol) t)
  8272. (replace-match "")))
  8273. (while (re-search-forward re (point-at-eol) t)
  8274. (setq s (match-string 1) n (string-to-number s))
  8275. (cond
  8276. ((setq a (assoc s replace))
  8277. (replace-match (concat key (cdr a)) t t))
  8278. ((and limit (> n limit))
  8279. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8280. (defun org-table-get-specials ()
  8281. "Get the column names and local parameters for this table."
  8282. (save-excursion
  8283. (let ((beg (org-table-begin)) (end (org-table-end))
  8284. names name fields fields1 field cnt
  8285. c v l line col types dlines hlines)
  8286. (setq org-table-column-names nil
  8287. org-table-local-parameters nil
  8288. org-table-named-field-locations nil
  8289. org-table-current-begin-line nil
  8290. org-table-current-begin-pos nil
  8291. org-table-current-line-types nil)
  8292. (goto-char beg)
  8293. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8294. (setq names (org-split-string (match-string 1) " *| *")
  8295. cnt 1)
  8296. (while (setq name (pop names))
  8297. (setq cnt (1+ cnt))
  8298. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8299. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8300. (setq org-table-column-names (nreverse org-table-column-names))
  8301. (setq org-table-column-name-regexp
  8302. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8303. (goto-char beg)
  8304. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8305. (setq fields (org-split-string (match-string 1) " *| *"))
  8306. (while (setq field (pop fields))
  8307. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8308. (push (cons (match-string 1 field) (match-string 2 field))
  8309. org-table-local-parameters))))
  8310. (goto-char beg)
  8311. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8312. (setq c (match-string 1)
  8313. fields (org-split-string (match-string 2) " *| *"))
  8314. (save-excursion
  8315. (beginning-of-line (if (equal c "_") 2 0))
  8316. (setq line (org-current-line) col 1)
  8317. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8318. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8319. (while (and fields1 (setq field (pop fields)))
  8320. (setq v (pop fields1) col (1+ col))
  8321. (when (and (stringp field) (stringp v)
  8322. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8323. (push (cons field v) org-table-local-parameters)
  8324. (push (list field line col) org-table-named-field-locations))))
  8325. ;; Analyse the line types
  8326. (goto-char beg)
  8327. (setq org-table-current-begin-line (org-current-line)
  8328. org-table-current-begin-pos (point)
  8329. l org-table-current-begin-line)
  8330. (while (looking-at "[ \t]*|\\(-\\)?")
  8331. (push (if (match-end 1) 'hline 'dline) types)
  8332. (if (match-end 1) (push l hlines) (push l dlines))
  8333. (beginning-of-line 2)
  8334. (setq l (1+ l)))
  8335. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8336. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8337. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8338. (defun org-table-maybe-eval-formula ()
  8339. "Check if the current field starts with \"=\" or \":=\".
  8340. If yes, store the formula and apply it."
  8341. ;; We already know we are in a table. Get field will only return a formula
  8342. ;; when appropriate. It might return a separator line, but no problem.
  8343. (when org-table-formula-evaluate-inline
  8344. (let* ((field (org-trim (or (org-table-get-field) "")))
  8345. named eq)
  8346. (when (string-match "^:?=\\(.*\\)" field)
  8347. (setq named (equal (string-to-char field) ?:)
  8348. eq (match-string 1 field))
  8349. (if (or (fboundp 'calc-eval)
  8350. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8351. (org-table-eval-formula (if named '(4) nil)
  8352. (org-table-formula-from-user eq))
  8353. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8354. (defvar org-recalc-commands nil
  8355. "List of commands triggering the recalculation of a line.
  8356. Will be filled automatically during use.")
  8357. (defvar org-recalc-marks
  8358. '((" " . "Unmarked: no special line, no automatic recalculation")
  8359. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8360. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8361. ("!" . "Column name definition line. Reference in formula as $name.")
  8362. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8363. ("_" . "Names for values in row below this one.")
  8364. ("^" . "Names for values in row above this one.")))
  8365. (defun org-table-rotate-recalc-marks (&optional newchar)
  8366. "Rotate the recalculation mark in the first column.
  8367. If in any row, the first field is not consistent with a mark,
  8368. insert a new column for the markers.
  8369. When there is an active region, change all the lines in the region,
  8370. after prompting for the marking character.
  8371. After each change, a message will be displayed indicating the meaning
  8372. of the new mark."
  8373. (interactive)
  8374. (unless (org-at-table-p) (error "Not at a table"))
  8375. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8376. (beg (org-table-begin))
  8377. (end (org-table-end))
  8378. (l (org-current-line))
  8379. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8380. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8381. (have-col
  8382. (save-excursion
  8383. (goto-char beg)
  8384. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8385. (col (org-table-current-column))
  8386. (forcenew (car (assoc newchar org-recalc-marks)))
  8387. epos new)
  8388. (when l1
  8389. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8390. (setq newchar (char-to-string (read-char-exclusive))
  8391. forcenew (car (assoc newchar org-recalc-marks))))
  8392. (if (and newchar (not forcenew))
  8393. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8394. newchar))
  8395. (if l1 (goto-line l1))
  8396. (save-excursion
  8397. (beginning-of-line 1)
  8398. (unless (looking-at org-table-dataline-regexp)
  8399. (error "Not at a table data line")))
  8400. (unless have-col
  8401. (org-table-goto-column 1)
  8402. (org-table-insert-column)
  8403. (org-table-goto-column (1+ col)))
  8404. (setq epos (point-at-eol))
  8405. (save-excursion
  8406. (beginning-of-line 1)
  8407. (org-table-get-field
  8408. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8409. (concat " "
  8410. (setq new (or forcenew
  8411. (cadr (member (match-string 1) marks))))
  8412. " ")
  8413. " # ")))
  8414. (if (and l1 l2)
  8415. (progn
  8416. (goto-line l1)
  8417. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8418. (and (looking-at org-table-dataline-regexp)
  8419. (org-table-get-field 1 (concat " " new " "))))
  8420. (goto-line l1)))
  8421. (if (not (= epos (point-at-eol))) (org-table-align))
  8422. (goto-line l)
  8423. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  8424. (defun org-table-maybe-recalculate-line ()
  8425. "Recompute the current line if marked for it, and if we haven't just done it."
  8426. (interactive)
  8427. (and org-table-allow-automatic-line-recalculation
  8428. (not (and (memq last-command org-recalc-commands)
  8429. (equal org-last-recalc-line (org-current-line))))
  8430. (save-excursion (beginning-of-line 1)
  8431. (looking-at org-table-auto-recalculate-regexp))
  8432. (org-table-recalculate) t))
  8433. (defvar org-table-formula-debug nil
  8434. "Non-nil means, debug table formulas.
  8435. When nil, simply write \"#ERROR\" in corrupted fields.")
  8436. (make-variable-buffer-local 'org-table-formula-debug)
  8437. (defvar modes)
  8438. (defsubst org-set-calc-mode (var &optional value)
  8439. (if (stringp var)
  8440. (setq var (assoc var '(("D" calc-angle-mode deg)
  8441. ("R" calc-angle-mode rad)
  8442. ("F" calc-prefer-frac t)
  8443. ("S" calc-symbolic-mode t)))
  8444. value (nth 2 var) var (nth 1 var)))
  8445. (if (memq var modes)
  8446. (setcar (cdr (memq var modes)) value)
  8447. (cons var (cons value modes)))
  8448. modes)
  8449. (defun org-table-eval-formula (&optional arg equation
  8450. suppress-align suppress-const
  8451. suppress-store suppress-analysis)
  8452. "Replace the table field value at the cursor by the result of a calculation.
  8453. This function makes use of Dave Gillespie's Calc package, in my view the
  8454. most exciting program ever written for GNU Emacs. So you need to have Calc
  8455. installed in order to use this function.
  8456. In a table, this command replaces the value in the current field with the
  8457. result of a formula. It also installs the formula as the \"current\" column
  8458. formula, by storing it in a special line below the table. When called
  8459. with a `C-u' prefix, the current field must ba a named field, and the
  8460. formula is installed as valid in only this specific field.
  8461. When called with two `C-u' prefixes, insert the active equation
  8462. for the field back into the current field, so that it can be
  8463. edited there. This is useful in order to use \\[org-table-show-reference]
  8464. to check the referenced fields.
  8465. When called, the command first prompts for a formula, which is read in
  8466. the minibuffer. Previously entered formulas are available through the
  8467. history list, and the last used formula is offered as a default.
  8468. These stored formulas are adapted correctly when moving, inserting, or
  8469. deleting columns with the corresponding commands.
  8470. The formula can be any algebraic expression understood by the Calc package.
  8471. For details, see the Org-mode manual.
  8472. This function can also be called from Lisp programs and offers
  8473. additional arguments: EQUATION can be the formula to apply. If this
  8474. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  8475. used to speed-up recursive calls by by-passing unnecessary aligns.
  8476. SUPPRESS-CONST suppresses the interpretation of constants in the
  8477. formula, assuming that this has been done already outside the function.
  8478. SUPPRESS-STORE means the formula should not be stored, either because
  8479. it is already stored, or because it is a modified equation that should
  8480. not overwrite the stored one."
  8481. (interactive "P")
  8482. (org-table-check-inside-data-field)
  8483. (or suppress-analysis (org-table-get-specials))
  8484. (if (equal arg '(16))
  8485. (let ((eq (org-table-current-field-formula)))
  8486. (or eq (error "No equation active for current field"))
  8487. (org-table-get-field nil eq)
  8488. (org-table-align)
  8489. (setq org-table-may-need-update t))
  8490. (let* (fields
  8491. (ndown (if (integerp arg) arg 1))
  8492. (org-table-automatic-realign nil)
  8493. (case-fold-search nil)
  8494. (down (> ndown 1))
  8495. (formula (if (and equation suppress-store)
  8496. equation
  8497. (org-table-get-formula equation (equal arg '(4)))))
  8498. (n0 (org-table-current-column))
  8499. (modes (copy-sequence org-calc-default-modes))
  8500. (numbers nil) ; was a variable, now fixed default
  8501. (keep-empty nil)
  8502. n form form0 bw fmt x ev orig c lispp literal)
  8503. ;; Parse the format string. Since we have a lot of modes, this is
  8504. ;; a lot of work. However, I think calc still uses most of the time.
  8505. (if (string-match ";" formula)
  8506. (let ((tmp (org-split-string formula ";")))
  8507. (setq formula (car tmp)
  8508. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  8509. (nth 1 tmp)))
  8510. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  8511. (setq c (string-to-char (match-string 1 fmt))
  8512. n (string-to-number (match-string 2 fmt)))
  8513. (if (= c ?p)
  8514. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  8515. (setq modes (org-set-calc-mode
  8516. 'calc-float-format
  8517. (list (cdr (assoc c '((?n . float) (?f . fix)
  8518. (?s . sci) (?e . eng))))
  8519. n))))
  8520. (setq fmt (replace-match "" t t fmt)))
  8521. (if (string-match "[NT]" fmt)
  8522. (setq numbers (equal (match-string 0 fmt) "N")
  8523. fmt (replace-match "" t t fmt)))
  8524. (if (string-match "L" fmt)
  8525. (setq literal t
  8526. fmt (replace-match "" t t fmt)))
  8527. (if (string-match "E" fmt)
  8528. (setq keep-empty t
  8529. fmt (replace-match "" t t fmt)))
  8530. (while (string-match "[DRFS]" fmt)
  8531. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  8532. (setq fmt (replace-match "" t t fmt)))
  8533. (unless (string-match "\\S-" fmt)
  8534. (setq fmt nil))))
  8535. (if (and (not suppress-const) org-table-formula-use-constants)
  8536. (setq formula (org-table-formula-substitute-names formula)))
  8537. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  8538. (while (> ndown 0)
  8539. (setq fields (org-split-string
  8540. (org-no-properties
  8541. (buffer-substring (point-at-bol) (point-at-eol)))
  8542. " *| *"))
  8543. (if (eq numbers t)
  8544. (setq fields (mapcar
  8545. (lambda (x) (number-to-string (string-to-number x)))
  8546. fields)))
  8547. (setq ndown (1- ndown))
  8548. (setq form (copy-sequence formula)
  8549. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  8550. (if (and lispp literal) (setq lispp 'literal))
  8551. ;; Check for old vertical references
  8552. (setq form (org-rewrite-old-row-references form))
  8553. ;; Insert complex ranges
  8554. (while (string-match org-table-range-regexp form)
  8555. (setq form
  8556. (replace-match
  8557. (save-match-data
  8558. (org-table-make-reference
  8559. (org-table-get-range (match-string 0 form) nil n0)
  8560. keep-empty numbers lispp))
  8561. t t form)))
  8562. ;; Insert simple ranges
  8563. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  8564. (setq form
  8565. (replace-match
  8566. (save-match-data
  8567. (org-table-make-reference
  8568. (org-sublist
  8569. fields (string-to-number (match-string 1 form))
  8570. (string-to-number (match-string 2 form)))
  8571. keep-empty numbers lispp))
  8572. t t form)))
  8573. (setq form0 form)
  8574. ;; Insert the references to fields in same row
  8575. (while (string-match "\\$\\([0-9]+\\)" form)
  8576. (setq n (string-to-number (match-string 1 form))
  8577. x (nth (1- (if (= n 0) n0 n)) fields))
  8578. (unless x (error "Invalid field specifier \"%s\""
  8579. (match-string 0 form)))
  8580. (setq form (replace-match
  8581. (save-match-data
  8582. (org-table-make-reference x nil numbers lispp))
  8583. t t form)))
  8584. (if lispp
  8585. (setq ev (condition-case nil
  8586. (eval (eval (read form)))
  8587. (error "#ERROR"))
  8588. ev (if (numberp ev) (number-to-string ev) ev))
  8589. (or (fboundp 'calc-eval)
  8590. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  8591. (setq ev (calc-eval (cons form modes)
  8592. (if numbers 'num))))
  8593. (when org-table-formula-debug
  8594. (with-output-to-temp-buffer "*Substitution History*"
  8595. (princ (format "Substitution history of formula
  8596. Orig: %s
  8597. $xyz-> %s
  8598. @r$c-> %s
  8599. $1-> %s\n" orig formula form0 form))
  8600. (if (listp ev)
  8601. (princ (format " %s^\nError: %s"
  8602. (make-string (car ev) ?\-) (nth 1 ev)))
  8603. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  8604. ev (or fmt "NONE")
  8605. (if fmt (format fmt (string-to-number ev)) ev)))))
  8606. (setq bw (get-buffer-window "*Substitution History*"))
  8607. (shrink-window-if-larger-than-buffer bw)
  8608. (unless (and (interactive-p) (not ndown))
  8609. (unless (let (inhibit-redisplay)
  8610. (y-or-n-p "Debugging Formula. Continue to next? "))
  8611. (org-table-align)
  8612. (error "Abort"))
  8613. (delete-window bw)
  8614. (message "")))
  8615. (if (listp ev) (setq fmt nil ev "#ERROR"))
  8616. (org-table-justify-field-maybe
  8617. (if fmt (format fmt (string-to-number ev)) ev))
  8618. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  8619. (call-interactively 'org-return)
  8620. (setq ndown 0)))
  8621. (and down (org-table-maybe-recalculate-line))
  8622. (or suppress-align (and org-table-may-need-update
  8623. (org-table-align))))))
  8624. (defun org-table-put-field-property (prop value)
  8625. (save-excursion
  8626. (put-text-property (progn (skip-chars-backward "^|") (point))
  8627. (progn (skip-chars-forward "^|") (point))
  8628. prop value)))
  8629. (defun org-table-get-range (desc &optional tbeg col highlight)
  8630. "Get a calc vector from a column, accorting to descriptor DESC.
  8631. Optional arguments TBEG and COL can give the beginning of the table and
  8632. the current column, to avoid unnecessary parsing.
  8633. HIGHLIGHT means, just highlight the range."
  8634. (if (not (equal (string-to-char desc) ?@))
  8635. (setq desc (concat "@" desc)))
  8636. (save-excursion
  8637. (or tbeg (setq tbeg (org-table-begin)))
  8638. (or col (setq col (org-table-current-column)))
  8639. (let ((thisline (org-current-line))
  8640. beg end c1 c2 r1 r2 rangep tmp)
  8641. (unless (string-match org-table-range-regexp desc)
  8642. (error "Invalid table range specifier `%s'" desc))
  8643. (setq rangep (match-end 3)
  8644. r1 (and (match-end 1) (match-string 1 desc))
  8645. r2 (and (match-end 4) (match-string 4 desc))
  8646. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  8647. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  8648. (and c1 (setq c1 (+ (string-to-number c1)
  8649. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  8650. (and c2 (setq c2 (+ (string-to-number c2)
  8651. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  8652. (if (equal r1 "") (setq r1 nil))
  8653. (if (equal r2 "") (setq r2 nil))
  8654. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  8655. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  8656. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  8657. (if (not r1) (setq r1 thisline))
  8658. (if (not r2) (setq r2 thisline))
  8659. (if (not c1) (setq c1 col))
  8660. (if (not c2) (setq c2 col))
  8661. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  8662. ;; just one field
  8663. (progn
  8664. (goto-line r1)
  8665. (while (not (looking-at org-table-dataline-regexp))
  8666. (beginning-of-line 2))
  8667. (prog1 (org-trim (org-table-get-field c1))
  8668. (if highlight (org-table-highlight-rectangle (point) (point)))))
  8669. ;; A range, return a vector
  8670. ;; First sort the numbers to get a regular ractangle
  8671. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  8672. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  8673. (goto-line r1)
  8674. (while (not (looking-at org-table-dataline-regexp))
  8675. (beginning-of-line 2))
  8676. (org-table-goto-column c1)
  8677. (setq beg (point))
  8678. (goto-line r2)
  8679. (while (not (looking-at org-table-dataline-regexp))
  8680. (beginning-of-line 0))
  8681. (org-table-goto-column c2)
  8682. (setq end (point))
  8683. (if highlight
  8684. (org-table-highlight-rectangle
  8685. beg (progn (skip-chars-forward "^|\n") (point))))
  8686. ;; return string representation of calc vector
  8687. (mapcar 'org-trim
  8688. (apply 'append (org-table-copy-region beg end)))))))
  8689. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  8690. "Analyze descriptor DESC and retrieve the corresponding line number.
  8691. The cursor is currently in line CLINE, the table begins in line BLINE,
  8692. and TABLE is a vector with line types."
  8693. (if (string-match "^[0-9]+$" desc)
  8694. (aref org-table-dlines (string-to-number desc))
  8695. (setq cline (or cline (org-current-line))
  8696. bline (or bline org-table-current-begin-line)
  8697. table (or table org-table-current-line-types))
  8698. (if (or
  8699. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  8700. ;; 1 2 3 4 5 6
  8701. (and (not (match-end 3)) (not (match-end 6)))
  8702. (and (match-end 3) (match-end 6) (not (match-end 5))))
  8703. (error "invalid row descriptor `%s'" desc))
  8704. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  8705. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  8706. (odir (and (match-end 5) (match-string 5 desc)))
  8707. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  8708. (i (- cline bline))
  8709. (rel (and (match-end 6)
  8710. (or (and (match-end 1) (not (match-end 3)))
  8711. (match-end 5)))))
  8712. (if (and hn (not hdir))
  8713. (progn
  8714. (setq i 0 hdir "+")
  8715. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  8716. (if (and (not hn) on (not odir))
  8717. (error "should never happen");;(aref org-table-dlines on)
  8718. (if (and hn (> hn 0))
  8719. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  8720. (if on
  8721. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  8722. (+ bline i)))))
  8723. (defun org-find-row-type (table i type backwards relative n)
  8724. (let ((l (length table)))
  8725. (while (> n 0)
  8726. (while (and (setq i (+ i (if backwards -1 1)))
  8727. (>= i 0) (< i l)
  8728. (not (eq (aref table i) type))
  8729. (if (and relative (eq (aref table i) 'hline))
  8730. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  8731. t)))
  8732. (setq n (1- n)))
  8733. (if (or (< i 0) (>= i l))
  8734. (error "Row descriptior leads outside table")
  8735. i)))
  8736. (defun org-rewrite-old-row-references (s)
  8737. (if (string-match "&[-+0-9I]" s)
  8738. (error "Formula contains old &row reference, please rewrite using @-syntax")
  8739. s))
  8740. (defun org-table-make-reference (elements keep-empty numbers lispp)
  8741. "Convert list ELEMENTS to something appropriate to insert into formula.
  8742. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  8743. NUMBERS indicates that everything should be converted to numbers.
  8744. LISPP means to return something appropriate for a Lisp list."
  8745. (if (stringp elements) ; just a single val
  8746. (if lispp
  8747. (if (eq lispp 'literal)
  8748. elements
  8749. (prin1-to-string (if numbers (string-to-number elements) elements)))
  8750. (if (equal elements "") (setq elements "0"))
  8751. (if numbers (number-to-string (string-to-number elements)) elements))
  8752. (unless keep-empty
  8753. (setq elements
  8754. (delq nil
  8755. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  8756. elements))))
  8757. (setq elements (or elements '("0")))
  8758. (if lispp
  8759. (mapconcat
  8760. (lambda (x)
  8761. (if (eq lispp 'literal)
  8762. x
  8763. (prin1-to-string (if numbers (string-to-number x) x))))
  8764. elements " ")
  8765. (concat "[" (mapconcat
  8766. (lambda (x)
  8767. (if numbers (number-to-string (string-to-number x)) x))
  8768. elements
  8769. ",") "]"))))
  8770. (defun org-table-recalculate (&optional all noalign)
  8771. "Recalculate the current table line by applying all stored formulas.
  8772. With prefix arg ALL, do this for all lines in the table."
  8773. (interactive "P")
  8774. (or (memq this-command org-recalc-commands)
  8775. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8776. (unless (org-at-table-p) (error "Not at a table"))
  8777. (if (equal all '(16))
  8778. (org-table-iterate)
  8779. (org-table-get-specials)
  8780. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8781. (lambda (a b) (string< (car a) (car b)))))
  8782. (inhibit-redisplay (not debug-on-error))
  8783. (line-re org-table-dataline-regexp)
  8784. (thisline (org-current-line))
  8785. (thiscol (org-table-current-column))
  8786. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8787. ;; Insert constants in all formulas
  8788. (setq eqlist
  8789. (mapcar (lambda (x)
  8790. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8791. x)
  8792. eqlist))
  8793. ;; Split the equation list
  8794. (while (setq eq (pop eqlist))
  8795. (if (<= (string-to-char (car eq)) ?9)
  8796. (push eq eqlnum)
  8797. (push eq eqlname)))
  8798. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8799. (if all
  8800. (progn
  8801. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8802. (goto-char (setq beg (org-table-begin)))
  8803. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8804. ;; This is a table with marked lines, compute selected lines
  8805. (setq line-re org-table-recalculate-regexp)
  8806. ;; Move forward to the first non-header line
  8807. (if (and (re-search-forward org-table-dataline-regexp end t)
  8808. (re-search-forward org-table-hline-regexp end t)
  8809. (re-search-forward org-table-dataline-regexp end t))
  8810. (setq beg (match-beginning 0))
  8811. nil))) ;; just leave beg where it is
  8812. (setq beg (point-at-bol)
  8813. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8814. (goto-char beg)
  8815. (and all (message "Re-applying formulas to full table..."))
  8816. ;; First find the named fields, and mark them untouchanble
  8817. (remove-text-properties beg end '(org-untouchable t))
  8818. (while (setq eq (pop eqlname))
  8819. (setq name (car eq)
  8820. a (assoc name org-table-named-field-locations))
  8821. (and (not a)
  8822. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8823. (setq a (list name
  8824. (aref org-table-dlines
  8825. (string-to-number (match-string 1 name)))
  8826. (string-to-number (match-string 2 name)))))
  8827. (when (and a (or all (equal (nth 1 a) thisline)))
  8828. (message "Re-applying formula to field: %s" name)
  8829. (goto-line (nth 1 a))
  8830. (org-table-goto-column (nth 2 a))
  8831. (push (append a (list (cdr eq))) eqlname1)
  8832. (org-table-put-field-property :org-untouchable t)))
  8833. ;; Now evauluate the column formulas, but skip fields covered by
  8834. ;; field formulas
  8835. (goto-char beg)
  8836. (while (re-search-forward line-re end t)
  8837. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8838. ;; Unprotected line, recalculate
  8839. (and all (message "Re-applying formulas to full table...(line %d)"
  8840. (setq cnt (1+ cnt))))
  8841. (setq org-last-recalc-line (org-current-line))
  8842. (setq eql eqlnum)
  8843. (while (setq entry (pop eql))
  8844. (goto-line org-last-recalc-line)
  8845. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8846. (unless (get-text-property (point) :org-untouchable)
  8847. (org-table-eval-formula nil (cdr entry)
  8848. 'noalign 'nocst 'nostore 'noanalysis)))))
  8849. ;; Now evaluate the field formulas
  8850. (while (setq eq (pop eqlname1))
  8851. (message "Re-applying formula to field: %s" (car eq))
  8852. (goto-line (nth 1 eq))
  8853. (org-table-goto-column (nth 2 eq))
  8854. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  8855. 'nostore 'noanalysis))
  8856. (goto-line thisline)
  8857. (org-table-goto-column thiscol)
  8858. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  8859. (or noalign (and org-table-may-need-update (org-table-align))
  8860. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  8861. ;; back to initial position
  8862. (message "Re-applying formulas...done")
  8863. (goto-line thisline)
  8864. (org-table-goto-column thiscol)
  8865. (or noalign (and org-table-may-need-update (org-table-align))
  8866. (and all (message "Re-applying formulas...done"))))))
  8867. (defun org-table-iterate (&optional arg)
  8868. "Recalculate the table until it does not change anymore."
  8869. (interactive "P")
  8870. (let ((imax (if arg (prefix-numeric-value arg) 10))
  8871. (i 0)
  8872. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  8873. thistbl)
  8874. (catch 'exit
  8875. (while (< i imax)
  8876. (setq i (1+ i))
  8877. (org-table-recalculate 'all)
  8878. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  8879. (if (not (string= lasttbl thistbl))
  8880. (setq lasttbl thistbl)
  8881. (if (> i 1)
  8882. (message "Convergence after %d iterations" i)
  8883. (message "Table was already stable"))
  8884. (throw 'exit t)))
  8885. (error "No convergence after %d iterations" i))))
  8886. (defun org-table-formula-substitute-names (f)
  8887. "Replace $const with values in string F."
  8888. (message "form %s" f) (sit-for 1)
  8889. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  8890. ;; First, check for column names
  8891. (while (setq start (string-match org-table-column-name-regexp f start))
  8892. (setq start (1+ start))
  8893. (setq a (assoc (match-string 1 f) org-table-column-names))
  8894. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  8895. ;; Parameters and constants
  8896. (setq start 0)
  8897. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  8898. (setq start (1+ start))
  8899. (if (setq a (save-match-data
  8900. (org-table-get-constant (match-string 1 f))))
  8901. (setq f (replace-match
  8902. (concat (if pp "(") a (if pp ")")) t t f))))
  8903. (if org-table-formula-debug
  8904. (put-text-property 0 (length f) :orig-formula f1 f))
  8905. f))
  8906. (defun org-table-get-constant (const)
  8907. "Find the value for a parameter or constant in a formula.
  8908. Parameters get priority."
  8909. (or (cdr (assoc const org-table-local-parameters))
  8910. (cdr (assoc const org-table-formula-constants-local))
  8911. (cdr (assoc const org-table-formula-constants))
  8912. (and (fboundp 'constants-get) (constants-get const))
  8913. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  8914. (org-entry-get nil (substring const 5) 'inherit))
  8915. "#UNDEFINED_NAME"))
  8916. (defvar org-table-fedit-map
  8917. (let ((map (make-sparse-keymap)))
  8918. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  8919. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  8920. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  8921. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  8922. (org-defkey map "\C-c?" 'org-table-show-reference)
  8923. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  8924. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  8925. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  8926. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  8927. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  8928. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  8929. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  8930. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  8931. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  8932. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  8933. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  8934. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  8935. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  8936. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  8937. map))
  8938. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  8939. '("Edit-Formulas"
  8940. ["Finish and Install" org-table-fedit-finish t]
  8941. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  8942. ["Abort" org-table-fedit-abort t]
  8943. "--"
  8944. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  8945. ["Complete Lisp Symbol" lisp-complete-symbol t]
  8946. "--"
  8947. "Shift Reference at Point"
  8948. ["Up" org-table-fedit-ref-up t]
  8949. ["Down" org-table-fedit-ref-down t]
  8950. ["Left" org-table-fedit-ref-left t]
  8951. ["Right" org-table-fedit-ref-right t]
  8952. "-"
  8953. "Change Test Row for Column Formulas"
  8954. ["Up" org-table-fedit-line-up t]
  8955. ["Down" org-table-fedit-line-down t]
  8956. "--"
  8957. ["Scroll Table Window" org-table-fedit-scroll t]
  8958. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  8959. ["Show Table Grid" org-table-fedit-toggle-coordinates
  8960. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  8961. org-table-overlay-coordinates)]
  8962. "--"
  8963. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  8964. :style toggle :selected org-table-buffer-is-an]))
  8965. (defvar org-pos)
  8966. (defun org-table-edit-formulas ()
  8967. "Edit the formulas of the current table in a separate buffer."
  8968. (interactive)
  8969. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  8970. (beginning-of-line 0))
  8971. (unless (org-at-table-p) (error "Not at a table"))
  8972. (org-table-get-specials)
  8973. (let ((key (org-table-current-field-formula 'key 'noerror))
  8974. (eql (sort (org-table-get-stored-formulas 'noerror)
  8975. 'org-table-formula-less-p))
  8976. (pos (move-marker (make-marker) (point)))
  8977. (startline 1)
  8978. (wc (current-window-configuration))
  8979. (titles '((column . "# Column Formulas\n")
  8980. (field . "# Field Formulas\n")
  8981. (named . "# Named Field Formulas\n")))
  8982. entry s type title)
  8983. (org-switch-to-buffer-other-window "*Edit Formulas*")
  8984. (erase-buffer)
  8985. ;; Keep global-font-lock-mode from turning on font-lock-mode
  8986. (let ((font-lock-global-modes '(not fundamental-mode)))
  8987. (fundamental-mode))
  8988. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  8989. (org-set-local 'org-pos pos)
  8990. (org-set-local 'org-window-configuration wc)
  8991. (use-local-map org-table-fedit-map)
  8992. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  8993. (easy-menu-add org-table-fedit-menu)
  8994. (setq startline (org-current-line))
  8995. (while (setq entry (pop eql))
  8996. (setq type (cond
  8997. ((equal (string-to-char (car entry)) ?@) 'field)
  8998. ((string-match "^[0-9]" (car entry)) 'column)
  8999. (t 'named)))
  9000. (when (setq title (assq type titles))
  9001. (or (bobp) (insert "\n"))
  9002. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9003. (setq titles (delq title titles)))
  9004. (if (equal key (car entry)) (setq startline (org-current-line)))
  9005. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9006. (car entry) " = " (cdr entry) "\n"))
  9007. (remove-text-properties 0 (length s) '(face nil) s)
  9008. (insert s))
  9009. (if (eq org-table-use-standard-references t)
  9010. (org-table-fedit-toggle-ref-type))
  9011. (goto-line startline)
  9012. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9013. (defun org-table-fedit-post-command ()
  9014. (when (not (memq this-command '(lisp-complete-symbol)))
  9015. (let ((win (selected-window)))
  9016. (save-excursion
  9017. (condition-case nil
  9018. (org-table-show-reference)
  9019. (error nil))
  9020. (select-window win)))))
  9021. (defun org-table-formula-to-user (s)
  9022. "Convert a formula from internal to user representation."
  9023. (if (eq org-table-use-standard-references t)
  9024. (org-table-convert-refs-to-an s)
  9025. s))
  9026. (defun org-table-formula-from-user (s)
  9027. "Convert a formula from user to internal representation."
  9028. (if org-table-use-standard-references
  9029. (org-table-convert-refs-to-rc s)
  9030. s))
  9031. (defun org-table-convert-refs-to-rc (s)
  9032. "Convert spreadsheet references from AB7 to @7$28.
  9033. Works for single references, but also for entire formulas and even the
  9034. full TBLFM line."
  9035. (let ((start 0))
  9036. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9037. (cond
  9038. ((match-end 3)
  9039. ;; format match, just advance
  9040. (setq start (match-end 0)))
  9041. ((and (> (match-beginning 0) 0)
  9042. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9043. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9044. ;; 3.e5 or something like this.
  9045. (setq start (match-end 0)))
  9046. (t
  9047. (setq start (match-beginning 0)
  9048. s (replace-match
  9049. (if (equal (match-string 2 s) "&")
  9050. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9051. (format "@%d$%d"
  9052. (string-to-number (match-string 2 s))
  9053. (org-letters-to-number (match-string 1 s))))
  9054. t t s)))))
  9055. s))
  9056. (defun org-table-convert-refs-to-an (s)
  9057. "Convert spreadsheet references from to @7$28 to AB7.
  9058. Works for single references, but also for entire formulas and even the
  9059. full TBLFM line."
  9060. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9061. (setq s (replace-match
  9062. (format "%s%d"
  9063. (org-number-to-letters
  9064. (string-to-number (match-string 2 s)))
  9065. (string-to-number (match-string 1 s)))
  9066. t t s)))
  9067. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9068. (setq s (replace-match (concat "\\1"
  9069. (org-number-to-letters
  9070. (string-to-number (match-string 2 s))) "&")
  9071. t nil s)))
  9072. s)
  9073. (defun org-letters-to-number (s)
  9074. "Convert a base 26 number represented by letters into an integer.
  9075. For example: AB -> 28."
  9076. (let ((n 0))
  9077. (setq s (upcase s))
  9078. (while (> (length s) 0)
  9079. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9080. s (substring s 1)))
  9081. n))
  9082. (defun org-number-to-letters (n)
  9083. "Convert an integer into a base 26 number represented by letters.
  9084. For example: 28 -> AB."
  9085. (let ((s ""))
  9086. (while (> n 0)
  9087. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9088. n (/ (1- n) 26)))
  9089. s))
  9090. (defun org-table-fedit-convert-buffer (function)
  9091. "Convert all references in this buffer, using FUNTION."
  9092. (let ((line (org-current-line)))
  9093. (goto-char (point-min))
  9094. (while (not (eobp))
  9095. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9096. (delete-region (point) (point-at-eol))
  9097. (or (eobp) (forward-char 1)))
  9098. (goto-line line)))
  9099. (defun org-table-fedit-toggle-ref-type ()
  9100. "Convert all references in the buffer from B3 to @3$2 and back."
  9101. (interactive)
  9102. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9103. (org-table-fedit-convert-buffer
  9104. (if org-table-buffer-is-an
  9105. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9106. (message "Reference type switched to %s"
  9107. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9108. (defun org-table-fedit-ref-up ()
  9109. "Shift the reference at point one row/hline up."
  9110. (interactive)
  9111. (org-table-fedit-shift-reference 'up))
  9112. (defun org-table-fedit-ref-down ()
  9113. "Shift the reference at point one row/hline down."
  9114. (interactive)
  9115. (org-table-fedit-shift-reference 'down))
  9116. (defun org-table-fedit-ref-left ()
  9117. "Shift the reference at point one field to the left."
  9118. (interactive)
  9119. (org-table-fedit-shift-reference 'left))
  9120. (defun org-table-fedit-ref-right ()
  9121. "Shift the reference at point one field to the right."
  9122. (interactive)
  9123. (org-table-fedit-shift-reference 'right))
  9124. (defun org-table-fedit-shift-reference (dir)
  9125. (cond
  9126. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9127. (if (memq dir '(left right))
  9128. (org-rematch-and-replace 1 (eq dir 'left))
  9129. (error "Cannot shift reference in this direction")))
  9130. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9131. ;; A B3-like reference
  9132. (if (memq dir '(up down))
  9133. (org-rematch-and-replace 2 (eq dir 'up))
  9134. (org-rematch-and-replace 1 (eq dir 'left))))
  9135. ((org-at-regexp-p
  9136. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9137. ;; An internal reference
  9138. (if (memq dir '(up down))
  9139. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9140. (org-rematch-and-replace 5 (eq dir 'left))))))
  9141. (defun org-rematch-and-replace (n &optional decr hline)
  9142. "Re-match the group N, and replace it with the shifted refrence."
  9143. (or (match-end n) (error "Cannot shift reference in this direction"))
  9144. (goto-char (match-beginning n))
  9145. (and (looking-at (regexp-quote (match-string n)))
  9146. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9147. t t)))
  9148. (defun org-shift-refpart (ref &optional decr hline)
  9149. "Shift a refrence part REF.
  9150. If DECR is set, decrease the references row/column, else increase.
  9151. If HLINE is set, this may be a hline reference, it certainly is not
  9152. a translation reference."
  9153. (save-match-data
  9154. (let* ((sign (string-match "^[-+]" ref)) n)
  9155. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9156. (cond
  9157. ((and hline (string-match "^I+" ref))
  9158. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9159. (setq n (+ n (if decr -1 1)))
  9160. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9161. (if sign
  9162. (setq sign (if (< n 0) "-" "+") n (abs n))
  9163. (setq n (max 1 n)))
  9164. (concat sign (make-string n ?I)))
  9165. ((string-match "^[0-9]+" ref)
  9166. (setq n (string-to-number (concat sign ref)))
  9167. (setq n (+ n (if decr -1 1)))
  9168. (if sign
  9169. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9170. (number-to-string (max 1 n))))
  9171. ((string-match "^[a-zA-Z]+" ref)
  9172. (org-number-to-letters
  9173. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9174. (t (error "Cannot shift reference"))))))
  9175. (defun org-table-fedit-toggle-coordinates ()
  9176. "Toggle the display of coordinates in the refrenced table."
  9177. (interactive)
  9178. (let ((pos (marker-position org-pos)))
  9179. (with-current-buffer (marker-buffer org-pos)
  9180. (save-excursion
  9181. (goto-char pos)
  9182. (org-table-toggle-coordinate-overlays)))))
  9183. (defun org-table-fedit-finish (&optional arg)
  9184. "Parse the buffer for formula definitions and install them.
  9185. With prefix ARG, apply the new formulas to the table."
  9186. (interactive "P")
  9187. (org-table-remove-rectangle-highlight)
  9188. (if org-table-use-standard-references
  9189. (progn
  9190. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9191. (setq org-table-buffer-is-an nil)))
  9192. (let ((pos org-pos) eql var form)
  9193. (goto-char (point-min))
  9194. (while (re-search-forward
  9195. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9196. nil t)
  9197. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9198. form (match-string 3))
  9199. (setq form (org-trim form))
  9200. (when (not (equal form ""))
  9201. (while (string-match "[ \t]*\n[ \t]*" form)
  9202. (setq form (replace-match " " t t form)))
  9203. (when (assoc var eql)
  9204. (error "Double formulas for %s" var))
  9205. (push (cons var form) eql)))
  9206. (setq org-pos nil)
  9207. (set-window-configuration org-window-configuration)
  9208. (select-window (get-buffer-window (marker-buffer pos)))
  9209. (goto-char pos)
  9210. (unless (org-at-table-p)
  9211. (error "Lost table position - cannot install formulae"))
  9212. (org-table-store-formulas eql)
  9213. (move-marker pos nil)
  9214. (kill-buffer "*Edit Formulas*")
  9215. (if arg
  9216. (org-table-recalculate 'all)
  9217. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9218. (defun org-table-fedit-abort ()
  9219. "Abort editing formulas, without installing the changes."
  9220. (interactive)
  9221. (org-table-remove-rectangle-highlight)
  9222. (let ((pos org-pos))
  9223. (set-window-configuration org-window-configuration)
  9224. (select-window (get-buffer-window (marker-buffer pos)))
  9225. (goto-char pos)
  9226. (move-marker pos nil)
  9227. (message "Formula editing aborted without installing changes")))
  9228. (defun org-table-fedit-lisp-indent ()
  9229. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9230. (interactive)
  9231. (let ((pos (point)) beg end ind)
  9232. (beginning-of-line 1)
  9233. (cond
  9234. ((looking-at "[ \t]")
  9235. (goto-char pos)
  9236. (call-interactively 'lisp-indent-line))
  9237. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9238. ((not (fboundp 'pp-buffer))
  9239. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9240. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9241. (goto-char (- (match-end 0) 2))
  9242. (setq beg (point))
  9243. (setq ind (make-string (current-column) ?\ ))
  9244. (condition-case nil (forward-sexp 1)
  9245. (error
  9246. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9247. (setq end (point))
  9248. (save-restriction
  9249. (narrow-to-region beg end)
  9250. (if (eq last-command this-command)
  9251. (progn
  9252. (goto-char (point-min))
  9253. (setq this-command nil)
  9254. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9255. (replace-match " ")))
  9256. (pp-buffer)
  9257. (untabify (point-min) (point-max))
  9258. (goto-char (1+ (point-min)))
  9259. (while (re-search-forward "^." nil t)
  9260. (beginning-of-line 1)
  9261. (insert ind))
  9262. (goto-char (point-max))
  9263. (backward-delete-char 1)))
  9264. (goto-char beg))
  9265. (t nil))))
  9266. (defvar org-show-positions nil)
  9267. (defun org-table-show-reference (&optional local)
  9268. "Show the location/value of the $ expression at point."
  9269. (interactive)
  9270. (org-table-remove-rectangle-highlight)
  9271. (catch 'exit
  9272. (let ((pos (if local (point) org-pos))
  9273. (face2 'highlight)
  9274. (org-inhibit-highlight-removal t)
  9275. (win (selected-window))
  9276. (org-show-positions nil)
  9277. var name e what match dest)
  9278. (if local (org-table-get-specials))
  9279. (setq what (cond
  9280. ((or (org-at-regexp-p org-table-range-regexp2)
  9281. (org-at-regexp-p org-table-translate-regexp)
  9282. (org-at-regexp-p org-table-range-regexp))
  9283. (setq match
  9284. (save-match-data
  9285. (org-table-convert-refs-to-rc (match-string 0))))
  9286. 'range)
  9287. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9288. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9289. ((not local) nil)
  9290. (t (error "No reference at point")))
  9291. match (and what (or match (match-string 0))))
  9292. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9293. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9294. 'secondary-selection))
  9295. (org-add-hook 'before-change-functions
  9296. 'org-table-remove-rectangle-highlight)
  9297. (if (eq what 'name) (setq var (substring match 1)))
  9298. (when (eq what 'range)
  9299. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9300. (setq match (org-table-formula-substitute-names match)))
  9301. (unless local
  9302. (save-excursion
  9303. (end-of-line 1)
  9304. (re-search-backward "^\\S-" nil t)
  9305. (beginning-of-line 1)
  9306. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9307. (setq dest
  9308. (save-match-data
  9309. (org-table-convert-refs-to-rc (match-string 1))))
  9310. (org-table-add-rectangle-overlay
  9311. (match-beginning 1) (match-end 1) face2))))
  9312. (if (and (markerp pos) (marker-buffer pos))
  9313. (if (get-buffer-window (marker-buffer pos))
  9314. (select-window (get-buffer-window (marker-buffer pos)))
  9315. (org-switch-to-buffer-other-window (get-buffer-window
  9316. (marker-buffer pos)))))
  9317. (goto-char pos)
  9318. (org-table-force-dataline)
  9319. (when dest
  9320. (setq name (substring dest 1))
  9321. (cond
  9322. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9323. (setq e (assoc name org-table-named-field-locations))
  9324. (goto-line (nth 1 e))
  9325. (org-table-goto-column (nth 2 e)))
  9326. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9327. (let ((l (string-to-number (match-string 1 dest)))
  9328. (c (string-to-number (match-string 2 dest))))
  9329. (goto-line (aref org-table-dlines l))
  9330. (org-table-goto-column c)))
  9331. (t (org-table-goto-column (string-to-number name))))
  9332. (move-marker pos (point))
  9333. (org-table-highlight-rectangle nil nil face2))
  9334. (cond
  9335. ((equal dest match))
  9336. ((not match))
  9337. ((eq what 'range)
  9338. (condition-case nil
  9339. (save-excursion
  9340. (org-table-get-range match nil nil 'highlight))
  9341. (error nil)))
  9342. ((setq e (assoc var org-table-named-field-locations))
  9343. (goto-line (nth 1 e))
  9344. (org-table-goto-column (nth 2 e))
  9345. (org-table-highlight-rectangle (point) (point))
  9346. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9347. ((setq e (assoc var org-table-column-names))
  9348. (org-table-goto-column (string-to-number (cdr e)))
  9349. (org-table-highlight-rectangle (point) (point))
  9350. (goto-char (org-table-begin))
  9351. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9352. (org-table-end) t)
  9353. (progn
  9354. (goto-char (match-beginning 1))
  9355. (org-table-highlight-rectangle)
  9356. (message "Named column (column %s)" (cdr e)))
  9357. (error "Column name not found")))
  9358. ((eq what 'column)
  9359. ;; column number
  9360. (org-table-goto-column (string-to-number (substring match 1)))
  9361. (org-table-highlight-rectangle (point) (point))
  9362. (message "Column %s" (substring match 1)))
  9363. ((setq e (assoc var org-table-local-parameters))
  9364. (goto-char (org-table-begin))
  9365. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9366. (progn
  9367. (goto-char (match-beginning 1))
  9368. (org-table-highlight-rectangle)
  9369. (message "Local parameter."))
  9370. (error "Parameter not found")))
  9371. (t
  9372. (cond
  9373. ((not var) (error "No reference at point"))
  9374. ((setq e (assoc var org-table-formula-constants-local))
  9375. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9376. var (cdr e)))
  9377. ((setq e (assoc var org-table-formula-constants))
  9378. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9379. var (cdr e)))
  9380. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9381. (message "Constant: $%s=%s, from `constants.el'%s."
  9382. var e (format " (%s units)" constants-unit-system)))
  9383. (t (error "Undefined name $%s" var)))))
  9384. (goto-char pos)
  9385. (when (and org-show-positions
  9386. (not (memq this-command '(org-table-fedit-scroll
  9387. org-table-fedit-scroll-down))))
  9388. (push pos org-show-positions)
  9389. (push org-table-current-begin-pos org-show-positions)
  9390. (let ((min (apply 'min org-show-positions))
  9391. (max (apply 'max org-show-positions)))
  9392. (goto-char min) (recenter 0)
  9393. (goto-char max)
  9394. (or (pos-visible-in-window-p max) (recenter -1))))
  9395. (select-window win))))
  9396. (defun org-table-force-dataline ()
  9397. "Make sure the cursor is in a dataline in a table."
  9398. (unless (save-excursion
  9399. (beginning-of-line 1)
  9400. (looking-at org-table-dataline-regexp))
  9401. (let* ((re org-table-dataline-regexp)
  9402. (p1 (save-excursion (re-search-forward re nil 'move)))
  9403. (p2 (save-excursion (re-search-backward re nil 'move))))
  9404. (cond ((and p1 p2)
  9405. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9406. p1 p2)))
  9407. ((or p1 p2) (goto-char (or p1 p2)))
  9408. (t (error "No table dataline around here"))))))
  9409. (defun org-table-fedit-line-up ()
  9410. "Move cursor one line up in the window showing the table."
  9411. (interactive)
  9412. (org-table-fedit-move 'previous-line))
  9413. (defun org-table-fedit-line-down ()
  9414. "Move cursor one line down in the window showing the table."
  9415. (interactive)
  9416. (org-table-fedit-move 'next-line))
  9417. (defun org-table-fedit-move (command)
  9418. "Move the cursor in the window shoinw the table.
  9419. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9420. (let ((org-table-allow-automatic-line-recalculation nil)
  9421. (pos org-pos) (win (selected-window)) p)
  9422. (select-window (get-buffer-window (marker-buffer org-pos)))
  9423. (setq p (point))
  9424. (call-interactively command)
  9425. (while (and (org-at-table-p)
  9426. (org-at-table-hline-p))
  9427. (call-interactively command))
  9428. (or (org-at-table-p) (goto-char p))
  9429. (move-marker pos (point))
  9430. (select-window win)))
  9431. (defun org-table-fedit-scroll (N)
  9432. (interactive "p")
  9433. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9434. (scroll-other-window N)))
  9435. (defun org-table-fedit-scroll-down (N)
  9436. (interactive "p")
  9437. (org-table-fedit-scroll (- N)))
  9438. (defvar org-table-rectangle-overlays nil)
  9439. (defun org-table-add-rectangle-overlay (beg end &optional face)
  9440. "Add a new overlay."
  9441. (let ((ov (org-make-overlay beg end)))
  9442. (org-overlay-put ov 'face (or face 'secondary-selection))
  9443. (push ov org-table-rectangle-overlays)))
  9444. (defun org-table-highlight-rectangle (&optional beg end face)
  9445. "Highlight rectangular region in a table."
  9446. (setq beg (or beg (point)) end (or end (point)))
  9447. (let ((b (min beg end))
  9448. (e (max beg end))
  9449. l1 c1 l2 c2 tmp)
  9450. (and (boundp 'org-show-positions)
  9451. (setq org-show-positions (cons b (cons e org-show-positions))))
  9452. (goto-char (min beg end))
  9453. (setq l1 (org-current-line)
  9454. c1 (org-table-current-column))
  9455. (goto-char (max beg end))
  9456. (setq l2 (org-current-line)
  9457. c2 (org-table-current-column))
  9458. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  9459. (goto-line l1)
  9460. (beginning-of-line 1)
  9461. (loop for line from l1 to l2 do
  9462. (when (looking-at org-table-dataline-regexp)
  9463. (org-table-goto-column c1)
  9464. (skip-chars-backward "^|\n") (setq beg (point))
  9465. (org-table-goto-column c2)
  9466. (skip-chars-forward "^|\n") (setq end (point))
  9467. (org-table-add-rectangle-overlay beg end face))
  9468. (beginning-of-line 2))
  9469. (goto-char b))
  9470. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  9471. (defun org-table-remove-rectangle-highlight (&rest ignore)
  9472. "Remove the rectangle overlays."
  9473. (unless org-inhibit-highlight-removal
  9474. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  9475. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  9476. (setq org-table-rectangle-overlays nil)))
  9477. (defvar org-table-coordinate-overlays nil
  9478. "Collects the cooordinate grid overlays, so that they can be removed.")
  9479. (make-variable-buffer-local 'org-table-coordinate-overlays)
  9480. (defun org-table-overlay-coordinates ()
  9481. "Add overlays to the table at point, to show row/column coordinates."
  9482. (interactive)
  9483. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9484. (setq org-table-coordinate-overlays nil)
  9485. (save-excursion
  9486. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  9487. (goto-char (org-table-begin))
  9488. (while (org-at-table-p)
  9489. (setq eol (point-at-eol))
  9490. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  9491. (push ov org-table-coordinate-overlays)
  9492. (setq hline (looking-at org-table-hline-regexp))
  9493. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  9494. (format "%4d" (setq id (1+ id)))))
  9495. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  9496. (when hline
  9497. (setq ic 0)
  9498. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  9499. (setq beg (1+ (match-beginning 0))
  9500. ic (1+ ic)
  9501. s1 (concat "$" (int-to-string ic))
  9502. s2 (org-number-to-letters ic)
  9503. str (if (eq org-table-use-standard-references t) s2 s1))
  9504. (setq ov (org-make-overlay beg (+ beg (length str))))
  9505. (push ov org-table-coordinate-overlays)
  9506. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  9507. (beginning-of-line 2)))))
  9508. (defun org-table-toggle-coordinate-overlays ()
  9509. "Toggle the display of Row/Column numbers in tables."
  9510. (interactive)
  9511. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  9512. (message "Row/Column number display turned %s"
  9513. (if org-table-overlay-coordinates "on" "off"))
  9514. (if (and (org-at-table-p) org-table-overlay-coordinates)
  9515. (org-table-align))
  9516. (unless org-table-overlay-coordinates
  9517. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9518. (setq org-table-coordinate-overlays nil)))
  9519. (defun org-table-toggle-formula-debugger ()
  9520. "Toggle the formula debugger in tables."
  9521. (interactive)
  9522. (setq org-table-formula-debug (not org-table-formula-debug))
  9523. (message "Formula debugging has been turned %s"
  9524. (if org-table-formula-debug "on" "off")))
  9525. ;;; The orgtbl minor mode
  9526. ;; Define a minor mode which can be used in other modes in order to
  9527. ;; integrate the org-mode table editor.
  9528. ;; This is really a hack, because the org-mode table editor uses several
  9529. ;; keys which normally belong to the major mode, for example the TAB and
  9530. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9531. ;; necessary to operate the table editor, but wraps the commands into a
  9532. ;; function which tests if the cursor is currently inside a table. If that
  9533. ;; is the case, the table editor command is executed. However, when any of
  9534. ;; those keys is used outside a table, the function uses `key-binding' to
  9535. ;; look up if the key has an associated command in another currently active
  9536. ;; keymap (minor modes, major mode, global), and executes that command.
  9537. ;; There might be problems if any of the keys used by the table editor is
  9538. ;; otherwise used as a prefix key.
  9539. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9540. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9541. ;; addresses this by checking explicitly for both bindings.
  9542. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9543. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9544. ;; Some modes bind other commands to simple characters, for example
  9545. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9546. ;; active, this binding is ignored inside tables and replaced with a
  9547. ;; modified self-insert.
  9548. (defvar orgtbl-mode nil
  9549. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9550. table editor in arbitrary modes.")
  9551. (make-variable-buffer-local 'orgtbl-mode)
  9552. (defvar orgtbl-mode-map (make-keymap)
  9553. "Keymap for `orgtbl-mode'.")
  9554. ;;;###autoload
  9555. (defun turn-on-orgtbl ()
  9556. "Unconditionally turn on `orgtbl-mode'."
  9557. (orgtbl-mode 1))
  9558. (defvar org-old-auto-fill-inhibit-regexp nil
  9559. "Local variable used by `orgtbl-mode'")
  9560. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  9561. "Matches a line belonging to an orgtbl.")
  9562. (defconst orgtbl-extra-font-lock-keywords
  9563. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  9564. 0 (quote 'org-table) 'prepend))
  9565. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  9566. ;;;###autoload
  9567. (defun orgtbl-mode (&optional arg)
  9568. "The `org-mode' table editor as a minor mode for use in other modes."
  9569. (interactive)
  9570. (if (org-mode-p)
  9571. ;; Exit without error, in case some hook functions calls this
  9572. ;; by accident in org-mode.
  9573. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9574. (setq orgtbl-mode
  9575. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9576. (if orgtbl-mode
  9577. (progn
  9578. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9579. ;; Make sure we are first in minor-mode-map-alist
  9580. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9581. (and c (setq minor-mode-map-alist
  9582. (cons c (delq c minor-mode-map-alist)))))
  9583. (org-set-local (quote org-table-may-need-update) t)
  9584. (org-add-hook 'before-change-functions 'org-before-change-function
  9585. nil 'local)
  9586. (org-set-local 'org-old-auto-fill-inhibit-regexp
  9587. auto-fill-inhibit-regexp)
  9588. (org-set-local 'auto-fill-inhibit-regexp
  9589. (if auto-fill-inhibit-regexp
  9590. (concat orgtbl-line-start-regexp "\\|"
  9591. auto-fill-inhibit-regexp)
  9592. orgtbl-line-start-regexp))
  9593. (org-add-to-invisibility-spec '(org-cwidth))
  9594. (when (fboundp 'font-lock-add-keywords)
  9595. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  9596. (org-restart-font-lock))
  9597. (easy-menu-add orgtbl-mode-menu)
  9598. (run-hooks 'orgtbl-mode-hook))
  9599. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9600. (org-cleanup-narrow-column-properties)
  9601. (org-remove-from-invisibility-spec '(org-cwidth))
  9602. (remove-hook 'before-change-functions 'org-before-change-function t)
  9603. (when (fboundp 'font-lock-remove-keywords)
  9604. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  9605. (org-restart-font-lock))
  9606. (easy-menu-remove orgtbl-mode-menu)
  9607. (force-mode-line-update 'all))))
  9608. (defun org-cleanup-narrow-column-properties ()
  9609. "Remove all properties related to narrow-column invisibility."
  9610. (let ((s 1))
  9611. (while (setq s (text-property-any s (point-max)
  9612. 'display org-narrow-column-arrow))
  9613. (remove-text-properties s (1+ s) '(display t)))
  9614. (setq s 1)
  9615. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  9616. (remove-text-properties s (1+ s) '(org-cwidth t)))
  9617. (setq s 1)
  9618. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  9619. (remove-text-properties s (1+ s) '(invisible t)))))
  9620. ;; Install it as a minor mode.
  9621. (put 'orgtbl-mode :included t)
  9622. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9623. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9624. (defun orgtbl-make-binding (fun n &rest keys)
  9625. "Create a function for binding in the table minor mode.
  9626. FUN is the command to call inside a table. N is used to create a unique
  9627. command name. KEYS are keys that should be checked in for a command
  9628. to execute outside of tables."
  9629. (eval
  9630. (list 'defun
  9631. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9632. '(arg)
  9633. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9634. "Outside of tables, run the binding of `"
  9635. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9636. "'.")
  9637. '(interactive "p")
  9638. (list 'if
  9639. '(org-at-table-p)
  9640. (list 'call-interactively (list 'quote fun))
  9641. (list 'let '(orgtbl-mode)
  9642. (list 'call-interactively
  9643. (append '(or)
  9644. (mapcar (lambda (k)
  9645. (list 'key-binding k))
  9646. keys)
  9647. '('orgtbl-error))))))))
  9648. (defun orgtbl-error ()
  9649. "Error when there is no default binding for a table key."
  9650. (interactive)
  9651. (error "This key is has no function outside tables"))
  9652. (defun orgtbl-setup ()
  9653. "Setup orgtbl keymaps."
  9654. (let ((nfunc 0)
  9655. (bindings
  9656. (list
  9657. '([(meta shift left)] org-table-delete-column)
  9658. '([(meta left)] org-table-move-column-left)
  9659. '([(meta right)] org-table-move-column-right)
  9660. '([(meta shift right)] org-table-insert-column)
  9661. '([(meta shift up)] org-table-kill-row)
  9662. '([(meta shift down)] org-table-insert-row)
  9663. '([(meta up)] org-table-move-row-up)
  9664. '([(meta down)] org-table-move-row-down)
  9665. '("\C-c\C-w" org-table-cut-region)
  9666. '("\C-c\M-w" org-table-copy-region)
  9667. '("\C-c\C-y" org-table-paste-rectangle)
  9668. '("\C-c-" org-table-insert-hline)
  9669. '("\C-c}" org-table-toggle-coordinate-overlays)
  9670. '("\C-c{" org-table-toggle-formula-debugger)
  9671. '("\C-m" org-table-next-row)
  9672. '([(shift return)] org-table-copy-down)
  9673. '("\C-c\C-q" org-table-wrap-region)
  9674. '("\C-c?" org-table-field-info)
  9675. '("\C-c " org-table-blank-field)
  9676. '("\C-c+" org-table-sum)
  9677. '("\C-c=" org-table-eval-formula)
  9678. '("\C-c'" org-table-edit-formulas)
  9679. '("\C-c`" org-table-edit-field)
  9680. '("\C-c*" org-table-recalculate)
  9681. '("\C-c|" org-table-create-or-convert-from-region)
  9682. '("\C-c^" org-table-sort-lines)
  9683. '([(control ?#)] org-table-rotate-recalc-marks)))
  9684. elt key fun cmd)
  9685. (while (setq elt (pop bindings))
  9686. (setq nfunc (1+ nfunc))
  9687. (setq key (org-key (car elt))
  9688. fun (nth 1 elt)
  9689. cmd (orgtbl-make-binding fun nfunc key))
  9690. (org-defkey orgtbl-mode-map key cmd))
  9691. ;; Special treatment needed for TAB and RET
  9692. (org-defkey orgtbl-mode-map [(return)]
  9693. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9694. (org-defkey orgtbl-mode-map "\C-m"
  9695. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9696. (org-defkey orgtbl-mode-map [(tab)]
  9697. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9698. (org-defkey orgtbl-mode-map "\C-i"
  9699. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  9700. (org-defkey orgtbl-mode-map [(shift tab)]
  9701. (orgtbl-make-binding 'org-table-previous-field 104
  9702. [(shift tab)] [(tab)] "\C-i"))
  9703. (org-defkey orgtbl-mode-map "\M-\C-m"
  9704. (orgtbl-make-binding 'org-table-wrap-region 105
  9705. "\M-\C-m" [(meta return)]))
  9706. (org-defkey orgtbl-mode-map [(meta return)]
  9707. (orgtbl-make-binding 'org-table-wrap-region 106
  9708. [(meta return)] "\M-\C-m"))
  9709. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  9710. (when orgtbl-optimized
  9711. ;; If the user wants maximum table support, we need to hijack
  9712. ;; some standard editing functions
  9713. (org-remap orgtbl-mode-map
  9714. 'self-insert-command 'orgtbl-self-insert-command
  9715. 'delete-char 'org-delete-char
  9716. 'delete-backward-char 'org-delete-backward-char)
  9717. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  9718. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9719. '("OrgTbl"
  9720. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9721. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9722. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9723. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9724. "--"
  9725. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9726. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9727. ["Copy Field from Above"
  9728. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9729. "--"
  9730. ("Column"
  9731. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9732. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9733. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9734. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  9735. ("Row"
  9736. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9737. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9738. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9739. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9740. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9741. "--"
  9742. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9743. ("Rectangle"
  9744. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9745. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9746. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9747. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9748. "--"
  9749. ("Radio tables"
  9750. ["Insert table template" orgtbl-insert-radio-table
  9751. (assq major-mode orgtbl-radio-table-templates)]
  9752. ["Comment/uncomment table" orgtbl-toggle-comment t])
  9753. "--"
  9754. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9755. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9756. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9757. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9758. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9759. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  9760. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9761. ["Sum Column/Rectangle" org-table-sum
  9762. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9763. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9764. ["Debug Formulas"
  9765. org-table-toggle-formula-debugger :active (org-at-table-p)
  9766. :keys "C-c {"
  9767. :style toggle :selected org-table-formula-debug]
  9768. ["Show Col/Row Numbers"
  9769. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9770. :keys "C-c }"
  9771. :style toggle :selected org-table-overlay-coordinates]
  9772. ))
  9773. t))
  9774. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9775. "If the cursor is inside a table, realign the table.
  9776. It it is a table to be sent away to a receiver, do it.
  9777. With prefix arg, also recompute table."
  9778. (interactive "P")
  9779. (let ((pos (point)) action)
  9780. (save-excursion
  9781. (beginning-of-line 1)
  9782. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9783. ((looking-at "[ \t]*|") pos)
  9784. ((looking-at "#\\+TBLFM:") 'recalc))))
  9785. (cond
  9786. ((integerp action)
  9787. (goto-char action)
  9788. (org-table-maybe-eval-formula)
  9789. (if arg
  9790. (call-interactively 'org-table-recalculate)
  9791. (org-table-maybe-recalculate-line))
  9792. (call-interactively 'org-table-align)
  9793. (orgtbl-send-table 'maybe))
  9794. ((eq action 'recalc)
  9795. (save-excursion
  9796. (beginning-of-line 1)
  9797. (skip-chars-backward " \r\n\t")
  9798. (if (org-at-table-p)
  9799. (org-call-with-arg 'org-table-recalculate t))))
  9800. (t (let (orgtbl-mode)
  9801. (call-interactively (key-binding "\C-c\C-c")))))))
  9802. (defun orgtbl-tab (arg)
  9803. "Justification and field motion for `orgtbl-mode'."
  9804. (interactive "P")
  9805. (if arg (org-table-edit-field t)
  9806. (org-table-justify-field-maybe)
  9807. (org-table-next-field)))
  9808. (defun orgtbl-ret ()
  9809. "Justification and field motion for `orgtbl-mode'."
  9810. (interactive)
  9811. (org-table-justify-field-maybe)
  9812. (org-table-next-row))
  9813. (defun orgtbl-self-insert-command (N)
  9814. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9815. If the cursor is in a table looking at whitespace, the whitespace is
  9816. overwritten, and the table is not marked as requiring realignment."
  9817. (interactive "p")
  9818. (if (and (org-at-table-p)
  9819. (or
  9820. (and org-table-auto-blank-field
  9821. (member last-command
  9822. '(orgtbl-hijacker-command-100
  9823. orgtbl-hijacker-command-101
  9824. orgtbl-hijacker-command-102
  9825. orgtbl-hijacker-command-103
  9826. orgtbl-hijacker-command-104
  9827. orgtbl-hijacker-command-105))
  9828. (org-table-blank-field))
  9829. t)
  9830. (eq N 1)
  9831. (looking-at "[^|\n]* +|"))
  9832. (let (org-table-may-need-update)
  9833. (goto-char (1- (match-end 0)))
  9834. (delete-backward-char 1)
  9835. (goto-char (match-beginning 0))
  9836. (self-insert-command N))
  9837. (setq org-table-may-need-update t)
  9838. (let (orgtbl-mode)
  9839. (call-interactively (key-binding (vector last-input-event))))))
  9840. (defun org-force-self-insert (N)
  9841. "Needed to enforce self-insert under remapping."
  9842. (interactive "p")
  9843. (self-insert-command N))
  9844. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9845. "Regula expression matching exponentials as produced by calc.")
  9846. (defvar org-table-clean-did-remove-column nil)
  9847. (defun orgtbl-export (table target)
  9848. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9849. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9850. org-table-last-alignment org-table-last-column-widths
  9851. maxcol column)
  9852. (if (not (fboundp func))
  9853. (error "Cannot export orgtbl table to %s" target))
  9854. (setq lines (org-table-clean-before-export lines))
  9855. (setq table
  9856. (mapcar
  9857. (lambda (x)
  9858. (if (string-match org-table-hline-regexp x)
  9859. 'hline
  9860. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  9861. lines))
  9862. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  9863. table)))
  9864. (loop for i from (1- maxcol) downto 0 do
  9865. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  9866. (setq column (delq nil column))
  9867. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  9868. (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))
  9869. (funcall func table nil)))
  9870. (defun orgtbl-send-table (&optional maybe)
  9871. "Send a tranformed version of this table to the receiver position.
  9872. With argument MAYBE, fail quietly if no transformation is defined for
  9873. this table."
  9874. (interactive)
  9875. (catch 'exit
  9876. (unless (org-at-table-p) (error "Not at a table"))
  9877. ;; when non-interactive, we assume align has just happened.
  9878. (when (interactive-p) (org-table-align))
  9879. (save-excursion
  9880. (goto-char (org-table-begin))
  9881. (beginning-of-line 0)
  9882. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  9883. (if maybe
  9884. (throw 'exit nil)
  9885. (error "Don't know how to transform this table."))))
  9886. (let* ((name (match-string 1))
  9887. beg
  9888. (transform (intern (match-string 2)))
  9889. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  9890. (skip (plist-get params :skip))
  9891. (skipcols (plist-get params :skipcols))
  9892. (txt (buffer-substring-no-properties
  9893. (org-table-begin) (org-table-end)))
  9894. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  9895. (lines (org-table-clean-before-export lines))
  9896. (i0 (if org-table-clean-did-remove-column 2 1))
  9897. (table (mapcar
  9898. (lambda (x)
  9899. (if (string-match org-table-hline-regexp x)
  9900. 'hline
  9901. (org-remove-by-index
  9902. (org-split-string (org-trim x) "\\s-*|\\s-*")
  9903. skipcols i0)))
  9904. lines))
  9905. (fun (if (= i0 2) 'cdr 'identity))
  9906. (org-table-last-alignment
  9907. (org-remove-by-index (funcall fun org-table-last-alignment)
  9908. skipcols i0))
  9909. (org-table-last-column-widths
  9910. (org-remove-by-index (funcall fun org-table-last-column-widths)
  9911. skipcols i0)))
  9912. (unless (fboundp transform)
  9913. (error "No such transformation function %s" transform))
  9914. (setq txt (funcall transform table params))
  9915. ;; Find the insertion place
  9916. (save-excursion
  9917. (goto-char (point-min))
  9918. (unless (re-search-forward
  9919. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  9920. (error "Don't know where to insert translated table"))
  9921. (goto-char (match-beginning 0))
  9922. (beginning-of-line 2)
  9923. (setq beg (point))
  9924. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  9925. (error "Cannot find end of insertion region"))
  9926. (beginning-of-line 1)
  9927. (delete-region beg (point))
  9928. (goto-char beg)
  9929. (insert txt "\n"))
  9930. (message "Table converted and installed at receiver location"))))
  9931. (defun org-remove-by-index (list indices &optional i0)
  9932. "Remove the elements in LIST with indices in INDICES.
  9933. First element has index 0, or I0 if given."
  9934. (if (not indices)
  9935. list
  9936. (if (integerp indices) (setq indices (list indices)))
  9937. (setq i0 (1- (or i0 0)))
  9938. (delq :rm (mapcar (lambda (x)
  9939. (setq i0 (1+ i0))
  9940. (if (memq i0 indices) :rm x))
  9941. list))))
  9942. (defun orgtbl-toggle-comment ()
  9943. "Comment or uncomment the orgtbl at point."
  9944. (interactive)
  9945. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  9946. (re2 (concat "^" orgtbl-line-start-regexp))
  9947. (commented (save-excursion (beginning-of-line 1)
  9948. (cond ((looking-at re1) t)
  9949. ((looking-at re2) nil)
  9950. (t (error "Not at an org table")))))
  9951. (re (if commented re1 re2))
  9952. beg end)
  9953. (save-excursion
  9954. (beginning-of-line 1)
  9955. (while (looking-at re) (beginning-of-line 0))
  9956. (beginning-of-line 2)
  9957. (setq beg (point))
  9958. (while (looking-at re) (beginning-of-line 2))
  9959. (setq end (point)))
  9960. (comment-region beg end (if commented '(4) nil))))
  9961. (defun orgtbl-insert-radio-table ()
  9962. "Insert a radio table template appropriate for this major mode."
  9963. (interactive)
  9964. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  9965. (txt (nth 1 e))
  9966. name pos)
  9967. (unless e (error "No radio table setup defined for %s" major-mode))
  9968. (setq name (read-string "Table name: "))
  9969. (while (string-match "%n" txt)
  9970. (setq txt (replace-match name t t txt)))
  9971. (or (bolp) (insert "\n"))
  9972. (setq pos (point))
  9973. (insert txt)
  9974. (goto-char pos)))
  9975. (defun org-get-param (params header i sym &optional hsym)
  9976. "Get parameter value for symbol SYM.
  9977. If this is a header line, actually get the value for the symbol with an
  9978. additional \"h\" inserted after the colon.
  9979. If the value is a protperty list, get the element for the current column.
  9980. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  9981. (let ((val (plist-get params sym)))
  9982. (and hsym header (setq val (or (plist-get params hsym) val)))
  9983. (if (consp val) (plist-get val i) val)))
  9984. (defun orgtbl-to-generic (table params)
  9985. "Convert the orgtbl-mode TABLE to some other format.
  9986. This generic routine can be used for many standard cases.
  9987. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9988. separator line, or a list of fields for that line.
  9989. PARAMS is a property list of parameters that can influence the conversion.
  9990. For the generic converter, some parameters are obligatory: You need to
  9991. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  9992. :splice, you must have :tstart and :tend.
  9993. Valid parameters are
  9994. :tstart String to start the table. Ignored when :splice is t.
  9995. :tend String to end the table. Ignored when :splice is t.
  9996. :splice When set to t, return only table body lines, don't wrap
  9997. them into :tstart and :tend. Default is nil.
  9998. :hline String to be inserted on horizontal separation lines.
  9999. May be nil to ignore hlines.
  10000. :lstart String to start a new table line.
  10001. :lend String to end a table line
  10002. :sep Separator between two fields
  10003. :lfmt Format for entire line, with enough %s to capture all fields.
  10004. If this is present, :lstart, :lend, and :sep are ignored.
  10005. :fmt A format to be used to wrap the field, should contain
  10006. %s for the original field value. For example, to wrap
  10007. everything in dollars, you could use :fmt \"$%s$\".
  10008. This may also be a property list with column numbers and
  10009. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10010. :hlstart :hlend :hlsep :hlfmt :hfmt
  10011. Same as above, specific for the header lines in the table.
  10012. All lines before the first hline are treated as header.
  10013. If any of these is not present, the data line value is used.
  10014. :efmt Use this format to print numbers with exponentials.
  10015. The format should have %s twice for inserting mantissa
  10016. and exponent, for example \"%s\\\\times10^{%s}\". This
  10017. may also be a property list with column numbers and
  10018. formats. :fmt will still be applied after :efmt.
  10019. In addition to this, the parameters :skip and :skipcols are always handled
  10020. directly by `orgtbl-send-table'. See manual."
  10021. (interactive)
  10022. (let* ((p params)
  10023. (splicep (plist-get p :splice))
  10024. (hline (plist-get p :hline))
  10025. rtn line i fm efm lfmt h)
  10026. ;; Do we have a header?
  10027. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10028. (setq h t))
  10029. ;; Put header
  10030. (unless splicep
  10031. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10032. ;; Now loop over all lines
  10033. (while (setq line (pop table))
  10034. (if (eq line 'hline)
  10035. ;; A horizontal separator line
  10036. (progn (if hline (push hline rtn))
  10037. (setq h nil)) ; no longer in header
  10038. ;; A normal line. Convert the fields, push line onto the result list
  10039. (setq i 0)
  10040. (setq line
  10041. (mapcar
  10042. (lambda (f)
  10043. (setq i (1+ i)
  10044. fm (org-get-param p h i :fmt :hfmt)
  10045. efm (org-get-param p h i :efmt))
  10046. (if (and efm (string-match orgtbl-exp-regexp f))
  10047. (setq f (format
  10048. efm (match-string 1 f) (match-string 2 f))))
  10049. (if fm (setq f (format fm f)))
  10050. f)
  10051. line))
  10052. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10053. (push (apply 'format lfmt line) rtn)
  10054. (push (concat
  10055. (org-get-param p h i :lstart :hlstart)
  10056. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10057. (org-get-param p h i :lend :hlend))
  10058. rtn))))
  10059. (unless splicep
  10060. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10061. (mapconcat 'identity (nreverse rtn) "\n")))
  10062. (defun orgtbl-to-latex (table params)
  10063. "Convert the orgtbl-mode TABLE to LaTeX.
  10064. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10065. separator line, or a list of fields for that line.
  10066. PARAMS is a property list of parameters that can influence the conversion.
  10067. Supports all parameters from `orgtbl-to-generic'. Most important for
  10068. LaTeX are:
  10069. :splice When set to t, return only table body lines, don't wrap
  10070. them into a tabular environment. Default is nil.
  10071. :fmt A format to be used to wrap the field, should contain %s for the
  10072. original field value. For example, to wrap everything in dollars,
  10073. use :fmt \"$%s$\". This may also be a property list with column
  10074. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10075. :efmt Format for transforming numbers with exponentials. The format
  10076. should have %s twice for inserting mantissa and exponent, for
  10077. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10078. This may also be a property list with column numbers and formats.
  10079. The general parameters :skip and :skipcols have already been applied when
  10080. this function is called."
  10081. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10082. org-table-last-alignment ""))
  10083. (params2
  10084. (list
  10085. :tstart (concat "\\begin{tabular}{" alignment "}")
  10086. :tend "\\end{tabular}"
  10087. :lstart "" :lend " \\\\" :sep " & "
  10088. :efmt "%s\\,(%s)" :hline "\\hline")))
  10089. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10090. (defun orgtbl-to-html (table params)
  10091. "Convert the orgtbl-mode TABLE to LaTeX.
  10092. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10093. separator line, or a list of fields for that line.
  10094. PARAMS is a property list of parameters that can influence the conversion.
  10095. Currently this function recognizes the following parameters:
  10096. :splice When set to t, return only table body lines, don't wrap
  10097. them into a <table> environment. Default is nil.
  10098. The general parameters :skip and :skipcols have already been applied when
  10099. this function is called. The function does *not* use `orgtbl-to-generic',
  10100. so you cannot specify parameters for it."
  10101. (let* ((splicep (plist-get params :splice))
  10102. html)
  10103. ;; Just call the formatter we already have
  10104. ;; We need to make text lines for it, so put the fields back together.
  10105. (setq html (org-format-org-table-html
  10106. (mapcar
  10107. (lambda (x)
  10108. (if (eq x 'hline)
  10109. "|----+----|"
  10110. (concat "| " (mapconcat 'identity x " | ") " |")))
  10111. table)
  10112. splicep))
  10113. (if (string-match "\n+\\'" html)
  10114. (setq html (replace-match "" t t html)))
  10115. html))
  10116. (defun orgtbl-to-texinfo (table params)
  10117. "Convert the orgtbl-mode TABLE to TeXInfo.
  10118. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10119. separator line, or a list of fields for that line.
  10120. PARAMS is a property list of parameters that can influence the conversion.
  10121. Supports all parameters from `orgtbl-to-generic'. Most important for
  10122. TeXInfo are:
  10123. :splice nil/t When set to t, return only table body lines, don't wrap
  10124. them into a multitable environment. Default is nil.
  10125. :fmt fmt A format to be used to wrap the field, should contain
  10126. %s for the original field value. For example, to wrap
  10127. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10128. This may also be a property list with column numbers and
  10129. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10130. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  10131. are computed automatically from the width of the columns
  10132. under org-mode.
  10133. The general parameters :skip and :skipcols have already been applied when
  10134. this function is called."
  10135. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10136. (colfrac (or (plist-get params :cf)
  10137. (mapconcat
  10138. (lambda (x) (format "%.3f" (/ (float x) total)))
  10139. org-table-last-column-widths " ")))
  10140. (params2
  10141. (list
  10142. :tstart (concat "@multitable @columnfractions " colfrac)
  10143. :tend "@end multitable"
  10144. :lstart "@item " :lend "" :sep " @tab "
  10145. :hlstart "@headitem ")))
  10146. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10147. ;;;; Link Stuff
  10148. ;;; Link abbreviations
  10149. (defun org-link-expand-abbrev (link)
  10150. "Apply replacements as defined in `org-link-abbrev-alist."
  10151. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10152. (let* ((key (match-string 1 link))
  10153. (as (or (assoc key org-link-abbrev-alist-local)
  10154. (assoc key org-link-abbrev-alist)))
  10155. (tag (and (match-end 2) (match-string 3 link)))
  10156. rpl)
  10157. (if (not as)
  10158. link
  10159. (setq rpl (cdr as))
  10160. (cond
  10161. ((symbolp rpl) (funcall rpl tag))
  10162. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10163. (t (concat rpl tag)))))
  10164. link))
  10165. ;;; Storing and inserting links
  10166. (defvar org-insert-link-history nil
  10167. "Minibuffer history for links inserted with `org-insert-link'.")
  10168. (defvar org-stored-links nil
  10169. "Contains the links stored with `org-store-link'.")
  10170. (defvar org-store-link-plist nil
  10171. "Plist with info about the most recently link created with `org-store-link'.")
  10172. (defvar org-link-protocols nil
  10173. "Link protocols added to Org-mode using `org-add-link-type'.")
  10174. (defvar org-store-link-functions nil
  10175. "List of functions that are called to create and store a link.
  10176. Each function will be called in turn until one returns a non-nil
  10177. value. Each function should check if it is responsible for creating
  10178. this link (for example by looking at the major mode).
  10179. If not, it must exit and return nil.
  10180. If yes, it should return a non-nil value after a calling
  10181. `org-store-link-properties' with a list of properties and values.
  10182. Special properties are:
  10183. :type The link prefix. like \"http\". This must be given.
  10184. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10185. This is obligatory as well.
  10186. :description Optional default description for the second pair
  10187. of brackets in an Org-mode link. The user can still change
  10188. this when inserting this link into an Org-mode buffer.
  10189. In addition to these, any additional properties can be specified
  10190. and then used in remember templates.")
  10191. (defun org-add-link-type (type &optional follow publish)
  10192. "Add TYPE to the list of `org-link-types'.
  10193. Re-compute all regular expressions depending on `org-link-types'
  10194. FOLLOW and PUBLISH are two functions. Both take the link path as
  10195. an argument.
  10196. FOLLOW should do whatever is necessary to follow the link, for example
  10197. to find a file or display a mail message.
  10198. PUBLISH takes the path and retuns the string that should be used when
  10199. this document is published."
  10200. (add-to-list 'org-link-types type t)
  10201. (org-make-link-regexps)
  10202. (add-to-list 'org-link-protocols
  10203. (list type follow publish)))
  10204. (defun org-add-agenda-custom-command (entry)
  10205. "Replace or add a command in `org-agenda-custom-commands'.
  10206. This is mostly for hacking and trying a new command - once the command
  10207. works you probably want to add it to `org-agenda-custom-commands' for good."
  10208. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10209. (if ass
  10210. (setcdr ass (cdr entry))
  10211. (push entry org-agenda-custom-commands))))
  10212. ;;;###autoload
  10213. (defun org-store-link (arg)
  10214. "\\<org-mode-map>Store an org-link to the current location.
  10215. This link can later be inserted into an org-buffer with
  10216. \\[org-insert-link].
  10217. For some link types, a prefix arg is interpreted:
  10218. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10219. For file links, arg negates `org-context-in-file-links'."
  10220. (interactive "P")
  10221. (setq org-store-link-plist nil) ; reset
  10222. (let (link cpltxt desc description search txt)
  10223. (cond
  10224. ((run-hook-with-args-until-success 'org-store-link-functions)
  10225. (setq link (plist-get org-store-link-plist :link)
  10226. desc (or (plist-get org-store-link-plist :description) link)))
  10227. ((eq major-mode 'bbdb-mode)
  10228. (let ((name (bbdb-record-name (bbdb-current-record)))
  10229. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10230. (setq cpltxt (concat "bbdb:" (or name company))
  10231. link (org-make-link cpltxt))
  10232. (org-store-link-props :type "bbdb" :name name :company company)))
  10233. ((eq major-mode 'Info-mode)
  10234. (setq link (org-make-link "info:"
  10235. (file-name-nondirectory Info-current-file)
  10236. ":" Info-current-node))
  10237. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10238. ":" Info-current-node))
  10239. (org-store-link-props :type "info" :file Info-current-file
  10240. :node Info-current-node))
  10241. ((eq major-mode 'calendar-mode)
  10242. (let ((cd (calendar-cursor-to-date)))
  10243. (setq link
  10244. (format-time-string
  10245. (car org-time-stamp-formats)
  10246. (apply 'encode-time
  10247. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10248. nil nil nil))))
  10249. (org-store-link-props :type "calendar" :date cd)))
  10250. ((or (eq major-mode 'vm-summary-mode)
  10251. (eq major-mode 'vm-presentation-mode))
  10252. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10253. (vm-follow-summary-cursor)
  10254. (save-excursion
  10255. (vm-select-folder-buffer)
  10256. (let* ((message (car vm-message-pointer))
  10257. (folder buffer-file-name)
  10258. (subject (vm-su-subject message))
  10259. (to (vm-get-header-contents message "To"))
  10260. (from (vm-get-header-contents message "From"))
  10261. (message-id (vm-su-message-id message)))
  10262. (org-store-link-props :type "vm" :from from :to to :subject subject
  10263. :message-id message-id)
  10264. (setq message-id (org-remove-angle-brackets message-id))
  10265. (setq folder (abbreviate-file-name folder))
  10266. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10267. folder)
  10268. (setq folder (replace-match "" t t folder)))
  10269. (setq cpltxt (org-email-link-description))
  10270. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10271. ((eq major-mode 'wl-summary-mode)
  10272. (let* ((msgnum (wl-summary-message-number))
  10273. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10274. msgnum 'message-id))
  10275. (wl-message-entity
  10276. (if (fboundp 'elmo-message-entity)
  10277. (elmo-message-entity
  10278. wl-summary-buffer-elmo-folder msgnum)
  10279. (elmo-msgdb-overview-get-entity
  10280. msgnum (wl-summary-buffer-msgdb))))
  10281. (from (wl-summary-line-from))
  10282. (to (elmo-message-entity-field wl-message-entity 'to))
  10283. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10284. (wl-summary-line-subject))))
  10285. (org-store-link-props :type "wl" :from from :to to
  10286. :subject subject :message-id message-id)
  10287. (setq message-id (org-remove-angle-brackets message-id))
  10288. (setq cpltxt (org-email-link-description))
  10289. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10290. "#" message-id))))
  10291. ((or (equal major-mode 'mh-folder-mode)
  10292. (equal major-mode 'mh-show-mode))
  10293. (let ((from (org-mhe-get-header "From:"))
  10294. (to (org-mhe-get-header "To:"))
  10295. (message-id (org-mhe-get-header "Message-Id:"))
  10296. (subject (org-mhe-get-header "Subject:")))
  10297. (org-store-link-props :type "mh" :from from :to to
  10298. :subject subject :message-id message-id)
  10299. (setq cpltxt (org-email-link-description))
  10300. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10301. (org-remove-angle-brackets message-id)))))
  10302. ((eq major-mode 'rmail-mode)
  10303. (save-excursion
  10304. (save-restriction
  10305. (rmail-narrow-to-non-pruned-header)
  10306. (let ((folder buffer-file-name)
  10307. (message-id (mail-fetch-field "message-id"))
  10308. (from (mail-fetch-field "from"))
  10309. (to (mail-fetch-field "to"))
  10310. (subject (mail-fetch-field "subject")))
  10311. (org-store-link-props
  10312. :type "rmail" :from from :to to
  10313. :subject subject :message-id message-id)
  10314. (setq message-id (org-remove-angle-brackets message-id))
  10315. (setq cpltxt (org-email-link-description))
  10316. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10317. ((eq major-mode 'gnus-group-mode)
  10318. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10319. (gnus-group-group-name)) ; version
  10320. ((fboundp 'gnus-group-name)
  10321. (gnus-group-name))
  10322. (t "???"))))
  10323. (unless group (error "Not on a group"))
  10324. (org-store-link-props :type "gnus" :group group)
  10325. (setq cpltxt (concat
  10326. (if (org-xor arg org-usenet-links-prefer-google)
  10327. "http://groups.google.com/groups?group="
  10328. "gnus:")
  10329. group)
  10330. link (org-make-link cpltxt))))
  10331. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10332. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10333. (let* ((group gnus-newsgroup-name)
  10334. (article (gnus-summary-article-number))
  10335. (header (gnus-summary-article-header article))
  10336. (from (mail-header-from header))
  10337. (message-id (mail-header-id header))
  10338. (date (mail-header-date header))
  10339. (subject (gnus-summary-subject-string)))
  10340. (org-store-link-props :type "gnus" :from from :subject subject
  10341. :message-id message-id :group group)
  10342. (setq cpltxt (org-email-link-description))
  10343. (if (org-xor arg org-usenet-links-prefer-google)
  10344. (setq link
  10345. (concat
  10346. cpltxt "\n "
  10347. (format "http://groups.google.com/groups?as_umsgid=%s"
  10348. (org-fixup-message-id-for-http message-id))))
  10349. (setq link (org-make-link "gnus:" group
  10350. "#" (number-to-string article))))))
  10351. ((eq major-mode 'w3-mode)
  10352. (setq cpltxt (url-view-url t)
  10353. link (org-make-link cpltxt))
  10354. (org-store-link-props :type "w3" :url (url-view-url t)))
  10355. ((eq major-mode 'w3m-mode)
  10356. (setq cpltxt (or w3m-current-title w3m-current-url)
  10357. link (org-make-link w3m-current-url))
  10358. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10359. ((setq search (run-hook-with-args-until-success
  10360. 'org-create-file-search-functions))
  10361. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10362. "::" search))
  10363. (setq cpltxt (or description link)))
  10364. ((eq major-mode 'image-mode)
  10365. (setq cpltxt (concat "file:"
  10366. (abbreviate-file-name buffer-file-name))
  10367. link (org-make-link cpltxt))
  10368. (org-store-link-props :type "image" :file buffer-file-name))
  10369. ((eq major-mode 'dired-mode)
  10370. ;; link to the file in the current line
  10371. (setq cpltxt (concat "file:"
  10372. (abbreviate-file-name
  10373. (expand-file-name
  10374. (dired-get-filename nil t))))
  10375. link (org-make-link cpltxt)))
  10376. ((and buffer-file-name (org-mode-p))
  10377. ;; Just link to current headline
  10378. (setq cpltxt (concat "file:"
  10379. (abbreviate-file-name buffer-file-name)))
  10380. ;; Add a context search string
  10381. (when (org-xor org-context-in-file-links arg)
  10382. ;; Check if we are on a target
  10383. (if (org-in-regexp "<<\\(.*?\\)>>")
  10384. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10385. (setq txt (cond
  10386. ((org-on-heading-p) nil)
  10387. ((org-region-active-p)
  10388. (buffer-substring (region-beginning) (region-end)))
  10389. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10390. (when (or (null txt) (string-match "\\S-" txt))
  10391. (setq cpltxt
  10392. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10393. desc "NONE"))))
  10394. (if (string-match "::\\'" cpltxt)
  10395. (setq cpltxt (substring cpltxt 0 -2)))
  10396. (setq link (org-make-link cpltxt)))
  10397. ((buffer-file-name (buffer-base-buffer))
  10398. ;; Just link to this file here.
  10399. (setq cpltxt (concat "file:"
  10400. (abbreviate-file-name
  10401. (buffer-file-name (buffer-base-buffer)))))
  10402. ;; Add a context string
  10403. (when (org-xor org-context-in-file-links arg)
  10404. (setq txt (if (org-region-active-p)
  10405. (buffer-substring (region-beginning) (region-end))
  10406. (buffer-substring (point-at-bol) (point-at-eol))))
  10407. ;; Only use search option if there is some text.
  10408. (when (string-match "\\S-" txt)
  10409. (setq cpltxt
  10410. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10411. desc "NONE")))
  10412. (setq link (org-make-link cpltxt)))
  10413. ((interactive-p)
  10414. (error "Cannot link to a buffer which is not visiting a file"))
  10415. (t (setq link nil)))
  10416. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10417. (setq link (or link cpltxt)
  10418. desc (or desc cpltxt))
  10419. (if (equal desc "NONE") (setq desc nil))
  10420. (if (and (interactive-p) link)
  10421. (progn
  10422. (setq org-stored-links
  10423. (cons (list link desc) org-stored-links))
  10424. (message "Stored: %s" (or desc link)))
  10425. (and link (org-make-link-string link desc)))))
  10426. (defun org-store-link-props (&rest plist)
  10427. "Store link properties, extract names and addresses."
  10428. (let (x adr)
  10429. (when (setq x (plist-get plist :from))
  10430. (setq adr (mail-extract-address-components x))
  10431. (plist-put plist :fromname (car adr))
  10432. (plist-put plist :fromaddress (nth 1 adr)))
  10433. (when (setq x (plist-get plist :to))
  10434. (setq adr (mail-extract-address-components x))
  10435. (plist-put plist :toname (car adr))
  10436. (plist-put plist :toaddress (nth 1 adr))))
  10437. (let ((from (plist-get plist :from))
  10438. (to (plist-get plist :to)))
  10439. (when (and from to org-from-is-user-regexp)
  10440. (plist-put plist :fromto
  10441. (if (string-match org-from-is-user-regexp from)
  10442. (concat "to %t")
  10443. (concat "from %f")))))
  10444. (setq org-store-link-plist plist))
  10445. (defun org-email-link-description (&optional fmt)
  10446. "Return the description part of an email link.
  10447. This takes information from `org-store-link-plist' and formats it
  10448. according to FMT (default from `org-email-link-description-format')."
  10449. (setq fmt (or fmt org-email-link-description-format))
  10450. (let* ((p org-store-link-plist)
  10451. (to (plist-get p :toaddress))
  10452. (from (plist-get p :fromaddress))
  10453. (table
  10454. (list
  10455. (cons "%c" (plist-get p :fromto))
  10456. (cons "%F" (plist-get p :from))
  10457. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  10458. (cons "%T" (plist-get p :to))
  10459. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  10460. (cons "%s" (plist-get p :subject))
  10461. (cons "%m" (plist-get p :message-id)))))
  10462. (when (string-match "%c" fmt)
  10463. ;; Check if the user wrote this message
  10464. (if (and org-from-is-user-regexp from to
  10465. (save-match-data (string-match org-from-is-user-regexp from)))
  10466. (setq fmt (replace-match "to %t" t t fmt))
  10467. (setq fmt (replace-match "from %f" t t fmt))))
  10468. (org-replace-escapes fmt table)))
  10469. (defun org-make-org-heading-search-string (&optional string heading)
  10470. "Make search string for STRING or current headline."
  10471. (interactive)
  10472. (let ((s (or string (org-get-heading))))
  10473. (unless (and string (not heading))
  10474. ;; We are using a headline, clean up garbage in there.
  10475. (if (string-match org-todo-regexp s)
  10476. (setq s (replace-match "" t t s)))
  10477. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  10478. (setq s (replace-match "" t t s)))
  10479. (setq s (org-trim s))
  10480. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10481. org-comment-string "\\)") s)
  10482. (setq s (replace-match "" t t s)))
  10483. (while (string-match org-ts-regexp s)
  10484. (setq s (replace-match "" t t s))))
  10485. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10486. (setq s (replace-match " " t t s)))
  10487. (or string (setq s (concat "*" s))) ; Add * for headlines
  10488. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10489. (defun org-make-link (&rest strings)
  10490. "Concatenate STRINGS."
  10491. (apply 'concat strings))
  10492. (defun org-make-link-string (link &optional description)
  10493. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10494. (unless (string-match "\\S-" link)
  10495. (error "Empty link"))
  10496. (when (stringp description)
  10497. ;; Remove brackets from the description, they are fatal.
  10498. (while (string-match "\\[\\|\\]" description)
  10499. (setq description (replace-match "" t t description))))
  10500. (when (equal (org-link-escape link) description)
  10501. ;; No description needed, it is identical
  10502. (setq description nil))
  10503. (when (and (not description)
  10504. (not (equal link (org-link-escape link))))
  10505. (setq description link))
  10506. (concat "[[" (org-link-escape link) "]"
  10507. (if description (concat "[" description "]") "")
  10508. "]"))
  10509. (defconst org-link-escape-chars
  10510. '((" " . "%20")
  10511. ("[" . "%5B")
  10512. ("]" . "%5d")
  10513. ("\340" . "%E0") ; `a
  10514. ("\342" . "%E2") ; ^a
  10515. ("\347" . "%E7") ; ,c
  10516. ("\350" . "%E8") ; `e
  10517. ("\351" . "%E9") ; 'e
  10518. ("\352" . "%EA") ; ^e
  10519. ("\356" . "%EE") ; ^i
  10520. ("\364" . "%F4") ; ^o
  10521. ("\371" . "%F9") ; `u
  10522. ("\373" . "%FB") ; ^u
  10523. (";" . "%3B")
  10524. ("?" . "%3F")
  10525. ("=" . "%3D")
  10526. ("+" . "%2B")
  10527. )
  10528. "Association list of escapes for some characters problematic in links.
  10529. This is the list that is used for internal purposes.")
  10530. (defconst org-link-escape-chars-browser
  10531. '((" " . "%20"))
  10532. "Association list of escapes for some characters problematic in links.
  10533. This is the list that is used before handing over to the browser.")
  10534. (defun org-link-escape (text &optional table)
  10535. "Escape charaters in TEXT that are problematic for links."
  10536. (setq table (or table org-link-escape-chars))
  10537. (when text
  10538. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  10539. table "\\|")))
  10540. (while (string-match re text)
  10541. (setq text
  10542. (replace-match
  10543. (cdr (assoc (match-string 0 text) table))
  10544. t t text)))
  10545. text)))
  10546. (defun org-link-unescape (text &optional table)
  10547. "Reverse the action of `org-link-escape'."
  10548. (setq table (or table org-link-escape-chars))
  10549. (when text
  10550. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  10551. table "\\|")))
  10552. (while (string-match re text)
  10553. (setq text
  10554. (replace-match
  10555. (car (rassoc (match-string 0 text) table))
  10556. t t text)))
  10557. text)))
  10558. (defun org-xor (a b)
  10559. "Exclusive or."
  10560. (if a (not b) b))
  10561. (defun org-get-header (header)
  10562. "Find a header field in the current buffer."
  10563. (save-excursion
  10564. (goto-char (point-min))
  10565. (let ((case-fold-search t) s)
  10566. (cond
  10567. ((eq header 'from)
  10568. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  10569. (setq s (match-string 1)))
  10570. (while (string-match "\"" s)
  10571. (setq s (replace-match "" t t s)))
  10572. (if (string-match "[<(].*" s)
  10573. (setq s (replace-match "" t t s))))
  10574. ((eq header 'message-id)
  10575. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  10576. (setq s (match-string 1))))
  10577. ((eq header 'subject)
  10578. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  10579. (setq s (match-string 1)))))
  10580. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  10581. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  10582. s)))
  10583. (defun org-fixup-message-id-for-http (s)
  10584. "Replace special characters in a message id, so it can be used in an http query."
  10585. (while (string-match "<" s)
  10586. (setq s (replace-match "%3C" t t s)))
  10587. (while (string-match ">" s)
  10588. (setq s (replace-match "%3E" t t s)))
  10589. (while (string-match "@" s)
  10590. (setq s (replace-match "%40" t t s)))
  10591. s)
  10592. ;;;###autoload
  10593. (defun org-insert-link-global ()
  10594. "Insert a link like Org-mode does.
  10595. This command can be called in any mode to insert a link in Org-mode syntax."
  10596. (interactive)
  10597. (org-run-like-in-org-mode 'org-insert-link))
  10598. (defun org-insert-link (&optional complete-file)
  10599. "Insert a link. At the prompt, enter the link.
  10600. Completion can be used to select a link previously stored with
  10601. `org-store-link'. When the empty string is entered (i.e. if you just
  10602. press RET at the prompt), the link defaults to the most recently
  10603. stored link. As SPC triggers completion in the minibuffer, you need to
  10604. use M-SPC or C-q SPC to force the insertion of a space character.
  10605. You will also be prompted for a description, and if one is given, it will
  10606. be displayed in the buffer instead of the link.
  10607. If there is already a link at point, this command will allow you to edit link
  10608. and description parts.
  10609. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  10610. selected using completion. The path to the file will be relative to
  10611. the current directory if the file is in the current directory or a
  10612. subdirectory. Otherwise, the link will be the absolute path as
  10613. completed in the minibuffer (i.e. normally ~/path/to/file).
  10614. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  10615. is in the current directory or below.
  10616. With three \\[universal-argument] prefixes, negate the meaning of
  10617. `org-keep-stored-link-after-insertion'."
  10618. (interactive "P")
  10619. (let* ((wcf (current-window-configuration))
  10620. (region (if (org-region-active-p)
  10621. (buffer-substring (region-beginning) (region-end))))
  10622. (remove (and region (list (region-beginning) (region-end))))
  10623. (desc region)
  10624. tmphist ; byte-compile incorrectly complains about this
  10625. link entry file)
  10626. (cond
  10627. ((org-in-regexp org-bracket-link-regexp 1)
  10628. ;; We do have a link at point, and we are going to edit it.
  10629. (setq remove (list (match-beginning 0) (match-end 0)))
  10630. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10631. (setq link (read-string "Link: "
  10632. (org-link-unescape
  10633. (org-match-string-no-properties 1)))))
  10634. ((or (org-in-regexp org-angle-link-re)
  10635. (org-in-regexp org-plain-link-re))
  10636. ;; Convert to bracket link
  10637. (setq remove (list (match-beginning 0) (match-end 0))
  10638. link (read-string "Link: "
  10639. (org-remove-angle-brackets (match-string 0)))))
  10640. ((equal complete-file '(4))
  10641. ;; Completing read for file names.
  10642. (setq file (read-file-name "File: "))
  10643. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10644. (pwd1 (file-name-as-directory (abbreviate-file-name
  10645. (expand-file-name ".")))))
  10646. (cond
  10647. ((equal complete-file '(16))
  10648. (setq link (org-make-link
  10649. "file:"
  10650. (abbreviate-file-name (expand-file-name file)))))
  10651. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10652. (setq link (org-make-link "file:" (match-string 1 file))))
  10653. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10654. (expand-file-name file))
  10655. (setq link (org-make-link
  10656. "file:" (match-string 1 (expand-file-name file)))))
  10657. (t (setq link (org-make-link "file:" file))))))
  10658. (t
  10659. ;; Read link, with completion for stored links.
  10660. (with-output-to-temp-buffer "*Org Links*"
  10661. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  10662. (when org-stored-links
  10663. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  10664. (princ (mapconcat
  10665. (lambda (x)
  10666. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  10667. (reverse org-stored-links) "\n"))))
  10668. (let ((cw (selected-window)))
  10669. (select-window (get-buffer-window "*Org Links*"))
  10670. (shrink-window-if-larger-than-buffer)
  10671. (setq truncate-lines t)
  10672. (select-window cw))
  10673. ;; Fake a link history, containing the stored links.
  10674. (setq tmphist (append (mapcar 'car org-stored-links)
  10675. org-insert-link-history))
  10676. (unwind-protect
  10677. (setq link (org-completing-read
  10678. "Link: "
  10679. (append
  10680. (mapcar (lambda (x) (list (concat (car x) ":")))
  10681. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  10682. (mapcar (lambda (x) (list (concat x ":")))
  10683. org-link-types))
  10684. nil nil nil
  10685. 'tmphist
  10686. (or (car (car org-stored-links)))))
  10687. (set-window-configuration wcf)
  10688. (kill-buffer "*Org Links*"))
  10689. (setq entry (assoc link org-stored-links))
  10690. (or entry (push link org-insert-link-history))
  10691. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10692. (not org-keep-stored-link-after-insertion))
  10693. (setq org-stored-links (delq (assoc link org-stored-links)
  10694. org-stored-links)))
  10695. (setq desc (or desc (nth 1 entry)))))
  10696. (if (string-match org-plain-link-re link)
  10697. ;; URL-like link, normalize the use of angular brackets.
  10698. (setq link (org-make-link (org-remove-angle-brackets link))))
  10699. ;; Check if we are linking to the current file with a search option
  10700. ;; If yes, simplify the link by using only the search option.
  10701. (when (and buffer-file-name
  10702. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10703. (let* ((path (match-string 1 link))
  10704. (case-fold-search nil)
  10705. (search (match-string 2 link)))
  10706. (save-match-data
  10707. (if (equal (file-truename buffer-file-name) (file-truename path))
  10708. ;; We are linking to this same file, with a search option
  10709. (setq link search)))))
  10710. ;; Check if we can/should use a relative path. If yes, simplify the link
  10711. (when (string-match "\\<file:\\(.*\\)" link)
  10712. (let* ((path (match-string 1 link))
  10713. (origpath path)
  10714. (desc-is-link (equal link desc))
  10715. (case-fold-search nil))
  10716. (cond
  10717. ((eq org-link-file-path-type 'absolute)
  10718. (setq path (abbreviate-file-name (expand-file-name path))))
  10719. ((eq org-link-file-path-type 'noabbrev)
  10720. (setq path (expand-file-name path)))
  10721. ((eq org-link-file-path-type 'relative)
  10722. (setq path (file-relative-name path)))
  10723. (t
  10724. (save-match-data
  10725. (if (string-match (concat "^" (regexp-quote
  10726. (file-name-as-directory
  10727. (expand-file-name "."))))
  10728. (expand-file-name path))
  10729. ;; We are linking a file with relative path name.
  10730. (setq path (substring (expand-file-name path)
  10731. (match-end 0)))))))
  10732. (setq link (concat "file:" path))
  10733. (if (equal desc origpath)
  10734. (setq desc path))))
  10735. (setq desc (read-string "Description: " desc))
  10736. (unless (string-match "\\S-" desc) (setq desc nil))
  10737. (if remove (apply 'delete-region remove))
  10738. (insert (org-make-link-string link desc))))
  10739. (defun org-completing-read (&rest args)
  10740. (let ((minibuffer-local-completion-map
  10741. (copy-keymap minibuffer-local-completion-map)))
  10742. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  10743. (apply 'completing-read args)))
  10744. ;;; Opening/following a link
  10745. (defvar org-link-search-failed nil)
  10746. (defun org-next-link ()
  10747. "Move forward to the next link.
  10748. If the link is in hidden text, expose it."
  10749. (interactive)
  10750. (when (and org-link-search-failed (eq this-command last-command))
  10751. (goto-char (point-min))
  10752. (message "Link search wrapped back to beginning of buffer"))
  10753. (setq org-link-search-failed nil)
  10754. (let* ((pos (point))
  10755. (ct (org-context))
  10756. (a (assoc :link ct)))
  10757. (if a (goto-char (nth 2 a)))
  10758. (if (re-search-forward org-any-link-re nil t)
  10759. (progn
  10760. (goto-char (match-beginning 0))
  10761. (if (org-invisible-p) (org-show-context)))
  10762. (goto-char pos)
  10763. (setq org-link-search-failed t)
  10764. (error "No further link found"))))
  10765. (defun org-previous-link ()
  10766. "Move backward to the previous link.
  10767. If the link is in hidden text, expose it."
  10768. (interactive)
  10769. (when (and org-link-search-failed (eq this-command last-command))
  10770. (goto-char (point-max))
  10771. (message "Link search wrapped back to end of buffer"))
  10772. (setq org-link-search-failed nil)
  10773. (let* ((pos (point))
  10774. (ct (org-context))
  10775. (a (assoc :link ct)))
  10776. (if a (goto-char (nth 1 a)))
  10777. (if (re-search-backward org-any-link-re nil t)
  10778. (progn
  10779. (goto-char (match-beginning 0))
  10780. (if (org-invisible-p) (org-show-context)))
  10781. (goto-char pos)
  10782. (setq org-link-search-failed t)
  10783. (error "No further link found"))))
  10784. (defun org-find-file-at-mouse (ev)
  10785. "Open file link or URL at mouse."
  10786. (interactive "e")
  10787. (mouse-set-point ev)
  10788. (org-open-at-point 'in-emacs))
  10789. (defun org-open-at-mouse (ev)
  10790. "Open file link or URL at mouse."
  10791. (interactive "e")
  10792. (mouse-set-point ev)
  10793. (org-open-at-point))
  10794. (defvar org-window-config-before-follow-link nil
  10795. "The window configuration before following a link.
  10796. This is saved in case the need arises to restore it.")
  10797. (defvar org-open-link-marker (make-marker)
  10798. "Marker pointing to the location where `org-open-at-point; was called.")
  10799. ;;;###autoload
  10800. (defun org-open-at-point-global ()
  10801. "Follow a link like Org-mode does.
  10802. This command can be called in any mode to follow a link that has
  10803. Org-mode syntax."
  10804. (interactive)
  10805. (org-run-like-in-org-mode 'org-open-at-point))
  10806. (defun org-open-at-point (&optional in-emacs)
  10807. "Open link at or after point.
  10808. If there is no link at point, this function will search forward up to
  10809. the end of the current subtree.
  10810. Normally, files will be opened by an appropriate application. If the
  10811. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  10812. (interactive "P")
  10813. (move-marker org-open-link-marker (point))
  10814. (setq org-window-config-before-follow-link (current-window-configuration))
  10815. (org-remove-occur-highlights nil nil t)
  10816. (if (org-at-timestamp-p t)
  10817. (org-follow-timestamp-link)
  10818. (let (type path link line search (pos (point)))
  10819. (catch 'match
  10820. (save-excursion
  10821. (skip-chars-forward "^]\n\r")
  10822. (when (org-in-regexp org-bracket-link-regexp)
  10823. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  10824. (while (string-match " *\n *" link)
  10825. (setq link (replace-match " " t t link)))
  10826. (setq link (org-link-expand-abbrev link))
  10827. (if (string-match org-link-re-with-space2 link)
  10828. (setq type (match-string 1 link) path (match-string 2 link))
  10829. (setq type "thisfile" path link))
  10830. (throw 'match t)))
  10831. (when (get-text-property (point) 'org-linked-text)
  10832. (setq type "thisfile"
  10833. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  10834. (1+ (point)) (point))
  10835. path (buffer-substring
  10836. (previous-single-property-change pos 'org-linked-text)
  10837. (next-single-property-change pos 'org-linked-text)))
  10838. (throw 'match t))
  10839. (save-excursion
  10840. (when (or (org-in-regexp org-angle-link-re)
  10841. (org-in-regexp org-plain-link-re))
  10842. (setq type (match-string 1) path (match-string 2))
  10843. (throw 'match t)))
  10844. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  10845. (setq type "tree-match"
  10846. path (match-string 1))
  10847. (throw 'match t))
  10848. (save-excursion
  10849. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  10850. (setq type "tags"
  10851. path (match-string 1))
  10852. (while (string-match ":" path)
  10853. (setq path (replace-match "+" t t path)))
  10854. (throw 'match t))))
  10855. (unless path
  10856. (error "No link found"))
  10857. ;; Remove any trailing spaces in path
  10858. (if (string-match " +\\'" path)
  10859. (setq path (replace-match "" t t path)))
  10860. (cond
  10861. ((assoc type org-link-protocols)
  10862. (funcall (nth 1 (assoc type org-link-protocols)) path))
  10863. ((equal type "mailto")
  10864. (let ((cmd (car org-link-mailto-program))
  10865. (args (cdr org-link-mailto-program)) args1
  10866. (address path) (subject "") a)
  10867. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  10868. (setq address (match-string 1 path)
  10869. subject (org-link-escape (match-string 2 path))))
  10870. (while args
  10871. (cond
  10872. ((not (stringp (car args))) (push (pop args) args1))
  10873. (t (setq a (pop args))
  10874. (if (string-match "%a" a)
  10875. (setq a (replace-match address t t a)))
  10876. (if (string-match "%s" a)
  10877. (setq a (replace-match subject t t a)))
  10878. (push a args1))))
  10879. (apply cmd (nreverse args1))))
  10880. ((member type '("http" "https" "ftp" "news"))
  10881. (browse-url (concat type ":" (org-link-escape
  10882. path org-link-escape-chars-browser))))
  10883. ((string= type "tags")
  10884. (org-tags-view in-emacs path))
  10885. ((string= type "thisfile")
  10886. (if in-emacs
  10887. (switch-to-buffer-other-window
  10888. (org-get-buffer-for-internal-link (current-buffer)))
  10889. (org-mark-ring-push))
  10890. (let ((cmd `(org-link-search
  10891. ,path
  10892. ,(cond ((equal in-emacs '(4)) 'occur)
  10893. ((equal in-emacs '(16)) 'org-occur)
  10894. (t nil))
  10895. ,pos)))
  10896. (condition-case nil (eval cmd)
  10897. (error (progn (widen) (eval cmd))))))
  10898. ((string= type "tree-match")
  10899. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  10900. ((string= type "file")
  10901. (if (string-match "::\\([0-9]+\\)\\'" path)
  10902. (setq line (string-to-number (match-string 1 path))
  10903. path (substring path 0 (match-beginning 0)))
  10904. (if (string-match "::\\(.+\\)\\'" path)
  10905. (setq search (match-string 1 path)
  10906. path (substring path 0 (match-beginning 0)))))
  10907. (org-open-file path in-emacs line search))
  10908. ((string= type "news")
  10909. (org-follow-gnus-link path))
  10910. ((string= type "bbdb")
  10911. (org-follow-bbdb-link path))
  10912. ((string= type "info")
  10913. (org-follow-info-link path))
  10914. ((string= type "gnus")
  10915. (let (group article)
  10916. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10917. (error "Error in Gnus link"))
  10918. (setq group (match-string 1 path)
  10919. article (match-string 3 path))
  10920. (org-follow-gnus-link group article)))
  10921. ((string= type "vm")
  10922. (let (folder article)
  10923. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10924. (error "Error in VM link"))
  10925. (setq folder (match-string 1 path)
  10926. article (match-string 3 path))
  10927. ;; in-emacs is the prefix arg, will be interpreted as read-only
  10928. (org-follow-vm-link folder article in-emacs)))
  10929. ((string= type "wl")
  10930. (let (folder article)
  10931. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10932. (error "Error in Wanderlust link"))
  10933. (setq folder (match-string 1 path)
  10934. article (match-string 3 path))
  10935. (org-follow-wl-link folder article)))
  10936. ((string= type "mhe")
  10937. (let (folder article)
  10938. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10939. (error "Error in MHE link"))
  10940. (setq folder (match-string 1 path)
  10941. article (match-string 3 path))
  10942. (org-follow-mhe-link folder article)))
  10943. ((string= type "rmail")
  10944. (let (folder article)
  10945. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10946. (error "Error in RMAIL link"))
  10947. (setq folder (match-string 1 path)
  10948. article (match-string 3 path))
  10949. (org-follow-rmail-link folder article)))
  10950. ((string= type "shell")
  10951. (let ((cmd path))
  10952. ;; The following is only for backward compatibility
  10953. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  10954. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  10955. (if (or (not org-confirm-shell-link-function)
  10956. (funcall org-confirm-shell-link-function
  10957. (format "Execute \"%s\" in shell? "
  10958. (org-add-props cmd nil
  10959. 'face 'org-warning))))
  10960. (progn
  10961. (message "Executing %s" cmd)
  10962. (shell-command cmd))
  10963. (error "Abort"))))
  10964. ((string= type "elisp")
  10965. (let ((cmd path))
  10966. (if (or (not org-confirm-elisp-link-function)
  10967. (funcall org-confirm-elisp-link-function
  10968. (format "Execute \"%s\" as elisp? "
  10969. (org-add-props cmd nil
  10970. 'face 'org-warning))))
  10971. (message "%s => %s" cmd (eval (read cmd)))
  10972. (error "Abort"))))
  10973. (t
  10974. (browse-url-at-point)))))
  10975. (move-marker org-open-link-marker nil))
  10976. ;;; File search
  10977. (defvar org-create-file-search-functions nil
  10978. "List of functions to construct the right search string for a file link.
  10979. These functions are called in turn with point at the location to
  10980. which the link should point.
  10981. A function in the hook should first test if it would like to
  10982. handle this file type, for example by checking the major-mode or
  10983. the file extension. If it decides not to handle this file, it
  10984. should just return nil to give other functions a chance. If it
  10985. does handle the file, it must return the search string to be used
  10986. when following the link. The search string will be part of the
  10987. file link, given after a double colon, and `org-open-at-point'
  10988. will automatically search for it. If special measures must be
  10989. taken to make the search successful, another function should be
  10990. added to the companion hook `org-execute-file-search-functions',
  10991. which see.
  10992. A function in this hook may also use `setq' to set the variable
  10993. `description' to provide a suggestion for the descriptive text to
  10994. be used for this link when it gets inserted into an Org-mode
  10995. buffer with \\[org-insert-link].")
  10996. (defvar org-execute-file-search-functions nil
  10997. "List of functions to execute a file search triggered by a link.
  10998. Functions added to this hook must accept a single argument, the
  10999. search string that was part of the file link, the part after the
  11000. double colon. The function must first check if it would like to
  11001. handle this search, for example by checking the major-mode or the
  11002. file extension. If it decides not to handle this search, it
  11003. should just return nil to give other functions a chance. If it
  11004. does handle the search, it must return a non-nil value to keep
  11005. other functions from trying.
  11006. Each function can access the current prefix argument through the
  11007. variable `current-prefix-argument'. Note that a single prefix is
  11008. used to force opening a link in Emacs, so it may be good to only
  11009. use a numeric or double prefix to guide the search function.
  11010. In case this is needed, a function in this hook can also restore
  11011. the window configuration before `org-open-at-point' was called using:
  11012. (set-window-configuration org-window-config-before-follow-link)")
  11013. (defun org-link-search (s &optional type avoid-pos)
  11014. "Search for a link search option.
  11015. If S is surrounded by forward slashes, it is interpreted as a
  11016. regular expression. In org-mode files, this will create an `org-occur'
  11017. sparse tree. In ordinary files, `occur' will be used to list matches.
  11018. If the current buffer is in `dired-mode', grep will be used to search
  11019. in all files. If AVOID-POS is given, ignore matches near that position."
  11020. (let ((case-fold-search t)
  11021. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11022. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11023. (append '(("") (" ") ("\t") ("\n"))
  11024. org-emphasis-alist)
  11025. "\\|") "\\)"))
  11026. (pos (point))
  11027. (pre "") (post "")
  11028. words re0 re1 re2 re3 re4 re5 re2a reall)
  11029. (cond
  11030. ;; First check if there are any special
  11031. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11032. ;; Now try the builtin stuff
  11033. ((save-excursion
  11034. (goto-char (point-min))
  11035. (and
  11036. (re-search-forward
  11037. (concat "<<" (regexp-quote s0) ">>") nil t)
  11038. (setq pos (match-beginning 0))))
  11039. ;; There is an exact target for this
  11040. (goto-char pos))
  11041. ((string-match "^/\\(.*\\)/$" s)
  11042. ;; A regular expression
  11043. (cond
  11044. ((org-mode-p)
  11045. (org-occur (match-string 1 s)))
  11046. ;;((eq major-mode 'dired-mode)
  11047. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11048. (t (org-do-occur (match-string 1 s)))))
  11049. (t
  11050. ;; A normal search strings
  11051. (when (equal (string-to-char s) ?*)
  11052. ;; Anchor on headlines, post may include tags.
  11053. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11054. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11055. s (substring s 1)))
  11056. (remove-text-properties
  11057. 0 (length s)
  11058. '(face nil mouse-face nil keymap nil fontified nil) s)
  11059. ;; Make a series of regular expressions to find a match
  11060. (setq words (org-split-string s "[ \n\r\t]+")
  11061. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11062. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11063. "\\)" markers)
  11064. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11065. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11066. re1 (concat pre re2 post)
  11067. re3 (concat pre re4 post)
  11068. re5 (concat pre ".*" re4)
  11069. re2 (concat pre re2)
  11070. re2a (concat pre re2a)
  11071. re4 (concat pre re4)
  11072. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11073. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11074. re5 "\\)"
  11075. ))
  11076. (cond
  11077. ((eq type 'org-occur) (org-occur reall))
  11078. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11079. (t (goto-char (point-min))
  11080. (if (or (org-search-not-self 1 re0 nil t)
  11081. (org-search-not-self 1 re1 nil t)
  11082. (org-search-not-self 1 re2 nil t)
  11083. (org-search-not-self 1 re2a nil t)
  11084. (org-search-not-self 1 re3 nil t)
  11085. (org-search-not-self 1 re4 nil t)
  11086. (org-search-not-self 1 re5 nil t)
  11087. )
  11088. (goto-char (match-beginning 1))
  11089. (goto-char pos)
  11090. (error "No match")))))
  11091. (t
  11092. ;; Normal string-search
  11093. (goto-char (point-min))
  11094. (if (search-forward s nil t)
  11095. (goto-char (match-beginning 0))
  11096. (error "No match"))))
  11097. (and (org-mode-p) (org-show-context 'link-search))))
  11098. (defun org-search-not-self (group &rest args)
  11099. "Execute `re-search-forward', but only accept matches that do not
  11100. enclose the position of `org-open-link-marker'."
  11101. (let ((m org-open-link-marker))
  11102. (catch 'exit
  11103. (while (apply 're-search-forward args)
  11104. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11105. (goto-char (match-end group))
  11106. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11107. (> (match-beginning 0) (marker-position m))
  11108. (< (match-end 0) (marker-position m)))
  11109. (save-match-data
  11110. (or (not (org-in-regexp
  11111. org-bracket-link-analytic-regexp 1))
  11112. (not (match-end 4)) ; no description
  11113. (and (<= (match-beginning 4) (point))
  11114. (>= (match-end 4) (point))))))
  11115. (throw 'exit (point))))))))
  11116. (defun org-get-buffer-for-internal-link (buffer)
  11117. "Return a buffer to be used for displaying the link target of internal links."
  11118. (cond
  11119. ((not org-display-internal-link-with-indirect-buffer)
  11120. buffer)
  11121. ((string-match "(Clone)$" (buffer-name buffer))
  11122. (message "Buffer is already a clone, not making another one")
  11123. ;; we also do not modify visibility in this case
  11124. buffer)
  11125. (t ; make a new indirect buffer for displaying the link
  11126. (let* ((bn (buffer-name buffer))
  11127. (ibn (concat bn "(Clone)"))
  11128. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11129. (with-current-buffer ib (org-overview))
  11130. ib))))
  11131. (defun org-do-occur (regexp &optional cleanup)
  11132. "Call the Emacs command `occur'.
  11133. If CLEANUP is non-nil, remove the printout of the regular expression
  11134. in the *Occur* buffer. This is useful if the regex is long and not useful
  11135. to read."
  11136. (occur regexp)
  11137. (when cleanup
  11138. (let ((cwin (selected-window)) win beg end)
  11139. (when (setq win (get-buffer-window "*Occur*"))
  11140. (select-window win))
  11141. (goto-char (point-min))
  11142. (when (re-search-forward "match[a-z]+" nil t)
  11143. (setq beg (match-end 0))
  11144. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11145. (setq end (1- (match-beginning 0)))))
  11146. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11147. (goto-char (point-min))
  11148. (select-window cwin))))
  11149. ;;; The mark ring for links jumps
  11150. (defvar org-mark-ring nil
  11151. "Mark ring for positions before jumps in Org-mode.")
  11152. (defvar org-mark-ring-last-goto nil
  11153. "Last position in the mark ring used to go back.")
  11154. ;; Fill and close the ring
  11155. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11156. (loop for i from 1 to org-mark-ring-length do
  11157. (push (make-marker) org-mark-ring))
  11158. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11159. org-mark-ring)
  11160. (defun org-mark-ring-push (&optional pos buffer)
  11161. "Put the current position or POS into the mark ring and rotate it."
  11162. (interactive)
  11163. (setq pos (or pos (point)))
  11164. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11165. (move-marker (car org-mark-ring)
  11166. (or pos (point))
  11167. (or buffer (current-buffer)))
  11168. (message
  11169. (substitute-command-keys
  11170. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11171. (defun org-mark-ring-goto (&optional n)
  11172. "Jump to the previous position in the mark ring.
  11173. With prefix arg N, jump back that many stored positions. When
  11174. called several times in succession, walk through the entire ring.
  11175. Org-mode commands jumping to a different position in the current file,
  11176. or to another Org-mode file, automatically push the old position
  11177. onto the ring."
  11178. (interactive "p")
  11179. (let (p m)
  11180. (if (eq last-command this-command)
  11181. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11182. (setq p org-mark-ring))
  11183. (setq org-mark-ring-last-goto p)
  11184. (setq m (car p))
  11185. (switch-to-buffer (marker-buffer m))
  11186. (goto-char m)
  11187. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11188. (defun org-remove-angle-brackets (s)
  11189. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11190. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11191. s)
  11192. (defun org-add-angle-brackets (s)
  11193. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11194. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11195. s)
  11196. ;;; Following specific links
  11197. (defun org-follow-timestamp-link ()
  11198. (cond
  11199. ((org-at-date-range-p t)
  11200. (let ((org-agenda-start-on-weekday)
  11201. (t1 (match-string 1))
  11202. (t2 (match-string 2)))
  11203. (setq t1 (time-to-days (org-time-string-to-time t1))
  11204. t2 (time-to-days (org-time-string-to-time t2)))
  11205. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11206. ((org-at-timestamp-p t)
  11207. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11208. (substring (match-string 1) 0 10)))
  11209. 1))
  11210. (t (error "This should not happen"))))
  11211. (defun org-follow-bbdb-link (name)
  11212. "Follow a BBDB link to NAME."
  11213. (require 'bbdb)
  11214. (let ((inhibit-redisplay (not debug-on-error))
  11215. (bbdb-electric-p nil))
  11216. (catch 'exit
  11217. ;; Exact match on name
  11218. (bbdb-name (concat "\\`" name "\\'") nil)
  11219. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11220. ;; Exact match on name
  11221. (bbdb-company (concat "\\`" name "\\'") nil)
  11222. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11223. ;; Partial match on name
  11224. (bbdb-name name nil)
  11225. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11226. ;; Partial match on company
  11227. (bbdb-company name nil)
  11228. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11229. ;; General match including network address and notes
  11230. (bbdb name nil)
  11231. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11232. (delete-window (get-buffer-window "*BBDB*"))
  11233. (error "No matching BBDB record")))))
  11234. (defun org-follow-info-link (name)
  11235. "Follow an info file & node link to NAME."
  11236. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11237. (string-match "\\(.*\\)" name))
  11238. (progn
  11239. (require 'info)
  11240. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11241. (Info-find-node (match-string 1 name) (match-string 2 name))
  11242. (Info-find-node (match-string 1 name) "Top")))
  11243. (message (concat "Could not open: " name))))
  11244. (defun org-follow-gnus-link (&optional group article)
  11245. "Follow a Gnus link to GROUP and ARTICLE."
  11246. (require 'gnus)
  11247. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11248. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11249. (cond ((and group article)
  11250. (gnus-group-read-group 1 nil group)
  11251. (gnus-summary-goto-article (string-to-number article) nil t))
  11252. (group (gnus-group-jump-to-group group))))
  11253. (defun org-follow-vm-link (&optional folder article readonly)
  11254. "Follow a VM link to FOLDER and ARTICLE."
  11255. (require 'vm)
  11256. (setq article (org-add-angle-brackets article))
  11257. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11258. ;; ange-ftp or efs or tramp access
  11259. (let ((user (or (match-string 1 folder) (user-login-name)))
  11260. (host (match-string 2 folder))
  11261. (file (match-string 3 folder)))
  11262. (cond
  11263. ((featurep 'tramp)
  11264. ;; use tramp to access the file
  11265. (if (featurep 'xemacs)
  11266. (setq folder (format "[%s@%s]%s" user host file))
  11267. (setq folder (format "/%s@%s:%s" user host file))))
  11268. (t
  11269. ;; use ange-ftp or efs
  11270. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11271. (setq folder (format "/%s@%s:%s" user host file))))))
  11272. (when folder
  11273. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11274. (sit-for 0.1)
  11275. (when article
  11276. (vm-select-folder-buffer)
  11277. (widen)
  11278. (let ((case-fold-search t))
  11279. (goto-char (point-min))
  11280. (if (not (re-search-forward
  11281. (concat "^" "message-id: *" (regexp-quote article))))
  11282. (error "Could not find the specified message in this folder"))
  11283. (vm-isearch-update)
  11284. (vm-isearch-narrow)
  11285. (vm-beginning-of-message)
  11286. (vm-summarize)))))
  11287. (defun org-follow-wl-link (folder article)
  11288. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11289. (if (and (string= folder "%")
  11290. article
  11291. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11292. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11293. ;; Thus, we recompose folder and article ids.
  11294. (setq folder (format "%s#%s" folder (match-string 1 article))
  11295. article (match-string 3 article)))
  11296. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11297. (error "No such folder: %s" folder))
  11298. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11299. (and article
  11300. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11301. (wl-summary-redisplay)))
  11302. (defun org-follow-rmail-link (folder article)
  11303. "Follow an RMAIL link to FOLDER and ARTICLE."
  11304. (setq article (org-add-angle-brackets article))
  11305. (let (message-number)
  11306. (save-excursion
  11307. (save-window-excursion
  11308. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11309. (setq message-number
  11310. (save-restriction
  11311. (widen)
  11312. (goto-char (point-max))
  11313. (if (re-search-backward
  11314. (concat "^Message-ID:\\s-+" (regexp-quote
  11315. (or article "")))
  11316. nil t)
  11317. (rmail-what-message))))))
  11318. (if message-number
  11319. (progn
  11320. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11321. (rmail-show-message message-number)
  11322. message-number)
  11323. (error "Message not found"))))
  11324. ;;; mh-e integration based on planner-mode
  11325. (defun org-mhe-get-message-real-folder ()
  11326. "Return the name of the current message real folder, so if you use
  11327. sequences, it will now work."
  11328. (save-excursion
  11329. (let* ((folder
  11330. (if (equal major-mode 'mh-folder-mode)
  11331. mh-current-folder
  11332. ;; Refer to the show buffer
  11333. mh-show-folder-buffer))
  11334. (end-index
  11335. (if (boundp 'mh-index-folder)
  11336. (min (length mh-index-folder) (length folder))))
  11337. )
  11338. ;; a simple test on mh-index-data does not work, because
  11339. ;; mh-index-data is always nil in a show buffer.
  11340. (if (and (boundp 'mh-index-folder)
  11341. (string= mh-index-folder (substring folder 0 end-index)))
  11342. (if (equal major-mode 'mh-show-mode)
  11343. (save-window-excursion
  11344. (let (pop-up-frames)
  11345. (when (buffer-live-p (get-buffer folder))
  11346. (progn
  11347. (pop-to-buffer folder)
  11348. (org-mhe-get-message-folder-from-index)
  11349. )
  11350. )))
  11351. (org-mhe-get-message-folder-from-index)
  11352. )
  11353. folder
  11354. )
  11355. )))
  11356. (defun org-mhe-get-message-folder-from-index ()
  11357. "Returns the name of the message folder in a index folder buffer."
  11358. (save-excursion
  11359. (mh-index-previous-folder)
  11360. (re-search-forward "^\\(+.*\\)$" nil t)
  11361. (message (match-string 1))))
  11362. (defun org-mhe-get-message-folder ()
  11363. "Return the name of the current message folder. Be careful if you
  11364. use sequences."
  11365. (save-excursion
  11366. (if (equal major-mode 'mh-folder-mode)
  11367. mh-current-folder
  11368. ;; Refer to the show buffer
  11369. mh-show-folder-buffer)))
  11370. (defun org-mhe-get-message-num ()
  11371. "Return the number of the current message. Be careful if you
  11372. use sequences."
  11373. (save-excursion
  11374. (if (equal major-mode 'mh-folder-mode)
  11375. (mh-get-msg-num nil)
  11376. ;; Refer to the show buffer
  11377. (mh-show-buffer-message-number))))
  11378. (defun org-mhe-get-header (header)
  11379. "Return a header of the message in folder mode. This will create a
  11380. show buffer for the corresponding message. If you have a more clever
  11381. idea..."
  11382. (let* ((folder (org-mhe-get-message-folder))
  11383. (num (org-mhe-get-message-num))
  11384. (buffer (get-buffer-create (concat "show-" folder)))
  11385. (header-field))
  11386. (with-current-buffer buffer
  11387. (mh-display-msg num folder)
  11388. (if (equal major-mode 'mh-folder-mode)
  11389. (mh-header-display)
  11390. (mh-show-header-display))
  11391. (set-buffer buffer)
  11392. (setq header-field (mh-get-header-field header))
  11393. (if (equal major-mode 'mh-folder-mode)
  11394. (mh-show)
  11395. (mh-show-show))
  11396. header-field)))
  11397. (defun org-follow-mhe-link (folder article)
  11398. "Follow an MHE link to FOLDER and ARTICLE.
  11399. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11400. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11401. ARTICLE is searched in all folders. Indexed searches (swish++,
  11402. namazu, and others supported by MH-E) will always search in all
  11403. folders."
  11404. (require 'mh-e)
  11405. (require 'mh-search)
  11406. (require 'mh-utils)
  11407. (mh-find-path)
  11408. (if (not article)
  11409. (mh-visit-folder (mh-normalize-folder-name folder))
  11410. (setq article (org-add-angle-brackets article))
  11411. (mh-search-choose)
  11412. (if (equal mh-searcher 'pick)
  11413. (progn
  11414. (mh-search folder (list "--message-id" article))
  11415. (when (and org-mhe-search-all-folders
  11416. (not (org-mhe-get-message-real-folder)))
  11417. (kill-this-buffer)
  11418. (mh-search "+" (list "--message-id" article))))
  11419. (mh-search "+" article))
  11420. (if (org-mhe-get-message-real-folder)
  11421. (mh-show-msg 1)
  11422. (kill-this-buffer)
  11423. (error "Message not found"))))
  11424. ;;; BibTeX links
  11425. ;; Use the custom search meachnism to construct and use search strings for
  11426. ;; file links to BibTeX database entries.
  11427. (defun org-create-file-search-in-bibtex ()
  11428. "Create the search string and description for a BibTeX database entry."
  11429. (when (eq major-mode 'bibtex-mode)
  11430. ;; yes, we want to construct this search string.
  11431. ;; Make a good description for this entry, using names, year and the title
  11432. ;; Put it into the `description' variable which is dynamically scoped.
  11433. (let ((bibtex-autokey-names 1)
  11434. (bibtex-autokey-names-stretch 1)
  11435. (bibtex-autokey-name-case-convert-function 'identity)
  11436. (bibtex-autokey-name-separator " & ")
  11437. (bibtex-autokey-additional-names " et al.")
  11438. (bibtex-autokey-year-length 4)
  11439. (bibtex-autokey-name-year-separator " ")
  11440. (bibtex-autokey-titlewords 3)
  11441. (bibtex-autokey-titleword-separator " ")
  11442. (bibtex-autokey-titleword-case-convert-function 'identity)
  11443. (bibtex-autokey-titleword-length 'infty)
  11444. (bibtex-autokey-year-title-separator ": "))
  11445. (setq description (bibtex-generate-autokey)))
  11446. ;; Now parse the entry, get the key and return it.
  11447. (save-excursion
  11448. (bibtex-beginning-of-entry)
  11449. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11450. (defun org-execute-file-search-in-bibtex (s)
  11451. "Find the link search string S as a key for a database entry."
  11452. (when (eq major-mode 'bibtex-mode)
  11453. ;; Yes, we want to do the search in this file.
  11454. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11455. (goto-char (point-min))
  11456. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11457. (regexp-quote s) "[ \t\n]*,") nil t)
  11458. (goto-char (match-beginning 0)))
  11459. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11460. ;; Use double prefix to indicate that any web link should be browsed
  11461. (let ((b (current-buffer)) (p (point)))
  11462. ;; Restore the window configuration because we just use the web link
  11463. (set-window-configuration org-window-config-before-follow-link)
  11464. (save-excursion (set-buffer b) (goto-char p)
  11465. (bibtex-url)))
  11466. (recenter 0)) ; Move entry start to beginning of window
  11467. ;; return t to indicate that the search is done.
  11468. t))
  11469. ;; Finally add the functions to the right hooks.
  11470. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11471. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11472. ;; end of Bibtex link setup
  11473. ;;; Following file links
  11474. (defun org-open-file (path &optional in-emacs line search)
  11475. "Open the file at PATH.
  11476. First, this expands any special file name abbreviations. Then the
  11477. configuration variable `org-file-apps' is checked if it contains an
  11478. entry for this file type, and if yes, the corresponding command is launched.
  11479. If no application is found, Emacs simply visits the file.
  11480. With optional argument IN-EMACS, Emacs will visit the file.
  11481. Optional LINE specifies a line to go to, optional SEARCH a string to
  11482. search for. If LINE or SEARCH is given, the file will always be
  11483. opened in Emacs.
  11484. If the file does not exist, an error is thrown."
  11485. (setq in-emacs (or in-emacs line search))
  11486. (let* ((file (if (equal path "")
  11487. buffer-file-name
  11488. (substitute-in-file-name (expand-file-name path))))
  11489. (apps (append org-file-apps (org-default-apps)))
  11490. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  11491. (dirp (if remp nil (file-directory-p file)))
  11492. (dfile (downcase file))
  11493. (old-buffer (current-buffer))
  11494. (old-pos (point))
  11495. (old-mode major-mode)
  11496. ext cmd)
  11497. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  11498. (setq ext (match-string 1 dfile))
  11499. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  11500. (setq ext (match-string 1 dfile))))
  11501. (if in-emacs
  11502. (setq cmd 'emacs)
  11503. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  11504. (and dirp (cdr (assoc 'directory apps)))
  11505. (cdr (assoc ext apps))
  11506. (cdr (assoc t apps)))))
  11507. (when (eq cmd 'mailcap)
  11508. (require 'mailcap)
  11509. (mailcap-parse-mailcaps)
  11510. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  11511. (command (mailcap-mime-info mime-type)))
  11512. (if (stringp command)
  11513. (setq cmd command)
  11514. (setq cmd 'emacs))))
  11515. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  11516. (not (file-exists-p file))
  11517. (not org-open-non-existing-files))
  11518. (error "No such file: %s" file))
  11519. (cond
  11520. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  11521. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  11522. (if (string-match "['\"]%s['\"]" cmd)
  11523. (setq cmd (replace-match "%s" t t cmd)))
  11524. (setq cmd (format cmd (shell-quote-argument file)))
  11525. (save-window-excursion
  11526. (start-process-shell-command cmd nil cmd)))
  11527. ((or (stringp cmd)
  11528. (eq cmd 'emacs))
  11529. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  11530. (widen)
  11531. (if line (goto-line line)
  11532. (if search (org-link-search search))))
  11533. ((consp cmd)
  11534. (eval cmd))
  11535. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  11536. (and (org-mode-p) (eq old-mode 'org-mode)
  11537. (or (not (equal old-buffer (current-buffer)))
  11538. (not (equal old-pos (point))))
  11539. (org-mark-ring-push old-pos old-buffer))))
  11540. (defun org-default-apps ()
  11541. "Return the default applications for this operating system."
  11542. (cond
  11543. ((eq system-type 'darwin)
  11544. org-file-apps-defaults-macosx)
  11545. ((eq system-type 'windows-nt)
  11546. org-file-apps-defaults-windowsnt)
  11547. (t org-file-apps-defaults-gnu)))
  11548. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  11549. (defun org-file-remote-p (file)
  11550. "Test whether FILE specifies a location on a remote system.
  11551. Return non-nil if the location is indeed remote.
  11552. For example, the filename \"/user@host:/foo\" specifies a location
  11553. on the system \"/user@host:\"."
  11554. (cond ((fboundp 'file-remote-p)
  11555. (file-remote-p file))
  11556. ((fboundp 'tramp-handle-file-remote-p)
  11557. (tramp-handle-file-remote-p file))
  11558. ((and (boundp 'ange-ftp-name-format)
  11559. (string-match (car ange-ftp-name-format) file))
  11560. t)
  11561. (t nil)))
  11562. ;;;; Hooks for remember.el
  11563. ;;;###autoload
  11564. (defun org-remember-annotation ()
  11565. "Return a link to the current location as an annotation for remember.el.
  11566. If you are using Org-mode files as target for data storage with
  11567. remember.el, then the annotations should include a link compatible with the
  11568. conventions in Org-mode. This function returns such a link."
  11569. (org-store-link nil))
  11570. (defconst org-remember-help
  11571. "Select a destination location for the note.
  11572. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  11573. RET on headline -> Store as sublevel entry to current headline
  11574. RET at beg-of-buf -> Append to file as level 2 headline
  11575. <left>/<right> -> before/after current headline, same headings level")
  11576. (defvar org-remember-previous-location nil)
  11577. (defvar org-force-remember-template-char) ;; dynamically scoped
  11578. ;;;###autoload
  11579. (defun org-remember-apply-template (&optional use-char skip-interactive)
  11580. "Initialize *remember* buffer with template, invoke `org-mode'.
  11581. This function should be placed into `remember-mode-hook' and in fact requires
  11582. to be run from that hook to fucntion properly."
  11583. (if org-remember-templates
  11584. (let* ((templates (mapcar (lambda (x)
  11585. (if (stringp (car x))
  11586. (append (list (nth 1 x) (car x)) (cddr x))
  11587. (append (list (car x) "") (cdr x))))
  11588. org-remember-templates))
  11589. (char (or use-char
  11590. (cond
  11591. ((= (length templates) 1)
  11592. (caar templates))
  11593. ((and (boundp 'org-force-remember-template-char)
  11594. org-force-remember-template-char)
  11595. (if (stringp org-force-remember-template-char)
  11596. (string-to-char org-force-remember-template-char)
  11597. org-force-remember-template-char))
  11598. (t
  11599. (message "Select template: %s"
  11600. (mapconcat
  11601. (lambda (x)
  11602. (cond
  11603. ((not (string-match "\\S-" (nth 1 x)))
  11604. (format "[%c]" (car x)))
  11605. ((equal (downcase (car x))
  11606. (downcase (aref (nth 1 x) 0)))
  11607. (format "[%c]%s" (car x) (substring (nth 1 x) 1)))
  11608. (t (format "[%c]%s" (car x) (nth 1 x)))))
  11609. templates " "))
  11610. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  11611. (when (equal char0 ?\C-g)
  11612. (jump-to-register remember-register)
  11613. (kill-buffer remember-buffer))
  11614. char0)))))
  11615. (entry (cddr (assoc char templates)))
  11616. (tpl (car entry))
  11617. (plist-p (if org-store-link-plist t nil))
  11618. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  11619. (string-match "\\S-" (nth 1 entry)))
  11620. (nth 1 entry)
  11621. org-default-notes-file))
  11622. (headline (nth 2 entry))
  11623. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  11624. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  11625. (v-u (concat "[" (substring v-t 1 -1) "]"))
  11626. (v-U (concat "[" (substring v-T 1 -1) "]"))
  11627. ;; `initial' and `annotation' are bound in `remember'
  11628. (v-i (if (boundp 'initial) initial))
  11629. (v-a (if (and (boundp 'annotation) annotation)
  11630. (if (equal annotation "[[]]") "" annotation)
  11631. ""))
  11632. (v-A (if (and v-a
  11633. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  11634. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  11635. v-a))
  11636. (v-n user-full-name)
  11637. (org-startup-folded nil)
  11638. org-time-was-given org-end-time-was-given x prompt char time)
  11639. (setq org-store-link-plist
  11640. (append (list :annotation v-a :initial v-i)
  11641. org-store-link-plist))
  11642. (unless tpl (setq tpl "") (message "No template") (ding))
  11643. (erase-buffer)
  11644. (insert (substitute-command-keys
  11645. (format
  11646. "## Filing location: Select interactively, default, or last used:
  11647. ## %s to select file and header location interactively.
  11648. ## %s \"%s\" -> \"* %s\"
  11649. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  11650. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  11651. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  11652. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  11653. (abbreviate-file-name (or file org-default-notes-file))
  11654. (or headline "")
  11655. (or (car org-remember-previous-location) "???")
  11656. (or (cdr org-remember-previous-location) "???"))))
  11657. (insert tpl) (goto-char (point-min))
  11658. ;; Simple %-escapes
  11659. (while (re-search-forward "%\\([tTuUaiA]\\)" nil t)
  11660. (when (and initial (equal (match-string 0) "%i"))
  11661. (save-match-data
  11662. (let* ((lead (buffer-substring
  11663. (point-at-bol) (match-beginning 0))))
  11664. (setq v-i (mapconcat 'identity
  11665. (org-split-string initial "\n")
  11666. (concat "\n" lead))))))
  11667. (replace-match
  11668. (or (eval (intern (concat "v-" (match-string 1)))) "")
  11669. t t))
  11670. ;; From the property list
  11671. (when plist-p
  11672. (goto-char (point-min))
  11673. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  11674. (and (setq x (plist-get org-store-link-plist
  11675. (intern (match-string 1))))
  11676. (replace-match x t t))))
  11677. ;; Turn on org-mode in the remember buffer, set local variables
  11678. (org-mode)
  11679. (org-set-local 'org-finish-function 'remember-buffer)
  11680. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  11681. (org-set-local 'org-default-notes-file file))
  11682. (if (and headline (stringp headline) (string-match "\\S-" headline))
  11683. (org-set-local 'org-remember-default-headline headline))
  11684. ;; Interactive template entries
  11685. (goto-char (point-min))
  11686. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  11687. (setq char (if (match-end 3) (match-string 3))
  11688. prompt (if (match-end 2) (match-string 2)))
  11689. (goto-char (match-beginning 0))
  11690. (replace-match "")
  11691. (cond
  11692. ((member char '("G" "g"))
  11693. (let* ((org-last-tags-completion-table
  11694. (org-global-tags-completion-table
  11695. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  11696. (org-add-colon-after-tag-completion t)
  11697. (ins (completing-read
  11698. (if prompt (concat prompt ": ") "Tags: ")
  11699. 'org-tags-completion-function nil nil nil
  11700. 'org-tags-history)))
  11701. (setq ins (mapconcat 'identity
  11702. (org-split-string ins (org-re "[^[:alnum:]]+"))
  11703. ":"))
  11704. (when (string-match "\\S-" ins)
  11705. (or (equal (char-before) ?:) (insert ":"))
  11706. (insert ins)
  11707. (or (equal (char-after) ?:) (insert ":")))))
  11708. (char
  11709. (setq org-time-was-given (equal (upcase char) char))
  11710. (setq time (org-read-date (equal (upcase char) "U") t nil
  11711. prompt))
  11712. (org-insert-time-stamp time org-time-was-given
  11713. (member char '("u" "U"))
  11714. nil nil (list org-end-time-was-given)))
  11715. (t
  11716. (insert (read-string
  11717. (if prompt (concat prompt ": ") "Enter string"))))))
  11718. (goto-char (point-min))
  11719. (if (re-search-forward "%\\?" nil t)
  11720. (replace-match "")
  11721. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  11722. (org-mode)
  11723. (org-set-local 'org-finish-function 'remember-buffer)))
  11724. ;;;###autoload
  11725. (defun org-remember (&optional org-force-remember-template-char)
  11726. "Call `remember'. If this is already a remember buffer, re-apply template.
  11727. If there is an active region, make sure remember uses it as initial content
  11728. of the remember buffer."
  11729. (interactive)
  11730. (if (eq org-finish-function 'remember-buffer)
  11731. (progn
  11732. (when (< (length org-remember-templates) 2)
  11733. (error "No other template available"))
  11734. (erase-buffer)
  11735. (let ((annotation (plist-get org-store-link-plist :annotation))
  11736. (initial (plist-get org-store-link-plist :initial)))
  11737. (org-remember-apply-template))
  11738. (message "Press C-c C-c to remember data"))
  11739. (if (org-region-active-p)
  11740. (remember (buffer-substring (point) (mark)))
  11741. (call-interactively 'remember))))
  11742. (defvar org-note-abort nil) ; dynamically scoped
  11743. ;;;###autoload
  11744. (defun org-remember-handler ()
  11745. "Store stuff from remember.el into an org file.
  11746. First prompts for an org file. If the user just presses return, the value
  11747. of `org-default-notes-file' is used.
  11748. Then the command offers the headings tree of the selected file in order to
  11749. file the text at a specific location.
  11750. You can either immediately press RET to get the note appended to the
  11751. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  11752. find a better place. Then press RET or <left> or <right> in insert the note.
  11753. Key Cursor position Note gets inserted
  11754. -----------------------------------------------------------------------------
  11755. RET buffer-start as level 1 heading at end of file
  11756. RET on headline as sublevel of the heading at cursor
  11757. RET no heading at cursor position, level taken from context.
  11758. Or use prefix arg to specify level manually.
  11759. <left> on headline as same level, before current heading
  11760. <right> on headline as same level, after current heading
  11761. So the fastest way to store the note is to press RET RET to append it to
  11762. the default file. This way your current train of thought is not
  11763. interrupted, in accordance with the principles of remember.el.
  11764. You can also get the fast execution without prompting by using
  11765. C-u C-c C-c to exit the remember buffer. See also the variable
  11766. `org-remember-store-without-prompt'.
  11767. Before being stored away, the function ensures that the text has a
  11768. headline, i.e. a first line that starts with a \"*\". If not, a headline
  11769. is constructed from the current date and some additional data.
  11770. If the variable `org-adapt-indentation' is non-nil, the entire text is
  11771. also indented so that it starts in the same column as the headline
  11772. \(i.e. after the stars).
  11773. See also the variable `org-reverse-note-order'."
  11774. (goto-char (point-min))
  11775. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  11776. (replace-match ""))
  11777. (goto-char (point-max))
  11778. (unless (equal (char-before) ?\n) (insert "\n"))
  11779. (catch 'quit
  11780. (if org-note-abort (throw 'quit nil))
  11781. (let* ((txt (buffer-substring (point-min) (point-max)))
  11782. (fastp (org-xor (equal current-prefix-arg '(4))
  11783. org-remember-store-without-prompt))
  11784. (file (if fastp org-default-notes-file (org-get-org-file)))
  11785. (heading org-remember-default-headline)
  11786. (visiting (org-find-base-buffer-visiting file))
  11787. (org-startup-folded nil)
  11788. (org-startup-align-all-tables nil)
  11789. (org-goto-start-pos 1)
  11790. spos exitcmd level indent reversed)
  11791. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  11792. (setq file (car org-remember-previous-location)
  11793. heading (cdr org-remember-previous-location)))
  11794. (setq current-prefix-arg nil)
  11795. ;; Modify text so that it becomes a nice subtree which can be inserted
  11796. ;; into an org tree.
  11797. (let* ((lines (split-string txt "\n"))
  11798. first)
  11799. (setq first (car lines) lines (cdr lines))
  11800. (if (string-match "^\\*+ " first)
  11801. ;; Is already a headline
  11802. (setq indent nil)
  11803. ;; We need to add a headline: Use time and first buffer line
  11804. (setq lines (cons first lines)
  11805. first (concat "* " (current-time-string)
  11806. " (" (remember-buffer-desc) ")")
  11807. indent " "))
  11808. (if (and org-adapt-indentation indent)
  11809. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  11810. (setq txt (concat first "\n"
  11811. (mapconcat 'identity lines "\n"))))
  11812. ;; Find the file
  11813. (if (not visiting) (find-file-noselect file))
  11814. (with-current-buffer (or visiting (get-file-buffer file))
  11815. (unless (org-mode-p)
  11816. (error "Target files for remember notes must be in Org-mode"))
  11817. (save-excursion
  11818. (save-restriction
  11819. (widen)
  11820. (and (goto-char (point-min))
  11821. (not (re-search-forward "^\\* " nil t))
  11822. (insert "\n* " (or heading "Notes") "\n"))
  11823. (setq reversed (org-notes-order-reversed-p))
  11824. ;; Find the default location
  11825. (when (and heading (stringp heading) (string-match "\\S-" heading))
  11826. (goto-char (point-min))
  11827. (if (re-search-forward
  11828. (concat "^\\*+[ \t]+" (regexp-quote heading)
  11829. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  11830. nil t)
  11831. (setq org-goto-start-pos (match-beginning 0))
  11832. (when fastp
  11833. (goto-char (point-max))
  11834. (unless (bolp) (newline))
  11835. (insert "* " heading "\n")
  11836. (setq org-goto-start-pos (point-at-bol 0)))))
  11837. ;; Ask the User for a location
  11838. (if fastp
  11839. (setq spos org-goto-start-pos
  11840. exitcmd 'return)
  11841. (setq spos (org-get-location (current-buffer) org-remember-help)
  11842. exitcmd (cdr spos)
  11843. spos (car spos)))
  11844. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  11845. ; not handle this note
  11846. (goto-char spos)
  11847. (cond ((org-on-heading-p t)
  11848. (org-back-to-heading t)
  11849. (setq level (funcall outline-level))
  11850. (cond
  11851. ((eq exitcmd 'return)
  11852. ;; sublevel of current
  11853. (setq org-remember-previous-location
  11854. (cons (abbreviate-file-name file)
  11855. (org-get-heading 'notags)))
  11856. (if reversed
  11857. (outline-next-heading)
  11858. (org-end-of-subtree)
  11859. (if (not (bolp))
  11860. (if (looking-at "[ \t]*\n")
  11861. (beginning-of-line 2)
  11862. (end-of-line 1)
  11863. (insert "\n"))))
  11864. (org-paste-subtree (org-get-legal-level level 1) txt))
  11865. ((eq exitcmd 'left)
  11866. ;; before current
  11867. (org-paste-subtree level txt))
  11868. ((eq exitcmd 'right)
  11869. ;; after current
  11870. (org-end-of-subtree t)
  11871. (org-paste-subtree level txt))
  11872. (t (error "This should not happen"))))
  11873. ((and (bobp) (not reversed))
  11874. ;; Put it at the end, one level below level 1
  11875. (save-restriction
  11876. (widen)
  11877. (goto-char (point-max))
  11878. (if (not (bolp)) (newline))
  11879. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  11880. ((and (bobp) reversed)
  11881. ;; Put it at the start, as level 1
  11882. (save-restriction
  11883. (widen)
  11884. (goto-char (point-min))
  11885. (re-search-forward "^\\*+ " nil t)
  11886. (beginning-of-line 1)
  11887. (org-paste-subtree 1 txt)))
  11888. (t
  11889. ;; Put it right there, with automatic level determined by
  11890. ;; org-paste-subtree or from prefix arg
  11891. (org-paste-subtree
  11892. (if (numberp current-prefix-arg) current-prefix-arg)
  11893. txt)))
  11894. (when remember-save-after-remembering
  11895. (save-buffer)
  11896. (if (not visiting) (kill-buffer (current-buffer)))))))))
  11897. t) ;; return t to indicate that we took care of this note.
  11898. (defun org-get-org-file ()
  11899. "Read a filename, with default directory `org-directory'."
  11900. (let ((default (or org-default-notes-file remember-data-file)))
  11901. (read-file-name (format "File name [%s]: " default)
  11902. (file-name-as-directory org-directory)
  11903. default)))
  11904. (defun org-notes-order-reversed-p ()
  11905. "Check if the current file should receive notes in reversed order."
  11906. (cond
  11907. ((not org-reverse-note-order) nil)
  11908. ((eq t org-reverse-note-order) t)
  11909. ((not (listp org-reverse-note-order)) nil)
  11910. (t (catch 'exit
  11911. (let ((all org-reverse-note-order)
  11912. entry)
  11913. (while (setq entry (pop all))
  11914. (if (string-match (car entry) buffer-file-name)
  11915. (throw 'exit (cdr entry))))
  11916. nil)))))
  11917. ;;;; Dynamic blocks
  11918. (defun org-find-dblock (name)
  11919. "Find the first dynamic block with name NAME in the buffer.
  11920. If not found, stay at current position and return nil."
  11921. (let (pos)
  11922. (save-excursion
  11923. (goto-char (point-min))
  11924. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  11925. nil t)
  11926. (match-beginning 0))))
  11927. (if pos (goto-char pos))
  11928. pos))
  11929. (defconst org-dblock-start-re
  11930. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  11931. "Matches the startline of a dynamic block, with parameters.")
  11932. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  11933. "Matches the end of a dyhamic block.")
  11934. (defun org-create-dblock (plist)
  11935. "Create a dynamic block section, with parameters taken from PLIST.
  11936. PLIST must containe a :name entry which is used as name of the block."
  11937. (unless (bolp) (newline))
  11938. (let ((name (plist-get plist :name)))
  11939. (insert "#+BEGIN: " name)
  11940. (while plist
  11941. (if (eq (car plist) :name)
  11942. (setq plist (cddr plist))
  11943. (insert " " (prin1-to-string (pop plist)))))
  11944. (insert "\n\n#+END:\n")
  11945. (beginning-of-line -2)))
  11946. (defun org-prepare-dblock ()
  11947. "Prepare dynamic block for refresh.
  11948. This empties the block, puts the cursor at the insert position and returns
  11949. the property list including an extra property :name with the block name."
  11950. (unless (looking-at org-dblock-start-re)
  11951. (error "Not at a dynamic block"))
  11952. (let* ((begdel (1+ (match-end 0)))
  11953. (name (org-no-properties (match-string 1)))
  11954. (params (append (list :name name)
  11955. (read (concat "(" (match-string 3) ")")))))
  11956. (unless (re-search-forward org-dblock-end-re nil t)
  11957. (error "Dynamic block not terminated"))
  11958. (delete-region begdel (match-beginning 0))
  11959. (goto-char begdel)
  11960. (open-line 1)
  11961. params))
  11962. (defun org-map-dblocks (&optional command)
  11963. "Apply COMMAND to all dynamic blocks in the current buffer.
  11964. If COMMAND is not given, use `org-update-dblock'."
  11965. (let ((cmd (or command 'org-update-dblock))
  11966. pos)
  11967. (save-excursion
  11968. (goto-char (point-min))
  11969. (while (re-search-forward org-dblock-start-re nil t)
  11970. (goto-char (setq pos (match-beginning 0)))
  11971. (condition-case nil
  11972. (funcall cmd)
  11973. (error (message "Error during update of dynamic block")))
  11974. (goto-char pos)
  11975. (unless (re-search-forward org-dblock-end-re nil t)
  11976. (error "Dynamic block not terminated"))))))
  11977. (defun org-dblock-update (&optional arg)
  11978. "User command for updating dynamic blocks.
  11979. Update the dynamic block at point. With prefix ARG, update all dynamic
  11980. blocks in the buffer."
  11981. (interactive "P")
  11982. (if arg
  11983. (org-update-all-dblocks)
  11984. (or (looking-at org-dblock-start-re)
  11985. (org-beginning-of-dblock))
  11986. (org-update-dblock)))
  11987. (defun org-update-dblock ()
  11988. "Update the dynamic block at point
  11989. This means to empty the block, parse for parameters and then call
  11990. the correct writing function."
  11991. (save-window-excursion
  11992. (let* ((pos (point))
  11993. (line (org-current-line))
  11994. (params (org-prepare-dblock))
  11995. (name (plist-get params :name))
  11996. (cmd (intern (concat "org-dblock-write:" name))))
  11997. (message "Updating dynamic block `%s' at line %d..." name line)
  11998. (funcall cmd params)
  11999. (message "Updating dynamic block `%s' at line %d...done" name line)
  12000. (goto-char pos))))
  12001. (defun org-beginning-of-dblock ()
  12002. "Find the beginning of the dynamic block at point.
  12003. Error if there is no scuh block at point."
  12004. (let ((pos (point))
  12005. beg)
  12006. (end-of-line 1)
  12007. (if (and (re-search-backward org-dblock-start-re nil t)
  12008. (setq beg (match-beginning 0))
  12009. (re-search-forward org-dblock-end-re nil t)
  12010. (> (match-end 0) pos))
  12011. (goto-char beg)
  12012. (goto-char pos)
  12013. (error "Not in a dynamic block"))))
  12014. (defun org-update-all-dblocks ()
  12015. "Update all dynamic blocks in the buffer.
  12016. This function can be used in a hook."
  12017. (when (org-mode-p)
  12018. (org-map-dblocks 'org-update-dblock)))
  12019. ;;;; Completion
  12020. (defconst org-additional-option-like-keywords
  12021. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12022. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:"))
  12023. (defun org-complete (&optional arg)
  12024. "Perform completion on word at point.
  12025. At the beginning of a headline, this completes TODO keywords as given in
  12026. `org-todo-keywords'.
  12027. If the current word is preceded by a backslash, completes the TeX symbols
  12028. that are supported for HTML support.
  12029. If the current word is preceded by \"#+\", completes special words for
  12030. setting file options.
  12031. In the line after \"#+STARTUP:, complete valid keywords.\"
  12032. At all other locations, this simply calls the value of
  12033. `org-completion-fallback-command'."
  12034. (interactive "P")
  12035. (org-without-partial-completion
  12036. (catch 'exit
  12037. (let* ((end (point))
  12038. (beg1 (save-excursion
  12039. (skip-chars-backward (org-re "[:alnum:]_@"))
  12040. (point)))
  12041. (beg (save-excursion
  12042. (skip-chars-backward "a-zA-Z0-9_:$")
  12043. (point)))
  12044. (confirm (lambda (x) (stringp (car x))))
  12045. (searchhead (equal (char-before beg) ?*))
  12046. (tag (and (equal (char-before beg1) ?:)
  12047. (equal (char-after (point-at-bol)) ?*)))
  12048. (prop (and (equal (char-before beg1) ?:)
  12049. (not (equal (char-after (point-at-bol)) ?*))))
  12050. (texp (equal (char-before beg) ?\\))
  12051. (link (equal (char-before beg) ?\[))
  12052. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12053. beg)
  12054. "#+"))
  12055. (startup (string-match "^#\\+STARTUP:.*"
  12056. (buffer-substring (point-at-bol) (point))))
  12057. (completion-ignore-case opt)
  12058. (type nil)
  12059. (tbl nil)
  12060. (table (cond
  12061. (opt
  12062. (setq type :opt)
  12063. (append
  12064. (mapcar
  12065. (lambda (x)
  12066. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12067. (cons (match-string 2 x) (match-string 1 x)))
  12068. (org-split-string (org-get-current-options) "\n"))
  12069. (mapcar 'list org-additional-option-like-keywords)))
  12070. (startup
  12071. (setq type :startup)
  12072. org-startup-options)
  12073. (link (append org-link-abbrev-alist-local
  12074. org-link-abbrev-alist))
  12075. (texp
  12076. (setq type :tex)
  12077. org-html-entities)
  12078. ((string-match "\\`\\*+[ \t]+\\'"
  12079. (buffer-substring (point-at-bol) beg))
  12080. (setq type :todo)
  12081. (mapcar 'list org-todo-keywords-1))
  12082. (searchhead
  12083. (setq type :searchhead)
  12084. (save-excursion
  12085. (goto-char (point-min))
  12086. (while (re-search-forward org-todo-line-regexp nil t)
  12087. (push (list
  12088. (org-make-org-heading-search-string
  12089. (match-string 3) t))
  12090. tbl)))
  12091. tbl)
  12092. (tag (setq type :tag beg beg1)
  12093. (or org-tag-alist (org-get-buffer-tags)))
  12094. (prop (setq type :prop beg beg1)
  12095. (mapcar 'list (org-buffer-property-keys)))
  12096. (t (progn
  12097. (call-interactively org-completion-fallback-command)
  12098. (throw 'exit nil)))))
  12099. (pattern (buffer-substring-no-properties beg end))
  12100. (completion (try-completion pattern table confirm)))
  12101. (cond ((eq completion t)
  12102. (if (not (assoc (upcase pattern) table))
  12103. (message "Already complete")
  12104. (if (equal type :opt)
  12105. (insert (substring (cdr (assoc (upcase pattern) table))
  12106. (length pattern)))
  12107. (if (memq type '(:tag :prop)) (insert ":")))))
  12108. ((null completion)
  12109. (message "Can't find completion for \"%s\"" pattern)
  12110. (ding))
  12111. ((not (string= pattern completion))
  12112. (delete-region beg end)
  12113. (if (string-match " +$" completion)
  12114. (setq completion (replace-match "" t t completion)))
  12115. (insert completion)
  12116. (if (get-buffer-window "*Completions*")
  12117. (delete-window (get-buffer-window "*Completions*")))
  12118. (if (assoc completion table)
  12119. (if (eq type :todo) (insert " ")
  12120. (if (memq type '(:tag :prop)) (insert ":"))))
  12121. (if (and (equal type :opt) (assoc completion table))
  12122. (message "%s" (substitute-command-keys
  12123. "Press \\[org-complete] again to insert example settings"))))
  12124. (t
  12125. (message "Making completion list...")
  12126. (let ((list (sort (all-completions pattern table confirm)
  12127. 'string<)))
  12128. (with-output-to-temp-buffer "*Completions*"
  12129. (condition-case nil
  12130. ;; Protection needed for XEmacs and emacs 21
  12131. (display-completion-list list pattern)
  12132. (error (display-completion-list list)))))
  12133. (message "Making completion list...%s" "done")))))))
  12134. ;;;; TODO, DEADLINE, Comments
  12135. (defun org-toggle-comment ()
  12136. "Change the COMMENT state of an entry."
  12137. (interactive)
  12138. (save-excursion
  12139. (org-back-to-heading)
  12140. (if (looking-at (concat outline-regexp
  12141. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  12142. (replace-match "" t t nil 1)
  12143. (if (looking-at outline-regexp)
  12144. (progn
  12145. (goto-char (match-end 0))
  12146. (insert org-comment-string " "))))))
  12147. (defvar org-last-todo-state-is-todo nil
  12148. "This is non-nil when the last TODO state change led to a TODO state.
  12149. If the last change removed the TODO tag or switched to DONE, then
  12150. this is nil.")
  12151. (defvar org-setting-tags nil) ; dynamically skiped
  12152. ;; FIXME: better place
  12153. (defun org-property-or-variable-value (var &optional inherit)
  12154. "Check if there is a property fixing the value of VAR.
  12155. If yes, return this value. If not, return the current value of the variable."
  12156. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12157. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12158. (read prop)
  12159. (symbol-value var))))
  12160. (defun org-parse-local-options (string var)
  12161. "Parse STRING for startup setting relevant for variable VAR."
  12162. (let ((rtn (symbol-value var))
  12163. e opts)
  12164. (save-match-data
  12165. (if (or (not string) (not (string-match "\\S-" string)))
  12166. rtn
  12167. (setq opts (delq nil (mapcar (lambda (x)
  12168. (setq e (assoc x org-startup-options))
  12169. (if (eq (nth 1 e) var) e nil))
  12170. (org-split-string string "[ \t]+"))))
  12171. (if (not opts)
  12172. rtn
  12173. (setq rtn nil)
  12174. (while (setq e (pop opts))
  12175. (if (not (nth 3 e))
  12176. (setq rtn (nth 2 e))
  12177. (if (not (listp rtn)) (setq rtn nil))
  12178. (push (nth 2 e) rtn)))
  12179. rtn)))))
  12180. (defvar org-blocker-hook nil
  12181. "Hook for functions that are allowed to block a state change.
  12182. Each function gets as its single argument a property list, see
  12183. `org-trigger-hook' for more information about this list.
  12184. If any of the functions in this hook returns nil, the state change
  12185. is blocked.")
  12186. (defvar org-trigger-hook nil
  12187. "Hook for functions that are triggered by a state change.
  12188. Each function gets as its single argument a property list with at least
  12189. the following elements:
  12190. (:type type-of-change :position pos-at-entry-start
  12191. :from old-state :to new-state)
  12192. Depending on the type, more properties may be present.
  12193. This mechanism is currently implemented for:
  12194. TODO state changes
  12195. ------------------
  12196. :type todo-state-change
  12197. :from previous state (keyword as a string), or nil
  12198. :to new state (keyword as a string), or nil")
  12199. (defun org-todo (&optional arg)
  12200. "Change the TODO state of an item.
  12201. The state of an item is given by a keyword at the start of the heading,
  12202. like
  12203. *** TODO Write paper
  12204. *** DONE Call mom
  12205. The different keywords are specified in the variable `org-todo-keywords'.
  12206. By default the available states are \"TODO\" and \"DONE\".
  12207. So for this example: when the item starts with TODO, it is changed to DONE.
  12208. When it starts with DONE, the DONE is removed. And when neither TODO nor
  12209. DONE are present, add TODO at the beginning of the heading.
  12210. With C-u prefix arg, use completion to determine the new state.
  12211. With numeric prefix arg, switch to that state.
  12212. For calling through lisp, arg is also interpreted in the following way:
  12213. 'none -> empty state
  12214. \"\"(empty string) -> switch to empty state
  12215. 'done -> switch to DONE
  12216. 'nextset -> switch to the next set of keywords
  12217. 'previousset -> switch to the previous set of keywords
  12218. \"WAITING\" -> switch to the specified keyword, but only if it
  12219. really is a member of `org-todo-keywords'."
  12220. (interactive "P")
  12221. (save-excursion
  12222. (catch 'exit
  12223. (org-back-to-heading)
  12224. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  12225. (or (looking-at (concat " +" org-todo-regexp " *"))
  12226. (looking-at " *"))
  12227. (let* ((startpos (line-beginning-position))
  12228. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  12229. (org-log-done (org-parse-local-options logging 'org-log-done))
  12230. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  12231. (this (match-string 1))
  12232. (hl-pos (match-beginning 0))
  12233. (head (org-get-todo-sequence-head this))
  12234. (ass (assoc head org-todo-kwd-alist))
  12235. (interpret (nth 1 ass))
  12236. (done-word (nth 3 ass))
  12237. (final-done-word (nth 4 ass))
  12238. (last-state (or this ""))
  12239. (completion-ignore-case t)
  12240. (member (member this org-todo-keywords-1))
  12241. (tail (cdr member))
  12242. (state (cond
  12243. ((and org-todo-key-trigger
  12244. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  12245. (and (not arg) org-use-fast-todo-selection
  12246. (not (eq org-use-fast-todo-selection 'prefix)))))
  12247. ;; Use fast selection
  12248. (org-fast-todo-selection))
  12249. ((and (equal arg '(4))
  12250. (or (not org-use-fast-todo-selection)
  12251. (not org-todo-key-trigger)))
  12252. ;; Read a state with completion
  12253. (completing-read "State: " (mapcar (lambda(x) (list x))
  12254. org-todo-keywords-1)
  12255. nil t))
  12256. ((eq arg 'right)
  12257. (if this
  12258. (if tail (car tail) nil)
  12259. (car org-todo-keywords-1)))
  12260. ((eq arg 'left)
  12261. (if (equal member org-todo-keywords-1)
  12262. nil
  12263. (if this
  12264. (nth (- (length org-todo-keywords-1) (length tail) 2)
  12265. org-todo-keywords-1)
  12266. (org-last org-todo-keywords-1))))
  12267. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  12268. (setq arg nil))) ; hack to fall back to cycling
  12269. (arg
  12270. ;; user or caller requests a specific state
  12271. (cond
  12272. ((equal arg "") nil)
  12273. ((eq arg 'none) nil)
  12274. ((eq arg 'done) (or done-word (car org-done-keywords)))
  12275. ((eq arg 'nextset)
  12276. (or (car (cdr (member head org-todo-heads)))
  12277. (car org-todo-heads)))
  12278. ((eq arg 'previousset)
  12279. (let ((org-todo-heads (reverse org-todo-heads)))
  12280. (or (car (cdr (member head org-todo-heads)))
  12281. (car org-todo-heads))))
  12282. ((car (member arg org-todo-keywords-1)))
  12283. ((nth (1- (prefix-numeric-value arg))
  12284. org-todo-keywords-1))))
  12285. ((null member) (or head (car org-todo-keywords-1)))
  12286. ((equal this final-done-word) nil) ;; -> make empty
  12287. ((null tail) nil) ;; -> first entry
  12288. ((eq interpret 'sequence)
  12289. (car tail))
  12290. ((memq interpret '(type priority))
  12291. (if (eq this-command last-command)
  12292. (car tail)
  12293. (if (> (length tail) 0)
  12294. (or done-word (car org-done-keywords))
  12295. nil)))
  12296. (t nil)))
  12297. (next (if state (concat " " state " ") " "))
  12298. (change-plist (list :type 'todo-state-change :from this :to state
  12299. :position startpos))
  12300. dostates)
  12301. (when org-blocker-hook
  12302. (unless (save-excursion
  12303. (save-match-data
  12304. (run-hook-with-args-until-failure
  12305. 'org-blocker-hook change-plist)))
  12306. (if (interactive-p)
  12307. (error "TODO state change from %s to %s blocked" this state)
  12308. ;; fail silently
  12309. (message "TODO state change from %s to %s blocked" this state)
  12310. (throw 'exit nil))))
  12311. (replace-match next t t)
  12312. (unless (pos-visible-in-window-p hl-pos)
  12313. (message "TODO state changed to %s" (org-trim next)))
  12314. (unless head
  12315. (setq head (org-get-todo-sequence-head state)
  12316. ass (assoc head org-todo-kwd-alist)
  12317. interpret (nth 1 ass)
  12318. done-word (nth 3 ass)
  12319. final-done-word (nth 4 ass)))
  12320. (when (memq arg '(nextset previousset))
  12321. (message "Keyword-Set %d/%d: %s"
  12322. (- (length org-todo-sets) -1
  12323. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  12324. (length org-todo-sets)
  12325. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  12326. (setq org-last-todo-state-is-todo
  12327. (not (member state org-done-keywords)))
  12328. (when (and org-log-done (not (memq arg '(nextset previousset))))
  12329. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  12330. (or (not org-todo-log-states)
  12331. (member state org-todo-log-states))))
  12332. (cond
  12333. ((and state (member state org-not-done-keywords)
  12334. (not (member this org-not-done-keywords)))
  12335. ;; This is now a todo state and was not one before
  12336. ;; Remove any CLOSED timestamp, and possibly log the state change
  12337. (org-add-planning-info nil nil 'closed)
  12338. (and dostates (org-add-log-maybe 'state state 'findpos)))
  12339. ((and state dostates)
  12340. ;; This is a non-nil state, and we need to log it
  12341. (org-add-log-maybe 'state state 'findpos))
  12342. ((and (member state org-done-keywords)
  12343. (not (member this org-done-keywords)))
  12344. ;; It is now done, and it was not done before
  12345. (org-add-planning-info 'closed (org-current-time))
  12346. (org-add-log-maybe 'done state 'findpos))))
  12347. ;; Fixup tag positioning
  12348. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  12349. (run-hooks 'org-after-todo-state-change-hook)
  12350. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  12351. (if (and arg (not (member state org-done-keywords)))
  12352. (setq head (org-get-todo-sequence-head state)))
  12353. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  12354. ;; Fixup cursor location if close to the keyword
  12355. (if (and (outline-on-heading-p)
  12356. (not (bolp))
  12357. (save-excursion (beginning-of-line 1)
  12358. (looking-at org-todo-line-regexp))
  12359. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  12360. (progn
  12361. (goto-char (or (match-end 2) (match-end 1)))
  12362. (just-one-space)))
  12363. (when org-trigger-hook
  12364. (save-excursion
  12365. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  12366. (defun org-get-todo-sequence-head (kwd)
  12367. "Return the head of the TODO sequence to which KWD belongs.
  12368. If KWD is not set, check if there is a text property remembering the
  12369. right sequence."
  12370. (let (p)
  12371. (cond
  12372. ((not kwd)
  12373. (or (get-text-property (point-at-bol) 'org-todo-head)
  12374. (progn
  12375. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  12376. nil (point-at-eol)))
  12377. (get-text-property p 'org-todo-head))))
  12378. ((not (member kwd org-todo-keywords-1))
  12379. (car org-todo-keywords-1))
  12380. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  12381. (defun org-fast-todo-selection ()
  12382. "Fast TODO keyword selection with single keys.
  12383. Returns the new TODO keyword, or nil if no state change should occur."
  12384. (let* ((fulltable org-todo-key-alist)
  12385. (done-keywords org-done-keywords) ;; needed for the faces.
  12386. (maxlen (apply 'max (mapcar
  12387. (lambda (x)
  12388. (if (stringp (car x)) (string-width (car x)) 0))
  12389. fulltable)))
  12390. (expert nil)
  12391. (fwidth (+ maxlen 3 1 3))
  12392. (ncol (/ (- (window-width) 4) fwidth))
  12393. tg cnt e c tbl
  12394. groups ingroup)
  12395. (save-window-excursion
  12396. (if expert
  12397. (set-buffer (get-buffer-create " *Org todo*"))
  12398. ; (delete-other-windows)
  12399. ; (split-window-vertically)
  12400. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12401. (erase-buffer)
  12402. (org-set-local 'org-done-keywords done-keywords)
  12403. (setq tbl fulltable cnt 0)
  12404. (while (setq e (pop tbl))
  12405. (cond
  12406. ((equal e '(:startgroup))
  12407. (push '() groups) (setq ingroup t)
  12408. (when (not (= cnt 0))
  12409. (setq cnt 0)
  12410. (insert "\n"))
  12411. (insert "{ "))
  12412. ((equal e '(:endgroup))
  12413. (setq ingroup nil cnt 0)
  12414. (insert "}\n"))
  12415. (t
  12416. (setq tg (car e) c (cdr e))
  12417. (if ingroup (push tg (car groups)))
  12418. (setq tg (org-add-props tg nil 'face
  12419. (org-get-todo-face tg)))
  12420. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12421. (insert "[" c "] " tg (make-string
  12422. (- fwidth 4 (length tg)) ?\ ))
  12423. (when (= (setq cnt (1+ cnt)) ncol)
  12424. (insert "\n")
  12425. (if ingroup (insert " "))
  12426. (setq cnt 0)))))
  12427. (insert "\n")
  12428. (goto-char (point-min))
  12429. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12430. (fit-window-to-buffer))
  12431. (message "[a-z..]:Set [SPC]:clear")
  12432. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12433. (cond
  12434. ((or (= c ?\C-g)
  12435. (and (= c ?q) (not (rassoc c fulltable))))
  12436. (setq quit-flag t))
  12437. ((= c ?\ ) nil)
  12438. ((setq e (rassoc c fulltable) tg (car e))
  12439. tg)
  12440. (t (setq quit-flag t))))))
  12441. (defun org-get-repeat ()
  12442. "Check if tere is a deadline/schedule with repeater in this entry."
  12443. (save-match-data
  12444. (save-excursion
  12445. (org-back-to-heading t)
  12446. (if (re-search-forward
  12447. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  12448. (match-string 1)))))
  12449. (defvar org-last-changed-timestamp)
  12450. (defvar org-log-post-message)
  12451. (defun org-auto-repeat-maybe ()
  12452. "Check if the current headline contains a repeated deadline/schedule.
  12453. If yes, set TODO state back to what it was and change the base date
  12454. of repeating deadline/scheduled time stamps to new date.
  12455. This function should be run in the `org-after-todo-state-change-hook'."
  12456. ;; last-state is dynamically scoped into this function
  12457. (let* ((repeat (org-get-repeat))
  12458. (aa (assoc last-state org-todo-kwd-alist))
  12459. (interpret (nth 1 aa))
  12460. (head (nth 2 aa))
  12461. (done-word (nth 3 aa))
  12462. (whata '(("d" . day) ("m" . month) ("y" . year)))
  12463. (msg "Entry repeats: ")
  12464. (org-log-done)
  12465. re type n what ts)
  12466. (when repeat
  12467. (org-todo (if (eq interpret 'type) last-state head))
  12468. (when (and org-log-repeat
  12469. (not (memq 'org-add-log-note
  12470. (default-value 'post-command-hook))))
  12471. ;; Make sure a note is taken
  12472. (let ((org-log-done '(done)))
  12473. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  12474. 'findpos)))
  12475. (org-back-to-heading t)
  12476. (org-add-planning-info nil nil 'closed)
  12477. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  12478. org-deadline-time-regexp "\\)"))
  12479. (while (re-search-forward
  12480. re (save-excursion (outline-next-heading) (point)) t)
  12481. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  12482. ts (match-string (if (match-end 2) 2 4)))
  12483. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  12484. (setq n (string-to-number (match-string 1 ts))
  12485. what (match-string 2 ts))
  12486. (if (equal what "w") (setq n (* n 7) what "d"))
  12487. (org-timestamp-change n (cdr (assoc what whata))))
  12488. (setq msg (concat msg type org-last-changed-timestamp " ")))
  12489. (setq org-log-post-message msg)
  12490. (message msg))))
  12491. (defun org-show-todo-tree (arg)
  12492. "Make a compact tree which shows all headlines marked with TODO.
  12493. The tree will show the lines where the regexp matches, and all higher
  12494. headlines above the match.
  12495. With \\[universal-argument] prefix, also show the DONE entries.
  12496. With a numeric prefix N, construct a sparse tree for the Nth element
  12497. of `org-todo-keywords-1'."
  12498. (interactive "P")
  12499. (let ((case-fold-search nil)
  12500. (kwd-re
  12501. (cond ((null arg) org-not-done-regexp)
  12502. ((equal arg '(4))
  12503. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  12504. (mapcar 'list org-todo-keywords-1))))
  12505. (concat "\\("
  12506. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12507. "\\)\\>")))
  12508. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12509. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12510. org-todo-keywords-1)))
  12511. (t (error "Invalid prefix argument: %s" arg)))))
  12512. (message "%d TODO entries found"
  12513. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  12514. (defun org-deadline (&optional remove)
  12515. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12516. With argument REMOVE, remove any deadline from the item."
  12517. (interactive "P")
  12518. (if remove
  12519. (progn
  12520. (org-add-planning-info nil nil 'deadline)
  12521. (message "Item no longer has a deadline."))
  12522. (org-add-planning-info 'deadline nil 'closed)))
  12523. (defun org-schedule (&optional remove)
  12524. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12525. With argument REMOVE, remove any scheduling date from the item."
  12526. (interactive "P")
  12527. (if remove
  12528. (progn
  12529. (org-add-planning-info nil nil 'scheduled)
  12530. (message "Item is no longer scheduled."))
  12531. (org-add-planning-info 'scheduled nil 'closed)))
  12532. (defun org-add-planning-info (what &optional time &rest remove)
  12533. "Insert new timestamp with keyword in the line directly after the headline.
  12534. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  12535. If non is given, the user is prompted for a date.
  12536. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12537. be removed."
  12538. (interactive)
  12539. (let (org-time-was-given org-end-time-was-given)
  12540. (when what (setq time (or time (org-read-date nil 'to-time))))
  12541. (when (and org-insert-labeled-timestamps-at-point
  12542. (member what '(scheduled deadline)))
  12543. (insert
  12544. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12545. (org-insert-time-stamp time org-time-was-given
  12546. nil nil nil (list org-end-time-was-given))
  12547. (setq what nil))
  12548. (save-excursion
  12549. (save-restriction
  12550. (let (col list elt ts buffer-invisibility-spec)
  12551. (org-back-to-heading t)
  12552. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  12553. (goto-char (match-end 1))
  12554. (setq col (current-column))
  12555. (goto-char (match-end 0))
  12556. (if (eobp) (insert "\n") (forward-char 1))
  12557. (if (and (not (looking-at outline-regexp))
  12558. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12559. "[^\r\n]*"))
  12560. (not (equal (match-string 1) org-clock-string)))
  12561. (narrow-to-region (match-beginning 0) (match-end 0))
  12562. (insert-before-markers "\n")
  12563. (backward-char 1)
  12564. (narrow-to-region (point) (point))
  12565. (indent-to-column col))
  12566. ;; Check if we have to remove something.
  12567. (setq list (cons what remove))
  12568. (while list
  12569. (setq elt (pop list))
  12570. (goto-char (point-min))
  12571. (when (or (and (eq elt 'scheduled)
  12572. (re-search-forward org-scheduled-time-regexp nil t))
  12573. (and (eq elt 'deadline)
  12574. (re-search-forward org-deadline-time-regexp nil t))
  12575. (and (eq elt 'closed)
  12576. (re-search-forward org-closed-time-regexp nil t)))
  12577. (replace-match "")
  12578. (if (looking-at "--+<[^>]+>") (replace-match ""))
  12579. (if (looking-at " +") (replace-match ""))))
  12580. (goto-char (point-max))
  12581. (when what
  12582. (insert
  12583. (if (not (equal (char-before) ?\ )) " " "")
  12584. (cond ((eq what 'scheduled) org-scheduled-string)
  12585. ((eq what 'deadline) org-deadline-string)
  12586. ((eq what 'closed) org-closed-string))
  12587. " ")
  12588. (setq ts (org-insert-time-stamp
  12589. time
  12590. (or org-time-was-given
  12591. (and (eq what 'closed) org-log-done-with-time))
  12592. (eq what 'closed)
  12593. nil nil (list org-end-time-was-given)))
  12594. (end-of-line 1))
  12595. (goto-char (point-min))
  12596. (widen)
  12597. (if (looking-at "[ \t]+\r?\n")
  12598. (replace-match ""))
  12599. ts)))))
  12600. (defvar org-log-note-marker (make-marker))
  12601. (defvar org-log-note-purpose nil)
  12602. (defvar org-log-note-state nil)
  12603. (defvar org-log-note-window-configuration nil)
  12604. (defvar org-log-note-return-to (make-marker))
  12605. (defvar org-log-post-message nil
  12606. "Message to be displayed after a log note has been stored.
  12607. The auto-repeater uses this.")
  12608. (defun org-add-log-maybe (&optional purpose state findpos)
  12609. "Set up the post command hook to take a note."
  12610. (save-excursion
  12611. (when (and (listp org-log-done)
  12612. (memq purpose org-log-done))
  12613. (when findpos
  12614. (org-back-to-heading t)
  12615. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  12616. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12617. "[^\r\n]*\\)?"))
  12618. (goto-char (match-end 0))
  12619. (unless org-log-states-order-reversed
  12620. (and (= (char-after) ?\n) (forward-char 1))
  12621. (org-skip-over-state-notes)
  12622. (skip-chars-backward " \t\n\r")))
  12623. (move-marker org-log-note-marker (point))
  12624. (setq org-log-note-purpose purpose)
  12625. (setq org-log-note-state state)
  12626. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  12627. (defun org-skip-over-state-notes ()
  12628. "Skip past the list of State notes in an entry."
  12629. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12630. (while (looking-at "[ \t]*- State")
  12631. (condition-case nil
  12632. (org-next-item)
  12633. (error (org-end-of-item)))))
  12634. (defun org-add-log-note (&optional purpose)
  12635. "Pop up a window for taking a note, and add this note later at point."
  12636. (remove-hook 'post-command-hook 'org-add-log-note)
  12637. (setq org-log-note-window-configuration (current-window-configuration))
  12638. (delete-other-windows)
  12639. (move-marker org-log-note-return-to (point))
  12640. (switch-to-buffer (marker-buffer org-log-note-marker))
  12641. (goto-char org-log-note-marker)
  12642. (org-switch-to-buffer-other-window "*Org Note*")
  12643. (erase-buffer)
  12644. (let ((org-inhibit-startup t)) (org-mode))
  12645. (insert (format "# Insert note for %s.
  12646. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12647. (cond
  12648. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12649. ((eq org-log-note-purpose 'done) "closed todo item")
  12650. ((eq org-log-note-purpose 'state)
  12651. (format "state change to \"%s\"" org-log-note-state))
  12652. (t (error "This should not happen")))))
  12653. (org-set-local 'org-finish-function 'org-store-log-note))
  12654. (defun org-store-log-note ()
  12655. "Finish taking a log note, and insert it to where it belongs."
  12656. (let ((txt (buffer-string))
  12657. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12658. lines ind)
  12659. (kill-buffer (current-buffer))
  12660. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  12661. (setq txt (replace-match "" t t txt)))
  12662. (if (string-match "\\s-+\\'" txt)
  12663. (setq txt (replace-match "" t t txt)))
  12664. (setq lines (org-split-string txt "\n"))
  12665. (when (and note (string-match "\\S-" note))
  12666. (setq note
  12667. (org-replace-escapes
  12668. note
  12669. (list (cons "%u" (user-login-name))
  12670. (cons "%U" user-full-name)
  12671. (cons "%t" (format-time-string
  12672. (org-time-stamp-format 'long 'inactive)
  12673. (current-time)))
  12674. (cons "%s" (if org-log-note-state
  12675. (concat "\"" org-log-note-state "\"")
  12676. "")))))
  12677. (if lines (setq note (concat note " \\\\")))
  12678. (push note lines))
  12679. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  12680. (when lines
  12681. (save-excursion
  12682. (set-buffer (marker-buffer org-log-note-marker))
  12683. (save-excursion
  12684. (goto-char org-log-note-marker)
  12685. (move-marker org-log-note-marker nil)
  12686. (end-of-line 1)
  12687. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12688. (indent-relative nil)
  12689. (insert " - " (pop lines))
  12690. (org-indent-line-function)
  12691. (beginning-of-line 1)
  12692. (looking-at "[ \t]*")
  12693. (setq ind (concat (match-string 0) " "))
  12694. (end-of-line 1)
  12695. (while lines (insert "\n" ind (pop lines)))))))
  12696. (set-window-configuration org-log-note-window-configuration)
  12697. (with-current-buffer (marker-buffer org-log-note-return-to)
  12698. (goto-char org-log-note-return-to))
  12699. (move-marker org-log-note-return-to nil)
  12700. (and org-log-post-message (message org-log-post-message)))
  12701. ;; FIXME: what else would be useful?
  12702. ;; - priority
  12703. ;; - date
  12704. (defun org-sparse-tree (&optional arg)
  12705. "Create a sparse tree, prompt for the details.
  12706. This command can create sparse trees. You first need to select the type
  12707. of match used to create the tree:
  12708. t Show entries with a specific TODO keyword.
  12709. T Show entries selected by a tags match.
  12710. p Enter a property name and its value (both with completion on existing
  12711. names/values) and show entries with that property.
  12712. r Show entries matching a regular expression"
  12713. (interactive "P")
  12714. (let (ans kwd value)
  12715. (message "Sparse tree: [r]egexp [t]odo-kwd [T]ag [p]roperty")
  12716. (setq ans (read-char-exclusive))
  12717. (cond
  12718. ((equal ans ?t)
  12719. (org-show-todo-tree '(4)))
  12720. ((equal ans ?T)
  12721. (call-interactively 'org-tags-sparse-tree))
  12722. ((member ans '(?p ?P))
  12723. (setq kwd (completing-read "Property: "
  12724. (mapcar 'list (org-buffer-property-keys))))
  12725. (setq value (completing-read "Value: "
  12726. (mapcar 'list (org-property-values kwd))))
  12727. (unless (string-match "\\`{.*}\\'" value)
  12728. (setq value (concat "\"" value "\"")))
  12729. (org-tags-sparse-tree arg (concat kwd "=" value)))
  12730. ((member ans '(?r ?R))
  12731. (call-interactively 'org-occur))
  12732. (t (error "No such sparse tree command \"%c\"" ans)))))
  12733. (defvar org-occur-highlights nil)
  12734. (make-variable-buffer-local 'org-occur-highlights)
  12735. (defun org-occur (regexp &optional keep-previous callback)
  12736. "Make a compact tree which shows all matches of REGEXP.
  12737. The tree will show the lines where the regexp matches, and all higher
  12738. headlines above the match. It will also show the heading after the match,
  12739. to make sure editing the matching entry is easy.
  12740. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12741. call to `org-occur' will be kept, to allow stacking of calls to this
  12742. command.
  12743. If CALLBACK is non-nil, it is a function which is called to confirm
  12744. that the match should indeed be shown."
  12745. (interactive "sRegexp: \nP")
  12746. (or keep-previous (org-remove-occur-highlights nil nil t))
  12747. (let ((cnt 0))
  12748. (save-excursion
  12749. (goto-char (point-min))
  12750. (if (or (not keep-previous) ; do not want to keep
  12751. (not org-occur-highlights)) ; no previous matches
  12752. ;; hide everything
  12753. (org-overview))
  12754. (while (re-search-forward regexp nil t)
  12755. (when (or (not callback)
  12756. (save-match-data (funcall callback)))
  12757. (setq cnt (1+ cnt))
  12758. (when org-highlight-sparse-tree-matches
  12759. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12760. (org-show-context 'occur-tree))))
  12761. (when org-remove-highlights-with-change
  12762. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12763. nil 'local))
  12764. (unless org-sparse-tree-open-archived-trees
  12765. (org-hide-archived-subtrees (point-min) (point-max)))
  12766. (run-hooks 'org-occur-hook)
  12767. (if (interactive-p)
  12768. (message "%d match(es) for regexp %s" cnt regexp))
  12769. cnt))
  12770. (defun org-show-context (&optional key)
  12771. "Make sure point and context and visible.
  12772. How much context is shown depends upon the variables
  12773. `org-show-hierarchy-above', `org-show-following-heading'. and
  12774. `org-show-siblings'."
  12775. (let ((heading-p (org-on-heading-p t))
  12776. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12777. (following-p (org-get-alist-option org-show-following-heading key))
  12778. (siblings-p (org-get-alist-option org-show-siblings key)))
  12779. (catch 'exit
  12780. ;; Show heading or entry text
  12781. (if heading-p
  12782. (org-flag-heading nil) ; only show the heading
  12783. (and (or (org-invisible-p) (org-invisible-p2))
  12784. (org-show-hidden-entry))) ; show entire entry
  12785. (when following-p
  12786. ;; Show next sibling, or heading below text
  12787. (save-excursion
  12788. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12789. (org-flag-heading nil))))
  12790. (when siblings-p (org-show-siblings))
  12791. (when hierarchy-p
  12792. ;; show all higher headings, possibly with siblings
  12793. (save-excursion
  12794. (while (and (condition-case nil
  12795. (progn (org-up-heading-all 1) t)
  12796. (error nil))
  12797. (not (bobp)))
  12798. (org-flag-heading nil)
  12799. (when siblings-p (org-show-siblings))))))))
  12800. (defun org-reveal (&optional siblings)
  12801. "Show current entry, hierarchy above it, and the following headline.
  12802. This can be used to show a consistent set of context around locations
  12803. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12804. not t for the search context.
  12805. With optional argument SIBLINGS, on each level of the hierarchy all
  12806. siblings are shown. This repairs the tree structure to what it would
  12807. look like when opened with hierarchical calls to `org-cycle'."
  12808. (interactive "P")
  12809. (let ((org-show-hierarchy-above t)
  12810. (org-show-following-heading t)
  12811. (org-show-siblings (if siblings t org-show-siblings)))
  12812. (org-show-context nil)))
  12813. (defun org-highlight-new-match (beg end)
  12814. "Highlight from BEG to END and mark the highlight is an occur headline."
  12815. (let ((ov (org-make-overlay beg end)))
  12816. (org-overlay-put ov 'face 'secondary-selection)
  12817. (push ov org-occur-highlights)))
  12818. (defun org-remove-occur-highlights (&optional beg end noremove)
  12819. "Remove the occur highlights from the buffer.
  12820. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12821. from the `before-change-functions' in the current buffer."
  12822. (interactive)
  12823. (unless org-inhibit-highlight-removal
  12824. (mapc 'org-delete-overlay org-occur-highlights)
  12825. (setq org-occur-highlights nil)
  12826. (unless noremove
  12827. (remove-hook 'before-change-functions
  12828. 'org-remove-occur-highlights 'local))))
  12829. ;;;; Priorities
  12830. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12831. "Regular expression matching the priority indicator.")
  12832. (defvar org-remove-priority-next-time nil)
  12833. (defun org-priority-up ()
  12834. "Increase the priority of the current item."
  12835. (interactive)
  12836. (org-priority 'up))
  12837. (defun org-priority-down ()
  12838. "Decrease the priority of the current item."
  12839. (interactive)
  12840. (org-priority 'down))
  12841. (defun org-priority (&optional action)
  12842. "Change the priority of an item by ARG.
  12843. ACTION can be `set', `up', `down', or a character."
  12844. (interactive)
  12845. (setq action (or action 'set))
  12846. (let (current new news have remove)
  12847. (save-excursion
  12848. (org-back-to-heading)
  12849. (if (looking-at org-priority-regexp)
  12850. (setq current (string-to-char (match-string 2))
  12851. have t)
  12852. (setq current org-default-priority))
  12853. (cond
  12854. ((or (eq action 'set) (integerp action))
  12855. (if (integerp action)
  12856. (setq new action)
  12857. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  12858. (setq new (read-char-exclusive)))
  12859. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12860. (= (upcase org-lowest-priority) org-lowest-priority))
  12861. (setq new (upcase new)))
  12862. (cond ((equal new ?\ ) (setq remove t))
  12863. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12864. (error "Priority must be between `%c' and `%c'"
  12865. org-highest-priority org-lowest-priority))))
  12866. ((eq action 'up)
  12867. (if (and (not have) (eq last-command this-command))
  12868. (setq new org-lowest-priority)
  12869. (setq new (if (and org-priority-start-cycle-with-default (not have))
  12870. org-default-priority (1- current)))))
  12871. ((eq action 'down)
  12872. (if (and (not have) (eq last-command this-command))
  12873. (setq new org-highest-priority)
  12874. (setq new (if (and org-priority-start-cycle-with-default (not have))
  12875. org-default-priority (1+ current)))))
  12876. (t (error "Invalid action")))
  12877. (if (or (< (upcase new) org-highest-priority)
  12878. (> (upcase new) org-lowest-priority))
  12879. (setq remove t))
  12880. (setq news (format "%c" new))
  12881. (if have
  12882. (if remove
  12883. (replace-match "" t t nil 1)
  12884. (replace-match news t t nil 2))
  12885. (if remove
  12886. (error "No priority cookie found in line")
  12887. (looking-at org-todo-line-regexp)
  12888. (if (match-end 2)
  12889. (progn
  12890. (goto-char (match-end 2))
  12891. (insert " [#" news "]"))
  12892. (goto-char (match-beginning 3))
  12893. (insert "[#" news "] ")))))
  12894. (org-preserve-lc (org-set-tags nil 'align))
  12895. (if remove
  12896. (message "Priority removed")
  12897. (message "Priority of current item set to %s" news))))
  12898. (defun org-get-priority (s)
  12899. "Find priority cookie and return priority."
  12900. (save-match-data
  12901. (if (not (string-match org-priority-regexp s))
  12902. (* 1000 (- org-lowest-priority org-default-priority))
  12903. (* 1000 (- org-lowest-priority
  12904. (string-to-char (match-string 2 s)))))))
  12905. ;;;; Tags
  12906. (defun org-scan-tags (action matcher &optional todo-only)
  12907. "Scan headline tags with inheritance and produce output ACTION.
  12908. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  12909. evaluated, testing if a given set of tags qualifies a headline for
  12910. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  12911. are included in the output."
  12912. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  12913. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12914. (org-re
  12915. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  12916. (props (list 'face nil
  12917. 'done-face 'org-done
  12918. 'undone-face nil
  12919. 'mouse-face 'highlight
  12920. 'org-not-done-regexp org-not-done-regexp
  12921. 'org-todo-regexp org-todo-regexp
  12922. 'keymap org-agenda-keymap
  12923. 'help-echo
  12924. (format "mouse-2 or RET jump to org file %s"
  12925. (abbreviate-file-name
  12926. (or (buffer-file-name (buffer-base-buffer))
  12927. (buffer-name (buffer-base-buffer)))))))
  12928. (case-fold-search nil)
  12929. lspos
  12930. tags tags-list tags-alist (llast 0) rtn level category i txt
  12931. todo marker entry priority)
  12932. (save-excursion
  12933. (goto-char (point-min))
  12934. (when (eq action 'sparse-tree)
  12935. (org-overview)
  12936. (org-remove-occur-highlights))
  12937. (while (re-search-forward re nil t)
  12938. (catch :skip
  12939. (setq todo (if (match-end 1) (match-string 2))
  12940. tags (if (match-end 4) (match-string 4)))
  12941. (goto-char (setq lspos (1+ (match-beginning 0))))
  12942. (setq level (org-reduced-level (funcall outline-level))
  12943. category (org-get-category))
  12944. (setq i llast llast level)
  12945. ;; remove tag lists from same and sublevels
  12946. (while (>= i level)
  12947. (when (setq entry (assoc i tags-alist))
  12948. (setq tags-alist (delete entry tags-alist)))
  12949. (setq i (1- i)))
  12950. ;; add the nex tags
  12951. (when tags
  12952. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  12953. tags-alist
  12954. (cons (cons level tags) tags-alist)))
  12955. ;; compile tags for current headline
  12956. (setq tags-list
  12957. (if org-use-tag-inheritance
  12958. (apply 'append (mapcar 'cdr tags-alist))
  12959. tags))
  12960. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  12961. (eval matcher)
  12962. (or (not org-agenda-skip-archived-trees)
  12963. (not (member org-archive-tag tags-list))))
  12964. (and (eq action 'agenda) (org-agenda-skip))
  12965. ;; list this headline
  12966. (if (eq action 'sparse-tree)
  12967. (progn
  12968. (and org-highlight-sparse-tree-matches
  12969. (org-get-heading) (match-end 0)
  12970. (org-highlight-new-match
  12971. (match-beginning 0) (match-beginning 1)))
  12972. (org-show-context 'tags-tree))
  12973. (setq txt (org-format-agenda-item
  12974. ""
  12975. (concat
  12976. (if org-tags-match-list-sublevels
  12977. (make-string (1- level) ?.) "")
  12978. (org-get-heading))
  12979. category tags-list)
  12980. priority (org-get-priority txt))
  12981. (goto-char lspos)
  12982. (setq marker (org-agenda-new-marker))
  12983. (org-add-props txt props
  12984. 'org-marker marker 'org-hd-marker marker 'org-category category
  12985. 'priority priority 'type "tagsmatch")
  12986. (push txt rtn))
  12987. ;; if we are to skip sublevels, jump to end of subtree
  12988. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  12989. (when (and (eq action 'sparse-tree)
  12990. (not org-sparse-tree-open-archived-trees))
  12991. (org-hide-archived-subtrees (point-min) (point-max)))
  12992. (nreverse rtn)))
  12993. (defvar todo-only) ;; dynamically scoped
  12994. (defun org-tags-sparse-tree (&optional todo-only match)
  12995. "Create a sparse tree according to tags string MATCH.
  12996. MATCH can contain positive and negative selection of tags, like
  12997. \"+WORK+URGENT-WITHBOSS\".
  12998. If optional argument TODO_ONLY is non-nil, only select lines that are
  12999. also TODO lines."
  13000. (interactive "P")
  13001. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13002. (defvar org-cached-props nil)
  13003. (defun org-cached-entry-get (pom property)
  13004. (if org-use-property-inheritance
  13005. ;; Caching is not possible, check it directly
  13006. (org-entry-get pom property 'inherit)
  13007. ;; Get all properties, so that we can do complicated checks easily
  13008. (cdr (assoc property (or org-cached-props
  13009. (setq org-cached-props
  13010. (org-entry-properties pom)))))))
  13011. (defun org-global-tags-completion-table (&optional files)
  13012. "Return the list of all tags in all agenda buffer/files."
  13013. (save-excursion
  13014. (org-uniquify
  13015. (apply 'append
  13016. (mapcar
  13017. (lambda (file)
  13018. (set-buffer (find-file-noselect file))
  13019. (org-get-buffer-tags))
  13020. (if (and files (car files))
  13021. files
  13022. (org-agenda-files)))))))
  13023. (defun org-make-tags-matcher (match)
  13024. "Create the TAGS//TODO matcher form for the selection string MATCH."
  13025. ;; todo-only is scoped dynamically into this function, and the function
  13026. ;; may change it it the matcher asksk for it.
  13027. (unless match
  13028. ;; Get a new match request, with completion
  13029. (let ((org-last-tags-completion-table
  13030. (org-global-tags-completion-table)))
  13031. (setq match (completing-read
  13032. "Match: " 'org-tags-completion-function nil nil nil
  13033. 'org-tags-history))))
  13034. ;; Parse the string and create a lisp form
  13035. (let ((match0 match)
  13036. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  13037. minus tag mm
  13038. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13039. orterms term orlist re-p level-p prop-p pn pv)
  13040. (if (string-match "/+" match)
  13041. ;; match contains also a todo-matching request
  13042. (progn
  13043. (setq tagsmatch (substring match 0 (match-beginning 0))
  13044. todomatch (substring match (match-end 0)))
  13045. (if (string-match "^!" todomatch)
  13046. (setq todo-only t todomatch (substring todomatch 1)))
  13047. (if (string-match "^\\s-*$" todomatch)
  13048. (setq todomatch nil)))
  13049. ;; only matching tags
  13050. (setq tagsmatch match todomatch nil))
  13051. ;; Make the tags matcher
  13052. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13053. (setq tagsmatcher t)
  13054. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13055. (while (setq term (pop orterms))
  13056. (while (and (equal (substring term -1) "\\") orterms)
  13057. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13058. (while (string-match re term)
  13059. (setq minus (and (match-end 1)
  13060. (equal (match-string 1 term) "-"))
  13061. tag (match-string 2 term)
  13062. re-p (equal (string-to-char tag) ?{)
  13063. level-p (match-end 3)
  13064. prop-p (match-end 4)
  13065. mm (cond
  13066. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13067. (level-p `(= level ,(string-to-number
  13068. (match-string 3 term))))
  13069. (prop-p
  13070. (setq pn (match-string 4 term)
  13071. pv (match-string 5 term)
  13072. re-p (equal (string-to-char pv) ?{)
  13073. pv (substring pv 1 -1))
  13074. (if re-p
  13075. `(string-match ,pv (or (org-cached-entry-get nil ,pn) ""))
  13076. `(equal ,pv (org-cached-entry-get nil ,pn))))
  13077. (t `(member ,(downcase tag) tags-list)))
  13078. mm (if minus (list 'not mm) mm)
  13079. term (substring term (match-end 0)))
  13080. (push mm tagsmatcher))
  13081. (push (if (> (length tagsmatcher) 1)
  13082. (cons 'and tagsmatcher)
  13083. (car tagsmatcher))
  13084. orlist)
  13085. (setq tagsmatcher nil))
  13086. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13087. (setq tagsmatcher
  13088. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13089. ;; Make the todo matcher
  13090. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13091. (setq todomatcher t)
  13092. (setq orterms (org-split-string todomatch "|") orlist nil)
  13093. (while (setq term (pop orterms))
  13094. (while (string-match re term)
  13095. (setq minus (and (match-end 1)
  13096. (equal (match-string 1 term) "-"))
  13097. kwd (match-string 2 term)
  13098. re-p (equal (string-to-char kwd) ?{)
  13099. term (substring term (match-end 0))
  13100. mm (if re-p
  13101. `(string-match ,(substring kwd 1 -1) todo)
  13102. (list 'equal 'todo kwd))
  13103. mm (if minus (list 'not mm) mm))
  13104. (push mm todomatcher))
  13105. (push (if (> (length todomatcher) 1)
  13106. (cons 'and todomatcher)
  13107. (car todomatcher))
  13108. orlist)
  13109. (setq todomatcher nil))
  13110. (setq todomatcher (if (> (length orlist) 1)
  13111. (cons 'or orlist) (car orlist))))
  13112. ;; Return the string and lisp forms of the matcher
  13113. (setq matcher (if todomatcher
  13114. (list 'and tagsmatcher todomatcher)
  13115. tagsmatcher))
  13116. (cons match0 matcher)))
  13117. (defun org-match-any-p (re list)
  13118. "Does re match any element of list?"
  13119. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13120. (delq nil list))
  13121. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  13122. (defvar org-tags-overlay (org-make-overlay 1 1))
  13123. (org-detach-overlay org-tags-overlay)
  13124. (defun org-align-tags-here (to-col)
  13125. ;; Assumes that this is a headline
  13126. (let ((pos (point)) (col (current-column)) tags)
  13127. (beginning-of-line 1)
  13128. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13129. (< pos (match-beginning 2)))
  13130. (progn
  13131. (setq tags (match-string 2))
  13132. (goto-char (match-beginning 1))
  13133. (insert " ")
  13134. (delete-region (point) (1+ (match-end 0)))
  13135. (backward-char 1)
  13136. (move-to-column
  13137. (max (1+ (current-column))
  13138. (1+ col)
  13139. (if (> to-col 0)
  13140. to-col
  13141. (- (abs to-col) (length tags))))
  13142. t)
  13143. (insert tags)
  13144. (move-to-column (min (current-column) col) t))
  13145. (goto-char pos))))
  13146. (defun org-set-tags (&optional arg just-align)
  13147. "Set the tags for the current headline.
  13148. With prefix ARG, realign all tags in headings in the current buffer."
  13149. (interactive "P")
  13150. (let* ((re (concat "^" outline-regexp))
  13151. (current (org-get-tags-string))
  13152. (col (current-column))
  13153. (org-setting-tags t)
  13154. table current-tags inherited-tags ; computed below when needed
  13155. tags p0 c0 c1 rpl)
  13156. (if arg
  13157. (save-excursion
  13158. (goto-char (point-min))
  13159. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13160. (while (re-search-forward re nil t)
  13161. (org-set-tags nil t)
  13162. (end-of-line 1)))
  13163. (message "All tags realigned to column %d" org-tags-column))
  13164. (if just-align
  13165. (setq tags current)
  13166. ;; Get a new set of tags from the user
  13167. (save-excursion
  13168. (setq table (or org-tag-alist (org-get-buffer-tags))
  13169. org-last-tags-completion-table table
  13170. current-tags (org-split-string current ":")
  13171. inherited-tags (nreverse
  13172. (nthcdr (length current-tags)
  13173. (nreverse (org-get-tags-at))))
  13174. tags
  13175. (if (or (eq t org-use-fast-tag-selection)
  13176. (and org-use-fast-tag-selection
  13177. (delq nil (mapcar 'cdr table))))
  13178. (org-fast-tag-selection
  13179. current-tags inherited-tags table
  13180. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  13181. (let ((org-add-colon-after-tag-completion t))
  13182. (org-trim
  13183. (org-without-partial-completion
  13184. (completing-read "Tags: " 'org-tags-completion-function
  13185. nil nil current 'org-tags-history)))))))
  13186. (while (string-match "[-+&]+" tags)
  13187. ;; No boolean logic, just a list
  13188. (setq tags (replace-match ":" t t tags))))
  13189. (if (string-match "\\`[\t ]*\\'" tags)
  13190. (setq tags "")
  13191. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13192. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13193. ;; Insert new tags at the correct column
  13194. (beginning-of-line 1)
  13195. (cond
  13196. ((and (equal current "") (equal tags "")))
  13197. ((re-search-forward
  13198. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13199. (point-at-eol) t)
  13200. (if (equal tags "")
  13201. (setq rpl "")
  13202. (goto-char (match-beginning 0))
  13203. (setq c0 (current-column) p0 (point)
  13204. c1 (max (1+ c0) (if (> org-tags-column 0)
  13205. org-tags-column
  13206. (- (- org-tags-column) (length tags))))
  13207. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13208. (replace-match rpl t t)
  13209. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  13210. tags)
  13211. (t (error "Tags alignment failed")))
  13212. (move-to-column col)
  13213. (unless just-align
  13214. (run-hooks 'org-after-tags-change-hook)))))
  13215. (defun org-change-tag-in-region (beg end tag off)
  13216. "Add or remove TAG for each entry in the region.
  13217. This works in the agenda, and also in an org-mode buffer."
  13218. (interactive
  13219. (list (region-beginning) (region-end)
  13220. (let ((org-last-tags-completion-table
  13221. (if (org-mode-p)
  13222. (org-get-buffer-tags)
  13223. (org-global-tags-completion-table))))
  13224. (completing-read
  13225. "Tag: " 'org-tags-completion-function nil nil nil
  13226. 'org-tags-history))
  13227. (progn
  13228. (message "[s]et or [r]emove? ")
  13229. (equal (read-char-exclusive) ?r))))
  13230. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13231. (let ((agendap (equal major-mode 'org-agenda-mode))
  13232. l1 l2 m buf pos newhead (cnt 0))
  13233. (goto-char end)
  13234. (setq l2 (1- (org-current-line)))
  13235. (goto-char beg)
  13236. (setq l1 (org-current-line))
  13237. (loop for l from l1 to l2 do
  13238. (goto-line l)
  13239. (setq m (get-text-property (point) 'org-hd-marker))
  13240. (when (or (and (org-mode-p) (org-on-heading-p))
  13241. (and agendap m))
  13242. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13243. pos (if agendap m (point)))
  13244. (with-current-buffer buf
  13245. (save-excursion
  13246. (save-restriction
  13247. (goto-char pos)
  13248. (setq cnt (1+ cnt))
  13249. (org-toggle-tag tag (if off 'off 'on))
  13250. (setq newhead (org-get-heading)))))
  13251. (and agendap (org-agenda-change-all-lines newhead m))))
  13252. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13253. (defun org-tags-completion-function (string predicate &optional flag)
  13254. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13255. (confirm (lambda (x) (stringp (car x)))))
  13256. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  13257. (setq s1 (match-string 1 string)
  13258. s2 (match-string 2 string))
  13259. (setq s1 "" s2 string))
  13260. (cond
  13261. ((eq flag nil)
  13262. ;; try completion
  13263. (setq rtn (try-completion s2 ctable confirm))
  13264. (if (stringp rtn)
  13265. (setq rtn
  13266. (concat s1 s2 (substring rtn (length s2))
  13267. (if (and org-add-colon-after-tag-completion
  13268. (assoc rtn ctable))
  13269. ":" ""))))
  13270. rtn)
  13271. ((eq flag t)
  13272. ;; all-completions
  13273. (all-completions s2 ctable confirm)
  13274. )
  13275. ((eq flag 'lambda)
  13276. ;; exact match?
  13277. (assoc s2 ctable)))
  13278. ))
  13279. (defun org-fast-tag-insert (kwd tags face &optional end)
  13280. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  13281. (insert (format "%-12s" (concat kwd ":"))
  13282. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13283. (or end "")))
  13284. (defun org-fast-tag-show-exit (flag)
  13285. (save-excursion
  13286. (goto-line 3)
  13287. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13288. (replace-match ""))
  13289. (when flag
  13290. (end-of-line 1)
  13291. (move-to-column (- (window-width) 19) t)
  13292. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13293. (defun org-set-current-tags-overlay (current prefix)
  13294. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13295. (if (featurep 'xemacs)
  13296. (org-overlay-display org-tags-overlay (concat prefix s)
  13297. 'secondary-selection)
  13298. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13299. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13300. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13301. "Fast tag selection with single keys.
  13302. CURRENT is the current list of tags in the headline, INHERITED is the
  13303. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13304. possibly with grouping information. TODO-TABLE is a similar table with
  13305. TODO keywords, should these have keys assigned to them.
  13306. If the keys are nil, a-z are automatically assigned.
  13307. Returns the new tags string, or nil to not change the current settings."
  13308. (let* ((fulltable (append table todo-table))
  13309. (maxlen (apply 'max (mapcar
  13310. (lambda (x)
  13311. (if (stringp (car x)) (string-width (car x)) 0))
  13312. fulltable)))
  13313. (buf (current-buffer))
  13314. (expert (eq org-fast-tag-selection-single-key 'expert))
  13315. (buffer-tags nil)
  13316. (fwidth (+ maxlen 3 1 3))
  13317. (ncol (/ (- (window-width) 4) fwidth))
  13318. (i-face 'org-done)
  13319. (c-face 'org-todo)
  13320. tg cnt e c char c1 c2 ntable tbl rtn
  13321. ov-start ov-end ov-prefix
  13322. (exit-after-next org-fast-tag-selection-single-key)
  13323. (done-keywords org-done-keywords)
  13324. groups ingroup)
  13325. (save-excursion
  13326. (beginning-of-line 1)
  13327. (if (looking-at
  13328. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13329. (setq ov-start (match-beginning 1)
  13330. ov-end (match-end 1)
  13331. ov-prefix "")
  13332. (setq ov-start (1- (point-at-eol))
  13333. ov-end (1+ ov-start))
  13334. (skip-chars-forward "^\n\r")
  13335. (setq ov-prefix
  13336. (concat
  13337. (buffer-substring (1- (point)) (point))
  13338. (if (> (current-column) org-tags-column)
  13339. " "
  13340. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13341. (org-move-overlay org-tags-overlay ov-start ov-end)
  13342. (save-window-excursion
  13343. (if expert
  13344. (set-buffer (get-buffer-create " *Org tags*"))
  13345. (delete-other-windows)
  13346. (split-window-vertically)
  13347. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13348. (erase-buffer)
  13349. (org-set-local 'org-done-keywords done-keywords)
  13350. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13351. (org-fast-tag-insert "Current" current c-face "\n\n")
  13352. (org-fast-tag-show-exit exit-after-next)
  13353. (org-set-current-tags-overlay current ov-prefix)
  13354. (setq tbl fulltable char ?a cnt 0)
  13355. (while (setq e (pop tbl))
  13356. (cond
  13357. ((equal e '(:startgroup))
  13358. (push '() groups) (setq ingroup t)
  13359. (when (not (= cnt 0))
  13360. (setq cnt 0)
  13361. (insert "\n"))
  13362. (insert "{ "))
  13363. ((equal e '(:endgroup))
  13364. (setq ingroup nil cnt 0)
  13365. (insert "}\n"))
  13366. (t
  13367. (setq tg (car e) c2 nil)
  13368. (if (cdr e)
  13369. (setq c (cdr e))
  13370. ;; automatically assign a character.
  13371. (setq c1 (string-to-char
  13372. (downcase (substring
  13373. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13374. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13375. (while (or (rassoc char ntable) (rassoc char table))
  13376. (setq char (1+ char)))
  13377. (setq c2 c1))
  13378. (setq c (or c2 char)))
  13379. (if ingroup (push tg (car groups)))
  13380. (setq tg (org-add-props tg nil 'face
  13381. (cond
  13382. ((not (assoc tg table))
  13383. (org-get-todo-face tg))
  13384. ((member tg current) c-face)
  13385. ((member tg inherited) i-face)
  13386. (t nil))))
  13387. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13388. (insert "[" c "] " tg (make-string
  13389. (- fwidth 4 (length tg)) ?\ ))
  13390. (push (cons tg c) ntable)
  13391. (when (= (setq cnt (1+ cnt)) ncol)
  13392. (insert "\n")
  13393. (if ingroup (insert " "))
  13394. (setq cnt 0)))))
  13395. (setq ntable (nreverse ntable))
  13396. (insert "\n")
  13397. (goto-char (point-min))
  13398. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13399. (fit-window-to-buffer))
  13400. (setq rtn
  13401. (catch 'exit
  13402. (while t
  13403. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  13404. (if groups " [!] no groups" " [!]groups")
  13405. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13406. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13407. (cond
  13408. ((= c ?\r) (throw 'exit t))
  13409. ((= c ?!)
  13410. (setq groups (not groups))
  13411. (goto-char (point-min))
  13412. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13413. ((= c ?\C-c)
  13414. (if (not expert)
  13415. (org-fast-tag-show-exit
  13416. (setq exit-after-next (not exit-after-next)))
  13417. (setq expert nil)
  13418. (delete-other-windows)
  13419. (split-window-vertically)
  13420. (org-switch-to-buffer-other-window " *Org tags*")
  13421. (and (fboundp 'fit-window-to-buffer)
  13422. (fit-window-to-buffer))))
  13423. ((or (= c ?\C-g)
  13424. (and (= c ?q) (not (rassoc c ntable))))
  13425. (org-detach-overlay org-tags-overlay)
  13426. (setq quit-flag t))
  13427. ((= c ?\ )
  13428. (setq current nil)
  13429. (if exit-after-next (setq exit-after-next 'now)))
  13430. ((= c ?\t)
  13431. (condition-case nil
  13432. (setq tg (completing-read
  13433. "Tag: "
  13434. (or buffer-tags
  13435. (with-current-buffer buf
  13436. (org-get-buffer-tags)))))
  13437. (quit (setq tg "")))
  13438. (when (string-match "\\S-" tg)
  13439. (add-to-list 'buffer-tags (list tg))
  13440. (if (member tg current)
  13441. (setq current (delete tg current))
  13442. (push tg current)))
  13443. (if exit-after-next (setq exit-after-next 'now)))
  13444. ((setq e (rassoc c todo-table) tg (car e))
  13445. (with-current-buffer buf
  13446. (save-excursion (org-todo tg)))
  13447. (if exit-after-next (setq exit-after-next 'now)))
  13448. ((setq e (rassoc c ntable) tg (car e))
  13449. (if (member tg current)
  13450. (setq current (delete tg current))
  13451. (loop for g in groups do
  13452. (if (member tg g)
  13453. (mapc (lambda (x)
  13454. (setq current (delete x current)))
  13455. g)))
  13456. (push tg current))
  13457. (if exit-after-next (setq exit-after-next 'now))))
  13458. ;; Create a sorted list
  13459. (setq current
  13460. (sort current
  13461. (lambda (a b)
  13462. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13463. (if (eq exit-after-next 'now) (throw 'exit t))
  13464. (goto-char (point-min))
  13465. (beginning-of-line 2)
  13466. (delete-region (point) (point-at-eol))
  13467. (org-fast-tag-insert "Current" current c-face)
  13468. (org-set-current-tags-overlay current ov-prefix)
  13469. (while (re-search-forward
  13470. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  13471. (setq tg (match-string 1))
  13472. (add-text-properties
  13473. (match-beginning 1) (match-end 1)
  13474. (list 'face
  13475. (cond
  13476. ((member tg current) c-face)
  13477. ((member tg inherited) i-face)
  13478. (t (get-text-property (match-beginning 1) 'face))))))
  13479. (goto-char (point-min)))))
  13480. (org-detach-overlay org-tags-overlay)
  13481. (if rtn
  13482. (mapconcat 'identity current ":")
  13483. nil))))
  13484. (defun org-get-tags-string ()
  13485. "Get the TAGS string in the current headline."
  13486. (unless (org-on-heading-p t)
  13487. (error "Not on a heading"))
  13488. (save-excursion
  13489. (beginning-of-line 1)
  13490. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13491. (org-match-string-no-properties 1)
  13492. "")))
  13493. (defun org-get-tags ()
  13494. "Get the list of tags specified in the current headline."
  13495. (org-split-string (org-get-tags-string) ":"))
  13496. (defun org-get-buffer-tags ()
  13497. "Get a table of all tags used in the buffer, for completion."
  13498. (let (tags)
  13499. (save-excursion
  13500. (goto-char (point-min))
  13501. (while (re-search-forward
  13502. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  13503. (when (equal (char-after (point-at-bol 0)) ?*)
  13504. (mapc (lambda (x) (add-to-list 'tags x))
  13505. (org-split-string (org-match-string-no-properties 1) ":")))))
  13506. (mapcar 'list tags)))
  13507. ;;;; Properties
  13508. ;;; Setting and retrieving properties
  13509. (defconst org-special-properties
  13510. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY")
  13511. "The special properties valid in Org-mode.
  13512. These are properties that are not defined in the property drawer,
  13513. but in some other way.")
  13514. (defconst org-default-properties
  13515. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  13516. "LOCATION" "LOGGING" "COLUMNS")
  13517. "Some properties that are used by Org-mode for various purposes.
  13518. Being in this list makes sure that they are offered for completion.")
  13519. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13520. "Regular expression matching the first line of a property drawer.")
  13521. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13522. "Regular expression matching the first line of a property drawer.")
  13523. (defun org-property-action ()
  13524. "Do an action on properties."
  13525. (interactive)
  13526. (let (c)
  13527. (org-at-property-p)
  13528. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13529. (setq c (read-char-exclusive))
  13530. (cond
  13531. ((equal c ?s)
  13532. (call-interactively 'org-set-property))
  13533. ((equal c ?d)
  13534. (call-interactively 'org-delete-property))
  13535. ((equal c ?D)
  13536. (call-interactively 'org-delete-property-globally))
  13537. ((equal c ?c)
  13538. (call-interactively 'org-compute-property-at-point))
  13539. (t (error "No such property action %c" c)))))
  13540. (defun org-at-property-p ()
  13541. "Is the cursor in a property line?"
  13542. ;; FIXME: Does not check if we are actually in the drawer.
  13543. ;; FIXME: also returns true on any drawers.....
  13544. ;; This is used by C-c C-c for property action.
  13545. (save-excursion
  13546. (beginning-of-line 1)
  13547. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  13548. (defmacro org-with-point-at (pom &rest body)
  13549. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  13550. (declare (indent 1) (debug t))
  13551. `(save-excursion
  13552. (if (markerp pom) (set-buffer (marker-buffer pom)))
  13553. (save-excursion
  13554. (goto-char (or pom (point)))
  13555. ,@body)))
  13556. (defun org-get-property-block (&optional beg end force)
  13557. "Return the (beg . end) range of the body of the property drawer.
  13558. BEG and END can be beginning and end of subtree, if not given
  13559. they will be found.
  13560. If the drawer does not exist and FORCE is non-nil, create the drawer."
  13561. (catch 'exit
  13562. (save-excursion
  13563. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  13564. (end (or end (progn (outline-next-heading) (point)))))
  13565. (goto-char beg)
  13566. (if (re-search-forward org-property-start-re end t)
  13567. (setq beg (1+ (match-end 0)))
  13568. (if force
  13569. (save-excursion
  13570. (org-insert-property-drawer)
  13571. (setq end (progn (outline-next-heading) (point))))
  13572. (throw 'exit nil))
  13573. (goto-char beg)
  13574. (if (re-search-forward org-property-start-re end t)
  13575. (setq beg (1+ (match-end 0)))))
  13576. (if (re-search-forward org-property-end-re end t)
  13577. (setq end (match-beginning 0))
  13578. (or force (throw 'exit nil))
  13579. (goto-char beg)
  13580. (setq end beg)
  13581. (org-indent-line-function)
  13582. (insert ":END:\n"))
  13583. (cons beg end)))))
  13584. (defun org-entry-properties (&optional pom which)
  13585. "Get all properties of the entry at point-or-marker POM.
  13586. This includes the TODO keyword, the tags, time strings for deadline,
  13587. scheduled, and clocking, and any additional properties defined in the
  13588. entry. The return value is an alist, keys may occur multiple times
  13589. if the property key was used several times.
  13590. POM may also be nil, in which case the current entry is used.
  13591. If WHICH is nil or `all', get all properties. If WHICH is
  13592. `special' or `standard', only get that subclass."
  13593. (setq which (or which 'all))
  13594. (org-with-point-at pom
  13595. (let ((clockstr (substring org-clock-string 0 -1))
  13596. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  13597. beg end range props sum-props key value)
  13598. (save-excursion
  13599. (when (condition-case nil (org-back-to-heading t) (error nil))
  13600. (setq beg (point))
  13601. (setq sum-props (get-text-property (point) 'org-summaries))
  13602. (outline-next-heading)
  13603. (setq end (point))
  13604. (when (memq which '(all special))
  13605. ;; Get the special properties, like TODO and tags
  13606. (goto-char beg)
  13607. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  13608. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13609. (when (looking-at org-priority-regexp)
  13610. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13611. (when (and (setq value (org-get-tags-string))
  13612. (string-match "\\S-" value))
  13613. (push (cons "TAGS" value) props))
  13614. (when (setq value (org-get-tags-at))
  13615. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  13616. props))
  13617. (while (re-search-forward org-keyword-time-regexp end t)
  13618. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  13619. (unless (member key excluded) (push key excluded))
  13620. (push (cons key
  13621. (if (equal key clockstr)
  13622. (org-no-properties
  13623. (org-trim
  13624. (buffer-substring
  13625. (match-beginning 2) (point-at-eol))))
  13626. (org-match-string-no-properties 2)))
  13627. props)))
  13628. (when (memq which '(all standard))
  13629. ;; Get the standard properties, like :PORP: ...
  13630. (setq range (org-get-property-block beg end))
  13631. (when range
  13632. (goto-char (car range))
  13633. (while (re-search-forward
  13634. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13635. (cdr range) t)
  13636. (setq key (org-match-string-no-properties 1)
  13637. value (org-trim (or (org-match-string-no-properties 2) "")))
  13638. (unless (member key excluded)
  13639. (push (cons key (or value "")) props)))))
  13640. (append sum-props (nreverse props)))))))
  13641. (defun org-entry-get (pom property &optional inherit)
  13642. "Get value of PROPERTY for entry at point-or-marker POM.
  13643. If INHERIT is non-nil and the entry does not have the property,
  13644. then also check higher levels of the hierarchy.
  13645. If the property is present but empty, the return value is the empty string.
  13646. If the property is not present at all, nil is returned."
  13647. (org-with-point-at pom
  13648. (if inherit
  13649. (org-entry-get-with-inheritance property)
  13650. (if (member property org-special-properties)
  13651. ;; We need a special property. Use brute force, get all properties.
  13652. (cdr (assoc property (org-entry-properties nil 'special)))
  13653. (let ((range (org-get-property-block)))
  13654. (if (and range
  13655. (goto-char (car range))
  13656. (re-search-forward
  13657. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  13658. (cdr range) t))
  13659. ;; Found the property, return it.
  13660. (if (match-end 1)
  13661. (org-match-string-no-properties 1)
  13662. "")))))))
  13663. (defun org-entry-delete (pom property)
  13664. "Delete the property PROPERTY from entry at point-or-marker POM."
  13665. (org-with-point-at pom
  13666. (if (member property org-special-properties)
  13667. nil ; cannot delete these properties.
  13668. (let ((range (org-get-property-block)))
  13669. (if (and range
  13670. (goto-char (car range))
  13671. (re-search-forward
  13672. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  13673. (cdr range) t))
  13674. (progn
  13675. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13676. t)
  13677. nil)))))
  13678. ;; Multi-values properties are properties that contain multiple values
  13679. ;; These values are assumed to be single words, separated by whitespace.
  13680. (defun org-entry-add-to-multivalued-property (pom property value)
  13681. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13682. (let* ((old (org-entry-get pom property))
  13683. (values (and old (org-split-string old "[ \t]"))))
  13684. (unless (member value values)
  13685. (setq values (cons value values))
  13686. (org-entry-put pom property
  13687. (mapconcat 'identity values " ")))))
  13688. (defun org-entry-remove-from-multivalued-property (pom property value)
  13689. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13690. (let* ((old (org-entry-get pom property))
  13691. (values (and old (org-split-string old "[ \t]"))))
  13692. (when (member value values)
  13693. (setq values (delete value values))
  13694. (org-entry-put pom property
  13695. (mapconcat 'identity values " ")))))
  13696. (defun org-entry-member-in-multivalued-property (pom property value)
  13697. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13698. (let* ((old (org-entry-get pom property))
  13699. (values (and old (org-split-string old "[ \t]"))))
  13700. (member value values)))
  13701. (defvar org-entry-property-inherited-from (make-marker))
  13702. (defun org-entry-get-with-inheritance (property)
  13703. "Get entry property, and search higher levels if not present."
  13704. (let (tmp)
  13705. (save-excursion
  13706. (save-restriction
  13707. (widen)
  13708. (catch 'ex
  13709. (while t
  13710. (when (setq tmp (org-entry-get nil property))
  13711. (org-back-to-heading t)
  13712. (move-marker org-entry-property-inherited-from (point))
  13713. (throw 'ex tmp))
  13714. (or (org-up-heading-safe) (throw 'ex nil)))))
  13715. (or tmp (cdr (assoc property org-local-properties))
  13716. (cdr (assoc property org-global-properties))))))
  13717. (defun org-entry-put (pom property value)
  13718. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13719. (org-with-point-at pom
  13720. (org-back-to-heading t)
  13721. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13722. range)
  13723. (cond
  13724. ((equal property "TODO")
  13725. (when (and (stringp value) (string-match "\\S-" value)
  13726. (not (member value org-todo-keywords-1)))
  13727. (error "\"%s\" is not a valid TODO state" value))
  13728. (if (or (not value)
  13729. (not (string-match "\\S-" value)))
  13730. (setq value 'none))
  13731. (org-todo value)
  13732. (org-set-tags nil 'align))
  13733. ((equal property "PRIORITY")
  13734. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13735. (string-to-char value) ?\ ))
  13736. (org-set-tags nil 'align))
  13737. ((equal property "SCHEDULED")
  13738. (if (re-search-forward org-scheduled-time-regexp end t)
  13739. (cond
  13740. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13741. ((eq value 'later) (org-timestamp-change 1 'day))
  13742. (t (call-interactively 'org-schedule)))
  13743. (call-interactively 'org-schedule)))
  13744. ((equal property "DEADLINE")
  13745. (if (re-search-forward org-deadline-time-regexp end t)
  13746. (cond
  13747. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13748. ((eq value 'later) (org-timestamp-change 1 'day))
  13749. (t (call-interactively 'org-deadline)))
  13750. (call-interactively 'org-deadline)))
  13751. ((member property org-special-properties)
  13752. (error "The %s property can not yet be set with `org-entry-put'"
  13753. property))
  13754. (t ; a non-special property
  13755. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13756. (setq range (org-get-property-block beg end 'force))
  13757. (goto-char (car range))
  13758. (if (re-search-forward
  13759. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  13760. (progn
  13761. (delete-region (match-beginning 1) (match-end 1))
  13762. (goto-char (match-beginning 1)))
  13763. (goto-char (cdr range))
  13764. (insert "\n")
  13765. (backward-char 1)
  13766. (org-indent-line-function)
  13767. (insert ":" property ":"))
  13768. (and value (insert " " value))
  13769. (org-indent-line-function)))))))
  13770. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  13771. "Get all property keys in the current buffer.
  13772. With INCLUDE-SPECIALS, also list the special properties that relect things
  13773. like tags and TODO state.
  13774. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13775. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  13776. (let (rtn range)
  13777. (save-excursion
  13778. (save-restriction
  13779. (widen)
  13780. (goto-char (point-min))
  13781. (while (re-search-forward org-property-start-re nil t)
  13782. (setq range (org-get-property-block))
  13783. (goto-char (car range))
  13784. (while (re-search-forward
  13785. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  13786. (cdr range) t)
  13787. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13788. (outline-next-heading))))
  13789. (when include-specials
  13790. (setq rtn (append org-special-properties rtn)))
  13791. (when include-defaults
  13792. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  13793. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13794. (defun org-property-values (key)
  13795. "Return a list of all values of property KEY."
  13796. (save-excursion
  13797. (save-restriction
  13798. (widen)
  13799. (goto-char (point-min))
  13800. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  13801. values)
  13802. (while (re-search-forward re nil t)
  13803. (add-to-list 'values (org-trim (match-string 1))))
  13804. (delete "" values)))))
  13805. (defun org-insert-property-drawer ()
  13806. "Insert a property drawer into the current entry."
  13807. (interactive)
  13808. (org-back-to-heading t)
  13809. (looking-at outline-regexp)
  13810. (let ((indent (- (match-end 0)(match-beginning 0)))
  13811. (beg (point))
  13812. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13813. end hiddenp)
  13814. (outline-next-heading)
  13815. (setq end (point))
  13816. (goto-char beg)
  13817. (while (re-search-forward re end t))
  13818. (setq hiddenp (org-invisible-p))
  13819. (end-of-line 1)
  13820. (and (equal (char-after) ?\n) (forward-char 1))
  13821. (org-skip-over-state-notes)
  13822. (skip-chars-backward " \t\n\r")
  13823. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13824. (beginning-of-line 0)
  13825. (indent-to-column indent)
  13826. (beginning-of-line 2)
  13827. (indent-to-column indent)
  13828. (beginning-of-line 0)
  13829. (if hiddenp
  13830. (save-excursion
  13831. (org-back-to-heading t)
  13832. (hide-entry))
  13833. (org-flag-drawer t))))
  13834. (defun org-set-property (property value)
  13835. "In the current entry, set PROPERTY to VALUE.
  13836. When called interactively, this will prompt for a property name, offering
  13837. completion on existing and default properties. And then it will prompt
  13838. for a value, offering competion either on allowed values (via an inherited
  13839. xxx_ALL property) or on existing values in other instances of this property
  13840. in the current file."
  13841. (interactive
  13842. (let* ((prop (completing-read
  13843. "Property: " (mapcar 'list (org-buffer-property-keys nil t))))
  13844. (cur (org-entry-get nil prop))
  13845. (allowed (org-property-get-allowed-values nil prop 'table))
  13846. (existing (mapcar 'list (org-property-values prop)))
  13847. (val (if allowed
  13848. (completing-read "Value: " allowed nil 'req-match)
  13849. (completing-read
  13850. (concat "Value" (if (and cur (string-match "\\S-" cur))
  13851. (concat "[" cur "]") "")
  13852. ": ")
  13853. existing nil nil "" nil cur))))
  13854. (list prop (if (equal val "") cur val))))
  13855. (unless (equal (org-entry-get nil property) value)
  13856. (org-entry-put nil property value)))
  13857. (defun org-delete-property (property)
  13858. "In the current entry, delete PROPERTY."
  13859. (interactive
  13860. (let* ((prop (completing-read
  13861. "Property: " (org-entry-properties nil 'standard))))
  13862. (list prop)))
  13863. (message (concat "Property " property
  13864. (if (org-entry-delete nil property)
  13865. " deleted"
  13866. " was not present in the entry"))))
  13867. (defun org-delete-property-globally (property)
  13868. "Remove PROPERTY globally, from all entries."
  13869. (interactive
  13870. (let* ((prop (completing-read
  13871. "Globally remove property: "
  13872. (mapcar 'list (org-buffer-property-keys)))))
  13873. (list prop)))
  13874. (save-excursion
  13875. (save-restriction
  13876. (widen)
  13877. (goto-char (point-min))
  13878. (let ((cnt 0))
  13879. (while (re-search-forward
  13880. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  13881. nil t)
  13882. (setq cnt (1+ cnt))
  13883. (replace-match ""))
  13884. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13885. (defvar org-columns-current-fmt-compiled) ; defined below
  13886. (defun org-compute-property-at-point ()
  13887. "Compute the property at point.
  13888. This looks for an enclosing column format, extracts the operator and
  13889. then applies it to the proerty in the column format's scope."
  13890. (interactive)
  13891. (unless (org-at-property-p)
  13892. (error "Not at a property"))
  13893. (let ((prop (org-match-string-no-properties 2)))
  13894. (org-columns-get-format-and-top-level)
  13895. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13896. (error "No operator defined for property %s" prop))
  13897. (org-columns-compute prop)))
  13898. (defun org-property-get-allowed-values (pom property &optional table)
  13899. "Get allowed values for the property PROPERTY.
  13900. When TABLE is non-nil, return an alist that can directly be used for
  13901. completion."
  13902. (let (vals)
  13903. (cond
  13904. ((equal property "TODO")
  13905. (setq vals (org-with-point-at pom
  13906. (append org-todo-keywords-1 '("")))))
  13907. ((equal property "PRIORITY")
  13908. (let ((n org-lowest-priority))
  13909. (while (>= n org-highest-priority)
  13910. (push (char-to-string n) vals)
  13911. (setq n (1- n)))))
  13912. ((member property org-special-properties))
  13913. (t
  13914. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13915. (when (and vals (string-match "\\S-" vals))
  13916. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13917. (setq vals (mapcar (lambda (x)
  13918. (cond ((stringp x) x)
  13919. ((numberp x) (number-to-string x))
  13920. ((symbolp x) (symbol-name x))
  13921. (t "???")))
  13922. vals)))))
  13923. (if table (mapcar 'list vals) vals)))
  13924. (defun org-property-previous-allowed-value (&optional previous)
  13925. "Switch to the next allowed value for this property."
  13926. (interactive)
  13927. (org-property-next-allowed-value t))
  13928. (defun org-property-next-allowed-value (&optional previous)
  13929. "Switch to the next allowed value for this property."
  13930. (interactive)
  13931. (unless (org-at-property-p)
  13932. (error "Not at a property"))
  13933. (let* ((key (match-string 2))
  13934. (value (match-string 3))
  13935. (allowed (or (org-property-get-allowed-values (point) key)
  13936. (and (member value '("[ ]" "[-]" "[X]"))
  13937. '("[ ]" "[X]"))))
  13938. nval)
  13939. (unless allowed
  13940. (error "Allowed values for this property have not been defined"))
  13941. (if previous (setq allowed (reverse allowed)))
  13942. (if (member value allowed)
  13943. (setq nval (car (cdr (member value allowed)))))
  13944. (setq nval (or nval (car allowed)))
  13945. (if (equal nval value)
  13946. (error "Only one allowed value for this property"))
  13947. (org-at-property-p)
  13948. (replace-match (concat " :" key ": " nval) t t)
  13949. (org-indent-line-function)
  13950. (beginning-of-line 1)
  13951. (skip-chars-forward " \t")))
  13952. (defun org-find-entry-with-id (ident)
  13953. "Locate the entry that contains the ID property with exact value IDENT.
  13954. IDENT can be a string, a symbol or a number, this function will search for
  13955. the string representation of it.
  13956. Return the position where this entry starts, or nil if there is no such entry."
  13957. (let ((id (cond
  13958. ((stringp ident) ident)
  13959. ((symbol-name ident) (symbol-name ident))
  13960. ((numberp ident) (number-to-string ident))
  13961. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13962. (case-fold-search nil))
  13963. (save-excursion
  13964. (save-restriction
  13965. (goto-char (point-min))
  13966. (when (re-search-forward
  13967. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13968. nil t)
  13969. (org-back-to-heading)
  13970. (point))))))
  13971. ;;; Column View
  13972. (defvar org-columns-overlays nil
  13973. "Holds the list of current column overlays.")
  13974. (defvar org-columns-current-fmt nil
  13975. "Local variable, holds the currently active column format.")
  13976. (defvar org-columns-current-fmt-compiled nil
  13977. "Local variable, holds the currently active column format.
  13978. This is the compiled version of the format.")
  13979. (defvar org-columns-current-widths nil
  13980. "Loval variable, holds the currently widths of fields.")
  13981. (defvar org-columns-current-maxwidths nil
  13982. "Loval variable, holds the currently active maximum column widths.")
  13983. (defvar org-columns-begin-marker (make-marker)
  13984. "Points to the position where last a column creation command was called.")
  13985. (defvar org-columns-top-level-marker (make-marker)
  13986. "Points to the position where current columns region starts.")
  13987. (defvar org-columns-map (make-sparse-keymap)
  13988. "The keymap valid in column display.")
  13989. (defun org-columns-content ()
  13990. "Switch to contents view while in columns view."
  13991. (interactive)
  13992. (org-overview)
  13993. (org-content))
  13994. (org-defkey org-columns-map "c" 'org-columns-content)
  13995. (org-defkey org-columns-map "o" 'org-overview)
  13996. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  13997. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  13998. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  13999. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  14000. (org-defkey org-columns-map "v" 'org-columns-show-value)
  14001. (org-defkey org-columns-map "q" 'org-columns-quit)
  14002. (org-defkey org-columns-map "r" 'org-columns-redo)
  14003. (org-defkey org-columns-map [left] 'backward-char)
  14004. (org-defkey org-columns-map "\M-b" 'backward-char)
  14005. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  14006. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  14007. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  14008. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  14009. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  14010. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  14011. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  14012. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  14013. (org-defkey org-columns-map "<" 'org-columns-narrow)
  14014. (org-defkey org-columns-map ">" 'org-columns-widen)
  14015. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  14016. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  14017. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  14018. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  14019. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  14020. '("Column"
  14021. ["Edit property" org-columns-edit-value t]
  14022. ["Next allowed value" org-columns-next-allowed-value t]
  14023. ["Previous allowed value" org-columns-previous-allowed-value t]
  14024. ["Show full value" org-columns-show-value t]
  14025. ["Edit allowed values" org-columns-edit-allowed t]
  14026. "--"
  14027. ["Edit column attributes" org-columns-edit-attributes t]
  14028. ["Increase column width" org-columns-widen t]
  14029. ["Decrease column width" org-columns-narrow t]
  14030. "--"
  14031. ["Move column right" org-columns-move-right t]
  14032. ["Move column left" org-columns-move-left t]
  14033. ["Add column" org-columns-new t]
  14034. ["Delete column" org-columns-delete t]
  14035. "--"
  14036. ["CONTENTS" org-columns-content t]
  14037. ["OVERVIEW" org-overview t]
  14038. ["Refresh columns display" org-columns-redo t]
  14039. "--"
  14040. ["Open link" org-columns-open-link t]
  14041. "--"
  14042. ["Quit" org-columns-quit t]))
  14043. (defun org-columns-new-overlay (beg end &optional string face)
  14044. "Create a new column overlay and add it to the list."
  14045. (let ((ov (org-make-overlay beg end)))
  14046. (org-overlay-put ov 'face (or face 'secondary-selection))
  14047. (org-overlay-display ov string face)
  14048. (push ov org-columns-overlays)
  14049. ov))
  14050. (defun org-columns-display-here (&optional props)
  14051. "Overlay the current line with column display."
  14052. (interactive)
  14053. (let* ((fmt org-columns-current-fmt-compiled)
  14054. (beg (point-at-bol))
  14055. (level-face (save-excursion
  14056. (beginning-of-line 1)
  14057. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  14058. (org-get-level-face 2))))
  14059. (color (list :foreground
  14060. (face-attribute (or level-face 'default) :foreground)))
  14061. props pom property ass width f string ov column val modval)
  14062. ;; Check if the entry is in another buffer.
  14063. (unless props
  14064. (if (eq major-mode 'org-agenda-mode)
  14065. (setq pom (or (get-text-property (point) 'org-hd-marker)
  14066. (get-text-property (point) 'org-marker))
  14067. props (if pom (org-entry-properties pom) nil))
  14068. (setq props (org-entry-properties nil))))
  14069. ;; Walk the format
  14070. (while (setq column (pop fmt))
  14071. (setq property (car column)
  14072. ass (if (equal property "ITEM")
  14073. (cons "ITEM"
  14074. (save-match-data
  14075. (org-no-properties
  14076. (org-remove-tabs
  14077. (buffer-substring-no-properties
  14078. (point-at-bol) (point-at-eol))))))
  14079. (assoc property props))
  14080. width (or (cdr (assoc property org-columns-current-maxwidths))
  14081. (nth 2 column)
  14082. (length property))
  14083. f (format "%%-%d.%ds | " width width)
  14084. val (or (cdr ass) "")
  14085. modval (if (equal property "ITEM")
  14086. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  14087. string (format f (or modval val)))
  14088. ;; Create the overlay
  14089. (org-unmodified
  14090. (setq ov (org-columns-new-overlay
  14091. beg (setq beg (1+ beg)) string
  14092. (list color 'org-column)))
  14093. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  14094. (org-overlay-put ov 'keymap org-columns-map)
  14095. (org-overlay-put ov 'org-columns-key property)
  14096. (org-overlay-put ov 'org-columns-value (cdr ass))
  14097. (org-overlay-put ov 'org-columns-value-modified modval)
  14098. (org-overlay-put ov 'org-columns-pom pom)
  14099. (org-overlay-put ov 'org-columns-format f))
  14100. (if (or (not (char-after beg))
  14101. (equal (char-after beg) ?\n))
  14102. (let ((inhibit-read-only t))
  14103. (save-excursion
  14104. (goto-char beg)
  14105. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  14106. ;; Make the rest of the line disappear.
  14107. (org-unmodified
  14108. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  14109. (org-overlay-put ov 'invisible t)
  14110. (org-overlay-put ov 'keymap org-columns-map)
  14111. (org-overlay-put ov 'intangible t)
  14112. (push ov org-columns-overlays)
  14113. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  14114. (org-overlay-put ov 'keymap org-columns-map)
  14115. (push ov org-columns-overlays)
  14116. (let ((inhibit-read-only t))
  14117. (put-text-property (max (point-min) (1- (point-at-bol)))
  14118. (min (point-max) (1+ (point-at-eol)))
  14119. 'read-only "Type `e' to edit property")))))
  14120. (defvar org-previous-header-line-format nil
  14121. "The header line format before column view was turned on.")
  14122. (defvar org-columns-inhibit-recalculation nil
  14123. "Inhibit recomputing of columns on column view startup.")
  14124. (defvar header-line-format)
  14125. (defun org-columns-display-here-title ()
  14126. "Overlay the newline before the current line with the table title."
  14127. (interactive)
  14128. (let ((fmt org-columns-current-fmt-compiled)
  14129. string (title "")
  14130. property width f column str widths)
  14131. (while (setq column (pop fmt))
  14132. (setq property (car column)
  14133. str (or (nth 1 column) property)
  14134. width (or (cdr (assoc property org-columns-current-maxwidths))
  14135. (nth 2 column)
  14136. (length str))
  14137. widths (push width widths)
  14138. f (format "%%-%d.%ds | " width width)
  14139. string (format f str)
  14140. title (concat title string)))
  14141. (setq title (concat
  14142. (org-add-props " " nil 'display '(space :align-to 0))
  14143. (org-add-props title nil 'face '(:weight bold :underline t))))
  14144. (org-set-local 'org-previous-header-line-format header-line-format)
  14145. (org-set-local 'org-columns-current-widths (nreverse widths))
  14146. (setq header-line-format title)))
  14147. (defun org-columns-remove-overlays ()
  14148. "Remove all currently active column overlays."
  14149. (interactive)
  14150. (when (marker-buffer org-columns-begin-marker)
  14151. (with-current-buffer (marker-buffer org-columns-begin-marker)
  14152. (when (local-variable-p 'org-previous-header-line-format)
  14153. (setq header-line-format org-previous-header-line-format)
  14154. (kill-local-variable 'org-previous-header-line-format))
  14155. (move-marker org-columns-begin-marker nil)
  14156. (move-marker org-columns-top-level-marker nil)
  14157. (org-unmodified
  14158. (mapc 'org-delete-overlay org-columns-overlays)
  14159. (setq org-columns-overlays nil)
  14160. (let ((inhibit-read-only t))
  14161. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  14162. (defun org-columns-cleanup-item (item fmt)
  14163. "Remove from ITEM what is a column in the format FMT."
  14164. (if (not org-complex-heading-regexp)
  14165. item
  14166. (when (string-match org-complex-heading-regexp item)
  14167. (concat
  14168. (org-add-props (concat (match-string 1 item) " ") nil
  14169. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  14170. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  14171. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  14172. " " (match-string 4 item)
  14173. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  14174. (defun org-columns-show-value ()
  14175. "Show the full value of the property."
  14176. (interactive)
  14177. (let ((value (get-char-property (point) 'org-columns-value)))
  14178. (message "Value is: %s" (or value ""))))
  14179. (defun org-columns-quit ()
  14180. "Remove the column overlays and in this way exit column editing."
  14181. (interactive)
  14182. (org-unmodified
  14183. (org-columns-remove-overlays)
  14184. (let ((inhibit-read-only t))
  14185. (remove-text-properties (point-min) (point-max) '(read-only t))))
  14186. (when (eq major-mode 'org-agenda-mode)
  14187. (message
  14188. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  14189. (defun org-columns-check-computed ()
  14190. "Check if this column value is computed.
  14191. If yes, throw an error indicating that changing it does not make sense."
  14192. (let ((val (get-char-property (point) 'org-columns-value)))
  14193. (when (and (stringp val)
  14194. (get-char-property 0 'org-computed val))
  14195. (error "This value is computed from the entry's children"))))
  14196. (defun org-columns-todo (&optional arg)
  14197. "Change the TODO state during column view."
  14198. (interactive "P")
  14199. (org-columns-edit-value "TODO"))
  14200. (defun org-columns-set-tags-or-toggle (&optional arg)
  14201. "Toggle checkbox at point, or set tags for current headline."
  14202. (interactive "P")
  14203. (if (string-match "\\`\\[[ xX-]\\]\\'"
  14204. (get-char-property (point) 'org-columns-value))
  14205. (org-columns-next-allowed-value)
  14206. (org-columns-edit-value "TAGS")))
  14207. (defun org-columns-edit-value (&optional key)
  14208. "Edit the value of the property at point in column view.
  14209. Where possible, use the standard interface for changing this line."
  14210. (interactive)
  14211. (org-columns-check-computed)
  14212. (let* ((external-key key)
  14213. (col (current-column))
  14214. (key (or key (get-char-property (point) 'org-columns-key)))
  14215. (value (get-char-property (point) 'org-columns-value))
  14216. (bol (point-at-bol)) (eol (point-at-eol))
  14217. (pom (or (get-text-property bol 'org-hd-marker)
  14218. (point))) ; keep despite of compiler waring
  14219. (line-overlays
  14220. (delq nil (mapcar (lambda (x)
  14221. (and (eq (overlay-buffer x) (current-buffer))
  14222. (>= (overlay-start x) bol)
  14223. (<= (overlay-start x) eol)
  14224. x))
  14225. org-columns-overlays)))
  14226. nval eval allowed)
  14227. (cond
  14228. ((equal key "ITEM")
  14229. (setq eval '(org-with-point-at pom
  14230. (org-edit-headline))))
  14231. ((equal key "TODO")
  14232. (setq eval '(org-with-point-at pom
  14233. (let ((current-prefix-arg
  14234. (if external-key current-prefix-arg '(4))))
  14235. (call-interactively 'org-todo)))))
  14236. ((equal key "PRIORITY")
  14237. (setq eval '(org-with-point-at pom
  14238. (call-interactively 'org-priority))))
  14239. ((equal key "TAGS")
  14240. (setq eval '(org-with-point-at pom
  14241. (let ((org-fast-tag-selection-single-key
  14242. (if (eq org-fast-tag-selection-single-key 'expert)
  14243. t org-fast-tag-selection-single-key)))
  14244. (call-interactively 'org-set-tags)))))
  14245. ((equal key "DEADLINE")
  14246. (setq eval '(org-with-point-at pom
  14247. (call-interactively 'org-deadline))))
  14248. ((equal key "SCHEDULED")
  14249. (setq eval '(org-with-point-at pom
  14250. (call-interactively 'org-schedule))))
  14251. (t
  14252. (setq allowed (org-property-get-allowed-values pom key 'table))
  14253. (if allowed
  14254. (setq nval (completing-read "Value: " allowed nil t))
  14255. (setq nval (read-string "Edit: " value)))
  14256. (setq nval (org-trim nval))
  14257. (when (not (equal nval value))
  14258. (setq eval '(org-entry-put pom key nval)))))
  14259. (when eval
  14260. (let ((inhibit-read-only t))
  14261. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  14262. (unwind-protect
  14263. (progn
  14264. (setq org-columns-overlays
  14265. (org-delete-all line-overlays org-columns-overlays))
  14266. (mapc 'org-delete-overlay line-overlays)
  14267. (org-columns-eval eval))
  14268. (org-columns-display-here))))
  14269. (move-to-column col)
  14270. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  14271. (org-columns-update key))))
  14272. (defun org-edit-headline () ; FIXME: this is not columns specific
  14273. "Edit the current headline, the part without TODO keyword, TAGS."
  14274. (org-back-to-heading)
  14275. (when (looking-at org-todo-line-regexp)
  14276. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  14277. (txt (match-string 3))
  14278. (post "")
  14279. txt2)
  14280. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  14281. (setq post (match-string 0 txt)
  14282. txt (substring txt 0 (match-beginning 0))))
  14283. (setq txt2 (read-string "Edit: " txt))
  14284. (when (not (equal txt txt2))
  14285. (beginning-of-line 1)
  14286. (insert pre txt2 post)
  14287. (delete-region (point) (point-at-eol))
  14288. (org-set-tags nil t)))))
  14289. (defun org-columns-edit-allowed ()
  14290. "Edit the list of allowed values for the current property."
  14291. (interactive)
  14292. (let* ((key (get-char-property (point) 'org-columns-key))
  14293. (key1 (concat key "_ALL"))
  14294. (allowed (org-entry-get (point) key1 t))
  14295. nval)
  14296. ;; FIXME: Cover editing TODO, TAGS etc inbiffer settings.????
  14297. (setq nval (read-string "Allowed: " allowed))
  14298. (org-entry-put
  14299. (cond ((marker-position org-entry-property-inherited-from)
  14300. org-entry-property-inherited-from)
  14301. ((marker-position org-columns-top-level-marker)
  14302. org-columns-top-level-marker))
  14303. key1 nval)))
  14304. (defun org-columns-eval (form)
  14305. (let (hidep)
  14306. (save-excursion
  14307. (beginning-of-line 1)
  14308. ;; `next-line' is needed here, because it skips invisible line.
  14309. (condition-case nil (next-line 1) (error nil))
  14310. (setq hidep (org-on-heading-p 1)))
  14311. (eval form)
  14312. (and hidep (hide-entry))))
  14313. (defun org-columns-previous-allowed-value ()
  14314. "Switch to the previous allowed value for this column."
  14315. (interactive)
  14316. (org-columns-next-allowed-value t))
  14317. (defun org-columns-next-allowed-value (&optional previous)
  14318. "Switch to the next allowed value for this column."
  14319. (interactive)
  14320. (org-columns-check-computed)
  14321. (let* ((col (current-column))
  14322. (key (get-char-property (point) 'org-columns-key))
  14323. (value (get-char-property (point) 'org-columns-value))
  14324. (bol (point-at-bol)) (eol (point-at-eol))
  14325. (pom (or (get-text-property bol 'org-hd-marker)
  14326. (point))) ; keep despite of compiler waring
  14327. (line-overlays
  14328. (delq nil (mapcar (lambda (x)
  14329. (and (eq (overlay-buffer x) (current-buffer))
  14330. (>= (overlay-start x) bol)
  14331. (<= (overlay-start x) eol)
  14332. x))
  14333. org-columns-overlays)))
  14334. (allowed (or (org-property-get-allowed-values pom key)
  14335. (and (equal
  14336. (nth 4 (assoc key org-columns-current-fmt-compiled))
  14337. 'checkbox) '("[ ]" "[X]"))))
  14338. nval)
  14339. (when (equal key "ITEM")
  14340. (error "Cannot edit item headline from here"))
  14341. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  14342. (error "Allowed values for this property have not been defined"))
  14343. (if (member key '("SCHEDULED" "DEADLINE"))
  14344. (setq nval (if previous 'earlier 'later))
  14345. (if previous (setq allowed (reverse allowed)))
  14346. (if (member value allowed)
  14347. (setq nval (car (cdr (member value allowed)))))
  14348. (setq nval (or nval (car allowed)))
  14349. (if (equal nval value)
  14350. (error "Only one allowed value for this property")))
  14351. (let ((inhibit-read-only t))
  14352. (remove-text-properties (1- bol) eol '(read-only t))
  14353. (unwind-protect
  14354. (progn
  14355. (setq org-columns-overlays
  14356. (org-delete-all line-overlays org-columns-overlays))
  14357. (mapc 'org-delete-overlay line-overlays)
  14358. (org-columns-eval '(org-entry-put pom key nval)))
  14359. (org-columns-display-here)))
  14360. (move-to-column col)
  14361. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  14362. (org-columns-update key))))
  14363. (defun org-verify-version (task)
  14364. (cond
  14365. ((eq task 'columns)
  14366. (if (or (featurep 'xemacs)
  14367. (< emacs-major-version 22))
  14368. (error "Emacs 22 is required for the columns feature")))))
  14369. (defun org-columns-open-link (&optional arg)
  14370. (interactive "P")
  14371. (let ((key (get-char-property (point) 'org-columns-key))
  14372. (value (get-char-property (point) 'org-columns-value)))
  14373. (org-open-link-from-string arg)))
  14374. (defun org-open-link-from-string (s &optional arg)
  14375. "Open a link in the string S, as if it was in Org-mode."
  14376. (interactive)
  14377. (with-temp-buffer
  14378. (let ((org-inhibit-startup t))
  14379. (org-mode)
  14380. (insert s)
  14381. (goto-char (point-min))
  14382. (org-open-at-point arg))))
  14383. (defun org-columns-get-format-and-top-level ()
  14384. (let (fmt)
  14385. (when (condition-case nil (org-back-to-heading) (error nil))
  14386. (move-marker org-entry-property-inherited-from nil)
  14387. (setq fmt (org-entry-get nil "COLUMNS" t)))
  14388. (setq fmt (or fmt org-columns-default-format))
  14389. (org-set-local 'org-columns-current-fmt fmt)
  14390. (org-columns-compile-format fmt)
  14391. (if (marker-position org-entry-property-inherited-from)
  14392. (move-marker org-columns-top-level-marker
  14393. org-entry-property-inherited-from)
  14394. (move-marker org-columns-top-level-marker (point)))
  14395. fmt))
  14396. (defun org-columns ()
  14397. "Turn on column view on an org-mode file."
  14398. (interactive)
  14399. (org-verify-version 'columns)
  14400. (org-columns-remove-overlays)
  14401. (move-marker org-columns-begin-marker (point))
  14402. (let (beg end fmt cache maxwidths)
  14403. (setq fmt (org-columns-get-format-and-top-level))
  14404. (save-excursion
  14405. (goto-char org-columns-top-level-marker)
  14406. (setq beg (point))
  14407. (unless org-columns-inhibit-recalculation
  14408. (org-columns-compute-all))
  14409. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  14410. (point-max)))
  14411. (goto-char beg)
  14412. ;; Get and cache the properties
  14413. (while (re-search-forward (concat "^" outline-regexp) end t)
  14414. (push (cons (org-current-line) (org-entry-properties)) cache))
  14415. (when cache
  14416. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  14417. (org-set-local 'org-columns-current-maxwidths maxwidths)
  14418. (org-columns-display-here-title)
  14419. (mapc (lambda (x)
  14420. (goto-line (car x))
  14421. (org-columns-display-here (cdr x)))
  14422. cache)))))
  14423. (defun org-columns-new (&optional prop title width op fmt)
  14424. "Insert a new column, to the leeft o the current column."
  14425. (interactive)
  14426. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  14427. cell)
  14428. (setq prop (completing-read
  14429. "Property: " (mapcar 'list (org-buffer-property-keys t))
  14430. nil nil prop))
  14431. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  14432. (setq width (read-string "Column width: " (if width (number-to-string width))))
  14433. (if (string-match "\\S-" width)
  14434. (setq width (string-to-number width))
  14435. (setq width nil))
  14436. (setq fmt (completing-read "Summary [none]: "
  14437. '(("none") ("add_numbers") ("add_times") ("checkbox"))
  14438. nil t))
  14439. (if (string-match "\\S-" fmt)
  14440. (setq fmt (intern fmt))
  14441. (setq fmt nil))
  14442. (if (eq fmt 'none) (setq fmt nil))
  14443. (if editp
  14444. (progn
  14445. (setcar editp prop)
  14446. (setcdr editp (list title width nil fmt)))
  14447. (setq cell (nthcdr (1- (current-column))
  14448. org-columns-current-fmt-compiled))
  14449. (setcdr cell (cons (list prop title width nil fmt)
  14450. (cdr cell))))
  14451. (org-columns-store-format)
  14452. (org-columns-redo)))
  14453. (defun org-columns-delete ()
  14454. "Delete the column at point from columns view."
  14455. (interactive)
  14456. (let* ((n (current-column))
  14457. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  14458. (when (y-or-n-p
  14459. (format "Are you sure you want to remove column \"%s\"? " title))
  14460. (setq org-columns-current-fmt-compiled
  14461. (delq (nth n org-columns-current-fmt-compiled)
  14462. org-columns-current-fmt-compiled))
  14463. (org-columns-store-format)
  14464. (org-columns-redo)
  14465. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  14466. (backward-char 1)))))
  14467. (defun org-columns-edit-attributes ()
  14468. "Edit the attributes of the current column."
  14469. (interactive)
  14470. (let* ((n (current-column))
  14471. (info (nth n org-columns-current-fmt-compiled)))
  14472. (apply 'org-columns-new info)))
  14473. (defun org-columns-widen (arg)
  14474. "Make the column wider by ARG characters."
  14475. (interactive "p")
  14476. (let* ((n (current-column))
  14477. (entry (nth n org-columns-current-fmt-compiled))
  14478. (width (or (nth 2 entry)
  14479. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  14480. (setq width (max 1 (+ width arg)))
  14481. (setcar (nthcdr 2 entry) width)
  14482. (org-columns-store-format)
  14483. (org-columns-redo)))
  14484. (defun org-columns-narrow (arg)
  14485. "Make the column nrrower by ARG characters."
  14486. (interactive "p")
  14487. (org-columns-widen (- arg)))
  14488. (defun org-columns-move-right ()
  14489. "Swap this column with the one to the right."
  14490. (interactive)
  14491. (let* ((n (current-column))
  14492. (cell (nthcdr n org-columns-current-fmt-compiled))
  14493. e)
  14494. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  14495. (error "Cannot shift this column further to the right"))
  14496. (setq e (car cell))
  14497. (setcar cell (car (cdr cell)))
  14498. (setcdr cell (cons e (cdr (cdr cell))))
  14499. (org-columns-store-format)
  14500. (org-columns-redo)
  14501. (forward-char 1)))
  14502. (defun org-columns-move-left ()
  14503. "Swap this column with the one to the left."
  14504. (interactive)
  14505. (let* ((n (current-column)))
  14506. (when (= n 0)
  14507. (error "Cannot shift this column further to the left"))
  14508. (backward-char 1)
  14509. (org-columns-move-right)
  14510. (backward-char 1)))
  14511. (defun org-columns-store-format ()
  14512. "Store the text version of the current columns format in appropriate place.
  14513. This is either in the COLUMNS property of the node starting the current column
  14514. display, or in the #+COLUMNS line of the current buffer."
  14515. (let (fmt (cnt 0))
  14516. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  14517. (org-set-local 'org-columns-current-fmt fmt)
  14518. (if (marker-position org-columns-top-level-marker)
  14519. (save-excursion
  14520. (goto-char org-columns-top-level-marker)
  14521. (if (and (org-at-heading-p)
  14522. (org-entry-get nil "COLUMNS"))
  14523. (org-entry-put nil "COLUMNS" fmt)
  14524. (goto-char (point-min))
  14525. ;; Overwrite all #+COLUMNS lines....
  14526. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  14527. (setq cnt (1+ cnt))
  14528. (replace-match (concat "#+COLUMNS: " fmt) t t))
  14529. (unless (> cnt 0)
  14530. (goto-char (point-min))
  14531. (or (org-on-heading-p t) (outline-next-heading))
  14532. (let ((inhibit-read-only t))
  14533. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  14534. (org-set-local 'org-columns-default-format fmt))))))
  14535. (defvar org-overriding-columns-format nil
  14536. "When set, overrides any other definition.")
  14537. (defvar org-agenda-view-columns-initially nil
  14538. "When set, switch to columns view immediately after creating the agenda.")
  14539. (defun org-agenda-columns ()
  14540. "Turn on column view in the agenda."
  14541. (interactive)
  14542. (org-verify-version 'columns)
  14543. (org-columns-remove-overlays)
  14544. (move-marker org-columns-begin-marker (point))
  14545. (let (fmt cache maxwidths m)
  14546. (cond
  14547. ((and (local-variable-p 'org-overriding-columns-format)
  14548. org-overriding-columns-format)
  14549. (setq fmt org-overriding-columns-format))
  14550. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  14551. (setq fmt (org-entry-get m "COLUMNS" t)))
  14552. ((and (boundp 'org-columns-current-fmt)
  14553. (local-variable-p 'org-columns-current-fmt)
  14554. org-columns-current-fmt)
  14555. (setq fmt org-columns-current-fmt))
  14556. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  14557. (setq m (get-text-property m 'org-hd-marker))
  14558. (setq fmt (org-entry-get m "COLUMNS" t))))
  14559. (setq fmt (or fmt org-columns-default-format))
  14560. (org-set-local 'org-columns-current-fmt fmt)
  14561. (org-columns-compile-format fmt)
  14562. (save-excursion
  14563. ;; Get and cache the properties
  14564. (goto-char (point-min))
  14565. (while (not (eobp))
  14566. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  14567. (get-text-property (point) 'org-marker)))
  14568. (push (cons (org-current-line) (org-entry-properties m)) cache))
  14569. (beginning-of-line 2))
  14570. (when cache
  14571. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  14572. (org-set-local 'org-columns-current-maxwidths maxwidths)
  14573. (org-columns-display-here-title)
  14574. (mapc (lambda (x)
  14575. (goto-line (car x))
  14576. (org-columns-display-here (cdr x)))
  14577. cache)))))
  14578. (defun org-columns-get-autowidth-alist (s cache)
  14579. "Derive the maximum column widths from the format and the cache."
  14580. (let ((start 0) rtn)
  14581. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  14582. (push (cons (match-string 1 s) 1) rtn)
  14583. (setq start (match-end 0)))
  14584. (mapc (lambda (x)
  14585. (setcdr x (apply 'max
  14586. (mapcar
  14587. (lambda (y)
  14588. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  14589. cache))))
  14590. rtn)
  14591. rtn))
  14592. (defun org-columns-compute-all ()
  14593. "Compute all columns that have operators defined."
  14594. (org-unmodified
  14595. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  14596. (let ((columns org-columns-current-fmt-compiled) col)
  14597. (while (setq col (pop columns))
  14598. (when (nth 3 col)
  14599. (save-excursion
  14600. (org-columns-compute (car col)))))))
  14601. (defun org-columns-update (property)
  14602. "Recompute PROPERTY, and update the columns display for it."
  14603. (org-columns-compute property)
  14604. (let (fmt val pos)
  14605. (save-excursion
  14606. (mapc (lambda (ov)
  14607. (when (equal (org-overlay-get ov 'org-columns-key) property)
  14608. (setq pos (org-overlay-start ov))
  14609. (goto-char pos)
  14610. (when (setq val (cdr (assoc property
  14611. (get-text-property
  14612. (point-at-bol) 'org-summaries))))
  14613. (setq fmt (org-overlay-get ov 'org-columns-format))
  14614. (org-overlay-put ov 'org-columns-value val)
  14615. (org-overlay-put ov 'display (format fmt val)))))
  14616. org-columns-overlays))))
  14617. (defun org-columns-compute (property)
  14618. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  14619. (interactive)
  14620. (let* ((re (concat "^" outline-regexp))
  14621. (lmax 30) ; Does anyone use deeper levels???
  14622. (lsum (make-vector lmax 0))
  14623. (lflag (make-vector lmax nil))
  14624. (level 0)
  14625. (ass (assoc property org-columns-current-fmt-compiled))
  14626. (format (nth 4 ass))
  14627. (beg org-columns-top-level-marker)
  14628. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  14629. (save-excursion
  14630. ;; Find the region to compute
  14631. (goto-char beg)
  14632. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  14633. (goto-char end)
  14634. ;; Walk the tree from the back and do the computations
  14635. (while (re-search-backward re beg t)
  14636. (setq sumpos (match-beginning 0)
  14637. last-level level
  14638. level (org-outline-level)
  14639. val (org-entry-get nil property)
  14640. valflag (and val (string-match "\\S-" val)))
  14641. (cond
  14642. ((< level last-level)
  14643. ;; put the sum of lower levels here as a property
  14644. (setq sum (aref lsum last-level) ; current sum
  14645. flag (aref lflag last-level) ; any valid entries from children?
  14646. str (org-column-number-to-string sum format)
  14647. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  14648. useval (if flag str1 (if valflag val ""))
  14649. sum-alist (get-text-property sumpos 'org-summaries))
  14650. (if (assoc property sum-alist)
  14651. (setcdr (assoc property sum-alist) useval)
  14652. (push (cons property useval) sum-alist)
  14653. (org-unmodified
  14654. (add-text-properties sumpos (1+ sumpos)
  14655. (list 'org-summaries sum-alist))))
  14656. (when val
  14657. (org-entry-put nil property (if flag str val)))
  14658. ;; add current to current level accumulator
  14659. (when (or flag valflag)
  14660. ;; FIXME: is this ok?????????
  14661. (aset lsum level (+ (aref lsum level)
  14662. (if flag sum (org-column-string-to-number
  14663. (if flag str val) format))))
  14664. (aset lflag level t))
  14665. ;; clear accumulators for deeper levels
  14666. (loop for l from (1+ level) to (1- lmax) do
  14667. (aset lsum l 0)
  14668. (aset lflag l nil)))
  14669. ((>= level last-level)
  14670. ;; add what we have here to the accumulator for this level
  14671. (aset lsum level (+ (aref lsum level)
  14672. (org-column-string-to-number (or val "0") format)))
  14673. (and valflag (aset lflag level t)))
  14674. (t (error "This should not happen")))))))
  14675. (defun org-columns-redo ()
  14676. "Construct the column display again."
  14677. (interactive)
  14678. (message "Recomputing columns...")
  14679. (save-excursion
  14680. (if (marker-position org-columns-begin-marker)
  14681. (goto-char org-columns-begin-marker))
  14682. (org-columns-remove-overlays)
  14683. (if (org-mode-p)
  14684. (call-interactively 'org-columns)
  14685. (call-interactively 'org-agenda-columns)))
  14686. (message "Recomputing columns...done"))
  14687. (defun org-columns-not-in-agenda ()
  14688. (if (eq major-mode 'org-agenda-mode)
  14689. (error "This command is only allowed in Org-mode buffers")))
  14690. (defun org-string-to-number (s)
  14691. "Convert string to number, and interpret hh:mm:ss."
  14692. (if (not (string-match ":" s))
  14693. (string-to-number s)
  14694. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  14695. (while l
  14696. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  14697. sum)))
  14698. (defun org-column-number-to-string (n fmt)
  14699. "Convert a computed column number to a string value, according to FMT."
  14700. (cond
  14701. ((eq fmt 'add_times)
  14702. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  14703. (format "%d:%02d" h m)))
  14704. ((eq fmt 'checkbox)
  14705. (cond ((= n (floor n)) "[X]")
  14706. ((> n 1.) "[-]")
  14707. (t "[ ]")))
  14708. (t (number-to-string n))))
  14709. (defun org-column-string-to-number (s fmt)
  14710. "Convert a column value to a number that can be used for column computing."
  14711. (cond
  14712. ((string-match ":" s)
  14713. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  14714. (while l
  14715. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  14716. sum))
  14717. ((eq fmt 'checkbox)
  14718. (if (equal s "[X]") 1. 0.000001))
  14719. (t (string-to-number s))))
  14720. (defun org-columns-uncompile-format (cfmt)
  14721. "Turn the compiled columns format back into a string representation."
  14722. (let ((rtn "") e s prop title op width fmt)
  14723. (while (setq e (pop cfmt))
  14724. (setq prop (car e)
  14725. title (nth 1 e)
  14726. width (nth 2 e)
  14727. op (nth 3 e)
  14728. fmt (nth 4 e))
  14729. (cond
  14730. ((eq fmt 'add_times) (setq op ":"))
  14731. ((eq fmt 'checkbox) (setq op "X"))
  14732. ((eq fmt 'add_numbers) (setq op "+")))
  14733. (if (equal title prop) (setq title nil))
  14734. (setq s (concat "%" (if width (number-to-string width))
  14735. prop
  14736. (if title (concat "(" title ")"))
  14737. (if op (concat "{" op "}"))))
  14738. (setq rtn (concat rtn " " s)))
  14739. (org-trim rtn)))
  14740. (defun org-columns-compile-format (fmt)
  14741. "Turn a column format string into an alist of specifications.
  14742. The alist has one entry for each column in the format. The elements of
  14743. that list are:
  14744. property the property
  14745. title the title field for the columns
  14746. width the column width in characters, can be nil for automatic
  14747. operator the operator if any
  14748. format the output format for computed results, derived from operator"
  14749. (let ((start 0) width prop title op f)
  14750. (setq org-columns-current-fmt-compiled nil)
  14751. (while (string-match
  14752. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  14753. fmt start)
  14754. (setq start (match-end 0)
  14755. width (match-string 1 fmt)
  14756. prop (match-string 2 fmt)
  14757. title (or (match-string 3 fmt) prop)
  14758. op (match-string 4 fmt)
  14759. f nil)
  14760. (if width (setq width (string-to-number width)))
  14761. (cond
  14762. ((equal op "+") (setq f 'add_numbers))
  14763. ((equal op ":") (setq f 'add_times))
  14764. ((equal op "X") (setq f 'checkbox)))
  14765. (push (list prop title width op f) org-columns-current-fmt-compiled))
  14766. (setq org-columns-current-fmt-compiled
  14767. (nreverse org-columns-current-fmt-compiled))))
  14768. ;;; Dynamic block for Column view
  14769. (defun org-columns-capture-view ()
  14770. "Get the column view of the current buffer and return it as a list.
  14771. The list will contains the title row and all other rows. Each row is
  14772. a list of fields."
  14773. (save-excursion
  14774. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  14775. (n (length title)) row tbl)
  14776. (goto-char (point-min))
  14777. (while (re-search-forward "^\\*+ " nil t)
  14778. (when (get-char-property (match-beginning 0) 'org-columns-key)
  14779. (setq row nil)
  14780. (loop for i from 0 to (1- n) do
  14781. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  14782. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  14783. "")
  14784. row))
  14785. (setq row (nreverse row))
  14786. (push row tbl)))
  14787. (append (list title 'hline) (nreverse tbl)))))
  14788. (defun org-dblock-write:columnview (params)
  14789. "Write the column view table.
  14790. PARAMS is a property list of parameters:
  14791. :width enforce same column widths with <N> specifiers.
  14792. :id the :ID: property of the entry where the columns view
  14793. should be built, as a string. When `local', call locally.
  14794. When `global' call column view with the cursor at the beginning
  14795. of the buffer (usually this means that the whole buffer switches
  14796. to column view).
  14797. :hlines When t, insert a hline before each item. When a number, insert
  14798. a hline before each level <= that number.
  14799. :vlines When t, make each column a colgroup to enforce vertical lines."
  14800. (let ((pos (move-marker (make-marker) (point)))
  14801. (hlines (plist-get params :hlines))
  14802. (vlines (plist-get params :vlines))
  14803. tbl id idpos nfields tmp)
  14804. (save-excursion
  14805. (save-restriction
  14806. (when (setq id (plist-get params :id))
  14807. (cond ((not id) nil)
  14808. ((eq id 'global) (goto-char (point-min)))
  14809. ((eq id 'local) nil)
  14810. ((setq idpos (org-find-entry-with-id id))
  14811. (goto-char idpos))
  14812. (t (error "Cannot find entry with :ID: %s" id))))
  14813. (org-columns)
  14814. (setq tbl (org-columns-capture-view))
  14815. (setq nfields (length (car tbl)))
  14816. (org-columns-quit)))
  14817. (goto-char pos)
  14818. (move-marker pos nil)
  14819. (when tbl
  14820. (when (plist-get params :hlines)
  14821. (setq tmp nil)
  14822. (while tbl
  14823. (if (eq (car tbl) 'hline)
  14824. (push (pop tbl) tmp)
  14825. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  14826. (if (and (not (eq (car tmp) 'hline))
  14827. (or (eq hlines t)
  14828. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  14829. (push 'hline tmp)))
  14830. (push (pop tbl) tmp)))
  14831. (setq tbl (nreverse tmp)))
  14832. (when vlines
  14833. (setq tbl (mapcar (lambda (x)
  14834. (if (eq 'hline x) x (cons "" x)))
  14835. tbl))
  14836. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  14837. (setq pos (point))
  14838. (insert (org-listtable-to-string tbl))
  14839. (when (plist-get params :width)
  14840. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  14841. org-columns-current-widths "|")))
  14842. (goto-char pos)
  14843. (org-table-align))))
  14844. (defun org-listtable-to-string (tbl)
  14845. "Convert a listtable TBL to a string that contains the Org-mode table.
  14846. The table still need to be alligned. The resulting string has no leading
  14847. and tailing newline characters."
  14848. (mapconcat
  14849. (lambda (x)
  14850. (cond
  14851. ((listp x)
  14852. (concat "|" (mapconcat 'identity x "|") "|"))
  14853. ((eq x 'hline) "|-|")
  14854. (t (error "Garbage in listtable: %s" x))))
  14855. tbl "\n"))
  14856. (defun org-insert-columns-dblock ()
  14857. "Create a dynamic block capturing a column view table."
  14858. (interactive)
  14859. (let ((defaults '(:name "columnview" :hlines 1))
  14860. (id (completing-read
  14861. "Capture columns (local, global, entry with :ID: property) [local]: "
  14862. (append '(("global") ("local"))
  14863. (mapcar 'list (org-property-values "ID"))))))
  14864. (if (equal id "") (setq id 'local))
  14865. (if (equal id "global") (setq id 'global))
  14866. (setq defaults (append defaults (list :id id)))
  14867. (org-create-dblock defaults)
  14868. (org-update-dblock)))
  14869. ;;;; Timestamps
  14870. (defvar org-last-changed-timestamp nil)
  14871. (defvar org-time-was-given) ; dynamically scoped parameter
  14872. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14873. (defvar org-ts-what) ; dynamically scoped parameter
  14874. (defun org-time-stamp (arg)
  14875. "Prompt for a date/time and insert a time stamp.
  14876. If the user specifies a time like HH:MM, or if this command is called
  14877. with a prefix argument, the time stamp will contain date and time.
  14878. Otherwise, only the date will be included. All parts of a date not
  14879. specified by the user will be filled in from the current date/time.
  14880. So if you press just return without typing anything, the time stamp
  14881. will represent the current date/time. If there is already a timestamp
  14882. at the cursor, it will be modified."
  14883. (interactive "P")
  14884. (let ((default-time
  14885. ;; Default time is either today, or, when entering a range,
  14886. ;; the range start.
  14887. (if (or (org-at-timestamp-p t)
  14888. (save-excursion
  14889. (re-search-backward
  14890. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14891. (- (point) 20) t)))
  14892. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14893. (current-time)))
  14894. org-time-was-given org-end-time-was-given time)
  14895. (cond
  14896. ((and (org-at-timestamp-p)
  14897. (eq last-command 'org-time-stamp)
  14898. (eq this-command 'org-time-stamp))
  14899. (insert "--")
  14900. (setq time (let ((this-command this-command))
  14901. (org-read-date arg 'totime nil nil default-time)))
  14902. (org-insert-time-stamp time (or org-time-was-given arg)))
  14903. ((org-at-timestamp-p)
  14904. (setq time (let ((this-command this-command))
  14905. (org-read-date arg 'totime nil nil default-time)))
  14906. (when (org-at-timestamp-p) ; just to get the match data
  14907. (replace-match "")
  14908. (setq org-last-changed-timestamp
  14909. (org-insert-time-stamp
  14910. time (or org-time-was-given arg)
  14911. nil nil nil (list org-end-time-was-given))))
  14912. (message "Timestamp updated"))
  14913. (t
  14914. (setq time (let ((this-command this-command))
  14915. (org-read-date arg 'totime nil nil default-time)))
  14916. (org-insert-time-stamp time (or org-time-was-given arg)
  14917. nil nil nil (list org-end-time-was-given))))))
  14918. (defun org-time-stamp-inactive (&optional arg)
  14919. "Insert an inactive time stamp.
  14920. An inactive time stamp is enclosed in square brackets instead of angle
  14921. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14922. does not link to the calendar and cannot be changed with the S-cursor keys.
  14923. So these are more for recording a certain time/date."
  14924. (interactive "P")
  14925. (let (org-time-was-given org-end-time-was-given time)
  14926. (setq time (org-read-date arg 'totime))
  14927. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  14928. nil nil (list org-end-time-was-given))))
  14929. (defvar org-date-ovl (org-make-overlay 1 1))
  14930. (org-overlay-put org-date-ovl 'face 'org-warning)
  14931. (org-detach-overlay org-date-ovl)
  14932. (defvar org-ans1) ; dynamically scoped parameter
  14933. (defvar org-ans2) ; dynamically scoped parameter
  14934. (defvar org-plain-time-of-day-regexp) ; defined below
  14935. (defun org-read-date (&optional with-time to-time from-string prompt
  14936. default-time)
  14937. "Read a date and make things smooth for the user.
  14938. The prompt will suggest to enter an ISO date, but you can also enter anything
  14939. which will at least partially be understood by `parse-time-string'.
  14940. Unrecognized parts of the date will default to the current day, month, year,
  14941. hour and minute. If this command is called to replace a timestamp at point,
  14942. of to enter the second timestamp of a range, the default time is taken from the
  14943. existing stamp. For example,
  14944. 3-2-5 --> 2003-02-05
  14945. feb 15 --> currentyear-02-15
  14946. sep 12 9 --> 2009-09-12
  14947. 12:45 --> today 12:45
  14948. 22 sept 0:34 --> currentyear-09-22 0:34
  14949. 12 --> currentyear-currentmonth-12
  14950. Fri --> nearest Friday (today or later)
  14951. etc.
  14952. Furthermore you can specify a relative date by giving, as the *first* thing
  14953. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  14954. change in days weeks, months, years.
  14955. With a single plus or minus, the date is relative to today. With a double
  14956. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14957. +4d --> four days from today
  14958. +4 --> same as above
  14959. +2w --> two weeks from today
  14960. ++5 --> five days from default date
  14961. The function understands only English month and weekday abbreviations,
  14962. but this can be configured with the variables `parse-time-months' and
  14963. `parse-time-weekdays'.
  14964. While prompting, a calendar is popped up - you can also select the
  14965. date with the mouse (button 1). The calendar shows a period of three
  14966. months. To scroll it to other months, use the keys `>' and `<'.
  14967. If you don't like the calendar, turn it off with
  14968. \(setq org-popup-calendar-for-date-prompt nil)
  14969. With optional argument TO-TIME, the date will immediately be converted
  14970. to an internal time.
  14971. With an optional argument WITH-TIME, the prompt will suggest to also
  14972. insert a time. Note that when WITH-TIME is not set, you can still
  14973. enter a time, and this function will inform the calling routine about
  14974. this change. The calling routine may then choose to change the format
  14975. used to insert the time stamp into the buffer to include the time.
  14976. With optional argument FROM-STRING, read fomr this string instead from
  14977. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14978. the time/date that is used for everything that is not specified by the
  14979. user."
  14980. (require 'parse-time)
  14981. (let* ((org-time-stamp-rounding-minutes
  14982. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  14983. (ct (org-current-time))
  14984. (def (or default-time ct))
  14985. (defdecode (decode-time def))
  14986. (calendar-move-hook nil)
  14987. (view-diary-entries-initially nil)
  14988. (view-calendar-holidays-initially nil)
  14989. (timestr (format-time-string
  14990. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  14991. (prompt (concat (if prompt (concat prompt " ") "")
  14992. (format "Date and/or time (default [%s]): " timestr)))
  14993. ans (org-ans0 "") org-ans1 org-ans2 delta deltan deltaw deltadef
  14994. second minute hour day month year tl wday wday1 pm h2 m2)
  14995. (cond
  14996. (from-string (setq ans from-string))
  14997. (org-popup-calendar-for-date-prompt
  14998. (save-excursion
  14999. (save-window-excursion
  15000. (calendar)
  15001. (calendar-forward-day (- (time-to-days def)
  15002. (calendar-absolute-from-gregorian
  15003. (calendar-current-date))))
  15004. (org-eval-in-calendar nil t)
  15005. (let* ((old-map (current-local-map))
  15006. (map (copy-keymap calendar-mode-map))
  15007. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  15008. (org-defkey map (kbd "RET") 'org-calendar-select)
  15009. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  15010. 'org-calendar-select-mouse)
  15011. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  15012. 'org-calendar-select-mouse)
  15013. (org-defkey minibuffer-local-map [(meta shift left)]
  15014. (lambda () (interactive)
  15015. (org-eval-in-calendar '(calendar-backward-month 1))))
  15016. (org-defkey minibuffer-local-map [(meta shift right)]
  15017. (lambda () (interactive)
  15018. (org-eval-in-calendar '(calendar-forward-month 1))))
  15019. (org-defkey minibuffer-local-map [(shift up)]
  15020. (lambda () (interactive)
  15021. (org-eval-in-calendar '(calendar-backward-week 1))))
  15022. (org-defkey minibuffer-local-map [(shift down)]
  15023. (lambda () (interactive)
  15024. (org-eval-in-calendar '(calendar-forward-week 1))))
  15025. (org-defkey minibuffer-local-map [(shift left)]
  15026. (lambda () (interactive)
  15027. (org-eval-in-calendar '(calendar-backward-day 1))))
  15028. (org-defkey minibuffer-local-map [(shift right)]
  15029. (lambda () (interactive)
  15030. (org-eval-in-calendar '(calendar-forward-day 1))))
  15031. (org-defkey minibuffer-local-map ">"
  15032. (lambda () (interactive)
  15033. (org-eval-in-calendar '(scroll-calendar-left 1))))
  15034. (org-defkey minibuffer-local-map "<"
  15035. (lambda () (interactive)
  15036. (org-eval-in-calendar '(scroll-calendar-right 1))))
  15037. (unwind-protect
  15038. (progn
  15039. (use-local-map map)
  15040. (setq org-ans0 (read-string prompt "" nil nil))
  15041. ;; org-ans0: from prompt
  15042. ;; org-ans1: from mouse click
  15043. ;; org-ans2: from calendar motion
  15044. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15045. (use-local-map old-map))))))
  15046. (t ; Naked prompt only
  15047. (setq ans (read-string prompt "" nil timestr))))
  15048. (org-detach-overlay org-date-ovl)
  15049. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  15050. (setq ans (replace-match "" t t ans)
  15051. deltan (car delta)
  15052. deltaw (nth 1 delta)
  15053. deltadef (nth 2 delta)))
  15054. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  15055. (when (string-match
  15056. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15057. (setq year (if (match-end 2)
  15058. (string-to-number (match-string 2 ans))
  15059. (string-to-number (format-time-string "%Y")))
  15060. month (string-to-number (match-string 3 ans))
  15061. day (string-to-number (match-string 4 ans)))
  15062. (if (< year 100) (setq year (+ 2000 year)))
  15063. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15064. t nil ans)))
  15065. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15066. ;; If there is a time with am/pm, and *no* time without it, we convert
  15067. ;; so that matching will be successful.
  15068. (loop for i from 1 to 2 do ; twice, for end time as well
  15069. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15070. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15071. (setq hour (string-to-number (match-string 1 ans))
  15072. minute (if (match-end 3)
  15073. (string-to-number (match-string 3 ans))
  15074. 0)
  15075. pm (equal ?p
  15076. (string-to-char (downcase (match-string 4 ans)))))
  15077. (if (and (= hour 12) (not pm))
  15078. (setq hour 0)
  15079. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15080. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15081. t t ans))))
  15082. ;; Check if a time range is given as a duration
  15083. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15084. (setq hour (string-to-number (match-string 1 ans))
  15085. h2 (+ hour (string-to-number (match-string 3 ans)))
  15086. minute (string-to-number (match-string 2 ans))
  15087. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  15088. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  15089. ;; Check if there is a time range
  15090. (when (and (boundp 'org-end-time-was-given)
  15091. (string-match org-plain-time-of-day-regexp ans)
  15092. (match-end 8))
  15093. (setq org-end-time-was-given (match-string 8 ans))
  15094. (setq ans (concat (substring ans 0 (match-beginning 7))
  15095. (substring ans (match-end 7)))))
  15096. (setq tl (parse-time-string ans)
  15097. day (or (nth 3 tl) (string-to-number (format-time-string "%d" def)))
  15098. month (or (nth 4 tl) (string-to-number (format-time-string "%m" def)))
  15099. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" def)))
  15100. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" def)))
  15101. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" def)))
  15102. second (or (nth 0 tl) 0)
  15103. wday (nth 6 tl))
  15104. (when deltan
  15105. (unless deltadef
  15106. (let ((now (decode-time (current-time))))
  15107. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15108. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15109. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15110. ((equal deltaw "m") (setq month (+ month deltan)))
  15111. ((equal deltaw "y") (setq year (+ year deltan)))))
  15112. (when (and wday (not (nth 3 tl)))
  15113. ;; Weekday was given, but no day, so pick that day in the week
  15114. ;; on or after the derived date.
  15115. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15116. (unless (equal wday wday1)
  15117. (setq day (+ day (% (- wday wday1 -7) 7)))))
  15118. (if (and (boundp 'org-time-was-given)
  15119. (nth 2 tl))
  15120. (setq org-time-was-given t))
  15121. (if (< year 100) (setq year (+ 2000 year)))
  15122. (if to-time
  15123. (encode-time second minute hour day month year)
  15124. (if (or (nth 1 tl) (nth 2 tl))
  15125. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  15126. (format "%04d-%02d-%02d" year month day)))))
  15127. ;(defun org-parse-for-shift (n1 n2 given-dec default-dec)
  15128. ; (cond
  15129. ; ((not (nth n1 given-dec))
  15130. ; (nth n1 default-dec))
  15131. ; ((or (> (nth n1 given-dec) (nth n1 (default-dec)))
  15132. ; (not org-read-date-prefer-future))
  15133. ; (nth n1 given-dec))
  15134. ; (t (1+
  15135. ; (if (nth 3 given-dec)
  15136. ; (nth 3 given-dec)
  15137. ; (if (> (nth
  15138. ; (setq given
  15139. ; (if (and
  15140. (defvar parse-time-weekdays)
  15141. (defun org-read-date-get-relative (s today default)
  15142. "Check string S for special relative date string.
  15143. TODAY and DEFAULT are internal times, for today and for a default.
  15144. Return shift list (N what def-flag)
  15145. WHAT is \"d\", \"w\", \"m\", or \"y\" for day. week, month, year.
  15146. N is the number if WHATs to shift
  15147. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15148. the DEFAULT date rather than TODAY."
  15149. (when (string-match
  15150. (concat
  15151. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  15152. "\\([0-9]+\\)?"
  15153. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15154. "\\([ \t]\\|$\\)") s)
  15155. (let* ((dir (if (match-end 1)
  15156. (string-to-char (substring (match-string 1 s) -1))
  15157. ?+))
  15158. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15159. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15160. (what (if (match-end 3) (match-string 3 s) "d"))
  15161. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15162. (date (if rel default today))
  15163. (wday (nth 6 (decode-time date)))
  15164. delta)
  15165. (if wday1
  15166. (progn
  15167. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15168. (if (= dir ?-) (setq delta (- delta 7)))
  15169. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15170. (list delta "d" rel))
  15171. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15172. (defun org-eval-in-calendar (form &optional keepdate)
  15173. "Eval FORM in the calendar window and return to current window.
  15174. Also, store the cursor date in variable org-ans2."
  15175. (let ((sw (selected-window)))
  15176. (select-window (get-buffer-window "*Calendar*"))
  15177. (eval form)
  15178. (when (and (not keepdate) (calendar-cursor-to-date))
  15179. (let* ((date (calendar-cursor-to-date))
  15180. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15181. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15182. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15183. (select-window sw)
  15184. ;; Update the prompt to show new default date
  15185. (save-excursion
  15186. (goto-char (point-min))
  15187. (when (and org-ans2
  15188. (re-search-forward "\\[[-0-9]+\\]" nil t)
  15189. (get-text-property (match-end 0) 'field))
  15190. (let ((inhibit-read-only t))
  15191. (replace-match (concat "[" org-ans2 "]") t t)
  15192. (add-text-properties (point-min) (1+ (match-end 0))
  15193. (text-properties-at (1+ (point-min)))))))))
  15194. (defun org-calendar-select ()
  15195. "Return to `org-read-date' with the date currently selected.
  15196. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15197. (interactive)
  15198. (when (calendar-cursor-to-date)
  15199. (let* ((date (calendar-cursor-to-date))
  15200. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15201. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15202. (if (active-minibuffer-window) (exit-minibuffer))))
  15203. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15204. "Insert a date stamp for the date given by the internal TIME.
  15205. WITH-HM means, use the stamp format that includes the time of the day.
  15206. INACTIVE means use square brackets instead of angular ones, so that the
  15207. stamp will not contribute to the agenda.
  15208. PRE and POST are optional strings to be inserted before and after the
  15209. stamp.
  15210. The command returns the inserted time stamp."
  15211. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15212. stamp)
  15213. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15214. (insert-before-markers (or pre ""))
  15215. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15216. (when (listp extra)
  15217. (setq extra (car extra))
  15218. (if (and (stringp extra)
  15219. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15220. (setq extra (format "-%02d:%02d"
  15221. (string-to-number (match-string 1 extra))
  15222. (string-to-number (match-string 2 extra))))
  15223. (setq extra nil)))
  15224. (when extra
  15225. (backward-char 1)
  15226. (insert-before-markers extra)
  15227. (forward-char 1))
  15228. (insert-before-markers (or post ""))
  15229. stamp))
  15230. (defun org-toggle-time-stamp-overlays ()
  15231. "Toggle the use of custom time stamp formats."
  15232. (interactive)
  15233. (setq org-display-custom-times (not org-display-custom-times))
  15234. (unless org-display-custom-times
  15235. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15236. (while (setq p (next-single-property-change p 'display))
  15237. (if (and (get-text-property p 'display)
  15238. (eq (get-text-property p 'face) 'org-date))
  15239. (remove-text-properties
  15240. p (setq p (next-single-property-change p 'display))
  15241. '(display t))))
  15242. (set-buffer-modified-p bmp)))
  15243. (if (featurep 'xemacs)
  15244. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15245. (org-restart-font-lock)
  15246. (setq org-table-may-need-update t)
  15247. (if org-display-custom-times
  15248. (message "Time stamps are overlayed with custom format")
  15249. (message "Time stamp overlays removed")))
  15250. (defun org-display-custom-time (beg end)
  15251. "Overlay modified time stamp format over timestamp between BED and END."
  15252. (let* ((ts (buffer-substring beg end))
  15253. t1 w1 with-hm tf time str w2 (off 0))
  15254. (save-match-data
  15255. (setq t1 (org-parse-time-string ts t))
  15256. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  15257. (setq off (- (match-end 0) (match-beginning 0)))))
  15258. (setq end (- end off))
  15259. (setq w1 (- end beg)
  15260. with-hm (and (nth 1 t1) (nth 2 t1))
  15261. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15262. time (org-fix-decoded-time t1)
  15263. str (org-add-props
  15264. (format-time-string
  15265. (substring tf 1 -1) (apply 'encode-time time))
  15266. nil 'mouse-face 'highlight)
  15267. w2 (length str))
  15268. (if (not (= w2 w1))
  15269. (add-text-properties (1+ beg) (+ 2 beg)
  15270. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15271. (if (featurep 'xemacs)
  15272. (progn
  15273. (put-text-property beg end 'invisible t)
  15274. (put-text-property beg end 'end-glyph (make-glyph str)))
  15275. (put-text-property beg end 'display str))))
  15276. (defun org-translate-time (string)
  15277. "Translate all timestamps in STRING to custom format.
  15278. But do this only if the variable `org-display-custom-times' is set."
  15279. (when org-display-custom-times
  15280. (save-match-data
  15281. (let* ((start 0)
  15282. (re org-ts-regexp-both)
  15283. t1 with-hm inactive tf time str beg end)
  15284. (while (setq start (string-match re string start))
  15285. (setq beg (match-beginning 0)
  15286. end (match-end 0)
  15287. t1 (save-match-data
  15288. (org-parse-time-string (substring string beg end) t))
  15289. with-hm (and (nth 1 t1) (nth 2 t1))
  15290. inactive (equal (substring string beg (1+ beg)) "[")
  15291. tf (funcall (if with-hm 'cdr 'car)
  15292. org-time-stamp-custom-formats)
  15293. time (org-fix-decoded-time t1)
  15294. str (format-time-string
  15295. (concat
  15296. (if inactive "[" "<") (substring tf 1 -1)
  15297. (if inactive "]" ">"))
  15298. (apply 'encode-time time))
  15299. string (replace-match str t t string)
  15300. start (+ start (length str)))))))
  15301. string)
  15302. (defun org-fix-decoded-time (time)
  15303. "Set 0 instead of nil for the first 6 elements of time.
  15304. Don't touch the rest."
  15305. (let ((n 0))
  15306. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15307. (defun org-days-to-time (timestamp-string)
  15308. "Difference between TIMESTAMP-STRING and now in days."
  15309. (- (time-to-days (org-time-string-to-time timestamp-string))
  15310. (time-to-days (current-time))))
  15311. (defun org-deadline-close (timestamp-string &optional ndays)
  15312. "Is the time in TIMESTAMP-STRING close to the current date?"
  15313. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15314. (and (< (org-days-to-time timestamp-string) ndays)
  15315. (not (org-entry-is-done-p))))
  15316. (defun org-get-wdays (ts)
  15317. "Get the deadline lead time appropriate for timestring TS."
  15318. (cond
  15319. ((<= org-deadline-warning-days 0)
  15320. ;; 0 or negative, enforce this value no matter what
  15321. (- org-deadline-warning-days))
  15322. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  15323. ;; lead time is specified.
  15324. (floor (* (string-to-number (match-string 1 ts))
  15325. (cdr (assoc (match-string 2 ts)
  15326. '(("d" . 1) ("w" . 7)
  15327. ("m" . 30.4) ("y" . 365.25)))))))
  15328. ;; go for the default.
  15329. (t org-deadline-warning-days)))
  15330. (defun org-calendar-select-mouse (ev)
  15331. "Return to `org-read-date' with the date currently selected.
  15332. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15333. (interactive "e")
  15334. (mouse-set-point ev)
  15335. (when (calendar-cursor-to-date)
  15336. (let* ((date (calendar-cursor-to-date))
  15337. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15338. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15339. (if (active-minibuffer-window) (exit-minibuffer))))
  15340. (defun org-check-deadlines (ndays)
  15341. "Check if there are any deadlines due or past due.
  15342. A deadline is considered due if it happens within `org-deadline-warning-days'
  15343. days from today's date. If the deadline appears in an entry marked DONE,
  15344. it is not shown. The prefix arg NDAYS can be used to test that many
  15345. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15346. (interactive "P")
  15347. (let* ((org-warn-days
  15348. (cond
  15349. ((equal ndays '(4)) 100000)
  15350. (ndays (prefix-numeric-value ndays))
  15351. (t (abs org-deadline-warning-days))))
  15352. (case-fold-search nil)
  15353. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15354. (callback
  15355. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15356. (message "%d deadlines past-due or due within %d days"
  15357. (org-occur regexp nil callback)
  15358. org-warn-days)))
  15359. (defun org-evaluate-time-range (&optional to-buffer)
  15360. "Evaluate a time range by computing the difference between start and end.
  15361. Normally the result is just printed in the echo area, but with prefix arg
  15362. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15363. If the time range is actually in a table, the result is inserted into the
  15364. next column.
  15365. For time difference computation, a year is assumed to be exactly 365
  15366. days in order to avoid rounding problems."
  15367. (interactive "P")
  15368. (or
  15369. (org-clock-update-time-maybe)
  15370. (save-excursion
  15371. (unless (org-at-date-range-p t)
  15372. (goto-char (point-at-bol))
  15373. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15374. (if (not (org-at-date-range-p t))
  15375. (error "Not at a time-stamp range, and none found in current line")))
  15376. (let* ((ts1 (match-string 1))
  15377. (ts2 (match-string 2))
  15378. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15379. (match-end (match-end 0))
  15380. (time1 (org-time-string-to-time ts1))
  15381. (time2 (org-time-string-to-time ts2))
  15382. (t1 (time-to-seconds time1))
  15383. (t2 (time-to-seconds time2))
  15384. (diff (abs (- t2 t1)))
  15385. (negative (< (- t2 t1) 0))
  15386. ;; (ys (floor (* 365 24 60 60)))
  15387. (ds (* 24 60 60))
  15388. (hs (* 60 60))
  15389. (fy "%dy %dd %02d:%02d")
  15390. (fy1 "%dy %dd")
  15391. (fd "%dd %02d:%02d")
  15392. (fd1 "%dd")
  15393. (fh "%02d:%02d")
  15394. y d h m align)
  15395. (if havetime
  15396. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15397. y 0
  15398. d (floor (/ diff ds)) diff (mod diff ds)
  15399. h (floor (/ diff hs)) diff (mod diff hs)
  15400. m (floor (/ diff 60)))
  15401. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15402. y 0
  15403. d (floor (+ (/ diff ds) 0.5))
  15404. h 0 m 0))
  15405. (if (not to-buffer)
  15406. (message (org-make-tdiff-string y d h m))
  15407. (when (org-at-table-p)
  15408. (goto-char match-end)
  15409. (setq align t)
  15410. (and (looking-at " *|") (goto-char (match-end 0))))
  15411. (if (looking-at
  15412. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15413. (replace-match ""))
  15414. (if negative (insert " -"))
  15415. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15416. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15417. (insert " " (format fh h m))))
  15418. (if align (org-table-align))
  15419. (message "Time difference inserted")))))
  15420. (defun org-make-tdiff-string (y d h m)
  15421. (let ((fmt "")
  15422. (l nil))
  15423. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15424. l (push y l)))
  15425. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15426. l (push d l)))
  15427. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15428. l (push h l)))
  15429. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15430. l (push m l)))
  15431. (apply 'format fmt (nreverse l))))
  15432. (defun org-time-string-to-time (s)
  15433. (apply 'encode-time (org-parse-time-string s)))
  15434. (defun org-time-string-to-absolute (s &optional daynr)
  15435. "Convert a time stamp to an absolute day number.
  15436. If there is a specifyer for a cyclic time stamp, get the closest date to
  15437. DAYNR."
  15438. (cond
  15439. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15440. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15441. daynr
  15442. (+ daynr 1000)))
  15443. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  15444. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15445. (time-to-days (current-time))) (match-string 0 s)))
  15446. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  15447. (defun org-time-from-absolute (d)
  15448. "Return the time corresponding to date D.
  15449. D may be an absolute day number, or a calendar-type list (month day year)."
  15450. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15451. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15452. (defun org-calendar-holiday ()
  15453. "List of holidays, for Diary display in Org-mode."
  15454. (let ((hl (check-calendar-holidays date)))
  15455. (if hl (mapconcat 'identity hl "; "))))
  15456. (defun org-diary-sexp-entry (sexp entry date)
  15457. "Process a SEXP diary ENTRY for DATE."
  15458. (require 'diary-lib)
  15459. (let ((result (if calendar-debug-sexp
  15460. (let ((stack-trace-on-error t))
  15461. (eval (car (read-from-string sexp))))
  15462. (condition-case nil
  15463. (eval (car (read-from-string sexp)))
  15464. (error
  15465. (beep)
  15466. (message "Bad sexp at line %d in %s: %s"
  15467. (org-current-line)
  15468. (buffer-file-name) sexp)
  15469. (sleep-for 2))))))
  15470. (cond ((stringp result) result)
  15471. ((and (consp result)
  15472. (stringp (cdr result))) (cdr result))
  15473. (result entry)
  15474. (t nil))))
  15475. (defun org-diary-to-ical-string (frombuf)
  15476. "Get iCalendar entreis from diary entries in buffer FROMBUF.
  15477. This uses the icalendar.el library."
  15478. (let* ((tmpdir (if (featurep 'xemacs)
  15479. (temp-directory)
  15480. temporary-file-directory))
  15481. (tmpfile (make-temp-name
  15482. (expand-file-name "orgics" tmpdir)))
  15483. buf rtn b e)
  15484. (save-excursion
  15485. (set-buffer frombuf)
  15486. (icalendar-export-region (point-min) (point-max) tmpfile)
  15487. (setq buf (find-buffer-visiting tmpfile))
  15488. (set-buffer buf)
  15489. (goto-char (point-min))
  15490. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15491. (setq b (match-beginning 0)))
  15492. (goto-char (point-max))
  15493. (if (re-search-backward "^END:VEVENT" nil t)
  15494. (setq e (match-end 0)))
  15495. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15496. (kill-buffer buf)
  15497. (kill-buffer frombuf)
  15498. (delete-file tmpfile)
  15499. rtn))
  15500. (defun org-closest-date (start current change)
  15501. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  15502. ;; Make the proper lists from the dates
  15503. (catch 'exit
  15504. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  15505. dn dw sday cday n1 n2
  15506. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15507. (setq start (org-date-to-gregorian start)
  15508. current (org-date-to-gregorian
  15509. (if org-agenda-repeating-timestamp-show-all
  15510. current
  15511. (time-to-days (current-time))))
  15512. sday (calendar-absolute-from-gregorian start)
  15513. cday (calendar-absolute-from-gregorian current))
  15514. (if (<= cday sday) (throw 'exit sday))
  15515. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  15516. (setq dn (string-to-number (match-string 1 change))
  15517. dw (cdr (assoc (match-string 2 change) a1)))
  15518. (error "Invalid change specifyer: %s" change))
  15519. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15520. (cond
  15521. ((eq dw 'day)
  15522. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15523. n2 (+ n1 dn)))
  15524. ((eq dw 'year)
  15525. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15526. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15527. (setq date1 (list m d y1)
  15528. n1 (calendar-absolute-from-gregorian date1)
  15529. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15530. n2 (calendar-absolute-from-gregorian date2)))
  15531. ((eq dw 'month)
  15532. ;; approx number of month between the tow dates
  15533. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15534. ;; How often does dn fit in there?
  15535. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15536. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15537. m (+ m nm)
  15538. ny (floor (/ m 12))
  15539. y (+ y ny)
  15540. m (- m (* ny 12)))
  15541. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15542. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15543. (setq m2 (+ m dn) y2 y)
  15544. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15545. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15546. (while (< n2 cday)
  15547. (setq n1 n2 m m2 y y2)
  15548. (setq m2 (+ m dn) y2 y)
  15549. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15550. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15551. (if org-agenda-repeating-timestamp-show-all
  15552. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  15553. (if (= cday n1) n1 n2)))))
  15554. (defun org-date-to-gregorian (date)
  15555. "Turn any specification of DATE into a gregorian date for the calendar."
  15556. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15557. ((and (listp date) (= (length date) 3)) date)
  15558. ((stringp date)
  15559. (setq date (org-parse-time-string date))
  15560. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15561. ((listp date)
  15562. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15563. (defun org-parse-time-string (s &optional nodefault)
  15564. "Parse the standard Org-mode time string.
  15565. This should be a lot faster than the normal `parse-time-string'.
  15566. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15567. hour and minute fields will be nil if not given."
  15568. (if (string-match org-ts-regexp0 s)
  15569. (list 0
  15570. (if (or (match-beginning 8) (not nodefault))
  15571. (string-to-number (or (match-string 8 s) "0")))
  15572. (if (or (match-beginning 7) (not nodefault))
  15573. (string-to-number (or (match-string 7 s) "0")))
  15574. (string-to-number (match-string 4 s))
  15575. (string-to-number (match-string 3 s))
  15576. (string-to-number (match-string 2 s))
  15577. nil nil nil)
  15578. (make-list 9 0)))
  15579. (defun org-timestamp-up (&optional arg)
  15580. "Increase the date item at the cursor by one.
  15581. If the cursor is on the year, change the year. If it is on the month or
  15582. the day, change that.
  15583. With prefix ARG, change by that many units."
  15584. (interactive "p")
  15585. (org-timestamp-change (prefix-numeric-value arg)))
  15586. (defun org-timestamp-down (&optional arg)
  15587. "Decrease the date item at the cursor by one.
  15588. If the cursor is on the year, change the year. If it is on the month or
  15589. the day, change that.
  15590. With prefix ARG, change by that many units."
  15591. (interactive "p")
  15592. (org-timestamp-change (- (prefix-numeric-value arg))))
  15593. (defun org-timestamp-up-day (&optional arg)
  15594. "Increase the date in the time stamp by one day.
  15595. With prefix ARG, change that many days."
  15596. (interactive "p")
  15597. (if (and (not (org-at-timestamp-p t))
  15598. (org-on-heading-p))
  15599. (org-todo 'up)
  15600. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  15601. (defun org-timestamp-down-day (&optional arg)
  15602. "Decrease the date in the time stamp by one day.
  15603. With prefix ARG, change that many days."
  15604. (interactive "p")
  15605. (if (and (not (org-at-timestamp-p t))
  15606. (org-on-heading-p))
  15607. (org-todo 'down)
  15608. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  15609. (defsubst org-pos-in-match-range (pos n)
  15610. (and (match-beginning n)
  15611. (<= (match-beginning n) pos)
  15612. (>= (match-end n) pos)))
  15613. (defun org-at-timestamp-p (&optional inactive-ok)
  15614. "Determine if the cursor is in or at a timestamp."
  15615. (interactive)
  15616. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15617. (pos (point))
  15618. (ans (or (looking-at tsr)
  15619. (save-excursion
  15620. (skip-chars-backward "^[<\n\r\t")
  15621. (if (> (point) (point-min)) (backward-char 1))
  15622. (and (looking-at tsr)
  15623. (> (- (match-end 0) pos) -1))))))
  15624. (and ans
  15625. (boundp 'org-ts-what)
  15626. (setq org-ts-what
  15627. (cond
  15628. ((= pos (match-beginning 0)) 'bracket)
  15629. ((= pos (1- (match-end 0))) 'bracket)
  15630. ((org-pos-in-match-range pos 2) 'year)
  15631. ((org-pos-in-match-range pos 3) 'month)
  15632. ((org-pos-in-match-range pos 7) 'hour)
  15633. ((org-pos-in-match-range pos 8) 'minute)
  15634. ((or (org-pos-in-match-range pos 4)
  15635. (org-pos-in-match-range pos 5)) 'day)
  15636. ((and (> pos (or (match-end 8) (match-end 5)))
  15637. (< pos (match-end 0)))
  15638. (- pos (or (match-end 8) (match-end 5))))
  15639. (t 'day))))
  15640. ans))
  15641. (defun org-toggle-timestamp-type ()
  15642. ""
  15643. (interactive)
  15644. (when (org-at-timestamp-p t)
  15645. (save-excursion
  15646. (goto-char (match-beginning 0))
  15647. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  15648. (goto-char (1- (match-end 0)))
  15649. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  15650. (message "Timestamp is now %sactive"
  15651. (if (equal (char-before) ?>) "in" ""))))
  15652. (defun org-timestamp-change (n &optional what)
  15653. "Change the date in the time stamp at point.
  15654. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15655. `year', `minute', `second'. If WHAT is not given, the cursor position
  15656. in the timestamp determines what will be changed."
  15657. (let ((pos (point))
  15658. with-hm inactive
  15659. org-ts-what
  15660. extra
  15661. ts time time0)
  15662. (if (not (org-at-timestamp-p t))
  15663. (error "Not at a timestamp"))
  15664. (if (and (not what) (eq org-ts-what 'bracket))
  15665. (org-toggle-timestamp-type)
  15666. (if (and (not what) (not (eq org-ts-what 'day))
  15667. org-display-custom-times
  15668. (get-text-property (point) 'display)
  15669. (not (get-text-property (1- (point)) 'display)))
  15670. (setq org-ts-what 'day))
  15671. (setq org-ts-what (or what org-ts-what)
  15672. inactive (= (char-after (match-beginning 0)) ?\[)
  15673. ts (match-string 0))
  15674. (replace-match "")
  15675. (if (string-match
  15676. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  15677. ts)
  15678. (setq extra (match-string 1 ts)))
  15679. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15680. (setq with-hm t))
  15681. (setq time0 (org-parse-time-string ts))
  15682. (setq time
  15683. (encode-time (or (car time0) 0)
  15684. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15685. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15686. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15687. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15688. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15689. (nthcdr 6 time0)))
  15690. (when (integerp org-ts-what)
  15691. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  15692. (if (eq what 'calendar)
  15693. (let ((cal-date (org-get-date-from-calendar)))
  15694. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15695. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15696. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15697. (setcar time0 (or (car time0) 0))
  15698. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15699. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15700. (setq time (apply 'encode-time time0))))
  15701. (setq org-last-changed-timestamp
  15702. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15703. (org-clock-update-time-maybe)
  15704. (goto-char pos)
  15705. ;; Try to recenter the calendar window, if any
  15706. (if (and org-calendar-follow-timestamp-change
  15707. (get-buffer-window "*Calendar*" t)
  15708. (memq org-ts-what '(day month year)))
  15709. (org-recenter-calendar (time-to-days time))))))
  15710. ;; FIXME: does not yet work for lead times
  15711. (defun org-modify-ts-extra (s pos n)
  15712. "Change the different parts of the lead-time and repeat fields in timestamp."
  15713. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15714. ng h m new)
  15715. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15716. (cond
  15717. ((or (org-pos-in-match-range pos 2)
  15718. (org-pos-in-match-range pos 3))
  15719. (setq m (string-to-number (match-string 3 s))
  15720. h (string-to-number (match-string 2 s)))
  15721. (if (org-pos-in-match-range pos 2)
  15722. (setq h (+ h n))
  15723. (setq m (+ m n)))
  15724. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15725. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15726. (setq h (min 24 (max 0 h)))
  15727. (setq ng 1 new (format "-%02d:%02d" h m)))
  15728. ((org-pos-in-match-range pos 6)
  15729. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15730. ((org-pos-in-match-range pos 5)
  15731. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  15732. (when ng
  15733. (setq s (concat
  15734. (substring s 0 (match-beginning ng))
  15735. new
  15736. (substring s (match-end ng))))))
  15737. s))
  15738. (defun org-recenter-calendar (date)
  15739. "If the calendar is visible, recenter it to DATE."
  15740. (let* ((win (selected-window))
  15741. (cwin (get-buffer-window "*Calendar*" t))
  15742. (calendar-move-hook nil))
  15743. (when cwin
  15744. (select-window cwin)
  15745. (calendar-goto-date (if (listp date) date
  15746. (calendar-gregorian-from-absolute date)))
  15747. (select-window win))))
  15748. (defun org-goto-calendar (&optional arg)
  15749. "Go to the Emacs calendar at the current date.
  15750. If there is a time stamp in the current line, go to that date.
  15751. A prefix ARG can be used to force the current date."
  15752. (interactive "P")
  15753. (let ((tsr org-ts-regexp) diff
  15754. (calendar-move-hook nil)
  15755. (view-calendar-holidays-initially nil)
  15756. (view-diary-entries-initially nil))
  15757. (if (or (org-at-timestamp-p)
  15758. (save-excursion
  15759. (beginning-of-line 1)
  15760. (looking-at (concat ".*" tsr))))
  15761. (let ((d1 (time-to-days (current-time)))
  15762. (d2 (time-to-days
  15763. (org-time-string-to-time (match-string 1)))))
  15764. (setq diff (- d2 d1))))
  15765. (calendar)
  15766. (calendar-goto-today)
  15767. (if (and diff (not arg)) (calendar-forward-day diff))))
  15768. (defun org-get-date-from-calendar ()
  15769. "Return a list (month day year) of date at point in calendar."
  15770. (with-current-buffer "*Calendar*"
  15771. (save-match-data
  15772. (calendar-cursor-to-date))))
  15773. (defun org-date-from-calendar ()
  15774. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15775. If there is already a time stamp at the cursor position, update it."
  15776. (interactive)
  15777. (if (org-at-timestamp-p t)
  15778. (org-timestamp-change 0 'calendar)
  15779. (let ((cal-date (org-get-date-from-calendar)))
  15780. (org-insert-time-stamp
  15781. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15782. ;; Make appt aware of appointments from the agenda
  15783. ;;;###autoload
  15784. (defun org-agenda-to-appt (&optional filter)
  15785. "Activate appointments found in `org-agenda-files'.
  15786. When prefixed, prompt for a regular expression and use it as a
  15787. filter: only add entries if they match this regular expression.
  15788. FILTER can be a string. In this case, use this string as a
  15789. regular expression to filter results.
  15790. FILTER can also be an alist, with the car of each cell being
  15791. either 'headline or 'category. For example:
  15792. '((headline \"IMPORTANT\")
  15793. (category \"Work\"))
  15794. will only add headlines containing IMPORTANT or headlines
  15795. belonging to the category \"Work\"."
  15796. (interactive "P")
  15797. (require 'calendar)
  15798. (if (equal filter '(4))
  15799. (setq filter (read-from-minibuffer "Regexp filter: ")))
  15800. (let* ((cnt 0) ; count added events
  15801. (today (org-date-to-gregorian
  15802. (time-to-days (current-time))))
  15803. (files (org-agenda-files)) entries file)
  15804. ;; Get all entries which may contain an appt
  15805. (while (setq file (pop files))
  15806. (setq entries
  15807. (append entries
  15808. (org-agenda-get-day-entries
  15809. file today
  15810. :timestamp :scheduled :deadline))))
  15811. (setq entries (delq nil entries))
  15812. ;; Map thru entries and find if they pass thru the filter
  15813. (mapc
  15814. (lambda(x)
  15815. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  15816. (cat (get-text-property 1 'org-category x))
  15817. (tod (get-text-property 1 'time-of-day x))
  15818. (ok (or (null filter)
  15819. (and (stringp filter) (string-match filter evt))
  15820. (and (listp filter)
  15821. (or (string-match
  15822. (cadr (assoc 'category filter)) cat)
  15823. (string-match
  15824. (cadr (assoc 'headline filter)) evt))))))
  15825. ;; FIXME Shall we remove text-properties for the appt text?
  15826. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  15827. (when (and ok tod)
  15828. (setq tod (number-to-string tod)
  15829. tod (when (string-match
  15830. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  15831. (concat (match-string 1 tod) ":"
  15832. (match-string 2 tod))))
  15833. (appt-add tod evt)
  15834. (setq cnt (1+ cnt))))) entries)
  15835. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  15836. ;;; The clock for measuring work time.
  15837. (defvar org-mode-line-string "")
  15838. (put 'org-mode-line-string 'risky-local-variable t)
  15839. (defvar org-mode-line-timer nil)
  15840. (defvar org-clock-heading "")
  15841. (defvar org-clock-start-time "")
  15842. (defun org-update-mode-line ()
  15843. (let* ((delta (- (time-to-seconds (current-time))
  15844. (time-to-seconds org-clock-start-time)))
  15845. (h (floor delta 3600))
  15846. (m (floor (- delta (* 3600 h)) 60)))
  15847. (setq org-mode-line-string
  15848. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  15849. 'help-echo "Org-mode clock is running"))
  15850. (force-mode-line-update)))
  15851. (defvar org-clock-marker (make-marker)
  15852. "Marker recording the last clock-in.")
  15853. (defvar org-clock-mode-line-entry nil
  15854. "Information for the modeline about the running clock.")
  15855. (defun org-clock-in ()
  15856. "Start the clock on the current item.
  15857. If necessary, clock-out of the currently active clock."
  15858. (interactive)
  15859. (org-clock-out t)
  15860. (let (ts)
  15861. (save-excursion
  15862. (org-back-to-heading t)
  15863. (if (looking-at org-todo-line-regexp)
  15864. (setq org-clock-heading (match-string 3))
  15865. (setq org-clock-heading "???"))
  15866. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  15867. (org-clock-find-position)
  15868. (insert "\n") (backward-char 1)
  15869. (indent-relative)
  15870. (insert org-clock-string " ")
  15871. (setq org-clock-start-time (current-time))
  15872. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  15873. (move-marker org-clock-marker (point) (buffer-base-buffer))
  15874. (or global-mode-string (setq global-mode-string '("")))
  15875. (or (memq 'org-mode-line-string global-mode-string)
  15876. (setq global-mode-string
  15877. (append global-mode-string '(org-mode-line-string))))
  15878. (org-update-mode-line)
  15879. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  15880. (message "Clock started at %s" ts))))
  15881. (defun org-clock-find-position ()
  15882. "Find the location where the next clock line should be inserted."
  15883. (org-back-to-heading t)
  15884. (catch 'exit
  15885. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  15886. (re (concat "^[ \t]*" org-clock-string))
  15887. (cnt 0)
  15888. first last)
  15889. (goto-char beg)
  15890. (when (eobp) (newline) (setq end (max (point) end)))
  15891. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  15892. ;; we seem to have a CLOCK drawer, so go there.
  15893. (beginning-of-line 2)
  15894. (throw 'exit t))
  15895. ;; Lets count the CLOCK lines
  15896. (goto-char beg)
  15897. (while (re-search-forward re end t)
  15898. (setq first (or first (match-beginning 0))
  15899. last (match-beginning 0)
  15900. cnt (1+ cnt)))
  15901. (when (and (integerp org-clock-into-drawer)
  15902. (>= (1+ cnt) org-clock-into-drawer))
  15903. ;; Wrap current entries into a new drawer
  15904. (goto-char last)
  15905. (beginning-of-line 2)
  15906. (if (org-at-item-p) (org-end-of-item))
  15907. (insert ":END:\n")
  15908. (beginning-of-line 0)
  15909. (org-indent-line-function)
  15910. (goto-char first)
  15911. (insert ":CLOCK:\n")
  15912. (beginning-of-line 0)
  15913. (org-indent-line-function)
  15914. (org-flag-drawer t)
  15915. (beginning-of-line 2)
  15916. (throw 'exit nil))
  15917. (goto-char beg)
  15918. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  15919. (not (equal (match-string 1) org-clock-string)))
  15920. ;; Planning info, skip to after it
  15921. (beginning-of-line 2)
  15922. (or (bolp) (newline)))
  15923. (when (eq t org-clock-into-drawer)
  15924. (insert ":CLOCK:\n:END:\n")
  15925. (beginning-of-line -1)
  15926. (org-indent-line-function)
  15927. (org-flag-drawer t)
  15928. (beginning-of-line 2)
  15929. (org-indent-line-function)))))
  15930. (defun org-clock-out (&optional fail-quietly)
  15931. "Stop the currently running clock.
  15932. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  15933. (interactive)
  15934. (catch 'exit
  15935. (if (not (marker-buffer org-clock-marker))
  15936. (if fail-quietly (throw 'exit t) (error "No active clock")))
  15937. (let (ts te s h m)
  15938. (save-excursion
  15939. (set-buffer (marker-buffer org-clock-marker))
  15940. (goto-char org-clock-marker)
  15941. (beginning-of-line 1)
  15942. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  15943. (equal (match-string 1) org-clock-string))
  15944. (setq ts (match-string 2))
  15945. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  15946. (goto-char (match-end 0))
  15947. (delete-region (point) (point-at-eol))
  15948. (insert "--")
  15949. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  15950. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  15951. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  15952. h (floor (/ s 3600))
  15953. s (- s (* 3600 h))
  15954. m (floor (/ s 60))
  15955. s (- s (* 60 s)))
  15956. (insert " => " (format "%2d:%02d" h m))
  15957. (move-marker org-clock-marker nil)
  15958. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  15959. (org-log-done (org-parse-local-options logging 'org-log-done))
  15960. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  15961. (org-add-log-maybe 'clock-out))
  15962. (when org-mode-line-timer
  15963. (cancel-timer org-mode-line-timer)
  15964. (setq org-mode-line-timer nil))
  15965. (setq global-mode-string
  15966. (delq 'org-mode-line-string global-mode-string))
  15967. (force-mode-line-update)
  15968. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  15969. (defun org-clock-cancel ()
  15970. "Cancel the running clock be removing the start timestamp."
  15971. (interactive)
  15972. (if (not (marker-buffer org-clock-marker))
  15973. (error "No active clock"))
  15974. (save-excursion
  15975. (set-buffer (marker-buffer org-clock-marker))
  15976. (goto-char org-clock-marker)
  15977. (delete-region (1- (point-at-bol)) (point-at-eol)))
  15978. (message "Clock canceled"))
  15979. (defun org-clock-goto (&optional delete-windows)
  15980. "Go to the currently clocked-in entry."
  15981. (interactive "P")
  15982. (if (not (marker-buffer org-clock-marker))
  15983. (error "No active clock"))
  15984. (switch-to-buffer-other-window
  15985. (marker-buffer org-clock-marker))
  15986. (if delete-windows (delete-other-windows))
  15987. (goto-char org-clock-marker)
  15988. (org-show-entry)
  15989. (org-back-to-heading)
  15990. (recenter))
  15991. (defvar org-clock-file-total-minutes nil
  15992. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  15993. (make-variable-buffer-local 'org-clock-file-total-minutes)
  15994. (defun org-clock-sum (&optional tstart tend)
  15995. "Sum the times for each subtree.
  15996. Puts the resulting times in minutes as a text property on each headline."
  15997. (interactive)
  15998. (let* ((bmp (buffer-modified-p))
  15999. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  16000. org-clock-string
  16001. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  16002. (lmax 30)
  16003. (ltimes (make-vector lmax 0))
  16004. (t1 0)
  16005. (level 0)
  16006. ts te dt
  16007. time)
  16008. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  16009. (save-excursion
  16010. (goto-char (point-max))
  16011. (while (re-search-backward re nil t)
  16012. (cond
  16013. ((match-end 2)
  16014. ;; Two time stamps
  16015. (setq ts (match-string 2)
  16016. te (match-string 3)
  16017. ts (time-to-seconds
  16018. (apply 'encode-time (org-parse-time-string ts)))
  16019. te (time-to-seconds
  16020. (apply 'encode-time (org-parse-time-string te)))
  16021. ts (if tstart (max ts tstart) ts)
  16022. te (if tend (min te tend) te)
  16023. dt (- te ts)
  16024. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  16025. ((match-end 4)
  16026. ;; A naket time
  16027. (setq t1 (+ t1 (string-to-number (match-string 5))
  16028. (* 60 (string-to-number (match-string 4))))))
  16029. (t ;; A headline
  16030. (setq level (- (match-end 1) (match-beginning 1)))
  16031. (when (or (> t1 0) (> (aref ltimes level) 0))
  16032. (loop for l from 0 to level do
  16033. (aset ltimes l (+ (aref ltimes l) t1)))
  16034. (setq t1 0 time (aref ltimes level))
  16035. (loop for l from level to (1- lmax) do
  16036. (aset ltimes l 0))
  16037. (goto-char (match-beginning 0))
  16038. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  16039. (setq org-clock-file-total-minutes (aref ltimes 0)))
  16040. (set-buffer-modified-p bmp)))
  16041. (defun org-clock-display (&optional total-only)
  16042. "Show subtree times in the entire buffer.
  16043. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  16044. in the echo area."
  16045. (interactive)
  16046. (org-remove-clock-overlays)
  16047. (let (time h m p)
  16048. (org-clock-sum)
  16049. (unless total-only
  16050. (save-excursion
  16051. (goto-char (point-min))
  16052. (while (or (and (equal (setq p (point)) (point-min))
  16053. (get-text-property p :org-clock-minutes))
  16054. (setq p (next-single-property-change
  16055. (point) :org-clock-minutes)))
  16056. (goto-char p)
  16057. (when (setq time (get-text-property p :org-clock-minutes))
  16058. (org-put-clock-overlay time (funcall outline-level))))
  16059. (setq h (/ org-clock-file-total-minutes 60)
  16060. m (- org-clock-file-total-minutes (* 60 h)))
  16061. ;; Arrange to remove the overlays upon next change.
  16062. (when org-remove-highlights-with-change
  16063. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  16064. nil 'local))))
  16065. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  16066. (defvar org-clock-overlays nil)
  16067. (make-variable-buffer-local 'org-clock-overlays)
  16068. (defun org-put-clock-overlay (time &optional level)
  16069. "Put an overlays on the current line, displaying TIME.
  16070. If LEVEL is given, prefix time with a corresponding number of stars.
  16071. This creates a new overlay and stores it in `org-clock-overlays', so that it
  16072. will be easy to remove."
  16073. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  16074. (l (if level (org-get-legal-level level 0) 0))
  16075. (off 0)
  16076. ov tx)
  16077. (move-to-column c)
  16078. (unless (eolp) (skip-chars-backward "^ \t"))
  16079. (skip-chars-backward " \t")
  16080. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  16081. tx (concat (buffer-substring (1- (point)) (point))
  16082. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  16083. (org-add-props (format "%s %2d:%02d%s"
  16084. (make-string l ?*) h m
  16085. (make-string (- 10 l) ?\ ))
  16086. '(face secondary-selection))
  16087. ""))
  16088. (if (not (featurep 'xemacs))
  16089. (org-overlay-put ov 'display tx)
  16090. (org-overlay-put ov 'invisible t)
  16091. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  16092. (push ov org-clock-overlays)))
  16093. (defun org-remove-clock-overlays (&optional beg end noremove)
  16094. "Remove the occur highlights from the buffer.
  16095. BEG and END are ignored. If NOREMOVE is nil, remove this function
  16096. from the `before-change-functions' in the current buffer."
  16097. (interactive)
  16098. (unless org-inhibit-highlight-removal
  16099. (mapc 'org-delete-overlay org-clock-overlays)
  16100. (setq org-clock-overlays nil)
  16101. (unless noremove
  16102. (remove-hook 'before-change-functions
  16103. 'org-remove-clock-overlays 'local))))
  16104. (defun org-clock-out-if-current ()
  16105. "Clock out if the current entry contains the running clock.
  16106. This is used to stop the clock after a TODO entry is marked DONE,
  16107. and is only done if the variable `org-clock-out-when-done' is not nil."
  16108. (when (and org-clock-out-when-done
  16109. (member state org-done-keywords)
  16110. (equal (marker-buffer org-clock-marker) (current-buffer))
  16111. (< (point) org-clock-marker)
  16112. (> (save-excursion (outline-next-heading) (point))
  16113. org-clock-marker))
  16114. ;; Clock out, but don't accept a logging message for this.
  16115. (let ((org-log-done (if (and (listp org-log-done)
  16116. (member 'clock-out org-log-done))
  16117. '(done)
  16118. org-log-done)))
  16119. (org-clock-out))))
  16120. (add-hook 'org-after-todo-state-change-hook
  16121. 'org-clock-out-if-current)
  16122. (defun org-check-running-clock ()
  16123. "Check if the current buffer contains the running clock.
  16124. If yes, offer to stop it and to save the buffer with the changes."
  16125. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  16126. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  16127. (buffer-name))))
  16128. (org-clock-out)
  16129. (when (y-or-n-p "Save changed buffer?")
  16130. (save-buffer))))
  16131. (defun org-clock-report (&optional arg)
  16132. "Create a table containing a report about clocked time.
  16133. If the cursor is inside an existing clocktable block, then the table
  16134. will be updated. If not, a new clocktable will be inserted.
  16135. When called with a prefix argument, move to the first clock table in the
  16136. buffer and update it."
  16137. (interactive "P")
  16138. (org-remove-clock-overlays)
  16139. (when arg (org-find-dblock "clocktable"))
  16140. (if (org-in-clocktable-p)
  16141. (goto-char (org-in-clocktable-p))
  16142. (org-create-dblock (list :name "clocktable"
  16143. :maxlevel 2 :scope 'file)))
  16144. (org-update-dblock))
  16145. (defun org-in-clocktable-p ()
  16146. "Check if the cursor is in a clocktable."
  16147. (let ((pos (point)) start)
  16148. (save-excursion
  16149. (end-of-line 1)
  16150. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  16151. (setq start (match-beginning 0))
  16152. (re-search-forward "^#\\+END:.*" nil t)
  16153. (>= (match-end 0) pos)
  16154. start))))
  16155. (defun org-clock-update-time-maybe ()
  16156. "If this is a CLOCK line, update it and return t.
  16157. Otherwise, return nil."
  16158. (interactive)
  16159. (save-excursion
  16160. (beginning-of-line 1)
  16161. (skip-chars-forward " \t")
  16162. (when (looking-at org-clock-string)
  16163. (let ((re (concat "[ \t]*" org-clock-string
  16164. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  16165. "\\([ \t]*=>.*\\)?"))
  16166. ts te h m s)
  16167. (if (not (looking-at re))
  16168. nil
  16169. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  16170. (end-of-line 1)
  16171. (setq ts (match-string 1)
  16172. te (match-string 2))
  16173. (setq s (- (time-to-seconds
  16174. (apply 'encode-time (org-parse-time-string te)))
  16175. (time-to-seconds
  16176. (apply 'encode-time (org-parse-time-string ts))))
  16177. h (floor (/ s 3600))
  16178. s (- s (* 3600 h))
  16179. m (floor (/ s 60))
  16180. s (- s (* 60 s)))
  16181. (insert " => " (format "%2d:%02d" h m))
  16182. t)))))
  16183. (defun org-clock-special-range (key &optional time as-strings)
  16184. "Return two times bordering a special time range.
  16185. Key is a symbol specifying the range and can be one of `today', `yesterday',
  16186. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  16187. A week starts Monday 0:00 and ends Sunday 24:00.
  16188. The range is determined relative to TIME. TIME defaults to the current time.
  16189. The return value is a cons cell with two internal times like the ones
  16190. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  16191. the returned times will be formatted strings."
  16192. (let* ((tm (decode-time (or time (current-time))))
  16193. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  16194. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  16195. (dow (nth 6 tm))
  16196. s1 m1 h1 d1 month1 y1 diff ts te fm)
  16197. (cond
  16198. ((eq key 'today)
  16199. (setq h 0 m 0 h1 24 m1 0))
  16200. ((eq key 'yesterday)
  16201. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  16202. ((eq key 'thisweek)
  16203. (setq diff (if (= dow 0) 6 (1- dow))
  16204. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  16205. ((eq key 'lastweek)
  16206. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  16207. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  16208. ((eq key 'thismonth)
  16209. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  16210. ((eq key 'lastmonth)
  16211. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  16212. ((eq key 'thisyear)
  16213. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  16214. ((eq key 'lastyear)
  16215. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  16216. (t (error "No such time block %s" key)))
  16217. (setq ts (encode-time s m h d month y)
  16218. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  16219. (or d1 d) (or month1 month) (or y1 y)))
  16220. (setq fm (cdr org-time-stamp-formats))
  16221. (if as-strings
  16222. (cons (format-time-string fm ts) (format-time-string fm te))
  16223. (cons ts te))))
  16224. (defun org-dblock-write:clocktable (params)
  16225. "Write the standard clocktable."
  16226. (let ((hlchars '((1 . "*") (2 . "/")))
  16227. (emph nil)
  16228. (ins (make-marker))
  16229. (total-time nil)
  16230. ipos time h m p level hlc hdl maxlevel
  16231. ts te cc block beg end pos scope tbl tostring multifile)
  16232. (setq scope (plist-get params :scope)
  16233. tostring (plist-get params :tostring)
  16234. multifile (plist-get params :multifile)
  16235. maxlevel (or (plist-get params :maxlevel) 3)
  16236. emph (plist-get params :emphasize)
  16237. ts (plist-get params :tstart)
  16238. te (plist-get params :tend)
  16239. block (plist-get params :block))
  16240. (when block
  16241. (setq cc (org-clock-special-range block nil t)
  16242. ts (car cc) te (cdr cc)))
  16243. (if ts (setq ts (time-to-seconds
  16244. (apply 'encode-time (org-parse-time-string ts)))))
  16245. (if te (setq te (time-to-seconds
  16246. (apply 'encode-time (org-parse-time-string te)))))
  16247. (move-marker ins (point))
  16248. (setq ipos (point))
  16249. ;; Get the right scope
  16250. (setq pos (point))
  16251. (save-restriction
  16252. (cond
  16253. ((not scope))
  16254. ((eq scope 'file) (widen))
  16255. ((eq scope 'subtree) (org-narrow-to-subtree))
  16256. ((eq scope 'tree)
  16257. (while (org-up-heading-safe))
  16258. (org-narrow-to-subtree))
  16259. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  16260. (symbol-name scope)))
  16261. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  16262. (catch 'exit
  16263. (while (org-up-heading-safe)
  16264. (looking-at outline-regexp)
  16265. (if (<= (org-reduced-level (funcall outline-level)) level)
  16266. (throw 'exit nil))))
  16267. (org-narrow-to-subtree))
  16268. ((or (listp scope) (eq scope 'agenda))
  16269. (let* ((files (if (listp scope) scope (org-agenda-files)))
  16270. (scope 'agenda)
  16271. (p1 (copy-sequence params))
  16272. file)
  16273. (plist-put p1 :tostring t)
  16274. (plist-put p1 :multifile t)
  16275. (plist-put p1 :scope 'file)
  16276. (org-prepare-agenda-buffers files)
  16277. (while (setq file (pop files))
  16278. (with-current-buffer (find-buffer-visiting file)
  16279. (push (org-clocktable-add-file
  16280. file (org-dblock-write:clocktable p1)) tbl)
  16281. (setq total-time (+ (or total-time 0)
  16282. org-clock-file-total-minutes)))))))
  16283. (goto-char pos)
  16284. (unless (eq scope 'agenda)
  16285. (org-clock-sum ts te)
  16286. (goto-char (point-min))
  16287. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  16288. (goto-char p)
  16289. (when (setq time (get-text-property p :org-clock-minutes))
  16290. (save-excursion
  16291. (beginning-of-line 1)
  16292. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  16293. (setq level (org-reduced-level
  16294. (- (match-end 1) (match-beginning 1))))
  16295. (<= level maxlevel))
  16296. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  16297. hdl (match-string 2)
  16298. h (/ time 60)
  16299. m (- time (* 60 h)))
  16300. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  16301. (push (concat
  16302. "| " (int-to-string level) "|" hlc hdl hlc " |"
  16303. (make-string (1- level) ?|)
  16304. hlc (format "%d:%02d" h m) hlc
  16305. " |") tbl))))))
  16306. (setq tbl (nreverse tbl))
  16307. (if tostring
  16308. (if tbl (mapconcat 'identity tbl "\n") nil)
  16309. (goto-char ins)
  16310. (insert-before-markers
  16311. "Clock summary at ["
  16312. (substring
  16313. (format-time-string (cdr org-time-stamp-formats))
  16314. 1 -1)
  16315. "]."
  16316. (if block
  16317. (format " Considered range is /%s/." block)
  16318. "")
  16319. "\n\n"
  16320. (if (eq scope 'agenda) "|File" "")
  16321. "|L|Headline|Time|\n")
  16322. (setq total-time (or total-time org-clock-file-total-minutes)
  16323. h (/ total-time 60)
  16324. m (- total-time (* 60 h)))
  16325. (insert-before-markers
  16326. "|-\n|"
  16327. (if (eq scope 'agenda) "|" "")
  16328. "|"
  16329. "*Total time*| "
  16330. (format "*%d:%02d*" h m)
  16331. "|\n|-\n")
  16332. (setq tbl (delq nil tbl))
  16333. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  16334. (equal (substring (car tbl) 0 2) "|-"))
  16335. (pop tbl))
  16336. (insert-before-markers (mapconcat
  16337. 'identity (delq nil tbl)
  16338. (if (eq scope 'agenda) "\n|-\n" "\n")))
  16339. (backward-delete-char 1)
  16340. (goto-char ipos)
  16341. (skip-chars-forward "^|")
  16342. (org-table-align)))))
  16343. (defun org-clocktable-add-file (file table)
  16344. (if table
  16345. (let ((lines (org-split-string table "\n"))
  16346. (ff (file-name-nondirectory file)))
  16347. (mapconcat 'identity
  16348. (mapcar (lambda (x)
  16349. (if (string-match org-table-dataline-regexp x)
  16350. (concat "|" ff x)
  16351. x))
  16352. lines)
  16353. "\n"))))
  16354. ;; FIXME: I don't think anybody uses this, ask David
  16355. (defun org-collect-clock-time-entries ()
  16356. "Return an internal list with clocking information.
  16357. This list has one entry for each CLOCK interval.
  16358. FIXME: describe the elements."
  16359. (interactive)
  16360. (let ((re (concat "^[ \t]*" org-clock-string
  16361. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  16362. rtn beg end next cont level title total closedp leafp
  16363. clockpos titlepos h m donep)
  16364. (save-excursion
  16365. (org-clock-sum)
  16366. (goto-char (point-min))
  16367. (while (re-search-forward re nil t)
  16368. (setq clockpos (match-beginning 0)
  16369. beg (match-string 1) end (match-string 2)
  16370. cont (match-end 0))
  16371. (setq beg (apply 'encode-time (org-parse-time-string beg))
  16372. end (apply 'encode-time (org-parse-time-string end)))
  16373. (org-back-to-heading t)
  16374. (setq donep (org-entry-is-done-p))
  16375. (setq titlepos (point)
  16376. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  16377. h (/ total 60) m (- total (* 60 h))
  16378. total (cons h m))
  16379. (looking-at "\\(\\*+\\) +\\(.*\\)")
  16380. (setq level (- (match-end 1) (match-beginning 1))
  16381. title (org-match-string-no-properties 2))
  16382. (save-excursion (outline-next-heading) (setq next (point)))
  16383. (setq closedp (re-search-forward org-closed-time-regexp next t))
  16384. (goto-char next)
  16385. (setq leafp (and (looking-at "^\\*+ ")
  16386. (<= (- (match-end 0) (point)) level)))
  16387. (push (list beg end clockpos closedp donep
  16388. total title titlepos level leafp)
  16389. rtn)
  16390. (goto-char cont)))
  16391. (nreverse rtn)))
  16392. ;;;; Agenda, and Diary Integration
  16393. ;;; Define the Org-agenda-mode
  16394. (defvar org-agenda-mode-map (make-sparse-keymap)
  16395. "Keymap for `org-agenda-mode'.")
  16396. (defvar org-agenda-menu) ; defined later in this file.
  16397. (defvar org-agenda-follow-mode nil)
  16398. (defvar org-agenda-show-log nil)
  16399. (defvar org-agenda-redo-command nil)
  16400. (defvar org-agenda-mode-hook nil)
  16401. (defvar org-agenda-type nil)
  16402. (defvar org-agenda-force-single-file nil)
  16403. (defun org-agenda-mode ()
  16404. "Mode for time-sorted view on action items in Org-mode files.
  16405. The following commands are available:
  16406. \\{org-agenda-mode-map}"
  16407. (interactive)
  16408. (kill-all-local-variables)
  16409. (setq org-agenda-undo-list nil
  16410. org-agenda-pending-undo-list nil)
  16411. (setq major-mode 'org-agenda-mode)
  16412. ;; Keep global-font-lock-mode from turning on font-lock-mode
  16413. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  16414. (setq mode-name "Org-Agenda")
  16415. (use-local-map org-agenda-mode-map)
  16416. (easy-menu-add org-agenda-menu)
  16417. (if org-startup-truncated (setq truncate-lines t))
  16418. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  16419. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  16420. ;; Make sure properties are removed when copying text
  16421. (when (boundp 'buffer-substring-filters)
  16422. (org-set-local 'buffer-substring-filters
  16423. (cons (lambda (x)
  16424. (set-text-properties 0 (length x) nil x) x)
  16425. buffer-substring-filters)))
  16426. (unless org-agenda-keep-modes
  16427. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  16428. org-agenda-show-log nil))
  16429. (easy-menu-change
  16430. '("Agenda") "Agenda Files"
  16431. (append
  16432. (list
  16433. (vector
  16434. (if (get 'org-agenda-files 'org-restrict)
  16435. "Restricted to single file"
  16436. "Edit File List")
  16437. '(org-edit-agenda-file-list)
  16438. (not (get 'org-agenda-files 'org-restrict)))
  16439. "--")
  16440. (mapcar 'org-file-menu-entry (org-agenda-files))))
  16441. (org-agenda-set-mode-name)
  16442. (apply
  16443. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  16444. (list 'org-agenda-mode-hook)))
  16445. (substitute-key-definition 'undo 'org-agenda-undo
  16446. org-agenda-mode-map global-map)
  16447. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  16448. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  16449. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  16450. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  16451. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  16452. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  16453. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  16454. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  16455. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  16456. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  16457. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  16458. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  16459. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  16460. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  16461. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  16462. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  16463. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  16464. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  16465. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  16466. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  16467. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  16468. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  16469. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  16470. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  16471. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  16472. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  16473. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  16474. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  16475. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  16476. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  16477. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  16478. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  16479. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  16480. (while l (org-defkey org-agenda-mode-map
  16481. (int-to-string (pop l)) 'digit-argument)))
  16482. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  16483. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  16484. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  16485. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  16486. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  16487. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  16488. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  16489. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  16490. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  16491. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  16492. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  16493. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  16494. (org-defkey org-agenda-mode-map "n" 'next-line)
  16495. (org-defkey org-agenda-mode-map "p" 'previous-line)
  16496. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  16497. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  16498. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  16499. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  16500. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  16501. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  16502. (eval-after-load "calendar"
  16503. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  16504. 'org-calendar-goto-agenda))
  16505. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  16506. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  16507. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  16508. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  16509. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  16510. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  16511. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  16512. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  16513. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  16514. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  16515. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  16516. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16517. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  16518. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  16519. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  16520. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  16521. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  16522. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  16523. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  16524. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  16525. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  16526. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  16527. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  16528. "Local keymap for agenda entries from Org-mode.")
  16529. (org-defkey org-agenda-keymap
  16530. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  16531. (org-defkey org-agenda-keymap
  16532. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  16533. (when org-agenda-mouse-1-follows-link
  16534. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  16535. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  16536. '("Agenda"
  16537. ("Agenda Files")
  16538. "--"
  16539. ["Show" org-agenda-show t]
  16540. ["Go To (other window)" org-agenda-goto t]
  16541. ["Go To (this window)" org-agenda-switch-to t]
  16542. ["Follow Mode" org-agenda-follow-mode
  16543. :style toggle :selected org-agenda-follow-mode :active t]
  16544. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  16545. "--"
  16546. ["Cycle TODO" org-agenda-todo t]
  16547. ["Archive subtree" org-agenda-archive t]
  16548. ["Delete subtree" org-agenda-kill t]
  16549. "--"
  16550. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  16551. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  16552. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  16553. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  16554. "--"
  16555. ("Tags and Properties"
  16556. ["Show all Tags" org-agenda-show-tags t]
  16557. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  16558. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  16559. "--"
  16560. ["Column View" org-columns t])
  16561. ("Date/Schedule"
  16562. ["Schedule" org-agenda-schedule t]
  16563. ["Set Deadline" org-agenda-deadline t]
  16564. "--"
  16565. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  16566. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  16567. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  16568. ("Clock"
  16569. ["Clock in" org-agenda-clock-in t]
  16570. ["Clock out" org-agenda-clock-out t]
  16571. ["Clock cancel" org-agenda-clock-cancel t]
  16572. ["Goto running clock" org-clock-goto t])
  16573. ("Priority"
  16574. ["Set Priority" org-agenda-priority t]
  16575. ["Increase Priority" org-agenda-priority-up t]
  16576. ["Decrease Priority" org-agenda-priority-down t]
  16577. ["Show Priority" org-agenda-show-priority t])
  16578. ("Calendar/Diary"
  16579. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  16580. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  16581. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  16582. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  16583. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  16584. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  16585. "--"
  16586. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  16587. "--"
  16588. ("View"
  16589. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  16590. :style radio :selected (equal org-agenda-ndays 1)]
  16591. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  16592. :style radio :selected (equal org-agenda-ndays 7)]
  16593. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  16594. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  16595. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  16596. :style radio :selected (member org-agenda-ndays '(365 366))]
  16597. "--"
  16598. ["Show Logbook entries" org-agenda-log-mode
  16599. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  16600. ["Include Diary" org-agenda-toggle-diary
  16601. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  16602. ["Use Time Grid" org-agenda-toggle-time-grid
  16603. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  16604. ["Write view to file" org-write-agenda t]
  16605. ["Rebuild buffer" org-agenda-redo t]
  16606. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  16607. "--"
  16608. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  16609. "--"
  16610. ["Quit" org-agenda-quit t]
  16611. ["Exit and Release Buffers" org-agenda-exit t]
  16612. ))
  16613. ;;; Agenda undo
  16614. (defvar org-agenda-allow-remote-undo t
  16615. "Non-nil means, allow remote undo from the agenda buffer.")
  16616. (defvar org-agenda-undo-list nil
  16617. "List of undoable operations in the agenda since last refresh.")
  16618. (defvar org-agenda-undo-has-started-in nil
  16619. "Buffers that have already seen `undo-start' in the current undo sequence.")
  16620. (defvar org-agenda-pending-undo-list nil
  16621. "In a series of undo commands, this is the list of remaning undo items.")
  16622. (defmacro org-if-unprotected (&rest body)
  16623. "Execute BODY if there is no `org-protected' text property at point."
  16624. (declare (debug t))
  16625. `(unless (get-text-property (point) 'org-protected)
  16626. ,@body))
  16627. (defmacro org-with-remote-undo (_buffer &rest _body)
  16628. "Execute BODY while recording undo information in two buffers."
  16629. (declare (indent 1) (debug t))
  16630. `(let ((_cline (org-current-line))
  16631. (_cmd this-command)
  16632. (_buf1 (current-buffer))
  16633. (_buf2 ,_buffer)
  16634. (_undo1 buffer-undo-list)
  16635. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  16636. _c1 _c2)
  16637. ,@_body
  16638. (when org-agenda-allow-remote-undo
  16639. (setq _c1 (org-verify-change-for-undo
  16640. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  16641. _c2 (org-verify-change-for-undo
  16642. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  16643. (when (or _c1 _c2)
  16644. ;; make sure there are undo boundaries
  16645. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  16646. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  16647. ;; remember which buffer to undo
  16648. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  16649. org-agenda-undo-list)))))
  16650. (defun org-agenda-undo ()
  16651. "Undo a remote editing step in the agenda.
  16652. This undoes changes both in the agenda buffer and in the remote buffer
  16653. that have been changed along."
  16654. (interactive)
  16655. (or org-agenda-allow-remote-undo
  16656. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  16657. (if (not (eq this-command last-command))
  16658. (setq org-agenda-undo-has-started-in nil
  16659. org-agenda-pending-undo-list org-agenda-undo-list))
  16660. (if (not org-agenda-pending-undo-list)
  16661. (error "No further undo information"))
  16662. (let* ((entry (pop org-agenda-pending-undo-list))
  16663. buf line cmd rembuf)
  16664. (setq cmd (pop entry) line (pop entry))
  16665. (setq rembuf (nth 2 entry))
  16666. (org-with-remote-undo rembuf
  16667. (while (bufferp (setq buf (pop entry)))
  16668. (if (pop entry)
  16669. (with-current-buffer buf
  16670. (let ((last-undo-buffer buf)
  16671. (inhibit-read-only t))
  16672. (unless (memq buf org-agenda-undo-has-started-in)
  16673. (push buf org-agenda-undo-has-started-in)
  16674. (make-local-variable 'pending-undo-list)
  16675. (undo-start))
  16676. (while (and pending-undo-list
  16677. (listp pending-undo-list)
  16678. (not (car pending-undo-list)))
  16679. (pop pending-undo-list))
  16680. (undo-more 1))))))
  16681. (goto-line line)
  16682. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  16683. (defun org-verify-change-for-undo (l1 l2)
  16684. "Verify that a real change occurred between the undo lists L1 and L2."
  16685. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  16686. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  16687. (not (eq l1 l2)))
  16688. ;;; Agenda dispatch
  16689. (defvar org-agenda-restrict nil)
  16690. (defvar org-agenda-restrict-begin (make-marker))
  16691. (defvar org-agenda-restrict-end (make-marker))
  16692. (defvar org-agenda-last-dispatch-buffer nil)
  16693. ;;;###autoload
  16694. (defun org-agenda (arg &optional keys restriction)
  16695. "Dispatch agenda commands to collect entries to the agenda buffer.
  16696. Prompts for a command to execute. Any prefix arg will be passed
  16697. on to the selected command. The default selections are:
  16698. a Call `org-agenda-list' to display the agenda for current day or week.
  16699. t Call `org-todo-list' to display the global todo list.
  16700. T Call `org-todo-list' to display the global todo list, select only
  16701. entries with a specific TODO keyword (the user gets a prompt).
  16702. m Call `org-tags-view' to display headlines with tags matching
  16703. a condition (the user is prompted for the condition).
  16704. M Like `m', but select only TODO entries, no ordinary headlines.
  16705. L Create a timeline for the current buffer.
  16706. e Export views to associated files.
  16707. More commands can be added by configuring the variable
  16708. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  16709. searches can be pre-defined in this way.
  16710. If the current buffer is in Org-mode and visiting a file, you can also
  16711. first press `<' once to indicate that the agenda should be temporarily
  16712. \(until the next use of \\[org-agenda]) restricted to the current file.
  16713. Pressing `<' twice means to restrict to the current subtree or region
  16714. \(if active)."
  16715. (interactive "P")
  16716. (catch 'exit
  16717. (let* ((prefix-descriptions nil)
  16718. (org-agenda-custom-commands
  16719. ;; normalize different versions
  16720. (delq nil
  16721. (mapcar
  16722. (lambda (x)
  16723. (cond ((stringp (cdr x))
  16724. (push x prefix-descriptions)
  16725. nil)
  16726. ((stringp (nth 1 x)) x)
  16727. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  16728. (t (cons (car x) (cons "" (cdr x))))))
  16729. org-agenda-custom-commands)))
  16730. (buf (current-buffer))
  16731. (bfn (buffer-file-name (buffer-base-buffer)))
  16732. entry key type match lprops ans)
  16733. ;; Turn off restriction
  16734. (put 'org-agenda-files 'org-restrict nil)
  16735. (setq org-agenda-restrict nil)
  16736. (move-marker org-agenda-restrict-begin nil)
  16737. (move-marker org-agenda-restrict-end nil)
  16738. ;; Delete old local properties
  16739. (put 'org-agenda-redo-command 'org-lprops nil)
  16740. ;; Remember where this call originated
  16741. (setq org-agenda-last-dispatch-buffer (current-buffer))
  16742. (unless keys
  16743. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  16744. keys (car ans)
  16745. restriction (cdr ans)))
  16746. ;; Estabish the restriction, if any
  16747. (when restriction
  16748. (put 'org-agenda-files 'org-restrict (list bfn))
  16749. (cond
  16750. ((eq restriction 'region)
  16751. (setq org-agenda-restrict t)
  16752. (move-marker org-agenda-restrict-begin (region-beginning))
  16753. (move-marker org-agenda-restrict-end (region-end)))
  16754. ((eq restriction 'subtree)
  16755. (save-excursion
  16756. (setq org-agenda-restrict t)
  16757. (org-back-to-heading t)
  16758. (move-marker org-agenda-restrict-begin (point))
  16759. (move-marker org-agenda-restrict-end
  16760. (progn (org-end-of-subtree t)))))))
  16761. (require 'calendar) ; FIXME: can we avoid this for some commands?
  16762. ;; For example the todo list should not need it (but does...)
  16763. (cond
  16764. ((setq entry (assoc keys org-agenda-custom-commands))
  16765. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  16766. (progn
  16767. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  16768. (put 'org-agenda-redo-command 'org-lprops lprops)
  16769. (cond
  16770. ((eq type 'agenda)
  16771. (org-let lprops '(org-agenda-list current-prefix-arg)))
  16772. ((eq type 'alltodo)
  16773. (org-let lprops '(org-todo-list current-prefix-arg)))
  16774. ((eq type 'stuck)
  16775. (org-let lprops '(org-agenda-list-stuck-projects
  16776. current-prefix-arg)))
  16777. ((eq type 'tags)
  16778. (org-let lprops '(org-tags-view current-prefix-arg match)))
  16779. ((eq type 'tags-todo)
  16780. (org-let lprops '(org-tags-view '(4) match)))
  16781. ((eq type 'todo)
  16782. (org-let lprops '(org-todo-list match)))
  16783. ((eq type 'tags-tree)
  16784. (org-check-for-org-mode)
  16785. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  16786. ((eq type 'todo-tree)
  16787. (org-check-for-org-mode)
  16788. (org-let lprops
  16789. '(org-occur (concat "^" outline-regexp "[ \t]*"
  16790. (regexp-quote match) "\\>"))))
  16791. ((eq type 'occur-tree)
  16792. (org-check-for-org-mode)
  16793. (org-let lprops '(org-occur match)))
  16794. ((functionp type)
  16795. (org-let lprops '(funcall type match)))
  16796. ((fboundp type)
  16797. (org-let lprops '(funcall type match)))
  16798. (t (error "Invalid custom agenda command type %s" type))))
  16799. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  16800. ((equal keys "C") (customize-variable 'org-agenda-custom-commands))
  16801. ((equal keys "a") (call-interactively 'org-agenda-list))
  16802. ((equal keys "t") (call-interactively 'org-todo-list))
  16803. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  16804. ((equal keys "m") (call-interactively 'org-tags-view))
  16805. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  16806. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  16807. ((equal keys "L")
  16808. (unless (org-mode-p)
  16809. (error "This is not an Org-mode file"))
  16810. (unless restriction
  16811. (put 'org-agenda-files 'org-restrict (list bfn))
  16812. (org-call-with-arg 'org-timeline arg)))
  16813. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  16814. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  16815. ((equal keys "!") (customize-variable 'org-stuck-projects))
  16816. (t (error "Invalid agenda key"))))))
  16817. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  16818. "The user interface for selecting an agenda command."
  16819. (catch 'exit
  16820. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  16821. (restrict-ok (and bfn (org-mode-p)))
  16822. (region-p (org-region-active-p))
  16823. (custom org-agenda-custom-commands)
  16824. (selstring "")
  16825. restriction second-time
  16826. c entry key type match prefixes rmheader header-end custom1 desc)
  16827. (save-window-excursion
  16828. (delete-other-windows)
  16829. (org-switch-to-buffer-other-window " *Agenda Commands*")
  16830. (erase-buffer)
  16831. (insert (eval-when-compile
  16832. (let ((header
  16833. "Press key for an agenda command: < Buffer,subtree/region restriction
  16834. -------------------------------- C Configure custom agenda commands
  16835. a Agenda for current week or day e Export agenda views
  16836. t List of all TODO entries T Entries with special TODO kwd
  16837. m Match a TAGS query M Like m, but only TODO entries
  16838. L Timeline for current buffer # List stuck projects (!=configure)
  16839. / Multi-occur
  16840. ")
  16841. (start 0))
  16842. (while (string-match
  16843. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  16844. header start)
  16845. (setq start (match-end 0))
  16846. (add-text-properties (match-beginning 2) (match-end 2)
  16847. '(face bold) header))
  16848. header)))
  16849. (setq header-end (move-marker (make-marker) (point)))
  16850. (while t
  16851. (setq custom1 custom)
  16852. (when (eq rmheader t)
  16853. (goto-line 1)
  16854. (re-search-forward ":" nil t)
  16855. (delete-region (match-end 0) (line-end-position))
  16856. (forward-char 1)
  16857. (looking-at "-+")
  16858. (delete-region (match-end 0) (line-end-position))
  16859. (move-marker header-end (match-end 0)))
  16860. (goto-char header-end)
  16861. (delete-region (point) (point-max))
  16862. (while (setq entry (pop custom1))
  16863. (setq key (car entry) desc (nth 1 entry)
  16864. type (nth 2 entry) match (nth 3 entry))
  16865. (if (> (length key) 1)
  16866. (add-to-list 'prefixes (string-to-char key))
  16867. (insert
  16868. (format
  16869. "\n%-4s%-14s: %s"
  16870. (org-add-props (copy-sequence key)
  16871. '(face bold))
  16872. (cond
  16873. ((string-match "\\S-" desc) desc)
  16874. ((eq type 'agenda) "Agenda for current week or day")
  16875. ((eq type 'alltodo) "List of all TODO entries")
  16876. ((eq type 'stuck) "List of stuck projects")
  16877. ((eq type 'todo) "TODO keyword")
  16878. ((eq type 'tags) "Tags query")
  16879. ((eq type 'tags-todo) "Tags (TODO)")
  16880. ((eq type 'tags-tree) "Tags tree")
  16881. ((eq type 'todo-tree) "TODO kwd tree")
  16882. ((eq type 'occur-tree) "Occur tree")
  16883. ((functionp type) (if (symbolp type)
  16884. (symbol-name type)
  16885. "Lambda expression"))
  16886. (t "???"))
  16887. (cond
  16888. ((stringp match)
  16889. (org-add-props match nil 'face 'org-warning))
  16890. (match
  16891. (format "set of %d commands" (length match)))
  16892. (t ""))))))
  16893. (when prefixes
  16894. (mapcar (lambda (x)
  16895. (insert
  16896. (format "\n%s %s"
  16897. (org-add-props (char-to-string x)
  16898. nil 'face 'bold)
  16899. (or (cdr (assoc (concat selstring (char-to-string x))
  16900. prefix-descriptions))
  16901. "Prefix key"))))
  16902. prefixes))
  16903. (goto-char (point-min))
  16904. (when (fboundp 'fit-window-to-buffer)
  16905. (if second-time
  16906. (if (not (pos-visible-in-window-p (point-max)))
  16907. (fit-window-to-buffer))
  16908. (setq second-time t)
  16909. (fit-window-to-buffer)))
  16910. (message "Press key for agenda command%s:"
  16911. (if restrict-ok
  16912. (if restriction
  16913. (format " (restricted to %s)" restriction)
  16914. " (unrestricted)")
  16915. ""))
  16916. (setq c (read-char-exclusive))
  16917. (message "")
  16918. (cond
  16919. ((assoc (char-to-string c) custom)
  16920. (setq selstring (concat selstring (char-to-string c)))
  16921. (throw 'exit (cons selstring restriction)))
  16922. ((memq c prefixes)
  16923. (setq selstring (concat selstring (char-to-string c))
  16924. prefixes nil
  16925. rmheader (or rmheader t)
  16926. custom (delq nil (mapcar
  16927. (lambda (x)
  16928. (if (or (= (length (car x)) 1)
  16929. (/= (string-to-char (car x)) c))
  16930. nil
  16931. (cons (substring (car x) 1) (cdr x))))
  16932. custom))))
  16933. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  16934. (message "Restriction is only possible in Org-mode buffers")
  16935. (ding) (sit-for 1))
  16936. ((eq c ?1)
  16937. (setq restriction 'buffer))
  16938. ((eq c ?0)
  16939. (setq restriction (if region-p 'region 'subtree)))
  16940. ((eq c ?<)
  16941. (setq restriction
  16942. (cond
  16943. ((eq restriction 'buffer)
  16944. (if region-p 'region 'subtree))
  16945. ((memq restriction '(subtree region))
  16946. nil)
  16947. (t 'buffer))))
  16948. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?/)))
  16949. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  16950. ((equal c ?q) (error "Abort"))
  16951. (t (error "Invalid key %c" c))))))))
  16952. (defun org-run-agenda-series (name series)
  16953. (org-prepare-agenda name)
  16954. (let* ((org-agenda-multi t)
  16955. (redo (list 'org-run-agenda-series name (list 'quote series)))
  16956. (cmds (car series))
  16957. (gprops (nth 1 series))
  16958. match ;; The byte compiler incorrectly complains about this. Keep it!
  16959. cmd type lprops)
  16960. (while (setq cmd (pop cmds))
  16961. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  16962. (cond
  16963. ((eq type 'agenda)
  16964. (org-let2 gprops lprops
  16965. '(call-interactively 'org-agenda-list)))
  16966. ((eq type 'alltodo)
  16967. (org-let2 gprops lprops
  16968. '(call-interactively 'org-todo-list)))
  16969. ((eq type 'stuck)
  16970. (org-let2 gprops lprops
  16971. '(call-interactively 'org-agenda-list-stuck-projects)))
  16972. ((eq type 'tags)
  16973. (org-let2 gprops lprops
  16974. '(org-tags-view current-prefix-arg match)))
  16975. ((eq type 'tags-todo)
  16976. (org-let2 gprops lprops
  16977. '(org-tags-view '(4) match)))
  16978. ((eq type 'todo)
  16979. (org-let2 gprops lprops
  16980. '(org-todo-list match)))
  16981. ((fboundp type)
  16982. (org-let2 gprops lprops
  16983. '(funcall type match)))
  16984. (t (error "Invalid type in command series"))))
  16985. (widen)
  16986. (setq org-agenda-redo-command redo)
  16987. (goto-char (point-min)))
  16988. (org-finalize-agenda))
  16989. ;;;###autoload
  16990. (defmacro org-batch-agenda (cmd-key &rest parameters)
  16991. "Run an agenda command in batch mode and send the result to STDOUT.
  16992. If CMD-KEY is a string of length 1, it is used as a key in
  16993. `org-agenda-custom-commands' and triggers this command. If it is a
  16994. longer string is is used as a tags/todo match string.
  16995. Paramters are alternating variable names and values that will be bound
  16996. before running the agenda command."
  16997. (let (pars)
  16998. (while parameters
  16999. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17000. (if (> (length cmd-key) 2)
  17001. (eval (list 'let (nreverse pars)
  17002. (list 'org-tags-view nil cmd-key)))
  17003. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17004. (set-buffer org-agenda-buffer-name)
  17005. (princ (org-encode-for-stdout (buffer-string)))))
  17006. (defun org-encode-for-stdout (string)
  17007. (if (fboundp 'encode-coding-string)
  17008. (encode-coding-string string buffer-file-coding-system)
  17009. string))
  17010. (defvar org-agenda-info nil)
  17011. ;;;###autoload
  17012. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  17013. "Run an agenda command in batch mode and send the result to STDOUT.
  17014. If CMD-KEY is a string of length 1, it is used as a key in
  17015. `org-agenda-custom-commands' and triggers this command. If it is a
  17016. longer string is is used as a tags/todo match string.
  17017. Paramters are alternating variable names and values that will be bound
  17018. before running the agenda command.
  17019. The output gives a line for each selected agenda item. Each
  17020. item is a list of comma-separated values, like this:
  17021. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  17022. category The category of the item
  17023. head The headline, without TODO kwd, TAGS and PRIORITY
  17024. type The type of the agenda entry, can be
  17025. todo selected in TODO match
  17026. tagsmatch selected in tags match
  17027. diary imported from diary
  17028. deadline a deadline on given date
  17029. scheduled scheduled on given date
  17030. timestamp entry has timestamp on given date
  17031. closed entry was closed on given date
  17032. upcoming-deadline warning about deadline
  17033. past-scheduled forwarded scheduled item
  17034. block entry has date block including g. date
  17035. todo The todo keyword, if any
  17036. tags All tags including inherited ones, separated by colons
  17037. date The relevant date, like 2007-2-14
  17038. time The time, like 15:00-16:50
  17039. extra Sting with extra planning info
  17040. priority-l The priority letter if any was given
  17041. priority-n The computed numerical priority
  17042. agenda-day The day in the agenda where this is listed"
  17043. (let (pars)
  17044. (while parameters
  17045. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17046. (push (list 'org-agenda-remove-tags t) pars)
  17047. (if (> (length cmd-key) 2)
  17048. (eval (list 'let (nreverse pars)
  17049. (list 'org-tags-view nil cmd-key)))
  17050. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17051. (set-buffer org-agenda-buffer-name)
  17052. (let* ((lines (org-split-string (buffer-string) "\n"))
  17053. line)
  17054. (while (setq line (pop lines))
  17055. (catch 'next
  17056. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  17057. (setq org-agenda-info
  17058. (org-fix-agenda-info (text-properties-at 0 line)))
  17059. (princ
  17060. (org-encode-for-stdout
  17061. (mapconcat 'org-agenda-export-csv-mapper
  17062. '(org-category txt type todo tags date time-of-day extra
  17063. priority-letter priority agenda-day)
  17064. ",")))
  17065. (princ "\n"))))))
  17066. (defun org-fix-agenda-info (props)
  17067. "Make sure all properties on an agenda item have a canonical form,
  17068. so the the export commands caneasily use it."
  17069. (let (tmp re)
  17070. (when (setq tmp (plist-get props 'tags))
  17071. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  17072. (when (setq tmp (plist-get props 'date))
  17073. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  17074. (let ((calendar-date-display-form '(year "-" month "-" day)))
  17075. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  17076. (setq tmp (calendar-date-string tmp)))
  17077. (setq props (plist-put props 'date tmp)))
  17078. (when (setq tmp (plist-get props 'day))
  17079. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  17080. (let ((calendar-date-display-form '(year "-" month "-" day)))
  17081. (setq tmp (calendar-date-string tmp)))
  17082. (setq props (plist-put props 'day tmp))
  17083. (setq props (plist-put props 'agenda-day tmp)))
  17084. (when (setq tmp (plist-get props 'txt))
  17085. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  17086. (plist-put props 'priority-letter (match-string 1 tmp))
  17087. (setq tmp (replace-match "" t t tmp)))
  17088. (when (and (setq re (plist-get props 'org-todo-regexp))
  17089. (setq re (concat "\\`\\.*" re " ?"))
  17090. (string-match re tmp))
  17091. (plist-put props 'todo (match-string 1 tmp))
  17092. (setq tmp (replace-match "" t t tmp)))
  17093. (plist-put props 'txt tmp)))
  17094. props)
  17095. (defun org-agenda-export-csv-mapper (prop)
  17096. (let ((res (plist-get org-agenda-info prop)))
  17097. (setq res
  17098. (cond
  17099. ((not res) "")
  17100. ((stringp res) res)
  17101. (t (prin1-to-string res))))
  17102. (while (string-match "," res)
  17103. (setq res (replace-match ";" t t res)))
  17104. (org-trim res)))
  17105. ;;;###autoload
  17106. (defun org-store-agenda-views (&rest parameters)
  17107. (interactive)
  17108. (eval (list 'org-batch-store-agenda-views)))
  17109. ;; FIXME, why is this a macro?????
  17110. ;;;###autoload
  17111. (defmacro org-batch-store-agenda-views (&rest parameters)
  17112. "Run all custom agenda commands that have a file argument."
  17113. (let ((cmds org-agenda-custom-commands)
  17114. (pop-up-frames nil)
  17115. (dir default-directory)
  17116. pars cmd thiscmdkey files opts)
  17117. (while parameters
  17118. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17119. (setq pars (reverse pars))
  17120. (save-window-excursion
  17121. (while cmds
  17122. (setq cmd (pop cmds)
  17123. thiscmdkey (car cmd)
  17124. opts (nth 3 cmd)
  17125. files (nth 4 cmd))
  17126. (if (stringp files) (setq files (list files)))
  17127. (when files
  17128. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  17129. (list 'org-agenda nil thiscmdkey)))
  17130. (set-buffer org-agenda-buffer-name)
  17131. (while files
  17132. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  17133. (list 'org-write-agenda
  17134. (expand-file-name (pop files) dir) t))))
  17135. (and (get-buffer org-agenda-buffer-name)
  17136. (kill-buffer org-agenda-buffer-name)))))))
  17137. (defun org-write-agenda (file &optional nosettings)
  17138. "Write the current buffer (an agenda view) as a file.
  17139. Depending on the extension of the file name, plain text (.txt),
  17140. HTML (.html or .htm) or Postscript (.ps) is produced.
  17141. If NOSETTINGS is given, do not scope the settings of
  17142. `org-agenda-exporter-settings' into the export commands. This is used when
  17143. the settings have already been scoped and we do not wish to overrule other,
  17144. higher priority settings."
  17145. (interactive "FWrite agenda to file: ")
  17146. (if (not (file-writable-p file))
  17147. (error "Cannot write agenda to file %s" file))
  17148. (cond
  17149. ((string-match "\\.html?\\'" file) (require 'htmlize))
  17150. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  17151. (org-let (if nosettings nil org-agenda-exporter-settings)
  17152. '(save-excursion
  17153. (save-window-excursion
  17154. (cond
  17155. ((string-match "\\.html?\\'" file)
  17156. (set-buffer (htmlize-buffer (current-buffer)))
  17157. (when (and org-agenda-export-html-style
  17158. (string-match "<style>" org-agenda-export-html-style))
  17159. ;; replace <style> section with org-agenda-export-html-style
  17160. (goto-char (point-min))
  17161. (kill-region (- (search-forward "<style") 6)
  17162. (search-forward "</style>"))
  17163. (insert org-agenda-export-html-style))
  17164. (write-file file)
  17165. (kill-buffer (current-buffer))
  17166. (message "HTML written to %s" file))
  17167. ((string-match "\\.ps\\'" file)
  17168. (ps-print-buffer-with-faces file)
  17169. (message "Postscript written to %s" file))
  17170. (t
  17171. (let ((bs (buffer-string)))
  17172. (find-file file)
  17173. (insert bs)
  17174. (save-buffer 0)
  17175. (kill-buffer (current-buffer))
  17176. (message "Plain text written to %s" file))))))
  17177. (set-buffer org-agenda-buffer-name)))
  17178. (defmacro org-no-read-only (&rest body)
  17179. "Inhibit read-only for BODY."
  17180. `(let ((inhibit-read-only t)) ,@body))
  17181. (defun org-check-for-org-mode ()
  17182. "Make sure current buffer is in org-mode. Error if not."
  17183. (or (org-mode-p)
  17184. (error "Cannot execute org-mode agenda command on buffer in %s."
  17185. major-mode)))
  17186. (defun org-fit-agenda-window ()
  17187. "Fit the window to the buffer size."
  17188. (and (memq org-agenda-window-setup '(reorganize-frame))
  17189. (fboundp 'fit-window-to-buffer)
  17190. (fit-window-to-buffer
  17191. nil
  17192. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  17193. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  17194. ;;; Agenda file list
  17195. (defun org-agenda-files (&optional unrestricted)
  17196. "Get the list of agenda files.
  17197. Optional UNRESTRICTED means return the full list even if a restriction
  17198. is currently in place."
  17199. (let ((files
  17200. (cond
  17201. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  17202. ((stringp org-agenda-files) (org-read-agenda-file-list))
  17203. ((listp org-agenda-files) org-agenda-files)
  17204. (t (error "Invalid value of `org-agenda-files'")))))
  17205. (setq files (apply 'append
  17206. (mapcar (lambda (f)
  17207. (if (file-directory-p f)
  17208. (directory-files f t "\\.org\\'")
  17209. (list f)))
  17210. files)))
  17211. (if org-agenda-skip-unavailable-files
  17212. (delq nil
  17213. (mapcar (function
  17214. (lambda (file)
  17215. (and (file-readable-p file) file)))
  17216. files))
  17217. files))) ; `org-check-agenda-file' will remove them from the list
  17218. (defun org-edit-agenda-file-list ()
  17219. "Edit the list of agenda files.
  17220. Depending on setup, this either uses customize to edit the variable
  17221. `org-agenda-files', or it visits the file that is holding the list. In the
  17222. latter case, the buffer is set up in a way that saving it automatically kills
  17223. the buffer and restores the previous window configuration."
  17224. (interactive)
  17225. (if (stringp org-agenda-files)
  17226. (let ((cw (current-window-configuration)))
  17227. (find-file org-agenda-files)
  17228. (org-set-local 'org-window-configuration cw)
  17229. (org-add-hook 'after-save-hook
  17230. (lambda ()
  17231. (set-window-configuration
  17232. (prog1 org-window-configuration
  17233. (kill-buffer (current-buffer))))
  17234. (org-install-agenda-files-menu)
  17235. (message "New agenda file list installed"))
  17236. nil 'local)
  17237. (message (substitute-command-keys
  17238. "Edit list and finish with \\[save-buffer]")))
  17239. (customize-variable 'org-agenda-files)))
  17240. (defun org-store-new-agenda-file-list (list)
  17241. "Set new value for the agenda file list and save it correcly."
  17242. (if (stringp org-agenda-files)
  17243. (let ((f org-agenda-files) b)
  17244. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  17245. (with-temp-file f
  17246. (insert (mapconcat 'identity list "\n") "\n")))
  17247. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  17248. (setq org-agenda-files list)
  17249. (customize-save-variable 'org-agenda-files org-agenda-files))))
  17250. (defun org-read-agenda-file-list ()
  17251. "Read the list of agenda files from a file."
  17252. (when (stringp org-agenda-files)
  17253. (with-temp-buffer
  17254. (insert-file-contents org-agenda-files)
  17255. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  17256. ;;;###autoload
  17257. (defun org-cycle-agenda-files ()
  17258. "Cycle through the files in `org-agenda-files'.
  17259. If the current buffer visits an agenda file, find the next one in the list.
  17260. If the current buffer does not, find the first agenda file."
  17261. (interactive)
  17262. (let* ((fs (org-agenda-files t))
  17263. (files (append fs (list (car fs))))
  17264. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  17265. file)
  17266. (unless files (error "No agenda files"))
  17267. (catch 'exit
  17268. (while (setq file (pop files))
  17269. (if (equal (file-truename file) tcf)
  17270. (when (car files)
  17271. (find-file (car files))
  17272. (throw 'exit t))))
  17273. (find-file (car fs)))
  17274. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  17275. (defun org-agenda-file-to-front (&optional to-end)
  17276. "Move/add the current file to the top of the agenda file list.
  17277. If the file is not present in the list, it is added to the front. If it is
  17278. present, it is moved there. With optional argument TO-END, add/move to the
  17279. end of the list."
  17280. (interactive "P")
  17281. (let ((org-agenda-skip-unavailable-files nil)
  17282. (file-alist (mapcar (lambda (x)
  17283. (cons (file-truename x) x))
  17284. (org-agenda-files t)))
  17285. (ctf (file-truename buffer-file-name))
  17286. x had)
  17287. (setq x (assoc ctf file-alist) had x)
  17288. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17289. (if to-end
  17290. (setq file-alist (append (delq x file-alist) (list x)))
  17291. (setq file-alist (cons x (delq x file-alist))))
  17292. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17293. (org-install-agenda-files-menu)
  17294. (message "File %s to %s of agenda file list"
  17295. (if had "moved" "added") (if to-end "end" "front"))))
  17296. (defun org-remove-file (&optional file)
  17297. "Remove current file from the list of files in variable `org-agenda-files'.
  17298. These are the files which are being checked for agenda entries.
  17299. Optional argument FILE means, use this file instead of the current."
  17300. (interactive)
  17301. (let* ((org-agenda-skip-unavailable-files nil)
  17302. (file (or file buffer-file-name))
  17303. (true-file (file-truename file))
  17304. (afile (abbreviate-file-name file))
  17305. (files (delq nil (mapcar
  17306. (lambda (x)
  17307. (if (equal true-file
  17308. (file-truename x))
  17309. nil x))
  17310. (org-agenda-files t)))))
  17311. (if (not (= (length files) (length (org-agenda-files t))))
  17312. (progn
  17313. (org-store-new-agenda-file-list files)
  17314. (org-install-agenda-files-menu)
  17315. (message "Removed file: %s" afile))
  17316. (message "File was not in list: %s" afile))))
  17317. (defun org-file-menu-entry (file)
  17318. (vector file (list 'find-file file) t))
  17319. (defun org-check-agenda-file (file)
  17320. "Make sure FILE exists. If not, ask user what to do."
  17321. (when (not (file-exists-p file))
  17322. (message "non-existent file %s. [R]emove from list or [A]bort?"
  17323. (abbreviate-file-name file))
  17324. (let ((r (downcase (read-char-exclusive))))
  17325. (cond
  17326. ((equal r ?r)
  17327. (org-remove-file file)
  17328. (throw 'nextfile t))
  17329. (t (error "Abort"))))))
  17330. ;;; Agenda prepare and finalize
  17331. (defvar org-agenda-multi nil) ; dynammically scoped
  17332. (defvar org-agenda-buffer-name "*Org Agenda*")
  17333. (defvar org-pre-agenda-window-conf nil)
  17334. (defvar org-agenda-name nil)
  17335. (defun org-prepare-agenda (&optional name)
  17336. (setq org-todo-keywords-for-agenda nil)
  17337. (setq org-done-keywords-for-agenda nil)
  17338. (if org-agenda-multi
  17339. (progn
  17340. (setq buffer-read-only nil)
  17341. (goto-char (point-max))
  17342. (unless (or (bobp) org-agenda-compact-blocks)
  17343. (insert "\n" (make-string (window-width) ?=) "\n"))
  17344. (narrow-to-region (point) (point-max)))
  17345. (org-agenda-maybe-reset-markers 'force)
  17346. (org-prepare-agenda-buffers (org-agenda-files))
  17347. (setq org-todo-keywords-for-agenda
  17348. (org-uniquify org-todo-keywords-for-agenda))
  17349. (setq org-done-keywords-for-agenda
  17350. (org-uniquify org-done-keywords-for-agenda))
  17351. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  17352. (awin (get-buffer-window abuf)))
  17353. (cond
  17354. ((equal (current-buffer) abuf) nil)
  17355. (awin (select-window awin))
  17356. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  17357. ((equal org-agenda-window-setup 'current-window)
  17358. (switch-to-buffer abuf))
  17359. ((equal org-agenda-window-setup 'other-window)
  17360. (org-switch-to-buffer-other-window abuf))
  17361. ((equal org-agenda-window-setup 'other-frame)
  17362. (switch-to-buffer-other-frame abuf))
  17363. ((equal org-agenda-window-setup 'reorganize-frame)
  17364. (delete-other-windows)
  17365. (org-switch-to-buffer-other-window abuf))))
  17366. (setq buffer-read-only nil)
  17367. (erase-buffer)
  17368. (org-agenda-mode)
  17369. (and name (not org-agenda-name)
  17370. (org-set-local 'org-agenda-name name)))
  17371. (setq buffer-read-only nil))
  17372. (defun org-finalize-agenda ()
  17373. "Finishing touch for the agenda buffer, called just before displaying it."
  17374. (unless org-agenda-multi
  17375. (save-excursion
  17376. (let ((inhibit-read-only t))
  17377. (goto-char (point-min))
  17378. (while (org-activate-bracket-links (point-max))
  17379. (add-text-properties (match-beginning 0) (match-end 0)
  17380. '(face org-link)))
  17381. (org-agenda-align-tags)
  17382. (unless org-agenda-with-colors
  17383. (remove-text-properties (point-min) (point-max) '(face nil))))
  17384. (if (and (boundp 'org-overriding-columns-format)
  17385. org-overriding-columns-format)
  17386. (org-set-local 'org-overriding-columns-format
  17387. org-overriding-columns-format))
  17388. (if (and (boundp 'org-agenda-view-columns-initially)
  17389. org-agenda-view-columns-initially)
  17390. (org-agenda-columns))
  17391. (when org-agenda-fontify-priorities
  17392. (org-fontify-priorities))
  17393. (run-hooks 'org-finalize-agenda-hook))))
  17394. (defun org-fontify-priorities ()
  17395. "Make highest priority lines bold, and lowest italic."
  17396. (interactive)
  17397. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  17398. (org-delete-overlay o)))
  17399. (overlays-in (point-min) (point-max)))
  17400. (save-excursion
  17401. (let ((ovs (org-overlays-in (point-min) (point-max)))
  17402. (inhibit-read-only t)
  17403. b e p ov h l)
  17404. (goto-char (point-min))
  17405. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  17406. (setq h (or (get-char-property (point) 'org-highest-priority)
  17407. org-highest-priority)
  17408. l (or (get-char-property (point) 'org-lowest-priority)
  17409. org-lowest-priority)
  17410. p (string-to-char (match-string 1))
  17411. b (match-beginning 0) e (line-end-position)
  17412. ov (org-make-overlay b e))
  17413. (org-overlay-put
  17414. ov 'face
  17415. (cond ((listp org-agenda-fontify-priorities)
  17416. (cdr (assoc p org-agenda-fontify-priorities)))
  17417. ((equal p l) 'italic)
  17418. ((equal p h) 'bold)))
  17419. (org-overlay-put ov 'org-type 'org-priority)))))
  17420. (defun org-prepare-agenda-buffers (files)
  17421. "Create buffers for all agenda files, protect archived trees and comments."
  17422. (interactive)
  17423. (let ((pa '(:org-archived t))
  17424. (pc '(:org-comment t))
  17425. (pall '(:org-archived t :org-comment t))
  17426. (inhibit-read-only t)
  17427. (rea (concat ":" org-archive-tag ":"))
  17428. bmp file re)
  17429. (save-excursion
  17430. (save-restriction
  17431. (while (setq file (pop files))
  17432. (org-check-agenda-file file)
  17433. (set-buffer (org-get-agenda-file-buffer file))
  17434. (widen)
  17435. (setq bmp (buffer-modified-p))
  17436. (org-refresh-category-properties)
  17437. (setq org-todo-keywords-for-agenda
  17438. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17439. (setq org-done-keywords-for-agenda
  17440. (append org-done-keywords-for-agenda org-done-keywords))
  17441. (save-excursion
  17442. (remove-text-properties (point-min) (point-max) pall)
  17443. (when org-agenda-skip-archived-trees
  17444. (goto-char (point-min))
  17445. (while (re-search-forward rea nil t)
  17446. (if (org-on-heading-p t)
  17447. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17448. (goto-char (point-min))
  17449. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  17450. (while (re-search-forward re nil t)
  17451. (add-text-properties
  17452. (match-beginning 0) (org-end-of-subtree t) pc)))
  17453. (set-buffer-modified-p bmp))))))
  17454. (defvar org-agenda-skip-function nil
  17455. "Function to be called at each match during agenda construction.
  17456. If this function returns nil, the current match should not be skipped.
  17457. Otherwise, the function must return a position from where the search
  17458. should be continued.
  17459. This may also be a Lisp form, it will be evaluated.
  17460. Never set this variable using `setq' or so, because then it will apply
  17461. to all future agenda commands. Instead, bind it with `let' to scope
  17462. it dynamically into the agenda-constructing command. A good way to set
  17463. it is through options in org-agenda-custom-commands.")
  17464. (defun org-agenda-skip ()
  17465. "Throw to `:skip' in places that should be skipped.
  17466. Also moves point to the end of the skipped region, so that search can
  17467. continue from there."
  17468. (let ((p (point-at-bol)) to fp)
  17469. (and org-agenda-skip-archived-trees
  17470. (get-text-property p :org-archived)
  17471. (org-end-of-subtree t)
  17472. (throw :skip t))
  17473. (and (get-text-property p :org-comment)
  17474. (org-end-of-subtree t)
  17475. (throw :skip t))
  17476. (if (equal (char-after p) ?#) (throw :skip t))
  17477. (when (and (or (setq fp (functionp org-agenda-skip-function))
  17478. (consp org-agenda-skip-function))
  17479. (setq to (save-excursion
  17480. (save-match-data
  17481. (if fp
  17482. (funcall org-agenda-skip-function)
  17483. (eval org-agenda-skip-function))))))
  17484. (goto-char to)
  17485. (throw :skip t))))
  17486. (defvar org-agenda-markers nil
  17487. "List of all currently active markers created by `org-agenda'.")
  17488. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  17489. "Creation time of the last agenda marker.")
  17490. (defun org-agenda-new-marker (&optional pos)
  17491. "Return a new agenda marker.
  17492. Org-mode keeps a list of these markers and resets them when they are
  17493. no longer in use."
  17494. (let ((m (copy-marker (or pos (point)))))
  17495. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  17496. (push m org-agenda-markers)
  17497. m))
  17498. (defun org-agenda-maybe-reset-markers (&optional force)
  17499. "Reset markers created by `org-agenda'. But only if they are old enough."
  17500. (if (or (and force (not org-agenda-multi))
  17501. (> (- (time-to-seconds (current-time))
  17502. org-agenda-last-marker-time)
  17503. 5))
  17504. (while org-agenda-markers
  17505. (move-marker (pop org-agenda-markers) nil))))
  17506. (defvar org-agenda-new-buffers nil
  17507. "Buffers created to visit agenda files.")
  17508. (defun org-get-agenda-file-buffer (file)
  17509. "Get a buffer visiting FILE. If the buffer needs to be created, add
  17510. it to the list of buffers which might be released later."
  17511. (let ((buf (org-find-base-buffer-visiting file)))
  17512. (if buf
  17513. buf ; just return it
  17514. ;; Make a new buffer and remember it
  17515. (setq buf (find-file-noselect file))
  17516. (if buf (push buf org-agenda-new-buffers))
  17517. buf)))
  17518. (defun org-release-buffers (blist)
  17519. "Release all buffers in list, asking the user for confirmation when needed.
  17520. When a buffer is unmodified, it is just killed. When modified, it is saved
  17521. \(if the user agrees) and then killed."
  17522. (let (buf file)
  17523. (while (setq buf (pop blist))
  17524. (setq file (buffer-file-name buf))
  17525. (when (and (buffer-modified-p buf)
  17526. file
  17527. (y-or-n-p (format "Save file %s? " file)))
  17528. (with-current-buffer buf (save-buffer)))
  17529. (kill-buffer buf))))
  17530. (defun org-get-category (&optional pos)
  17531. "Get the category applying to position POS."
  17532. (get-text-property (or pos (point)) 'org-category))
  17533. ;;; Agenda timeline
  17534. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  17535. (defun org-timeline (&optional include-all)
  17536. "Show a time-sorted view of the entries in the current org file.
  17537. Only entries with a time stamp of today or later will be listed. With
  17538. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  17539. under the current date.
  17540. If the buffer contains an active region, only check the region for
  17541. dates."
  17542. (interactive "P")
  17543. (require 'calendar)
  17544. (org-compile-prefix-format 'timeline)
  17545. (org-set-sorting-strategy 'timeline)
  17546. (let* ((dopast t)
  17547. (dotodo include-all)
  17548. (doclosed org-agenda-show-log)
  17549. (entry buffer-file-name)
  17550. (date (calendar-current-date))
  17551. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  17552. (end (if (org-region-active-p) (region-end) (point-max)))
  17553. (day-numbers (org-get-all-dates beg end 'no-ranges
  17554. t doclosed ; always include today
  17555. org-timeline-show-empty-dates))
  17556. (org-deadline-warning-days 0)
  17557. (org-agenda-only-exact-dates t)
  17558. (today (time-to-days (current-time)))
  17559. (past t)
  17560. args
  17561. s e rtn d emptyp)
  17562. (setq org-agenda-redo-command
  17563. (list 'progn
  17564. (list 'org-switch-to-buffer-other-window (current-buffer))
  17565. (list 'org-timeline (list 'quote include-all))))
  17566. (if (not dopast)
  17567. ;; Remove past dates from the list of dates.
  17568. (setq day-numbers (delq nil (mapcar (lambda(x)
  17569. (if (>= x today) x nil))
  17570. day-numbers))))
  17571. (org-prepare-agenda (concat "Timeline "
  17572. (file-name-nondirectory buffer-file-name)))
  17573. (if doclosed (push :closed args))
  17574. (push :timestamp args)
  17575. (push :deadline args)
  17576. (push :scheduled args)
  17577. (push :sexp args)
  17578. (if dotodo (push :todo args))
  17579. (while (setq d (pop day-numbers))
  17580. (if (and (listp d) (eq (car d) :omitted))
  17581. (progn
  17582. (setq s (point))
  17583. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  17584. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  17585. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  17586. (if (and (>= d today)
  17587. dopast
  17588. past)
  17589. (progn
  17590. (setq past nil)
  17591. (insert (make-string 79 ?-) "\n")))
  17592. (setq date (calendar-gregorian-from-absolute d))
  17593. (setq s (point))
  17594. (setq rtn (and (not emptyp)
  17595. (apply 'org-agenda-get-day-entries entry
  17596. date args)))
  17597. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  17598. (progn
  17599. (insert
  17600. (if (stringp org-agenda-format-date)
  17601. (format-time-string org-agenda-format-date
  17602. (org-time-from-absolute date))
  17603. (funcall org-agenda-format-date date))
  17604. "\n")
  17605. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  17606. (put-text-property s (1- (point)) 'org-date-line t)
  17607. (if (equal d today)
  17608. (put-text-property s (1- (point)) 'org-today t))
  17609. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  17610. (put-text-property s (1- (point)) 'day d)))))
  17611. (goto-char (point-min))
  17612. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  17613. (point-min)))
  17614. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  17615. (org-finalize-agenda)
  17616. (setq buffer-read-only t)))
  17617. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  17618. "Return a list of all relevant day numbers from BEG to END buffer positions.
  17619. If NO-RANGES is non-nil, include only the start and end dates of a range,
  17620. not every single day in the range. If FORCE-TODAY is non-nil, make
  17621. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  17622. inactive time stamps (those in square brackets) are included.
  17623. When EMPTY is non-nil, also include days without any entries."
  17624. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  17625. dates dates1 date day day1 day2 ts1 ts2)
  17626. (if force-today
  17627. (setq dates (list (time-to-days (current-time)))))
  17628. (save-excursion
  17629. (goto-char beg)
  17630. (while (re-search-forward re end t)
  17631. (setq day (time-to-days (org-time-string-to-time
  17632. (substring (match-string 1) 0 10))))
  17633. (or (memq day dates) (push day dates)))
  17634. (unless no-ranges
  17635. (goto-char beg)
  17636. (while (re-search-forward org-tr-regexp end t)
  17637. (setq ts1 (substring (match-string 1) 0 10)
  17638. ts2 (substring (match-string 2) 0 10)
  17639. day1 (time-to-days (org-time-string-to-time ts1))
  17640. day2 (time-to-days (org-time-string-to-time ts2)))
  17641. (while (< (setq day1 (1+ day1)) day2)
  17642. (or (memq day1 dates) (push day1 dates)))))
  17643. (setq dates (sort dates '<))
  17644. (when empty
  17645. (while (setq day (pop dates))
  17646. (setq day2 (car dates))
  17647. (push day dates1)
  17648. (when (and day2 empty)
  17649. (if (or (eq empty t)
  17650. (and (numberp empty) (<= (- day2 day) empty)))
  17651. (while (< (setq day (1+ day)) day2)
  17652. (push (list day) dates1))
  17653. (push (cons :omitted (- day2 day)) dates1))))
  17654. (setq dates (nreverse dates1)))
  17655. dates)))
  17656. ;;; Agenda Daily/Weekly
  17657. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  17658. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  17659. (defvar org-agenda-last-arguments nil
  17660. "The arguments of the previous call to org-agenda")
  17661. (defvar org-starting-day nil) ; local variable in the agenda buffer
  17662. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  17663. (defvar org-include-all-loc nil) ; local variable
  17664. (defvar org-agenda-remove-date nil) ; dynamically scoped
  17665. ;;;###autoload
  17666. (defun org-agenda-list (&optional include-all start-day ndays)
  17667. "Produce a weekly view from all files in variable `org-agenda-files'.
  17668. The view will be for the current week, but from the overview buffer you
  17669. will be able to go to other weeks.
  17670. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  17671. also be shown, under the current date.
  17672. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  17673. on the days are also shown. See the variable `org-log-done' for how
  17674. to turn on logging.
  17675. START-DAY defaults to TODAY, or to the most recent match for the weekday
  17676. given in `org-agenda-start-on-weekday'.
  17677. NDAYS defaults to `org-agenda-ndays'."
  17678. (interactive "P")
  17679. (setq ndays (or ndays org-agenda-ndays)
  17680. start-day (or start-day org-agenda-start-day))
  17681. (if org-agenda-overriding-arguments
  17682. (setq include-all (car org-agenda-overriding-arguments)
  17683. start-day (nth 1 org-agenda-overriding-arguments)
  17684. ndays (nth 2 org-agenda-overriding-arguments)))
  17685. (if (stringp start-day)
  17686. ;; Convert to an absolute day number
  17687. (setq start-day (time-to-days (org-read-date nil t start-day))))
  17688. (setq org-agenda-last-arguments (list include-all start-day ndays))
  17689. (org-compile-prefix-format 'agenda)
  17690. (org-set-sorting-strategy 'agenda)
  17691. (require 'calendar)
  17692. (let* ((org-agenda-start-on-weekday
  17693. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  17694. org-agenda-start-on-weekday nil))
  17695. (thefiles (org-agenda-files))
  17696. (files thefiles)
  17697. (today (time-to-days (current-time)))
  17698. (sd (or start-day today))
  17699. (start (if (or (null org-agenda-start-on-weekday)
  17700. (< org-agenda-ndays 7))
  17701. sd
  17702. (let* ((nt (calendar-day-of-week
  17703. (calendar-gregorian-from-absolute sd)))
  17704. (n1 org-agenda-start-on-weekday)
  17705. (d (- nt n1)))
  17706. (- sd (+ (if (< d 0) 7 0) d)))))
  17707. (day-numbers (list start))
  17708. (day-cnt 0)
  17709. (inhibit-redisplay (not debug-on-error))
  17710. s e rtn rtnall file date d start-pos end-pos todayp nd)
  17711. (setq org-agenda-redo-command
  17712. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  17713. ;; Make the list of days
  17714. (setq ndays (or ndays org-agenda-ndays)
  17715. nd ndays)
  17716. (while (> ndays 1)
  17717. (push (1+ (car day-numbers)) day-numbers)
  17718. (setq ndays (1- ndays)))
  17719. (setq day-numbers (nreverse day-numbers))
  17720. (org-prepare-agenda "Day/Week")
  17721. (org-set-local 'org-starting-day (car day-numbers))
  17722. (org-set-local 'org-include-all-loc include-all)
  17723. (org-set-local 'org-agenda-span
  17724. (org-agenda-ndays-to-span nd))
  17725. (when (and (or include-all org-agenda-include-all-todo)
  17726. (member today day-numbers))
  17727. (setq files thefiles
  17728. rtnall nil)
  17729. (while (setq file (pop files))
  17730. (catch 'nextfile
  17731. (org-check-agenda-file file)
  17732. (setq date (calendar-gregorian-from-absolute today)
  17733. rtn (org-agenda-get-day-entries
  17734. file date :todo))
  17735. (setq rtnall (append rtnall rtn))))
  17736. (when rtnall
  17737. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  17738. (add-text-properties (point-min) (1- (point))
  17739. (list 'face 'org-agenda-structure))
  17740. (insert (org-finalize-agenda-entries rtnall) "\n")))
  17741. (unless org-agenda-compact-blocks
  17742. (setq s (point))
  17743. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  17744. "-agenda:\n")
  17745. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  17746. 'org-date-line t)))
  17747. (while (setq d (pop day-numbers))
  17748. (setq date (calendar-gregorian-from-absolute d)
  17749. s (point))
  17750. (if (or (setq todayp (= d today))
  17751. (and (not start-pos) (= d sd)))
  17752. (setq start-pos (point))
  17753. (if (and start-pos (not end-pos))
  17754. (setq end-pos (point))))
  17755. (setq files thefiles
  17756. rtnall nil)
  17757. (while (setq file (pop files))
  17758. (catch 'nextfile
  17759. (org-check-agenda-file file)
  17760. (if org-agenda-show-log
  17761. (setq rtn (org-agenda-get-day-entries
  17762. file date
  17763. :deadline :scheduled :timestamp :sexp :closed))
  17764. (setq rtn (org-agenda-get-day-entries
  17765. file date
  17766. :deadline :scheduled :sexp :timestamp)))
  17767. (setq rtnall (append rtnall rtn))))
  17768. (if org-agenda-include-diary
  17769. (progn
  17770. (require 'diary-lib)
  17771. (setq rtn (org-get-entries-from-diary date))
  17772. (setq rtnall (append rtnall rtn))))
  17773. (if (or rtnall org-agenda-show-all-dates)
  17774. (progn
  17775. (setq day-cnt (1+ day-cnt))
  17776. (insert
  17777. (if (stringp org-agenda-format-date)
  17778. (format-time-string org-agenda-format-date
  17779. (org-time-from-absolute date))
  17780. (funcall org-agenda-format-date date))
  17781. "\n")
  17782. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  17783. (put-text-property s (1- (point)) 'org-date-line t)
  17784. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  17785. (if todayp (put-text-property s (1- (point)) 'org-today t))
  17786. (if rtnall (insert
  17787. (org-finalize-agenda-entries
  17788. (org-agenda-add-time-grid-maybe
  17789. rtnall nd todayp))
  17790. "\n"))
  17791. (put-text-property s (1- (point)) 'day d)
  17792. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  17793. (goto-char (point-min))
  17794. (org-fit-agenda-window)
  17795. (unless (and (pos-visible-in-window-p (point-min))
  17796. (pos-visible-in-window-p (point-max)))
  17797. (goto-char (1- (point-max)))
  17798. (recenter -1)
  17799. (if (not (pos-visible-in-window-p (or start-pos 1)))
  17800. (progn
  17801. (goto-char (or start-pos 1))
  17802. (recenter 1))))
  17803. (goto-char (or start-pos 1))
  17804. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  17805. (org-finalize-agenda)
  17806. (setq buffer-read-only t)
  17807. (message "")))
  17808. (defun org-agenda-ndays-to-span (n)
  17809. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  17810. ;;; Agenda TODO list
  17811. (defvar org-select-this-todo-keyword nil)
  17812. (defvar org-last-arg nil)
  17813. ;;;###autoload
  17814. (defun org-todo-list (arg)
  17815. "Show all TODO entries from all agenda file in a single list.
  17816. The prefix arg can be used to select a specific TODO keyword and limit
  17817. the list to these. When using \\[universal-argument], you will be prompted
  17818. for a keyword. A numeric prefix directly selects the Nth keyword in
  17819. `org-todo-keywords-1'."
  17820. (interactive "P")
  17821. (require 'calendar)
  17822. (org-compile-prefix-format 'todo)
  17823. (org-set-sorting-strategy 'todo)
  17824. (org-prepare-agenda "TODO")
  17825. (let* ((today (time-to-days (current-time)))
  17826. (date (calendar-gregorian-from-absolute today))
  17827. (kwds org-todo-keywords-for-agenda)
  17828. (completion-ignore-case t)
  17829. (org-select-this-todo-keyword
  17830. (if (stringp arg) arg
  17831. (and arg (integerp arg) (> arg 0)
  17832. (nth (1- arg) kwds))))
  17833. rtn rtnall files file pos)
  17834. (when (equal arg '(4))
  17835. (setq org-select-this-todo-keyword
  17836. (completing-read "Keyword (or KWD1|K2D2|...): "
  17837. (mapcar 'list kwds) nil nil)))
  17838. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  17839. (org-set-local 'org-last-arg arg)
  17840. (setq org-agenda-redo-command
  17841. '(org-todo-list (or current-prefix-arg org-last-arg)))
  17842. (setq files (org-agenda-files)
  17843. rtnall nil)
  17844. (while (setq file (pop files))
  17845. (catch 'nextfile
  17846. (org-check-agenda-file file)
  17847. (setq rtn (org-agenda-get-day-entries file date :todo))
  17848. (setq rtnall (append rtnall rtn))))
  17849. (if org-agenda-overriding-header
  17850. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  17851. nil 'face 'org-agenda-structure) "\n")
  17852. (insert "Global list of TODO items of type: ")
  17853. (add-text-properties (point-min) (1- (point))
  17854. (list 'face 'org-agenda-structure))
  17855. (setq pos (point))
  17856. (insert (or org-select-this-todo-keyword "ALL") "\n")
  17857. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  17858. (setq pos (point))
  17859. (unless org-agenda-multi
  17860. (insert "Available with `N r': (0)ALL")
  17861. (let ((n 0) s)
  17862. (mapc (lambda (x)
  17863. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  17864. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  17865. (insert "\n "))
  17866. (insert " " s))
  17867. kwds))
  17868. (insert "\n"))
  17869. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  17870. (when rtnall
  17871. (insert (org-finalize-agenda-entries rtnall) "\n"))
  17872. (goto-char (point-min))
  17873. (org-fit-agenda-window)
  17874. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  17875. (org-finalize-agenda)
  17876. (setq buffer-read-only t)))
  17877. ;;; Agenda tags match
  17878. ;;;###autoload
  17879. (defun org-tags-view (&optional todo-only match)
  17880. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  17881. The prefix arg TODO-ONLY limits the search to TODO entries."
  17882. (interactive "P")
  17883. (org-compile-prefix-format 'tags)
  17884. (org-set-sorting-strategy 'tags)
  17885. (let* ((org-tags-match-list-sublevels
  17886. (if todo-only t org-tags-match-list-sublevels))
  17887. (completion-ignore-case t)
  17888. rtn rtnall files file pos matcher
  17889. buffer)
  17890. (setq matcher (org-make-tags-matcher match)
  17891. match (car matcher) matcher (cdr matcher))
  17892. (org-prepare-agenda (concat "TAGS " match))
  17893. (setq org-agenda-redo-command
  17894. (list 'org-tags-view (list 'quote todo-only)
  17895. (list 'if 'current-prefix-arg nil match)))
  17896. (setq files (org-agenda-files)
  17897. rtnall nil)
  17898. (while (setq file (pop files))
  17899. (catch 'nextfile
  17900. (org-check-agenda-file file)
  17901. (setq buffer (if (file-exists-p file)
  17902. (org-get-agenda-file-buffer file)
  17903. (error "No such file %s" file)))
  17904. (if (not buffer)
  17905. ;; If file does not exist, merror message to agenda
  17906. (setq rtn (list
  17907. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  17908. rtnall (append rtnall rtn))
  17909. (with-current-buffer buffer
  17910. (unless (org-mode-p)
  17911. (error "Agenda file %s is not in `org-mode'" file))
  17912. (save-excursion
  17913. (save-restriction
  17914. (if org-agenda-restrict
  17915. (narrow-to-region org-agenda-restrict-begin
  17916. org-agenda-restrict-end)
  17917. (widen))
  17918. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  17919. (setq rtnall (append rtnall rtn))))))))
  17920. (if org-agenda-overriding-header
  17921. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  17922. nil 'face 'org-agenda-structure) "\n")
  17923. (insert "Headlines with TAGS match: ")
  17924. (add-text-properties (point-min) (1- (point))
  17925. (list 'face 'org-agenda-structure))
  17926. (setq pos (point))
  17927. (insert match "\n")
  17928. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  17929. (setq pos (point))
  17930. (unless org-agenda-multi
  17931. (insert "Press `C-u r' to search again with new search string\n"))
  17932. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  17933. (when rtnall
  17934. (insert (org-finalize-agenda-entries rtnall) "\n"))
  17935. (goto-char (point-min))
  17936. (org-fit-agenda-window)
  17937. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  17938. (org-finalize-agenda)
  17939. (setq buffer-read-only t)))
  17940. ;;; Agenda Finding stuck projects
  17941. (defvar org-agenda-skip-regexp nil
  17942. "Regular expression used in skipping subtrees for the agenda.
  17943. This is basically a temporary global variable that can be set and then
  17944. used by user-defined selections using `org-agenda-skip-function'.")
  17945. (defvar org-agenda-overriding-header nil
  17946. "When this is set during todo and tags searches, will replace header.")
  17947. (defun org-agenda-skip-subtree-when-regexp-matches ()
  17948. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  17949. If yes, it returns the end position of this tree, causing agenda commands
  17950. to skip this subtree. This is a function that can be put into
  17951. `org-agenda-skip-function' for the duration of a command."
  17952. (let ((end (save-excursion (org-end-of-subtree t)))
  17953. skip)
  17954. (save-excursion
  17955. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  17956. (and skip end)))
  17957. (defun org-agenda-skip-entry-if (&rest conditions)
  17958. "Skip entry if any of CONDITIONS is true.
  17959. See `org-agenda-skip-if for details."
  17960. (org-agenda-skip-if nil conditions))
  17961. (defun org-agenda-skip-subtree-if (&rest conditions)
  17962. "Skip entry if any of CONDITIONS is true.
  17963. See `org-agenda-skip-if for details."
  17964. (org-agenda-skip-if t conditions))
  17965. (defun org-agenda-skip-if (subtree conditions)
  17966. "Checks current entity for CONDITIONS.
  17967. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  17968. the entry, i.e. the text before the next heading is checked.
  17969. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  17970. from different tests. Valid conditions are:
  17971. scheduled Check if there is a scheduled cookie
  17972. notscheduled Check if there is no scheduled cookie
  17973. deadline Check if there is a deadline
  17974. notdeadline Check if there is no deadline
  17975. regexp Check if regexp matches
  17976. notregexp Check if regexp does not match.
  17977. The regexp is taken from the conditions list, it must com right after the
  17978. `regexp' of `notregexp' element.
  17979. If any of these conditions is met, this function returns the end point of
  17980. the entity, causing the search to continue from there. This is a function
  17981. that can be put into `org-agenda-skip-function' for the duration of a command."
  17982. (let (beg end m r)
  17983. (org-back-to-heading t)
  17984. (setq beg (point)
  17985. end (if subtree
  17986. (progn (org-end-of-subtree t) (point))
  17987. (progn (outline-next-heading) (1- (point)))))
  17988. (goto-char beg)
  17989. (and
  17990. (or
  17991. (and (memq 'scheduled conditions)
  17992. (re-search-forward org-scheduled-time-regexp end t))
  17993. (and (memq 'notscheduled conditions)
  17994. (not (re-search-forward org-scheduled-time-regexp end t)))
  17995. (and (memq 'deadline conditions)
  17996. (re-search-forward org-deadline-time-regexp end t))
  17997. (and (memq 'notdeadline conditions)
  17998. (not (re-search-forward org-deadline-time-regexp end t)))
  17999. (and (setq m (memq 'regexp conditions))
  18000. (stringp (setq r (nth 1 m)))
  18001. (re-search-forward (nth 1 m) end t))
  18002. (and (setq m (memq 'notregexp conditions))
  18003. (stringp (setq r (nth 1 m)))
  18004. (not (re-search-forward (nth 1 m) end t))))
  18005. end)))
  18006. (defun org-agenda-list-stuck-projects (&rest ignore)
  18007. "Create agenda view for projects that are stuck.
  18008. Stuck projects are project that have no next actions. For the definitions
  18009. of what a project is and how to check if it stuck, customize the variable
  18010. `org-stuck-projects'.
  18011. MATCH is being ignored."
  18012. (interactive)
  18013. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  18014. ;; FIXME: we could have used org-agenda-skip-if here.
  18015. (org-agenda-overriding-header "List of stuck projects: ")
  18016. (matcher (nth 0 org-stuck-projects))
  18017. (todo (nth 1 org-stuck-projects))
  18018. (todo-wds (if (member "*" todo)
  18019. (progn
  18020. (org-prepare-agenda-buffers (org-agenda-files))
  18021. (org-delete-all
  18022. org-done-keywords-for-agenda
  18023. (copy-sequence org-todo-keywords-for-agenda)))
  18024. todo))
  18025. (todo-re (concat "^\\*+[ \t]+\\("
  18026. (mapconcat 'identity todo-wds "\\|")
  18027. "\\)\\>"))
  18028. (tags (nth 2 org-stuck-projects))
  18029. (tags-re (if (member "*" tags)
  18030. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  18031. (concat "^\\*+ .*:\\("
  18032. (mapconcat 'identity tags "\\|")
  18033. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  18034. (gen-re (nth 3 org-stuck-projects))
  18035. (re-list
  18036. (delq nil
  18037. (list
  18038. (if todo todo-re)
  18039. (if tags tags-re)
  18040. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  18041. gen-re)))))
  18042. (setq org-agenda-skip-regexp
  18043. (if re-list
  18044. (mapconcat 'identity re-list "\\|")
  18045. (error "No information how to identify unstuck projects")))
  18046. (org-tags-view nil matcher)
  18047. (with-current-buffer org-agenda-buffer-name
  18048. (setq org-agenda-redo-command
  18049. '(org-agenda-list-stuck-projects
  18050. (or current-prefix-arg org-last-arg))))))
  18051. ;;; Diary integration
  18052. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  18053. (defun org-get-entries-from-diary (date)
  18054. "Get the (Emacs Calendar) diary entries for DATE."
  18055. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  18056. (diary-display-hook '(fancy-diary-display))
  18057. (pop-up-frames nil)
  18058. (list-diary-entries-hook
  18059. (cons 'org-diary-default-entry list-diary-entries-hook))
  18060. (diary-file-name-prefix-function nil) ; turn this feature off
  18061. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  18062. entries
  18063. (org-disable-agenda-to-diary t))
  18064. (save-excursion
  18065. (save-window-excursion
  18066. (funcall (if (fboundp 'diary-list-entries)
  18067. 'diary-list-entries 'list-diary-entries)
  18068. date 1)))
  18069. (if (not (get-buffer fancy-diary-buffer))
  18070. (setq entries nil)
  18071. (with-current-buffer fancy-diary-buffer
  18072. (setq buffer-read-only nil)
  18073. (if (zerop (buffer-size))
  18074. ;; No entries
  18075. (setq entries nil)
  18076. ;; Omit the date and other unnecessary stuff
  18077. (org-agenda-cleanup-fancy-diary)
  18078. ;; Add prefix to each line and extend the text properties
  18079. (if (zerop (buffer-size))
  18080. (setq entries nil)
  18081. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  18082. (set-buffer-modified-p nil)
  18083. (kill-buffer fancy-diary-buffer)))
  18084. (when entries
  18085. (setq entries (org-split-string entries "\n"))
  18086. (setq entries
  18087. (mapcar
  18088. (lambda (x)
  18089. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  18090. ;; Extend the text properties to the beginning of the line
  18091. (org-add-props x (text-properties-at (1- (length x)) x)
  18092. 'type "diary" 'date date))
  18093. entries)))))
  18094. (defun org-agenda-cleanup-fancy-diary ()
  18095. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  18096. This gets rid of the date, the underline under the date, and
  18097. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  18098. date. It also removes lines that contain only whitespace."
  18099. (goto-char (point-min))
  18100. (if (looking-at ".*?:[ \t]*")
  18101. (progn
  18102. (replace-match "")
  18103. (re-search-forward "\n=+$" nil t)
  18104. (replace-match "")
  18105. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  18106. (re-search-forward "\n=+$" nil t)
  18107. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  18108. (goto-char (point-min))
  18109. (while (re-search-forward "^ +\n" nil t)
  18110. (replace-match ""))
  18111. (goto-char (point-min))
  18112. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  18113. (replace-match "")))
  18114. ;; Make sure entries from the diary have the right text properties.
  18115. (eval-after-load "diary-lib"
  18116. '(if (boundp 'diary-modify-entry-list-string-function)
  18117. ;; We can rely on the hook, nothing to do
  18118. nil
  18119. ;; Hook not avaiable, must use advice to make this work
  18120. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  18121. "Make the position visible."
  18122. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  18123. (stringp string)
  18124. buffer-file-name)
  18125. (setq string (org-modify-diary-entry-string string))))))
  18126. (defun org-modify-diary-entry-string (string)
  18127. "Add text properties to string, allowing org-mode to act on it."
  18128. (org-add-props string nil
  18129. 'mouse-face 'highlight
  18130. 'keymap org-agenda-keymap
  18131. 'help-echo (if buffer-file-name
  18132. (format "mouse-2 or RET jump to diary file %s"
  18133. (abbreviate-file-name buffer-file-name))
  18134. "")
  18135. 'org-agenda-diary-link t
  18136. 'org-marker (org-agenda-new-marker (point-at-bol))))
  18137. (defun org-diary-default-entry ()
  18138. "Add a dummy entry to the diary.
  18139. Needed to avoid empty dates which mess up holiday display."
  18140. ;; Catch the error if dealing with the new add-to-diary-alist
  18141. (when org-disable-agenda-to-diary
  18142. (condition-case nil
  18143. (add-to-diary-list original-date "Org-mode dummy" "")
  18144. (error
  18145. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  18146. ;;;###autoload
  18147. (defun org-diary (&rest args)
  18148. "Return diary information from org-files.
  18149. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  18150. It accesses org files and extracts information from those files to be
  18151. listed in the diary. The function accepts arguments specifying what
  18152. items should be listed. The following arguments are allowed:
  18153. :timestamp List the headlines of items containing a date stamp or
  18154. date range matching the selected date. Deadlines will
  18155. also be listed, on the expiration day.
  18156. :sexp List entries resulting from diary-like sexps.
  18157. :deadline List any deadlines past due, or due within
  18158. `org-deadline-warning-days'. The listing occurs only
  18159. in the diary for *today*, not at any other date. If
  18160. an entry is marked DONE, it is no longer listed.
  18161. :scheduled List all items which are scheduled for the given date.
  18162. The diary for *today* also contains items which were
  18163. scheduled earlier and are not yet marked DONE.
  18164. :todo List all TODO items from the org-file. This may be a
  18165. long list - so this is not turned on by default.
  18166. Like deadlines, these entries only show up in the
  18167. diary for *today*, not at any other date.
  18168. The call in the diary file should look like this:
  18169. &%%(org-diary) ~/path/to/some/orgfile.org
  18170. Use a separate line for each org file to check. Or, if you omit the file name,
  18171. all files listed in `org-agenda-files' will be checked automatically:
  18172. &%%(org-diary)
  18173. If you don't give any arguments (as in the example above), the default
  18174. arguments (:deadline :scheduled :timestamp :sexp) are used.
  18175. So the example above may also be written as
  18176. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  18177. The function expects the lisp variables `entry' and `date' to be provided
  18178. by the caller, because this is how the calendar works. Don't use this
  18179. function from a program - use `org-agenda-get-day-entries' instead."
  18180. (org-agenda-maybe-reset-markers)
  18181. (org-compile-prefix-format 'agenda)
  18182. (org-set-sorting-strategy 'agenda)
  18183. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  18184. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  18185. (list entry)
  18186. (org-agenda-files t)))
  18187. file rtn results)
  18188. (org-prepare-agenda-buffers files)
  18189. ;; If this is called during org-agenda, don't return any entries to
  18190. ;; the calendar. Org Agenda will list these entries itself.
  18191. (if org-disable-agenda-to-diary (setq files nil))
  18192. (while (setq file (pop files))
  18193. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  18194. (setq results (append results rtn)))
  18195. (if results
  18196. (concat (org-finalize-agenda-entries results) "\n"))))
  18197. ;;; Agenda entry finders
  18198. (defun org-agenda-get-day-entries (file date &rest args)
  18199. "Does the work for `org-diary' and `org-agenda'.
  18200. FILE is the path to a file to be checked for entries. DATE is date like
  18201. the one returned by `calendar-current-date'. ARGS are symbols indicating
  18202. which kind of entries should be extracted. For details about these, see
  18203. the documentation of `org-diary'."
  18204. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  18205. (let* ((org-startup-folded nil)
  18206. (org-startup-align-all-tables nil)
  18207. (buffer (if (file-exists-p file)
  18208. (org-get-agenda-file-buffer file)
  18209. (error "No such file %s" file)))
  18210. arg results rtn)
  18211. (if (not buffer)
  18212. ;; If file does not exist, make sure an error message ends up in diary
  18213. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  18214. (with-current-buffer buffer
  18215. (unless (org-mode-p)
  18216. (error "Agenda file %s is not in `org-mode'" file))
  18217. (let ((case-fold-search nil))
  18218. (save-excursion
  18219. (save-restriction
  18220. (if org-agenda-restrict
  18221. (narrow-to-region org-agenda-restrict-begin
  18222. org-agenda-restrict-end)
  18223. (widen))
  18224. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  18225. (while (setq arg (pop args))
  18226. (cond
  18227. ((and (eq arg :todo)
  18228. (equal date (calendar-current-date)))
  18229. (setq rtn (org-agenda-get-todos))
  18230. (setq results (append results rtn)))
  18231. ((eq arg :timestamp)
  18232. (setq rtn (org-agenda-get-blocks))
  18233. (setq results (append results rtn))
  18234. (setq rtn (org-agenda-get-timestamps))
  18235. (setq results (append results rtn)))
  18236. ((eq arg :sexp)
  18237. (setq rtn (org-agenda-get-sexps))
  18238. (setq results (append results rtn)))
  18239. ((eq arg :scheduled)
  18240. (setq rtn (org-agenda-get-scheduled))
  18241. (setq results (append results rtn)))
  18242. ((eq arg :closed)
  18243. (setq rtn (org-agenda-get-closed))
  18244. (setq results (append results rtn)))
  18245. ((eq arg :deadline)
  18246. (setq rtn (org-agenda-get-deadlines))
  18247. (setq results (append results rtn))))))))
  18248. results))))
  18249. ;; FIXME: this works only if the cursor is *not* at the
  18250. ;; beginning of the entry
  18251. ;(defun org-entry-is-done-p ()
  18252. ; "Is the current entry marked DONE?"
  18253. ; (save-excursion
  18254. ; (and (re-search-backward "[\r\n]\\*+ " nil t)
  18255. ; (looking-at org-nl-done-regexp))))
  18256. (defun org-entry-is-todo-p ()
  18257. (member (org-get-todo-state) org-not-done-keywords))
  18258. (defun org-entry-is-done-p ()
  18259. (member (org-get-todo-state) org-done-keywords))
  18260. (defun org-get-todo-state ()
  18261. (save-excursion
  18262. (org-back-to-heading t)
  18263. (and (looking-at org-todo-line-regexp)
  18264. (match-end 2)
  18265. (match-string 2))))
  18266. (defun org-at-date-range-p (&optional inactive-ok)
  18267. "Is the cursor inside a date range?"
  18268. (interactive)
  18269. (save-excursion
  18270. (catch 'exit
  18271. (let ((pos (point)))
  18272. (skip-chars-backward "^[<\r\n")
  18273. (skip-chars-backward "<[")
  18274. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  18275. (>= (match-end 0) pos)
  18276. (throw 'exit t))
  18277. (skip-chars-backward "^<[\r\n")
  18278. (skip-chars-backward "<[")
  18279. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  18280. (>= (match-end 0) pos)
  18281. (throw 'exit t)))
  18282. nil)))
  18283. (defun org-agenda-get-todos ()
  18284. "Return the TODO information for agenda display."
  18285. (let* ((props (list 'face nil
  18286. 'done-face 'org-done
  18287. 'org-not-done-regexp org-not-done-regexp
  18288. 'org-todo-regexp org-todo-regexp
  18289. 'mouse-face 'highlight
  18290. 'keymap org-agenda-keymap
  18291. 'help-echo
  18292. (format "mouse-2 or RET jump to org file %s"
  18293. (abbreviate-file-name buffer-file-name))))
  18294. ;; FIXME: get rid of the \n at some point but watch out
  18295. (regexp (concat "^\\*+[ \t]+\\("
  18296. (if org-select-this-todo-keyword
  18297. (if (equal org-select-this-todo-keyword "*")
  18298. org-todo-regexp
  18299. (concat "\\<\\("
  18300. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  18301. "\\)\\>"))
  18302. org-not-done-regexp)
  18303. "[^\n\r]*\\)"))
  18304. marker priority category tags
  18305. ee txt beg end)
  18306. (goto-char (point-min))
  18307. (while (re-search-forward regexp nil t)
  18308. (catch :skip
  18309. (save-match-data
  18310. (beginning-of-line)
  18311. (setq beg (point) end (progn (outline-next-heading) (point)))
  18312. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  18313. (re-search-forward org-ts-regexp end t))
  18314. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  18315. (re-search-forward org-scheduled-time-regexp end t))
  18316. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  18317. (re-search-forward org-deadline-time-regexp end t)
  18318. (org-deadline-close (match-string 1))))
  18319. (goto-char (1+ beg))
  18320. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  18321. (throw :skip nil)))
  18322. (goto-char beg)
  18323. (org-agenda-skip)
  18324. (goto-char (match-beginning 1))
  18325. (setq marker (org-agenda-new-marker (match-beginning 0))
  18326. category (org-get-category)
  18327. tags (org-get-tags-at (point))
  18328. txt (org-format-agenda-item "" (match-string 1) category tags)
  18329. priority (1+ (org-get-priority txt)))
  18330. (org-add-props txt props
  18331. 'org-marker marker 'org-hd-marker marker
  18332. 'priority priority 'org-category category
  18333. 'type "todo")
  18334. (push txt ee)
  18335. (if org-agenda-todo-list-sublevels
  18336. (goto-char (match-end 1))
  18337. (org-end-of-subtree 'invisible))))
  18338. (nreverse ee)))
  18339. (defconst org-agenda-no-heading-message
  18340. "No heading for this item in buffer or region.")
  18341. (defun org-agenda-get-timestamps ()
  18342. "Return the date stamp information for agenda display."
  18343. (let* ((props (list 'face nil
  18344. 'org-not-done-regexp org-not-done-regexp
  18345. 'org-todo-regexp org-todo-regexp
  18346. 'mouse-face 'highlight
  18347. 'keymap org-agenda-keymap
  18348. 'help-echo
  18349. (format "mouse-2 or RET jump to org file %s"
  18350. (abbreviate-file-name buffer-file-name))))
  18351. (d1 (calendar-absolute-from-gregorian date))
  18352. (remove-re
  18353. (concat
  18354. (regexp-quote
  18355. (format-time-string
  18356. "<%Y-%m-%d"
  18357. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18358. ".*?>"))
  18359. (regexp
  18360. (concat
  18361. (regexp-quote
  18362. (substring
  18363. (format-time-string
  18364. (car org-time-stamp-formats)
  18365. (apply 'encode-time ; DATE bound by calendar
  18366. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  18367. 0 11))
  18368. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  18369. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  18370. marker hdmarker deadlinep scheduledp donep tmp priority category
  18371. ee txt timestr tags b0 b3 e3)
  18372. (goto-char (point-min))
  18373. (while (re-search-forward regexp nil t)
  18374. (setq b0 (match-beginning 0)
  18375. b3 (match-beginning 3) e3 (match-end 3))
  18376. (catch :skip
  18377. (and (org-at-date-range-p) (throw :skip nil))
  18378. (org-agenda-skip)
  18379. (if (and (match-end 1)
  18380. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  18381. (throw :skip nil))
  18382. (if (and e3
  18383. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  18384. (throw :skip nil))
  18385. (setq marker (org-agenda-new-marker b0)
  18386. category (org-get-category b0)
  18387. tmp (buffer-substring (max (point-min)
  18388. (- b0 org-ds-keyword-length))
  18389. b0)
  18390. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  18391. deadlinep (string-match org-deadline-regexp tmp)
  18392. scheduledp (string-match org-scheduled-regexp tmp)
  18393. donep (org-entry-is-done-p))
  18394. (if (or scheduledp deadlinep) (throw :skip t))
  18395. (if (string-match ">" timestr)
  18396. ;; substring should only run to end of time stamp
  18397. (setq timestr (substring timestr 0 (match-end 0))))
  18398. (save-excursion
  18399. (if (re-search-backward "^\\*+ " nil t)
  18400. (progn
  18401. (goto-char (match-beginning 0))
  18402. (setq hdmarker (org-agenda-new-marker)
  18403. tags (org-get-tags-at))
  18404. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  18405. (setq txt (org-format-agenda-item
  18406. nil (match-string 1) category tags timestr nil
  18407. remove-re)))
  18408. (setq txt org-agenda-no-heading-message))
  18409. (setq priority (org-get-priority txt))
  18410. (org-add-props txt props
  18411. 'org-marker marker 'org-hd-marker hdmarker)
  18412. (org-add-props txt nil 'priority priority
  18413. 'org-category category 'date date
  18414. 'type "timestamp")
  18415. (push txt ee))
  18416. (outline-next-heading)))
  18417. (nreverse ee)))
  18418. (defun org-agenda-get-sexps ()
  18419. "Return the sexp information for agenda display."
  18420. (require 'diary-lib)
  18421. (let* ((props (list 'face nil
  18422. 'mouse-face 'highlight
  18423. 'keymap org-agenda-keymap
  18424. 'help-echo
  18425. (format "mouse-2 or RET jump to org file %s"
  18426. (abbreviate-file-name buffer-file-name))))
  18427. (regexp "^&?%%(")
  18428. marker category ee txt tags entry result beg b sexp sexp-entry)
  18429. (goto-char (point-min))
  18430. (while (re-search-forward regexp nil t)
  18431. (catch :skip
  18432. (org-agenda-skip)
  18433. (setq beg (match-beginning 0))
  18434. (goto-char (1- (match-end 0)))
  18435. (setq b (point))
  18436. (forward-sexp 1)
  18437. (setq sexp (buffer-substring b (point)))
  18438. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  18439. (org-trim (match-string 1))
  18440. ""))
  18441. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  18442. (when result
  18443. (setq marker (org-agenda-new-marker beg)
  18444. category (org-get-category beg))
  18445. (if (string-match "\\S-" result)
  18446. (setq txt result)
  18447. (setq txt "SEXP entry returned empty string"))
  18448. (setq txt (org-format-agenda-item
  18449. "" txt category tags 'time))
  18450. (org-add-props txt props 'org-marker marker)
  18451. (org-add-props txt nil
  18452. 'org-category category 'date date
  18453. 'type "sexp")
  18454. (push txt ee))))
  18455. (nreverse ee)))
  18456. (defun org-agenda-get-closed ()
  18457. "Return the logged TODO entries for agenda display."
  18458. (let* ((props (list 'mouse-face 'highlight
  18459. 'org-not-done-regexp org-not-done-regexp
  18460. 'org-todo-regexp org-todo-regexp
  18461. 'keymap org-agenda-keymap
  18462. 'help-echo
  18463. (format "mouse-2 or RET jump to org file %s"
  18464. (abbreviate-file-name buffer-file-name))))
  18465. (regexp (concat
  18466. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  18467. (regexp-quote
  18468. (substring
  18469. (format-time-string
  18470. (car org-time-stamp-formats)
  18471. (apply 'encode-time ; DATE bound by calendar
  18472. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  18473. 1 11))))
  18474. marker hdmarker priority category tags closedp
  18475. ee txt timestr)
  18476. (goto-char (point-min))
  18477. (while (re-search-forward regexp nil t)
  18478. (catch :skip
  18479. (org-agenda-skip)
  18480. (setq marker (org-agenda-new-marker (match-beginning 0))
  18481. closedp (equal (match-string 1) org-closed-string)
  18482. category (org-get-category (match-beginning 0))
  18483. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  18484. ;; donep (org-entry-is-done-p)
  18485. )
  18486. (if (string-match "\\]" timestr)
  18487. ;; substring should only run to end of time stamp
  18488. (setq timestr (substring timestr 0 (match-end 0))))
  18489. (save-excursion
  18490. (if (re-search-backward "^\\*+ " nil t)
  18491. (progn
  18492. (goto-char (match-beginning 0))
  18493. (setq hdmarker (org-agenda-new-marker)
  18494. tags (org-get-tags-at))
  18495. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  18496. (setq txt (org-format-agenda-item
  18497. (if closedp "Closed: " "Clocked: ")
  18498. (match-string 1) category tags timestr)))
  18499. (setq txt org-agenda-no-heading-message))
  18500. (setq priority 100000)
  18501. (org-add-props txt props
  18502. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  18503. 'priority priority 'org-category category
  18504. 'type "closed" 'date date
  18505. 'undone-face 'org-warning 'done-face 'org-done)
  18506. (push txt ee))
  18507. (outline-next-heading)))
  18508. (nreverse ee)))
  18509. (defun org-agenda-get-deadlines ()
  18510. "Return the deadline information for agenda display."
  18511. (let* ((props (list 'mouse-face 'highlight
  18512. 'org-not-done-regexp org-not-done-regexp
  18513. 'org-todo-regexp org-todo-regexp
  18514. 'keymap org-agenda-keymap
  18515. 'help-echo
  18516. (format "mouse-2 or RET jump to org file %s"
  18517. (abbreviate-file-name buffer-file-name))))
  18518. (regexp org-deadline-time-regexp)
  18519. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  18520. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  18521. d2 diff dfrac wdays pos pos1 category tags
  18522. ee txt head face s upcomingp donep timestr)
  18523. (goto-char (point-min))
  18524. (while (re-search-forward regexp nil t)
  18525. (catch :skip
  18526. (org-agenda-skip)
  18527. (setq s (match-string 1)
  18528. pos (1- (match-beginning 1))
  18529. d2 (org-time-string-to-absolute (match-string 1) d1)
  18530. diff (- d2 d1)
  18531. wdays (org-get-wdays s)
  18532. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  18533. upcomingp (and todayp (> diff 0)))
  18534. ;; When to show a deadline in the calendar:
  18535. ;; If the expiration is within wdays warning time.
  18536. ;; Past-due deadlines are only shown on the current date
  18537. (if (or (and (<= diff wdays)
  18538. (and todayp (not org-agenda-only-exact-dates)))
  18539. (= diff 0))
  18540. (save-excursion
  18541. (setq category (org-get-category))
  18542. (if (re-search-backward "^\\*+[ \t]+" nil t)
  18543. (progn
  18544. (goto-char (match-end 0))
  18545. (setq pos1 (match-beginning 0))
  18546. (setq tags (org-get-tags-at pos1))
  18547. (setq head (buffer-substring-no-properties
  18548. (point)
  18549. (progn (skip-chars-forward "^\r\n")
  18550. (point))))
  18551. (setq donep (string-match org-looking-at-done-regexp head))
  18552. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  18553. (setq timestr
  18554. (concat (substring s (match-beginning 1)) " "))
  18555. (setq timestr 'time))
  18556. (if (and donep
  18557. (or org-agenda-skip-deadline-if-done
  18558. (not (= diff 0))))
  18559. (setq txt nil)
  18560. (setq txt (org-format-agenda-item
  18561. (if (= diff 0)
  18562. (car org-agenda-deadline-leaders)
  18563. (format (nth 1 org-agenda-deadline-leaders)
  18564. diff))
  18565. head category tags timestr))))
  18566. (setq txt org-agenda-no-heading-message))
  18567. (when txt
  18568. (setq face (org-agenda-deadline-face dfrac))
  18569. (org-add-props txt props
  18570. 'org-marker (org-agenda-new-marker pos)
  18571. 'org-hd-marker (org-agenda-new-marker pos1)
  18572. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  18573. (org-get-priority txt))
  18574. 'org-category category
  18575. 'type (if upcomingp "upcoming-deadline" "deadline")
  18576. 'date (if upcomingp date d2)
  18577. 'face (if donep 'org-done face)
  18578. 'undone-face face 'done-face 'org-done)
  18579. (push txt ee))))))
  18580. (nreverse ee)))
  18581. (defun org-agenda-deadline-face (fraction)
  18582. "Return the face to displaying a deadline item.
  18583. FRACTION is what fraction of the head-warning time has passed."
  18584. (let ((faces org-agenda-deadline-faces) f)
  18585. (catch 'exit
  18586. (while (setq f (pop faces))
  18587. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  18588. (defun org-agenda-get-scheduled ()
  18589. "Return the scheduled information for agenda display."
  18590. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  18591. 'org-todo-regexp org-todo-regexp
  18592. 'done-face 'org-done
  18593. 'mouse-face 'highlight
  18594. 'keymap org-agenda-keymap
  18595. 'help-echo
  18596. (format "mouse-2 or RET jump to org file %s"
  18597. (abbreviate-file-name buffer-file-name))))
  18598. (regexp org-scheduled-time-regexp)
  18599. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  18600. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  18601. d2 diff pos pos1 category tags
  18602. ee txt head pastschedp donep face timestr s)
  18603. (goto-char (point-min))
  18604. (while (re-search-forward regexp nil t)
  18605. (catch :skip
  18606. (org-agenda-skip)
  18607. (setq s (match-string 1)
  18608. pos (1- (match-beginning 1))
  18609. d2 (org-time-string-to-absolute (match-string 1) d1)
  18610. diff (- d2 d1))
  18611. (setq pastschedp (and todayp (< diff 0)))
  18612. ;; When to show a scheduled item in the calendar:
  18613. ;; If it is on or past the date.
  18614. (if (or (and (< diff 0)
  18615. (and todayp (not org-agenda-only-exact-dates)))
  18616. (= diff 0))
  18617. (save-excursion
  18618. (setq category (org-get-category))
  18619. (if (re-search-backward "^\\*+[ \t]+" nil t)
  18620. (progn
  18621. (goto-char (match-end 0))
  18622. (setq pos1 (match-beginning 0))
  18623. (setq tags (org-get-tags-at))
  18624. (setq head (buffer-substring-no-properties
  18625. (point)
  18626. (progn (skip-chars-forward "^\r\n") (point))))
  18627. (setq donep (string-match org-looking-at-done-regexp head))
  18628. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  18629. (setq timestr
  18630. (concat (substring s (match-beginning 1)) " "))
  18631. (setq timestr 'time))
  18632. (if (and donep
  18633. (or org-agenda-skip-scheduled-if-done
  18634. (not (= diff 0))))
  18635. (setq txt nil)
  18636. (setq txt (org-format-agenda-item
  18637. (if (= diff 0)
  18638. (car org-agenda-scheduled-leaders)
  18639. (format (nth 1 org-agenda-scheduled-leaders)
  18640. (- 1 diff)))
  18641. head category tags timestr))))
  18642. (setq txt org-agenda-no-heading-message))
  18643. (when txt
  18644. (setq face (if pastschedp
  18645. 'org-scheduled-previously
  18646. 'org-scheduled-today))
  18647. (org-add-props txt props
  18648. 'undone-face face
  18649. 'face (if donep 'org-done face)
  18650. 'org-marker (org-agenda-new-marker pos)
  18651. 'org-hd-marker (org-agenda-new-marker pos1)
  18652. 'type (if pastschedp "past-scheduled" "scheduled")
  18653. 'date (if pastschedp d2 date)
  18654. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  18655. 'org-category category)
  18656. (push txt ee))))))
  18657. (nreverse ee)))
  18658. (defun org-agenda-get-blocks ()
  18659. "Return the date-range information for agenda display."
  18660. (let* ((props (list 'face nil
  18661. 'org-not-done-regexp org-not-done-regexp
  18662. 'org-todo-regexp org-todo-regexp
  18663. 'mouse-face 'highlight
  18664. 'keymap org-agenda-keymap
  18665. 'help-echo
  18666. (format "mouse-2 or RET jump to org file %s"
  18667. (abbreviate-file-name buffer-file-name))))
  18668. (regexp org-tr-regexp)
  18669. (d0 (calendar-absolute-from-gregorian date))
  18670. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  18671. (goto-char (point-min))
  18672. (while (re-search-forward regexp nil t)
  18673. (catch :skip
  18674. (org-agenda-skip)
  18675. (setq pos (point))
  18676. (setq timestr (match-string 0)
  18677. s1 (match-string 1)
  18678. s2 (match-string 2)
  18679. d1 (time-to-days (org-time-string-to-time s1))
  18680. d2 (time-to-days (org-time-string-to-time s2)))
  18681. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  18682. ;; Only allow days between the limits, because the normal
  18683. ;; date stamps will catch the limits.
  18684. (save-excursion
  18685. (setq marker (org-agenda-new-marker (point)))
  18686. (setq category (org-get-category))
  18687. (if (re-search-backward "^\\*+ " nil t)
  18688. (progn
  18689. (goto-char (match-beginning 0))
  18690. (setq hdmarker (org-agenda-new-marker (point)))
  18691. (setq tags (org-get-tags-at))
  18692. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  18693. (setq txt (org-format-agenda-item
  18694. (format (if (= d1 d2) "" "(%d/%d): ")
  18695. (1+ (- d0 d1)) (1+ (- d2 d1)))
  18696. (match-string 1) category tags
  18697. (if (= d0 d1) timestr))))
  18698. (setq txt org-agenda-no-heading-message))
  18699. (org-add-props txt props
  18700. 'org-marker marker 'org-hd-marker hdmarker
  18701. 'type "block" 'date date
  18702. 'priority (org-get-priority txt) 'org-category category)
  18703. (push txt ee)))
  18704. (goto-char pos)))
  18705. ;; Sort the entries by expiration date.
  18706. (nreverse ee)))
  18707. ;;; Agenda presentation and sorting
  18708. (defconst org-plain-time-of-day-regexp
  18709. (concat
  18710. "\\(\\<[012]?[0-9]"
  18711. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  18712. "\\(--?"
  18713. "\\(\\<[012]?[0-9]"
  18714. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  18715. "\\)?")
  18716. "Regular expression to match a plain time or time range.
  18717. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  18718. groups carry important information:
  18719. 0 the full match
  18720. 1 the first time, range or not
  18721. 8 the second time, if it is a range.")
  18722. (defconst org-plain-time-extension-regexp
  18723. (concat
  18724. "\\(\\<[012]?[0-9]"
  18725. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  18726. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  18727. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  18728. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  18729. groups carry important information:
  18730. 0 the full match
  18731. 7 hours of duration
  18732. 9 minutes of duration")
  18733. (defconst org-stamp-time-of-day-regexp
  18734. (concat
  18735. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  18736. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  18737. "\\(--?"
  18738. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  18739. "Regular expression to match a timestamp time or time range.
  18740. After a match, the following groups carry important information:
  18741. 0 the full match
  18742. 1 date plus weekday, for backreferencing to make sure both times on same day
  18743. 2 the first time, range or not
  18744. 4 the second time, if it is a range.")
  18745. (defvar org-prefix-has-time nil
  18746. "A flag, set by `org-compile-prefix-format'.
  18747. The flag is set if the currently compiled format contains a `%t'.")
  18748. (defvar org-prefix-has-tag nil
  18749. "A flag, set by `org-compile-prefix-format'.
  18750. The flag is set if the currently compiled format contains a `%T'.")
  18751. (defun org-format-agenda-item (extra txt &optional category tags dotime
  18752. noprefix remove-re)
  18753. "Format TXT to be inserted into the agenda buffer.
  18754. In particular, it adds the prefix and corresponding text properties. EXTRA
  18755. must be a string and replaces the `%s' specifier in the prefix format.
  18756. CATEGORY (string, symbol or nil) may be used to overrule the default
  18757. category taken from local variable or file name. It will replace the `%c'
  18758. specifier in the format. DOTIME, when non-nil, indicates that a
  18759. time-of-day should be extracted from TXT for sorting of this entry, and for
  18760. the `%t' specifier in the format. When DOTIME is a string, this string is
  18761. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  18762. only the correctly processes TXT should be returned - this is used by
  18763. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  18764. Any match of REMOVE-RE will be removed from TXT."
  18765. (save-match-data
  18766. ;; Diary entries sometimes have extra whitespace at the beginning
  18767. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  18768. (let* ((category (or category
  18769. org-category
  18770. (if buffer-file-name
  18771. (file-name-sans-extension
  18772. (file-name-nondirectory buffer-file-name))
  18773. "")))
  18774. (tag (if tags (nth (1- (length tags)) tags) ""))
  18775. time ; time and tag are needed for the eval of the prefix format
  18776. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  18777. (time-of-day (and dotime (org-get-time-of-day ts)))
  18778. stamp plain s0 s1 s2 rtn srp)
  18779. (when (and dotime time-of-day org-prefix-has-time)
  18780. ;; Extract starting and ending time and move them to prefix
  18781. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  18782. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  18783. (setq s0 (match-string 0 ts)
  18784. srp (and stamp (match-end 3))
  18785. s1 (match-string (if plain 1 2) ts)
  18786. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  18787. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  18788. ;; them, we might want to remove them there to avoid duplication.
  18789. ;; The user can turn this off with a variable.
  18790. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  18791. (string-match (concat (regexp-quote s0) " *") txt)
  18792. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  18793. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  18794. (= (match-beginning 0) 0)
  18795. t))
  18796. (setq txt (replace-match "" nil nil txt))))
  18797. ;; Normalize the time(s) to 24 hour
  18798. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  18799. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  18800. (when (and s1 (not s2) org-agenda-default-appointment-duration
  18801. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  18802. (let ((m (+ (string-to-number (match-string 2 s1))
  18803. (* 60 (string-to-number (match-string 1 s1)))
  18804. org-agenda-default-appointment-duration))
  18805. h)
  18806. (setq h (/ m 60) m (- m (* h 60)))
  18807. (setq s2 (format "%02d:%02d" h m))))
  18808. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  18809. txt)
  18810. ;; Tags are in the string
  18811. (if (or (eq org-agenda-remove-tags t)
  18812. (and org-agenda-remove-tags
  18813. org-prefix-has-tag))
  18814. (setq txt (replace-match "" t t txt))
  18815. (setq txt (replace-match
  18816. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  18817. (match-string 2 txt))
  18818. t t txt))))
  18819. (when remove-re
  18820. (while (string-match remove-re txt)
  18821. (setq txt (replace-match "" t t txt))))
  18822. ;; Create the final string
  18823. (if noprefix
  18824. (setq rtn txt)
  18825. ;; Prepare the variables needed in the eval of the compiled format
  18826. (setq time (cond (s2 (concat s1 "-" s2))
  18827. (s1 (concat s1 "......"))
  18828. (t ""))
  18829. extra (or extra "")
  18830. category (if (symbolp category) (symbol-name category) category))
  18831. ;; Evaluate the compiled format
  18832. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  18833. ;; And finally add the text properties
  18834. (org-add-props rtn nil
  18835. 'org-category (downcase category) 'tags tags
  18836. 'org-highest-priority org-highest-priority
  18837. 'org-lowest-priority org-lowest-priority
  18838. 'prefix-length (- (length rtn) (length txt))
  18839. 'time-of-day time-of-day
  18840. 'txt txt
  18841. 'time time
  18842. 'extra extra
  18843. 'dotime dotime))))
  18844. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  18845. (defvar org-agenda-sorting-strategy-selected nil)
  18846. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  18847. (catch 'exit
  18848. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  18849. ((and todayp (member 'today (car org-agenda-time-grid))))
  18850. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  18851. ((member 'weekly (car org-agenda-time-grid)))
  18852. (t (throw 'exit list)))
  18853. (let* ((have (delq nil (mapcar
  18854. (lambda (x) (get-text-property 1 'time-of-day x))
  18855. list)))
  18856. (string (nth 1 org-agenda-time-grid))
  18857. (gridtimes (nth 2 org-agenda-time-grid))
  18858. (req (car org-agenda-time-grid))
  18859. (remove (member 'remove-match req))
  18860. new time)
  18861. (if (and (member 'require-timed req) (not have))
  18862. ;; don't show empty grid
  18863. (throw 'exit list))
  18864. (while (setq time (pop gridtimes))
  18865. (unless (and remove (member time have))
  18866. (setq time (int-to-string time))
  18867. (push (org-format-agenda-item
  18868. nil string "" nil
  18869. (concat (substring time 0 -2) ":" (substring time -2)))
  18870. new)
  18871. (put-text-property
  18872. 1 (length (car new)) 'face 'org-time-grid (car new))))
  18873. (if (member 'time-up org-agenda-sorting-strategy-selected)
  18874. (append new list)
  18875. (append list new)))))
  18876. (defun org-compile-prefix-format (key)
  18877. "Compile the prefix format into a Lisp form that can be evaluated.
  18878. The resulting form is returned and stored in the variable
  18879. `org-prefix-format-compiled'."
  18880. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  18881. (let ((s (cond
  18882. ((stringp org-agenda-prefix-format)
  18883. org-agenda-prefix-format)
  18884. ((assq key org-agenda-prefix-format)
  18885. (cdr (assq key org-agenda-prefix-format)))
  18886. (t " %-12:c%?-12t% s")))
  18887. (start 0)
  18888. varform vars var e c f opt)
  18889. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  18890. s start)
  18891. (setq var (cdr (assoc (match-string 4 s)
  18892. '(("c" . category) ("t" . time) ("s" . extra)
  18893. ("T" . tag))))
  18894. c (or (match-string 3 s) "")
  18895. opt (match-beginning 1)
  18896. start (1+ (match-beginning 0)))
  18897. (if (equal var 'time) (setq org-prefix-has-time t))
  18898. (if (equal var 'tag) (setq org-prefix-has-tag t))
  18899. (setq f (concat "%" (match-string 2 s) "s"))
  18900. (if opt
  18901. (setq varform
  18902. `(if (equal "" ,var)
  18903. ""
  18904. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  18905. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  18906. (setq s (replace-match "%s" t nil s))
  18907. (push varform vars))
  18908. (setq vars (nreverse vars))
  18909. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  18910. (defun org-set-sorting-strategy (key)
  18911. (if (symbolp (car org-agenda-sorting-strategy))
  18912. ;; the old format
  18913. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  18914. (setq org-agenda-sorting-strategy-selected
  18915. (or (cdr (assq key org-agenda-sorting-strategy))
  18916. (cdr (assq 'agenda org-agenda-sorting-strategy))
  18917. '(time-up category-keep priority-down)))))
  18918. (defun org-get-time-of-day (s &optional string mod24)
  18919. "Check string S for a time of day.
  18920. If found, return it as a military time number between 0 and 2400.
  18921. If not found, return nil.
  18922. The optional STRING argument forces conversion into a 5 character wide string
  18923. HH:MM."
  18924. (save-match-data
  18925. (when
  18926. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  18927. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  18928. (let* ((h (string-to-number (match-string 1 s)))
  18929. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  18930. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  18931. (am-p (equal ampm "am"))
  18932. (h1 (cond ((not ampm) h)
  18933. ((= h 12) (if am-p 0 12))
  18934. (t (+ h (if am-p 0 12)))))
  18935. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  18936. (mod h1 24) h1))
  18937. (t0 (+ (* 100 h2) m))
  18938. (t1 (concat (if (>= h1 24) "+" " ")
  18939. (if (< t0 100) "0" "")
  18940. (if (< t0 10) "0" "")
  18941. (int-to-string t0))))
  18942. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  18943. (defun org-finalize-agenda-entries (list &optional nosort)
  18944. "Sort and concatenate the agenda items."
  18945. (setq list (mapcar 'org-agenda-highlight-todo list))
  18946. (if nosort
  18947. list
  18948. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  18949. (defun org-agenda-highlight-todo (x)
  18950. (let (re pl)
  18951. (if (eq x 'line)
  18952. (save-excursion
  18953. (beginning-of-line 1)
  18954. (setq re (get-text-property (point) 'org-todo-regexp))
  18955. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  18956. (and (looking-at (concat "[ \t]*\\.*" re))
  18957. (add-text-properties (match-beginning 0) (match-end 0)
  18958. (list 'face (org-get-todo-face 0)))))
  18959. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  18960. pl (get-text-property 0 'prefix-length x))
  18961. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  18962. (add-text-properties
  18963. (or (match-end 1) (match-end 0)) (match-end 0)
  18964. (list 'face (org-get-todo-face (match-string 2 x)))
  18965. x))
  18966. x)))
  18967. (defsubst org-cmp-priority (a b)
  18968. "Compare the priorities of string A and B."
  18969. (let ((pa (or (get-text-property 1 'priority a) 0))
  18970. (pb (or (get-text-property 1 'priority b) 0)))
  18971. (cond ((> pa pb) +1)
  18972. ((< pa pb) -1)
  18973. (t nil))))
  18974. (defsubst org-cmp-category (a b)
  18975. "Compare the string values of categories of strings A and B."
  18976. (let ((ca (or (get-text-property 1 'org-category a) ""))
  18977. (cb (or (get-text-property 1 'org-category b) "")))
  18978. (cond ((string-lessp ca cb) -1)
  18979. ((string-lessp cb ca) +1)
  18980. (t nil))))
  18981. (defsubst org-cmp-tag (a b)
  18982. "Compare the string values of categories of strings A and B."
  18983. (let ((ta (car (last (get-text-property 1 'tags a))))
  18984. (tb (car (last (get-text-property 1 'tags b)))))
  18985. (cond ((not ta) +1)
  18986. ((not tb) -1)
  18987. ((string-lessp ta tb) -1)
  18988. ((string-lessp tb ta) +1)
  18989. (t nil))))
  18990. (defsubst org-cmp-time (a b)
  18991. "Compare the time-of-day values of strings A and B."
  18992. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  18993. (ta (or (get-text-property 1 'time-of-day a) def))
  18994. (tb (or (get-text-property 1 'time-of-day b) def)))
  18995. (cond ((< ta tb) -1)
  18996. ((< tb ta) +1)
  18997. (t nil))))
  18998. (defun org-entries-lessp (a b)
  18999. "Predicate for sorting agenda entries."
  19000. ;; The following variables will be used when the form is evaluated.
  19001. ;; So even though the compiler complains, keep them.
  19002. (let* ((time-up (org-cmp-time a b))
  19003. (time-down (if time-up (- time-up) nil))
  19004. (priority-up (org-cmp-priority a b))
  19005. (priority-down (if priority-up (- priority-up) nil))
  19006. (category-up (org-cmp-category a b))
  19007. (category-down (if category-up (- category-up) nil))
  19008. (category-keep (if category-up +1 nil))
  19009. (tag-up (org-cmp-tag a b))
  19010. (tag-down (if tag-up (- tag-up) nil)))
  19011. (cdr (assoc
  19012. (eval (cons 'or org-agenda-sorting-strategy-selected))
  19013. '((-1 . t) (1 . nil) (nil . nil))))))
  19014. ;;; Agenda commands
  19015. (defun org-agenda-check-type (error &rest types)
  19016. "Check if agenda buffer is of allowed type.
  19017. If ERROR is non-nil, throw an error, otherwise just return nil."
  19018. (if (memq org-agenda-type types)
  19019. t
  19020. (if error
  19021. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  19022. nil)))
  19023. (defun org-agenda-quit ()
  19024. "Exit agenda by removing the window or the buffer."
  19025. (interactive)
  19026. (let ((buf (current-buffer)))
  19027. (if (not (one-window-p)) (delete-window))
  19028. (kill-buffer buf)
  19029. (org-agenda-maybe-reset-markers 'force)
  19030. (org-columns-remove-overlays))
  19031. ;; Maybe restore the pre-agenda window configuration.
  19032. (and org-agenda-restore-windows-after-quit
  19033. (not (eq org-agenda-window-setup 'other-frame))
  19034. org-pre-agenda-window-conf
  19035. (set-window-configuration org-pre-agenda-window-conf)))
  19036. (defun org-agenda-exit ()
  19037. "Exit agenda by removing the window or the buffer.
  19038. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  19039. Org-mode buffers visited directly by the user will not be touched."
  19040. (interactive)
  19041. (org-release-buffers org-agenda-new-buffers)
  19042. (setq org-agenda-new-buffers nil)
  19043. (org-agenda-quit))
  19044. (defun org-save-all-org-buffers ()
  19045. "Save all Org-mode buffers without user confirmation."
  19046. (interactive)
  19047. (message "Saving all Org-mode buffers...")
  19048. (save-some-buffers t 'org-mode-p)
  19049. (message "Saving all Org-mode buffers... done"))
  19050. (defun org-agenda-redo ()
  19051. "Rebuild Agenda.
  19052. When this is the global TODO list, a prefix argument will be interpreted."
  19053. (interactive)
  19054. (let* ((org-agenda-keep-modes t)
  19055. (line (org-current-line))
  19056. (window-line (- line (org-current-line (window-start))))
  19057. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  19058. (message "Rebuilding agenda buffer...")
  19059. (org-let lprops '(eval org-agenda-redo-command))
  19060. (setq org-agenda-undo-list nil
  19061. org-agenda-pending-undo-list nil)
  19062. (message "Rebuilding agenda buffer...done")
  19063. (goto-line line)
  19064. (recenter window-line)))
  19065. (defun org-agenda-goto-date (date)
  19066. "Jump to DATE in agenda."
  19067. (interactive (list (org-read-date)))
  19068. (org-agenda-list nil date))
  19069. (defun org-agenda-goto-today ()
  19070. "Go to today."
  19071. (interactive)
  19072. (org-agenda-check-type t 'timeline 'agenda)
  19073. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  19074. (cond
  19075. (tdpos (goto-char tdpos))
  19076. ((eq org-agenda-type 'agenda)
  19077. (let* ((sd (time-to-days (current-time)))
  19078. (comp (org-agenda-compute-time-span sd org-agenda-span))
  19079. (org-agenda-overriding-arguments org-agenda-last-arguments))
  19080. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  19081. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  19082. (org-agenda-redo)
  19083. (org-agenda-find-same-or-today-or-agenda)))
  19084. (t (error "Cannot find today")))))
  19085. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  19086. (goto-char
  19087. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  19088. (text-property-any (point-min) (point-max) 'org-today t)
  19089. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  19090. (point-min))))
  19091. (defun org-agenda-later (arg)
  19092. "Go forward in time by thee current span.
  19093. With prefix ARG, go forward that many times the current span."
  19094. (interactive "p")
  19095. (org-agenda-check-type t 'agenda)
  19096. (let* ((span org-agenda-span)
  19097. (sd org-starting-day)
  19098. (greg (calendar-gregorian-from-absolute sd))
  19099. (cnt (get-text-property (point) 'org-day-cnt))
  19100. greg2 nd)
  19101. (cond
  19102. ((eq span 'day)
  19103. (setq sd (+ arg sd) nd 1))
  19104. ((eq span 'week)
  19105. (setq sd (+ (* 7 arg) sd) nd 7))
  19106. ((eq span 'month)
  19107. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  19108. sd (calendar-absolute-from-gregorian greg2))
  19109. (setcar greg2 (1+ (car greg2)))
  19110. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  19111. ((eq span 'year)
  19112. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  19113. sd (calendar-absolute-from-gregorian greg2))
  19114. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  19115. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  19116. (let ((org-agenda-overriding-arguments
  19117. (list (car org-agenda-last-arguments) sd nd t)))
  19118. (org-agenda-redo)
  19119. (org-agenda-find-same-or-today-or-agenda cnt))))
  19120. (defun org-agenda-earlier (arg)
  19121. "Go backward in time by the current span.
  19122. With prefix ARG, go backward that many times the current span."
  19123. (interactive "p")
  19124. (org-agenda-later (- arg)))
  19125. (defun org-agenda-day-view ()
  19126. "Switch to daily view for agenda."
  19127. (interactive)
  19128. (setq org-agenda-ndays 1)
  19129. (org-agenda-change-time-span 'day))
  19130. (defun org-agenda-week-view ()
  19131. "Switch to daily view for agenda."
  19132. (interactive)
  19133. (setq org-agenda-ndays 7)
  19134. (org-agenda-change-time-span 'week))
  19135. (defun org-agenda-month-view ()
  19136. "Switch to daily view for agenda."
  19137. (interactive)
  19138. (org-agenda-change-time-span 'month))
  19139. (defun org-agenda-year-view ()
  19140. "Switch to daily view for agenda."
  19141. (interactive)
  19142. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  19143. (org-agenda-change-time-span 'year)
  19144. (error "Abort")))
  19145. (defun org-agenda-change-time-span (span)
  19146. "Change the agenda view to SPAN.
  19147. SPAN may be `day', `week', `month', `year'."
  19148. (org-agenda-check-type t 'agenda)
  19149. (if (equal org-agenda-span span)
  19150. (error "Viewing span is already \"%s\"" span))
  19151. (let* ((sd (or (get-text-property (point) 'day)
  19152. org-starting-day))
  19153. (computed (org-agenda-compute-time-span sd span))
  19154. (org-agenda-overriding-arguments
  19155. (list (car org-agenda-last-arguments)
  19156. (car computed) (cdr computed) t)))
  19157. (org-agenda-redo)
  19158. (org-agenda-find-same-or-today-or-agenda))
  19159. (org-agenda-set-mode-name)
  19160. (message "Switched to %s view" span))
  19161. (defun org-agenda-compute-time-span (sd span)
  19162. "Compute starting date and number of days for agenda.
  19163. SPAN may be `day', `week', `month', `year'. The return value
  19164. is a cons cell with the starting date and the number of days,
  19165. so that the date SD will be in that range."
  19166. (let* ((greg (calendar-gregorian-from-absolute sd))
  19167. nd)
  19168. (cond
  19169. ((eq span 'day)
  19170. (setq nd 1))
  19171. ((eq span 'week)
  19172. (let* ((nt (calendar-day-of-week
  19173. (calendar-gregorian-from-absolute sd)))
  19174. (d (if org-agenda-start-on-weekday
  19175. (- nt org-agenda-start-on-weekday)
  19176. 0)))
  19177. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  19178. (setq nd 7)))
  19179. ((eq span 'month)
  19180. (setq sd (calendar-absolute-from-gregorian
  19181. (list (car greg) 1 (nth 2 greg)))
  19182. nd (- (calendar-absolute-from-gregorian
  19183. (list (1+ (car greg)) 1 (nth 2 greg)))
  19184. sd)))
  19185. ((eq span 'year)
  19186. (setq sd (calendar-absolute-from-gregorian
  19187. (list 1 1 (nth 2 greg)))
  19188. nd (- (calendar-absolute-from-gregorian
  19189. (list 1 1 (1+ (nth 2 greg))))
  19190. sd))))
  19191. (cons sd nd)))
  19192. ;; FIXME: does not work if user makes date format that starts with a blank
  19193. (defun org-agenda-next-date-line (&optional arg)
  19194. "Jump to the next line indicating a date in agenda buffer."
  19195. (interactive "p")
  19196. (org-agenda-check-type t 'agenda 'timeline)
  19197. (beginning-of-line 1)
  19198. (if (looking-at "^\\S-") (forward-char 1))
  19199. (if (not (re-search-forward "^\\S-" nil t arg))
  19200. (progn
  19201. (backward-char 1)
  19202. (error "No next date after this line in this buffer")))
  19203. (goto-char (match-beginning 0)))
  19204. (defun org-agenda-previous-date-line (&optional arg)
  19205. "Jump to the previous line indicating a date in agenda buffer."
  19206. (interactive "p")
  19207. (org-agenda-check-type t 'agenda 'timeline)
  19208. (beginning-of-line 1)
  19209. (if (not (re-search-backward "^\\S-" nil t arg))
  19210. (error "No previous date before this line in this buffer")))
  19211. ;; Initialize the highlight
  19212. (defvar org-hl (org-make-overlay 1 1))
  19213. (org-overlay-put org-hl 'face 'highlight)
  19214. (defun org-highlight (begin end &optional buffer)
  19215. "Highlight a region with overlay."
  19216. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  19217. org-hl begin end (or buffer (current-buffer))))
  19218. (defun org-unhighlight ()
  19219. "Detach overlay INDEX."
  19220. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  19221. ;; FIXME this is currently not used.
  19222. (defun org-highlight-until-next-command (beg end &optional buffer)
  19223. (org-highlight beg end buffer)
  19224. (add-hook 'pre-command-hook 'org-unhighlight-once))
  19225. (defun org-unhighlight-once ()
  19226. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  19227. (org-unhighlight))
  19228. (defun org-agenda-follow-mode ()
  19229. "Toggle follow mode in an agenda buffer."
  19230. (interactive)
  19231. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  19232. (org-agenda-set-mode-name)
  19233. (message "Follow mode is %s"
  19234. (if org-agenda-follow-mode "on" "off")))
  19235. (defun org-agenda-log-mode ()
  19236. "Toggle log mode in an agenda buffer."
  19237. (interactive)
  19238. (org-agenda-check-type t 'agenda 'timeline)
  19239. (setq org-agenda-show-log (not org-agenda-show-log))
  19240. (org-agenda-set-mode-name)
  19241. (org-agenda-redo)
  19242. (message "Log mode is %s"
  19243. (if org-agenda-show-log "on" "off")))
  19244. (defun org-agenda-toggle-diary ()
  19245. "Toggle diary inclusion in an agenda buffer."
  19246. (interactive)
  19247. (org-agenda-check-type t 'agenda)
  19248. (setq org-agenda-include-diary (not org-agenda-include-diary))
  19249. (org-agenda-redo)
  19250. (org-agenda-set-mode-name)
  19251. (message "Diary inclusion turned %s"
  19252. (if org-agenda-include-diary "on" "off")))
  19253. (defun org-agenda-toggle-time-grid ()
  19254. "Toggle time grid in an agenda buffer."
  19255. (interactive)
  19256. (org-agenda-check-type t 'agenda)
  19257. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  19258. (org-agenda-redo)
  19259. (org-agenda-set-mode-name)
  19260. (message "Time-grid turned %s"
  19261. (if org-agenda-use-time-grid "on" "off")))
  19262. (defun org-agenda-set-mode-name ()
  19263. "Set the mode name to indicate all the small mode settings."
  19264. (setq mode-name
  19265. (concat "Org-Agenda"
  19266. (if (equal org-agenda-ndays 1) " Day" "")
  19267. (if (equal org-agenda-ndays 7) " Week" "")
  19268. (if org-agenda-follow-mode " Follow" "")
  19269. (if org-agenda-include-diary " Diary" "")
  19270. (if org-agenda-use-time-grid " Grid" "")
  19271. (if org-agenda-show-log " Log" "")))
  19272. (force-mode-line-update))
  19273. (defun org-agenda-post-command-hook ()
  19274. (and (eolp) (not (bolp)) (backward-char 1))
  19275. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  19276. (if (and org-agenda-follow-mode
  19277. (get-text-property (point) 'org-marker))
  19278. (org-agenda-show)))
  19279. (defun org-agenda-show-priority ()
  19280. "Show the priority of the current item.
  19281. This priority is composed of the main priority given with the [#A] cookies,
  19282. and by additional input from the age of a schedules or deadline entry."
  19283. (interactive)
  19284. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  19285. (message "Priority is %d" (if pri pri -1000))))
  19286. (defun org-agenda-show-tags ()
  19287. "Show the tags applicable to the current item."
  19288. (interactive)
  19289. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  19290. (if tags
  19291. (message "Tags are :%s:"
  19292. (org-no-properties (mapconcat 'identity tags ":")))
  19293. (message "No tags associated with this line"))))
  19294. (defun org-agenda-goto (&optional highlight)
  19295. "Go to the Org-mode file which contains the item at point."
  19296. (interactive)
  19297. (let* ((marker (or (get-text-property (point) 'org-marker)
  19298. (org-agenda-error)))
  19299. (buffer (marker-buffer marker))
  19300. (pos (marker-position marker)))
  19301. (switch-to-buffer-other-window buffer)
  19302. (widen)
  19303. (goto-char pos)
  19304. (when (org-mode-p)
  19305. (org-show-context 'agenda)
  19306. (save-excursion
  19307. (and (outline-next-heading)
  19308. (org-flag-heading nil)))) ; show the next heading
  19309. (run-hooks 'org-agenda-after-show-hook)
  19310. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  19311. (defvar org-agenda-after-show-hook nil
  19312. "Normal hook run after an item has been shown from the agenda.
  19313. Point is in the buffer where the item originated.")
  19314. (defun org-agenda-kill ()
  19315. "Kill the entry or subtree belonging to the current agenda entry."
  19316. (interactive)
  19317. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  19318. (let* ((marker (or (get-text-property (point) 'org-marker)
  19319. (org-agenda-error)))
  19320. (buffer (marker-buffer marker))
  19321. (pos (marker-position marker))
  19322. (type (get-text-property (point) 'type))
  19323. dbeg dend (n 0) conf)
  19324. (org-with-remote-undo buffer
  19325. (with-current-buffer buffer
  19326. (save-excursion
  19327. (goto-char pos)
  19328. (if (and (org-mode-p) (not (member type '("sexp"))))
  19329. (setq dbeg (progn (org-back-to-heading t) (point))
  19330. dend (org-end-of-subtree t t))
  19331. (setq dbeg (point-at-bol)
  19332. dend (min (point-max) (1+ (point-at-eol)))))
  19333. (goto-char dbeg)
  19334. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  19335. (setq conf (or (eq t org-agenda-confirm-kill)
  19336. (and (numberp org-agenda-confirm-kill)
  19337. (> n org-agenda-confirm-kill))))
  19338. (and conf
  19339. (not (y-or-n-p
  19340. (format "Delete entry with %d lines in buffer \"%s\"? "
  19341. n (buffer-name buffer))))
  19342. (error "Abort"))
  19343. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  19344. (with-current-buffer buffer (delete-region dbeg dend))
  19345. (message "Agenda item and source killed"))))
  19346. (defun org-agenda-archive ()
  19347. "Kill the entry or subtree belonging to the current agenda entry."
  19348. (interactive)
  19349. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  19350. (let* ((marker (or (get-text-property (point) 'org-marker)
  19351. (org-agenda-error)))
  19352. (buffer (marker-buffer marker))
  19353. (pos (marker-position marker)))
  19354. (org-with-remote-undo buffer
  19355. (with-current-buffer buffer
  19356. (if (org-mode-p)
  19357. (save-excursion
  19358. (goto-char pos)
  19359. (org-remove-subtree-entries-from-agenda)
  19360. (org-back-to-heading t)
  19361. (org-archive-subtree))
  19362. (error "Archiving works only in Org-mode files"))))))
  19363. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  19364. "Remove all lines in the agenda that correspond to a given subtree.
  19365. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  19366. If this information is not given, the function uses the tree at point."
  19367. (let ((buf (or buf (current-buffer))) m p)
  19368. (save-excursion
  19369. (unless (and beg end)
  19370. (org-back-to-heading t)
  19371. (setq beg (point))
  19372. (org-end-of-subtree t)
  19373. (setq end (point)))
  19374. (set-buffer (get-buffer org-agenda-buffer-name))
  19375. (save-excursion
  19376. (goto-char (point-max))
  19377. (beginning-of-line 1)
  19378. (while (not (bobp))
  19379. (when (and (setq m (get-text-property (point) 'org-marker))
  19380. (equal buf (marker-buffer m))
  19381. (setq p (marker-position m))
  19382. (>= p beg)
  19383. (<= p end))
  19384. (let ((inhibit-read-only t))
  19385. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  19386. (beginning-of-line 0))))))
  19387. (defun org-agenda-open-link ()
  19388. "Follow the link in the current line, if any."
  19389. (interactive)
  19390. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  19391. (save-excursion
  19392. (save-restriction
  19393. (narrow-to-region (point-at-bol) (point-at-eol))
  19394. (org-open-at-point))))
  19395. (defun org-agenda-copy-local-variable (var)
  19396. "Get a variable from a referenced buffer and install it here."
  19397. (let ((m (get-text-property (point) 'org-marker)))
  19398. (when (and m (buffer-live-p (marker-buffer m)))
  19399. (org-set-local var (with-current-buffer (marker-buffer m)
  19400. (symbol-value var))))))
  19401. (defun org-agenda-switch-to (&optional delete-other-windows)
  19402. "Go to the Org-mode file which contains the item at point."
  19403. (interactive)
  19404. (let* ((marker (or (get-text-property (point) 'org-marker)
  19405. (org-agenda-error)))
  19406. (buffer (marker-buffer marker))
  19407. (pos (marker-position marker)))
  19408. (switch-to-buffer buffer)
  19409. (and delete-other-windows (delete-other-windows))
  19410. (widen)
  19411. (goto-char pos)
  19412. (when (org-mode-p)
  19413. (org-show-context 'agenda)
  19414. (save-excursion
  19415. (and (outline-next-heading)
  19416. (org-flag-heading nil)))))) ; show the next heading
  19417. (defun org-agenda-goto-mouse (ev)
  19418. "Go to the Org-mode file which contains the item at the mouse click."
  19419. (interactive "e")
  19420. (mouse-set-point ev)
  19421. (org-agenda-goto))
  19422. (defun org-agenda-show ()
  19423. "Display the Org-mode file which contains the item at point."
  19424. (interactive)
  19425. (let ((win (selected-window)))
  19426. (org-agenda-goto t)
  19427. (select-window win)))
  19428. (defun org-agenda-recenter (arg)
  19429. "Display the Org-mode file which contains the item at point and recenter."
  19430. (interactive "P")
  19431. (let ((win (selected-window)))
  19432. (org-agenda-goto t)
  19433. (recenter arg)
  19434. (select-window win)))
  19435. (defun org-agenda-show-mouse (ev)
  19436. "Display the Org-mode file which contains the item at the mouse click."
  19437. (interactive "e")
  19438. (mouse-set-point ev)
  19439. (org-agenda-show))
  19440. (defun org-agenda-check-no-diary ()
  19441. "Check if the entry is a diary link and abort if yes."
  19442. (if (get-text-property (point) 'org-agenda-diary-link)
  19443. (org-agenda-error)))
  19444. (defun org-agenda-error ()
  19445. (error "Command not allowed in this line"))
  19446. (defun org-agenda-tree-to-indirect-buffer ()
  19447. "Show the subtree corresponding to the current entry in an indirect buffer.
  19448. This calls the command `org-tree-to-indirect-buffer' from the original
  19449. Org-mode buffer.
  19450. With numerical prefix arg ARG, go up to this level and then take that tree.
  19451. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  19452. dedicated frame)."
  19453. (interactive)
  19454. (org-agenda-check-no-diary)
  19455. (let* ((marker (or (get-text-property (point) 'org-marker)
  19456. (org-agenda-error)))
  19457. (buffer (marker-buffer marker))
  19458. (pos (marker-position marker)))
  19459. (with-current-buffer buffer
  19460. (save-excursion
  19461. (goto-char pos)
  19462. (call-interactively 'org-tree-to-indirect-buffer)))))
  19463. (defvar org-last-heading-marker (make-marker)
  19464. "Marker pointing to the headline that last changed its TODO state
  19465. by a remote command from the agenda.")
  19466. (defun org-agenda-todo-nextset ()
  19467. "Switch TODO entry to next sequence."
  19468. (interactive)
  19469. (org-agenda-todo 'nextset))
  19470. (defun org-agenda-todo-previousset ()
  19471. "Switch TODO entry to previous sequence."
  19472. (interactive)
  19473. (org-agenda-todo 'previousset))
  19474. (defun org-agenda-todo (&optional arg)
  19475. "Cycle TODO state of line at point, also in Org-mode file.
  19476. This changes the line at point, all other lines in the agenda referring to
  19477. the same tree node, and the headline of the tree node in the Org-mode file."
  19478. (interactive "P")
  19479. (org-agenda-check-no-diary)
  19480. (let* ((col (current-column))
  19481. (marker (or (get-text-property (point) 'org-marker)
  19482. (org-agenda-error)))
  19483. (buffer (marker-buffer marker))
  19484. (pos (marker-position marker))
  19485. (hdmarker (get-text-property (point) 'org-hd-marker))
  19486. (inhibit-read-only t)
  19487. newhead)
  19488. (org-with-remote-undo buffer
  19489. (with-current-buffer buffer
  19490. (widen)
  19491. (goto-char pos)
  19492. (org-show-context 'agenda)
  19493. (save-excursion
  19494. (and (outline-next-heading)
  19495. (org-flag-heading nil))) ; show the next heading
  19496. (org-todo arg)
  19497. (and (bolp) (forward-char 1))
  19498. (setq newhead (org-get-heading))
  19499. (save-excursion
  19500. (org-back-to-heading)
  19501. (move-marker org-last-heading-marker (point))))
  19502. (beginning-of-line 1)
  19503. (save-excursion
  19504. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  19505. (move-to-column col))))
  19506. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  19507. "Change all lines in the agenda buffer which match HDMARKER.
  19508. The new content of the line will be NEWHEAD (as modified by
  19509. `org-format-agenda-item'). HDMARKER is checked with
  19510. `equal' against all `org-hd-marker' text properties in the file.
  19511. If FIXFACE is non-nil, the face of each item is modified acording to
  19512. the new TODO state."
  19513. (let* ((inhibit-read-only t)
  19514. props m pl undone-face done-face finish new dotime cat tags)
  19515. (save-excursion
  19516. (goto-char (point-max))
  19517. (beginning-of-line 1)
  19518. (while (not finish)
  19519. (setq finish (bobp))
  19520. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  19521. (equal m hdmarker))
  19522. (setq props (text-properties-at (point))
  19523. dotime (get-text-property (point) 'dotime)
  19524. cat (get-text-property (point) 'org-category)
  19525. tags (get-text-property (point) 'tags)
  19526. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  19527. pl (get-text-property (point) 'prefix-length)
  19528. undone-face (get-text-property (point) 'undone-face)
  19529. done-face (get-text-property (point) 'done-face))
  19530. (move-to-column pl)
  19531. (cond
  19532. ((equal new "")
  19533. (beginning-of-line 1)
  19534. (and (looking-at ".*\n?") (replace-match "")))
  19535. ((looking-at ".*")
  19536. (replace-match new t t)
  19537. (beginning-of-line 1)
  19538. (add-text-properties (point-at-bol) (point-at-eol) props)
  19539. (when fixface
  19540. (add-text-properties
  19541. (point-at-bol) (point-at-eol)
  19542. (list 'face
  19543. (if org-last-todo-state-is-todo
  19544. undone-face done-face))))
  19545. (org-agenda-highlight-todo 'line)
  19546. (beginning-of-line 1))
  19547. (t (error "Line update did not work"))))
  19548. (beginning-of-line 0)))
  19549. (org-finalize-agenda)))
  19550. (defun org-agenda-align-tags (&optional line)
  19551. "Align all tags in agenda items to `org-agenda-tags-column'."
  19552. (let ((inhibit-read-only t) l c)
  19553. (save-excursion
  19554. (goto-char (if line (point-at-bol) (point-min)))
  19555. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  19556. (if line (point-at-eol) nil) t)
  19557. (add-text-properties
  19558. (match-beginning 2) (match-end 2)
  19559. (list 'face (list 'org-tag (get-text-property
  19560. (match-beginning 2) 'face))))
  19561. (setq l (- (match-end 2) (match-beginning 2))
  19562. c (if (< org-agenda-tags-column 0)
  19563. (- (abs org-agenda-tags-column) l)
  19564. org-agenda-tags-column))
  19565. (delete-region (match-beginning 1) (match-end 1))
  19566. (goto-char (match-beginning 1))
  19567. (insert (org-add-props
  19568. (make-string (max 1 (- c (current-column))) ?\ )
  19569. (text-properties-at (point))))))))
  19570. (defun org-agenda-priority-up ()
  19571. "Increase the priority of line at point, also in Org-mode file."
  19572. (interactive)
  19573. (org-agenda-priority 'up))
  19574. (defun org-agenda-priority-down ()
  19575. "Decrease the priority of line at point, also in Org-mode file."
  19576. (interactive)
  19577. (org-agenda-priority 'down))
  19578. (defun org-agenda-priority (&optional force-direction)
  19579. "Set the priority of line at point, also in Org-mode file.
  19580. This changes the line at point, all other lines in the agenda referring to
  19581. the same tree node, and the headline of the tree node in the Org-mode file."
  19582. (interactive)
  19583. (org-agenda-check-no-diary)
  19584. (let* ((marker (or (get-text-property (point) 'org-marker)
  19585. (org-agenda-error)))
  19586. (hdmarker (get-text-property (point) 'org-hd-marker))
  19587. (buffer (marker-buffer hdmarker))
  19588. (pos (marker-position hdmarker))
  19589. (inhibit-read-only t)
  19590. newhead)
  19591. (org-with-remote-undo buffer
  19592. (with-current-buffer buffer
  19593. (widen)
  19594. (goto-char pos)
  19595. (org-show-context 'agenda)
  19596. (save-excursion
  19597. (and (outline-next-heading)
  19598. (org-flag-heading nil))) ; show the next heading
  19599. (funcall 'org-priority force-direction)
  19600. (end-of-line 1)
  19601. (setq newhead (org-get-heading)))
  19602. (org-agenda-change-all-lines newhead hdmarker)
  19603. (beginning-of-line 1))))
  19604. (defun org-get-tags-at (&optional pos)
  19605. "Get a list of all headline tags applicable at POS.
  19606. POS defaults to point. If tags are inherited, the list contains
  19607. the targets in the same sequence as the headlines appear, i.e.
  19608. the tags of the current headline come last."
  19609. (interactive)
  19610. (let (tags lastpos)
  19611. (save-excursion
  19612. (save-restriction
  19613. (widen)
  19614. (goto-char (or pos (point)))
  19615. (save-match-data
  19616. (org-back-to-heading t)
  19617. (condition-case nil
  19618. (while (not (equal lastpos (point)))
  19619. (setq lastpos (point))
  19620. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  19621. (setq tags (append (org-split-string
  19622. (org-match-string-no-properties 1) ":")
  19623. tags)))
  19624. (or org-use-tag-inheritance (error ""))
  19625. (org-up-heading-all 1))
  19626. (error nil))))
  19627. tags)))
  19628. ;; FIXME: should fix the tags property of the agenda line.
  19629. (defun org-agenda-set-tags ()
  19630. "Set tags for the current headline."
  19631. (interactive)
  19632. (org-agenda-check-no-diary)
  19633. (if (and (org-region-active-p) (interactive-p))
  19634. (call-interactively 'org-change-tag-in-region)
  19635. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  19636. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  19637. (org-agenda-error)))
  19638. (buffer (marker-buffer hdmarker))
  19639. (pos (marker-position hdmarker))
  19640. (inhibit-read-only t)
  19641. newhead)
  19642. (org-with-remote-undo buffer
  19643. (with-current-buffer buffer
  19644. (widen)
  19645. (goto-char pos)
  19646. (save-excursion
  19647. (org-show-context 'agenda))
  19648. (save-excursion
  19649. (and (outline-next-heading)
  19650. (org-flag-heading nil))) ; show the next heading
  19651. (goto-char pos)
  19652. (call-interactively 'org-set-tags)
  19653. (end-of-line 1)
  19654. (setq newhead (org-get-heading)))
  19655. (org-agenda-change-all-lines newhead hdmarker)
  19656. (beginning-of-line 1)))))
  19657. (defun org-agenda-toggle-archive-tag ()
  19658. "Toggle the archive tag for the current entry."
  19659. (interactive)
  19660. (org-agenda-check-no-diary)
  19661. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  19662. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  19663. (org-agenda-error)))
  19664. (buffer (marker-buffer hdmarker))
  19665. (pos (marker-position hdmarker))
  19666. (inhibit-read-only t)
  19667. newhead)
  19668. (org-with-remote-undo buffer
  19669. (with-current-buffer buffer
  19670. (widen)
  19671. (goto-char pos)
  19672. (org-show-context 'agenda)
  19673. (save-excursion
  19674. (and (outline-next-heading)
  19675. (org-flag-heading nil))) ; show the next heading
  19676. (call-interactively 'org-toggle-archive-tag)
  19677. (end-of-line 1)
  19678. (setq newhead (org-get-heading)))
  19679. (org-agenda-change-all-lines newhead hdmarker)
  19680. (beginning-of-line 1))))
  19681. (defun org-agenda-date-later (arg &optional what)
  19682. "Change the date of this item to one day later."
  19683. (interactive "p")
  19684. (org-agenda-check-type t 'agenda 'timeline)
  19685. (org-agenda-check-no-diary)
  19686. (let* ((marker (or (get-text-property (point) 'org-marker)
  19687. (org-agenda-error)))
  19688. (buffer (marker-buffer marker))
  19689. (pos (marker-position marker)))
  19690. (org-with-remote-undo buffer
  19691. (with-current-buffer buffer
  19692. (widen)
  19693. (goto-char pos)
  19694. (if (not (org-at-timestamp-p))
  19695. (error "Cannot find time stamp"))
  19696. (org-timestamp-change arg (or what 'day)))
  19697. (org-agenda-show-new-time marker org-last-changed-timestamp))
  19698. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  19699. (defun org-agenda-date-earlier (arg &optional what)
  19700. "Change the date of this item to one day earlier."
  19701. (interactive "p")
  19702. (org-agenda-date-later (- arg) what))
  19703. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  19704. "Show new date stamp via text properties."
  19705. ;; We use text properties to make this undoable
  19706. (let ((inhibit-read-only t))
  19707. (setq stamp (concat " " prefix " => " stamp))
  19708. (save-excursion
  19709. (goto-char (point-max))
  19710. (while (not (bobp))
  19711. (when (equal marker (get-text-property (point) 'org-marker))
  19712. (move-to-column (- (window-width) (length stamp)) t)
  19713. (if (featurep 'xemacs)
  19714. ;; Use `duplicable' property to trigger undo recording
  19715. (let ((ex (make-extent nil nil))
  19716. (gl (make-glyph stamp)))
  19717. (set-glyph-face gl 'secondary-selection)
  19718. (set-extent-properties
  19719. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  19720. (insert-extent ex (1- (point)) (point-at-eol)))
  19721. (add-text-properties
  19722. (1- (point)) (point-at-eol)
  19723. (list 'display (org-add-props stamp nil
  19724. 'face 'secondary-selection))))
  19725. (beginning-of-line 1))
  19726. (beginning-of-line 0)))))
  19727. (defun org-agenda-date-prompt (arg)
  19728. "Change the date of this item. Date is prompted for, with default today.
  19729. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  19730. be used to request time specification in the time stamp."
  19731. (interactive "P")
  19732. (org-agenda-check-type t 'agenda 'timeline)
  19733. (org-agenda-check-no-diary)
  19734. (let* ((marker (or (get-text-property (point) 'org-marker)
  19735. (org-agenda-error)))
  19736. (buffer (marker-buffer marker))
  19737. (pos (marker-position marker)))
  19738. (org-with-remote-undo buffer
  19739. (with-current-buffer buffer
  19740. (widen)
  19741. (goto-char pos)
  19742. (if (not (org-at-timestamp-p))
  19743. (error "Cannot find time stamp"))
  19744. (org-time-stamp arg)
  19745. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  19746. (defun org-agenda-schedule (arg)
  19747. "Schedule the item at point."
  19748. (interactive "P")
  19749. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  19750. (org-agenda-check-no-diary)
  19751. (let* ((marker (or (get-text-property (point) 'org-marker)
  19752. (org-agenda-error)))
  19753. (buffer (marker-buffer marker))
  19754. (pos (marker-position marker))
  19755. (org-insert-labeled-timestamps-at-point nil)
  19756. ts)
  19757. (org-with-remote-undo buffer
  19758. (with-current-buffer buffer
  19759. (widen)
  19760. (goto-char pos)
  19761. (setq ts (org-schedule arg)))
  19762. (org-agenda-show-new-time marker ts "S"))
  19763. (message "Item scheduled for %s" ts)))
  19764. (defun org-agenda-deadline (arg)
  19765. "Schedule the item at point."
  19766. (interactive "P")
  19767. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  19768. (org-agenda-check-no-diary)
  19769. (let* ((marker (or (get-text-property (point) 'org-marker)
  19770. (org-agenda-error)))
  19771. (buffer (marker-buffer marker))
  19772. (pos (marker-position marker))
  19773. (org-insert-labeled-timestamps-at-point nil)
  19774. ts)
  19775. (org-with-remote-undo buffer
  19776. (with-current-buffer buffer
  19777. (widen)
  19778. (goto-char pos)
  19779. (setq ts (org-deadline arg)))
  19780. (org-agenda-show-new-time marker ts "S"))
  19781. (message "Deadline for this item set to %s" ts)))
  19782. (defun org-get-heading (&optional no-tags)
  19783. "Return the heading of the current entry, without the stars."
  19784. (save-excursion
  19785. (org-back-to-heading t)
  19786. (if (looking-at
  19787. (if no-tags
  19788. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  19789. "\\*+[ \t]+\\([^\r\n]*\\)"))
  19790. (match-string 1) "")))
  19791. (defun org-agenda-clock-in (&optional arg)
  19792. "Start the clock on the currently selected item."
  19793. (interactive "P")
  19794. (org-agenda-check-no-diary)
  19795. (let* ((marker (or (get-text-property (point) 'org-marker)
  19796. (org-agenda-error)))
  19797. (pos (marker-position marker)))
  19798. (org-with-remote-undo (marker-buffer marker)
  19799. (with-current-buffer (marker-buffer marker)
  19800. (widen)
  19801. (goto-char pos)
  19802. (org-clock-in)))))
  19803. (defun org-agenda-clock-out (&optional arg)
  19804. "Stop the currently running clock."
  19805. (interactive "P")
  19806. (unless (marker-buffer org-clock-marker)
  19807. (error "No running clock"))
  19808. (org-with-remote-undo (marker-buffer org-clock-marker)
  19809. (org-clock-out)))
  19810. (defun org-agenda-clock-cancel (&optional arg)
  19811. "Cancel the currently running clock."
  19812. (interactive "P")
  19813. (unless (marker-buffer org-clock-marker)
  19814. (error "No running clock"))
  19815. (org-with-remote-undo (marker-buffer org-clock-marker)
  19816. (org-clock-cancel)))
  19817. (defun org-agenda-diary-entry ()
  19818. "Make a diary entry, like the `i' command from the calendar.
  19819. All the standard commands work: block, weekly etc."
  19820. (interactive)
  19821. (org-agenda-check-type t 'agenda 'timeline)
  19822. (require 'diary-lib)
  19823. (let* ((char (progn
  19824. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  19825. (read-char-exclusive)))
  19826. (cmd (cdr (assoc char
  19827. '((?d . insert-diary-entry)
  19828. (?w . insert-weekly-diary-entry)
  19829. (?m . insert-monthly-diary-entry)
  19830. (?y . insert-yearly-diary-entry)
  19831. (?a . insert-anniversary-diary-entry)
  19832. (?b . insert-block-diary-entry)
  19833. (?c . insert-cyclic-diary-entry)))))
  19834. (oldf (symbol-function 'calendar-cursor-to-date))
  19835. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  19836. (point (point))
  19837. (mark (or (mark t) (point))))
  19838. (unless cmd
  19839. (error "No command associated with <%c>" char))
  19840. (unless (and (get-text-property point 'day)
  19841. (or (not (equal ?b char))
  19842. (get-text-property mark 'day)))
  19843. (error "Don't know which date to use for diary entry"))
  19844. ;; We implement this by hacking the `calendar-cursor-to-date' function
  19845. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  19846. (let ((calendar-mark-ring
  19847. (list (calendar-gregorian-from-absolute
  19848. (or (get-text-property mark 'day)
  19849. (get-text-property point 'day))))))
  19850. (unwind-protect
  19851. (progn
  19852. (fset 'calendar-cursor-to-date
  19853. (lambda (&optional error)
  19854. (calendar-gregorian-from-absolute
  19855. (get-text-property point 'day))))
  19856. (call-interactively cmd))
  19857. (fset 'calendar-cursor-to-date oldf)))))
  19858. (defun org-agenda-execute-calendar-command (cmd)
  19859. "Execute a calendar command from the agenda, with the date associated to
  19860. the cursor position."
  19861. (org-agenda-check-type t 'agenda 'timeline)
  19862. (require 'diary-lib)
  19863. (unless (get-text-property (point) 'day)
  19864. (error "Don't know which date to use for calendar command"))
  19865. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  19866. (point (point))
  19867. (date (calendar-gregorian-from-absolute
  19868. (get-text-property point 'day)))
  19869. ;; the following 3 vars are needed in the calendar
  19870. (displayed-day (extract-calendar-day date))
  19871. (displayed-month (extract-calendar-month date))
  19872. (displayed-year (extract-calendar-year date)))
  19873. (unwind-protect
  19874. (progn
  19875. (fset 'calendar-cursor-to-date
  19876. (lambda (&optional error)
  19877. (calendar-gregorian-from-absolute
  19878. (get-text-property point 'day))))
  19879. (call-interactively cmd))
  19880. (fset 'calendar-cursor-to-date oldf))))
  19881. (defun org-agenda-phases-of-moon ()
  19882. "Display the phases of the moon for the 3 months around the cursor date."
  19883. (interactive)
  19884. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  19885. (defun org-agenda-holidays ()
  19886. "Display the holidays for the 3 months around the cursor date."
  19887. (interactive)
  19888. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  19889. (defun org-agenda-sunrise-sunset (arg)
  19890. "Display sunrise and sunset for the cursor date.
  19891. Latitude and longitude can be specified with the variables
  19892. `calendar-latitude' and `calendar-longitude'. When called with prefix
  19893. argument, latitude and longitude will be prompted for."
  19894. (interactive "P")
  19895. (let ((calendar-longitude (if arg nil calendar-longitude))
  19896. (calendar-latitude (if arg nil calendar-latitude))
  19897. (calendar-location-name
  19898. (if arg "the given coordinates" calendar-location-name)))
  19899. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  19900. (defun org-agenda-goto-calendar ()
  19901. "Open the Emacs calendar with the date at the cursor."
  19902. (interactive)
  19903. (org-agenda-check-type t 'agenda 'timeline)
  19904. (let* ((day (or (get-text-property (point) 'day)
  19905. (error "Don't know which date to open in calendar")))
  19906. (date (calendar-gregorian-from-absolute day))
  19907. (calendar-move-hook nil)
  19908. (view-calendar-holidays-initially nil)
  19909. (view-diary-entries-initially nil))
  19910. (calendar)
  19911. (calendar-goto-date date)))
  19912. (defun org-calendar-goto-agenda ()
  19913. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  19914. This is a command that has to be installed in `calendar-mode-map'."
  19915. (interactive)
  19916. (org-agenda-list nil (calendar-absolute-from-gregorian
  19917. (calendar-cursor-to-date))
  19918. nil))
  19919. (defun org-agenda-convert-date ()
  19920. (interactive)
  19921. (org-agenda-check-type t 'agenda 'timeline)
  19922. (let ((day (get-text-property (point) 'day))
  19923. date s)
  19924. (unless day
  19925. (error "Don't know which date to convert"))
  19926. (setq date (calendar-gregorian-from-absolute day))
  19927. (setq s (concat
  19928. "Gregorian: " (calendar-date-string date) "\n"
  19929. "ISO: " (calendar-iso-date-string date) "\n"
  19930. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  19931. "Julian: " (calendar-julian-date-string date) "\n"
  19932. "Astron. JD: " (calendar-astro-date-string date)
  19933. " (Julian date number at noon UTC)\n"
  19934. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  19935. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  19936. "French: " (calendar-french-date-string date) "\n"
  19937. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  19938. "Mayan: " (calendar-mayan-date-string date) "\n"
  19939. "Coptic: " (calendar-coptic-date-string date) "\n"
  19940. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  19941. "Persian: " (calendar-persian-date-string date) "\n"
  19942. "Chinese: " (calendar-chinese-date-string date) "\n"))
  19943. (with-output-to-temp-buffer "*Dates*"
  19944. (princ s))
  19945. (if (fboundp 'fit-window-to-buffer)
  19946. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  19947. ;;;; Embedded LaTeX
  19948. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  19949. "Keymap for the minor `org-cdlatex-mode'.")
  19950. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  19951. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  19952. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  19953. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  19954. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  19955. (defvar org-cdlatex-texmathp-advice-is-done nil
  19956. "Flag remembering if we have applied the advice to texmathp already.")
  19957. (define-minor-mode org-cdlatex-mode
  19958. "Toggle the minor `org-cdlatex-mode'.
  19959. This mode supports entering LaTeX environment and math in LaTeX fragments
  19960. in Org-mode.
  19961. \\{org-cdlatex-mode-map}"
  19962. nil " OCDL" nil
  19963. (when org-cdlatex-mode (require 'cdlatex))
  19964. (unless org-cdlatex-texmathp-advice-is-done
  19965. (setq org-cdlatex-texmathp-advice-is-done t)
  19966. (defadvice texmathp (around org-math-always-on activate)
  19967. "Always return t in org-mode buffers.
  19968. This is because we want to insert math symbols without dollars even outside
  19969. the LaTeX math segments. If Orgmode thinks that point is actually inside
  19970. en embedded LaTeX fragement, let texmathp do its job.
  19971. \\[org-cdlatex-mode-map]"
  19972. (interactive)
  19973. (let (p)
  19974. (cond
  19975. ((not (org-mode-p)) ad-do-it)
  19976. ((eq this-command 'cdlatex-math-symbol)
  19977. (setq ad-return-value t
  19978. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  19979. (t
  19980. (let ((p (org-inside-LaTeX-fragment-p)))
  19981. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  19982. (setq ad-return-value t
  19983. texmathp-why '("Org-mode embedded math" . 0))
  19984. (if p ad-do-it)))))))))
  19985. (defun turn-on-org-cdlatex ()
  19986. "Unconditionally turn on `org-cdlatex-mode'."
  19987. (org-cdlatex-mode 1))
  19988. (defun org-inside-LaTeX-fragment-p ()
  19989. "Test if point is inside a LaTeX fragment.
  19990. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  19991. sequence appearing also before point.
  19992. Even though the matchers for math are configurable, this function assumes
  19993. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  19994. delimiters are skipped when they have been removed by customization.
  19995. The return value is nil, or a cons cell with the delimiter and
  19996. and the position of this delimiter.
  19997. This function does a reasonably good job, but can locally be fooled by
  19998. for example currency specifications. For example it will assume being in
  19999. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  20000. fragments that are properly closed, but during editing, we have to live
  20001. with the uncertainty caused by missing closing delimiters. This function
  20002. looks only before point, not after."
  20003. (catch 'exit
  20004. (let ((pos (point))
  20005. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  20006. (lim (progn
  20007. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  20008. (point)))
  20009. dd-on str (start 0) m re)
  20010. (goto-char pos)
  20011. (when dodollar
  20012. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  20013. re (nth 1 (assoc "$" org-latex-regexps)))
  20014. (while (string-match re str start)
  20015. (cond
  20016. ((= (match-end 0) (length str))
  20017. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  20018. ((= (match-end 0) (- (length str) 5))
  20019. (throw 'exit nil))
  20020. (t (setq start (match-end 0))))))
  20021. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  20022. (goto-char pos)
  20023. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  20024. (and (match-beginning 2) (throw 'exit nil))
  20025. ;; count $$
  20026. (while (re-search-backward "\\$\\$" lim t)
  20027. (setq dd-on (not dd-on)))
  20028. (goto-char pos)
  20029. (if dd-on (cons "$$" m))))))
  20030. (defun org-try-cdlatex-tab ()
  20031. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  20032. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  20033. - inside a LaTeX fragment, or
  20034. - after the first word in a line, where an abbreviation expansion could
  20035. insert a LaTeX environment."
  20036. (when org-cdlatex-mode
  20037. (cond
  20038. ((save-excursion
  20039. (skip-chars-backward "a-zA-Z0-9*")
  20040. (skip-chars-backward " \t")
  20041. (bolp))
  20042. (cdlatex-tab) t)
  20043. ((org-inside-LaTeX-fragment-p)
  20044. (cdlatex-tab) t)
  20045. (t nil))))
  20046. (defun org-cdlatex-underscore-caret (&optional arg)
  20047. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  20048. Revert to the normal definition outside of these fragments."
  20049. (interactive "P")
  20050. (if (org-inside-LaTeX-fragment-p)
  20051. (call-interactively 'cdlatex-sub-superscript)
  20052. (let (org-cdlatex-mode)
  20053. (call-interactively (key-binding (vector last-input-event))))))
  20054. (defun org-cdlatex-math-modify (&optional arg)
  20055. "Execute `cdlatex-math-modify' in LaTeX fragments.
  20056. Revert to the normal definition outside of these fragments."
  20057. (interactive "P")
  20058. (if (org-inside-LaTeX-fragment-p)
  20059. (call-interactively 'cdlatex-math-modify)
  20060. (let (org-cdlatex-mode)
  20061. (call-interactively (key-binding (vector last-input-event))))))
  20062. (defvar org-latex-fragment-image-overlays nil
  20063. "List of overlays carrying the images of latex fragments.")
  20064. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  20065. (defun org-remove-latex-fragment-image-overlays ()
  20066. "Remove all overlays with LaTeX fragment images in current buffer."
  20067. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  20068. (setq org-latex-fragment-image-overlays nil))
  20069. (defun org-preview-latex-fragment (&optional subtree)
  20070. "Preview the LaTeX fragment at point, or all locally or globally.
  20071. If the cursor is in a LaTeX fragment, create the image and overlay
  20072. it over the source code. If there is no fragment at point, display
  20073. all fragments in the current text, from one headline to the next. With
  20074. prefix SUBTREE, display all fragments in the current subtree. With a
  20075. double prefix `C-u C-u', or when the cursor is before the first headline,
  20076. display all fragments in the buffer.
  20077. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  20078. (interactive "P")
  20079. (org-remove-latex-fragment-image-overlays)
  20080. (save-excursion
  20081. (save-restriction
  20082. (let (beg end at msg)
  20083. (cond
  20084. ((or (equal subtree '(16))
  20085. (not (save-excursion
  20086. (re-search-backward (concat "^" outline-regexp) nil t))))
  20087. (setq beg (point-min) end (point-max)
  20088. msg "Creating images for buffer...%s"))
  20089. ((equal subtree '(4))
  20090. (org-back-to-heading)
  20091. (setq beg (point) end (org-end-of-subtree t)
  20092. msg "Creating images for subtree...%s"))
  20093. (t
  20094. (if (setq at (org-inside-LaTeX-fragment-p))
  20095. (goto-char (max (point-min) (- (cdr at) 2)))
  20096. (org-back-to-heading))
  20097. (setq beg (point) end (progn (outline-next-heading) (point))
  20098. msg (if at "Creating image...%s"
  20099. "Creating images for entry...%s"))))
  20100. (message msg "")
  20101. (narrow-to-region beg end)
  20102. (goto-char beg)
  20103. (org-format-latex
  20104. (concat "ltxpng/" (file-name-sans-extension
  20105. (file-name-nondirectory
  20106. buffer-file-name)))
  20107. default-directory 'overlays msg at 'forbuffer)
  20108. (message msg "done. Use `C-c C-c' to remove images.")))))
  20109. (defvar org-latex-regexps
  20110. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  20111. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  20112. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  20113. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  20114. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  20115. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  20116. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  20117. "Regular expressions for matching embedded LaTeX.")
  20118. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  20119. "Replace LaTeX fragments with links to an image, and produce images."
  20120. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  20121. (let* ((prefixnodir (file-name-nondirectory prefix))
  20122. (absprefix (expand-file-name prefix dir))
  20123. (todir (file-name-directory absprefix))
  20124. (opt org-format-latex-options)
  20125. (matchers (plist-get opt :matchers))
  20126. (re-list org-latex-regexps)
  20127. (cnt 0) txt link beg end re e checkdir
  20128. m n block linkfile movefile ov)
  20129. ;; Check if there are old images files with this prefix, and remove them
  20130. (when (file-directory-p todir)
  20131. (mapc 'delete-file
  20132. (directory-files
  20133. todir 'full
  20134. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  20135. ;; Check the different regular expressions
  20136. (while (setq e (pop re-list))
  20137. (setq m (car e) re (nth 1 e) n (nth 2 e)
  20138. block (if (nth 3 e) "\n\n" ""))
  20139. (when (member m matchers)
  20140. (goto-char (point-min))
  20141. (while (re-search-forward re nil t)
  20142. (when (or (not at) (equal (cdr at) (match-beginning n)))
  20143. (setq txt (match-string n)
  20144. beg (match-beginning n) end (match-end n)
  20145. cnt (1+ cnt)
  20146. linkfile (format "%s_%04d.png" prefix cnt)
  20147. movefile (format "%s_%04d.png" absprefix cnt)
  20148. link (concat block "[[file:" linkfile "]]" block))
  20149. (if msg (message msg cnt))
  20150. (goto-char beg)
  20151. (unless checkdir ; make sure the directory exists
  20152. (setq checkdir t)
  20153. (or (file-directory-p todir) (make-directory todir)))
  20154. (org-create-formula-image
  20155. txt movefile opt forbuffer)
  20156. (if overlays
  20157. (progn
  20158. (setq ov (org-make-overlay beg end))
  20159. (if (featurep 'xemacs)
  20160. (progn
  20161. (org-overlay-put ov 'invisible t)
  20162. (org-overlay-put
  20163. ov 'end-glyph
  20164. (make-glyph (vector 'png :file movefile))))
  20165. (org-overlay-put
  20166. ov 'display
  20167. (list 'image :type 'png :file movefile :ascent 'center)))
  20168. (push ov org-latex-fragment-image-overlays)
  20169. (goto-char end))
  20170. (delete-region beg end)
  20171. (insert link))))))))
  20172. ;; This function borrows from Ganesh Swami's latex2png.el
  20173. (defun org-create-formula-image (string tofile options buffer)
  20174. (let* ((tmpdir (if (featurep 'xemacs)
  20175. (temp-directory)
  20176. temporary-file-directory))
  20177. (texfilebase (make-temp-name
  20178. (expand-file-name "orgtex" tmpdir)))
  20179. (texfile (concat texfilebase ".tex"))
  20180. (dvifile (concat texfilebase ".dvi"))
  20181. (pngfile (concat texfilebase ".png"))
  20182. (fnh (face-attribute 'default :height nil))
  20183. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  20184. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  20185. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  20186. "Black"))
  20187. (bg (or (plist-get options (if buffer :background :html-background))
  20188. "Transparent")))
  20189. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  20190. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  20191. (with-temp-file texfile
  20192. (insert org-format-latex-header
  20193. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  20194. (let ((dir default-directory))
  20195. (condition-case nil
  20196. (progn
  20197. (cd tmpdir)
  20198. (call-process "latex" nil nil nil texfile))
  20199. (error nil))
  20200. (cd dir))
  20201. (if (not (file-exists-p dvifile))
  20202. (progn (message "Failed to create dvi file from %s" texfile) nil)
  20203. (call-process "dvipng" nil nil nil
  20204. "-E" "-fg" fg "-bg" bg
  20205. "-D" dpi
  20206. ;;"-x" scale "-y" scale
  20207. "-T" "tight"
  20208. "-o" pngfile
  20209. dvifile)
  20210. (if (not (file-exists-p pngfile))
  20211. (progn (message "Failed to create png file from %s" texfile) nil)
  20212. ;; Use the requested file name and clean up
  20213. (copy-file pngfile tofile 'replace)
  20214. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  20215. (delete-file (concat texfilebase e)))
  20216. pngfile))))
  20217. (defun org-dvipng-color (attr)
  20218. "Return an rgb color specification for dvipng."
  20219. (apply 'format "rgb %s %s %s"
  20220. (mapcar 'org-normalize-color
  20221. (color-values (face-attribute 'default attr nil)))))
  20222. (defun org-normalize-color (value)
  20223. "Return string to be used as color value for an RGB component."
  20224. (format "%g" (/ value 65535.0)))
  20225. ;;;; Exporting
  20226. ;;; Variables, constants, and parameter plists
  20227. (defconst org-level-max 20)
  20228. (defvar org-export-html-preamble nil
  20229. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  20230. (defvar org-export-html-postamble nil
  20231. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  20232. (defvar org-export-html-auto-preamble t
  20233. "Should default preamble be inserted? Set by publishing functions.")
  20234. (defvar org-export-html-auto-postamble t
  20235. "Should default postamble be inserted? Set by publishing functions.")
  20236. (defvar org-current-export-file nil) ; dynamically scoped parameter
  20237. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  20238. (defconst org-export-plist-vars
  20239. '((:language . org-export-default-language)
  20240. (:customtime . org-display-custom-times)
  20241. (:headline-levels . org-export-headline-levels)
  20242. (:section-numbers . org-export-with-section-numbers)
  20243. (:table-of-contents . org-export-with-toc)
  20244. (:preserve-breaks . org-export-preserve-breaks)
  20245. (:archived-trees . org-export-with-archived-trees)
  20246. (:emphasize . org-export-with-emphasize)
  20247. (:sub-superscript . org-export-with-sub-superscripts)
  20248. (:footnotes . org-export-with-footnotes)
  20249. (:drawers . org-export-with-drawers)
  20250. (:tags . org-export-with-tags)
  20251. (:TeX-macros . org-export-with-TeX-macros)
  20252. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  20253. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  20254. (:fixed-width . org-export-with-fixed-width)
  20255. (:timestamps . org-export-with-timestamps)
  20256. (:author-info . org-export-author-info)
  20257. (:time-stamp-file . org-export-time-stamp-file)
  20258. (:tables . org-export-with-tables)
  20259. (:table-auto-headline . org-export-highlight-first-table-line)
  20260. (:style . org-export-html-style)
  20261. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  20262. (:convert-org-links . org-export-html-link-org-files-as-html)
  20263. (:inline-images . org-export-html-inline-images)
  20264. (:html-extension . org-export-html-extension)
  20265. (:expand-quoted-html . org-export-html-expand)
  20266. (:timestamp . org-export-html-with-timestamp)
  20267. (:publishing-directory . org-export-publishing-directory)
  20268. (:preamble . org-export-html-preamble)
  20269. (:postamble . org-export-html-postamble)
  20270. (:auto-preamble . org-export-html-auto-preamble)
  20271. (:auto-postamble . org-export-html-auto-postamble)
  20272. (:author . user-full-name)
  20273. (:email . user-mail-address)))
  20274. (defun org-default-export-plist ()
  20275. "Return the property list with default settings for the export variables."
  20276. (let ((l org-export-plist-vars) rtn e)
  20277. (while (setq e (pop l))
  20278. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  20279. rtn))
  20280. (defun org-infile-export-plist ()
  20281. "Return the property list with file-local settings for export."
  20282. (save-excursion
  20283. (goto-char 0)
  20284. (let ((re (org-make-options-regexp
  20285. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  20286. p key val text options)
  20287. (while (re-search-forward re nil t)
  20288. (setq key (org-match-string-no-properties 1)
  20289. val (org-match-string-no-properties 2))
  20290. (cond
  20291. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  20292. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  20293. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  20294. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  20295. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  20296. ((string-equal key "TEXT")
  20297. (setq text (if text (concat text "\n" val) val)))
  20298. ((string-equal key "OPTIONS") (setq options val))))
  20299. (setq p (plist-put p :text text))
  20300. (when options
  20301. (let ((op '(("H" . :headline-levels)
  20302. ("num" . :section-numbers)
  20303. ("toc" . :table-of-contents)
  20304. ("\\n" . :preserve-breaks)
  20305. ("@" . :expand-quoted-html)
  20306. (":" . :fixed-width)
  20307. ("|" . :tables)
  20308. ("^" . :sub-superscript)
  20309. ("f" . :footnotes)
  20310. ("d" . :drawers)
  20311. ("tags" . :tags)
  20312. ("*" . :emphasize)
  20313. ("TeX" . :TeX-macros)
  20314. ("LaTeX" . :LaTeX-fragments)
  20315. ("skip" . :skip-before-1st-heading)
  20316. ("author" . :author-info)
  20317. ("timestamp" . :time-stamp-file)))
  20318. o)
  20319. (while (setq o (pop op))
  20320. (if (string-match (concat (regexp-quote (car o))
  20321. ":\\([^ \t\n\r;,.]*\\)")
  20322. options)
  20323. (setq p (plist-put p (cdr o)
  20324. (car (read-from-string
  20325. (match-string 1 options)))))))))
  20326. p)))
  20327. (defun org-export-directory (type plist)
  20328. (let* ((val (plist-get plist :publishing-directory))
  20329. (dir (if (listp val)
  20330. (or (cdr (assoc type val)) ".")
  20331. val)))
  20332. dir))
  20333. (defun org-skip-comments (lines)
  20334. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  20335. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  20336. (re2 "^\\(\\*+\\)[ \t\n\r]")
  20337. (case-fold-search nil)
  20338. rtn line level)
  20339. (while (setq line (pop lines))
  20340. (cond
  20341. ((and (string-match re1 line)
  20342. (setq level (- (match-end 1) (match-beginning 1))))
  20343. ;; Beginning of a COMMENT subtree. Skip it.
  20344. (while (and (setq line (pop lines))
  20345. (or (not (string-match re2 line))
  20346. (> (- (match-end 1) (match-beginning 1)) level))))
  20347. (setq lines (cons line lines)))
  20348. ((string-match "^#" line)
  20349. ;; an ordinary comment line
  20350. )
  20351. ((and org-export-table-remove-special-lines
  20352. (string-match "^[ \t]*|" line)
  20353. (or (string-match "^[ \t]*| *[!_^] *|" line)
  20354. (and (string-match "| *<[0-9]+> *|" line)
  20355. (not (string-match "| *[^ <|]" line)))))
  20356. ;; a special table line that should be removed
  20357. )
  20358. (t (setq rtn (cons line rtn)))))
  20359. (nreverse rtn)))
  20360. (defun org-export (&optional arg)
  20361. (interactive)
  20362. (let ((help "[t] insert the export option template
  20363. \[v] limit export to visible part of outline tree
  20364. \[a] export as ASCII
  20365. \[h] export as HTML
  20366. \[H] export as HTML to temporary buffer
  20367. \[R] export region as HTML
  20368. \[b] export as HTML and browse immediately
  20369. \[x] export as XOXO
  20370. \[l] export as LaTeX
  20371. \[L] export as LaTeX to temporary buffer
  20372. \[i] export current file as iCalendar file
  20373. \[I] export all agenda files as iCalendar files
  20374. \[c] export agenda files into combined iCalendar file
  20375. \[F] publish current file
  20376. \[P] publish current project
  20377. \[X] publish... (project will be prompted for)
  20378. \[A] publish all projects")
  20379. (cmds
  20380. '((?t . org-insert-export-options-template)
  20381. (?v . org-export-visible)
  20382. (?a . org-export-as-ascii)
  20383. (?h . org-export-as-html)
  20384. (?b . org-export-as-html-and-open)
  20385. (?H . org-export-as-html-to-buffer)
  20386. (?R . org-export-region-as-html)
  20387. (?x . org-export-as-xoxo)
  20388. (?l . org-export-as-latex)
  20389. (?L . org-export-as-latex-to-buffer)
  20390. (?i . org-export-icalendar-this-file)
  20391. (?I . org-export-icalendar-all-agenda-files)
  20392. (?c . org-export-icalendar-combine-agenda-files)
  20393. (?F . org-publish-current-file)
  20394. (?P . org-publish-current-project)
  20395. (?X . org-publish)
  20396. (?A . org-publish-all)))
  20397. r1 r2 ass)
  20398. (save-window-excursion
  20399. (delete-other-windows)
  20400. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  20401. (princ help))
  20402. (message "Select command: ")
  20403. (setq r1 (read-char-exclusive)))
  20404. (setq r2 (if (< r1 27) (+ r1 96) r1))
  20405. (if (setq ass (assq r2 cmds))
  20406. (call-interactively (cdr ass))
  20407. (error "No command associated with key %c" r1))))
  20408. (defconst org-html-entities
  20409. '(("nbsp")
  20410. ("iexcl")
  20411. ("cent")
  20412. ("pound")
  20413. ("curren")
  20414. ("yen")
  20415. ("brvbar")
  20416. ("vert" . "&#124;")
  20417. ("sect")
  20418. ("uml")
  20419. ("copy")
  20420. ("ordf")
  20421. ("laquo")
  20422. ("not")
  20423. ("shy")
  20424. ("reg")
  20425. ("macr")
  20426. ("deg")
  20427. ("plusmn")
  20428. ("sup2")
  20429. ("sup3")
  20430. ("acute")
  20431. ("micro")
  20432. ("para")
  20433. ("middot")
  20434. ("odot"."o")
  20435. ("star"."*")
  20436. ("cedil")
  20437. ("sup1")
  20438. ("ordm")
  20439. ("raquo")
  20440. ("frac14")
  20441. ("frac12")
  20442. ("frac34")
  20443. ("iquest")
  20444. ("Agrave")
  20445. ("Aacute")
  20446. ("Acirc")
  20447. ("Atilde")
  20448. ("Auml")
  20449. ("Aring") ("AA"."&Aring;")
  20450. ("AElig")
  20451. ("Ccedil")
  20452. ("Egrave")
  20453. ("Eacute")
  20454. ("Ecirc")
  20455. ("Euml")
  20456. ("Igrave")
  20457. ("Iacute")
  20458. ("Icirc")
  20459. ("Iuml")
  20460. ("ETH")
  20461. ("Ntilde")
  20462. ("Ograve")
  20463. ("Oacute")
  20464. ("Ocirc")
  20465. ("Otilde")
  20466. ("Ouml")
  20467. ("times")
  20468. ("Oslash")
  20469. ("Ugrave")
  20470. ("Uacute")
  20471. ("Ucirc")
  20472. ("Uuml")
  20473. ("Yacute")
  20474. ("THORN")
  20475. ("szlig")
  20476. ("agrave")
  20477. ("aacute")
  20478. ("acirc")
  20479. ("atilde")
  20480. ("auml")
  20481. ("aring")
  20482. ("aelig")
  20483. ("ccedil")
  20484. ("egrave")
  20485. ("eacute")
  20486. ("ecirc")
  20487. ("euml")
  20488. ("igrave")
  20489. ("iacute")
  20490. ("icirc")
  20491. ("iuml")
  20492. ("eth")
  20493. ("ntilde")
  20494. ("ograve")
  20495. ("oacute")
  20496. ("ocirc")
  20497. ("otilde")
  20498. ("ouml")
  20499. ("divide")
  20500. ("oslash")
  20501. ("ugrave")
  20502. ("uacute")
  20503. ("ucirc")
  20504. ("uuml")
  20505. ("yacute")
  20506. ("thorn")
  20507. ("yuml")
  20508. ("fnof")
  20509. ("Alpha")
  20510. ("Beta")
  20511. ("Gamma")
  20512. ("Delta")
  20513. ("Epsilon")
  20514. ("Zeta")
  20515. ("Eta")
  20516. ("Theta")
  20517. ("Iota")
  20518. ("Kappa")
  20519. ("Lambda")
  20520. ("Mu")
  20521. ("Nu")
  20522. ("Xi")
  20523. ("Omicron")
  20524. ("Pi")
  20525. ("Rho")
  20526. ("Sigma")
  20527. ("Tau")
  20528. ("Upsilon")
  20529. ("Phi")
  20530. ("Chi")
  20531. ("Psi")
  20532. ("Omega")
  20533. ("alpha")
  20534. ("beta")
  20535. ("gamma")
  20536. ("delta")
  20537. ("epsilon")
  20538. ("varepsilon"."&epsilon;")
  20539. ("zeta")
  20540. ("eta")
  20541. ("theta")
  20542. ("iota")
  20543. ("kappa")
  20544. ("lambda")
  20545. ("mu")
  20546. ("nu")
  20547. ("xi")
  20548. ("omicron")
  20549. ("pi")
  20550. ("rho")
  20551. ("sigmaf") ("varsigma"."&sigmaf;")
  20552. ("sigma")
  20553. ("tau")
  20554. ("upsilon")
  20555. ("phi")
  20556. ("chi")
  20557. ("psi")
  20558. ("omega")
  20559. ("thetasym") ("vartheta"."&thetasym;")
  20560. ("upsih")
  20561. ("piv")
  20562. ("bull") ("bullet"."&bull;")
  20563. ("hellip") ("dots"."&hellip;")
  20564. ("prime")
  20565. ("Prime")
  20566. ("oline")
  20567. ("frasl")
  20568. ("weierp")
  20569. ("image")
  20570. ("real")
  20571. ("trade")
  20572. ("alefsym")
  20573. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  20574. ("uarr") ("uparrow"."&uarr;")
  20575. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  20576. ("darr")("downarrow"."&darr;")
  20577. ("harr") ("leftrightarrow"."&harr;")
  20578. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  20579. ("lArr") ("Leftarrow"."&lArr;")
  20580. ("uArr") ("Uparrow"."&uArr;")
  20581. ("rArr") ("Rightarrow"."&rArr;")
  20582. ("dArr") ("Downarrow"."&dArr;")
  20583. ("hArr") ("Leftrightarrow"."&hArr;")
  20584. ("forall")
  20585. ("part") ("partial"."&part;")
  20586. ("exist") ("exists"."&exist;")
  20587. ("empty") ("emptyset"."&empty;")
  20588. ("nabla")
  20589. ("isin") ("in"."&isin;")
  20590. ("notin")
  20591. ("ni")
  20592. ("prod")
  20593. ("sum")
  20594. ("minus")
  20595. ("lowast") ("ast"."&lowast;")
  20596. ("radic")
  20597. ("prop") ("proptp"."&prop;")
  20598. ("infin") ("infty"."&infin;")
  20599. ("ang") ("angle"."&ang;")
  20600. ("and") ("vee"."&and;")
  20601. ("or") ("wedge"."&or;")
  20602. ("cap")
  20603. ("cup")
  20604. ("int")
  20605. ("there4")
  20606. ("sim")
  20607. ("cong") ("simeq"."&cong;")
  20608. ("asymp")("approx"."&asymp;")
  20609. ("ne") ("neq"."&ne;")
  20610. ("equiv")
  20611. ("le")
  20612. ("ge")
  20613. ("sub") ("subset"."&sub;")
  20614. ("sup") ("supset"."&sup;")
  20615. ("nsub")
  20616. ("sube")
  20617. ("supe")
  20618. ("oplus")
  20619. ("otimes")
  20620. ("perp")
  20621. ("sdot") ("cdot"."&sdot;")
  20622. ("lceil")
  20623. ("rceil")
  20624. ("lfloor")
  20625. ("rfloor")
  20626. ("lang")
  20627. ("rang")
  20628. ("loz") ("Diamond"."&loz;")
  20629. ("spades") ("spadesuit"."&spades;")
  20630. ("clubs") ("clubsuit"."&clubs;")
  20631. ("hearts") ("diamondsuit"."&hearts;")
  20632. ("diams") ("diamondsuit"."&diams;")
  20633. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  20634. ("quot")
  20635. ("amp")
  20636. ("lt")
  20637. ("gt")
  20638. ("OElig")
  20639. ("oelig")
  20640. ("Scaron")
  20641. ("scaron")
  20642. ("Yuml")
  20643. ("circ")
  20644. ("tilde")
  20645. ("ensp")
  20646. ("emsp")
  20647. ("thinsp")
  20648. ("zwnj")
  20649. ("zwj")
  20650. ("lrm")
  20651. ("rlm")
  20652. ("ndash")
  20653. ("mdash")
  20654. ("lsquo")
  20655. ("rsquo")
  20656. ("sbquo")
  20657. ("ldquo")
  20658. ("rdquo")
  20659. ("bdquo")
  20660. ("dagger")
  20661. ("Dagger")
  20662. ("permil")
  20663. ("lsaquo")
  20664. ("rsaquo")
  20665. ("euro")
  20666. ("arccos"."arccos")
  20667. ("arcsin"."arcsin")
  20668. ("arctan"."arctan")
  20669. ("arg"."arg")
  20670. ("cos"."cos")
  20671. ("cosh"."cosh")
  20672. ("cot"."cot")
  20673. ("coth"."coth")
  20674. ("csc"."csc")
  20675. ("deg"."deg")
  20676. ("det"."det")
  20677. ("dim"."dim")
  20678. ("exp"."exp")
  20679. ("gcd"."gcd")
  20680. ("hom"."hom")
  20681. ("inf"."inf")
  20682. ("ker"."ker")
  20683. ("lg"."lg")
  20684. ("lim"."lim")
  20685. ("liminf"."liminf")
  20686. ("limsup"."limsup")
  20687. ("ln"."ln")
  20688. ("log"."log")
  20689. ("max"."max")
  20690. ("min"."min")
  20691. ("Pr"."Pr")
  20692. ("sec"."sec")
  20693. ("sin"."sin")
  20694. ("sinh"."sinh")
  20695. ("sup"."sup")
  20696. ("tan"."tan")
  20697. ("tanh"."tanh")
  20698. )
  20699. "Entities for TeX->HTML translation.
  20700. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  20701. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  20702. In that case, \"\\ent\" will be translated to \"&other;\".
  20703. The list contains HTML entities for Latin-1, Greek and other symbols.
  20704. It is supplemented by a number of commonly used TeX macros with appropriate
  20705. translations. There is currently no way for users to extend this.")
  20706. ;;; General functions for all backends
  20707. (defun org-cleaned-string-for-export (string &rest parameters)
  20708. "Cleanup a buffer STRING so that links can be created safely."
  20709. (interactive)
  20710. (let* ((re-radio (and org-target-link-regexp
  20711. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  20712. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  20713. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  20714. (re-archive (concat ":" org-archive-tag ":"))
  20715. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  20716. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  20717. (htmlp (plist-get parameters :for-html))
  20718. (asciip (plist-get parameters :for-ascii))
  20719. (latexp (plist-get parameters :for-LaTeX))
  20720. (commentsp (plist-get parameters :comments))
  20721. (archived-trees (plist-get parameters :archived-trees))
  20722. (inhibit-read-only t)
  20723. (outline-regexp "\\*+ ")
  20724. a b xx
  20725. rtn p)
  20726. (with-current-buffer (get-buffer-create " org-mode-tmp")
  20727. (erase-buffer)
  20728. (insert string)
  20729. ;; Remove license-to-kill stuff
  20730. (while (setq p (text-property-any (point-min) (point-max)
  20731. :org-license-to-kill t))
  20732. (delete-region p (next-single-property-change p :org-license-to-kill)))
  20733. (let ((org-inhibit-startup t)) (org-mode))
  20734. (untabify (point-min) (point-max))
  20735. ;; Get the correct stuff before the first headline
  20736. (when (plist-get parameters :skip-before-1st-heading)
  20737. (goto-char (point-min))
  20738. (when (re-search-forward "^\\*+[ \t]" nil t)
  20739. (delete-region (point-min) (match-beginning 0))
  20740. (goto-char (point-min))
  20741. (insert "\n")))
  20742. (when (plist-get parameters :add-text)
  20743. (goto-char (point-min))
  20744. (insert (plist-get parameters :add-text) "\n"))
  20745. ;; Get rid of archived trees
  20746. (when (not (eq archived-trees t))
  20747. (goto-char (point-min))
  20748. (while (re-search-forward re-archive nil t)
  20749. (if (not (org-on-heading-p t))
  20750. (org-end-of-subtree t)
  20751. (beginning-of-line 1)
  20752. (setq a (if archived-trees
  20753. (1+ (point-at-eol)) (point))
  20754. b (org-end-of-subtree t))
  20755. (if (> b a) (delete-region a b)))))
  20756. ;; Get rid of drawers
  20757. (unless (eq t org-export-with-drawers)
  20758. (goto-char (point-min))
  20759. (let ((re (concat "^[ \t]*:\\("
  20760. (mapconcat 'identity
  20761. (if (listp org-export-with-drawers)
  20762. org-export-with-drawers
  20763. org-drawers)
  20764. "\\|")
  20765. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  20766. (while (re-search-forward re nil t)
  20767. (replace-match ""))))
  20768. ;; Find targets in comments and move them out of comments,
  20769. ;; but mark them as targets that should be invisible
  20770. (goto-char (point-min))
  20771. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  20772. (replace-match "\\1(INVISIBLE)"))
  20773. ;; Protect backend specific stuff, throw away the others.
  20774. (goto-char (point-min))
  20775. (let ((formatters
  20776. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  20777. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  20778. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  20779. fmt)
  20780. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  20781. (add-text-properties (match-beginning 0) (match-end 0)
  20782. '(org-protected t)))
  20783. (while formatters
  20784. (setq fmt (pop formatters))
  20785. (when (car fmt)
  20786. (goto-char (point-min))
  20787. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  20788. ":[ \t]*\\(.*\\)") nil t)
  20789. (replace-match "\\1" t)
  20790. (add-text-properties
  20791. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  20792. '(org-protected t))))
  20793. (goto-char (point-min))
  20794. (while (re-search-forward
  20795. (concat "^#\\+"
  20796. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  20797. (cadddr fmt) "\\>.*\n?") nil t)
  20798. (if (car fmt)
  20799. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  20800. '(org-protected t))
  20801. (delete-region (match-beginning 0) (match-end 0))))))
  20802. ;; Protect quoted subtrees
  20803. (goto-char (point-min))
  20804. (while (re-search-forward re-quote nil t)
  20805. (goto-char (match-beginning 0))
  20806. (end-of-line 1)
  20807. (add-text-properties (point) (org-end-of-subtree t)
  20808. '(org-protected t)))
  20809. ;; Remove subtrees that are commented
  20810. (goto-char (point-min))
  20811. (while (re-search-forward re-commented nil t)
  20812. (goto-char (match-beginning 0))
  20813. (delete-region (point) (org-end-of-subtree t)))
  20814. ;; Remove special table lines
  20815. (when org-export-table-remove-special-lines
  20816. (goto-char (point-min))
  20817. (while (re-search-forward "^[ \t]*|" nil t)
  20818. (beginning-of-line 1)
  20819. (if (or (looking-at "[ \t]*| *[!_^] *|")
  20820. (and (looking-at ".*?| *<[0-9]+> *|")
  20821. (not (looking-at ".*?| *[^ <|]"))))
  20822. (delete-region (max (point-min) (1- (point-at-bol)))
  20823. (point-at-eol))
  20824. (end-of-line 1))))
  20825. ;; Specific LaTeX stuff
  20826. (when latexp
  20827. (require 'org-export-latex nil)
  20828. (org-export-latex-cleaned-string))
  20829. ;; Specific HTML stuff
  20830. (when htmlp
  20831. ;; Convert LaTeX fragments to images
  20832. (when (plist-get parameters :LaTeX-fragments)
  20833. (org-format-latex
  20834. (concat "ltxpng/" (file-name-sans-extension
  20835. (file-name-nondirectory
  20836. org-current-export-file)))
  20837. org-current-export-dir nil "Creating LaTeX image %s"))
  20838. (message "Exporting..."))
  20839. ;; Remove or replace comments
  20840. (goto-char (point-min))
  20841. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  20842. (if commentsp
  20843. (progn (add-text-properties
  20844. (match-beginning 0) (match-end 0) '(org-protected t))
  20845. (replace-match (format commentsp (match-string 1)) t t))
  20846. (replace-match "")))
  20847. ;; Find matches for radio targets and turn them into internal links
  20848. (goto-char (point-min))
  20849. (when re-radio
  20850. (while (re-search-forward re-radio nil t)
  20851. (org-if-unprotected
  20852. (replace-match "\\1[[\\2]]"))))
  20853. ;; Find all links that contain a newline and put them into a single line
  20854. (goto-char (point-min))
  20855. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  20856. (org-if-unprotected
  20857. (replace-match "\\1 \\3")
  20858. (goto-char (match-beginning 0))))
  20859. ;; Normalize links: Convert angle and plain links into bracket links
  20860. ;; Expand link abbreviations
  20861. (goto-char (point-min))
  20862. (while (re-search-forward re-plain-link nil t)
  20863. (goto-char (1- (match-end 0)))
  20864. (org-if-unprotected
  20865. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  20866. ":" (match-string 3) "]]")))
  20867. ;; added 'org-link face to links
  20868. (put-text-property 0 (length s) 'face 'org-link s)
  20869. (replace-match s t t))))
  20870. (goto-char (point-min))
  20871. (while (re-search-forward re-angle-link nil t)
  20872. (goto-char (1- (match-end 0)))
  20873. (org-if-unprotected
  20874. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  20875. ":" (match-string 3) "]]")))
  20876. (put-text-property 0 (length s) 'face 'org-link s)
  20877. (replace-match s t t))))
  20878. (goto-char (point-min))
  20879. (while (re-search-forward org-bracket-link-regexp nil t)
  20880. (org-if-unprotected
  20881. (let* ((s (concat "[[" (setq xx (save-match-data
  20882. (org-link-expand-abbrev (match-string 1))))
  20883. "]"
  20884. (if (match-end 3)
  20885. (match-string 2)
  20886. (concat "[" xx "]"))
  20887. "]")))
  20888. (put-text-property 0 (length s) 'face 'org-link s)
  20889. (replace-match s t t))))
  20890. ;; Find multiline emphasis and put them into single line
  20891. (when (plist-get parameters :emph-multiline)
  20892. (goto-char (point-min))
  20893. (while (re-search-forward org-emph-re nil t)
  20894. (if (not (= (char-after (match-beginning 3))
  20895. (char-after (match-beginning 4))))
  20896. (org-if-unprotected
  20897. (subst-char-in-region (match-beginning 0) (match-end 0)
  20898. ?\n ?\ t)
  20899. (goto-char (1- (match-end 0))))
  20900. (goto-char (1+ (match-beginning 0))))))
  20901. (setq rtn (buffer-string)))
  20902. (kill-buffer " org-mode-tmp")
  20903. rtn))
  20904. (defun org-export-grab-title-from-buffer ()
  20905. "Get a title for the current document, from looking at the buffer."
  20906. (let ((inhibit-read-only t))
  20907. (save-excursion
  20908. (goto-char (point-min))
  20909. (let ((end (save-excursion (outline-next-heading) (point))))
  20910. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  20911. ;; Mark the line so that it will not be exported as normal text.
  20912. (org-unmodified
  20913. (add-text-properties (match-beginning 0) (match-end 0)
  20914. (list :org-license-to-kill t)))
  20915. ;; Return the title string
  20916. (org-trim (match-string 0)))))))
  20917. (defun org-export-get-title-from-subtree ()
  20918. "Return subtree title and exclude it from export."
  20919. (let (title (m (mark)))
  20920. (save-excursion
  20921. (goto-char (region-beginning))
  20922. (when (and (org-at-heading-p)
  20923. (>= (org-end-of-subtree t t) (region-end)))
  20924. ;; This is a subtree, we take the title from the first heading
  20925. (goto-char (region-beginning))
  20926. (looking-at org-todo-line-regexp)
  20927. (setq title (match-string 3))
  20928. (org-unmodified
  20929. (add-text-properties (point) (1+ (point-at-eol))
  20930. (list :org-license-to-kill t)))))
  20931. title))
  20932. (defun org-solidify-link-text (s &optional alist)
  20933. "Take link text and make a safe target out of it."
  20934. (save-match-data
  20935. (let* ((rtn
  20936. (mapconcat
  20937. 'identity
  20938. (org-split-string s "[ \t\r\n]+") "--"))
  20939. (a (assoc rtn alist)))
  20940. (or (cdr a) rtn))))
  20941. (defun org-get-min-level (lines)
  20942. "Get the minimum level in LINES."
  20943. (let ((re "^\\(\\*+\\) ") l min)
  20944. (catch 'exit
  20945. (while (setq l (pop lines))
  20946. (if (string-match re l)
  20947. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  20948. 1)))
  20949. ;; Variable holding the vector with section numbers
  20950. (defvar org-section-numbers (make-vector org-level-max 0))
  20951. (defun org-init-section-numbers ()
  20952. "Initialize the vector for the section numbers."
  20953. (let* ((level -1)
  20954. (numbers (nreverse (org-split-string "" "\\.")))
  20955. (depth (1- (length org-section-numbers)))
  20956. (i depth) number-string)
  20957. (while (>= i 0)
  20958. (if (> i level)
  20959. (aset org-section-numbers i 0)
  20960. (setq number-string (or (car numbers) "0"))
  20961. (if (string-match "\\`[A-Z]\\'" number-string)
  20962. (aset org-section-numbers i
  20963. (- (string-to-char number-string) ?A -1))
  20964. (aset org-section-numbers i (string-to-number number-string)))
  20965. (pop numbers))
  20966. (setq i (1- i)))))
  20967. (defun org-section-number (&optional level)
  20968. "Return a string with the current section number.
  20969. When LEVEL is non-nil, increase section numbers on that level."
  20970. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  20971. (when level
  20972. (when (> level -1)
  20973. (aset org-section-numbers
  20974. level (1+ (aref org-section-numbers level))))
  20975. (setq idx (1+ level))
  20976. (while (<= idx depth)
  20977. (if (not (= idx 1))
  20978. (aset org-section-numbers idx 0))
  20979. (setq idx (1+ idx))))
  20980. (setq idx 0)
  20981. (while (<= idx depth)
  20982. (setq n (aref org-section-numbers idx))
  20983. (setq string (concat string (if (not (string= string "")) "." "")
  20984. (int-to-string n)))
  20985. (setq idx (1+ idx)))
  20986. (save-match-data
  20987. (if (string-match "\\`\\([@0]\\.\\)+" string)
  20988. (setq string (replace-match "" t nil string)))
  20989. (if (string-match "\\(\\.0\\)+\\'" string)
  20990. (setq string (replace-match "" t nil string))))
  20991. string))
  20992. ;;; ASCII export
  20993. (defvar org-last-level nil) ; dynamically scoped variable
  20994. (defvar org-min-level nil) ; dynamically scoped variable
  20995. (defvar org-levels-open nil) ; dynamically scoped parameter
  20996. (defvar org-ascii-current-indentation nil) ; For communication
  20997. (defun org-export-as-ascii (arg)
  20998. "Export the outline as a pretty ASCII file.
  20999. If there is an active region, export only the region.
  21000. The prefix ARG specifies how many levels of the outline should become
  21001. underlined headlines. The default is 3."
  21002. (interactive "P")
  21003. (setq-default org-todo-line-regexp org-todo-line-regexp)
  21004. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  21005. (org-infile-export-plist)))
  21006. (region-p (org-region-active-p))
  21007. (subtree-p
  21008. (when region-p
  21009. (save-excursion
  21010. (goto-char (region-beginning))
  21011. (and (org-at-heading-p)
  21012. (>= (org-end-of-subtree t t) (region-end))))))
  21013. (custom-times org-display-custom-times)
  21014. (org-ascii-current-indentation '(0 . 0))
  21015. (level 0) line txt
  21016. (umax nil)
  21017. (umax-toc nil)
  21018. (case-fold-search nil)
  21019. (filename (concat (file-name-as-directory
  21020. (org-export-directory :ascii opt-plist))
  21021. (file-name-sans-extension
  21022. (or (and subtree-p
  21023. (org-entry-get (region-beginning)
  21024. "EXPORT_FILE_NAME" t))
  21025. (file-name-nondirectory buffer-file-name)))
  21026. ".txt"))
  21027. (filename (if (equal (file-truename filename)
  21028. (file-truename buffer-file-name))
  21029. (concat filename ".txt")
  21030. filename))
  21031. (buffer (find-file-noselect filename))
  21032. (org-levels-open (make-vector org-level-max nil))
  21033. (odd org-odd-levels-only)
  21034. (date (plist-get opt-plist :date))
  21035. (author (plist-get opt-plist :author))
  21036. (title (or (and subtree-p (org-export-get-title-from-subtree))
  21037. (plist-get opt-plist :title)
  21038. (and (not
  21039. (plist-get opt-plist :skip-before-1st-heading))
  21040. (org-export-grab-title-from-buffer))
  21041. (file-name-sans-extension
  21042. (file-name-nondirectory buffer-file-name))))
  21043. (email (plist-get opt-plist :email))
  21044. (language (plist-get opt-plist :language))
  21045. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  21046. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  21047. (todo nil)
  21048. (lang-words nil)
  21049. (region
  21050. (buffer-substring
  21051. (if (org-region-active-p) (region-beginning) (point-min))
  21052. (if (org-region-active-p) (region-end) (point-max))))
  21053. (lines (org-split-string
  21054. (org-cleaned-string-for-export
  21055. region
  21056. :for-ascii t
  21057. :skip-before-1st-heading
  21058. (plist-get opt-plist :skip-before-1st-heading)
  21059. :archived-trees
  21060. (plist-get opt-plist :archived-trees)
  21061. :add-text (plist-get opt-plist :text))
  21062. "\n"))
  21063. thetoc have-headings first-heading-pos
  21064. table-open table-buffer)
  21065. (let ((inhibit-read-only t))
  21066. (org-unmodified
  21067. (remove-text-properties (point-min) (point-max)
  21068. '(:org-license-to-kill t))))
  21069. (setq org-min-level (org-get-min-level lines))
  21070. (setq org-last-level org-min-level)
  21071. (org-init-section-numbers)
  21072. (find-file-noselect filename)
  21073. (setq lang-words (or (assoc language org-export-language-setup)
  21074. (assoc "en" org-export-language-setup)))
  21075. (switch-to-buffer-other-window buffer)
  21076. (erase-buffer)
  21077. (fundamental-mode)
  21078. ;; create local variables for all options, to make sure all called
  21079. ;; functions get the correct information
  21080. (mapc (lambda (x)
  21081. (set (make-local-variable (cdr x))
  21082. (plist-get opt-plist (car x))))
  21083. org-export-plist-vars)
  21084. (org-set-local 'org-odd-levels-only odd)
  21085. (setq umax (if arg (prefix-numeric-value arg)
  21086. org-export-headline-levels))
  21087. (setq umax-toc (if (integerp org-export-with-toc)
  21088. (min org-export-with-toc umax)
  21089. umax))
  21090. ;; File header
  21091. (if title (org-insert-centered title ?=))
  21092. (insert "\n")
  21093. (if (and (or author email)
  21094. org-export-author-info)
  21095. (insert (concat (nth 1 lang-words) ": " (or author "")
  21096. (if email (concat " <" email ">") "")
  21097. "\n")))
  21098. (cond
  21099. ((and date (string-match "%" date))
  21100. (setq date (format-time-string date (current-time))))
  21101. (date)
  21102. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  21103. (if (and date org-export-time-stamp-file)
  21104. (insert (concat (nth 2 lang-words) ": " date"\n")))
  21105. (insert "\n\n")
  21106. (if org-export-with-toc
  21107. (progn
  21108. (push (concat (nth 3 lang-words) "\n") thetoc)
  21109. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  21110. (mapc '(lambda (line)
  21111. (if (string-match org-todo-line-regexp
  21112. line)
  21113. ;; This is a headline
  21114. (progn
  21115. (setq have-headings t)
  21116. (setq level (- (match-end 1) (match-beginning 1))
  21117. level (org-tr-level level)
  21118. txt (match-string 3 line)
  21119. todo
  21120. (or (and org-export-mark-todo-in-toc
  21121. (match-beginning 2)
  21122. (not (member (match-string 2 line)
  21123. org-done-keywords)))
  21124. ; TODO, not DONE
  21125. (and org-export-mark-todo-in-toc
  21126. (= level umax-toc)
  21127. (org-search-todo-below
  21128. line lines level))))
  21129. (setq txt (org-html-expand-for-ascii txt))
  21130. (while (string-match org-bracket-link-regexp txt)
  21131. (setq txt
  21132. (replace-match
  21133. (match-string (if (match-end 2) 3 1) txt)
  21134. t t txt)))
  21135. (if (and (memq org-export-with-tags '(not-in-toc nil))
  21136. (string-match
  21137. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  21138. txt))
  21139. (setq txt (replace-match "" t t txt)))
  21140. (if (string-match quote-re0 txt)
  21141. (setq txt (replace-match "" t t txt)))
  21142. (if org-export-with-section-numbers
  21143. (setq txt (concat (org-section-number level)
  21144. " " txt)))
  21145. (if (<= level umax-toc)
  21146. (progn
  21147. (push
  21148. (concat
  21149. (make-string
  21150. (* (max 0 (- level org-min-level)) 4) ?\ )
  21151. (format (if todo "%s (*)\n" "%s\n") txt))
  21152. thetoc)
  21153. (setq org-last-level level))
  21154. ))))
  21155. lines)
  21156. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  21157. (org-init-section-numbers)
  21158. (while (setq line (pop lines))
  21159. ;; Remove the quoted HTML tags.
  21160. (setq line (org-html-expand-for-ascii line))
  21161. ;; Remove targets
  21162. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  21163. (setq line (replace-match "" t t line)))
  21164. ;; Replace internal links
  21165. (while (string-match org-bracket-link-regexp line)
  21166. (setq line (replace-match
  21167. (if (match-end 3) "[\\3]" "[\\1]")
  21168. t nil line)))
  21169. (when custom-times
  21170. (setq line (org-translate-time line)))
  21171. (cond
  21172. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  21173. ;; a Headline
  21174. (setq first-heading-pos (or first-heading-pos (point)))
  21175. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  21176. txt (match-string 2 line))
  21177. (org-ascii-level-start level txt umax lines))
  21178. ((and org-export-with-tables
  21179. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  21180. (if (not table-open)
  21181. ;; New table starts
  21182. (setq table-open t table-buffer nil))
  21183. ;; Accumulate lines
  21184. (setq table-buffer (cons line table-buffer))
  21185. (when (or (not lines)
  21186. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  21187. (car lines))))
  21188. (setq table-open nil
  21189. table-buffer (nreverse table-buffer))
  21190. (insert (mapconcat
  21191. (lambda (x)
  21192. (org-fix-indentation x org-ascii-current-indentation))
  21193. (org-format-table-ascii table-buffer)
  21194. "\n") "\n")))
  21195. (t
  21196. (setq line (org-fix-indentation line org-ascii-current-indentation))
  21197. (if (and org-export-with-fixed-width
  21198. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  21199. (setq line (replace-match "\\1" nil nil line)))
  21200. (insert line "\n"))))
  21201. (normal-mode)
  21202. ;; insert the table of contents
  21203. (when thetoc
  21204. (goto-char (point-min))
  21205. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  21206. (progn
  21207. (goto-char (match-beginning 0))
  21208. (replace-match ""))
  21209. (goto-char first-heading-pos))
  21210. (mapc 'insert thetoc)
  21211. (or (looking-at "[ \t]*\n[ \t]*\n")
  21212. (insert "\n\n")))
  21213. ;; Convert whitespace place holders
  21214. (goto-char (point-min))
  21215. (let (beg end)
  21216. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  21217. (setq end (next-single-property-change beg 'org-whitespace))
  21218. (goto-char beg)
  21219. (delete-region beg end)
  21220. (insert (make-string (- end beg) ?\ ))))
  21221. (save-buffer)
  21222. ;; remove display and invisible chars
  21223. (let (beg end)
  21224. (goto-char (point-min))
  21225. (while (setq beg (next-single-property-change (point) 'display))
  21226. (setq end (next-single-property-change beg 'display))
  21227. (delete-region beg end)
  21228. (goto-char beg)
  21229. (insert "=>"))
  21230. (goto-char (point-min))
  21231. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  21232. (setq end (next-single-property-change beg 'org-cwidth))
  21233. (delete-region beg end)
  21234. (goto-char beg)))
  21235. (goto-char (point-min))))
  21236. (defun org-search-todo-below (line lines level)
  21237. "Search the subtree below LINE for any TODO entries."
  21238. (let ((rest (cdr (memq line lines)))
  21239. (re org-todo-line-regexp)
  21240. line lv todo)
  21241. (catch 'exit
  21242. (while (setq line (pop rest))
  21243. (if (string-match re line)
  21244. (progn
  21245. (setq lv (- (match-end 1) (match-beginning 1))
  21246. todo (and (match-beginning 2)
  21247. (not (member (match-string 2 line)
  21248. org-done-keywords))))
  21249. ; TODO, not DONE
  21250. (if (<= lv level) (throw 'exit nil))
  21251. (if todo (throw 'exit t))))))))
  21252. (defun org-html-expand-for-ascii (line)
  21253. "Handle quoted HTML for ASCII export."
  21254. (if org-export-html-expand
  21255. (while (string-match "@<[^<>\n]*>" line)
  21256. ;; We just remove the tags for now.
  21257. (setq line (replace-match "" nil nil line))))
  21258. line)
  21259. (defun org-insert-centered (s &optional underline)
  21260. "Insert the string S centered and underline it with character UNDERLINE."
  21261. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  21262. (insert (make-string ind ?\ ) s "\n")
  21263. (if underline
  21264. (insert (make-string ind ?\ )
  21265. (make-string (string-width s) underline)
  21266. "\n"))))
  21267. (defun org-ascii-level-start (level title umax &optional lines)
  21268. "Insert a new level in ASCII export."
  21269. (let (char (n (- level umax 1)) (ind 0))
  21270. (if (> level umax)
  21271. (progn
  21272. (insert (make-string (* 2 n) ?\ )
  21273. (char-to-string (nth (% n (length org-export-ascii-bullets))
  21274. org-export-ascii-bullets))
  21275. " " title "\n")
  21276. ;; find the indentation of the next non-empty line
  21277. (catch 'stop
  21278. (while lines
  21279. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  21280. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  21281. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  21282. (pop lines)))
  21283. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  21284. (if (or (not (equal (char-before) ?\n))
  21285. (not (equal (char-before (1- (point))) ?\n)))
  21286. (insert "\n"))
  21287. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  21288. (unless org-export-with-tags
  21289. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  21290. (setq title (replace-match "" t t title))))
  21291. (if org-export-with-section-numbers
  21292. (setq title (concat (org-section-number level) " " title)))
  21293. (insert title "\n" (make-string (string-width title) char) "\n")
  21294. (setq org-ascii-current-indentation '(0 . 0)))))
  21295. (defun org-export-visible (type arg)
  21296. "Create a copy of the visible part of the current buffer, and export it.
  21297. The copy is created in a temporary buffer and removed after use.
  21298. TYPE is the final key (as a string) that also select the export command in
  21299. the `C-c C-e' export dispatcher.
  21300. As a special case, if the you type SPC at the prompt, the temporary
  21301. org-mode file will not be removed but presented to you so that you can
  21302. continue to use it. The prefix arg ARG is passed through to the exporting
  21303. command."
  21304. (interactive
  21305. (list (progn
  21306. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  21307. (read-char-exclusive))
  21308. current-prefix-arg))
  21309. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  21310. (error "Invalid export key"))
  21311. (let* ((binding (cdr (assoc type
  21312. '((?a . org-export-as-ascii)
  21313. (?\C-a . org-export-as-ascii)
  21314. (?b . org-export-as-html-and-open)
  21315. (?\C-b . org-export-as-html-and-open)
  21316. (?h . org-export-as-html)
  21317. (?H . org-export-as-html-to-buffer)
  21318. (?R . org-export-region-as-html)
  21319. (?x . org-export-as-xoxo)))))
  21320. (keepp (equal type ?\ ))
  21321. (file buffer-file-name)
  21322. (buffer (get-buffer-create "*Org Export Visible*"))
  21323. s e)
  21324. ;; Need to hack the drawers here.
  21325. (save-excursion
  21326. (goto-char (point-min))
  21327. (while (re-search-forward org-drawer-regexp nil t)
  21328. (goto-char (match-beginning 1))
  21329. (or (org-invisible-p) (org-flag-drawer nil))))
  21330. (with-current-buffer buffer (erase-buffer))
  21331. (save-excursion
  21332. (setq s (goto-char (point-min)))
  21333. (while (not (= (point) (point-max)))
  21334. (goto-char (org-find-invisible))
  21335. (append-to-buffer buffer s (point))
  21336. (setq s (goto-char (org-find-visible))))
  21337. (org-cycle-hide-drawers 'all)
  21338. (goto-char (point-min))
  21339. (unless keepp
  21340. ;; Copy all comment lines to the end, to make sure #+ settings are
  21341. ;; still available for the second export step. Kind of a hack, but
  21342. ;; does do the trick.
  21343. (if (looking-at "#[^\r\n]*")
  21344. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  21345. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  21346. (append-to-buffer buffer (1+ (match-beginning 0))
  21347. (min (point-max) (1+ (match-end 0))))))
  21348. (set-buffer buffer)
  21349. (let ((buffer-file-name file)
  21350. (org-inhibit-startup t))
  21351. (org-mode)
  21352. (show-all)
  21353. (unless keepp (funcall binding arg))))
  21354. (if (not keepp)
  21355. (kill-buffer buffer)
  21356. (switch-to-buffer-other-window buffer)
  21357. (goto-char (point-min)))))
  21358. (defun org-find-visible ()
  21359. (let ((s (point)))
  21360. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  21361. (get-char-property s 'invisible)))
  21362. s))
  21363. (defun org-find-invisible ()
  21364. (let ((s (point)))
  21365. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  21366. (not (get-char-property s 'invisible))))
  21367. s))
  21368. ;;; HTML export
  21369. (defun org-get-current-options ()
  21370. "Return a string with current options as keyword options.
  21371. Does include HTML export options as well as TODO and CATEGORY stuff."
  21372. (format
  21373. "#+TITLE: %s
  21374. #+AUTHOR: %s
  21375. #+EMAIL: %s
  21376. #+LANGUAGE: %s
  21377. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  21378. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s d:%s tags:%s
  21379. #+CATEGORY: %s
  21380. #+SEQ_TODO: %s
  21381. #+TYP_TODO: %s
  21382. #+PRIORITIES: %c %c %c
  21383. #+DRAWERS: %s
  21384. #+STARTUP: %s %s %s %s %s
  21385. #+TAGS: %s
  21386. #+ARCHIVE: %s
  21387. #+LINK: %s
  21388. "
  21389. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  21390. org-export-headline-levels
  21391. org-export-with-section-numbers
  21392. org-export-with-toc
  21393. org-export-preserve-breaks
  21394. org-export-html-expand
  21395. org-export-with-fixed-width
  21396. org-export-with-tables
  21397. org-export-with-sub-superscripts
  21398. org-export-with-footnotes
  21399. org-export-with-emphasize
  21400. org-export-with-TeX-macros
  21401. org-export-with-LaTeX-fragments
  21402. org-export-skip-text-before-1st-heading
  21403. org-export-with-drawers
  21404. org-export-with-tags
  21405. (file-name-nondirectory buffer-file-name)
  21406. "TODO FEEDBACK VERIFY DONE"
  21407. "Me Jason Marie DONE"
  21408. org-highest-priority org-lowest-priority org-default-priority
  21409. (mapconcat 'identity org-drawers " ")
  21410. (cdr (assoc org-startup-folded
  21411. '((nil . "showall") (t . "overview") (content . "content"))))
  21412. (if org-odd-levels-only "odd" "oddeven")
  21413. (if org-hide-leading-stars "hidestars" "showstars")
  21414. (if org-startup-align-all-tables "align" "noalign")
  21415. (cond ((eq t org-log-done) "logdone")
  21416. ((not org-log-done) "nologging")
  21417. ((listp org-log-done)
  21418. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  21419. org-log-done " ")))
  21420. (or (mapconcat (lambda (x)
  21421. (cond
  21422. ((equal '(:startgroup) x) "{")
  21423. ((equal '(:endgroup) x) "}")
  21424. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  21425. (t (car x))))
  21426. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  21427. org-archive-location
  21428. "org file:~/org/%s.org"
  21429. ))
  21430. (defun org-insert-export-options-template ()
  21431. "Insert into the buffer a template with information for exporting."
  21432. (interactive)
  21433. (if (not (bolp)) (newline))
  21434. (let ((s (org-get-current-options)))
  21435. (and (string-match "#\\+CATEGORY" s)
  21436. (setq s (substring s 0 (match-beginning 0))))
  21437. (insert s)))
  21438. (defun org-toggle-fixed-width-section (arg)
  21439. "Toggle the fixed-width export.
  21440. If there is no active region, the QUOTE keyword at the current headline is
  21441. inserted or removed. When present, it causes the text between this headline
  21442. and the next to be exported as fixed-width text, and unmodified.
  21443. If there is an active region, this command adds or removes a colon as the
  21444. first character of this line. If the first character of a line is a colon,
  21445. this line is also exported in fixed-width font."
  21446. (interactive "P")
  21447. (let* ((cc 0)
  21448. (regionp (org-region-active-p))
  21449. (beg (if regionp (region-beginning) (point)))
  21450. (end (if regionp (region-end)))
  21451. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  21452. (re "[ \t]*\\(:\\)")
  21453. off)
  21454. (if regionp
  21455. (save-excursion
  21456. (goto-char beg)
  21457. (setq cc (current-column))
  21458. (beginning-of-line 1)
  21459. (setq off (looking-at re))
  21460. (while (> nlines 0)
  21461. (setq nlines (1- nlines))
  21462. (beginning-of-line 1)
  21463. (cond
  21464. (arg
  21465. (move-to-column cc t)
  21466. (insert ":\n")
  21467. (forward-line -1))
  21468. ((and off (looking-at re))
  21469. (replace-match "" t t nil 1))
  21470. ((not off) (move-to-column cc t) (insert ":")))
  21471. (forward-line 1)))
  21472. (save-excursion
  21473. (org-back-to-heading)
  21474. (if (looking-at (concat outline-regexp
  21475. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  21476. (replace-match "" t t nil 1)
  21477. (if (looking-at outline-regexp)
  21478. (progn
  21479. (goto-char (match-end 0))
  21480. (insert org-quote-string " "))))))))
  21481. (defun org-export-as-html-and-open (arg)
  21482. "Export the outline as HTML and immediately open it with a browser.
  21483. If there is an active region, export only the region.
  21484. The prefix ARG specifies how many levels of the outline should become
  21485. headlines. The default is 3. Lower levels will become bulleted lists."
  21486. (interactive "P")
  21487. (org-export-as-html arg 'hidden)
  21488. (org-open-file buffer-file-name))
  21489. (defun org-export-as-html-batch ()
  21490. "Call `org-export-as-html', may be used in batch processing as
  21491. emacs --batch
  21492. --load=$HOME/lib/emacs/org.el
  21493. --eval \"(setq org-export-headline-levels 2)\"
  21494. --visit=MyFile --funcall org-export-as-html-batch"
  21495. (org-export-as-html org-export-headline-levels 'hidden))
  21496. (defun org-export-as-html-to-buffer (arg)
  21497. "Call `org-exort-as-html` with output to a temporary buffer.
  21498. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  21499. (interactive "P")
  21500. (org-export-as-html arg nil nil "*Org HTML Export*")
  21501. (switch-to-buffer-other-window "*Org HTML Export*"))
  21502. (defun org-replace-region-by-html (beg end)
  21503. "Assume the current region has org-mode syntax, and convert it to HTML.
  21504. This can be used in any buffer. For example, you could write an
  21505. itemized list in org-mode syntax in an HTML buffer and then use this
  21506. command to convert it."
  21507. (interactive "r")
  21508. (let (reg html buf pop-up-frames)
  21509. (save-window-excursion
  21510. (if (org-mode-p)
  21511. (setq html (org-export-region-as-html
  21512. beg end t 'string))
  21513. (setq reg (buffer-substring beg end)
  21514. buf (get-buffer-create "*Org tmp*"))
  21515. (with-current-buffer buf
  21516. (erase-buffer)
  21517. (insert reg)
  21518. (org-mode)
  21519. (setq html (org-export-region-as-html
  21520. (point-min) (point-max) t 'string)))
  21521. (kill-buffer buf)))
  21522. (delete-region beg end)
  21523. (insert html)))
  21524. (defun org-export-region-as-html (beg end &optional body-only buffer)
  21525. "Convert region from BEG to END in org-mode buffer to HTML.
  21526. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  21527. contents, and only produce the region of converted text, useful for
  21528. cut-and-paste operations.
  21529. If BUFFER is a buffer or a string, use/create that buffer as a target
  21530. of the converted HTML. If BUFFER is the symbol `string', return the
  21531. produced HTML as a string and leave not buffer behind. For example,
  21532. a Lisp program could call this function in the following way:
  21533. (setq html (org-export-region-as-html beg end t 'string))
  21534. When called interactively, the output buffer is selected, and shown
  21535. in a window. A non-interactive call will only retunr the buffer."
  21536. (interactive "r\nP")
  21537. (when (interactive-p)
  21538. (setq buffer "*Org HTML Export*"))
  21539. (let ((transient-mark-mode t) (zmacs-regions t)
  21540. rtn)
  21541. (goto-char end)
  21542. (set-mark (point)) ;; to activate the region
  21543. (goto-char beg)
  21544. (setq rtn (org-export-as-html
  21545. nil nil nil
  21546. buffer body-only))
  21547. (if (fboundp 'deactivate-mark) (deactivate-mark))
  21548. (if (and (interactive-p) (bufferp rtn))
  21549. (switch-to-buffer-other-window rtn)
  21550. rtn)))
  21551. (defun org-export-as-html (arg &optional hidden ext-plist
  21552. to-buffer body-only)
  21553. "Export the outline as a pretty HTML file.
  21554. If there is an active region, export only the region. The prefix
  21555. ARG specifies how many levels of the outline should become
  21556. headlines. The default is 3. Lower levels will become bulleted
  21557. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  21558. EXT-PLIST is a property list with external parameters overriding
  21559. org-mode's default settings, but still inferior to file-local
  21560. settings. When TO-BUFFER is non-nil, create a buffer with that
  21561. name and export to that buffer. If TO-BUFFER is the symbol `string',
  21562. don't leave any buffer behind but just return the resulting HTML as
  21563. a string. When BODY-ONLY is set, don't produce the file header and footer,
  21564. simply return the content of <body>...</body>, without even
  21565. the body tags themselves."
  21566. (interactive "P")
  21567. ;; Make sure we have a file name when we need it.
  21568. (when (and (not (or to-buffer body-only))
  21569. (not buffer-file-name))
  21570. (if (buffer-base-buffer)
  21571. (org-set-local 'buffer-file-name
  21572. (with-current-buffer (buffer-base-buffer)
  21573. buffer-file-name))
  21574. (error "Need a file name to be able to export.")))
  21575. (message "Exporting...")
  21576. (setq-default org-todo-line-regexp org-todo-line-regexp)
  21577. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  21578. (setq-default org-done-keywords org-done-keywords)
  21579. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  21580. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  21581. ext-plist
  21582. (org-infile-export-plist)))
  21583. (style (plist-get opt-plist :style))
  21584. (link-validate (plist-get opt-plist :link-validation-function))
  21585. valid thetoc have-headings first-heading-pos
  21586. (odd org-odd-levels-only)
  21587. (region-p (org-region-active-p))
  21588. (subtree-p
  21589. (when region-p
  21590. (save-excursion
  21591. (goto-char (region-beginning))
  21592. (and (org-at-heading-p)
  21593. (>= (org-end-of-subtree t t) (region-end))))))
  21594. ;; The following two are dynamically scoped into other
  21595. ;; routines below.
  21596. (org-current-export-dir (org-export-directory :html opt-plist))
  21597. (org-current-export-file buffer-file-name)
  21598. (level 0) (line "") (origline "") txt todo
  21599. (umax nil)
  21600. (umax-toc nil)
  21601. (filename (if to-buffer nil
  21602. (concat (file-name-as-directory
  21603. (org-export-directory :html opt-plist))
  21604. (file-name-sans-extension
  21605. (or (and subtree-p
  21606. (org-entry-get (region-beginning)
  21607. "EXPORT_FILE_NAME" t))
  21608. (file-name-nondirectory buffer-file-name)))
  21609. "." org-export-html-extension)))
  21610. (current-dir (if buffer-file-name
  21611. (file-name-directory buffer-file-name)
  21612. default-directory))
  21613. (buffer (if to-buffer
  21614. (cond
  21615. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  21616. (t (get-buffer-create to-buffer)))
  21617. (find-file-noselect filename)))
  21618. (org-levels-open (make-vector org-level-max nil))
  21619. (date (plist-get opt-plist :date))
  21620. (author (plist-get opt-plist :author))
  21621. (title (or (and subtree-p (org-export-get-title-from-subtree))
  21622. (plist-get opt-plist :title)
  21623. (and (not
  21624. (plist-get opt-plist :skip-before-1st-heading))
  21625. (org-export-grab-title-from-buffer))
  21626. (and buffer-file-name
  21627. (file-name-sans-extension
  21628. (file-name-nondirectory buffer-file-name)))
  21629. "UNTITLED"))
  21630. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  21631. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  21632. (inquote nil)
  21633. (infixed nil)
  21634. (in-local-list nil)
  21635. (local-list-num nil)
  21636. (local-list-indent nil)
  21637. (llt org-plain-list-ordered-item-terminator)
  21638. (email (plist-get opt-plist :email))
  21639. (language (plist-get opt-plist :language))
  21640. (lang-words nil)
  21641. (target-alist nil) tg
  21642. (head-count 0) cnt
  21643. (start 0)
  21644. (coding-system (and (boundp 'buffer-file-coding-system)
  21645. buffer-file-coding-system))
  21646. (coding-system-for-write (or org-export-html-coding-system
  21647. coding-system))
  21648. (save-buffer-coding-system (or org-export-html-coding-system
  21649. coding-system))
  21650. (charset (and coding-system-for-write
  21651. (fboundp 'coding-system-get)
  21652. (coding-system-get coding-system-for-write
  21653. 'mime-charset)))
  21654. (region
  21655. (buffer-substring
  21656. (if region-p (region-beginning) (point-min))
  21657. (if region-p (region-end) (point-max))))
  21658. (lines
  21659. (org-split-string
  21660. (org-cleaned-string-for-export
  21661. region
  21662. :emph-multiline t
  21663. :for-html t
  21664. :skip-before-1st-heading
  21665. (plist-get opt-plist :skip-before-1st-heading)
  21666. :archived-trees
  21667. (plist-get opt-plist :archived-trees)
  21668. :add-text
  21669. (plist-get opt-plist :text)
  21670. :LaTeX-fragments
  21671. (plist-get opt-plist :LaTeX-fragments))
  21672. "[\r\n]"))
  21673. table-open type
  21674. table-buffer table-orig-buffer
  21675. ind start-is-num starter didclose
  21676. rpl path desc descp desc1 desc2 link
  21677. )
  21678. (let ((inhibit-read-only t))
  21679. (org-unmodified
  21680. (remove-text-properties (point-min) (point-max)
  21681. '(:org-license-to-kill t))))
  21682. (message "Exporting...")
  21683. (setq org-min-level (org-get-min-level lines))
  21684. (setq org-last-level org-min-level)
  21685. (org-init-section-numbers)
  21686. (cond
  21687. ((and date (string-match "%" date))
  21688. (setq date (format-time-string date (current-time))))
  21689. (date)
  21690. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  21691. ;; Get the language-dependent settings
  21692. (setq lang-words (or (assoc language org-export-language-setup)
  21693. (assoc "en" org-export-language-setup)))
  21694. ;; Switch to the output buffer
  21695. (set-buffer buffer)
  21696. (erase-buffer)
  21697. (fundamental-mode)
  21698. (and (fboundp 'set-buffer-file-coding-system)
  21699. (set-buffer-file-coding-system coding-system-for-write))
  21700. (let ((case-fold-search nil)
  21701. (org-odd-levels-only odd))
  21702. ;; create local variables for all options, to make sure all called
  21703. ;; functions get the correct information
  21704. (mapc (lambda (x)
  21705. (set (make-local-variable (cdr x))
  21706. (plist-get opt-plist (car x))))
  21707. org-export-plist-vars)
  21708. (setq umax (if arg (prefix-numeric-value arg)
  21709. org-export-headline-levels))
  21710. (setq umax-toc (if (integerp org-export-with-toc)
  21711. (min org-export-with-toc umax)
  21712. umax))
  21713. (unless body-only
  21714. ;; File header
  21715. (insert (format
  21716. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  21717. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  21718. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  21719. lang=\"%s\" xml:lang=\"%s\">
  21720. <head>
  21721. <title>%s</title>
  21722. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  21723. <meta name=\"generator\" content=\"Org-mode\"/>
  21724. <meta name=\"generated\" content=\"%s\"/>
  21725. <meta name=\"author\" content=\"%s\"/>
  21726. %s
  21727. </head><body>
  21728. "
  21729. language language (org-html-expand title)
  21730. (or charset "iso-8859-1") date author style))
  21731. (insert (or (plist-get opt-plist :preamble) ""))
  21732. (when (plist-get opt-plist :auto-preamble)
  21733. (if title (insert (format org-export-html-title-format
  21734. (org-html-expand title))))))
  21735. (if (and org-export-with-toc (not body-only))
  21736. (progn
  21737. (push (format "<h%d>%s</h%d>\n"
  21738. org-export-html-toplevel-hlevel
  21739. (nth 3 lang-words)
  21740. org-export-html-toplevel-hlevel)
  21741. thetoc)
  21742. (push "<ul>\n<li>" thetoc)
  21743. (setq lines
  21744. (mapcar '(lambda (line)
  21745. (if (string-match org-todo-line-regexp line)
  21746. ;; This is a headline
  21747. (progn
  21748. (setq have-headings t)
  21749. (setq level (- (match-end 1) (match-beginning 1))
  21750. level (org-tr-level level)
  21751. txt (save-match-data
  21752. (org-html-expand
  21753. (org-export-cleanup-toc-line
  21754. (match-string 3 line))))
  21755. todo
  21756. (or (and org-export-mark-todo-in-toc
  21757. (match-beginning 2)
  21758. (not (member (match-string 2 line)
  21759. org-done-keywords)))
  21760. ; TODO, not DONE
  21761. (and org-export-mark-todo-in-toc
  21762. (= level umax-toc)
  21763. (org-search-todo-below
  21764. line lines level))))
  21765. (if (string-match
  21766. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  21767. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  21768. (if (string-match quote-re0 txt)
  21769. (setq txt (replace-match "" t t txt)))
  21770. (if org-export-with-section-numbers
  21771. (setq txt (concat (org-section-number level)
  21772. " " txt)))
  21773. (if (<= level (max umax umax-toc))
  21774. (setq head-count (+ head-count 1)))
  21775. (if (<= level umax-toc)
  21776. (progn
  21777. (if (> level org-last-level)
  21778. (progn
  21779. (setq cnt (- level org-last-level))
  21780. (while (>= (setq cnt (1- cnt)) 0)
  21781. (push "\n<ul>\n<li>" thetoc))
  21782. (push "\n" thetoc)))
  21783. (if (< level org-last-level)
  21784. (progn
  21785. (setq cnt (- org-last-level level))
  21786. (while (>= (setq cnt (1- cnt)) 0)
  21787. (push "</li>\n</ul>" thetoc))
  21788. (push "\n" thetoc)))
  21789. ;; Check for targets
  21790. (while (string-match org-target-regexp line)
  21791. (setq tg (match-string 1 line)
  21792. line (replace-match
  21793. (concat "@<span class=\"target\">" tg "@</span> ")
  21794. t t line))
  21795. (push (cons (org-solidify-link-text tg)
  21796. (format "sec-%d" head-count))
  21797. target-alist))
  21798. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  21799. (setq txt (replace-match "" t t txt)))
  21800. (push
  21801. (format
  21802. (if todo
  21803. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  21804. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  21805. head-count txt) thetoc)
  21806. (setq org-last-level level))
  21807. )))
  21808. line)
  21809. lines))
  21810. (while (> org-last-level (1- org-min-level))
  21811. (setq org-last-level (1- org-last-level))
  21812. (push "</li>\n</ul>\n" thetoc))
  21813. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  21814. (setq head-count 0)
  21815. (org-init-section-numbers)
  21816. (while (setq line (pop lines) origline line)
  21817. (catch 'nextline
  21818. ;; end of quote section?
  21819. (when (and inquote (string-match "^\\*+ " line))
  21820. (insert "</pre>\n")
  21821. (setq inquote nil))
  21822. ;; inside a quote section?
  21823. (when inquote
  21824. (insert (org-html-protect line) "\n")
  21825. (throw 'nextline nil))
  21826. ;; verbatim lines
  21827. (when (and org-export-with-fixed-width
  21828. (string-match "^[ \t]*:\\(.*\\)" line))
  21829. (when (not infixed)
  21830. (setq infixed t)
  21831. (insert "<pre>\n"))
  21832. (insert (org-html-protect (match-string 1 line)) "\n")
  21833. (when (and lines
  21834. (not (string-match "^[ \t]*\\(:.*\\)"
  21835. (car lines))))
  21836. (setq infixed nil)
  21837. (insert "</pre>\n"))
  21838. (throw 'nextline nil))
  21839. ;; Protected HTML
  21840. (when (get-text-property 0 'org-protected line)
  21841. (let (par)
  21842. (when (re-search-backward
  21843. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  21844. (setq par (match-string 1))
  21845. (replace-match "\\2\n"))
  21846. (insert line "\n")
  21847. (while (and lines
  21848. (get-text-property 0 'org-protected (car lines)))
  21849. (insert (pop lines) "\n"))
  21850. (and par (insert "<p>\n")))
  21851. (throw 'nextline nil))
  21852. ;; Horizontal line
  21853. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  21854. (insert "\n<hr/>\n")
  21855. (throw 'nextline nil))
  21856. ;; make targets to anchors
  21857. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  21858. (cond
  21859. ((match-end 2)
  21860. (setq line (replace-match
  21861. (concat "@<a name=\""
  21862. (org-solidify-link-text (match-string 1 line))
  21863. "\">\\nbsp@</a>")
  21864. t t line)))
  21865. ((and org-export-with-toc (equal (string-to-char line) ?*))
  21866. (setq line (replace-match
  21867. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  21868. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  21869. t t line)))
  21870. (t
  21871. (setq line (replace-match
  21872. (concat "@<a name=\""
  21873. (org-solidify-link-text (match-string 1 line))
  21874. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  21875. t t line)))))
  21876. (setq line (org-html-handle-time-stamps line))
  21877. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  21878. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  21879. ;; Also handle sub_superscripts and checkboxes
  21880. (setq line (org-html-expand line))
  21881. ;; Format the links
  21882. (setq start 0)
  21883. (while (string-match org-bracket-link-analytic-regexp line start)
  21884. (setq start (match-beginning 0))
  21885. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  21886. (setq path (match-string 3 line))
  21887. (setq desc1 (if (match-end 5) (match-string 5 line))
  21888. desc2 (if (match-end 2) (concat type ":" path) path)
  21889. descp (and desc1 (not (equal desc1 desc2)))
  21890. desc (or desc1 desc2))
  21891. ;; Make an image out of the description if that is so wanted
  21892. (when (and descp (org-file-image-p desc))
  21893. (save-match-data
  21894. (if (string-match "^file:" desc)
  21895. (setq desc (substring desc (match-end 0)))))
  21896. (setq desc (concat "<img src=\"" desc "\"/>")))
  21897. ;; FIXME: do we need to unescape here somewhere?
  21898. (cond
  21899. ((equal type "internal")
  21900. (setq rpl
  21901. (concat
  21902. "<a href=\"#"
  21903. (org-solidify-link-text
  21904. (save-match-data (org-link-unescape path)) target-alist)
  21905. "\">" desc "</a>")))
  21906. ((member type '("http" "https"))
  21907. ;; standard URL, just check if we need to inline an image
  21908. (if (and (or (eq t org-export-html-inline-images)
  21909. (and org-export-html-inline-images (not descp)))
  21910. (org-file-image-p path))
  21911. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  21912. (setq link (concat type ":" path))
  21913. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  21914. ((member type '("ftp" "mailto" "news"))
  21915. ;; standard URL
  21916. (setq link (concat type ":" path))
  21917. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  21918. ((string= type "file")
  21919. ;; FILE link
  21920. (let* ((filename path)
  21921. (abs-p (file-name-absolute-p filename))
  21922. thefile file-is-image-p search)
  21923. (save-match-data
  21924. (if (string-match "::\\(.*\\)" filename)
  21925. (setq search (match-string 1 filename)
  21926. filename (replace-match "" t nil filename)))
  21927. (setq valid
  21928. (if (functionp link-validate)
  21929. (funcall link-validate filename current-dir)
  21930. t))
  21931. (setq file-is-image-p (org-file-image-p filename))
  21932. (setq thefile (if abs-p (expand-file-name filename) filename))
  21933. (when (and org-export-html-link-org-files-as-html
  21934. (string-match "\\.org$" thefile))
  21935. (setq thefile (concat (substring thefile 0
  21936. (match-beginning 0))
  21937. "." org-export-html-extension))
  21938. (if (and search
  21939. ;; make sure this is can be used as target search
  21940. (not (string-match "^[0-9]*$" search))
  21941. (not (string-match "^\\*" search))
  21942. (not (string-match "^/.*/$" search)))
  21943. (setq thefile (concat thefile "#"
  21944. (org-solidify-link-text
  21945. (org-link-unescape search)))))
  21946. (when (string-match "^file:" desc)
  21947. (setq desc (replace-match "" t t desc))
  21948. (if (string-match "\\.org$" desc)
  21949. (setq desc (replace-match "" t t desc))))))
  21950. (setq rpl (if (and file-is-image-p
  21951. (or (eq t org-export-html-inline-images)
  21952. (and org-export-html-inline-images
  21953. (not descp))))
  21954. (concat "<img src=\"" thefile "\"/>")
  21955. (concat "<a href=\"" thefile "\">" desc "</a>")))
  21956. (if (not valid) (setq rpl desc))))
  21957. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  21958. (setq rpl (concat "<i>&lt;" type ":"
  21959. (save-match-data (org-link-unescape path))
  21960. "&gt;</i>"))))
  21961. (setq line (replace-match rpl t t line)
  21962. start (+ start (length rpl))))
  21963. ;; TODO items
  21964. (if (and (string-match org-todo-line-regexp line)
  21965. (match-beginning 2))
  21966. (setq line
  21967. (concat (substring line 0 (match-beginning 2))
  21968. "<span class=\""
  21969. (if (member (match-string 2 line)
  21970. org-done-keywords)
  21971. "done" "todo")
  21972. "\">" (match-string 2 line)
  21973. "</span>" (substring line (match-end 2)))))
  21974. ;; Does this contain a reference to a footnote?
  21975. (when org-export-with-footnotes
  21976. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  21977. (let ((n (match-string 2 line)))
  21978. (setq line
  21979. (replace-match
  21980. (format
  21981. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  21982. (match-string 1 line) n n n)
  21983. t t line)))))
  21984. (cond
  21985. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  21986. ;; This is a headline
  21987. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  21988. txt (match-string 2 line))
  21989. (if (string-match quote-re0 txt)
  21990. (setq txt (replace-match "" t t txt)))
  21991. (if (<= level (max umax umax-toc))
  21992. (setq head-count (+ head-count 1)))
  21993. (when in-local-list
  21994. ;; Close any local lists before inserting a new header line
  21995. (while local-list-num
  21996. (org-close-li)
  21997. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  21998. (pop local-list-num))
  21999. (setq local-list-indent nil
  22000. in-local-list nil))
  22001. (setq first-heading-pos (or first-heading-pos (point)))
  22002. (org-html-level-start level txt umax
  22003. (and org-export-with-toc (<= level umax))
  22004. head-count)
  22005. ;; QUOTES
  22006. (when (string-match quote-re line)
  22007. (insert "<pre>")
  22008. (setq inquote t)))
  22009. ((and org-export-with-tables
  22010. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22011. (if (not table-open)
  22012. ;; New table starts
  22013. (setq table-open t table-buffer nil table-orig-buffer nil))
  22014. ;; Accumulate lines
  22015. (setq table-buffer (cons line table-buffer)
  22016. table-orig-buffer (cons origline table-orig-buffer))
  22017. (when (or (not lines)
  22018. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22019. (car lines))))
  22020. (setq table-open nil
  22021. table-buffer (nreverse table-buffer)
  22022. table-orig-buffer (nreverse table-orig-buffer))
  22023. (org-close-par-maybe)
  22024. (insert (org-format-table-html table-buffer table-orig-buffer))))
  22025. (t
  22026. ;; Normal lines
  22027. (when (string-match
  22028. (cond
  22029. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  22030. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  22031. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  22032. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  22033. line)
  22034. (setq ind (org-get-string-indentation line)
  22035. start-is-num (match-beginning 4)
  22036. starter (if (match-beginning 2)
  22037. (substring (match-string 2 line) 0 -1))
  22038. line (substring line (match-beginning 5)))
  22039. (unless (string-match "[^ \t]" line)
  22040. ;; empty line. Pretend indentation is large.
  22041. (setq ind (if org-empty-line-terminates-plain-lists
  22042. 0
  22043. (1+ (or (car local-list-indent) 1)))))
  22044. (setq didclose nil)
  22045. (while (and in-local-list
  22046. (or (and (= ind (car local-list-indent))
  22047. (not starter))
  22048. (< ind (car local-list-indent))))
  22049. (setq didclose t)
  22050. (org-close-li)
  22051. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  22052. (pop local-list-num) (pop local-list-indent)
  22053. (setq in-local-list local-list-indent))
  22054. (cond
  22055. ((and starter
  22056. (or (not in-local-list)
  22057. (> ind (car local-list-indent))))
  22058. ;; Start new (level of) list
  22059. (org-close-par-maybe)
  22060. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  22061. (push start-is-num local-list-num)
  22062. (push ind local-list-indent)
  22063. (setq in-local-list t))
  22064. (starter
  22065. ;; continue current list
  22066. (org-close-li)
  22067. (insert "<li>\n"))
  22068. (didclose
  22069. ;; we did close a list, normal text follows: need <p>
  22070. (org-open-par)))
  22071. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  22072. (setq line
  22073. (replace-match
  22074. (if (equal (match-string 1 line) "X")
  22075. "<b>[X]</b>"
  22076. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  22077. t t line))))
  22078. ;; Empty lines start a new paragraph. If hand-formatted lists
  22079. ;; are not fully interpreted, lines starting with "-", "+", "*"
  22080. ;; also start a new paragraph.
  22081. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  22082. ;; Is this the start of a footnote?
  22083. (when org-export-with-footnotes
  22084. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  22085. (org-close-par-maybe)
  22086. (let ((n (match-string 1 line)))
  22087. (setq line (replace-match
  22088. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  22089. ;; Check if the line break needs to be conserved
  22090. (cond
  22091. ((string-match "\\\\\\\\[ \t]*$" line)
  22092. (setq line (replace-match "<br/>" t t line)))
  22093. (org-export-preserve-breaks
  22094. (setq line (concat line "<br/>"))))
  22095. (insert line "\n")))))
  22096. ;; Properly close all local lists and other lists
  22097. (when inquote (insert "</pre>\n"))
  22098. (when in-local-list
  22099. ;; Close any local lists before inserting a new header line
  22100. (while local-list-num
  22101. (org-close-li)
  22102. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  22103. (pop local-list-num))
  22104. (setq local-list-indent nil
  22105. in-local-list nil))
  22106. (org-html-level-start 0 nil umax
  22107. (and org-export-with-toc (<= level umax))
  22108. head-count)
  22109. (unless body-only
  22110. (when (plist-get opt-plist :auto-postamble)
  22111. (insert "<div id=\"postamble\">")
  22112. (when (and org-export-author-info author)
  22113. (insert "<p class=\"author\"> "
  22114. (nth 1 lang-words) ": " author "\n")
  22115. (when email
  22116. (insert "<a href=\"mailto:" email "\">&lt;"
  22117. email "&gt;</a>\n"))
  22118. (insert "</p>\n"))
  22119. (when (and date org-export-time-stamp-file)
  22120. (insert "<p class=\"date\"> "
  22121. (nth 2 lang-words) ": "
  22122. date "</p>\n"))
  22123. (insert "</div>"))
  22124. (if org-export-html-with-timestamp
  22125. (insert org-export-html-html-helper-timestamp))
  22126. (insert (or (plist-get opt-plist :postamble) ""))
  22127. (insert "</body>\n</html>\n"))
  22128. (normal-mode)
  22129. (if (eq major-mode default-major-mode) (html-mode))
  22130. ;; insert the table of contents
  22131. (goto-char (point-min))
  22132. (when thetoc
  22133. (if (or (re-search-forward
  22134. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  22135. (re-search-forward
  22136. "\\[TABLE-OF-CONTENTS\\]" nil t))
  22137. (progn
  22138. (goto-char (match-beginning 0))
  22139. (replace-match ""))
  22140. (goto-char first-heading-pos)
  22141. (when (looking-at "\\s-*</p>")
  22142. (goto-char (match-end 0))
  22143. (insert "\n")))
  22144. (insert "<div id=\"table-of-contents\">\n")
  22145. (mapc 'insert thetoc)
  22146. (insert "</div>\n"))
  22147. ;; remove empty paragraphs and lists
  22148. (goto-char (point-min))
  22149. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  22150. (replace-match ""))
  22151. (goto-char (point-min))
  22152. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  22153. (replace-match ""))
  22154. ;; Convert whitespace place holders
  22155. (goto-char (point-min))
  22156. (let (beg end n)
  22157. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22158. (setq n (get-text-property beg 'org-whitespace)
  22159. end (next-single-property-change beg 'org-whitespace))
  22160. (goto-char beg)
  22161. (delete-region beg end)
  22162. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  22163. (make-string n ?x)))))
  22164. (or to-buffer (save-buffer))
  22165. (goto-char (point-min))
  22166. (message "Exporting... done")
  22167. (if (eq to-buffer 'string)
  22168. (prog1 (buffer-substring (point-min) (point-max))
  22169. (kill-buffer (current-buffer)))
  22170. (current-buffer)))))
  22171. (defvar org-table-colgroup-info nil)
  22172. (defun org-format-table-ascii (lines)
  22173. "Format a table for ascii export."
  22174. (if (stringp lines)
  22175. (setq lines (org-split-string lines "\n")))
  22176. (if (not (string-match "^[ \t]*|" (car lines)))
  22177. ;; Table made by table.el - test for spanning
  22178. lines
  22179. ;; A normal org table
  22180. ;; Get rid of hlines at beginning and end
  22181. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  22182. (setq lines (nreverse lines))
  22183. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  22184. (setq lines (nreverse lines))
  22185. (when org-export-table-remove-special-lines
  22186. ;; Check if the table has a marking column. If yes remove the
  22187. ;; column and the special lines
  22188. (setq lines (org-table-clean-before-export lines)))
  22189. ;; Get rid of the vertical lines except for grouping
  22190. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  22191. rtn line vl1 start)
  22192. (while (setq line (pop lines))
  22193. (if (string-match org-table-hline-regexp line)
  22194. (and (string-match "|\\(.*\\)|" line)
  22195. (setq line (replace-match " \\1" t nil line)))
  22196. (setq start 0 vl1 vl)
  22197. (while (string-match "|" line start)
  22198. (setq start (match-end 0))
  22199. (or (pop vl1) (setq line (replace-match " " t t line)))))
  22200. (push line rtn))
  22201. (nreverse rtn))))
  22202. (defun org-colgroup-info-to-vline-list (info)
  22203. (let (vl new last)
  22204. (while info
  22205. (setq last new new (pop info))
  22206. (if (or (memq last '(:end :startend))
  22207. (memq new '(:start :startend)))
  22208. (push t vl)
  22209. (push nil vl)))
  22210. (setq vl (nreverse vl))
  22211. (and vl (setcar vl nil))
  22212. vl))
  22213. (defun org-format-table-html (lines olines)
  22214. "Find out which HTML converter to use and return the HTML code."
  22215. (if (stringp lines)
  22216. (setq lines (org-split-string lines "\n")))
  22217. (if (string-match "^[ \t]*|" (car lines))
  22218. ;; A normal org table
  22219. (org-format-org-table-html lines)
  22220. ;; Table made by table.el - test for spanning
  22221. (let* ((hlines (delq nil (mapcar
  22222. (lambda (x)
  22223. (if (string-match "^[ \t]*\\+-" x) x
  22224. nil))
  22225. lines)))
  22226. (first (car hlines))
  22227. (ll (and (string-match "\\S-+" first)
  22228. (match-string 0 first)))
  22229. (re (concat "^[ \t]*" (regexp-quote ll)))
  22230. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  22231. hlines))))
  22232. (if (and (not spanning)
  22233. (not org-export-prefer-native-exporter-for-tables))
  22234. ;; We can use my own converter with HTML conversions
  22235. (org-format-table-table-html lines)
  22236. ;; Need to use the code generator in table.el, with the original text.
  22237. (org-format-table-table-html-using-table-generate-source olines)))))
  22238. (defun org-format-org-table-html (lines &optional splice)
  22239. "Format a table into HTML."
  22240. ;; Get rid of hlines at beginning and end
  22241. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  22242. (setq lines (nreverse lines))
  22243. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  22244. (setq lines (nreverse lines))
  22245. (when org-export-table-remove-special-lines
  22246. ;; Check if the table has a marking column. If yes remove the
  22247. ;; column and the special lines
  22248. (setq lines (org-table-clean-before-export lines)))
  22249. (let ((head (and org-export-highlight-first-table-line
  22250. (delq nil (mapcar
  22251. (lambda (x) (string-match "^[ \t]*|-" x))
  22252. (cdr lines)))))
  22253. (nlines 0) fnum i
  22254. tbopen line fields html gr colgropen)
  22255. (if splice (setq head nil))
  22256. (unless splice (push (if head "<thead>" "<tbody>") html))
  22257. (setq tbopen t)
  22258. (while (setq line (pop lines))
  22259. (catch 'next-line
  22260. (if (string-match "^[ \t]*|-" line)
  22261. (progn
  22262. (unless splice
  22263. (push (if head "</thead>" "</tbody>") html)
  22264. (if lines (push "<tbody>" html) (setq tbopen nil)))
  22265. (setq head nil) ;; head ends here, first time around
  22266. ;; ignore this line
  22267. (throw 'next-line t)))
  22268. ;; Break the line into fields
  22269. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  22270. (unless fnum (setq fnum (make-vector (length fields) 0)))
  22271. (setq nlines (1+ nlines) i -1)
  22272. (push (concat "<tr>"
  22273. (mapconcat
  22274. (lambda (x)
  22275. (setq i (1+ i))
  22276. (if (and (< i nlines)
  22277. (string-match org-table-number-regexp x))
  22278. (incf (aref fnum i)))
  22279. (if head
  22280. (concat (car org-export-table-header-tags) x
  22281. (cdr org-export-table-header-tags))
  22282. (concat (car org-export-table-data-tags) x
  22283. (cdr org-export-table-data-tags))))
  22284. fields "")
  22285. "</tr>")
  22286. html)))
  22287. (unless splice (if tbopen (push "</tbody>" html)))
  22288. (unless splice (push "</table>\n" html))
  22289. (setq html (nreverse html))
  22290. (unless splice
  22291. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  22292. (push (mapconcat
  22293. (lambda (x)
  22294. (setq gr (pop org-table-colgroup-info))
  22295. (format "%s<COL align=\"%s\"></COL>%s"
  22296. (if (memq gr '(:start :startend))
  22297. (prog1
  22298. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  22299. (setq colgropen t))
  22300. "")
  22301. (if (> (/ (float x) nlines) org-table-number-fraction)
  22302. "right" "left")
  22303. (if (memq gr '(:end :startend))
  22304. (progn (setq colgropen nil) "</colgroup>")
  22305. "")))
  22306. fnum "")
  22307. html)
  22308. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  22309. (push org-export-html-table-tag html))
  22310. (concat (mapconcat 'identity html "\n") "\n")))
  22311. (defun org-table-clean-before-export (lines)
  22312. "Check if the table has a marking column.
  22313. If yes remove the column and the special lines."
  22314. (setq org-table-colgroup-info nil)
  22315. (if (memq nil
  22316. (mapcar
  22317. (lambda (x) (or (string-match "^[ \t]*|-" x)
  22318. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  22319. lines))
  22320. (progn
  22321. (setq org-table-clean-did-remove-column nil)
  22322. (delq nil
  22323. (mapcar
  22324. (lambda (x)
  22325. (cond
  22326. ((string-match "^[ \t]*| */ *|" x)
  22327. (setq org-table-colgroup-info
  22328. (mapcar (lambda (x)
  22329. (cond ((member x '("<" "&lt;")) :start)
  22330. ((member x '(">" "&gt;")) :end)
  22331. ((member x '("<>" "&lt;&gt;")) :startend)
  22332. (t nil)))
  22333. (org-split-string x "[ \t]*|[ \t]*")))
  22334. nil)
  22335. (t x)))
  22336. lines)))
  22337. (setq org-table-clean-did-remove-column t)
  22338. (delq nil
  22339. (mapcar
  22340. (lambda (x)
  22341. (cond
  22342. ((string-match "^[ \t]*| */ *|" x)
  22343. (setq org-table-colgroup-info
  22344. (mapcar (lambda (x)
  22345. (cond ((member x '("<" "&lt;")) :start)
  22346. ((member x '(">" "&gt;")) :end)
  22347. ((member x '("<>" "&lt;&gt;")) :startend)
  22348. (t nil)))
  22349. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  22350. nil)
  22351. ((string-match "^[ \t]*| *[!_^/] *|" x)
  22352. nil) ; ignore this line
  22353. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  22354. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  22355. ;; remove the first column
  22356. (replace-match "\\1|" t nil x))
  22357. (t (error "This should not happen"))))
  22358. lines))))
  22359. (defun org-format-table-table-html (lines)
  22360. "Format a table generated by table.el into HTML.
  22361. This conversion does *not* use `table-generate-source' from table.el.
  22362. This has the advantage that Org-mode's HTML conversions can be used.
  22363. But it has the disadvantage, that no cell- or row-spanning is allowed."
  22364. (let (line field-buffer
  22365. (head org-export-highlight-first-table-line)
  22366. fields html empty)
  22367. (setq html (concat org-export-html-table-tag "\n"))
  22368. (while (setq line (pop lines))
  22369. (setq empty "&nbsp;")
  22370. (catch 'next-line
  22371. (if (string-match "^[ \t]*\\+-" line)
  22372. (progn
  22373. (if field-buffer
  22374. (progn
  22375. (setq
  22376. html
  22377. (concat
  22378. html
  22379. "<tr>"
  22380. (mapconcat
  22381. (lambda (x)
  22382. (if (equal x "") (setq x empty))
  22383. (if head
  22384. (concat (car org-export-table-header-tags) x
  22385. (cdr org-export-table-header-tags))
  22386. (concat (car org-export-table-data-tags) x
  22387. (cdr org-export-table-data-tags))))
  22388. field-buffer "\n")
  22389. "</tr>\n"))
  22390. (setq head nil)
  22391. (setq field-buffer nil)))
  22392. ;; Ignore this line
  22393. (throw 'next-line t)))
  22394. ;; Break the line into fields and store the fields
  22395. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  22396. (if field-buffer
  22397. (setq field-buffer (mapcar
  22398. (lambda (x)
  22399. (concat x "<br/>" (pop fields)))
  22400. field-buffer))
  22401. (setq field-buffer fields))))
  22402. (setq html (concat html "</table>\n"))
  22403. html))
  22404. (defun org-format-table-table-html-using-table-generate-source (lines)
  22405. "Format a table into html, using `table-generate-source' from table.el.
  22406. This has the advantage that cell- or row-spanning is allowed.
  22407. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  22408. (require 'table)
  22409. (with-current-buffer (get-buffer-create " org-tmp1 ")
  22410. (erase-buffer)
  22411. (insert (mapconcat 'identity lines "\n"))
  22412. (goto-char (point-min))
  22413. (if (not (re-search-forward "|[^+]" nil t))
  22414. (error "Error processing table"))
  22415. (table-recognize-table)
  22416. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  22417. (table-generate-source 'html " org-tmp2 ")
  22418. (set-buffer " org-tmp2 ")
  22419. (buffer-substring (point-min) (point-max))))
  22420. (defun org-html-handle-time-stamps (s)
  22421. "Format time stamps in string S, or remove them."
  22422. (catch 'exit
  22423. (let (r b)
  22424. (while (string-match org-maybe-keyword-time-regexp s)
  22425. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  22426. ;; never export CLOCK
  22427. (throw 'exit ""))
  22428. (or b (setq b (substring s 0 (match-beginning 0))))
  22429. (if (not org-export-with-timestamps)
  22430. (setq r (concat r (substring s 0 (match-beginning 0)))
  22431. s (substring s (match-end 0)))
  22432. (setq r (concat
  22433. r (substring s 0 (match-beginning 0))
  22434. (if (match-end 1)
  22435. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  22436. (match-string 1 s)))
  22437. (format " @<span class=\"timestamp\">%s@</span>"
  22438. (substring
  22439. (org-translate-time (match-string 3 s)) 1 -1)))
  22440. s (substring s (match-end 0)))))
  22441. ;; Line break if line started and ended with time stamp stuff
  22442. (if (not r)
  22443. s
  22444. (setq r (concat r s))
  22445. (unless (string-match "\\S-" (concat b s))
  22446. (setq r (concat r "@<br/>")))
  22447. r))))
  22448. (defun org-html-protect (s)
  22449. ;; convert & to &amp;, < to &lt; and > to &gt;
  22450. (let ((start 0))
  22451. (while (string-match "&" s start)
  22452. (setq s (replace-match "&amp;" t t s)
  22453. start (1+ (match-beginning 0))))
  22454. (while (string-match "<" s)
  22455. (setq s (replace-match "&lt;" t t s)))
  22456. (while (string-match ">" s)
  22457. (setq s (replace-match "&gt;" t t s))))
  22458. s)
  22459. (defun org-export-cleanup-toc-line (s)
  22460. "Remove tags and time staps from lines going into the toc."
  22461. (when (memq org-export-with-tags '(not-in-toc nil))
  22462. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  22463. (setq s (replace-match "" t t s))))
  22464. (when org-export-remove-timestamps-from-toc
  22465. (while (string-match org-maybe-keyword-time-regexp s)
  22466. (setq s (replace-match "" t t s))))
  22467. (while (string-match org-bracket-link-regexp s)
  22468. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  22469. t t s)))
  22470. s)
  22471. (defun org-html-expand (string)
  22472. "Prepare STRING for HTML export. Applies all active conversions.
  22473. If there are links in the string, don't modify these."
  22474. (let* ((re (concat org-bracket-link-regexp "\\|"
  22475. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  22476. m s l res)
  22477. (while (setq m (string-match re string))
  22478. (setq s (substring string 0 m)
  22479. l (match-string 0 string)
  22480. string (substring string (match-end 0)))
  22481. (push (org-html-do-expand s) res)
  22482. (push l res))
  22483. (push (org-html-do-expand string) res)
  22484. (apply 'concat (nreverse res))))
  22485. (defun org-html-do-expand (s)
  22486. "Apply all active conversions to translate special ASCII to HTML."
  22487. (setq s (org-html-protect s))
  22488. (if org-export-html-expand
  22489. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  22490. (setq s (replace-match "<\\1>" t nil s))))
  22491. (if org-export-with-emphasize
  22492. (setq s (org-export-html-convert-emphasize s)))
  22493. (if org-export-with-sub-superscripts
  22494. (setq s (org-export-html-convert-sub-super s)))
  22495. (if org-export-with-TeX-macros
  22496. (let ((start 0) wd ass)
  22497. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  22498. (setq wd (match-string 1 s))
  22499. (if (setq ass (assoc wd org-html-entities))
  22500. (setq s (replace-match (or (cdr ass)
  22501. (concat "&" (car ass) ";"))
  22502. t t s))
  22503. (setq start (+ start (length wd)))))))
  22504. s)
  22505. (defun org-create-multibrace-regexp (left right n)
  22506. "Create a regular expression which will match a balanced sexp.
  22507. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  22508. as single character strings.
  22509. The regexp returned will match the entire expression including the
  22510. delimiters. It will also define a single group which contains the
  22511. match except for the outermost delimiters. The maximum depth of
  22512. stacked delimiters is N. Escaping delimiters is not possible."
  22513. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  22514. (or "\\|")
  22515. (re nothing)
  22516. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  22517. (while (> n 1)
  22518. (setq n (1- n)
  22519. re (concat re or next)
  22520. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  22521. (concat left "\\(" re "\\)" right)))
  22522. (defvar org-match-substring-regexp
  22523. (concat
  22524. "\\([^\\]\\)\\([_^]\\)\\("
  22525. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  22526. "\\|"
  22527. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  22528. "\\|"
  22529. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  22530. "The regular expression matching a sub- or superscript.")
  22531. ;(let ((s "a\\_b"))
  22532. ; (and (string-match org-match-substring-regexp s)
  22533. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  22534. (defun org-export-html-convert-sub-super (string)
  22535. "Convert sub- and superscripts in STRING to HTML."
  22536. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  22537. (while (string-match org-match-substring-regexp string s)
  22538. (if (and requireb (match-end 8))
  22539. (setq s (match-end 2))
  22540. (setq s (match-end 1)
  22541. key (if (string= (match-string 2 string) "_") "sub" "sup")
  22542. c (or (match-string 8 string)
  22543. (match-string 6 string)
  22544. (match-string 5 string))
  22545. string (replace-match
  22546. (concat (match-string 1 string)
  22547. "<" key ">" c "</" key ">")
  22548. t t string))))
  22549. (while (string-match "\\\\\\([_^]\\)" string)
  22550. (setq string (replace-match (match-string 1 string) t t string)))
  22551. string))
  22552. (defun org-export-html-convert-emphasize (string)
  22553. "Apply emphasis."
  22554. (let ((s 0))
  22555. (while (string-match org-emph-re string s)
  22556. (if (not (equal
  22557. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  22558. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  22559. (setq string (replace-match
  22560. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  22561. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  22562. "\\5") t nil string))
  22563. (setq s (1+ s))))
  22564. string))
  22565. (defvar org-par-open nil)
  22566. (defun org-open-par ()
  22567. "Insert <p>, but first close previous paragraph if any."
  22568. (org-close-par-maybe)
  22569. (insert "\n<p>")
  22570. (setq org-par-open t))
  22571. (defun org-close-par-maybe ()
  22572. "Close paragraph if there is one open."
  22573. (when org-par-open
  22574. (insert "</p>")
  22575. (setq org-par-open nil)))
  22576. (defun org-close-li ()
  22577. "Close <li> if necessary."
  22578. (org-close-par-maybe)
  22579. (insert "</li>\n"))
  22580. (defvar body-only) ; dynamically scoped into this.
  22581. (defun org-html-level-start (level title umax with-toc head-count)
  22582. "Insert a new level in HTML export.
  22583. When TITLE is nil, just close all open levels."
  22584. (org-close-par-maybe)
  22585. (let ((l org-level-max))
  22586. (while (>= l (1+ level))
  22587. (if (aref org-levels-open (1- l))
  22588. (progn
  22589. (org-html-level-close l umax)
  22590. (aset org-levels-open (1- l) nil)))
  22591. (setq l (1- l)))
  22592. (when title
  22593. ;; If title is nil, this means this function is called to close
  22594. ;; all levels, so the rest is done only if title is given
  22595. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22596. (setq title (replace-match
  22597. (if org-export-with-tags
  22598. (save-match-data
  22599. (concat
  22600. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  22601. (mapconcat 'identity (org-split-string
  22602. (match-string 1 title) ":")
  22603. "&nbsp;")
  22604. "</span>"))
  22605. "")
  22606. t t title)))
  22607. (if (> level umax)
  22608. (progn
  22609. (if (aref org-levels-open (1- level))
  22610. (progn
  22611. (org-close-li)
  22612. (insert "<li>" title "<br/>\n"))
  22613. (aset org-levels-open (1- level) t)
  22614. (org-close-par-maybe)
  22615. (insert "<ul>\n<li>" title "<br/>\n")))
  22616. (aset org-levels-open (1- level) t)
  22617. (if (and org-export-with-section-numbers (not body-only))
  22618. (setq title (concat (org-section-number level) " " title)))
  22619. (setq level (+ level org-export-html-toplevel-hlevel -1))
  22620. (if with-toc
  22621. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  22622. level level head-count title level))
  22623. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  22624. (org-open-par)))))
  22625. (defun org-html-level-close (level max-outline-level)
  22626. "Terminate one level in HTML export."
  22627. (if (<= level max-outline-level)
  22628. (insert "</div>\n")
  22629. (org-close-li)
  22630. (insert "</ul>\n")))
  22631. ;;; iCalendar export
  22632. ;;;###autoload
  22633. (defun org-export-icalendar-this-file ()
  22634. "Export current file as an iCalendar file.
  22635. The iCalendar file will be located in the same directory as the Org-mode
  22636. file, but with extension `.ics'."
  22637. (interactive)
  22638. (org-export-icalendar nil buffer-file-name))
  22639. ;;;###autoload
  22640. (defun org-export-icalendar-all-agenda-files ()
  22641. "Export all files in `org-agenda-files' to iCalendar .ics files.
  22642. Each iCalendar file will be located in the same directory as the Org-mode
  22643. file, but with extension `.ics'."
  22644. (interactive)
  22645. (apply 'org-export-icalendar nil (org-agenda-files t)))
  22646. ;;;###autoload
  22647. (defun org-export-icalendar-combine-agenda-files ()
  22648. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  22649. The file is stored under the name `org-combined-agenda-icalendar-file'."
  22650. (interactive)
  22651. (apply 'org-export-icalendar t (org-agenda-files t)))
  22652. (defun org-export-icalendar (combine &rest files)
  22653. "Create iCalendar files for all elements of FILES.
  22654. If COMBINE is non-nil, combine all calendar entries into a single large
  22655. file and store it under the name `org-combined-agenda-icalendar-file'."
  22656. (save-excursion
  22657. (org-prepare-agenda-buffers files)
  22658. (let* ((dir (org-export-directory
  22659. :ical (list :publishing-directory
  22660. org-export-publishing-directory)))
  22661. file ical-file ical-buffer category started org-agenda-new-buffers)
  22662. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  22663. (when combine
  22664. (setq ical-file
  22665. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  22666. org-combined-agenda-icalendar-file
  22667. (expand-file-name org-combined-agenda-icalendar-file dir))
  22668. ical-buffer (org-get-agenda-file-buffer ical-file))
  22669. (set-buffer ical-buffer) (erase-buffer))
  22670. (while (setq file (pop files))
  22671. (catch 'nextfile
  22672. (org-check-agenda-file file)
  22673. (set-buffer (org-get-agenda-file-buffer file))
  22674. (unless combine
  22675. (setq ical-file (concat (file-name-as-directory dir)
  22676. (file-name-sans-extension
  22677. (file-name-nondirectory buffer-file-name))
  22678. ".ics"))
  22679. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  22680. (with-current-buffer ical-buffer (erase-buffer)))
  22681. (setq category (or org-category
  22682. (file-name-sans-extension
  22683. (file-name-nondirectory buffer-file-name))))
  22684. (if (symbolp category) (setq category (symbol-name category)))
  22685. (let ((standard-output ical-buffer))
  22686. (if combine
  22687. (and (not started) (setq started t)
  22688. (org-start-icalendar-file org-icalendar-combined-name))
  22689. (org-start-icalendar-file category))
  22690. (org-print-icalendar-entries combine)
  22691. (when (or (and combine (not files)) (not combine))
  22692. (org-finish-icalendar-file)
  22693. (set-buffer ical-buffer)
  22694. (save-buffer)
  22695. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  22696. (org-release-buffers org-agenda-new-buffers))))
  22697. (defvar org-after-save-iCalendar-file-hook nil
  22698. "Hook run after an iCalendar file has been saved.
  22699. The iCalendar buffer is still current when this hook is run.
  22700. A good way to use this is to tell a desktop calenndar application to re-read
  22701. the iCalendar file.")
  22702. (defun org-print-icalendar-entries (&optional combine)
  22703. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  22704. When COMBINE is non nil, add the category to each line."
  22705. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  22706. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  22707. (dts (org-ical-ts-to-string
  22708. (format-time-string (cdr org-time-stamp-formats) (current-time))
  22709. "DTSTART"))
  22710. hd ts ts2 state status (inc t) pos b sexp rrule
  22711. scheduledp deadlinep tmp pri category entry location summary desc
  22712. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  22713. (org-refresh-category-properties)
  22714. (save-excursion
  22715. (goto-char (point-min))
  22716. (while (re-search-forward re1 nil t)
  22717. (catch :skip
  22718. (org-agenda-skip)
  22719. (setq pos (match-beginning 0)
  22720. ts (match-string 0)
  22721. inc t
  22722. hd (org-get-heading)
  22723. summary (org-entry-get nil "SUMMARY")
  22724. desc (or (org-entry-get nil "DESCRIPTION")
  22725. (org-get-cleaned-entry org-icalendar-include-body))
  22726. location (org-entry-get nil "LOCATION")
  22727. category (org-get-category))
  22728. (if (looking-at re2)
  22729. (progn
  22730. (goto-char (match-end 0))
  22731. (setq ts2 (match-string 1) inc nil))
  22732. (setq tmp (buffer-substring (max (point-min)
  22733. (- pos org-ds-keyword-length))
  22734. pos)
  22735. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  22736. (progn
  22737. (setq inc nil)
  22738. (replace-match "\\1" t nil ts))
  22739. ts)
  22740. deadlinep (string-match org-deadline-regexp tmp)
  22741. scheduledp (string-match org-scheduled-regexp tmp)
  22742. ;; donep (org-entry-is-done-p)
  22743. ))
  22744. (if (or (string-match org-tr-regexp hd)
  22745. (string-match org-ts-regexp hd))
  22746. (setq hd (replace-match "" t t hd)))
  22747. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  22748. (setq rrule
  22749. (concat "\nRRULE:FREQ="
  22750. (cdr (assoc
  22751. (match-string 2 ts)
  22752. '(("d" . "DAILY")("w" . "WEEKLY")
  22753. ("m" . "MONTHLY")("y" . "YEARLY"))))
  22754. ";INTERVAL=" (match-string 1 ts)))
  22755. (setq rrule ""))
  22756. (setq summary (or summary hd))
  22757. (if (string-match org-bracket-link-regexp summary)
  22758. (setq summary
  22759. (replace-match (if (match-end 3)
  22760. (match-string 3 summary)
  22761. (match-string 1 summary))
  22762. t t summary)))
  22763. (if deadlinep (setq summary (concat "DL: " summary)))
  22764. (if scheduledp (setq summary (concat "S: " summary)))
  22765. (if (string-match "\\`<%%" ts)
  22766. (with-current-buffer sexp-buffer
  22767. (insert (substring ts 1 -1) " " summary "\n"))
  22768. (princ (format "BEGIN:VEVENT
  22769. %s
  22770. %s%s
  22771. SUMMARY:%s%s%s
  22772. CATEGORIES:%s
  22773. END:VEVENT\n"
  22774. (org-ical-ts-to-string ts "DTSTART")
  22775. (org-ical-ts-to-string ts2 "DTEND" inc)
  22776. rrule summary
  22777. (if (and desc (string-match "\\S-" desc))
  22778. (concat "\nDESCRIPTION: " desc) "")
  22779. (if (and location (string-match "\\S-" location))
  22780. (concat "\nLOCATION: " location) "")
  22781. category)))))
  22782. (when (and org-icalendar-include-sexps
  22783. (condition-case nil (require 'icalendar) (error nil))
  22784. (fboundp 'icalendar-export-region))
  22785. ;; Get all the literal sexps
  22786. (goto-char (point-min))
  22787. (while (re-search-forward "^&?%%(" nil t)
  22788. (catch :skip
  22789. (org-agenda-skip)
  22790. (setq b (match-beginning 0))
  22791. (goto-char (1- (match-end 0)))
  22792. (forward-sexp 1)
  22793. (end-of-line 1)
  22794. (setq sexp (buffer-substring b (point)))
  22795. (with-current-buffer sexp-buffer
  22796. (insert sexp "\n"))
  22797. (princ (org-diary-to-ical-string sexp-buffer)))))
  22798. (when org-icalendar-include-todo
  22799. (goto-char (point-min))
  22800. (while (re-search-forward org-todo-line-regexp nil t)
  22801. (catch :skip
  22802. (org-agenda-skip)
  22803. (setq state (match-string 2))
  22804. (setq status (if (member state org-done-keywords)
  22805. "COMPLETED" "NEEDS-ACTION"))
  22806. (when (and state
  22807. (or (not (member state org-done-keywords))
  22808. (eq org-icalendar-include-todo 'all))
  22809. (not (member org-archive-tag (org-get-tags-at)))
  22810. )
  22811. (setq hd (match-string 3)
  22812. summary (org-entry-get nil "SUMMARY")
  22813. desc (or (org-entry-get nil "DESCRIPTION")
  22814. (org-get-cleaned-entry org-icalendar-include-body))
  22815. location (org-entry-get nil "LOCATION"))
  22816. (if (string-match org-bracket-link-regexp hd)
  22817. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  22818. (match-string 1 hd))
  22819. t t hd)))
  22820. (if (string-match org-priority-regexp hd)
  22821. (setq pri (string-to-char (match-string 2 hd))
  22822. hd (concat (substring hd 0 (match-beginning 1))
  22823. (substring hd (match-end 1))))
  22824. (setq pri org-default-priority))
  22825. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  22826. (- org-lowest-priority org-highest-priority))))))
  22827. (princ (format "BEGIN:VTODO
  22828. %s
  22829. SUMMARY:%s%s%s
  22830. CATEGORIES:%s
  22831. SEQUENCE:1
  22832. PRIORITY:%d
  22833. STATUS:%s
  22834. END:VTODO\n"
  22835. dts
  22836. (or summary hd)
  22837. (if (and location (string-match "\\S-" location))
  22838. (concat "\nLOCATION: " location) "")
  22839. (if (and desc (string-match "\\S-" desc))
  22840. (concat "\nDESCRIPTION: " desc) "")
  22841. category pri status)))))))))
  22842. (defun org-get-cleaned-entry (what)
  22843. "Clean-up description string."
  22844. (when what
  22845. (save-excursion
  22846. (org-back-to-heading t)
  22847. (let ((s (buffer-substring (point-at-bol 2) (org-end-of-subtree t)))
  22848. (re (concat org-drawer-regexp "[^\000]*?:END:.*\n?"))
  22849. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  22850. (while (string-match re s) (setq s (replace-match "" t t s)))
  22851. (while (string-match re2 s) (setq s (replace-match "" t t s)))
  22852. (if (string-match "[ \t\r\n]+\\'" s) (setq s (replace-match "" t t s)))
  22853. (while (string-match "[ \t]*\n[ \t]*" s)
  22854. (setq s (replace-match "\\n" t t s)))
  22855. (setq s (org-trim s))
  22856. (if (and (numberp what)
  22857. (> (length s) what))
  22858. (substring s 0 what)
  22859. s)))))
  22860. (defun org-start-icalendar-file (name)
  22861. "Start an iCalendar file by inserting the header."
  22862. (let ((user user-full-name)
  22863. (name (or name "unknown"))
  22864. (timezone (cadr (current-time-zone))))
  22865. (princ
  22866. (format "BEGIN:VCALENDAR
  22867. VERSION:2.0
  22868. X-WR-CALNAME:%s
  22869. PRODID:-//%s//Emacs with Org-mode//EN
  22870. X-WR-TIMEZONE:%s
  22871. CALSCALE:GREGORIAN\n" name user timezone))))
  22872. (defun org-finish-icalendar-file ()
  22873. "Finish an iCalendar file by inserting the END statement."
  22874. (princ "END:VCALENDAR\n"))
  22875. (defun org-ical-ts-to-string (s keyword &optional inc)
  22876. "Take a time string S and convert it to iCalendar format.
  22877. KEYWORD is added in front, to make a complete line like DTSTART....
  22878. When INC is non-nil, increase the hour by two (if time string contains
  22879. a time), or the day by one (if it does not contain a time)."
  22880. (let ((t1 (org-parse-time-string s 'nodefault))
  22881. t2 fmt have-time time)
  22882. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  22883. (setq t2 t1 have-time t)
  22884. (setq t2 (org-parse-time-string s)))
  22885. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  22886. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  22887. (when inc
  22888. (if have-time
  22889. (if org-agenda-default-appointment-duration
  22890. (setq mi (+ org-agenda-default-appointment-duration mi))
  22891. (setq h (+ 2 h)))
  22892. (setq d (1+ d))))
  22893. (setq time (encode-time s mi h d m y)))
  22894. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  22895. (concat keyword (format-time-string fmt time))))
  22896. ;;; XOXO export
  22897. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  22898. (with-current-buffer buffer
  22899. (apply 'insert output)))
  22900. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  22901. (defun org-export-as-xoxo (&optional buffer)
  22902. "Export the org buffer as XOXO.
  22903. The XOXO buffer is named *xoxo-<source buffer name>*"
  22904. (interactive (list (current-buffer)))
  22905. ;; A quickie abstraction
  22906. ;; Output everything as XOXO
  22907. (with-current-buffer (get-buffer buffer)
  22908. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  22909. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22910. (org-infile-export-plist)))
  22911. (filename (concat (file-name-as-directory
  22912. (org-export-directory :xoxo opt-plist))
  22913. (file-name-sans-extension
  22914. (file-name-nondirectory buffer-file-name))
  22915. ".html"))
  22916. (out (find-file-noselect filename))
  22917. (last-level 1)
  22918. (hanging-li nil))
  22919. ;; Check the output buffer is empty.
  22920. (with-current-buffer out (erase-buffer))
  22921. ;; Kick off the output
  22922. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  22923. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  22924. (let* ((hd (match-string-no-properties 1))
  22925. (level (length hd))
  22926. (text (concat
  22927. (match-string-no-properties 2)
  22928. (save-excursion
  22929. (goto-char (match-end 0))
  22930. (let ((str ""))
  22931. (catch 'loop
  22932. (while 't
  22933. (forward-line)
  22934. (if (looking-at "^[ \t]\\(.*\\)")
  22935. (setq str (concat str (match-string-no-properties 1)))
  22936. (throw 'loop str)))))))))
  22937. ;; Handle level rendering
  22938. (cond
  22939. ((> level last-level)
  22940. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  22941. ((< level last-level)
  22942. (dotimes (- (- last-level level) 1)
  22943. (if hanging-li
  22944. (org-export-as-xoxo-insert-into out "</li>\n"))
  22945. (org-export-as-xoxo-insert-into out "</ol>\n"))
  22946. (when hanging-li
  22947. (org-export-as-xoxo-insert-into out "</li>\n")
  22948. (setq hanging-li nil)))
  22949. ((equal level last-level)
  22950. (if hanging-li
  22951. (org-export-as-xoxo-insert-into out "</li>\n")))
  22952. )
  22953. (setq last-level level)
  22954. ;; And output the new li
  22955. (setq hanging-li 't)
  22956. (if (equal ?+ (elt text 0))
  22957. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  22958. (org-export-as-xoxo-insert-into out "<li>" text))))
  22959. ;; Finally finish off the ol
  22960. (dotimes (- last-level 1)
  22961. (if hanging-li
  22962. (org-export-as-xoxo-insert-into out "</li>\n"))
  22963. (org-export-as-xoxo-insert-into out "</ol>\n"))
  22964. ;; Finish the buffer off and clean it up.
  22965. (switch-to-buffer-other-window out)
  22966. (indent-region (point-min) (point-max) nil)
  22967. (save-buffer)
  22968. (goto-char (point-min))
  22969. )))
  22970. ;;;; Key bindings
  22971. ;; Make `C-c C-x' a prefix key
  22972. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  22973. ;; TAB key with modifiers
  22974. (org-defkey org-mode-map "\C-i" 'org-cycle)
  22975. (org-defkey org-mode-map [(tab)] 'org-cycle)
  22976. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  22977. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  22978. (org-defkey org-mode-map "\M-\t" 'org-complete)
  22979. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  22980. ;; The following line is necessary under Suse GNU/Linux
  22981. (unless (featurep 'xemacs)
  22982. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  22983. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  22984. (define-key org-mode-map [backtab] 'org-shifttab)
  22985. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  22986. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  22987. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  22988. ;; Cursor keys with modifiers
  22989. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  22990. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  22991. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  22992. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  22993. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  22994. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  22995. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  22996. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  22997. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  22998. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  22999. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  23000. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  23001. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  23002. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  23003. ;;; Extra keys for tty access.
  23004. ;; We only set them when really needed because otherwise the
  23005. ;; menus don't show the simple keys
  23006. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  23007. (not window-system))
  23008. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  23009. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  23010. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  23011. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  23012. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  23013. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  23014. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  23015. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  23016. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  23017. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  23018. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  23019. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  23020. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  23021. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  23022. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  23023. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  23024. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  23025. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  23026. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  23027. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  23028. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  23029. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  23030. ;; All the other keys
  23031. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  23032. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  23033. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  23034. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  23035. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  23036. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  23037. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  23038. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  23039. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  23040. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  23041. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  23042. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  23043. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  23044. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  23045. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  23046. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  23047. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  23048. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  23049. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  23050. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  23051. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  23052. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  23053. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  23054. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  23055. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  23056. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  23057. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  23058. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  23059. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  23060. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  23061. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  23062. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  23063. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  23064. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  23065. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  23066. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  23067. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  23068. (org-defkey org-mode-map "\C-c^" 'org-sort)
  23069. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  23070. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  23071. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  23072. (org-defkey org-mode-map "\C-m" 'org-return)
  23073. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  23074. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  23075. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  23076. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  23077. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  23078. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  23079. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  23080. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  23081. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  23082. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  23083. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  23084. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  23085. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  23086. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  23087. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  23088. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  23089. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  23090. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  23091. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  23092. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  23093. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  23094. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  23095. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  23096. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  23097. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  23098. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  23099. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  23100. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  23101. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  23102. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  23103. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  23104. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  23105. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  23106. (when (featurep 'xemacs)
  23107. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  23108. (defsubst org-table-p () (org-at-table-p))
  23109. (defun org-self-insert-command (N)
  23110. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  23111. If the cursor is in a table looking at whitespace, the whitespace is
  23112. overwritten, and the table is not marked as requiring realignment."
  23113. (interactive "p")
  23114. (if (and (org-table-p)
  23115. (progn
  23116. ;; check if we blank the field, and if that triggers align
  23117. (and org-table-auto-blank-field
  23118. (member last-command
  23119. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  23120. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  23121. ;; got extra space, this field does not determine column width
  23122. (let (org-table-may-need-update) (org-table-blank-field))
  23123. ;; no extra space, this field may determine column width
  23124. (org-table-blank-field)))
  23125. t)
  23126. (eq N 1)
  23127. (looking-at "[^|\n]* |"))
  23128. (let (org-table-may-need-update)
  23129. (goto-char (1- (match-end 0)))
  23130. (delete-backward-char 1)
  23131. (goto-char (match-beginning 0))
  23132. (self-insert-command N))
  23133. (setq org-table-may-need-update t)
  23134. (self-insert-command N)
  23135. (org-fix-tags-on-the-fly)))
  23136. (defun org-fix-tags-on-the-fly ()
  23137. (when (and (equal (char-after (point-at-bol)) ?*)
  23138. (org-on-heading-p))
  23139. (org-align-tags-here org-tags-column)))
  23140. (defun org-delete-backward-char (N)
  23141. "Like `delete-backward-char', insert whitespace at field end in tables.
  23142. When deleting backwards, in tables this function will insert whitespace in
  23143. front of the next \"|\" separator, to keep the table aligned. The table will
  23144. still be marked for re-alignment if the field did fill the entire column,
  23145. because, in this case the deletion might narrow the column."
  23146. (interactive "p")
  23147. (if (and (org-table-p)
  23148. (eq N 1)
  23149. (string-match "|" (buffer-substring (point-at-bol) (point)))
  23150. (looking-at ".*?|"))
  23151. (let ((pos (point))
  23152. (noalign (looking-at "[^|\n\r]* |"))
  23153. (c org-table-may-need-update))
  23154. (backward-delete-char N)
  23155. (skip-chars-forward "^|")
  23156. (insert " ")
  23157. (goto-char (1- pos))
  23158. ;; noalign: if there were two spaces at the end, this field
  23159. ;; does not determine the width of the column.
  23160. (if noalign (setq org-table-may-need-update c)))
  23161. (backward-delete-char N)
  23162. (org-fix-tags-on-the-fly)))
  23163. (defun org-delete-char (N)
  23164. "Like `delete-char', but insert whitespace at field end in tables.
  23165. When deleting characters, in tables this function will insert whitespace in
  23166. front of the next \"|\" separator, to keep the table aligned. The table will
  23167. still be marked for re-alignment if the field did fill the entire column,
  23168. because, in this case the deletion might narrow the column."
  23169. (interactive "p")
  23170. (if (and (org-table-p)
  23171. (not (bolp))
  23172. (not (= (char-after) ?|))
  23173. (eq N 1))
  23174. (if (looking-at ".*?|")
  23175. (let ((pos (point))
  23176. (noalign (looking-at "[^|\n\r]* |"))
  23177. (c org-table-may-need-update))
  23178. (replace-match (concat
  23179. (substring (match-string 0) 1 -1)
  23180. " |"))
  23181. (goto-char pos)
  23182. ;; noalign: if there were two spaces at the end, this field
  23183. ;; does not determine the width of the column.
  23184. (if noalign (setq org-table-may-need-update c)))
  23185. (delete-char N))
  23186. (delete-char N)
  23187. (org-fix-tags-on-the-fly)))
  23188. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  23189. (put 'org-self-insert-command 'delete-selection t)
  23190. (put 'orgtbl-self-insert-command 'delete-selection t)
  23191. (put 'org-delete-char 'delete-selection 'supersede)
  23192. (put 'org-delete-backward-char 'delete-selection 'supersede)
  23193. ;; Make `flyspell-mode' delay after some commands
  23194. (put 'org-self-insert-command 'flyspell-delayed t)
  23195. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  23196. (put 'org-delete-char 'flyspell-delayed t)
  23197. (put 'org-delete-backward-char 'flyspell-delayed t)
  23198. (eval-after-load "pabbrev"
  23199. '(progn
  23200. (add-to-list 'pabbrev-expand-after-command-list
  23201. 'orgtbl-self-insert-command t)
  23202. (add-to-list 'pabbrev-expand-after-command-list
  23203. 'org-self-insert-command t)))
  23204. ;; How to do this: Measure non-white length of current string
  23205. ;; If equal to column width, we should realign.
  23206. (defun org-remap (map &rest commands)
  23207. "In MAP, remap the functions given in COMMANDS.
  23208. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  23209. (let (new old)
  23210. (while commands
  23211. (setq old (pop commands) new (pop commands))
  23212. (if (fboundp 'command-remapping)
  23213. (org-defkey map (vector 'remap old) new)
  23214. (substitute-key-definition old new map global-map)))))
  23215. (when (eq org-enable-table-editor 'optimized)
  23216. ;; If the user wants maximum table support, we need to hijack
  23217. ;; some standard editing functions
  23218. (org-remap org-mode-map
  23219. 'self-insert-command 'org-self-insert-command
  23220. 'delete-char 'org-delete-char
  23221. 'delete-backward-char 'org-delete-backward-char)
  23222. (org-defkey org-mode-map "|" 'org-force-self-insert))
  23223. (defun org-shiftcursor-error ()
  23224. "Throw an error because Shift-Cursor command was applied in wrong context."
  23225. (error "This command is active in special context like tables, headlines or timestamps"))
  23226. (defun org-shifttab (&optional arg)
  23227. "Global visibility cycling or move to previous table field.
  23228. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  23229. on context.
  23230. See the individual commands for more information."
  23231. (interactive "P")
  23232. (cond
  23233. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  23234. (arg (message "Content view to level: ")
  23235. (org-content (prefix-numeric-value arg))
  23236. (setq org-cycle-global-status 'overview))
  23237. (t (call-interactively 'org-global-cycle))))
  23238. (defun org-shiftmetaleft ()
  23239. "Promote subtree or delete table column.
  23240. Calls `org-promote-subtree', `org-outdent-item',
  23241. or `org-table-delete-column', depending on context.
  23242. See the individual commands for more information."
  23243. (interactive)
  23244. (cond
  23245. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  23246. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  23247. ((org-at-item-p) (call-interactively 'org-outdent-item))
  23248. (t (org-shiftcursor-error))))
  23249. (defun org-shiftmetaright ()
  23250. "Demote subtree or insert table column.
  23251. Calls `org-demote-subtree', `org-indent-item',
  23252. or `org-table-insert-column', depending on context.
  23253. See the individual commands for more information."
  23254. (interactive)
  23255. (cond
  23256. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  23257. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  23258. ((org-at-item-p) (call-interactively 'org-indent-item))
  23259. (t (org-shiftcursor-error))))
  23260. (defun org-shiftmetaup (&optional arg)
  23261. "Move subtree up or kill table row.
  23262. Calls `org-move-subtree-up' or `org-table-kill-row' or
  23263. `org-move-item-up' depending on context. See the individual commands
  23264. for more information."
  23265. (interactive "P")
  23266. (cond
  23267. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  23268. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  23269. ((org-at-item-p) (call-interactively 'org-move-item-up))
  23270. (t (org-shiftcursor-error))))
  23271. (defun org-shiftmetadown (&optional arg)
  23272. "Move subtree down or insert table row.
  23273. Calls `org-move-subtree-down' or `org-table-insert-row' or
  23274. `org-move-item-down', depending on context. See the individual
  23275. commands for more information."
  23276. (interactive "P")
  23277. (cond
  23278. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  23279. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  23280. ((org-at-item-p) (call-interactively 'org-move-item-down))
  23281. (t (org-shiftcursor-error))))
  23282. (defun org-metaleft (&optional arg)
  23283. "Promote heading or move table column to left.
  23284. Calls `org-do-promote' or `org-table-move-column', depending on context.
  23285. With no specific context, calls the Emacs default `backward-word'.
  23286. See the individual commands for more information."
  23287. (interactive "P")
  23288. (cond
  23289. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  23290. ((or (org-on-heading-p) (org-region-active-p))
  23291. (call-interactively 'org-do-promote))
  23292. ((org-at-item-p) (call-interactively 'org-outdent-item))
  23293. (t (call-interactively 'backward-word))))
  23294. (defun org-metaright (&optional arg)
  23295. "Demote subtree or move table column to right.
  23296. Calls `org-do-demote' or `org-table-move-column', depending on context.
  23297. With no specific context, calls the Emacs default `forward-word'.
  23298. See the individual commands for more information."
  23299. (interactive "P")
  23300. (cond
  23301. ((org-at-table-p) (call-interactively 'org-table-move-column))
  23302. ((or (org-on-heading-p) (org-region-active-p))
  23303. (call-interactively 'org-do-demote))
  23304. ((org-at-item-p) (call-interactively 'org-indent-item))
  23305. (t (call-interactively 'forward-word))))
  23306. (defun org-metaup (&optional arg)
  23307. "Move subtree up or move table row up.
  23308. Calls `org-move-subtree-up' or `org-table-move-row' or
  23309. `org-move-item-up', depending on context. See the individual commands
  23310. for more information."
  23311. (interactive "P")
  23312. (cond
  23313. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  23314. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  23315. ((org-at-item-p) (call-interactively 'org-move-item-up))
  23316. (t (transpose-lines 1) (beginning-of-line -1))))
  23317. (defun org-metadown (&optional arg)
  23318. "Move subtree down or move table row down.
  23319. Calls `org-move-subtree-down' or `org-table-move-row' or
  23320. `org-move-item-down', depending on context. See the individual
  23321. commands for more information."
  23322. (interactive "P")
  23323. (cond
  23324. ((org-at-table-p) (call-interactively 'org-table-move-row))
  23325. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  23326. ((org-at-item-p) (call-interactively 'org-move-item-down))
  23327. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  23328. (defun org-shiftup (&optional arg)
  23329. "Increase item in timestamp or increase priority of current headline.
  23330. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  23331. depending on context. See the individual commands for more information."
  23332. (interactive "P")
  23333. (cond
  23334. ((org-at-timestamp-p t)
  23335. (call-interactively (if org-edit-timestamp-down-means-later
  23336. 'org-timestamp-down 'org-timestamp-up)))
  23337. ((org-on-heading-p) (call-interactively 'org-priority-up))
  23338. ((org-at-item-p) (call-interactively 'org-previous-item))
  23339. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  23340. (defun org-shiftdown (&optional arg)
  23341. "Decrease item in timestamp or decrease priority of current headline.
  23342. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  23343. depending on context. See the individual commands for more information."
  23344. (interactive "P")
  23345. (cond
  23346. ((org-at-timestamp-p t)
  23347. (call-interactively (if org-edit-timestamp-down-means-later
  23348. 'org-timestamp-up 'org-timestamp-down)))
  23349. ((org-on-heading-p) (call-interactively 'org-priority-down))
  23350. (t (call-interactively 'org-next-item))))
  23351. (defun org-shiftright ()
  23352. "Next TODO keyword or timestamp one day later, depending on context."
  23353. (interactive)
  23354. (cond
  23355. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  23356. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  23357. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  23358. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  23359. (t (org-shiftcursor-error))))
  23360. (defun org-shiftleft ()
  23361. "Previous TODO keyword or timestamp one day earlier, depending on context."
  23362. (interactive)
  23363. (cond
  23364. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  23365. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  23366. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  23367. ((org-at-property-p)
  23368. (call-interactively 'org-property-previous-allowed-value))
  23369. (t (org-shiftcursor-error))))
  23370. (defun org-shiftcontrolright ()
  23371. "Switch to next TODO set."
  23372. (interactive)
  23373. (cond
  23374. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  23375. (t (org-shiftcursor-error))))
  23376. (defun org-shiftcontrolleft ()
  23377. "Switch to previous TODO set."
  23378. (interactive)
  23379. (cond
  23380. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  23381. (t (org-shiftcursor-error))))
  23382. (defun org-ctrl-c-ret ()
  23383. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  23384. (interactive)
  23385. (cond
  23386. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  23387. (t (call-interactively 'org-insert-heading))))
  23388. (defun org-copy-special ()
  23389. "Copy region in table or copy current subtree.
  23390. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  23391. See the individual commands for more information."
  23392. (interactive)
  23393. (call-interactively
  23394. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  23395. (defun org-cut-special ()
  23396. "Cut region in table or cut current subtree.
  23397. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  23398. See the individual commands for more information."
  23399. (interactive)
  23400. (call-interactively
  23401. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  23402. (defun org-paste-special (arg)
  23403. "Paste rectangular region into table, or past subtree relative to level.
  23404. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  23405. See the individual commands for more information."
  23406. (interactive "P")
  23407. (if (org-at-table-p)
  23408. (org-table-paste-rectangle)
  23409. (org-paste-subtree arg)))
  23410. (defun org-ctrl-c-ctrl-c (&optional arg)
  23411. "Set tags in headline, or update according to changed information at point.
  23412. This command does many different things, depending on context:
  23413. - If the cursor is in a headline, prompt for tags and insert them
  23414. into the current line, aligned to `org-tags-column'. When called
  23415. with prefix arg, realign all tags in the current buffer.
  23416. - If the cursor is in one of the special #+KEYWORD lines, this
  23417. triggers scanning the buffer for these lines and updating the
  23418. information.
  23419. - If the cursor is inside a table, realign the table. This command
  23420. works even if the automatic table editor has been turned off.
  23421. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  23422. the entire table.
  23423. - If the cursor is a the beginning of a dynamic block, update it.
  23424. - If the cursor is inside a table created by the table.el package,
  23425. activate that table.
  23426. - If the current buffer is a remember buffer, close note and file it.
  23427. with a prefix argument, file it without further interaction to the default
  23428. location.
  23429. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  23430. links in this buffer.
  23431. - If the cursor is on a numbered item in a plain list, renumber the
  23432. ordered list."
  23433. (interactive "P")
  23434. (let ((org-enable-table-editor t))
  23435. (cond
  23436. ((or org-clock-overlays
  23437. org-occur-highlights
  23438. org-latex-fragment-image-overlays)
  23439. (org-remove-clock-overlays)
  23440. (org-remove-occur-highlights)
  23441. (org-remove-latex-fragment-image-overlays)
  23442. (message "Temporary highlights/overlays removed from current buffer"))
  23443. ((and (local-variable-p 'org-finish-function (current-buffer))
  23444. (fboundp org-finish-function))
  23445. (funcall org-finish-function))
  23446. ((org-at-property-p)
  23447. (call-interactively 'org-property-action))
  23448. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  23449. ((org-on-heading-p) (call-interactively 'org-set-tags))
  23450. ((org-at-table.el-p)
  23451. (require 'table)
  23452. (beginning-of-line 1)
  23453. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  23454. (call-interactively 'table-recognize-table))
  23455. ((org-at-table-p)
  23456. (org-table-maybe-eval-formula)
  23457. (if arg
  23458. (call-interactively 'org-table-recalculate)
  23459. (org-table-maybe-recalculate-line))
  23460. (call-interactively 'org-table-align))
  23461. ((org-at-item-checkbox-p)
  23462. (call-interactively 'org-toggle-checkbox))
  23463. ((org-at-item-p)
  23464. (call-interactively 'org-maybe-renumber-ordered-list))
  23465. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  23466. ;; Dynamic block
  23467. (beginning-of-line 1)
  23468. (org-update-dblock))
  23469. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  23470. (cond
  23471. ((equal (match-string 1) "TBLFM")
  23472. ;; Recalculate the table before this line
  23473. (save-excursion
  23474. (beginning-of-line 1)
  23475. (skip-chars-backward " \r\n\t")
  23476. (if (org-at-table-p)
  23477. (org-call-with-arg 'org-table-recalculate t))))
  23478. (t
  23479. (call-interactively 'org-mode-restart))))
  23480. (t (error "C-c C-c can do nothing useful at this location.")))))
  23481. (defun org-mode-restart ()
  23482. "Restart Org-mode, to scan again for special lines.
  23483. Also updates the keyword regular expressions."
  23484. (interactive)
  23485. (let ((org-inhibit-startup t)) (org-mode))
  23486. (message "Org-mode restarted to refresh keyword and special line setup"))
  23487. (defun org-kill-note-or-show-branches ()
  23488. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  23489. (interactive)
  23490. (if (not org-finish-function)
  23491. (call-interactively 'show-branches)
  23492. (let ((org-note-abort t))
  23493. (funcall org-finish-function))))
  23494. (defun org-return ()
  23495. "Goto next table row or insert a newline.
  23496. Calls `org-table-next-row' or `newline', depending on context.
  23497. See the individual commands for more information."
  23498. (interactive)
  23499. (cond
  23500. ((bobp) (newline))
  23501. ((org-at-table-p)
  23502. (org-table-justify-field-maybe)
  23503. (call-interactively 'org-table-next-row))
  23504. (t (newline))))
  23505. (defun org-ctrl-c-minus ()
  23506. "Insert separator line in table or modify bullet type in list.
  23507. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  23508. depending on context."
  23509. (interactive)
  23510. (cond
  23511. ((org-at-table-p)
  23512. (call-interactively 'org-table-insert-hline))
  23513. ((org-on-heading-p)
  23514. ;; Convert to item
  23515. (save-excursion
  23516. (beginning-of-line 1)
  23517. (if (looking-at "\\*+ ")
  23518. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  23519. ((org-in-item-p)
  23520. (call-interactively 'org-cycle-list-bullet))
  23521. (t (error "`C-c -' does have no function here."))))
  23522. (defun org-meta-return (&optional arg)
  23523. "Insert a new heading or wrap a region in a table.
  23524. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  23525. See the individual commands for more information."
  23526. (interactive "P")
  23527. (cond
  23528. ((org-at-table-p)
  23529. (call-interactively 'org-table-wrap-region))
  23530. (t (call-interactively 'org-insert-heading))))
  23531. ;;; Menu entries
  23532. ;; Define the Org-mode menus
  23533. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  23534. '("Tbl"
  23535. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  23536. ["Next Field" org-cycle (org-at-table-p)]
  23537. ["Previous Field" org-shifttab (org-at-table-p)]
  23538. ["Next Row" org-return (org-at-table-p)]
  23539. "--"
  23540. ["Blank Field" org-table-blank-field (org-at-table-p)]
  23541. ["Edit Field" org-table-edit-field (org-at-table-p)]
  23542. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  23543. "--"
  23544. ("Column"
  23545. ["Move Column Left" org-metaleft (org-at-table-p)]
  23546. ["Move Column Right" org-metaright (org-at-table-p)]
  23547. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  23548. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  23549. ("Row"
  23550. ["Move Row Up" org-metaup (org-at-table-p)]
  23551. ["Move Row Down" org-metadown (org-at-table-p)]
  23552. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  23553. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  23554. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  23555. "--"
  23556. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  23557. ("Rectangle"
  23558. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  23559. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  23560. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  23561. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  23562. "--"
  23563. ("Calculate"
  23564. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  23565. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  23566. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  23567. "--"
  23568. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  23569. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  23570. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  23571. "--"
  23572. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  23573. "--"
  23574. ["Sum Column/Rectangle" org-table-sum
  23575. (or (org-at-table-p) (org-region-active-p))]
  23576. ["Which Column?" org-table-current-column (org-at-table-p)])
  23577. ["Debug Formulas"
  23578. org-table-toggle-formula-debugger
  23579. :style toggle :selected org-table-formula-debug]
  23580. ["Show Col/Row Numbers"
  23581. org-table-toggle-coordinate-overlays
  23582. :style toggle :selected org-table-overlay-coordinates]
  23583. "--"
  23584. ["Create" org-table-create (and (not (org-at-table-p))
  23585. org-enable-table-editor)]
  23586. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  23587. ["Import from File" org-table-import (not (org-at-table-p))]
  23588. ["Export to File" org-table-export (org-at-table-p)]
  23589. "--"
  23590. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  23591. (easy-menu-define org-org-menu org-mode-map "Org menu"
  23592. '("Org"
  23593. ("Show/Hide"
  23594. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  23595. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  23596. ["Sparse Tree" org-occur t]
  23597. ["Reveal Context" org-reveal t]
  23598. ["Show All" show-all t]
  23599. "--"
  23600. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  23601. "--"
  23602. ["New Heading" org-insert-heading t]
  23603. ("Navigate Headings"
  23604. ["Up" outline-up-heading t]
  23605. ["Next" outline-next-visible-heading t]
  23606. ["Previous" outline-previous-visible-heading t]
  23607. ["Next Same Level" outline-forward-same-level t]
  23608. ["Previous Same Level" outline-backward-same-level t]
  23609. "--"
  23610. ["Jump" org-goto t])
  23611. ("Edit Structure"
  23612. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  23613. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  23614. "--"
  23615. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  23616. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  23617. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  23618. "--"
  23619. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  23620. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  23621. ["Demote Heading" org-metaright (not (org-at-table-p))]
  23622. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  23623. "--"
  23624. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  23625. "--"
  23626. ["Convert to odd levels" org-convert-to-odd-levels t]
  23627. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  23628. ("Editing"
  23629. ["Emphasis..." org-emphasize t])
  23630. ("Archive"
  23631. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  23632. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  23633. ; :active t :keys "C-u C-c C-x C-a"]
  23634. ["Sparse trees open ARCHIVE trees"
  23635. (setq org-sparse-tree-open-archived-trees
  23636. (not org-sparse-tree-open-archived-trees))
  23637. :style toggle :selected org-sparse-tree-open-archived-trees]
  23638. ["Cycling opens ARCHIVE trees"
  23639. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  23640. :style toggle :selected org-cycle-open-archived-trees]
  23641. ["Agenda includes ARCHIVE trees"
  23642. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  23643. :style toggle :selected (not org-agenda-skip-archived-trees)]
  23644. "--"
  23645. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  23646. ; ["Check and Move Children" (org-archive-subtree '(4))
  23647. ; :active t :keys "C-u C-c C-x C-s"]
  23648. )
  23649. "--"
  23650. ("TODO Lists"
  23651. ["TODO/DONE/-" org-todo t]
  23652. ("Select keyword"
  23653. ["Next keyword" org-shiftright (org-on-heading-p)]
  23654. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  23655. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  23656. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  23657. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  23658. ["Show TODO Tree" org-show-todo-tree t]
  23659. ["Global TODO list" org-todo-list t]
  23660. "--"
  23661. ["Set Priority" org-priority t]
  23662. ["Priority Up" org-shiftup t]
  23663. ["Priority Down" org-shiftdown t])
  23664. ("TAGS and Properties"
  23665. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  23666. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  23667. "--"
  23668. ["Set property" 'org-set-property t]
  23669. ["Column view of properties" org-columns t]
  23670. ["Insert Column View DBlock" org-insert-columns-dblock t])
  23671. ("Dates and Scheduling"
  23672. ["Timestamp" org-time-stamp t]
  23673. ["Timestamp (inactive)" org-time-stamp-inactive t]
  23674. ("Change Date"
  23675. ["1 Day Later" org-shiftright t]
  23676. ["1 Day Earlier" org-shiftleft t]
  23677. ["1 ... Later" org-shiftup t]
  23678. ["1 ... Earlier" org-shiftdown t])
  23679. ["Compute Time Range" org-evaluate-time-range t]
  23680. ["Schedule Item" org-schedule t]
  23681. ["Deadline" org-deadline t]
  23682. "--"
  23683. ["Custom time format" org-toggle-time-stamp-overlays
  23684. :style radio :selected org-display-custom-times]
  23685. "--"
  23686. ["Goto Calendar" org-goto-calendar t]
  23687. ["Date from Calendar" org-date-from-calendar t])
  23688. ("Logging work"
  23689. ["Clock in" org-clock-in t]
  23690. ["Clock out" org-clock-out t]
  23691. ["Clock cancel" org-clock-cancel t]
  23692. ["Goto running clock" org-clock-goto t]
  23693. ["Display times" org-clock-display t]
  23694. ["Create clock table" org-clock-report t]
  23695. "--"
  23696. ["Record DONE time"
  23697. (progn (setq org-log-done (not org-log-done))
  23698. (message "Switching to %s will %s record a timestamp"
  23699. (car org-done-keywords)
  23700. (if org-log-done "automatically" "not")))
  23701. :style toggle :selected org-log-done])
  23702. "--"
  23703. ["Agenda Command..." org-agenda t]
  23704. ("File List for Agenda")
  23705. ("Special views current file"
  23706. ["TODO Tree" org-show-todo-tree t]
  23707. ["Check Deadlines" org-check-deadlines t]
  23708. ["Timeline" org-timeline t]
  23709. ["Tags Tree" org-tags-sparse-tree t])
  23710. "--"
  23711. ("Hyperlinks"
  23712. ["Store Link (Global)" org-store-link t]
  23713. ["Insert Link" org-insert-link t]
  23714. ["Follow Link" org-open-at-point t]
  23715. "--"
  23716. ["Next link" org-next-link t]
  23717. ["Previous link" org-previous-link t]
  23718. "--"
  23719. ["Descriptive Links"
  23720. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  23721. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  23722. ["Literal Links"
  23723. (progn
  23724. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  23725. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  23726. "--"
  23727. ["Export/Publish..." org-export t]
  23728. ("LaTeX"
  23729. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  23730. :selected org-cdlatex-mode]
  23731. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  23732. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  23733. ["Modify math symbol" org-cdlatex-math-modify
  23734. (org-inside-LaTeX-fragment-p)]
  23735. ["Export LaTeX fragments as images"
  23736. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  23737. :style toggle :selected org-export-with-LaTeX-fragments])
  23738. "--"
  23739. ("Documentation"
  23740. ["Show Version" org-version t]
  23741. ["Info Documentation" org-info t])
  23742. ("Customize"
  23743. ["Browse Org Group" org-customize t]
  23744. "--"
  23745. ["Expand This Menu" org-create-customize-menu
  23746. (fboundp 'customize-menu-create)])
  23747. "--"
  23748. ["Refresh setup" org-mode-restart t]
  23749. ))
  23750. (defun org-info (&optional node)
  23751. "Read documentation for Org-mode in the info system.
  23752. With optional NODE, go directly to that node."
  23753. (interactive)
  23754. (require 'info)
  23755. (Info-goto-node (format "(org)%s" (or node ""))))
  23756. (defun org-install-agenda-files-menu ()
  23757. (let ((bl (buffer-list)))
  23758. (save-excursion
  23759. (while bl
  23760. (set-buffer (pop bl))
  23761. (if (org-mode-p) (setq bl nil)))
  23762. (when (org-mode-p)
  23763. (easy-menu-change
  23764. '("Org") "File List for Agenda"
  23765. (append
  23766. (list
  23767. ["Edit File List" (org-edit-agenda-file-list) t]
  23768. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  23769. ["Remove Current File from List" org-remove-file t]
  23770. ["Cycle through agenda files" org-cycle-agenda-files t]
  23771. ["Occur in all agenda files" org-occur-in-agenda-files t]
  23772. "--")
  23773. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  23774. ;;;; Documentation
  23775. (defun org-customize ()
  23776. "Call the customize function with org as argument."
  23777. (interactive)
  23778. (customize-browse 'org))
  23779. (defun org-create-customize-menu ()
  23780. "Create a full customization menu for Org-mode, insert it into the menu."
  23781. (interactive)
  23782. (if (fboundp 'customize-menu-create)
  23783. (progn
  23784. (easy-menu-change
  23785. '("Org") "Customize"
  23786. `(["Browse Org group" org-customize t]
  23787. "--"
  23788. ,(customize-menu-create 'org)
  23789. ["Set" Custom-set t]
  23790. ["Save" Custom-save t]
  23791. ["Reset to Current" Custom-reset-current t]
  23792. ["Reset to Saved" Custom-reset-saved t]
  23793. ["Reset to Standard Settings" Custom-reset-standard t]))
  23794. (message "\"Org\"-menu now contains full customization menu"))
  23795. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  23796. ;;;; Miscellaneous stuff
  23797. ;;; Generally useful functions
  23798. (defun org-context ()
  23799. "Return a list of contexts of the current cursor position.
  23800. If several contexts apply, all are returned.
  23801. Each context entry is a list with a symbol naming the context, and
  23802. two positions indicating start and end of the context. Possible
  23803. contexts are:
  23804. :headline anywhere in a headline
  23805. :headline-stars on the leading stars in a headline
  23806. :todo-keyword on a TODO keyword (including DONE) in a headline
  23807. :tags on the TAGS in a headline
  23808. :priority on the priority cookie in a headline
  23809. :item on the first line of a plain list item
  23810. :item-bullet on the bullet/number of a plain list item
  23811. :checkbox on the checkbox in a plain list item
  23812. :table in an org-mode table
  23813. :table-special on a special filed in a table
  23814. :table-table in a table.el table
  23815. :link on a hyperlink
  23816. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  23817. :target on a <<target>>
  23818. :radio-target on a <<<radio-target>>>
  23819. :latex-fragment on a LaTeX fragment
  23820. :latex-preview on a LaTeX fragment with overlayed preview image
  23821. This function expects the position to be visible because it uses font-lock
  23822. faces as a help to recognize the following contexts: :table-special, :link,
  23823. and :keyword."
  23824. (let* ((f (get-text-property (point) 'face))
  23825. (faces (if (listp f) f (list f)))
  23826. (p (point)) clist o)
  23827. ;; First the large context
  23828. (cond
  23829. ((org-on-heading-p t)
  23830. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  23831. (when (progn
  23832. (beginning-of-line 1)
  23833. (looking-at org-todo-line-tags-regexp))
  23834. (push (org-point-in-group p 1 :headline-stars) clist)
  23835. (push (org-point-in-group p 2 :todo-keyword) clist)
  23836. (push (org-point-in-group p 4 :tags) clist))
  23837. (goto-char p)
  23838. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  23839. (if (looking-at "\\[#[A-Z0-9]\\]")
  23840. (push (org-point-in-group p 0 :priority) clist)))
  23841. ((org-at-item-p)
  23842. (push (org-point-in-group p 2 :item-bullet) clist)
  23843. (push (list :item (point-at-bol)
  23844. (save-excursion (org-end-of-item) (point)))
  23845. clist)
  23846. (and (org-at-item-checkbox-p)
  23847. (push (org-point-in-group p 0 :checkbox) clist)))
  23848. ((org-at-table-p)
  23849. (push (list :table (org-table-begin) (org-table-end)) clist)
  23850. (if (memq 'org-formula faces)
  23851. (push (list :table-special
  23852. (previous-single-property-change p 'face)
  23853. (next-single-property-change p 'face)) clist)))
  23854. ((org-at-table-p 'any)
  23855. (push (list :table-table) clist)))
  23856. (goto-char p)
  23857. ;; Now the small context
  23858. (cond
  23859. ((org-at-timestamp-p)
  23860. (push (org-point-in-group p 0 :timestamp) clist))
  23861. ((memq 'org-link faces)
  23862. (push (list :link
  23863. (previous-single-property-change p 'face)
  23864. (next-single-property-change p 'face)) clist))
  23865. ((memq 'org-special-keyword faces)
  23866. (push (list :keyword
  23867. (previous-single-property-change p 'face)
  23868. (next-single-property-change p 'face)) clist))
  23869. ((org-on-target-p)
  23870. (push (org-point-in-group p 0 :target) clist)
  23871. (goto-char (1- (match-beginning 0)))
  23872. (if (looking-at org-radio-target-regexp)
  23873. (push (org-point-in-group p 0 :radio-target) clist))
  23874. (goto-char p))
  23875. ((setq o (car (delq nil
  23876. (mapcar
  23877. (lambda (x)
  23878. (if (memq x org-latex-fragment-image-overlays) x))
  23879. (org-overlays-at (point))))))
  23880. (push (list :latex-fragment
  23881. (org-overlay-start o) (org-overlay-end o)) clist)
  23882. (push (list :latex-preview
  23883. (org-overlay-start o) (org-overlay-end o)) clist))
  23884. ((org-inside-LaTeX-fragment-p)
  23885. ;; FIXME: positions wrong.
  23886. (push (list :latex-fragment (point) (point)) clist)))
  23887. (setq clist (nreverse (delq nil clist)))
  23888. clist))
  23889. ;; FIXME: Compare with at-regexp-p Do we need both?
  23890. (defun org-in-regexp (re &optional nlines visually)
  23891. "Check if point is inside a match of regexp.
  23892. Normally only the current line is checked, but you can include NLINES extra
  23893. lines both before and after point into the search.
  23894. If VISUALLY is set, require that the cursor is not after the match but
  23895. really on, so that the block visually is on the match."
  23896. (catch 'exit
  23897. (let ((pos (point))
  23898. (eol (point-at-eol (+ 1 (or nlines 0))))
  23899. (inc (if visually 1 0)))
  23900. (save-excursion
  23901. (beginning-of-line (- 1 (or nlines 0)))
  23902. (while (re-search-forward re eol t)
  23903. (if (and (<= (match-beginning 0) pos)
  23904. (>= (+ inc (match-end 0)) pos))
  23905. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  23906. (defun org-at-regexp-p (regexp)
  23907. "Is point inside a match of REGEXP in the current line?"
  23908. (catch 'exit
  23909. (save-excursion
  23910. (let ((pos (point)) (end (point-at-eol)))
  23911. (beginning-of-line 1)
  23912. (while (re-search-forward regexp end t)
  23913. (if (and (<= (match-beginning 0) pos)
  23914. (>= (match-end 0) pos))
  23915. (throw 'exit t)))
  23916. nil))))
  23917. (defun org-occur-in-agenda-files (regexp &optional nlines)
  23918. "Call `multi-occur' with buffers for all agenda files."
  23919. (interactive "sOrg-files matching: \np")
  23920. (let* ((files (org-agenda-files))
  23921. (tnames (mapcar 'file-truename files))
  23922. (extra org-agenda-multi-occur-extra-files)
  23923. f)
  23924. (while (setq f (pop extra))
  23925. (unless (member (file-truename f) tnames)
  23926. (add-to-list 'files f 'append)
  23927. (add-to-list 'tnames (file-truename f) 'append)))
  23928. (multi-occur
  23929. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  23930. regexp)))
  23931. (defun org-uniquify (list)
  23932. "Remove duplicate elements from LIST."
  23933. (let (res)
  23934. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  23935. res))
  23936. (defun org-delete-all (elts list)
  23937. "Remove all elements in ELTS from LIST."
  23938. (while elts
  23939. (setq list (delete (pop elts) list)))
  23940. list)
  23941. (defun org-point-in-group (point group &optional context)
  23942. "Check if POINT is in match-group GROUP.
  23943. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  23944. match. If the match group does ot exist or point is not inside it,
  23945. return nil."
  23946. (and (match-beginning group)
  23947. (>= point (match-beginning group))
  23948. (<= point (match-end group))
  23949. (if context
  23950. (list context (match-beginning group) (match-end group))
  23951. t)))
  23952. (defun org-switch-to-buffer-other-window (&rest args)
  23953. "Switch to buffer in a second window on the current frame.
  23954. In particular, do not allow pop-up frames."
  23955. (let (pop-up-frames special-display-buffer-names special-display-regexps
  23956. special-display-function)
  23957. (apply 'switch-to-buffer-other-window args)))
  23958. (defun org-combine-plists (&rest plists)
  23959. "Create a single property list from all plists in PLISTS.
  23960. The process starts by copying the first list, and then setting properties
  23961. from the other lists. Settings in the last list are the most significant
  23962. ones and overrule settings in the other lists."
  23963. (let ((rtn (copy-sequence (pop plists)))
  23964. p v ls)
  23965. (while plists
  23966. (setq ls (pop plists))
  23967. (while ls
  23968. (setq p (pop ls) v (pop ls))
  23969. (setq rtn (plist-put rtn p v))))
  23970. rtn))
  23971. (defun org-move-line-down (arg)
  23972. "Move the current line down. With prefix argument, move it past ARG lines."
  23973. (interactive "p")
  23974. (let ((col (current-column))
  23975. beg end pos)
  23976. (beginning-of-line 1) (setq beg (point))
  23977. (beginning-of-line 2) (setq end (point))
  23978. (beginning-of-line (+ 1 arg))
  23979. (setq pos (move-marker (make-marker) (point)))
  23980. (insert (delete-and-extract-region beg end))
  23981. (goto-char pos)
  23982. (move-to-column col)))
  23983. (defun org-move-line-up (arg)
  23984. "Move the current line up. With prefix argument, move it past ARG lines."
  23985. (interactive "p")
  23986. (let ((col (current-column))
  23987. beg end pos)
  23988. (beginning-of-line 1) (setq beg (point))
  23989. (beginning-of-line 2) (setq end (point))
  23990. (beginning-of-line (- arg))
  23991. (setq pos (move-marker (make-marker) (point)))
  23992. (insert (delete-and-extract-region beg end))
  23993. (goto-char pos)
  23994. (move-to-column col)))
  23995. (defun org-replace-escapes (string table)
  23996. "Replace %-escapes in STRING with values in TABLE.
  23997. TABLE is an association list with keys like \"%a\" and string values.
  23998. The sequences in STRING may contain normal field width and padding information,
  23999. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  24000. so values can contain further %-escapes if they are define later in TABLE."
  24001. (let ((case-fold-search nil)
  24002. e re rpl)
  24003. (while (setq e (pop table))
  24004. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  24005. (while (string-match re string)
  24006. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  24007. (cdr e)))
  24008. (setq string (replace-match rpl t t string))))
  24009. string))
  24010. (defun org-sublist (list start end)
  24011. "Return a section of LIST, from START to END.
  24012. Counting starts at 1."
  24013. (let (rtn (c start))
  24014. (setq list (nthcdr (1- start) list))
  24015. (while (and list (<= c end))
  24016. (push (pop list) rtn)
  24017. (setq c (1+ c)))
  24018. (nreverse rtn)))
  24019. (defun org-find-base-buffer-visiting (file)
  24020. "Like `find-buffer-visiting' but alway return the base buffer and
  24021. not an indirect buffer"
  24022. (let ((buf (find-buffer-visiting file)))
  24023. (if buf
  24024. (or (buffer-base-buffer buf) buf)
  24025. nil)))
  24026. (defun org-image-file-name-regexp ()
  24027. "Return regexp matching the file names of images."
  24028. (if (fboundp 'image-file-name-regexp)
  24029. (image-file-name-regexp)
  24030. (let ((image-file-name-extensions
  24031. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  24032. "xbm" "xpm" "pbm" "pgm" "ppm")))
  24033. (concat "\\."
  24034. (regexp-opt (nconc (mapcar 'upcase
  24035. image-file-name-extensions)
  24036. image-file-name-extensions)
  24037. t)
  24038. "\\'"))))
  24039. (defun org-file-image-p (file)
  24040. "Return non-nil if FILE is an image."
  24041. (save-match-data
  24042. (string-match (org-image-file-name-regexp) file)))
  24043. ;;; Paragraph filling stuff.
  24044. ;; We want this to be just right, so use the full arsenal.
  24045. (defun org-indent-line-function ()
  24046. "Indent line like previous, but further if previous was headline or item."
  24047. (interactive)
  24048. (let* ((pos (point))
  24049. (itemp (org-at-item-p))
  24050. column bpos bcol tpos tcol bullet btype bullet-type)
  24051. ;; Find the previous relevant line
  24052. (beginning-of-line 1)
  24053. (cond
  24054. ((looking-at "#") (setq column 0))
  24055. ((looking-at "\\*+ ") (setq column 0))
  24056. (t
  24057. (beginning-of-line 0)
  24058. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  24059. (beginning-of-line 0))
  24060. (cond
  24061. ((looking-at "\\*+[ \t]+")
  24062. (goto-char (match-end 0))
  24063. (setq column (current-column)))
  24064. ((org-in-item-p)
  24065. (org-beginning-of-item)
  24066. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  24067. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  24068. (setq bpos (match-beginning 1) tpos (match-end 0)
  24069. bcol (progn (goto-char bpos) (current-column))
  24070. tcol (progn (goto-char tpos) (current-column))
  24071. bullet (match-string 1)
  24072. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  24073. (if (not itemp)
  24074. (setq column tcol)
  24075. (goto-char pos)
  24076. (beginning-of-line 1)
  24077. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  24078. (setq bullet (match-string 1)
  24079. btype (if (string-match "[0-9]" bullet) "n" bullet))
  24080. (setq column (if (equal btype bullet-type) bcol tcol))))
  24081. (t (setq column (org-get-indentation))))))
  24082. (goto-char pos)
  24083. (if (<= (current-column) (current-indentation))
  24084. (indent-line-to column)
  24085. (save-excursion (indent-line-to column)))
  24086. (setq column (current-column))
  24087. (beginning-of-line 1)
  24088. (if (looking-at
  24089. "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  24090. (replace-match (concat "\\1" (format org-property-format
  24091. (match-string 2) (match-string 3)))
  24092. t nil))
  24093. (move-to-column column)))
  24094. (defun org-set-autofill-regexps ()
  24095. (interactive)
  24096. ;; In the paragraph separator we include headlines, because filling
  24097. ;; text in a line directly attached to a headline would otherwise
  24098. ;; fill the headline as well.
  24099. (org-set-local 'comment-start-skip "^#+[ \t]*")
  24100. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  24101. ;; The paragraph starter includes hand-formatted lists.
  24102. (org-set-local 'paragraph-start
  24103. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  24104. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  24105. ;; But only if the user has not turned off tables or fixed-width regions
  24106. (org-set-local
  24107. 'auto-fill-inhibit-regexp
  24108. (concat "\\*+ \\|#\\+"
  24109. "\\|[ \t]*" org-keyword-time-regexp
  24110. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  24111. (concat
  24112. "\\|[ \t]*["
  24113. (if org-enable-table-editor "|" "")
  24114. (if org-enable-fixed-width-editor ":" "")
  24115. "]"))))
  24116. ;; We use our own fill-paragraph function, to make sure that tables
  24117. ;; and fixed-width regions are not wrapped. That function will pass
  24118. ;; through to `fill-paragraph' when appropriate.
  24119. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  24120. ; Adaptive filling: To get full control, first make sure that
  24121. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  24122. (org-set-local 'adaptive-fill-regexp "\000")
  24123. (org-set-local 'adaptive-fill-function
  24124. 'org-adaptive-fill-function))
  24125. (defun org-fill-paragraph (&optional justify)
  24126. "Re-align a table, pass through to fill-paragraph if no table."
  24127. (let ((table-p (org-at-table-p))
  24128. (table.el-p (org-at-table.el-p)))
  24129. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  24130. (table.el-p t) ; skip table.el tables
  24131. (table-p (org-table-align) t) ; align org-mode tables
  24132. (t nil)))) ; call paragraph-fill
  24133. ;; For reference, this is the default value of adaptive-fill-regexp
  24134. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  24135. (defun org-adaptive-fill-function ()
  24136. "Return a fill prefix for org-mode files.
  24137. In particular, this makes sure hanging paragraphs for hand-formatted lists
  24138. work correctly."
  24139. (cond ((looking-at "#[ \t]+")
  24140. (match-string 0))
  24141. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  24142. (save-excursion
  24143. (goto-char (match-end 0))
  24144. (make-string (current-column) ?\ )))
  24145. (t nil)))
  24146. ;;;; Functions extending outline functionality
  24147. (defun org-beginning-of-line (&optional arg)
  24148. "Go to the beginning of the current line. If that is invisible, continue
  24149. to a visible line beginning. This makes the function of C-a more intuitive.
  24150. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  24151. first attempt, and only move to after the tags when the cursor is already
  24152. beyond the end of the headline."
  24153. (interactive "P")
  24154. (let ((pos (point)))
  24155. (beginning-of-line 1)
  24156. (if (bobp)
  24157. nil
  24158. (backward-char 1)
  24159. (if (org-invisible-p)
  24160. (while (and (not (bobp)) (org-invisible-p))
  24161. (backward-char 1)
  24162. (beginning-of-line 1))
  24163. (forward-char 1)))
  24164. (when org-special-ctrl-a/e
  24165. (cond
  24166. ((and (looking-at org-todo-line-regexp)
  24167. (= (char-after (match-end 1)) ?\ ))
  24168. (goto-char
  24169. (if (eq org-special-ctrl-a/e t)
  24170. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  24171. ((= pos (point)) (match-beginning 3))
  24172. (t (point)))
  24173. (cond ((> pos (point)) (point))
  24174. ((not (eq last-command this-command)) (point))
  24175. (t (match-beginning 3))))))
  24176. ((org-at-item-p)
  24177. (goto-char
  24178. (if (eq org-special-ctrl-a/e t)
  24179. (cond ((> pos (match-end 4)) (match-end 4))
  24180. ((= pos (point)) (match-end 4))
  24181. (t (point)))
  24182. (cond ((> pos (point)) (point))
  24183. ((not (eq last-command this-command)) (point))
  24184. (t (match-end 4))))))))))
  24185. (defun org-end-of-line (&optional arg)
  24186. "Go to the end of the line.
  24187. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  24188. first attempt, and only move to after the tags when the cursor is already
  24189. beyond the end of the headline."
  24190. (interactive "P")
  24191. (if (or (not org-special-ctrl-a/e)
  24192. (not (org-on-heading-p)))
  24193. (end-of-line arg)
  24194. (let ((pos (point)))
  24195. (beginning-of-line 1)
  24196. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  24197. (if (eq org-special-ctrl-a/e t)
  24198. (if (or (< pos (match-beginning 1))
  24199. (= pos (match-end 0)))
  24200. (goto-char (match-beginning 1))
  24201. (goto-char (match-end 0)))
  24202. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  24203. (goto-char (match-end 0))
  24204. (goto-char (match-beginning 1))))
  24205. (end-of-line arg)))))
  24206. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  24207. (define-key org-mode-map "\C-e" 'org-end-of-line)
  24208. (defun org-invisible-p ()
  24209. "Check if point is at a character currently not visible."
  24210. ;; Early versions of noutline don't have `outline-invisible-p'.
  24211. (if (fboundp 'outline-invisible-p)
  24212. (outline-invisible-p)
  24213. (get-char-property (point) 'invisible)))
  24214. (defun org-invisible-p2 ()
  24215. "Check if point is at a character currently not visible."
  24216. (save-excursion
  24217. (if (and (eolp) (not (bobp))) (backward-char 1))
  24218. ;; Early versions of noutline don't have `outline-invisible-p'.
  24219. (if (fboundp 'outline-invisible-p)
  24220. (outline-invisible-p)
  24221. (get-char-property (point) 'invisible))))
  24222. (defalias 'org-back-to-heading 'outline-back-to-heading)
  24223. (defalias 'org-on-heading-p 'outline-on-heading-p)
  24224. (defalias 'org-at-heading-p 'outline-on-heading-p)
  24225. (defun org-at-heading-or-item-p ()
  24226. (or (org-on-heading-p) (org-at-item-p)))
  24227. (defun org-on-target-p ()
  24228. (or (org-in-regexp org-radio-target-regexp)
  24229. (org-in-regexp org-target-regexp)))
  24230. (defun org-up-heading-all (arg)
  24231. "Move to the heading line of which the present line is a subheading.
  24232. This function considers both visible and invisible heading lines.
  24233. With argument, move up ARG levels."
  24234. (if (fboundp 'outline-up-heading-all)
  24235. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  24236. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  24237. (defun org-up-heading-safe ()
  24238. "Move to the heading line of which the present line is a subheading.
  24239. This version will not throw an error. It will return the level of the
  24240. headline found, or nil if no higher level is found."
  24241. (let ((pos (point)) start-level level
  24242. (re (concat "^" outline-regexp)))
  24243. (catch 'exit
  24244. (outline-back-to-heading t)
  24245. (setq start-level (funcall outline-level))
  24246. (if (equal start-level 1) (throw 'exit nil))
  24247. (while (re-search-backward re nil t)
  24248. (setq level (funcall outline-level))
  24249. (if (< level start-level) (throw 'exit level)))
  24250. nil)))
  24251. (defun org-goto-sibling (&optional previous)
  24252. "Goto the next sibling, even if it is invisible.
  24253. When PREVIOUS is set, go to the previous sibling instead. Returns t
  24254. when a sibling was found. When none is found, return nil and don't
  24255. move point."
  24256. (let ((fun (if previous 're-search-backward 're-search-forward))
  24257. (pos (point))
  24258. (re (concat "^" outline-regexp))
  24259. level l)
  24260. (when (condition-case nil (org-back-to-heading t) (error nil))
  24261. (setq level (funcall outline-level))
  24262. (catch 'exit
  24263. (or previous (forward-char 1))
  24264. (while (funcall fun re nil t)
  24265. (setq l (funcall outline-level))
  24266. (when (< l level) (goto-char pos) (throw 'exit nil))
  24267. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  24268. (goto-char pos)
  24269. nil))))
  24270. (defun org-show-siblings ()
  24271. "Show all siblings of the current headline."
  24272. (save-excursion
  24273. (while (org-goto-sibling) (org-flag-heading nil)))
  24274. (save-excursion
  24275. (while (org-goto-sibling 'previous)
  24276. (org-flag-heading nil))))
  24277. (defun org-show-hidden-entry ()
  24278. "Show an entry where even the heading is hidden."
  24279. (save-excursion
  24280. (org-show-entry)))
  24281. (defun org-flag-heading (flag &optional entry)
  24282. "Flag the current heading. FLAG non-nil means make invisible.
  24283. When ENTRY is non-nil, show the entire entry."
  24284. (save-excursion
  24285. (org-back-to-heading t)
  24286. ;; Check if we should show the entire entry
  24287. (if entry
  24288. (progn
  24289. (org-show-entry)
  24290. (save-excursion
  24291. (and (outline-next-heading)
  24292. (org-flag-heading nil))))
  24293. (outline-flag-region (max (point-min) (1- (point)))
  24294. (save-excursion (outline-end-of-heading) (point))
  24295. flag))))
  24296. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  24297. ;; This is an exact copy of the original function, but it uses
  24298. ;; `org-back-to-heading', to make it work also in invisible
  24299. ;; trees. And is uses an invisible-OK argument.
  24300. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  24301. (org-back-to-heading invisible-OK)
  24302. (let ((first t)
  24303. (level (funcall outline-level)))
  24304. (while (and (not (eobp))
  24305. (or first (> (funcall outline-level) level)))
  24306. (setq first nil)
  24307. (outline-next-heading))
  24308. (unless to-heading
  24309. (if (memq (preceding-char) '(?\n ?\^M))
  24310. (progn
  24311. ;; Go to end of line before heading
  24312. (forward-char -1)
  24313. (if (memq (preceding-char) '(?\n ?\^M))
  24314. ;; leave blank line before heading
  24315. (forward-char -1))))))
  24316. (point))
  24317. (defun org-show-subtree ()
  24318. "Show everything after this heading at deeper levels."
  24319. (outline-flag-region
  24320. (point)
  24321. (save-excursion
  24322. (outline-end-of-subtree) (outline-next-heading) (point))
  24323. nil))
  24324. (defun org-show-entry ()
  24325. "Show the body directly following this heading.
  24326. Show the heading too, if it is currently invisible."
  24327. (interactive)
  24328. (save-excursion
  24329. (condition-case nil
  24330. (progn
  24331. (org-back-to-heading t)
  24332. (outline-flag-region
  24333. (max (point-min) (1- (point)))
  24334. (save-excursion
  24335. (re-search-forward
  24336. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  24337. (or (match-beginning 1) (point-max)))
  24338. nil))
  24339. (error nil))))
  24340. (defun org-make-options-regexp (kwds)
  24341. "Make a regular expression for keyword lines."
  24342. (concat
  24343. "^"
  24344. "#?[ \t]*\\+\\("
  24345. (mapconcat 'regexp-quote kwds "\\|")
  24346. "\\):[ \t]*"
  24347. "\\(.+\\)"))
  24348. ;; Make isearch reveal the necessary context
  24349. (defun org-isearch-end ()
  24350. "Reveal context after isearch exits."
  24351. (when isearch-success ; only if search was successful
  24352. (if (featurep 'xemacs)
  24353. ;; Under XEmacs, the hook is run in the correct place,
  24354. ;; we directly show the context.
  24355. (org-show-context 'isearch)
  24356. ;; In Emacs the hook runs *before* restoring the overlays.
  24357. ;; So we have to use a one-time post-command-hook to do this.
  24358. ;; (Emacs 22 has a special variable, see function `org-mode')
  24359. (unless (and (boundp 'isearch-mode-end-hook-quit)
  24360. isearch-mode-end-hook-quit)
  24361. ;; Only when the isearch was not quitted.
  24362. (org-add-hook 'post-command-hook 'org-isearch-post-command
  24363. 'append 'local)))))
  24364. (defun org-isearch-post-command ()
  24365. "Remove self from hook, and show context."
  24366. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  24367. (org-show-context 'isearch))
  24368. ;;;; Address problems with some other packages
  24369. ;; Make flyspell not check words in links, to not mess up our keymap
  24370. (defun org-mode-flyspell-verify ()
  24371. "Don't let flyspell put overlays at active buttons."
  24372. (not (get-text-property (point) 'keymap)))
  24373. ;; Make `bookmark-jump' show the jump location if it was hidden.
  24374. (eval-after-load "bookmark"
  24375. '(if (boundp 'bookmark-after-jump-hook)
  24376. ;; We can use the hook
  24377. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  24378. ;; Hook not available, use advice
  24379. (defadvice bookmark-jump (after org-make-visible activate)
  24380. "Make the position visible."
  24381. (org-bookmark-jump-unhide))))
  24382. (defun org-bookmark-jump-unhide ()
  24383. "Unhide the current position, to show the bookmark location."
  24384. (and (org-mode-p)
  24385. (or (org-invisible-p)
  24386. (save-excursion (goto-char (max (point-min) (1- (point))))
  24387. (org-invisible-p)))
  24388. (org-show-context 'bookmark-jump)))
  24389. ;; Make session.el ignore our circular variable
  24390. (eval-after-load "session"
  24391. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  24392. ;;;; Experimental code
  24393. (defun org-closed-in-range ()
  24394. "Sparse tree of items closed in a certain time range.
  24395. Still experimental, may disappear in the furture."
  24396. (interactive)
  24397. ;; Get the time interval from the user.
  24398. (let* ((time1 (time-to-seconds
  24399. (org-read-date nil 'to-time nil "Starting date: ")))
  24400. (time2 (time-to-seconds
  24401. (org-read-date nil 'to-time nil "End date:")))
  24402. ;; callback function
  24403. (callback (lambda ()
  24404. (let ((time
  24405. (time-to-seconds
  24406. (apply 'encode-time
  24407. (org-parse-time-string
  24408. (match-string 1))))))
  24409. ;; check if time in interval
  24410. (and (>= time time1) (<= time time2))))))
  24411. ;; make tree, check each match with the callback
  24412. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  24413. (defun org-fill-paragraph-experimental (&optional justify)
  24414. "Re-align a table, pass through to fill-paragraph if no table."
  24415. (let ((table-p (org-at-table-p))
  24416. (table.el-p (org-at-table.el-p)))
  24417. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  24418. (table.el-p t) ; skip table.el tables
  24419. (table-p (org-table-align) t) ; align org-mode tables
  24420. ((save-excursion
  24421. (let ((pos (1+ (point-at-eol))))
  24422. (backward-paragraph 1)
  24423. (re-search-forward "\\\\\\\\[ \t]*$" pos t)))
  24424. (save-excursion
  24425. (save-restriction
  24426. (narrow-to-region (1+ (match-end 0)) (point-max))
  24427. (fill-paragraph nil)
  24428. t)))
  24429. (t nil)))) ; call paragraph-fill
  24430. ;; FIXME: this needs a much better algorithm
  24431. (defun org-assign-fast-keys (alist)
  24432. "Assign fast keys to a keyword-key alist.
  24433. Respect keys that are already there."
  24434. (let (new e k c c1 c2 (char ?a))
  24435. (while (setq e (pop alist))
  24436. (cond
  24437. ((equal e '(:startgroup)) (push e new))
  24438. ((equal e '(:endgroup)) (push e new))
  24439. (t
  24440. (setq k (car e) c2 nil)
  24441. (if (cdr e)
  24442. (setq c (cdr e))
  24443. ;; automatically assign a character.
  24444. (setq c1 (string-to-char
  24445. (downcase (substring
  24446. k (if (= (string-to-char k) ?@) 1 0)))))
  24447. (if (or (rassoc c1 new) (rassoc c1 alist))
  24448. (while (or (rassoc char new) (rassoc char alist))
  24449. (setq char (1+ char)))
  24450. (setq c2 c1))
  24451. (setq c (or c2 char)))
  24452. (push (cons k c) new))))
  24453. (nreverse new)))
  24454. ;(defcustom org-read-date-prefer-future nil
  24455. ; "Non-nil means, when reading an incomplete date from the user, assume future.
  24456. ;This affects the following situations:
  24457. ;1. The user give a day, but no month.
  24458. ; In this case, if the day number if after today, the current month will
  24459. ; be used, otherwise the next month.
  24460. ;2. The user gives a month but not a year.
  24461. ; In this case, the the given month is after the current month, the current
  24462. ; year will be used. Otherwise the next year will be used.;
  24463. ;
  24464. ;When nil, always the current month and year will be used."
  24465. ; :group 'org-time ;????
  24466. ; :type 'boolean)
  24467. ;;;; Finish up
  24468. (provide 'org)
  24469. (run-hooks 'org-load-hook)
  24470. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  24471. ;;; org.el ends here