org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.20
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.20"
  81. "The version number of the file org.el.")
  82. (defun org-version ()
  83. (interactive)
  84. (message "Org-mode version %s" org-version))
  85. ;;; Compatibility constants
  86. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  87. (defconst org-format-transports-properties-p
  88. (let ((x "a"))
  89. (add-text-properties 0 1 '(test t) x)
  90. (get-text-property 0 'test (format "%s" x)))
  91. "Does format transport text properties?")
  92. (defmacro org-bound-and-true-p (var)
  93. "Return the value of symbol VAR if it is bound, else nil."
  94. `(and (boundp (quote ,var)) ,var))
  95. (defmacro org-unmodified (&rest body)
  96. "Execute body without changing `buffer-modified-p'."
  97. `(set-buffer-modified-p
  98. (prog1 (buffer-modified-p) ,@body)))
  99. (defmacro org-re (s)
  100. "Replace posix classes in regular expression."
  101. (if (featurep 'xemacs)
  102. (let ((ss s))
  103. (save-match-data
  104. (while (string-match "\\[:alnum:\\]" ss)
  105. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  106. (while (string-match "\\[:alpha:\\]" ss)
  107. (setq ss (replace-match "a-zA-Z" t t ss)))
  108. ss))
  109. s))
  110. (defmacro org-preserve-lc (&rest body)
  111. `(let ((_line (org-current-line))
  112. (_col (current-column)))
  113. (unwind-protect
  114. (progn ,@body)
  115. (goto-line _line)
  116. (move-to-column _col))))
  117. (defmacro org-without-partial-completion (&rest body)
  118. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  119. partial-completion-mode)))
  120. (unwind-protect
  121. (progn
  122. (if pc-mode (partial-completion-mode -1))
  123. ,@body)
  124. (if pc-mode (partial-completion-mode 1)))))
  125. ;;; The custom variables
  126. (defgroup org nil
  127. "Outline-based notes management and organizer."
  128. :tag "Org"
  129. :group 'outlines
  130. :group 'hypermedia
  131. :group 'calendar)
  132. ;; FIXME: Needs a separate group...
  133. (defcustom org-completion-fallback-command 'hippie-expand
  134. "The expansion command called by \\[org-complete] in normal context.
  135. Normal means, no org-mode-specific context."
  136. :group 'org
  137. :type 'function)
  138. (defgroup org-startup nil
  139. "Options concerning startup of Org-mode."
  140. :tag "Org Startup"
  141. :group 'org)
  142. (defcustom org-startup-folded t
  143. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  144. This can also be configured on a per-file basis by adding one of
  145. the following lines anywhere in the buffer:
  146. #+STARTUP: fold
  147. #+STARTUP: nofold
  148. #+STARTUP: content"
  149. :group 'org-startup
  150. :type '(choice
  151. (const :tag "nofold: show all" nil)
  152. (const :tag "fold: overview" t)
  153. (const :tag "content: all headlines" content)))
  154. (defcustom org-startup-truncated t
  155. "Non-nil means, entering Org-mode will set `truncate-lines'.
  156. This is useful since some lines containing links can be very long and
  157. uninteresting. Also tables look terrible when wrapped."
  158. :group 'org-startup
  159. :type 'boolean)
  160. (defcustom org-startup-align-all-tables nil
  161. "Non-nil means, align all tables when visiting a file.
  162. This is useful when the column width in tables is forced with <N> cookies
  163. in table fields. Such tables will look correct only after the first re-align.
  164. This can also be configured on a per-file basis by adding one of
  165. the following lines anywhere in the buffer:
  166. #+STARTUP: align
  167. #+STARTUP: noalign"
  168. :group 'org-startup
  169. :type 'boolean)
  170. (defcustom org-insert-mode-line-in-empty-file nil
  171. "Non-nil means insert the first line setting Org-mode in empty files.
  172. When the function `org-mode' is called interactively in an empty file, this
  173. normally means that the file name does not automatically trigger Org-mode.
  174. To ensure that the file will always be in Org-mode in the future, a
  175. line enforcing Org-mode will be inserted into the buffer, if this option
  176. has been set."
  177. :group 'org-startup
  178. :type 'boolean)
  179. (defcustom org-replace-disputed-keys nil
  180. "Non-nil means use alternative key bindings for some keys.
  181. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  182. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  183. If you want to use Org-mode together with one of these other modes,
  184. or more generally if you would like to move some Org-mode commands to
  185. other keys, set this variable and configure the keys with the variable
  186. `org-disputed-keys'.
  187. This option is only relevant at load-time of Org-mode, and must be set
  188. *before* org.el is loaded. Changing it requires a restart of Emacs to
  189. become effective."
  190. :group 'org-startup
  191. :type 'boolean)
  192. (if (fboundp 'defvaralias)
  193. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  194. (defcustom org-disputed-keys
  195. '(([(shift up)] . [(meta p)])
  196. ([(shift down)] . [(meta n)])
  197. ([(shift left)] . [(meta -)])
  198. ([(shift right)] . [(meta +)])
  199. ([(control shift right)] . [(meta shift +)])
  200. ([(control shift left)] . [(meta shift -)]))
  201. "Keys for which Org-mode and other modes compete.
  202. This is an alist, cars are the default keys, second element specifies
  203. the alternative to use when `org-replace-disputed-keys' is t.
  204. Keys can be specified in any syntax supported by `define-key'.
  205. The value of this option takes effect only at Org-mode's startup,
  206. therefore you'll have to restart Emacs to apply it after changing."
  207. :group 'org-startup
  208. :type 'alist)
  209. (defun org-key (key)
  210. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  211. Or return the original if not disputed."
  212. (if org-replace-disputed-keys
  213. (let* ((nkey (key-description key))
  214. (x (org-find-if (lambda (x)
  215. (equal (key-description (car x)) nkey))
  216. org-disputed-keys)))
  217. (if x (cdr x) key))
  218. key))
  219. (defun org-find-if (predicate seq)
  220. (catch 'exit
  221. (while seq
  222. (if (funcall predicate (car seq))
  223. (throw 'exit (car seq))
  224. (pop seq)))))
  225. (defun org-defkey (keymap key def)
  226. "Define a key, possibly translated, as returned by `org-key'."
  227. (define-key keymap (org-key key) def))
  228. (defcustom org-ellipsis nil
  229. "The ellipsis to use in the Org-mode outline.
  230. When nil, just use the standard three dots. When a string, use that instead,
  231. When a face, use the standart 3 dots, but with the specified face.
  232. The change affects only Org-mode (which will then use its own display table).
  233. Changing this requires executing `M-x org-mode' in a buffer to become
  234. effective."
  235. :group 'org-startup
  236. :type '(choice (const :tag "Default" nil)
  237. (face :tag "Face" :value org-warning)
  238. (string :tag "String" :value "...#")))
  239. (defvar org-display-table nil
  240. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  241. (defgroup org-keywords nil
  242. "Keywords in Org-mode."
  243. :tag "Org Keywords"
  244. :group 'org)
  245. (defcustom org-deadline-string "DEADLINE:"
  246. "String to mark deadline entries.
  247. A deadline is this string, followed by a time stamp. Should be a word,
  248. terminated by a colon. You can insert a schedule keyword and
  249. a timestamp with \\[org-deadline].
  250. Changes become only effective after restarting Emacs."
  251. :group 'org-keywords
  252. :type 'string)
  253. (defcustom org-scheduled-string "SCHEDULED:"
  254. "String to mark scheduled TODO entries.
  255. A schedule is this string, followed by a time stamp. Should be a word,
  256. terminated by a colon. You can insert a schedule keyword and
  257. a timestamp with \\[org-schedule].
  258. Changes become only effective after restarting Emacs."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-closed-string "CLOSED:"
  262. "String used as the prefix for timestamps logging closing a TODO entry."
  263. :group 'org-keywords
  264. :type 'string)
  265. (defcustom org-clock-string "CLOCK:"
  266. "String used as prefix for timestamps clocking work hours on an item."
  267. :group 'org-keywords
  268. :type 'string)
  269. (defcustom org-comment-string "COMMENT"
  270. "Entries starting with this keyword will never be exported.
  271. An entry can be toggled between COMMENT and normal with
  272. \\[org-toggle-comment].
  273. Changes become only effective after restarting Emacs."
  274. :group 'org-keywords
  275. :type 'string)
  276. (defcustom org-quote-string "QUOTE"
  277. "Entries starting with this keyword will be exported in fixed-width font.
  278. Quoting applies only to the text in the entry following the headline, and does
  279. not extend beyond the next headline, even if that is lower level.
  280. An entry can be toggled between QUOTE and normal with
  281. \\[org-toggle-fixed-width-section]."
  282. :group 'org-keywords
  283. :type 'string)
  284. (defconst org-repeat-re
  285. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  286. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  287. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  288. "Regular expression for specifying repeated events.
  289. After a match, group 1 contains the repeat expression.")
  290. (defgroup org-structure nil
  291. "Options concerning the general structure of Org-mode files."
  292. :tag "Org Structure"
  293. :group 'org)
  294. (defgroup org-reveal-location nil
  295. "Options about how to make context of a location visible."
  296. :tag "Org Reveal Location"
  297. :group 'org-structure)
  298. (defconst org-context-choice
  299. '(choice
  300. (const :tag "Always" t)
  301. (const :tag "Never" nil)
  302. (repeat :greedy t :tag "Individual contexts"
  303. (cons
  304. (choice :tag "Context"
  305. (const agenda)
  306. (const org-goto)
  307. (const occur-tree)
  308. (const tags-tree)
  309. (const link-search)
  310. (const mark-goto)
  311. (const bookmark-jump)
  312. (const isearch)
  313. (const default))
  314. (boolean))))
  315. "Contexts for the reveal options.")
  316. (defcustom org-show-hierarchy-above '((default . t))
  317. "Non-nil means, show full hierarchy when revealing a location.
  318. Org-mode often shows locations in an org-mode file which might have
  319. been invisible before. When this is set, the hierarchy of headings
  320. above the exposed location is shown.
  321. Turning this off for example for sparse trees makes them very compact.
  322. Instead of t, this can also be an alist specifying this option for different
  323. contexts. Valid contexts are
  324. agenda when exposing an entry from the agenda
  325. org-goto when using the command `org-goto' on key C-c C-j
  326. occur-tree when using the command `org-occur' on key C-c /
  327. tags-tree when constructing a sparse tree based on tags matches
  328. link-search when exposing search matches associated with a link
  329. mark-goto when exposing the jump goal of a mark
  330. bookmark-jump when exposing a bookmark location
  331. isearch when exiting from an incremental search
  332. default default for all contexts not set explicitly"
  333. :group 'org-reveal-location
  334. :type org-context-choice)
  335. (defcustom org-show-following-heading '((default . nil))
  336. "Non-nil means, show following heading when revealing a location.
  337. Org-mode often shows locations in an org-mode file which might have
  338. been invisible before. When this is set, the heading following the
  339. match is shown.
  340. Turning this off for example for sparse trees makes them very compact,
  341. but makes it harder to edit the location of the match. In such a case,
  342. use the command \\[org-reveal] to show more context.
  343. Instead of t, this can also be an alist specifying this option for different
  344. contexts. See `org-show-hierarchy-above' for valid contexts."
  345. :group 'org-reveal-location
  346. :type org-context-choice)
  347. (defcustom org-show-siblings '((default . nil) (isearch t))
  348. "Non-nil means, show all sibling heading when revealing a location.
  349. Org-mode often shows locations in an org-mode file which might have
  350. been invisible before. When this is set, the sibling of the current entry
  351. heading are all made visible. If `org-show-hierarchy-above' is t,
  352. the same happens on each level of the hierarchy above the current entry.
  353. By default this is on for the isearch context, off for all other contexts.
  354. Turning this off for example for sparse trees makes them very compact,
  355. but makes it harder to edit the location of the match. In such a case,
  356. use the command \\[org-reveal] to show more context.
  357. Instead of t, this can also be an alist specifying this option for different
  358. contexts. See `org-show-hierarchy-above' for valid contexts."
  359. :group 'org-reveal-location
  360. :type org-context-choice)
  361. (defcustom org-show-entry-below '((default . nil))
  362. "Non-nil means, show the entry below a headline when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the text below the headline that is
  365. exposed is also shown.
  366. By default this is off for all contexts.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. See `org-show-hierarchy-above' for valid contexts."
  369. :group 'org-reveal-location
  370. :type org-context-choice)
  371. (defgroup org-cycle nil
  372. "Options concerning visibility cycling in Org-mode."
  373. :tag "Org Cycle"
  374. :group 'org-structure)
  375. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  376. "Names of drawers. Drawers are not opened by cycling on the headline above.
  377. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  378. this:
  379. :DRAWERNAME:
  380. .....
  381. :END:
  382. The drawer \"PROPERTIES\" is special for capturing properties through
  383. the property API.
  384. Drawers can be defined on the per-file basis with a line like:
  385. #+DRAWERS: HIDDEN STATE PROPERTIES"
  386. :group 'org-structure
  387. :type '(repeat (string :tag "Drawer Name")))
  388. (defcustom org-cycle-global-at-bob nil
  389. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  390. This makes it possible to do global cycling without having to use S-TAB or
  391. C-u TAB. For this special case to work, the first line of the buffer
  392. must not be a headline - it may be empty ot some other text. When used in
  393. this way, `org-cycle-hook' is disables temporarily, to make sure the
  394. cursor stays at the beginning of the buffer.
  395. When this option is nil, don't do anything special at the beginning
  396. of the buffer."
  397. :group 'org-cycle
  398. :type 'boolean)
  399. (defcustom org-cycle-emulate-tab t
  400. "Where should `org-cycle' emulate TAB.
  401. nil Never
  402. white Only in completely white lines
  403. whitestart Only at the beginning of lines, before the first non-white char
  404. t Everywhere except in headlines
  405. exc-hl-bol Everywhere except at the start of a headline
  406. If TAB is used in a place where it does not emulate TAB, the current subtree
  407. visibility is cycled."
  408. :group 'org-cycle
  409. :type '(choice (const :tag "Never" nil)
  410. (const :tag "Only in completely white lines" white)
  411. (const :tag "Before first char in a line" whitestart)
  412. (const :tag "Everywhere except in headlines" t)
  413. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  414. ))
  415. (defcustom org-cycle-separator-lines 2
  416. "Number of empty lines needed to keep an empty line between collapsed trees.
  417. If you leave an empty line between the end of a subtree and the following
  418. headline, this empty line is hidden when the subtree is folded.
  419. Org-mode will leave (exactly) one empty line visible if the number of
  420. empty lines is equal or larger to the number given in this variable.
  421. So the default 2 means, at least 2 empty lines after the end of a subtree
  422. are needed to produce free space between a collapsed subtree and the
  423. following headline.
  424. Special case: when 0, never leave empty lines in collapsed view."
  425. :group 'org-cycle
  426. :type 'integer)
  427. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  428. org-cycle-hide-drawers
  429. org-cycle-show-empty-lines
  430. org-optimize-window-after-visibility-change)
  431. "Hook that is run after `org-cycle' has changed the buffer visibility.
  432. The function(s) in this hook must accept a single argument which indicates
  433. the new state that was set by the most recent `org-cycle' command. The
  434. argument is a symbol. After a global state change, it can have the values
  435. `overview', `content', or `all'. After a local state change, it can have
  436. the values `folded', `children', or `subtree'."
  437. :group 'org-cycle
  438. :type 'hook)
  439. (defgroup org-edit-structure nil
  440. "Options concerning structure editing in Org-mode."
  441. :tag "Org Edit Structure"
  442. :group 'org-structure)
  443. (defcustom org-special-ctrl-a/e nil
  444. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  445. When t, `C-a' will bring back the cursor to the beginning of the
  446. headline text, i.e. after the stars and after a possible TODO keyword.
  447. In an item, this will be the position after the bullet.
  448. When the cursor is already at that position, another `C-a' will bring
  449. it to the beginning of the line.
  450. `C-e' will jump to the end of the headline, ignoring the presence of tags
  451. in the headline. A second `C-e' will then jump to the true end of the
  452. line, after any tags.
  453. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  454. and only a directly following, identical keypress will bring the cursor
  455. to the special positions."
  456. :group 'org-edit-structure
  457. :type '(choice
  458. (const :tag "off" nil)
  459. (const :tag "after bullet first" t)
  460. (const :tag "border first" reversed)))
  461. (if (fboundp 'defvaralias)
  462. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  463. (defcustom org-odd-levels-only nil
  464. "Non-nil means, skip even levels and only use odd levels for the outline.
  465. This has the effect that two stars are being added/taken away in
  466. promotion/demotion commands. It also influences how levels are
  467. handled by the exporters.
  468. Changing it requires restart of `font-lock-mode' to become effective
  469. for fontification also in regions already fontified.
  470. You may also set this on a per-file basis by adding one of the following
  471. lines to the buffer:
  472. #+STARTUP: odd
  473. #+STARTUP: oddeven"
  474. :group 'org-edit-structure
  475. :group 'org-font-lock
  476. :type 'boolean)
  477. (defcustom org-adapt-indentation t
  478. "Non-nil means, adapt indentation when promoting and demoting.
  479. When this is set and the *entire* text in an entry is indented, the
  480. indentation is increased by one space in a demotion command, and
  481. decreased by one in a promotion command. If any line in the entry
  482. body starts at column 0, indentation is not changed at all."
  483. :group 'org-edit-structure
  484. :type 'boolean)
  485. (defcustom org-blank-before-new-entry '((heading . nil)
  486. (plain-list-item . nil))
  487. "Should `org-insert-heading' leave a blank line before new heading/item?
  488. The value is an alist, with `heading' and `plain-list-item' as car,
  489. and a boolean flag as cdr."
  490. :group 'org-edit-structure
  491. :type '(list
  492. (cons (const heading) (boolean))
  493. (cons (const plain-list-item) (boolean))))
  494. (defcustom org-insert-heading-hook nil
  495. "Hook being run after inserting a new heading."
  496. :group 'org-edit-structure
  497. :type 'hook)
  498. (defcustom org-enable-fixed-width-editor t
  499. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  500. This currently only means, they are never auto-wrapped.
  501. When nil, such lines will be treated like ordinary lines.
  502. See also the QUOTE keyword."
  503. :group 'org-edit-structure
  504. :type 'boolean)
  505. (defcustom org-goto-auto-isearch t
  506. "Non-nil means, typing characters in org-goto starts incremental search."
  507. :group 'org-edit-structure
  508. :type 'boolean)
  509. (defgroup org-sparse-trees nil
  510. "Options concerning sparse trees in Org-mode."
  511. :tag "Org Sparse Trees"
  512. :group 'org-structure)
  513. (defcustom org-highlight-sparse-tree-matches t
  514. "Non-nil means, highlight all matches that define a sparse tree.
  515. The highlights will automatically disappear the next time the buffer is
  516. changed by an edit command."
  517. :group 'org-sparse-trees
  518. :type 'boolean)
  519. (defcustom org-remove-highlights-with-change t
  520. "Non-nil means, any change to the buffer will remove temporary highlights.
  521. Such highlights are created by `org-occur' and `org-clock-display'.
  522. When nil, `C-c C-c needs to be used to get rid of the highlights.
  523. The highlights created by `org-preview-latex-fragment' always need
  524. `C-c C-c' to be removed."
  525. :group 'org-sparse-trees
  526. :group 'org-time
  527. :type 'boolean)
  528. (defcustom org-occur-hook '(org-first-headline-recenter)
  529. "Hook that is run after `org-occur' has constructed a sparse tree.
  530. This can be used to recenter the window to show as much of the structure
  531. as possible."
  532. :group 'org-sparse-trees
  533. :type 'hook)
  534. (defgroup org-plain-lists nil
  535. "Options concerning plain lists in Org-mode."
  536. :tag "Org Plain lists"
  537. :group 'org-structure)
  538. (defcustom org-cycle-include-plain-lists nil
  539. "Non-nil means, include plain lists into visibility cycling.
  540. This means that during cycling, plain list items will *temporarily* be
  541. interpreted as outline headlines with a level given by 1000+i where i is the
  542. indentation of the bullet. In all other operations, plain list items are
  543. not seen as headlines. For example, you cannot assign a TODO keyword to
  544. such an item."
  545. :group 'org-plain-lists
  546. :type 'boolean)
  547. (defcustom org-plain-list-ordered-item-terminator t
  548. "The character that makes a line with leading number an ordered list item.
  549. Valid values are ?. and ?\). To get both terminators, use t. While
  550. ?. may look nicer, it creates the danger that a line with leading
  551. number may be incorrectly interpreted as an item. ?\) therefore is
  552. the safe choice."
  553. :group 'org-plain-lists
  554. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  555. (const :tag "paren like in \"2)\"" ?\))
  556. (const :tab "both" t)))
  557. (defcustom org-auto-renumber-ordered-lists t
  558. "Non-nil means, automatically renumber ordered plain lists.
  559. Renumbering happens when the sequence have been changed with
  560. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  561. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  562. :group 'org-plain-lists
  563. :type 'boolean)
  564. (defcustom org-provide-checkbox-statistics t
  565. "Non-nil means, update checkbox statistics after insert and toggle.
  566. When this is set, checkbox statistics is updated each time you either insert
  567. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  568. with \\[org-ctrl-c-ctrl-c\\]."
  569. :group 'org-plain-lists
  570. :type 'boolean)
  571. (defgroup org-archive nil
  572. "Options concerning archiving in Org-mode."
  573. :tag "Org Archive"
  574. :group 'org-structure)
  575. (defcustom org-archive-tag "ARCHIVE"
  576. "The tag that marks a subtree as archived.
  577. An archived subtree does not open during visibility cycling, and does
  578. not contribute to the agenda listings.
  579. After changing this, font-lock must be restarted in the relevant buffers to
  580. get the proper fontification."
  581. :group 'org-archive
  582. :group 'org-keywords
  583. :type 'string)
  584. (defcustom org-agenda-skip-archived-trees t
  585. "Non-nil means, the agenda will skip any items located in archived trees.
  586. An archived tree is a tree marked with the tag ARCHIVE."
  587. :group 'org-archive
  588. :group 'org-agenda-skip
  589. :type 'boolean)
  590. (defcustom org-cycle-open-archived-trees nil
  591. "Non-nil means, `org-cycle' will open archived trees.
  592. An archived tree is a tree marked with the tag ARCHIVE.
  593. When nil, archived trees will stay folded. You can still open them with
  594. normal outline commands like `show-all', but not with the cycling commands."
  595. :group 'org-archive
  596. :group 'org-cycle
  597. :type 'boolean)
  598. (defcustom org-sparse-tree-open-archived-trees nil
  599. "Non-nil means sparse tree construction shows matches in archived trees.
  600. When nil, matches in these trees are highlighted, but the trees are kept in
  601. collapsed state."
  602. :group 'org-archive
  603. :group 'org-sparse-trees
  604. :type 'boolean)
  605. (defcustom org-archive-location "%s_archive::"
  606. "The location where subtrees should be archived.
  607. This string consists of two parts, separated by a double-colon.
  608. The first part is a file name - when omitted, archiving happens in the same
  609. file. %s will be replaced by the current file name (without directory part).
  610. Archiving to a different file is useful to keep archived entries from
  611. contributing to the Org-mode Agenda.
  612. The part after the double colon is a headline. The archived entries will be
  613. filed under that headline. When omitted, the subtrees are simply filed away
  614. at the end of the file, as top-level entries.
  615. Here are a few examples:
  616. \"%s_archive::\"
  617. If the current file is Projects.org, archive in file
  618. Projects.org_archive, as top-level trees. This is the default.
  619. \"::* Archived Tasks\"
  620. Archive in the current file, under the top-level headline
  621. \"* Archived Tasks\".
  622. \"~/org/archive.org::\"
  623. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  624. \"basement::** Finished Tasks\"
  625. Archive in file ./basement (relative path), as level 3 trees
  626. below the level 2 heading \"** Finished Tasks\".
  627. You may set this option on a per-file basis by adding to the buffer a
  628. line like
  629. #+ARCHIVE: basement::** Finished Tasks"
  630. :group 'org-archive
  631. :type 'string)
  632. (defcustom org-archive-mark-done t
  633. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  634. This can be a string to set the keyword to use. When t, Org-mode will
  635. use the first keyword in its list that means done."
  636. :group 'org-archive
  637. :type '(choice
  638. (const :tag "No" nil)
  639. (const :tag "Yes" t)
  640. (string :tag "Use this keyword")))
  641. (defcustom org-archive-stamp-time t
  642. "Non-nil means, add a time stamp to entries moved to an archive file.
  643. This variable is obsolete and has no effect anymore, instead add ot remove
  644. `time' from the variablle `org-archive-save-context-info'."
  645. :group 'org-archive
  646. :type 'boolean)
  647. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  648. "Parts of context info that should be stored as properties when archiving.
  649. When a subtree is moved to an archive file, it looses information given by
  650. context, like inherited tags, the category, and possibly also the TODO
  651. state (depending on the variable `org-archive-mark-done').
  652. This variable can be a list of any of the following symbols:
  653. time The time of archiving.
  654. file The file where the entry originates.
  655. itags The local tags, in the headline of the subtree.
  656. ltags The tags the subtree inherits from further up the hierarchy.
  657. todo The pre-archive TODO state.
  658. category The category, taken from file name or #+CATEGORY lines.
  659. olpath The outline path to the item. These are all headlines above
  660. the current item, separated by /, like a file path.
  661. For each symbol present in the list, a property will be created in
  662. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  663. information."
  664. :group 'org-archive
  665. :type '(set :greedy t
  666. (const :tag "Time" time)
  667. (const :tag "File" file)
  668. (const :tag "Category" category)
  669. (const :tag "TODO state" todo)
  670. (const :tag "TODO state" priority)
  671. (const :tag "Inherited tags" itags)
  672. (const :tag "Outline path" olpath)
  673. (const :tag "Local tags" ltags)))
  674. (defgroup org-imenu-and-speedbar nil
  675. "Options concerning imenu and speedbar in Org-mode."
  676. :tag "Org Imenu and Speedbar"
  677. :group 'org-structure)
  678. (defcustom org-imenu-depth 2
  679. "The maximum level for Imenu access to Org-mode headlines.
  680. This also applied for speedbar access."
  681. :group 'org-imenu-and-speedbar
  682. :type 'number)
  683. (defgroup org-table nil
  684. "Options concerning tables in Org-mode."
  685. :tag "Org Table"
  686. :group 'org)
  687. (defcustom org-enable-table-editor 'optimized
  688. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  689. When nil, such lines will be treated like ordinary lines.
  690. When equal to the symbol `optimized', the table editor will be optimized to
  691. do the following:
  692. - Automatic overwrite mode in front of whitespace in table fields.
  693. This makes the structure of the table stay in tact as long as the edited
  694. field does not exceed the column width.
  695. - Minimize the number of realigns. Normally, the table is aligned each time
  696. TAB or RET are pressed to move to another field. With optimization this
  697. happens only if changes to a field might have changed the column width.
  698. Optimization requires replacing the functions `self-insert-command',
  699. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  700. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  701. very good at guessing when a re-align will be necessary, but you can always
  702. force one with \\[org-ctrl-c-ctrl-c].
  703. If you would like to use the optimized version in Org-mode, but the
  704. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  705. This variable can be used to turn on and off the table editor during a session,
  706. but in order to toggle optimization, a restart is required.
  707. See also the variable `org-table-auto-blank-field'."
  708. :group 'org-table
  709. :type '(choice
  710. (const :tag "off" nil)
  711. (const :tag "on" t)
  712. (const :tag "on, optimized" optimized)))
  713. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  714. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  715. In the optimized version, the table editor takes over all simple keys that
  716. normally just insert a character. In tables, the characters are inserted
  717. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  718. for empty fields). Outside tables, the correct binding of the keys is
  719. restored.
  720. The default for this option is t if the optimized version is also used in
  721. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  722. this variable requires a restart of Emacs to become effective."
  723. :group 'org-table
  724. :type 'boolean)
  725. (defcustom orgtbl-radio-table-templates
  726. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  727. % END RECEIVE ORGTBL %n
  728. \\begin{comment}
  729. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  730. | | |
  731. \\end{comment}\n")
  732. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  733. @c END RECEIVE ORGTBL %n
  734. @ignore
  735. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  736. | | |
  737. @end ignore\n")
  738. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  739. <!-- END RECEIVE ORGTBL %n -->
  740. <!--
  741. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  742. | | |
  743. -->\n"))
  744. "Templates for radio tables in different major modes.
  745. All occurrences of %n in a template will be replaced with the name of the
  746. table, obtained by prompting the user."
  747. :group 'org-table
  748. :type '(repeat
  749. (list (symbol :tag "Major mode")
  750. (string :tag "Format"))))
  751. (defgroup org-table-settings nil
  752. "Settings for tables in Org-mode."
  753. :tag "Org Table Settings"
  754. :group 'org-table)
  755. (defcustom org-table-default-size "5x2"
  756. "The default size for newly created tables, Columns x Rows."
  757. :group 'org-table-settings
  758. :type 'string)
  759. (defcustom org-table-number-regexp
  760. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  761. "Regular expression for recognizing numbers in table columns.
  762. If a table column contains mostly numbers, it will be aligned to the
  763. right. If not, it will be aligned to the left.
  764. The default value of this option is a regular expression which allows
  765. anything which looks remotely like a number as used in scientific
  766. context. For example, all of the following will be considered a
  767. number:
  768. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  769. Other options offered by the customize interface are more restrictive."
  770. :group 'org-table-settings
  771. :type '(choice
  772. (const :tag "Positive Integers"
  773. "^[0-9]+$")
  774. (const :tag "Integers"
  775. "^[-+]?[0-9]+$")
  776. (const :tag "Floating Point Numbers"
  777. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  778. (const :tag "Floating Point Number or Integer"
  779. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  780. (const :tag "Exponential, Floating point, Integer"
  781. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  782. (const :tag "Very General Number-Like, including hex"
  783. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  784. (string :tag "Regexp:")))
  785. (defcustom org-table-number-fraction 0.5
  786. "Fraction of numbers in a column required to make the column align right.
  787. In a column all non-white fields are considered. If at least this
  788. fraction of fields is matched by `org-table-number-fraction',
  789. alignment to the right border applies."
  790. :group 'org-table-settings
  791. :type 'number)
  792. (defgroup org-table-editing nil
  793. "Behavior of tables during editing in Org-mode."
  794. :tag "Org Table Editing"
  795. :group 'org-table)
  796. (defcustom org-table-automatic-realign t
  797. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  798. When nil, aligning is only done with \\[org-table-align], or after column
  799. removal/insertion."
  800. :group 'org-table-editing
  801. :type 'boolean)
  802. (defcustom org-table-auto-blank-field t
  803. "Non-nil means, automatically blank table field when starting to type into it.
  804. This only happens when typing immediately after a field motion
  805. command (TAB, S-TAB or RET).
  806. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  807. :group 'org-table-editing
  808. :type 'boolean)
  809. (defcustom org-table-tab-jumps-over-hlines t
  810. "Non-nil means, tab in the last column of a table with jump over a hline.
  811. If a horizontal separator line is following the current line,
  812. `org-table-next-field' can either create a new row before that line, or jump
  813. over the line. When this option is nil, a new line will be created before
  814. this line."
  815. :group 'org-table-editing
  816. :type 'boolean)
  817. (defcustom org-table-tab-recognizes-table.el t
  818. "Non-nil means, TAB will automatically notice a table.el table.
  819. When it sees such a table, it moves point into it and - if necessary -
  820. calls `table-recognize-table'."
  821. :group 'org-table-editing
  822. :type 'boolean)
  823. (defgroup org-table-calculation nil
  824. "Options concerning tables in Org-mode."
  825. :tag "Org Table Calculation"
  826. :group 'org-table)
  827. (defcustom org-table-use-standard-references t
  828. "Should org-mode work with table refrences like B3 instead of @3$2?
  829. Possible values are:
  830. nil never use them
  831. from accept as input, do not present for editing
  832. t: accept as input and present for editing"
  833. :group 'org-table-calculation
  834. :type '(choice
  835. (const :tag "Never, don't even check unser input for them" nil)
  836. (const :tag "Always, both as user input, and when editing" t)
  837. (const :tag "Convert user input, don't offer during editing" 'from)))
  838. (defcustom org-table-copy-increment t
  839. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  840. :group 'org-table-calculation
  841. :type 'boolean)
  842. (defcustom org-calc-default-modes
  843. '(calc-internal-prec 12
  844. calc-float-format (float 5)
  845. calc-angle-mode deg
  846. calc-prefer-frac nil
  847. calc-symbolic-mode nil
  848. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  849. calc-display-working-message t
  850. )
  851. "List with Calc mode settings for use in calc-eval for table formulas.
  852. The list must contain alternating symbols (Calc modes variables and values).
  853. Don't remove any of the default settings, just change the values. Org-mode
  854. relies on the variables to be present in the list."
  855. :group 'org-table-calculation
  856. :type 'plist)
  857. (defcustom org-table-formula-evaluate-inline t
  858. "Non-nil means, TAB and RET evaluate a formula in current table field.
  859. If the current field starts with an equal sign, it is assumed to be a formula
  860. which should be evaluated as described in the manual and in the documentation
  861. string of the command `org-table-eval-formula'. This feature requires the
  862. Emacs calc package.
  863. When this variable is nil, formula calculation is only available through
  864. the command \\[org-table-eval-formula]."
  865. :group 'org-table-calculation
  866. :type 'boolean)
  867. (defcustom org-table-formula-use-constants t
  868. "Non-nil means, interpret constants in formulas in tables.
  869. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  870. by the value given in `org-table-formula-constants', or by a value obtained
  871. from the `constants.el' package."
  872. :group 'org-table-calculation
  873. :type 'boolean)
  874. (defcustom org-table-formula-constants nil
  875. "Alist with constant names and values, for use in table formulas.
  876. The car of each element is a name of a constant, without the `$' before it.
  877. The cdr is the value as a string. For example, if you'd like to use the
  878. speed of light in a formula, you would configure
  879. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  880. and then use it in an equation like `$1*$c'.
  881. Constants can also be defined on a per-file basis using a line like
  882. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  883. :group 'org-table-calculation
  884. :type '(repeat
  885. (cons (string :tag "name")
  886. (string :tag "value"))))
  887. (defvar org-table-formula-constants-local nil
  888. "Local version of `org-table-formula-constants'.")
  889. (make-variable-buffer-local 'org-table-formula-constants-local)
  890. (defcustom org-table-allow-automatic-line-recalculation t
  891. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  892. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  893. :group 'org-table-calculation
  894. :type 'boolean)
  895. (defgroup org-link nil
  896. "Options concerning links in Org-mode."
  897. :tag "Org Link"
  898. :group 'org)
  899. (defvar org-link-abbrev-alist-local nil
  900. "Buffer-local version of `org-link-abbrev-alist', which see.
  901. The value of this is taken from the #+LINK lines.")
  902. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  903. (defcustom org-link-abbrev-alist nil
  904. "Alist of link abbreviations.
  905. The car of each element is a string, to be replaced at the start of a link.
  906. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  907. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  908. [[linkkey:tag][description]]
  909. If REPLACE is a string, the tag will simply be appended to create the link.
  910. If the string contains \"%s\", the tag will be inserted there.
  911. REPLACE may also be a function that will be called with the tag as the
  912. only argument to create the link, which should be returned as a string.
  913. See the manual for examples."
  914. :group 'org-link
  915. :type 'alist)
  916. (defcustom org-descriptive-links t
  917. "Non-nil means, hide link part and only show description of bracket links.
  918. Bracket links are like [[link][descritpion]]. This variable sets the initial
  919. state in new org-mode buffers. The setting can then be toggled on a
  920. per-buffer basis from the Org->Hyperlinks menu."
  921. :group 'org-link
  922. :type 'boolean)
  923. (defcustom org-link-file-path-type 'adaptive
  924. "How the path name in file links should be stored.
  925. Valid values are:
  926. relative Relative to the current directory, i.e. the directory of the file
  927. into which the link is being inserted.
  928. absolute Absolute path, if possible with ~ for home directory.
  929. noabbrev Absolute path, no abbreviation of home directory.
  930. adaptive Use relative path for files in the current directory and sub-
  931. directories of it. For other files, use an absolute path."
  932. :group 'org-link
  933. :type '(choice
  934. (const relative)
  935. (const absolute)
  936. (const noabbrev)
  937. (const adaptive)))
  938. (defcustom org-activate-links '(bracket angle plain radio tag date)
  939. "Types of links that should be activated in Org-mode files.
  940. This is a list of symbols, each leading to the activation of a certain link
  941. type. In principle, it does not hurt to turn on most link types - there may
  942. be a small gain when turning off unused link types. The types are:
  943. bracket The recommended [[link][description]] or [[link]] links with hiding.
  944. angular Links in angular brackes that may contain whitespace like
  945. <bbdb:Carsten Dominik>.
  946. plain Plain links in normal text, no whitespace, like http://google.com.
  947. radio Text that is matched by a radio target, see manual for details.
  948. tag Tag settings in a headline (link to tag search).
  949. date Time stamps (link to calendar).
  950. Changing this variable requires a restart of Emacs to become effective."
  951. :group 'org-link
  952. :type '(set (const :tag "Double bracket links (new style)" bracket)
  953. (const :tag "Angular bracket links (old style)" angular)
  954. (const :tag "plain text links" plain)
  955. (const :tag "Radio target matches" radio)
  956. (const :tag "Tags" tag)
  957. (const :tag "Tags" target)
  958. (const :tag "Timestamps" date)))
  959. (defgroup org-link-store nil
  960. "Options concerning storing links in Org-mode"
  961. :tag "Org Store Link"
  962. :group 'org-link)
  963. (defcustom org-email-link-description-format "Email %c: %.30s"
  964. "Format of the description part of a link to an email or usenet message.
  965. The following %-excapes will be replaced by corresponding information:
  966. %F full \"From\" field
  967. %f name, taken from \"From\" field, address if no name
  968. %T full \"To\" field
  969. %t first name in \"To\" field, address if no name
  970. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  971. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  972. %s subject
  973. %m message-id.
  974. You may use normal field width specification between the % and the letter.
  975. This is for example useful to limit the length of the subject.
  976. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  977. :group 'org-link-store
  978. :type 'string)
  979. (defcustom org-from-is-user-regexp
  980. (let (r1 r2)
  981. (when (and user-mail-address (not (string= user-mail-address "")))
  982. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  983. (when (and user-full-name (not (string= user-full-name "")))
  984. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  985. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  986. "Regexp mached against the \"From:\" header of an email or usenet message.
  987. It should match if the message is from the user him/herself."
  988. :group 'org-link-store
  989. :type 'regexp)
  990. (defcustom org-context-in-file-links t
  991. "Non-nil means, file links from `org-store-link' contain context.
  992. A search string will be added to the file name with :: as separator and
  993. used to find the context when the link is activated by the command
  994. `org-open-at-point'.
  995. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  996. negates this setting for the duration of the command."
  997. :group 'org-link-store
  998. :type 'boolean)
  999. (defcustom org-keep-stored-link-after-insertion nil
  1000. "Non-nil means, keep link in list for entire session.
  1001. The command `org-store-link' adds a link pointing to the current
  1002. location to an internal list. These links accumulate during a session.
  1003. The command `org-insert-link' can be used to insert links into any
  1004. Org-mode file (offering completion for all stored links). When this
  1005. option is nil, every link which has been inserted once using \\[org-insert-link]
  1006. will be removed from the list, to make completing the unused links
  1007. more efficient."
  1008. :group 'org-link-store
  1009. :type 'boolean)
  1010. (defcustom org-usenet-links-prefer-google nil
  1011. "Non-nil means, `org-store-link' will create web links to Google groups.
  1012. When nil, Gnus will be used for such links.
  1013. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1014. negates this setting for the duration of the command."
  1015. :group 'org-link-store
  1016. :type 'boolean)
  1017. (defgroup org-link-follow nil
  1018. "Options concerning following links in Org-mode"
  1019. :tag "Org Follow Link"
  1020. :group 'org-link)
  1021. (defcustom org-tab-follows-link nil
  1022. "Non-nil means, on links TAB will follow the link.
  1023. Needs to be set before org.el is loaded."
  1024. :group 'org-link-follow
  1025. :type 'boolean)
  1026. (defcustom org-return-follows-link nil
  1027. "Non-nil means, on links RET will follow the link.
  1028. Needs to be set before org.el is loaded."
  1029. :group 'org-link-follow
  1030. :type 'boolean)
  1031. (defcustom org-mouse-1-follows-link t
  1032. "Non-nil means, mouse-1 on a link will follow the link.
  1033. A longer mouse click will still set point. Does not wortk on XEmacs.
  1034. Needs to be set before org.el is loaded."
  1035. :group 'org-link-follow
  1036. :type 'boolean)
  1037. (defcustom org-mark-ring-length 4
  1038. "Number of different positions to be recorded in the ring
  1039. Changing this requires a restart of Emacs to work correctly."
  1040. :group 'org-link-follow
  1041. :type 'interger)
  1042. (defcustom org-link-frame-setup
  1043. '((vm . vm-visit-folder-other-frame)
  1044. (gnus . gnus-other-frame)
  1045. (file . find-file-other-window))
  1046. "Setup the frame configuration for following links.
  1047. When following a link with Emacs, it may often be useful to display
  1048. this link in another window or frame. This variable can be used to
  1049. set this up for the different types of links.
  1050. For VM, use any of
  1051. `vm-visit-folder'
  1052. `vm-visit-folder-other-frame'
  1053. For Gnus, use any of
  1054. `gnus'
  1055. `gnus-other-frame'
  1056. For FILE, use any of
  1057. `find-file'
  1058. `find-file-other-window'
  1059. `find-file-other-frame'
  1060. For the calendar, use the variable `calendar-setup'.
  1061. For BBDB, it is currently only possible to display the matches in
  1062. another window."
  1063. :group 'org-link-follow
  1064. :type '(list
  1065. (cons (const vm)
  1066. (choice
  1067. (const vm-visit-folder)
  1068. (const vm-visit-folder-other-window)
  1069. (const vm-visit-folder-other-frame)))
  1070. (cons (const gnus)
  1071. (choice
  1072. (const gnus)
  1073. (const gnus-other-frame)))
  1074. (cons (const file)
  1075. (choice
  1076. (const find-file)
  1077. (const find-file-other-window)
  1078. (const find-file-other-frame)))))
  1079. (defcustom org-display-internal-link-with-indirect-buffer nil
  1080. "Non-nil means, use indirect buffer to display infile links.
  1081. Activating internal links (from one location in a file to another location
  1082. in the same file) normally just jumps to the location. When the link is
  1083. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1084. another window. When this option is set, the other window actually displays
  1085. an indirect buffer clone of the current buffer, to avoid any visibility
  1086. changes to the current buffer."
  1087. :group 'org-link-follow
  1088. :type 'boolean)
  1089. (defcustom org-open-non-existing-files nil
  1090. "Non-nil means, `org-open-file' will open non-existing files.
  1091. When nil, an error will be generated."
  1092. :group 'org-link-follow
  1093. :type 'boolean)
  1094. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1095. "Function and arguments to call for following mailto links.
  1096. This is a list with the first element being a lisp function, and the
  1097. remaining elements being arguments to the function. In string arguments,
  1098. %a will be replaced by the address, and %s will be replaced by the subject
  1099. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1100. :group 'org-link-follow
  1101. :type '(choice
  1102. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1103. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1104. (const :tag "message-mail" (message-mail "%a" "%s"))
  1105. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1106. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1107. "Non-nil means, ask for confirmation before executing shell links.
  1108. Shell links can be dangerous: just think about a link
  1109. [[shell:rm -rf ~/*][Google Search]]
  1110. This link would show up in your Org-mode document as \"Google Search\",
  1111. but really it would remove your entire home directory.
  1112. Therefore we advise against setting this variable to nil.
  1113. Just change it to `y-or-n-p' of you want to confirm with a
  1114. single keystroke rather than having to type \"yes\"."
  1115. :group 'org-link-follow
  1116. :type '(choice
  1117. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1118. (const :tag "with y-or-n (faster)" y-or-n-p)
  1119. (const :tag "no confirmation (dangerous)" nil)))
  1120. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1121. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1122. Elisp links can be dangerous: just think about a link
  1123. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1124. This link would show up in your Org-mode document as \"Google Search\",
  1125. but really it would remove your entire home directory.
  1126. Therefore we advise against setting this variable to nil.
  1127. Just change it to `y-or-n-p' of you want to confirm with a
  1128. single keystroke rather than having to type \"yes\"."
  1129. :group 'org-link-follow
  1130. :type '(choice
  1131. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1132. (const :tag "with y-or-n (faster)" y-or-n-p)
  1133. (const :tag "no confirmation (dangerous)" nil)))
  1134. (defconst org-file-apps-defaults-gnu
  1135. '((remote . emacs)
  1136. (t . mailcap))
  1137. "Default file applications on a UNIX or GNU/Linux system.
  1138. See `org-file-apps'.")
  1139. (defconst org-file-apps-defaults-macosx
  1140. '((remote . emacs)
  1141. (t . "open %s")
  1142. ("ps" . "gv %s")
  1143. ("ps.gz" . "gv %s")
  1144. ("eps" . "gv %s")
  1145. ("eps.gz" . "gv %s")
  1146. ("dvi" . "xdvi %s")
  1147. ("fig" . "xfig %s"))
  1148. "Default file applications on a MacOS X system.
  1149. The system \"open\" is known as a default, but we use X11 applications
  1150. for some files for which the OS does not have a good default.
  1151. See `org-file-apps'.")
  1152. (defconst org-file-apps-defaults-windowsnt
  1153. (list
  1154. '(remote . emacs)
  1155. (cons t
  1156. (list (if (featurep 'xemacs)
  1157. 'mswindows-shell-execute
  1158. 'w32-shell-execute)
  1159. "open" 'file)))
  1160. "Default file applications on a Windows NT system.
  1161. The system \"open\" is used for most files.
  1162. See `org-file-apps'.")
  1163. (defcustom org-file-apps
  1164. '(
  1165. ("txt" . emacs)
  1166. ("tex" . emacs)
  1167. ("ltx" . emacs)
  1168. ("org" . emacs)
  1169. ("el" . emacs)
  1170. ("bib" . emacs)
  1171. )
  1172. "External applications for opening `file:path' items in a document.
  1173. Org-mode uses system defaults for different file types, but
  1174. you can use this variable to set the application for a given file
  1175. extension. The entries in this list are cons cells where the car identifies
  1176. files and the cdr the corresponding command. Possible values for the
  1177. file identifier are
  1178. \"ext\" A string identifying an extension
  1179. `directory' Matches a directory
  1180. `remote' Matches a remote file, accessible through tramp or efs.
  1181. Remote files most likely should be visited through Emacs
  1182. because external applications cannot handle such paths.
  1183. t Default for all remaining files
  1184. Possible values for the command are:
  1185. `emacs' The file will be visited by the current Emacs process.
  1186. `default' Use the default application for this file type.
  1187. string A command to be executed by a shell; %s will be replaced
  1188. by the path to the file.
  1189. sexp A Lisp form which will be evaluated. The file path will
  1190. be available in the Lisp variable `file'.
  1191. For more examples, see the system specific constants
  1192. `org-file-apps-defaults-macosx'
  1193. `org-file-apps-defaults-windowsnt'
  1194. `org-file-apps-defaults-gnu'."
  1195. :group 'org-link-follow
  1196. :type '(repeat
  1197. (cons (choice :value ""
  1198. (string :tag "Extension")
  1199. (const :tag "Default for unrecognized files" t)
  1200. (const :tag "Remote file" remote)
  1201. (const :tag "Links to a directory" directory))
  1202. (choice :value ""
  1203. (const :tag "Visit with Emacs" emacs)
  1204. (const :tag "Use system default" default)
  1205. (string :tag "Command")
  1206. (sexp :tag "Lisp form")))))
  1207. (defcustom org-mhe-search-all-folders nil
  1208. "Non-nil means, that the search for the mh-message will be extended to
  1209. all folders if the message cannot be found in the folder given in the link.
  1210. Searching all folders is very efficient with one of the search engines
  1211. supported by MH-E, but will be slow with pick."
  1212. :group 'org-link-follow
  1213. :type 'boolean)
  1214. (defgroup org-remember nil
  1215. "Options concerning interaction with remember.el."
  1216. :tag "Org Remember"
  1217. :group 'org)
  1218. (defcustom org-directory "~/org"
  1219. "Directory with org files.
  1220. This directory will be used as default to prompt for org files.
  1221. Used by the hooks for remember.el."
  1222. :group 'org-remember
  1223. :type 'directory)
  1224. (defcustom org-default-notes-file "~/.notes"
  1225. "Default target for storing notes.
  1226. Used by the hooks for remember.el. This can be a string, or nil to mean
  1227. the value of `remember-data-file'.
  1228. You can set this on a per-template basis with the variable
  1229. `org-remember-templates'."
  1230. :group 'org-remember
  1231. :type '(choice
  1232. (const :tag "Default from remember-data-file" nil)
  1233. file))
  1234. (defcustom org-remember-store-without-prompt t
  1235. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1236. In this case, you need `C-u C-c C-c' to get the prompts for
  1237. note file and headline.
  1238. When this variable is nil, `C-c C-c' give you the prompts, and
  1239. `C-u C-c C-c' trigger the fasttrack."
  1240. :group 'org-remember
  1241. :type 'boolean)
  1242. (defcustom org-remember-interactive-interface 'refile
  1243. "The interface to be used for interactive filing of remember notes.
  1244. This is only used when the interactive mode for selecting a filing
  1245. location is used (see the variable `org-remember-store-without-prompt').
  1246. Allowed vaues are:
  1247. outline The interface shows an outline of the relevant file
  1248. and the correct heading is found by moving through
  1249. the outline or by searching with incremental search.
  1250. outline-path-completion Headlines in the current buffer are offered via
  1251. completion.
  1252. refile Use the refile interface, and offer headlines,
  1253. possibly from different buffers."
  1254. :group 'org-remember
  1255. :type '(choice
  1256. (const :tag "Refile" refile)
  1257. (const :tag "Outline" outline)
  1258. (const :tag "Outline-path-completion" outline-path-completion)))
  1259. (defcustom org-goto-interface 'outline
  1260. "The default interface to be used for `org-goto'.
  1261. Allowed vaues are:
  1262. outline The interface shows an outline of the relevant file
  1263. and the correct heading is found by moving through
  1264. the outline or by searching with incremental search.
  1265. outline-path-completion Headlines in the current buffer are offered via
  1266. completion."
  1267. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1268. :type '(choice
  1269. (const :tag "Outline" outline)
  1270. (const :tag "Outline-path-completion" outline-path-completion)))
  1271. (defcustom org-remember-default-headline ""
  1272. "The headline that should be the default location in the notes file.
  1273. When filing remember notes, the cursor will start at that position.
  1274. You can set this on a per-template basis with the variable
  1275. `org-remember-templates'."
  1276. :group 'org-remember
  1277. :type 'string)
  1278. (defcustom org-remember-templates nil
  1279. "Templates for the creation of remember buffers.
  1280. When nil, just let remember make the buffer.
  1281. When not nil, this is a list of 5-element lists. In each entry, the first
  1282. element is the name of the template, which should be a single short word.
  1283. The second element is a character, a unique key to select this template.
  1284. The third element is the template. The fourth element is optional and can
  1285. specify a destination file for remember items created with this template.
  1286. The default file is given by `org-default-notes-file'. An optional fifth
  1287. element can specify the headline in that file that should be offered
  1288. first when the user is asked to file the entry. The default headline is
  1289. given in the variable `org-remember-default-headline'.
  1290. The template specifies the structure of the remember buffer. It should have
  1291. a first line starting with a star, to act as the org-mode headline.
  1292. Furthermore, the following %-escapes will be replaced with content:
  1293. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1294. A default value and a completion table ca be specified like this:
  1295. %^{prompt|default|completion2|completion3|...}
  1296. %t time stamp, date only
  1297. %T time stamp with date and time
  1298. %u, %U like the above, but inactive time stamps
  1299. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1300. You may define a prompt like %^{Please specify birthday}t
  1301. %n user name (taken from `user-full-name')
  1302. %a annotation, normally the link created with org-store-link
  1303. %i initial content, the region when remember is called with C-u.
  1304. If %i is indented, the entire inserted text will be indented
  1305. as well.
  1306. %c content of the clipboard, or current kill ring head
  1307. %^g prompt for tags, with completion on tags in target file
  1308. %^G prompt for tags, with completion all tags in all agenda files
  1309. %:keyword specific information for certain link types, see below
  1310. %[pathname] insert the contents of the file given by `pathname'
  1311. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1312. %! Store this note immediately after filling the template
  1313. %? After completing the template, position cursor here.
  1314. Apart from these general escapes, you can access information specific to the
  1315. link type that is created. For example, calling `remember' in emails or gnus
  1316. will record the author and the subject of the message, which you can access
  1317. with %:author and %:subject, respectively. Here is a complete list of what
  1318. is recorded for each link type.
  1319. Link type | Available information
  1320. -------------------+------------------------------------------------------
  1321. bbdb | %:type %:name %:company
  1322. vm, wl, mh, rmail | %:type %:subject %:message-id
  1323. | %:from %:fromname %:fromaddress
  1324. | %:to %:toname %:toaddress
  1325. | %:fromto (either \"to NAME\" or \"from NAME\")
  1326. gnus | %:group, for messages also all email fields
  1327. w3, w3m | %:type %:url
  1328. info | %:type %:file %:node
  1329. calendar | %:type %:date"
  1330. :group 'org-remember
  1331. :get (lambda (var) ; Make sure all entries have 5 elements
  1332. (mapcar (lambda (x)
  1333. (if (not (stringp (car x))) (setq x (cons "" x)))
  1334. (cond ((= (length x) 4) (append x '("")))
  1335. ((= (length x) 3) (append x '("" "")))
  1336. (t x)))
  1337. (default-value var)))
  1338. :type '(repeat
  1339. :tag "enabled"
  1340. (list :value ("" ?a "\n" nil nil)
  1341. (string :tag "Name")
  1342. (character :tag "Selection Key")
  1343. (string :tag "Template")
  1344. (choice
  1345. (file :tag "Destination file")
  1346. (const :tag "Prompt for file" nil))
  1347. (choice
  1348. (string :tag "Destination headline")
  1349. (const :tag "Selection interface for heading")))))
  1350. (defcustom org-reverse-note-order nil
  1351. "Non-nil means, store new notes at the beginning of a file or entry.
  1352. When nil, new notes will be filed to the end of a file or entry.
  1353. This can also be a list with cons cells of regular expressions that
  1354. are matched against file names, and values."
  1355. :group 'org-remember
  1356. :type '(choice
  1357. (const :tag "Reverse always" t)
  1358. (const :tag "Reverse never" nil)
  1359. (repeat :tag "By file name regexp"
  1360. (cons regexp boolean))))
  1361. (defcustom org-refile-targets nil
  1362. "Targets for refiling entries with \\[org-refile].
  1363. This is list of cons cells. Each cell contains:
  1364. - a specification of the files to be considered, either a list of files,
  1365. or a symbol whose function or value fields will be used to retrieve
  1366. a file name or a list of file names. Nil means, refile to a different
  1367. heading in the current buffer.
  1368. - A specification of how to find candidate refile targets. This may be
  1369. any of
  1370. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1371. This tag has to be present in all target headlines, inheritance will
  1372. not be considered.
  1373. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1374. todo keyword.
  1375. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1376. headlines that are refiling targets.
  1377. - a cons cell (:level . N). Any headline of level N is considered a target.
  1378. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1379. ;; FIXME: what if there are a var and func with same name???
  1380. :group 'org-remember
  1381. :type '(repeat
  1382. (cons
  1383. (choice :value org-agenda-files
  1384. (const :tag "All agenda files" org-agenda-files)
  1385. (const :tag "Current buffer" nil)
  1386. (function) (variable) (file))
  1387. (choice :tag "Identify target headline by"
  1388. (cons :tag "Specific tag" (const :tag) (string))
  1389. (cons :tag "TODO keyword" (const :todo) (string))
  1390. (cons :tag "Regular expression" (const :regexp) (regexp))
  1391. (cons :tag "Level number" (const :level) (integer))
  1392. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1393. (defcustom org-refile-use-outline-path nil
  1394. "Non-nil means, provide refile targets as paths.
  1395. So a level 3 headline will be available as level1/level2/level3.
  1396. When the value is `file', also include the file name (without directory)
  1397. into the path. When `full-file-path', include the full file path."
  1398. :group 'org-remember
  1399. :type '(choice
  1400. (const :tag "Not" nil)
  1401. (const :tag "Yes" t)
  1402. (const :tag "Start with file name" file)
  1403. (const :tag "Start with full file path" full-file-path)))
  1404. (defgroup org-todo nil
  1405. "Options concerning TODO items in Org-mode."
  1406. :tag "Org TODO"
  1407. :group 'org)
  1408. (defgroup org-progress nil
  1409. "Options concerning Progress logging in Org-mode."
  1410. :tag "Org Progress"
  1411. :group 'org-time)
  1412. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1413. "List of TODO entry keyword sequences and their interpretation.
  1414. \\<org-mode-map>This is a list of sequences.
  1415. Each sequence starts with a symbol, either `sequence' or `type',
  1416. indicating if the keywords should be interpreted as a sequence of
  1417. action steps, or as different types of TODO items. The first
  1418. keywords are states requiring action - these states will select a headline
  1419. for inclusion into the global TODO list Org-mode produces. If one of
  1420. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1421. signify that no further action is necessary. If \"|\" is not found,
  1422. the last keyword is treated as the only DONE state of the sequence.
  1423. The command \\[org-todo] cycles an entry through these states, and one
  1424. additional state where no keyword is present. For details about this
  1425. cycling, see the manual.
  1426. TODO keywords and interpretation can also be set on a per-file basis with
  1427. the special #+SEQ_TODO and #+TYP_TODO lines.
  1428. For backward compatibility, this variable may also be just a list
  1429. of keywords - in this case the interptetation (sequence or type) will be
  1430. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1431. :group 'org-todo
  1432. :group 'org-keywords
  1433. :type '(choice
  1434. (repeat :tag "Old syntax, just keywords"
  1435. (string :tag "Keyword"))
  1436. (repeat :tag "New syntax"
  1437. (cons
  1438. (choice
  1439. :tag "Interpretation"
  1440. (const :tag "Sequence (cycling hits every state)" sequence)
  1441. (const :tag "Type (cycling directly to DONE)" type))
  1442. (repeat
  1443. (string :tag "Keyword"))))))
  1444. (defvar org-todo-keywords-1 nil)
  1445. (make-variable-buffer-local 'org-todo-keywords-1)
  1446. (defvar org-todo-keywords-for-agenda nil)
  1447. (defvar org-done-keywords-for-agenda nil)
  1448. (defvar org-not-done-keywords nil)
  1449. (make-variable-buffer-local 'org-not-done-keywords)
  1450. (defvar org-done-keywords nil)
  1451. (make-variable-buffer-local 'org-done-keywords)
  1452. (defvar org-todo-heads nil)
  1453. (make-variable-buffer-local 'org-todo-heads)
  1454. (defvar org-todo-sets nil)
  1455. (make-variable-buffer-local 'org-todo-sets)
  1456. (defvar org-todo-log-states nil)
  1457. (make-variable-buffer-local 'org-todo-log-states)
  1458. (defvar org-todo-kwd-alist nil)
  1459. (make-variable-buffer-local 'org-todo-kwd-alist)
  1460. (defvar org-todo-key-alist nil)
  1461. (make-variable-buffer-local 'org-todo-key-alist)
  1462. (defvar org-todo-key-trigger nil)
  1463. (make-variable-buffer-local 'org-todo-key-trigger)
  1464. (defcustom org-todo-interpretation 'sequence
  1465. "Controls how TODO keywords are interpreted.
  1466. This variable is in principle obsolete and is only used for
  1467. backward compatibility, if the interpretation of todo keywords is
  1468. not given already in `org-todo-keywords'. See that variable for
  1469. more information."
  1470. :group 'org-todo
  1471. :group 'org-keywords
  1472. :type '(choice (const sequence)
  1473. (const type)))
  1474. (defcustom org-use-fast-todo-selection 'prefix
  1475. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1476. This variable describes if and under what circumstances the cycling
  1477. mechanism for TODO keywords will be replaced by a single-key, direct
  1478. selection scheme.
  1479. When nil, fast selection is never used.
  1480. When the symbol `prefix', it will be used when `org-todo' is called with
  1481. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1482. in an agenda buffer.
  1483. When t, fast selection is used by default. In this case, the prefix
  1484. argument forces cycling instead.
  1485. In all cases, the special interface is only used if access keys have actually
  1486. been assigned by the user, i.e. if keywords in the configuration are followed
  1487. by a letter in parenthesis, like TODO(t)."
  1488. :group 'org-todo
  1489. :type '(choice
  1490. (const :tag "Never" nil)
  1491. (const :tag "By default" t)
  1492. (const :tag "Only with C-u C-c C-t" prefix)))
  1493. (defcustom org-after-todo-state-change-hook nil
  1494. "Hook which is run after the state of a TODO item was changed.
  1495. The new state (a string with a TODO keyword, or nil) is available in the
  1496. Lisp variable `state'."
  1497. :group 'org-todo
  1498. :type 'hook)
  1499. (defcustom org-log-done nil
  1500. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1501. When the state of an entry is changed from nothing or a DONE state to
  1502. a not-done TODO state, remove a previous closing date.
  1503. This can also be a list of symbols indicating under which conditions
  1504. the time stamp recording the action should be annotated with a short note.
  1505. Valid members of this list are
  1506. done Offer to record a note when marking entries done
  1507. state Offer to record a note whenever changing the TODO state
  1508. of an item. This is only relevant if TODO keywords are
  1509. interpreted as sequence, see variable `org-todo-interpretation'.
  1510. When `state' is set, this includes tracking `done'.
  1511. clock-out Offer to record a note when clocking out of an item.
  1512. A separate window will then pop up and allow you to type a note.
  1513. After finishing with C-c C-c, the note will be added directly after the
  1514. timestamp, as a plain list item. See also the variable
  1515. `org-log-note-headings'.
  1516. Logging can also be configured on a per-file basis by adding one of
  1517. the following lines anywhere in the buffer:
  1518. #+STARTUP: logdone
  1519. #+STARTUP: nologging
  1520. #+STARTUP: lognotedone
  1521. #+STARTUP: lognotestate
  1522. #+STARTUP: lognoteclock-out
  1523. You can have local logging settings for a subtree by setting the LOGGING
  1524. property to one or more of these keywords."
  1525. :group 'org-todo
  1526. :group 'org-progress
  1527. :type '(choice
  1528. (const :tag "off" nil)
  1529. (const :tag "on" t)
  1530. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1531. (const :tag "when item is marked DONE" done)
  1532. (const :tag "when TODO state changes" state)
  1533. (const :tag "when clocking out" clock-out))))
  1534. (defcustom org-log-done-with-time t
  1535. "Non-nil means, the CLOSED time stamp will contain date and time.
  1536. When nil, only the date will be recorded."
  1537. :group 'org-progress
  1538. :type 'boolean)
  1539. (defcustom org-log-note-headings
  1540. '((done . "CLOSING NOTE %t")
  1541. (state . "State %-12s %t")
  1542. (clock-out . ""))
  1543. "Headings for notes added when clocking out or closing TODO items.
  1544. The value is an alist, with the car being a symbol indicating the note
  1545. context, and the cdr is the heading to be used. The heading may also be the
  1546. empty string.
  1547. %t in the heading will be replaced by a time stamp.
  1548. %s will be replaced by the new TODO state, in double quotes.
  1549. %u will be replaced by the user name.
  1550. %U will be replaced by the full user name."
  1551. :group 'org-todo
  1552. :group 'org-progress
  1553. :type '(list :greedy t
  1554. (cons (const :tag "Heading when closing an item" done) string)
  1555. (cons (const :tag
  1556. "Heading when changing todo state (todo sequence only)"
  1557. state) string)
  1558. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1559. (defcustom org-log-states-order-reversed t
  1560. "Non-nil means, the latest state change note will be directly after heading.
  1561. When nil, the notes will be orderer according to time."
  1562. :group 'org-todo
  1563. :group 'org-progress
  1564. :type 'boolean)
  1565. (defcustom org-log-repeat t
  1566. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1567. When nil, no note will be taken.
  1568. This option can also be set with on a per-file-basis with
  1569. #+STARTUP: logrepeat
  1570. #+STARTUP: nologrepeat
  1571. You can have local logging settings for a subtree by setting the LOGGING
  1572. property to one or more of these keywords."
  1573. :group 'org-todo
  1574. :group 'org-progress
  1575. :type 'boolean)
  1576. (defcustom org-clock-into-drawer 2
  1577. "Should clocking info be wrapped into a drawer?
  1578. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1579. If necessary, the drawer will be created.
  1580. When nil, the drawer will not be created, but used when present.
  1581. When an integer and the number of clocking entries in an item
  1582. reaches or exceeds this number, a drawer will be created."
  1583. :group 'org-todo
  1584. :group 'org-progress
  1585. :type '(choice
  1586. (const :tag "Always" t)
  1587. (const :tag "Only when drawer exists" nil)
  1588. (integer :tag "When at least N clock entries")))
  1589. (defcustom org-clock-out-when-done t
  1590. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1591. Nil means, clock will keep running until stopped explicitly with
  1592. `C-c C-x C-o', or until the clock is started in a different item."
  1593. :group 'org-progress
  1594. :type 'boolean)
  1595. (defcustom org-clock-in-switch-to-state nil
  1596. "Set task to a special todo state while clocking it.
  1597. The value should be the state to which the entry should be switched."
  1598. :group 'org-progress
  1599. :group 'org-todo
  1600. :type '(choice
  1601. (const :tag "Don't force a state" nil)
  1602. (string :tag "State")))
  1603. (defgroup org-priorities nil
  1604. "Priorities in Org-mode."
  1605. :tag "Org Priorities"
  1606. :group 'org-todo)
  1607. (defcustom org-highest-priority ?A
  1608. "The highest priority of TODO items. A character like ?A, ?B etc.
  1609. Must have a smaller ASCII number than `org-lowest-priority'."
  1610. :group 'org-priorities
  1611. :type 'character)
  1612. (defcustom org-lowest-priority ?C
  1613. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1614. Must have a larger ASCII number than `org-highest-priority'."
  1615. :group 'org-priorities
  1616. :type 'character)
  1617. (defcustom org-default-priority ?B
  1618. "The default priority of TODO items.
  1619. This is the priority an item get if no explicit priority is given."
  1620. :group 'org-priorities
  1621. :type 'character)
  1622. (defcustom org-priority-start-cycle-with-default t
  1623. "Non-nil means, start with default priority when starting to cycle.
  1624. When this is nil, the first step in the cycle will be (depending on the
  1625. command used) one higher or lower that the default priority."
  1626. :group 'org-priorities
  1627. :type 'boolean)
  1628. (defgroup org-time nil
  1629. "Options concerning time stamps and deadlines in Org-mode."
  1630. :tag "Org Time"
  1631. :group 'org)
  1632. (defcustom org-insert-labeled-timestamps-at-point nil
  1633. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1634. When nil, these labeled time stamps are forces into the second line of an
  1635. entry, just after the headline. When scheduling from the global TODO list,
  1636. the time stamp will always be forced into the second line."
  1637. :group 'org-time
  1638. :type 'boolean)
  1639. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1640. "Formats for `format-time-string' which are used for time stamps.
  1641. It is not recommended to change this constant.")
  1642. (defcustom org-time-stamp-rounding-minutes 0
  1643. "Number of minutes to round time stamps to upon insertion.
  1644. When zero, insert the time unmodified. Useful rounding numbers
  1645. should be factors of 60, so for example 5, 10, 15.
  1646. When this is not zero, you can still force an exact time-stamp by using
  1647. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1648. :group 'org-time
  1649. :type 'integer)
  1650. (defcustom org-display-custom-times nil
  1651. "Non-nil means, overlay custom formats over all time stamps.
  1652. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1653. To turn this on on a per-file basis, insert anywhere in the file:
  1654. #+STARTUP: customtime"
  1655. :group 'org-time
  1656. :set 'set-default
  1657. :type 'sexp)
  1658. (make-variable-buffer-local 'org-display-custom-times)
  1659. (defcustom org-time-stamp-custom-formats
  1660. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1661. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1662. These are overlayed over the default ISO format if the variable
  1663. `org-display-custom-times' is set. Time like %H:%M should be at the
  1664. end of the second format."
  1665. :group 'org-time
  1666. :type 'sexp)
  1667. (defun org-time-stamp-format (&optional long inactive)
  1668. "Get the right format for a time string."
  1669. (let ((f (if long (cdr org-time-stamp-formats)
  1670. (car org-time-stamp-formats))))
  1671. (if inactive
  1672. (concat "[" (substring f 1 -1) "]")
  1673. f)))
  1674. (defcustom org-read-date-prefer-future t
  1675. "Non-nil means, assume future for incomplete date input from user.
  1676. This affects the following situations:
  1677. 1. The user gives a day, but no month.
  1678. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1679. read this as the third of *next* month. However, if you enter \"17\",
  1680. it will be considered as *this* month.
  1681. 2. The user gives a month but not a year.
  1682. For example, if it is april and you enter \"feb 2\", this will be read
  1683. as feb 2, *next* year. \"May 5\", however, will be this year.
  1684. When this option is nil, the current month and year will always be used
  1685. as defaults."
  1686. :group 'org-time
  1687. :type 'boolean)
  1688. (defcustom org-read-date-display-live t
  1689. "Non-nil means, display current interpretation of date prompt live.
  1690. This display will be in an overlay, in the minibuffer."
  1691. :group 'org-time
  1692. :type 'boolean)
  1693. (defcustom org-read-date-popup-calendar t
  1694. "Non-nil means, pop up a calendar when prompting for a date.
  1695. In the calendar, the date can be selected with mouse-1. However, the
  1696. minibuffer will also be active, and you can simply enter the date as well.
  1697. When nil, only the minibuffer will be available."
  1698. :group 'org-time
  1699. :type 'boolean)
  1700. (if (fboundp 'defvaralias)
  1701. (defvaralias 'org-popup-calendar-for-date-prompt
  1702. 'org-read-date-popup-calendar))
  1703. (defcustom org-extend-today-until 0
  1704. "The hour when your day really ends.
  1705. This has influence for the following applications:
  1706. - When switching the agenda to \"today\". It it is still earlier than
  1707. the time given here, the day recognized as TODAY is actually yesterday.
  1708. - When a date is read from the user and it is still before the time given
  1709. here, the current date and time will be assumed to be yesterday, 23:59.
  1710. FIXME:
  1711. IMPORTANT: This is still a very experimental feature, it may disappear
  1712. again or it may be extended to mean more things."
  1713. :group 'org-time
  1714. :type 'number)
  1715. (defcustom org-edit-timestamp-down-means-later nil
  1716. "Non-nil means, S-down will increase the time in a time stamp.
  1717. When nil, S-up will increase."
  1718. :group 'org-time
  1719. :type 'boolean)
  1720. (defcustom org-calendar-follow-timestamp-change t
  1721. "Non-nil means, make the calendar window follow timestamp changes.
  1722. When a timestamp is modified and the calendar window is visible, it will be
  1723. moved to the new date."
  1724. :group 'org-time
  1725. :type 'boolean)
  1726. (defcustom org-clock-heading-function nil
  1727. "When non-nil, should be a function to create `org-clock-heading'.
  1728. This is the string shown in the mode line when a clock is running.
  1729. The function is called with point at the beginning of the headline."
  1730. :group 'org-time ; FIXME: Should we have a separate group????
  1731. :type 'function)
  1732. (defgroup org-tags nil
  1733. "Options concerning tags in Org-mode."
  1734. :tag "Org Tags"
  1735. :group 'org)
  1736. (defcustom org-tag-alist nil
  1737. "List of tags allowed in Org-mode files.
  1738. When this list is nil, Org-mode will base TAG input on what is already in the
  1739. buffer.
  1740. The value of this variable is an alist, the car of each entry must be a
  1741. keyword as a string, the cdr may be a character that is used to select
  1742. that tag through the fast-tag-selection interface.
  1743. See the manual for details."
  1744. :group 'org-tags
  1745. :type '(repeat
  1746. (choice
  1747. (cons (string :tag "Tag name")
  1748. (character :tag "Access char"))
  1749. (const :tag "Start radio group" (:startgroup))
  1750. (const :tag "End radio group" (:endgroup)))))
  1751. (defcustom org-use-fast-tag-selection 'auto
  1752. "Non-nil means, use fast tag selection scheme.
  1753. This is a special interface to select and deselect tags with single keys.
  1754. When nil, fast selection is never used.
  1755. When the symbol `auto', fast selection is used if and only if selection
  1756. characters for tags have been configured, either through the variable
  1757. `org-tag-alist' or through a #+TAGS line in the buffer.
  1758. When t, fast selection is always used and selection keys are assigned
  1759. automatically if necessary."
  1760. :group 'org-tags
  1761. :type '(choice
  1762. (const :tag "Always" t)
  1763. (const :tag "Never" nil)
  1764. (const :tag "When selection characters are configured" 'auto)))
  1765. (defcustom org-fast-tag-selection-single-key nil
  1766. "Non-nil means, fast tag selection exits after first change.
  1767. When nil, you have to press RET to exit it.
  1768. During fast tag selection, you can toggle this flag with `C-c'.
  1769. This variable can also have the value `expert'. In this case, the window
  1770. displaying the tags menu is not even shown, until you press C-c again."
  1771. :group 'org-tags
  1772. :type '(choice
  1773. (const :tag "No" nil)
  1774. (const :tag "Yes" t)
  1775. (const :tag "Expert" expert)))
  1776. (defvar org-fast-tag-selection-include-todo nil
  1777. "Non-nil means, fast tags selection interface will also offer TODO states.
  1778. This is an undocumented feature, you should not rely on it.")
  1779. (defcustom org-tags-column -80
  1780. "The column to which tags should be indented in a headline.
  1781. If this number is positive, it specifies the column. If it is negative,
  1782. it means that the tags should be flushright to that column. For example,
  1783. -80 works well for a normal 80 character screen."
  1784. :group 'org-tags
  1785. :type 'integer)
  1786. (defcustom org-auto-align-tags t
  1787. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1788. These operations change the length of a headline and therefore shift
  1789. the tags around. With this options turned on, after each such operation
  1790. the tags are again aligned to `org-tags-column'."
  1791. :group 'org-tags
  1792. :type 'boolean)
  1793. (defcustom org-use-tag-inheritance t
  1794. "Non-nil means, tags in levels apply also for sublevels.
  1795. When nil, only the tags directly given in a specific line apply there.
  1796. If you turn off this option, you very likely want to turn on the
  1797. companion option `org-tags-match-list-sublevels'."
  1798. :group 'org-tags
  1799. :type 'boolean)
  1800. (defcustom org-tags-match-list-sublevels nil
  1801. "Non-nil means list also sublevels of headlines matching tag search.
  1802. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1803. the sublevels of a headline matching a tag search often also match
  1804. the same search. Listing all of them can create very long lists.
  1805. Setting this variable to nil causes subtrees of a match to be skipped.
  1806. This option is off by default, because inheritance in on. If you turn
  1807. inheritance off, you very likely want to turn this option on.
  1808. As a special case, if the tag search is restricted to TODO items, the
  1809. value of this variable is ignored and sublevels are always checked, to
  1810. make sure all corresponding TODO items find their way into the list."
  1811. :group 'org-tags
  1812. :type 'boolean)
  1813. (defvar org-tags-history nil
  1814. "History of minibuffer reads for tags.")
  1815. (defvar org-last-tags-completion-table nil
  1816. "The last used completion table for tags.")
  1817. (defvar org-after-tags-change-hook nil
  1818. "Hook that is run after the tags in a line have changed.")
  1819. (defgroup org-properties nil
  1820. "Options concerning properties in Org-mode."
  1821. :tag "Org Properties"
  1822. :group 'org)
  1823. (defcustom org-property-format "%-10s %s"
  1824. "How property key/value pairs should be formatted by `indent-line'.
  1825. When `indent-line' hits a property definition, it will format the line
  1826. according to this format, mainly to make sure that the values are
  1827. lined-up with respect to each other."
  1828. :group 'org-properties
  1829. :type 'string)
  1830. (defcustom org-use-property-inheritance nil
  1831. "Non-nil means, properties apply also for sublevels.
  1832. This setting is only relevant during property searches, not when querying
  1833. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1834. you need to call `org-entry-get' with the inheritance flag.
  1835. Turning this on can cause significant overhead when doing a search, so
  1836. this is turned off by default.
  1837. When nil, only the properties directly given in the current entry count.
  1838. The value may also be a list of properties that shouldhave inheritance.
  1839. However, note that some special properties use inheritance under special
  1840. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1841. and the properties ending in \"_ALL\" when they are used as descriptor
  1842. for valid values of a property."
  1843. :group 'org-properties
  1844. :type '(choice
  1845. (const :tag "Not" nil)
  1846. (const :tag "Always" nil)
  1847. (repeat :tag "Specific properties" (string :tag "Property"))))
  1848. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1849. "The default column format, if no other format has been defined.
  1850. This variable can be set on the per-file basis by inserting a line
  1851. #+COLUMNS: %25ITEM ....."
  1852. :group 'org-properties
  1853. :type 'string)
  1854. (defcustom org-global-properties nil
  1855. "List of property/value pairs that can be inherited by any entry.
  1856. You can set buffer-local values for this by adding lines like
  1857. #+PROPERTY: NAME VALUE"
  1858. :group 'org-properties
  1859. :type '(repeat
  1860. (cons (string :tag "Property")
  1861. (string :tag "Value"))))
  1862. (defvar org-local-properties nil
  1863. "List of property/value pairs that can be inherited by any entry.
  1864. Valid for the current buffer.
  1865. This variable is populated from #+PROPERTY lines.")
  1866. (defgroup org-agenda nil
  1867. "Options concerning agenda views in Org-mode."
  1868. :tag "Org Agenda"
  1869. :group 'org)
  1870. (defvar org-category nil
  1871. "Variable used by org files to set a category for agenda display.
  1872. Such files should use a file variable to set it, for example
  1873. # -*- mode: org; org-category: \"ELisp\"
  1874. or contain a special line
  1875. #+CATEGORY: ELisp
  1876. If the file does not specify a category, then file's base name
  1877. is used instead.")
  1878. (make-variable-buffer-local 'org-category)
  1879. (defcustom org-agenda-files nil
  1880. "The files to be used for agenda display.
  1881. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1882. \\[org-remove-file]. You can also use customize to edit the list.
  1883. If an entry is a directory, all files in that directory that are matched by
  1884. `org-agenda-file-regexp' will be part of the file list.
  1885. If the value of the variable is not a list but a single file name, then
  1886. the list of agenda files is actually stored and maintained in that file, one
  1887. agenda file per line."
  1888. :group 'org-agenda
  1889. :type '(choice
  1890. (repeat :tag "List of files and directories" file)
  1891. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1892. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1893. "Regular expression to match files for `org-agenda-files'.
  1894. If any element in the list in that variable contains a directory instead
  1895. of a normal file, all files in that directory that are matched by this
  1896. regular expression will be included."
  1897. :group 'org-agenda
  1898. :type 'regexp)
  1899. (defcustom org-agenda-skip-unavailable-files nil
  1900. "t means to just skip non-reachable files in `org-agenda-files'.
  1901. Nil means to remove them, after a query, from the list."
  1902. :group 'org-agenda
  1903. :type 'boolean)
  1904. (defcustom org-agenda-multi-occur-extra-files nil
  1905. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1906. The files in `org-agenda-files' are always searched."
  1907. :group 'org-agenda
  1908. :type '(repeat file))
  1909. (defcustom org-agenda-confirm-kill 1
  1910. "When set, remote killing from the agenda buffer needs confirmation.
  1911. When t, a confirmation is always needed. When a number N, confirmation is
  1912. only needed when the text to be killed contains more than N non-white lines."
  1913. :group 'org-agenda
  1914. :type '(choice
  1915. (const :tag "Never" nil)
  1916. (const :tag "Always" t)
  1917. (number :tag "When more than N lines")))
  1918. (defcustom org-calendar-to-agenda-key [?c]
  1919. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1920. The command `org-calendar-goto-agenda' will be bound to this key. The
  1921. default is the character `c' because then `c' can be used to switch back and
  1922. forth between agenda and calendar."
  1923. :group 'org-agenda
  1924. :type 'sexp)
  1925. (defcustom org-agenda-compact-blocks nil
  1926. "Non-nil means, make the block agenda more compact.
  1927. This is done by leaving out unnecessary lines."
  1928. :group 'org-agenda
  1929. :type nil)
  1930. (defgroup org-agenda-export nil
  1931. "Options concerning exporting agenda views in Org-mode."
  1932. :tag "Org Agenda Export"
  1933. :group 'org-agenda)
  1934. (defcustom org-agenda-with-colors t
  1935. "Non-nil means, use colors in agenda views."
  1936. :group 'org-agenda-export
  1937. :type 'boolean)
  1938. (defcustom org-agenda-exporter-settings nil
  1939. "Alist of variable/value pairs that should be active during agenda export.
  1940. This is a good place to set uptions for ps-print and for htmlize."
  1941. :group 'org-agenda-export
  1942. :type '(repeat
  1943. (list
  1944. (variable)
  1945. (sexp :tag "Value"))))
  1946. (defcustom org-agenda-export-html-style ""
  1947. "The style specification for exported HTML Agenda files.
  1948. If this variable contains a string, it will replace the default <style>
  1949. section as produced by `htmlize'.
  1950. Since there are different ways of setting style information, this variable
  1951. needs to contain the full HTML structure to provide a style, including the
  1952. surrounding HTML tags. The style specifications should include definitions
  1953. the fonts used by the agenda, here is an example:
  1954. <style type=\"text/css\">
  1955. p { font-weight: normal; color: gray; }
  1956. .org-agenda-structure {
  1957. font-size: 110%;
  1958. color: #003399;
  1959. font-weight: 600;
  1960. }
  1961. .org-todo {
  1962. color: #cc6666;Week-agenda:
  1963. font-weight: bold;
  1964. }
  1965. .org-done {
  1966. color: #339933;
  1967. }
  1968. .title { text-align: center; }
  1969. .todo, .deadline { color: red; }
  1970. .done { color: green; }
  1971. </style>
  1972. or, if you want to keep the style in a file,
  1973. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1974. As the value of this option simply gets inserted into the HTML <head> header,
  1975. you can \"misuse\" it to also add other text to the header. However,
  1976. <style>...</style> is required, if not present the variable will be ignored."
  1977. :group 'org-agenda-export
  1978. :group 'org-export-html
  1979. :type 'string)
  1980. (defgroup org-agenda-custom-commands nil
  1981. "Options concerning agenda views in Org-mode."
  1982. :tag "Org Agenda Custom Commands"
  1983. :group 'org-agenda)
  1984. (defcustom org-agenda-custom-commands nil
  1985. "Custom commands for the agenda.
  1986. These commands will be offered on the splash screen displayed by the
  1987. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1988. (key desc type match options files)
  1989. key The key (one or more characters as a string) to be associated
  1990. with the command.
  1991. desc A description of the commend, when omitted or nil, a default
  1992. description is built using MATCH.
  1993. type The command type, any of the following symbols:
  1994. todo Entries with a specific TODO keyword, in all agenda files.
  1995. tags Tags match in all agenda files.
  1996. tags-todo Tags match in all agenda files, TODO entries only.
  1997. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1998. tags-tree Sparse tree with all tags matches in *current* file.
  1999. occur-tree Occur sparse tree for *current* file.
  2000. ... A user-defined function.
  2001. match What to search for:
  2002. - a single keyword for TODO keyword searches
  2003. - a tags match expression for tags searches
  2004. - a regular expression for occur searches
  2005. options A list of option settings, similar to that in a let form, so like
  2006. this: ((opt1 val1) (opt2 val2) ...)
  2007. files A list of files file to write the produced agenda buffer to
  2008. with the command `org-store-agenda-views'.
  2009. If a file name ends in \".html\", an HTML version of the buffer
  2010. is written out. If it ends in \".ps\", a postscript version is
  2011. produced. Otherwide, only the plain text is written to the file.
  2012. You can also define a set of commands, to create a composite agenda buffer.
  2013. In this case, an entry looks like this:
  2014. (key desc (cmd1 cmd2 ...) general-options file)
  2015. where
  2016. desc A description string to be displayed in the dispatcher menu.
  2017. cmd An agenda command, similar to the above. However, tree commands
  2018. are no allowed, but instead you can get agenda and global todo list.
  2019. So valid commands for a set are:
  2020. (agenda)
  2021. (alltodo)
  2022. (stuck)
  2023. (todo \"match\" options files)
  2024. (tags \"match\" options files)
  2025. (tags-todo \"match\" options files)
  2026. Each command can carry a list of options, and another set of options can be
  2027. given for the whole set of commands. Individual command options take
  2028. precedence over the general options.
  2029. When using several characters as key to a command, the first characters
  2030. are prefix commands. For the dispatcher to display useful information, you
  2031. should provide a description for the prefix, like
  2032. (setq org-agenda-custom-commands
  2033. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2034. (\"hl\" tags \"+HOME+Lisa\")
  2035. (\"hp\" tags \"+HOME+Peter\")
  2036. (\"hk\" tags \"+HOME+Kim\")))"
  2037. :group 'org-agenda-custom-commands
  2038. :type '(repeat
  2039. (choice :value ("a" "" tags "" nil)
  2040. (list :tag "Single command"
  2041. (string :tag "Access Key(s) ")
  2042. (option (string :tag "Description"))
  2043. (choice
  2044. (const :tag "Agenda" agenda)
  2045. (const :tag "TODO list" alltodo)
  2046. (const :tag "Stuck projects" stuck)
  2047. (const :tag "Tags search (all agenda files)" tags)
  2048. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2049. (const :tag "TODO keyword search (all agenda files)" todo)
  2050. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2051. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2052. (const :tag "Occur tree (current buffer)" occur-tree)
  2053. (sexp :tag "Other, user-defined function"))
  2054. (string :tag "Match")
  2055. (repeat :tag "Local options"
  2056. (list (variable :tag "Option") (sexp :tag "Value")))
  2057. (option (repeat :tag "Export" (file :tag "Export to"))))
  2058. (list :tag "Command series, all agenda files"
  2059. (string :tag "Access Key(s)")
  2060. (string :tag "Description ")
  2061. (repeat
  2062. (choice
  2063. (const :tag "Agenda" (agenda))
  2064. (const :tag "TODO list" (alltodo))
  2065. (const :tag "Stuck projects" (stuck))
  2066. (list :tag "Tags search"
  2067. (const :format "" tags)
  2068. (string :tag "Match")
  2069. (repeat :tag "Local options"
  2070. (list (variable :tag "Option")
  2071. (sexp :tag "Value"))))
  2072. (list :tag "Tags search, TODO entries only"
  2073. (const :format "" tags-todo)
  2074. (string :tag "Match")
  2075. (repeat :tag "Local options"
  2076. (list (variable :tag "Option")
  2077. (sexp :tag "Value"))))
  2078. (list :tag "TODO keyword search"
  2079. (const :format "" todo)
  2080. (string :tag "Match")
  2081. (repeat :tag "Local options"
  2082. (list (variable :tag "Option")
  2083. (sexp :tag "Value"))))
  2084. (list :tag "Other, user-defined function"
  2085. (symbol :tag "function")
  2086. (string :tag "Match")
  2087. (repeat :tag "Local options"
  2088. (list (variable :tag "Option")
  2089. (sexp :tag "Value"))))))
  2090. (repeat :tag "General options"
  2091. (list (variable :tag "Option")
  2092. (sexp :tag "Value")))
  2093. (option (repeat :tag "Export" (file :tag "Export to"))))
  2094. (cons :tag "Prefix key documentation"
  2095. (string :tag "Access Key(s)")
  2096. (string :tag "Description ")))))
  2097. (defcustom org-stuck-projects
  2098. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2099. "How to identify stuck projects.
  2100. This is a list of four items:
  2101. 1. A tags/todo matcher string that is used to identify a project.
  2102. The entire tree below a headline matched by this is considered one project.
  2103. 2. A list of TODO keywords identifying non-stuck projects.
  2104. If the project subtree contains any headline with one of these todo
  2105. keywords, the project is considered to be not stuck. If you specify
  2106. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2107. 3. A list of tags identifying non-stuck projects.
  2108. If the project subtree contains any headline with one of these tags,
  2109. the project is considered to be not stuck. If you specify \"*\" as
  2110. a tag, any tag will mark the project unstuck.
  2111. 4. An arbitrary regular expression matching non-stuck projects.
  2112. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2113. or `C-c a #' to produce the list."
  2114. :group 'org-agenda-custom-commands
  2115. :type '(list
  2116. (string :tag "Tags/TODO match to identify a project")
  2117. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2118. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2119. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2120. (defgroup org-agenda-skip nil
  2121. "Options concerning skipping parts of agenda files."
  2122. :tag "Org Agenda Skip"
  2123. :group 'org-agenda)
  2124. (defcustom org-agenda-todo-list-sublevels t
  2125. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2126. When nil, the sublevels of a TODO entry are not checked, resulting in
  2127. potentially much shorter TODO lists."
  2128. :group 'org-agenda-skip
  2129. :group 'org-todo
  2130. :type 'boolean)
  2131. (defcustom org-agenda-todo-ignore-with-date nil
  2132. "Non-nil means, don't show entries with a date in the global todo list.
  2133. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2134. but don't want them to show up in the TODO list.
  2135. When this is set, it also covers deadlines and scheduled items, the settings
  2136. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2137. will be ignored."
  2138. :group 'org-agenda-skip
  2139. :group 'org-todo
  2140. :type 'boolean)
  2141. (defcustom org-agenda-todo-ignore-scheduled nil
  2142. "Non-nil means, don't show scheduled entries in the global todo list.
  2143. The idea behind this is that by scheduling it, you have already taken care
  2144. of this item.
  2145. See also `org-agenda-todo-ignore-with-date'."
  2146. :group 'org-agenda-skip
  2147. :group 'org-todo
  2148. :type 'boolean)
  2149. (defcustom org-agenda-todo-ignore-deadlines nil
  2150. "Non-nil means, don't show near deadline entries in the global todo list.
  2151. Near means closer than `org-deadline-warning-days' days.
  2152. The idea behind this is that such items will appear in the agenda anyway.
  2153. See also `org-agenda-todo-ignore-with-date'."
  2154. :group 'org-agenda-skip
  2155. :group 'org-todo
  2156. :type 'boolean)
  2157. (defcustom org-agenda-skip-scheduled-if-done nil
  2158. "Non-nil means don't show scheduled items in agenda when they are done.
  2159. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2160. it applies only to the actual date of the scheduling. Warnings about
  2161. an item with a past scheduling dates are always turned off when the item
  2162. is DONE."
  2163. :group 'org-agenda-skip
  2164. :type 'boolean)
  2165. (defcustom org-agenda-skip-deadline-if-done nil
  2166. "Non-nil means don't show deadines when the corresponding item is done.
  2167. When nil, the deadline is still shown and should give you a happy feeling.
  2168. This is relevant for the daily/weekly agenda. And it applied only to the
  2169. actualy date of the deadline. Warnings about approching and past-due
  2170. deadlines are always turned off when the item is DONE."
  2171. :group 'org-agenda-skip
  2172. :type 'boolean)
  2173. (defcustom org-agenda-skip-timestamp-if-done nil
  2174. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2175. :group 'org-agenda-skip
  2176. :type 'boolean)
  2177. (defcustom org-timeline-show-empty-dates 3
  2178. "Non-nil means, `org-timeline' also shows dates without an entry.
  2179. When nil, only the days which actually have entries are shown.
  2180. When t, all days between the first and the last date are shown.
  2181. When an integer, show also empty dates, but if there is a gap of more than
  2182. N days, just insert a special line indicating the size of the gap."
  2183. :group 'org-agenda-skip
  2184. :type '(choice
  2185. (const :tag "None" nil)
  2186. (const :tag "All" t)
  2187. (number :tag "at most")))
  2188. (defgroup org-agenda-startup nil
  2189. "Options concerning initial settings in the Agenda in Org Mode."
  2190. :tag "Org Agenda Startup"
  2191. :group 'org-agenda)
  2192. (defcustom org-finalize-agenda-hook nil
  2193. "Hook run just before displaying an agenda buffer."
  2194. :group 'org-agenda-startup
  2195. :type 'hook)
  2196. (defcustom org-agenda-mouse-1-follows-link nil
  2197. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2198. A longer mouse click will still set point. Does not wortk on XEmacs.
  2199. Needs to be set before org.el is loaded."
  2200. :group 'org-agenda-startup
  2201. :type 'boolean)
  2202. (defcustom org-agenda-start-with-follow-mode nil
  2203. "The initial value of follow-mode in a newly created agenda window."
  2204. :group 'org-agenda-startup
  2205. :type 'boolean)
  2206. (defgroup org-agenda-windows nil
  2207. "Options concerning the windows used by the Agenda in Org Mode."
  2208. :tag "Org Agenda Windows"
  2209. :group 'org-agenda)
  2210. (defcustom org-agenda-window-setup 'reorganize-frame
  2211. "How the agenda buffer should be displayed.
  2212. Possible values for this option are:
  2213. current-window Show agenda in the current window, keeping all other windows.
  2214. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2215. other-window Use `switch-to-buffer-other-window' to display agenda.
  2216. reorganize-frame Show only two windows on the current frame, the current
  2217. window and the agenda.
  2218. See also the variable `org-agenda-restore-windows-after-quit'."
  2219. :group 'org-agenda-windows
  2220. :type '(choice
  2221. (const current-window)
  2222. (const other-frame)
  2223. (const other-window)
  2224. (const reorganize-frame)))
  2225. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2226. "The min and max height of the agenda window as a fraction of frame height.
  2227. The value of the variable is a cons cell with two numbers between 0 and 1.
  2228. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2229. :group 'org-agenda-windows
  2230. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2231. (defcustom org-agenda-restore-windows-after-quit nil
  2232. "Non-nil means, restore window configuration open exiting agenda.
  2233. Before the window configuration is changed for displaying the agenda,
  2234. the current status is recorded. When the agenda is exited with
  2235. `q' or `x' and this option is set, the old state is restored. If
  2236. `org-agenda-window-setup' is `other-frame', the value of this
  2237. option will be ignored.."
  2238. :group 'org-agenda-windows
  2239. :type 'boolean)
  2240. (defcustom org-indirect-buffer-display 'other-window
  2241. "How should indirect tree buffers be displayed?
  2242. This applies to indirect buffers created with the commands
  2243. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2244. Valid values are:
  2245. current-window Display in the current window
  2246. other-window Just display in another window.
  2247. dedicated-frame Create one new frame, and re-use it each time.
  2248. new-frame Make a new frame each time. Note that in this case
  2249. previously-made indirect buffers are kept, and you need to
  2250. kill these buffers yourself."
  2251. :group 'org-structure
  2252. :group 'org-agenda-windows
  2253. :type '(choice
  2254. (const :tag "In current window" current-window)
  2255. (const :tag "In current frame, other window" other-window)
  2256. (const :tag "Each time a new frame" new-frame)
  2257. (const :tag "One dedicated frame" dedicated-frame)))
  2258. (defgroup org-agenda-daily/weekly nil
  2259. "Options concerning the daily/weekly agenda."
  2260. :tag "Org Agenda Daily/Weekly"
  2261. :group 'org-agenda)
  2262. (defcustom org-agenda-ndays 7
  2263. "Number of days to include in overview display.
  2264. Should be 1 or 7."
  2265. :group 'org-agenda-daily/weekly
  2266. :type 'number)
  2267. (defcustom org-agenda-start-on-weekday 1
  2268. "Non-nil means, start the overview always on the specified weekday.
  2269. 0 denotes Sunday, 1 denotes Monday etc.
  2270. When nil, always start on the current day."
  2271. :group 'org-agenda-daily/weekly
  2272. :type '(choice (const :tag "Today" nil)
  2273. (number :tag "Weekday No.")))
  2274. (defcustom org-agenda-show-all-dates t
  2275. "Non-nil means, `org-agenda' shows every day in the selected range.
  2276. When nil, only the days which actually have entries are shown."
  2277. :group 'org-agenda-daily/weekly
  2278. :type 'boolean)
  2279. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2280. "Format string for displaying dates in the agenda.
  2281. Used by the daily/weekly agenda and by the timeline. This should be
  2282. a format string understood by `format-time-string', or a function returning
  2283. the formatted date as a string. The function must take a single argument,
  2284. a calendar-style date list like (month day year)."
  2285. :group 'org-agenda-daily/weekly
  2286. :type '(choice
  2287. (string :tag "Format string")
  2288. (function :tag "Function")))
  2289. (defun org-agenda-format-date-aligned (date)
  2290. "Format a date string for display in the daily/weekly agenda, or timeline.
  2291. This function makes sure that dates are aligned for easy reading."
  2292. (format "%-9s %2d %s %4d"
  2293. (calendar-day-name date)
  2294. (extract-calendar-day date)
  2295. (calendar-month-name (extract-calendar-month date))
  2296. (extract-calendar-year date)))
  2297. (defcustom org-agenda-include-diary nil
  2298. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2299. :group 'org-agenda-daily/weekly
  2300. :type 'boolean)
  2301. (defcustom org-agenda-include-all-todo nil
  2302. "Set means weekly/daily agenda will always contain all TODO entries.
  2303. The TODO entries will be listed at the top of the agenda, before
  2304. the entries for specific days."
  2305. :group 'org-agenda-daily/weekly
  2306. :type 'boolean)
  2307. (defcustom org-agenda-repeating-timestamp-show-all t
  2308. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2309. When nil, only one occurence is shown, either today or the
  2310. nearest into the future."
  2311. :group 'org-agenda-daily/weekly
  2312. :type 'boolean)
  2313. (defcustom org-deadline-warning-days 14
  2314. "No. of days before expiration during which a deadline becomes active.
  2315. This variable governs the display in sparse trees and in the agenda.
  2316. When negative, it means use this number (the absolute value of it)
  2317. even if a deadline has a different individual lead time specified."
  2318. :group 'org-time
  2319. :group 'org-agenda-daily/weekly
  2320. :type 'number)
  2321. (defcustom org-scheduled-past-days 10000
  2322. "No. of days to continue listing scheduled items that are not marked DONE.
  2323. When an item is scheduled on a date, it shows up in the agenda on this
  2324. day and will be listed until it is marked done for the number of days
  2325. given here."
  2326. :group 'org-agenda-daily/weekly
  2327. :type 'number)
  2328. (defgroup org-agenda-time-grid nil
  2329. "Options concerning the time grid in the Org-mode Agenda."
  2330. :tag "Org Agenda Time Grid"
  2331. :group 'org-agenda)
  2332. (defcustom org-agenda-use-time-grid t
  2333. "Non-nil means, show a time grid in the agenda schedule.
  2334. A time grid is a set of lines for specific times (like every two hours between
  2335. 8:00 and 20:00). The items scheduled for a day at specific times are
  2336. sorted in between these lines.
  2337. For details about when the grid will be shown, and what it will look like, see
  2338. the variable `org-agenda-time-grid'."
  2339. :group 'org-agenda-time-grid
  2340. :type 'boolean)
  2341. (defcustom org-agenda-time-grid
  2342. '((daily today require-timed)
  2343. "----------------"
  2344. (800 1000 1200 1400 1600 1800 2000))
  2345. "The settings for time grid for agenda display.
  2346. This is a list of three items. The first item is again a list. It contains
  2347. symbols specifying conditions when the grid should be displayed:
  2348. daily if the agenda shows a single day
  2349. weekly if the agenda shows an entire week
  2350. today show grid on current date, independent of daily/weekly display
  2351. require-timed show grid only if at least one item has a time specification
  2352. The second item is a string which will be places behing the grid time.
  2353. The third item is a list of integers, indicating the times that should have
  2354. a grid line."
  2355. :group 'org-agenda-time-grid
  2356. :type
  2357. '(list
  2358. (set :greedy t :tag "Grid Display Options"
  2359. (const :tag "Show grid in single day agenda display" daily)
  2360. (const :tag "Show grid in weekly agenda display" weekly)
  2361. (const :tag "Always show grid for today" today)
  2362. (const :tag "Show grid only if any timed entries are present"
  2363. require-timed)
  2364. (const :tag "Skip grid times already present in an entry"
  2365. remove-match))
  2366. (string :tag "Grid String")
  2367. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2368. (defgroup org-agenda-sorting nil
  2369. "Options concerning sorting in the Org-mode Agenda."
  2370. :tag "Org Agenda Sorting"
  2371. :group 'org-agenda)
  2372. (defconst org-sorting-choice
  2373. '(choice
  2374. (const time-up) (const time-down)
  2375. (const category-keep) (const category-up) (const category-down)
  2376. (const tag-down) (const tag-up)
  2377. (const priority-up) (const priority-down))
  2378. "Sorting choices.")
  2379. (defcustom org-agenda-sorting-strategy
  2380. '((agenda time-up category-keep priority-down)
  2381. (todo category-keep priority-down)
  2382. (tags category-keep priority-down))
  2383. "Sorting structure for the agenda items of a single day.
  2384. This is a list of symbols which will be used in sequence to determine
  2385. if an entry should be listed before another entry. The following
  2386. symbols are recognized:
  2387. time-up Put entries with time-of-day indications first, early first
  2388. time-down Put entries with time-of-day indications first, late first
  2389. category-keep Keep the default order of categories, corresponding to the
  2390. sequence in `org-agenda-files'.
  2391. category-up Sort alphabetically by category, A-Z.
  2392. category-down Sort alphabetically by category, Z-A.
  2393. tag-up Sort alphabetically by last tag, A-Z.
  2394. tag-down Sort alphabetically by last tag, Z-A.
  2395. priority-up Sort numerically by priority, high priority last.
  2396. priority-down Sort numerically by priority, high priority first.
  2397. The different possibilities will be tried in sequence, and testing stops
  2398. if one comparison returns a \"not-equal\". For example, the default
  2399. '(time-up category-keep priority-down)
  2400. means: Pull out all entries having a specified time of day and sort them,
  2401. in order to make a time schedule for the current day the first thing in the
  2402. agenda listing for the day. Of the entries without a time indication, keep
  2403. the grouped in categories, don't sort the categories, but keep them in
  2404. the sequence given in `org-agenda-files'. Within each category sort by
  2405. priority.
  2406. Leaving out `category-keep' would mean that items will be sorted across
  2407. categories by priority.
  2408. Instead of a single list, this can also be a set of list for specific
  2409. contents, with a context symbol in the car of the list, any of
  2410. `agenda', `todo', `tags' for the corresponding agenda views."
  2411. :group 'org-agenda-sorting
  2412. :type `(choice
  2413. (repeat :tag "General" ,org-sorting-choice)
  2414. (list :tag "Individually"
  2415. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2416. (repeat ,org-sorting-choice))
  2417. (cons (const :tag "Strategy for TODO lists" todo)
  2418. (repeat ,org-sorting-choice))
  2419. (cons (const :tag "Strategy for Tags matches" tags)
  2420. (repeat ,org-sorting-choice)))))
  2421. (defcustom org-sort-agenda-notime-is-late t
  2422. "Non-nil means, items without time are considered late.
  2423. This is only relevant for sorting. When t, items which have no explicit
  2424. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2425. do have a time. When nil, the default time is before 0:00. You can use this
  2426. option to decide if the schedule for today should come before or after timeless
  2427. agenda entries."
  2428. :group 'org-agenda-sorting
  2429. :type 'boolean)
  2430. (defgroup org-agenda-line-format nil
  2431. "Options concerning the entry prefix in the Org-mode agenda display."
  2432. :tag "Org Agenda Line Format"
  2433. :group 'org-agenda)
  2434. (defcustom org-agenda-prefix-format
  2435. '((agenda . " %-12:c%?-12t% s")
  2436. (timeline . " % s")
  2437. (todo . " %-12:c")
  2438. (tags . " %-12:c"))
  2439. "Format specifications for the prefix of items in the agenda views.
  2440. An alist with four entries, for the different agenda types. The keys to the
  2441. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2442. are format strings.
  2443. This format works similar to a printf format, with the following meaning:
  2444. %c the category of the item, \"Diary\" for entries from the diary, or
  2445. as given by the CATEGORY keyword or derived from the file name.
  2446. %T the *last* tag of the item. Last because inherited tags come
  2447. first in the list.
  2448. %t the time-of-day specification if one applies to the entry, in the
  2449. format HH:MM
  2450. %s Scheduling/Deadline information, a short string
  2451. All specifiers work basically like the standard `%s' of printf, but may
  2452. contain two additional characters: A question mark just after the `%' and
  2453. a whitespace/punctuation character just before the final letter.
  2454. If the first character after `%' is a question mark, the entire field
  2455. will only be included if the corresponding value applies to the
  2456. current entry. This is useful for fields which should have fixed
  2457. width when present, but zero width when absent. For example,
  2458. \"%?-12t\" will result in a 12 character time field if a time of the
  2459. day is specified, but will completely disappear in entries which do
  2460. not contain a time.
  2461. If there is punctuation or whitespace character just before the final
  2462. format letter, this character will be appended to the field value if
  2463. the value is not empty. For example, the format \"%-12:c\" leads to
  2464. \"Diary: \" if the category is \"Diary\". If the category were be
  2465. empty, no additional colon would be interted.
  2466. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2467. - Indent the line with two space characters
  2468. - Give the category in a 12 chars wide field, padded with whitespace on
  2469. the right (because of `-'). Append a colon if there is a category
  2470. (because of `:').
  2471. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2472. time, don't put in an empty field, just skip it (because of '?').
  2473. - Finally, put the scheduling information and append a whitespace.
  2474. As another example, if you don't want the time-of-day of entries in
  2475. the prefix, you could use:
  2476. (setq org-agenda-prefix-format \" %-11:c% s\")
  2477. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2478. `org-agenda-remove-tags'."
  2479. :type '(choice
  2480. (string :tag "General format")
  2481. (list :greedy t :tag "View dependent"
  2482. (cons (const agenda) (string :tag "Format"))
  2483. (cons (const timeline) (string :tag "Format"))
  2484. (cons (const todo) (string :tag "Format"))
  2485. (cons (const tags) (string :tag "Format"))))
  2486. :group 'org-agenda-line-format)
  2487. (defvar org-prefix-format-compiled nil
  2488. "The compiled version of the most recently used prefix format.
  2489. See the variable `org-agenda-prefix-format'.")
  2490. (defcustom org-agenda-todo-keyword-format "%-1s"
  2491. "Format for the TODO keyword in agenda lines.
  2492. Set this to something like \"%-12s\" if you want all TODO keywords
  2493. to occupy a fixed space in the agenda display."
  2494. :group 'org-agenda-line-format
  2495. :type 'string)
  2496. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2497. "Text preceeding scheduled items in the agenda view.
  2498. This is a list with two strings. The first applies when the item is
  2499. scheduled on the current day. The second applies when it has been scheduled
  2500. previously, it may contain a %d to capture how many days ago the item was
  2501. scheduled."
  2502. :group 'org-agenda-line-format
  2503. :type '(list
  2504. (string :tag "Scheduled today ")
  2505. (string :tag "Scheduled previously")))
  2506. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2507. "Text preceeding deadline items in the agenda view.
  2508. This is a list with two strings. The first applies when the item has its
  2509. deadline on the current day. The second applies when it is in the past or
  2510. in the future, it may contain %d to capture how many days away the deadline
  2511. is (was)."
  2512. :group 'org-agenda-line-format
  2513. :type '(list
  2514. (string :tag "Deadline today ")
  2515. (string :tag "Deadline relative")))
  2516. (defcustom org-agenda-remove-times-when-in-prefix t
  2517. "Non-nil means, remove duplicate time specifications in agenda items.
  2518. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2519. time-of-day specification in a headline or diary entry is extracted and
  2520. placed into the prefix. If this option is non-nil, the original specification
  2521. \(a timestamp or -range, or just a plain time(range) specification like
  2522. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2523. cluttered.
  2524. The option can be t or nil. It may also be the symbol `beg', indicating
  2525. that the time should only be removed what it is located at the beginning of
  2526. the headline/diary entry."
  2527. :group 'org-agenda-line-format
  2528. :type '(choice
  2529. (const :tag "Always" t)
  2530. (const :tag "Never" nil)
  2531. (const :tag "When at beginning of entry" beg)))
  2532. (defcustom org-agenda-default-appointment-duration nil
  2533. "Default duration for appointments that only have a starting time.
  2534. When nil, no duration is specified in such cases.
  2535. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2536. :group 'org-agenda-line-format
  2537. :type '(choice
  2538. (integer :tag "Minutes")
  2539. (const :tag "No default duration")))
  2540. (defcustom org-agenda-remove-tags nil
  2541. "Non-nil means, remove the tags from the headline copy in the agenda.
  2542. When this is the symbol `prefix', only remove tags when
  2543. `org-agenda-prefix-format' contains a `%T' specifier."
  2544. :group 'org-agenda-line-format
  2545. :type '(choice
  2546. (const :tag "Always" t)
  2547. (const :tag "Never" nil)
  2548. (const :tag "When prefix format contains %T" prefix)))
  2549. (if (fboundp 'defvaralias)
  2550. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2551. 'org-agenda-remove-tags))
  2552. (defcustom org-agenda-tags-column -80
  2553. "Shift tags in agenda items to this column.
  2554. If this number is positive, it specifies the column. If it is negative,
  2555. it means that the tags should be flushright to that column. For example,
  2556. -80 works well for a normal 80 character screen."
  2557. :group 'org-agenda-line-format
  2558. :type 'integer)
  2559. (if (fboundp 'defvaralias)
  2560. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2561. (defcustom org-agenda-fontify-priorities t
  2562. "Non-nil means, highlight low and high priorities in agenda.
  2563. When t, the highest priority entries are bold, lowest priority italic.
  2564. This may also be an association list of priority faces. The face may be
  2565. a names face, or a list like `(:background \"Red\")'."
  2566. :group 'org-agenda-line-format
  2567. :type '(choice
  2568. (const :tag "Never" nil)
  2569. (const :tag "Defaults" t)
  2570. (repeat :tag "Specify"
  2571. (list (character :tag "Priority" :value ?A)
  2572. (sexp :tag "face")))))
  2573. (defgroup org-latex nil
  2574. "Options for embedding LaTeX code into Org-mode"
  2575. :tag "Org LaTeX"
  2576. :group 'org)
  2577. (defcustom org-format-latex-options
  2578. '(:foreground default :background default :scale 1.0
  2579. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2580. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2581. "Options for creating images from LaTeX fragments.
  2582. This is a property list with the following properties:
  2583. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2584. `default' means use the forground of the default face.
  2585. :background the background color, or \"Transparent\".
  2586. `default' means use the background of the default face.
  2587. :scale a scaling factor for the size of the images
  2588. :html-foreground, :html-background, :html-scale
  2589. The same numbers for HTML export.
  2590. :matchers a list indicating which matchers should be used to
  2591. find LaTeX fragments. Valid members of this list are:
  2592. \"begin\" find environments
  2593. \"$\" find math expressions surrounded by $...$
  2594. \"$$\" find math expressions surrounded by $$....$$
  2595. \"\\(\" find math expressions surrounded by \\(...\\)
  2596. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2597. :group 'org-latex
  2598. :type 'plist)
  2599. (defcustom org-format-latex-header "\\documentclass{article}
  2600. \\usepackage{fullpage} % do not remove
  2601. \\usepackage{amssymb}
  2602. \\usepackage[usenames]{color}
  2603. \\usepackage{amsmath}
  2604. \\usepackage{latexsym}
  2605. \\usepackage[mathscr]{eucal}
  2606. \\pagestyle{empty} % do not remove"
  2607. "The document header used for processing LaTeX fragments."
  2608. :group 'org-latex
  2609. :type 'string)
  2610. (defgroup org-export nil
  2611. "Options for exporting org-listings."
  2612. :tag "Org Export"
  2613. :group 'org)
  2614. (defgroup org-export-general nil
  2615. "General options for exporting Org-mode files."
  2616. :tag "Org Export General"
  2617. :group 'org-export)
  2618. ;; FIXME
  2619. (defvar org-export-publishing-directory nil)
  2620. (defcustom org-export-with-special-strings t
  2621. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2622. When this option is turned on, these strings will be exported as:
  2623. Org HTML LaTeX
  2624. -----+----------+--------
  2625. \\- &shy; \\-
  2626. -- &ndash; --
  2627. --- &mdash; ---
  2628. ... &hellip; \ldots
  2629. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2630. :group 'org-export-translation
  2631. :type 'boolean)
  2632. (defcustom org-export-language-setup
  2633. '(("en" "Author" "Date" "Table of Contents")
  2634. ("cs" "Autor" "Datum" "Obsah")
  2635. ("da" "Ophavsmand" "Dato" "Indhold")
  2636. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2637. ("es" "Autor" "Fecha" "\xcdndice")
  2638. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2639. ("it" "Autore" "Data" "Indice")
  2640. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2641. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2642. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2643. "Terms used in export text, translated to different languages.
  2644. Use the variable `org-export-default-language' to set the language,
  2645. or use the +OPTION lines for a per-file setting."
  2646. :group 'org-export-general
  2647. :type '(repeat
  2648. (list
  2649. (string :tag "HTML language tag")
  2650. (string :tag "Author")
  2651. (string :tag "Date")
  2652. (string :tag "Table of Contents"))))
  2653. (defcustom org-export-default-language "en"
  2654. "The default language of HTML export, as a string.
  2655. This should have an association in `org-export-language-setup'."
  2656. :group 'org-export-general
  2657. :type 'string)
  2658. (defcustom org-export-skip-text-before-1st-heading t
  2659. "Non-nil means, skip all text before the first headline when exporting.
  2660. When nil, that text is exported as well."
  2661. :group 'org-export-general
  2662. :type 'boolean)
  2663. (defcustom org-export-headline-levels 3
  2664. "The last level which is still exported as a headline.
  2665. Inferior levels will produce itemize lists when exported.
  2666. Note that a numeric prefix argument to an exporter function overrides
  2667. this setting.
  2668. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2669. :group 'org-export-general
  2670. :type 'number)
  2671. (defcustom org-export-with-section-numbers t
  2672. "Non-nil means, add section numbers to headlines when exporting.
  2673. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2674. :group 'org-export-general
  2675. :type 'boolean)
  2676. (defcustom org-export-with-toc t
  2677. "Non-nil means, create a table of contents in exported files.
  2678. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2679. When an integer, include levels up to N in the toc, this may then be
  2680. different from `org-export-headline-levels', but it will not be allowed
  2681. to be larger than the number of headline levels.
  2682. When nil, no table of contents is made.
  2683. Headlines which contain any TODO items will be marked with \"(*)\" in
  2684. ASCII export, and with red color in HTML output, if the option
  2685. `org-export-mark-todo-in-toc' is set.
  2686. In HTML output, the TOC will be clickable.
  2687. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2688. or \"toc:3\"."
  2689. :group 'org-export-general
  2690. :type '(choice
  2691. (const :tag "No Table of Contents" nil)
  2692. (const :tag "Full Table of Contents" t)
  2693. (integer :tag "TOC to level")))
  2694. (defcustom org-export-mark-todo-in-toc nil
  2695. "Non-nil means, mark TOC lines that contain any open TODO items."
  2696. :group 'org-export-general
  2697. :type 'boolean)
  2698. (defcustom org-export-preserve-breaks nil
  2699. "Non-nil means, preserve all line breaks when exporting.
  2700. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2701. export, line breaks will always be preserved, regardless of this variable.
  2702. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2703. :group 'org-export-general
  2704. :type 'boolean)
  2705. (defcustom org-export-with-archived-trees 'headline
  2706. "Whether subtrees with the ARCHIVE tag should be exported.
  2707. This can have three different values
  2708. nil Do not export, pretend this tree is not present
  2709. t Do export the entire tree
  2710. headline Only export the headline, but skip the tree below it."
  2711. :group 'org-export-general
  2712. :group 'org-archive
  2713. :type '(choice
  2714. (const :tag "not at all" nil)
  2715. (const :tag "headline only" 'headline)
  2716. (const :tag "entirely" t)))
  2717. (defcustom org-export-author-info t
  2718. "Non-nil means, insert author name and email into the exported file.
  2719. This option can also be set with the +OPTIONS line,
  2720. e.g. \"author-info:nil\"."
  2721. :group 'org-export-general
  2722. :type 'boolean)
  2723. (defcustom org-export-time-stamp-file t
  2724. "Non-nil means, insert a time stamp into the exported file.
  2725. The time stamp shows when the file was created.
  2726. This option can also be set with the +OPTIONS line,
  2727. e.g. \"timestamp:nil\"."
  2728. :group 'org-export-general
  2729. :type 'boolean)
  2730. (defcustom org-export-with-timestamps t
  2731. "If nil, do not export time stamps and associated keywords."
  2732. :group 'org-export-general
  2733. :type 'boolean)
  2734. (defcustom org-export-remove-timestamps-from-toc t
  2735. "If nil, remove timestamps from the table of contents entries."
  2736. :group 'org-export-general
  2737. :type 'boolean)
  2738. (defcustom org-export-with-tags 'not-in-toc
  2739. "If nil, do not export tags, just remove them from headlines.
  2740. If this is the symbol `not-in-toc', tags will be removed from table of
  2741. contents entries, but still be shown in the headlines of the document.
  2742. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2743. :group 'org-export-general
  2744. :type '(choice
  2745. (const :tag "Off" nil)
  2746. (const :tag "Not in TOC" not-in-toc)
  2747. (const :tag "On" t)))
  2748. (defcustom org-export-with-drawers nil
  2749. "Non-nil means, export with drawers like the property drawer.
  2750. When t, all drawers are exported. This may also be a list of
  2751. drawer names to export."
  2752. :group 'org-export-general
  2753. :type '(choice
  2754. (const :tag "All drawers" t)
  2755. (const :tag "None" nil)
  2756. (repeat :tag "Selected drawers"
  2757. (string :tag "Drawer name"))))
  2758. (defgroup org-export-translation nil
  2759. "Options for translating special ascii sequences for the export backends."
  2760. :tag "Org Export Translation"
  2761. :group 'org-export)
  2762. (defcustom org-export-with-emphasize t
  2763. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2764. If the export target supports emphasizing text, the word will be
  2765. typeset in bold, italic, or underlined, respectively. Works only for
  2766. single words, but you can say: I *really* *mean* *this*.
  2767. Not all export backends support this.
  2768. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2769. :group 'org-export-translation
  2770. :type 'boolean)
  2771. (defcustom org-export-with-footnotes t
  2772. "If nil, export [1] as a footnote marker.
  2773. Lines starting with [1] will be formatted as footnotes.
  2774. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2775. :group 'org-export-translation
  2776. :type 'boolean)
  2777. (defcustom org-export-with-sub-superscripts t
  2778. "Non-nil means, interpret \"_\" and \"^\" for export.
  2779. When this option is turned on, you can use TeX-like syntax for sub- and
  2780. superscripts. Several characters after \"_\" or \"^\" will be
  2781. considered as a single item - so grouping with {} is normally not
  2782. needed. For example, the following things will be parsed as single
  2783. sub- or superscripts.
  2784. 10^24 or 10^tau several digits will be considered 1 item.
  2785. 10^-12 or 10^-tau a leading sign with digits or a word
  2786. x^2-y^3 will be read as x^2 - y^3, because items are
  2787. terminated by almost any nonword/nondigit char.
  2788. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2789. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2790. sub/superscript. If you set this variable to the symbol `{}',
  2791. the braces are *required* in order to trigger interpretations as
  2792. sub/superscript. This can be helpful in documents that need \"_\"
  2793. frequently in plain text.
  2794. Not all export backends support this, but HTML does.
  2795. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2796. :group 'org-export-translation
  2797. :type '(choice
  2798. (const :tag "Always interpret" t)
  2799. (const :tag "Only with braces" {})
  2800. (const :tag "Never interpret" nil)))
  2801. (defcustom org-export-with-special-strings t
  2802. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2803. When this option is turned on, these strings will be exported as:
  2804. \\- : &shy;
  2805. -- : &ndash;
  2806. --- : &mdash;
  2807. Not all export backends support this, but HTML does.
  2808. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2809. :group 'org-export-translation
  2810. :type 'boolean)
  2811. (defcustom org-export-with-TeX-macros t
  2812. "Non-nil means, interpret simple TeX-like macros when exporting.
  2813. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2814. No only real TeX macros will work here, but the standard HTML entities
  2815. for math can be used as macro names as well. For a list of supported
  2816. names in HTML export, see the constant `org-html-entities'.
  2817. Not all export backends support this.
  2818. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2819. :group 'org-export-translation
  2820. :group 'org-export-latex
  2821. :type 'boolean)
  2822. (defcustom org-export-with-LaTeX-fragments nil
  2823. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2824. When set, the exporter will find LaTeX environments if the \\begin line is
  2825. the first non-white thing on a line. It will also find the math delimiters
  2826. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2827. display math.
  2828. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2829. :group 'org-export-translation
  2830. :group 'org-export-latex
  2831. :type 'boolean)
  2832. (defcustom org-export-with-fixed-width t
  2833. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2834. This can be used to have pre-formatted text, fragments of code etc. For
  2835. example:
  2836. : ;; Some Lisp examples
  2837. : (while (defc cnt)
  2838. : (ding))
  2839. will be looking just like this in also HTML. See also the QUOTE keyword.
  2840. Not all export backends support this.
  2841. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2842. :group 'org-export-translation
  2843. :type 'boolean)
  2844. (defcustom org-match-sexp-depth 3
  2845. "Number of stacked braces for sub/superscript matching.
  2846. This has to be set before loading org.el to be effective."
  2847. :group 'org-export-translation
  2848. :type 'integer)
  2849. (defgroup org-export-tables nil
  2850. "Options for exporting tables in Org-mode."
  2851. :tag "Org Export Tables"
  2852. :group 'org-export)
  2853. (defcustom org-export-with-tables t
  2854. "If non-nil, lines starting with \"|\" define a table.
  2855. For example:
  2856. | Name | Address | Birthday |
  2857. |-------------+----------+-----------|
  2858. | Arthur Dent | England | 29.2.2100 |
  2859. Not all export backends support this.
  2860. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2861. :group 'org-export-tables
  2862. :type 'boolean)
  2863. (defcustom org-export-highlight-first-table-line t
  2864. "Non-nil means, highlight the first table line.
  2865. In HTML export, this means use <th> instead of <td>.
  2866. In tables created with table.el, this applies to the first table line.
  2867. In Org-mode tables, all lines before the first horizontal separator
  2868. line will be formatted with <th> tags."
  2869. :group 'org-export-tables
  2870. :type 'boolean)
  2871. (defcustom org-export-table-remove-special-lines t
  2872. "Remove special lines and marking characters in calculating tables.
  2873. This removes the special marking character column from tables that are set
  2874. up for spreadsheet calculations. It also removes the entire lines
  2875. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2876. the values of constants may be useful to have."
  2877. :group 'org-export-tables
  2878. :type 'boolean)
  2879. (defcustom org-export-prefer-native-exporter-for-tables nil
  2880. "Non-nil means, always export tables created with table.el natively.
  2881. Natively means, use the HTML code generator in table.el.
  2882. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2883. the table does not use row- or column-spanning). This has the
  2884. advantage, that the automatic HTML conversions for math symbols and
  2885. sub/superscripts can be applied. Org-mode's HTML generator is also
  2886. much faster."
  2887. :group 'org-export-tables
  2888. :type 'boolean)
  2889. (defgroup org-export-ascii nil
  2890. "Options specific for ASCII export of Org-mode files."
  2891. :tag "Org Export ASCII"
  2892. :group 'org-export)
  2893. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2894. "Characters for underlining headings in ASCII export.
  2895. In the given sequence, these characters will be used for level 1, 2, ..."
  2896. :group 'org-export-ascii
  2897. :type '(repeat character))
  2898. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2899. "Bullet characters for headlines converted to lists in ASCII export.
  2900. The first character is used for the first lest level generated in this
  2901. way, and so on. If there are more levels than characters given here,
  2902. the list will be repeated.
  2903. Note that plain lists will keep the same bullets as the have in the
  2904. Org-mode file."
  2905. :group 'org-export-ascii
  2906. :type '(repeat character))
  2907. (defgroup org-export-xml nil
  2908. "Options specific for XML export of Org-mode files."
  2909. :tag "Org Export XML"
  2910. :group 'org-export)
  2911. (defgroup org-export-html nil
  2912. "Options specific for HTML export of Org-mode files."
  2913. :tag "Org Export HTML"
  2914. :group 'org-export)
  2915. (defcustom org-export-html-coding-system nil
  2916. ""
  2917. :group 'org-export-html
  2918. :type 'coding-system)
  2919. (defcustom org-export-html-extension "html"
  2920. "The extension for exported HTML files."
  2921. :group 'org-export-html
  2922. :type 'string)
  2923. (defcustom org-export-html-style
  2924. "<style type=\"text/css\">
  2925. html {
  2926. font-family: Times, serif;
  2927. font-size: 12pt;
  2928. }
  2929. .title { text-align: center; }
  2930. .todo { color: red; }
  2931. .done { color: green; }
  2932. .timestamp { color: grey }
  2933. .timestamp-kwd { color: CadetBlue }
  2934. .tag { background-color:lightblue; font-weight:normal }
  2935. .target { background-color: lavender; }
  2936. pre {
  2937. border: 1pt solid #AEBDCC;
  2938. background-color: #F3F5F7;
  2939. padding: 5pt;
  2940. font-family: courier, monospace;
  2941. }
  2942. table { border-collapse: collapse; }
  2943. td, th {
  2944. vertical-align: top;
  2945. <!--border: 1pt solid #ADB9CC;-->
  2946. }
  2947. </style>"
  2948. "The default style specification for exported HTML files.
  2949. Since there are different ways of setting style information, this variable
  2950. needs to contain the full HTML structure to provide a style, including the
  2951. surrounding HTML tags. The style specifications should include definitions
  2952. for new classes todo, done, title, and deadline. For example, legal values
  2953. would be:
  2954. <style type=\"text/css\">
  2955. p { font-weight: normal; color: gray; }
  2956. h1 { color: black; }
  2957. .title { text-align: center; }
  2958. .todo, .deadline { color: red; }
  2959. .done { color: green; }
  2960. </style>
  2961. or, if you want to keep the style in a file,
  2962. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2963. As the value of this option simply gets inserted into the HTML <head> header,
  2964. you can \"misuse\" it to add arbitrary text to the header."
  2965. :group 'org-export-html
  2966. :type 'string)
  2967. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2968. "Format for typesetting the document title in HTML export."
  2969. :group 'org-export-html
  2970. :type 'string)
  2971. (defcustom org-export-html-toplevel-hlevel 2
  2972. "The <H> level for level 1 headings in HTML export."
  2973. :group 'org-export-html
  2974. :type 'string)
  2975. (defcustom org-export-html-link-org-files-as-html t
  2976. "Non-nil means, make file links to `file.org' point to `file.html'.
  2977. When org-mode is exporting an org-mode file to HTML, links to
  2978. non-html files are directly put into a href tag in HTML.
  2979. However, links to other Org-mode files (recognized by the
  2980. extension `.org.) should become links to the corresponding html
  2981. file, assuming that the linked org-mode file will also be
  2982. converted to HTML.
  2983. When nil, the links still point to the plain `.org' file."
  2984. :group 'org-export-html
  2985. :type 'boolean)
  2986. (defcustom org-export-html-inline-images 'maybe
  2987. "Non-nil means, inline images into exported HTML pages.
  2988. This is done using an <img> tag. When nil, an anchor with href is used to
  2989. link to the image. If this option is `maybe', then images in links with
  2990. an empty description will be inlined, while images with a description will
  2991. be linked only."
  2992. :group 'org-export-html
  2993. :type '(choice (const :tag "Never" nil)
  2994. (const :tag "Always" t)
  2995. (const :tag "When there is no description" maybe)))
  2996. ;; FIXME: rename
  2997. (defcustom org-export-html-expand t
  2998. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2999. When nil, these tags will be exported as plain text and therefore
  3000. not be interpreted by a browser.
  3001. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3002. :group 'org-export-html
  3003. :type 'boolean)
  3004. (defcustom org-export-html-table-tag
  3005. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3006. "The HTML tag that is used to start a table.
  3007. This must be a <table> tag, but you may change the options like
  3008. borders and spacing."
  3009. :group 'org-export-html
  3010. :type 'string)
  3011. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3012. "The opening tag for table header fields.
  3013. This is customizable so that alignment options can be specified."
  3014. :group 'org-export-tables
  3015. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3016. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3017. "The opening tag for table data fields.
  3018. This is customizable so that alignment options can be specified."
  3019. :group 'org-export-tables
  3020. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3021. (defcustom org-export-html-with-timestamp nil
  3022. "If non-nil, write `org-export-html-html-helper-timestamp'
  3023. into the exported HTML text. Otherwise, the buffer will just be saved
  3024. to a file."
  3025. :group 'org-export-html
  3026. :type 'boolean)
  3027. (defcustom org-export-html-html-helper-timestamp
  3028. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3029. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3030. :group 'org-export-html
  3031. :type 'string)
  3032. (defgroup org-export-icalendar nil
  3033. "Options specific for iCalendar export of Org-mode files."
  3034. :tag "Org Export iCalendar"
  3035. :group 'org-export)
  3036. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3037. "The file name for the iCalendar file covering all agenda files.
  3038. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3039. The file name should be absolute, the file will be overwritten without warning."
  3040. :group 'org-export-icalendar
  3041. :type 'file)
  3042. (defcustom org-icalendar-include-todo nil
  3043. "Non-nil means, export to iCalendar files should also cover TODO items."
  3044. :group 'org-export-icalendar
  3045. :type '(choice
  3046. (const :tag "None" nil)
  3047. (const :tag "Unfinished" t)
  3048. (const :tag "All" all)))
  3049. (defcustom org-icalendar-include-sexps t
  3050. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3051. These are entries like in the diary, but directly in an Org-mode file."
  3052. :group 'org-export-icalendar
  3053. :type 'boolean)
  3054. (defcustom org-icalendar-include-body 100
  3055. "Amount of text below headline to be included in iCalendar export.
  3056. This is a number of characters that should maximally be included.
  3057. Properties, scheduling and clocking lines will always be removed.
  3058. The text will be inserted into the DESCRIPTION field."
  3059. :group 'org-export-icalendar
  3060. :type '(choice
  3061. (const :tag "Nothing" nil)
  3062. (const :tag "Everything" t)
  3063. (integer :tag "Max characters")))
  3064. (defcustom org-icalendar-combined-name "OrgMode"
  3065. "Calendar name for the combined iCalendar representing all agenda files."
  3066. :group 'org-export-icalendar
  3067. :type 'string)
  3068. (defgroup org-font-lock nil
  3069. "Font-lock settings for highlighting in Org-mode."
  3070. :tag "Org Font Lock"
  3071. :group 'org)
  3072. (defcustom org-level-color-stars-only nil
  3073. "Non-nil means fontify only the stars in each headline.
  3074. When nil, the entire headline is fontified.
  3075. Changing it requires restart of `font-lock-mode' to become effective
  3076. also in regions already fontified."
  3077. :group 'org-font-lock
  3078. :type 'boolean)
  3079. (defcustom org-hide-leading-stars nil
  3080. "Non-nil means, hide the first N-1 stars in a headline.
  3081. This works by using the face `org-hide' for these stars. This
  3082. face is white for a light background, and black for a dark
  3083. background. You may have to customize the face `org-hide' to
  3084. make this work.
  3085. Changing it requires restart of `font-lock-mode' to become effective
  3086. also in regions already fontified.
  3087. You may also set this on a per-file basis by adding one of the following
  3088. lines to the buffer:
  3089. #+STARTUP: hidestars
  3090. #+STARTUP: showstars"
  3091. :group 'org-font-lock
  3092. :type 'boolean)
  3093. (defcustom org-fontify-done-headline nil
  3094. "Non-nil means, change the face of a headline if it is marked DONE.
  3095. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3096. When this is non-nil, the headline after the keyword is set to the
  3097. `org-headline-done' as an additional indication."
  3098. :group 'org-font-lock
  3099. :type 'boolean)
  3100. (defcustom org-fontify-emphasized-text t
  3101. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3102. Changing this variable requires a restart of Emacs to take effect."
  3103. :group 'org-font-lock
  3104. :type 'boolean)
  3105. (defcustom org-highlight-latex-fragments-and-specials nil
  3106. "Non-nil means, fontify what is treated specially by the exporters."
  3107. :group 'org-font-lock
  3108. :type 'boolean)
  3109. (defcustom org-hide-emphasis-markers nil
  3110. "Non-nil mean font-lock should hide the emphasis marker characters."
  3111. :group 'org-font-lock
  3112. :type 'boolean)
  3113. (defvar org-emph-re nil
  3114. "Regular expression for matching emphasis.")
  3115. (defvar org-verbatim-re nil
  3116. "Regular expression for matching verbatim text.")
  3117. (defvar org-emphasis-regexp-components) ; defined just below
  3118. (defvar org-emphasis-alist) ; defined just below
  3119. (defun org-set-emph-re (var val)
  3120. "Set variable and compute the emphasis regular expression."
  3121. (set var val)
  3122. (when (and (boundp 'org-emphasis-alist)
  3123. (boundp 'org-emphasis-regexp-components)
  3124. org-emphasis-alist org-emphasis-regexp-components)
  3125. (let* ((e org-emphasis-regexp-components)
  3126. (pre (car e))
  3127. (post (nth 1 e))
  3128. (border (nth 2 e))
  3129. (body (nth 3 e))
  3130. (nl (nth 4 e))
  3131. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3132. (body1 (concat body "*?"))
  3133. (markers (mapconcat 'car org-emphasis-alist ""))
  3134. (vmarkers (mapconcat
  3135. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3136. org-emphasis-alist "")))
  3137. ;; make sure special characters appear at the right position in the class
  3138. (if (string-match "\\^" markers)
  3139. (setq markers (concat (replace-match "" t t markers) "^")))
  3140. (if (string-match "-" markers)
  3141. (setq markers (concat (replace-match "" t t markers) "-")))
  3142. (if (string-match "\\^" vmarkers)
  3143. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3144. (if (string-match "-" vmarkers)
  3145. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3146. (if (> nl 0)
  3147. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3148. (int-to-string nl) "\\}")))
  3149. ;; Make the regexp
  3150. (setq org-emph-re
  3151. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3152. "\\("
  3153. "\\([" markers "]\\)"
  3154. "\\("
  3155. "[^" border "]\\|"
  3156. "[^" border (if (and nil stacked) markers) "]"
  3157. body1
  3158. "[^" border (if (and nil stacked) markers) "]"
  3159. "\\)"
  3160. "\\3\\)"
  3161. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3162. (setq org-verbatim-re
  3163. (concat "\\([" pre "]\\|^\\)"
  3164. "\\("
  3165. "\\([" vmarkers "]\\)"
  3166. "\\("
  3167. "[^" border "]\\|"
  3168. "[^" border "]"
  3169. body1
  3170. "[^" border "]"
  3171. "\\)"
  3172. "\\3\\)"
  3173. "\\([" post "]\\|$\\)")))))
  3174. (defcustom org-emphasis-regexp-components
  3175. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3176. "Components used to build the regular expression for emphasis.
  3177. This is a list with 6 entries. Terminology: In an emphasis string
  3178. like \" *strong word* \", we call the initial space PREMATCH, the final
  3179. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3180. and \"trong wor\" is the body. The different components in this variable
  3181. specify what is allowed/forbidden in each part:
  3182. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3183. post Chars allowed as postmatch. End of line will be allowed too.
  3184. border The chars *forbidden* as border characters.
  3185. body-regexp A regexp like \".\" to match a body character. Don't use
  3186. non-shy groups here, and don't allow newline here.
  3187. newline The maximum number of newlines allowed in an emphasis exp.
  3188. Use customize to modify this, or restart Emacs after changing it."
  3189. :group 'org-font-lock
  3190. :set 'org-set-emph-re
  3191. :type '(list
  3192. (sexp :tag "Allowed chars in pre ")
  3193. (sexp :tag "Allowed chars in post ")
  3194. (sexp :tag "Forbidden chars in border ")
  3195. (sexp :tag "Regexp for body ")
  3196. (integer :tag "number of newlines allowed")
  3197. (option (boolean :tag "Stacking (DISABLED) "))))
  3198. (defcustom org-emphasis-alist
  3199. '(("*" bold "<b>" "</b>")
  3200. ("/" italic "<i>" "</i>")
  3201. ("_" underline "<u>" "</u>")
  3202. ("=" org-code "<code>" "</code>" verbatim)
  3203. ("~" org-verbatim "" "" verbatim)
  3204. ("+" (:strike-through t) "<del>" "</del>")
  3205. )
  3206. "Special syntax for emphasized text.
  3207. Text starting and ending with a special character will be emphasized, for
  3208. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3209. characters, the face to be used by font-lock for highlighting in Org-mode
  3210. Emacs buffers, and the HTML tags to be used for this.
  3211. Use customize to modify this, or restart Emacs after changing it."
  3212. :group 'org-font-lock
  3213. :set 'org-set-emph-re
  3214. :type '(repeat
  3215. (list
  3216. (string :tag "Marker character")
  3217. (choice
  3218. (face :tag "Font-lock-face")
  3219. (plist :tag "Face property list"))
  3220. (string :tag "HTML start tag")
  3221. (string :tag "HTML end tag")
  3222. (option (const verbatim)))))
  3223. ;;; The faces
  3224. (defgroup org-faces nil
  3225. "Faces in Org-mode."
  3226. :tag "Org Faces"
  3227. :group 'org-font-lock)
  3228. (defun org-compatible-face (inherits specs)
  3229. "Make a compatible face specification.
  3230. If INHERITS is an existing face and if the Emacs version supports it,
  3231. just inherit the face. If not, use SPECS to define the face.
  3232. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3233. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3234. to the top of the list. The `min-colors' attribute will be removed from
  3235. any other entries, and any resulting duplicates will be removed entirely."
  3236. (cond
  3237. ((and inherits (facep inherits)
  3238. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3239. ;; In Emacs 23, we use inheritance where possible.
  3240. ;; We only do this in Emacs 23, because only there the outline
  3241. ;; faces have been changed to the original org-mode-level-faces.
  3242. (list (list t :inherit inherits)))
  3243. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3244. ;; These do not understand the `min-colors' attribute.
  3245. (let (r e a)
  3246. (while (setq e (pop specs))
  3247. (cond
  3248. ((memq (car e) '(t default)) (push e r))
  3249. ((setq a (member '(min-colors 8) (car e)))
  3250. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3251. (cdr e)))))
  3252. ((setq a (assq 'min-colors (car e)))
  3253. (setq e (cons (delq a (car e)) (cdr e)))
  3254. (or (assoc (car e) r) (push e r)))
  3255. (t (or (assoc (car e) r) (push e r)))))
  3256. (nreverse r)))
  3257. (t specs)))
  3258. (put 'org-compatible-face 'lisp-indent-function 1)
  3259. (defface org-hide
  3260. '((((background light)) (:foreground "white"))
  3261. (((background dark)) (:foreground "black")))
  3262. "Face used to hide leading stars in headlines.
  3263. The forground color of this face should be equal to the background
  3264. color of the frame."
  3265. :group 'org-faces)
  3266. (defface org-level-1 ;; font-lock-function-name-face
  3267. (org-compatible-face 'outline-1
  3268. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3269. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3270. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3271. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3272. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3273. (t (:bold t))))
  3274. "Face used for level 1 headlines."
  3275. :group 'org-faces)
  3276. (defface org-level-2 ;; font-lock-variable-name-face
  3277. (org-compatible-face 'outline-2
  3278. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3279. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3280. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3281. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3282. (t (:bold t))))
  3283. "Face used for level 2 headlines."
  3284. :group 'org-faces)
  3285. (defface org-level-3 ;; font-lock-keyword-face
  3286. (org-compatible-face 'outline-3
  3287. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3288. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3289. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3290. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3291. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3292. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3293. (t (:bold t))))
  3294. "Face used for level 3 headlines."
  3295. :group 'org-faces)
  3296. (defface org-level-4 ;; font-lock-comment-face
  3297. (org-compatible-face 'outline-4
  3298. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3299. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3300. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3301. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3302. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3303. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3304. (t (:bold t))))
  3305. "Face used for level 4 headlines."
  3306. :group 'org-faces)
  3307. (defface org-level-5 ;; font-lock-type-face
  3308. (org-compatible-face 'outline-5
  3309. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3310. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3311. (((class color) (min-colors 8)) (:foreground "green"))))
  3312. "Face used for level 5 headlines."
  3313. :group 'org-faces)
  3314. (defface org-level-6 ;; font-lock-constant-face
  3315. (org-compatible-face 'outline-6
  3316. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3317. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3318. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3319. "Face used for level 6 headlines."
  3320. :group 'org-faces)
  3321. (defface org-level-7 ;; font-lock-builtin-face
  3322. (org-compatible-face 'outline-7
  3323. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3324. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3325. (((class color) (min-colors 8)) (:foreground "blue"))))
  3326. "Face used for level 7 headlines."
  3327. :group 'org-faces)
  3328. (defface org-level-8 ;; font-lock-string-face
  3329. (org-compatible-face 'outline-8
  3330. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3331. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3332. (((class color) (min-colors 8)) (:foreground "green"))))
  3333. "Face used for level 8 headlines."
  3334. :group 'org-faces)
  3335. (defface org-special-keyword ;; font-lock-string-face
  3336. (org-compatible-face nil
  3337. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3338. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3339. (t (:italic t))))
  3340. "Face used for special keywords."
  3341. :group 'org-faces)
  3342. (defface org-drawer ;; font-lock-function-name-face
  3343. (org-compatible-face nil
  3344. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3345. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3346. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3347. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3348. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3349. (t (:bold t))))
  3350. "Face used for drawers."
  3351. :group 'org-faces)
  3352. (defface org-property-value nil
  3353. "Face used for the value of a property."
  3354. :group 'org-faces)
  3355. (defface org-column
  3356. (org-compatible-face nil
  3357. '((((class color) (min-colors 16) (background light))
  3358. (:background "grey90"))
  3359. (((class color) (min-colors 16) (background dark))
  3360. (:background "grey30"))
  3361. (((class color) (min-colors 8))
  3362. (:background "cyan" :foreground "black"))
  3363. (t (:inverse-video t))))
  3364. "Face for column display of entry properties."
  3365. :group 'org-faces)
  3366. (when (fboundp 'set-face-attribute)
  3367. ;; Make sure that a fixed-width face is used when we have a column table.
  3368. (set-face-attribute 'org-column nil
  3369. :height (face-attribute 'default :height)
  3370. :family (face-attribute 'default :family)))
  3371. (defface org-warning
  3372. (org-compatible-face 'font-lock-warning-face
  3373. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3374. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3375. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3376. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3377. (t (:bold t))))
  3378. "Face for deadlines and TODO keywords."
  3379. :group 'org-faces)
  3380. (defface org-archived ; similar to shadow
  3381. (org-compatible-face 'shadow
  3382. '((((class color grayscale) (min-colors 88) (background light))
  3383. (:foreground "grey50"))
  3384. (((class color grayscale) (min-colors 88) (background dark))
  3385. (:foreground "grey70"))
  3386. (((class color) (min-colors 8) (background light))
  3387. (:foreground "green"))
  3388. (((class color) (min-colors 8) (background dark))
  3389. (:foreground "yellow"))))
  3390. "Face for headline with the ARCHIVE tag."
  3391. :group 'org-faces)
  3392. (defface org-link
  3393. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3394. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3395. (t (:underline t)))
  3396. "Face for links."
  3397. :group 'org-faces)
  3398. (defface org-ellipsis
  3399. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3400. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3401. (t (:strike-through t)))
  3402. "Face for the ellipsis in folded text."
  3403. :group 'org-faces)
  3404. (defface org-target
  3405. '((((class color) (background light)) (:underline t))
  3406. (((class color) (background dark)) (:underline t))
  3407. (t (:underline t)))
  3408. "Face for links."
  3409. :group 'org-faces)
  3410. (defface org-date
  3411. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3412. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3413. (t (:underline t)))
  3414. "Face for links."
  3415. :group 'org-faces)
  3416. (defface org-sexp-date
  3417. '((((class color) (background light)) (:foreground "Purple"))
  3418. (((class color) (background dark)) (:foreground "Cyan"))
  3419. (t (:underline t)))
  3420. "Face for links."
  3421. :group 'org-faces)
  3422. (defface org-tag
  3423. '((t (:bold t)))
  3424. "Face for tags."
  3425. :group 'org-faces)
  3426. (defface org-todo ; font-lock-warning-face
  3427. (org-compatible-face nil
  3428. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3429. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3430. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3431. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3432. (t (:inverse-video t :bold t))))
  3433. "Face for TODO keywords."
  3434. :group 'org-faces)
  3435. (defface org-done ;; font-lock-type-face
  3436. (org-compatible-face nil
  3437. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3438. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3439. (((class color) (min-colors 8)) (:foreground "green"))
  3440. (t (:bold t))))
  3441. "Face used for todo keywords that indicate DONE items."
  3442. :group 'org-faces)
  3443. (defface org-headline-done ;; font-lock-string-face
  3444. (org-compatible-face nil
  3445. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3446. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3447. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3448. "Face used to indicate that a headline is DONE.
  3449. This face is only used if `org-fontify-done-headline' is set. If applies
  3450. to the part of the headline after the DONE keyword."
  3451. :group 'org-faces)
  3452. (defcustom org-todo-keyword-faces nil
  3453. "Faces for specific TODO keywords.
  3454. This is a list of cons cells, with TODO keywords in the car
  3455. and faces in the cdr. The face can be a symbol, or a property
  3456. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3457. :group 'org-faces
  3458. :group 'org-todo
  3459. :type '(repeat
  3460. (cons
  3461. (string :tag "keyword")
  3462. (sexp :tag "face"))))
  3463. (defface org-table ;; font-lock-function-name-face
  3464. (org-compatible-face nil
  3465. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3466. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3467. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3468. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3469. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3470. (((class color) (min-colors 8) (background dark)))))
  3471. "Face used for tables."
  3472. :group 'org-faces)
  3473. (defface org-formula
  3474. (org-compatible-face nil
  3475. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3476. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3477. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3478. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3479. (t (:bold t :italic t))))
  3480. "Face for formulas."
  3481. :group 'org-faces)
  3482. (defface org-code
  3483. (org-compatible-face nil
  3484. '((((class color grayscale) (min-colors 88) (background light))
  3485. (:foreground "grey50"))
  3486. (((class color grayscale) (min-colors 88) (background dark))
  3487. (:foreground "grey70"))
  3488. (((class color) (min-colors 8) (background light))
  3489. (:foreground "green"))
  3490. (((class color) (min-colors 8) (background dark))
  3491. (:foreground "yellow"))))
  3492. "Face for fixed-with text like code snippets."
  3493. :group 'org-faces
  3494. :version "22.1")
  3495. (defface org-verbatim
  3496. (org-compatible-face nil
  3497. '((((class color grayscale) (min-colors 88) (background light))
  3498. (:foreground "grey50" :underline t))
  3499. (((class color grayscale) (min-colors 88) (background dark))
  3500. (:foreground "grey70" :underline t))
  3501. (((class color) (min-colors 8) (background light))
  3502. (:foreground "green" :underline t))
  3503. (((class color) (min-colors 8) (background dark))
  3504. (:foreground "yellow" :underline t))))
  3505. "Face for fixed-with text like code snippets."
  3506. :group 'org-faces
  3507. :version "22.1")
  3508. (defface org-agenda-structure ;; font-lock-function-name-face
  3509. (org-compatible-face nil
  3510. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3511. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3512. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3513. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3514. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3515. (t (:bold t))))
  3516. "Face used in agenda for captions and dates."
  3517. :group 'org-faces)
  3518. (defface org-scheduled-today
  3519. (org-compatible-face nil
  3520. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3521. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3522. (((class color) (min-colors 8)) (:foreground "green"))
  3523. (t (:bold t :italic t))))
  3524. "Face for items scheduled for a certain day."
  3525. :group 'org-faces)
  3526. (defface org-scheduled-previously
  3527. (org-compatible-face nil
  3528. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3529. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3530. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3531. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3532. (t (:bold t))))
  3533. "Face for items scheduled previously, and not yet done."
  3534. :group 'org-faces)
  3535. (defface org-upcoming-deadline
  3536. (org-compatible-face nil
  3537. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3538. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3539. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3540. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3541. (t (:bold t))))
  3542. "Face for items scheduled previously, and not yet done."
  3543. :group 'org-faces)
  3544. (defcustom org-agenda-deadline-faces
  3545. '((1.0 . org-warning)
  3546. (0.5 . org-upcoming-deadline)
  3547. (0.0 . default))
  3548. "Faces for showing deadlines in the agenda.
  3549. This is a list of cons cells. The cdr of each cell is a face to be used,
  3550. and it can also just be like '(:foreground \"yellow\").
  3551. Each car is a fraction of the head-warning time that must have passed for
  3552. this the face in the cdr to be used for display. The numbers must be
  3553. given in descending order. The head-warning time is normally taken
  3554. from `org-deadline-warning-days', but can also be specified in the deadline
  3555. timestamp itself, like this:
  3556. DEADLINE: <2007-08-13 Mon -8d>
  3557. You may use d for days, w for weeks, m for months and y for years. Months
  3558. and years will only be treated in an approximate fashion (30.4 days for a
  3559. month and 365.24 days for a year)."
  3560. :group 'org-faces
  3561. :group 'org-agenda-daily/weekly
  3562. :type '(repeat
  3563. (cons
  3564. (number :tag "Fraction of head-warning time passed")
  3565. (sexp :tag "Face"))))
  3566. ;; FIXME: this is not a good face yet.
  3567. (defface org-agenda-restriction-lock
  3568. (org-compatible-face nil
  3569. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3570. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3571. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3572. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3573. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3574. (t (:inverse-video t))))
  3575. "Face for showing the agenda restriction lock."
  3576. :group 'org-faces)
  3577. (defface org-time-grid ;; font-lock-variable-name-face
  3578. (org-compatible-face nil
  3579. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3580. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3581. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3582. "Face used for time grids."
  3583. :group 'org-faces)
  3584. (defconst org-level-faces
  3585. '(org-level-1 org-level-2 org-level-3 org-level-4
  3586. org-level-5 org-level-6 org-level-7 org-level-8
  3587. ))
  3588. (defcustom org-n-level-faces (length org-level-faces)
  3589. "The number different faces to be used for headlines.
  3590. Org-mode defines 8 different headline faces, so this can be at most 8.
  3591. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3592. :type 'number
  3593. :group 'org-faces)
  3594. ;;; Functions and variables from ther packages
  3595. ;; Declared here to avoid compiler warnings
  3596. (eval-and-compile
  3597. (unless (fboundp 'declare-function)
  3598. (defmacro declare-function (fn file &optional arglist fileonly))))
  3599. ;; XEmacs only
  3600. (defvar outline-mode-menu-heading)
  3601. (defvar outline-mode-menu-show)
  3602. (defvar outline-mode-menu-hide)
  3603. (defvar zmacs-regions) ; XEmacs regions
  3604. ;; Emacs only
  3605. (defvar mark-active)
  3606. ;; Various packages
  3607. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3608. (declare-function add-to-diary-list "diary-lib"
  3609. (date string specifier &optional marker globcolor literal))
  3610. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3611. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3612. (declare-function Info-goto-node "info" (nodename &optional fork))
  3613. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3614. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3615. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3616. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3617. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3618. (declare-function bbdb-record-name "ext:bbdb" (record))
  3619. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3620. (declare-function bibtex-generate-autokey "bibtex" ())
  3621. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3622. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3623. (defvar calc-embedded-close-formula)
  3624. (defvar calc-embedded-open-formula)
  3625. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3626. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3627. (declare-function calendar-check-holidays "holidays" (date))
  3628. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3629. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3630. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3631. (declare-function calendar-forward-day "cal-move" (arg))
  3632. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3633. (declare-function calendar-goto-date "cal-move" (date))
  3634. (declare-function calendar-goto-today "cal-move" ())
  3635. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3636. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3637. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3638. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3639. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3640. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3641. (defvar calendar-mode-map)
  3642. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3643. (declare-function cdlatex-tab "ext:cdlatex" ())
  3644. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3645. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3646. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3647. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3648. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3649. (defvar font-lock-unfontify-region-function)
  3650. (declare-function gnus-article-show-summary "gnus-art" ())
  3651. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3652. (defvar gnus-other-frame-object)
  3653. (defvar gnus-group-name)
  3654. (defvar gnus-article-current)
  3655. (defvar Info-current-file)
  3656. (defvar Info-current-node)
  3657. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3658. (declare-function mh-find-path "mh-utils" ())
  3659. (declare-function mh-get-header-field "mh-utils" (field))
  3660. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3661. (declare-function mh-header-display "mh-show" ())
  3662. (declare-function mh-index-previous-folder "mh-search" ())
  3663. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3664. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3665. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3666. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3667. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3668. (declare-function mh-show-header-display "mh-show" t t)
  3669. (declare-function mh-show-msg "mh-show" (msg))
  3670. (declare-function mh-show-show "mh-show" t t)
  3671. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3672. (defvar mh-progs)
  3673. (defvar mh-current-folder)
  3674. (defvar mh-show-folder-buffer)
  3675. (defvar mh-index-folder)
  3676. (defvar mh-searcher)
  3677. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3678. (declare-function parse-time-string "parse-time" (string))
  3679. (declare-function remember "remember" (&optional initial))
  3680. (declare-function remember-buffer-desc "remember" ())
  3681. (defvar remember-save-after-remembering)
  3682. (defvar remember-data-file)
  3683. (defvar remember-register)
  3684. (defvar remember-buffer)
  3685. (defvar remember-handler-functions)
  3686. (defvar remember-annotation-functions)
  3687. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3688. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3689. (declare-function rmail-what-message "rmail" ())
  3690. (defvar texmathp-why)
  3691. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3692. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3693. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3694. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3695. (declare-function vm-isearch-update "ext:vm-search" ())
  3696. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3697. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3698. (declare-function vm-su-subject "ext:vm-summary" (m))
  3699. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3700. (defvar vm-message-pointer)
  3701. (defvar vm-folder-directory)
  3702. (defvar w3m-current-url)
  3703. (defvar w3m-current-title)
  3704. ;; backward compatibility to old version of wl
  3705. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3706. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3707. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3708. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3709. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3710. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3711. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3712. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3713. (defvar wl-summary-buffer-elmo-folder)
  3714. (defvar wl-summary-buffer-folder-name)
  3715. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3716. (defvar org-latex-regexps)
  3717. (defvar constants-unit-system)
  3718. ;;; Variables for pre-computed regular expressions, all buffer local
  3719. (defvar org-drawer-regexp nil
  3720. "Matches first line of a hidden block.")
  3721. (make-variable-buffer-local 'org-drawer-regexp)
  3722. (defvar org-todo-regexp nil
  3723. "Matches any of the TODO state keywords.")
  3724. (make-variable-buffer-local 'org-todo-regexp)
  3725. (defvar org-not-done-regexp nil
  3726. "Matches any of the TODO state keywords except the last one.")
  3727. (make-variable-buffer-local 'org-not-done-regexp)
  3728. (defvar org-todo-line-regexp nil
  3729. "Matches a headline and puts TODO state into group 2 if present.")
  3730. (make-variable-buffer-local 'org-todo-line-regexp)
  3731. (defvar org-complex-heading-regexp nil
  3732. "Matches a headline and puts everything into groups:
  3733. group 1: the stars
  3734. group 2: The todo keyword, maybe
  3735. group 3: Priority cookie
  3736. group 4: True headline
  3737. group 5: Tags")
  3738. (make-variable-buffer-local 'org-complex-heading-regexp)
  3739. (defvar org-todo-line-tags-regexp nil
  3740. "Matches a headline and puts TODO state into group 2 if present.
  3741. Also put tags into group 4 if tags are present.")
  3742. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3743. (defvar org-nl-done-regexp nil
  3744. "Matches newline followed by a headline with the DONE keyword.")
  3745. (make-variable-buffer-local 'org-nl-done-regexp)
  3746. (defvar org-looking-at-done-regexp nil
  3747. "Matches the DONE keyword a point.")
  3748. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3749. (defvar org-ds-keyword-length 12
  3750. "Maximum length of the Deadline and SCHEDULED keywords.")
  3751. (make-variable-buffer-local 'org-ds-keyword-length)
  3752. (defvar org-deadline-regexp nil
  3753. "Matches the DEADLINE keyword.")
  3754. (make-variable-buffer-local 'org-deadline-regexp)
  3755. (defvar org-deadline-time-regexp nil
  3756. "Matches the DEADLINE keyword together with a time stamp.")
  3757. (make-variable-buffer-local 'org-deadline-time-regexp)
  3758. (defvar org-deadline-line-regexp nil
  3759. "Matches the DEADLINE keyword and the rest of the line.")
  3760. (make-variable-buffer-local 'org-deadline-line-regexp)
  3761. (defvar org-scheduled-regexp nil
  3762. "Matches the SCHEDULED keyword.")
  3763. (make-variable-buffer-local 'org-scheduled-regexp)
  3764. (defvar org-scheduled-time-regexp nil
  3765. "Matches the SCHEDULED keyword together with a time stamp.")
  3766. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3767. (defvar org-closed-time-regexp nil
  3768. "Matches the CLOSED keyword together with a time stamp.")
  3769. (make-variable-buffer-local 'org-closed-time-regexp)
  3770. (defvar org-keyword-time-regexp nil
  3771. "Matches any of the 4 keywords, together with the time stamp.")
  3772. (make-variable-buffer-local 'org-keyword-time-regexp)
  3773. (defvar org-keyword-time-not-clock-regexp nil
  3774. "Matches any of the 3 keywords, together with the time stamp.")
  3775. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3776. (defvar org-maybe-keyword-time-regexp nil
  3777. "Matches a timestamp, possibly preceeded by a keyword.")
  3778. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3779. (defvar org-planning-or-clock-line-re nil
  3780. "Matches a line with planning or clock info.")
  3781. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3782. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3783. rear-nonsticky t mouse-map t fontified t)
  3784. "Properties to remove when a string without properties is wanted.")
  3785. (defsubst org-match-string-no-properties (num &optional string)
  3786. (if (featurep 'xemacs)
  3787. (let ((s (match-string num string)))
  3788. (remove-text-properties 0 (length s) org-rm-props s)
  3789. s)
  3790. (match-string-no-properties num string)))
  3791. (defsubst org-no-properties (s)
  3792. (if (fboundp 'set-text-properties)
  3793. (set-text-properties 0 (length s) nil s)
  3794. (remove-text-properties 0 (length s) org-rm-props s))
  3795. s)
  3796. (defsubst org-get-alist-option (option key)
  3797. (cond ((eq key t) t)
  3798. ((eq option t) t)
  3799. ((assoc key option) (cdr (assoc key option)))
  3800. (t (cdr (assq 'default option)))))
  3801. (defsubst org-inhibit-invisibility ()
  3802. "Modified `buffer-invisibility-spec' for Emacs 21.
  3803. Some ops with invisible text do not work correctly on Emacs 21. For these
  3804. we turn off invisibility temporarily. Use this in a `let' form."
  3805. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3806. (defsubst org-set-local (var value)
  3807. "Make VAR local in current buffer and set it to VALUE."
  3808. (set (make-variable-buffer-local var) value))
  3809. (defsubst org-mode-p ()
  3810. "Check if the current buffer is in Org-mode."
  3811. (eq major-mode 'org-mode))
  3812. (defsubst org-last (list)
  3813. "Return the last element of LIST."
  3814. (car (last list)))
  3815. (defun org-let (list &rest body)
  3816. (eval (cons 'let (cons list body))))
  3817. (put 'org-let 'lisp-indent-function 1)
  3818. (defun org-let2 (list1 list2 &rest body)
  3819. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3820. (put 'org-let2 'lisp-indent-function 2)
  3821. (defconst org-startup-options
  3822. '(("fold" org-startup-folded t)
  3823. ("overview" org-startup-folded t)
  3824. ("nofold" org-startup-folded nil)
  3825. ("showall" org-startup-folded nil)
  3826. ("content" org-startup-folded content)
  3827. ("hidestars" org-hide-leading-stars t)
  3828. ("showstars" org-hide-leading-stars nil)
  3829. ("odd" org-odd-levels-only t)
  3830. ("oddeven" org-odd-levels-only nil)
  3831. ("align" org-startup-align-all-tables t)
  3832. ("noalign" org-startup-align-all-tables nil)
  3833. ("customtime" org-display-custom-times t)
  3834. ("logging" org-log-done t)
  3835. ("logdone" org-log-done t)
  3836. ("nologging" org-log-done nil)
  3837. ("lognotedone" org-log-done done push)
  3838. ("lognotestate" org-log-done state push)
  3839. ("lognoteclock-out" org-log-done clock-out push)
  3840. ("logrepeat" org-log-repeat t)
  3841. ("nologrepeat" org-log-repeat nil)
  3842. ("constcgs" constants-unit-system cgs)
  3843. ("constSI" constants-unit-system SI))
  3844. "Variable associated with STARTUP options for org-mode.
  3845. Each element is a list of three items: The startup options as written
  3846. in the #+STARTUP line, the corresponding variable, and the value to
  3847. set this variable to if the option is found. An optional forth element PUSH
  3848. means to push this value onto the list in the variable.")
  3849. (defun org-set-regexps-and-options ()
  3850. "Precompute regular expressions for current buffer."
  3851. (when (org-mode-p)
  3852. (org-set-local 'org-todo-kwd-alist nil)
  3853. (org-set-local 'org-todo-key-alist nil)
  3854. (org-set-local 'org-todo-key-trigger nil)
  3855. (org-set-local 'org-todo-keywords-1 nil)
  3856. (org-set-local 'org-done-keywords nil)
  3857. (org-set-local 'org-todo-heads nil)
  3858. (org-set-local 'org-todo-sets nil)
  3859. (org-set-local 'org-todo-log-states nil)
  3860. (let ((re (org-make-options-regexp
  3861. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3862. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3863. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3864. (splitre "[ \t]+")
  3865. kwds kws0 kwsa key value cat arch tags const links hw dws
  3866. tail sep kws1 prio props drawers
  3867. ex log)
  3868. (save-excursion
  3869. (save-restriction
  3870. (widen)
  3871. (goto-char (point-min))
  3872. (while (re-search-forward re nil t)
  3873. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3874. (cond
  3875. ((equal key "CATEGORY")
  3876. (if (string-match "[ \t]+$" value)
  3877. (setq value (replace-match "" t t value)))
  3878. (setq cat value))
  3879. ((member key '("SEQ_TODO" "TODO"))
  3880. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3881. ((equal key "TYP_TODO")
  3882. (push (cons 'type (org-split-string value splitre)) kwds))
  3883. ((equal key "TAGS")
  3884. (setq tags (append tags (org-split-string value splitre))))
  3885. ((equal key "COLUMNS")
  3886. (org-set-local 'org-columns-default-format value))
  3887. ((equal key "LINK")
  3888. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3889. (push (cons (match-string 1 value)
  3890. (org-trim (match-string 2 value)))
  3891. links)))
  3892. ((equal key "PRIORITIES")
  3893. (setq prio (org-split-string value " +")))
  3894. ((equal key "PROPERTY")
  3895. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3896. (push (cons (match-string 1 value) (match-string 2 value))
  3897. props)))
  3898. ((equal key "DRAWERS")
  3899. (setq drawers (org-split-string value splitre)))
  3900. ((equal key "CONSTANTS")
  3901. (setq const (append const (org-split-string value splitre))))
  3902. ((equal key "STARTUP")
  3903. (let ((opts (org-split-string value splitre))
  3904. l var val)
  3905. (while (setq l (pop opts))
  3906. (when (setq l (assoc l org-startup-options))
  3907. (setq var (nth 1 l) val (nth 2 l))
  3908. (if (not (nth 3 l))
  3909. (set (make-local-variable var) val)
  3910. (if (not (listp (symbol-value var)))
  3911. (set (make-local-variable var) nil))
  3912. (set (make-local-variable var) (symbol-value var))
  3913. (add-to-list var val))))))
  3914. ((equal key "ARCHIVE")
  3915. (string-match " *$" value)
  3916. (setq arch (replace-match "" t t value))
  3917. (remove-text-properties 0 (length arch)
  3918. '(face t fontified t) arch)))
  3919. )))
  3920. (when cat
  3921. (org-set-local 'org-category (intern cat))
  3922. (push (cons "CATEGORY" cat) props))
  3923. (when prio
  3924. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3925. (setq prio (mapcar 'string-to-char prio))
  3926. (org-set-local 'org-highest-priority (nth 0 prio))
  3927. (org-set-local 'org-lowest-priority (nth 1 prio))
  3928. (org-set-local 'org-default-priority (nth 2 prio)))
  3929. (and props (org-set-local 'org-local-properties (nreverse props)))
  3930. (and drawers (org-set-local 'org-drawers drawers))
  3931. (and arch (org-set-local 'org-archive-location arch))
  3932. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3933. ;; Process the TODO keywords
  3934. (unless kwds
  3935. ;; Use the global values as if they had been given locally.
  3936. (setq kwds (default-value 'org-todo-keywords))
  3937. (if (stringp (car kwds))
  3938. (setq kwds (list (cons org-todo-interpretation
  3939. (default-value 'org-todo-keywords)))))
  3940. (setq kwds (reverse kwds)))
  3941. (setq kwds (nreverse kwds))
  3942. (let (inter kws kw)
  3943. (while (setq kws (pop kwds))
  3944. (setq inter (pop kws) sep (member "|" kws)
  3945. kws0 (delete "|" (copy-sequence kws))
  3946. kwsa nil
  3947. kws1 (mapcar
  3948. (lambda (x)
  3949. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3950. (progn
  3951. (setq kw (match-string 1 x)
  3952. ex (and (match-end 2) (match-string 2 x))
  3953. log (and ex (string-match "@" ex))
  3954. key (and ex (substring ex 0 1)))
  3955. (if (equal key "@") (setq key nil))
  3956. (push (cons kw (and key (string-to-char key))) kwsa)
  3957. (and log (push kw org-todo-log-states))
  3958. kw)
  3959. (error "Invalid TODO keyword %s" x)))
  3960. kws0)
  3961. kwsa (if kwsa (append '((:startgroup))
  3962. (nreverse kwsa)
  3963. '((:endgroup))))
  3964. hw (car kws1)
  3965. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3966. tail (list inter hw (car dws) (org-last dws)))
  3967. (add-to-list 'org-todo-heads hw 'append)
  3968. (push kws1 org-todo-sets)
  3969. (setq org-done-keywords (append org-done-keywords dws nil))
  3970. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3971. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3972. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3973. (setq org-todo-sets (nreverse org-todo-sets)
  3974. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3975. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3976. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3977. ;; Process the constants
  3978. (when const
  3979. (let (e cst)
  3980. (while (setq e (pop const))
  3981. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3982. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3983. (setq org-table-formula-constants-local cst)))
  3984. ;; Process the tags.
  3985. (when tags
  3986. (let (e tgs)
  3987. (while (setq e (pop tags))
  3988. (cond
  3989. ((equal e "{") (push '(:startgroup) tgs))
  3990. ((equal e "}") (push '(:endgroup) tgs))
  3991. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3992. (push (cons (match-string 1 e)
  3993. (string-to-char (match-string 2 e)))
  3994. tgs))
  3995. (t (push (list e) tgs))))
  3996. (org-set-local 'org-tag-alist nil)
  3997. (while (setq e (pop tgs))
  3998. (or (and (stringp (car e))
  3999. (assoc (car e) org-tag-alist))
  4000. (push e org-tag-alist))))))
  4001. ;; Compute the regular expressions and other local variables
  4002. (if (not org-done-keywords)
  4003. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4004. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4005. (length org-scheduled-string)))
  4006. org-drawer-regexp
  4007. (concat "^[ \t]*:\\("
  4008. (mapconcat 'regexp-quote org-drawers "\\|")
  4009. "\\):[ \t]*$")
  4010. org-not-done-keywords
  4011. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4012. org-todo-regexp
  4013. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4014. "\\|") "\\)\\>")
  4015. org-not-done-regexp
  4016. (concat "\\<\\("
  4017. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4018. "\\)\\>")
  4019. org-todo-line-regexp
  4020. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4021. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4022. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4023. org-complex-heading-regexp
  4024. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4025. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4026. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4027. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4028. org-nl-done-regexp
  4029. (concat "\n\\*+[ \t]+"
  4030. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4031. "\\)" "\\>")
  4032. org-todo-line-tags-regexp
  4033. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4034. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4035. (org-re
  4036. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4037. org-looking-at-done-regexp
  4038. (concat "^" "\\(?:"
  4039. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4040. "\\>")
  4041. org-deadline-regexp (concat "\\<" org-deadline-string)
  4042. org-deadline-time-regexp
  4043. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4044. org-deadline-line-regexp
  4045. (concat "\\<\\(" org-deadline-string "\\).*")
  4046. org-scheduled-regexp
  4047. (concat "\\<" org-scheduled-string)
  4048. org-scheduled-time-regexp
  4049. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4050. org-closed-time-regexp
  4051. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4052. org-keyword-time-regexp
  4053. (concat "\\<\\(" org-scheduled-string
  4054. "\\|" org-deadline-string
  4055. "\\|" org-closed-string
  4056. "\\|" org-clock-string "\\)"
  4057. " *[[<]\\([^]>]+\\)[]>]")
  4058. org-keyword-time-not-clock-regexp
  4059. (concat "\\<\\(" org-scheduled-string
  4060. "\\|" org-deadline-string
  4061. "\\|" org-closed-string
  4062. "\\)"
  4063. " *[[<]\\([^]>]+\\)[]>]")
  4064. org-maybe-keyword-time-regexp
  4065. (concat "\\(\\<\\(" org-scheduled-string
  4066. "\\|" org-deadline-string
  4067. "\\|" org-closed-string
  4068. "\\|" org-clock-string "\\)\\)?"
  4069. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4070. org-planning-or-clock-line-re
  4071. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4072. "\\|" org-deadline-string
  4073. "\\|" org-closed-string "\\|" org-clock-string
  4074. "\\)\\>\\)")
  4075. )
  4076. (org-compute-latex-and-specials-regexp)
  4077. (org-set-font-lock-defaults)))
  4078. (defun org-remove-keyword-keys (list)
  4079. (mapcar (lambda (x)
  4080. (if (string-match "(..?)$" x)
  4081. (substring x 0 (match-beginning 0))
  4082. x))
  4083. list))
  4084. ;; FIXME: this could be done much better, using second characters etc.
  4085. (defun org-assign-fast-keys (alist)
  4086. "Assign fast keys to a keyword-key alist.
  4087. Respect keys that are already there."
  4088. (let (new e k c c1 c2 (char ?a))
  4089. (while (setq e (pop alist))
  4090. (cond
  4091. ((equal e '(:startgroup)) (push e new))
  4092. ((equal e '(:endgroup)) (push e new))
  4093. (t
  4094. (setq k (car e) c2 nil)
  4095. (if (cdr e)
  4096. (setq c (cdr e))
  4097. ;; automatically assign a character.
  4098. (setq c1 (string-to-char
  4099. (downcase (substring
  4100. k (if (= (string-to-char k) ?@) 1 0)))))
  4101. (if (or (rassoc c1 new) (rassoc c1 alist))
  4102. (while (or (rassoc char new) (rassoc char alist))
  4103. (setq char (1+ char)))
  4104. (setq c2 c1))
  4105. (setq c (or c2 char)))
  4106. (push (cons k c) new))))
  4107. (nreverse new)))
  4108. ;;; Some variables ujsed in various places
  4109. (defvar org-window-configuration nil
  4110. "Used in various places to store a window configuration.")
  4111. (defvar org-finish-function nil
  4112. "Function to be called when `C-c C-c' is used.
  4113. This is for getting out of special buffers like remember.")
  4114. ;; FIXME: Occasionally check by commenting these, to make sure
  4115. ;; no other functions uses these, forgetting to let-bind them.
  4116. (defvar entry)
  4117. (defvar state)
  4118. (defvar last-state)
  4119. (defvar date)
  4120. (defvar description)
  4121. ;; Defined somewhere in this file, but used before definition.
  4122. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4123. (defvar org-agenda-buffer-name)
  4124. (defvar org-agenda-undo-list)
  4125. (defvar org-agenda-pending-undo-list)
  4126. (defvar org-agenda-overriding-header)
  4127. (defvar orgtbl-mode)
  4128. (defvar org-html-entities)
  4129. (defvar org-struct-menu)
  4130. (defvar org-org-menu)
  4131. (defvar org-tbl-menu)
  4132. (defvar org-agenda-keymap)
  4133. ;;;; Emacs/XEmacs compatibility
  4134. ;; Overlay compatibility functions
  4135. (defun org-make-overlay (beg end &optional buffer)
  4136. (if (featurep 'xemacs)
  4137. (make-extent beg end buffer)
  4138. (make-overlay beg end buffer)))
  4139. (defun org-delete-overlay (ovl)
  4140. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4141. (defun org-detach-overlay (ovl)
  4142. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4143. (defun org-move-overlay (ovl beg end &optional buffer)
  4144. (if (featurep 'xemacs)
  4145. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4146. (move-overlay ovl beg end buffer)))
  4147. (defun org-overlay-put (ovl prop value)
  4148. (if (featurep 'xemacs)
  4149. (set-extent-property ovl prop value)
  4150. (overlay-put ovl prop value)))
  4151. (defun org-overlay-display (ovl text &optional face evap)
  4152. "Make overlay OVL display TEXT with face FACE."
  4153. (if (featurep 'xemacs)
  4154. (let ((gl (make-glyph text)))
  4155. (and face (set-glyph-face gl face))
  4156. (set-extent-property ovl 'invisible t)
  4157. (set-extent-property ovl 'end-glyph gl))
  4158. (overlay-put ovl 'display text)
  4159. (if face (overlay-put ovl 'face face))
  4160. (if evap (overlay-put ovl 'evaporate t))))
  4161. (defun org-overlay-before-string (ovl text &optional face evap)
  4162. "Make overlay OVL display TEXT with face FACE."
  4163. (if (featurep 'xemacs)
  4164. (let ((gl (make-glyph text)))
  4165. (and face (set-glyph-face gl face))
  4166. (set-extent-property ovl 'begin-glyph gl))
  4167. (if face (org-add-props text nil 'face face))
  4168. (overlay-put ovl 'before-string text)
  4169. (if evap (overlay-put ovl 'evaporate t))))
  4170. (defun org-overlay-get (ovl prop)
  4171. (if (featurep 'xemacs)
  4172. (extent-property ovl prop)
  4173. (overlay-get ovl prop)))
  4174. (defun org-overlays-at (pos)
  4175. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4176. (defun org-overlays-in (&optional start end)
  4177. (if (featurep 'xemacs)
  4178. (extent-list nil start end)
  4179. (overlays-in start end)))
  4180. (defun org-overlay-start (o)
  4181. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4182. (defun org-overlay-end (o)
  4183. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4184. (defun org-find-overlays (prop &optional pos delete)
  4185. "Find all overlays specifying PROP at POS or point.
  4186. If DELETE is non-nil, delete all those overlays."
  4187. (let ((overlays (org-overlays-at (or pos (point))))
  4188. ov found)
  4189. (while (setq ov (pop overlays))
  4190. (if (org-overlay-get ov prop)
  4191. (if delete (org-delete-overlay ov) (push ov found))))
  4192. found))
  4193. ;; Region compatibility
  4194. (defun org-add-hook (hook function &optional append local)
  4195. "Add-hook, compatible with both Emacsen."
  4196. (if (and local (featurep 'xemacs))
  4197. (add-local-hook hook function append)
  4198. (add-hook hook function append local)))
  4199. (defvar org-ignore-region nil
  4200. "To temporarily disable the active region.")
  4201. (defun org-region-active-p ()
  4202. "Is `transient-mark-mode' on and the region active?
  4203. Works on both Emacs and XEmacs."
  4204. (if org-ignore-region
  4205. nil
  4206. (if (featurep 'xemacs)
  4207. (and zmacs-regions (region-active-p))
  4208. (if (fboundp 'use-region-p)
  4209. (use-region-p)
  4210. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4211. ;; Invisibility compatibility
  4212. (defun org-add-to-invisibility-spec (arg)
  4213. "Add elements to `buffer-invisibility-spec'.
  4214. See documentation for `buffer-invisibility-spec' for the kind of elements
  4215. that can be added."
  4216. (cond
  4217. ((fboundp 'add-to-invisibility-spec)
  4218. (add-to-invisibility-spec arg))
  4219. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4220. (setq buffer-invisibility-spec (list arg)))
  4221. (t
  4222. (setq buffer-invisibility-spec
  4223. (cons arg buffer-invisibility-spec)))))
  4224. (defun org-remove-from-invisibility-spec (arg)
  4225. "Remove elements from `buffer-invisibility-spec'."
  4226. (if (fboundp 'remove-from-invisibility-spec)
  4227. (remove-from-invisibility-spec arg)
  4228. (if (consp buffer-invisibility-spec)
  4229. (setq buffer-invisibility-spec
  4230. (delete arg buffer-invisibility-spec)))))
  4231. (defun org-in-invisibility-spec-p (arg)
  4232. "Is ARG a member of `buffer-invisibility-spec'?"
  4233. (if (consp buffer-invisibility-spec)
  4234. (member arg buffer-invisibility-spec)
  4235. nil))
  4236. ;;;; Define the Org-mode
  4237. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4238. (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."))
  4239. ;; We use a before-change function to check if a table might need
  4240. ;; an update.
  4241. (defvar org-table-may-need-update t
  4242. "Indicates that a table might need an update.
  4243. This variable is set by `org-before-change-function'.
  4244. `org-table-align' sets it back to nil.")
  4245. (defvar org-mode-map)
  4246. (defvar org-mode-hook nil)
  4247. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4248. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4249. (defvar org-table-buffer-is-an nil)
  4250. (defconst org-outline-regexp "\\*+ ")
  4251. ;;;###autoload
  4252. (define-derived-mode org-mode outline-mode "Org"
  4253. "Outline-based notes management and organizer, alias
  4254. \"Carsten's outline-mode for keeping track of everything.\"
  4255. Org-mode develops organizational tasks around a NOTES file which
  4256. contains information about projects as plain text. Org-mode is
  4257. implemented on top of outline-mode, which is ideal to keep the content
  4258. of large files well structured. It supports ToDo items, deadlines and
  4259. time stamps, which magically appear in the diary listing of the Emacs
  4260. calendar. Tables are easily created with a built-in table editor.
  4261. Plain text URL-like links connect to websites, emails (VM), Usenet
  4262. messages (Gnus), BBDB entries, and any files related to the project.
  4263. For printing and sharing of notes, an Org-mode file (or a part of it)
  4264. can be exported as a structured ASCII or HTML file.
  4265. The following commands are available:
  4266. \\{org-mode-map}"
  4267. ;; Get rid of Outline menus, they are not needed
  4268. ;; Need to do this here because define-derived-mode sets up
  4269. ;; the keymap so late. Still, it is a waste to call this each time
  4270. ;; we switch another buffer into org-mode.
  4271. (if (featurep 'xemacs)
  4272. (when (boundp 'outline-mode-menu-heading)
  4273. ;; Assume this is Greg's port, it used easymenu
  4274. (easy-menu-remove outline-mode-menu-heading)
  4275. (easy-menu-remove outline-mode-menu-show)
  4276. (easy-menu-remove outline-mode-menu-hide))
  4277. (define-key org-mode-map [menu-bar headings] 'undefined)
  4278. (define-key org-mode-map [menu-bar hide] 'undefined)
  4279. (define-key org-mode-map [menu-bar show] 'undefined))
  4280. (easy-menu-add org-org-menu)
  4281. (easy-menu-add org-tbl-menu)
  4282. (org-install-agenda-files-menu)
  4283. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4284. (org-add-to-invisibility-spec '(org-cwidth))
  4285. (when (featurep 'xemacs)
  4286. (org-set-local 'line-move-ignore-invisible t))
  4287. (org-set-local 'outline-regexp org-outline-regexp)
  4288. (org-set-local 'outline-level 'org-outline-level)
  4289. (when (and org-ellipsis
  4290. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4291. (fboundp 'make-glyph-code))
  4292. (unless org-display-table
  4293. (setq org-display-table (make-display-table)))
  4294. (set-display-table-slot
  4295. org-display-table 4
  4296. (vconcat (mapcar
  4297. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4298. org-ellipsis)))
  4299. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4300. (setq buffer-display-table org-display-table))
  4301. (org-set-regexps-and-options)
  4302. ;; Calc embedded
  4303. (org-set-local 'calc-embedded-open-mode "# ")
  4304. (modify-syntax-entry ?# "<")
  4305. (modify-syntax-entry ?@ "w")
  4306. (if org-startup-truncated (setq truncate-lines t))
  4307. (org-set-local 'font-lock-unfontify-region-function
  4308. 'org-unfontify-region)
  4309. ;; Activate before-change-function
  4310. (org-set-local 'org-table-may-need-update t)
  4311. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4312. 'local)
  4313. ;; Check for running clock before killing a buffer
  4314. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4315. ;; Paragraphs and auto-filling
  4316. (org-set-autofill-regexps)
  4317. (setq indent-line-function 'org-indent-line-function)
  4318. (org-update-radio-target-regexp)
  4319. ;; Comment characters
  4320. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4321. (org-set-local 'comment-padding " ")
  4322. ;; Imenu
  4323. (org-set-local 'imenu-create-index-function
  4324. 'org-imenu-get-tree)
  4325. ;; Make isearch reveal context
  4326. (if (or (featurep 'xemacs)
  4327. (not (boundp 'outline-isearch-open-invisible-function)))
  4328. ;; Emacs 21 and XEmacs make use of the hook
  4329. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4330. ;; Emacs 22 deals with this through a special variable
  4331. (org-set-local 'outline-isearch-open-invisible-function
  4332. (lambda (&rest ignore) (org-show-context 'isearch))))
  4333. ;; If empty file that did not turn on org-mode automatically, make it to.
  4334. (if (and org-insert-mode-line-in-empty-file
  4335. (interactive-p)
  4336. (= (point-min) (point-max)))
  4337. (insert "# -*- mode: org -*-\n\n"))
  4338. (unless org-inhibit-startup
  4339. (when org-startup-align-all-tables
  4340. (let ((bmp (buffer-modified-p)))
  4341. (org-table-map-tables 'org-table-align)
  4342. (set-buffer-modified-p bmp)))
  4343. (org-cycle-hide-drawers 'all)
  4344. (cond
  4345. ((eq org-startup-folded t)
  4346. (org-cycle '(4)))
  4347. ((eq org-startup-folded 'content)
  4348. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4349. (org-cycle '(4)) (org-cycle '(4)))))))
  4350. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4351. (defsubst org-call-with-arg (command arg)
  4352. "Call COMMAND interactively, but pretend prefix are was ARG."
  4353. (let ((current-prefix-arg arg)) (call-interactively command)))
  4354. (defsubst org-current-line (&optional pos)
  4355. (save-excursion
  4356. (and pos (goto-char pos))
  4357. ;; works also in narrowed buffer, because we start at 1, not point-min
  4358. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4359. (defun org-current-time ()
  4360. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4361. (if (> org-time-stamp-rounding-minutes 0)
  4362. (let ((r org-time-stamp-rounding-minutes)
  4363. (time (decode-time)))
  4364. (apply 'encode-time
  4365. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4366. (nthcdr 2 time))))
  4367. (current-time)))
  4368. (defun org-add-props (string plist &rest props)
  4369. "Add text properties to entire string, from beginning to end.
  4370. PLIST may be a list of properties, PROPS are individual properties and values
  4371. that will be added to PLIST. Returns the string that was modified."
  4372. (add-text-properties
  4373. 0 (length string) (if props (append plist props) plist) string)
  4374. string)
  4375. (put 'org-add-props 'lisp-indent-function 2)
  4376. ;;;; Font-Lock stuff, including the activators
  4377. (defvar org-mouse-map (make-sparse-keymap))
  4378. (org-defkey org-mouse-map
  4379. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4380. (org-defkey org-mouse-map
  4381. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4382. (when org-mouse-1-follows-link
  4383. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4384. (when org-tab-follows-link
  4385. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4386. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4387. (when org-return-follows-link
  4388. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4389. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4390. (require 'font-lock)
  4391. (defconst org-non-link-chars "]\t\n\r<>")
  4392. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4393. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4394. (defvar org-link-re-with-space nil
  4395. "Matches a link with spaces, optional angular brackets around it.")
  4396. (defvar org-link-re-with-space2 nil
  4397. "Matches a link with spaces, optional angular brackets around it.")
  4398. (defvar org-angle-link-re nil
  4399. "Matches link with angular brackets, spaces are allowed.")
  4400. (defvar org-plain-link-re nil
  4401. "Matches plain link, without spaces.")
  4402. (defvar org-bracket-link-regexp nil
  4403. "Matches a link in double brackets.")
  4404. (defvar org-bracket-link-analytic-regexp nil
  4405. "Regular expression used to analyze links.
  4406. Here is what the match groups contain after a match:
  4407. 1: http:
  4408. 2: http
  4409. 3: path
  4410. 4: [desc]
  4411. 5: desc")
  4412. (defvar org-any-link-re nil
  4413. "Regular expression matching any link.")
  4414. (defun org-make-link-regexps ()
  4415. "Update the link regular expressions.
  4416. This should be called after the variable `org-link-types' has changed."
  4417. (setq org-link-re-with-space
  4418. (concat
  4419. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4420. "\\([^" org-non-link-chars " ]"
  4421. "[^" org-non-link-chars "]*"
  4422. "[^" org-non-link-chars " ]\\)>?")
  4423. org-link-re-with-space2
  4424. (concat
  4425. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4426. "\\([^" org-non-link-chars " ]"
  4427. "[^]\t\n\r]*"
  4428. "[^" org-non-link-chars " ]\\)>?")
  4429. org-angle-link-re
  4430. (concat
  4431. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4432. "\\([^" org-non-link-chars " ]"
  4433. "[^" org-non-link-chars "]*"
  4434. "\\)>")
  4435. org-plain-link-re
  4436. (concat
  4437. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4438. "\\([^]\t\n\r<>,;() ]+\\)")
  4439. org-bracket-link-regexp
  4440. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4441. org-bracket-link-analytic-regexp
  4442. (concat
  4443. "\\[\\["
  4444. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4445. "\\([^]]+\\)"
  4446. "\\]"
  4447. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4448. "\\]")
  4449. org-any-link-re
  4450. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4451. org-angle-link-re "\\)\\|\\("
  4452. org-plain-link-re "\\)")))
  4453. (org-make-link-regexps)
  4454. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4455. "Regular expression for fast time stamp matching.")
  4456. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4457. "Regular expression for fast time stamp matching.")
  4458. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4459. "Regular expression matching time strings for analysis.
  4460. This one does not require the space after the date.")
  4461. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4462. "Regular expression matching time strings for analysis.")
  4463. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4464. "Regular expression matching time stamps, with groups.")
  4465. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4466. "Regular expression matching time stamps (also [..]), with groups.")
  4467. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4468. "Regular expression matching a time stamp range.")
  4469. (defconst org-tr-regexp-both
  4470. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4471. "Regular expression matching a time stamp range.")
  4472. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4473. org-ts-regexp "\\)?")
  4474. "Regular expression matching a time stamp or time stamp range.")
  4475. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4476. org-ts-regexp-both "\\)?")
  4477. "Regular expression matching a time stamp or time stamp range.
  4478. The time stamps may be either active or inactive.")
  4479. (defvar org-emph-face nil)
  4480. (defun org-do-emphasis-faces (limit)
  4481. "Run through the buffer and add overlays to links."
  4482. (let (rtn)
  4483. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4484. (if (not (= (char-after (match-beginning 3))
  4485. (char-after (match-beginning 4))))
  4486. (progn
  4487. (setq rtn t)
  4488. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4489. 'face
  4490. (nth 1 (assoc (match-string 3)
  4491. org-emphasis-alist)))
  4492. (add-text-properties (match-beginning 2) (match-end 2)
  4493. '(font-lock-multiline t))
  4494. (when org-hide-emphasis-markers
  4495. (add-text-properties (match-end 4) (match-beginning 5)
  4496. '(invisible org-link))
  4497. (add-text-properties (match-beginning 3) (match-end 3)
  4498. '(invisible org-link)))))
  4499. (backward-char 1))
  4500. rtn))
  4501. (defun org-emphasize (&optional char)
  4502. "Insert or change an emphasis, i.e. a font like bold or italic.
  4503. If there is an active region, change that region to a new emphasis.
  4504. If there is no region, just insert the marker characters and position
  4505. the cursor between them.
  4506. CHAR should be either the marker character, or the first character of the
  4507. HTML tag associated with that emphasis. If CHAR is a space, the means
  4508. to remove the emphasis of the selected region.
  4509. If char is not given (for example in an interactive call) it
  4510. will be prompted for."
  4511. (interactive)
  4512. (let ((eal org-emphasis-alist) e det
  4513. (erc org-emphasis-regexp-components)
  4514. (prompt "")
  4515. (string "") beg end move tag c s)
  4516. (if (org-region-active-p)
  4517. (setq beg (region-beginning) end (region-end)
  4518. string (buffer-substring beg end))
  4519. (setq move t))
  4520. (while (setq e (pop eal))
  4521. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4522. c (aref tag 0))
  4523. (push (cons c (string-to-char (car e))) det)
  4524. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4525. (substring tag 1)))))
  4526. (unless char
  4527. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4528. (setq char (read-char-exclusive)))
  4529. (setq char (or (cdr (assoc char det)) char))
  4530. (if (equal char ?\ )
  4531. (setq s "" move nil)
  4532. (unless (assoc (char-to-string char) org-emphasis-alist)
  4533. (error "No such emphasis marker: \"%c\"" char))
  4534. (setq s (char-to-string char)))
  4535. (while (and (> (length string) 1)
  4536. (equal (substring string 0 1) (substring string -1))
  4537. (assoc (substring string 0 1) org-emphasis-alist))
  4538. (setq string (substring string 1 -1)))
  4539. (setq string (concat s string s))
  4540. (if beg (delete-region beg end))
  4541. (unless (or (bolp)
  4542. (string-match (concat "[" (nth 0 erc) "\n]")
  4543. (char-to-string (char-before (point)))))
  4544. (insert " "))
  4545. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4546. (char-to-string (char-after (point))))
  4547. (insert " ") (backward-char 1))
  4548. (insert string)
  4549. (and move (backward-char 1))))
  4550. (defconst org-nonsticky-props
  4551. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4552. (defun org-activate-plain-links (limit)
  4553. "Run through the buffer and add overlays to links."
  4554. (catch 'exit
  4555. (let (f)
  4556. (while (re-search-forward org-plain-link-re limit t)
  4557. (setq f (get-text-property (match-beginning 0) 'face))
  4558. (if (or (eq f 'org-tag)
  4559. (and (listp f) (memq 'org-tag f)))
  4560. nil
  4561. (add-text-properties (match-beginning 0) (match-end 0)
  4562. (list 'mouse-face 'highlight
  4563. 'rear-nonsticky org-nonsticky-props
  4564. 'keymap org-mouse-map
  4565. ))
  4566. (throw 'exit t))))))
  4567. (defun org-activate-code (limit)
  4568. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4569. (unless (get-text-property (match-beginning 1) 'face)
  4570. (remove-text-properties (match-beginning 0) (match-end 0)
  4571. '(display t invisible t intangible t))
  4572. t)))
  4573. (defun org-activate-angle-links (limit)
  4574. "Run through the buffer and add overlays to links."
  4575. (if (re-search-forward org-angle-link-re limit t)
  4576. (progn
  4577. (add-text-properties (match-beginning 0) (match-end 0)
  4578. (list 'mouse-face 'highlight
  4579. 'rear-nonsticky org-nonsticky-props
  4580. 'keymap org-mouse-map
  4581. ))
  4582. t)))
  4583. (defmacro org-maybe-intangible (props)
  4584. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4585. In emacs 21, invisible text is not avoided by the command loop, so the
  4586. intangible property is needed to make sure point skips this text.
  4587. In Emacs 22, this is not necessary. The intangible text property has
  4588. led to problems with flyspell. These problems are fixed in flyspell.el,
  4589. but we still avoid setting the property in Emacs 22 and later.
  4590. We use a macro so that the test can happen at compilation time."
  4591. (if (< emacs-major-version 22)
  4592. `(append '(intangible t) ,props)
  4593. props))
  4594. (defun org-activate-bracket-links (limit)
  4595. "Run through the buffer and add overlays to bracketed links."
  4596. (if (re-search-forward org-bracket-link-regexp limit t)
  4597. (let* ((help (concat "LINK: "
  4598. (org-match-string-no-properties 1)))
  4599. ;; FIXME: above we should remove the escapes.
  4600. ;; but that requires another match, protecting match data,
  4601. ;; a lot of overhead for font-lock.
  4602. (ip (org-maybe-intangible
  4603. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4604. 'keymap org-mouse-map 'mouse-face 'highlight
  4605. 'font-lock-multiline t 'help-echo help)))
  4606. (vp (list 'rear-nonsticky org-nonsticky-props
  4607. 'keymap org-mouse-map 'mouse-face 'highlight
  4608. ' font-lock-multiline t 'help-echo help)))
  4609. ;; We need to remove the invisible property here. Table narrowing
  4610. ;; may have made some of this invisible.
  4611. (remove-text-properties (match-beginning 0) (match-end 0)
  4612. '(invisible nil))
  4613. (if (match-end 3)
  4614. (progn
  4615. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4616. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4617. (add-text-properties (match-end 3) (match-end 0) ip))
  4618. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4619. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4620. (add-text-properties (match-end 1) (match-end 0) ip))
  4621. t)))
  4622. (defun org-activate-dates (limit)
  4623. "Run through the buffer and add overlays to dates."
  4624. (if (re-search-forward org-tsr-regexp-both limit t)
  4625. (progn
  4626. (add-text-properties (match-beginning 0) (match-end 0)
  4627. (list 'mouse-face 'highlight
  4628. 'rear-nonsticky org-nonsticky-props
  4629. 'keymap org-mouse-map))
  4630. (when org-display-custom-times
  4631. (if (match-end 3)
  4632. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4633. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4634. t)))
  4635. (defvar org-target-link-regexp nil
  4636. "Regular expression matching radio targets in plain text.")
  4637. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4638. "Regular expression matching a link target.")
  4639. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4640. "Regular expression matching a radio target.")
  4641. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4642. "Regular expression matching any target.")
  4643. (defun org-activate-target-links (limit)
  4644. "Run through the buffer and add overlays to target matches."
  4645. (when org-target-link-regexp
  4646. (let ((case-fold-search t))
  4647. (if (re-search-forward org-target-link-regexp limit t)
  4648. (progn
  4649. (add-text-properties (match-beginning 0) (match-end 0)
  4650. (list 'mouse-face 'highlight
  4651. 'rear-nonsticky org-nonsticky-props
  4652. 'keymap org-mouse-map
  4653. 'help-echo "Radio target link"
  4654. 'org-linked-text t))
  4655. t)))))
  4656. (defun org-update-radio-target-regexp ()
  4657. "Find all radio targets in this file and update the regular expression."
  4658. (interactive)
  4659. (when (memq 'radio org-activate-links)
  4660. (setq org-target-link-regexp
  4661. (org-make-target-link-regexp (org-all-targets 'radio)))
  4662. (org-restart-font-lock)))
  4663. (defun org-hide-wide-columns (limit)
  4664. (let (s e)
  4665. (setq s (text-property-any (point) (or limit (point-max))
  4666. 'org-cwidth t))
  4667. (when s
  4668. (setq e (next-single-property-change s 'org-cwidth))
  4669. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4670. (goto-char e)
  4671. t)))
  4672. (defvar org-latex-and-specials-regexp nil
  4673. "Regular expression for highlighting export special stuff.")
  4674. (defvar org-match-substring-regexp)
  4675. (defvar org-match-substring-with-braces-regexp)
  4676. (defvar org-export-html-special-string-regexps)
  4677. (defun org-compute-latex-and-specials-regexp ()
  4678. "Compute regular expression for stuff treated specially by exporters."
  4679. (if (not org-highlight-latex-fragments-and-specials)
  4680. (org-set-local 'org-latex-and-specials-regexp nil)
  4681. (let*
  4682. ((matchers (plist-get org-format-latex-options :matchers))
  4683. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4684. org-latex-regexps)))
  4685. (options (org-combine-plists (org-default-export-plist)
  4686. (org-infile-export-plist)))
  4687. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4688. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4689. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4690. (org-export-html-expand (plist-get options :expand-quoted-html))
  4691. (org-export-with-special-strings (plist-get options :special-strings))
  4692. (re-sub
  4693. (cond
  4694. ((equal org-export-with-sub-superscripts '{})
  4695. (list org-match-substring-with-braces-regexp))
  4696. (org-export-with-sub-superscripts
  4697. (list org-match-substring-regexp))
  4698. (t nil)))
  4699. (re-latex
  4700. (if org-export-with-LaTeX-fragments
  4701. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4702. (re-macros
  4703. (if org-export-with-TeX-macros
  4704. (list (concat "\\\\"
  4705. (regexp-opt
  4706. (append (mapcar 'car org-html-entities)
  4707. (if (boundp 'org-latex-entities)
  4708. org-latex-entities nil))
  4709. 'words))) ; FIXME
  4710. ))
  4711. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4712. (re-special (if org-export-with-special-strings
  4713. (mapcar (lambda (x) (car x))
  4714. org-export-html-special-string-regexps)))
  4715. (re-rest
  4716. (delq nil
  4717. (list
  4718. (if org-export-html-expand "@<[^>\n]+>")
  4719. ))))
  4720. (org-set-local
  4721. 'org-latex-and-specials-regexp
  4722. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4723. re-rest) "\\|")))))
  4724. (defface org-latex-and-export-specials
  4725. (let ((font (cond ((assq :inherit custom-face-attributes)
  4726. '(:inherit underline))
  4727. (t '(:underline t)))))
  4728. `((((class grayscale) (background light))
  4729. (:foreground "DimGray" ,@font))
  4730. (((class grayscale) (background dark))
  4731. (:foreground "LightGray" ,@font))
  4732. (((class color) (background light))
  4733. (:foreground "SaddleBrown"))
  4734. (((class color) (background dark))
  4735. (:foreground "burlywood"))
  4736. (t (,@font))))
  4737. "Face used to highlight math latex and other special exporter stuff."
  4738. :group 'org-faces)
  4739. (defun org-do-latex-and-special-faces (limit)
  4740. "Run through the buffer and add overlays to links."
  4741. (when org-latex-and-specials-regexp
  4742. (let (rtn d)
  4743. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4744. limit t))
  4745. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4746. 'face))
  4747. '(org-code org-verbatim underline)))
  4748. (progn
  4749. (setq rtn t
  4750. d (cond ((member (char-after (1+ (match-beginning 0)))
  4751. '(?_ ?^)) 1)
  4752. (t 0)))
  4753. (font-lock-prepend-text-property
  4754. (+ d (match-beginning 0)) (match-end 0)
  4755. 'face 'org-latex-and-export-specials)
  4756. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4757. '(font-lock-multiline t)))))
  4758. rtn)))
  4759. (defun org-restart-font-lock ()
  4760. "Restart font-lock-mode, to force refontification."
  4761. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4762. (font-lock-mode -1)
  4763. (font-lock-mode 1)))
  4764. (defun org-all-targets (&optional radio)
  4765. "Return a list of all targets in this file.
  4766. With optional argument RADIO, only find radio targets."
  4767. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4768. rtn)
  4769. (save-excursion
  4770. (goto-char (point-min))
  4771. (while (re-search-forward re nil t)
  4772. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4773. rtn)))
  4774. (defun org-make-target-link-regexp (targets)
  4775. "Make regular expression matching all strings in TARGETS.
  4776. The regular expression finds the targets also if there is a line break
  4777. between words."
  4778. (and targets
  4779. (concat
  4780. "\\<\\("
  4781. (mapconcat
  4782. (lambda (x)
  4783. (while (string-match " +" x)
  4784. (setq x (replace-match "\\s-+" t t x)))
  4785. x)
  4786. targets
  4787. "\\|")
  4788. "\\)\\>")))
  4789. (defun org-activate-tags (limit)
  4790. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4791. (progn
  4792. (add-text-properties (match-beginning 1) (match-end 1)
  4793. (list 'mouse-face 'highlight
  4794. 'rear-nonsticky org-nonsticky-props
  4795. 'keymap org-mouse-map))
  4796. t)))
  4797. (defun org-outline-level ()
  4798. (save-excursion
  4799. (looking-at outline-regexp)
  4800. (if (match-beginning 1)
  4801. (+ (org-get-string-indentation (match-string 1)) 1000)
  4802. (1- (- (match-end 0) (match-beginning 0))))))
  4803. (defvar org-font-lock-keywords nil)
  4804. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4805. "Regular expression matching a property line.")
  4806. (defun org-set-font-lock-defaults ()
  4807. (let* ((em org-fontify-emphasized-text)
  4808. (lk org-activate-links)
  4809. (org-font-lock-extra-keywords
  4810. (list
  4811. ;; Headlines
  4812. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4813. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4814. ;; Table lines
  4815. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4816. (1 'org-table t))
  4817. ;; Table internals
  4818. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4819. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4820. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4821. ;; Drawers
  4822. (list org-drawer-regexp '(0 'org-special-keyword t))
  4823. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4824. ;; Properties
  4825. (list org-property-re
  4826. '(1 'org-special-keyword t)
  4827. '(3 'org-property-value t))
  4828. (if org-format-transports-properties-p
  4829. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4830. ;; Links
  4831. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4832. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4833. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4834. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4835. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4836. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4837. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4838. '(org-hide-wide-columns (0 nil append))
  4839. ;; TODO lines
  4840. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4841. '(1 (org-get-todo-face 1) t))
  4842. ;; DONE
  4843. (if org-fontify-done-headline
  4844. (list (concat "^[*]+ +\\<\\("
  4845. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4846. "\\)\\(.*\\)")
  4847. '(2 'org-headline-done t))
  4848. nil)
  4849. ;; Priorities
  4850. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4851. ;; Special keywords
  4852. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4853. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4854. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4855. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4856. ;; Emphasis
  4857. (if em
  4858. (if (featurep 'xemacs)
  4859. '(org-do-emphasis-faces (0 nil append))
  4860. '(org-do-emphasis-faces)))
  4861. ;; Checkboxes
  4862. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4863. 2 'bold prepend)
  4864. (if org-provide-checkbox-statistics
  4865. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4866. (0 (org-get-checkbox-statistics-face) t)))
  4867. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4868. '(1 'org-archived prepend))
  4869. ;; Specials
  4870. '(org-do-latex-and-special-faces)
  4871. ;; Code
  4872. '(org-activate-code (1 'org-code t))
  4873. ;; COMMENT
  4874. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4875. "\\|" org-quote-string "\\)\\>")
  4876. '(1 'org-special-keyword t))
  4877. '("^#.*" (0 'font-lock-comment-face t))
  4878. )))
  4879. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4880. ;; Now set the full font-lock-keywords
  4881. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4882. (org-set-local 'font-lock-defaults
  4883. '(org-font-lock-keywords t nil nil backward-paragraph))
  4884. (kill-local-variable 'font-lock-keywords) nil))
  4885. (defvar org-m nil)
  4886. (defvar org-l nil)
  4887. (defvar org-f nil)
  4888. (defun org-get-level-face (n)
  4889. "Get the right face for match N in font-lock matching of healdines."
  4890. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4891. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4892. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4893. (cond
  4894. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4895. ((eq n 2) org-f)
  4896. (t (if org-level-color-stars-only nil org-f))))
  4897. (defun org-get-todo-face (kwd)
  4898. "Get the right face for a TODO keyword KWD.
  4899. If KWD is a number, get the corresponding match group."
  4900. (if (numberp kwd) (setq kwd (match-string kwd)))
  4901. (or (cdr (assoc kwd org-todo-keyword-faces))
  4902. (and (member kwd org-done-keywords) 'org-done)
  4903. 'org-todo))
  4904. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4905. "Remove fontification and activation overlays from links."
  4906. (font-lock-default-unfontify-region beg end)
  4907. (let* ((buffer-undo-list t)
  4908. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4909. (inhibit-modification-hooks t)
  4910. deactivate-mark buffer-file-name buffer-file-truename)
  4911. (remove-text-properties beg end
  4912. '(mouse-face t keymap t org-linked-text t
  4913. invisible t intangible t))))
  4914. ;;;; Visibility cycling, including org-goto and indirect buffer
  4915. ;;; Cycling
  4916. (defvar org-cycle-global-status nil)
  4917. (make-variable-buffer-local 'org-cycle-global-status)
  4918. (defvar org-cycle-subtree-status nil)
  4919. (make-variable-buffer-local 'org-cycle-subtree-status)
  4920. ;;;###autoload
  4921. (defun org-cycle (&optional arg)
  4922. "Visibility cycling for Org-mode.
  4923. - When this function is called with a prefix argument, rotate the entire
  4924. buffer through 3 states (global cycling)
  4925. 1. OVERVIEW: Show only top-level headlines.
  4926. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4927. 3. SHOW ALL: Show everything.
  4928. - When point is at the beginning of a headline, rotate the subtree started
  4929. by this line through 3 different states (local cycling)
  4930. 1. FOLDED: Only the main headline is shown.
  4931. 2. CHILDREN: The main headline and the direct children are shown.
  4932. From this state, you can move to one of the children
  4933. and zoom in further.
  4934. 3. SUBTREE: Show the entire subtree, including body text.
  4935. - When there is a numeric prefix, go up to a heading with level ARG, do
  4936. a `show-subtree' and return to the previous cursor position. If ARG
  4937. is negative, go up that many levels.
  4938. - When point is not at the beginning of a headline, execute
  4939. `indent-relative', like TAB normally does. See the option
  4940. `org-cycle-emulate-tab' for details.
  4941. - Special case: if point is at the beginning of the buffer and there is
  4942. no headline in line 1, this function will act as if called with prefix arg.
  4943. But only if also the variable `org-cycle-global-at-bob' is t."
  4944. (interactive "P")
  4945. (let* ((outline-regexp
  4946. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4947. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4948. outline-regexp))
  4949. (bob-special (and org-cycle-global-at-bob (bobp)
  4950. (not (looking-at outline-regexp))))
  4951. (org-cycle-hook
  4952. (if bob-special
  4953. (delq 'org-optimize-window-after-visibility-change
  4954. (copy-sequence org-cycle-hook))
  4955. org-cycle-hook))
  4956. (pos (point)))
  4957. (if (or bob-special (equal arg '(4)))
  4958. ;; special case: use global cycling
  4959. (setq arg t))
  4960. (cond
  4961. ((org-at-table-p 'any)
  4962. ;; Enter the table or move to the next field in the table
  4963. (or (org-table-recognize-table.el)
  4964. (progn
  4965. (if arg (org-table-edit-field t)
  4966. (org-table-justify-field-maybe)
  4967. (call-interactively 'org-table-next-field)))))
  4968. ((eq arg t) ;; Global cycling
  4969. (cond
  4970. ((and (eq last-command this-command)
  4971. (eq org-cycle-global-status 'overview))
  4972. ;; We just created the overview - now do table of contents
  4973. ;; This can be slow in very large buffers, so indicate action
  4974. (message "CONTENTS...")
  4975. (org-content)
  4976. (message "CONTENTS...done")
  4977. (setq org-cycle-global-status 'contents)
  4978. (run-hook-with-args 'org-cycle-hook 'contents))
  4979. ((and (eq last-command this-command)
  4980. (eq org-cycle-global-status 'contents))
  4981. ;; We just showed the table of contents - now show everything
  4982. (show-all)
  4983. (message "SHOW ALL")
  4984. (setq org-cycle-global-status 'all)
  4985. (run-hook-with-args 'org-cycle-hook 'all))
  4986. (t
  4987. ;; Default action: go to overview
  4988. (org-overview)
  4989. (message "OVERVIEW")
  4990. (setq org-cycle-global-status 'overview)
  4991. (run-hook-with-args 'org-cycle-hook 'overview))))
  4992. ((and org-drawers org-drawer-regexp
  4993. (save-excursion
  4994. (beginning-of-line 1)
  4995. (looking-at org-drawer-regexp)))
  4996. ;; Toggle block visibility
  4997. (org-flag-drawer
  4998. (not (get-char-property (match-end 0) 'invisible))))
  4999. ((integerp arg)
  5000. ;; Show-subtree, ARG levels up from here.
  5001. (save-excursion
  5002. (org-back-to-heading)
  5003. (outline-up-heading (if (< arg 0) (- arg)
  5004. (- (funcall outline-level) arg)))
  5005. (org-show-subtree)))
  5006. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5007. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5008. ;; At a heading: rotate between three different views
  5009. (org-back-to-heading)
  5010. (let ((goal-column 0) eoh eol eos)
  5011. ;; First, some boundaries
  5012. (save-excursion
  5013. (org-back-to-heading)
  5014. (save-excursion
  5015. (beginning-of-line 2)
  5016. (while (and (not (eobp)) ;; this is like `next-line'
  5017. (get-char-property (1- (point)) 'invisible))
  5018. (beginning-of-line 2)) (setq eol (point)))
  5019. (outline-end-of-heading) (setq eoh (point))
  5020. (org-end-of-subtree t)
  5021. (unless (eobp)
  5022. (skip-chars-forward " \t\n")
  5023. (beginning-of-line 1) ; in case this is an item
  5024. )
  5025. (setq eos (1- (point))))
  5026. ;; Find out what to do next and set `this-command'
  5027. (cond
  5028. ((= eos eoh)
  5029. ;; Nothing is hidden behind this heading
  5030. (message "EMPTY ENTRY")
  5031. (setq org-cycle-subtree-status nil)
  5032. (save-excursion
  5033. (goto-char eos)
  5034. (outline-next-heading)
  5035. (if (org-invisible-p) (org-flag-heading nil))))
  5036. ((or (>= eol eos)
  5037. (not (string-match "\\S-" (buffer-substring eol eos))))
  5038. ;; Entire subtree is hidden in one line: open it
  5039. (org-show-entry)
  5040. (show-children)
  5041. (message "CHILDREN")
  5042. (save-excursion
  5043. (goto-char eos)
  5044. (outline-next-heading)
  5045. (if (org-invisible-p) (org-flag-heading nil)))
  5046. (setq org-cycle-subtree-status 'children)
  5047. (run-hook-with-args 'org-cycle-hook 'children))
  5048. ((and (eq last-command this-command)
  5049. (eq org-cycle-subtree-status 'children))
  5050. ;; We just showed the children, now show everything.
  5051. (org-show-subtree)
  5052. (message "SUBTREE")
  5053. (setq org-cycle-subtree-status 'subtree)
  5054. (run-hook-with-args 'org-cycle-hook 'subtree))
  5055. (t
  5056. ;; Default action: hide the subtree.
  5057. (hide-subtree)
  5058. (message "FOLDED")
  5059. (setq org-cycle-subtree-status 'folded)
  5060. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5061. ;; TAB emulation
  5062. (buffer-read-only (org-back-to-heading))
  5063. ((org-try-cdlatex-tab))
  5064. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5065. (or (not (bolp))
  5066. (not (looking-at outline-regexp))))
  5067. (call-interactively (global-key-binding "\t")))
  5068. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5069. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5070. (or (and (eq org-cycle-emulate-tab 'white)
  5071. (= (match-end 0) (point-at-eol)))
  5072. (and (eq org-cycle-emulate-tab 'whitestart)
  5073. (>= (match-end 0) pos))))
  5074. t
  5075. (eq org-cycle-emulate-tab t))
  5076. ; (if (and (looking-at "[ \n\r\t]")
  5077. ; (string-match "^[ \t]*$" (buffer-substring
  5078. ; (point-at-bol) (point))))
  5079. ; (progn
  5080. ; (beginning-of-line 1)
  5081. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5082. (call-interactively (global-key-binding "\t")))
  5083. (t (save-excursion
  5084. (org-back-to-heading)
  5085. (org-cycle))))))
  5086. ;;;###autoload
  5087. (defun org-global-cycle (&optional arg)
  5088. "Cycle the global visibility. For details see `org-cycle'."
  5089. (interactive "P")
  5090. (let ((org-cycle-include-plain-lists
  5091. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5092. (if (integerp arg)
  5093. (progn
  5094. (show-all)
  5095. (hide-sublevels arg)
  5096. (setq org-cycle-global-status 'contents))
  5097. (org-cycle '(4)))))
  5098. (defun org-overview ()
  5099. "Switch to overview mode, shoing only top-level headlines.
  5100. Really, this shows all headlines with level equal or greater than the level
  5101. of the first headline in the buffer. This is important, because if the
  5102. first headline is not level one, then (hide-sublevels 1) gives confusing
  5103. results."
  5104. (interactive)
  5105. (let ((level (save-excursion
  5106. (goto-char (point-min))
  5107. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5108. (progn
  5109. (goto-char (match-beginning 0))
  5110. (funcall outline-level))))))
  5111. (and level (hide-sublevels level))))
  5112. (defun org-content (&optional arg)
  5113. "Show all headlines in the buffer, like a table of contents.
  5114. With numerical argument N, show content up to level N."
  5115. (interactive "P")
  5116. (save-excursion
  5117. ;; Visit all headings and show their offspring
  5118. (and (integerp arg) (org-overview))
  5119. (goto-char (point-max))
  5120. (catch 'exit
  5121. (while (and (progn (condition-case nil
  5122. (outline-previous-visible-heading 1)
  5123. (error (goto-char (point-min))))
  5124. t)
  5125. (looking-at outline-regexp))
  5126. (if (integerp arg)
  5127. (show-children (1- arg))
  5128. (show-branches))
  5129. (if (bobp) (throw 'exit nil))))))
  5130. (defun org-optimize-window-after-visibility-change (state)
  5131. "Adjust the window after a change in outline visibility.
  5132. This function is the default value of the hook `org-cycle-hook'."
  5133. (when (get-buffer-window (current-buffer))
  5134. (cond
  5135. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5136. ; ((eq state 'overview) (org-beginning-of-line))
  5137. ((eq state 'content) nil)
  5138. ((eq state 'all) nil)
  5139. ((eq state 'folded) nil)
  5140. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5141. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5142. (defun org-compact-display-after-subtree-move ()
  5143. (let (beg end)
  5144. (save-excursion
  5145. (if (org-up-heading-safe)
  5146. (progn
  5147. (hide-subtree)
  5148. (show-entry)
  5149. (show-children)
  5150. (org-cycle-show-empty-lines 'children)
  5151. (org-cycle-hide-drawers 'children))
  5152. (org-overview)))))
  5153. (defun org-cycle-show-empty-lines (state)
  5154. "Show empty lines above all visible headlines.
  5155. The region to be covered depends on STATE when called through
  5156. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5157. entire buffer covered. Note that an empty line is only shown if there
  5158. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5159. (when (> org-cycle-separator-lines 0)
  5160. (save-excursion
  5161. (let* ((n org-cycle-separator-lines)
  5162. (re (cond
  5163. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5164. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5165. (t (let ((ns (number-to-string (- n 2))))
  5166. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5167. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5168. beg end)
  5169. (cond
  5170. ((memq state '(overview contents t))
  5171. (setq beg (point-min) end (point-max)))
  5172. ((memq state '(children folded))
  5173. (setq beg (point) end (progn (org-end-of-subtree t t)
  5174. (beginning-of-line 2)
  5175. (point)))))
  5176. (when beg
  5177. (goto-char beg)
  5178. (while (re-search-forward re end t)
  5179. (if (not (get-char-property (match-end 1) 'invisible))
  5180. (outline-flag-region
  5181. (match-beginning 1) (match-end 1) nil)))))))
  5182. ;; Never hide empty lines at the end of the file.
  5183. (save-excursion
  5184. (goto-char (point-max))
  5185. (outline-previous-heading)
  5186. (outline-end-of-heading)
  5187. (if (and (looking-at "[ \t\n]+")
  5188. (= (match-end 0) (point-max)))
  5189. (outline-flag-region (point) (match-end 0) nil))))
  5190. (defun org-subtree-end-visible-p ()
  5191. "Is the end of the current subtree visible?"
  5192. (pos-visible-in-window-p
  5193. (save-excursion (org-end-of-subtree t) (point))))
  5194. (defun org-first-headline-recenter (&optional N)
  5195. "Move cursor to the first headline and recenter the headline.
  5196. Optional argument N means, put the headline into the Nth line of the window."
  5197. (goto-char (point-min))
  5198. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5199. (beginning-of-line)
  5200. (recenter (prefix-numeric-value N))))
  5201. ;;; Org-goto
  5202. (defvar org-goto-window-configuration nil)
  5203. (defvar org-goto-marker nil)
  5204. (defvar org-goto-map
  5205. (let ((map (make-sparse-keymap)))
  5206. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5207. (while (setq cmd (pop cmds))
  5208. (substitute-key-definition cmd cmd map global-map)))
  5209. (suppress-keymap map)
  5210. (org-defkey map "\C-m" 'org-goto-ret)
  5211. (org-defkey map [(return)] 'org-goto-ret)
  5212. (org-defkey map [(left)] 'org-goto-left)
  5213. (org-defkey map [(right)] 'org-goto-right)
  5214. (org-defkey map [(control ?g)] 'org-goto-quit)
  5215. (org-defkey map "\C-i" 'org-cycle)
  5216. (org-defkey map [(tab)] 'org-cycle)
  5217. (org-defkey map [(down)] 'outline-next-visible-heading)
  5218. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5219. (if org-goto-auto-isearch
  5220. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5221. (org-defkey map "q" 'org-goto-quit)
  5222. (org-defkey map "n" 'outline-next-visible-heading)
  5223. (org-defkey map "p" 'outline-previous-visible-heading)
  5224. (org-defkey map "f" 'outline-forward-same-level)
  5225. (org-defkey map "b" 'outline-backward-same-level)
  5226. (org-defkey map "u" 'outline-up-heading))
  5227. (org-defkey map "/" 'org-occur)
  5228. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5229. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5230. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5231. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5232. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5233. map))
  5234. (defconst org-goto-help
  5235. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5236. RET=jump to location [Q]uit and return to previous location
  5237. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5238. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5239. (defun org-goto (&optional alternative-interface)
  5240. "Look up a different location in the current file, keeping current visibility.
  5241. When you want look-up or go to a different location in a document, the
  5242. fastest way is often to fold the entire buffer and then dive into the tree.
  5243. This method has the disadvantage, that the previous location will be folded,
  5244. which may not be what you want.
  5245. This command works around this by showing a copy of the current buffer
  5246. in an indirect buffer, in overview mode. You can dive into the tree in
  5247. that copy, use org-occur and incremental search to find a location.
  5248. When pressing RET or `Q', the command returns to the original buffer in
  5249. which the visibility is still unchanged. After RET is will also jump to
  5250. the location selected in the indirect buffer and expose the
  5251. the headline hierarchy above."
  5252. (interactive "P")
  5253. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5254. (org-refile-use-outline-path t)
  5255. (interface
  5256. (if (not alternative-interface)
  5257. org-goto-interface
  5258. (if (eq org-goto-interface 'outline)
  5259. 'outline-path-completion
  5260. 'outline)))
  5261. (org-goto-start-pos (point))
  5262. (selected-point
  5263. (if (eq interface 'outline)
  5264. (car (org-get-location (current-buffer) org-goto-help))
  5265. (nth 3 (org-refile-get-location "Goto: ")))))
  5266. (if selected-point
  5267. (progn
  5268. (org-mark-ring-push org-goto-start-pos)
  5269. (goto-char selected-point)
  5270. (if (or (org-invisible-p) (org-invisible-p2))
  5271. (org-show-context 'org-goto)))
  5272. (message "Quit"))))
  5273. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5274. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5275. (defun org-get-location (buf help)
  5276. "Let the user select a location in the Org-mode buffer BUF.
  5277. This function uses a recursive edit. It returns the selected position
  5278. or nil."
  5279. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5280. (isearch-hide-immediately nil)
  5281. (isearch-search-fun-function
  5282. (lambda () 'org-goto-local-search-forward-headings))
  5283. (org-goto-selected-point org-goto-exit-command))
  5284. (save-excursion
  5285. (save-window-excursion
  5286. (delete-other-windows)
  5287. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5288. (switch-to-buffer
  5289. (condition-case nil
  5290. (make-indirect-buffer (current-buffer) "*org-goto*")
  5291. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5292. (with-output-to-temp-buffer "*Help*"
  5293. (princ help))
  5294. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5295. (setq buffer-read-only nil)
  5296. (let ((org-startup-truncated t)
  5297. (org-startup-folded nil)
  5298. (org-startup-align-all-tables nil))
  5299. (org-mode)
  5300. (org-overview))
  5301. (setq buffer-read-only t)
  5302. (if (and (boundp 'org-goto-start-pos)
  5303. (integer-or-marker-p org-goto-start-pos))
  5304. (let ((org-show-hierarchy-above t)
  5305. (org-show-siblings t)
  5306. (org-show-following-heading t))
  5307. (goto-char org-goto-start-pos)
  5308. (and (org-invisible-p) (org-show-context)))
  5309. (goto-char (point-min)))
  5310. (org-beginning-of-line)
  5311. (message "Select location and press RET")
  5312. (use-local-map org-goto-map)
  5313. (recursive-edit)
  5314. ))
  5315. (kill-buffer "*org-goto*")
  5316. (cons org-goto-selected-point org-goto-exit-command)))
  5317. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5318. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5319. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5320. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5321. (defun org-goto-local-search-forward-headings (string bound noerror)
  5322. "Search and make sure that anu matches are in headlines."
  5323. (catch 'return
  5324. (while (search-forward string bound noerror)
  5325. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5326. (and (member :headline context)
  5327. (not (member :tags context))))
  5328. (throw 'return (point))))))
  5329. (defun org-goto-local-auto-isearch ()
  5330. "Start isearch."
  5331. (interactive)
  5332. (goto-char (point-min))
  5333. (let ((keys (this-command-keys)))
  5334. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5335. (isearch-mode t)
  5336. (isearch-process-search-char (string-to-char keys)))))
  5337. (defun org-goto-ret (&optional arg)
  5338. "Finish `org-goto' by going to the new location."
  5339. (interactive "P")
  5340. (setq org-goto-selected-point (point)
  5341. org-goto-exit-command 'return)
  5342. (throw 'exit nil))
  5343. (defun org-goto-left ()
  5344. "Finish `org-goto' by going to the new location."
  5345. (interactive)
  5346. (if (org-on-heading-p)
  5347. (progn
  5348. (beginning-of-line 1)
  5349. (setq org-goto-selected-point (point)
  5350. org-goto-exit-command 'left)
  5351. (throw 'exit nil))
  5352. (error "Not on a heading")))
  5353. (defun org-goto-right ()
  5354. "Finish `org-goto' by going to the new location."
  5355. (interactive)
  5356. (if (org-on-heading-p)
  5357. (progn
  5358. (setq org-goto-selected-point (point)
  5359. org-goto-exit-command 'right)
  5360. (throw 'exit nil))
  5361. (error "Not on a heading")))
  5362. (defun org-goto-quit ()
  5363. "Finish `org-goto' without cursor motion."
  5364. (interactive)
  5365. (setq org-goto-selected-point nil)
  5366. (setq org-goto-exit-command 'quit)
  5367. (throw 'exit nil))
  5368. ;;; Indirect buffer display of subtrees
  5369. (defvar org-indirect-dedicated-frame nil
  5370. "This is the frame being used for indirect tree display.")
  5371. (defvar org-last-indirect-buffer nil)
  5372. (defun org-tree-to-indirect-buffer (&optional arg)
  5373. "Create indirect buffer and narrow it to current subtree.
  5374. With numerical prefix ARG, go up to this level and then take that tree.
  5375. If ARG is negative, go up that many levels.
  5376. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5377. indirect buffer previously made with this command, to avoid proliferation of
  5378. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5379. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5380. is kept so that you can work with several indirect buffers at the same time.
  5381. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5382. requests that a new frame be made for the new buffer, so that the dedicated
  5383. frame is not changed."
  5384. (interactive "P")
  5385. (let ((cbuf (current-buffer))
  5386. (cwin (selected-window))
  5387. (pos (point))
  5388. beg end level heading ibuf)
  5389. (save-excursion
  5390. (org-back-to-heading t)
  5391. (when (numberp arg)
  5392. (setq level (org-outline-level))
  5393. (if (< arg 0) (setq arg (+ level arg)))
  5394. (while (> (setq level (org-outline-level)) arg)
  5395. (outline-up-heading 1 t)))
  5396. (setq beg (point)
  5397. heading (org-get-heading))
  5398. (org-end-of-subtree t) (setq end (point)))
  5399. (if (and (buffer-live-p org-last-indirect-buffer)
  5400. (not (eq org-indirect-buffer-display 'new-frame))
  5401. (not arg))
  5402. (kill-buffer org-last-indirect-buffer))
  5403. (setq ibuf (org-get-indirect-buffer cbuf)
  5404. org-last-indirect-buffer ibuf)
  5405. (cond
  5406. ((or (eq org-indirect-buffer-display 'new-frame)
  5407. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5408. (select-frame (make-frame))
  5409. (delete-other-windows)
  5410. (switch-to-buffer ibuf)
  5411. (org-set-frame-title heading))
  5412. ((eq org-indirect-buffer-display 'dedicated-frame)
  5413. (raise-frame
  5414. (select-frame (or (and org-indirect-dedicated-frame
  5415. (frame-live-p org-indirect-dedicated-frame)
  5416. org-indirect-dedicated-frame)
  5417. (setq org-indirect-dedicated-frame (make-frame)))))
  5418. (delete-other-windows)
  5419. (switch-to-buffer ibuf)
  5420. (org-set-frame-title (concat "Indirect: " heading)))
  5421. ((eq org-indirect-buffer-display 'current-window)
  5422. (switch-to-buffer ibuf))
  5423. ((eq org-indirect-buffer-display 'other-window)
  5424. (pop-to-buffer ibuf))
  5425. (t (error "Invalid value.")))
  5426. (if (featurep 'xemacs)
  5427. (save-excursion (org-mode) (turn-on-font-lock)))
  5428. (narrow-to-region beg end)
  5429. (show-all)
  5430. (goto-char pos)
  5431. (and (window-live-p cwin) (select-window cwin))))
  5432. (defun org-get-indirect-buffer (&optional buffer)
  5433. (setq buffer (or buffer (current-buffer)))
  5434. (let ((n 1) (base (buffer-name buffer)) bname)
  5435. (while (buffer-live-p
  5436. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5437. (setq n (1+ n)))
  5438. (condition-case nil
  5439. (make-indirect-buffer buffer bname 'clone)
  5440. (error (make-indirect-buffer buffer bname)))))
  5441. (defun org-set-frame-title (title)
  5442. "Set the title of the current frame to the string TITLE."
  5443. ;; FIXME: how to name a single frame in XEmacs???
  5444. (unless (featurep 'xemacs)
  5445. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5446. ;;;; Structure editing
  5447. ;;; Inserting headlines
  5448. (defun org-insert-heading (&optional force-heading)
  5449. "Insert a new heading or item with same depth at point.
  5450. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5451. If point is at the beginning of a headline, insert a sibling before the
  5452. current headline. If point is in the middle of a headline, split the headline
  5453. at that position and make the rest of the headline part of the sibling below
  5454. the current headline."
  5455. (interactive "P")
  5456. (if (= (buffer-size) 0)
  5457. (insert "\n* ")
  5458. (when (or force-heading (not (org-insert-item)))
  5459. (let* ((head (save-excursion
  5460. (condition-case nil
  5461. (progn
  5462. (org-back-to-heading)
  5463. (match-string 0))
  5464. (error "*"))))
  5465. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5466. pos)
  5467. (cond
  5468. ((and (org-on-heading-p) (bolp)
  5469. (or (bobp)
  5470. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5471. (open-line (if blank 2 1)))
  5472. ((and (bolp)
  5473. (or (bobp)
  5474. (save-excursion
  5475. (backward-char 1) (not (org-invisible-p)))))
  5476. nil)
  5477. (t (newline (if blank 2 1))))
  5478. (insert head) (just-one-space)
  5479. (setq pos (point))
  5480. (end-of-line 1)
  5481. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5482. (run-hooks 'org-insert-heading-hook)))))
  5483. (defun org-insert-heading-after-current ()
  5484. "Insert a new heading with same level as current, after current subtree."
  5485. (interactive)
  5486. (org-back-to-heading)
  5487. (org-insert-heading)
  5488. (org-move-subtree-down)
  5489. (end-of-line 1))
  5490. (defun org-insert-todo-heading (arg)
  5491. "Insert a new heading with the same level and TODO state as current heading.
  5492. If the heading has no TODO state, or if the state is DONE, use the first
  5493. state (TODO by default). Also with prefix arg, force first state."
  5494. (interactive "P")
  5495. (when (not (org-insert-item 'checkbox))
  5496. (org-insert-heading)
  5497. (save-excursion
  5498. (org-back-to-heading)
  5499. (outline-previous-heading)
  5500. (looking-at org-todo-line-regexp))
  5501. (if (or arg
  5502. (not (match-beginning 2))
  5503. (member (match-string 2) org-done-keywords))
  5504. (insert (car org-todo-keywords-1) " ")
  5505. (insert (match-string 2) " "))))
  5506. (defun org-insert-subheading (arg)
  5507. "Insert a new subheading and demote it.
  5508. Works for outline headings and for plain lists alike."
  5509. (interactive "P")
  5510. (org-insert-heading arg)
  5511. (cond
  5512. ((org-on-heading-p) (org-do-demote))
  5513. ((org-at-item-p) (org-indent-item 1))))
  5514. (defun org-insert-todo-subheading (arg)
  5515. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5516. Works for outline headings and for plain lists alike."
  5517. (interactive "P")
  5518. (org-insert-todo-heading arg)
  5519. (cond
  5520. ((org-on-heading-p) (org-do-demote))
  5521. ((org-at-item-p) (org-indent-item 1))))
  5522. ;;; Promotion and Demotion
  5523. (defun org-promote-subtree ()
  5524. "Promote the entire subtree.
  5525. See also `org-promote'."
  5526. (interactive)
  5527. (save-excursion
  5528. (org-map-tree 'org-promote))
  5529. (org-fix-position-after-promote))
  5530. (defun org-demote-subtree ()
  5531. "Demote the entire subtree. See `org-demote'.
  5532. See also `org-promote'."
  5533. (interactive)
  5534. (save-excursion
  5535. (org-map-tree 'org-demote))
  5536. (org-fix-position-after-promote))
  5537. (defun org-do-promote ()
  5538. "Promote the current heading higher up the tree.
  5539. If the region is active in `transient-mark-mode', promote all headings
  5540. in the region."
  5541. (interactive)
  5542. (save-excursion
  5543. (if (org-region-active-p)
  5544. (org-map-region 'org-promote (region-beginning) (region-end))
  5545. (org-promote)))
  5546. (org-fix-position-after-promote))
  5547. (defun org-do-demote ()
  5548. "Demote the current heading lower down the tree.
  5549. If the region is active in `transient-mark-mode', demote all headings
  5550. in the region."
  5551. (interactive)
  5552. (save-excursion
  5553. (if (org-region-active-p)
  5554. (org-map-region 'org-demote (region-beginning) (region-end))
  5555. (org-demote)))
  5556. (org-fix-position-after-promote))
  5557. (defun org-fix-position-after-promote ()
  5558. "Make sure that after pro/demotion cursor position is right."
  5559. (let ((pos (point)))
  5560. (when (save-excursion
  5561. (beginning-of-line 1)
  5562. (looking-at org-todo-line-regexp)
  5563. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5564. (cond ((eobp) (insert " "))
  5565. ((eolp) (insert " "))
  5566. ((equal (char-after) ?\ ) (forward-char 1))))))
  5567. (defun org-reduced-level (l)
  5568. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5569. (defun org-get-legal-level (level &optional change)
  5570. "Rectify a level change under the influence of `org-odd-levels-only'
  5571. LEVEL is a current level, CHANGE is by how much the level should be
  5572. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5573. even level numbers will become the next higher odd number."
  5574. (if org-odd-levels-only
  5575. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5576. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5577. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5578. (max 1 (+ level change))))
  5579. (defun org-promote ()
  5580. "Promote the current heading higher up the tree.
  5581. If the region is active in `transient-mark-mode', promote all headings
  5582. in the region."
  5583. (org-back-to-heading t)
  5584. (let* ((level (save-match-data (funcall outline-level)))
  5585. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5586. (diff (abs (- level (length up-head) -1))))
  5587. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5588. (replace-match up-head nil t)
  5589. ;; Fixup tag positioning
  5590. (and org-auto-align-tags (org-set-tags nil t))
  5591. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5592. (defun org-demote ()
  5593. "Demote the current heading lower down the tree.
  5594. If the region is active in `transient-mark-mode', demote all headings
  5595. in the region."
  5596. (org-back-to-heading t)
  5597. (let* ((level (save-match-data (funcall outline-level)))
  5598. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5599. (diff (abs (- level (length down-head) -1))))
  5600. (replace-match down-head nil t)
  5601. ;; Fixup tag positioning
  5602. (and org-auto-align-tags (org-set-tags nil t))
  5603. (if org-adapt-indentation (org-fixup-indentation diff))))
  5604. (defun org-map-tree (fun)
  5605. "Call FUN for every heading underneath the current one."
  5606. (org-back-to-heading)
  5607. (let ((level (funcall outline-level)))
  5608. (save-excursion
  5609. (funcall fun)
  5610. (while (and (progn
  5611. (outline-next-heading)
  5612. (> (funcall outline-level) level))
  5613. (not (eobp)))
  5614. (funcall fun)))))
  5615. (defun org-map-region (fun beg end)
  5616. "Call FUN for every heading between BEG and END."
  5617. (let ((org-ignore-region t))
  5618. (save-excursion
  5619. (setq end (copy-marker end))
  5620. (goto-char beg)
  5621. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5622. (< (point) end))
  5623. (funcall fun))
  5624. (while (and (progn
  5625. (outline-next-heading)
  5626. (< (point) end))
  5627. (not (eobp)))
  5628. (funcall fun)))))
  5629. (defun org-fixup-indentation (diff)
  5630. "Change the indentation in the current entry by DIFF
  5631. However, if any line in the current entry has no indentation, or if it
  5632. would end up with no indentation after the change, nothing at all is done."
  5633. (save-excursion
  5634. (let ((end (save-excursion (outline-next-heading)
  5635. (point-marker)))
  5636. (prohibit (if (> diff 0)
  5637. "^\\S-"
  5638. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5639. col)
  5640. (unless (save-excursion (end-of-line 1)
  5641. (re-search-forward prohibit end t))
  5642. (while (and (< (point) end)
  5643. (re-search-forward "^[ \t]+" end t))
  5644. (goto-char (match-end 0))
  5645. (setq col (current-column))
  5646. (if (< diff 0) (replace-match ""))
  5647. (indent-to (+ diff col))))
  5648. (move-marker end nil))))
  5649. (defun org-convert-to-odd-levels ()
  5650. "Convert an org-mode file with all levels allowed to one with odd levels.
  5651. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5652. level 5 etc."
  5653. (interactive)
  5654. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5655. (let ((org-odd-levels-only nil) n)
  5656. (save-excursion
  5657. (goto-char (point-min))
  5658. (while (re-search-forward "^\\*\\*+ " nil t)
  5659. (setq n (- (length (match-string 0)) 2))
  5660. (while (>= (setq n (1- n)) 0)
  5661. (org-demote))
  5662. (end-of-line 1))))))
  5663. (defun org-convert-to-oddeven-levels ()
  5664. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5665. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5666. section with an even level, conversion would destroy the structure of the file. An error
  5667. is signaled in this case."
  5668. (interactive)
  5669. (goto-char (point-min))
  5670. ;; First check if there are no even levels
  5671. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5672. (org-show-context t)
  5673. (error "Not all levels are odd in this file. Conversion not possible."))
  5674. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5675. (let ((org-odd-levels-only nil) n)
  5676. (save-excursion
  5677. (goto-char (point-min))
  5678. (while (re-search-forward "^\\*\\*+ " nil t)
  5679. (setq n (/ (1- (length (match-string 0))) 2))
  5680. (while (>= (setq n (1- n)) 0)
  5681. (org-promote))
  5682. (end-of-line 1))))))
  5683. (defun org-tr-level (n)
  5684. "Make N odd if required."
  5685. (if org-odd-levels-only (1+ (/ n 2)) n))
  5686. ;;; Vertical tree motion, cutting and pasting of subtrees
  5687. (defun org-move-subtree-up (&optional arg)
  5688. "Move the current subtree up past ARG headlines of the same level."
  5689. (interactive "p")
  5690. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5691. (defun org-move-subtree-down (&optional arg)
  5692. "Move the current subtree down past ARG headlines of the same level."
  5693. (interactive "p")
  5694. (setq arg (prefix-numeric-value arg))
  5695. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5696. 'outline-get-last-sibling))
  5697. (ins-point (make-marker))
  5698. (cnt (abs arg))
  5699. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5700. ;; Select the tree
  5701. (org-back-to-heading)
  5702. (setq beg0 (point))
  5703. (save-excursion
  5704. (setq ne-beg (org-back-over-empty-lines))
  5705. (setq beg (point)))
  5706. (save-match-data
  5707. (save-excursion (outline-end-of-heading)
  5708. (setq folded (org-invisible-p)))
  5709. (outline-end-of-subtree))
  5710. (outline-next-heading)
  5711. (setq ne-end (org-back-over-empty-lines))
  5712. (setq end (point))
  5713. (goto-char beg0)
  5714. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5715. ;; include less whitespace
  5716. (save-excursion
  5717. (goto-char beg)
  5718. (forward-line (- ne-beg ne-end))
  5719. (setq beg (point))))
  5720. ;; Find insertion point, with error handling
  5721. (while (> cnt 0)
  5722. (or (and (funcall movfunc) (looking-at outline-regexp))
  5723. (progn (goto-char beg0)
  5724. (error "Cannot move past superior level or buffer limit")))
  5725. (setq cnt (1- cnt)))
  5726. (if (> arg 0)
  5727. ;; Moving forward - still need to move over subtree
  5728. (progn (org-end-of-subtree t t)
  5729. (save-excursion
  5730. (org-back-over-empty-lines)
  5731. (or (bolp) (newline)))))
  5732. (setq ne-ins (org-back-over-empty-lines))
  5733. (move-marker ins-point (point))
  5734. (setq txt (buffer-substring beg end))
  5735. (delete-region beg end)
  5736. (outline-flag-region (1- beg) beg nil)
  5737. (outline-flag-region (1- (point)) (point) nil)
  5738. (insert txt)
  5739. (or (bolp) (insert "\n"))
  5740. (setq ins-end (point))
  5741. (goto-char ins-point)
  5742. (org-skip-whitespace)
  5743. (when (and (< arg 0)
  5744. (org-first-sibling-p)
  5745. (> ne-ins ne-beg))
  5746. ;; Move whitespace back to beginning
  5747. (save-excursion
  5748. (goto-char ins-end)
  5749. (let ((kill-whole-line t))
  5750. (kill-line (- ne-ins ne-beg)) (point)))
  5751. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5752. (move-marker ins-point nil)
  5753. (org-compact-display-after-subtree-move)
  5754. (unless folded
  5755. (org-show-entry)
  5756. (show-children)
  5757. (org-cycle-hide-drawers 'children))))
  5758. (defvar org-subtree-clip ""
  5759. "Clipboard for cut and paste of subtrees.
  5760. This is actually only a copy of the kill, because we use the normal kill
  5761. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5762. (defvar org-subtree-clip-folded nil
  5763. "Was the last copied subtree folded?
  5764. This is used to fold the tree back after pasting.")
  5765. (defun org-cut-subtree (&optional n)
  5766. "Cut the current subtree into the clipboard.
  5767. With prefix arg N, cut this many sequential subtrees.
  5768. This is a short-hand for marking the subtree and then cutting it."
  5769. (interactive "p")
  5770. (org-copy-subtree n 'cut))
  5771. (defun org-copy-subtree (&optional n cut)
  5772. "Cut the current subtree into the clipboard.
  5773. With prefix arg N, cut this many sequential subtrees.
  5774. This is a short-hand for marking the subtree and then copying it.
  5775. If CUT is non-nil, actually cut the subtree."
  5776. (interactive "p")
  5777. (let (beg end folded (beg0 (point)))
  5778. (if (interactive-p)
  5779. (org-back-to-heading nil) ; take what looks like a subtree
  5780. (org-back-to-heading t)) ; take what is really there
  5781. (org-back-over-empty-lines)
  5782. (setq beg (point))
  5783. (skip-chars-forward " \t\r\n")
  5784. (save-match-data
  5785. (save-excursion (outline-end-of-heading)
  5786. (setq folded (org-invisible-p)))
  5787. (condition-case nil
  5788. (outline-forward-same-level (1- n))
  5789. (error nil))
  5790. (org-end-of-subtree t t))
  5791. (org-back-over-empty-lines)
  5792. (setq end (point))
  5793. (goto-char beg0)
  5794. (when (> end beg)
  5795. (setq org-subtree-clip-folded folded)
  5796. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5797. (setq org-subtree-clip (current-kill 0))
  5798. (message "%s: Subtree(s) with %d characters"
  5799. (if cut "Cut" "Copied")
  5800. (length org-subtree-clip)))))
  5801. (defun org-paste-subtree (&optional level tree)
  5802. "Paste the clipboard as a subtree, with modification of headline level.
  5803. The entire subtree is promoted or demoted in order to match a new headline
  5804. level. By default, the new level is derived from the visible headings
  5805. before and after the insertion point, and taken to be the inferior headline
  5806. level of the two. So if the previous visible heading is level 3 and the
  5807. next is level 4 (or vice versa), level 4 will be used for insertion.
  5808. This makes sure that the subtree remains an independent subtree and does
  5809. not swallow low level entries.
  5810. You can also force a different level, either by using a numeric prefix
  5811. argument, or by inserting the heading marker by hand. For example, if the
  5812. cursor is after \"*****\", then the tree will be shifted to level 5.
  5813. If you want to insert the tree as is, just use \\[yank].
  5814. If optional TREE is given, use this text instead of the kill ring."
  5815. (interactive "P")
  5816. (unless (org-kill-is-subtree-p tree)
  5817. (error "%s"
  5818. (substitute-command-keys
  5819. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5820. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5821. (^re (concat "^\\(" outline-regexp "\\)"))
  5822. (re (concat "\\(" outline-regexp "\\)"))
  5823. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5824. (old-level (if (string-match ^re txt)
  5825. (- (match-end 0) (match-beginning 0) 1)
  5826. -1))
  5827. (force-level (cond (level (prefix-numeric-value level))
  5828. ((string-match
  5829. ^re_ (buffer-substring (point-at-bol) (point)))
  5830. (- (match-end 1) (match-beginning 1)))
  5831. (t nil)))
  5832. (previous-level (save-excursion
  5833. (condition-case nil
  5834. (progn
  5835. (outline-previous-visible-heading 1)
  5836. (if (looking-at re)
  5837. (- (match-end 0) (match-beginning 0) 1)
  5838. 1))
  5839. (error 1))))
  5840. (next-level (save-excursion
  5841. (condition-case nil
  5842. (progn
  5843. (or (looking-at outline-regexp)
  5844. (outline-next-visible-heading 1))
  5845. (if (looking-at re)
  5846. (- (match-end 0) (match-beginning 0) 1)
  5847. 1))
  5848. (error 1))))
  5849. (new-level (or force-level (max previous-level next-level)))
  5850. (shift (if (or (= old-level -1)
  5851. (= new-level -1)
  5852. (= old-level new-level))
  5853. 0
  5854. (- new-level old-level)))
  5855. (delta (if (> shift 0) -1 1))
  5856. (func (if (> shift 0) 'org-demote 'org-promote))
  5857. (org-odd-levels-only nil)
  5858. beg end)
  5859. ;; Remove the forced level indicator
  5860. (if force-level
  5861. (delete-region (point-at-bol) (point)))
  5862. ;; Paste
  5863. (beginning-of-line 1)
  5864. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5865. (setq beg (point))
  5866. (insert-before-markers txt) ;; FIXME: correct fix????
  5867. (unless (string-match "\n\\'" txt) (insert "\n"))
  5868. (setq end (point))
  5869. (goto-char beg)
  5870. (skip-chars-forward " \t\n\r")
  5871. (setq beg (point))
  5872. ;; Shift if necessary
  5873. (unless (= shift 0)
  5874. (save-restriction
  5875. (narrow-to-region beg end)
  5876. (while (not (= shift 0))
  5877. (org-map-region func (point-min) (point-max))
  5878. (setq shift (+ delta shift)))
  5879. (goto-char (point-min))))
  5880. (when (interactive-p)
  5881. (message "Clipboard pasted as level %d subtree" new-level))
  5882. (if (and kill-ring
  5883. (eq org-subtree-clip (current-kill 0))
  5884. org-subtree-clip-folded)
  5885. ;; The tree was folded before it was killed/copied
  5886. (hide-subtree))))
  5887. (defun org-kill-is-subtree-p (&optional txt)
  5888. "Check if the current kill is an outline subtree, or a set of trees.
  5889. Returns nil if kill does not start with a headline, or if the first
  5890. headline level is not the largest headline level in the tree.
  5891. So this will actually accept several entries of equal levels as well,
  5892. which is OK for `org-paste-subtree'.
  5893. If optional TXT is given, check this string instead of the current kill."
  5894. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5895. (start-level (and kill
  5896. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5897. org-outline-regexp "\\)")
  5898. kill)
  5899. (- (match-end 2) (match-beginning 2) 1)))
  5900. (re (concat "^" org-outline-regexp))
  5901. (start (1+ (match-beginning 2))))
  5902. (if (not start-level)
  5903. (progn
  5904. nil) ;; does not even start with a heading
  5905. (catch 'exit
  5906. (while (setq start (string-match re kill (1+ start)))
  5907. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5908. (throw 'exit nil)))
  5909. t))))
  5910. (defun org-narrow-to-subtree ()
  5911. "Narrow buffer to the current subtree."
  5912. (interactive)
  5913. (save-excursion
  5914. (narrow-to-region
  5915. (progn (org-back-to-heading) (point))
  5916. (progn (org-end-of-subtree t t) (point)))))
  5917. ;;; Outline Sorting
  5918. (defun org-sort (with-case)
  5919. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5920. Optional argument WITH-CASE means sort case-sensitively."
  5921. (interactive "P")
  5922. (if (org-at-table-p)
  5923. (org-call-with-arg 'org-table-sort-lines with-case)
  5924. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5925. (defvar org-priority-regexp) ; defined later in the file
  5926. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5927. "Sort entries on a certain level of an outline tree.
  5928. If there is an active region, the entries in the region are sorted.
  5929. Else, if the cursor is before the first entry, sort the top-level items.
  5930. Else, the children of the entry at point are sorted.
  5931. Sorting can be alphabetically, numerically, and by date/time as given by
  5932. the first time stamp in the entry. The command prompts for the sorting
  5933. type unless it has been given to the function through the SORTING-TYPE
  5934. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5935. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5936. called with point at the beginning of the record. It must return either
  5937. a string or a number that should serve as the sorting key for that record.
  5938. Comparing entries ignores case by default. However, with an optional argument
  5939. WITH-CASE, the sorting considers case as well."
  5940. (interactive "P")
  5941. (let ((case-func (if with-case 'identity 'downcase))
  5942. start beg end stars re re2
  5943. txt what tmp plain-list-p)
  5944. ;; Find beginning and end of region to sort
  5945. (cond
  5946. ((org-region-active-p)
  5947. ;; we will sort the region
  5948. (setq end (region-end)
  5949. what "region")
  5950. (goto-char (region-beginning))
  5951. (if (not (org-on-heading-p)) (outline-next-heading))
  5952. (setq start (point)))
  5953. ((org-at-item-p)
  5954. ;; we will sort this plain list
  5955. (org-beginning-of-item-list) (setq start (point))
  5956. (org-end-of-item-list) (setq end (point))
  5957. (goto-char start)
  5958. (setq plain-list-p t
  5959. what "plain list"))
  5960. ((or (org-on-heading-p)
  5961. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5962. ;; we will sort the children of the current headline
  5963. (org-back-to-heading)
  5964. (setq start (point)
  5965. end (progn (org-end-of-subtree t t)
  5966. (org-back-over-empty-lines)
  5967. (point))
  5968. what "children")
  5969. (goto-char start)
  5970. (show-subtree)
  5971. (outline-next-heading))
  5972. (t
  5973. ;; we will sort the top-level entries in this file
  5974. (goto-char (point-min))
  5975. (or (org-on-heading-p) (outline-next-heading))
  5976. (setq start (point) end (point-max) what "top-level")
  5977. (goto-char start)
  5978. (show-all)))
  5979. (setq beg (point))
  5980. (if (>= beg end) (error "Nothing to sort"))
  5981. (unless plain-list-p
  5982. (looking-at "\\(\\*+\\)")
  5983. (setq stars (match-string 1)
  5984. re (concat "^" (regexp-quote stars) " +")
  5985. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5986. txt (buffer-substring beg end))
  5987. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5988. (if (and (not (equal stars "*")) (string-match re2 txt))
  5989. (error "Region to sort contains a level above the first entry")))
  5990. (unless sorting-type
  5991. (message
  5992. (if plain-list-p
  5993. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5994. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  5995. what)
  5996. (setq sorting-type (read-char-exclusive))
  5997. (and (= (downcase sorting-type) ?f)
  5998. (setq getkey-func
  5999. (completing-read "Sort using function: "
  6000. obarray 'fboundp t nil nil))
  6001. (setq getkey-func (intern getkey-func)))
  6002. (and (= (downcase sorting-type) ?r)
  6003. (setq property
  6004. (completing-read "Property: "
  6005. (mapcar 'list (org-buffer-property-keys t))
  6006. nil t))))
  6007. (message "Sorting entries...")
  6008. (save-restriction
  6009. (narrow-to-region start end)
  6010. (let ((dcst (downcase sorting-type))
  6011. (now (current-time)))
  6012. (sort-subr
  6013. (/= dcst sorting-type)
  6014. ;; This function moves to the beginning character of the "record" to
  6015. ;; be sorted.
  6016. (if plain-list-p
  6017. (lambda nil
  6018. (if (org-at-item-p) t (goto-char (point-max))))
  6019. (lambda nil
  6020. (if (re-search-forward re nil t)
  6021. (goto-char (match-beginning 0))
  6022. (goto-char (point-max)))))
  6023. ;; This function moves to the last character of the "record" being
  6024. ;; sorted.
  6025. (if plain-list-p
  6026. 'org-end-of-item
  6027. (lambda nil
  6028. (save-match-data
  6029. (condition-case nil
  6030. (outline-forward-same-level 1)
  6031. (error
  6032. (goto-char (point-max)))))))
  6033. ;; This function returns the value that gets sorted against.
  6034. (if plain-list-p
  6035. (lambda nil
  6036. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6037. (cond
  6038. ((= dcst ?n)
  6039. (string-to-number (buffer-substring (match-end 0)
  6040. (point-at-eol))))
  6041. ((= dcst ?a)
  6042. (buffer-substring (match-end 0) (point-at-eol)))
  6043. ((= dcst ?t)
  6044. (if (re-search-forward org-ts-regexp
  6045. (point-at-eol) t)
  6046. (org-time-string-to-time (match-string 0))
  6047. now))
  6048. ((= dcst ?f)
  6049. (if getkey-func
  6050. (progn
  6051. (setq tmp (funcall getkey-func))
  6052. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6053. tmp)
  6054. (error "Invalid key function `%s'" getkey-func)))
  6055. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6056. (lambda nil
  6057. (cond
  6058. ((= dcst ?n)
  6059. (if (looking-at outline-regexp)
  6060. (string-to-number (buffer-substring (match-end 0)
  6061. (point-at-eol)))
  6062. nil))
  6063. ((= dcst ?a)
  6064. (funcall case-func (buffer-substring (point-at-bol)
  6065. (point-at-eol))))
  6066. ((= dcst ?t)
  6067. (if (re-search-forward org-ts-regexp
  6068. (save-excursion
  6069. (forward-line 2)
  6070. (point)) t)
  6071. (org-time-string-to-time (match-string 0))
  6072. now))
  6073. ((= dcst ?p)
  6074. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6075. (string-to-char (match-string 2))
  6076. org-default-priority))
  6077. ((= dcst ?r)
  6078. (or (org-entry-get nil property) ""))
  6079. ((= dcst ?f)
  6080. (if getkey-func
  6081. (progn
  6082. (setq tmp (funcall getkey-func))
  6083. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6084. tmp)
  6085. (error "Invalid key function `%s'" getkey-func)))
  6086. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6087. nil
  6088. (cond
  6089. ((= dcst ?a) 'string<)
  6090. ((= dcst ?t) 'time-less-p)
  6091. (t nil)))))
  6092. (message "Sorting entries...done")))
  6093. (defun org-do-sort (table what &optional with-case sorting-type)
  6094. "Sort TABLE of WHAT according to SORTING-TYPE.
  6095. The user will be prompted for the SORTING-TYPE if the call to this
  6096. function does not specify it. WHAT is only for the prompt, to indicate
  6097. what is being sorted. The sorting key will be extracted from
  6098. the car of the elements of the table.
  6099. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6100. (unless sorting-type
  6101. (message
  6102. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6103. what)
  6104. (setq sorting-type (read-char-exclusive)))
  6105. (let ((dcst (downcase sorting-type))
  6106. extractfun comparefun)
  6107. ;; Define the appropriate functions
  6108. (cond
  6109. ((= dcst ?n)
  6110. (setq extractfun 'string-to-number
  6111. comparefun (if (= dcst sorting-type) '< '>)))
  6112. ((= dcst ?a)
  6113. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6114. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6115. comparefun (if (= dcst sorting-type)
  6116. 'string<
  6117. (lambda (a b) (and (not (string< a b))
  6118. (not (string= a b)))))))
  6119. ((= dcst ?t)
  6120. (setq extractfun
  6121. (lambda (x)
  6122. (if (string-match org-ts-regexp x)
  6123. (time-to-seconds
  6124. (org-time-string-to-time (match-string 0 x)))
  6125. 0))
  6126. comparefun (if (= dcst sorting-type) '< '>)))
  6127. (t (error "Invalid sorting type `%c'" sorting-type)))
  6128. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6129. table)
  6130. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6131. ;;;; Plain list items, including checkboxes
  6132. ;;; Plain list items
  6133. (defun org-at-item-p ()
  6134. "Is point in a line starting a hand-formatted item?"
  6135. (let ((llt org-plain-list-ordered-item-terminator))
  6136. (save-excursion
  6137. (goto-char (point-at-bol))
  6138. (looking-at
  6139. (cond
  6140. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6141. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6142. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6143. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6144. (defun org-in-item-p ()
  6145. "It the cursor inside a plain list item.
  6146. Does not have to be the first line."
  6147. (save-excursion
  6148. (condition-case nil
  6149. (progn
  6150. (org-beginning-of-item)
  6151. (org-at-item-p)
  6152. t)
  6153. (error nil))))
  6154. (defun org-insert-item (&optional checkbox)
  6155. "Insert a new item at the current level.
  6156. Return t when things worked, nil when we are not in an item."
  6157. (when (save-excursion
  6158. (condition-case nil
  6159. (progn
  6160. (org-beginning-of-item)
  6161. (org-at-item-p)
  6162. (if (org-invisible-p) (error "Invisible item"))
  6163. t)
  6164. (error nil)))
  6165. (let* ((bul (match-string 0))
  6166. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6167. (match-end 0)))
  6168. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6169. pos)
  6170. (cond
  6171. ((and (org-at-item-p) (<= (point) eow))
  6172. ;; before the bullet
  6173. (beginning-of-line 1)
  6174. (open-line (if blank 2 1)))
  6175. ((<= (point) eow)
  6176. (beginning-of-line 1))
  6177. (t (newline (if blank 2 1))))
  6178. (insert bul (if checkbox "[ ]" ""))
  6179. (just-one-space)
  6180. (setq pos (point))
  6181. (end-of-line 1)
  6182. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6183. (org-maybe-renumber-ordered-list)
  6184. (and checkbox (org-update-checkbox-count-maybe))
  6185. t))
  6186. ;;; Checkboxes
  6187. (defun org-at-item-checkbox-p ()
  6188. "Is point at a line starting a plain-list item with a checklet?"
  6189. (and (org-at-item-p)
  6190. (save-excursion
  6191. (goto-char (match-end 0))
  6192. (skip-chars-forward " \t")
  6193. (looking-at "\\[[- X]\\]"))))
  6194. (defun org-toggle-checkbox (&optional arg)
  6195. "Toggle the checkbox in the current line."
  6196. (interactive "P")
  6197. (catch 'exit
  6198. (let (beg end status (firstnew 'unknown))
  6199. (cond
  6200. ((org-region-active-p)
  6201. (setq beg (region-beginning) end (region-end)))
  6202. ((org-on-heading-p)
  6203. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6204. ((org-at-item-checkbox-p)
  6205. (let ((pos (point)))
  6206. (replace-match
  6207. (cond (arg "[-]")
  6208. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6209. (t "[ ]"))
  6210. t t)
  6211. (goto-char pos))
  6212. (throw 'exit t))
  6213. (t (error "Not at a checkbox or heading, and no active region")))
  6214. (save-excursion
  6215. (goto-char beg)
  6216. (while (< (point) end)
  6217. (when (org-at-item-checkbox-p)
  6218. (setq status (equal (match-string 0) "[X]"))
  6219. (when (eq firstnew 'unknown)
  6220. (setq firstnew (not status)))
  6221. (replace-match
  6222. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6223. (beginning-of-line 2)))))
  6224. (org-update-checkbox-count-maybe))
  6225. (defun org-update-checkbox-count-maybe ()
  6226. "Update checkbox statistics unless turned off by user."
  6227. (when org-provide-checkbox-statistics
  6228. (org-update-checkbox-count)))
  6229. (defun org-update-checkbox-count (&optional all)
  6230. "Update the checkbox statistics in the current section.
  6231. This will find all statistic cookies like [57%] and [6/12] and update them
  6232. with the current numbers. With optional prefix argument ALL, do this for
  6233. the whole buffer."
  6234. (interactive "P")
  6235. (save-excursion
  6236. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6237. (beg (condition-case nil
  6238. (progn (outline-back-to-heading) (point))
  6239. (error (point-min))))
  6240. (end (move-marker (make-marker)
  6241. (progn (outline-next-heading) (point))))
  6242. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  6243. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6244. b1 e1 f1 c-on c-off lim (cstat 0))
  6245. (when all
  6246. (goto-char (point-min))
  6247. (outline-next-heading)
  6248. (setq beg (point) end (point-max)))
  6249. (goto-char beg)
  6250. (while (re-search-forward re end t)
  6251. (setq cstat (1+ cstat)
  6252. b1 (match-beginning 0)
  6253. e1 (match-end 0)
  6254. f1 (match-beginning 1)
  6255. lim (cond
  6256. ((org-on-heading-p) (outline-next-heading) (point))
  6257. ((org-at-item-p) (org-end-of-item) (point))
  6258. (t nil))
  6259. c-on 0 c-off 0)
  6260. (goto-char e1)
  6261. (when lim
  6262. (while (re-search-forward re-box lim t)
  6263. (if (member (match-string 2) '("[ ]" "[-]"))
  6264. (setq c-off (1+ c-off))
  6265. (setq c-on (1+ c-on))))
  6266. ; (delete-region b1 e1)
  6267. (goto-char b1)
  6268. (insert (if f1
  6269. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6270. (format "[%d/%d]" c-on (+ c-on c-off))))
  6271. (and (looking-at "\\[.*?\\]")
  6272. (replace-match ""))))
  6273. (when (interactive-p)
  6274. (message "Checkbox satistics updated %s (%d places)"
  6275. (if all "in entire file" "in current outline entry") cstat)))))
  6276. (defun org-get-checkbox-statistics-face ()
  6277. "Select the face for checkbox statistics.
  6278. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6279. it will be `org-todo'."
  6280. (if (match-end 1)
  6281. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6282. (if (and (> (match-end 2) (match-beginning 2))
  6283. (equal (match-string 2) (match-string 3)))
  6284. 'org-done
  6285. 'org-todo)))
  6286. (defun org-get-indentation (&optional line)
  6287. "Get the indentation of the current line, interpreting tabs.
  6288. When LINE is given, assume it represents a line and compute its indentation."
  6289. (if line
  6290. (if (string-match "^ *" (org-remove-tabs line))
  6291. (match-end 0))
  6292. (save-excursion
  6293. (beginning-of-line 1)
  6294. (skip-chars-forward " \t")
  6295. (current-column))))
  6296. (defun org-remove-tabs (s &optional width)
  6297. "Replace tabulators in S with spaces.
  6298. Assumes that s is a single line, starting in column 0."
  6299. (setq width (or width tab-width))
  6300. (while (string-match "\t" s)
  6301. (setq s (replace-match
  6302. (make-string
  6303. (- (* width (/ (+ (match-beginning 0) width) width))
  6304. (match-beginning 0)) ?\ )
  6305. t t s)))
  6306. s)
  6307. (defun org-fix-indentation (line ind)
  6308. "Fix indentation in LINE.
  6309. IND is a cons cell with target and minimum indentation.
  6310. If the current indenation in LINE is smaller than the minimum,
  6311. leave it alone. If it is larger than ind, set it to the target."
  6312. (let* ((l (org-remove-tabs line))
  6313. (i (org-get-indentation l))
  6314. (i1 (car ind)) (i2 (cdr ind)))
  6315. (if (>= i i2) (setq l (substring line i2)))
  6316. (if (> i1 0)
  6317. (concat (make-string i1 ?\ ) l)
  6318. l)))
  6319. (defcustom org-empty-line-terminates-plain-lists nil
  6320. "Non-nil means, an empty line ends all plain list levels.
  6321. When nil, empty lines are part of the preceeding item."
  6322. :group 'org-plain-lists
  6323. :type 'boolean)
  6324. (defun org-beginning-of-item ()
  6325. "Go to the beginning of the current hand-formatted item.
  6326. If the cursor is not in an item, throw an error."
  6327. (interactive)
  6328. (let ((pos (point))
  6329. (limit (save-excursion
  6330. (condition-case nil
  6331. (progn
  6332. (org-back-to-heading)
  6333. (beginning-of-line 2) (point))
  6334. (error (point-min)))))
  6335. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6336. ind ind1)
  6337. (if (org-at-item-p)
  6338. (beginning-of-line 1)
  6339. (beginning-of-line 1)
  6340. (skip-chars-forward " \t")
  6341. (setq ind (current-column))
  6342. (if (catch 'exit
  6343. (while t
  6344. (beginning-of-line 0)
  6345. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6346. (if (looking-at "[ \t]*$")
  6347. (setq ind1 ind-empty)
  6348. (skip-chars-forward " \t")
  6349. (setq ind1 (current-column)))
  6350. (if (< ind1 ind)
  6351. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6352. nil
  6353. (goto-char pos)
  6354. (error "Not in an item")))))
  6355. (defun org-end-of-item ()
  6356. "Go to the end of the current hand-formatted item.
  6357. If the cursor is not in an item, throw an error."
  6358. (interactive)
  6359. (let* ((pos (point))
  6360. ind1
  6361. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6362. (limit (save-excursion (outline-next-heading) (point)))
  6363. (ind (save-excursion
  6364. (org-beginning-of-item)
  6365. (skip-chars-forward " \t")
  6366. (current-column)))
  6367. (end (catch 'exit
  6368. (while t
  6369. (beginning-of-line 2)
  6370. (if (eobp) (throw 'exit (point)))
  6371. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6372. (if (looking-at "[ \t]*$")
  6373. (setq ind1 ind-empty)
  6374. (skip-chars-forward " \t")
  6375. (setq ind1 (current-column)))
  6376. (if (<= ind1 ind)
  6377. (throw 'exit (point-at-bol)))))))
  6378. (if end
  6379. (goto-char end)
  6380. (goto-char pos)
  6381. (error "Not in an item"))))
  6382. (defun org-next-item ()
  6383. "Move to the beginning of the next item in the current plain list.
  6384. Error if not at a plain list, or if this is the last item in the list."
  6385. (interactive)
  6386. (let (ind ind1 (pos (point)))
  6387. (org-beginning-of-item)
  6388. (setq ind (org-get-indentation))
  6389. (org-end-of-item)
  6390. (setq ind1 (org-get-indentation))
  6391. (unless (and (org-at-item-p) (= ind ind1))
  6392. (goto-char pos)
  6393. (error "On last item"))))
  6394. (defun org-previous-item ()
  6395. "Move to the beginning of the previous item in the current plain list.
  6396. Error if not at a plain list, or if this is the first item in the list."
  6397. (interactive)
  6398. (let (beg ind ind1 (pos (point)))
  6399. (org-beginning-of-item)
  6400. (setq beg (point))
  6401. (setq ind (org-get-indentation))
  6402. (goto-char beg)
  6403. (catch 'exit
  6404. (while t
  6405. (beginning-of-line 0)
  6406. (if (looking-at "[ \t]*$")
  6407. nil
  6408. (if (<= (setq ind1 (org-get-indentation)) ind)
  6409. (throw 'exit t)))))
  6410. (condition-case nil
  6411. (if (or (not (org-at-item-p))
  6412. (< ind1 (1- ind)))
  6413. (error "")
  6414. (org-beginning-of-item))
  6415. (error (goto-char pos)
  6416. (error "On first item")))))
  6417. (defun org-first-list-item-p ()
  6418. "Is this heading the item in a plain list?"
  6419. (unless (org-at-item-p)
  6420. (error "Not at a plain list item"))
  6421. (org-beginning-of-item)
  6422. (= (point) (save-excursion (org-beginning-of-item-list))))
  6423. (defun org-move-item-down ()
  6424. "Move the plain list item at point down, i.e. swap with following item.
  6425. Subitems (items with larger indentation) are considered part of the item,
  6426. so this really moves item trees."
  6427. (interactive)
  6428. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6429. (org-beginning-of-item)
  6430. (setq beg0 (point))
  6431. (save-excursion
  6432. (setq ne-beg (org-back-over-empty-lines))
  6433. (setq beg (point)))
  6434. (goto-char beg0)
  6435. (setq ind (org-get-indentation))
  6436. (org-end-of-item)
  6437. (setq end0 (point))
  6438. (setq ind1 (org-get-indentation))
  6439. (setq ne-end (org-back-over-empty-lines))
  6440. (setq end (point))
  6441. (goto-char beg0)
  6442. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6443. ;; include less whitespace
  6444. (save-excursion
  6445. (goto-char beg)
  6446. (forward-line (- ne-beg ne-end))
  6447. (setq beg (point))))
  6448. (goto-char end0)
  6449. (if (and (org-at-item-p) (= ind ind1))
  6450. (progn
  6451. (org-end-of-item)
  6452. (org-back-over-empty-lines)
  6453. (setq txt (buffer-substring beg end))
  6454. (save-excursion
  6455. (delete-region beg end))
  6456. (setq pos (point))
  6457. (insert txt)
  6458. (goto-char pos) (org-skip-whitespace)
  6459. (org-maybe-renumber-ordered-list))
  6460. (goto-char pos)
  6461. (error "Cannot move this item further down"))))
  6462. (defun org-move-item-up (arg)
  6463. "Move the plain list item at point up, i.e. swap with previous item.
  6464. Subitems (items with larger indentation) are considered part of the item,
  6465. so this really moves item trees."
  6466. (interactive "p")
  6467. (let (beg beg0 end end0 ind ind1 (pos (point)) txt
  6468. ne-beg ne-end ne-ins ins-end)
  6469. (org-beginning-of-item)
  6470. (setq beg0 (point))
  6471. (setq ind (org-get-indentation))
  6472. (save-excursion
  6473. (setq ne-beg (org-back-over-empty-lines))
  6474. (setq beg (point)))
  6475. (goto-char beg0)
  6476. (org-end-of-item)
  6477. (setq ne-end (org-back-over-empty-lines))
  6478. (setq end (point))
  6479. (goto-char beg0)
  6480. (catch 'exit
  6481. (while t
  6482. (beginning-of-line 0)
  6483. (if (looking-at "[ \t]*$")
  6484. (if org-empty-line-terminates-plain-lists
  6485. (progn
  6486. (goto-char pos)
  6487. (error "Cannot move this item further up"))
  6488. nil)
  6489. (if (<= (setq ind1 (org-get-indentation)) ind)
  6490. (throw 'exit t)))))
  6491. (condition-case nil
  6492. (org-beginning-of-item)
  6493. (error (goto-char beg)
  6494. (error "Cannot move this item further up")))
  6495. (setq ind1 (org-get-indentation))
  6496. (if (and (org-at-item-p) (= ind ind1))
  6497. (progn
  6498. (setq ne-ins (org-back-over-empty-lines))
  6499. (setq txt (buffer-substring beg end))
  6500. (save-excursion
  6501. (delete-region beg end))
  6502. (setq pos (point))
  6503. (insert txt)
  6504. (setq ins-end (point))
  6505. (goto-char pos) (org-skip-whitespace)
  6506. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6507. ;; Move whitespace back to beginning
  6508. (save-excursion
  6509. (goto-char ins-end)
  6510. (let ((kill-whole-line t))
  6511. (kill-line (- ne-ins ne-beg)) (point)))
  6512. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6513. (org-maybe-renumber-ordered-list))
  6514. (goto-char pos)
  6515. (error "Cannot move this item further up"))))
  6516. (defun org-maybe-renumber-ordered-list ()
  6517. "Renumber the ordered list at point if setup allows it.
  6518. This tests the user option `org-auto-renumber-ordered-lists' before
  6519. doing the renumbering."
  6520. (interactive)
  6521. (when (and org-auto-renumber-ordered-lists
  6522. (org-at-item-p))
  6523. (if (match-beginning 3)
  6524. (org-renumber-ordered-list 1)
  6525. (org-fix-bullet-type))))
  6526. (defun org-maybe-renumber-ordered-list-safe ()
  6527. (condition-case nil
  6528. (save-excursion
  6529. (org-maybe-renumber-ordered-list))
  6530. (error nil)))
  6531. (defun org-cycle-list-bullet (&optional which)
  6532. "Cycle through the different itemize/enumerate bullets.
  6533. This cycle the entire list level through the sequence:
  6534. `-' -> `+' -> `*' -> `1.' -> `1)'
  6535. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6536. 0 meand `-', 1 means `+' etc."
  6537. (interactive "P")
  6538. (org-preserve-lc
  6539. (org-beginning-of-item-list)
  6540. (org-at-item-p)
  6541. (beginning-of-line 1)
  6542. (let ((current (match-string 0))
  6543. (prevp (eq which 'previous))
  6544. new)
  6545. (setq new (cond
  6546. ((and (numberp which)
  6547. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6548. ((string-match "-" current) (if prevp "1)" "+"))
  6549. ((string-match "\\+" current)
  6550. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6551. ((string-match "\\*" current) (if prevp "+" "1."))
  6552. ((string-match "\\." current) (if prevp "*" "1)"))
  6553. ((string-match ")" current) (if prevp "1." "-"))
  6554. (t (error "This should not happen"))))
  6555. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6556. (org-fix-bullet-type)
  6557. (org-maybe-renumber-ordered-list))))
  6558. (defun org-get-string-indentation (s)
  6559. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6560. (let ((n -1) (i 0) (w tab-width) c)
  6561. (catch 'exit
  6562. (while (< (setq n (1+ n)) (length s))
  6563. (setq c (aref s n))
  6564. (cond ((= c ?\ ) (setq i (1+ i)))
  6565. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6566. (t (throw 'exit t)))))
  6567. i))
  6568. (defun org-renumber-ordered-list (arg)
  6569. "Renumber an ordered plain list.
  6570. Cursor needs to be in the first line of an item, the line that starts
  6571. with something like \"1.\" or \"2)\"."
  6572. (interactive "p")
  6573. (unless (and (org-at-item-p)
  6574. (match-beginning 3))
  6575. (error "This is not an ordered list"))
  6576. (let ((line (org-current-line))
  6577. (col (current-column))
  6578. (ind (org-get-string-indentation
  6579. (buffer-substring (point-at-bol) (match-beginning 3))))
  6580. ;; (term (substring (match-string 3) -1))
  6581. ind1 (n (1- arg))
  6582. fmt)
  6583. ;; find where this list begins
  6584. (org-beginning-of-item-list)
  6585. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6586. (setq fmt (concat "%d" (match-string 1)))
  6587. (beginning-of-line 0)
  6588. ;; walk forward and replace these numbers
  6589. (catch 'exit
  6590. (while t
  6591. (catch 'next
  6592. (beginning-of-line 2)
  6593. (if (eobp) (throw 'exit nil))
  6594. (if (looking-at "[ \t]*$") (throw 'next nil))
  6595. (skip-chars-forward " \t") (setq ind1 (current-column))
  6596. (if (> ind1 ind) (throw 'next t))
  6597. (if (< ind1 ind) (throw 'exit t))
  6598. (if (not (org-at-item-p)) (throw 'exit nil))
  6599. (delete-region (match-beginning 2) (match-end 2))
  6600. (goto-char (match-beginning 2))
  6601. (insert (format fmt (setq n (1+ n)))))))
  6602. (goto-line line)
  6603. (move-to-column col)))
  6604. (defun org-fix-bullet-type ()
  6605. "Make sure all items in this list have the same bullet as the firsst item."
  6606. (interactive)
  6607. (unless (org-at-item-p) (error "This is not a list"))
  6608. (let ((line (org-current-line))
  6609. (col (current-column))
  6610. (ind (current-indentation))
  6611. ind1 bullet)
  6612. ;; find where this list begins
  6613. (org-beginning-of-item-list)
  6614. (beginning-of-line 1)
  6615. ;; find out what the bullet type is
  6616. (looking-at "[ \t]*\\(\\S-+\\)")
  6617. (setq bullet (match-string 1))
  6618. ;; walk forward and replace these numbers
  6619. (beginning-of-line 0)
  6620. (catch 'exit
  6621. (while t
  6622. (catch 'next
  6623. (beginning-of-line 2)
  6624. (if (eobp) (throw 'exit nil))
  6625. (if (looking-at "[ \t]*$") (throw 'next nil))
  6626. (skip-chars-forward " \t") (setq ind1 (current-column))
  6627. (if (> ind1 ind) (throw 'next t))
  6628. (if (< ind1 ind) (throw 'exit t))
  6629. (if (not (org-at-item-p)) (throw 'exit nil))
  6630. (skip-chars-forward " \t")
  6631. (looking-at "\\S-+")
  6632. (replace-match bullet))))
  6633. (goto-line line)
  6634. (move-to-column col)
  6635. (if (string-match "[0-9]" bullet)
  6636. (org-renumber-ordered-list 1))))
  6637. (defun org-beginning-of-item-list ()
  6638. "Go to the beginning of the current item list.
  6639. I.e. to the first item in this list."
  6640. (interactive)
  6641. (org-beginning-of-item)
  6642. (let ((pos (point-at-bol))
  6643. (ind (org-get-indentation))
  6644. ind1)
  6645. ;; find where this list begins
  6646. (catch 'exit
  6647. (while t
  6648. (catch 'next
  6649. (beginning-of-line 0)
  6650. (if (looking-at "[ \t]*$")
  6651. (throw (if (bobp) 'exit 'next) t))
  6652. (skip-chars-forward " \t") (setq ind1 (current-column))
  6653. (if (or (< ind1 ind)
  6654. (and (= ind1 ind)
  6655. (not (org-at-item-p)))
  6656. (bobp))
  6657. (throw 'exit t)
  6658. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6659. (goto-char pos)))
  6660. (defun org-end-of-item-list ()
  6661. "Go to the end of the current item list.
  6662. I.e. to the text after the last item."
  6663. (interactive)
  6664. (org-beginning-of-item)
  6665. (let ((pos (point-at-bol))
  6666. (ind (org-get-indentation))
  6667. ind1)
  6668. ;; find where this list begins
  6669. (catch 'exit
  6670. (while t
  6671. (catch 'next
  6672. (beginning-of-line 2)
  6673. (if (looking-at "[ \t]*$")
  6674. (throw (if (eobp) 'exit 'next) t))
  6675. (skip-chars-forward " \t") (setq ind1 (current-column))
  6676. (if (or (< ind1 ind)
  6677. (and (= ind1 ind)
  6678. (not (org-at-item-p)))
  6679. (eobp))
  6680. (progn
  6681. (setq pos (point-at-bol))
  6682. (throw 'exit t))))))
  6683. (goto-char pos)))
  6684. (defvar org-last-indent-begin-marker (make-marker))
  6685. (defvar org-last-indent-end-marker (make-marker))
  6686. (defun org-outdent-item (arg)
  6687. "Outdent a local list item."
  6688. (interactive "p")
  6689. (org-indent-item (- arg)))
  6690. (defun org-indent-item (arg)
  6691. "Indent a local list item."
  6692. (interactive "p")
  6693. (unless (org-at-item-p)
  6694. (error "Not on an item"))
  6695. (save-excursion
  6696. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6697. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6698. (setq beg org-last-indent-begin-marker
  6699. end org-last-indent-end-marker)
  6700. (org-beginning-of-item)
  6701. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6702. (org-end-of-item)
  6703. (setq end (move-marker org-last-indent-end-marker (point))))
  6704. (goto-char beg)
  6705. (setq tmp (org-item-indent-positions)
  6706. ind (car tmp)
  6707. ind-down (nth 2 tmp)
  6708. ind-up (nth 1 tmp)
  6709. delta (if (> arg 0)
  6710. (if ind-down (- ind-down ind) 2)
  6711. (if ind-up (- ind-up ind) -2)))
  6712. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6713. (while (< (point) end)
  6714. (beginning-of-line 1)
  6715. (skip-chars-forward " \t") (setq ind1 (current-column))
  6716. (delete-region (point-at-bol) (point))
  6717. (or (eolp) (indent-to-column (+ ind1 delta)))
  6718. (beginning-of-line 2))))
  6719. (org-fix-bullet-type)
  6720. (org-maybe-renumber-ordered-list-safe)
  6721. (save-excursion
  6722. (beginning-of-line 0)
  6723. (condition-case nil (org-beginning-of-item) (error nil))
  6724. (org-maybe-renumber-ordered-list-safe)))
  6725. (defun org-item-indent-positions ()
  6726. "Return indentation for plain list items.
  6727. This returns a list with three values: The current indentation, the
  6728. parent indentation and the indentation a child should habe.
  6729. Assumes cursor in item line."
  6730. (let* ((bolpos (point-at-bol))
  6731. (ind (org-get-indentation))
  6732. ind-down ind-up pos)
  6733. (save-excursion
  6734. (org-beginning-of-item-list)
  6735. (skip-chars-backward "\n\r \t")
  6736. (when (org-in-item-p)
  6737. (org-beginning-of-item)
  6738. (setq ind-up (org-get-indentation))))
  6739. (setq pos (point))
  6740. (save-excursion
  6741. (cond
  6742. ((and (condition-case nil (progn (org-previous-item) t)
  6743. (error nil))
  6744. (or (forward-char 1) t)
  6745. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6746. (setq ind-down (org-get-indentation)))
  6747. ((and (goto-char pos)
  6748. (org-at-item-p))
  6749. (goto-char (match-end 0))
  6750. (skip-chars-forward " \t")
  6751. (setq ind-down (current-column)))))
  6752. (list ind ind-up ind-down)))
  6753. ;;; The orgstruct minor mode
  6754. ;; Define a minor mode which can be used in other modes in order to
  6755. ;; integrate the org-mode structure editing commands.
  6756. ;; This is really a hack, because the org-mode structure commands use
  6757. ;; keys which normally belong to the major mode. Here is how it
  6758. ;; works: The minor mode defines all the keys necessary to operate the
  6759. ;; structure commands, but wraps the commands into a function which
  6760. ;; tests if the cursor is currently at a headline or a plain list
  6761. ;; item. If that is the case, the structure command is used,
  6762. ;; temporarily setting many Org-mode variables like regular
  6763. ;; expressions for filling etc. However, when any of those keys is
  6764. ;; used at a different location, function uses `key-binding' to look
  6765. ;; up if the key has an associated command in another currently active
  6766. ;; keymap (minor modes, major mode, global), and executes that
  6767. ;; command. There might be problems if any of the keys is otherwise
  6768. ;; used as a prefix key.
  6769. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6770. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6771. ;; addresses this by checking explicitly for both bindings.
  6772. (defvar orgstruct-mode-map (make-sparse-keymap)
  6773. "Keymap for the minor `orgstruct-mode'.")
  6774. (defvar org-local-vars nil
  6775. "List of local variables, for use by `orgstruct-mode'")
  6776. ;;;###autoload
  6777. (define-minor-mode orgstruct-mode
  6778. "Toggle the minor more `orgstruct-mode'.
  6779. This mode is for using Org-mode structure commands in other modes.
  6780. The following key behave as if Org-mode was active, if the cursor
  6781. is on a headline, or on a plain list item (both in the definition
  6782. of Org-mode).
  6783. M-up Move entry/item up
  6784. M-down Move entry/item down
  6785. M-left Promote
  6786. M-right Demote
  6787. M-S-up Move entry/item up
  6788. M-S-down Move entry/item down
  6789. M-S-left Promote subtree
  6790. M-S-right Demote subtree
  6791. M-q Fill paragraph and items like in Org-mode
  6792. C-c ^ Sort entries
  6793. C-c - Cycle list bullet
  6794. TAB Cycle item visibility
  6795. M-RET Insert new heading/item
  6796. S-M-RET Insert new TODO heading / Chekbox item
  6797. C-c C-c Set tags / toggle checkbox"
  6798. nil " OrgStruct" nil
  6799. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6800. ;;;###autoload
  6801. (defun turn-on-orgstruct ()
  6802. "Unconditionally turn on `orgstruct-mode'."
  6803. (orgstruct-mode 1))
  6804. ;;;###autoload
  6805. (defun turn-on-orgstruct++ ()
  6806. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6807. In addition to setting orgstruct-mode, this also exports all indentation and
  6808. autofilling variables from org-mode into the buffer. Note that turning
  6809. off orgstruct-mode will *not* remove these additional settings."
  6810. (orgstruct-mode 1)
  6811. (let (var val)
  6812. (mapc
  6813. (lambda (x)
  6814. (when (string-match
  6815. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6816. (symbol-name (car x)))
  6817. (setq var (car x) val (nth 1 x))
  6818. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6819. org-local-vars)))
  6820. (defun orgstruct-error ()
  6821. "Error when there is no default binding for a structure key."
  6822. (interactive)
  6823. (error "This key has no function outside structure elements"))
  6824. (defun orgstruct-setup ()
  6825. "Setup orgstruct keymaps."
  6826. (let ((nfunc 0)
  6827. (bindings
  6828. (list
  6829. '([(meta up)] org-metaup)
  6830. '([(meta down)] org-metadown)
  6831. '([(meta left)] org-metaleft)
  6832. '([(meta right)] org-metaright)
  6833. '([(meta shift up)] org-shiftmetaup)
  6834. '([(meta shift down)] org-shiftmetadown)
  6835. '([(meta shift left)] org-shiftmetaleft)
  6836. '([(meta shift right)] org-shiftmetaright)
  6837. '([(shift up)] org-shiftup)
  6838. '([(shift down)] org-shiftdown)
  6839. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6840. '("\M-q" fill-paragraph)
  6841. '("\C-c^" org-sort)
  6842. '("\C-c-" org-cycle-list-bullet)))
  6843. elt key fun cmd)
  6844. (while (setq elt (pop bindings))
  6845. (setq nfunc (1+ nfunc))
  6846. (setq key (org-key (car elt))
  6847. fun (nth 1 elt)
  6848. cmd (orgstruct-make-binding fun nfunc key))
  6849. (org-defkey orgstruct-mode-map key cmd))
  6850. ;; Special treatment needed for TAB and RET
  6851. (org-defkey orgstruct-mode-map [(tab)]
  6852. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6853. (org-defkey orgstruct-mode-map "\C-i"
  6854. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6855. (org-defkey orgstruct-mode-map "\M-\C-m"
  6856. (orgstruct-make-binding 'org-insert-heading 105
  6857. "\M-\C-m" [(meta return)]))
  6858. (org-defkey orgstruct-mode-map [(meta return)]
  6859. (orgstruct-make-binding 'org-insert-heading 106
  6860. [(meta return)] "\M-\C-m"))
  6861. (org-defkey orgstruct-mode-map [(shift meta return)]
  6862. (orgstruct-make-binding 'org-insert-todo-heading 107
  6863. [(meta return)] "\M-\C-m"))
  6864. (unless org-local-vars
  6865. (setq org-local-vars (org-get-local-variables)))
  6866. t))
  6867. (defun orgstruct-make-binding (fun n &rest keys)
  6868. "Create a function for binding in the structure minor mode.
  6869. FUN is the command to call inside a table. N is used to create a unique
  6870. command name. KEYS are keys that should be checked in for a command
  6871. to execute outside of tables."
  6872. (eval
  6873. (list 'defun
  6874. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6875. '(arg)
  6876. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6877. "Outside of structure, run the binding of `"
  6878. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6879. "'.")
  6880. '(interactive "p")
  6881. (list 'if
  6882. '(org-context-p 'headline 'item)
  6883. (list 'org-run-like-in-org-mode (list 'quote fun))
  6884. (list 'let '(orgstruct-mode)
  6885. (list 'call-interactively
  6886. (append '(or)
  6887. (mapcar (lambda (k)
  6888. (list 'key-binding k))
  6889. keys)
  6890. '('orgstruct-error))))))))
  6891. (defun org-context-p (&rest contexts)
  6892. "Check if local context is and of CONTEXTS.
  6893. Possible values in the list of contexts are `table', `headline', and `item'."
  6894. (let ((pos (point)))
  6895. (goto-char (point-at-bol))
  6896. (prog1 (or (and (memq 'table contexts)
  6897. (looking-at "[ \t]*|"))
  6898. (and (memq 'headline contexts)
  6899. (looking-at "\\*+"))
  6900. (and (memq 'item contexts)
  6901. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6902. (goto-char pos))))
  6903. (defun org-get-local-variables ()
  6904. "Return a list of all local variables in an org-mode buffer."
  6905. (let (varlist)
  6906. (with-current-buffer (get-buffer-create "*Org tmp*")
  6907. (erase-buffer)
  6908. (org-mode)
  6909. (setq varlist (buffer-local-variables)))
  6910. (kill-buffer "*Org tmp*")
  6911. (delq nil
  6912. (mapcar
  6913. (lambda (x)
  6914. (setq x
  6915. (if (symbolp x)
  6916. (list x)
  6917. (list (car x) (list 'quote (cdr x)))))
  6918. (if (string-match
  6919. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6920. (symbol-name (car x)))
  6921. x nil))
  6922. varlist))))
  6923. ;;;###autoload
  6924. (defun org-run-like-in-org-mode (cmd)
  6925. (unless org-local-vars
  6926. (setq org-local-vars (org-get-local-variables)))
  6927. (eval (list 'let org-local-vars
  6928. (list 'call-interactively (list 'quote cmd)))))
  6929. ;;;; Archiving
  6930. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6931. (defun org-archive-subtree (&optional find-done)
  6932. "Move the current subtree to the archive.
  6933. The archive can be a certain top-level heading in the current file, or in
  6934. a different file. The tree will be moved to that location, the subtree
  6935. heading be marked DONE, and the current time will be added.
  6936. When called with prefix argument FIND-DONE, find whole trees without any
  6937. open TODO items and archive them (after getting confirmation from the user).
  6938. If the cursor is not at a headline when this comand is called, try all level
  6939. 1 trees. If the cursor is on a headline, only try the direct children of
  6940. this heading."
  6941. (interactive "P")
  6942. (if find-done
  6943. (org-archive-all-done)
  6944. ;; Save all relevant TODO keyword-relatex variables
  6945. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6946. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6947. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6948. (tr-org-done-keywords org-done-keywords)
  6949. (tr-org-todo-regexp org-todo-regexp)
  6950. (tr-org-todo-line-regexp org-todo-line-regexp)
  6951. (tr-org-odd-levels-only org-odd-levels-only)
  6952. (this-buffer (current-buffer))
  6953. (org-archive-location org-archive-location)
  6954. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6955. ;; start of variables that will be used for saving context
  6956. ;; The compiler complains about them - keep them anyway!
  6957. (file (abbreviate-file-name (buffer-file-name)))
  6958. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  6959. (time (format-time-string
  6960. (substring (cdr org-time-stamp-formats) 1 -1)
  6961. (current-time)))
  6962. afile heading buffer level newfile-p
  6963. category todo priority
  6964. ;; start of variables that will be used for savind context
  6965. ltags itags prop)
  6966. ;; Try to find a local archive location
  6967. (save-excursion
  6968. (save-restriction
  6969. (widen)
  6970. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6971. (if (and prop (string-match "\\S-" prop))
  6972. (setq org-archive-location prop)
  6973. (if (or (re-search-backward re nil t)
  6974. (re-search-forward re nil t))
  6975. (setq org-archive-location (match-string 1))))))
  6976. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6977. (progn
  6978. (setq afile (format (match-string 1 org-archive-location)
  6979. (file-name-nondirectory buffer-file-name))
  6980. heading (match-string 2 org-archive-location)))
  6981. (error "Invalid `org-archive-location'"))
  6982. (if (> (length afile) 0)
  6983. (setq newfile-p (not (file-exists-p afile))
  6984. buffer (find-file-noselect afile))
  6985. (setq buffer (current-buffer)))
  6986. (unless buffer
  6987. (error "Cannot access file \"%s\"" afile))
  6988. (if (and (> (length heading) 0)
  6989. (string-match "^\\*+" heading))
  6990. (setq level (match-end 0))
  6991. (setq heading nil level 0))
  6992. (save-excursion
  6993. (org-back-to-heading t)
  6994. ;; Get context information that will be lost by moving the tree
  6995. (org-refresh-category-properties)
  6996. (setq category (org-get-category)
  6997. todo (and (looking-at org-todo-line-regexp)
  6998. (match-string 2))
  6999. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7000. ltags (org-get-tags)
  7001. itags (org-delete-all ltags (org-get-tags-at)))
  7002. (setq ltags (mapconcat 'identity ltags " ")
  7003. itags (mapconcat 'identity itags " "))
  7004. ;; We first only copy, in case something goes wrong
  7005. ;; we need to protect this-command, to avoid kill-region sets it,
  7006. ;; which would lead to duplication of subtrees
  7007. (let (this-command) (org-copy-subtree))
  7008. (set-buffer buffer)
  7009. ;; Enforce org-mode for the archive buffer
  7010. (if (not (org-mode-p))
  7011. ;; Force the mode for future visits.
  7012. (let ((org-insert-mode-line-in-empty-file t)
  7013. (org-inhibit-startup t))
  7014. (call-interactively 'org-mode)))
  7015. (when newfile-p
  7016. (goto-char (point-max))
  7017. (insert (format "\nArchived entries from file %s\n\n"
  7018. (buffer-file-name this-buffer))))
  7019. ;; Force the TODO keywords of the original buffer
  7020. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7021. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7022. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7023. (org-done-keywords tr-org-done-keywords)
  7024. (org-todo-regexp tr-org-todo-regexp)
  7025. (org-todo-line-regexp tr-org-todo-line-regexp)
  7026. (org-odd-levels-only
  7027. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7028. org-odd-levels-only
  7029. tr-org-odd-levels-only)))
  7030. (goto-char (point-min))
  7031. (if heading
  7032. (progn
  7033. (if (re-search-forward
  7034. (concat "^" (regexp-quote heading)
  7035. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7036. nil t)
  7037. (goto-char (match-end 0))
  7038. ;; Heading not found, just insert it at the end
  7039. (goto-char (point-max))
  7040. (or (bolp) (insert "\n"))
  7041. (insert "\n" heading "\n")
  7042. (end-of-line 0))
  7043. ;; Make the subtree visible
  7044. (show-subtree)
  7045. (org-end-of-subtree t)
  7046. (skip-chars-backward " \t\r\n")
  7047. (and (looking-at "[ \t\r\n]*")
  7048. (replace-match "\n\n")))
  7049. ;; No specific heading, just go to end of file.
  7050. (goto-char (point-max)) (insert "\n"))
  7051. ;; Paste
  7052. (org-paste-subtree (org-get-legal-level level 1))
  7053. ;; Mark the entry as done
  7054. (when (and org-archive-mark-done
  7055. (looking-at org-todo-line-regexp)
  7056. (or (not (match-end 2))
  7057. (not (member (match-string 2) org-done-keywords))))
  7058. (let (org-log-done)
  7059. (org-todo
  7060. (car (or (member org-archive-mark-done org-done-keywords)
  7061. org-done-keywords)))))
  7062. ;; Add the context info
  7063. (when org-archive-save-context-info
  7064. (let ((l org-archive-save-context-info) e n v)
  7065. (while (setq e (pop l))
  7066. (when (and (setq v (symbol-value e))
  7067. (stringp v) (string-match "\\S-" v))
  7068. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7069. (org-entry-put (point) n v)))))
  7070. ;; Save the buffer, if it is not the same buffer.
  7071. (if (not (eq this-buffer buffer)) (save-buffer))))
  7072. ;; Here we are back in the original buffer. Everything seems to have
  7073. ;; worked. So now cut the tree and finish up.
  7074. (let (this-command) (org-cut-subtree))
  7075. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7076. (message "Subtree archived %s"
  7077. (if (eq this-buffer buffer)
  7078. (concat "under heading: " heading)
  7079. (concat "in file: " (abbreviate-file-name afile)))))))
  7080. (defun org-refresh-category-properties ()
  7081. "Refresh category text properties in teh buffer."
  7082. (let ((def-cat (cond
  7083. ((null org-category)
  7084. (if buffer-file-name
  7085. (file-name-sans-extension
  7086. (file-name-nondirectory buffer-file-name))
  7087. "???"))
  7088. ((symbolp org-category) (symbol-name org-category))
  7089. (t org-category)))
  7090. beg end cat pos optionp)
  7091. (org-unmodified
  7092. (save-excursion
  7093. (save-restriction
  7094. (widen)
  7095. (goto-char (point-min))
  7096. (put-text-property (point) (point-max) 'org-category def-cat)
  7097. (while (re-search-forward
  7098. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7099. (setq pos (match-end 0)
  7100. optionp (equal (char-after (match-beginning 0)) ?#)
  7101. cat (org-trim (match-string 2)))
  7102. (if optionp
  7103. (setq beg (point-at-bol) end (point-max))
  7104. (org-back-to-heading t)
  7105. (setq beg (point) end (org-end-of-subtree t t)))
  7106. (put-text-property beg end 'org-category cat)
  7107. (goto-char pos)))))))
  7108. (defun org-archive-all-done (&optional tag)
  7109. "Archive sublevels of the current tree without open TODO items.
  7110. If the cursor is not on a headline, try all level 1 trees. If
  7111. it is on a headline, try all direct children.
  7112. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7113. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7114. (rea (concat ".*:" org-archive-tag ":"))
  7115. (begm (make-marker))
  7116. (endm (make-marker))
  7117. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7118. "Move subtree to archive (no open TODO items)? "))
  7119. beg end (cntarch 0))
  7120. (if (org-on-heading-p)
  7121. (progn
  7122. (setq re1 (concat "^" (regexp-quote
  7123. (make-string
  7124. (1+ (- (match-end 0) (match-beginning 0) 1))
  7125. ?*))
  7126. " "))
  7127. (move-marker begm (point))
  7128. (move-marker endm (org-end-of-subtree t)))
  7129. (setq re1 "^* ")
  7130. (move-marker begm (point-min))
  7131. (move-marker endm (point-max)))
  7132. (save-excursion
  7133. (goto-char begm)
  7134. (while (re-search-forward re1 endm t)
  7135. (setq beg (match-beginning 0)
  7136. end (save-excursion (org-end-of-subtree t) (point)))
  7137. (goto-char beg)
  7138. (if (re-search-forward re end t)
  7139. (goto-char end)
  7140. (goto-char beg)
  7141. (if (and (or (not tag) (not (looking-at rea)))
  7142. (y-or-n-p question))
  7143. (progn
  7144. (if tag
  7145. (org-toggle-tag org-archive-tag 'on)
  7146. (org-archive-subtree))
  7147. (setq cntarch (1+ cntarch)))
  7148. (goto-char end)))))
  7149. (message "%d trees archived" cntarch)))
  7150. (defun org-cycle-hide-drawers (state)
  7151. "Re-hide all drawers after a visibility state change."
  7152. (when (and (org-mode-p)
  7153. (not (memq state '(overview folded))))
  7154. (save-excursion
  7155. (let* ((globalp (memq state '(contents all)))
  7156. (beg (if globalp (point-min) (point)))
  7157. (end (if globalp (point-max) (org-end-of-subtree t))))
  7158. (goto-char beg)
  7159. (while (re-search-forward org-drawer-regexp end t)
  7160. (org-flag-drawer t))))))
  7161. (defun org-flag-drawer (flag)
  7162. (save-excursion
  7163. (beginning-of-line 1)
  7164. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7165. (let ((b (match-end 0))
  7166. (outline-regexp org-outline-regexp))
  7167. (if (re-search-forward
  7168. "^[ \t]*:END:"
  7169. (save-excursion (outline-next-heading) (point)) t)
  7170. (outline-flag-region b (point-at-eol) flag)
  7171. (error ":END: line missing"))))))
  7172. (defun org-cycle-hide-archived-subtrees (state)
  7173. "Re-hide all archived subtrees after a visibility state change."
  7174. (when (and (not org-cycle-open-archived-trees)
  7175. (not (memq state '(overview folded))))
  7176. (save-excursion
  7177. (let* ((globalp (memq state '(contents all)))
  7178. (beg (if globalp (point-min) (point)))
  7179. (end (if globalp (point-max) (org-end-of-subtree t))))
  7180. (org-hide-archived-subtrees beg end)
  7181. (goto-char beg)
  7182. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7183. (message "%s" (substitute-command-keys
  7184. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7185. (defun org-force-cycle-archived ()
  7186. "Cycle subtree even if it is archived."
  7187. (interactive)
  7188. (setq this-command 'org-cycle)
  7189. (let ((org-cycle-open-archived-trees t))
  7190. (call-interactively 'org-cycle)))
  7191. (defun org-hide-archived-subtrees (beg end)
  7192. "Re-hide all archived subtrees after a visibility state change."
  7193. (save-excursion
  7194. (let* ((re (concat ":" org-archive-tag ":")))
  7195. (goto-char beg)
  7196. (while (re-search-forward re end t)
  7197. (and (org-on-heading-p) (hide-subtree))
  7198. (org-end-of-subtree t)))))
  7199. (defun org-toggle-tag (tag &optional onoff)
  7200. "Toggle the tag TAG for the current line.
  7201. If ONOFF is `on' or `off', don't toggle but set to this state."
  7202. (unless (org-on-heading-p t) (error "Not on headling"))
  7203. (let (res current)
  7204. (save-excursion
  7205. (beginning-of-line)
  7206. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7207. (point-at-eol) t)
  7208. (progn
  7209. (setq current (match-string 1))
  7210. (replace-match ""))
  7211. (setq current ""))
  7212. (setq current (nreverse (org-split-string current ":")))
  7213. (cond
  7214. ((eq onoff 'on)
  7215. (setq res t)
  7216. (or (member tag current) (push tag current)))
  7217. ((eq onoff 'off)
  7218. (or (not (member tag current)) (setq current (delete tag current))))
  7219. (t (if (member tag current)
  7220. (setq current (delete tag current))
  7221. (setq res t)
  7222. (push tag current))))
  7223. (end-of-line 1)
  7224. (if current
  7225. (progn
  7226. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7227. (org-set-tags nil t))
  7228. (delete-horizontal-space))
  7229. (run-hooks 'org-after-tags-change-hook))
  7230. res))
  7231. (defun org-toggle-archive-tag (&optional arg)
  7232. "Toggle the archive tag for the current headline.
  7233. With prefix ARG, check all children of current headline and offer tagging
  7234. the children that do not contain any open TODO items."
  7235. (interactive "P")
  7236. (if arg
  7237. (org-archive-all-done 'tag)
  7238. (let (set)
  7239. (save-excursion
  7240. (org-back-to-heading t)
  7241. (setq set (org-toggle-tag org-archive-tag))
  7242. (when set (hide-subtree)))
  7243. (and set (beginning-of-line 1))
  7244. (message "Subtree %s" (if set "archived" "unarchived")))))
  7245. ;;;; Tables
  7246. ;;; The table editor
  7247. ;; Watch out: Here we are talking about two different kind of tables.
  7248. ;; Most of the code is for the tables created with the Org-mode table editor.
  7249. ;; Sometimes, we talk about tables created and edited with the table.el
  7250. ;; Emacs package. We call the former org-type tables, and the latter
  7251. ;; table.el-type tables.
  7252. (defun org-before-change-function (beg end)
  7253. "Every change indicates that a table might need an update."
  7254. (setq org-table-may-need-update t))
  7255. (defconst org-table-line-regexp "^[ \t]*|"
  7256. "Detects an org-type table line.")
  7257. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7258. "Detects an org-type table line.")
  7259. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7260. "Detects a table line marked for automatic recalculation.")
  7261. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7262. "Detects a table line marked for automatic recalculation.")
  7263. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7264. "Detects a table line marked for automatic recalculation.")
  7265. (defconst org-table-hline-regexp "^[ \t]*|-"
  7266. "Detects an org-type table hline.")
  7267. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7268. "Detects a table-type table hline.")
  7269. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7270. "Detects an org-type or table-type table.")
  7271. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7272. "Searching from within a table (any type) this finds the first line
  7273. outside the table.")
  7274. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7275. "Searching from within a table (any type) this finds the first line
  7276. outside the table.")
  7277. (defvar org-table-last-highlighted-reference nil)
  7278. (defvar org-table-formula-history nil)
  7279. (defvar org-table-column-names nil
  7280. "Alist with column names, derived from the `!' line.")
  7281. (defvar org-table-column-name-regexp nil
  7282. "Regular expression matching the current column names.")
  7283. (defvar org-table-local-parameters nil
  7284. "Alist with parameter names, derived from the `$' line.")
  7285. (defvar org-table-named-field-locations nil
  7286. "Alist with locations of named fields.")
  7287. (defvar org-table-current-line-types nil
  7288. "Table row types, non-nil only for the duration of a comand.")
  7289. (defvar org-table-current-begin-line nil
  7290. "Table begin line, non-nil only for the duration of a comand.")
  7291. (defvar org-table-current-begin-pos nil
  7292. "Table begin position, non-nil only for the duration of a comand.")
  7293. (defvar org-table-dlines nil
  7294. "Vector of data line line numbers in the current table.")
  7295. (defvar org-table-hlines nil
  7296. "Vector of hline line numbers in the current table.")
  7297. (defconst org-table-range-regexp
  7298. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7299. ;; 1 2 3 4 5
  7300. "Regular expression for matching ranges in formulas.")
  7301. (defconst org-table-range-regexp2
  7302. (concat
  7303. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7304. "\\.\\."
  7305. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7306. "Match a range for reference display.")
  7307. (defconst org-table-translate-regexp
  7308. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7309. "Match a reference that needs translation, for reference display.")
  7310. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7311. (defun org-table-create-with-table.el ()
  7312. "Use the table.el package to insert a new table.
  7313. If there is already a table at point, convert between Org-mode tables
  7314. and table.el tables."
  7315. (interactive)
  7316. (require 'table)
  7317. (cond
  7318. ((org-at-table.el-p)
  7319. (if (y-or-n-p "Convert table to Org-mode table? ")
  7320. (org-table-convert)))
  7321. ((org-at-table-p)
  7322. (if (y-or-n-p "Convert table to table.el table? ")
  7323. (org-table-convert)))
  7324. (t (call-interactively 'table-insert))))
  7325. (defun org-table-create-or-convert-from-region (arg)
  7326. "Convert region to table, or create an empty table.
  7327. If there is an active region, convert it to a table, using the function
  7328. `org-table-convert-region'. See the documentation of that function
  7329. to learn how the prefix argument is interpreted to determine the field
  7330. separator.
  7331. If there is no such region, create an empty table with `org-table-create'."
  7332. (interactive "P")
  7333. (if (org-region-active-p)
  7334. (org-table-convert-region (region-beginning) (region-end) arg)
  7335. (org-table-create arg)))
  7336. (defun org-table-create (&optional size)
  7337. "Query for a size and insert a table skeleton.
  7338. SIZE is a string Columns x Rows like for example \"3x2\"."
  7339. (interactive "P")
  7340. (unless size
  7341. (setq size (read-string
  7342. (concat "Table size Columns x Rows [e.g. "
  7343. org-table-default-size "]: ")
  7344. "" nil org-table-default-size)))
  7345. (let* ((pos (point))
  7346. (indent (make-string (current-column) ?\ ))
  7347. (split (org-split-string size " *x *"))
  7348. (rows (string-to-number (nth 1 split)))
  7349. (columns (string-to-number (car split)))
  7350. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7351. "\n")))
  7352. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7353. (point-at-bol) (point)))
  7354. (beginning-of-line 1)
  7355. (newline))
  7356. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7357. (dotimes (i rows) (insert line))
  7358. (goto-char pos)
  7359. (if (> rows 1)
  7360. ;; Insert a hline after the first row.
  7361. (progn
  7362. (end-of-line 1)
  7363. (insert "\n|-")
  7364. (goto-char pos)))
  7365. (org-table-align)))
  7366. (defun org-table-convert-region (beg0 end0 &optional separator)
  7367. "Convert region to a table.
  7368. The region goes from BEG0 to END0, but these borders will be moved
  7369. slightly, to make sure a beginning of line in the first line is included.
  7370. SEPARATOR specifies the field separator in the lines. It can have the
  7371. following values:
  7372. '(4) Use the comma as a field separator
  7373. '(16) Use a TAB as field separator
  7374. integer When a number, use that many spaces as field separator
  7375. nil When nil, the command tries to be smart and figure out the
  7376. separator in the following way:
  7377. - when each line contains a TAB, assume TAB-separated material
  7378. - when each line contains a comme, assume CSV material
  7379. - else, assume one or more SPACE charcters as separator."
  7380. (interactive "rP")
  7381. (let* ((beg (min beg0 end0))
  7382. (end (max beg0 end0))
  7383. re)
  7384. (goto-char beg)
  7385. (beginning-of-line 1)
  7386. (setq beg (move-marker (make-marker) (point)))
  7387. (goto-char end)
  7388. (if (bolp) (backward-char 1) (end-of-line 1))
  7389. (setq end (move-marker (make-marker) (point)))
  7390. ;; Get the right field separator
  7391. (unless separator
  7392. (goto-char beg)
  7393. (setq separator
  7394. (cond
  7395. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7396. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7397. (t 1))))
  7398. (setq re (cond
  7399. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7400. ((equal separator '(16)) "^\\|\t")
  7401. ((integerp separator)
  7402. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7403. (t (error "This should not happen"))))
  7404. (goto-char beg)
  7405. (while (re-search-forward re end t)
  7406. (replace-match "| " t t))
  7407. (goto-char beg)
  7408. (insert " ")
  7409. (org-table-align)))
  7410. (defun org-table-import (file arg)
  7411. "Import FILE as a table.
  7412. The file is assumed to be tab-separated. Such files can be produced by most
  7413. spreadsheet and database applications. If no tabs (at least one per line)
  7414. are found, lines will be split on whitespace into fields."
  7415. (interactive "f\nP")
  7416. (or (bolp) (newline))
  7417. (let ((beg (point))
  7418. (pm (point-max)))
  7419. (insert-file-contents file)
  7420. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7421. (defun org-table-export ()
  7422. "Export table as a tab-separated file.
  7423. Such a file can be imported into a spreadsheet program like Excel."
  7424. (interactive)
  7425. (let* ((beg (org-table-begin))
  7426. (end (org-table-end))
  7427. (table (buffer-substring beg end))
  7428. (file (read-file-name "Export table to: "))
  7429. buf)
  7430. (unless (or (not (file-exists-p file))
  7431. (y-or-n-p (format "Overwrite file %s? " file)))
  7432. (error "Abort"))
  7433. (with-current-buffer (find-file-noselect file)
  7434. (setq buf (current-buffer))
  7435. (erase-buffer)
  7436. (fundamental-mode)
  7437. (insert table)
  7438. (goto-char (point-min))
  7439. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7440. (replace-match "" t t)
  7441. (end-of-line 1))
  7442. (goto-char (point-min))
  7443. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7444. (replace-match "" t t)
  7445. (goto-char (min (1+ (point)) (point-max))))
  7446. (goto-char (point-min))
  7447. (while (re-search-forward "^-[-+]*$" nil t)
  7448. (replace-match "")
  7449. (if (looking-at "\n")
  7450. (delete-char 1)))
  7451. (goto-char (point-min))
  7452. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7453. (replace-match "\t" t t))
  7454. (save-buffer))
  7455. (kill-buffer buf)))
  7456. (defvar org-table-aligned-begin-marker (make-marker)
  7457. "Marker at the beginning of the table last aligned.
  7458. Used to check if cursor still is in that table, to minimize realignment.")
  7459. (defvar org-table-aligned-end-marker (make-marker)
  7460. "Marker at the end of the table last aligned.
  7461. Used to check if cursor still is in that table, to minimize realignment.")
  7462. (defvar org-table-last-alignment nil
  7463. "List of flags for flushright alignment, from the last re-alignment.
  7464. This is being used to correctly align a single field after TAB or RET.")
  7465. (defvar org-table-last-column-widths nil
  7466. "List of max width of fields in each column.
  7467. This is being used to correctly align a single field after TAB or RET.")
  7468. (defvar org-table-overlay-coordinates nil
  7469. "Overlay coordinates after each align of a table.")
  7470. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7471. (defvar org-last-recalc-line nil)
  7472. (defconst org-narrow-column-arrow "=>"
  7473. "Used as display property in narrowed table columns.")
  7474. (defun org-table-align ()
  7475. "Align the table at point by aligning all vertical bars."
  7476. (interactive)
  7477. (let* (
  7478. ;; Limits of table
  7479. (beg (org-table-begin))
  7480. (end (org-table-end))
  7481. ;; Current cursor position
  7482. (linepos (org-current-line))
  7483. (colpos (org-table-current-column))
  7484. (winstart (window-start))
  7485. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7486. lines (new "") lengths l typenums ty fields maxfields i
  7487. column
  7488. (indent "") cnt frac
  7489. rfmt hfmt
  7490. (spaces '(1 . 1))
  7491. (sp1 (car spaces))
  7492. (sp2 (cdr spaces))
  7493. (rfmt1 (concat
  7494. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7495. (hfmt1 (concat
  7496. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7497. emptystrings links dates emph narrow fmax f1 len c e)
  7498. (untabify beg end)
  7499. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7500. ;; Check if we have links or dates
  7501. (goto-char beg)
  7502. (setq links (re-search-forward org-bracket-link-regexp end t))
  7503. (goto-char beg)
  7504. (setq emph (and org-hide-emphasis-markers
  7505. (re-search-forward org-emph-re end t)))
  7506. (goto-char beg)
  7507. (setq dates (and org-display-custom-times
  7508. (re-search-forward org-ts-regexp-both end t)))
  7509. ;; Make sure the link properties are right
  7510. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7511. ;; Make sure the date properties are right
  7512. (when dates (goto-char beg) (while (org-activate-dates end)))
  7513. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7514. ;; Check if we are narrowing any columns
  7515. (goto-char beg)
  7516. (setq narrow (and org-format-transports-properties-p
  7517. (re-search-forward "<[0-9]+>" end t)))
  7518. ;; Get the rows
  7519. (setq lines (org-split-string
  7520. (buffer-substring beg end) "\n"))
  7521. ;; Store the indentation of the first line
  7522. (if (string-match "^ *" (car lines))
  7523. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7524. ;; Mark the hlines by setting the corresponding element to nil
  7525. ;; At the same time, we remove trailing space.
  7526. (setq lines (mapcar (lambda (l)
  7527. (if (string-match "^ *|-" l)
  7528. nil
  7529. (if (string-match "[ \t]+$" l)
  7530. (substring l 0 (match-beginning 0))
  7531. l)))
  7532. lines))
  7533. ;; Get the data fields by splitting the lines.
  7534. (setq fields (mapcar
  7535. (lambda (l)
  7536. (org-split-string l " *| *"))
  7537. (delq nil (copy-sequence lines))))
  7538. ;; How many fields in the longest line?
  7539. (condition-case nil
  7540. (setq maxfields (apply 'max (mapcar 'length fields)))
  7541. (error
  7542. (kill-region beg end)
  7543. (org-table-create org-table-default-size)
  7544. (error "Empty table - created default table")))
  7545. ;; A list of empty strings to fill any short rows on output
  7546. (setq emptystrings (make-list maxfields ""))
  7547. ;; Check for special formatting.
  7548. (setq i -1)
  7549. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7550. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7551. ;; Check if there is an explicit width specified
  7552. (when narrow
  7553. (setq c column fmax nil)
  7554. (while c
  7555. (setq e (pop c))
  7556. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7557. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7558. ;; Find fields that are wider than fmax, and shorten them
  7559. (when fmax
  7560. (loop for xx in column do
  7561. (when (and (stringp xx)
  7562. (> (org-string-width xx) fmax))
  7563. (org-add-props xx nil
  7564. 'help-echo
  7565. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7566. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7567. (unless (> f1 1)
  7568. (error "Cannot narrow field starting with wide link \"%s\""
  7569. (match-string 0 xx)))
  7570. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7571. (add-text-properties (- f1 2) f1
  7572. (list 'display org-narrow-column-arrow)
  7573. xx)))))
  7574. ;; Get the maximum width for each column
  7575. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7576. ;; Get the fraction of numbers, to decide about alignment of the column
  7577. (setq cnt 0 frac 0.0)
  7578. (loop for x in column do
  7579. (if (equal x "")
  7580. nil
  7581. (setq frac ( / (+ (* frac cnt)
  7582. (if (string-match org-table-number-regexp x) 1 0))
  7583. (setq cnt (1+ cnt))))))
  7584. (push (>= frac org-table-number-fraction) typenums))
  7585. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7586. ;; Store the alignment of this table, for later editing of single fields
  7587. (setq org-table-last-alignment typenums
  7588. org-table-last-column-widths lengths)
  7589. ;; With invisible characters, `format' does not get the field width right
  7590. ;; So we need to make these fields wide by hand.
  7591. (when (or links emph)
  7592. (loop for i from 0 upto (1- maxfields) do
  7593. (setq len (nth i lengths))
  7594. (loop for j from 0 upto (1- (length fields)) do
  7595. (setq c (nthcdr i (car (nthcdr j fields))))
  7596. (if (and (stringp (car c))
  7597. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7598. ; (string-match org-bracket-link-regexp (car c))
  7599. (< (org-string-width (car c)) len))
  7600. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7601. ;; Compute the formats needed for output of the table
  7602. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7603. (while (setq l (pop lengths))
  7604. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7605. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7606. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7607. (setq rfmt (concat rfmt "\n")
  7608. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7609. (setq new (mapconcat
  7610. (lambda (l)
  7611. (if l (apply 'format rfmt
  7612. (append (pop fields) emptystrings))
  7613. hfmt))
  7614. lines ""))
  7615. ;; Replace the old one
  7616. (delete-region beg end)
  7617. (move-marker end nil)
  7618. (move-marker org-table-aligned-begin-marker (point))
  7619. (insert new)
  7620. (move-marker org-table-aligned-end-marker (point))
  7621. (when (and orgtbl-mode (not (org-mode-p)))
  7622. (goto-char org-table-aligned-begin-marker)
  7623. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7624. ;; Try to move to the old location
  7625. (goto-line winstartline)
  7626. (setq winstart (point-at-bol))
  7627. (goto-line linepos)
  7628. (set-window-start (selected-window) winstart 'noforce)
  7629. (org-table-goto-column colpos)
  7630. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7631. (setq org-table-may-need-update nil)
  7632. ))
  7633. (defun org-string-width (s)
  7634. "Compute width of string, ignoring invisible characters.
  7635. This ignores character with invisibility property `org-link', and also
  7636. characters with property `org-cwidth', because these will become invisible
  7637. upon the next fontification round."
  7638. (let (b l)
  7639. (when (or (eq t buffer-invisibility-spec)
  7640. (assq 'org-link buffer-invisibility-spec))
  7641. (while (setq b (text-property-any 0 (length s)
  7642. 'invisible 'org-link s))
  7643. (setq s (concat (substring s 0 b)
  7644. (substring s (or (next-single-property-change
  7645. b 'invisible s) (length s)))))))
  7646. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7647. (setq s (concat (substring s 0 b)
  7648. (substring s (or (next-single-property-change
  7649. b 'org-cwidth s) (length s))))))
  7650. (setq l (string-width s) b -1)
  7651. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7652. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7653. l))
  7654. (defun org-table-begin (&optional table-type)
  7655. "Find the beginning of the table and return its position.
  7656. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7657. (save-excursion
  7658. (if (not (re-search-backward
  7659. (if table-type org-table-any-border-regexp
  7660. org-table-border-regexp)
  7661. nil t))
  7662. (progn (goto-char (point-min)) (point))
  7663. (goto-char (match-beginning 0))
  7664. (beginning-of-line 2)
  7665. (point))))
  7666. (defun org-table-end (&optional table-type)
  7667. "Find the end of the table and return its position.
  7668. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7669. (save-excursion
  7670. (if (not (re-search-forward
  7671. (if table-type org-table-any-border-regexp
  7672. org-table-border-regexp)
  7673. nil t))
  7674. (goto-char (point-max))
  7675. (goto-char (match-beginning 0)))
  7676. (point-marker)))
  7677. (defun org-table-justify-field-maybe (&optional new)
  7678. "Justify the current field, text to left, number to right.
  7679. Optional argument NEW may specify text to replace the current field content."
  7680. (cond
  7681. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7682. ((org-at-table-hline-p))
  7683. ((and (not new)
  7684. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7685. (current-buffer)))
  7686. (< (point) org-table-aligned-begin-marker)
  7687. (>= (point) org-table-aligned-end-marker)))
  7688. ;; This is not the same table, force a full re-align
  7689. (setq org-table-may-need-update t))
  7690. (t ;; realign the current field, based on previous full realign
  7691. (let* ((pos (point)) s
  7692. (col (org-table-current-column))
  7693. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7694. l f n o e)
  7695. (when (> col 0)
  7696. (skip-chars-backward "^|\n")
  7697. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7698. (progn
  7699. (setq s (match-string 1)
  7700. o (match-string 0)
  7701. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7702. e (not (= (match-beginning 2) (match-end 2))))
  7703. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7704. l (if e "|" (setq org-table-may-need-update t) ""))
  7705. n (format f s))
  7706. (if new
  7707. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7708. (setq n (format f new))
  7709. (setq n (concat new "|") org-table-may-need-update t)))
  7710. (or (equal n o)
  7711. (let (org-table-may-need-update)
  7712. (replace-match n t t))))
  7713. (setq org-table-may-need-update t))
  7714. (goto-char pos))))))
  7715. (defun org-table-next-field ()
  7716. "Go to the next field in the current table, creating new lines as needed.
  7717. Before doing so, re-align the table if necessary."
  7718. (interactive)
  7719. (org-table-maybe-eval-formula)
  7720. (org-table-maybe-recalculate-line)
  7721. (if (and org-table-automatic-realign
  7722. org-table-may-need-update)
  7723. (org-table-align))
  7724. (let ((end (org-table-end)))
  7725. (if (org-at-table-hline-p)
  7726. (end-of-line 1))
  7727. (condition-case nil
  7728. (progn
  7729. (re-search-forward "|" end)
  7730. (if (looking-at "[ \t]*$")
  7731. (re-search-forward "|" end))
  7732. (if (and (looking-at "-")
  7733. org-table-tab-jumps-over-hlines
  7734. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7735. (goto-char (match-beginning 1)))
  7736. (if (looking-at "-")
  7737. (progn
  7738. (beginning-of-line 0)
  7739. (org-table-insert-row 'below))
  7740. (if (looking-at " ") (forward-char 1))))
  7741. (error
  7742. (org-table-insert-row 'below)))))
  7743. (defun org-table-previous-field ()
  7744. "Go to the previous field in the table.
  7745. Before doing so, re-align the table if necessary."
  7746. (interactive)
  7747. (org-table-justify-field-maybe)
  7748. (org-table-maybe-recalculate-line)
  7749. (if (and org-table-automatic-realign
  7750. org-table-may-need-update)
  7751. (org-table-align))
  7752. (if (org-at-table-hline-p)
  7753. (end-of-line 1))
  7754. (re-search-backward "|" (org-table-begin))
  7755. (re-search-backward "|" (org-table-begin))
  7756. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7757. (re-search-backward "|" (org-table-begin)))
  7758. (if (looking-at "| ?")
  7759. (goto-char (match-end 0))))
  7760. (defun org-table-next-row ()
  7761. "Go to the next row (same column) in the current table.
  7762. Before doing so, re-align the table if necessary."
  7763. (interactive)
  7764. (org-table-maybe-eval-formula)
  7765. (org-table-maybe-recalculate-line)
  7766. (if (or (looking-at "[ \t]*$")
  7767. (save-excursion (skip-chars-backward " \t") (bolp)))
  7768. (newline)
  7769. (if (and org-table-automatic-realign
  7770. org-table-may-need-update)
  7771. (org-table-align))
  7772. (let ((col (org-table-current-column)))
  7773. (beginning-of-line 2)
  7774. (if (or (not (org-at-table-p))
  7775. (org-at-table-hline-p))
  7776. (progn
  7777. (beginning-of-line 0)
  7778. (org-table-insert-row 'below)))
  7779. (org-table-goto-column col)
  7780. (skip-chars-backward "^|\n\r")
  7781. (if (looking-at " ") (forward-char 1)))))
  7782. (defun org-table-copy-down (n)
  7783. "Copy a field down in the current column.
  7784. If the field at the cursor is empty, copy into it the content of the nearest
  7785. non-empty field above. With argument N, use the Nth non-empty field.
  7786. If the current field is not empty, it is copied down to the next row, and
  7787. the cursor is moved with it. Therefore, repeating this command causes the
  7788. column to be filled row-by-row.
  7789. If the variable `org-table-copy-increment' is non-nil and the field is an
  7790. integer or a timestamp, it will be incremented while copying. In the case of
  7791. a timestamp, if the cursor is on the year, change the year. If it is on the
  7792. month or the day, change that. Point will stay on the current date field
  7793. in order to easily repeat the interval."
  7794. (interactive "p")
  7795. (let* ((colpos (org-table-current-column))
  7796. (col (current-column))
  7797. (field (org-table-get-field))
  7798. (non-empty (string-match "[^ \t]" field))
  7799. (beg (org-table-begin))
  7800. txt)
  7801. (org-table-check-inside-data-field)
  7802. (if non-empty
  7803. (progn
  7804. (setq txt (org-trim field))
  7805. (org-table-next-row)
  7806. (org-table-blank-field))
  7807. (save-excursion
  7808. (setq txt
  7809. (catch 'exit
  7810. (while (progn (beginning-of-line 1)
  7811. (re-search-backward org-table-dataline-regexp
  7812. beg t))
  7813. (org-table-goto-column colpos t)
  7814. (if (and (looking-at
  7815. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7816. (= (setq n (1- n)) 0))
  7817. (throw 'exit (match-string 1))))))))
  7818. (if txt
  7819. (progn
  7820. (if (and org-table-copy-increment
  7821. (string-match "^[0-9]+$" txt))
  7822. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7823. (insert txt)
  7824. (move-to-column col)
  7825. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7826. (org-timestamp-up 1)
  7827. (org-table-maybe-recalculate-line))
  7828. (org-table-align)
  7829. (move-to-column col))
  7830. (error "No non-empty field found"))))
  7831. (defun org-table-check-inside-data-field ()
  7832. "Is point inside a table data field?
  7833. I.e. not on a hline or before the first or after the last column?
  7834. This actually throws an error, so it aborts the current command."
  7835. (if (or (not (org-at-table-p))
  7836. (= (org-table-current-column) 0)
  7837. (org-at-table-hline-p)
  7838. (looking-at "[ \t]*$"))
  7839. (error "Not in table data field")))
  7840. (defvar org-table-clip nil
  7841. "Clipboard for table regions.")
  7842. (defun org-table-blank-field ()
  7843. "Blank the current table field or active region."
  7844. (interactive)
  7845. (org-table-check-inside-data-field)
  7846. (if (and (interactive-p) (org-region-active-p))
  7847. (let (org-table-clip)
  7848. (org-table-cut-region (region-beginning) (region-end)))
  7849. (skip-chars-backward "^|")
  7850. (backward-char 1)
  7851. (if (looking-at "|[^|\n]+")
  7852. (let* ((pos (match-beginning 0))
  7853. (match (match-string 0))
  7854. (len (org-string-width match)))
  7855. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7856. (goto-char (+ 2 pos))
  7857. (substring match 1)))))
  7858. (defun org-table-get-field (&optional n replace)
  7859. "Return the value of the field in column N of current row.
  7860. N defaults to current field.
  7861. If REPLACE is a string, replace field with this value. The return value
  7862. is always the old value."
  7863. (and n (org-table-goto-column n))
  7864. (skip-chars-backward "^|\n")
  7865. (backward-char 1)
  7866. (if (looking-at "|[^|\r\n]*")
  7867. (let* ((pos (match-beginning 0))
  7868. (val (buffer-substring (1+ pos) (match-end 0))))
  7869. (if replace
  7870. (replace-match (concat "|" replace) t t))
  7871. (goto-char (min (point-at-eol) (+ 2 pos)))
  7872. val)
  7873. (forward-char 1) ""))
  7874. (defun org-table-field-info (arg)
  7875. "Show info about the current field, and highlight any reference at point."
  7876. (interactive "P")
  7877. (org-table-get-specials)
  7878. (save-excursion
  7879. (let* ((pos (point))
  7880. (col (org-table-current-column))
  7881. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7882. (name (car (rassoc (list (org-current-line) col)
  7883. org-table-named-field-locations)))
  7884. (eql (org-table-get-stored-formulas))
  7885. (dline (org-table-current-dline))
  7886. (ref (format "@%d$%d" dline col))
  7887. (ref1 (org-table-convert-refs-to-an ref))
  7888. (fequation (or (assoc name eql) (assoc ref eql)))
  7889. (cequation (assoc (int-to-string col) eql))
  7890. (eqn (or fequation cequation)))
  7891. (goto-char pos)
  7892. (condition-case nil
  7893. (org-table-show-reference 'local)
  7894. (error nil))
  7895. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7896. dline col
  7897. (if cname (concat " or $" cname) "")
  7898. dline col ref1
  7899. (if name (concat " or $" name) "")
  7900. ;; FIXME: formula info not correct if special table line
  7901. (if eqn
  7902. (concat ", formula: "
  7903. (org-table-formula-to-user
  7904. (concat
  7905. (if (string-match "^[$@]"(car eqn)) "" "$")
  7906. (car eqn) "=" (cdr eqn))))
  7907. "")))))
  7908. (defun org-table-current-column ()
  7909. "Find out which column we are in.
  7910. When called interactively, column is also displayed in echo area."
  7911. (interactive)
  7912. (if (interactive-p) (org-table-check-inside-data-field))
  7913. (save-excursion
  7914. (let ((cnt 0) (pos (point)))
  7915. (beginning-of-line 1)
  7916. (while (search-forward "|" pos t)
  7917. (setq cnt (1+ cnt)))
  7918. (if (interactive-p) (message "This is table column %d" cnt))
  7919. cnt)))
  7920. (defun org-table-current-dline ()
  7921. "Find out what table data line we are in.
  7922. Only datalins count for this."
  7923. (interactive)
  7924. (if (interactive-p) (org-table-check-inside-data-field))
  7925. (save-excursion
  7926. (let ((cnt 0) (pos (point)))
  7927. (goto-char (org-table-begin))
  7928. (while (<= (point) pos)
  7929. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7930. (beginning-of-line 2))
  7931. (if (interactive-p) (message "This is table line %d" cnt))
  7932. cnt)))
  7933. (defun org-table-goto-column (n &optional on-delim force)
  7934. "Move the cursor to the Nth column in the current table line.
  7935. With optional argument ON-DELIM, stop with point before the left delimiter
  7936. of the field.
  7937. If there are less than N fields, just go to after the last delimiter.
  7938. However, when FORCE is non-nil, create new columns if necessary."
  7939. (interactive "p")
  7940. (let ((pos (point-at-eol)))
  7941. (beginning-of-line 1)
  7942. (when (> n 0)
  7943. (while (and (> (setq n (1- n)) -1)
  7944. (or (search-forward "|" pos t)
  7945. (and force
  7946. (progn (end-of-line 1)
  7947. (skip-chars-backward "^|")
  7948. (insert " | "))))))
  7949. ; (backward-char 2) t)))))
  7950. (when (and force (not (looking-at ".*|")))
  7951. (save-excursion (end-of-line 1) (insert " | ")))
  7952. (if on-delim
  7953. (backward-char 1)
  7954. (if (looking-at " ") (forward-char 1))))))
  7955. (defun org-at-table-p (&optional table-type)
  7956. "Return t if the cursor is inside an org-type table.
  7957. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7958. (if org-enable-table-editor
  7959. (save-excursion
  7960. (beginning-of-line 1)
  7961. (looking-at (if table-type org-table-any-line-regexp
  7962. org-table-line-regexp)))
  7963. nil))
  7964. (defun org-at-table.el-p ()
  7965. "Return t if and only if we are at a table.el table."
  7966. (and (org-at-table-p 'any)
  7967. (save-excursion
  7968. (goto-char (org-table-begin 'any))
  7969. (looking-at org-table1-hline-regexp))))
  7970. (defun org-table-recognize-table.el ()
  7971. "If there is a table.el table nearby, recognize it and move into it."
  7972. (if org-table-tab-recognizes-table.el
  7973. (if (org-at-table.el-p)
  7974. (progn
  7975. (beginning-of-line 1)
  7976. (if (looking-at org-table-dataline-regexp)
  7977. nil
  7978. (if (looking-at org-table1-hline-regexp)
  7979. (progn
  7980. (beginning-of-line 2)
  7981. (if (looking-at org-table-any-border-regexp)
  7982. (beginning-of-line -1)))))
  7983. (if (re-search-forward "|" (org-table-end t) t)
  7984. (progn
  7985. (require 'table)
  7986. (if (table--at-cell-p (point))
  7987. t
  7988. (message "recognizing table.el table...")
  7989. (table-recognize-table)
  7990. (message "recognizing table.el table...done")))
  7991. (error "This should not happen..."))
  7992. t)
  7993. nil)
  7994. nil))
  7995. (defun org-at-table-hline-p ()
  7996. "Return t if the cursor is inside a hline in a table."
  7997. (if org-enable-table-editor
  7998. (save-excursion
  7999. (beginning-of-line 1)
  8000. (looking-at org-table-hline-regexp))
  8001. nil))
  8002. (defun org-table-insert-column ()
  8003. "Insert a new column into the table."
  8004. (interactive)
  8005. (if (not (org-at-table-p))
  8006. (error "Not at a table"))
  8007. (org-table-find-dataline)
  8008. (let* ((col (max 1 (org-table-current-column)))
  8009. (beg (org-table-begin))
  8010. (end (org-table-end))
  8011. ;; Current cursor position
  8012. (linepos (org-current-line))
  8013. (colpos col))
  8014. (goto-char beg)
  8015. (while (< (point) end)
  8016. (if (org-at-table-hline-p)
  8017. nil
  8018. (org-table-goto-column col t)
  8019. (insert "| "))
  8020. (beginning-of-line 2))
  8021. (move-marker end nil)
  8022. (goto-line linepos)
  8023. (org-table-goto-column colpos)
  8024. (org-table-align)
  8025. (org-table-fix-formulas "$" nil (1- col) 1)))
  8026. (defun org-table-find-dataline ()
  8027. "Find a dataline in the current table, which is needed for column commands."
  8028. (if (and (org-at-table-p)
  8029. (not (org-at-table-hline-p)))
  8030. t
  8031. (let ((col (current-column))
  8032. (end (org-table-end)))
  8033. (move-to-column col)
  8034. (while (and (< (point) end)
  8035. (or (not (= (current-column) col))
  8036. (org-at-table-hline-p)))
  8037. (beginning-of-line 2)
  8038. (move-to-column col))
  8039. (if (and (org-at-table-p)
  8040. (not (org-at-table-hline-p)))
  8041. t
  8042. (error
  8043. "Please position cursor in a data line for column operations")))))
  8044. (defun org-table-delete-column ()
  8045. "Delete a column from the table."
  8046. (interactive)
  8047. (if (not (org-at-table-p))
  8048. (error "Not at a table"))
  8049. (org-table-find-dataline)
  8050. (org-table-check-inside-data-field)
  8051. (let* ((col (org-table-current-column))
  8052. (beg (org-table-begin))
  8053. (end (org-table-end))
  8054. ;; Current cursor position
  8055. (linepos (org-current-line))
  8056. (colpos col))
  8057. (goto-char beg)
  8058. (while (< (point) end)
  8059. (if (org-at-table-hline-p)
  8060. nil
  8061. (org-table-goto-column col t)
  8062. (and (looking-at "|[^|\n]+|")
  8063. (replace-match "|")))
  8064. (beginning-of-line 2))
  8065. (move-marker end nil)
  8066. (goto-line linepos)
  8067. (org-table-goto-column colpos)
  8068. (org-table-align)
  8069. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8070. col -1 col)))
  8071. (defun org-table-move-column-right ()
  8072. "Move column to the right."
  8073. (interactive)
  8074. (org-table-move-column nil))
  8075. (defun org-table-move-column-left ()
  8076. "Move column to the left."
  8077. (interactive)
  8078. (org-table-move-column 'left))
  8079. (defun org-table-move-column (&optional left)
  8080. "Move the current column to the right. With arg LEFT, move to the left."
  8081. (interactive "P")
  8082. (if (not (org-at-table-p))
  8083. (error "Not at a table"))
  8084. (org-table-find-dataline)
  8085. (org-table-check-inside-data-field)
  8086. (let* ((col (org-table-current-column))
  8087. (col1 (if left (1- col) col))
  8088. (beg (org-table-begin))
  8089. (end (org-table-end))
  8090. ;; Current cursor position
  8091. (linepos (org-current-line))
  8092. (colpos (if left (1- col) (1+ col))))
  8093. (if (and left (= col 1))
  8094. (error "Cannot move column further left"))
  8095. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8096. (error "Cannot move column further right"))
  8097. (goto-char beg)
  8098. (while (< (point) end)
  8099. (if (org-at-table-hline-p)
  8100. nil
  8101. (org-table-goto-column col1 t)
  8102. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8103. (replace-match "|\\2|\\1|")))
  8104. (beginning-of-line 2))
  8105. (move-marker end nil)
  8106. (goto-line linepos)
  8107. (org-table-goto-column colpos)
  8108. (org-table-align)
  8109. (org-table-fix-formulas
  8110. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8111. (cons (number-to-string colpos) (number-to-string col))))))
  8112. (defun org-table-move-row-down ()
  8113. "Move table row down."
  8114. (interactive)
  8115. (org-table-move-row nil))
  8116. (defun org-table-move-row-up ()
  8117. "Move table row up."
  8118. (interactive)
  8119. (org-table-move-row 'up))
  8120. (defun org-table-move-row (&optional up)
  8121. "Move the current table line down. With arg UP, move it up."
  8122. (interactive "P")
  8123. (let* ((col (current-column))
  8124. (pos (point))
  8125. (hline1p (save-excursion (beginning-of-line 1)
  8126. (looking-at org-table-hline-regexp)))
  8127. (dline1 (org-table-current-dline))
  8128. (dline2 (+ dline1 (if up -1 1)))
  8129. (tonew (if up 0 2))
  8130. txt hline2p)
  8131. (beginning-of-line tonew)
  8132. (unless (org-at-table-p)
  8133. (goto-char pos)
  8134. (error "Cannot move row further"))
  8135. (setq hline2p (looking-at org-table-hline-regexp))
  8136. (goto-char pos)
  8137. (beginning-of-line 1)
  8138. (setq pos (point))
  8139. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8140. (delete-region (point) (1+ (point-at-eol)))
  8141. (beginning-of-line tonew)
  8142. (insert txt)
  8143. (beginning-of-line 0)
  8144. (move-to-column col)
  8145. (unless (or hline1p hline2p)
  8146. (org-table-fix-formulas
  8147. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8148. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8149. (defun org-table-insert-row (&optional arg)
  8150. "Insert a new row above the current line into the table.
  8151. With prefix ARG, insert below the current line."
  8152. (interactive "P")
  8153. (if (not (org-at-table-p))
  8154. (error "Not at a table"))
  8155. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8156. (new (org-table-clean-line line)))
  8157. ;; Fix the first field if necessary
  8158. (if (string-match "^[ \t]*| *[#$] *|" line)
  8159. (setq new (replace-match (match-string 0 line) t t new)))
  8160. (beginning-of-line (if arg 2 1))
  8161. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8162. (beginning-of-line 0)
  8163. (re-search-forward "| ?" (point-at-eol) t)
  8164. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8165. (org-table-align))
  8166. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8167. (defun org-table-insert-hline (&optional above)
  8168. "Insert a horizontal-line below the current line into the table.
  8169. With prefix ABOVE, insert above the current line."
  8170. (interactive "P")
  8171. (if (not (org-at-table-p))
  8172. (error "Not at a table"))
  8173. (let ((line (org-table-clean-line
  8174. (buffer-substring (point-at-bol) (point-at-eol))))
  8175. (col (current-column)))
  8176. (while (string-match "|\\( +\\)|" line)
  8177. (setq line (replace-match
  8178. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8179. ?-) "|") t t line)))
  8180. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8181. (beginning-of-line (if above 1 2))
  8182. (insert line "\n")
  8183. (beginning-of-line (if above 1 -1))
  8184. (move-to-column col)
  8185. (and org-table-overlay-coordinates (org-table-align))))
  8186. (defun org-table-hline-and-move (&optional same-column)
  8187. "Insert a hline and move to the row below that line."
  8188. (interactive "P")
  8189. (let ((col (org-table-current-column)))
  8190. (org-table-maybe-eval-formula)
  8191. (org-table-maybe-recalculate-line)
  8192. (org-table-insert-hline)
  8193. (end-of-line 2)
  8194. (if (looking-at "\n[ \t]*|-")
  8195. (progn (insert "\n|") (org-table-align))
  8196. (org-table-next-field))
  8197. (if same-column (org-table-goto-column col))))
  8198. (defun org-table-clean-line (s)
  8199. "Convert a table line S into a string with only \"|\" and space.
  8200. In particular, this does handle wide and invisible characters."
  8201. (if (string-match "^[ \t]*|-" s)
  8202. ;; It's a hline, just map the characters
  8203. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8204. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8205. (setq s (replace-match
  8206. (concat "|" (make-string (org-string-width (match-string 1 s))
  8207. ?\ ) "|")
  8208. t t s)))
  8209. s))
  8210. (defun org-table-kill-row ()
  8211. "Delete the current row or horizontal line from the table."
  8212. (interactive)
  8213. (if (not (org-at-table-p))
  8214. (error "Not at a table"))
  8215. (let ((col (current-column))
  8216. (dline (org-table-current-dline)))
  8217. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8218. (if (not (org-at-table-p)) (beginning-of-line 0))
  8219. (move-to-column col)
  8220. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8221. dline -1 dline)))
  8222. (defun org-table-sort-lines (with-case &optional sorting-type)
  8223. "Sort table lines according to the column at point.
  8224. The position of point indicates the column to be used for
  8225. sorting, and the range of lines is the range between the nearest
  8226. horizontal separator lines, or the entire table of no such lines
  8227. exist. If point is before the first column, you will be prompted
  8228. for the sorting column. If there is an active region, the mark
  8229. specifies the first line and the sorting column, while point
  8230. should be in the last line to be included into the sorting.
  8231. The command then prompts for the sorting type which can be
  8232. alphabetically, numerically, or by time (as given in a time stamp
  8233. in the field). Sorting in reverse order is also possible.
  8234. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8235. If SORTING-TYPE is specified when this function is called from a Lisp
  8236. program, no prompting will take place. SORTING-TYPE must be a character,
  8237. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8238. should be done in reverse order."
  8239. (interactive "P")
  8240. (let* ((thisline (org-current-line))
  8241. (thiscol (org-table-current-column))
  8242. beg end bcol ecol tend tbeg column lns pos)
  8243. (when (equal thiscol 0)
  8244. (if (interactive-p)
  8245. (setq thiscol
  8246. (string-to-number
  8247. (read-string "Use column N for sorting: ")))
  8248. (setq thiscol 1))
  8249. (org-table-goto-column thiscol))
  8250. (org-table-check-inside-data-field)
  8251. (if (org-region-active-p)
  8252. (progn
  8253. (setq beg (region-beginning) end (region-end))
  8254. (goto-char beg)
  8255. (setq column (org-table-current-column)
  8256. beg (point-at-bol))
  8257. (goto-char end)
  8258. (setq end (point-at-bol 2)))
  8259. (setq column (org-table-current-column)
  8260. pos (point)
  8261. tbeg (org-table-begin)
  8262. tend (org-table-end))
  8263. (if (re-search-backward org-table-hline-regexp tbeg t)
  8264. (setq beg (point-at-bol 2))
  8265. (goto-char tbeg)
  8266. (setq beg (point-at-bol 1)))
  8267. (goto-char pos)
  8268. (if (re-search-forward org-table-hline-regexp tend t)
  8269. (setq end (point-at-bol 1))
  8270. (goto-char tend)
  8271. (setq end (point-at-bol))))
  8272. (setq beg (move-marker (make-marker) beg)
  8273. end (move-marker (make-marker) end))
  8274. (untabify beg end)
  8275. (goto-char beg)
  8276. (org-table-goto-column column)
  8277. (skip-chars-backward "^|")
  8278. (setq bcol (current-column))
  8279. (org-table-goto-column (1+ column))
  8280. (skip-chars-backward "^|")
  8281. (setq ecol (1- (current-column)))
  8282. (org-table-goto-column column)
  8283. (setq lns (mapcar (lambda(x) (cons
  8284. (org-sort-remove-invisible
  8285. (nth (1- column)
  8286. (org-split-string x "[ \t]*|[ \t]*")))
  8287. x))
  8288. (org-split-string (buffer-substring beg end) "\n")))
  8289. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8290. (delete-region beg end)
  8291. (move-marker beg nil)
  8292. (move-marker end nil)
  8293. (insert (mapconcat 'cdr lns "\n") "\n")
  8294. (goto-line thisline)
  8295. (org-table-goto-column thiscol)
  8296. (message "%d lines sorted, based on column %d" (length lns) column)))
  8297. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8298. (defun org-sort-remove-invisible (s)
  8299. (remove-text-properties 0 (length s) org-rm-props s)
  8300. (while (string-match org-bracket-link-regexp s)
  8301. (setq s (replace-match (if (match-end 2)
  8302. (match-string 3 s)
  8303. (match-string 1 s)) t t s)))
  8304. s)
  8305. (defun org-table-cut-region (beg end)
  8306. "Copy region in table to the clipboard and blank all relevant fields."
  8307. (interactive "r")
  8308. (org-table-copy-region beg end 'cut))
  8309. (defun org-table-copy-region (beg end &optional cut)
  8310. "Copy rectangular region in table to clipboard.
  8311. A special clipboard is used which can only be accessed
  8312. with `org-table-paste-rectangle'."
  8313. (interactive "rP")
  8314. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8315. region cols
  8316. (rpl (if cut " " nil)))
  8317. (goto-char beg)
  8318. (org-table-check-inside-data-field)
  8319. (setq l01 (org-current-line)
  8320. c01 (org-table-current-column))
  8321. (goto-char end)
  8322. (org-table-check-inside-data-field)
  8323. (setq l02 (org-current-line)
  8324. c02 (org-table-current-column))
  8325. (setq l1 (min l01 l02) l2 (max l01 l02)
  8326. c1 (min c01 c02) c2 (max c01 c02))
  8327. (catch 'exit
  8328. (while t
  8329. (catch 'nextline
  8330. (if (> l1 l2) (throw 'exit t))
  8331. (goto-line l1)
  8332. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8333. (setq cols nil ic1 c1 ic2 c2)
  8334. (while (< ic1 (1+ ic2))
  8335. (push (org-table-get-field ic1 rpl) cols)
  8336. (setq ic1 (1+ ic1)))
  8337. (push (nreverse cols) region)
  8338. (setq l1 (1+ l1)))))
  8339. (setq org-table-clip (nreverse region))
  8340. (if cut (org-table-align))
  8341. org-table-clip))
  8342. (defun org-table-paste-rectangle ()
  8343. "Paste a rectangular region into a table.
  8344. The upper right corner ends up in the current field. All involved fields
  8345. will be overwritten. If the rectangle does not fit into the present table,
  8346. the table is enlarged as needed. The process ignores horizontal separator
  8347. lines."
  8348. (interactive)
  8349. (unless (and org-table-clip (listp org-table-clip))
  8350. (error "First cut/copy a region to paste!"))
  8351. (org-table-check-inside-data-field)
  8352. (let* ((clip org-table-clip)
  8353. (line (org-current-line))
  8354. (col (org-table-current-column))
  8355. (org-enable-table-editor t)
  8356. (org-table-automatic-realign nil)
  8357. c cols field)
  8358. (while (setq cols (pop clip))
  8359. (while (org-at-table-hline-p) (beginning-of-line 2))
  8360. (if (not (org-at-table-p))
  8361. (progn (end-of-line 0) (org-table-next-field)))
  8362. (setq c col)
  8363. (while (setq field (pop cols))
  8364. (org-table-goto-column c nil 'force)
  8365. (org-table-get-field nil field)
  8366. (setq c (1+ c)))
  8367. (beginning-of-line 2))
  8368. (goto-line line)
  8369. (org-table-goto-column col)
  8370. (org-table-align)))
  8371. (defun org-table-convert ()
  8372. "Convert from `org-mode' table to table.el and back.
  8373. Obviously, this only works within limits. When an Org-mode table is
  8374. converted to table.el, all horizontal separator lines get lost, because
  8375. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8376. A table.el table can be converted to an Org-mode table only if it does not
  8377. do row or column spanning. Multiline cells will become multiple cells.
  8378. Beware, Org-mode does not test if the table can be successfully converted - it
  8379. blindly applies a recipe that works for simple tables."
  8380. (interactive)
  8381. (require 'table)
  8382. (if (org-at-table.el-p)
  8383. ;; convert to Org-mode table
  8384. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8385. (end (move-marker (make-marker) (org-table-end t))))
  8386. (table-unrecognize-region beg end)
  8387. (goto-char beg)
  8388. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8389. (replace-match ""))
  8390. (goto-char beg))
  8391. (if (org-at-table-p)
  8392. ;; convert to table.el table
  8393. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8394. (end (move-marker (make-marker) (org-table-end))))
  8395. ;; first, get rid of all horizontal lines
  8396. (goto-char beg)
  8397. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8398. (replace-match ""))
  8399. ;; insert a hline before first
  8400. (goto-char beg)
  8401. (org-table-insert-hline 'above)
  8402. (beginning-of-line -1)
  8403. ;; insert a hline after each line
  8404. (while (progn (beginning-of-line 3) (< (point) end))
  8405. (org-table-insert-hline))
  8406. (goto-char beg)
  8407. (setq end (move-marker end (org-table-end)))
  8408. ;; replace "+" at beginning and ending of hlines
  8409. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8410. (replace-match "\\1+-"))
  8411. (goto-char beg)
  8412. (while (re-search-forward "-|[ \t]*$" end t)
  8413. (replace-match "-+"))
  8414. (goto-char beg)))))
  8415. (defun org-table-wrap-region (arg)
  8416. "Wrap several fields in a column like a paragraph.
  8417. This is useful if you'd like to spread the contents of a field over several
  8418. lines, in order to keep the table compact.
  8419. If there is an active region, and both point and mark are in the same column,
  8420. the text in the column is wrapped to minimum width for the given number of
  8421. lines. Generally, this makes the table more compact. A prefix ARG may be
  8422. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8423. formats the selected text to two lines. If the region was longer than two
  8424. lines, the remaining lines remain empty. A negative prefix argument reduces
  8425. the current number of lines by that amount. The wrapped text is pasted back
  8426. into the table. If you formatted it to more lines than it was before, fields
  8427. further down in the table get overwritten - so you might need to make space in
  8428. the table first.
  8429. If there is no region, the current field is split at the cursor position and
  8430. the text fragment to the right of the cursor is prepended to the field one
  8431. line down.
  8432. If there is no region, but you specify a prefix ARG, the current field gets
  8433. blank, and the content is appended to the field above."
  8434. (interactive "P")
  8435. (org-table-check-inside-data-field)
  8436. (if (org-region-active-p)
  8437. ;; There is a region: fill as a paragraph
  8438. (let* ((beg (region-beginning))
  8439. (cline (save-excursion (goto-char beg) (org-current-line)))
  8440. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8441. nlines)
  8442. (org-table-cut-region (region-beginning) (region-end))
  8443. (if (> (length (car org-table-clip)) 1)
  8444. (error "Region must be limited to single column"))
  8445. (setq nlines (if arg
  8446. (if (< arg 1)
  8447. (+ (length org-table-clip) arg)
  8448. arg)
  8449. (length org-table-clip)))
  8450. (setq org-table-clip
  8451. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8452. nil nlines)))
  8453. (goto-line cline)
  8454. (org-table-goto-column ccol)
  8455. (org-table-paste-rectangle))
  8456. ;; No region, split the current field at point
  8457. (if arg
  8458. ;; combine with field above
  8459. (let ((s (org-table-blank-field))
  8460. (col (org-table-current-column)))
  8461. (beginning-of-line 0)
  8462. (while (org-at-table-hline-p) (beginning-of-line 0))
  8463. (org-table-goto-column col)
  8464. (skip-chars-forward "^|")
  8465. (skip-chars-backward " ")
  8466. (insert " " (org-trim s))
  8467. (org-table-align))
  8468. ;; split field
  8469. (when (looking-at "\\([^|]+\\)+|")
  8470. (let ((s (match-string 1)))
  8471. (replace-match " |")
  8472. (goto-char (match-beginning 0))
  8473. (org-table-next-row)
  8474. (insert (org-trim s) " ")
  8475. (org-table-align))))))
  8476. (defvar org-field-marker nil)
  8477. (defun org-table-edit-field (arg)
  8478. "Edit table field in a different window.
  8479. This is mainly useful for fields that contain hidden parts.
  8480. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8481. it can be edited in place."
  8482. (interactive "P")
  8483. (if arg
  8484. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8485. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8486. (remove-text-properties b e '(org-cwidth t invisible t
  8487. display t intangible t))
  8488. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8489. (font-lock-fontify-block)))
  8490. (let ((pos (move-marker (make-marker) (point)))
  8491. (field (org-table-get-field))
  8492. (cw (current-window-configuration))
  8493. p)
  8494. (org-switch-to-buffer-other-window "*Org tmp*")
  8495. (erase-buffer)
  8496. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8497. (let ((org-inhibit-startup t)) (org-mode))
  8498. (goto-char (setq p (point-max)))
  8499. (insert (org-trim field))
  8500. (remove-text-properties p (point-max)
  8501. '(invisible t org-cwidth t display t
  8502. intangible t))
  8503. (goto-char p)
  8504. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8505. (org-set-local 'org-window-configuration cw)
  8506. (org-set-local 'org-field-marker pos)
  8507. (message "Edit and finish with C-c C-c"))))
  8508. (defun org-table-finish-edit-field ()
  8509. "Finish editing a table data field.
  8510. Remove all newline characters, insert the result into the table, realign
  8511. the table and kill the editing buffer."
  8512. (let ((pos org-field-marker)
  8513. (cw org-window-configuration)
  8514. (cb (current-buffer))
  8515. text)
  8516. (goto-char (point-min))
  8517. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8518. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8519. (replace-match " "))
  8520. (setq text (org-trim (buffer-string)))
  8521. (set-window-configuration cw)
  8522. (kill-buffer cb)
  8523. (select-window (get-buffer-window (marker-buffer pos)))
  8524. (goto-char pos)
  8525. (move-marker pos nil)
  8526. (org-table-check-inside-data-field)
  8527. (org-table-get-field nil text)
  8528. (org-table-align)
  8529. (message "New field value inserted")))
  8530. (defun org-trim (s)
  8531. "Remove whitespace at beginning and end of string."
  8532. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8533. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8534. s)
  8535. (defun org-wrap (string &optional width lines)
  8536. "Wrap string to either a number of lines, or a width in characters.
  8537. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8538. that costs. If there is a word longer than WIDTH, the text is actually
  8539. wrapped to the length of that word.
  8540. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8541. many lines, whatever width that takes.
  8542. The return value is a list of lines, without newlines at the end."
  8543. (let* ((words (org-split-string string "[ \t\n]+"))
  8544. (maxword (apply 'max (mapcar 'org-string-width words)))
  8545. w ll)
  8546. (cond (width
  8547. (org-do-wrap words (max maxword width)))
  8548. (lines
  8549. (setq w maxword)
  8550. (setq ll (org-do-wrap words maxword))
  8551. (if (<= (length ll) lines)
  8552. ll
  8553. (setq ll words)
  8554. (while (> (length ll) lines)
  8555. (setq w (1+ w))
  8556. (setq ll (org-do-wrap words w)))
  8557. ll))
  8558. (t (error "Cannot wrap this")))))
  8559. (defun org-do-wrap (words width)
  8560. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8561. (let (lines line)
  8562. (while words
  8563. (setq line (pop words))
  8564. (while (and words (< (+ (length line) (length (car words))) width))
  8565. (setq line (concat line " " (pop words))))
  8566. (setq lines (push line lines)))
  8567. (nreverse lines)))
  8568. (defun org-split-string (string &optional separators)
  8569. "Splits STRING into substrings at SEPARATORS.
  8570. No empty strings are returned if there are matches at the beginning
  8571. and end of string."
  8572. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8573. (start 0)
  8574. notfirst
  8575. (list nil))
  8576. (while (and (string-match rexp string
  8577. (if (and notfirst
  8578. (= start (match-beginning 0))
  8579. (< start (length string)))
  8580. (1+ start) start))
  8581. (< (match-beginning 0) (length string)))
  8582. (setq notfirst t)
  8583. (or (eq (match-beginning 0) 0)
  8584. (and (eq (match-beginning 0) (match-end 0))
  8585. (eq (match-beginning 0) start))
  8586. (setq list
  8587. (cons (substring string start (match-beginning 0))
  8588. list)))
  8589. (setq start (match-end 0)))
  8590. (or (eq start (length string))
  8591. (setq list
  8592. (cons (substring string start)
  8593. list)))
  8594. (nreverse list)))
  8595. (defun org-table-map-tables (function)
  8596. "Apply FUNCTION to the start of all tables in the buffer."
  8597. (save-excursion
  8598. (save-restriction
  8599. (widen)
  8600. (goto-char (point-min))
  8601. (while (re-search-forward org-table-any-line-regexp nil t)
  8602. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8603. (beginning-of-line 1)
  8604. (if (looking-at org-table-line-regexp)
  8605. (save-excursion (funcall function)))
  8606. (re-search-forward org-table-any-border-regexp nil 1))))
  8607. (message "Mapping tables: done"))
  8608. (defvar org-timecnt) ; dynamically scoped parameter
  8609. (defun org-table-sum (&optional beg end nlast)
  8610. "Sum numbers in region of current table column.
  8611. The result will be displayed in the echo area, and will be available
  8612. as kill to be inserted with \\[yank].
  8613. If there is an active region, it is interpreted as a rectangle and all
  8614. numbers in that rectangle will be summed. If there is no active
  8615. region and point is located in a table column, sum all numbers in that
  8616. column.
  8617. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8618. numbers are assumed to be times as well (in decimal hours) and the
  8619. numbers are added as such.
  8620. If NLAST is a number, only the NLAST fields will actually be summed."
  8621. (interactive)
  8622. (save-excursion
  8623. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8624. (cond
  8625. ((and beg end)) ; beg and end given explicitly
  8626. ((org-region-active-p)
  8627. (setq beg (region-beginning) end (region-end)))
  8628. (t
  8629. (setq col (org-table-current-column))
  8630. (goto-char (org-table-begin))
  8631. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8632. (error "No table data"))
  8633. (org-table-goto-column col)
  8634. (setq beg (point))
  8635. (goto-char (org-table-end))
  8636. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8637. (error "No table data"))
  8638. (org-table-goto-column col)
  8639. (setq end (point))))
  8640. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8641. (items1 (cond ((not nlast) items)
  8642. ((>= nlast (length items)) items)
  8643. (t (setq items (reverse items))
  8644. (setcdr (nthcdr (1- nlast) items) nil)
  8645. (nreverse items))))
  8646. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8647. items1)))
  8648. (res (apply '+ numbers))
  8649. (sres (if (= org-timecnt 0)
  8650. (format "%g" res)
  8651. (setq diff (* 3600 res)
  8652. h (floor (/ diff 3600)) diff (mod diff 3600)
  8653. m (floor (/ diff 60)) diff (mod diff 60)
  8654. s diff)
  8655. (format "%d:%02d:%02d" h m s))))
  8656. (kill-new sres)
  8657. (if (interactive-p)
  8658. (message "%s"
  8659. (substitute-command-keys
  8660. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8661. (length numbers) sres))))
  8662. sres))))
  8663. (defun org-table-get-number-for-summing (s)
  8664. (let (n)
  8665. (if (string-match "^ *|? *" s)
  8666. (setq s (replace-match "" nil nil s)))
  8667. (if (string-match " *|? *$" s)
  8668. (setq s (replace-match "" nil nil s)))
  8669. (setq n (string-to-number s))
  8670. (cond
  8671. ((and (string-match "0" s)
  8672. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8673. ((string-match "\\`[ \t]+\\'" s) nil)
  8674. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8675. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8676. (m (string-to-number (or (match-string 2 s) "0")))
  8677. (s (string-to-number (or (match-string 4 s) "0"))))
  8678. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8679. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8680. ((equal n 0) nil)
  8681. (t n))))
  8682. (defun org-table-current-field-formula (&optional key noerror)
  8683. "Return the formula active for the current field.
  8684. Assumes that specials are in place.
  8685. If KEY is given, return the key to this formula.
  8686. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8687. (let* ((name (car (rassoc (list (org-current-line)
  8688. (org-table-current-column))
  8689. org-table-named-field-locations)))
  8690. (col (org-table-current-column))
  8691. (scol (int-to-string col))
  8692. (ref (format "@%d$%d" (org-table-current-dline) col))
  8693. (stored-list (org-table-get-stored-formulas noerror))
  8694. (ass (or (assoc name stored-list)
  8695. (assoc ref stored-list)
  8696. (assoc scol stored-list))))
  8697. (if key
  8698. (car ass)
  8699. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8700. (cdr ass))))))
  8701. (defun org-table-get-formula (&optional equation named)
  8702. "Read a formula from the minibuffer, offer stored formula as default.
  8703. When NAMED is non-nil, look for a named equation."
  8704. (let* ((stored-list (org-table-get-stored-formulas))
  8705. (name (car (rassoc (list (org-current-line)
  8706. (org-table-current-column))
  8707. org-table-named-field-locations)))
  8708. (ref (format "@%d$%d" (org-table-current-dline)
  8709. (org-table-current-column)))
  8710. (refass (assoc ref stored-list))
  8711. (scol (if named
  8712. (if name name ref)
  8713. (int-to-string (org-table-current-column))))
  8714. (dummy (and (or name refass) (not named)
  8715. (not (y-or-n-p "Replace field formula with column formula? " ))
  8716. (error "Abort")))
  8717. (name (or name ref))
  8718. (org-table-may-need-update nil)
  8719. (stored (cdr (assoc scol stored-list)))
  8720. (eq (cond
  8721. ((and stored equation (string-match "^ *=? *$" equation))
  8722. stored)
  8723. ((stringp equation)
  8724. equation)
  8725. (t (org-table-formula-from-user
  8726. (read-string
  8727. (org-table-formula-to-user
  8728. (format "%s formula %s%s="
  8729. (if named "Field" "Column")
  8730. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8731. scol))
  8732. (if stored (org-table-formula-to-user stored) "")
  8733. 'org-table-formula-history
  8734. )))))
  8735. mustsave)
  8736. (when (not (string-match "\\S-" eq))
  8737. ;; remove formula
  8738. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8739. (org-table-store-formulas stored-list)
  8740. (error "Formula removed"))
  8741. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8742. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8743. (if (and name (not named))
  8744. ;; We set the column equation, delete the named one.
  8745. (setq stored-list (delq (assoc name stored-list) stored-list)
  8746. mustsave t))
  8747. (if stored
  8748. (setcdr (assoc scol stored-list) eq)
  8749. (setq stored-list (cons (cons scol eq) stored-list)))
  8750. (if (or mustsave (not (equal stored eq)))
  8751. (org-table-store-formulas stored-list))
  8752. eq))
  8753. (defun org-table-store-formulas (alist)
  8754. "Store the list of formulas below the current table."
  8755. (setq alist (sort alist 'org-table-formula-less-p))
  8756. (save-excursion
  8757. (goto-char (org-table-end))
  8758. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8759. (progn
  8760. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8761. (goto-char (match-beginning 2))
  8762. (delete-region (match-beginning 2) (match-end 0)))
  8763. (insert "#+TBLFM:"))
  8764. (insert " "
  8765. (mapconcat (lambda (x)
  8766. (concat
  8767. (if (equal (string-to-char (car x)) ?@) "" "$")
  8768. (car x) "=" (cdr x)))
  8769. alist "::")
  8770. "\n")))
  8771. (defsubst org-table-formula-make-cmp-string (a)
  8772. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8773. (concat
  8774. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8775. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8776. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8777. (defun org-table-formula-less-p (a b)
  8778. "Compare two formulas for sorting."
  8779. (let ((as (org-table-formula-make-cmp-string (car a)))
  8780. (bs (org-table-formula-make-cmp-string (car b))))
  8781. (and as bs (string< as bs))))
  8782. (defun org-table-get-stored-formulas (&optional noerror)
  8783. "Return an alist with the stored formulas directly after current table."
  8784. (interactive)
  8785. (let (scol eq eq-alist strings string seen)
  8786. (save-excursion
  8787. (goto-char (org-table-end))
  8788. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8789. (setq strings (org-split-string (match-string 2) " *:: *"))
  8790. (while (setq string (pop strings))
  8791. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8792. (setq scol (if (match-end 2)
  8793. (match-string 2 string)
  8794. (match-string 1 string))
  8795. eq (match-string 3 string)
  8796. eq-alist (cons (cons scol eq) eq-alist))
  8797. (if (member scol seen)
  8798. (if noerror
  8799. (progn
  8800. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8801. (ding)
  8802. (sit-for 2))
  8803. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8804. (push scol seen))))))
  8805. (nreverse eq-alist)))
  8806. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8807. "Modify the equations after the table structure has been edited.
  8808. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8809. For all numbers larger than LIMIT, shift them by DELTA."
  8810. (save-excursion
  8811. (goto-char (org-table-end))
  8812. (when (looking-at "#\\+TBLFM:")
  8813. (let ((re (concat key "\\([0-9]+\\)"))
  8814. (re2
  8815. (when remove
  8816. (if (equal key "$")
  8817. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8818. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8819. s n a)
  8820. (when remove
  8821. (while (re-search-forward re2 (point-at-eol) t)
  8822. (replace-match "")))
  8823. (while (re-search-forward re (point-at-eol) t)
  8824. (setq s (match-string 1) n (string-to-number s))
  8825. (cond
  8826. ((setq a (assoc s replace))
  8827. (replace-match (concat key (cdr a)) t t))
  8828. ((and limit (> n limit))
  8829. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8830. (defun org-table-get-specials ()
  8831. "Get the column names and local parameters for this table."
  8832. (save-excursion
  8833. (let ((beg (org-table-begin)) (end (org-table-end))
  8834. names name fields fields1 field cnt
  8835. c v l line col types dlines hlines)
  8836. (setq org-table-column-names nil
  8837. org-table-local-parameters nil
  8838. org-table-named-field-locations nil
  8839. org-table-current-begin-line nil
  8840. org-table-current-begin-pos nil
  8841. org-table-current-line-types nil)
  8842. (goto-char beg)
  8843. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8844. (setq names (org-split-string (match-string 1) " *| *")
  8845. cnt 1)
  8846. (while (setq name (pop names))
  8847. (setq cnt (1+ cnt))
  8848. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8849. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8850. (setq org-table-column-names (nreverse org-table-column-names))
  8851. (setq org-table-column-name-regexp
  8852. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8853. (goto-char beg)
  8854. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8855. (setq fields (org-split-string (match-string 1) " *| *"))
  8856. (while (setq field (pop fields))
  8857. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8858. (push (cons (match-string 1 field) (match-string 2 field))
  8859. org-table-local-parameters))))
  8860. (goto-char beg)
  8861. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8862. (setq c (match-string 1)
  8863. fields (org-split-string (match-string 2) " *| *"))
  8864. (save-excursion
  8865. (beginning-of-line (if (equal c "_") 2 0))
  8866. (setq line (org-current-line) col 1)
  8867. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8868. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8869. (while (and fields1 (setq field (pop fields)))
  8870. (setq v (pop fields1) col (1+ col))
  8871. (when (and (stringp field) (stringp v)
  8872. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8873. (push (cons field v) org-table-local-parameters)
  8874. (push (list field line col) org-table-named-field-locations))))
  8875. ;; Analyse the line types
  8876. (goto-char beg)
  8877. (setq org-table-current-begin-line (org-current-line)
  8878. org-table-current-begin-pos (point)
  8879. l org-table-current-begin-line)
  8880. (while (looking-at "[ \t]*|\\(-\\)?")
  8881. (push (if (match-end 1) 'hline 'dline) types)
  8882. (if (match-end 1) (push l hlines) (push l dlines))
  8883. (beginning-of-line 2)
  8884. (setq l (1+ l)))
  8885. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8886. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8887. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8888. (defun org-table-maybe-eval-formula ()
  8889. "Check if the current field starts with \"=\" or \":=\".
  8890. If yes, store the formula and apply it."
  8891. ;; We already know we are in a table. Get field will only return a formula
  8892. ;; when appropriate. It might return a separator line, but no problem.
  8893. (when org-table-formula-evaluate-inline
  8894. (let* ((field (org-trim (or (org-table-get-field) "")))
  8895. named eq)
  8896. (when (string-match "^:?=\\(.*\\)" field)
  8897. (setq named (equal (string-to-char field) ?:)
  8898. eq (match-string 1 field))
  8899. (if (or (fboundp 'calc-eval)
  8900. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8901. (org-table-eval-formula (if named '(4) nil)
  8902. (org-table-formula-from-user eq))
  8903. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8904. (defvar org-recalc-commands nil
  8905. "List of commands triggering the recalculation of a line.
  8906. Will be filled automatically during use.")
  8907. (defvar org-recalc-marks
  8908. '((" " . "Unmarked: no special line, no automatic recalculation")
  8909. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8910. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8911. ("!" . "Column name definition line. Reference in formula as $name.")
  8912. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8913. ("_" . "Names for values in row below this one.")
  8914. ("^" . "Names for values in row above this one.")))
  8915. (defun org-table-rotate-recalc-marks (&optional newchar)
  8916. "Rotate the recalculation mark in the first column.
  8917. If in any row, the first field is not consistent with a mark,
  8918. insert a new column for the markers.
  8919. When there is an active region, change all the lines in the region,
  8920. after prompting for the marking character.
  8921. After each change, a message will be displayed indicating the meaning
  8922. of the new mark."
  8923. (interactive)
  8924. (unless (org-at-table-p) (error "Not at a table"))
  8925. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8926. (beg (org-table-begin))
  8927. (end (org-table-end))
  8928. (l (org-current-line))
  8929. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8930. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8931. (have-col
  8932. (save-excursion
  8933. (goto-char beg)
  8934. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8935. (col (org-table-current-column))
  8936. (forcenew (car (assoc newchar org-recalc-marks)))
  8937. epos new)
  8938. (when l1
  8939. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8940. (setq newchar (char-to-string (read-char-exclusive))
  8941. forcenew (car (assoc newchar org-recalc-marks))))
  8942. (if (and newchar (not forcenew))
  8943. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8944. newchar))
  8945. (if l1 (goto-line l1))
  8946. (save-excursion
  8947. (beginning-of-line 1)
  8948. (unless (looking-at org-table-dataline-regexp)
  8949. (error "Not at a table data line")))
  8950. (unless have-col
  8951. (org-table-goto-column 1)
  8952. (org-table-insert-column)
  8953. (org-table-goto-column (1+ col)))
  8954. (setq epos (point-at-eol))
  8955. (save-excursion
  8956. (beginning-of-line 1)
  8957. (org-table-get-field
  8958. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8959. (concat " "
  8960. (setq new (or forcenew
  8961. (cadr (member (match-string 1) marks))))
  8962. " ")
  8963. " # ")))
  8964. (if (and l1 l2)
  8965. (progn
  8966. (goto-line l1)
  8967. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8968. (and (looking-at org-table-dataline-regexp)
  8969. (org-table-get-field 1 (concat " " new " "))))
  8970. (goto-line l1)))
  8971. (if (not (= epos (point-at-eol))) (org-table-align))
  8972. (goto-line l)
  8973. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  8974. (defun org-table-maybe-recalculate-line ()
  8975. "Recompute the current line if marked for it, and if we haven't just done it."
  8976. (interactive)
  8977. (and org-table-allow-automatic-line-recalculation
  8978. (not (and (memq last-command org-recalc-commands)
  8979. (equal org-last-recalc-line (org-current-line))))
  8980. (save-excursion (beginning-of-line 1)
  8981. (looking-at org-table-auto-recalculate-regexp))
  8982. (org-table-recalculate) t))
  8983. (defvar org-table-formula-debug nil
  8984. "Non-nil means, debug table formulas.
  8985. When nil, simply write \"#ERROR\" in corrupted fields.")
  8986. (make-variable-buffer-local 'org-table-formula-debug)
  8987. (defvar modes)
  8988. (defsubst org-set-calc-mode (var &optional value)
  8989. (if (stringp var)
  8990. (setq var (assoc var '(("D" calc-angle-mode deg)
  8991. ("R" calc-angle-mode rad)
  8992. ("F" calc-prefer-frac t)
  8993. ("S" calc-symbolic-mode t)))
  8994. value (nth 2 var) var (nth 1 var)))
  8995. (if (memq var modes)
  8996. (setcar (cdr (memq var modes)) value)
  8997. (cons var (cons value modes)))
  8998. modes)
  8999. (defun org-table-eval-formula (&optional arg equation
  9000. suppress-align suppress-const
  9001. suppress-store suppress-analysis)
  9002. "Replace the table field value at the cursor by the result of a calculation.
  9003. This function makes use of Dave Gillespie's Calc package, in my view the
  9004. most exciting program ever written for GNU Emacs. So you need to have Calc
  9005. installed in order to use this function.
  9006. In a table, this command replaces the value in the current field with the
  9007. result of a formula. It also installs the formula as the \"current\" column
  9008. formula, by storing it in a special line below the table. When called
  9009. with a `C-u' prefix, the current field must ba a named field, and the
  9010. formula is installed as valid in only this specific field.
  9011. When called with two `C-u' prefixes, insert the active equation
  9012. for the field back into the current field, so that it can be
  9013. edited there. This is useful in order to use \\[org-table-show-reference]
  9014. to check the referenced fields.
  9015. When called, the command first prompts for a formula, which is read in
  9016. the minibuffer. Previously entered formulas are available through the
  9017. history list, and the last used formula is offered as a default.
  9018. These stored formulas are adapted correctly when moving, inserting, or
  9019. deleting columns with the corresponding commands.
  9020. The formula can be any algebraic expression understood by the Calc package.
  9021. For details, see the Org-mode manual.
  9022. This function can also be called from Lisp programs and offers
  9023. additional arguments: EQUATION can be the formula to apply. If this
  9024. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9025. used to speed-up recursive calls by by-passing unnecessary aligns.
  9026. SUPPRESS-CONST suppresses the interpretation of constants in the
  9027. formula, assuming that this has been done already outside the function.
  9028. SUPPRESS-STORE means the formula should not be stored, either because
  9029. it is already stored, or because it is a modified equation that should
  9030. not overwrite the stored one."
  9031. (interactive "P")
  9032. (org-table-check-inside-data-field)
  9033. (or suppress-analysis (org-table-get-specials))
  9034. (if (equal arg '(16))
  9035. (let ((eq (org-table-current-field-formula)))
  9036. (or eq (error "No equation active for current field"))
  9037. (org-table-get-field nil eq)
  9038. (org-table-align)
  9039. (setq org-table-may-need-update t))
  9040. (let* (fields
  9041. (ndown (if (integerp arg) arg 1))
  9042. (org-table-automatic-realign nil)
  9043. (case-fold-search nil)
  9044. (down (> ndown 1))
  9045. (formula (if (and equation suppress-store)
  9046. equation
  9047. (org-table-get-formula equation (equal arg '(4)))))
  9048. (n0 (org-table-current-column))
  9049. (modes (copy-sequence org-calc-default-modes))
  9050. (numbers nil) ; was a variable, now fixed default
  9051. (keep-empty nil)
  9052. n form form0 bw fmt x ev orig c lispp literal)
  9053. ;; Parse the format string. Since we have a lot of modes, this is
  9054. ;; a lot of work. However, I think calc still uses most of the time.
  9055. (if (string-match ";" formula)
  9056. (let ((tmp (org-split-string formula ";")))
  9057. (setq formula (car tmp)
  9058. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9059. (nth 1 tmp)))
  9060. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9061. (setq c (string-to-char (match-string 1 fmt))
  9062. n (string-to-number (match-string 2 fmt)))
  9063. (if (= c ?p)
  9064. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9065. (setq modes (org-set-calc-mode
  9066. 'calc-float-format
  9067. (list (cdr (assoc c '((?n . float) (?f . fix)
  9068. (?s . sci) (?e . eng))))
  9069. n))))
  9070. (setq fmt (replace-match "" t t fmt)))
  9071. (if (string-match "[NT]" fmt)
  9072. (setq numbers (equal (match-string 0 fmt) "N")
  9073. fmt (replace-match "" t t fmt)))
  9074. (if (string-match "L" fmt)
  9075. (setq literal t
  9076. fmt (replace-match "" t t fmt)))
  9077. (if (string-match "E" fmt)
  9078. (setq keep-empty t
  9079. fmt (replace-match "" t t fmt)))
  9080. (while (string-match "[DRFS]" fmt)
  9081. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9082. (setq fmt (replace-match "" t t fmt)))
  9083. (unless (string-match "\\S-" fmt)
  9084. (setq fmt nil))))
  9085. (if (and (not suppress-const) org-table-formula-use-constants)
  9086. (setq formula (org-table-formula-substitute-names formula)))
  9087. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9088. (while (> ndown 0)
  9089. (setq fields (org-split-string
  9090. (org-no-properties
  9091. (buffer-substring (point-at-bol) (point-at-eol)))
  9092. " *| *"))
  9093. (if (eq numbers t)
  9094. (setq fields (mapcar
  9095. (lambda (x) (number-to-string (string-to-number x)))
  9096. fields)))
  9097. (setq ndown (1- ndown))
  9098. (setq form (copy-sequence formula)
  9099. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9100. (if (and lispp literal) (setq lispp 'literal))
  9101. ;; Check for old vertical references
  9102. (setq form (org-rewrite-old-row-references form))
  9103. ;; Insert complex ranges
  9104. (while (string-match org-table-range-regexp form)
  9105. (setq form
  9106. (replace-match
  9107. (save-match-data
  9108. (org-table-make-reference
  9109. (org-table-get-range (match-string 0 form) nil n0)
  9110. keep-empty numbers lispp))
  9111. t t form)))
  9112. ;; Insert simple ranges
  9113. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9114. (setq form
  9115. (replace-match
  9116. (save-match-data
  9117. (org-table-make-reference
  9118. (org-sublist
  9119. fields (string-to-number (match-string 1 form))
  9120. (string-to-number (match-string 2 form)))
  9121. keep-empty numbers lispp))
  9122. t t form)))
  9123. (setq form0 form)
  9124. ;; Insert the references to fields in same row
  9125. (while (string-match "\\$\\([0-9]+\\)" form)
  9126. (setq n (string-to-number (match-string 1 form))
  9127. x (nth (1- (if (= n 0) n0 n)) fields))
  9128. (unless x (error "Invalid field specifier \"%s\""
  9129. (match-string 0 form)))
  9130. (setq form (replace-match
  9131. (save-match-data
  9132. (org-table-make-reference x nil numbers lispp))
  9133. t t form)))
  9134. (if lispp
  9135. (setq ev (condition-case nil
  9136. (eval (eval (read form)))
  9137. (error "#ERROR"))
  9138. ev (if (numberp ev) (number-to-string ev) ev))
  9139. (or (fboundp 'calc-eval)
  9140. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9141. (setq ev (calc-eval (cons form modes)
  9142. (if numbers 'num))))
  9143. (when org-table-formula-debug
  9144. (with-output-to-temp-buffer "*Substitution History*"
  9145. (princ (format "Substitution history of formula
  9146. Orig: %s
  9147. $xyz-> %s
  9148. @r$c-> %s
  9149. $1-> %s\n" orig formula form0 form))
  9150. (if (listp ev)
  9151. (princ (format " %s^\nError: %s"
  9152. (make-string (car ev) ?\-) (nth 1 ev)))
  9153. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9154. ev (or fmt "NONE")
  9155. (if fmt (format fmt (string-to-number ev)) ev)))))
  9156. (setq bw (get-buffer-window "*Substitution History*"))
  9157. (shrink-window-if-larger-than-buffer bw)
  9158. (unless (and (interactive-p) (not ndown))
  9159. (unless (let (inhibit-redisplay)
  9160. (y-or-n-p "Debugging Formula. Continue to next? "))
  9161. (org-table-align)
  9162. (error "Abort"))
  9163. (delete-window bw)
  9164. (message "")))
  9165. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9166. (org-table-justify-field-maybe
  9167. (if fmt (format fmt (string-to-number ev)) ev))
  9168. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9169. (call-interactively 'org-return)
  9170. (setq ndown 0)))
  9171. (and down (org-table-maybe-recalculate-line))
  9172. (or suppress-align (and org-table-may-need-update
  9173. (org-table-align))))))
  9174. (defun org-table-put-field-property (prop value)
  9175. (save-excursion
  9176. (put-text-property (progn (skip-chars-backward "^|") (point))
  9177. (progn (skip-chars-forward "^|") (point))
  9178. prop value)))
  9179. (defun org-table-get-range (desc &optional tbeg col highlight)
  9180. "Get a calc vector from a column, accorting to descriptor DESC.
  9181. Optional arguments TBEG and COL can give the beginning of the table and
  9182. the current column, to avoid unnecessary parsing.
  9183. HIGHLIGHT means, just highlight the range."
  9184. (if (not (equal (string-to-char desc) ?@))
  9185. (setq desc (concat "@" desc)))
  9186. (save-excursion
  9187. (or tbeg (setq tbeg (org-table-begin)))
  9188. (or col (setq col (org-table-current-column)))
  9189. (let ((thisline (org-current-line))
  9190. beg end c1 c2 r1 r2 rangep tmp)
  9191. (unless (string-match org-table-range-regexp desc)
  9192. (error "Invalid table range specifier `%s'" desc))
  9193. (setq rangep (match-end 3)
  9194. r1 (and (match-end 1) (match-string 1 desc))
  9195. r2 (and (match-end 4) (match-string 4 desc))
  9196. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9197. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9198. (and c1 (setq c1 (+ (string-to-number c1)
  9199. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9200. (and c2 (setq c2 (+ (string-to-number c2)
  9201. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9202. (if (equal r1 "") (setq r1 nil))
  9203. (if (equal r2 "") (setq r2 nil))
  9204. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9205. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9206. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9207. (if (not r1) (setq r1 thisline))
  9208. (if (not r2) (setq r2 thisline))
  9209. (if (not c1) (setq c1 col))
  9210. (if (not c2) (setq c2 col))
  9211. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9212. ;; just one field
  9213. (progn
  9214. (goto-line r1)
  9215. (while (not (looking-at org-table-dataline-regexp))
  9216. (beginning-of-line 2))
  9217. (prog1 (org-trim (org-table-get-field c1))
  9218. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9219. ;; A range, return a vector
  9220. ;; First sort the numbers to get a regular ractangle
  9221. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9222. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9223. (goto-line r1)
  9224. (while (not (looking-at org-table-dataline-regexp))
  9225. (beginning-of-line 2))
  9226. (org-table-goto-column c1)
  9227. (setq beg (point))
  9228. (goto-line r2)
  9229. (while (not (looking-at org-table-dataline-regexp))
  9230. (beginning-of-line 0))
  9231. (org-table-goto-column c2)
  9232. (setq end (point))
  9233. (if highlight
  9234. (org-table-highlight-rectangle
  9235. beg (progn (skip-chars-forward "^|\n") (point))))
  9236. ;; return string representation of calc vector
  9237. (mapcar 'org-trim
  9238. (apply 'append (org-table-copy-region beg end)))))))
  9239. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9240. "Analyze descriptor DESC and retrieve the corresponding line number.
  9241. The cursor is currently in line CLINE, the table begins in line BLINE,
  9242. and TABLE is a vector with line types."
  9243. (if (string-match "^[0-9]+$" desc)
  9244. (aref org-table-dlines (string-to-number desc))
  9245. (setq cline (or cline (org-current-line))
  9246. bline (or bline org-table-current-begin-line)
  9247. table (or table org-table-current-line-types))
  9248. (if (or
  9249. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9250. ;; 1 2 3 4 5 6
  9251. (and (not (match-end 3)) (not (match-end 6)))
  9252. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9253. (error "invalid row descriptor `%s'" desc))
  9254. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9255. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9256. (odir (and (match-end 5) (match-string 5 desc)))
  9257. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9258. (i (- cline bline))
  9259. (rel (and (match-end 6)
  9260. (or (and (match-end 1) (not (match-end 3)))
  9261. (match-end 5)))))
  9262. (if (and hn (not hdir))
  9263. (progn
  9264. (setq i 0 hdir "+")
  9265. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9266. (if (and (not hn) on (not odir))
  9267. (error "should never happen");;(aref org-table-dlines on)
  9268. (if (and hn (> hn 0))
  9269. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9270. (if on
  9271. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9272. (+ bline i)))))
  9273. (defun org-find-row-type (table i type backwards relative n)
  9274. (let ((l (length table)))
  9275. (while (> n 0)
  9276. (while (and (setq i (+ i (if backwards -1 1)))
  9277. (>= i 0) (< i l)
  9278. (not (eq (aref table i) type))
  9279. (if (and relative (eq (aref table i) 'hline))
  9280. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9281. t)))
  9282. (setq n (1- n)))
  9283. (if (or (< i 0) (>= i l))
  9284. (error "Row descriptior leads outside table")
  9285. i)))
  9286. (defun org-rewrite-old-row-references (s)
  9287. (if (string-match "&[-+0-9I]" s)
  9288. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9289. s))
  9290. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9291. "Convert list ELEMENTS to something appropriate to insert into formula.
  9292. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9293. NUMBERS indicates that everything should be converted to numbers.
  9294. LISPP means to return something appropriate for a Lisp list."
  9295. (if (stringp elements) ; just a single val
  9296. (if lispp
  9297. (if (eq lispp 'literal)
  9298. elements
  9299. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9300. (if (equal elements "") (setq elements "0"))
  9301. (if numbers (number-to-string (string-to-number elements)) elements))
  9302. (unless keep-empty
  9303. (setq elements
  9304. (delq nil
  9305. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9306. elements))))
  9307. (setq elements (or elements '("0")))
  9308. (if lispp
  9309. (mapconcat
  9310. (lambda (x)
  9311. (if (eq lispp 'literal)
  9312. x
  9313. (prin1-to-string (if numbers (string-to-number x) x))))
  9314. elements " ")
  9315. (concat "[" (mapconcat
  9316. (lambda (x)
  9317. (if numbers (number-to-string (string-to-number x)) x))
  9318. elements
  9319. ",") "]"))))
  9320. (defun org-table-recalculate (&optional all noalign)
  9321. "Recalculate the current table line by applying all stored formulas.
  9322. With prefix arg ALL, do this for all lines in the table."
  9323. (interactive "P")
  9324. (or (memq this-command org-recalc-commands)
  9325. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9326. (unless (org-at-table-p) (error "Not at a table"))
  9327. (if (equal all '(16))
  9328. (org-table-iterate)
  9329. (org-table-get-specials)
  9330. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9331. (lambda (a b) (string< (car a) (car b)))))
  9332. (inhibit-redisplay (not debug-on-error))
  9333. (line-re org-table-dataline-regexp)
  9334. (thisline (org-current-line))
  9335. (thiscol (org-table-current-column))
  9336. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9337. ;; Insert constants in all formulas
  9338. (setq eqlist
  9339. (mapcar (lambda (x)
  9340. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9341. x)
  9342. eqlist))
  9343. ;; Split the equation list
  9344. (while (setq eq (pop eqlist))
  9345. (if (<= (string-to-char (car eq)) ?9)
  9346. (push eq eqlnum)
  9347. (push eq eqlname)))
  9348. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9349. (if all
  9350. (progn
  9351. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9352. (goto-char (setq beg (org-table-begin)))
  9353. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9354. ;; This is a table with marked lines, compute selected lines
  9355. (setq line-re org-table-recalculate-regexp)
  9356. ;; Move forward to the first non-header line
  9357. (if (and (re-search-forward org-table-dataline-regexp end t)
  9358. (re-search-forward org-table-hline-regexp end t)
  9359. (re-search-forward org-table-dataline-regexp end t))
  9360. (setq beg (match-beginning 0))
  9361. nil))) ;; just leave beg where it is
  9362. (setq beg (point-at-bol)
  9363. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9364. (goto-char beg)
  9365. (and all (message "Re-applying formulas to full table..."))
  9366. ;; First find the named fields, and mark them untouchanble
  9367. (remove-text-properties beg end '(org-untouchable t))
  9368. (while (setq eq (pop eqlname))
  9369. (setq name (car eq)
  9370. a (assoc name org-table-named-field-locations))
  9371. (and (not a)
  9372. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9373. (setq a (list name
  9374. (aref org-table-dlines
  9375. (string-to-number (match-string 1 name)))
  9376. (string-to-number (match-string 2 name)))))
  9377. (when (and a (or all (equal (nth 1 a) thisline)))
  9378. (message "Re-applying formula to field: %s" name)
  9379. (goto-line (nth 1 a))
  9380. (org-table-goto-column (nth 2 a))
  9381. (push (append a (list (cdr eq))) eqlname1)
  9382. (org-table-put-field-property :org-untouchable t)))
  9383. ;; Now evauluate the column formulas, but skip fields covered by
  9384. ;; field formulas
  9385. (goto-char beg)
  9386. (while (re-search-forward line-re end t)
  9387. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9388. ;; Unprotected line, recalculate
  9389. (and all (message "Re-applying formulas to full table...(line %d)"
  9390. (setq cnt (1+ cnt))))
  9391. (setq org-last-recalc-line (org-current-line))
  9392. (setq eql eqlnum)
  9393. (while (setq entry (pop eql))
  9394. (goto-line org-last-recalc-line)
  9395. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9396. (unless (get-text-property (point) :org-untouchable)
  9397. (org-table-eval-formula nil (cdr entry)
  9398. 'noalign 'nocst 'nostore 'noanalysis)))))
  9399. ;; Now evaluate the field formulas
  9400. (while (setq eq (pop eqlname1))
  9401. (message "Re-applying formula to field: %s" (car eq))
  9402. (goto-line (nth 1 eq))
  9403. (org-table-goto-column (nth 2 eq))
  9404. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9405. 'nostore 'noanalysis))
  9406. (goto-line thisline)
  9407. (org-table-goto-column thiscol)
  9408. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9409. (or noalign (and org-table-may-need-update (org-table-align))
  9410. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9411. ;; back to initial position
  9412. (message "Re-applying formulas...done")
  9413. (goto-line thisline)
  9414. (org-table-goto-column thiscol)
  9415. (or noalign (and org-table-may-need-update (org-table-align))
  9416. (and all (message "Re-applying formulas...done"))))))
  9417. (defun org-table-iterate (&optional arg)
  9418. "Recalculate the table until it does not change anymore."
  9419. (interactive "P")
  9420. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9421. (i 0)
  9422. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9423. thistbl)
  9424. (catch 'exit
  9425. (while (< i imax)
  9426. (setq i (1+ i))
  9427. (org-table-recalculate 'all)
  9428. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9429. (if (not (string= lasttbl thistbl))
  9430. (setq lasttbl thistbl)
  9431. (if (> i 1)
  9432. (message "Convergence after %d iterations" i)
  9433. (message "Table was already stable"))
  9434. (throw 'exit t)))
  9435. (error "No convergence after %d iterations" i))))
  9436. (defun org-table-formula-substitute-names (f)
  9437. "Replace $const with values in string F."
  9438. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9439. ;; First, check for column names
  9440. (while (setq start (string-match org-table-column-name-regexp f start))
  9441. (setq start (1+ start))
  9442. (setq a (assoc (match-string 1 f) org-table-column-names))
  9443. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9444. ;; Parameters and constants
  9445. (setq start 0)
  9446. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9447. (setq start (1+ start))
  9448. (if (setq a (save-match-data
  9449. (org-table-get-constant (match-string 1 f))))
  9450. (setq f (replace-match
  9451. (concat (if pp "(") a (if pp ")")) t t f))))
  9452. (if org-table-formula-debug
  9453. (put-text-property 0 (length f) :orig-formula f1 f))
  9454. f))
  9455. (defun org-table-get-constant (const)
  9456. "Find the value for a parameter or constant in a formula.
  9457. Parameters get priority."
  9458. (or (cdr (assoc const org-table-local-parameters))
  9459. (cdr (assoc const org-table-formula-constants-local))
  9460. (cdr (assoc const org-table-formula-constants))
  9461. (and (fboundp 'constants-get) (constants-get const))
  9462. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9463. (org-entry-get nil (substring const 5) 'inherit))
  9464. "#UNDEFINED_NAME"))
  9465. (defvar org-table-fedit-map
  9466. (let ((map (make-sparse-keymap)))
  9467. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9468. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9469. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9470. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9471. (org-defkey map "\C-c?" 'org-table-show-reference)
  9472. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9473. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9474. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9475. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9476. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9477. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9478. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9479. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9480. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9481. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9482. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9483. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9484. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9485. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9486. map))
  9487. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9488. '("Edit-Formulas"
  9489. ["Finish and Install" org-table-fedit-finish t]
  9490. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9491. ["Abort" org-table-fedit-abort t]
  9492. "--"
  9493. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9494. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9495. "--"
  9496. "Shift Reference at Point"
  9497. ["Up" org-table-fedit-ref-up t]
  9498. ["Down" org-table-fedit-ref-down t]
  9499. ["Left" org-table-fedit-ref-left t]
  9500. ["Right" org-table-fedit-ref-right t]
  9501. "-"
  9502. "Change Test Row for Column Formulas"
  9503. ["Up" org-table-fedit-line-up t]
  9504. ["Down" org-table-fedit-line-down t]
  9505. "--"
  9506. ["Scroll Table Window" org-table-fedit-scroll t]
  9507. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9508. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9509. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9510. org-table-overlay-coordinates)]
  9511. "--"
  9512. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9513. :style toggle :selected org-table-buffer-is-an]))
  9514. (defvar org-pos)
  9515. (defun org-table-edit-formulas ()
  9516. "Edit the formulas of the current table in a separate buffer."
  9517. (interactive)
  9518. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9519. (beginning-of-line 0))
  9520. (unless (org-at-table-p) (error "Not at a table"))
  9521. (org-table-get-specials)
  9522. (let ((key (org-table-current-field-formula 'key 'noerror))
  9523. (eql (sort (org-table-get-stored-formulas 'noerror)
  9524. 'org-table-formula-less-p))
  9525. (pos (move-marker (make-marker) (point)))
  9526. (startline 1)
  9527. (wc (current-window-configuration))
  9528. (titles '((column . "# Column Formulas\n")
  9529. (field . "# Field Formulas\n")
  9530. (named . "# Named Field Formulas\n")))
  9531. entry s type title)
  9532. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9533. (erase-buffer)
  9534. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9535. (let ((font-lock-global-modes '(not fundamental-mode)))
  9536. (fundamental-mode))
  9537. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9538. (org-set-local 'org-pos pos)
  9539. (org-set-local 'org-window-configuration wc)
  9540. (use-local-map org-table-fedit-map)
  9541. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9542. (easy-menu-add org-table-fedit-menu)
  9543. (setq startline (org-current-line))
  9544. (while (setq entry (pop eql))
  9545. (setq type (cond
  9546. ((equal (string-to-char (car entry)) ?@) 'field)
  9547. ((string-match "^[0-9]" (car entry)) 'column)
  9548. (t 'named)))
  9549. (when (setq title (assq type titles))
  9550. (or (bobp) (insert "\n"))
  9551. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9552. (setq titles (delq title titles)))
  9553. (if (equal key (car entry)) (setq startline (org-current-line)))
  9554. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9555. (car entry) " = " (cdr entry) "\n"))
  9556. (remove-text-properties 0 (length s) '(face nil) s)
  9557. (insert s))
  9558. (if (eq org-table-use-standard-references t)
  9559. (org-table-fedit-toggle-ref-type))
  9560. (goto-line startline)
  9561. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9562. (defun org-table-fedit-post-command ()
  9563. (when (not (memq this-command '(lisp-complete-symbol)))
  9564. (let ((win (selected-window)))
  9565. (save-excursion
  9566. (condition-case nil
  9567. (org-table-show-reference)
  9568. (error nil))
  9569. (select-window win)))))
  9570. (defun org-table-formula-to-user (s)
  9571. "Convert a formula from internal to user representation."
  9572. (if (eq org-table-use-standard-references t)
  9573. (org-table-convert-refs-to-an s)
  9574. s))
  9575. (defun org-table-formula-from-user (s)
  9576. "Convert a formula from user to internal representation."
  9577. (if org-table-use-standard-references
  9578. (org-table-convert-refs-to-rc s)
  9579. s))
  9580. (defun org-table-convert-refs-to-rc (s)
  9581. "Convert spreadsheet references from AB7 to @7$28.
  9582. Works for single references, but also for entire formulas and even the
  9583. full TBLFM line."
  9584. (let ((start 0))
  9585. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9586. (cond
  9587. ((match-end 3)
  9588. ;; format match, just advance
  9589. (setq start (match-end 0)))
  9590. ((and (> (match-beginning 0) 0)
  9591. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9592. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9593. ;; 3.e5 or something like this.
  9594. (setq start (match-end 0)))
  9595. (t
  9596. (setq start (match-beginning 0)
  9597. s (replace-match
  9598. (if (equal (match-string 2 s) "&")
  9599. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9600. (format "@%d$%d"
  9601. (string-to-number (match-string 2 s))
  9602. (org-letters-to-number (match-string 1 s))))
  9603. t t s)))))
  9604. s))
  9605. (defun org-table-convert-refs-to-an (s)
  9606. "Convert spreadsheet references from to @7$28 to AB7.
  9607. Works for single references, but also for entire formulas and even the
  9608. full TBLFM line."
  9609. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9610. (setq s (replace-match
  9611. (format "%s%d"
  9612. (org-number-to-letters
  9613. (string-to-number (match-string 2 s)))
  9614. (string-to-number (match-string 1 s)))
  9615. t t s)))
  9616. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9617. (setq s (replace-match (concat "\\1"
  9618. (org-number-to-letters
  9619. (string-to-number (match-string 2 s))) "&")
  9620. t nil s)))
  9621. s)
  9622. (defun org-letters-to-number (s)
  9623. "Convert a base 26 number represented by letters into an integer.
  9624. For example: AB -> 28."
  9625. (let ((n 0))
  9626. (setq s (upcase s))
  9627. (while (> (length s) 0)
  9628. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9629. s (substring s 1)))
  9630. n))
  9631. (defun org-number-to-letters (n)
  9632. "Convert an integer into a base 26 number represented by letters.
  9633. For example: 28 -> AB."
  9634. (let ((s ""))
  9635. (while (> n 0)
  9636. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9637. n (/ (1- n) 26)))
  9638. s))
  9639. (defun org-table-fedit-convert-buffer (function)
  9640. "Convert all references in this buffer, using FUNTION."
  9641. (let ((line (org-current-line)))
  9642. (goto-char (point-min))
  9643. (while (not (eobp))
  9644. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9645. (delete-region (point) (point-at-eol))
  9646. (or (eobp) (forward-char 1)))
  9647. (goto-line line)))
  9648. (defun org-table-fedit-toggle-ref-type ()
  9649. "Convert all references in the buffer from B3 to @3$2 and back."
  9650. (interactive)
  9651. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9652. (org-table-fedit-convert-buffer
  9653. (if org-table-buffer-is-an
  9654. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9655. (message "Reference type switched to %s"
  9656. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9657. (defun org-table-fedit-ref-up ()
  9658. "Shift the reference at point one row/hline up."
  9659. (interactive)
  9660. (org-table-fedit-shift-reference 'up))
  9661. (defun org-table-fedit-ref-down ()
  9662. "Shift the reference at point one row/hline down."
  9663. (interactive)
  9664. (org-table-fedit-shift-reference 'down))
  9665. (defun org-table-fedit-ref-left ()
  9666. "Shift the reference at point one field to the left."
  9667. (interactive)
  9668. (org-table-fedit-shift-reference 'left))
  9669. (defun org-table-fedit-ref-right ()
  9670. "Shift the reference at point one field to the right."
  9671. (interactive)
  9672. (org-table-fedit-shift-reference 'right))
  9673. (defun org-table-fedit-shift-reference (dir)
  9674. (cond
  9675. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9676. (if (memq dir '(left right))
  9677. (org-rematch-and-replace 1 (eq dir 'left))
  9678. (error "Cannot shift reference in this direction")))
  9679. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9680. ;; A B3-like reference
  9681. (if (memq dir '(up down))
  9682. (org-rematch-and-replace 2 (eq dir 'up))
  9683. (org-rematch-and-replace 1 (eq dir 'left))))
  9684. ((org-at-regexp-p
  9685. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9686. ;; An internal reference
  9687. (if (memq dir '(up down))
  9688. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9689. (org-rematch-and-replace 5 (eq dir 'left))))))
  9690. (defun org-rematch-and-replace (n &optional decr hline)
  9691. "Re-match the group N, and replace it with the shifted refrence."
  9692. (or (match-end n) (error "Cannot shift reference in this direction"))
  9693. (goto-char (match-beginning n))
  9694. (and (looking-at (regexp-quote (match-string n)))
  9695. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9696. t t)))
  9697. (defun org-shift-refpart (ref &optional decr hline)
  9698. "Shift a refrence part REF.
  9699. If DECR is set, decrease the references row/column, else increase.
  9700. If HLINE is set, this may be a hline reference, it certainly is not
  9701. a translation reference."
  9702. (save-match-data
  9703. (let* ((sign (string-match "^[-+]" ref)) n)
  9704. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9705. (cond
  9706. ((and hline (string-match "^I+" ref))
  9707. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9708. (setq n (+ n (if decr -1 1)))
  9709. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9710. (if sign
  9711. (setq sign (if (< n 0) "-" "+") n (abs n))
  9712. (setq n (max 1 n)))
  9713. (concat sign (make-string n ?I)))
  9714. ((string-match "^[0-9]+" ref)
  9715. (setq n (string-to-number (concat sign ref)))
  9716. (setq n (+ n (if decr -1 1)))
  9717. (if sign
  9718. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9719. (number-to-string (max 1 n))))
  9720. ((string-match "^[a-zA-Z]+" ref)
  9721. (org-number-to-letters
  9722. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9723. (t (error "Cannot shift reference"))))))
  9724. (defun org-table-fedit-toggle-coordinates ()
  9725. "Toggle the display of coordinates in the refrenced table."
  9726. (interactive)
  9727. (let ((pos (marker-position org-pos)))
  9728. (with-current-buffer (marker-buffer org-pos)
  9729. (save-excursion
  9730. (goto-char pos)
  9731. (org-table-toggle-coordinate-overlays)))))
  9732. (defun org-table-fedit-finish (&optional arg)
  9733. "Parse the buffer for formula definitions and install them.
  9734. With prefix ARG, apply the new formulas to the table."
  9735. (interactive "P")
  9736. (org-table-remove-rectangle-highlight)
  9737. (if org-table-use-standard-references
  9738. (progn
  9739. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9740. (setq org-table-buffer-is-an nil)))
  9741. (let ((pos org-pos) eql var form)
  9742. (goto-char (point-min))
  9743. (while (re-search-forward
  9744. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9745. nil t)
  9746. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9747. form (match-string 3))
  9748. (setq form (org-trim form))
  9749. (when (not (equal form ""))
  9750. (while (string-match "[ \t]*\n[ \t]*" form)
  9751. (setq form (replace-match " " t t form)))
  9752. (when (assoc var eql)
  9753. (error "Double formulas for %s" var))
  9754. (push (cons var form) eql)))
  9755. (setq org-pos nil)
  9756. (set-window-configuration org-window-configuration)
  9757. (select-window (get-buffer-window (marker-buffer pos)))
  9758. (goto-char pos)
  9759. (unless (org-at-table-p)
  9760. (error "Lost table position - cannot install formulae"))
  9761. (org-table-store-formulas eql)
  9762. (move-marker pos nil)
  9763. (kill-buffer "*Edit Formulas*")
  9764. (if arg
  9765. (org-table-recalculate 'all)
  9766. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9767. (defun org-table-fedit-abort ()
  9768. "Abort editing formulas, without installing the changes."
  9769. (interactive)
  9770. (org-table-remove-rectangle-highlight)
  9771. (let ((pos org-pos))
  9772. (set-window-configuration org-window-configuration)
  9773. (select-window (get-buffer-window (marker-buffer pos)))
  9774. (goto-char pos)
  9775. (move-marker pos nil)
  9776. (message "Formula editing aborted without installing changes")))
  9777. (defun org-table-fedit-lisp-indent ()
  9778. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9779. (interactive)
  9780. (let ((pos (point)) beg end ind)
  9781. (beginning-of-line 1)
  9782. (cond
  9783. ((looking-at "[ \t]")
  9784. (goto-char pos)
  9785. (call-interactively 'lisp-indent-line))
  9786. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9787. ((not (fboundp 'pp-buffer))
  9788. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9789. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9790. (goto-char (- (match-end 0) 2))
  9791. (setq beg (point))
  9792. (setq ind (make-string (current-column) ?\ ))
  9793. (condition-case nil (forward-sexp 1)
  9794. (error
  9795. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9796. (setq end (point))
  9797. (save-restriction
  9798. (narrow-to-region beg end)
  9799. (if (eq last-command this-command)
  9800. (progn
  9801. (goto-char (point-min))
  9802. (setq this-command nil)
  9803. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9804. (replace-match " ")))
  9805. (pp-buffer)
  9806. (untabify (point-min) (point-max))
  9807. (goto-char (1+ (point-min)))
  9808. (while (re-search-forward "^." nil t)
  9809. (beginning-of-line 1)
  9810. (insert ind))
  9811. (goto-char (point-max))
  9812. (backward-delete-char 1)))
  9813. (goto-char beg))
  9814. (t nil))))
  9815. (defvar org-show-positions nil)
  9816. (defun org-table-show-reference (&optional local)
  9817. "Show the location/value of the $ expression at point."
  9818. (interactive)
  9819. (org-table-remove-rectangle-highlight)
  9820. (catch 'exit
  9821. (let ((pos (if local (point) org-pos))
  9822. (face2 'highlight)
  9823. (org-inhibit-highlight-removal t)
  9824. (win (selected-window))
  9825. (org-show-positions nil)
  9826. var name e what match dest)
  9827. (if local (org-table-get-specials))
  9828. (setq what (cond
  9829. ((or (org-at-regexp-p org-table-range-regexp2)
  9830. (org-at-regexp-p org-table-translate-regexp)
  9831. (org-at-regexp-p org-table-range-regexp))
  9832. (setq match
  9833. (save-match-data
  9834. (org-table-convert-refs-to-rc (match-string 0))))
  9835. 'range)
  9836. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9837. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9838. ((not local) nil)
  9839. (t (error "No reference at point")))
  9840. match (and what (or match (match-string 0))))
  9841. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9842. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9843. 'secondary-selection))
  9844. (org-add-hook 'before-change-functions
  9845. 'org-table-remove-rectangle-highlight)
  9846. (if (eq what 'name) (setq var (substring match 1)))
  9847. (when (eq what 'range)
  9848. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9849. (setq match (org-table-formula-substitute-names match)))
  9850. (unless local
  9851. (save-excursion
  9852. (end-of-line 1)
  9853. (re-search-backward "^\\S-" nil t)
  9854. (beginning-of-line 1)
  9855. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9856. (setq dest
  9857. (save-match-data
  9858. (org-table-convert-refs-to-rc (match-string 1))))
  9859. (org-table-add-rectangle-overlay
  9860. (match-beginning 1) (match-end 1) face2))))
  9861. (if (and (markerp pos) (marker-buffer pos))
  9862. (if (get-buffer-window (marker-buffer pos))
  9863. (select-window (get-buffer-window (marker-buffer pos)))
  9864. (org-switch-to-buffer-other-window (get-buffer-window
  9865. (marker-buffer pos)))))
  9866. (goto-char pos)
  9867. (org-table-force-dataline)
  9868. (when dest
  9869. (setq name (substring dest 1))
  9870. (cond
  9871. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9872. (setq e (assoc name org-table-named-field-locations))
  9873. (goto-line (nth 1 e))
  9874. (org-table-goto-column (nth 2 e)))
  9875. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9876. (let ((l (string-to-number (match-string 1 dest)))
  9877. (c (string-to-number (match-string 2 dest))))
  9878. (goto-line (aref org-table-dlines l))
  9879. (org-table-goto-column c)))
  9880. (t (org-table-goto-column (string-to-number name))))
  9881. (move-marker pos (point))
  9882. (org-table-highlight-rectangle nil nil face2))
  9883. (cond
  9884. ((equal dest match))
  9885. ((not match))
  9886. ((eq what 'range)
  9887. (condition-case nil
  9888. (save-excursion
  9889. (org-table-get-range match nil nil 'highlight))
  9890. (error nil)))
  9891. ((setq e (assoc var org-table-named-field-locations))
  9892. (goto-line (nth 1 e))
  9893. (org-table-goto-column (nth 2 e))
  9894. (org-table-highlight-rectangle (point) (point))
  9895. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9896. ((setq e (assoc var org-table-column-names))
  9897. (org-table-goto-column (string-to-number (cdr e)))
  9898. (org-table-highlight-rectangle (point) (point))
  9899. (goto-char (org-table-begin))
  9900. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9901. (org-table-end) t)
  9902. (progn
  9903. (goto-char (match-beginning 1))
  9904. (org-table-highlight-rectangle)
  9905. (message "Named column (column %s)" (cdr e)))
  9906. (error "Column name not found")))
  9907. ((eq what 'column)
  9908. ;; column number
  9909. (org-table-goto-column (string-to-number (substring match 1)))
  9910. (org-table-highlight-rectangle (point) (point))
  9911. (message "Column %s" (substring match 1)))
  9912. ((setq e (assoc var org-table-local-parameters))
  9913. (goto-char (org-table-begin))
  9914. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9915. (progn
  9916. (goto-char (match-beginning 1))
  9917. (org-table-highlight-rectangle)
  9918. (message "Local parameter."))
  9919. (error "Parameter not found")))
  9920. (t
  9921. (cond
  9922. ((not var) (error "No reference at point"))
  9923. ((setq e (assoc var org-table-formula-constants-local))
  9924. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9925. var (cdr e)))
  9926. ((setq e (assoc var org-table-formula-constants))
  9927. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9928. var (cdr e)))
  9929. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9930. (message "Constant: $%s=%s, from `constants.el'%s."
  9931. var e (format " (%s units)" constants-unit-system)))
  9932. (t (error "Undefined name $%s" var)))))
  9933. (goto-char pos)
  9934. (when (and org-show-positions
  9935. (not (memq this-command '(org-table-fedit-scroll
  9936. org-table-fedit-scroll-down))))
  9937. (push pos org-show-positions)
  9938. (push org-table-current-begin-pos org-show-positions)
  9939. (let ((min (apply 'min org-show-positions))
  9940. (max (apply 'max org-show-positions)))
  9941. (goto-char min) (recenter 0)
  9942. (goto-char max)
  9943. (or (pos-visible-in-window-p max) (recenter -1))))
  9944. (select-window win))))
  9945. (defun org-table-force-dataline ()
  9946. "Make sure the cursor is in a dataline in a table."
  9947. (unless (save-excursion
  9948. (beginning-of-line 1)
  9949. (looking-at org-table-dataline-regexp))
  9950. (let* ((re org-table-dataline-regexp)
  9951. (p1 (save-excursion (re-search-forward re nil 'move)))
  9952. (p2 (save-excursion (re-search-backward re nil 'move))))
  9953. (cond ((and p1 p2)
  9954. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9955. p1 p2)))
  9956. ((or p1 p2) (goto-char (or p1 p2)))
  9957. (t (error "No table dataline around here"))))))
  9958. (defun org-table-fedit-line-up ()
  9959. "Move cursor one line up in the window showing the table."
  9960. (interactive)
  9961. (org-table-fedit-move 'previous-line))
  9962. (defun org-table-fedit-line-down ()
  9963. "Move cursor one line down in the window showing the table."
  9964. (interactive)
  9965. (org-table-fedit-move 'next-line))
  9966. (defun org-table-fedit-move (command)
  9967. "Move the cursor in the window shoinw the table.
  9968. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9969. (let ((org-table-allow-automatic-line-recalculation nil)
  9970. (pos org-pos) (win (selected-window)) p)
  9971. (select-window (get-buffer-window (marker-buffer org-pos)))
  9972. (setq p (point))
  9973. (call-interactively command)
  9974. (while (and (org-at-table-p)
  9975. (org-at-table-hline-p))
  9976. (call-interactively command))
  9977. (or (org-at-table-p) (goto-char p))
  9978. (move-marker pos (point))
  9979. (select-window win)))
  9980. (defun org-table-fedit-scroll (N)
  9981. (interactive "p")
  9982. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9983. (scroll-other-window N)))
  9984. (defun org-table-fedit-scroll-down (N)
  9985. (interactive "p")
  9986. (org-table-fedit-scroll (- N)))
  9987. (defvar org-table-rectangle-overlays nil)
  9988. (defun org-table-add-rectangle-overlay (beg end &optional face)
  9989. "Add a new overlay."
  9990. (let ((ov (org-make-overlay beg end)))
  9991. (org-overlay-put ov 'face (or face 'secondary-selection))
  9992. (push ov org-table-rectangle-overlays)))
  9993. (defun org-table-highlight-rectangle (&optional beg end face)
  9994. "Highlight rectangular region in a table."
  9995. (setq beg (or beg (point)) end (or end (point)))
  9996. (let ((b (min beg end))
  9997. (e (max beg end))
  9998. l1 c1 l2 c2 tmp)
  9999. (and (boundp 'org-show-positions)
  10000. (setq org-show-positions (cons b (cons e org-show-positions))))
  10001. (goto-char (min beg end))
  10002. (setq l1 (org-current-line)
  10003. c1 (org-table-current-column))
  10004. (goto-char (max beg end))
  10005. (setq l2 (org-current-line)
  10006. c2 (org-table-current-column))
  10007. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10008. (goto-line l1)
  10009. (beginning-of-line 1)
  10010. (loop for line from l1 to l2 do
  10011. (when (looking-at org-table-dataline-regexp)
  10012. (org-table-goto-column c1)
  10013. (skip-chars-backward "^|\n") (setq beg (point))
  10014. (org-table-goto-column c2)
  10015. (skip-chars-forward "^|\n") (setq end (point))
  10016. (org-table-add-rectangle-overlay beg end face))
  10017. (beginning-of-line 2))
  10018. (goto-char b))
  10019. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10020. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10021. "Remove the rectangle overlays."
  10022. (unless org-inhibit-highlight-removal
  10023. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10024. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10025. (setq org-table-rectangle-overlays nil)))
  10026. (defvar org-table-coordinate-overlays nil
  10027. "Collects the cooordinate grid overlays, so that they can be removed.")
  10028. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10029. (defun org-table-overlay-coordinates ()
  10030. "Add overlays to the table at point, to show row/column coordinates."
  10031. (interactive)
  10032. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10033. (setq org-table-coordinate-overlays nil)
  10034. (save-excursion
  10035. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10036. (goto-char (org-table-begin))
  10037. (while (org-at-table-p)
  10038. (setq eol (point-at-eol))
  10039. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10040. (push ov org-table-coordinate-overlays)
  10041. (setq hline (looking-at org-table-hline-regexp))
  10042. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10043. (format "%4d" (setq id (1+ id)))))
  10044. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10045. (when hline
  10046. (setq ic 0)
  10047. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10048. (setq beg (1+ (match-beginning 0))
  10049. ic (1+ ic)
  10050. s1 (concat "$" (int-to-string ic))
  10051. s2 (org-number-to-letters ic)
  10052. str (if (eq org-table-use-standard-references t) s2 s1))
  10053. (setq ov (org-make-overlay beg (+ beg (length str))))
  10054. (push ov org-table-coordinate-overlays)
  10055. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10056. (beginning-of-line 2)))))
  10057. (defun org-table-toggle-coordinate-overlays ()
  10058. "Toggle the display of Row/Column numbers in tables."
  10059. (interactive)
  10060. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10061. (message "Row/Column number display turned %s"
  10062. (if org-table-overlay-coordinates "on" "off"))
  10063. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10064. (org-table-align))
  10065. (unless org-table-overlay-coordinates
  10066. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10067. (setq org-table-coordinate-overlays nil)))
  10068. (defun org-table-toggle-formula-debugger ()
  10069. "Toggle the formula debugger in tables."
  10070. (interactive)
  10071. (setq org-table-formula-debug (not org-table-formula-debug))
  10072. (message "Formula debugging has been turned %s"
  10073. (if org-table-formula-debug "on" "off")))
  10074. ;;; The orgtbl minor mode
  10075. ;; Define a minor mode which can be used in other modes in order to
  10076. ;; integrate the org-mode table editor.
  10077. ;; This is really a hack, because the org-mode table editor uses several
  10078. ;; keys which normally belong to the major mode, for example the TAB and
  10079. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10080. ;; necessary to operate the table editor, but wraps the commands into a
  10081. ;; function which tests if the cursor is currently inside a table. If that
  10082. ;; is the case, the table editor command is executed. However, when any of
  10083. ;; those keys is used outside a table, the function uses `key-binding' to
  10084. ;; look up if the key has an associated command in another currently active
  10085. ;; keymap (minor modes, major mode, global), and executes that command.
  10086. ;; There might be problems if any of the keys used by the table editor is
  10087. ;; otherwise used as a prefix key.
  10088. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10089. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10090. ;; addresses this by checking explicitly for both bindings.
  10091. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10092. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10093. ;; Some modes bind other commands to simple characters, for example
  10094. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10095. ;; active, this binding is ignored inside tables and replaced with a
  10096. ;; modified self-insert.
  10097. (defvar orgtbl-mode nil
  10098. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10099. table editor in arbitrary modes.")
  10100. (make-variable-buffer-local 'orgtbl-mode)
  10101. (defvar orgtbl-mode-map (make-keymap)
  10102. "Keymap for `orgtbl-mode'.")
  10103. ;;;###autoload
  10104. (defun turn-on-orgtbl ()
  10105. "Unconditionally turn on `orgtbl-mode'."
  10106. (orgtbl-mode 1))
  10107. (defvar org-old-auto-fill-inhibit-regexp nil
  10108. "Local variable used by `orgtbl-mode'")
  10109. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10110. "Matches a line belonging to an orgtbl.")
  10111. (defconst orgtbl-extra-font-lock-keywords
  10112. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10113. 0 (quote 'org-table) 'prepend))
  10114. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10115. ;;;###autoload
  10116. (defun orgtbl-mode (&optional arg)
  10117. "The `org-mode' table editor as a minor mode for use in other modes."
  10118. (interactive)
  10119. (if (org-mode-p)
  10120. ;; Exit without error, in case some hook functions calls this
  10121. ;; by accident in org-mode.
  10122. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10123. (setq orgtbl-mode
  10124. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10125. (if orgtbl-mode
  10126. (progn
  10127. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10128. ;; Make sure we are first in minor-mode-map-alist
  10129. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10130. (and c (setq minor-mode-map-alist
  10131. (cons c (delq c minor-mode-map-alist)))))
  10132. (org-set-local (quote org-table-may-need-update) t)
  10133. (org-add-hook 'before-change-functions 'org-before-change-function
  10134. nil 'local)
  10135. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10136. auto-fill-inhibit-regexp)
  10137. (org-set-local 'auto-fill-inhibit-regexp
  10138. (if auto-fill-inhibit-regexp
  10139. (concat orgtbl-line-start-regexp "\\|"
  10140. auto-fill-inhibit-regexp)
  10141. orgtbl-line-start-regexp))
  10142. (org-add-to-invisibility-spec '(org-cwidth))
  10143. (when (fboundp 'font-lock-add-keywords)
  10144. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10145. (org-restart-font-lock))
  10146. (easy-menu-add orgtbl-mode-menu)
  10147. (run-hooks 'orgtbl-mode-hook))
  10148. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10149. (org-cleanup-narrow-column-properties)
  10150. (org-remove-from-invisibility-spec '(org-cwidth))
  10151. (remove-hook 'before-change-functions 'org-before-change-function t)
  10152. (when (fboundp 'font-lock-remove-keywords)
  10153. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10154. (org-restart-font-lock))
  10155. (easy-menu-remove orgtbl-mode-menu)
  10156. (force-mode-line-update 'all))))
  10157. (defun org-cleanup-narrow-column-properties ()
  10158. "Remove all properties related to narrow-column invisibility."
  10159. (let ((s 1))
  10160. (while (setq s (text-property-any s (point-max)
  10161. 'display org-narrow-column-arrow))
  10162. (remove-text-properties s (1+ s) '(display t)))
  10163. (setq s 1)
  10164. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10165. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10166. (setq s 1)
  10167. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10168. (remove-text-properties s (1+ s) '(invisible t)))))
  10169. ;; Install it as a minor mode.
  10170. (put 'orgtbl-mode :included t)
  10171. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10172. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10173. (defun orgtbl-make-binding (fun n &rest keys)
  10174. "Create a function for binding in the table minor mode.
  10175. FUN is the command to call inside a table. N is used to create a unique
  10176. command name. KEYS are keys that should be checked in for a command
  10177. to execute outside of tables."
  10178. (eval
  10179. (list 'defun
  10180. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10181. '(arg)
  10182. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10183. "Outside of tables, run the binding of `"
  10184. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10185. "'.")
  10186. '(interactive "p")
  10187. (list 'if
  10188. '(org-at-table-p)
  10189. (list 'call-interactively (list 'quote fun))
  10190. (list 'let '(orgtbl-mode)
  10191. (list 'call-interactively
  10192. (append '(or)
  10193. (mapcar (lambda (k)
  10194. (list 'key-binding k))
  10195. keys)
  10196. '('orgtbl-error))))))))
  10197. (defun orgtbl-error ()
  10198. "Error when there is no default binding for a table key."
  10199. (interactive)
  10200. (error "This key has no function outside tables"))
  10201. (defun orgtbl-setup ()
  10202. "Setup orgtbl keymaps."
  10203. (let ((nfunc 0)
  10204. (bindings
  10205. (list
  10206. '([(meta shift left)] org-table-delete-column)
  10207. '([(meta left)] org-table-move-column-left)
  10208. '([(meta right)] org-table-move-column-right)
  10209. '([(meta shift right)] org-table-insert-column)
  10210. '([(meta shift up)] org-table-kill-row)
  10211. '([(meta shift down)] org-table-insert-row)
  10212. '([(meta up)] org-table-move-row-up)
  10213. '([(meta down)] org-table-move-row-down)
  10214. '("\C-c\C-w" org-table-cut-region)
  10215. '("\C-c\M-w" org-table-copy-region)
  10216. '("\C-c\C-y" org-table-paste-rectangle)
  10217. '("\C-c-" org-table-insert-hline)
  10218. '("\C-c}" org-table-toggle-coordinate-overlays)
  10219. '("\C-c{" org-table-toggle-formula-debugger)
  10220. '("\C-m" org-table-next-row)
  10221. '([(shift return)] org-table-copy-down)
  10222. '("\C-c\C-q" org-table-wrap-region)
  10223. '("\C-c?" org-table-field-info)
  10224. '("\C-c " org-table-blank-field)
  10225. '("\C-c+" org-table-sum)
  10226. '("\C-c=" org-table-eval-formula)
  10227. '("\C-c'" org-table-edit-formulas)
  10228. '("\C-c`" org-table-edit-field)
  10229. '("\C-c*" org-table-recalculate)
  10230. '("\C-c|" org-table-create-or-convert-from-region)
  10231. '("\C-c^" org-table-sort-lines)
  10232. '([(control ?#)] org-table-rotate-recalc-marks)))
  10233. elt key fun cmd)
  10234. (while (setq elt (pop bindings))
  10235. (setq nfunc (1+ nfunc))
  10236. (setq key (org-key (car elt))
  10237. fun (nth 1 elt)
  10238. cmd (orgtbl-make-binding fun nfunc key))
  10239. (org-defkey orgtbl-mode-map key cmd))
  10240. ;; Special treatment needed for TAB and RET
  10241. (org-defkey orgtbl-mode-map [(return)]
  10242. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10243. (org-defkey orgtbl-mode-map "\C-m"
  10244. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10245. (org-defkey orgtbl-mode-map [(tab)]
  10246. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10247. (org-defkey orgtbl-mode-map "\C-i"
  10248. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10249. (org-defkey orgtbl-mode-map [(shift tab)]
  10250. (orgtbl-make-binding 'org-table-previous-field 104
  10251. [(shift tab)] [(tab)] "\C-i"))
  10252. (org-defkey orgtbl-mode-map "\M-\C-m"
  10253. (orgtbl-make-binding 'org-table-wrap-region 105
  10254. "\M-\C-m" [(meta return)]))
  10255. (org-defkey orgtbl-mode-map [(meta return)]
  10256. (orgtbl-make-binding 'org-table-wrap-region 106
  10257. [(meta return)] "\M-\C-m"))
  10258. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10259. (when orgtbl-optimized
  10260. ;; If the user wants maximum table support, we need to hijack
  10261. ;; some standard editing functions
  10262. (org-remap orgtbl-mode-map
  10263. 'self-insert-command 'orgtbl-self-insert-command
  10264. 'delete-char 'org-delete-char
  10265. 'delete-backward-char 'org-delete-backward-char)
  10266. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10267. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10268. '("OrgTbl"
  10269. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10270. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10271. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10272. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10273. "--"
  10274. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10275. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10276. ["Copy Field from Above"
  10277. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10278. "--"
  10279. ("Column"
  10280. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10281. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10282. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10283. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10284. ("Row"
  10285. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10286. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10287. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10288. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10289. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10290. "--"
  10291. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10292. ("Rectangle"
  10293. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10294. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10295. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10296. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10297. "--"
  10298. ("Radio tables"
  10299. ["Insert table template" orgtbl-insert-radio-table
  10300. (assq major-mode orgtbl-radio-table-templates)]
  10301. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10302. "--"
  10303. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10304. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10305. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10306. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10307. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10308. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10309. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10310. ["Sum Column/Rectangle" org-table-sum
  10311. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10312. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10313. ["Debug Formulas"
  10314. org-table-toggle-formula-debugger :active (org-at-table-p)
  10315. :keys "C-c {"
  10316. :style toggle :selected org-table-formula-debug]
  10317. ["Show Col/Row Numbers"
  10318. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10319. :keys "C-c }"
  10320. :style toggle :selected org-table-overlay-coordinates]
  10321. ))
  10322. t))
  10323. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10324. "If the cursor is inside a table, realign the table.
  10325. It it is a table to be sent away to a receiver, do it.
  10326. With prefix arg, also recompute table."
  10327. (interactive "P")
  10328. (let ((pos (point)) action)
  10329. (save-excursion
  10330. (beginning-of-line 1)
  10331. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10332. ((looking-at "[ \t]*|") pos)
  10333. ((looking-at "#\\+TBLFM:") 'recalc))))
  10334. (cond
  10335. ((integerp action)
  10336. (goto-char action)
  10337. (org-table-maybe-eval-formula)
  10338. (if arg
  10339. (call-interactively 'org-table-recalculate)
  10340. (org-table-maybe-recalculate-line))
  10341. (call-interactively 'org-table-align)
  10342. (orgtbl-send-table 'maybe))
  10343. ((eq action 'recalc)
  10344. (save-excursion
  10345. (beginning-of-line 1)
  10346. (skip-chars-backward " \r\n\t")
  10347. (if (org-at-table-p)
  10348. (org-call-with-arg 'org-table-recalculate t))))
  10349. (t (let (orgtbl-mode)
  10350. (call-interactively (key-binding "\C-c\C-c")))))))
  10351. (defun orgtbl-tab (arg)
  10352. "Justification and field motion for `orgtbl-mode'."
  10353. (interactive "P")
  10354. (if arg (org-table-edit-field t)
  10355. (org-table-justify-field-maybe)
  10356. (org-table-next-field)))
  10357. (defun orgtbl-ret ()
  10358. "Justification and field motion for `orgtbl-mode'."
  10359. (interactive)
  10360. (org-table-justify-field-maybe)
  10361. (org-table-next-row))
  10362. (defun orgtbl-self-insert-command (N)
  10363. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10364. If the cursor is in a table looking at whitespace, the whitespace is
  10365. overwritten, and the table is not marked as requiring realignment."
  10366. (interactive "p")
  10367. (if (and (org-at-table-p)
  10368. (or
  10369. (and org-table-auto-blank-field
  10370. (member last-command
  10371. '(orgtbl-hijacker-command-100
  10372. orgtbl-hijacker-command-101
  10373. orgtbl-hijacker-command-102
  10374. orgtbl-hijacker-command-103
  10375. orgtbl-hijacker-command-104
  10376. orgtbl-hijacker-command-105))
  10377. (org-table-blank-field))
  10378. t)
  10379. (eq N 1)
  10380. (looking-at "[^|\n]* +|"))
  10381. (let (org-table-may-need-update)
  10382. (goto-char (1- (match-end 0)))
  10383. (delete-backward-char 1)
  10384. (goto-char (match-beginning 0))
  10385. (self-insert-command N))
  10386. (setq org-table-may-need-update t)
  10387. (let (orgtbl-mode)
  10388. (call-interactively (key-binding (vector last-input-event))))))
  10389. (defun org-force-self-insert (N)
  10390. "Needed to enforce self-insert under remapping."
  10391. (interactive "p")
  10392. (self-insert-command N))
  10393. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10394. "Regula expression matching exponentials as produced by calc.")
  10395. (defvar org-table-clean-did-remove-column nil)
  10396. (defun orgtbl-export (table target)
  10397. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10398. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10399. org-table-last-alignment org-table-last-column-widths
  10400. maxcol column)
  10401. (if (not (fboundp func))
  10402. (error "Cannot export orgtbl table to %s" target))
  10403. (setq lines (org-table-clean-before-export lines))
  10404. (setq table
  10405. (mapcar
  10406. (lambda (x)
  10407. (if (string-match org-table-hline-regexp x)
  10408. 'hline
  10409. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10410. lines))
  10411. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10412. table)))
  10413. (loop for i from (1- maxcol) downto 0 do
  10414. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10415. (setq column (delq nil column))
  10416. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10417. (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))
  10418. (funcall func table nil)))
  10419. (defun orgtbl-send-table (&optional maybe)
  10420. "Send a tranformed version of this table to the receiver position.
  10421. With argument MAYBE, fail quietly if no transformation is defined for
  10422. this table."
  10423. (interactive)
  10424. (catch 'exit
  10425. (unless (org-at-table-p) (error "Not at a table"))
  10426. ;; when non-interactive, we assume align has just happened.
  10427. (when (interactive-p) (org-table-align))
  10428. (save-excursion
  10429. (goto-char (org-table-begin))
  10430. (beginning-of-line 0)
  10431. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10432. (if maybe
  10433. (throw 'exit nil)
  10434. (error "Don't know how to transform this table."))))
  10435. (let* ((name (match-string 1))
  10436. beg
  10437. (transform (intern (match-string 2)))
  10438. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10439. (skip (plist-get params :skip))
  10440. (skipcols (plist-get params :skipcols))
  10441. (txt (buffer-substring-no-properties
  10442. (org-table-begin) (org-table-end)))
  10443. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10444. (lines (org-table-clean-before-export lines))
  10445. (i0 (if org-table-clean-did-remove-column 2 1))
  10446. (table (mapcar
  10447. (lambda (x)
  10448. (if (string-match org-table-hline-regexp x)
  10449. 'hline
  10450. (org-remove-by-index
  10451. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10452. skipcols i0)))
  10453. lines))
  10454. (fun (if (= i0 2) 'cdr 'identity))
  10455. (org-table-last-alignment
  10456. (org-remove-by-index (funcall fun org-table-last-alignment)
  10457. skipcols i0))
  10458. (org-table-last-column-widths
  10459. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10460. skipcols i0)))
  10461. (unless (fboundp transform)
  10462. (error "No such transformation function %s" transform))
  10463. (setq txt (funcall transform table params))
  10464. ;; Find the insertion place
  10465. (save-excursion
  10466. (goto-char (point-min))
  10467. (unless (re-search-forward
  10468. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10469. (error "Don't know where to insert translated table"))
  10470. (goto-char (match-beginning 0))
  10471. (beginning-of-line 2)
  10472. (setq beg (point))
  10473. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10474. (error "Cannot find end of insertion region"))
  10475. (beginning-of-line 1)
  10476. (delete-region beg (point))
  10477. (goto-char beg)
  10478. (insert txt "\n"))
  10479. (message "Table converted and installed at receiver location"))))
  10480. (defun org-remove-by-index (list indices &optional i0)
  10481. "Remove the elements in LIST with indices in INDICES.
  10482. First element has index 0, or I0 if given."
  10483. (if (not indices)
  10484. list
  10485. (if (integerp indices) (setq indices (list indices)))
  10486. (setq i0 (1- (or i0 0)))
  10487. (delq :rm (mapcar (lambda (x)
  10488. (setq i0 (1+ i0))
  10489. (if (memq i0 indices) :rm x))
  10490. list))))
  10491. (defun orgtbl-toggle-comment ()
  10492. "Comment or uncomment the orgtbl at point."
  10493. (interactive)
  10494. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10495. (re2 (concat "^" orgtbl-line-start-regexp))
  10496. (commented (save-excursion (beginning-of-line 1)
  10497. (cond ((looking-at re1) t)
  10498. ((looking-at re2) nil)
  10499. (t (error "Not at an org table")))))
  10500. (re (if commented re1 re2))
  10501. beg end)
  10502. (save-excursion
  10503. (beginning-of-line 1)
  10504. (while (looking-at re) (beginning-of-line 0))
  10505. (beginning-of-line 2)
  10506. (setq beg (point))
  10507. (while (looking-at re) (beginning-of-line 2))
  10508. (setq end (point)))
  10509. (comment-region beg end (if commented '(4) nil))))
  10510. (defun orgtbl-insert-radio-table ()
  10511. "Insert a radio table template appropriate for this major mode."
  10512. (interactive)
  10513. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10514. (txt (nth 1 e))
  10515. name pos)
  10516. (unless e (error "No radio table setup defined for %s" major-mode))
  10517. (setq name (read-string "Table name: "))
  10518. (while (string-match "%n" txt)
  10519. (setq txt (replace-match name t t txt)))
  10520. (or (bolp) (insert "\n"))
  10521. (setq pos (point))
  10522. (insert txt)
  10523. (goto-char pos)))
  10524. (defun org-get-param (params header i sym &optional hsym)
  10525. "Get parameter value for symbol SYM.
  10526. If this is a header line, actually get the value for the symbol with an
  10527. additional \"h\" inserted after the colon.
  10528. If the value is a protperty list, get the element for the current column.
  10529. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10530. (let ((val (plist-get params sym)))
  10531. (and hsym header (setq val (or (plist-get params hsym) val)))
  10532. (if (consp val) (plist-get val i) val)))
  10533. (defun orgtbl-to-generic (table params)
  10534. "Convert the orgtbl-mode TABLE to some other format.
  10535. This generic routine can be used for many standard cases.
  10536. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10537. separator line, or a list of fields for that line.
  10538. PARAMS is a property list of parameters that can influence the conversion.
  10539. For the generic converter, some parameters are obligatory: You need to
  10540. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10541. :splice, you must have :tstart and :tend.
  10542. Valid parameters are
  10543. :tstart String to start the table. Ignored when :splice is t.
  10544. :tend String to end the table. Ignored when :splice is t.
  10545. :splice When set to t, return only table body lines, don't wrap
  10546. them into :tstart and :tend. Default is nil.
  10547. :hline String to be inserted on horizontal separation lines.
  10548. May be nil to ignore hlines.
  10549. :lstart String to start a new table line.
  10550. :lend String to end a table line
  10551. :sep Separator between two fields
  10552. :lfmt Format for entire line, with enough %s to capture all fields.
  10553. If this is present, :lstart, :lend, and :sep are ignored.
  10554. :fmt A format to be used to wrap the field, should contain
  10555. %s for the original field value. For example, to wrap
  10556. everything in dollars, you could use :fmt \"$%s$\".
  10557. This may also be a property list with column numbers and
  10558. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10559. :hlstart :hlend :hlsep :hlfmt :hfmt
  10560. Same as above, specific for the header lines in the table.
  10561. All lines before the first hline are treated as header.
  10562. If any of these is not present, the data line value is used.
  10563. :efmt Use this format to print numbers with exponentials.
  10564. The format should have %s twice for inserting mantissa
  10565. and exponent, for example \"%s\\\\times10^{%s}\". This
  10566. may also be a property list with column numbers and
  10567. formats. :fmt will still be applied after :efmt.
  10568. In addition to this, the parameters :skip and :skipcols are always handled
  10569. directly by `orgtbl-send-table'. See manual."
  10570. (interactive)
  10571. (let* ((p params)
  10572. (splicep (plist-get p :splice))
  10573. (hline (plist-get p :hline))
  10574. rtn line i fm efm lfmt h)
  10575. ;; Do we have a header?
  10576. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10577. (setq h t))
  10578. ;; Put header
  10579. (unless splicep
  10580. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10581. ;; Now loop over all lines
  10582. (while (setq line (pop table))
  10583. (if (eq line 'hline)
  10584. ;; A horizontal separator line
  10585. (progn (if hline (push hline rtn))
  10586. (setq h nil)) ; no longer in header
  10587. ;; A normal line. Convert the fields, push line onto the result list
  10588. (setq i 0)
  10589. (setq line
  10590. (mapcar
  10591. (lambda (f)
  10592. (setq i (1+ i)
  10593. fm (org-get-param p h i :fmt :hfmt)
  10594. efm (org-get-param p h i :efmt))
  10595. (if (and efm (string-match orgtbl-exp-regexp f))
  10596. (setq f (format
  10597. efm (match-string 1 f) (match-string 2 f))))
  10598. (if fm (setq f (format fm f)))
  10599. f)
  10600. line))
  10601. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10602. (push (apply 'format lfmt line) rtn)
  10603. (push (concat
  10604. (org-get-param p h i :lstart :hlstart)
  10605. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10606. (org-get-param p h i :lend :hlend))
  10607. rtn))))
  10608. (unless splicep
  10609. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10610. (mapconcat 'identity (nreverse rtn) "\n")))
  10611. (defun orgtbl-to-latex (table params)
  10612. "Convert the orgtbl-mode TABLE to LaTeX.
  10613. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10614. separator line, or a list of fields for that line.
  10615. PARAMS is a property list of parameters that can influence the conversion.
  10616. Supports all parameters from `orgtbl-to-generic'. Most important for
  10617. LaTeX are:
  10618. :splice When set to t, return only table body lines, don't wrap
  10619. them into a tabular environment. Default is nil.
  10620. :fmt A format to be used to wrap the field, should contain %s for the
  10621. original field value. For example, to wrap everything in dollars,
  10622. use :fmt \"$%s$\". This may also be a property list with column
  10623. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10624. :efmt Format for transforming numbers with exponentials. The format
  10625. should have %s twice for inserting mantissa and exponent, for
  10626. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10627. This may also be a property list with column numbers and formats.
  10628. The general parameters :skip and :skipcols have already been applied when
  10629. this function is called."
  10630. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10631. org-table-last-alignment ""))
  10632. (params2
  10633. (list
  10634. :tstart (concat "\\begin{tabular}{" alignment "}")
  10635. :tend "\\end{tabular}"
  10636. :lstart "" :lend " \\\\" :sep " & "
  10637. :efmt "%s\\,(%s)" :hline "\\hline")))
  10638. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10639. (defun orgtbl-to-html (table params)
  10640. "Convert the orgtbl-mode TABLE to LaTeX.
  10641. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10642. separator line, or a list of fields for that line.
  10643. PARAMS is a property list of parameters that can influence the conversion.
  10644. Currently this function recognizes the following parameters:
  10645. :splice When set to t, return only table body lines, don't wrap
  10646. them into a <table> environment. Default is nil.
  10647. The general parameters :skip and :skipcols have already been applied when
  10648. this function is called. The function does *not* use `orgtbl-to-generic',
  10649. so you cannot specify parameters for it."
  10650. (let* ((splicep (plist-get params :splice))
  10651. html)
  10652. ;; Just call the formatter we already have
  10653. ;; We need to make text lines for it, so put the fields back together.
  10654. (setq html (org-format-org-table-html
  10655. (mapcar
  10656. (lambda (x)
  10657. (if (eq x 'hline)
  10658. "|----+----|"
  10659. (concat "| " (mapconcat 'identity x " | ") " |")))
  10660. table)
  10661. splicep))
  10662. (if (string-match "\n+\\'" html)
  10663. (setq html (replace-match "" t t html)))
  10664. html))
  10665. (defun orgtbl-to-texinfo (table params)
  10666. "Convert the orgtbl-mode TABLE to TeXInfo.
  10667. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10668. separator line, or a list of fields for that line.
  10669. PARAMS is a property list of parameters that can influence the conversion.
  10670. Supports all parameters from `orgtbl-to-generic'. Most important for
  10671. TeXInfo are:
  10672. :splice nil/t When set to t, return only table body lines, don't wrap
  10673. them into a multitable environment. Default is nil.
  10674. :fmt fmt A format to be used to wrap the field, should contain
  10675. %s for the original field value. For example, to wrap
  10676. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10677. This may also be a property list with column numbers and
  10678. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10679. :cf \"f1 f2..\" The column fractions for the table. By default these
  10680. are computed automatically from the width of the columns
  10681. under org-mode.
  10682. The general parameters :skip and :skipcols have already been applied when
  10683. this function is called."
  10684. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10685. (colfrac (or (plist-get params :cf)
  10686. (mapconcat
  10687. (lambda (x) (format "%.3f" (/ (float x) total)))
  10688. org-table-last-column-widths " ")))
  10689. (params2
  10690. (list
  10691. :tstart (concat "@multitable @columnfractions " colfrac)
  10692. :tend "@end multitable"
  10693. :lstart "@item " :lend "" :sep " @tab "
  10694. :hlstart "@headitem ")))
  10695. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10696. ;;;; Link Stuff
  10697. ;;; Link abbreviations
  10698. (defun org-link-expand-abbrev (link)
  10699. "Apply replacements as defined in `org-link-abbrev-alist."
  10700. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10701. (let* ((key (match-string 1 link))
  10702. (as (or (assoc key org-link-abbrev-alist-local)
  10703. (assoc key org-link-abbrev-alist)))
  10704. (tag (and (match-end 2) (match-string 3 link)))
  10705. rpl)
  10706. (if (not as)
  10707. link
  10708. (setq rpl (cdr as))
  10709. (cond
  10710. ((symbolp rpl) (funcall rpl tag))
  10711. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10712. (t (concat rpl tag)))))
  10713. link))
  10714. ;;; Storing and inserting links
  10715. (defvar org-insert-link-history nil
  10716. "Minibuffer history for links inserted with `org-insert-link'.")
  10717. (defvar org-stored-links nil
  10718. "Contains the links stored with `org-store-link'.")
  10719. (defvar org-store-link-plist nil
  10720. "Plist with info about the most recently link created with `org-store-link'.")
  10721. (defvar org-link-protocols nil
  10722. "Link protocols added to Org-mode using `org-add-link-type'.")
  10723. (defvar org-store-link-functions nil
  10724. "List of functions that are called to create and store a link.
  10725. Each function will be called in turn until one returns a non-nil
  10726. value. Each function should check if it is responsible for creating
  10727. this link (for example by looking at the major mode).
  10728. If not, it must exit and return nil.
  10729. If yes, it should return a non-nil value after a calling
  10730. `org-store-link-props' with a list of properties and values.
  10731. Special properties are:
  10732. :type The link prefix. like \"http\". This must be given.
  10733. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10734. This is obligatory as well.
  10735. :description Optional default description for the second pair
  10736. of brackets in an Org-mode link. The user can still change
  10737. this when inserting this link into an Org-mode buffer.
  10738. In addition to these, any additional properties can be specified
  10739. and then used in remember templates.")
  10740. (defun org-add-link-type (type &optional follow publish)
  10741. "Add TYPE to the list of `org-link-types'.
  10742. Re-compute all regular expressions depending on `org-link-types'
  10743. FOLLOW and PUBLISH are two functions. Both take the link path as
  10744. an argument.
  10745. FOLLOW should do whatever is necessary to follow the link, for example
  10746. to find a file or display a mail message.
  10747. PUBLISH takes the path and retuns the string that should be used when
  10748. this document is published. FIMXE: This is actually not yet implemented."
  10749. (add-to-list 'org-link-types type t)
  10750. (org-make-link-regexps)
  10751. (add-to-list 'org-link-protocols
  10752. (list type follow publish)))
  10753. (defun org-add-agenda-custom-command (entry)
  10754. "Replace or add a command in `org-agenda-custom-commands'.
  10755. This is mostly for hacking and trying a new command - once the command
  10756. works you probably want to add it to `org-agenda-custom-commands' for good."
  10757. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10758. (if ass
  10759. (setcdr ass (cdr entry))
  10760. (push entry org-agenda-custom-commands))))
  10761. ;;;###autoload
  10762. (defun org-store-link (arg)
  10763. "\\<org-mode-map>Store an org-link to the current location.
  10764. This link can later be inserted into an org-buffer with
  10765. \\[org-insert-link].
  10766. For some link types, a prefix arg is interpreted:
  10767. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10768. For file links, arg negates `org-context-in-file-links'."
  10769. (interactive "P")
  10770. (setq org-store-link-plist nil) ; reset
  10771. (let (link cpltxt desc description search txt)
  10772. (cond
  10773. ((run-hook-with-args-until-success 'org-store-link-functions)
  10774. (setq link (plist-get org-store-link-plist :link)
  10775. desc (or (plist-get org-store-link-plist :description) link)))
  10776. ((eq major-mode 'bbdb-mode)
  10777. (let ((name (bbdb-record-name (bbdb-current-record)))
  10778. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10779. (setq cpltxt (concat "bbdb:" (or name company))
  10780. link (org-make-link cpltxt))
  10781. (org-store-link-props :type "bbdb" :name name :company company)))
  10782. ((eq major-mode 'Info-mode)
  10783. (setq link (org-make-link "info:"
  10784. (file-name-nondirectory Info-current-file)
  10785. ":" Info-current-node))
  10786. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10787. ":" Info-current-node))
  10788. (org-store-link-props :type "info" :file Info-current-file
  10789. :node Info-current-node))
  10790. ((eq major-mode 'calendar-mode)
  10791. (let ((cd (calendar-cursor-to-date)))
  10792. (setq link
  10793. (format-time-string
  10794. (car org-time-stamp-formats)
  10795. (apply 'encode-time
  10796. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10797. nil nil nil))))
  10798. (org-store-link-props :type "calendar" :date cd)))
  10799. ((or (eq major-mode 'vm-summary-mode)
  10800. (eq major-mode 'vm-presentation-mode))
  10801. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10802. (vm-follow-summary-cursor)
  10803. (save-excursion
  10804. (vm-select-folder-buffer)
  10805. (let* ((message (car vm-message-pointer))
  10806. (folder buffer-file-name)
  10807. (subject (vm-su-subject message))
  10808. (to (vm-get-header-contents message "To"))
  10809. (from (vm-get-header-contents message "From"))
  10810. (message-id (vm-su-message-id message)))
  10811. (org-store-link-props :type "vm" :from from :to to :subject subject
  10812. :message-id message-id)
  10813. (setq message-id (org-remove-angle-brackets message-id))
  10814. (setq folder (abbreviate-file-name folder))
  10815. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10816. folder)
  10817. (setq folder (replace-match "" t t folder)))
  10818. (setq cpltxt (org-email-link-description))
  10819. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10820. ((eq major-mode 'wl-summary-mode)
  10821. (let* ((msgnum (wl-summary-message-number))
  10822. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10823. msgnum 'message-id))
  10824. (wl-message-entity
  10825. (if (fboundp 'elmo-message-entity)
  10826. (elmo-message-entity
  10827. wl-summary-buffer-elmo-folder msgnum)
  10828. (elmo-msgdb-overview-get-entity
  10829. msgnum (wl-summary-buffer-msgdb))))
  10830. (from (wl-summary-line-from))
  10831. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10832. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10833. (wl-summary-line-subject))))
  10834. (org-store-link-props :type "wl" :from from :to to
  10835. :subject subject :message-id message-id)
  10836. (setq message-id (org-remove-angle-brackets message-id))
  10837. (setq cpltxt (org-email-link-description))
  10838. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10839. "#" message-id))))
  10840. ((or (equal major-mode 'mh-folder-mode)
  10841. (equal major-mode 'mh-show-mode))
  10842. (let ((from (org-mhe-get-header "From:"))
  10843. (to (org-mhe-get-header "To:"))
  10844. (message-id (org-mhe-get-header "Message-Id:"))
  10845. (subject (org-mhe-get-header "Subject:")))
  10846. (org-store-link-props :type "mh" :from from :to to
  10847. :subject subject :message-id message-id)
  10848. (setq cpltxt (org-email-link-description))
  10849. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10850. (org-remove-angle-brackets message-id)))))
  10851. ((eq major-mode 'rmail-mode)
  10852. (save-excursion
  10853. (save-restriction
  10854. (rmail-narrow-to-non-pruned-header)
  10855. (let ((folder buffer-file-name)
  10856. (message-id (mail-fetch-field "message-id"))
  10857. (from (mail-fetch-field "from"))
  10858. (to (mail-fetch-field "to"))
  10859. (subject (mail-fetch-field "subject")))
  10860. (org-store-link-props
  10861. :type "rmail" :from from :to to
  10862. :subject subject :message-id message-id)
  10863. (setq message-id (org-remove-angle-brackets message-id))
  10864. (setq cpltxt (org-email-link-description))
  10865. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10866. ((eq major-mode 'gnus-group-mode)
  10867. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10868. (gnus-group-group-name)) ; version
  10869. ((fboundp 'gnus-group-name)
  10870. (gnus-group-name))
  10871. (t "???"))))
  10872. (unless group (error "Not on a group"))
  10873. (org-store-link-props :type "gnus" :group group)
  10874. (setq cpltxt (concat
  10875. (if (org-xor arg org-usenet-links-prefer-google)
  10876. "http://groups.google.com/groups?group="
  10877. "gnus:")
  10878. group)
  10879. link (org-make-link cpltxt))))
  10880. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10881. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10882. (let* ((group gnus-newsgroup-name)
  10883. (article (gnus-summary-article-number))
  10884. (header (gnus-summary-article-header article))
  10885. (from (mail-header-from header))
  10886. (message-id (mail-header-id header))
  10887. (date (mail-header-date header))
  10888. (subject (gnus-summary-subject-string)))
  10889. (org-store-link-props :type "gnus" :from from :subject subject
  10890. :message-id message-id :group group)
  10891. (setq cpltxt (org-email-link-description))
  10892. (if (org-xor arg org-usenet-links-prefer-google)
  10893. (setq link
  10894. (concat
  10895. cpltxt "\n "
  10896. (format "http://groups.google.com/groups?as_umsgid=%s"
  10897. (org-fixup-message-id-for-http message-id))))
  10898. (setq link (org-make-link "gnus:" group
  10899. "#" (number-to-string article))))))
  10900. ((eq major-mode 'w3-mode)
  10901. (setq cpltxt (url-view-url t)
  10902. link (org-make-link cpltxt))
  10903. (org-store-link-props :type "w3" :url (url-view-url t)))
  10904. ((eq major-mode 'w3m-mode)
  10905. (setq cpltxt (or w3m-current-title w3m-current-url)
  10906. link (org-make-link w3m-current-url))
  10907. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10908. ((setq search (run-hook-with-args-until-success
  10909. 'org-create-file-search-functions))
  10910. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10911. "::" search))
  10912. (setq cpltxt (or description link)))
  10913. ((eq major-mode 'image-mode)
  10914. (setq cpltxt (concat "file:"
  10915. (abbreviate-file-name buffer-file-name))
  10916. link (org-make-link cpltxt))
  10917. (org-store-link-props :type "image" :file buffer-file-name))
  10918. ((eq major-mode 'dired-mode)
  10919. ;; link to the file in the current line
  10920. (setq cpltxt (concat "file:"
  10921. (abbreviate-file-name
  10922. (expand-file-name
  10923. (dired-get-filename nil t))))
  10924. link (org-make-link cpltxt)))
  10925. ((and buffer-file-name (org-mode-p))
  10926. ;; Just link to current headline
  10927. (setq cpltxt (concat "file:"
  10928. (abbreviate-file-name buffer-file-name)))
  10929. ;; Add a context search string
  10930. (when (org-xor org-context-in-file-links arg)
  10931. ;; Check if we are on a target
  10932. (if (org-in-regexp "<<\\(.*?\\)>>")
  10933. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10934. (setq txt (cond
  10935. ((org-on-heading-p) nil)
  10936. ((org-region-active-p)
  10937. (buffer-substring (region-beginning) (region-end)))
  10938. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10939. (when (or (null txt) (string-match "\\S-" txt))
  10940. (setq cpltxt
  10941. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10942. desc "NONE"))))
  10943. (if (string-match "::\\'" cpltxt)
  10944. (setq cpltxt (substring cpltxt 0 -2)))
  10945. (setq link (org-make-link cpltxt)))
  10946. ((buffer-file-name (buffer-base-buffer))
  10947. ;; Just link to this file here.
  10948. (setq cpltxt (concat "file:"
  10949. (abbreviate-file-name
  10950. (buffer-file-name (buffer-base-buffer)))))
  10951. ;; Add a context string
  10952. (when (org-xor org-context-in-file-links arg)
  10953. (setq txt (if (org-region-active-p)
  10954. (buffer-substring (region-beginning) (region-end))
  10955. (buffer-substring (point-at-bol) (point-at-eol))))
  10956. ;; Only use search option if there is some text.
  10957. (when (string-match "\\S-" txt)
  10958. (setq cpltxt
  10959. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10960. desc "NONE")))
  10961. (setq link (org-make-link cpltxt)))
  10962. ((interactive-p)
  10963. (error "Cannot link to a buffer which is not visiting a file"))
  10964. (t (setq link nil)))
  10965. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10966. (setq link (or link cpltxt)
  10967. desc (or desc cpltxt))
  10968. (if (equal desc "NONE") (setq desc nil))
  10969. (if (and (interactive-p) link)
  10970. (progn
  10971. (setq org-stored-links
  10972. (cons (list link desc) org-stored-links))
  10973. (message "Stored: %s" (or desc link)))
  10974. (and link (org-make-link-string link desc)))))
  10975. (defun org-store-link-props (&rest plist)
  10976. "Store link properties, extract names and addresses."
  10977. (let (x adr)
  10978. (when (setq x (plist-get plist :from))
  10979. (setq adr (mail-extract-address-components x))
  10980. (plist-put plist :fromname (car adr))
  10981. (plist-put plist :fromaddress (nth 1 adr)))
  10982. (when (setq x (plist-get plist :to))
  10983. (setq adr (mail-extract-address-components x))
  10984. (plist-put plist :toname (car adr))
  10985. (plist-put plist :toaddress (nth 1 adr))))
  10986. (let ((from (plist-get plist :from))
  10987. (to (plist-get plist :to)))
  10988. (when (and from to org-from-is-user-regexp)
  10989. (plist-put plist :fromto
  10990. (if (string-match org-from-is-user-regexp from)
  10991. (concat "to %t")
  10992. (concat "from %f")))))
  10993. (setq org-store-link-plist plist))
  10994. (defun org-email-link-description (&optional fmt)
  10995. "Return the description part of an email link.
  10996. This takes information from `org-store-link-plist' and formats it
  10997. according to FMT (default from `org-email-link-description-format')."
  10998. (setq fmt (or fmt org-email-link-description-format))
  10999. (let* ((p org-store-link-plist)
  11000. (to (plist-get p :toaddress))
  11001. (from (plist-get p :fromaddress))
  11002. (table
  11003. (list
  11004. (cons "%c" (plist-get p :fromto))
  11005. (cons "%F" (plist-get p :from))
  11006. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11007. (cons "%T" (plist-get p :to))
  11008. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11009. (cons "%s" (plist-get p :subject))
  11010. (cons "%m" (plist-get p :message-id)))))
  11011. (when (string-match "%c" fmt)
  11012. ;; Check if the user wrote this message
  11013. (if (and org-from-is-user-regexp from to
  11014. (save-match-data (string-match org-from-is-user-regexp from)))
  11015. (setq fmt (replace-match "to %t" t t fmt))
  11016. (setq fmt (replace-match "from %f" t t fmt))))
  11017. (org-replace-escapes fmt table)))
  11018. (defun org-make-org-heading-search-string (&optional string heading)
  11019. "Make search string for STRING or current headline."
  11020. (interactive)
  11021. (let ((s (or string (org-get-heading))))
  11022. (unless (and string (not heading))
  11023. ;; We are using a headline, clean up garbage in there.
  11024. (if (string-match org-todo-regexp s)
  11025. (setq s (replace-match "" t t s)))
  11026. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11027. (setq s (replace-match "" t t s)))
  11028. (setq s (org-trim s))
  11029. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11030. org-comment-string "\\)") s)
  11031. (setq s (replace-match "" t t s)))
  11032. (while (string-match org-ts-regexp s)
  11033. (setq s (replace-match "" t t s))))
  11034. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11035. (setq s (replace-match " " t t s)))
  11036. (or string (setq s (concat "*" s))) ; Add * for headlines
  11037. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11038. (defun org-make-link (&rest strings)
  11039. "Concatenate STRINGS."
  11040. (apply 'concat strings))
  11041. (defun org-make-link-string (link &optional description)
  11042. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11043. (unless (string-match "\\S-" link)
  11044. (error "Empty link"))
  11045. (when (stringp description)
  11046. ;; Remove brackets from the description, they are fatal.
  11047. (while (string-match "\\[" description)
  11048. (setq description (replace-match "{" t t description)))
  11049. (while (string-match "\\]" description)
  11050. (setq description (replace-match "}" t t description))))
  11051. (when (equal (org-link-escape link) description)
  11052. ;; No description needed, it is identical
  11053. (setq description nil))
  11054. (when (and (not description)
  11055. (not (equal link (org-link-escape link))))
  11056. (setq description link))
  11057. (concat "[[" (org-link-escape link) "]"
  11058. (if description (concat "[" description "]") "")
  11059. "]"))
  11060. (defconst org-link-escape-chars
  11061. '((?\ . "%20")
  11062. (?\[ . "%5B")
  11063. (?\] . "%5D")
  11064. (?\340 . "%E0") ; `a
  11065. (?\342 . "%E2") ; ^a
  11066. (?\347 . "%E7") ; ,c
  11067. (?\350 . "%E8") ; `e
  11068. (?\351 . "%E9") ; 'e
  11069. (?\352 . "%EA") ; ^e
  11070. (?\356 . "%EE") ; ^i
  11071. (?\364 . "%F4") ; ^o
  11072. (?\371 . "%F9") ; `u
  11073. (?\373 . "%FB") ; ^u
  11074. (?\; . "%3B")
  11075. (?? . "%3F")
  11076. (?= . "%3D")
  11077. (?+ . "%2B")
  11078. )
  11079. "Association list of escapes for some characters problematic in links.
  11080. This is the list that is used for internal purposes.")
  11081. (defconst org-link-escape-chars-browser
  11082. '((?\ . "%20")) ; 32 for the SPC char
  11083. "Association list of escapes for some characters problematic in links.
  11084. This is the list that is used before handing over to the browser.")
  11085. (defun org-link-escape (text &optional table)
  11086. "Escape charaters in TEXT that are problematic for links."
  11087. (setq table (or table org-link-escape-chars))
  11088. (when text
  11089. (let ((re (mapconcat (lambda (x) (regexp-quote
  11090. (char-to-string (car x))))
  11091. table "\\|")))
  11092. (while (string-match re text)
  11093. (setq text
  11094. (replace-match
  11095. (cdr (assoc (string-to-char (match-string 0 text))
  11096. table))
  11097. t t text)))
  11098. text)))
  11099. (defun org-link-unescape (text &optional table)
  11100. "Reverse the action of `org-link-escape'."
  11101. (setq table (or table org-link-escape-chars))
  11102. (when text
  11103. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11104. table "\\|")))
  11105. (while (string-match re text)
  11106. (setq text
  11107. (replace-match
  11108. (char-to-string (car (rassoc (match-string 0 text) table)))
  11109. t t text)))
  11110. text)))
  11111. (defun org-xor (a b)
  11112. "Exclusive or."
  11113. (if a (not b) b))
  11114. (defun org-get-header (header)
  11115. "Find a header field in the current buffer."
  11116. (save-excursion
  11117. (goto-char (point-min))
  11118. (let ((case-fold-search t) s)
  11119. (cond
  11120. ((eq header 'from)
  11121. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11122. (setq s (match-string 1)))
  11123. (while (string-match "\"" s)
  11124. (setq s (replace-match "" t t s)))
  11125. (if (string-match "[<(].*" s)
  11126. (setq s (replace-match "" t t s))))
  11127. ((eq header 'message-id)
  11128. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11129. (setq s (match-string 1))))
  11130. ((eq header 'subject)
  11131. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11132. (setq s (match-string 1)))))
  11133. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11134. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11135. s)))
  11136. (defun org-fixup-message-id-for-http (s)
  11137. "Replace special characters in a message id, so it can be used in an http query."
  11138. (while (string-match "<" s)
  11139. (setq s (replace-match "%3C" t t s)))
  11140. (while (string-match ">" s)
  11141. (setq s (replace-match "%3E" t t s)))
  11142. (while (string-match "@" s)
  11143. (setq s (replace-match "%40" t t s)))
  11144. s)
  11145. ;;;###autoload
  11146. (defun org-insert-link-global ()
  11147. "Insert a link like Org-mode does.
  11148. This command can be called in any mode to insert a link in Org-mode syntax."
  11149. (interactive)
  11150. (org-run-like-in-org-mode 'org-insert-link))
  11151. (defun org-insert-link (&optional complete-file)
  11152. "Insert a link. At the prompt, enter the link.
  11153. Completion can be used to select a link previously stored with
  11154. `org-store-link'. When the empty string is entered (i.e. if you just
  11155. press RET at the prompt), the link defaults to the most recently
  11156. stored link. As SPC triggers completion in the minibuffer, you need to
  11157. use M-SPC or C-q SPC to force the insertion of a space character.
  11158. You will also be prompted for a description, and if one is given, it will
  11159. be displayed in the buffer instead of the link.
  11160. If there is already a link at point, this command will allow you to edit link
  11161. and description parts.
  11162. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11163. selected using completion. The path to the file will be relative to
  11164. the current directory if the file is in the current directory or a
  11165. subdirectory. Otherwise, the link will be the absolute path as
  11166. completed in the minibuffer (i.e. normally ~/path/to/file).
  11167. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11168. is in the current directory or below.
  11169. With three \\[universal-argument] prefixes, negate the meaning of
  11170. `org-keep-stored-link-after-insertion'."
  11171. (interactive "P")
  11172. (let* ((wcf (current-window-configuration))
  11173. (region (if (org-region-active-p)
  11174. (buffer-substring (region-beginning) (region-end))))
  11175. (remove (and region (list (region-beginning) (region-end))))
  11176. (desc region)
  11177. tmphist ; byte-compile incorrectly complains about this
  11178. link entry file)
  11179. (cond
  11180. ((org-in-regexp org-bracket-link-regexp 1)
  11181. ;; We do have a link at point, and we are going to edit it.
  11182. (setq remove (list (match-beginning 0) (match-end 0)))
  11183. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11184. (setq link (read-string "Link: "
  11185. (org-link-unescape
  11186. (org-match-string-no-properties 1)))))
  11187. ((or (org-in-regexp org-angle-link-re)
  11188. (org-in-regexp org-plain-link-re))
  11189. ;; Convert to bracket link
  11190. (setq remove (list (match-beginning 0) (match-end 0))
  11191. link (read-string "Link: "
  11192. (org-remove-angle-brackets (match-string 0)))))
  11193. ((equal complete-file '(4))
  11194. ;; Completing read for file names.
  11195. (setq file (read-file-name "File: "))
  11196. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11197. (pwd1 (file-name-as-directory (abbreviate-file-name
  11198. (expand-file-name ".")))))
  11199. (cond
  11200. ((equal complete-file '(16))
  11201. (setq link (org-make-link
  11202. "file:"
  11203. (abbreviate-file-name (expand-file-name file)))))
  11204. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11205. (setq link (org-make-link "file:" (match-string 1 file))))
  11206. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11207. (expand-file-name file))
  11208. (setq link (org-make-link
  11209. "file:" (match-string 1 (expand-file-name file)))))
  11210. (t (setq link (org-make-link "file:" file))))))
  11211. (t
  11212. ;; Read link, with completion for stored links.
  11213. (with-output-to-temp-buffer "*Org Links*"
  11214. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11215. (when org-stored-links
  11216. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11217. (princ (mapconcat
  11218. (lambda (x)
  11219. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11220. (reverse org-stored-links) "\n"))))
  11221. (let ((cw (selected-window)))
  11222. (select-window (get-buffer-window "*Org Links*"))
  11223. (shrink-window-if-larger-than-buffer)
  11224. (setq truncate-lines t)
  11225. (select-window cw))
  11226. ;; Fake a link history, containing the stored links.
  11227. (setq tmphist (append (mapcar 'car org-stored-links)
  11228. org-insert-link-history))
  11229. (unwind-protect
  11230. (setq link (org-completing-read
  11231. "Link: "
  11232. (append
  11233. (mapcar (lambda (x) (list (concat (car x) ":")))
  11234. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11235. (mapcar (lambda (x) (list (concat x ":")))
  11236. org-link-types))
  11237. nil nil nil
  11238. 'tmphist
  11239. (or (car (car org-stored-links)))))
  11240. (set-window-configuration wcf)
  11241. (kill-buffer "*Org Links*"))
  11242. (setq entry (assoc link org-stored-links))
  11243. (or entry (push link org-insert-link-history))
  11244. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11245. (not org-keep-stored-link-after-insertion))
  11246. (setq org-stored-links (delq (assoc link org-stored-links)
  11247. org-stored-links)))
  11248. (setq desc (or desc (nth 1 entry)))))
  11249. (if (string-match org-plain-link-re link)
  11250. ;; URL-like link, normalize the use of angular brackets.
  11251. (setq link (org-make-link (org-remove-angle-brackets link))))
  11252. ;; Check if we are linking to the current file with a search option
  11253. ;; If yes, simplify the link by using only the search option.
  11254. (when (and buffer-file-name
  11255. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11256. (let* ((path (match-string 1 link))
  11257. (case-fold-search nil)
  11258. (search (match-string 2 link)))
  11259. (save-match-data
  11260. (if (equal (file-truename buffer-file-name) (file-truename path))
  11261. ;; We are linking to this same file, with a search option
  11262. (setq link search)))))
  11263. ;; Check if we can/should use a relative path. If yes, simplify the link
  11264. (when (string-match "\\<file:\\(.*\\)" link)
  11265. (let* ((path (match-string 1 link))
  11266. (origpath path)
  11267. (desc-is-link (equal link desc))
  11268. (case-fold-search nil))
  11269. (cond
  11270. ((eq org-link-file-path-type 'absolute)
  11271. (setq path (abbreviate-file-name (expand-file-name path))))
  11272. ((eq org-link-file-path-type 'noabbrev)
  11273. (setq path (expand-file-name path)))
  11274. ((eq org-link-file-path-type 'relative)
  11275. (setq path (file-relative-name path)))
  11276. (t
  11277. (save-match-data
  11278. (if (string-match (concat "^" (regexp-quote
  11279. (file-name-as-directory
  11280. (expand-file-name "."))))
  11281. (expand-file-name path))
  11282. ;; We are linking a file with relative path name.
  11283. (setq path (substring (expand-file-name path)
  11284. (match-end 0)))))))
  11285. (setq link (concat "file:" path))
  11286. (if (equal desc origpath)
  11287. (setq desc path))))
  11288. (setq desc (read-string "Description: " desc))
  11289. (unless (string-match "\\S-" desc) (setq desc nil))
  11290. (if remove (apply 'delete-region remove))
  11291. (insert (org-make-link-string link desc))))
  11292. (defun org-completing-read (&rest args)
  11293. (let ((minibuffer-local-completion-map
  11294. (copy-keymap minibuffer-local-completion-map)))
  11295. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11296. (apply 'completing-read args)))
  11297. ;;; Opening/following a link
  11298. (defvar org-link-search-failed nil)
  11299. (defun org-next-link ()
  11300. "Move forward to the next link.
  11301. If the link is in hidden text, expose it."
  11302. (interactive)
  11303. (when (and org-link-search-failed (eq this-command last-command))
  11304. (goto-char (point-min))
  11305. (message "Link search wrapped back to beginning of buffer"))
  11306. (setq org-link-search-failed nil)
  11307. (let* ((pos (point))
  11308. (ct (org-context))
  11309. (a (assoc :link ct)))
  11310. (if a (goto-char (nth 2 a)))
  11311. (if (re-search-forward org-any-link-re nil t)
  11312. (progn
  11313. (goto-char (match-beginning 0))
  11314. (if (org-invisible-p) (org-show-context)))
  11315. (goto-char pos)
  11316. (setq org-link-search-failed t)
  11317. (error "No further link found"))))
  11318. (defun org-previous-link ()
  11319. "Move backward to the previous link.
  11320. If the link is in hidden text, expose it."
  11321. (interactive)
  11322. (when (and org-link-search-failed (eq this-command last-command))
  11323. (goto-char (point-max))
  11324. (message "Link search wrapped back to end of buffer"))
  11325. (setq org-link-search-failed nil)
  11326. (let* ((pos (point))
  11327. (ct (org-context))
  11328. (a (assoc :link ct)))
  11329. (if a (goto-char (nth 1 a)))
  11330. (if (re-search-backward org-any-link-re nil t)
  11331. (progn
  11332. (goto-char (match-beginning 0))
  11333. (if (org-invisible-p) (org-show-context)))
  11334. (goto-char pos)
  11335. (setq org-link-search-failed t)
  11336. (error "No further link found"))))
  11337. (defun org-find-file-at-mouse (ev)
  11338. "Open file link or URL at mouse."
  11339. (interactive "e")
  11340. (mouse-set-point ev)
  11341. (org-open-at-point 'in-emacs))
  11342. (defun org-open-at-mouse (ev)
  11343. "Open file link or URL at mouse."
  11344. (interactive "e")
  11345. (mouse-set-point ev)
  11346. (org-open-at-point))
  11347. (defvar org-window-config-before-follow-link nil
  11348. "The window configuration before following a link.
  11349. This is saved in case the need arises to restore it.")
  11350. (defvar org-open-link-marker (make-marker)
  11351. "Marker pointing to the location where `org-open-at-point; was called.")
  11352. ;;;###autoload
  11353. (defun org-open-at-point-global ()
  11354. "Follow a link like Org-mode does.
  11355. This command can be called in any mode to follow a link that has
  11356. Org-mode syntax."
  11357. (interactive)
  11358. (org-run-like-in-org-mode 'org-open-at-point))
  11359. (defun org-open-at-point (&optional in-emacs)
  11360. "Open link at or after point.
  11361. If there is no link at point, this function will search forward up to
  11362. the end of the current subtree.
  11363. Normally, files will be opened by an appropriate application. If the
  11364. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11365. (interactive "P")
  11366. (catch 'abort
  11367. (move-marker org-open-link-marker (point))
  11368. (setq org-window-config-before-follow-link (current-window-configuration))
  11369. (org-remove-occur-highlights nil nil t)
  11370. (if (org-at-timestamp-p t)
  11371. (org-follow-timestamp-link)
  11372. (let (type path link line search (pos (point)))
  11373. (catch 'match
  11374. (save-excursion
  11375. (skip-chars-forward "^]\n\r")
  11376. (when (org-in-regexp org-bracket-link-regexp)
  11377. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11378. (while (string-match " *\n *" link)
  11379. (setq link (replace-match " " t t link)))
  11380. (setq link (org-link-expand-abbrev link))
  11381. (if (string-match org-link-re-with-space2 link)
  11382. (setq type (match-string 1 link) path (match-string 2 link))
  11383. (setq type "thisfile" path link))
  11384. (throw 'match t)))
  11385. (when (get-text-property (point) 'org-linked-text)
  11386. (setq type "thisfile"
  11387. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11388. (1+ (point)) (point))
  11389. path (buffer-substring
  11390. (previous-single-property-change pos 'org-linked-text)
  11391. (next-single-property-change pos 'org-linked-text)))
  11392. (throw 'match t))
  11393. (save-excursion
  11394. (when (or (org-in-regexp org-angle-link-re)
  11395. (org-in-regexp org-plain-link-re))
  11396. (setq type (match-string 1) path (match-string 2))
  11397. (throw 'match t)))
  11398. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11399. (setq type "tree-match"
  11400. path (match-string 1))
  11401. (throw 'match t))
  11402. (save-excursion
  11403. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11404. (setq type "tags"
  11405. path (match-string 1))
  11406. (while (string-match ":" path)
  11407. (setq path (replace-match "+" t t path)))
  11408. (throw 'match t))))
  11409. (unless path
  11410. (error "No link found"))
  11411. ;; Remove any trailing spaces in path
  11412. (if (string-match " +\\'" path)
  11413. (setq path (replace-match "" t t path)))
  11414. (cond
  11415. ((assoc type org-link-protocols)
  11416. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11417. ((equal type "mailto")
  11418. (let ((cmd (car org-link-mailto-program))
  11419. (args (cdr org-link-mailto-program)) args1
  11420. (address path) (subject "") a)
  11421. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11422. (setq address (match-string 1 path)
  11423. subject (org-link-escape (match-string 2 path))))
  11424. (while args
  11425. (cond
  11426. ((not (stringp (car args))) (push (pop args) args1))
  11427. (t (setq a (pop args))
  11428. (if (string-match "%a" a)
  11429. (setq a (replace-match address t t a)))
  11430. (if (string-match "%s" a)
  11431. (setq a (replace-match subject t t a)))
  11432. (push a args1))))
  11433. (apply cmd (nreverse args1))))
  11434. ((member type '("http" "https" "ftp" "news"))
  11435. (browse-url (concat type ":" (org-link-escape
  11436. path org-link-escape-chars-browser))))
  11437. ((member type '("message"))
  11438. (browse-url (concat type ":" path)))
  11439. ((string= type "tags")
  11440. (org-tags-view in-emacs path))
  11441. ((string= type "thisfile")
  11442. (if in-emacs
  11443. (switch-to-buffer-other-window
  11444. (org-get-buffer-for-internal-link (current-buffer)))
  11445. (org-mark-ring-push))
  11446. (let ((cmd `(org-link-search
  11447. ,path
  11448. ,(cond ((equal in-emacs '(4)) 'occur)
  11449. ((equal in-emacs '(16)) 'org-occur)
  11450. (t nil))
  11451. ,pos)))
  11452. (condition-case nil (eval cmd)
  11453. (error (progn (widen) (eval cmd))))))
  11454. ((string= type "tree-match")
  11455. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11456. ((string= type "file")
  11457. (if (string-match "::\\([0-9]+\\)\\'" path)
  11458. (setq line (string-to-number (match-string 1 path))
  11459. path (substring path 0 (match-beginning 0)))
  11460. (if (string-match "::\\(.+\\)\\'" path)
  11461. (setq search (match-string 1 path)
  11462. path (substring path 0 (match-beginning 0)))))
  11463. (if (string-match "[*?{]" (file-name-nondirectory path))
  11464. (dired path)
  11465. (org-open-file path in-emacs line search)))
  11466. ((string= type "news")
  11467. (org-follow-gnus-link path))
  11468. ((string= type "bbdb")
  11469. (org-follow-bbdb-link path))
  11470. ((string= type "info")
  11471. (org-follow-info-link path))
  11472. ((string= type "gnus")
  11473. (let (group article)
  11474. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11475. (error "Error in Gnus link"))
  11476. (setq group (match-string 1 path)
  11477. article (match-string 3 path))
  11478. (org-follow-gnus-link group article)))
  11479. ((string= type "vm")
  11480. (let (folder article)
  11481. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11482. (error "Error in VM link"))
  11483. (setq folder (match-string 1 path)
  11484. article (match-string 3 path))
  11485. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11486. (org-follow-vm-link folder article in-emacs)))
  11487. ((string= type "wl")
  11488. (let (folder article)
  11489. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11490. (error "Error in Wanderlust link"))
  11491. (setq folder (match-string 1 path)
  11492. article (match-string 3 path))
  11493. (org-follow-wl-link folder article)))
  11494. ((string= type "mhe")
  11495. (let (folder article)
  11496. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11497. (error "Error in MHE link"))
  11498. (setq folder (match-string 1 path)
  11499. article (match-string 3 path))
  11500. (org-follow-mhe-link folder article)))
  11501. ((string= type "rmail")
  11502. (let (folder article)
  11503. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11504. (error "Error in RMAIL link"))
  11505. (setq folder (match-string 1 path)
  11506. article (match-string 3 path))
  11507. (org-follow-rmail-link folder article)))
  11508. ((string= type "shell")
  11509. (let ((cmd path))
  11510. (if (or (not org-confirm-shell-link-function)
  11511. (funcall org-confirm-shell-link-function
  11512. (format "Execute \"%s\" in shell? "
  11513. (org-add-props cmd nil
  11514. 'face 'org-warning))))
  11515. (progn
  11516. (message "Executing %s" cmd)
  11517. (shell-command cmd))
  11518. (error "Abort"))))
  11519. ((string= type "elisp")
  11520. (let ((cmd path))
  11521. (if (or (not org-confirm-elisp-link-function)
  11522. (funcall org-confirm-elisp-link-function
  11523. (format "Execute \"%s\" as elisp? "
  11524. (org-add-props cmd nil
  11525. 'face 'org-warning))))
  11526. (message "%s => %s" cmd (eval (read cmd)))
  11527. (error "Abort"))))
  11528. (t
  11529. (browse-url-at-point)))))
  11530. (move-marker org-open-link-marker nil)))
  11531. ;;; File search
  11532. (defvar org-create-file-search-functions nil
  11533. "List of functions to construct the right search string for a file link.
  11534. These functions are called in turn with point at the location to
  11535. which the link should point.
  11536. A function in the hook should first test if it would like to
  11537. handle this file type, for example by checking the major-mode or
  11538. the file extension. If it decides not to handle this file, it
  11539. should just return nil to give other functions a chance. If it
  11540. does handle the file, it must return the search string to be used
  11541. when following the link. The search string will be part of the
  11542. file link, given after a double colon, and `org-open-at-point'
  11543. will automatically search for it. If special measures must be
  11544. taken to make the search successful, another function should be
  11545. added to the companion hook `org-execute-file-search-functions',
  11546. which see.
  11547. A function in this hook may also use `setq' to set the variable
  11548. `description' to provide a suggestion for the descriptive text to
  11549. be used for this link when it gets inserted into an Org-mode
  11550. buffer with \\[org-insert-link].")
  11551. (defvar org-execute-file-search-functions nil
  11552. "List of functions to execute a file search triggered by a link.
  11553. Functions added to this hook must accept a single argument, the
  11554. search string that was part of the file link, the part after the
  11555. double colon. The function must first check if it would like to
  11556. handle this search, for example by checking the major-mode or the
  11557. file extension. If it decides not to handle this search, it
  11558. should just return nil to give other functions a chance. If it
  11559. does handle the search, it must return a non-nil value to keep
  11560. other functions from trying.
  11561. Each function can access the current prefix argument through the
  11562. variable `current-prefix-argument'. Note that a single prefix is
  11563. used to force opening a link in Emacs, so it may be good to only
  11564. use a numeric or double prefix to guide the search function.
  11565. In case this is needed, a function in this hook can also restore
  11566. the window configuration before `org-open-at-point' was called using:
  11567. (set-window-configuration org-window-config-before-follow-link)")
  11568. (defun org-link-search (s &optional type avoid-pos)
  11569. "Search for a link search option.
  11570. If S is surrounded by forward slashes, it is interpreted as a
  11571. regular expression. In org-mode files, this will create an `org-occur'
  11572. sparse tree. In ordinary files, `occur' will be used to list matches.
  11573. If the current buffer is in `dired-mode', grep will be used to search
  11574. in all files. If AVOID-POS is given, ignore matches near that position."
  11575. (let ((case-fold-search t)
  11576. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11577. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11578. (append '(("") (" ") ("\t") ("\n"))
  11579. org-emphasis-alist)
  11580. "\\|") "\\)"))
  11581. (pos (point))
  11582. (pre "") (post "")
  11583. words re0 re1 re2 re3 re4 re5 re2a reall)
  11584. (cond
  11585. ;; First check if there are any special
  11586. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11587. ;; Now try the builtin stuff
  11588. ((save-excursion
  11589. (goto-char (point-min))
  11590. (and
  11591. (re-search-forward
  11592. (concat "<<" (regexp-quote s0) ">>") nil t)
  11593. (setq pos (match-beginning 0))))
  11594. ;; There is an exact target for this
  11595. (goto-char pos))
  11596. ((string-match "^/\\(.*\\)/$" s)
  11597. ;; A regular expression
  11598. (cond
  11599. ((org-mode-p)
  11600. (org-occur (match-string 1 s)))
  11601. ;;((eq major-mode 'dired-mode)
  11602. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11603. (t (org-do-occur (match-string 1 s)))))
  11604. (t
  11605. ;; A normal search strings
  11606. (when (equal (string-to-char s) ?*)
  11607. ;; Anchor on headlines, post may include tags.
  11608. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11609. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11610. s (substring s 1)))
  11611. (remove-text-properties
  11612. 0 (length s)
  11613. '(face nil mouse-face nil keymap nil fontified nil) s)
  11614. ;; Make a series of regular expressions to find a match
  11615. (setq words (org-split-string s "[ \n\r\t]+")
  11616. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11617. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11618. "\\)" markers)
  11619. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11620. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11621. re1 (concat pre re2 post)
  11622. re3 (concat pre re4 post)
  11623. re5 (concat pre ".*" re4)
  11624. re2 (concat pre re2)
  11625. re2a (concat pre re2a)
  11626. re4 (concat pre re4)
  11627. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11628. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11629. re5 "\\)"
  11630. ))
  11631. (cond
  11632. ((eq type 'org-occur) (org-occur reall))
  11633. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11634. (t (goto-char (point-min))
  11635. (if (or (org-search-not-self 1 re0 nil t)
  11636. (org-search-not-self 1 re1 nil t)
  11637. (org-search-not-self 1 re2 nil t)
  11638. (org-search-not-self 1 re2a nil t)
  11639. (org-search-not-self 1 re3 nil t)
  11640. (org-search-not-self 1 re4 nil t)
  11641. (org-search-not-self 1 re5 nil t)
  11642. )
  11643. (goto-char (match-beginning 1))
  11644. (goto-char pos)
  11645. (error "No match")))))
  11646. (t
  11647. ;; Normal string-search
  11648. (goto-char (point-min))
  11649. (if (search-forward s nil t)
  11650. (goto-char (match-beginning 0))
  11651. (error "No match"))))
  11652. (and (org-mode-p) (org-show-context 'link-search))))
  11653. (defun org-search-not-self (group &rest args)
  11654. "Execute `re-search-forward', but only accept matches that do not
  11655. enclose the position of `org-open-link-marker'."
  11656. (let ((m org-open-link-marker))
  11657. (catch 'exit
  11658. (while (apply 're-search-forward args)
  11659. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11660. (goto-char (match-end group))
  11661. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11662. (> (match-beginning 0) (marker-position m))
  11663. (< (match-end 0) (marker-position m)))
  11664. (save-match-data
  11665. (or (not (org-in-regexp
  11666. org-bracket-link-analytic-regexp 1))
  11667. (not (match-end 4)) ; no description
  11668. (and (<= (match-beginning 4) (point))
  11669. (>= (match-end 4) (point))))))
  11670. (throw 'exit (point))))))))
  11671. (defun org-get-buffer-for-internal-link (buffer)
  11672. "Return a buffer to be used for displaying the link target of internal links."
  11673. (cond
  11674. ((not org-display-internal-link-with-indirect-buffer)
  11675. buffer)
  11676. ((string-match "(Clone)$" (buffer-name buffer))
  11677. (message "Buffer is already a clone, not making another one")
  11678. ;; we also do not modify visibility in this case
  11679. buffer)
  11680. (t ; make a new indirect buffer for displaying the link
  11681. (let* ((bn (buffer-name buffer))
  11682. (ibn (concat bn "(Clone)"))
  11683. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11684. (with-current-buffer ib (org-overview))
  11685. ib))))
  11686. (defun org-do-occur (regexp &optional cleanup)
  11687. "Call the Emacs command `occur'.
  11688. If CLEANUP is non-nil, remove the printout of the regular expression
  11689. in the *Occur* buffer. This is useful if the regex is long and not useful
  11690. to read."
  11691. (occur regexp)
  11692. (when cleanup
  11693. (let ((cwin (selected-window)) win beg end)
  11694. (when (setq win (get-buffer-window "*Occur*"))
  11695. (select-window win))
  11696. (goto-char (point-min))
  11697. (when (re-search-forward "match[a-z]+" nil t)
  11698. (setq beg (match-end 0))
  11699. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11700. (setq end (1- (match-beginning 0)))))
  11701. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11702. (goto-char (point-min))
  11703. (select-window cwin))))
  11704. ;;; The mark ring for links jumps
  11705. (defvar org-mark-ring nil
  11706. "Mark ring for positions before jumps in Org-mode.")
  11707. (defvar org-mark-ring-last-goto nil
  11708. "Last position in the mark ring used to go back.")
  11709. ;; Fill and close the ring
  11710. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11711. (loop for i from 1 to org-mark-ring-length do
  11712. (push (make-marker) org-mark-ring))
  11713. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11714. org-mark-ring)
  11715. (defun org-mark-ring-push (&optional pos buffer)
  11716. "Put the current position or POS into the mark ring and rotate it."
  11717. (interactive)
  11718. (setq pos (or pos (point)))
  11719. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11720. (move-marker (car org-mark-ring)
  11721. (or pos (point))
  11722. (or buffer (current-buffer)))
  11723. (message "%s"
  11724. (substitute-command-keys
  11725. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11726. (defun org-mark-ring-goto (&optional n)
  11727. "Jump to the previous position in the mark ring.
  11728. With prefix arg N, jump back that many stored positions. When
  11729. called several times in succession, walk through the entire ring.
  11730. Org-mode commands jumping to a different position in the current file,
  11731. or to another Org-mode file, automatically push the old position
  11732. onto the ring."
  11733. (interactive "p")
  11734. (let (p m)
  11735. (if (eq last-command this-command)
  11736. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11737. (setq p org-mark-ring))
  11738. (setq org-mark-ring-last-goto p)
  11739. (setq m (car p))
  11740. (switch-to-buffer (marker-buffer m))
  11741. (goto-char m)
  11742. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11743. (defun org-remove-angle-brackets (s)
  11744. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11745. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11746. s)
  11747. (defun org-add-angle-brackets (s)
  11748. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11749. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11750. s)
  11751. ;;; Following specific links
  11752. (defun org-follow-timestamp-link ()
  11753. (cond
  11754. ((org-at-date-range-p t)
  11755. (let ((org-agenda-start-on-weekday)
  11756. (t1 (match-string 1))
  11757. (t2 (match-string 2)))
  11758. (setq t1 (time-to-days (org-time-string-to-time t1))
  11759. t2 (time-to-days (org-time-string-to-time t2)))
  11760. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11761. ((org-at-timestamp-p t)
  11762. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11763. (substring (match-string 1) 0 10)))
  11764. 1))
  11765. (t (error "This should not happen"))))
  11766. (defun org-follow-bbdb-link (name)
  11767. "Follow a BBDB link to NAME."
  11768. (require 'bbdb)
  11769. (let ((inhibit-redisplay (not debug-on-error))
  11770. (bbdb-electric-p nil))
  11771. (catch 'exit
  11772. ;; Exact match on name
  11773. (bbdb-name (concat "\\`" name "\\'") nil)
  11774. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11775. ;; Exact match on name
  11776. (bbdb-company (concat "\\`" name "\\'") nil)
  11777. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11778. ;; Partial match on name
  11779. (bbdb-name name nil)
  11780. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11781. ;; Partial match on company
  11782. (bbdb-company name nil)
  11783. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11784. ;; General match including network address and notes
  11785. (bbdb name nil)
  11786. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11787. (delete-window (get-buffer-window "*BBDB*"))
  11788. (error "No matching BBDB record")))))
  11789. (defun org-follow-info-link (name)
  11790. "Follow an info file & node link to NAME."
  11791. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11792. (string-match "\\(.*\\)" name))
  11793. (progn
  11794. (require 'info)
  11795. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11796. (Info-find-node (match-string 1 name) (match-string 2 name))
  11797. (Info-find-node (match-string 1 name) "Top")))
  11798. (message "Could not open: %s" name)))
  11799. (defun org-follow-gnus-link (&optional group article)
  11800. "Follow a Gnus link to GROUP and ARTICLE."
  11801. (require 'gnus)
  11802. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11803. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11804. (cond ((and group article)
  11805. (gnus-group-read-group 1 nil group)
  11806. (gnus-summary-goto-article (string-to-number article) nil t))
  11807. (group (gnus-group-jump-to-group group))))
  11808. (defun org-follow-vm-link (&optional folder article readonly)
  11809. "Follow a VM link to FOLDER and ARTICLE."
  11810. (require 'vm)
  11811. (setq article (org-add-angle-brackets article))
  11812. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11813. ;; ange-ftp or efs or tramp access
  11814. (let ((user (or (match-string 1 folder) (user-login-name)))
  11815. (host (match-string 2 folder))
  11816. (file (match-string 3 folder)))
  11817. (cond
  11818. ((featurep 'tramp)
  11819. ;; use tramp to access the file
  11820. (if (featurep 'xemacs)
  11821. (setq folder (format "[%s@%s]%s" user host file))
  11822. (setq folder (format "/%s@%s:%s" user host file))))
  11823. (t
  11824. ;; use ange-ftp or efs
  11825. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11826. (setq folder (format "/%s@%s:%s" user host file))))))
  11827. (when folder
  11828. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11829. (sit-for 0.1)
  11830. (when article
  11831. (vm-select-folder-buffer)
  11832. (widen)
  11833. (let ((case-fold-search t))
  11834. (goto-char (point-min))
  11835. (if (not (re-search-forward
  11836. (concat "^" "message-id: *" (regexp-quote article))))
  11837. (error "Could not find the specified message in this folder"))
  11838. (vm-isearch-update)
  11839. (vm-isearch-narrow)
  11840. (vm-beginning-of-message)
  11841. (vm-summarize)))))
  11842. (defun org-follow-wl-link (folder article)
  11843. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11844. (if (and (string= folder "%")
  11845. article
  11846. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11847. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11848. ;; Thus, we recompose folder and article ids.
  11849. (setq folder (format "%s#%s" folder (match-string 1 article))
  11850. article (match-string 3 article)))
  11851. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11852. (error "No such folder: %s" folder))
  11853. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11854. (and article
  11855. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11856. (wl-summary-redisplay)))
  11857. (defun org-follow-rmail-link (folder article)
  11858. "Follow an RMAIL link to FOLDER and ARTICLE."
  11859. (setq article (org-add-angle-brackets article))
  11860. (let (message-number)
  11861. (save-excursion
  11862. (save-window-excursion
  11863. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11864. (setq message-number
  11865. (save-restriction
  11866. (widen)
  11867. (goto-char (point-max))
  11868. (if (re-search-backward
  11869. (concat "^Message-ID:\\s-+" (regexp-quote
  11870. (or article "")))
  11871. nil t)
  11872. (rmail-what-message))))))
  11873. (if message-number
  11874. (progn
  11875. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11876. (rmail-show-message message-number)
  11877. message-number)
  11878. (error "Message not found"))))
  11879. ;;; mh-e integration based on planner-mode
  11880. (defun org-mhe-get-message-real-folder ()
  11881. "Return the name of the current message real folder, so if you use
  11882. sequences, it will now work."
  11883. (save-excursion
  11884. (let* ((folder
  11885. (if (equal major-mode 'mh-folder-mode)
  11886. mh-current-folder
  11887. ;; Refer to the show buffer
  11888. mh-show-folder-buffer))
  11889. (end-index
  11890. (if (boundp 'mh-index-folder)
  11891. (min (length mh-index-folder) (length folder))))
  11892. )
  11893. ;; a simple test on mh-index-data does not work, because
  11894. ;; mh-index-data is always nil in a show buffer.
  11895. (if (and (boundp 'mh-index-folder)
  11896. (string= mh-index-folder (substring folder 0 end-index)))
  11897. (if (equal major-mode 'mh-show-mode)
  11898. (save-window-excursion
  11899. (let (pop-up-frames)
  11900. (when (buffer-live-p (get-buffer folder))
  11901. (progn
  11902. (pop-to-buffer folder)
  11903. (org-mhe-get-message-folder-from-index)
  11904. )
  11905. )))
  11906. (org-mhe-get-message-folder-from-index)
  11907. )
  11908. folder
  11909. )
  11910. )))
  11911. (defun org-mhe-get-message-folder-from-index ()
  11912. "Returns the name of the message folder in a index folder buffer."
  11913. (save-excursion
  11914. (mh-index-previous-folder)
  11915. (re-search-forward "^\\(+.*\\)$" nil t)
  11916. (message "%s" (match-string 1))))
  11917. (defun org-mhe-get-message-folder ()
  11918. "Return the name of the current message folder. Be careful if you
  11919. use sequences."
  11920. (save-excursion
  11921. (if (equal major-mode 'mh-folder-mode)
  11922. mh-current-folder
  11923. ;; Refer to the show buffer
  11924. mh-show-folder-buffer)))
  11925. (defun org-mhe-get-message-num ()
  11926. "Return the number of the current message. Be careful if you
  11927. use sequences."
  11928. (save-excursion
  11929. (if (equal major-mode 'mh-folder-mode)
  11930. (mh-get-msg-num nil)
  11931. ;; Refer to the show buffer
  11932. (mh-show-buffer-message-number))))
  11933. (defun org-mhe-get-header (header)
  11934. "Return a header of the message in folder mode. This will create a
  11935. show buffer for the corresponding message. If you have a more clever
  11936. idea..."
  11937. (let* ((folder (org-mhe-get-message-folder))
  11938. (num (org-mhe-get-message-num))
  11939. (buffer (get-buffer-create (concat "show-" folder)))
  11940. (header-field))
  11941. (with-current-buffer buffer
  11942. (mh-display-msg num folder)
  11943. (if (equal major-mode 'mh-folder-mode)
  11944. (mh-header-display)
  11945. (mh-show-header-display))
  11946. (set-buffer buffer)
  11947. (setq header-field (mh-get-header-field header))
  11948. (if (equal major-mode 'mh-folder-mode)
  11949. (mh-show)
  11950. (mh-show-show))
  11951. header-field)))
  11952. (defun org-follow-mhe-link (folder article)
  11953. "Follow an MHE link to FOLDER and ARTICLE.
  11954. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11955. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11956. ARTICLE is searched in all folders. Indexed searches (swish++,
  11957. namazu, and others supported by MH-E) will always search in all
  11958. folders."
  11959. (require 'mh-e)
  11960. (require 'mh-search)
  11961. (require 'mh-utils)
  11962. (mh-find-path)
  11963. (if (not article)
  11964. (mh-visit-folder (mh-normalize-folder-name folder))
  11965. (setq article (org-add-angle-brackets article))
  11966. (mh-search-choose)
  11967. (if (equal mh-searcher 'pick)
  11968. (progn
  11969. (mh-search folder (list "--message-id" article))
  11970. (when (and org-mhe-search-all-folders
  11971. (not (org-mhe-get-message-real-folder)))
  11972. (kill-this-buffer)
  11973. (mh-search "+" (list "--message-id" article))))
  11974. (mh-search "+" article))
  11975. (if (org-mhe-get-message-real-folder)
  11976. (mh-show-msg 1)
  11977. (kill-this-buffer)
  11978. (error "Message not found"))))
  11979. ;;; BibTeX links
  11980. ;; Use the custom search meachnism to construct and use search strings for
  11981. ;; file links to BibTeX database entries.
  11982. (defun org-create-file-search-in-bibtex ()
  11983. "Create the search string and description for a BibTeX database entry."
  11984. (when (eq major-mode 'bibtex-mode)
  11985. ;; yes, we want to construct this search string.
  11986. ;; Make a good description for this entry, using names, year and the title
  11987. ;; Put it into the `description' variable which is dynamically scoped.
  11988. (let ((bibtex-autokey-names 1)
  11989. (bibtex-autokey-names-stretch 1)
  11990. (bibtex-autokey-name-case-convert-function 'identity)
  11991. (bibtex-autokey-name-separator " & ")
  11992. (bibtex-autokey-additional-names " et al.")
  11993. (bibtex-autokey-year-length 4)
  11994. (bibtex-autokey-name-year-separator " ")
  11995. (bibtex-autokey-titlewords 3)
  11996. (bibtex-autokey-titleword-separator " ")
  11997. (bibtex-autokey-titleword-case-convert-function 'identity)
  11998. (bibtex-autokey-titleword-length 'infty)
  11999. (bibtex-autokey-year-title-separator ": "))
  12000. (setq description (bibtex-generate-autokey)))
  12001. ;; Now parse the entry, get the key and return it.
  12002. (save-excursion
  12003. (bibtex-beginning-of-entry)
  12004. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12005. (defun org-execute-file-search-in-bibtex (s)
  12006. "Find the link search string S as a key for a database entry."
  12007. (when (eq major-mode 'bibtex-mode)
  12008. ;; Yes, we want to do the search in this file.
  12009. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12010. (goto-char (point-min))
  12011. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12012. (regexp-quote s) "[ \t\n]*,") nil t)
  12013. (goto-char (match-beginning 0)))
  12014. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12015. ;; Use double prefix to indicate that any web link should be browsed
  12016. (let ((b (current-buffer)) (p (point)))
  12017. ;; Restore the window configuration because we just use the web link
  12018. (set-window-configuration org-window-config-before-follow-link)
  12019. (save-excursion (set-buffer b) (goto-char p)
  12020. (bibtex-url)))
  12021. (recenter 0)) ; Move entry start to beginning of window
  12022. ;; return t to indicate that the search is done.
  12023. t))
  12024. ;; Finally add the functions to the right hooks.
  12025. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12026. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12027. ;; end of Bibtex link setup
  12028. ;;; Following file links
  12029. (defun org-open-file (path &optional in-emacs line search)
  12030. "Open the file at PATH.
  12031. First, this expands any special file name abbreviations. Then the
  12032. configuration variable `org-file-apps' is checked if it contains an
  12033. entry for this file type, and if yes, the corresponding command is launched.
  12034. If no application is found, Emacs simply visits the file.
  12035. With optional argument IN-EMACS, Emacs will visit the file.
  12036. Optional LINE specifies a line to go to, optional SEARCH a string to
  12037. search for. If LINE or SEARCH is given, the file will always be
  12038. opened in Emacs.
  12039. If the file does not exist, an error is thrown."
  12040. (setq in-emacs (or in-emacs line search))
  12041. (let* ((file (if (equal path "")
  12042. buffer-file-name
  12043. (substitute-in-file-name (expand-file-name path))))
  12044. (apps (append org-file-apps (org-default-apps)))
  12045. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12046. (dirp (if remp nil (file-directory-p file)))
  12047. (dfile (downcase file))
  12048. (old-buffer (current-buffer))
  12049. (old-pos (point))
  12050. (old-mode major-mode)
  12051. ext cmd)
  12052. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12053. (setq ext (match-string 1 dfile))
  12054. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12055. (setq ext (match-string 1 dfile))))
  12056. (if in-emacs
  12057. (setq cmd 'emacs)
  12058. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12059. (and dirp (cdr (assoc 'directory apps)))
  12060. (cdr (assoc ext apps))
  12061. (cdr (assoc t apps)))))
  12062. (when (eq cmd 'mailcap)
  12063. (require 'mailcap)
  12064. (mailcap-parse-mailcaps)
  12065. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12066. (command (mailcap-mime-info mime-type)))
  12067. (if (stringp command)
  12068. (setq cmd command)
  12069. (setq cmd 'emacs))))
  12070. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12071. (not (file-exists-p file))
  12072. (not org-open-non-existing-files))
  12073. (error "No such file: %s" file))
  12074. (cond
  12075. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12076. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12077. (while (string-match "['\"]%s['\"]" cmd)
  12078. (setq cmd (replace-match "%s" t t cmd)))
  12079. (while (string-match "%s" cmd)
  12080. (setq cmd (replace-match
  12081. (save-match-data (shell-quote-argument file))
  12082. t t cmd)))
  12083. (save-window-excursion
  12084. (start-process-shell-command cmd nil cmd)))
  12085. ((or (stringp cmd)
  12086. (eq cmd 'emacs))
  12087. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12088. (widen)
  12089. (if line (goto-line line)
  12090. (if search (org-link-search search))))
  12091. ((consp cmd)
  12092. (eval cmd))
  12093. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12094. (and (org-mode-p) (eq old-mode 'org-mode)
  12095. (or (not (equal old-buffer (current-buffer)))
  12096. (not (equal old-pos (point))))
  12097. (org-mark-ring-push old-pos old-buffer))))
  12098. (defun org-default-apps ()
  12099. "Return the default applications for this operating system."
  12100. (cond
  12101. ((eq system-type 'darwin)
  12102. org-file-apps-defaults-macosx)
  12103. ((eq system-type 'windows-nt)
  12104. org-file-apps-defaults-windowsnt)
  12105. (t org-file-apps-defaults-gnu)))
  12106. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12107. (defun org-file-remote-p (file)
  12108. "Test whether FILE specifies a location on a remote system.
  12109. Return non-nil if the location is indeed remote.
  12110. For example, the filename \"/user@host:/foo\" specifies a location
  12111. on the system \"/user@host:\"."
  12112. (cond ((fboundp 'file-remote-p)
  12113. (file-remote-p file))
  12114. ((fboundp 'tramp-handle-file-remote-p)
  12115. (tramp-handle-file-remote-p file))
  12116. ((and (boundp 'ange-ftp-name-format)
  12117. (string-match (car ange-ftp-name-format) file))
  12118. t)
  12119. (t nil)))
  12120. ;;;; Hooks for remember.el, and refiling
  12121. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12122. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12123. ;;;###autoload
  12124. (defun org-remember-insinuate ()
  12125. "Setup remember.el for use wiht Org-mode."
  12126. (require 'remember)
  12127. (setq remember-annotation-functions '(org-remember-annotation))
  12128. (setq remember-handler-functions '(org-remember-handler))
  12129. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12130. ;;;###autoload
  12131. (defun org-remember-annotation ()
  12132. "Return a link to the current location as an annotation for remember.el.
  12133. If you are using Org-mode files as target for data storage with
  12134. remember.el, then the annotations should include a link compatible with the
  12135. conventions in Org-mode. This function returns such a link."
  12136. (org-store-link nil))
  12137. (defconst org-remember-help
  12138. "Select a destination location for the note.
  12139. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12140. RET on headline -> Store as sublevel entry to current headline
  12141. RET at beg-of-buf -> Append to file as level 2 headline
  12142. <left>/<right> -> before/after current headline, same headings level")
  12143. (defvar org-remember-previous-location nil)
  12144. (defvar org-force-remember-template-char) ;; dynamically scoped
  12145. (defun org-select-remember-template (&optional use-char)
  12146. (when org-remember-templates
  12147. (let* ((templates (mapcar (lambda (x)
  12148. (if (stringp (car x))
  12149. (append (list (nth 1 x) (car x)) (cddr x))
  12150. (append (list (car x) "") (cdr x))))
  12151. org-remember-templates))
  12152. (char (or use-char
  12153. (cond
  12154. ((= (length templates) 1)
  12155. (caar templates))
  12156. ((and (boundp 'org-force-remember-template-char)
  12157. org-force-remember-template-char)
  12158. (if (stringp org-force-remember-template-char)
  12159. (string-to-char org-force-remember-template-char)
  12160. org-force-remember-template-char))
  12161. (t
  12162. (message "Select template: %s"
  12163. (mapconcat
  12164. (lambda (x)
  12165. (cond
  12166. ((not (string-match "\\S-" (nth 1 x)))
  12167. (format "[%c]" (car x)))
  12168. ((equal (downcase (car x))
  12169. (downcase (aref (nth 1 x) 0)))
  12170. (format "[%c]%s" (car x)
  12171. (substring (nth 1 x) 1)))
  12172. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12173. templates " "))
  12174. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12175. (when (equal char0 ?\C-g)
  12176. (jump-to-register remember-register)
  12177. (kill-buffer remember-buffer))
  12178. char0))))))
  12179. (cddr (assoc char templates)))))
  12180. (defvar x-last-selected-text)
  12181. (defvar x-last-selected-text-primary)
  12182. ;;;###autoload
  12183. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12184. "Initialize *remember* buffer with template, invoke `org-mode'.
  12185. This function should be placed into `remember-mode-hook' and in fact requires
  12186. to be run from that hook to function properly."
  12187. (if org-remember-templates
  12188. (let* ((entry (org-select-remember-template use-char))
  12189. (tpl (car entry))
  12190. (plist-p (if org-store-link-plist t nil))
  12191. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12192. (string-match "\\S-" (nth 1 entry)))
  12193. (nth 1 entry)
  12194. org-default-notes-file))
  12195. (headline (nth 2 entry))
  12196. (v-c (or (and (eq window-system 'x)
  12197. (fboundp 'x-cut-buffer-or-selection-value)
  12198. (x-cut-buffer-or-selection-value))
  12199. (org-bound-and-true-p x-last-selected-text)
  12200. (org-bound-and-true-p x-last-selected-text-primary)
  12201. (and (> (length kill-ring) 0) (current-kill 0))))
  12202. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12203. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12204. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12205. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12206. ;; `initial' and `annotation' are bound in `remember'
  12207. (v-i (if (boundp 'initial) initial))
  12208. (v-a (if (and (boundp 'annotation) annotation)
  12209. (if (equal annotation "[[]]") "" annotation)
  12210. ""))
  12211. (v-A (if (and v-a
  12212. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12213. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12214. v-a))
  12215. (v-n user-full-name)
  12216. (org-startup-folded nil)
  12217. org-time-was-given org-end-time-was-given x
  12218. prompt completions char time pos default histvar)
  12219. (setq org-store-link-plist
  12220. (append (list :annotation v-a :initial v-i)
  12221. org-store-link-plist))
  12222. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12223. (erase-buffer)
  12224. (insert (substitute-command-keys
  12225. (format
  12226. "## Filing location: Select interactively, default, or last used:
  12227. ## %s to select file and header location interactively.
  12228. ## %s \"%s\" -> \"* %s\"
  12229. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12230. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12231. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12232. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12233. (abbreviate-file-name (or file org-default-notes-file))
  12234. (or headline "")
  12235. (or (car org-remember-previous-location) "???")
  12236. (or (cdr org-remember-previous-location) "???"))))
  12237. (insert tpl) (goto-char (point-min))
  12238. ;; Simple %-escapes
  12239. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12240. (when (and initial (equal (match-string 0) "%i"))
  12241. (save-match-data
  12242. (let* ((lead (buffer-substring
  12243. (point-at-bol) (match-beginning 0))))
  12244. (setq v-i (mapconcat 'identity
  12245. (org-split-string initial "\n")
  12246. (concat "\n" lead))))))
  12247. (replace-match
  12248. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12249. t t))
  12250. ;; %[] Insert contents of a file.
  12251. (goto-char (point-min))
  12252. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12253. (let ((start (match-beginning 0))
  12254. (end (match-end 0))
  12255. (filename (expand-file-name (match-string 1))))
  12256. (goto-char start)
  12257. (delete-region start end)
  12258. (condition-case error
  12259. (insert-file-contents filename)
  12260. (error (insert (format "%%![Couldn't insert %s: %s]"
  12261. filename error))))))
  12262. ;; %() embedded elisp
  12263. (goto-char (point-min))
  12264. (while (re-search-forward "%\\((.+)\\)" nil t)
  12265. (goto-char (match-beginning 0))
  12266. (let ((template-start (point)))
  12267. (forward-char 1)
  12268. (let ((result
  12269. (condition-case error
  12270. (eval (read (current-buffer)))
  12271. (error (format "%%![Error: %s]" error)))))
  12272. (delete-region template-start (point))
  12273. (insert result))))
  12274. ;; From the property list
  12275. (when plist-p
  12276. (goto-char (point-min))
  12277. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12278. (and (setq x (or (plist-get org-store-link-plist
  12279. (intern (match-string 1))) ""))
  12280. (replace-match x t t))))
  12281. ;; Turn on org-mode in the remember buffer, set local variables
  12282. (org-mode)
  12283. (org-set-local 'org-finish-function 'org-remember-finalize)
  12284. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12285. (org-set-local 'org-default-notes-file file))
  12286. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12287. (org-set-local 'org-remember-default-headline headline))
  12288. ;; Interactive template entries
  12289. (goto-char (point-min))
  12290. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12291. (setq char (if (match-end 3) (match-string 3))
  12292. prompt (if (match-end 2) (match-string 2)))
  12293. (goto-char (match-beginning 0))
  12294. (replace-match "")
  12295. (setq completions nil default nil)
  12296. (when prompt
  12297. (setq completions (org-split-string prompt "|")
  12298. prompt (pop completions)
  12299. default (car completions)
  12300. histvar (intern (concat
  12301. "org-remember-template-prompt-history::"
  12302. (or prompt "")))
  12303. completions (mapcar 'list completions)))
  12304. (cond
  12305. ((member char '("G" "g"))
  12306. (let* ((org-last-tags-completion-table
  12307. (org-global-tags-completion-table
  12308. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12309. (org-add-colon-after-tag-completion t)
  12310. (ins (completing-read
  12311. (if prompt (concat prompt ": ") "Tags: ")
  12312. 'org-tags-completion-function nil nil nil
  12313. 'org-tags-history)))
  12314. (setq ins (mapconcat 'identity
  12315. (org-split-string ins (org-re "[^[:alnum:]]+"))
  12316. ":"))
  12317. (when (string-match "\\S-" ins)
  12318. (or (equal (char-before) ?:) (insert ":"))
  12319. (insert ins)
  12320. (or (equal (char-after) ?:) (insert ":")))))
  12321. (char
  12322. (setq org-time-was-given (equal (upcase char) char))
  12323. (setq time (org-read-date (equal (upcase char) "U") t nil
  12324. prompt))
  12325. (org-insert-time-stamp time org-time-was-given
  12326. (member char '("u" "U"))
  12327. nil nil (list org-end-time-was-given)))
  12328. (t
  12329. (insert (org-completing-read
  12330. (concat (if prompt prompt "Enter string")
  12331. (if default (concat " [" default "]"))
  12332. ": ")
  12333. completions nil nil nil histvar default)))))
  12334. (goto-char (point-min))
  12335. (if (re-search-forward "%\\?" nil t)
  12336. (replace-match "")
  12337. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12338. (org-mode)
  12339. (org-set-local 'org-finish-function 'org-remember-finalize))
  12340. (when (save-excursion
  12341. (goto-char (point-min))
  12342. (re-search-forward "%!" nil t))
  12343. (replace-match "")
  12344. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12345. (defun org-remember-finish-immediately ()
  12346. "File remember note immediately.
  12347. This should be run in `post-command-hook' and will remove itself
  12348. from that hook."
  12349. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12350. (when org-finish-function
  12351. (funcall org-finish-function)))
  12352. (defun org-remember-finalize ()
  12353. "Finalize the remember process."
  12354. (unless (fboundp 'remember-finalize)
  12355. (defalias 'remember-finalize 'remember-buffer))
  12356. (when (and org-clock-marker
  12357. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12358. ;; FIXME: test this, this is w/o notetaking!
  12359. (let (org-log-done) (org-clock-out)))
  12360. (when buffer-file-name
  12361. (save-buffer)
  12362. (setq buffer-file-name nil))
  12363. (remember-finalize))
  12364. ;;;###autoload
  12365. (defun org-remember (&optional goto org-force-remember-template-char)
  12366. "Call `remember'. If this is already a remember buffer, re-apply template.
  12367. If there is an active region, make sure remember uses it as initial content
  12368. of the remember buffer.
  12369. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12370. anything, just go to the file/headline where the selected template usually
  12371. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12372. note stored by remember.
  12373. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12374. associated with a template in `org-remember-templates'."
  12375. (interactive "P")
  12376. (cond
  12377. ((equal goto '(4)) (org-go-to-remember-target))
  12378. ((equal goto '(16)) (org-remember-goto-last-stored))
  12379. (t
  12380. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12381. (progn
  12382. (when (< (length org-remember-templates) 2)
  12383. (error "No other template available"))
  12384. (erase-buffer)
  12385. (let ((annotation (plist-get org-store-link-plist :annotation))
  12386. (initial (plist-get org-store-link-plist :initial)))
  12387. (org-remember-apply-template))
  12388. (message "Press C-c C-c to remember data"))
  12389. (if (org-region-active-p)
  12390. (remember (buffer-substring (point) (mark)))
  12391. (call-interactively 'remember))))))
  12392. (defun org-remember-goto-last-stored ()
  12393. "Go to the location where the last remember note was stored."
  12394. (interactive)
  12395. (bookmark-jump "org-remember-last-stored")
  12396. (message "This is the last note stored by remember"))
  12397. (defun org-go-to-remember-target (&optional template-key)
  12398. "Go to the target location of a remember template.
  12399. The user is queried for the template."
  12400. (interactive)
  12401. (let* ((entry (org-select-remember-template template-key))
  12402. (file (nth 1 entry))
  12403. (heading (nth 2 entry))
  12404. visiting)
  12405. (unless (and file (stringp file) (string-match "\\S-" file))
  12406. (setq file org-default-notes-file))
  12407. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12408. (setq heading org-remember-default-headline))
  12409. (setq visiting (org-find-base-buffer-visiting file))
  12410. (if (not visiting) (find-file-noselect file))
  12411. (switch-to-buffer (or visiting (get-file-buffer file)))
  12412. (widen)
  12413. (goto-char (point-min))
  12414. (if (re-search-forward
  12415. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12416. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12417. nil t)
  12418. (goto-char (match-beginning 0))
  12419. (error "Target headline not found: %s" heading))))
  12420. (defvar org-note-abort nil) ; dynamically scoped
  12421. ;;;###autoload
  12422. (defun org-remember-handler ()
  12423. "Store stuff from remember.el into an org file.
  12424. First prompts for an org file. If the user just presses return, the value
  12425. of `org-default-notes-file' is used.
  12426. Then the command offers the headings tree of the selected file in order to
  12427. file the text at a specific location.
  12428. You can either immediately press RET to get the note appended to the
  12429. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12430. find a better place. Then press RET or <left> or <right> in insert the note.
  12431. Key Cursor position Note gets inserted
  12432. -----------------------------------------------------------------------------
  12433. RET buffer-start as level 1 heading at end of file
  12434. RET on headline as sublevel of the heading at cursor
  12435. RET no heading at cursor position, level taken from context.
  12436. Or use prefix arg to specify level manually.
  12437. <left> on headline as same level, before current heading
  12438. <right> on headline as same level, after current heading
  12439. So the fastest way to store the note is to press RET RET to append it to
  12440. the default file. This way your current train of thought is not
  12441. interrupted, in accordance with the principles of remember.el.
  12442. You can also get the fast execution without prompting by using
  12443. C-u C-c C-c to exit the remember buffer. See also the variable
  12444. `org-remember-store-without-prompt'.
  12445. Before being stored away, the function ensures that the text has a
  12446. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12447. is constructed from the current date and some additional data.
  12448. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12449. also indented so that it starts in the same column as the headline
  12450. \(i.e. after the stars).
  12451. See also the variable `org-reverse-note-order'."
  12452. (goto-char (point-min))
  12453. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12454. (replace-match ""))
  12455. (goto-char (point-max))
  12456. (beginning-of-line 1)
  12457. (while (looking-at "[ \t]*$\\|##.*")
  12458. (delete-region (1- (point)) (point-max))
  12459. (beginning-of-line 1))
  12460. (catch 'quit
  12461. (if org-note-abort (throw 'quit nil))
  12462. (let* ((txt (buffer-substring (point-min) (point-max)))
  12463. (fastp (org-xor (equal current-prefix-arg '(4))
  12464. org-remember-store-without-prompt))
  12465. (file (cond
  12466. (fastp org-default-notes-file)
  12467. ((and (eq org-remember-interactive-interface 'refile)
  12468. org-refile-targets)
  12469. org-default-notes-file)
  12470. ((not (and (equal current-prefix-arg '(16))
  12471. org-remember-previous-location))
  12472. (org-get-org-file))))
  12473. (heading org-remember-default-headline)
  12474. (visiting (and file (org-find-base-buffer-visiting file)))
  12475. (org-startup-folded nil)
  12476. (org-startup-align-all-tables nil)
  12477. (org-goto-start-pos 1)
  12478. spos exitcmd level indent reversed)
  12479. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12480. (setq file (car org-remember-previous-location)
  12481. heading (cdr org-remember-previous-location)
  12482. fastp t))
  12483. (setq current-prefix-arg nil)
  12484. (if (string-match "[ \t\n]+\\'" txt)
  12485. (setq txt (replace-match "" t t txt)))
  12486. ;; Modify text so that it becomes a nice subtree which can be inserted
  12487. ;; into an org tree.
  12488. (let* ((lines (split-string txt "\n"))
  12489. first)
  12490. (setq first (car lines) lines (cdr lines))
  12491. (if (string-match "^\\*+ " first)
  12492. ;; Is already a headline
  12493. (setq indent nil)
  12494. ;; We need to add a headline: Use time and first buffer line
  12495. (setq lines (cons first lines)
  12496. first (concat "* " (current-time-string)
  12497. " (" (remember-buffer-desc) ")")
  12498. indent " "))
  12499. (if (and org-adapt-indentation indent)
  12500. (setq lines (mapcar
  12501. (lambda (x)
  12502. (if (string-match "\\S-" x)
  12503. (concat indent x) x))
  12504. lines)))
  12505. (setq txt (concat first "\n"
  12506. (mapconcat 'identity lines "\n"))))
  12507. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12508. (setq txt (replace-match "\n\n" t t txt))
  12509. (if (string-match "[ \t\n]*\\'" txt)
  12510. (setq txt (replace-match "\n" t t txt))))
  12511. ;; Put the modified text back into the remember buffer, for refile.
  12512. (erase-buffer)
  12513. (insert txt)
  12514. (goto-char (point-min))
  12515. (when (and (eq org-remember-interactive-interface 'refile)
  12516. (not fastp))
  12517. (org-refile nil (or visiting (find-file-noselect file)))
  12518. (throw 'quit t))
  12519. ;; Find the file
  12520. (if (not visiting) (find-file-noselect file))
  12521. (with-current-buffer (or visiting (get-file-buffer file))
  12522. (unless (org-mode-p)
  12523. (error "Target files for remember notes must be in Org-mode"))
  12524. (save-excursion
  12525. (save-restriction
  12526. (widen)
  12527. (and (goto-char (point-min))
  12528. (not (re-search-forward "^\\* " nil t))
  12529. (insert "\n* " (or heading "Notes") "\n"))
  12530. (setq reversed (org-notes-order-reversed-p))
  12531. ;; Find the default location
  12532. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12533. (goto-char (point-min))
  12534. (if (re-search-forward
  12535. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12536. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12537. nil t)
  12538. (setq org-goto-start-pos (match-beginning 0))
  12539. (when fastp
  12540. (goto-char (point-max))
  12541. (unless (bolp) (newline))
  12542. (insert "* " heading "\n")
  12543. (setq org-goto-start-pos (point-at-bol 0)))))
  12544. ;; Ask the User for a location, using the appropriate interface
  12545. (cond
  12546. (fastp (setq spos org-goto-start-pos
  12547. exitcmd 'return))
  12548. ((eq org-remember-interactive-interface 'outline)
  12549. (setq spos (org-get-location (current-buffer)
  12550. org-remember-help)
  12551. exitcmd (cdr spos)
  12552. spos (car spos)))
  12553. ((eq org-remember-interactive-interface 'outline-path-completion)
  12554. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12555. (org-refile-use-outline-path t))
  12556. (setq spos (org-refile-get-location "Heading: ")
  12557. exitcmd 'return
  12558. spos (nth 3 spos))))
  12559. (t (error "this should not hapen")))
  12560. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12561. ; not handle this note
  12562. (goto-char spos)
  12563. (cond ((org-on-heading-p t)
  12564. (org-back-to-heading t)
  12565. (setq level (funcall outline-level))
  12566. (cond
  12567. ((eq exitcmd 'return)
  12568. ;; sublevel of current
  12569. (setq org-remember-previous-location
  12570. (cons (abbreviate-file-name file)
  12571. (org-get-heading 'notags)))
  12572. (if reversed
  12573. (outline-next-heading)
  12574. (org-end-of-subtree t)
  12575. (if (not (bolp))
  12576. (if (looking-at "[ \t]*\n")
  12577. (beginning-of-line 2)
  12578. (end-of-line 1)
  12579. (insert "\n"))))
  12580. (bookmark-set "org-remember-last-stored")
  12581. (org-paste-subtree (org-get-legal-level level 1) txt))
  12582. ((eq exitcmd 'left)
  12583. ;; before current
  12584. (bookmark-set "org-remember-last-stored")
  12585. (org-paste-subtree level txt))
  12586. ((eq exitcmd 'right)
  12587. ;; after current
  12588. (org-end-of-subtree t)
  12589. (bookmark-set "org-remember-last-stored")
  12590. (org-paste-subtree level txt))
  12591. (t (error "This should not happen"))))
  12592. ((and (bobp) (not reversed))
  12593. ;; Put it at the end, one level below level 1
  12594. (save-restriction
  12595. (widen)
  12596. (goto-char (point-max))
  12597. (if (not (bolp)) (newline))
  12598. (bookmark-set "org-remember-last-stored")
  12599. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12600. ((and (bobp) reversed)
  12601. ;; Put it at the start, as level 1
  12602. (save-restriction
  12603. (widen)
  12604. (goto-char (point-min))
  12605. (re-search-forward "^\\*+ " nil t)
  12606. (beginning-of-line 1)
  12607. (bookmark-set "org-remember-last-stored")
  12608. (org-paste-subtree 1 txt)))
  12609. (t
  12610. ;; Put it right there, with automatic level determined by
  12611. ;; org-paste-subtree or from prefix arg
  12612. (bookmark-set "org-remember-last-stored")
  12613. (org-paste-subtree
  12614. (if (numberp current-prefix-arg) current-prefix-arg)
  12615. txt)))
  12616. (when remember-save-after-remembering
  12617. (save-buffer)
  12618. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12619. t) ;; return t to indicate that we took care of this note.
  12620. (defun org-get-org-file ()
  12621. "Read a filename, with default directory `org-directory'."
  12622. (let ((default (or org-default-notes-file remember-data-file)))
  12623. (read-file-name (format "File name [%s]: " default)
  12624. (file-name-as-directory org-directory)
  12625. default)))
  12626. (defun org-notes-order-reversed-p ()
  12627. "Check if the current file should receive notes in reversed order."
  12628. (cond
  12629. ((not org-reverse-note-order) nil)
  12630. ((eq t org-reverse-note-order) t)
  12631. ((not (listp org-reverse-note-order)) nil)
  12632. (t (catch 'exit
  12633. (let ((all org-reverse-note-order)
  12634. entry)
  12635. (while (setq entry (pop all))
  12636. (if (string-match (car entry) buffer-file-name)
  12637. (throw 'exit (cdr entry))))
  12638. nil)))))
  12639. ;;; Refiling
  12640. (defvar org-refile-target-table nil
  12641. "The list of refile targets, created by `org-refile'.")
  12642. (defvar org-agenda-new-buffers nil
  12643. "Buffers created to visit agenda files.")
  12644. (defun org-get-refile-targets (&optional default-buffer)
  12645. "Produce a table with refile targets."
  12646. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12647. targets txt re files f desc descre)
  12648. (with-current-buffer (or default-buffer (current-buffer))
  12649. (while (setq entry (pop entries))
  12650. (setq files (car entry) desc (cdr entry))
  12651. (cond
  12652. ((null files) (setq files (list (current-buffer))))
  12653. ((eq files 'org-agenda-files)
  12654. (setq files (org-agenda-files 'unrestricted)))
  12655. ((and (symbolp files) (fboundp files))
  12656. (setq files (funcall files)))
  12657. ((and (symbolp files) (boundp files))
  12658. (setq files (symbol-value files))))
  12659. (if (stringp files) (setq files (list files)))
  12660. (cond
  12661. ((eq (car desc) :tag)
  12662. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12663. ((eq (car desc) :todo)
  12664. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12665. ((eq (car desc) :regexp)
  12666. (setq descre (cdr desc)))
  12667. ((eq (car desc) :level)
  12668. (setq descre (concat "^\\*\\{" (number-to-string
  12669. (if org-odd-levels-only
  12670. (1- (* 2 (cdr desc)))
  12671. (cdr desc)))
  12672. "\\}[ \t]")))
  12673. ((eq (car desc) :maxlevel)
  12674. (setq descre (concat "^\\*\\{1," (number-to-string
  12675. (if org-odd-levels-only
  12676. (1- (* 2 (cdr desc)))
  12677. (cdr desc)))
  12678. "\\}[ \t]")))
  12679. (t (error "Bad refiling target description %s" desc)))
  12680. (while (setq f (pop files))
  12681. (save-excursion
  12682. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12683. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12684. (save-excursion
  12685. (save-restriction
  12686. (widen)
  12687. (goto-char (point-min))
  12688. (while (re-search-forward descre nil t)
  12689. (goto-char (point-at-bol))
  12690. (when (looking-at org-complex-heading-regexp)
  12691. (setq txt (match-string 4)
  12692. re (concat "^" (regexp-quote
  12693. (buffer-substring (match-beginning 1)
  12694. (match-end 4)))))
  12695. (if (match-end 5) (setq re (concat re "[ \t]+"
  12696. (regexp-quote
  12697. (match-string 5)))))
  12698. (setq re (concat re "[ \t]*$"))
  12699. (when org-refile-use-outline-path
  12700. (setq txt (mapconcat 'identity
  12701. (append
  12702. (if (eq org-refile-use-outline-path 'file)
  12703. (list (file-name-nondirectory
  12704. (buffer-file-name (buffer-base-buffer))))
  12705. (if (eq org-refile-use-outline-path 'full-file-path)
  12706. (list (buffer-file-name (buffer-base-buffer)))))
  12707. (org-get-outline-path)
  12708. (list txt))
  12709. "/")))
  12710. (push (list txt f re (point)) targets))
  12711. (goto-char (point-at-eol))))))))
  12712. (nreverse targets))))
  12713. (defun org-get-outline-path ()
  12714. "Return the outline path to the current entry, as a list."
  12715. (let (rtn)
  12716. (save-excursion
  12717. (while (org-up-heading-safe)
  12718. (when (looking-at org-complex-heading-regexp)
  12719. (push (org-match-string-no-properties 4) rtn)))
  12720. rtn)))
  12721. (defvar org-refile-history nil
  12722. "History for refiling operations.")
  12723. (defun org-refile (&optional goto default-buffer)
  12724. "Move the entry at point to another heading.
  12725. The list of target headings is compiled using the information in
  12726. `org-refile-targets', which see. This list is created upon first use, and
  12727. you can update it by calling this command with a double prefix (`C-u C-u').
  12728. FIXME: Can we find a better way of updating?
  12729. At the target location, the entry is filed as a subitem of the target heading.
  12730. Depending on `org-reverse-note-order', the new subitem will either be the
  12731. first of the last subitem.
  12732. With prefix are GOTO, the command will only visit the target location,
  12733. not actually move anything.
  12734. With a double prefix `C-c C-c', go to the location where the last refiling
  12735. operation has put the subtree.
  12736. With a double prefix argument, the command can be used to jump to any
  12737. heading in the current buffer."
  12738. (interactive "P")
  12739. (let* ((cbuf (current-buffer))
  12740. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12741. (fname (and filename (file-truename filename)))
  12742. pos it nbuf file re level reversed)
  12743. (if (equal goto '(16))
  12744. (org-refile-goto-last-stored)
  12745. (when (setq it (org-refile-get-location
  12746. (if goto "Goto: " "Refile to: ") default-buffer))
  12747. (setq file (nth 1 it)
  12748. re (nth 2 it)
  12749. pos (nth 3 it))
  12750. (setq nbuf (or (find-buffer-visiting file)
  12751. (find-file-noselect file)))
  12752. (if goto
  12753. (progn
  12754. (switch-to-buffer nbuf)
  12755. (goto-char pos)
  12756. (org-show-context 'org-goto))
  12757. (org-copy-special)
  12758. (save-excursion
  12759. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12760. (find-file-noselect file))))
  12761. (setq reversed (org-notes-order-reversed-p))
  12762. (save-excursion
  12763. (save-restriction
  12764. (widen)
  12765. (goto-char pos)
  12766. (looking-at outline-regexp)
  12767. (setq level (org-get-legal-level (funcall outline-level) 1))
  12768. (goto-char (or (save-excursion
  12769. (if reversed
  12770. (outline-next-heading)
  12771. (outline-get-next-sibling)))
  12772. (point-max)))
  12773. (bookmark-set "org-refile-last-stored")
  12774. (org-paste-subtree level))))
  12775. (org-cut-special)
  12776. (message "Entry refiled to \"%s\"" (car it)))))))
  12777. (defun org-refile-goto-last-stored ()
  12778. "Go to the location where the last refile was stored."
  12779. (interactive)
  12780. (bookmark-jump "org-refile-last-stored")
  12781. (message "This is the location of the last refile"))
  12782. (defun org-refile-get-location (&optional prompt default-buffer)
  12783. "Prompt the user for a refile location, using PROMPT."
  12784. (let ((org-refile-targets org-refile-targets)
  12785. (org-refile-use-outline-path org-refile-use-outline-path))
  12786. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12787. (unless org-refile-target-table
  12788. (error "No refile targets"))
  12789. (let* ((cbuf (current-buffer))
  12790. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12791. (fname (and filename (file-truename filename)))
  12792. (tbl (mapcar
  12793. (lambda (x)
  12794. (if (not (equal fname (file-truename (nth 1 x))))
  12795. (cons (concat (car x) " (" (file-name-nondirectory
  12796. (nth 1 x)) ")")
  12797. (cdr x))
  12798. x))
  12799. org-refile-target-table))
  12800. (completion-ignore-case t)
  12801. pos it nbuf file re level reversed)
  12802. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12803. tbl)))
  12804. ;;;; Dynamic blocks
  12805. (defun org-find-dblock (name)
  12806. "Find the first dynamic block with name NAME in the buffer.
  12807. If not found, stay at current position and return nil."
  12808. (let (pos)
  12809. (save-excursion
  12810. (goto-char (point-min))
  12811. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12812. nil t)
  12813. (match-beginning 0))))
  12814. (if pos (goto-char pos))
  12815. pos))
  12816. (defconst org-dblock-start-re
  12817. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12818. "Matches the startline of a dynamic block, with parameters.")
  12819. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12820. "Matches the end of a dyhamic block.")
  12821. (defun org-create-dblock (plist)
  12822. "Create a dynamic block section, with parameters taken from PLIST.
  12823. PLIST must containe a :name entry which is used as name of the block."
  12824. (unless (bolp) (newline))
  12825. (let ((name (plist-get plist :name)))
  12826. (insert "#+BEGIN: " name)
  12827. (while plist
  12828. (if (eq (car plist) :name)
  12829. (setq plist (cddr plist))
  12830. (insert " " (prin1-to-string (pop plist)))))
  12831. (insert "\n\n#+END:\n")
  12832. (beginning-of-line -2)))
  12833. (defun org-prepare-dblock ()
  12834. "Prepare dynamic block for refresh.
  12835. This empties the block, puts the cursor at the insert position and returns
  12836. the property list including an extra property :name with the block name."
  12837. (unless (looking-at org-dblock-start-re)
  12838. (error "Not at a dynamic block"))
  12839. (let* ((begdel (1+ (match-end 0)))
  12840. (name (org-no-properties (match-string 1)))
  12841. (params (append (list :name name)
  12842. (read (concat "(" (match-string 3) ")")))))
  12843. (unless (re-search-forward org-dblock-end-re nil t)
  12844. (error "Dynamic block not terminated"))
  12845. (delete-region begdel (match-beginning 0))
  12846. (goto-char begdel)
  12847. (open-line 1)
  12848. params))
  12849. (defun org-map-dblocks (&optional command)
  12850. "Apply COMMAND to all dynamic blocks in the current buffer.
  12851. If COMMAND is not given, use `org-update-dblock'."
  12852. (let ((cmd (or command 'org-update-dblock))
  12853. pos)
  12854. (save-excursion
  12855. (goto-char (point-min))
  12856. (while (re-search-forward org-dblock-start-re nil t)
  12857. (goto-char (setq pos (match-beginning 0)))
  12858. (condition-case nil
  12859. (funcall cmd)
  12860. (error (message "Error during update of dynamic block")))
  12861. (goto-char pos)
  12862. (unless (re-search-forward org-dblock-end-re nil t)
  12863. (error "Dynamic block not terminated"))))))
  12864. (defun org-dblock-update (&optional arg)
  12865. "User command for updating dynamic blocks.
  12866. Update the dynamic block at point. With prefix ARG, update all dynamic
  12867. blocks in the buffer."
  12868. (interactive "P")
  12869. (if arg
  12870. (org-update-all-dblocks)
  12871. (or (looking-at org-dblock-start-re)
  12872. (org-beginning-of-dblock))
  12873. (org-update-dblock)))
  12874. (defun org-update-dblock ()
  12875. "Update the dynamic block at point
  12876. This means to empty the block, parse for parameters and then call
  12877. the correct writing function."
  12878. (save-window-excursion
  12879. (let* ((pos (point))
  12880. (line (org-current-line))
  12881. (params (org-prepare-dblock))
  12882. (name (plist-get params :name))
  12883. (cmd (intern (concat "org-dblock-write:" name))))
  12884. (message "Updating dynamic block `%s' at line %d..." name line)
  12885. (funcall cmd params)
  12886. (message "Updating dynamic block `%s' at line %d...done" name line)
  12887. (goto-char pos))))
  12888. (defun org-beginning-of-dblock ()
  12889. "Find the beginning of the dynamic block at point.
  12890. Error if there is no scuh block at point."
  12891. (let ((pos (point))
  12892. beg)
  12893. (end-of-line 1)
  12894. (if (and (re-search-backward org-dblock-start-re nil t)
  12895. (setq beg (match-beginning 0))
  12896. (re-search-forward org-dblock-end-re nil t)
  12897. (> (match-end 0) pos))
  12898. (goto-char beg)
  12899. (goto-char pos)
  12900. (error "Not in a dynamic block"))))
  12901. (defun org-update-all-dblocks ()
  12902. "Update all dynamic blocks in the buffer.
  12903. This function can be used in a hook."
  12904. (when (org-mode-p)
  12905. (org-map-dblocks 'org-update-dblock)))
  12906. ;;;; Completion
  12907. (defconst org-additional-option-like-keywords
  12908. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12909. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12910. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12911. (defun org-complete (&optional arg)
  12912. "Perform completion on word at point.
  12913. At the beginning of a headline, this completes TODO keywords as given in
  12914. `org-todo-keywords'.
  12915. If the current word is preceded by a backslash, completes the TeX symbols
  12916. that are supported for HTML support.
  12917. If the current word is preceded by \"#+\", completes special words for
  12918. setting file options.
  12919. In the line after \"#+STARTUP:, complete valid keywords.\"
  12920. At all other locations, this simply calls the value of
  12921. `org-completion-fallback-command'."
  12922. (interactive "P")
  12923. (org-without-partial-completion
  12924. (catch 'exit
  12925. (let* ((end (point))
  12926. (beg1 (save-excursion
  12927. (skip-chars-backward (org-re "[:alnum:]_@"))
  12928. (point)))
  12929. (beg (save-excursion
  12930. (skip-chars-backward "a-zA-Z0-9_:$")
  12931. (point)))
  12932. (confirm (lambda (x) (stringp (car x))))
  12933. (searchhead (equal (char-before beg) ?*))
  12934. (tag (and (equal (char-before beg1) ?:)
  12935. (equal (char-after (point-at-bol)) ?*)))
  12936. (prop (and (equal (char-before beg1) ?:)
  12937. (not (equal (char-after (point-at-bol)) ?*))))
  12938. (texp (equal (char-before beg) ?\\))
  12939. (link (equal (char-before beg) ?\[))
  12940. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12941. beg)
  12942. "#+"))
  12943. (startup (string-match "^#\\+STARTUP:.*"
  12944. (buffer-substring (point-at-bol) (point))))
  12945. (completion-ignore-case opt)
  12946. (type nil)
  12947. (tbl nil)
  12948. (table (cond
  12949. (opt
  12950. (setq type :opt)
  12951. (append
  12952. (mapcar
  12953. (lambda (x)
  12954. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12955. (cons (match-string 2 x) (match-string 1 x)))
  12956. (org-split-string (org-get-current-options) "\n"))
  12957. (mapcar 'list org-additional-option-like-keywords)))
  12958. (startup
  12959. (setq type :startup)
  12960. org-startup-options)
  12961. (link (append org-link-abbrev-alist-local
  12962. org-link-abbrev-alist))
  12963. (texp
  12964. (setq type :tex)
  12965. org-html-entities)
  12966. ((string-match "\\`\\*+[ \t]+\\'"
  12967. (buffer-substring (point-at-bol) beg))
  12968. (setq type :todo)
  12969. (mapcar 'list org-todo-keywords-1))
  12970. (searchhead
  12971. (setq type :searchhead)
  12972. (save-excursion
  12973. (goto-char (point-min))
  12974. (while (re-search-forward org-todo-line-regexp nil t)
  12975. (push (list
  12976. (org-make-org-heading-search-string
  12977. (match-string 3) t))
  12978. tbl)))
  12979. tbl)
  12980. (tag (setq type :tag beg beg1)
  12981. (or org-tag-alist (org-get-buffer-tags)))
  12982. (prop (setq type :prop beg beg1)
  12983. (mapcar 'list (org-buffer-property-keys)))
  12984. (t (progn
  12985. (call-interactively org-completion-fallback-command)
  12986. (throw 'exit nil)))))
  12987. (pattern (buffer-substring-no-properties beg end))
  12988. (completion (try-completion pattern table confirm)))
  12989. (cond ((eq completion t)
  12990. (if (not (assoc (upcase pattern) table))
  12991. (message "Already complete")
  12992. (if (equal type :opt)
  12993. (insert (substring (cdr (assoc (upcase pattern) table))
  12994. (length pattern)))
  12995. (if (memq type '(:tag :prop)) (insert ":")))))
  12996. ((null completion)
  12997. (message "Can't find completion for \"%s\"" pattern)
  12998. (ding))
  12999. ((not (string= pattern completion))
  13000. (delete-region beg end)
  13001. (if (string-match " +$" completion)
  13002. (setq completion (replace-match "" t t completion)))
  13003. (insert completion)
  13004. (if (get-buffer-window "*Completions*")
  13005. (delete-window (get-buffer-window "*Completions*")))
  13006. (if (assoc completion table)
  13007. (if (eq type :todo) (insert " ")
  13008. (if (memq type '(:tag :prop)) (insert ":"))))
  13009. (if (and (equal type :opt) (assoc completion table))
  13010. (message "%s" (substitute-command-keys
  13011. "Press \\[org-complete] again to insert example settings"))))
  13012. (t
  13013. (message "Making completion list...")
  13014. (let ((list (sort (all-completions pattern table confirm)
  13015. 'string<)))
  13016. (with-output-to-temp-buffer "*Completions*"
  13017. (condition-case nil
  13018. ;; Protection needed for XEmacs and emacs 21
  13019. (display-completion-list list pattern)
  13020. (error (display-completion-list list)))))
  13021. (message "Making completion list...%s" "done")))))))
  13022. ;;;; TODO, DEADLINE, Comments
  13023. (defun org-toggle-comment ()
  13024. "Change the COMMENT state of an entry."
  13025. (interactive)
  13026. (save-excursion
  13027. (org-back-to-heading)
  13028. (let (case-fold-search)
  13029. (if (looking-at (concat outline-regexp
  13030. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13031. (replace-match "" t t nil 1)
  13032. (if (looking-at outline-regexp)
  13033. (progn
  13034. (goto-char (match-end 0))
  13035. (insert org-comment-string " ")))))))
  13036. (defvar org-last-todo-state-is-todo nil
  13037. "This is non-nil when the last TODO state change led to a TODO state.
  13038. If the last change removed the TODO tag or switched to DONE, then
  13039. this is nil.")
  13040. (defvar org-setting-tags nil) ; dynamically skiped
  13041. ;; FIXME: better place
  13042. (defun org-property-or-variable-value (var &optional inherit)
  13043. "Check if there is a property fixing the value of VAR.
  13044. If yes, return this value. If not, return the current value of the variable."
  13045. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13046. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13047. (read prop)
  13048. (symbol-value var))))
  13049. (defun org-parse-local-options (string var)
  13050. "Parse STRING for startup setting relevant for variable VAR."
  13051. (let ((rtn (symbol-value var))
  13052. e opts)
  13053. (save-match-data
  13054. (if (or (not string) (not (string-match "\\S-" string)))
  13055. rtn
  13056. (setq opts (delq nil (mapcar (lambda (x)
  13057. (setq e (assoc x org-startup-options))
  13058. (if (eq (nth 1 e) var) e nil))
  13059. (org-split-string string "[ \t]+"))))
  13060. (if (not opts)
  13061. rtn
  13062. (setq rtn nil)
  13063. (while (setq e (pop opts))
  13064. (if (not (nth 3 e))
  13065. (setq rtn (nth 2 e))
  13066. (if (not (listp rtn)) (setq rtn nil))
  13067. (push (nth 2 e) rtn)))
  13068. rtn)))))
  13069. (defvar org-blocker-hook nil
  13070. "Hook for functions that are allowed to block a state change.
  13071. Each function gets as its single argument a property list, see
  13072. `org-trigger-hook' for more information about this list.
  13073. If any of the functions in this hook returns nil, the state change
  13074. is blocked.")
  13075. (defvar org-trigger-hook nil
  13076. "Hook for functions that are triggered by a state change.
  13077. Each function gets as its single argument a property list with at least
  13078. the following elements:
  13079. (:type type-of-change :position pos-at-entry-start
  13080. :from old-state :to new-state)
  13081. Depending on the type, more properties may be present.
  13082. This mechanism is currently implemented for:
  13083. TODO state changes
  13084. ------------------
  13085. :type todo-state-change
  13086. :from previous state (keyword as a string), or nil
  13087. :to new state (keyword as a string), or nil")
  13088. (defun org-todo (&optional arg)
  13089. "Change the TODO state of an item.
  13090. The state of an item is given by a keyword at the start of the heading,
  13091. like
  13092. *** TODO Write paper
  13093. *** DONE Call mom
  13094. The different keywords are specified in the variable `org-todo-keywords'.
  13095. By default the available states are \"TODO\" and \"DONE\".
  13096. So for this example: when the item starts with TODO, it is changed to DONE.
  13097. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13098. DONE are present, add TODO at the beginning of the heading.
  13099. With C-u prefix arg, use completion to determine the new state.
  13100. With numeric prefix arg, switch to that state.
  13101. For calling through lisp, arg is also interpreted in the following way:
  13102. 'none -> empty state
  13103. \"\"(empty string) -> switch to empty state
  13104. 'done -> switch to DONE
  13105. 'nextset -> switch to the next set of keywords
  13106. 'previousset -> switch to the previous set of keywords
  13107. \"WAITING\" -> switch to the specified keyword, but only if it
  13108. really is a member of `org-todo-keywords'."
  13109. (interactive "P")
  13110. (save-excursion
  13111. (catch 'exit
  13112. (org-back-to-heading)
  13113. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13114. (or (looking-at (concat " +" org-todo-regexp " *"))
  13115. (looking-at " *"))
  13116. (let* ((match-data (match-data))
  13117. (startpos (point-at-bol))
  13118. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13119. (org-log-done (org-parse-local-options logging 'org-log-done))
  13120. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  13121. (this (match-string 1))
  13122. (hl-pos (match-beginning 0))
  13123. (head (org-get-todo-sequence-head this))
  13124. (ass (assoc head org-todo-kwd-alist))
  13125. (interpret (nth 1 ass))
  13126. (done-word (nth 3 ass))
  13127. (final-done-word (nth 4 ass))
  13128. (last-state (or this ""))
  13129. (completion-ignore-case t)
  13130. (member (member this org-todo-keywords-1))
  13131. (tail (cdr member))
  13132. (state (cond
  13133. ((and org-todo-key-trigger
  13134. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13135. (and (not arg) org-use-fast-todo-selection
  13136. (not (eq org-use-fast-todo-selection 'prefix)))))
  13137. ;; Use fast selection
  13138. (org-fast-todo-selection))
  13139. ((and (equal arg '(4))
  13140. (or (not org-use-fast-todo-selection)
  13141. (not org-todo-key-trigger)))
  13142. ;; Read a state with completion
  13143. (completing-read "State: " (mapcar (lambda(x) (list x))
  13144. org-todo-keywords-1)
  13145. nil t))
  13146. ((eq arg 'right)
  13147. (if this
  13148. (if tail (car tail) nil)
  13149. (car org-todo-keywords-1)))
  13150. ((eq arg 'left)
  13151. (if (equal member org-todo-keywords-1)
  13152. nil
  13153. (if this
  13154. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13155. org-todo-keywords-1)
  13156. (org-last org-todo-keywords-1))))
  13157. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13158. (setq arg nil))) ; hack to fall back to cycling
  13159. (arg
  13160. ;; user or caller requests a specific state
  13161. (cond
  13162. ((equal arg "") nil)
  13163. ((eq arg 'none) nil)
  13164. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13165. ((eq arg 'nextset)
  13166. (or (car (cdr (member head org-todo-heads)))
  13167. (car org-todo-heads)))
  13168. ((eq arg 'previousset)
  13169. (let ((org-todo-heads (reverse org-todo-heads)))
  13170. (or (car (cdr (member head org-todo-heads)))
  13171. (car org-todo-heads))))
  13172. ((car (member arg org-todo-keywords-1)))
  13173. ((nth (1- (prefix-numeric-value arg))
  13174. org-todo-keywords-1))))
  13175. ((null member) (or head (car org-todo-keywords-1)))
  13176. ((equal this final-done-word) nil) ;; -> make empty
  13177. ((null tail) nil) ;; -> first entry
  13178. ((eq interpret 'sequence)
  13179. (car tail))
  13180. ((memq interpret '(type priority))
  13181. (if (eq this-command last-command)
  13182. (car tail)
  13183. (if (> (length tail) 0)
  13184. (or done-word (car org-done-keywords))
  13185. nil)))
  13186. (t nil)))
  13187. (next (if state (concat " " state " ") " "))
  13188. (change-plist (list :type 'todo-state-change :from this :to state
  13189. :position startpos))
  13190. dostates)
  13191. (when org-blocker-hook
  13192. (unless (save-excursion
  13193. (save-match-data
  13194. (run-hook-with-args-until-failure
  13195. 'org-blocker-hook change-plist)))
  13196. (if (interactive-p)
  13197. (error "TODO state change from %s to %s blocked" this state)
  13198. ;; fail silently
  13199. (message "TODO state change from %s to %s blocked" this state)
  13200. (throw 'exit nil))))
  13201. (store-match-data match-data)
  13202. (replace-match next t t)
  13203. (unless (pos-visible-in-window-p hl-pos)
  13204. (message "TODO state changed to %s" (org-trim next)))
  13205. (unless head
  13206. (setq head (org-get-todo-sequence-head state)
  13207. ass (assoc head org-todo-kwd-alist)
  13208. interpret (nth 1 ass)
  13209. done-word (nth 3 ass)
  13210. final-done-word (nth 4 ass)))
  13211. (when (memq arg '(nextset previousset))
  13212. (message "Keyword-Set %d/%d: %s"
  13213. (- (length org-todo-sets) -1
  13214. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13215. (length org-todo-sets)
  13216. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13217. (setq org-last-todo-state-is-todo
  13218. (not (member state org-done-keywords)))
  13219. (when (and org-log-done (not (memq arg '(nextset previousset))))
  13220. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  13221. (or (not org-todo-log-states)
  13222. (member state org-todo-log-states))))
  13223. (cond
  13224. ((and state (member state org-not-done-keywords)
  13225. (not (member this org-not-done-keywords)))
  13226. ;; This is now a todo state and was not one before
  13227. ;; Remove any CLOSED timestamp, and possibly log the state change
  13228. (org-add-planning-info nil nil 'closed)
  13229. (and dostates (org-add-log-maybe 'state state 'findpos)))
  13230. ((and state dostates)
  13231. ;; This is a non-nil state, and we need to log it
  13232. (org-add-log-maybe 'state state 'findpos))
  13233. ((and (member state org-done-keywords)
  13234. (not (member this org-done-keywords)))
  13235. ;; It is now done, and it was not done before
  13236. (org-add-planning-info 'closed (org-current-time))
  13237. (org-add-log-maybe 'done state 'findpos))))
  13238. ;; Fixup tag positioning
  13239. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13240. (run-hooks 'org-after-todo-state-change-hook)
  13241. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  13242. (if (and arg (not (member state org-done-keywords)))
  13243. (setq head (org-get-todo-sequence-head state)))
  13244. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13245. ;; Fixup cursor location if close to the keyword
  13246. (if (and (outline-on-heading-p)
  13247. (not (bolp))
  13248. (save-excursion (beginning-of-line 1)
  13249. (looking-at org-todo-line-regexp))
  13250. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13251. (progn
  13252. (goto-char (or (match-end 2) (match-end 1)))
  13253. (just-one-space)))
  13254. (when org-trigger-hook
  13255. (save-excursion
  13256. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13257. (defun org-get-todo-sequence-head (kwd)
  13258. "Return the head of the TODO sequence to which KWD belongs.
  13259. If KWD is not set, check if there is a text property remembering the
  13260. right sequence."
  13261. (let (p)
  13262. (cond
  13263. ((not kwd)
  13264. (or (get-text-property (point-at-bol) 'org-todo-head)
  13265. (progn
  13266. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13267. nil (point-at-eol)))
  13268. (get-text-property p 'org-todo-head))))
  13269. ((not (member kwd org-todo-keywords-1))
  13270. (car org-todo-keywords-1))
  13271. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13272. (defun org-fast-todo-selection ()
  13273. "Fast TODO keyword selection with single keys.
  13274. Returns the new TODO keyword, or nil if no state change should occur."
  13275. (let* ((fulltable org-todo-key-alist)
  13276. (done-keywords org-done-keywords) ;; needed for the faces.
  13277. (maxlen (apply 'max (mapcar
  13278. (lambda (x)
  13279. (if (stringp (car x)) (string-width (car x)) 0))
  13280. fulltable)))
  13281. (expert nil)
  13282. (fwidth (+ maxlen 3 1 3))
  13283. (ncol (/ (- (window-width) 4) fwidth))
  13284. tg cnt e c tbl
  13285. groups ingroup)
  13286. (save-window-excursion
  13287. (if expert
  13288. (set-buffer (get-buffer-create " *Org todo*"))
  13289. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13290. (erase-buffer)
  13291. (org-set-local 'org-done-keywords done-keywords)
  13292. (setq tbl fulltable cnt 0)
  13293. (while (setq e (pop tbl))
  13294. (cond
  13295. ((equal e '(:startgroup))
  13296. (push '() groups) (setq ingroup t)
  13297. (when (not (= cnt 0))
  13298. (setq cnt 0)
  13299. (insert "\n"))
  13300. (insert "{ "))
  13301. ((equal e '(:endgroup))
  13302. (setq ingroup nil cnt 0)
  13303. (insert "}\n"))
  13304. (t
  13305. (setq tg (car e) c (cdr e))
  13306. (if ingroup (push tg (car groups)))
  13307. (setq tg (org-add-props tg nil 'face
  13308. (org-get-todo-face tg)))
  13309. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13310. (insert "[" c "] " tg (make-string
  13311. (- fwidth 4 (length tg)) ?\ ))
  13312. (when (= (setq cnt (1+ cnt)) ncol)
  13313. (insert "\n")
  13314. (if ingroup (insert " "))
  13315. (setq cnt 0)))))
  13316. (insert "\n")
  13317. (goto-char (point-min))
  13318. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13319. (fit-window-to-buffer))
  13320. (message "[a-z..]:Set [SPC]:clear")
  13321. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13322. (cond
  13323. ((or (= c ?\C-g)
  13324. (and (= c ?q) (not (rassoc c fulltable))))
  13325. (setq quit-flag t))
  13326. ((= c ?\ ) nil)
  13327. ((setq e (rassoc c fulltable) tg (car e))
  13328. tg)
  13329. (t (setq quit-flag t))))))
  13330. (defun org-get-repeat ()
  13331. "Check if tere is a deadline/schedule with repeater in this entry."
  13332. (save-match-data
  13333. (save-excursion
  13334. (org-back-to-heading t)
  13335. (if (re-search-forward
  13336. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13337. (match-string 1)))))
  13338. (defvar org-last-changed-timestamp)
  13339. (defvar org-log-post-message)
  13340. (defun org-auto-repeat-maybe ()
  13341. "Check if the current headline contains a repeated deadline/schedule.
  13342. If yes, set TODO state back to what it was and change the base date
  13343. of repeating deadline/scheduled time stamps to new date.
  13344. This function should be run in the `org-after-todo-state-change-hook'."
  13345. ;; last-state is dynamically scoped into this function
  13346. (let* ((repeat (org-get-repeat))
  13347. (aa (assoc last-state org-todo-kwd-alist))
  13348. (interpret (nth 1 aa))
  13349. (head (nth 2 aa))
  13350. (done-word (nth 3 aa))
  13351. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13352. (msg "Entry repeats: ")
  13353. (org-log-done)
  13354. re type n what ts)
  13355. (when repeat
  13356. (org-todo (if (eq interpret 'type) last-state head))
  13357. (when (and org-log-repeat
  13358. (not (memq 'org-add-log-note
  13359. (default-value 'post-command-hook))))
  13360. ;; Make sure a note is taken
  13361. (let ((org-log-done '(done)))
  13362. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  13363. 'findpos)))
  13364. (org-back-to-heading t)
  13365. (org-add-planning-info nil nil 'closed)
  13366. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13367. org-deadline-time-regexp "\\)\\|\\("
  13368. org-ts-regexp "\\)"))
  13369. (while (re-search-forward
  13370. re (save-excursion (outline-next-heading) (point)) t)
  13371. (setq type (if (match-end 1) org-scheduled-string
  13372. (if (match-end 3) org-deadline-string "Plain:"))
  13373. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13374. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13375. (setq n (string-to-number (match-string 1 ts))
  13376. what (match-string 2 ts))
  13377. (if (equal what "w") (setq n (* n 7) what "d"))
  13378. (org-timestamp-change n (cdr (assoc what whata)))
  13379. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13380. (setq org-log-post-message msg)
  13381. (message "%s" msg))))
  13382. (defun org-show-todo-tree (arg)
  13383. "Make a compact tree which shows all headlines marked with TODO.
  13384. The tree will show the lines where the regexp matches, and all higher
  13385. headlines above the match.
  13386. With \\[universal-argument] prefix, also show the DONE entries.
  13387. With a numeric prefix N, construct a sparse tree for the Nth element
  13388. of `org-todo-keywords-1'."
  13389. (interactive "P")
  13390. (let ((case-fold-search nil)
  13391. (kwd-re
  13392. (cond ((null arg) org-not-done-regexp)
  13393. ((equal arg '(4))
  13394. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13395. (mapcar 'list org-todo-keywords-1))))
  13396. (concat "\\("
  13397. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13398. "\\)\\>")))
  13399. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13400. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13401. org-todo-keywords-1)))
  13402. (t (error "Invalid prefix argument: %s" arg)))))
  13403. (message "%d TODO entries found"
  13404. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13405. (defun org-deadline (&optional remove)
  13406. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13407. With argument REMOVE, remove any deadline from the item."
  13408. (interactive "P")
  13409. (if remove
  13410. (progn
  13411. (org-add-planning-info nil nil 'deadline)
  13412. (message "Item no longer has a deadline."))
  13413. (org-add-planning-info 'deadline nil 'closed)))
  13414. (defun org-schedule (&optional remove)
  13415. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13416. With argument REMOVE, remove any scheduling date from the item."
  13417. (interactive "P")
  13418. (if remove
  13419. (progn
  13420. (org-add-planning-info nil nil 'scheduled)
  13421. (message "Item is no longer scheduled."))
  13422. (org-add-planning-info 'scheduled nil 'closed)))
  13423. (defun org-add-planning-info (what &optional time &rest remove)
  13424. "Insert new timestamp with keyword in the line directly after the headline.
  13425. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13426. If non is given, the user is prompted for a date.
  13427. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13428. be removed."
  13429. (interactive)
  13430. (let (org-time-was-given org-end-time-was-given)
  13431. (when what (setq time (or time (org-read-date nil 'to-time))))
  13432. (when (and org-insert-labeled-timestamps-at-point
  13433. (member what '(scheduled deadline)))
  13434. (insert
  13435. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13436. (org-insert-time-stamp time org-time-was-given
  13437. nil nil nil (list org-end-time-was-given))
  13438. (setq what nil))
  13439. (save-excursion
  13440. (save-restriction
  13441. (let (col list elt ts buffer-invisibility-spec)
  13442. (org-back-to-heading t)
  13443. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13444. (goto-char (match-end 1))
  13445. (setq col (current-column))
  13446. (goto-char (match-end 0))
  13447. (if (eobp) (insert "\n") (forward-char 1))
  13448. (if (and (not (looking-at outline-regexp))
  13449. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13450. "[^\r\n]*"))
  13451. (not (equal (match-string 1) org-clock-string)))
  13452. (narrow-to-region (match-beginning 0) (match-end 0))
  13453. (insert-before-markers "\n")
  13454. (backward-char 1)
  13455. (narrow-to-region (point) (point))
  13456. (indent-to-column col))
  13457. ;; Check if we have to remove something.
  13458. (setq list (cons what remove))
  13459. (while list
  13460. (setq elt (pop list))
  13461. (goto-char (point-min))
  13462. (when (or (and (eq elt 'scheduled)
  13463. (re-search-forward org-scheduled-time-regexp nil t))
  13464. (and (eq elt 'deadline)
  13465. (re-search-forward org-deadline-time-regexp nil t))
  13466. (and (eq elt 'closed)
  13467. (re-search-forward org-closed-time-regexp nil t)))
  13468. (replace-match "")
  13469. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13470. (if (looking-at " +") (replace-match ""))))
  13471. (goto-char (point-max))
  13472. (when what
  13473. (insert
  13474. (if (not (equal (char-before) ?\ )) " " "")
  13475. (cond ((eq what 'scheduled) org-scheduled-string)
  13476. ((eq what 'deadline) org-deadline-string)
  13477. ((eq what 'closed) org-closed-string))
  13478. " ")
  13479. (setq ts (org-insert-time-stamp
  13480. time
  13481. (or org-time-was-given
  13482. (and (eq what 'closed) org-log-done-with-time))
  13483. (eq what 'closed)
  13484. nil nil (list org-end-time-was-given)))
  13485. (end-of-line 1))
  13486. (goto-char (point-min))
  13487. (widen)
  13488. (if (looking-at "[ \t]+\r?\n")
  13489. (replace-match ""))
  13490. ts)))))
  13491. (defvar org-log-note-marker (make-marker))
  13492. (defvar org-log-note-purpose nil)
  13493. (defvar org-log-note-state nil)
  13494. (defvar org-log-note-window-configuration nil)
  13495. (defvar org-log-note-return-to (make-marker))
  13496. (defvar org-log-post-message nil
  13497. "Message to be displayed after a log note has been stored.
  13498. The auto-repeater uses this.")
  13499. (defun org-add-log-maybe (&optional purpose state findpos)
  13500. "Set up the post command hook to take a note."
  13501. (save-excursion
  13502. (when (and (listp org-log-done)
  13503. (memq purpose org-log-done))
  13504. (when findpos
  13505. (org-back-to-heading t)
  13506. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13507. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13508. "[^\r\n]*\\)?"))
  13509. (goto-char (match-end 0))
  13510. (unless org-log-states-order-reversed
  13511. (and (= (char-after) ?\n) (forward-char 1))
  13512. (org-skip-over-state-notes)
  13513. (skip-chars-backward " \t\n\r")))
  13514. (move-marker org-log-note-marker (point))
  13515. (setq org-log-note-purpose purpose)
  13516. (setq org-log-note-state state)
  13517. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  13518. (defun org-skip-over-state-notes ()
  13519. "Skip past the list of State notes in an entry."
  13520. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13521. (while (looking-at "[ \t]*- State")
  13522. (condition-case nil
  13523. (org-next-item)
  13524. (error (org-end-of-item)))))
  13525. (defun org-add-log-note (&optional purpose)
  13526. "Pop up a window for taking a note, and add this note later at point."
  13527. (remove-hook 'post-command-hook 'org-add-log-note)
  13528. (setq org-log-note-window-configuration (current-window-configuration))
  13529. (delete-other-windows)
  13530. (move-marker org-log-note-return-to (point))
  13531. (switch-to-buffer (marker-buffer org-log-note-marker))
  13532. (goto-char org-log-note-marker)
  13533. (org-switch-to-buffer-other-window "*Org Note*")
  13534. (erase-buffer)
  13535. (let ((org-inhibit-startup t)) (org-mode))
  13536. (insert (format "# Insert note for %s.
  13537. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13538. (cond
  13539. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13540. ((eq org-log-note-purpose 'done) "closed todo item")
  13541. ((eq org-log-note-purpose 'state)
  13542. (format "state change to \"%s\"" org-log-note-state))
  13543. (t (error "This should not happen")))))
  13544. (org-set-local 'org-finish-function 'org-store-log-note))
  13545. (defun org-store-log-note ()
  13546. "Finish taking a log note, and insert it to where it belongs."
  13547. (let ((txt (buffer-string))
  13548. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13549. lines ind)
  13550. (kill-buffer (current-buffer))
  13551. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13552. (setq txt (replace-match "" t t txt)))
  13553. (if (string-match "\\s-+\\'" txt)
  13554. (setq txt (replace-match "" t t txt)))
  13555. (setq lines (org-split-string txt "\n"))
  13556. (when (and note (string-match "\\S-" note))
  13557. (setq note
  13558. (org-replace-escapes
  13559. note
  13560. (list (cons "%u" (user-login-name))
  13561. (cons "%U" user-full-name)
  13562. (cons "%t" (format-time-string
  13563. (org-time-stamp-format 'long 'inactive)
  13564. (current-time)))
  13565. (cons "%s" (if org-log-note-state
  13566. (concat "\"" org-log-note-state "\"")
  13567. "")))))
  13568. (if lines (setq note (concat note " \\\\")))
  13569. (push note lines))
  13570. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13571. (when lines
  13572. (save-excursion
  13573. (set-buffer (marker-buffer org-log-note-marker))
  13574. (save-excursion
  13575. (goto-char org-log-note-marker)
  13576. (move-marker org-log-note-marker nil)
  13577. (end-of-line 1)
  13578. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13579. (indent-relative nil)
  13580. (insert "- " (pop lines))
  13581. (org-indent-line-function)
  13582. (beginning-of-line 1)
  13583. (looking-at "[ \t]*")
  13584. (setq ind (concat (match-string 0) " "))
  13585. (end-of-line 1)
  13586. (while lines (insert "\n" ind (pop lines)))))))
  13587. (set-window-configuration org-log-note-window-configuration)
  13588. (with-current-buffer (marker-buffer org-log-note-return-to)
  13589. (goto-char org-log-note-return-to))
  13590. (move-marker org-log-note-return-to nil)
  13591. (and org-log-post-message (message "%s" org-log-post-message)))
  13592. ;; FIXME: what else would be useful?
  13593. ;; - priority
  13594. ;; - date
  13595. (defun org-sparse-tree (&optional arg)
  13596. "Create a sparse tree, prompt for the details.
  13597. This command can create sparse trees. You first need to select the type
  13598. of match used to create the tree:
  13599. t Show entries with a specific TODO keyword.
  13600. T Show entries selected by a tags match.
  13601. p Enter a property name and its value (both with completion on existing
  13602. names/values) and show entries with that property.
  13603. r Show entries matching a regular expression
  13604. d Show deadlines due within `org-deadline-warning-days'."
  13605. (interactive "P")
  13606. (let (ans kwd value)
  13607. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13608. (setq ans (read-char-exclusive))
  13609. (cond
  13610. ((equal ans ?d)
  13611. (call-interactively 'org-check-deadlines))
  13612. ((equal ans ?b)
  13613. (call-interactively 'org-check-before-date))
  13614. ((equal ans ?t)
  13615. (org-show-todo-tree '(4)))
  13616. ((equal ans ?T)
  13617. (call-interactively 'org-tags-sparse-tree))
  13618. ((member ans '(?p ?P))
  13619. (setq kwd (completing-read "Property: "
  13620. (mapcar 'list (org-buffer-property-keys))))
  13621. (setq value (completing-read "Value: "
  13622. (mapcar 'list (org-property-values kwd))))
  13623. (unless (string-match "\\`{.*}\\'" value)
  13624. (setq value (concat "\"" value "\"")))
  13625. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13626. ((member ans '(?r ?R ?/))
  13627. (call-interactively 'org-occur))
  13628. (t (error "No such sparse tree command \"%c\"" ans)))))
  13629. (defvar org-occur-highlights nil)
  13630. (make-variable-buffer-local 'org-occur-highlights)
  13631. (defun org-occur (regexp &optional keep-previous callback)
  13632. "Make a compact tree which shows all matches of REGEXP.
  13633. The tree will show the lines where the regexp matches, and all higher
  13634. headlines above the match. It will also show the heading after the match,
  13635. to make sure editing the matching entry is easy.
  13636. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13637. call to `org-occur' will be kept, to allow stacking of calls to this
  13638. command.
  13639. If CALLBACK is non-nil, it is a function which is called to confirm
  13640. that the match should indeed be shown."
  13641. (interactive "sRegexp: \nP")
  13642. (or keep-previous (org-remove-occur-highlights nil nil t))
  13643. (let ((cnt 0))
  13644. (save-excursion
  13645. (goto-char (point-min))
  13646. (if (or (not keep-previous) ; do not want to keep
  13647. (not org-occur-highlights)) ; no previous matches
  13648. ;; hide everything
  13649. (org-overview))
  13650. (while (re-search-forward regexp nil t)
  13651. (when (or (not callback)
  13652. (save-match-data (funcall callback)))
  13653. (setq cnt (1+ cnt))
  13654. (when org-highlight-sparse-tree-matches
  13655. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13656. (org-show-context 'occur-tree))))
  13657. (when org-remove-highlights-with-change
  13658. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13659. nil 'local))
  13660. (unless org-sparse-tree-open-archived-trees
  13661. (org-hide-archived-subtrees (point-min) (point-max)))
  13662. (run-hooks 'org-occur-hook)
  13663. (if (interactive-p)
  13664. (message "%d match(es) for regexp %s" cnt regexp))
  13665. cnt))
  13666. (defun org-show-context (&optional key)
  13667. "Make sure point and context and visible.
  13668. How much context is shown depends upon the variables
  13669. `org-show-hierarchy-above', `org-show-following-heading'. and
  13670. `org-show-siblings'."
  13671. (let ((heading-p (org-on-heading-p t))
  13672. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13673. (following-p (org-get-alist-option org-show-following-heading key))
  13674. (entry-p (org-get-alist-option org-show-entry-below key))
  13675. (siblings-p (org-get-alist-option org-show-siblings key)))
  13676. (catch 'exit
  13677. ;; Show heading or entry text
  13678. (if (and heading-p (not entry-p))
  13679. (org-flag-heading nil) ; only show the heading
  13680. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13681. (org-show-hidden-entry))) ; show entire entry
  13682. (when following-p
  13683. ;; Show next sibling, or heading below text
  13684. (save-excursion
  13685. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13686. (org-flag-heading nil))))
  13687. (when siblings-p (org-show-siblings))
  13688. (when hierarchy-p
  13689. ;; show all higher headings, possibly with siblings
  13690. (save-excursion
  13691. (while (and (condition-case nil
  13692. (progn (org-up-heading-all 1) t)
  13693. (error nil))
  13694. (not (bobp)))
  13695. (org-flag-heading nil)
  13696. (when siblings-p (org-show-siblings))))))))
  13697. (defun org-reveal (&optional siblings)
  13698. "Show current entry, hierarchy above it, and the following headline.
  13699. This can be used to show a consistent set of context around locations
  13700. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13701. not t for the search context.
  13702. With optional argument SIBLINGS, on each level of the hierarchy all
  13703. siblings are shown. This repairs the tree structure to what it would
  13704. look like when opened with hierarchical calls to `org-cycle'."
  13705. (interactive "P")
  13706. (let ((org-show-hierarchy-above t)
  13707. (org-show-following-heading t)
  13708. (org-show-siblings (if siblings t org-show-siblings)))
  13709. (org-show-context nil)))
  13710. (defun org-highlight-new-match (beg end)
  13711. "Highlight from BEG to END and mark the highlight is an occur headline."
  13712. (let ((ov (org-make-overlay beg end)))
  13713. (org-overlay-put ov 'face 'secondary-selection)
  13714. (push ov org-occur-highlights)))
  13715. (defun org-remove-occur-highlights (&optional beg end noremove)
  13716. "Remove the occur highlights from the buffer.
  13717. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13718. from the `before-change-functions' in the current buffer."
  13719. (interactive)
  13720. (unless org-inhibit-highlight-removal
  13721. (mapc 'org-delete-overlay org-occur-highlights)
  13722. (setq org-occur-highlights nil)
  13723. (unless noremove
  13724. (remove-hook 'before-change-functions
  13725. 'org-remove-occur-highlights 'local))))
  13726. ;;;; Priorities
  13727. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13728. "Regular expression matching the priority indicator.")
  13729. (defvar org-remove-priority-next-time nil)
  13730. (defun org-priority-up ()
  13731. "Increase the priority of the current item."
  13732. (interactive)
  13733. (org-priority 'up))
  13734. (defun org-priority-down ()
  13735. "Decrease the priority of the current item."
  13736. (interactive)
  13737. (org-priority 'down))
  13738. (defun org-priority (&optional action)
  13739. "Change the priority of an item by ARG.
  13740. ACTION can be `set', `up', `down', or a character."
  13741. (interactive)
  13742. (setq action (or action 'set))
  13743. (let (current new news have remove)
  13744. (save-excursion
  13745. (org-back-to-heading)
  13746. (if (looking-at org-priority-regexp)
  13747. (setq current (string-to-char (match-string 2))
  13748. have t)
  13749. (setq current org-default-priority))
  13750. (cond
  13751. ((or (eq action 'set) (integerp action))
  13752. (if (integerp action)
  13753. (setq new action)
  13754. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13755. (setq new (read-char-exclusive)))
  13756. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13757. (= (upcase org-lowest-priority) org-lowest-priority))
  13758. (setq new (upcase new)))
  13759. (cond ((equal new ?\ ) (setq remove t))
  13760. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13761. (error "Priority must be between `%c' and `%c'"
  13762. org-highest-priority org-lowest-priority))))
  13763. ((eq action 'up)
  13764. (if (and (not have) (eq last-command this-command))
  13765. (setq new org-lowest-priority)
  13766. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13767. org-default-priority (1- current)))))
  13768. ((eq action 'down)
  13769. (if (and (not have) (eq last-command this-command))
  13770. (setq new org-highest-priority)
  13771. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13772. org-default-priority (1+ current)))))
  13773. (t (error "Invalid action")))
  13774. (if (or (< (upcase new) org-highest-priority)
  13775. (> (upcase new) org-lowest-priority))
  13776. (setq remove t))
  13777. (setq news (format "%c" new))
  13778. (if have
  13779. (if remove
  13780. (replace-match "" t t nil 1)
  13781. (replace-match news t t nil 2))
  13782. (if remove
  13783. (error "No priority cookie found in line")
  13784. (looking-at org-todo-line-regexp)
  13785. (if (match-end 2)
  13786. (progn
  13787. (goto-char (match-end 2))
  13788. (insert " [#" news "]"))
  13789. (goto-char (match-beginning 3))
  13790. (insert "[#" news "] ")))))
  13791. (org-preserve-lc (org-set-tags nil 'align))
  13792. (if remove
  13793. (message "Priority removed")
  13794. (message "Priority of current item set to %s" news))))
  13795. (defun org-get-priority (s)
  13796. "Find priority cookie and return priority."
  13797. (save-match-data
  13798. (if (not (string-match org-priority-regexp s))
  13799. (* 1000 (- org-lowest-priority org-default-priority))
  13800. (* 1000 (- org-lowest-priority
  13801. (string-to-char (match-string 2 s)))))))
  13802. ;;;; Tags
  13803. (defun org-scan-tags (action matcher &optional todo-only)
  13804. "Scan headline tags with inheritance and produce output ACTION.
  13805. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13806. evaluated, testing if a given set of tags qualifies a headline for
  13807. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13808. are included in the output."
  13809. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13810. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13811. (org-re
  13812. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13813. (props (list 'face nil
  13814. 'done-face 'org-done
  13815. 'undone-face nil
  13816. 'mouse-face 'highlight
  13817. 'org-not-done-regexp org-not-done-regexp
  13818. 'org-todo-regexp org-todo-regexp
  13819. 'keymap org-agenda-keymap
  13820. 'help-echo
  13821. (format "mouse-2 or RET jump to org file %s"
  13822. (abbreviate-file-name
  13823. (or (buffer-file-name (buffer-base-buffer))
  13824. (buffer-name (buffer-base-buffer)))))))
  13825. (case-fold-search nil)
  13826. lspos
  13827. tags tags-list tags-alist (llast 0) rtn level category i txt
  13828. todo marker entry priority)
  13829. (save-excursion
  13830. (goto-char (point-min))
  13831. (when (eq action 'sparse-tree)
  13832. (org-overview)
  13833. (org-remove-occur-highlights))
  13834. (while (re-search-forward re nil t)
  13835. (catch :skip
  13836. (setq todo (if (match-end 1) (match-string 2))
  13837. tags (if (match-end 4) (match-string 4)))
  13838. (goto-char (setq lspos (1+ (match-beginning 0))))
  13839. (setq level (org-reduced-level (funcall outline-level))
  13840. category (org-get-category))
  13841. (setq i llast llast level)
  13842. ;; remove tag lists from same and sublevels
  13843. (while (>= i level)
  13844. (when (setq entry (assoc i tags-alist))
  13845. (setq tags-alist (delete entry tags-alist)))
  13846. (setq i (1- i)))
  13847. ;; add the nex tags
  13848. (when tags
  13849. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13850. tags-alist
  13851. (cons (cons level tags) tags-alist)))
  13852. ;; compile tags for current headline
  13853. (setq tags-list
  13854. (if org-use-tag-inheritance
  13855. (apply 'append (mapcar 'cdr tags-alist))
  13856. tags))
  13857. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13858. (eval matcher)
  13859. (or (not org-agenda-skip-archived-trees)
  13860. (not (member org-archive-tag tags-list))))
  13861. (and (eq action 'agenda) (org-agenda-skip))
  13862. ;; list this headline
  13863. (if (eq action 'sparse-tree)
  13864. (progn
  13865. (and org-highlight-sparse-tree-matches
  13866. (org-get-heading) (match-end 0)
  13867. (org-highlight-new-match
  13868. (match-beginning 0) (match-beginning 1)))
  13869. (org-show-context 'tags-tree))
  13870. (setq txt (org-format-agenda-item
  13871. ""
  13872. (concat
  13873. (if org-tags-match-list-sublevels
  13874. (make-string (1- level) ?.) "")
  13875. (org-get-heading))
  13876. category tags-list)
  13877. priority (org-get-priority txt))
  13878. (goto-char lspos)
  13879. (setq marker (org-agenda-new-marker))
  13880. (org-add-props txt props
  13881. 'org-marker marker 'org-hd-marker marker 'org-category category
  13882. 'priority priority 'type "tagsmatch")
  13883. (push txt rtn))
  13884. ;; if we are to skip sublevels, jump to end of subtree
  13885. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13886. (when (and (eq action 'sparse-tree)
  13887. (not org-sparse-tree-open-archived-trees))
  13888. (org-hide-archived-subtrees (point-min) (point-max)))
  13889. (nreverse rtn)))
  13890. (defvar todo-only) ;; dynamically scoped
  13891. (defun org-tags-sparse-tree (&optional todo-only match)
  13892. "Create a sparse tree according to tags string MATCH.
  13893. MATCH can contain positive and negative selection of tags, like
  13894. \"+WORK+URGENT-WITHBOSS\".
  13895. If optional argument TODO_ONLY is non-nil, only select lines that are
  13896. also TODO lines."
  13897. (interactive "P")
  13898. (org-prepare-agenda-buffers (list (current-buffer)))
  13899. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13900. (defvar org-cached-props nil)
  13901. (defun org-cached-entry-get (pom property)
  13902. (if (or (eq t org-use-property-inheritance)
  13903. (member property org-use-property-inheritance))
  13904. ;; Caching is not possible, check it directly
  13905. (org-entry-get pom property 'inherit)
  13906. ;; Get all properties, so that we can do complicated checks easily
  13907. (cdr (assoc property (or org-cached-props
  13908. (setq org-cached-props
  13909. (org-entry-properties pom)))))))
  13910. (defun org-global-tags-completion-table (&optional files)
  13911. "Return the list of all tags in all agenda buffer/files."
  13912. (save-excursion
  13913. (org-uniquify
  13914. (delq nil
  13915. (apply 'append
  13916. (mapcar
  13917. (lambda (file)
  13918. (set-buffer (find-file-noselect file))
  13919. (append (org-get-buffer-tags)
  13920. (mapcar (lambda (x) (if (stringp (car-safe x))
  13921. (list (car-safe x)) nil))
  13922. org-tag-alist)))
  13923. (if (and files (car files))
  13924. files
  13925. (org-agenda-files))))))))
  13926. (defun org-make-tags-matcher (match)
  13927. "Create the TAGS//TODO matcher form for the selection string MATCH."
  13928. ;; todo-only is scoped dynamically into this function, and the function
  13929. ;; may change it it the matcher asksk for it.
  13930. (unless match
  13931. ;; Get a new match request, with completion
  13932. (let ((org-last-tags-completion-table
  13933. (org-global-tags-completion-table)))
  13934. (setq match (completing-read
  13935. "Match: " 'org-tags-completion-function nil nil nil
  13936. 'org-tags-history))))
  13937. ;; Parse the string and create a lisp form
  13938. (let ((match0 match)
  13939. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  13940. minus tag mm
  13941. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13942. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  13943. (if (string-match "/+" match)
  13944. ;; match contains also a todo-matching request
  13945. (progn
  13946. (setq tagsmatch (substring match 0 (match-beginning 0))
  13947. todomatch (substring match (match-end 0)))
  13948. (if (string-match "^!" todomatch)
  13949. (setq todo-only t todomatch (substring todomatch 1)))
  13950. (if (string-match "^\\s-*$" todomatch)
  13951. (setq todomatch nil)))
  13952. ;; only matching tags
  13953. (setq tagsmatch match todomatch nil))
  13954. ;; Make the tags matcher
  13955. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13956. (setq tagsmatcher t)
  13957. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13958. (while (setq term (pop orterms))
  13959. (while (and (equal (substring term -1) "\\") orterms)
  13960. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13961. (while (string-match re term)
  13962. (setq minus (and (match-end 1)
  13963. (equal (match-string 1 term) "-"))
  13964. tag (match-string 2 term)
  13965. re-p (equal (string-to-char tag) ?{)
  13966. level-p (match-end 3)
  13967. prop-p (match-end 4)
  13968. mm (cond
  13969. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13970. (level-p `(= level ,(string-to-number
  13971. (match-string 3 term))))
  13972. (prop-p
  13973. (setq pn (match-string 4 term)
  13974. pv (match-string 5 term)
  13975. cat-p (equal pn "CATEGORY")
  13976. re-p (equal (string-to-char pv) ?{)
  13977. pv (substring pv 1 -1))
  13978. (if (equal pn "CATEGORY")
  13979. (setq gv '(get-text-property (point) 'org-category))
  13980. (setq gv `(org-cached-entry-get nil ,pn)))
  13981. (if re-p
  13982. `(string-match ,pv (or ,gv ""))
  13983. `(equal ,pv ,gv)))
  13984. (t `(member ,(downcase tag) tags-list)))
  13985. mm (if minus (list 'not mm) mm)
  13986. term (substring term (match-end 0)))
  13987. (push mm tagsmatcher))
  13988. (push (if (> (length tagsmatcher) 1)
  13989. (cons 'and tagsmatcher)
  13990. (car tagsmatcher))
  13991. orlist)
  13992. (setq tagsmatcher nil))
  13993. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13994. (setq tagsmatcher
  13995. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13996. ;; Make the todo matcher
  13997. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13998. (setq todomatcher t)
  13999. (setq orterms (org-split-string todomatch "|") orlist nil)
  14000. (while (setq term (pop orterms))
  14001. (while (string-match re term)
  14002. (setq minus (and (match-end 1)
  14003. (equal (match-string 1 term) "-"))
  14004. kwd (match-string 2 term)
  14005. re-p (equal (string-to-char kwd) ?{)
  14006. term (substring term (match-end 0))
  14007. mm (if re-p
  14008. `(string-match ,(substring kwd 1 -1) todo)
  14009. (list 'equal 'todo kwd))
  14010. mm (if minus (list 'not mm) mm))
  14011. (push mm todomatcher))
  14012. (push (if (> (length todomatcher) 1)
  14013. (cons 'and todomatcher)
  14014. (car todomatcher))
  14015. orlist)
  14016. (setq todomatcher nil))
  14017. (setq todomatcher (if (> (length orlist) 1)
  14018. (cons 'or orlist) (car orlist))))
  14019. ;; Return the string and lisp forms of the matcher
  14020. (setq matcher (if todomatcher
  14021. (list 'and tagsmatcher todomatcher)
  14022. tagsmatcher))
  14023. (cons match0 matcher)))
  14024. (defun org-match-any-p (re list)
  14025. "Does re match any element of list?"
  14026. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14027. (delq nil list))
  14028. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14029. (defvar org-tags-overlay (org-make-overlay 1 1))
  14030. (org-detach-overlay org-tags-overlay)
  14031. (defun org-align-tags-here (to-col)
  14032. ;; Assumes that this is a headline
  14033. (let ((pos (point)) (col (current-column)) tags)
  14034. (beginning-of-line 1)
  14035. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14036. (< pos (match-beginning 2)))
  14037. (progn
  14038. (setq tags (match-string 2))
  14039. (goto-char (match-beginning 1))
  14040. (insert " ")
  14041. (delete-region (point) (1+ (match-end 0)))
  14042. (backward-char 1)
  14043. (move-to-column
  14044. (max (1+ (current-column))
  14045. (1+ col)
  14046. (if (> to-col 0)
  14047. to-col
  14048. (- (abs to-col) (length tags))))
  14049. t)
  14050. (insert tags)
  14051. (move-to-column (min (current-column) col) t))
  14052. (goto-char pos))))
  14053. (defun org-set-tags (&optional arg just-align)
  14054. "Set the tags for the current headline.
  14055. With prefix ARG, realign all tags in headings in the current buffer."
  14056. (interactive "P")
  14057. (let* ((re (concat "^" outline-regexp))
  14058. (current (org-get-tags-string))
  14059. (col (current-column))
  14060. (org-setting-tags t)
  14061. table current-tags inherited-tags ; computed below when needed
  14062. tags p0 c0 c1 rpl)
  14063. (if arg
  14064. (save-excursion
  14065. (goto-char (point-min))
  14066. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14067. (while (re-search-forward re nil t)
  14068. (org-set-tags nil t)
  14069. (end-of-line 1)))
  14070. (message "All tags realigned to column %d" org-tags-column))
  14071. (if just-align
  14072. (setq tags current)
  14073. ;; Get a new set of tags from the user
  14074. (save-excursion
  14075. (setq table (or org-tag-alist (org-get-buffer-tags))
  14076. org-last-tags-completion-table table
  14077. current-tags (org-split-string current ":")
  14078. inherited-tags (nreverse
  14079. (nthcdr (length current-tags)
  14080. (nreverse (org-get-tags-at))))
  14081. tags
  14082. (if (or (eq t org-use-fast-tag-selection)
  14083. (and org-use-fast-tag-selection
  14084. (delq nil (mapcar 'cdr table))))
  14085. (org-fast-tag-selection
  14086. current-tags inherited-tags table
  14087. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14088. (let ((org-add-colon-after-tag-completion t))
  14089. (org-trim
  14090. (org-without-partial-completion
  14091. (completing-read "Tags: " 'org-tags-completion-function
  14092. nil nil current 'org-tags-history)))))))
  14093. (while (string-match "[-+&]+" tags)
  14094. ;; No boolean logic, just a list
  14095. (setq tags (replace-match ":" t t tags))))
  14096. (if (string-match "\\`[\t ]*\\'" tags)
  14097. (setq tags "")
  14098. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14099. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14100. ;; Insert new tags at the correct column
  14101. (beginning-of-line 1)
  14102. (cond
  14103. ((and (equal current "") (equal tags "")))
  14104. ((re-search-forward
  14105. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14106. (point-at-eol) t)
  14107. (if (equal tags "")
  14108. (setq rpl "")
  14109. (goto-char (match-beginning 0))
  14110. (setq c0 (current-column) p0 (point)
  14111. c1 (max (1+ c0) (if (> org-tags-column 0)
  14112. org-tags-column
  14113. (- (- org-tags-column) (length tags))))
  14114. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14115. (replace-match rpl t t)
  14116. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  14117. tags)
  14118. (t (error "Tags alignment failed")))
  14119. (move-to-column col)
  14120. (unless just-align
  14121. (run-hooks 'org-after-tags-change-hook)))))
  14122. (defun org-change-tag-in-region (beg end tag off)
  14123. "Add or remove TAG for each entry in the region.
  14124. This works in the agenda, and also in an org-mode buffer."
  14125. (interactive
  14126. (list (region-beginning) (region-end)
  14127. (let ((org-last-tags-completion-table
  14128. (if (org-mode-p)
  14129. (org-get-buffer-tags)
  14130. (org-global-tags-completion-table))))
  14131. (completing-read
  14132. "Tag: " 'org-tags-completion-function nil nil nil
  14133. 'org-tags-history))
  14134. (progn
  14135. (message "[s]et or [r]emove? ")
  14136. (equal (read-char-exclusive) ?r))))
  14137. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14138. (let ((agendap (equal major-mode 'org-agenda-mode))
  14139. l1 l2 m buf pos newhead (cnt 0))
  14140. (goto-char end)
  14141. (setq l2 (1- (org-current-line)))
  14142. (goto-char beg)
  14143. (setq l1 (org-current-line))
  14144. (loop for l from l1 to l2 do
  14145. (goto-line l)
  14146. (setq m (get-text-property (point) 'org-hd-marker))
  14147. (when (or (and (org-mode-p) (org-on-heading-p))
  14148. (and agendap m))
  14149. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14150. pos (if agendap m (point)))
  14151. (with-current-buffer buf
  14152. (save-excursion
  14153. (save-restriction
  14154. (goto-char pos)
  14155. (setq cnt (1+ cnt))
  14156. (org-toggle-tag tag (if off 'off 'on))
  14157. (setq newhead (org-get-heading)))))
  14158. (and agendap (org-agenda-change-all-lines newhead m))))
  14159. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14160. (defun org-tags-completion-function (string predicate &optional flag)
  14161. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14162. (confirm (lambda (x) (stringp (car x)))))
  14163. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14164. (setq s1 (match-string 1 string)
  14165. s2 (match-string 2 string))
  14166. (setq s1 "" s2 string))
  14167. (cond
  14168. ((eq flag nil)
  14169. ;; try completion
  14170. (setq rtn (try-completion s2 ctable confirm))
  14171. (if (stringp rtn)
  14172. (setq rtn
  14173. (concat s1 s2 (substring rtn (length s2))
  14174. (if (and org-add-colon-after-tag-completion
  14175. (assoc rtn ctable))
  14176. ":" ""))))
  14177. rtn)
  14178. ((eq flag t)
  14179. ;; all-completions
  14180. (all-completions s2 ctable confirm)
  14181. )
  14182. ((eq flag 'lambda)
  14183. ;; exact match?
  14184. (assoc s2 ctable)))
  14185. ))
  14186. (defun org-fast-tag-insert (kwd tags face &optional end)
  14187. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14188. (insert (format "%-12s" (concat kwd ":"))
  14189. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14190. (or end "")))
  14191. (defun org-fast-tag-show-exit (flag)
  14192. (save-excursion
  14193. (goto-line 3)
  14194. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14195. (replace-match ""))
  14196. (when flag
  14197. (end-of-line 1)
  14198. (move-to-column (- (window-width) 19) t)
  14199. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14200. (defun org-set-current-tags-overlay (current prefix)
  14201. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14202. (if (featurep 'xemacs)
  14203. (org-overlay-display org-tags-overlay (concat prefix s)
  14204. 'secondary-selection)
  14205. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14206. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14207. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14208. "Fast tag selection with single keys.
  14209. CURRENT is the current list of tags in the headline, INHERITED is the
  14210. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14211. possibly with grouping information. TODO-TABLE is a similar table with
  14212. TODO keywords, should these have keys assigned to them.
  14213. If the keys are nil, a-z are automatically assigned.
  14214. Returns the new tags string, or nil to not change the current settings."
  14215. (let* ((fulltable (append table todo-table))
  14216. (maxlen (apply 'max (mapcar
  14217. (lambda (x)
  14218. (if (stringp (car x)) (string-width (car x)) 0))
  14219. fulltable)))
  14220. (buf (current-buffer))
  14221. (expert (eq org-fast-tag-selection-single-key 'expert))
  14222. (buffer-tags nil)
  14223. (fwidth (+ maxlen 3 1 3))
  14224. (ncol (/ (- (window-width) 4) fwidth))
  14225. (i-face 'org-done)
  14226. (c-face 'org-todo)
  14227. tg cnt e c char c1 c2 ntable tbl rtn
  14228. ov-start ov-end ov-prefix
  14229. (exit-after-next org-fast-tag-selection-single-key)
  14230. (done-keywords org-done-keywords)
  14231. groups ingroup)
  14232. (save-excursion
  14233. (beginning-of-line 1)
  14234. (if (looking-at
  14235. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14236. (setq ov-start (match-beginning 1)
  14237. ov-end (match-end 1)
  14238. ov-prefix "")
  14239. (setq ov-start (1- (point-at-eol))
  14240. ov-end (1+ ov-start))
  14241. (skip-chars-forward "^\n\r")
  14242. (setq ov-prefix
  14243. (concat
  14244. (buffer-substring (1- (point)) (point))
  14245. (if (> (current-column) org-tags-column)
  14246. " "
  14247. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14248. (org-move-overlay org-tags-overlay ov-start ov-end)
  14249. (save-window-excursion
  14250. (if expert
  14251. (set-buffer (get-buffer-create " *Org tags*"))
  14252. (delete-other-windows)
  14253. (split-window-vertically)
  14254. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14255. (erase-buffer)
  14256. (org-set-local 'org-done-keywords done-keywords)
  14257. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14258. (org-fast-tag-insert "Current" current c-face "\n\n")
  14259. (org-fast-tag-show-exit exit-after-next)
  14260. (org-set-current-tags-overlay current ov-prefix)
  14261. (setq tbl fulltable char ?a cnt 0)
  14262. (while (setq e (pop tbl))
  14263. (cond
  14264. ((equal e '(:startgroup))
  14265. (push '() groups) (setq ingroup t)
  14266. (when (not (= cnt 0))
  14267. (setq cnt 0)
  14268. (insert "\n"))
  14269. (insert "{ "))
  14270. ((equal e '(:endgroup))
  14271. (setq ingroup nil cnt 0)
  14272. (insert "}\n"))
  14273. (t
  14274. (setq tg (car e) c2 nil)
  14275. (if (cdr e)
  14276. (setq c (cdr e))
  14277. ;; automatically assign a character.
  14278. (setq c1 (string-to-char
  14279. (downcase (substring
  14280. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14281. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14282. (while (or (rassoc char ntable) (rassoc char table))
  14283. (setq char (1+ char)))
  14284. (setq c2 c1))
  14285. (setq c (or c2 char)))
  14286. (if ingroup (push tg (car groups)))
  14287. (setq tg (org-add-props tg nil 'face
  14288. (cond
  14289. ((not (assoc tg table))
  14290. (org-get-todo-face tg))
  14291. ((member tg current) c-face)
  14292. ((member tg inherited) i-face)
  14293. (t nil))))
  14294. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14295. (insert "[" c "] " tg (make-string
  14296. (- fwidth 4 (length tg)) ?\ ))
  14297. (push (cons tg c) ntable)
  14298. (when (= (setq cnt (1+ cnt)) ncol)
  14299. (insert "\n")
  14300. (if ingroup (insert " "))
  14301. (setq cnt 0)))))
  14302. (setq ntable (nreverse ntable))
  14303. (insert "\n")
  14304. (goto-char (point-min))
  14305. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14306. (fit-window-to-buffer))
  14307. (setq rtn
  14308. (catch 'exit
  14309. (while t
  14310. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14311. (if groups " [!] no groups" " [!]groups")
  14312. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14313. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14314. (cond
  14315. ((= c ?\r) (throw 'exit t))
  14316. ((= c ?!)
  14317. (setq groups (not groups))
  14318. (goto-char (point-min))
  14319. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14320. ((= c ?\C-c)
  14321. (if (not expert)
  14322. (org-fast-tag-show-exit
  14323. (setq exit-after-next (not exit-after-next)))
  14324. (setq expert nil)
  14325. (delete-other-windows)
  14326. (split-window-vertically)
  14327. (org-switch-to-buffer-other-window " *Org tags*")
  14328. (and (fboundp 'fit-window-to-buffer)
  14329. (fit-window-to-buffer))))
  14330. ((or (= c ?\C-g)
  14331. (and (= c ?q) (not (rassoc c ntable))))
  14332. (org-detach-overlay org-tags-overlay)
  14333. (setq quit-flag t))
  14334. ((= c ?\ )
  14335. (setq current nil)
  14336. (if exit-after-next (setq exit-after-next 'now)))
  14337. ((= c ?\t)
  14338. (condition-case nil
  14339. (setq tg (completing-read
  14340. "Tag: "
  14341. (or buffer-tags
  14342. (with-current-buffer buf
  14343. (org-get-buffer-tags)))))
  14344. (quit (setq tg "")))
  14345. (when (string-match "\\S-" tg)
  14346. (add-to-list 'buffer-tags (list tg))
  14347. (if (member tg current)
  14348. (setq current (delete tg current))
  14349. (push tg current)))
  14350. (if exit-after-next (setq exit-after-next 'now)))
  14351. ((setq e (rassoc c todo-table) tg (car e))
  14352. (with-current-buffer buf
  14353. (save-excursion (org-todo tg)))
  14354. (if exit-after-next (setq exit-after-next 'now)))
  14355. ((setq e (rassoc c ntable) tg (car e))
  14356. (if (member tg current)
  14357. (setq current (delete tg current))
  14358. (loop for g in groups do
  14359. (if (member tg g)
  14360. (mapc (lambda (x)
  14361. (setq current (delete x current)))
  14362. g)))
  14363. (push tg current))
  14364. (if exit-after-next (setq exit-after-next 'now))))
  14365. ;; Create a sorted list
  14366. (setq current
  14367. (sort current
  14368. (lambda (a b)
  14369. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14370. (if (eq exit-after-next 'now) (throw 'exit t))
  14371. (goto-char (point-min))
  14372. (beginning-of-line 2)
  14373. (delete-region (point) (point-at-eol))
  14374. (org-fast-tag-insert "Current" current c-face)
  14375. (org-set-current-tags-overlay current ov-prefix)
  14376. (while (re-search-forward
  14377. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14378. (setq tg (match-string 1))
  14379. (add-text-properties
  14380. (match-beginning 1) (match-end 1)
  14381. (list 'face
  14382. (cond
  14383. ((member tg current) c-face)
  14384. ((member tg inherited) i-face)
  14385. (t (get-text-property (match-beginning 1) 'face))))))
  14386. (goto-char (point-min)))))
  14387. (org-detach-overlay org-tags-overlay)
  14388. (if rtn
  14389. (mapconcat 'identity current ":")
  14390. nil))))
  14391. (defun org-get-tags-string ()
  14392. "Get the TAGS string in the current headline."
  14393. (unless (org-on-heading-p t)
  14394. (error "Not on a heading"))
  14395. (save-excursion
  14396. (beginning-of-line 1)
  14397. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14398. (org-match-string-no-properties 1)
  14399. "")))
  14400. (defun org-get-tags ()
  14401. "Get the list of tags specified in the current headline."
  14402. (org-split-string (org-get-tags-string) ":"))
  14403. (defun org-get-buffer-tags ()
  14404. "Get a table of all tags used in the buffer, for completion."
  14405. (let (tags)
  14406. (save-excursion
  14407. (goto-char (point-min))
  14408. (while (re-search-forward
  14409. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14410. (when (equal (char-after (point-at-bol 0)) ?*)
  14411. (mapc (lambda (x) (add-to-list 'tags x))
  14412. (org-split-string (org-match-string-no-properties 1) ":")))))
  14413. (mapcar 'list tags)))
  14414. ;;;; Properties
  14415. ;;; Setting and retrieving properties
  14416. (defconst org-special-properties
  14417. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14418. "TIMESTAMP" "TIMESTAMP_IA")
  14419. "The special properties valid in Org-mode.
  14420. These are properties that are not defined in the property drawer,
  14421. but in some other way.")
  14422. (defconst org-default-properties
  14423. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14424. "LOCATION" "LOGGING" "COLUMNS")
  14425. "Some properties that are used by Org-mode for various purposes.
  14426. Being in this list makes sure that they are offered for completion.")
  14427. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14428. "Regular expression matching the first line of a property drawer.")
  14429. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14430. "Regular expression matching the first line of a property drawer.")
  14431. (defun org-property-action ()
  14432. "Do an action on properties."
  14433. (interactive)
  14434. (let (c)
  14435. (org-at-property-p)
  14436. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14437. (setq c (read-char-exclusive))
  14438. (cond
  14439. ((equal c ?s)
  14440. (call-interactively 'org-set-property))
  14441. ((equal c ?d)
  14442. (call-interactively 'org-delete-property))
  14443. ((equal c ?D)
  14444. (call-interactively 'org-delete-property-globally))
  14445. ((equal c ?c)
  14446. (call-interactively 'org-compute-property-at-point))
  14447. (t (error "No such property action %c" c)))))
  14448. (defun org-at-property-p ()
  14449. "Is the cursor in a property line?"
  14450. ;; FIXME: Does not check if we are actually in the drawer.
  14451. ;; FIXME: also returns true on any drawers.....
  14452. ;; This is used by C-c C-c for property action.
  14453. (save-excursion
  14454. (beginning-of-line 1)
  14455. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14456. (defmacro org-with-point-at (pom &rest body)
  14457. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14458. (declare (indent 1) (debug t))
  14459. `(save-excursion
  14460. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14461. (save-excursion
  14462. (goto-char (or pom (point)))
  14463. ,@body)))
  14464. (defun org-get-property-block (&optional beg end force)
  14465. "Return the (beg . end) range of the body of the property drawer.
  14466. BEG and END can be beginning and end of subtree, if not given
  14467. they will be found.
  14468. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14469. (catch 'exit
  14470. (save-excursion
  14471. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14472. (end (or end (progn (outline-next-heading) (point)))))
  14473. (goto-char beg)
  14474. (if (re-search-forward org-property-start-re end t)
  14475. (setq beg (1+ (match-end 0)))
  14476. (if force
  14477. (save-excursion
  14478. (org-insert-property-drawer)
  14479. (setq end (progn (outline-next-heading) (point))))
  14480. (throw 'exit nil))
  14481. (goto-char beg)
  14482. (if (re-search-forward org-property-start-re end t)
  14483. (setq beg (1+ (match-end 0)))))
  14484. (if (re-search-forward org-property-end-re end t)
  14485. (setq end (match-beginning 0))
  14486. (or force (throw 'exit nil))
  14487. (goto-char beg)
  14488. (setq end beg)
  14489. (org-indent-line-function)
  14490. (insert ":END:\n"))
  14491. (cons beg end)))))
  14492. (defun org-entry-properties (&optional pom which)
  14493. "Get all properties of the entry at point-or-marker POM.
  14494. This includes the TODO keyword, the tags, time strings for deadline,
  14495. scheduled, and clocking, and any additional properties defined in the
  14496. entry. The return value is an alist, keys may occur multiple times
  14497. if the property key was used several times.
  14498. POM may also be nil, in which case the current entry is used.
  14499. If WHICH is nil or `all', get all properties. If WHICH is
  14500. `special' or `standard', only get that subclass."
  14501. (setq which (or which 'all))
  14502. (org-with-point-at pom
  14503. (let ((clockstr (substring org-clock-string 0 -1))
  14504. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14505. beg end range props sum-props key value string clocksum)
  14506. (save-excursion
  14507. (when (condition-case nil (org-back-to-heading t) (error nil))
  14508. (setq beg (point))
  14509. (setq sum-props (get-text-property (point) 'org-summaries))
  14510. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14511. (outline-next-heading)
  14512. (setq end (point))
  14513. (when (memq which '(all special))
  14514. ;; Get the special properties, like TODO and tags
  14515. (goto-char beg)
  14516. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14517. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14518. (when (looking-at org-priority-regexp)
  14519. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14520. (when (and (setq value (org-get-tags-string))
  14521. (string-match "\\S-" value))
  14522. (push (cons "TAGS" value) props))
  14523. (when (setq value (org-get-tags-at))
  14524. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14525. props))
  14526. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14527. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14528. string (if (equal key clockstr)
  14529. (org-no-properties
  14530. (org-trim
  14531. (buffer-substring
  14532. (match-beginning 3) (goto-char (point-at-eol)))))
  14533. (substring (org-match-string-no-properties 3) 1 -1)))
  14534. (unless key
  14535. (if (= (char-after (match-beginning 3)) ?\[)
  14536. (setq key "TIMESTAMP_IA")
  14537. (setq key "TIMESTAMP")))
  14538. (when (or (equal key clockstr) (not (assoc key props)))
  14539. (push (cons key string) props)))
  14540. )
  14541. (when (memq which '(all standard))
  14542. ;; Get the standard properties, like :PORP: ...
  14543. (setq range (org-get-property-block beg end))
  14544. (when range
  14545. (goto-char (car range))
  14546. (while (re-search-forward
  14547. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14548. (cdr range) t)
  14549. (setq key (org-match-string-no-properties 1)
  14550. value (org-trim (or (org-match-string-no-properties 2) "")))
  14551. (unless (member key excluded)
  14552. (push (cons key (or value "")) props)))))
  14553. (if clocksum
  14554. (push (cons "CLOCKSUM"
  14555. (org-column-number-to-string (/ (float clocksum) 60.)
  14556. 'add_times))
  14557. props))
  14558. (append sum-props (nreverse props)))))))
  14559. (defun org-entry-get (pom property &optional inherit)
  14560. "Get value of PROPERTY for entry at point-or-marker POM.
  14561. If INHERIT is non-nil and the entry does not have the property,
  14562. then also check higher levels of the hierarchy.
  14563. If the property is present but empty, the return value is the empty string.
  14564. If the property is not present at all, nil is returned."
  14565. (org-with-point-at pom
  14566. (if inherit
  14567. (org-entry-get-with-inheritance property)
  14568. (if (member property org-special-properties)
  14569. ;; We need a special property. Use brute force, get all properties.
  14570. (cdr (assoc property (org-entry-properties nil 'special)))
  14571. (let ((range (org-get-property-block)))
  14572. (if (and range
  14573. (goto-char (car range))
  14574. (re-search-forward
  14575. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14576. (cdr range) t))
  14577. ;; Found the property, return it.
  14578. (if (match-end 1)
  14579. (org-match-string-no-properties 1)
  14580. "")))))))
  14581. (defun org-entry-delete (pom property)
  14582. "Delete the property PROPERTY from entry at point-or-marker POM."
  14583. (org-with-point-at pom
  14584. (if (member property org-special-properties)
  14585. nil ; cannot delete these properties.
  14586. (let ((range (org-get-property-block)))
  14587. (if (and range
  14588. (goto-char (car range))
  14589. (re-search-forward
  14590. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14591. (cdr range) t))
  14592. (progn
  14593. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14594. t)
  14595. nil)))))
  14596. ;; Multi-values properties are properties that contain multiple values
  14597. ;; These values are assumed to be single words, separated by whitespace.
  14598. (defun org-entry-add-to-multivalued-property (pom property value)
  14599. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14600. (let* ((old (org-entry-get pom property))
  14601. (values (and old (org-split-string old "[ \t]"))))
  14602. (unless (member value values)
  14603. (setq values (cons value values))
  14604. (org-entry-put pom property
  14605. (mapconcat 'identity values " ")))))
  14606. (defun org-entry-remove-from-multivalued-property (pom property value)
  14607. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14608. (let* ((old (org-entry-get pom property))
  14609. (values (and old (org-split-string old "[ \t]"))))
  14610. (when (member value values)
  14611. (setq values (delete value values))
  14612. (org-entry-put pom property
  14613. (mapconcat 'identity values " ")))))
  14614. (defun org-entry-member-in-multivalued-property (pom property value)
  14615. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14616. (let* ((old (org-entry-get pom property))
  14617. (values (and old (org-split-string old "[ \t]"))))
  14618. (member value values)))
  14619. (defvar org-entry-property-inherited-from (make-marker))
  14620. (defun org-entry-get-with-inheritance (property)
  14621. "Get entry property, and search higher levels if not present."
  14622. (let (tmp)
  14623. (save-excursion
  14624. (save-restriction
  14625. (widen)
  14626. (catch 'ex
  14627. (while t
  14628. (when (setq tmp (org-entry-get nil property))
  14629. (org-back-to-heading t)
  14630. (move-marker org-entry-property-inherited-from (point))
  14631. (throw 'ex tmp))
  14632. (or (org-up-heading-safe) (throw 'ex nil)))))
  14633. (or tmp (cdr (assoc property org-local-properties))
  14634. (cdr (assoc property org-global-properties))))))
  14635. (defun org-entry-put (pom property value)
  14636. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14637. (org-with-point-at pom
  14638. (org-back-to-heading t)
  14639. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14640. range)
  14641. (cond
  14642. ((equal property "TODO")
  14643. (when (and (stringp value) (string-match "\\S-" value)
  14644. (not (member value org-todo-keywords-1)))
  14645. (error "\"%s\" is not a valid TODO state" value))
  14646. (if (or (not value)
  14647. (not (string-match "\\S-" value)))
  14648. (setq value 'none))
  14649. (org-todo value)
  14650. (org-set-tags nil 'align))
  14651. ((equal property "PRIORITY")
  14652. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14653. (string-to-char value) ?\ ))
  14654. (org-set-tags nil 'align))
  14655. ((equal property "SCHEDULED")
  14656. (if (re-search-forward org-scheduled-time-regexp end t)
  14657. (cond
  14658. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14659. ((eq value 'later) (org-timestamp-change 1 'day))
  14660. (t (call-interactively 'org-schedule)))
  14661. (call-interactively 'org-schedule)))
  14662. ((equal property "DEADLINE")
  14663. (if (re-search-forward org-deadline-time-regexp end t)
  14664. (cond
  14665. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14666. ((eq value 'later) (org-timestamp-change 1 'day))
  14667. (t (call-interactively 'org-deadline)))
  14668. (call-interactively 'org-deadline)))
  14669. ((member property org-special-properties)
  14670. (error "The %s property can not yet be set with `org-entry-put'"
  14671. property))
  14672. (t ; a non-special property
  14673. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14674. (setq range (org-get-property-block beg end 'force))
  14675. (goto-char (car range))
  14676. (if (re-search-forward
  14677. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14678. (progn
  14679. (delete-region (match-beginning 1) (match-end 1))
  14680. (goto-char (match-beginning 1)))
  14681. (goto-char (cdr range))
  14682. (insert "\n")
  14683. (backward-char 1)
  14684. (org-indent-line-function)
  14685. (insert ":" property ":"))
  14686. (and value (insert " " value))
  14687. (org-indent-line-function)))))))
  14688. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  14689. "Get all property keys in the current buffer.
  14690. With INCLUDE-SPECIALS, also list the special properties that relect things
  14691. like tags and TODO state.
  14692. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14693. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  14694. (let (rtn range)
  14695. (save-excursion
  14696. (save-restriction
  14697. (widen)
  14698. (goto-char (point-min))
  14699. (while (re-search-forward org-property-start-re nil t)
  14700. (setq range (org-get-property-block))
  14701. (goto-char (car range))
  14702. (while (re-search-forward
  14703. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  14704. (cdr range) t)
  14705. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14706. (outline-next-heading))))
  14707. (when include-specials
  14708. (setq rtn (append org-special-properties rtn)))
  14709. (when include-defaults
  14710. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14711. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14712. (defun org-property-values (key)
  14713. "Return a list of all values of property KEY."
  14714. (save-excursion
  14715. (save-restriction
  14716. (widen)
  14717. (goto-char (point-min))
  14718. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14719. values)
  14720. (while (re-search-forward re nil t)
  14721. (add-to-list 'values (org-trim (match-string 1))))
  14722. (delete "" values)))))
  14723. (defun org-insert-property-drawer ()
  14724. "Insert a property drawer into the current entry."
  14725. (interactive)
  14726. (org-back-to-heading t)
  14727. (looking-at outline-regexp)
  14728. (let ((indent (- (match-end 0)(match-beginning 0)))
  14729. (beg (point))
  14730. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14731. end hiddenp)
  14732. (outline-next-heading)
  14733. (setq end (point))
  14734. (goto-char beg)
  14735. (while (re-search-forward re end t))
  14736. (setq hiddenp (org-invisible-p))
  14737. (end-of-line 1)
  14738. (and (equal (char-after) ?\n) (forward-char 1))
  14739. (org-skip-over-state-notes)
  14740. (skip-chars-backward " \t\n\r")
  14741. (if (eq (char-before) ?*) (forward-char 1))
  14742. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14743. (beginning-of-line 0)
  14744. (indent-to-column indent)
  14745. (beginning-of-line 2)
  14746. (indent-to-column indent)
  14747. (beginning-of-line 0)
  14748. (if hiddenp
  14749. (save-excursion
  14750. (org-back-to-heading t)
  14751. (hide-entry))
  14752. (org-flag-drawer t))))
  14753. (defun org-set-property (property value)
  14754. "In the current entry, set PROPERTY to VALUE.
  14755. When called interactively, this will prompt for a property name, offering
  14756. completion on existing and default properties. And then it will prompt
  14757. for a value, offering competion either on allowed values (via an inherited
  14758. xxx_ALL property) or on existing values in other instances of this property
  14759. in the current file."
  14760. (interactive
  14761. (let* ((prop (completing-read
  14762. "Property: " (mapcar 'list (org-buffer-property-keys nil t))))
  14763. (cur (org-entry-get nil prop))
  14764. (allowed (org-property-get-allowed-values nil prop 'table))
  14765. (existing (mapcar 'list (org-property-values prop)))
  14766. (val (if allowed
  14767. (completing-read "Value: " allowed nil 'req-match)
  14768. (completing-read
  14769. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14770. (concat "[" cur "]") "")
  14771. ": ")
  14772. existing nil nil "" nil cur))))
  14773. (list prop (if (equal val "") cur val))))
  14774. (unless (equal (org-entry-get nil property) value)
  14775. (org-entry-put nil property value)))
  14776. (defun org-delete-property (property)
  14777. "In the current entry, delete PROPERTY."
  14778. (interactive
  14779. (let* ((prop (completing-read
  14780. "Property: " (org-entry-properties nil 'standard))))
  14781. (list prop)))
  14782. (message "Property %s %s" property
  14783. (if (org-entry-delete nil property)
  14784. "deleted"
  14785. "was not present in the entry")))
  14786. (defun org-delete-property-globally (property)
  14787. "Remove PROPERTY globally, from all entries."
  14788. (interactive
  14789. (let* ((prop (completing-read
  14790. "Globally remove property: "
  14791. (mapcar 'list (org-buffer-property-keys)))))
  14792. (list prop)))
  14793. (save-excursion
  14794. (save-restriction
  14795. (widen)
  14796. (goto-char (point-min))
  14797. (let ((cnt 0))
  14798. (while (re-search-forward
  14799. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14800. nil t)
  14801. (setq cnt (1+ cnt))
  14802. (replace-match ""))
  14803. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14804. (defvar org-columns-current-fmt-compiled) ; defined below
  14805. (defun org-compute-property-at-point ()
  14806. "Compute the property at point.
  14807. This looks for an enclosing column format, extracts the operator and
  14808. then applies it to the proerty in the column format's scope."
  14809. (interactive)
  14810. (unless (org-at-property-p)
  14811. (error "Not at a property"))
  14812. (let ((prop (org-match-string-no-properties 2)))
  14813. (org-columns-get-format-and-top-level)
  14814. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14815. (error "No operator defined for property %s" prop))
  14816. (org-columns-compute prop)))
  14817. (defun org-property-get-allowed-values (pom property &optional table)
  14818. "Get allowed values for the property PROPERTY.
  14819. When TABLE is non-nil, return an alist that can directly be used for
  14820. completion."
  14821. (let (vals)
  14822. (cond
  14823. ((equal property "TODO")
  14824. (setq vals (org-with-point-at pom
  14825. (append org-todo-keywords-1 '("")))))
  14826. ((equal property "PRIORITY")
  14827. (let ((n org-lowest-priority))
  14828. (while (>= n org-highest-priority)
  14829. (push (char-to-string n) vals)
  14830. (setq n (1- n)))))
  14831. ((member property org-special-properties))
  14832. (t
  14833. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14834. (when (and vals (string-match "\\S-" vals))
  14835. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14836. (setq vals (mapcar (lambda (x)
  14837. (cond ((stringp x) x)
  14838. ((numberp x) (number-to-string x))
  14839. ((symbolp x) (symbol-name x))
  14840. (t "???")))
  14841. vals)))))
  14842. (if table (mapcar 'list vals) vals)))
  14843. (defun org-property-previous-allowed-value (&optional previous)
  14844. "Switch to the next allowed value for this property."
  14845. (interactive)
  14846. (org-property-next-allowed-value t))
  14847. (defun org-property-next-allowed-value (&optional previous)
  14848. "Switch to the next allowed value for this property."
  14849. (interactive)
  14850. (unless (org-at-property-p)
  14851. (error "Not at a property"))
  14852. (let* ((key (match-string 2))
  14853. (value (match-string 3))
  14854. (allowed (or (org-property-get-allowed-values (point) key)
  14855. (and (member value '("[ ]" "[-]" "[X]"))
  14856. '("[ ]" "[X]"))))
  14857. nval)
  14858. (unless allowed
  14859. (error "Allowed values for this property have not been defined"))
  14860. (if previous (setq allowed (reverse allowed)))
  14861. (if (member value allowed)
  14862. (setq nval (car (cdr (member value allowed)))))
  14863. (setq nval (or nval (car allowed)))
  14864. (if (equal nval value)
  14865. (error "Only one allowed value for this property"))
  14866. (org-at-property-p)
  14867. (replace-match (concat " :" key ": " nval) t t)
  14868. (org-indent-line-function)
  14869. (beginning-of-line 1)
  14870. (skip-chars-forward " \t")))
  14871. (defun org-find-entry-with-id (ident)
  14872. "Locate the entry that contains the ID property with exact value IDENT.
  14873. IDENT can be a string, a symbol or a number, this function will search for
  14874. the string representation of it.
  14875. Return the position where this entry starts, or nil if there is no such entry."
  14876. (let ((id (cond
  14877. ((stringp ident) ident)
  14878. ((symbol-name ident) (symbol-name ident))
  14879. ((numberp ident) (number-to-string ident))
  14880. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14881. (case-fold-search nil))
  14882. (save-excursion
  14883. (save-restriction
  14884. (goto-char (point-min))
  14885. (when (re-search-forward
  14886. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14887. nil t)
  14888. (org-back-to-heading)
  14889. (point))))))
  14890. ;;; Column View
  14891. (defvar org-columns-overlays nil
  14892. "Holds the list of current column overlays.")
  14893. (defvar org-columns-current-fmt nil
  14894. "Local variable, holds the currently active column format.")
  14895. (defvar org-columns-current-fmt-compiled nil
  14896. "Local variable, holds the currently active column format.
  14897. This is the compiled version of the format.")
  14898. (defvar org-columns-current-widths nil
  14899. "Loval variable, holds the currently widths of fields.")
  14900. (defvar org-columns-current-maxwidths nil
  14901. "Loval variable, holds the currently active maximum column widths.")
  14902. (defvar org-columns-begin-marker (make-marker)
  14903. "Points to the position where last a column creation command was called.")
  14904. (defvar org-columns-top-level-marker (make-marker)
  14905. "Points to the position where current columns region starts.")
  14906. (defvar org-columns-map (make-sparse-keymap)
  14907. "The keymap valid in column display.")
  14908. (defun org-columns-content ()
  14909. "Switch to contents view while in columns view."
  14910. (interactive)
  14911. (org-overview)
  14912. (org-content))
  14913. (org-defkey org-columns-map "c" 'org-columns-content)
  14914. (org-defkey org-columns-map "o" 'org-overview)
  14915. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  14916. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  14917. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  14918. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  14919. (org-defkey org-columns-map "v" 'org-columns-show-value)
  14920. (org-defkey org-columns-map "q" 'org-columns-quit)
  14921. (org-defkey org-columns-map "r" 'org-columns-redo)
  14922. (org-defkey org-columns-map "g" 'org-columns-redo)
  14923. (org-defkey org-columns-map [left] 'backward-char)
  14924. (org-defkey org-columns-map "\M-b" 'backward-char)
  14925. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  14926. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  14927. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  14928. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  14929. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  14930. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  14931. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  14932. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  14933. (org-defkey org-columns-map "<" 'org-columns-narrow)
  14934. (org-defkey org-columns-map ">" 'org-columns-widen)
  14935. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  14936. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  14937. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  14938. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  14939. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  14940. '("Column"
  14941. ["Edit property" org-columns-edit-value t]
  14942. ["Next allowed value" org-columns-next-allowed-value t]
  14943. ["Previous allowed value" org-columns-previous-allowed-value t]
  14944. ["Show full value" org-columns-show-value t]
  14945. ["Edit allowed values" org-columns-edit-allowed t]
  14946. "--"
  14947. ["Edit column attributes" org-columns-edit-attributes t]
  14948. ["Increase column width" org-columns-widen t]
  14949. ["Decrease column width" org-columns-narrow t]
  14950. "--"
  14951. ["Move column right" org-columns-move-right t]
  14952. ["Move column left" org-columns-move-left t]
  14953. ["Add column" org-columns-new t]
  14954. ["Delete column" org-columns-delete t]
  14955. "--"
  14956. ["CONTENTS" org-columns-content t]
  14957. ["OVERVIEW" org-overview t]
  14958. ["Refresh columns display" org-columns-redo t]
  14959. "--"
  14960. ["Open link" org-columns-open-link t]
  14961. "--"
  14962. ["Quit" org-columns-quit t]))
  14963. (defun org-columns-new-overlay (beg end &optional string face)
  14964. "Create a new column overlay and add it to the list."
  14965. (let ((ov (org-make-overlay beg end)))
  14966. (org-overlay-put ov 'face (or face 'secondary-selection))
  14967. (org-overlay-display ov string face)
  14968. (push ov org-columns-overlays)
  14969. ov))
  14970. (defun org-columns-display-here (&optional props)
  14971. "Overlay the current line with column display."
  14972. (interactive)
  14973. (let* ((fmt org-columns-current-fmt-compiled)
  14974. (beg (point-at-bol))
  14975. (level-face (save-excursion
  14976. (beginning-of-line 1)
  14977. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  14978. (org-get-level-face 2))))
  14979. (color (list :foreground
  14980. (face-attribute (or level-face 'default) :foreground)))
  14981. props pom property ass width f string ov column val modval)
  14982. ;; Check if the entry is in another buffer.
  14983. (unless props
  14984. (if (eq major-mode 'org-agenda-mode)
  14985. (setq pom (or (get-text-property (point) 'org-hd-marker)
  14986. (get-text-property (point) 'org-marker))
  14987. props (if pom (org-entry-properties pom) nil))
  14988. (setq props (org-entry-properties nil))))
  14989. ;; Walk the format
  14990. (while (setq column (pop fmt))
  14991. (setq property (car column)
  14992. ass (if (equal property "ITEM")
  14993. (cons "ITEM"
  14994. (save-match-data
  14995. (org-no-properties
  14996. (org-remove-tabs
  14997. (buffer-substring-no-properties
  14998. (point-at-bol) (point-at-eol))))))
  14999. (assoc property props))
  15000. width (or (cdr (assoc property org-columns-current-maxwidths))
  15001. (nth 2 column)
  15002. (length property))
  15003. f (format "%%-%d.%ds | " width width)
  15004. val (or (cdr ass) "")
  15005. modval (if (equal property "ITEM")
  15006. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15007. string (format f (or modval val)))
  15008. ;; Create the overlay
  15009. (org-unmodified
  15010. (setq ov (org-columns-new-overlay
  15011. beg (setq beg (1+ beg)) string
  15012. (list color 'org-column)))
  15013. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15014. (org-overlay-put ov 'keymap org-columns-map)
  15015. (org-overlay-put ov 'org-columns-key property)
  15016. (org-overlay-put ov 'org-columns-value (cdr ass))
  15017. (org-overlay-put ov 'org-columns-value-modified modval)
  15018. (org-overlay-put ov 'org-columns-pom pom)
  15019. (org-overlay-put ov 'org-columns-format f))
  15020. (if (or (not (char-after beg))
  15021. (equal (char-after beg) ?\n))
  15022. (let ((inhibit-read-only t))
  15023. (save-excursion
  15024. (goto-char beg)
  15025. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15026. ;; Make the rest of the line disappear.
  15027. (org-unmodified
  15028. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15029. (org-overlay-put ov 'invisible t)
  15030. (org-overlay-put ov 'keymap org-columns-map)
  15031. (org-overlay-put ov 'intangible t)
  15032. (push ov org-columns-overlays)
  15033. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15034. (org-overlay-put ov 'keymap org-columns-map)
  15035. (push ov org-columns-overlays)
  15036. (let ((inhibit-read-only t))
  15037. (put-text-property (max (point-min) (1- (point-at-bol)))
  15038. (min (point-max) (1+ (point-at-eol)))
  15039. 'read-only "Type `e' to edit property")))))
  15040. (defvar org-previous-header-line-format nil
  15041. "The header line format before column view was turned on.")
  15042. (defvar org-columns-inhibit-recalculation nil
  15043. "Inhibit recomputing of columns on column view startup.")
  15044. (defvar header-line-format)
  15045. (defun org-columns-display-here-title ()
  15046. "Overlay the newline before the current line with the table title."
  15047. (interactive)
  15048. (let ((fmt org-columns-current-fmt-compiled)
  15049. string (title "")
  15050. property width f column str widths)
  15051. (while (setq column (pop fmt))
  15052. (setq property (car column)
  15053. str (or (nth 1 column) property)
  15054. width (or (cdr (assoc property org-columns-current-maxwidths))
  15055. (nth 2 column)
  15056. (length str))
  15057. widths (push width widths)
  15058. f (format "%%-%d.%ds | " width width)
  15059. string (format f str)
  15060. title (concat title string)))
  15061. (setq title (concat
  15062. (org-add-props " " nil 'display '(space :align-to 0))
  15063. (org-add-props title nil 'face '(:weight bold :underline t))))
  15064. (org-set-local 'org-previous-header-line-format header-line-format)
  15065. (org-set-local 'org-columns-current-widths (nreverse widths))
  15066. (setq header-line-format title)))
  15067. (defun org-columns-remove-overlays ()
  15068. "Remove all currently active column overlays."
  15069. (interactive)
  15070. (when (marker-buffer org-columns-begin-marker)
  15071. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15072. (when (local-variable-p 'org-previous-header-line-format)
  15073. (setq header-line-format org-previous-header-line-format)
  15074. (kill-local-variable 'org-previous-header-line-format))
  15075. (move-marker org-columns-begin-marker nil)
  15076. (move-marker org-columns-top-level-marker nil)
  15077. (org-unmodified
  15078. (mapc 'org-delete-overlay org-columns-overlays)
  15079. (setq org-columns-overlays nil)
  15080. (let ((inhibit-read-only t))
  15081. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15082. (defun org-columns-cleanup-item (item fmt)
  15083. "Remove from ITEM what is a column in the format FMT."
  15084. (if (not org-complex-heading-regexp)
  15085. item
  15086. (when (string-match org-complex-heading-regexp item)
  15087. (concat
  15088. (org-add-props (concat (match-string 1 item) " ") nil
  15089. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15090. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15091. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15092. " " (match-string 4 item)
  15093. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15094. (defun org-columns-show-value ()
  15095. "Show the full value of the property."
  15096. (interactive)
  15097. (let ((value (get-char-property (point) 'org-columns-value)))
  15098. (message "Value is: %s" (or value ""))))
  15099. (defun org-columns-quit ()
  15100. "Remove the column overlays and in this way exit column editing."
  15101. (interactive)
  15102. (org-unmodified
  15103. (org-columns-remove-overlays)
  15104. (let ((inhibit-read-only t))
  15105. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15106. (when (eq major-mode 'org-agenda-mode)
  15107. (message
  15108. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15109. (defun org-columns-check-computed ()
  15110. "Check if this column value is computed.
  15111. If yes, throw an error indicating that changing it does not make sense."
  15112. (let ((val (get-char-property (point) 'org-columns-value)))
  15113. (when (and (stringp val)
  15114. (get-char-property 0 'org-computed val))
  15115. (error "This value is computed from the entry's children"))))
  15116. (defun org-columns-todo (&optional arg)
  15117. "Change the TODO state during column view."
  15118. (interactive "P")
  15119. (org-columns-edit-value "TODO"))
  15120. (defun org-columns-set-tags-or-toggle (&optional arg)
  15121. "Toggle checkbox at point, or set tags for current headline."
  15122. (interactive "P")
  15123. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15124. (get-char-property (point) 'org-columns-value))
  15125. (org-columns-next-allowed-value)
  15126. (org-columns-edit-value "TAGS")))
  15127. (defun org-columns-edit-value (&optional key)
  15128. "Edit the value of the property at point in column view.
  15129. Where possible, use the standard interface for changing this line."
  15130. (interactive)
  15131. (org-columns-check-computed)
  15132. (let* ((external-key key)
  15133. (col (current-column))
  15134. (key (or key (get-char-property (point) 'org-columns-key)))
  15135. (value (get-char-property (point) 'org-columns-value))
  15136. (bol (point-at-bol)) (eol (point-at-eol))
  15137. (pom (or (get-text-property bol 'org-hd-marker)
  15138. (point))) ; keep despite of compiler waring
  15139. (line-overlays
  15140. (delq nil (mapcar (lambda (x)
  15141. (and (eq (overlay-buffer x) (current-buffer))
  15142. (>= (overlay-start x) bol)
  15143. (<= (overlay-start x) eol)
  15144. x))
  15145. org-columns-overlays)))
  15146. nval eval allowed)
  15147. (cond
  15148. ((equal key "CLOCKSUM")
  15149. (error "This special column cannot be edited"))
  15150. ((equal key "ITEM")
  15151. (setq eval '(org-with-point-at pom
  15152. (org-edit-headline))))
  15153. ((equal key "TODO")
  15154. (setq eval '(org-with-point-at pom
  15155. (let ((current-prefix-arg
  15156. (if external-key current-prefix-arg '(4))))
  15157. (call-interactively 'org-todo)))))
  15158. ((equal key "PRIORITY")
  15159. (setq eval '(org-with-point-at pom
  15160. (call-interactively 'org-priority))))
  15161. ((equal key "TAGS")
  15162. (setq eval '(org-with-point-at pom
  15163. (let ((org-fast-tag-selection-single-key
  15164. (if (eq org-fast-tag-selection-single-key 'expert)
  15165. t org-fast-tag-selection-single-key)))
  15166. (call-interactively 'org-set-tags)))))
  15167. ((equal key "DEADLINE")
  15168. (setq eval '(org-with-point-at pom
  15169. (call-interactively 'org-deadline))))
  15170. ((equal key "SCHEDULED")
  15171. (setq eval '(org-with-point-at pom
  15172. (call-interactively 'org-schedule))))
  15173. (t
  15174. (setq allowed (org-property-get-allowed-values pom key 'table))
  15175. (if allowed
  15176. (setq nval (completing-read "Value: " allowed nil t))
  15177. (setq nval (read-string "Edit: " value)))
  15178. (setq nval (org-trim nval))
  15179. (when (not (equal nval value))
  15180. (setq eval '(org-entry-put pom key nval)))))
  15181. (when eval
  15182. (let ((inhibit-read-only t))
  15183. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15184. (unwind-protect
  15185. (progn
  15186. (setq org-columns-overlays
  15187. (org-delete-all line-overlays org-columns-overlays))
  15188. (mapc 'org-delete-overlay line-overlays)
  15189. (org-columns-eval eval))
  15190. (org-columns-display-here))))
  15191. (move-to-column col)
  15192. (if (and (org-mode-p)
  15193. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15194. (org-columns-update key))))
  15195. (defun org-edit-headline () ; FIXME: this is not columns specific
  15196. "Edit the current headline, the part without TODO keyword, TAGS."
  15197. (org-back-to-heading)
  15198. (when (looking-at org-todo-line-regexp)
  15199. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15200. (txt (match-string 3))
  15201. (post "")
  15202. txt2)
  15203. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15204. (setq post (match-string 0 txt)
  15205. txt (substring txt 0 (match-beginning 0))))
  15206. (setq txt2 (read-string "Edit: " txt))
  15207. (when (not (equal txt txt2))
  15208. (beginning-of-line 1)
  15209. (insert pre txt2 post)
  15210. (delete-region (point) (point-at-eol))
  15211. (org-set-tags nil t)))))
  15212. (defun org-columns-edit-allowed ()
  15213. "Edit the list of allowed values for the current property."
  15214. (interactive)
  15215. (let* ((key (get-char-property (point) 'org-columns-key))
  15216. (key1 (concat key "_ALL"))
  15217. (allowed (org-entry-get (point) key1 t))
  15218. nval)
  15219. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15220. (setq nval (read-string "Allowed: " allowed))
  15221. (org-entry-put
  15222. (cond ((marker-position org-entry-property-inherited-from)
  15223. org-entry-property-inherited-from)
  15224. ((marker-position org-columns-top-level-marker)
  15225. org-columns-top-level-marker))
  15226. key1 nval)))
  15227. (defmacro org-no-warnings (&rest body)
  15228. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15229. (defun org-columns-eval (form)
  15230. (let (hidep)
  15231. (save-excursion
  15232. (beginning-of-line 1)
  15233. ;; `next-line' is needed here, because it skips invisible line.
  15234. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15235. (setq hidep (org-on-heading-p 1)))
  15236. (eval form)
  15237. (and hidep (hide-entry))))
  15238. (defun org-columns-previous-allowed-value ()
  15239. "Switch to the previous allowed value for this column."
  15240. (interactive)
  15241. (org-columns-next-allowed-value t))
  15242. (defun org-columns-next-allowed-value (&optional previous)
  15243. "Switch to the next allowed value for this column."
  15244. (interactive)
  15245. (org-columns-check-computed)
  15246. (let* ((col (current-column))
  15247. (key (get-char-property (point) 'org-columns-key))
  15248. (value (get-char-property (point) 'org-columns-value))
  15249. (bol (point-at-bol)) (eol (point-at-eol))
  15250. (pom (or (get-text-property bol 'org-hd-marker)
  15251. (point))) ; keep despite of compiler waring
  15252. (line-overlays
  15253. (delq nil (mapcar (lambda (x)
  15254. (and (eq (overlay-buffer x) (current-buffer))
  15255. (>= (overlay-start x) bol)
  15256. (<= (overlay-start x) eol)
  15257. x))
  15258. org-columns-overlays)))
  15259. (allowed (or (org-property-get-allowed-values pom key)
  15260. (and (equal
  15261. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15262. 'checkbox) '("[ ]" "[X]"))))
  15263. nval)
  15264. (when (equal key "ITEM")
  15265. (error "Cannot edit item headline from here"))
  15266. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15267. (error "Allowed values for this property have not been defined"))
  15268. (if (member key '("SCHEDULED" "DEADLINE"))
  15269. (setq nval (if previous 'earlier 'later))
  15270. (if previous (setq allowed (reverse allowed)))
  15271. (if (member value allowed)
  15272. (setq nval (car (cdr (member value allowed)))))
  15273. (setq nval (or nval (car allowed)))
  15274. (if (equal nval value)
  15275. (error "Only one allowed value for this property")))
  15276. (let ((inhibit-read-only t))
  15277. (remove-text-properties (1- bol) eol '(read-only t))
  15278. (unwind-protect
  15279. (progn
  15280. (setq org-columns-overlays
  15281. (org-delete-all line-overlays org-columns-overlays))
  15282. (mapc 'org-delete-overlay line-overlays)
  15283. (org-columns-eval '(org-entry-put pom key nval)))
  15284. (org-columns-display-here)))
  15285. (move-to-column col)
  15286. (if (and (org-mode-p)
  15287. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15288. (org-columns-update key))))
  15289. (defun org-verify-version (task)
  15290. (cond
  15291. ((eq task 'columns)
  15292. (if (or (featurep 'xemacs)
  15293. (< emacs-major-version 22))
  15294. (error "Emacs 22 is required for the columns feature")))))
  15295. (defun org-columns-open-link (&optional arg)
  15296. (interactive "P")
  15297. (let ((key (get-char-property (point) 'org-columns-key))
  15298. (value (get-char-property (point) 'org-columns-value)))
  15299. (org-open-link-from-string arg)))
  15300. (defun org-open-link-from-string (s &optional arg)
  15301. "Open a link in the string S, as if it was in Org-mode."
  15302. (interactive)
  15303. (with-temp-buffer
  15304. (let ((org-inhibit-startup t))
  15305. (org-mode)
  15306. (insert s)
  15307. (goto-char (point-min))
  15308. (org-open-at-point arg))))
  15309. (defun org-columns-get-format-and-top-level ()
  15310. (let (fmt)
  15311. (when (condition-case nil (org-back-to-heading) (error nil))
  15312. (move-marker org-entry-property-inherited-from nil)
  15313. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15314. (setq fmt (or fmt org-columns-default-format))
  15315. (org-set-local 'org-columns-current-fmt fmt)
  15316. (org-columns-compile-format fmt)
  15317. (if (marker-position org-entry-property-inherited-from)
  15318. (move-marker org-columns-top-level-marker
  15319. org-entry-property-inherited-from)
  15320. (move-marker org-columns-top-level-marker (point)))
  15321. fmt))
  15322. (defun org-columns ()
  15323. "Turn on column view on an org-mode file."
  15324. (interactive)
  15325. (org-verify-version 'columns)
  15326. (org-columns-remove-overlays)
  15327. (move-marker org-columns-begin-marker (point))
  15328. (let (beg end fmt cache maxwidths clocksump)
  15329. (setq fmt (org-columns-get-format-and-top-level))
  15330. (save-excursion
  15331. (goto-char org-columns-top-level-marker)
  15332. (setq beg (point))
  15333. (unless org-columns-inhibit-recalculation
  15334. (org-columns-compute-all))
  15335. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15336. (point-max)))
  15337. ;; Get and cache the properties
  15338. (goto-char beg)
  15339. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15340. (setq clocksump t)
  15341. (save-excursion
  15342. (save-restriction
  15343. (narrow-to-region beg end)
  15344. (org-clock-sum))))
  15345. (while (re-search-forward (concat "^" outline-regexp) end t)
  15346. (push (cons (org-current-line) (org-entry-properties)) cache))
  15347. (when cache
  15348. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15349. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15350. (org-columns-display-here-title)
  15351. (mapc (lambda (x)
  15352. (goto-line (car x))
  15353. (org-columns-display-here (cdr x)))
  15354. cache)))))
  15355. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15356. "Insert a new column, to the leeft o the current column."
  15357. (interactive)
  15358. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15359. cell)
  15360. (setq prop (completing-read
  15361. "Property: " (mapcar 'list (org-buffer-property-keys t))
  15362. nil nil prop))
  15363. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15364. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15365. (if (string-match "\\S-" width)
  15366. (setq width (string-to-number width))
  15367. (setq width nil))
  15368. (setq fmt (completing-read "Summary [none]: "
  15369. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox"))
  15370. nil t))
  15371. (if (string-match "\\S-" fmt)
  15372. (setq fmt (intern fmt))
  15373. (setq fmt nil))
  15374. (if (eq fmt 'none) (setq fmt nil))
  15375. (if editp
  15376. (progn
  15377. (setcar editp prop)
  15378. (setcdr editp (list title width nil fmt)))
  15379. (setq cell (nthcdr (1- (current-column))
  15380. org-columns-current-fmt-compiled))
  15381. (setcdr cell (cons (list prop title width nil fmt)
  15382. (cdr cell))))
  15383. (org-columns-store-format)
  15384. (org-columns-redo)))
  15385. (defun org-columns-delete ()
  15386. "Delete the column at point from columns view."
  15387. (interactive)
  15388. (let* ((n (current-column))
  15389. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15390. (when (y-or-n-p
  15391. (format "Are you sure you want to remove column \"%s\"? " title))
  15392. (setq org-columns-current-fmt-compiled
  15393. (delq (nth n org-columns-current-fmt-compiled)
  15394. org-columns-current-fmt-compiled))
  15395. (org-columns-store-format)
  15396. (org-columns-redo)
  15397. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15398. (backward-char 1)))))
  15399. (defun org-columns-edit-attributes ()
  15400. "Edit the attributes of the current column."
  15401. (interactive)
  15402. (let* ((n (current-column))
  15403. (info (nth n org-columns-current-fmt-compiled)))
  15404. (apply 'org-columns-new info)))
  15405. (defun org-columns-widen (arg)
  15406. "Make the column wider by ARG characters."
  15407. (interactive "p")
  15408. (let* ((n (current-column))
  15409. (entry (nth n org-columns-current-fmt-compiled))
  15410. (width (or (nth 2 entry)
  15411. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15412. (setq width (max 1 (+ width arg)))
  15413. (setcar (nthcdr 2 entry) width)
  15414. (org-columns-store-format)
  15415. (org-columns-redo)))
  15416. (defun org-columns-narrow (arg)
  15417. "Make the column nrrower by ARG characters."
  15418. (interactive "p")
  15419. (org-columns-widen (- arg)))
  15420. (defun org-columns-move-right ()
  15421. "Swap this column with the one to the right."
  15422. (interactive)
  15423. (let* ((n (current-column))
  15424. (cell (nthcdr n org-columns-current-fmt-compiled))
  15425. e)
  15426. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15427. (error "Cannot shift this column further to the right"))
  15428. (setq e (car cell))
  15429. (setcar cell (car (cdr cell)))
  15430. (setcdr cell (cons e (cdr (cdr cell))))
  15431. (org-columns-store-format)
  15432. (org-columns-redo)
  15433. (forward-char 1)))
  15434. (defun org-columns-move-left ()
  15435. "Swap this column with the one to the left."
  15436. (interactive)
  15437. (let* ((n (current-column)))
  15438. (when (= n 0)
  15439. (error "Cannot shift this column further to the left"))
  15440. (backward-char 1)
  15441. (org-columns-move-right)
  15442. (backward-char 1)))
  15443. (defun org-columns-store-format ()
  15444. "Store the text version of the current columns format in appropriate place.
  15445. This is either in the COLUMNS property of the node starting the current column
  15446. display, or in the #+COLUMNS line of the current buffer."
  15447. (let (fmt (cnt 0))
  15448. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15449. (org-set-local 'org-columns-current-fmt fmt)
  15450. (if (marker-position org-columns-top-level-marker)
  15451. (save-excursion
  15452. (goto-char org-columns-top-level-marker)
  15453. (if (and (org-at-heading-p)
  15454. (org-entry-get nil "COLUMNS"))
  15455. (org-entry-put nil "COLUMNS" fmt)
  15456. (goto-char (point-min))
  15457. ;; Overwrite all #+COLUMNS lines....
  15458. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15459. (setq cnt (1+ cnt))
  15460. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15461. (unless (> cnt 0)
  15462. (goto-char (point-min))
  15463. (or (org-on-heading-p t) (outline-next-heading))
  15464. (let ((inhibit-read-only t))
  15465. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15466. (org-set-local 'org-columns-default-format fmt))))))
  15467. (defvar org-overriding-columns-format nil
  15468. "When set, overrides any other definition.")
  15469. (defvar org-agenda-view-columns-initially nil
  15470. "When set, switch to columns view immediately after creating the agenda.")
  15471. (defun org-agenda-columns ()
  15472. "Turn on column view in the agenda."
  15473. (interactive)
  15474. (org-verify-version 'columns)
  15475. (org-columns-remove-overlays)
  15476. (move-marker org-columns-begin-marker (point))
  15477. (let (fmt cache maxwidths m)
  15478. (cond
  15479. ((and (local-variable-p 'org-overriding-columns-format)
  15480. org-overriding-columns-format)
  15481. (setq fmt org-overriding-columns-format))
  15482. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15483. (setq fmt (org-entry-get m "COLUMNS" t)))
  15484. ((and (boundp 'org-columns-current-fmt)
  15485. (local-variable-p 'org-columns-current-fmt)
  15486. org-columns-current-fmt)
  15487. (setq fmt org-columns-current-fmt))
  15488. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15489. (setq m (get-text-property m 'org-hd-marker))
  15490. (setq fmt (org-entry-get m "COLUMNS" t))))
  15491. (setq fmt (or fmt org-columns-default-format))
  15492. (org-set-local 'org-columns-current-fmt fmt)
  15493. (org-columns-compile-format fmt)
  15494. (save-excursion
  15495. ;; Get and cache the properties
  15496. (goto-char (point-min))
  15497. (while (not (eobp))
  15498. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15499. (get-text-property (point) 'org-marker)))
  15500. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15501. (beginning-of-line 2))
  15502. (when cache
  15503. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15504. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15505. (org-columns-display-here-title)
  15506. (mapc (lambda (x)
  15507. (goto-line (car x))
  15508. (org-columns-display-here (cdr x)))
  15509. cache)))))
  15510. (defun org-columns-get-autowidth-alist (s cache)
  15511. "Derive the maximum column widths from the format and the cache."
  15512. (let ((start 0) rtn)
  15513. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  15514. (push (cons (match-string 1 s) 1) rtn)
  15515. (setq start (match-end 0)))
  15516. (mapc (lambda (x)
  15517. (setcdr x (apply 'max
  15518. (mapcar
  15519. (lambda (y)
  15520. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15521. cache))))
  15522. rtn)
  15523. rtn))
  15524. (defun org-columns-compute-all ()
  15525. "Compute all columns that have operators defined."
  15526. (org-unmodified
  15527. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15528. (let ((columns org-columns-current-fmt-compiled) col)
  15529. (while (setq col (pop columns))
  15530. (when (nth 3 col)
  15531. (save-excursion
  15532. (org-columns-compute (car col)))))))
  15533. (defun org-columns-update (property)
  15534. "Recompute PROPERTY, and update the columns display for it."
  15535. (org-columns-compute property)
  15536. (let (fmt val pos)
  15537. (save-excursion
  15538. (mapc (lambda (ov)
  15539. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15540. (setq pos (org-overlay-start ov))
  15541. (goto-char pos)
  15542. (when (setq val (cdr (assoc property
  15543. (get-text-property
  15544. (point-at-bol) 'org-summaries))))
  15545. (setq fmt (org-overlay-get ov 'org-columns-format))
  15546. (org-overlay-put ov 'org-columns-value val)
  15547. (org-overlay-put ov 'display (format fmt val)))))
  15548. org-columns-overlays))))
  15549. (defun org-columns-compute (property)
  15550. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15551. (interactive)
  15552. (let* ((re (concat "^" outline-regexp))
  15553. (lmax 30) ; Does anyone use deeper levels???
  15554. (lsum (make-vector lmax 0))
  15555. (lflag (make-vector lmax nil))
  15556. (level 0)
  15557. (ass (assoc property org-columns-current-fmt-compiled))
  15558. (format (nth 4 ass))
  15559. (printf (nth 5 ass))
  15560. (beg org-columns-top-level-marker)
  15561. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15562. (save-excursion
  15563. ;; Find the region to compute
  15564. (goto-char beg)
  15565. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15566. (goto-char end)
  15567. ;; Walk the tree from the back and do the computations
  15568. (while (re-search-backward re beg t)
  15569. (setq sumpos (match-beginning 0)
  15570. last-level level
  15571. level (org-outline-level)
  15572. val (org-entry-get nil property)
  15573. valflag (and val (string-match "\\S-" val)))
  15574. (cond
  15575. ((< level last-level)
  15576. ;; put the sum of lower levels here as a property
  15577. (setq sum (aref lsum last-level) ; current sum
  15578. flag (aref lflag last-level) ; any valid entries from children?
  15579. str (org-column-number-to-string sum format printf)
  15580. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15581. useval (if flag str1 (if valflag val ""))
  15582. sum-alist (get-text-property sumpos 'org-summaries))
  15583. (if (assoc property sum-alist)
  15584. (setcdr (assoc property sum-alist) useval)
  15585. (push (cons property useval) sum-alist)
  15586. (org-unmodified
  15587. (add-text-properties sumpos (1+ sumpos)
  15588. (list 'org-summaries sum-alist))))
  15589. (when val
  15590. (org-entry-put nil property (if flag str val)))
  15591. ;; add current to current level accumulator
  15592. (when (or flag valflag)
  15593. (aset lsum level (+ (aref lsum level)
  15594. (if flag sum (org-column-string-to-number
  15595. (if flag str val) format))))
  15596. (aset lflag level t))
  15597. ;; clear accumulators for deeper levels
  15598. (loop for l from (1+ level) to (1- lmax) do
  15599. (aset lsum l 0)
  15600. (aset lflag l nil)))
  15601. ((>= level last-level)
  15602. ;; add what we have here to the accumulator for this level
  15603. (aset lsum level (+ (aref lsum level)
  15604. (org-column-string-to-number (or val "0") format)))
  15605. (and valflag (aset lflag level t)))
  15606. (t (error "This should not happen")))))))
  15607. (defun org-columns-redo ()
  15608. "Construct the column display again."
  15609. (interactive)
  15610. (message "Recomputing columns...")
  15611. (save-excursion
  15612. (if (marker-position org-columns-begin-marker)
  15613. (goto-char org-columns-begin-marker))
  15614. (org-columns-remove-overlays)
  15615. (if (org-mode-p)
  15616. (call-interactively 'org-columns)
  15617. (call-interactively 'org-agenda-columns)))
  15618. (message "Recomputing columns...done"))
  15619. (defun org-columns-not-in-agenda ()
  15620. (if (eq major-mode 'org-agenda-mode)
  15621. (error "This command is only allowed in Org-mode buffers")))
  15622. (defun org-string-to-number (s)
  15623. "Convert string to number, and interpret hh:mm:ss."
  15624. (if (not (string-match ":" s))
  15625. (string-to-number s)
  15626. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15627. (while l
  15628. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15629. sum)))
  15630. (defun org-column-number-to-string (n fmt &optional printf)
  15631. "Convert a computed column number to a string value, according to FMT."
  15632. (cond
  15633. ((eq fmt 'add_times)
  15634. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15635. (format "%d:%02d" h m)))
  15636. ((eq fmt 'checkbox)
  15637. (cond ((= n (floor n)) "[X]")
  15638. ((> n 1.) "[-]")
  15639. (t "[ ]")))
  15640. (printf (format printf n))
  15641. ((eq fmt 'currency)
  15642. (format "%.2f" n))
  15643. (t (number-to-string n))))
  15644. (defun org-column-string-to-number (s fmt)
  15645. "Convert a column value to a number that can be used for column computing."
  15646. (cond
  15647. ((string-match ":" s)
  15648. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15649. (while l
  15650. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15651. sum))
  15652. ((eq fmt 'checkbox)
  15653. (if (equal s "[X]") 1. 0.000001))
  15654. (t (string-to-number s))))
  15655. (defun org-columns-uncompile-format (cfmt)
  15656. "Turn the compiled columns format back into a string representation."
  15657. (let ((rtn "") e s prop title op width fmt printf)
  15658. (while (setq e (pop cfmt))
  15659. (setq prop (car e)
  15660. title (nth 1 e)
  15661. width (nth 2 e)
  15662. op (nth 3 e)
  15663. fmt (nth 4 e)
  15664. printf (nth 5 e))
  15665. (cond
  15666. ((eq fmt 'add_times) (setq op ":"))
  15667. ((eq fmt 'checkbox) (setq op "X"))
  15668. ((eq fmt 'add_numbers) (setq op "+"))
  15669. ((eq fmt 'currency) (setq op "$")))
  15670. (if (and op printf) (setq op (concat op ";" printf)))
  15671. (if (equal title prop) (setq title nil))
  15672. (setq s (concat "%" (if width (number-to-string width))
  15673. prop
  15674. (if title (concat "(" title ")"))
  15675. (if op (concat "{" op "}"))))
  15676. (setq rtn (concat rtn " " s)))
  15677. (org-trim rtn)))
  15678. (defun org-columns-compile-format (fmt)
  15679. "Turn a column format string into an alist of specifications.
  15680. The alist has one entry for each column in the format. The elements of
  15681. that list are:
  15682. property the property
  15683. title the title field for the columns
  15684. width the column width in characters, can be nil for automatic
  15685. operator the operator if any
  15686. format the output format for computed results, derived from operator
  15687. printf a printf format for computed values"
  15688. (let ((start 0) width prop title op f printf)
  15689. (setq org-columns-current-fmt-compiled nil)
  15690. (while (string-match
  15691. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15692. fmt start)
  15693. (setq start (match-end 0)
  15694. width (match-string 1 fmt)
  15695. prop (match-string 2 fmt)
  15696. title (or (match-string 3 fmt) prop)
  15697. op (match-string 4 fmt)
  15698. f nil
  15699. printf nil)
  15700. (if width (setq width (string-to-number width)))
  15701. (when (and op (string-match ";" op))
  15702. (setq printf (substring op (match-end 0))
  15703. op (substring op 0 (match-beginning 0))))
  15704. (cond
  15705. ((equal op "+") (setq f 'add_numbers))
  15706. ((equal op "$") (setq f 'currency))
  15707. ((equal op ":") (setq f 'add_times))
  15708. ((equal op "X") (setq f 'checkbox)))
  15709. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15710. (setq org-columns-current-fmt-compiled
  15711. (nreverse org-columns-current-fmt-compiled))))
  15712. ;;; Dynamic block for Column view
  15713. (defun org-columns-capture-view ()
  15714. "Get the column view of the current buffer and return it as a list.
  15715. The list will contains the title row and all other rows. Each row is
  15716. a list of fields."
  15717. (save-excursion
  15718. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15719. (n (length title)) row tbl)
  15720. (goto-char (point-min))
  15721. (while (re-search-forward "^\\*+ " nil t)
  15722. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15723. (setq row nil)
  15724. (loop for i from 0 to (1- n) do
  15725. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15726. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15727. "")
  15728. row))
  15729. (setq row (nreverse row))
  15730. (push row tbl)))
  15731. (append (list title 'hline) (nreverse tbl)))))
  15732. (defun org-dblock-write:columnview (params)
  15733. "Write the column view table.
  15734. PARAMS is a property list of parameters:
  15735. :width enforce same column widths with <N> specifiers.
  15736. :id the :ID: property of the entry where the columns view
  15737. should be built, as a string. When `local', call locally.
  15738. When `global' call column view with the cursor at the beginning
  15739. of the buffer (usually this means that the whole buffer switches
  15740. to column view).
  15741. :hlines When t, insert a hline before each item. When a number, insert
  15742. a hline before each level <= that number.
  15743. :vlines When t, make each column a colgroup to enforce vertical lines."
  15744. (let ((pos (move-marker (make-marker) (point)))
  15745. (hlines (plist-get params :hlines))
  15746. (vlines (plist-get params :vlines))
  15747. tbl id idpos nfields tmp)
  15748. (save-excursion
  15749. (save-restriction
  15750. (when (setq id (plist-get params :id))
  15751. (cond ((not id) nil)
  15752. ((eq id 'global) (goto-char (point-min)))
  15753. ((eq id 'local) nil)
  15754. ((setq idpos (org-find-entry-with-id id))
  15755. (goto-char idpos))
  15756. (t (error "Cannot find entry with :ID: %s" id))))
  15757. (org-columns)
  15758. (setq tbl (org-columns-capture-view))
  15759. (setq nfields (length (car tbl)))
  15760. (org-columns-quit)))
  15761. (goto-char pos)
  15762. (move-marker pos nil)
  15763. (when tbl
  15764. (when (plist-get params :hlines)
  15765. (setq tmp nil)
  15766. (while tbl
  15767. (if (eq (car tbl) 'hline)
  15768. (push (pop tbl) tmp)
  15769. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15770. (if (and (not (eq (car tmp) 'hline))
  15771. (or (eq hlines t)
  15772. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15773. (push 'hline tmp)))
  15774. (push (pop tbl) tmp)))
  15775. (setq tbl (nreverse tmp)))
  15776. (when vlines
  15777. (setq tbl (mapcar (lambda (x)
  15778. (if (eq 'hline x) x (cons "" x)))
  15779. tbl))
  15780. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15781. (setq pos (point))
  15782. (insert (org-listtable-to-string tbl))
  15783. (when (plist-get params :width)
  15784. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15785. org-columns-current-widths "|")))
  15786. (goto-char pos)
  15787. (org-table-align))))
  15788. (defun org-listtable-to-string (tbl)
  15789. "Convert a listtable TBL to a string that contains the Org-mode table.
  15790. The table still need to be alligned. The resulting string has no leading
  15791. and tailing newline characters."
  15792. (mapconcat
  15793. (lambda (x)
  15794. (cond
  15795. ((listp x)
  15796. (concat "|" (mapconcat 'identity x "|") "|"))
  15797. ((eq x 'hline) "|-|")
  15798. (t (error "Garbage in listtable: %s" x))))
  15799. tbl "\n"))
  15800. (defun org-insert-columns-dblock ()
  15801. "Create a dynamic block capturing a column view table."
  15802. (interactive)
  15803. (let ((defaults '(:name "columnview" :hlines 1))
  15804. (id (completing-read
  15805. "Capture columns (local, global, entry with :ID: property) [local]: "
  15806. (append '(("global") ("local"))
  15807. (mapcar 'list (org-property-values "ID"))))))
  15808. (if (equal id "") (setq id 'local))
  15809. (if (equal id "global") (setq id 'global))
  15810. (setq defaults (append defaults (list :id id)))
  15811. (org-create-dblock defaults)
  15812. (org-update-dblock)))
  15813. ;;;; Timestamps
  15814. (defvar org-last-changed-timestamp nil)
  15815. (defvar org-time-was-given) ; dynamically scoped parameter
  15816. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15817. (defvar org-ts-what) ; dynamically scoped parameter
  15818. (defun org-time-stamp (arg)
  15819. "Prompt for a date/time and insert a time stamp.
  15820. If the user specifies a time like HH:MM, or if this command is called
  15821. with a prefix argument, the time stamp will contain date and time.
  15822. Otherwise, only the date will be included. All parts of a date not
  15823. specified by the user will be filled in from the current date/time.
  15824. So if you press just return without typing anything, the time stamp
  15825. will represent the current date/time. If there is already a timestamp
  15826. at the cursor, it will be modified."
  15827. (interactive "P")
  15828. (let* ((ts nil)
  15829. (default-time
  15830. ;; Default time is either today, or, when entering a range,
  15831. ;; the range start.
  15832. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15833. (save-excursion
  15834. (re-search-backward
  15835. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15836. (- (point) 20) t)))
  15837. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15838. (current-time)))
  15839. (default-input (and ts (org-get-compact-tod ts)))
  15840. org-time-was-given org-end-time-was-given time)
  15841. (cond
  15842. ((and (org-at-timestamp-p)
  15843. (eq last-command 'org-time-stamp)
  15844. (eq this-command 'org-time-stamp))
  15845. (insert "--")
  15846. (setq time (let ((this-command this-command))
  15847. (org-read-date arg 'totime nil nil default-time default-input)))
  15848. (org-insert-time-stamp time (or org-time-was-given arg)))
  15849. ((org-at-timestamp-p)
  15850. (setq time (let ((this-command this-command))
  15851. (org-read-date arg 'totime nil nil default-time default-input)))
  15852. (when (org-at-timestamp-p) ; just to get the match data
  15853. (replace-match "")
  15854. (setq org-last-changed-timestamp
  15855. (org-insert-time-stamp
  15856. time (or org-time-was-given arg)
  15857. nil nil nil (list org-end-time-was-given))))
  15858. (message "Timestamp updated"))
  15859. (t
  15860. (setq time (let ((this-command this-command))
  15861. (org-read-date arg 'totime nil nil default-time default-input)))
  15862. (org-insert-time-stamp time (or org-time-was-given arg)
  15863. nil nil nil (list org-end-time-was-given))))))
  15864. ;; FIXME: can we use this for something else????
  15865. ;; like computing time differences?????
  15866. (defun org-get-compact-tod (s)
  15867. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15868. (let* ((t1 (match-string 1 s))
  15869. (h1 (string-to-number (match-string 2 s)))
  15870. (m1 (string-to-number (match-string 3 s)))
  15871. (t2 (and (match-end 4) (match-string 5 s)))
  15872. (h2 (and t2 (string-to-number (match-string 6 s))))
  15873. (m2 (and t2 (string-to-number (match-string 7 s))))
  15874. dh dm)
  15875. (if (not t2)
  15876. t1
  15877. (setq dh (- h2 h1) dm (- m2 m1))
  15878. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15879. (concat t1 "+" (number-to-string dh)
  15880. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  15881. (defun org-time-stamp-inactive (&optional arg)
  15882. "Insert an inactive time stamp.
  15883. An inactive time stamp is enclosed in square brackets instead of angle
  15884. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15885. does not link to the calendar and cannot be changed with the S-cursor keys.
  15886. So these are more for recording a certain time/date."
  15887. (interactive "P")
  15888. (let (org-time-was-given org-end-time-was-given time)
  15889. (setq time (org-read-date arg 'totime))
  15890. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  15891. nil nil (list org-end-time-was-given))))
  15892. (defvar org-date-ovl (org-make-overlay 1 1))
  15893. (org-overlay-put org-date-ovl 'face 'org-warning)
  15894. (org-detach-overlay org-date-ovl)
  15895. (defvar org-ans1) ; dynamically scoped parameter
  15896. (defvar org-ans2) ; dynamically scoped parameter
  15897. (defvar org-plain-time-of-day-regexp) ; defined below
  15898. (defvar org-read-date-overlay nil)
  15899. (defvar org-dcst nil) ; dynamically scoped
  15900. (defun org-read-date (&optional with-time to-time from-string prompt
  15901. default-time default-input)
  15902. "Read a date, possibly a time, and make things smooth for the user.
  15903. The prompt will suggest to enter an ISO date, but you can also enter anything
  15904. which will at least partially be understood by `parse-time-string'.
  15905. Unrecognized parts of the date will default to the current day, month, year,
  15906. hour and minute. If this command is called to replace a timestamp at point,
  15907. of to enter the second timestamp of a range, the default time is taken from the
  15908. existing stamp. For example,
  15909. 3-2-5 --> 2003-02-05
  15910. feb 15 --> currentyear-02-15
  15911. sep 12 9 --> 2009-09-12
  15912. 12:45 --> today 12:45
  15913. 22 sept 0:34 --> currentyear-09-22 0:34
  15914. 12 --> currentyear-currentmonth-12
  15915. Fri --> nearest Friday (today or later)
  15916. etc.
  15917. Furthermore you can specify a relative date by giving, as the *first* thing
  15918. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  15919. change in days weeks, months, years.
  15920. With a single plus or minus, the date is relative to today. With a double
  15921. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15922. +4d --> four days from today
  15923. +4 --> same as above
  15924. +2w --> two weeks from today
  15925. ++5 --> five days from default date
  15926. The function understands only English month and weekday abbreviations,
  15927. but this can be configured with the variables `parse-time-months' and
  15928. `parse-time-weekdays'.
  15929. While prompting, a calendar is popped up - you can also select the
  15930. date with the mouse (button 1). The calendar shows a period of three
  15931. months. To scroll it to other months, use the keys `>' and `<'.
  15932. If you don't like the calendar, turn it off with
  15933. \(setq org-read-date-popup-calendar nil)
  15934. With optional argument TO-TIME, the date will immediately be converted
  15935. to an internal time.
  15936. With an optional argument WITH-TIME, the prompt will suggest to also
  15937. insert a time. Note that when WITH-TIME is not set, you can still
  15938. enter a time, and this function will inform the calling routine about
  15939. this change. The calling routine may then choose to change the format
  15940. used to insert the time stamp into the buffer to include the time.
  15941. With optional argument FROM-STRING, read from this string instead from
  15942. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15943. the time/date that is used for everything that is not specified by the
  15944. user."
  15945. (require 'parse-time)
  15946. (let* ((org-time-stamp-rounding-minutes
  15947. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  15948. (org-dcst org-display-custom-times)
  15949. (ct (org-current-time))
  15950. (def (or default-time ct))
  15951. (defdecode (decode-time def))
  15952. (dummy (progn
  15953. (when (< (nth 2 defdecode) org-extend-today-until)
  15954. (setcar (nthcdr 2 defdecode) -1)
  15955. (setcar (nthcdr 1 defdecode) 59)
  15956. (setq def (apply 'encode-time defdecode)
  15957. defdecode (decode-time def)))))
  15958. (calendar-move-hook nil)
  15959. (view-diary-entries-initially nil)
  15960. (view-calendar-holidays-initially nil)
  15961. (timestr (format-time-string
  15962. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  15963. (prompt (concat (if prompt (concat prompt " ") "")
  15964. (format "Date+time [%s]: " timestr)))
  15965. ans (org-ans0 "") org-ans1 org-ans2 final)
  15966. (cond
  15967. (from-string (setq ans from-string))
  15968. (org-read-date-popup-calendar
  15969. (save-excursion
  15970. (save-window-excursion
  15971. (calendar)
  15972. (calendar-forward-day (- (time-to-days def)
  15973. (calendar-absolute-from-gregorian
  15974. (calendar-current-date))))
  15975. (org-eval-in-calendar nil t)
  15976. (let* ((old-map (current-local-map))
  15977. (map (copy-keymap calendar-mode-map))
  15978. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  15979. (org-defkey map (kbd "RET") 'org-calendar-select)
  15980. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  15981. 'org-calendar-select-mouse)
  15982. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  15983. 'org-calendar-select-mouse)
  15984. (org-defkey minibuffer-local-map [(meta shift left)]
  15985. (lambda () (interactive)
  15986. (org-eval-in-calendar '(calendar-backward-month 1))))
  15987. (org-defkey minibuffer-local-map [(meta shift right)]
  15988. (lambda () (interactive)
  15989. (org-eval-in-calendar '(calendar-forward-month 1))))
  15990. (org-defkey minibuffer-local-map [(meta shift up)]
  15991. (lambda () (interactive)
  15992. (org-eval-in-calendar '(calendar-backward-year 1))))
  15993. (org-defkey minibuffer-local-map [(meta shift down)]
  15994. (lambda () (interactive)
  15995. (org-eval-in-calendar '(calendar-forward-year 1))))
  15996. (org-defkey minibuffer-local-map [(shift up)]
  15997. (lambda () (interactive)
  15998. (org-eval-in-calendar '(calendar-backward-week 1))))
  15999. (org-defkey minibuffer-local-map [(shift down)]
  16000. (lambda () (interactive)
  16001. (org-eval-in-calendar '(calendar-forward-week 1))))
  16002. (org-defkey minibuffer-local-map [(shift left)]
  16003. (lambda () (interactive)
  16004. (org-eval-in-calendar '(calendar-backward-day 1))))
  16005. (org-defkey minibuffer-local-map [(shift right)]
  16006. (lambda () (interactive)
  16007. (org-eval-in-calendar '(calendar-forward-day 1))))
  16008. (org-defkey minibuffer-local-map ">"
  16009. (lambda () (interactive)
  16010. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16011. (org-defkey minibuffer-local-map "<"
  16012. (lambda () (interactive)
  16013. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16014. (unwind-protect
  16015. (progn
  16016. (use-local-map map)
  16017. (add-hook 'post-command-hook 'org-read-date-display)
  16018. (setq org-ans0 (read-string prompt default-input nil nil))
  16019. ;; org-ans0: from prompt
  16020. ;; org-ans1: from mouse click
  16021. ;; org-ans2: from calendar motion
  16022. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16023. (remove-hook 'post-command-hook 'org-read-date-display)
  16024. (use-local-map old-map)
  16025. (when org-read-date-overlay
  16026. (org-delete-overlay org-read-date-overlay)
  16027. (setq org-read-date-overlay nil)))))))
  16028. (t ; Naked prompt only
  16029. (unwind-protect
  16030. (setq ans (read-string prompt default-input nil timestr))
  16031. (when org-read-date-overlay
  16032. (org-delete-overlay org-read-date-overlay)
  16033. (setq org-read-date-overlay nil)))))
  16034. (setq final (org-read-date-analyze ans def defdecode))
  16035. (if to-time
  16036. (apply 'encode-time final)
  16037. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16038. (format "%04d-%02d-%02d %02d:%02d"
  16039. (nth 5 final) (nth 4 final) (nth 3 final)
  16040. (nth 2 final) (nth 1 final))
  16041. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16042. (defvar def)
  16043. (defvar defdecode)
  16044. (defvar with-time)
  16045. (defun org-read-date-display ()
  16046. "Display the currrent date prompt interpretation in the minibuffer."
  16047. (when org-read-date-display-live
  16048. (when org-read-date-overlay
  16049. (org-delete-overlay org-read-date-overlay))
  16050. (let ((p (point)))
  16051. (end-of-line 1)
  16052. (while (not (equal (buffer-substring
  16053. (max (point-min) (- (point) 4)) (point))
  16054. " "))
  16055. (insert " "))
  16056. (goto-char p))
  16057. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16058. " " (or org-ans1 org-ans2)))
  16059. (org-end-time-was-given nil)
  16060. (f (org-read-date-analyze ans def defdecode))
  16061. (fmts (if org-dcst
  16062. org-time-stamp-custom-formats
  16063. org-time-stamp-formats))
  16064. (fmt (if (or with-time
  16065. (and (boundp 'org-time-was-given) org-time-was-given))
  16066. (cdr fmts)
  16067. (car fmts)))
  16068. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16069. (when (and org-end-time-was-given
  16070. (string-match org-plain-time-of-day-regexp txt))
  16071. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16072. org-end-time-was-given
  16073. (substring txt (match-end 0)))))
  16074. (setq org-read-date-overlay
  16075. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16076. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16077. (defun org-read-date-analyze (ans def defdecode)
  16078. "Analyze the combined answer of the date prompt."
  16079. ;; FIXME: cleanup and comment
  16080. (let (delta deltan deltaw deltadef year month day
  16081. hour minute second wday pm h2 m2 tl wday1)
  16082. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16083. (setq ans (replace-match "" t t ans)
  16084. deltan (car delta)
  16085. deltaw (nth 1 delta)
  16086. deltadef (nth 2 delta)))
  16087. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16088. (when (string-match
  16089. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16090. (setq year (if (match-end 2)
  16091. (string-to-number (match-string 2 ans))
  16092. (string-to-number (format-time-string "%Y")))
  16093. month (string-to-number (match-string 3 ans))
  16094. day (string-to-number (match-string 4 ans)))
  16095. (if (< year 100) (setq year (+ 2000 year)))
  16096. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16097. t nil ans)))
  16098. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16099. ;; If there is a time with am/pm, and *no* time without it, we convert
  16100. ;; so that matching will be successful.
  16101. (loop for i from 1 to 2 do ; twice, for end time as well
  16102. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16103. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16104. (setq hour (string-to-number (match-string 1 ans))
  16105. minute (if (match-end 3)
  16106. (string-to-number (match-string 3 ans))
  16107. 0)
  16108. pm (equal ?p
  16109. (string-to-char (downcase (match-string 4 ans)))))
  16110. (if (and (= hour 12) (not pm))
  16111. (setq hour 0)
  16112. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16113. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16114. t t ans))))
  16115. ;; Check if a time range is given as a duration
  16116. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16117. (setq hour (string-to-number (match-string 1 ans))
  16118. h2 (+ hour (string-to-number (match-string 3 ans)))
  16119. minute (string-to-number (match-string 2 ans))
  16120. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16121. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16122. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16123. ;; Check if there is a time range
  16124. (when (boundp 'org-end-time-was-given)
  16125. (setq org-time-was-given nil)
  16126. (when (and (string-match org-plain-time-of-day-regexp ans)
  16127. (match-end 8))
  16128. (setq org-end-time-was-given (match-string 8 ans))
  16129. (setq ans (concat (substring ans 0 (match-beginning 7))
  16130. (substring ans (match-end 7))))))
  16131. (setq tl (parse-time-string ans)
  16132. day (or (nth 3 tl) (nth 3 defdecode))
  16133. month (or (nth 4 tl)
  16134. (if (and org-read-date-prefer-future
  16135. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16136. (1+ (nth 4 defdecode))
  16137. (nth 4 defdecode)))
  16138. year (or (nth 5 tl)
  16139. (if (and org-read-date-prefer-future
  16140. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16141. (1+ (nth 5 defdecode))
  16142. (nth 5 defdecode)))
  16143. hour (or (nth 2 tl) (nth 2 defdecode))
  16144. minute (or (nth 1 tl) (nth 1 defdecode))
  16145. second (or (nth 0 tl) 0)
  16146. wday (nth 6 tl))
  16147. (when deltan
  16148. (unless deltadef
  16149. (let ((now (decode-time (current-time))))
  16150. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16151. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16152. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16153. ((equal deltaw "m") (setq month (+ month deltan)))
  16154. ((equal deltaw "y") (setq year (+ year deltan)))))
  16155. (when (and wday (not (nth 3 tl)))
  16156. ;; Weekday was given, but no day, so pick that day in the week
  16157. ;; on or after the derived date.
  16158. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16159. (unless (equal wday wday1)
  16160. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16161. (if (and (boundp 'org-time-was-given)
  16162. (nth 2 tl))
  16163. (setq org-time-was-given t))
  16164. (if (< year 100) (setq year (+ 2000 year)))
  16165. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16166. (list second minute hour day month year)))
  16167. (defvar parse-time-weekdays)
  16168. (defun org-read-date-get-relative (s today default)
  16169. "Check string S for special relative date string.
  16170. TODAY and DEFAULT are internal times, for today and for a default.
  16171. Return shift list (N what def-flag)
  16172. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16173. N is the number of WHATs to shift.
  16174. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16175. the DEFAULT date rather than TODAY."
  16176. (when (string-match
  16177. (concat
  16178. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16179. "\\([0-9]+\\)?"
  16180. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16181. "\\([ \t]\\|$\\)") s)
  16182. (let* ((dir (if (match-end 1)
  16183. (string-to-char (substring (match-string 1 s) -1))
  16184. ?+))
  16185. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16186. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16187. (what (if (match-end 3) (match-string 3 s) "d"))
  16188. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16189. (date (if rel default today))
  16190. (wday (nth 6 (decode-time date)))
  16191. delta)
  16192. (if wday1
  16193. (progn
  16194. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16195. (if (= dir ?-) (setq delta (- delta 7)))
  16196. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16197. (list delta "d" rel))
  16198. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16199. (defun org-eval-in-calendar (form &optional keepdate)
  16200. "Eval FORM in the calendar window and return to current window.
  16201. Also, store the cursor date in variable org-ans2."
  16202. (let ((sw (selected-window)))
  16203. (select-window (get-buffer-window "*Calendar*"))
  16204. (eval form)
  16205. (when (and (not keepdate) (calendar-cursor-to-date))
  16206. (let* ((date (calendar-cursor-to-date))
  16207. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16208. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16209. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16210. (select-window sw)))
  16211. ; ;; Update the prompt to show new default date
  16212. ; (save-excursion
  16213. ; (goto-char (point-min))
  16214. ; (when (and org-ans2
  16215. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16216. ; (get-text-property (match-end 0) 'field))
  16217. ; (let ((inhibit-read-only t))
  16218. ; (replace-match (concat "[" org-ans2 "]") t t)
  16219. ; (add-text-properties (point-min) (1+ (match-end 0))
  16220. ; (text-properties-at (1+ (point-min)))))))))
  16221. (defun org-calendar-select ()
  16222. "Return to `org-read-date' with the date currently selected.
  16223. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16224. (interactive)
  16225. (when (calendar-cursor-to-date)
  16226. (let* ((date (calendar-cursor-to-date))
  16227. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16228. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16229. (if (active-minibuffer-window) (exit-minibuffer))))
  16230. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16231. "Insert a date stamp for the date given by the internal TIME.
  16232. WITH-HM means, use the stamp format that includes the time of the day.
  16233. INACTIVE means use square brackets instead of angular ones, so that the
  16234. stamp will not contribute to the agenda.
  16235. PRE and POST are optional strings to be inserted before and after the
  16236. stamp.
  16237. The command returns the inserted time stamp."
  16238. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16239. stamp)
  16240. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16241. (insert-before-markers (or pre ""))
  16242. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16243. (when (listp extra)
  16244. (setq extra (car extra))
  16245. (if (and (stringp extra)
  16246. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16247. (setq extra (format "-%02d:%02d"
  16248. (string-to-number (match-string 1 extra))
  16249. (string-to-number (match-string 2 extra))))
  16250. (setq extra nil)))
  16251. (when extra
  16252. (backward-char 1)
  16253. (insert-before-markers extra)
  16254. (forward-char 1))
  16255. (insert-before-markers (or post ""))
  16256. stamp))
  16257. (defun org-toggle-time-stamp-overlays ()
  16258. "Toggle the use of custom time stamp formats."
  16259. (interactive)
  16260. (setq org-display-custom-times (not org-display-custom-times))
  16261. (unless org-display-custom-times
  16262. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16263. (while (setq p (next-single-property-change p 'display))
  16264. (if (and (get-text-property p 'display)
  16265. (eq (get-text-property p 'face) 'org-date))
  16266. (remove-text-properties
  16267. p (setq p (next-single-property-change p 'display))
  16268. '(display t))))
  16269. (set-buffer-modified-p bmp)))
  16270. (if (featurep 'xemacs)
  16271. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16272. (org-restart-font-lock)
  16273. (setq org-table-may-need-update t)
  16274. (if org-display-custom-times
  16275. (message "Time stamps are overlayed with custom format")
  16276. (message "Time stamp overlays removed")))
  16277. (defun org-display-custom-time (beg end)
  16278. "Overlay modified time stamp format over timestamp between BED and END."
  16279. (let* ((ts (buffer-substring beg end))
  16280. t1 w1 with-hm tf time str w2 (off 0))
  16281. (save-match-data
  16282. (setq t1 (org-parse-time-string ts t))
  16283. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16284. (setq off (- (match-end 0) (match-beginning 0)))))
  16285. (setq end (- end off))
  16286. (setq w1 (- end beg)
  16287. with-hm (and (nth 1 t1) (nth 2 t1))
  16288. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16289. time (org-fix-decoded-time t1)
  16290. str (org-add-props
  16291. (format-time-string
  16292. (substring tf 1 -1) (apply 'encode-time time))
  16293. nil 'mouse-face 'highlight)
  16294. w2 (length str))
  16295. (if (not (= w2 w1))
  16296. (add-text-properties (1+ beg) (+ 2 beg)
  16297. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16298. (if (featurep 'xemacs)
  16299. (progn
  16300. (put-text-property beg end 'invisible t)
  16301. (put-text-property beg end 'end-glyph (make-glyph str)))
  16302. (put-text-property beg end 'display str))))
  16303. (defun org-translate-time (string)
  16304. "Translate all timestamps in STRING to custom format.
  16305. But do this only if the variable `org-display-custom-times' is set."
  16306. (when org-display-custom-times
  16307. (save-match-data
  16308. (let* ((start 0)
  16309. (re org-ts-regexp-both)
  16310. t1 with-hm inactive tf time str beg end)
  16311. (while (setq start (string-match re string start))
  16312. (setq beg (match-beginning 0)
  16313. end (match-end 0)
  16314. t1 (save-match-data
  16315. (org-parse-time-string (substring string beg end) t))
  16316. with-hm (and (nth 1 t1) (nth 2 t1))
  16317. inactive (equal (substring string beg (1+ beg)) "[")
  16318. tf (funcall (if with-hm 'cdr 'car)
  16319. org-time-stamp-custom-formats)
  16320. time (org-fix-decoded-time t1)
  16321. str (format-time-string
  16322. (concat
  16323. (if inactive "[" "<") (substring tf 1 -1)
  16324. (if inactive "]" ">"))
  16325. (apply 'encode-time time))
  16326. string (replace-match str t t string)
  16327. start (+ start (length str)))))))
  16328. string)
  16329. (defun org-fix-decoded-time (time)
  16330. "Set 0 instead of nil for the first 6 elements of time.
  16331. Don't touch the rest."
  16332. (let ((n 0))
  16333. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16334. (defun org-days-to-time (timestamp-string)
  16335. "Difference between TIMESTAMP-STRING and now in days."
  16336. (- (time-to-days (org-time-string-to-time timestamp-string))
  16337. (time-to-days (current-time))))
  16338. (defun org-deadline-close (timestamp-string &optional ndays)
  16339. "Is the time in TIMESTAMP-STRING close to the current date?"
  16340. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16341. (and (< (org-days-to-time timestamp-string) ndays)
  16342. (not (org-entry-is-done-p))))
  16343. (defun org-get-wdays (ts)
  16344. "Get the deadline lead time appropriate for timestring TS."
  16345. (cond
  16346. ((<= org-deadline-warning-days 0)
  16347. ;; 0 or negative, enforce this value no matter what
  16348. (- org-deadline-warning-days))
  16349. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16350. ;; lead time is specified.
  16351. (floor (* (string-to-number (match-string 1 ts))
  16352. (cdr (assoc (match-string 2 ts)
  16353. '(("d" . 1) ("w" . 7)
  16354. ("m" . 30.4) ("y" . 365.25)))))))
  16355. ;; go for the default.
  16356. (t org-deadline-warning-days)))
  16357. (defun org-calendar-select-mouse (ev)
  16358. "Return to `org-read-date' with the date currently selected.
  16359. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16360. (interactive "e")
  16361. (mouse-set-point ev)
  16362. (when (calendar-cursor-to-date)
  16363. (let* ((date (calendar-cursor-to-date))
  16364. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16365. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16366. (if (active-minibuffer-window) (exit-minibuffer))))
  16367. (defun org-check-deadlines (ndays)
  16368. "Check if there are any deadlines due or past due.
  16369. A deadline is considered due if it happens within `org-deadline-warning-days'
  16370. days from today's date. If the deadline appears in an entry marked DONE,
  16371. it is not shown. The prefix arg NDAYS can be used to test that many
  16372. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16373. (interactive "P")
  16374. (let* ((org-warn-days
  16375. (cond
  16376. ((equal ndays '(4)) 100000)
  16377. (ndays (prefix-numeric-value ndays))
  16378. (t (abs org-deadline-warning-days))))
  16379. (case-fold-search nil)
  16380. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16381. (callback
  16382. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16383. (message "%d deadlines past-due or due within %d days"
  16384. (org-occur regexp nil callback)
  16385. org-warn-days)))
  16386. (defun org-check-before-date (date)
  16387. "Check if there are deadlines or scheduled entries before DATE."
  16388. (interactive (list (org-read-date)))
  16389. (let ((case-fold-search nil)
  16390. (regexp (concat "\\<\\(" org-deadline-string
  16391. "\\|" org-scheduled-string
  16392. "\\) *<\\([^>]+\\)>"))
  16393. (callback
  16394. (lambda () (time-less-p
  16395. (org-time-string-to-time (match-string 2))
  16396. (org-time-string-to-time date)))))
  16397. (message "%d entries before %s"
  16398. (org-occur regexp nil callback) date)))
  16399. (defun org-evaluate-time-range (&optional to-buffer)
  16400. "Evaluate a time range by computing the difference between start and end.
  16401. Normally the result is just printed in the echo area, but with prefix arg
  16402. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16403. If the time range is actually in a table, the result is inserted into the
  16404. next column.
  16405. For time difference computation, a year is assumed to be exactly 365
  16406. days in order to avoid rounding problems."
  16407. (interactive "P")
  16408. (or
  16409. (org-clock-update-time-maybe)
  16410. (save-excursion
  16411. (unless (org-at-date-range-p t)
  16412. (goto-char (point-at-bol))
  16413. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16414. (if (not (org-at-date-range-p t))
  16415. (error "Not at a time-stamp range, and none found in current line")))
  16416. (let* ((ts1 (match-string 1))
  16417. (ts2 (match-string 2))
  16418. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16419. (match-end (match-end 0))
  16420. (time1 (org-time-string-to-time ts1))
  16421. (time2 (org-time-string-to-time ts2))
  16422. (t1 (time-to-seconds time1))
  16423. (t2 (time-to-seconds time2))
  16424. (diff (abs (- t2 t1)))
  16425. (negative (< (- t2 t1) 0))
  16426. ;; (ys (floor (* 365 24 60 60)))
  16427. (ds (* 24 60 60))
  16428. (hs (* 60 60))
  16429. (fy "%dy %dd %02d:%02d")
  16430. (fy1 "%dy %dd")
  16431. (fd "%dd %02d:%02d")
  16432. (fd1 "%dd")
  16433. (fh "%02d:%02d")
  16434. y d h m align)
  16435. (if havetime
  16436. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16437. y 0
  16438. d (floor (/ diff ds)) diff (mod diff ds)
  16439. h (floor (/ diff hs)) diff (mod diff hs)
  16440. m (floor (/ diff 60)))
  16441. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16442. y 0
  16443. d (floor (+ (/ diff ds) 0.5))
  16444. h 0 m 0))
  16445. (if (not to-buffer)
  16446. (message "%s" (org-make-tdiff-string y d h m))
  16447. (if (org-at-table-p)
  16448. (progn
  16449. (goto-char match-end)
  16450. (setq align t)
  16451. (and (looking-at " *|") (goto-char (match-end 0))))
  16452. (goto-char match-end))
  16453. (if (looking-at
  16454. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16455. (replace-match ""))
  16456. (if negative (insert " -"))
  16457. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16458. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16459. (insert " " (format fh h m))))
  16460. (if align (org-table-align))
  16461. (message "Time difference inserted")))))
  16462. (defun org-make-tdiff-string (y d h m)
  16463. (let ((fmt "")
  16464. (l nil))
  16465. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16466. l (push y l)))
  16467. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16468. l (push d l)))
  16469. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16470. l (push h l)))
  16471. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16472. l (push m l)))
  16473. (apply 'format fmt (nreverse l))))
  16474. (defun org-time-string-to-time (s)
  16475. (apply 'encode-time (org-parse-time-string s)))
  16476. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16477. "Convert a time stamp to an absolute day number.
  16478. If there is a specifyer for a cyclic time stamp, get the closest date to
  16479. DAYNR."
  16480. (cond
  16481. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16482. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16483. daynr
  16484. (+ daynr 1000)))
  16485. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16486. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16487. (time-to-days (current-time))) (match-string 0 s)
  16488. prefer))
  16489. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16490. (defun org-time-from-absolute (d)
  16491. "Return the time corresponding to date D.
  16492. D may be an absolute day number, or a calendar-type list (month day year)."
  16493. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16494. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16495. (defun org-calendar-holiday ()
  16496. "List of holidays, for Diary display in Org-mode."
  16497. (require 'holidays)
  16498. (let ((hl (funcall
  16499. (if (fboundp 'calendar-check-holidays)
  16500. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16501. (if hl (mapconcat 'identity hl "; "))))
  16502. (defun org-diary-sexp-entry (sexp entry date)
  16503. "Process a SEXP diary ENTRY for DATE."
  16504. (require 'diary-lib)
  16505. (let ((result (if calendar-debug-sexp
  16506. (let ((stack-trace-on-error t))
  16507. (eval (car (read-from-string sexp))))
  16508. (condition-case nil
  16509. (eval (car (read-from-string sexp)))
  16510. (error
  16511. (beep)
  16512. (message "Bad sexp at line %d in %s: %s"
  16513. (org-current-line)
  16514. (buffer-file-name) sexp)
  16515. (sleep-for 2))))))
  16516. (cond ((stringp result) result)
  16517. ((and (consp result)
  16518. (stringp (cdr result))) (cdr result))
  16519. (result entry)
  16520. (t nil))))
  16521. (defun org-diary-to-ical-string (frombuf)
  16522. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16523. This uses the icalendar.el library."
  16524. (let* ((tmpdir (if (featurep 'xemacs)
  16525. (temp-directory)
  16526. temporary-file-directory))
  16527. (tmpfile (make-temp-name
  16528. (expand-file-name "orgics" tmpdir)))
  16529. buf rtn b e)
  16530. (save-excursion
  16531. (set-buffer frombuf)
  16532. (icalendar-export-region (point-min) (point-max) tmpfile)
  16533. (setq buf (find-buffer-visiting tmpfile))
  16534. (set-buffer buf)
  16535. (goto-char (point-min))
  16536. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16537. (setq b (match-beginning 0)))
  16538. (goto-char (point-max))
  16539. (if (re-search-backward "^END:VEVENT" nil t)
  16540. (setq e (match-end 0)))
  16541. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16542. (kill-buffer buf)
  16543. (kill-buffer frombuf)
  16544. (delete-file tmpfile)
  16545. rtn))
  16546. (defun org-closest-date (start current change prefer)
  16547. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16548. When PREFER is `past' return a date that is either CURRENT or past.
  16549. When PREFER is `future', return a date that is either CURRENT or future."
  16550. ;; Make the proper lists from the dates
  16551. (catch 'exit
  16552. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16553. dn dw sday cday n1 n2
  16554. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16555. (setq start (org-date-to-gregorian start)
  16556. current (org-date-to-gregorian
  16557. (if org-agenda-repeating-timestamp-show-all
  16558. current
  16559. (time-to-days (current-time))))
  16560. sday (calendar-absolute-from-gregorian start)
  16561. cday (calendar-absolute-from-gregorian current))
  16562. (if (<= cday sday) (throw 'exit sday))
  16563. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16564. (setq dn (string-to-number (match-string 1 change))
  16565. dw (cdr (assoc (match-string 2 change) a1)))
  16566. (error "Invalid change specifyer: %s" change))
  16567. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16568. (cond
  16569. ((eq dw 'day)
  16570. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16571. n2 (+ n1 dn)))
  16572. ((eq dw 'year)
  16573. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16574. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16575. (setq date1 (list m d y1)
  16576. n1 (calendar-absolute-from-gregorian date1)
  16577. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16578. n2 (calendar-absolute-from-gregorian date2)))
  16579. ((eq dw 'month)
  16580. ;; approx number of month between the tow dates
  16581. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16582. ;; How often does dn fit in there?
  16583. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16584. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16585. m (+ m nm)
  16586. ny (floor (/ m 12))
  16587. y (+ y ny)
  16588. m (- m (* ny 12)))
  16589. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16590. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16591. (setq m2 (+ m dn) y2 y)
  16592. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16593. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16594. (while (< n2 cday)
  16595. (setq n1 n2 m m2 y y2)
  16596. (setq m2 (+ m dn) y2 y)
  16597. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16598. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16599. (if org-agenda-repeating-timestamp-show-all
  16600. (cond
  16601. ((eq prefer 'past) n1)
  16602. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16603. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16604. (cond
  16605. ((eq prefer 'past) n1)
  16606. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16607. (t (if (= cday n1) n1 n2)))))))
  16608. (defun org-date-to-gregorian (date)
  16609. "Turn any specification of DATE into a gregorian date for the calendar."
  16610. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16611. ((and (listp date) (= (length date) 3)) date)
  16612. ((stringp date)
  16613. (setq date (org-parse-time-string date))
  16614. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16615. ((listp date)
  16616. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16617. (defun org-parse-time-string (s &optional nodefault)
  16618. "Parse the standard Org-mode time string.
  16619. This should be a lot faster than the normal `parse-time-string'.
  16620. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16621. hour and minute fields will be nil if not given."
  16622. (if (string-match org-ts-regexp0 s)
  16623. (list 0
  16624. (if (or (match-beginning 8) (not nodefault))
  16625. (string-to-number (or (match-string 8 s) "0")))
  16626. (if (or (match-beginning 7) (not nodefault))
  16627. (string-to-number (or (match-string 7 s) "0")))
  16628. (string-to-number (match-string 4 s))
  16629. (string-to-number (match-string 3 s))
  16630. (string-to-number (match-string 2 s))
  16631. nil nil nil)
  16632. (make-list 9 0)))
  16633. (defun org-timestamp-up (&optional arg)
  16634. "Increase the date item at the cursor by one.
  16635. If the cursor is on the year, change the year. If it is on the month or
  16636. the day, change that.
  16637. With prefix ARG, change by that many units."
  16638. (interactive "p")
  16639. (org-timestamp-change (prefix-numeric-value arg)))
  16640. (defun org-timestamp-down (&optional arg)
  16641. "Decrease the date item at the cursor by one.
  16642. If the cursor is on the year, change the year. If it is on the month or
  16643. the day, change that.
  16644. With prefix ARG, change by that many units."
  16645. (interactive "p")
  16646. (org-timestamp-change (- (prefix-numeric-value arg))))
  16647. (defun org-timestamp-up-day (&optional arg)
  16648. "Increase the date in the time stamp by one day.
  16649. With prefix ARG, change that many days."
  16650. (interactive "p")
  16651. (if (and (not (org-at-timestamp-p t))
  16652. (org-on-heading-p))
  16653. (org-todo 'up)
  16654. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16655. (defun org-timestamp-down-day (&optional arg)
  16656. "Decrease the date in the time stamp by one day.
  16657. With prefix ARG, change that many days."
  16658. (interactive "p")
  16659. (if (and (not (org-at-timestamp-p t))
  16660. (org-on-heading-p))
  16661. (org-todo 'down)
  16662. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16663. (defsubst org-pos-in-match-range (pos n)
  16664. (and (match-beginning n)
  16665. (<= (match-beginning n) pos)
  16666. (>= (match-end n) pos)))
  16667. (defun org-at-timestamp-p (&optional inactive-ok)
  16668. "Determine if the cursor is in or at a timestamp."
  16669. (interactive)
  16670. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16671. (pos (point))
  16672. (ans (or (looking-at tsr)
  16673. (save-excursion
  16674. (skip-chars-backward "^[<\n\r\t")
  16675. (if (> (point) (point-min)) (backward-char 1))
  16676. (and (looking-at tsr)
  16677. (> (- (match-end 0) pos) -1))))))
  16678. (and ans
  16679. (boundp 'org-ts-what)
  16680. (setq org-ts-what
  16681. (cond
  16682. ((= pos (match-beginning 0)) 'bracket)
  16683. ((= pos (1- (match-end 0))) 'bracket)
  16684. ((org-pos-in-match-range pos 2) 'year)
  16685. ((org-pos-in-match-range pos 3) 'month)
  16686. ((org-pos-in-match-range pos 7) 'hour)
  16687. ((org-pos-in-match-range pos 8) 'minute)
  16688. ((or (org-pos-in-match-range pos 4)
  16689. (org-pos-in-match-range pos 5)) 'day)
  16690. ((and (> pos (or (match-end 8) (match-end 5)))
  16691. (< pos (match-end 0)))
  16692. (- pos (or (match-end 8) (match-end 5))))
  16693. (t 'day))))
  16694. ans))
  16695. (defun org-toggle-timestamp-type ()
  16696. ""
  16697. (interactive)
  16698. (when (org-at-timestamp-p t)
  16699. (save-excursion
  16700. (goto-char (match-beginning 0))
  16701. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16702. (goto-char (1- (match-end 0)))
  16703. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16704. (message "Timestamp is now %sactive"
  16705. (if (equal (char-before) ?>) "in" ""))))
  16706. (defun org-timestamp-change (n &optional what)
  16707. "Change the date in the time stamp at point.
  16708. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16709. `year', `minute', `second'. If WHAT is not given, the cursor position
  16710. in the timestamp determines what will be changed."
  16711. (let ((pos (point))
  16712. with-hm inactive
  16713. org-ts-what
  16714. extra
  16715. ts time time0)
  16716. (if (not (org-at-timestamp-p t))
  16717. (error "Not at a timestamp"))
  16718. (if (and (not what) (eq org-ts-what 'bracket))
  16719. (org-toggle-timestamp-type)
  16720. (if (and (not what) (not (eq org-ts-what 'day))
  16721. org-display-custom-times
  16722. (get-text-property (point) 'display)
  16723. (not (get-text-property (1- (point)) 'display)))
  16724. (setq org-ts-what 'day))
  16725. (setq org-ts-what (or what org-ts-what)
  16726. inactive (= (char-after (match-beginning 0)) ?\[)
  16727. ts (match-string 0))
  16728. (replace-match "")
  16729. (if (string-match
  16730. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16731. ts)
  16732. (setq extra (match-string 1 ts)))
  16733. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16734. (setq with-hm t))
  16735. (setq time0 (org-parse-time-string ts))
  16736. (setq time
  16737. (encode-time (or (car time0) 0)
  16738. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16739. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16740. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16741. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16742. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16743. (nthcdr 6 time0)))
  16744. (when (integerp org-ts-what)
  16745. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16746. (if (eq what 'calendar)
  16747. (let ((cal-date (org-get-date-from-calendar)))
  16748. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16749. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16750. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16751. (setcar time0 (or (car time0) 0))
  16752. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16753. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16754. (setq time (apply 'encode-time time0))))
  16755. (setq org-last-changed-timestamp
  16756. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16757. (org-clock-update-time-maybe)
  16758. (goto-char pos)
  16759. ;; Try to recenter the calendar window, if any
  16760. (if (and org-calendar-follow-timestamp-change
  16761. (get-buffer-window "*Calendar*" t)
  16762. (memq org-ts-what '(day month year)))
  16763. (org-recenter-calendar (time-to-days time))))))
  16764. ;; FIXME: does not yet work for lead times
  16765. (defun org-modify-ts-extra (s pos n)
  16766. "Change the different parts of the lead-time and repeat fields in timestamp."
  16767. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16768. ng h m new)
  16769. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16770. (cond
  16771. ((or (org-pos-in-match-range pos 2)
  16772. (org-pos-in-match-range pos 3))
  16773. (setq m (string-to-number (match-string 3 s))
  16774. h (string-to-number (match-string 2 s)))
  16775. (if (org-pos-in-match-range pos 2)
  16776. (setq h (+ h n))
  16777. (setq m (+ m n)))
  16778. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16779. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16780. (setq h (min 24 (max 0 h)))
  16781. (setq ng 1 new (format "-%02d:%02d" h m)))
  16782. ((org-pos-in-match-range pos 6)
  16783. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16784. ((org-pos-in-match-range pos 5)
  16785. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16786. (when ng
  16787. (setq s (concat
  16788. (substring s 0 (match-beginning ng))
  16789. new
  16790. (substring s (match-end ng))))))
  16791. s))
  16792. (defun org-recenter-calendar (date)
  16793. "If the calendar is visible, recenter it to DATE."
  16794. (let* ((win (selected-window))
  16795. (cwin (get-buffer-window "*Calendar*" t))
  16796. (calendar-move-hook nil))
  16797. (when cwin
  16798. (select-window cwin)
  16799. (calendar-goto-date (if (listp date) date
  16800. (calendar-gregorian-from-absolute date)))
  16801. (select-window win))))
  16802. (defun org-goto-calendar (&optional arg)
  16803. "Go to the Emacs calendar at the current date.
  16804. If there is a time stamp in the current line, go to that date.
  16805. A prefix ARG can be used to force the current date."
  16806. (interactive "P")
  16807. (let ((tsr org-ts-regexp) diff
  16808. (calendar-move-hook nil)
  16809. (view-calendar-holidays-initially nil)
  16810. (view-diary-entries-initially nil))
  16811. (if (or (org-at-timestamp-p)
  16812. (save-excursion
  16813. (beginning-of-line 1)
  16814. (looking-at (concat ".*" tsr))))
  16815. (let ((d1 (time-to-days (current-time)))
  16816. (d2 (time-to-days
  16817. (org-time-string-to-time (match-string 1)))))
  16818. (setq diff (- d2 d1))))
  16819. (calendar)
  16820. (calendar-goto-today)
  16821. (if (and diff (not arg)) (calendar-forward-day diff))))
  16822. (defun org-get-date-from-calendar ()
  16823. "Return a list (month day year) of date at point in calendar."
  16824. (with-current-buffer "*Calendar*"
  16825. (save-match-data
  16826. (calendar-cursor-to-date))))
  16827. (defun org-date-from-calendar ()
  16828. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16829. If there is already a time stamp at the cursor position, update it."
  16830. (interactive)
  16831. (if (org-at-timestamp-p t)
  16832. (org-timestamp-change 0 'calendar)
  16833. (let ((cal-date (org-get-date-from-calendar)))
  16834. (org-insert-time-stamp
  16835. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16836. ;; Make appt aware of appointments from the agenda
  16837. ;;;###autoload
  16838. (defun org-agenda-to-appt (&optional filter)
  16839. "Activate appointments found in `org-agenda-files'.
  16840. When prefixed, prompt for a regular expression and use it as a
  16841. filter: only add entries if they match this regular expression.
  16842. FILTER can be a string. In this case, use this string as a
  16843. regular expression to filter results.
  16844. FILTER can also be an alist, with the car of each cell being
  16845. either 'headline or 'category. For example:
  16846. '((headline \"IMPORTANT\")
  16847. (category \"Work\"))
  16848. will only add headlines containing IMPORTANT or headlines
  16849. belonging to the category \"Work\"."
  16850. (interactive "P")
  16851. (require 'calendar)
  16852. (if (equal filter '(4))
  16853. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16854. (let* ((cnt 0) ; count added events
  16855. (org-agenda-new-buffers nil)
  16856. (today (org-date-to-gregorian
  16857. (time-to-days (current-time))))
  16858. (files (org-agenda-files)) entries file)
  16859. ;; Get all entries which may contain an appt
  16860. (while (setq file (pop files))
  16861. (setq entries
  16862. (append entries
  16863. (org-agenda-get-day-entries
  16864. file today
  16865. :timestamp :scheduled :deadline))))
  16866. (setq entries (delq nil entries))
  16867. ;; Map thru entries and find if they pass thru the filter
  16868. (mapc
  16869. (lambda(x)
  16870. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  16871. (cat (get-text-property 1 'org-category x))
  16872. (tod (get-text-property 1 'time-of-day x))
  16873. (ok (or (null filter)
  16874. (and (stringp filter) (string-match filter evt))
  16875. (and (listp filter)
  16876. (or (string-match
  16877. (cadr (assoc 'category filter)) cat)
  16878. (string-match
  16879. (cadr (assoc 'headline filter)) evt))))))
  16880. ;; FIXME: Shall we remove text-properties for the appt text?
  16881. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  16882. (when (and ok tod)
  16883. (setq tod (number-to-string tod)
  16884. tod (when (string-match
  16885. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  16886. (concat (match-string 1 tod) ":"
  16887. (match-string 2 tod))))
  16888. (appt-add tod evt)
  16889. (setq cnt (1+ cnt))))) entries)
  16890. (org-release-buffers org-agenda-new-buffers)
  16891. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  16892. ;;; The clock for measuring work time.
  16893. (defvar org-mode-line-string "")
  16894. (put 'org-mode-line-string 'risky-local-variable t)
  16895. (defvar org-mode-line-timer nil)
  16896. (defvar org-clock-heading "")
  16897. (defvar org-clock-start-time "")
  16898. (defun org-update-mode-line ()
  16899. (let* ((delta (- (time-to-seconds (current-time))
  16900. (time-to-seconds org-clock-start-time)))
  16901. (h (floor delta 3600))
  16902. (m (floor (- delta (* 3600 h)) 60)))
  16903. (setq org-mode-line-string
  16904. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  16905. 'help-echo "Org-mode clock is running"))
  16906. (force-mode-line-update)))
  16907. (defvar org-clock-marker (make-marker)
  16908. "Marker recording the last clock-in.")
  16909. (defvar org-clock-mode-line-entry nil
  16910. "Information for the modeline about the running clock.")
  16911. (defun org-clock-in ()
  16912. "Start the clock on the current item.
  16913. If necessary, clock-out of the currently active clock."
  16914. (interactive)
  16915. (org-clock-out t)
  16916. (let (ts)
  16917. (save-excursion
  16918. (org-back-to-heading t)
  16919. (when (and org-clock-in-switch-to-state
  16920. (not (looking-at (concat outline-regexp "[ \t]*"
  16921. org-clock-in-switch-to-state
  16922. "\\>"))))
  16923. (org-todo org-clock-in-switch-to-state))
  16924. (if (and org-clock-heading-function
  16925. (functionp org-clock-heading-function))
  16926. (setq org-clock-heading (funcall org-clock-heading-function))
  16927. (if (looking-at org-complex-heading-regexp)
  16928. (setq org-clock-heading (match-string 4))
  16929. (setq org-clock-heading "???")))
  16930. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  16931. (org-clock-find-position)
  16932. (insert "\n") (backward-char 1)
  16933. (indent-relative)
  16934. (insert org-clock-string " ")
  16935. (setq org-clock-start-time (current-time))
  16936. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16937. (move-marker org-clock-marker (point) (buffer-base-buffer))
  16938. (or global-mode-string (setq global-mode-string '("")))
  16939. (or (memq 'org-mode-line-string global-mode-string)
  16940. (setq global-mode-string
  16941. (append global-mode-string '(org-mode-line-string))))
  16942. (org-update-mode-line)
  16943. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  16944. (message "Clock started at %s" ts))))
  16945. (defun org-clock-find-position ()
  16946. "Find the location where the next clock line should be inserted."
  16947. (org-back-to-heading t)
  16948. (catch 'exit
  16949. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  16950. (re (concat "^[ \t]*" org-clock-string))
  16951. (cnt 0)
  16952. first last)
  16953. (goto-char beg)
  16954. (when (eobp) (newline) (setq end (max (point) end)))
  16955. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  16956. ;; we seem to have a CLOCK drawer, so go there.
  16957. (beginning-of-line 2)
  16958. (throw 'exit t))
  16959. ;; Lets count the CLOCK lines
  16960. (goto-char beg)
  16961. (while (re-search-forward re end t)
  16962. (setq first (or first (match-beginning 0))
  16963. last (match-beginning 0)
  16964. cnt (1+ cnt)))
  16965. (when (and (integerp org-clock-into-drawer)
  16966. (>= (1+ cnt) org-clock-into-drawer))
  16967. ;; Wrap current entries into a new drawer
  16968. (goto-char last)
  16969. (beginning-of-line 2)
  16970. (if (org-at-item-p) (org-end-of-item))
  16971. (insert ":END:\n")
  16972. (beginning-of-line 0)
  16973. (org-indent-line-function)
  16974. (goto-char first)
  16975. (insert ":CLOCK:\n")
  16976. (beginning-of-line 0)
  16977. (org-indent-line-function)
  16978. (org-flag-drawer t)
  16979. (beginning-of-line 2)
  16980. (throw 'exit nil))
  16981. (goto-char beg)
  16982. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16983. (not (equal (match-string 1) org-clock-string)))
  16984. ;; Planning info, skip to after it
  16985. (beginning-of-line 2)
  16986. (or (bolp) (newline)))
  16987. (when (eq t org-clock-into-drawer)
  16988. (insert ":CLOCK:\n:END:\n")
  16989. (beginning-of-line -1)
  16990. (org-indent-line-function)
  16991. (org-flag-drawer t)
  16992. (beginning-of-line 2)
  16993. (org-indent-line-function)))))
  16994. (defun org-clock-out (&optional fail-quietly)
  16995. "Stop the currently running clock.
  16996. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  16997. (interactive)
  16998. (catch 'exit
  16999. (if (not (marker-buffer org-clock-marker))
  17000. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17001. (let (ts te s h m)
  17002. (save-excursion
  17003. (set-buffer (marker-buffer org-clock-marker))
  17004. (goto-char org-clock-marker)
  17005. (beginning-of-line 1)
  17006. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17007. (equal (match-string 1) org-clock-string))
  17008. (setq ts (match-string 2))
  17009. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17010. (goto-char (match-end 0))
  17011. (delete-region (point) (point-at-eol))
  17012. (insert "--")
  17013. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17014. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17015. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17016. h (floor (/ s 3600))
  17017. s (- s (* 3600 h))
  17018. m (floor (/ s 60))
  17019. s (- s (* 60 s)))
  17020. (insert " => " (format "%2d:%02d" h m))
  17021. (move-marker org-clock-marker nil)
  17022. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  17023. (org-log-done (org-parse-local-options logging 'org-log-done))
  17024. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  17025. (org-add-log-maybe 'clock-out))
  17026. (when org-mode-line-timer
  17027. (cancel-timer org-mode-line-timer)
  17028. (setq org-mode-line-timer nil))
  17029. (setq global-mode-string
  17030. (delq 'org-mode-line-string global-mode-string))
  17031. (force-mode-line-update)
  17032. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17033. (defun org-clock-cancel ()
  17034. "Cancel the running clock be removing the start timestamp."
  17035. (interactive)
  17036. (if (not (marker-buffer org-clock-marker))
  17037. (error "No active clock"))
  17038. (save-excursion
  17039. (set-buffer (marker-buffer org-clock-marker))
  17040. (goto-char org-clock-marker)
  17041. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17042. (setq global-mode-string
  17043. (delq 'org-mode-line-string global-mode-string))
  17044. (force-mode-line-update)
  17045. (message "Clock canceled"))
  17046. (defun org-clock-goto (&optional delete-windows)
  17047. "Go to the currently clocked-in entry."
  17048. (interactive "P")
  17049. (if (not (marker-buffer org-clock-marker))
  17050. (error "No active clock"))
  17051. (switch-to-buffer-other-window
  17052. (marker-buffer org-clock-marker))
  17053. (if delete-windows (delete-other-windows))
  17054. (goto-char org-clock-marker)
  17055. (org-show-entry)
  17056. (org-back-to-heading)
  17057. (recenter))
  17058. (defvar org-clock-file-total-minutes nil
  17059. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17060. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17061. (defun org-clock-sum (&optional tstart tend)
  17062. "Sum the times for each subtree.
  17063. Puts the resulting times in minutes as a text property on each headline."
  17064. (interactive)
  17065. (let* ((bmp (buffer-modified-p))
  17066. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17067. org-clock-string
  17068. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17069. (lmax 30)
  17070. (ltimes (make-vector lmax 0))
  17071. (t1 0)
  17072. (level 0)
  17073. ts te dt
  17074. time)
  17075. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17076. (save-excursion
  17077. (goto-char (point-max))
  17078. (while (re-search-backward re nil t)
  17079. (cond
  17080. ((match-end 2)
  17081. ;; Two time stamps
  17082. (setq ts (match-string 2)
  17083. te (match-string 3)
  17084. ts (time-to-seconds
  17085. (apply 'encode-time (org-parse-time-string ts)))
  17086. te (time-to-seconds
  17087. (apply 'encode-time (org-parse-time-string te)))
  17088. ts (if tstart (max ts tstart) ts)
  17089. te (if tend (min te tend) te)
  17090. dt (- te ts)
  17091. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17092. ((match-end 4)
  17093. ;; A naket time
  17094. (setq t1 (+ t1 (string-to-number (match-string 5))
  17095. (* 60 (string-to-number (match-string 4))))))
  17096. (t ;; A headline
  17097. (setq level (- (match-end 1) (match-beginning 1)))
  17098. (when (or (> t1 0) (> (aref ltimes level) 0))
  17099. (loop for l from 0 to level do
  17100. (aset ltimes l (+ (aref ltimes l) t1)))
  17101. (setq t1 0 time (aref ltimes level))
  17102. (loop for l from level to (1- lmax) do
  17103. (aset ltimes l 0))
  17104. (goto-char (match-beginning 0))
  17105. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17106. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17107. (set-buffer-modified-p bmp)))
  17108. (defun org-clock-display (&optional total-only)
  17109. "Show subtree times in the entire buffer.
  17110. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17111. in the echo area."
  17112. (interactive)
  17113. (org-remove-clock-overlays)
  17114. (let (time h m p)
  17115. (org-clock-sum)
  17116. (unless total-only
  17117. (save-excursion
  17118. (goto-char (point-min))
  17119. (while (or (and (equal (setq p (point)) (point-min))
  17120. (get-text-property p :org-clock-minutes))
  17121. (setq p (next-single-property-change
  17122. (point) :org-clock-minutes)))
  17123. (goto-char p)
  17124. (when (setq time (get-text-property p :org-clock-minutes))
  17125. (org-put-clock-overlay time (funcall outline-level))))
  17126. (setq h (/ org-clock-file-total-minutes 60)
  17127. m (- org-clock-file-total-minutes (* 60 h)))
  17128. ;; Arrange to remove the overlays upon next change.
  17129. (when org-remove-highlights-with-change
  17130. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17131. nil 'local))))
  17132. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17133. (defvar org-clock-overlays nil)
  17134. (make-variable-buffer-local 'org-clock-overlays)
  17135. (defun org-put-clock-overlay (time &optional level)
  17136. "Put an overlays on the current line, displaying TIME.
  17137. If LEVEL is given, prefix time with a corresponding number of stars.
  17138. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17139. will be easy to remove."
  17140. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17141. (l (if level (org-get-legal-level level 0) 0))
  17142. (off 0)
  17143. ov tx)
  17144. (move-to-column c)
  17145. (unless (eolp) (skip-chars-backward "^ \t"))
  17146. (skip-chars-backward " \t")
  17147. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17148. tx (concat (buffer-substring (1- (point)) (point))
  17149. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17150. (org-add-props (format "%s %2d:%02d%s"
  17151. (make-string l ?*) h m
  17152. (make-string (- 10 l) ?\ ))
  17153. '(face secondary-selection))
  17154. ""))
  17155. (if (not (featurep 'xemacs))
  17156. (org-overlay-put ov 'display tx)
  17157. (org-overlay-put ov 'invisible t)
  17158. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17159. (push ov org-clock-overlays)))
  17160. (defun org-remove-clock-overlays (&optional beg end noremove)
  17161. "Remove the occur highlights from the buffer.
  17162. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17163. from the `before-change-functions' in the current buffer."
  17164. (interactive)
  17165. (unless org-inhibit-highlight-removal
  17166. (mapc 'org-delete-overlay org-clock-overlays)
  17167. (setq org-clock-overlays nil)
  17168. (unless noremove
  17169. (remove-hook 'before-change-functions
  17170. 'org-remove-clock-overlays 'local))))
  17171. (defun org-clock-out-if-current ()
  17172. "Clock out if the current entry contains the running clock.
  17173. This is used to stop the clock after a TODO entry is marked DONE,
  17174. and is only done if the variable `org-clock-out-when-done' is not nil."
  17175. (when (and org-clock-out-when-done
  17176. (member state org-done-keywords)
  17177. (equal (marker-buffer org-clock-marker) (current-buffer))
  17178. (< (point) org-clock-marker)
  17179. (> (save-excursion (outline-next-heading) (point))
  17180. org-clock-marker))
  17181. ;; Clock out, but don't accept a logging message for this.
  17182. (let ((org-log-done (if (and (listp org-log-done)
  17183. (member 'clock-out org-log-done))
  17184. '(done)
  17185. org-log-done)))
  17186. (org-clock-out))))
  17187. (add-hook 'org-after-todo-state-change-hook
  17188. 'org-clock-out-if-current)
  17189. (defun org-check-running-clock ()
  17190. "Check if the current buffer contains the running clock.
  17191. If yes, offer to stop it and to save the buffer with the changes."
  17192. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17193. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17194. (buffer-name))))
  17195. (org-clock-out)
  17196. (when (y-or-n-p "Save changed buffer?")
  17197. (save-buffer))))
  17198. (defun org-clock-report (&optional arg)
  17199. "Create a table containing a report about clocked time.
  17200. If the cursor is inside an existing clocktable block, then the table
  17201. will be updated. If not, a new clocktable will be inserted.
  17202. When called with a prefix argument, move to the first clock table in the
  17203. buffer and update it."
  17204. (interactive "P")
  17205. (org-remove-clock-overlays)
  17206. (when arg (org-find-dblock "clocktable"))
  17207. (if (org-in-clocktable-p)
  17208. (goto-char (org-in-clocktable-p))
  17209. (org-create-dblock (list :name "clocktable"
  17210. :maxlevel 2 :scope 'file)))
  17211. (org-update-dblock))
  17212. (defun org-in-clocktable-p ()
  17213. "Check if the cursor is in a clocktable."
  17214. (let ((pos (point)) start)
  17215. (save-excursion
  17216. (end-of-line 1)
  17217. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17218. (setq start (match-beginning 0))
  17219. (re-search-forward "^#\\+END:.*" nil t)
  17220. (>= (match-end 0) pos)
  17221. start))))
  17222. (defun org-clock-update-time-maybe ()
  17223. "If this is a CLOCK line, update it and return t.
  17224. Otherwise, return nil."
  17225. (interactive)
  17226. (save-excursion
  17227. (beginning-of-line 1)
  17228. (skip-chars-forward " \t")
  17229. (when (looking-at org-clock-string)
  17230. (let ((re (concat "[ \t]*" org-clock-string
  17231. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17232. "\\([ \t]*=>.*\\)?"))
  17233. ts te h m s)
  17234. (if (not (looking-at re))
  17235. nil
  17236. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17237. (end-of-line 1)
  17238. (setq ts (match-string 1)
  17239. te (match-string 2))
  17240. (setq s (- (time-to-seconds
  17241. (apply 'encode-time (org-parse-time-string te)))
  17242. (time-to-seconds
  17243. (apply 'encode-time (org-parse-time-string ts))))
  17244. h (floor (/ s 3600))
  17245. s (- s (* 3600 h))
  17246. m (floor (/ s 60))
  17247. s (- s (* 60 s)))
  17248. (insert " => " (format "%2d:%02d" h m))
  17249. t)))))
  17250. (defun org-clock-special-range (key &optional time as-strings)
  17251. "Return two times bordering a special time range.
  17252. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17253. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17254. A week starts Monday 0:00 and ends Sunday 24:00.
  17255. The range is determined relative to TIME. TIME defaults to the current time.
  17256. The return value is a cons cell with two internal times like the ones
  17257. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17258. the returned times will be formatted strings."
  17259. (let* ((tm (decode-time (or time (current-time))))
  17260. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17261. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17262. (dow (nth 6 tm))
  17263. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17264. (cond
  17265. ((eq key 'today)
  17266. (setq h 0 m 0 h1 24 m1 0))
  17267. ((eq key 'yesterday)
  17268. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17269. ((eq key 'thisweek)
  17270. (setq diff (if (= dow 0) 6 (1- dow))
  17271. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17272. ((eq key 'lastweek)
  17273. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17274. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17275. ((eq key 'thismonth)
  17276. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17277. ((eq key 'lastmonth)
  17278. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17279. ((eq key 'thisyear)
  17280. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17281. ((eq key 'lastyear)
  17282. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17283. (t (error "No such time block %s" key)))
  17284. (setq ts (encode-time s m h d month y)
  17285. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17286. (or d1 d) (or month1 month) (or y1 y)))
  17287. (setq fm (cdr org-time-stamp-formats))
  17288. (if as-strings
  17289. (cons (format-time-string fm ts) (format-time-string fm te))
  17290. (cons ts te))))
  17291. (defun org-dblock-write:clocktable (params)
  17292. "Write the standard clocktable."
  17293. (catch 'exit
  17294. (let* ((hlchars '((1 . "*") (2 . "/")))
  17295. (ins (make-marker))
  17296. (total-time nil)
  17297. (scope (plist-get params :scope))
  17298. (tostring (plist-get params :tostring))
  17299. (multifile (plist-get params :multifile))
  17300. (header (plist-get params :header))
  17301. (maxlevel (or (plist-get params :maxlevel) 3))
  17302. (step (plist-get params :step))
  17303. (emph (plist-get params :emphasize))
  17304. (ts (plist-get params :tstart))
  17305. (te (plist-get params :tend))
  17306. (block (plist-get params :block))
  17307. ipos time h m p level hlc hdl
  17308. cc beg end pos tbl)
  17309. (when step
  17310. (org-clocktable-steps params)
  17311. (throw 'exit nil))
  17312. (when block
  17313. (setq cc (org-clock-special-range block nil t)
  17314. ts (car cc) te (cdr cc)))
  17315. (if ts (setq ts (time-to-seconds
  17316. (apply 'encode-time (org-parse-time-string ts)))))
  17317. (if te (setq te (time-to-seconds
  17318. (apply 'encode-time (org-parse-time-string te)))))
  17319. (move-marker ins (point))
  17320. (setq ipos (point))
  17321. ;; Get the right scope
  17322. (setq pos (point))
  17323. (save-restriction
  17324. (cond
  17325. ((not scope))
  17326. ((eq scope 'file) (widen))
  17327. ((eq scope 'subtree) (org-narrow-to-subtree))
  17328. ((eq scope 'tree)
  17329. (while (org-up-heading-safe))
  17330. (org-narrow-to-subtree))
  17331. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17332. (symbol-name scope)))
  17333. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17334. (catch 'exit
  17335. (while (org-up-heading-safe)
  17336. (looking-at outline-regexp)
  17337. (if (<= (org-reduced-level (funcall outline-level)) level)
  17338. (throw 'exit nil))))
  17339. (org-narrow-to-subtree))
  17340. ((or (listp scope) (eq scope 'agenda))
  17341. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17342. (scope 'agenda)
  17343. (p1 (copy-sequence params))
  17344. file)
  17345. (plist-put p1 :tostring t)
  17346. (plist-put p1 :multifile t)
  17347. (plist-put p1 :scope 'file)
  17348. (org-prepare-agenda-buffers files)
  17349. (while (setq file (pop files))
  17350. (with-current-buffer (find-buffer-visiting file)
  17351. (push (org-clocktable-add-file
  17352. file (org-dblock-write:clocktable p1)) tbl)
  17353. (setq total-time (+ (or total-time 0)
  17354. org-clock-file-total-minutes)))))))
  17355. (goto-char pos)
  17356. (unless (eq scope 'agenda)
  17357. (org-clock-sum ts te)
  17358. (goto-char (point-min))
  17359. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17360. (goto-char p)
  17361. (when (setq time (get-text-property p :org-clock-minutes))
  17362. (save-excursion
  17363. (beginning-of-line 1)
  17364. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17365. (setq level (org-reduced-level
  17366. (- (match-end 1) (match-beginning 1))))
  17367. (<= level maxlevel))
  17368. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17369. hdl (match-string 2)
  17370. h (/ time 60)
  17371. m (- time (* 60 h)))
  17372. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17373. (push (concat
  17374. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17375. (make-string (1- level) ?|)
  17376. hlc (format "%d:%02d" h m) hlc
  17377. " |") tbl))))))
  17378. (setq tbl (nreverse tbl))
  17379. (if tostring
  17380. (if tbl (mapconcat 'identity tbl "\n") nil)
  17381. (goto-char ins)
  17382. (insert-before-markers
  17383. (or header
  17384. (concat
  17385. "Clock summary at ["
  17386. (substring
  17387. (format-time-string (cdr org-time-stamp-formats))
  17388. 1 -1)
  17389. "]."
  17390. (if block
  17391. (format " Considered range is /%s/." block)
  17392. "")
  17393. "\n\n"))
  17394. (if (eq scope 'agenda) "|File" "")
  17395. "|L|Headline|Time|\n")
  17396. (setq total-time (or total-time org-clock-file-total-minutes)
  17397. h (/ total-time 60)
  17398. m (- total-time (* 60 h)))
  17399. (insert-before-markers
  17400. "|-\n|"
  17401. (if (eq scope 'agenda) "|" "")
  17402. "|"
  17403. "*Total time*| "
  17404. (format "*%d:%02d*" h m)
  17405. "|\n|-\n")
  17406. (setq tbl (delq nil tbl))
  17407. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17408. (equal (substring (car tbl) 0 2) "|-"))
  17409. (pop tbl))
  17410. (insert-before-markers (mapconcat
  17411. 'identity (delq nil tbl)
  17412. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17413. (backward-delete-char 1)
  17414. (goto-char ipos)
  17415. (skip-chars-forward "^|")
  17416. (org-table-align))))))
  17417. (defun org-clocktable-steps (params)
  17418. (let* ((p1 (copy-sequence params))
  17419. (ts (plist-get p1 :tstart))
  17420. (te (plist-get p1 :tend))
  17421. (step0 (plist-get p1 :step))
  17422. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17423. (block (plist-get p1 :block))
  17424. cc)
  17425. (when block
  17426. (setq cc (org-clock-special-range block nil t)
  17427. ts (car cc) te (cdr cc)))
  17428. (if ts (setq ts (time-to-seconds
  17429. (apply 'encode-time (org-parse-time-string ts)))))
  17430. (if te (setq te (time-to-seconds
  17431. (apply 'encode-time (org-parse-time-string te)))))
  17432. (plist-put p1 :header "")
  17433. (plist-put p1 :step nil)
  17434. (plist-put p1 :block nil)
  17435. (while (< ts te)
  17436. (or (bolp) (insert "\n"))
  17437. (plist-put p1 :tstart (format-time-string
  17438. (car org-time-stamp-formats)
  17439. (seconds-to-time ts)))
  17440. (plist-put p1 :tend (format-time-string
  17441. (car org-time-stamp-formats)
  17442. (seconds-to-time (setq ts (+ ts step)))))
  17443. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17444. (plist-get p1 :tstart) "\n")
  17445. (org-dblock-write:clocktable p1)
  17446. (re-search-forward "#\\+END:")
  17447. (end-of-line 0))))
  17448. (defun org-clocktable-add-file (file table)
  17449. (if table
  17450. (let ((lines (org-split-string table "\n"))
  17451. (ff (file-name-nondirectory file)))
  17452. (mapconcat 'identity
  17453. (mapcar (lambda (x)
  17454. (if (string-match org-table-dataline-regexp x)
  17455. (concat "|" ff x)
  17456. x))
  17457. lines)
  17458. "\n"))))
  17459. ;; FIXME: I don't think anybody uses this, ask David
  17460. (defun org-collect-clock-time-entries ()
  17461. "Return an internal list with clocking information.
  17462. This list has one entry for each CLOCK interval.
  17463. FIXME: describe the elements."
  17464. (interactive)
  17465. (let ((re (concat "^[ \t]*" org-clock-string
  17466. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17467. rtn beg end next cont level title total closedp leafp
  17468. clockpos titlepos h m donep)
  17469. (save-excursion
  17470. (org-clock-sum)
  17471. (goto-char (point-min))
  17472. (while (re-search-forward re nil t)
  17473. (setq clockpos (match-beginning 0)
  17474. beg (match-string 1) end (match-string 2)
  17475. cont (match-end 0))
  17476. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17477. end (apply 'encode-time (org-parse-time-string end)))
  17478. (org-back-to-heading t)
  17479. (setq donep (org-entry-is-done-p))
  17480. (setq titlepos (point)
  17481. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17482. h (/ total 60) m (- total (* 60 h))
  17483. total (cons h m))
  17484. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17485. (setq level (- (match-end 1) (match-beginning 1))
  17486. title (org-match-string-no-properties 2))
  17487. (save-excursion (outline-next-heading) (setq next (point)))
  17488. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17489. (goto-char next)
  17490. (setq leafp (and (looking-at "^\\*+ ")
  17491. (<= (- (match-end 0) (point)) level)))
  17492. (push (list beg end clockpos closedp donep
  17493. total title titlepos level leafp)
  17494. rtn)
  17495. (goto-char cont)))
  17496. (nreverse rtn)))
  17497. ;;;; Agenda, and Diary Integration
  17498. ;;; Define the Org-agenda-mode
  17499. (defvar org-agenda-mode-map (make-sparse-keymap)
  17500. "Keymap for `org-agenda-mode'.")
  17501. (defvar org-agenda-menu) ; defined later in this file.
  17502. (defvar org-agenda-follow-mode nil)
  17503. (defvar org-agenda-show-log nil)
  17504. (defvar org-agenda-redo-command nil)
  17505. (defvar org-agenda-mode-hook nil)
  17506. (defvar org-agenda-type nil)
  17507. (defvar org-agenda-force-single-file nil)
  17508. (defun org-agenda-mode ()
  17509. "Mode for time-sorted view on action items in Org-mode files.
  17510. The following commands are available:
  17511. \\{org-agenda-mode-map}"
  17512. (interactive)
  17513. (kill-all-local-variables)
  17514. (setq org-agenda-undo-list nil
  17515. org-agenda-pending-undo-list nil)
  17516. (setq major-mode 'org-agenda-mode)
  17517. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17518. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17519. (setq mode-name "Org-Agenda")
  17520. (use-local-map org-agenda-mode-map)
  17521. (easy-menu-add org-agenda-menu)
  17522. (if org-startup-truncated (setq truncate-lines t))
  17523. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17524. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17525. ;; Make sure properties are removed when copying text
  17526. (when (boundp 'buffer-substring-filters)
  17527. (org-set-local 'buffer-substring-filters
  17528. (cons (lambda (x)
  17529. (set-text-properties 0 (length x) nil x) x)
  17530. buffer-substring-filters)))
  17531. (unless org-agenda-keep-modes
  17532. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17533. org-agenda-show-log nil))
  17534. (easy-menu-change
  17535. '("Agenda") "Agenda Files"
  17536. (append
  17537. (list
  17538. (vector
  17539. (if (get 'org-agenda-files 'org-restrict)
  17540. "Restricted to single file"
  17541. "Edit File List")
  17542. '(org-edit-agenda-file-list)
  17543. (not (get 'org-agenda-files 'org-restrict)))
  17544. "--")
  17545. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17546. (org-agenda-set-mode-name)
  17547. (apply
  17548. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17549. (list 'org-agenda-mode-hook)))
  17550. (substitute-key-definition 'undo 'org-agenda-undo
  17551. org-agenda-mode-map global-map)
  17552. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17553. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17554. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17555. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17556. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17557. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17558. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17559. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17560. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17561. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17562. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17563. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17564. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17565. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17566. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17567. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17568. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17569. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17570. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17571. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17572. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17573. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17574. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17575. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17576. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17577. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17578. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17579. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17580. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17581. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17582. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17583. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17584. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17585. (while l (org-defkey org-agenda-mode-map
  17586. (int-to-string (pop l)) 'digit-argument)))
  17587. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17588. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17589. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17590. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17591. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17592. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17593. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17594. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17595. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17596. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17597. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17598. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17599. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17600. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17601. (org-defkey org-agenda-mode-map "n" 'next-line)
  17602. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17603. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17604. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17605. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17606. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17607. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17608. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17609. (eval-after-load "calendar"
  17610. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17611. 'org-calendar-goto-agenda))
  17612. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17613. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17614. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17615. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17616. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17617. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17618. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17619. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17620. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17621. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17622. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17623. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17624. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17625. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17626. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17627. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17628. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17629. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17630. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17631. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17632. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17633. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17634. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17635. "Local keymap for agenda entries from Org-mode.")
  17636. (org-defkey org-agenda-keymap
  17637. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17638. (org-defkey org-agenda-keymap
  17639. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17640. (when org-agenda-mouse-1-follows-link
  17641. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17642. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17643. '("Agenda"
  17644. ("Agenda Files")
  17645. "--"
  17646. ["Show" org-agenda-show t]
  17647. ["Go To (other window)" org-agenda-goto t]
  17648. ["Go To (this window)" org-agenda-switch-to t]
  17649. ["Follow Mode" org-agenda-follow-mode
  17650. :style toggle :selected org-agenda-follow-mode :active t]
  17651. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17652. "--"
  17653. ["Cycle TODO" org-agenda-todo t]
  17654. ["Archive subtree" org-agenda-archive t]
  17655. ["Delete subtree" org-agenda-kill t]
  17656. "--"
  17657. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17658. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17659. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17660. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17661. "--"
  17662. ("Tags and Properties"
  17663. ["Show all Tags" org-agenda-show-tags t]
  17664. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17665. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17666. "--"
  17667. ["Column View" org-columns t])
  17668. ("Date/Schedule"
  17669. ["Schedule" org-agenda-schedule t]
  17670. ["Set Deadline" org-agenda-deadline t]
  17671. "--"
  17672. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17673. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17674. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17675. ("Clock"
  17676. ["Clock in" org-agenda-clock-in t]
  17677. ["Clock out" org-agenda-clock-out t]
  17678. ["Clock cancel" org-agenda-clock-cancel t]
  17679. ["Goto running clock" org-clock-goto t])
  17680. ("Priority"
  17681. ["Set Priority" org-agenda-priority t]
  17682. ["Increase Priority" org-agenda-priority-up t]
  17683. ["Decrease Priority" org-agenda-priority-down t]
  17684. ["Show Priority" org-agenda-show-priority t])
  17685. ("Calendar/Diary"
  17686. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17687. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17688. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17689. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17690. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17691. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17692. "--"
  17693. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17694. "--"
  17695. ("View"
  17696. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17697. :style radio :selected (equal org-agenda-ndays 1)]
  17698. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17699. :style radio :selected (equal org-agenda-ndays 7)]
  17700. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17701. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17702. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17703. :style radio :selected (member org-agenda-ndays '(365 366))]
  17704. "--"
  17705. ["Show Logbook entries" org-agenda-log-mode
  17706. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17707. ["Include Diary" org-agenda-toggle-diary
  17708. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17709. ["Use Time Grid" org-agenda-toggle-time-grid
  17710. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17711. ["Write view to file" org-write-agenda t]
  17712. ["Rebuild buffer" org-agenda-redo t]
  17713. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17714. "--"
  17715. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17716. "--"
  17717. ["Quit" org-agenda-quit t]
  17718. ["Exit and Release Buffers" org-agenda-exit t]
  17719. ))
  17720. ;;; Agenda undo
  17721. (defvar org-agenda-allow-remote-undo t
  17722. "Non-nil means, allow remote undo from the agenda buffer.")
  17723. (defvar org-agenda-undo-list nil
  17724. "List of undoable operations in the agenda since last refresh.")
  17725. (defvar org-agenda-undo-has-started-in nil
  17726. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17727. (defvar org-agenda-pending-undo-list nil
  17728. "In a series of undo commands, this is the list of remaning undo items.")
  17729. (defmacro org-if-unprotected (&rest body)
  17730. "Execute BODY if there is no `org-protected' text property at point."
  17731. (declare (debug t))
  17732. `(unless (get-text-property (point) 'org-protected)
  17733. ,@body))
  17734. (defmacro org-with-remote-undo (_buffer &rest _body)
  17735. "Execute BODY while recording undo information in two buffers."
  17736. (declare (indent 1) (debug t))
  17737. `(let ((_cline (org-current-line))
  17738. (_cmd this-command)
  17739. (_buf1 (current-buffer))
  17740. (_buf2 ,_buffer)
  17741. (_undo1 buffer-undo-list)
  17742. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17743. _c1 _c2)
  17744. ,@_body
  17745. (when org-agenda-allow-remote-undo
  17746. (setq _c1 (org-verify-change-for-undo
  17747. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17748. _c2 (org-verify-change-for-undo
  17749. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17750. (when (or _c1 _c2)
  17751. ;; make sure there are undo boundaries
  17752. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17753. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17754. ;; remember which buffer to undo
  17755. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17756. org-agenda-undo-list)))))
  17757. (defun org-agenda-undo ()
  17758. "Undo a remote editing step in the agenda.
  17759. This undoes changes both in the agenda buffer and in the remote buffer
  17760. that have been changed along."
  17761. (interactive)
  17762. (or org-agenda-allow-remote-undo
  17763. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17764. (if (not (eq this-command last-command))
  17765. (setq org-agenda-undo-has-started-in nil
  17766. org-agenda-pending-undo-list org-agenda-undo-list))
  17767. (if (not org-agenda-pending-undo-list)
  17768. (error "No further undo information"))
  17769. (let* ((entry (pop org-agenda-pending-undo-list))
  17770. buf line cmd rembuf)
  17771. (setq cmd (pop entry) line (pop entry))
  17772. (setq rembuf (nth 2 entry))
  17773. (org-with-remote-undo rembuf
  17774. (while (bufferp (setq buf (pop entry)))
  17775. (if (pop entry)
  17776. (with-current-buffer buf
  17777. (let ((last-undo-buffer buf)
  17778. (inhibit-read-only t))
  17779. (unless (memq buf org-agenda-undo-has-started-in)
  17780. (push buf org-agenda-undo-has-started-in)
  17781. (make-local-variable 'pending-undo-list)
  17782. (undo-start))
  17783. (while (and pending-undo-list
  17784. (listp pending-undo-list)
  17785. (not (car pending-undo-list)))
  17786. (pop pending-undo-list))
  17787. (undo-more 1))))))
  17788. (goto-line line)
  17789. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17790. (defun org-verify-change-for-undo (l1 l2)
  17791. "Verify that a real change occurred between the undo lists L1 and L2."
  17792. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17793. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17794. (not (eq l1 l2)))
  17795. ;;; Agenda dispatch
  17796. (defvar org-agenda-restrict nil)
  17797. (defvar org-agenda-restrict-begin (make-marker))
  17798. (defvar org-agenda-restrict-end (make-marker))
  17799. (defvar org-agenda-last-dispatch-buffer nil)
  17800. (defvar org-agenda-overriding-restriction nil)
  17801. ;;;###autoload
  17802. (defun org-agenda (arg &optional keys restriction)
  17803. "Dispatch agenda commands to collect entries to the agenda buffer.
  17804. Prompts for a command to execute. Any prefix arg will be passed
  17805. on to the selected command. The default selections are:
  17806. a Call `org-agenda-list' to display the agenda for current day or week.
  17807. t Call `org-todo-list' to display the global todo list.
  17808. T Call `org-todo-list' to display the global todo list, select only
  17809. entries with a specific TODO keyword (the user gets a prompt).
  17810. m Call `org-tags-view' to display headlines with tags matching
  17811. a condition (the user is prompted for the condition).
  17812. M Like `m', but select only TODO entries, no ordinary headlines.
  17813. L Create a timeline for the current buffer.
  17814. e Export views to associated files.
  17815. More commands can be added by configuring the variable
  17816. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17817. searches can be pre-defined in this way.
  17818. If the current buffer is in Org-mode and visiting a file, you can also
  17819. first press `<' once to indicate that the agenda should be temporarily
  17820. \(until the next use of \\[org-agenda]) restricted to the current file.
  17821. Pressing `<' twice means to restrict to the current subtree or region
  17822. \(if active)."
  17823. (interactive "P")
  17824. (catch 'exit
  17825. (let* ((prefix-descriptions nil)
  17826. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17827. (org-agenda-custom-commands
  17828. ;; normalize different versions
  17829. (delq nil
  17830. (mapcar
  17831. (lambda (x)
  17832. (cond ((stringp (cdr x))
  17833. (push x prefix-descriptions)
  17834. nil)
  17835. ((stringp (nth 1 x)) x)
  17836. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17837. (t (cons (car x) (cons "" (cdr x))))))
  17838. org-agenda-custom-commands)))
  17839. (buf (current-buffer))
  17840. (bfn (buffer-file-name (buffer-base-buffer)))
  17841. entry key type match lprops ans)
  17842. ;; Turn off restriction unless there is an overriding one
  17843. (unless org-agenda-overriding-restriction
  17844. (put 'org-agenda-files 'org-restrict nil)
  17845. (setq org-agenda-restrict nil)
  17846. (move-marker org-agenda-restrict-begin nil)
  17847. (move-marker org-agenda-restrict-end nil))
  17848. ;; Delete old local properties
  17849. (put 'org-agenda-redo-command 'org-lprops nil)
  17850. ;; Remember where this call originated
  17851. (setq org-agenda-last-dispatch-buffer (current-buffer))
  17852. (unless keys
  17853. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  17854. keys (car ans)
  17855. restriction (cdr ans)))
  17856. ;; Estabish the restriction, if any
  17857. (when (and (not org-agenda-overriding-restriction) restriction)
  17858. (put 'org-agenda-files 'org-restrict (list bfn))
  17859. (cond
  17860. ((eq restriction 'region)
  17861. (setq org-agenda-restrict t)
  17862. (move-marker org-agenda-restrict-begin (region-beginning))
  17863. (move-marker org-agenda-restrict-end (region-end)))
  17864. ((eq restriction 'subtree)
  17865. (save-excursion
  17866. (setq org-agenda-restrict t)
  17867. (org-back-to-heading t)
  17868. (move-marker org-agenda-restrict-begin (point))
  17869. (move-marker org-agenda-restrict-end
  17870. (progn (org-end-of-subtree t)))))))
  17871. (require 'calendar) ; FIXME: can we avoid this for some commands?
  17872. ;; For example the todo list should not need it (but does...)
  17873. (cond
  17874. ((setq entry (assoc keys org-agenda-custom-commands))
  17875. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  17876. (progn
  17877. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  17878. (put 'org-agenda-redo-command 'org-lprops lprops)
  17879. (cond
  17880. ((eq type 'agenda)
  17881. (org-let lprops '(org-agenda-list current-prefix-arg)))
  17882. ((eq type 'alltodo)
  17883. (org-let lprops '(org-todo-list current-prefix-arg)))
  17884. ((eq type 'stuck)
  17885. (org-let lprops '(org-agenda-list-stuck-projects
  17886. current-prefix-arg)))
  17887. ((eq type 'tags)
  17888. (org-let lprops '(org-tags-view current-prefix-arg match)))
  17889. ((eq type 'tags-todo)
  17890. (org-let lprops '(org-tags-view '(4) match)))
  17891. ((eq type 'todo)
  17892. (org-let lprops '(org-todo-list match)))
  17893. ((eq type 'tags-tree)
  17894. (org-check-for-org-mode)
  17895. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  17896. ((eq type 'todo-tree)
  17897. (org-check-for-org-mode)
  17898. (org-let lprops
  17899. '(org-occur (concat "^" outline-regexp "[ \t]*"
  17900. (regexp-quote match) "\\>"))))
  17901. ((eq type 'occur-tree)
  17902. (org-check-for-org-mode)
  17903. (org-let lprops '(org-occur match)))
  17904. ((functionp type)
  17905. (org-let lprops '(funcall type match)))
  17906. ((fboundp type)
  17907. (org-let lprops '(funcall type match)))
  17908. (t (error "Invalid custom agenda command type %s" type))))
  17909. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  17910. ((equal keys "C")
  17911. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  17912. (customize-variable 'org-agenda-custom-commands))
  17913. ((equal keys "a") (call-interactively 'org-agenda-list))
  17914. ((equal keys "t") (call-interactively 'org-todo-list))
  17915. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  17916. ((equal keys "m") (call-interactively 'org-tags-view))
  17917. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  17918. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  17919. ((equal keys "L")
  17920. (unless (org-mode-p)
  17921. (error "This is not an Org-mode file"))
  17922. (unless restriction
  17923. (put 'org-agenda-files 'org-restrict (list bfn))
  17924. (org-call-with-arg 'org-timeline arg)))
  17925. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  17926. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  17927. ((equal keys "!") (customize-variable 'org-stuck-projects))
  17928. (t (error "Invalid agenda key"))))))
  17929. (defun org-agenda-normalize-custom-commands (cmds)
  17930. (delq nil
  17931. (mapcar
  17932. (lambda (x)
  17933. (cond ((stringp (cdr x)) nil)
  17934. ((stringp (nth 1 x)) x)
  17935. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17936. (t (cons (car x) (cons "" (cdr x))))))
  17937. cmds)))
  17938. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  17939. "The user interface for selecting an agenda command."
  17940. (catch 'exit
  17941. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  17942. (restrict-ok (and bfn (org-mode-p)))
  17943. (region-p (org-region-active-p))
  17944. (custom org-agenda-custom-commands)
  17945. (selstring "")
  17946. restriction second-time
  17947. c entry key type match prefixes rmheader header-end custom1 desc)
  17948. (save-window-excursion
  17949. (delete-other-windows)
  17950. (org-switch-to-buffer-other-window " *Agenda Commands*")
  17951. (erase-buffer)
  17952. (insert (eval-when-compile
  17953. (let ((header
  17954. "
  17955. Press key for an agenda command: < Buffer,subtree/region restriction
  17956. -------------------------------- > Remove restriction
  17957. a Agenda for current week or day e Export agenda views
  17958. t List of all TODO entries T Entries with special TODO kwd
  17959. m Match a TAGS query M Like m, but only TODO entries
  17960. L Timeline for current buffer # List stuck projects (!=configure)
  17961. / Multi-occur C Configure custom agenda commands
  17962. ")
  17963. (start 0))
  17964. (while (string-match
  17965. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  17966. header start)
  17967. (setq start (match-end 0))
  17968. (add-text-properties (match-beginning 2) (match-end 2)
  17969. '(face bold) header))
  17970. header)))
  17971. (setq header-end (move-marker (make-marker) (point)))
  17972. (while t
  17973. (setq custom1 custom)
  17974. (when (eq rmheader t)
  17975. (goto-line 1)
  17976. (re-search-forward ":" nil t)
  17977. (delete-region (match-end 0) (point-at-eol))
  17978. (forward-char 1)
  17979. (looking-at "-+")
  17980. (delete-region (match-end 0) (point-at-eol))
  17981. (move-marker header-end (match-end 0)))
  17982. (goto-char header-end)
  17983. (delete-region (point) (point-max))
  17984. (while (setq entry (pop custom1))
  17985. (setq key (car entry) desc (nth 1 entry)
  17986. type (nth 2 entry) match (nth 3 entry))
  17987. (if (> (length key) 1)
  17988. (add-to-list 'prefixes (string-to-char key))
  17989. (insert
  17990. (format
  17991. "\n%-4s%-14s: %s"
  17992. (org-add-props (copy-sequence key)
  17993. '(face bold))
  17994. (cond
  17995. ((string-match "\\S-" desc) desc)
  17996. ((eq type 'agenda) "Agenda for current week or day")
  17997. ((eq type 'alltodo) "List of all TODO entries")
  17998. ((eq type 'stuck) "List of stuck projects")
  17999. ((eq type 'todo) "TODO keyword")
  18000. ((eq type 'tags) "Tags query")
  18001. ((eq type 'tags-todo) "Tags (TODO)")
  18002. ((eq type 'tags-tree) "Tags tree")
  18003. ((eq type 'todo-tree) "TODO kwd tree")
  18004. ((eq type 'occur-tree) "Occur tree")
  18005. ((functionp type) (if (symbolp type)
  18006. (symbol-name type)
  18007. "Lambda expression"))
  18008. (t "???"))
  18009. (cond
  18010. ((stringp match)
  18011. (org-add-props match nil 'face 'org-warning))
  18012. (match
  18013. (format "set of %d commands" (length match)))
  18014. (t ""))))))
  18015. (when prefixes
  18016. (mapc (lambda (x)
  18017. (insert
  18018. (format "\n%s %s"
  18019. (org-add-props (char-to-string x)
  18020. nil 'face 'bold)
  18021. (or (cdr (assoc (concat selstring (char-to-string x))
  18022. prefix-descriptions))
  18023. "Prefix key"))))
  18024. prefixes))
  18025. (goto-char (point-min))
  18026. (when (fboundp 'fit-window-to-buffer)
  18027. (if second-time
  18028. (if (not (pos-visible-in-window-p (point-max)))
  18029. (fit-window-to-buffer))
  18030. (setq second-time t)
  18031. (fit-window-to-buffer)))
  18032. (message "Press key for agenda command%s:"
  18033. (if (or restrict-ok org-agenda-overriding-restriction)
  18034. (if org-agenda-overriding-restriction
  18035. " (restriction lock active)"
  18036. (if restriction
  18037. (format " (restricted to %s)" restriction)
  18038. " (unrestricted)"))
  18039. ""))
  18040. (setq c (read-char-exclusive))
  18041. (message "")
  18042. (cond
  18043. ((assoc (char-to-string c) custom)
  18044. (setq selstring (concat selstring (char-to-string c)))
  18045. (throw 'exit (cons selstring restriction)))
  18046. ((memq c prefixes)
  18047. (setq selstring (concat selstring (char-to-string c))
  18048. prefixes nil
  18049. rmheader (or rmheader t)
  18050. custom (delq nil (mapcar
  18051. (lambda (x)
  18052. (if (or (= (length (car x)) 1)
  18053. (/= (string-to-char (car x)) c))
  18054. nil
  18055. (cons (substring (car x) 1) (cdr x))))
  18056. custom))))
  18057. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18058. (message "Restriction is only possible in Org-mode buffers")
  18059. (ding) (sit-for 1))
  18060. ((eq c ?1)
  18061. (org-agenda-remove-restriction-lock 'noupdate)
  18062. (setq restriction 'buffer))
  18063. ((eq c ?0)
  18064. (org-agenda-remove-restriction-lock 'noupdate)
  18065. (setq restriction (if region-p 'region 'subtree)))
  18066. ((eq c ?<)
  18067. (org-agenda-remove-restriction-lock 'noupdate)
  18068. (setq restriction
  18069. (cond
  18070. ((eq restriction 'buffer)
  18071. (if region-p 'region 'subtree))
  18072. ((memq restriction '(subtree region))
  18073. nil)
  18074. (t 'buffer))))
  18075. ((eq c ?>)
  18076. (org-agenda-remove-restriction-lock 'noupdate)
  18077. (setq restriction nil))
  18078. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18079. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18080. ((and (> (length selstring) 0) (eq c ?\d))
  18081. (delete-window)
  18082. (org-agenda-get-restriction-and-command prefix-descriptions))
  18083. ((equal c ?q) (error "Abort"))
  18084. (t (error "Invalid key %c" c))))))))
  18085. (defun org-run-agenda-series (name series)
  18086. (org-prepare-agenda name)
  18087. (let* ((org-agenda-multi t)
  18088. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18089. (cmds (car series))
  18090. (gprops (nth 1 series))
  18091. match ;; The byte compiler incorrectly complains about this. Keep it!
  18092. cmd type lprops)
  18093. (while (setq cmd (pop cmds))
  18094. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18095. (cond
  18096. ((eq type 'agenda)
  18097. (org-let2 gprops lprops
  18098. '(call-interactively 'org-agenda-list)))
  18099. ((eq type 'alltodo)
  18100. (org-let2 gprops lprops
  18101. '(call-interactively 'org-todo-list)))
  18102. ((eq type 'stuck)
  18103. (org-let2 gprops lprops
  18104. '(call-interactively 'org-agenda-list-stuck-projects)))
  18105. ((eq type 'tags)
  18106. (org-let2 gprops lprops
  18107. '(org-tags-view current-prefix-arg match)))
  18108. ((eq type 'tags-todo)
  18109. (org-let2 gprops lprops
  18110. '(org-tags-view '(4) match)))
  18111. ((eq type 'todo)
  18112. (org-let2 gprops lprops
  18113. '(org-todo-list match)))
  18114. ((fboundp type)
  18115. (org-let2 gprops lprops
  18116. '(funcall type match)))
  18117. (t (error "Invalid type in command series"))))
  18118. (widen)
  18119. (setq org-agenda-redo-command redo)
  18120. (goto-char (point-min)))
  18121. (org-finalize-agenda))
  18122. ;;;###autoload
  18123. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18124. "Run an agenda command in batch mode and send the result to STDOUT.
  18125. If CMD-KEY is a string of length 1, it is used as a key in
  18126. `org-agenda-custom-commands' and triggers this command. If it is a
  18127. longer string it is used as a tags/todo match string.
  18128. Paramters are alternating variable names and values that will be bound
  18129. before running the agenda command."
  18130. (let (pars)
  18131. (while parameters
  18132. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18133. (if (> (length cmd-key) 2)
  18134. (eval (list 'let (nreverse pars)
  18135. (list 'org-tags-view nil cmd-key)))
  18136. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18137. (set-buffer org-agenda-buffer-name)
  18138. (princ (org-encode-for-stdout (buffer-string)))))
  18139. (defun org-encode-for-stdout (string)
  18140. (if (fboundp 'encode-coding-string)
  18141. (encode-coding-string string buffer-file-coding-system)
  18142. string))
  18143. (defvar org-agenda-info nil)
  18144. ;;;###autoload
  18145. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18146. "Run an agenda command in batch mode and send the result to STDOUT.
  18147. If CMD-KEY is a string of length 1, it is used as a key in
  18148. `org-agenda-custom-commands' and triggers this command. If it is a
  18149. longer string it is used as a tags/todo match string.
  18150. Paramters are alternating variable names and values that will be bound
  18151. before running the agenda command.
  18152. The output gives a line for each selected agenda item. Each
  18153. item is a list of comma-separated values, like this:
  18154. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18155. category The category of the item
  18156. head The headline, without TODO kwd, TAGS and PRIORITY
  18157. type The type of the agenda entry, can be
  18158. todo selected in TODO match
  18159. tagsmatch selected in tags match
  18160. diary imported from diary
  18161. deadline a deadline on given date
  18162. scheduled scheduled on given date
  18163. timestamp entry has timestamp on given date
  18164. closed entry was closed on given date
  18165. upcoming-deadline warning about deadline
  18166. past-scheduled forwarded scheduled item
  18167. block entry has date block including g. date
  18168. todo The todo keyword, if any
  18169. tags All tags including inherited ones, separated by colons
  18170. date The relevant date, like 2007-2-14
  18171. time The time, like 15:00-16:50
  18172. extra Sting with extra planning info
  18173. priority-l The priority letter if any was given
  18174. priority-n The computed numerical priority
  18175. agenda-day The day in the agenda where this is listed"
  18176. (let (pars)
  18177. (while parameters
  18178. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18179. (push (list 'org-agenda-remove-tags t) pars)
  18180. (if (> (length cmd-key) 2)
  18181. (eval (list 'let (nreverse pars)
  18182. (list 'org-tags-view nil cmd-key)))
  18183. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18184. (set-buffer org-agenda-buffer-name)
  18185. (let* ((lines (org-split-string (buffer-string) "\n"))
  18186. line)
  18187. (while (setq line (pop lines))
  18188. (catch 'next
  18189. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18190. (setq org-agenda-info
  18191. (org-fix-agenda-info (text-properties-at 0 line)))
  18192. (princ
  18193. (org-encode-for-stdout
  18194. (mapconcat 'org-agenda-export-csv-mapper
  18195. '(org-category txt type todo tags date time-of-day extra
  18196. priority-letter priority agenda-day)
  18197. ",")))
  18198. (princ "\n"))))))
  18199. (defun org-fix-agenda-info (props)
  18200. "Make sure all properties on an agenda item have a canonical form,
  18201. so the export commands can easily use it."
  18202. (let (tmp re)
  18203. (when (setq tmp (plist-get props 'tags))
  18204. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18205. (when (setq tmp (plist-get props 'date))
  18206. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18207. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18208. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18209. (setq tmp (calendar-date-string tmp)))
  18210. (setq props (plist-put props 'date tmp)))
  18211. (when (setq tmp (plist-get props 'day))
  18212. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18213. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18214. (setq tmp (calendar-date-string tmp)))
  18215. (setq props (plist-put props 'day tmp))
  18216. (setq props (plist-put props 'agenda-day tmp)))
  18217. (when (setq tmp (plist-get props 'txt))
  18218. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18219. (plist-put props 'priority-letter (match-string 1 tmp))
  18220. (setq tmp (replace-match "" t t tmp)))
  18221. (when (and (setq re (plist-get props 'org-todo-regexp))
  18222. (setq re (concat "\\`\\.*" re " ?"))
  18223. (string-match re tmp))
  18224. (plist-put props 'todo (match-string 1 tmp))
  18225. (setq tmp (replace-match "" t t tmp)))
  18226. (plist-put props 'txt tmp)))
  18227. props)
  18228. (defun org-agenda-export-csv-mapper (prop)
  18229. (let ((res (plist-get org-agenda-info prop)))
  18230. (setq res
  18231. (cond
  18232. ((not res) "")
  18233. ((stringp res) res)
  18234. (t (prin1-to-string res))))
  18235. (while (string-match "," res)
  18236. (setq res (replace-match ";" t t res)))
  18237. (org-trim res)))
  18238. ;;;###autoload
  18239. (defun org-store-agenda-views (&rest parameters)
  18240. (interactive)
  18241. (eval (list 'org-batch-store-agenda-views)))
  18242. ;; FIXME, why is this a macro?????
  18243. ;;;###autoload
  18244. (defmacro org-batch-store-agenda-views (&rest parameters)
  18245. "Run all custom agenda commands that have a file argument."
  18246. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18247. (pop-up-frames nil)
  18248. (dir default-directory)
  18249. pars cmd thiscmdkey files opts)
  18250. (while parameters
  18251. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18252. (setq pars (reverse pars))
  18253. (save-window-excursion
  18254. (while cmds
  18255. (setq cmd (pop cmds)
  18256. thiscmdkey (car cmd)
  18257. opts (nth 4 cmd)
  18258. files (nth 5 cmd))
  18259. (if (stringp files) (setq files (list files)))
  18260. (when files
  18261. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18262. (list 'org-agenda nil thiscmdkey)))
  18263. (set-buffer org-agenda-buffer-name)
  18264. (while files
  18265. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18266. (list 'org-write-agenda
  18267. (expand-file-name (pop files) dir) t))))
  18268. (and (get-buffer org-agenda-buffer-name)
  18269. (kill-buffer org-agenda-buffer-name)))))))
  18270. (defun org-write-agenda (file &optional nosettings)
  18271. "Write the current buffer (an agenda view) as a file.
  18272. Depending on the extension of the file name, plain text (.txt),
  18273. HTML (.html or .htm) or Postscript (.ps) is produced.
  18274. If NOSETTINGS is given, do not scope the settings of
  18275. `org-agenda-exporter-settings' into the export commands. This is used when
  18276. the settings have already been scoped and we do not wish to overrule other,
  18277. higher priority settings."
  18278. (interactive "FWrite agenda to file: ")
  18279. (if (not (file-writable-p file))
  18280. (error "Cannot write agenda to file %s" file))
  18281. (cond
  18282. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18283. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18284. (org-let (if nosettings nil org-agenda-exporter-settings)
  18285. '(save-excursion
  18286. (save-window-excursion
  18287. (cond
  18288. ((string-match "\\.html?\\'" file)
  18289. (set-buffer (htmlize-buffer (current-buffer)))
  18290. (when (and org-agenda-export-html-style
  18291. (string-match "<style>" org-agenda-export-html-style))
  18292. ;; replace <style> section with org-agenda-export-html-style
  18293. (goto-char (point-min))
  18294. (kill-region (- (search-forward "<style") 6)
  18295. (search-forward "</style>"))
  18296. (insert org-agenda-export-html-style))
  18297. (write-file file)
  18298. (kill-buffer (current-buffer))
  18299. (message "HTML written to %s" file))
  18300. ((string-match "\\.ps\\'" file)
  18301. (ps-print-buffer-with-faces file)
  18302. (message "Postscript written to %s" file))
  18303. (t
  18304. (let ((bs (buffer-string)))
  18305. (find-file file)
  18306. (insert bs)
  18307. (save-buffer 0)
  18308. (kill-buffer (current-buffer))
  18309. (message "Plain text written to %s" file))))))
  18310. (set-buffer org-agenda-buffer-name)))
  18311. (defmacro org-no-read-only (&rest body)
  18312. "Inhibit read-only for BODY."
  18313. `(let ((inhibit-read-only t)) ,@body))
  18314. (defun org-check-for-org-mode ()
  18315. "Make sure current buffer is in org-mode. Error if not."
  18316. (or (org-mode-p)
  18317. (error "Cannot execute org-mode agenda command on buffer in %s."
  18318. major-mode)))
  18319. (defun org-fit-agenda-window ()
  18320. "Fit the window to the buffer size."
  18321. (and (memq org-agenda-window-setup '(reorganize-frame))
  18322. (fboundp 'fit-window-to-buffer)
  18323. (fit-window-to-buffer
  18324. nil
  18325. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18326. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18327. ;;; Agenda file list
  18328. (defun org-agenda-files (&optional unrestricted)
  18329. "Get the list of agenda files.
  18330. Optional UNRESTRICTED means return the full list even if a restriction
  18331. is currently in place."
  18332. (let ((files
  18333. (cond
  18334. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18335. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18336. ((listp org-agenda-files) org-agenda-files)
  18337. (t (error "Invalid value of `org-agenda-files'")))))
  18338. (setq files (apply 'append
  18339. (mapcar (lambda (f)
  18340. (if (file-directory-p f)
  18341. (directory-files f t
  18342. org-agenda-file-regexp)
  18343. (list f)))
  18344. files)))
  18345. (if org-agenda-skip-unavailable-files
  18346. (delq nil
  18347. (mapcar (function
  18348. (lambda (file)
  18349. (and (file-readable-p file) file)))
  18350. files))
  18351. files))) ; `org-check-agenda-file' will remove them from the list
  18352. (defun org-edit-agenda-file-list ()
  18353. "Edit the list of agenda files.
  18354. Depending on setup, this either uses customize to edit the variable
  18355. `org-agenda-files', or it visits the file that is holding the list. In the
  18356. latter case, the buffer is set up in a way that saving it automatically kills
  18357. the buffer and restores the previous window configuration."
  18358. (interactive)
  18359. (if (stringp org-agenda-files)
  18360. (let ((cw (current-window-configuration)))
  18361. (find-file org-agenda-files)
  18362. (org-set-local 'org-window-configuration cw)
  18363. (org-add-hook 'after-save-hook
  18364. (lambda ()
  18365. (set-window-configuration
  18366. (prog1 org-window-configuration
  18367. (kill-buffer (current-buffer))))
  18368. (org-install-agenda-files-menu)
  18369. (message "New agenda file list installed"))
  18370. nil 'local)
  18371. (message "%s" (substitute-command-keys
  18372. "Edit list and finish with \\[save-buffer]")))
  18373. (customize-variable 'org-agenda-files)))
  18374. (defun org-store-new-agenda-file-list (list)
  18375. "Set new value for the agenda file list and save it correcly."
  18376. (if (stringp org-agenda-files)
  18377. (let ((f org-agenda-files) b)
  18378. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18379. (with-temp-file f
  18380. (insert (mapconcat 'identity list "\n") "\n")))
  18381. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18382. (setq org-agenda-files list)
  18383. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18384. (defun org-read-agenda-file-list ()
  18385. "Read the list of agenda files from a file."
  18386. (when (stringp org-agenda-files)
  18387. (with-temp-buffer
  18388. (insert-file-contents org-agenda-files)
  18389. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18390. ;;;###autoload
  18391. (defun org-cycle-agenda-files ()
  18392. "Cycle through the files in `org-agenda-files'.
  18393. If the current buffer visits an agenda file, find the next one in the list.
  18394. If the current buffer does not, find the first agenda file."
  18395. (interactive)
  18396. (let* ((fs (org-agenda-files t))
  18397. (files (append fs (list (car fs))))
  18398. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18399. file)
  18400. (unless files (error "No agenda files"))
  18401. (catch 'exit
  18402. (while (setq file (pop files))
  18403. (if (equal (file-truename file) tcf)
  18404. (when (car files)
  18405. (find-file (car files))
  18406. (throw 'exit t))))
  18407. (find-file (car fs)))
  18408. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18409. (defun org-agenda-file-to-front (&optional to-end)
  18410. "Move/add the current file to the top of the agenda file list.
  18411. If the file is not present in the list, it is added to the front. If it is
  18412. present, it is moved there. With optional argument TO-END, add/move to the
  18413. end of the list."
  18414. (interactive "P")
  18415. (let ((org-agenda-skip-unavailable-files nil)
  18416. (file-alist (mapcar (lambda (x)
  18417. (cons (file-truename x) x))
  18418. (org-agenda-files t)))
  18419. (ctf (file-truename buffer-file-name))
  18420. x had)
  18421. (setq x (assoc ctf file-alist) had x)
  18422. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18423. (if to-end
  18424. (setq file-alist (append (delq x file-alist) (list x)))
  18425. (setq file-alist (cons x (delq x file-alist))))
  18426. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18427. (org-install-agenda-files-menu)
  18428. (message "File %s to %s of agenda file list"
  18429. (if had "moved" "added") (if to-end "end" "front"))))
  18430. (defun org-remove-file (&optional file)
  18431. "Remove current file from the list of files in variable `org-agenda-files'.
  18432. These are the files which are being checked for agenda entries.
  18433. Optional argument FILE means, use this file instead of the current."
  18434. (interactive)
  18435. (let* ((org-agenda-skip-unavailable-files nil)
  18436. (file (or file buffer-file-name))
  18437. (true-file (file-truename file))
  18438. (afile (abbreviate-file-name file))
  18439. (files (delq nil (mapcar
  18440. (lambda (x)
  18441. (if (equal true-file
  18442. (file-truename x))
  18443. nil x))
  18444. (org-agenda-files t)))))
  18445. (if (not (= (length files) (length (org-agenda-files t))))
  18446. (progn
  18447. (org-store-new-agenda-file-list files)
  18448. (org-install-agenda-files-menu)
  18449. (message "Removed file: %s" afile))
  18450. (message "File was not in list: %s (not removed)" afile))))
  18451. (defun org-file-menu-entry (file)
  18452. (vector file (list 'find-file file) t))
  18453. (defun org-check-agenda-file (file)
  18454. "Make sure FILE exists. If not, ask user what to do."
  18455. (when (not (file-exists-p file))
  18456. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18457. (abbreviate-file-name file))
  18458. (let ((r (downcase (read-char-exclusive))))
  18459. (cond
  18460. ((equal r ?r)
  18461. (org-remove-file file)
  18462. (throw 'nextfile t))
  18463. (t (error "Abort"))))))
  18464. ;;; Agenda prepare and finalize
  18465. (defvar org-agenda-multi nil) ; dynammically scoped
  18466. (defvar org-agenda-buffer-name "*Org Agenda*")
  18467. (defvar org-pre-agenda-window-conf nil)
  18468. (defvar org-agenda-name nil)
  18469. (defun org-prepare-agenda (&optional name)
  18470. (setq org-todo-keywords-for-agenda nil)
  18471. (setq org-done-keywords-for-agenda nil)
  18472. (if org-agenda-multi
  18473. (progn
  18474. (setq buffer-read-only nil)
  18475. (goto-char (point-max))
  18476. (unless (or (bobp) org-agenda-compact-blocks)
  18477. (insert "\n" (make-string (window-width) ?=) "\n"))
  18478. (narrow-to-region (point) (point-max)))
  18479. (org-agenda-maybe-reset-markers 'force)
  18480. (org-prepare-agenda-buffers (org-agenda-files))
  18481. (setq org-todo-keywords-for-agenda
  18482. (org-uniquify org-todo-keywords-for-agenda))
  18483. (setq org-done-keywords-for-agenda
  18484. (org-uniquify org-done-keywords-for-agenda))
  18485. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18486. (awin (get-buffer-window abuf)))
  18487. (cond
  18488. ((equal (current-buffer) abuf) nil)
  18489. (awin (select-window awin))
  18490. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18491. ((equal org-agenda-window-setup 'current-window)
  18492. (switch-to-buffer abuf))
  18493. ((equal org-agenda-window-setup 'other-window)
  18494. (org-switch-to-buffer-other-window abuf))
  18495. ((equal org-agenda-window-setup 'other-frame)
  18496. (switch-to-buffer-other-frame abuf))
  18497. ((equal org-agenda-window-setup 'reorganize-frame)
  18498. (delete-other-windows)
  18499. (org-switch-to-buffer-other-window abuf))))
  18500. (setq buffer-read-only nil)
  18501. (erase-buffer)
  18502. (org-agenda-mode)
  18503. (and name (not org-agenda-name)
  18504. (org-set-local 'org-agenda-name name)))
  18505. (setq buffer-read-only nil))
  18506. (defun org-finalize-agenda ()
  18507. "Finishing touch for the agenda buffer, called just before displaying it."
  18508. (unless org-agenda-multi
  18509. (save-excursion
  18510. (let ((inhibit-read-only t))
  18511. (goto-char (point-min))
  18512. (while (org-activate-bracket-links (point-max))
  18513. (add-text-properties (match-beginning 0) (match-end 0)
  18514. '(face org-link)))
  18515. (org-agenda-align-tags)
  18516. (unless org-agenda-with-colors
  18517. (remove-text-properties (point-min) (point-max) '(face nil))))
  18518. (if (and (boundp 'org-overriding-columns-format)
  18519. org-overriding-columns-format)
  18520. (org-set-local 'org-overriding-columns-format
  18521. org-overriding-columns-format))
  18522. (if (and (boundp 'org-agenda-view-columns-initially)
  18523. org-agenda-view-columns-initially)
  18524. (org-agenda-columns))
  18525. (when org-agenda-fontify-priorities
  18526. (org-fontify-priorities))
  18527. (run-hooks 'org-finalize-agenda-hook)
  18528. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18529. )))
  18530. (defun org-fontify-priorities ()
  18531. "Make highest priority lines bold, and lowest italic."
  18532. (interactive)
  18533. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18534. (org-delete-overlay o)))
  18535. (org-overlays-in (point-min) (point-max)))
  18536. (save-excursion
  18537. (let ((inhibit-read-only t)
  18538. b e p ov h l)
  18539. (goto-char (point-min))
  18540. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18541. (setq h (or (get-char-property (point) 'org-highest-priority)
  18542. org-highest-priority)
  18543. l (or (get-char-property (point) 'org-lowest-priority)
  18544. org-lowest-priority)
  18545. p (string-to-char (match-string 1))
  18546. b (match-beginning 0) e (point-at-eol)
  18547. ov (org-make-overlay b e))
  18548. (org-overlay-put
  18549. ov 'face
  18550. (cond ((listp org-agenda-fontify-priorities)
  18551. (cdr (assoc p org-agenda-fontify-priorities)))
  18552. ((equal p l) 'italic)
  18553. ((equal p h) 'bold)))
  18554. (org-overlay-put ov 'org-type 'org-priority)))))
  18555. (defun org-prepare-agenda-buffers (files)
  18556. "Create buffers for all agenda files, protect archived trees and comments."
  18557. (interactive)
  18558. (let ((pa '(:org-archived t))
  18559. (pc '(:org-comment t))
  18560. (pall '(:org-archived t :org-comment t))
  18561. (inhibit-read-only t)
  18562. (rea (concat ":" org-archive-tag ":"))
  18563. bmp file re)
  18564. (save-excursion
  18565. (save-restriction
  18566. (while (setq file (pop files))
  18567. (if (bufferp file)
  18568. (set-buffer file)
  18569. (org-check-agenda-file file)
  18570. (set-buffer (org-get-agenda-file-buffer file)))
  18571. (widen)
  18572. (setq bmp (buffer-modified-p))
  18573. (org-refresh-category-properties)
  18574. (setq org-todo-keywords-for-agenda
  18575. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18576. (setq org-done-keywords-for-agenda
  18577. (append org-done-keywords-for-agenda org-done-keywords))
  18578. (save-excursion
  18579. (remove-text-properties (point-min) (point-max) pall)
  18580. (when org-agenda-skip-archived-trees
  18581. (goto-char (point-min))
  18582. (while (re-search-forward rea nil t)
  18583. (if (org-on-heading-p t)
  18584. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18585. (goto-char (point-min))
  18586. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18587. (while (re-search-forward re nil t)
  18588. (add-text-properties
  18589. (match-beginning 0) (org-end-of-subtree t) pc)))
  18590. (set-buffer-modified-p bmp))))))
  18591. (defvar org-agenda-skip-function nil
  18592. "Function to be called at each match during agenda construction.
  18593. If this function returns nil, the current match should not be skipped.
  18594. Otherwise, the function must return a position from where the search
  18595. should be continued.
  18596. This may also be a Lisp form, it will be evaluated.
  18597. Never set this variable using `setq' or so, because then it will apply
  18598. to all future agenda commands. Instead, bind it with `let' to scope
  18599. it dynamically into the agenda-constructing command. A good way to set
  18600. it is through options in org-agenda-custom-commands.")
  18601. (defun org-agenda-skip ()
  18602. "Throw to `:skip' in places that should be skipped.
  18603. Also moves point to the end of the skipped region, so that search can
  18604. continue from there."
  18605. (let ((p (point-at-bol)) to fp)
  18606. (and org-agenda-skip-archived-trees
  18607. (get-text-property p :org-archived)
  18608. (org-end-of-subtree t)
  18609. (throw :skip t))
  18610. (and (get-text-property p :org-comment)
  18611. (org-end-of-subtree t)
  18612. (throw :skip t))
  18613. (if (equal (char-after p) ?#) (throw :skip t))
  18614. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18615. (consp org-agenda-skip-function))
  18616. (setq to (save-excursion
  18617. (save-match-data
  18618. (if fp
  18619. (funcall org-agenda-skip-function)
  18620. (eval org-agenda-skip-function))))))
  18621. (goto-char to)
  18622. (throw :skip t))))
  18623. (defvar org-agenda-markers nil
  18624. "List of all currently active markers created by `org-agenda'.")
  18625. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18626. "Creation time of the last agenda marker.")
  18627. (defun org-agenda-new-marker (&optional pos)
  18628. "Return a new agenda marker.
  18629. Org-mode keeps a list of these markers and resets them when they are
  18630. no longer in use."
  18631. (let ((m (copy-marker (or pos (point)))))
  18632. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18633. (push m org-agenda-markers)
  18634. m))
  18635. (defun org-agenda-maybe-reset-markers (&optional force)
  18636. "Reset markers created by `org-agenda'. But only if they are old enough."
  18637. (if (or (and force (not org-agenda-multi))
  18638. (> (- (time-to-seconds (current-time))
  18639. org-agenda-last-marker-time)
  18640. 5))
  18641. (while org-agenda-markers
  18642. (move-marker (pop org-agenda-markers) nil))))
  18643. (defun org-get-agenda-file-buffer (file)
  18644. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18645. it to the list of buffers which might be released later."
  18646. (let ((buf (org-find-base-buffer-visiting file)))
  18647. (if buf
  18648. buf ; just return it
  18649. ;; Make a new buffer and remember it
  18650. (setq buf (find-file-noselect file))
  18651. (if buf (push buf org-agenda-new-buffers))
  18652. buf)))
  18653. (defun org-release-buffers (blist)
  18654. "Release all buffers in list, asking the user for confirmation when needed.
  18655. When a buffer is unmodified, it is just killed. When modified, it is saved
  18656. \(if the user agrees) and then killed."
  18657. (let (buf file)
  18658. (while (setq buf (pop blist))
  18659. (setq file (buffer-file-name buf))
  18660. (when (and (buffer-modified-p buf)
  18661. file
  18662. (y-or-n-p (format "Save file %s? " file)))
  18663. (with-current-buffer buf (save-buffer)))
  18664. (kill-buffer buf))))
  18665. (defun org-get-category (&optional pos)
  18666. "Get the category applying to position POS."
  18667. (get-text-property (or pos (point)) 'org-category))
  18668. ;;; Agenda timeline
  18669. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18670. (defun org-timeline (&optional include-all)
  18671. "Show a time-sorted view of the entries in the current org file.
  18672. Only entries with a time stamp of today or later will be listed. With
  18673. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18674. under the current date.
  18675. If the buffer contains an active region, only check the region for
  18676. dates."
  18677. (interactive "P")
  18678. (require 'calendar)
  18679. (org-compile-prefix-format 'timeline)
  18680. (org-set-sorting-strategy 'timeline)
  18681. (let* ((dopast t)
  18682. (dotodo include-all)
  18683. (doclosed org-agenda-show-log)
  18684. (entry buffer-file-name)
  18685. (date (calendar-current-date))
  18686. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18687. (end (if (org-region-active-p) (region-end) (point-max)))
  18688. (day-numbers (org-get-all-dates beg end 'no-ranges
  18689. t doclosed ; always include today
  18690. org-timeline-show-empty-dates))
  18691. (org-deadline-warning-days 0)
  18692. (org-agenda-only-exact-dates t)
  18693. (today (time-to-days (current-time)))
  18694. (past t)
  18695. args
  18696. s e rtn d emptyp)
  18697. (setq org-agenda-redo-command
  18698. (list 'progn
  18699. (list 'org-switch-to-buffer-other-window (current-buffer))
  18700. (list 'org-timeline (list 'quote include-all))))
  18701. (if (not dopast)
  18702. ;; Remove past dates from the list of dates.
  18703. (setq day-numbers (delq nil (mapcar (lambda(x)
  18704. (if (>= x today) x nil))
  18705. day-numbers))))
  18706. (org-prepare-agenda (concat "Timeline "
  18707. (file-name-nondirectory buffer-file-name)))
  18708. (if doclosed (push :closed args))
  18709. (push :timestamp args)
  18710. (push :deadline args)
  18711. (push :scheduled args)
  18712. (push :sexp args)
  18713. (if dotodo (push :todo args))
  18714. (while (setq d (pop day-numbers))
  18715. (if (and (listp d) (eq (car d) :omitted))
  18716. (progn
  18717. (setq s (point))
  18718. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18719. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18720. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18721. (if (and (>= d today)
  18722. dopast
  18723. past)
  18724. (progn
  18725. (setq past nil)
  18726. (insert (make-string 79 ?-) "\n")))
  18727. (setq date (calendar-gregorian-from-absolute d))
  18728. (setq s (point))
  18729. (setq rtn (and (not emptyp)
  18730. (apply 'org-agenda-get-day-entries entry
  18731. date args)))
  18732. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18733. (progn
  18734. (insert
  18735. (if (stringp org-agenda-format-date)
  18736. (format-time-string org-agenda-format-date
  18737. (org-time-from-absolute date))
  18738. (funcall org-agenda-format-date date))
  18739. "\n")
  18740. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18741. (put-text-property s (1- (point)) 'org-date-line t)
  18742. (if (equal d today)
  18743. (put-text-property s (1- (point)) 'org-today t))
  18744. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18745. (put-text-property s (1- (point)) 'day d)))))
  18746. (goto-char (point-min))
  18747. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18748. (point-min)))
  18749. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18750. (org-finalize-agenda)
  18751. (setq buffer-read-only t)))
  18752. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18753. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18754. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18755. not every single day in the range. If FORCE-TODAY is non-nil, make
  18756. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18757. inactive time stamps (those in square brackets) are included.
  18758. When EMPTY is non-nil, also include days without any entries."
  18759. (let ((re (concat
  18760. (if pre-re pre-re "")
  18761. (if inactive org-ts-regexp-both org-ts-regexp)))
  18762. dates dates1 date day day1 day2 ts1 ts2)
  18763. (if force-today
  18764. (setq dates (list (time-to-days (current-time)))))
  18765. (save-excursion
  18766. (goto-char beg)
  18767. (while (re-search-forward re end t)
  18768. (setq day (time-to-days (org-time-string-to-time
  18769. (substring (match-string 1) 0 10))))
  18770. (or (memq day dates) (push day dates)))
  18771. (unless no-ranges
  18772. (goto-char beg)
  18773. (while (re-search-forward org-tr-regexp end t)
  18774. (setq ts1 (substring (match-string 1) 0 10)
  18775. ts2 (substring (match-string 2) 0 10)
  18776. day1 (time-to-days (org-time-string-to-time ts1))
  18777. day2 (time-to-days (org-time-string-to-time ts2)))
  18778. (while (< (setq day1 (1+ day1)) day2)
  18779. (or (memq day1 dates) (push day1 dates)))))
  18780. (setq dates (sort dates '<))
  18781. (when empty
  18782. (while (setq day (pop dates))
  18783. (setq day2 (car dates))
  18784. (push day dates1)
  18785. (when (and day2 empty)
  18786. (if (or (eq empty t)
  18787. (and (numberp empty) (<= (- day2 day) empty)))
  18788. (while (< (setq day (1+ day)) day2)
  18789. (push (list day) dates1))
  18790. (push (cons :omitted (- day2 day)) dates1))))
  18791. (setq dates (nreverse dates1)))
  18792. dates)))
  18793. ;;; Agenda Daily/Weekly
  18794. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18795. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18796. (defvar org-agenda-last-arguments nil
  18797. "The arguments of the previous call to org-agenda")
  18798. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18799. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18800. (defvar org-include-all-loc nil) ; local variable
  18801. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18802. ;;;###autoload
  18803. (defun org-agenda-list (&optional include-all start-day ndays)
  18804. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18805. The view will be for the current day or week, but from the overview buffer
  18806. you will be able to go to other days/weeks.
  18807. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18808. all unfinished TODO items will also be shown, before the agenda.
  18809. This feature is considered obsolete, please use the TODO list or a block
  18810. agenda instead.
  18811. With a numeric prefix argument in an interactive call, the agenda will
  18812. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18813. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18814. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18815. given in `org-agenda-start-on-weekday'."
  18816. (interactive "P")
  18817. (if (and (integerp include-all) (> include-all 0))
  18818. (setq ndays include-all include-all nil))
  18819. (setq ndays (or ndays org-agenda-ndays)
  18820. start-day (or start-day org-agenda-start-day))
  18821. (if org-agenda-overriding-arguments
  18822. (setq include-all (car org-agenda-overriding-arguments)
  18823. start-day (nth 1 org-agenda-overriding-arguments)
  18824. ndays (nth 2 org-agenda-overriding-arguments)))
  18825. (if (stringp start-day)
  18826. ;; Convert to an absolute day number
  18827. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18828. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18829. (org-compile-prefix-format 'agenda)
  18830. (org-set-sorting-strategy 'agenda)
  18831. (require 'calendar)
  18832. (let* ((org-agenda-start-on-weekday
  18833. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18834. org-agenda-start-on-weekday nil))
  18835. (thefiles (org-agenda-files))
  18836. (files thefiles)
  18837. (today (time-to-days
  18838. (time-subtract (current-time)
  18839. (list 0 (* 3600 org-extend-today-until) 0))))
  18840. (sd (or start-day today))
  18841. (start (if (or (null org-agenda-start-on-weekday)
  18842. (< org-agenda-ndays 7))
  18843. sd
  18844. (let* ((nt (calendar-day-of-week
  18845. (calendar-gregorian-from-absolute sd)))
  18846. (n1 org-agenda-start-on-weekday)
  18847. (d (- nt n1)))
  18848. (- sd (+ (if (< d 0) 7 0) d)))))
  18849. (day-numbers (list start))
  18850. (day-cnt 0)
  18851. (inhibit-redisplay (not debug-on-error))
  18852. s e rtn rtnall file date d start-pos end-pos todayp nd)
  18853. (setq org-agenda-redo-command
  18854. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  18855. ;; Make the list of days
  18856. (setq ndays (or ndays org-agenda-ndays)
  18857. nd ndays)
  18858. (while (> ndays 1)
  18859. (push (1+ (car day-numbers)) day-numbers)
  18860. (setq ndays (1- ndays)))
  18861. (setq day-numbers (nreverse day-numbers))
  18862. (org-prepare-agenda "Day/Week")
  18863. (org-set-local 'org-starting-day (car day-numbers))
  18864. (org-set-local 'org-include-all-loc include-all)
  18865. (org-set-local 'org-agenda-span
  18866. (org-agenda-ndays-to-span nd))
  18867. (when (and (or include-all org-agenda-include-all-todo)
  18868. (member today day-numbers))
  18869. (setq files thefiles
  18870. rtnall nil)
  18871. (while (setq file (pop files))
  18872. (catch 'nextfile
  18873. (org-check-agenda-file file)
  18874. (setq date (calendar-gregorian-from-absolute today)
  18875. rtn (org-agenda-get-day-entries
  18876. file date :todo))
  18877. (setq rtnall (append rtnall rtn))))
  18878. (when rtnall
  18879. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  18880. (add-text-properties (point-min) (1- (point))
  18881. (list 'face 'org-agenda-structure))
  18882. (insert (org-finalize-agenda-entries rtnall) "\n")))
  18883. (unless org-agenda-compact-blocks
  18884. (setq s (point))
  18885. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  18886. "-agenda:\n")
  18887. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  18888. 'org-date-line t)))
  18889. (while (setq d (pop day-numbers))
  18890. (setq date (calendar-gregorian-from-absolute d)
  18891. s (point))
  18892. (if (or (setq todayp (= d today))
  18893. (and (not start-pos) (= d sd)))
  18894. (setq start-pos (point))
  18895. (if (and start-pos (not end-pos))
  18896. (setq end-pos (point))))
  18897. (setq files thefiles
  18898. rtnall nil)
  18899. (while (setq file (pop files))
  18900. (catch 'nextfile
  18901. (org-check-agenda-file file)
  18902. (if org-agenda-show-log
  18903. (setq rtn (org-agenda-get-day-entries
  18904. file date
  18905. :deadline :scheduled :timestamp :sexp :closed))
  18906. (setq rtn (org-agenda-get-day-entries
  18907. file date
  18908. :deadline :scheduled :sexp :timestamp)))
  18909. (setq rtnall (append rtnall rtn))))
  18910. (if org-agenda-include-diary
  18911. (progn
  18912. (require 'diary-lib)
  18913. (setq rtn (org-get-entries-from-diary date))
  18914. (setq rtnall (append rtnall rtn))))
  18915. (if (or rtnall org-agenda-show-all-dates)
  18916. (progn
  18917. (setq day-cnt (1+ day-cnt))
  18918. (insert
  18919. (if (stringp org-agenda-format-date)
  18920. (format-time-string org-agenda-format-date
  18921. (org-time-from-absolute date))
  18922. (funcall org-agenda-format-date date))
  18923. "\n")
  18924. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18925. (put-text-property s (1- (point)) 'org-date-line t)
  18926. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  18927. (if todayp (put-text-property s (1- (point)) 'org-today t))
  18928. (if rtnall (insert
  18929. (org-finalize-agenda-entries
  18930. (org-agenda-add-time-grid-maybe
  18931. rtnall nd todayp))
  18932. "\n"))
  18933. (put-text-property s (1- (point)) 'day d)
  18934. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  18935. (goto-char (point-min))
  18936. (org-fit-agenda-window)
  18937. (unless (and (pos-visible-in-window-p (point-min))
  18938. (pos-visible-in-window-p (point-max)))
  18939. (goto-char (1- (point-max)))
  18940. (recenter -1)
  18941. (if (not (pos-visible-in-window-p (or start-pos 1)))
  18942. (progn
  18943. (goto-char (or start-pos 1))
  18944. (recenter 1))))
  18945. (goto-char (or start-pos 1))
  18946. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  18947. (org-finalize-agenda)
  18948. (setq buffer-read-only t)
  18949. (message "")))
  18950. (defun org-agenda-ndays-to-span (n)
  18951. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  18952. ;;; Agenda TODO list
  18953. (defvar org-select-this-todo-keyword nil)
  18954. (defvar org-last-arg nil)
  18955. ;;;###autoload
  18956. (defun org-todo-list (arg)
  18957. "Show all TODO entries from all agenda file in a single list.
  18958. The prefix arg can be used to select a specific TODO keyword and limit
  18959. the list to these. When using \\[universal-argument], you will be prompted
  18960. for a keyword. A numeric prefix directly selects the Nth keyword in
  18961. `org-todo-keywords-1'."
  18962. (interactive "P")
  18963. (require 'calendar)
  18964. (org-compile-prefix-format 'todo)
  18965. (org-set-sorting-strategy 'todo)
  18966. (org-prepare-agenda "TODO")
  18967. (let* ((today (time-to-days (current-time)))
  18968. (date (calendar-gregorian-from-absolute today))
  18969. (kwds org-todo-keywords-for-agenda)
  18970. (completion-ignore-case t)
  18971. (org-select-this-todo-keyword
  18972. (if (stringp arg) arg
  18973. (and arg (integerp arg) (> arg 0)
  18974. (nth (1- arg) kwds))))
  18975. rtn rtnall files file pos)
  18976. (when (equal arg '(4))
  18977. (setq org-select-this-todo-keyword
  18978. (completing-read "Keyword (or KWD1|K2D2|...): "
  18979. (mapcar 'list kwds) nil nil)))
  18980. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  18981. (org-set-local 'org-last-arg arg)
  18982. (setq org-agenda-redo-command
  18983. '(org-todo-list (or current-prefix-arg org-last-arg)))
  18984. (setq files (org-agenda-files)
  18985. rtnall nil)
  18986. (while (setq file (pop files))
  18987. (catch 'nextfile
  18988. (org-check-agenda-file file)
  18989. (setq rtn (org-agenda-get-day-entries file date :todo))
  18990. (setq rtnall (append rtnall rtn))))
  18991. (if org-agenda-overriding-header
  18992. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18993. nil 'face 'org-agenda-structure) "\n")
  18994. (insert "Global list of TODO items of type: ")
  18995. (add-text-properties (point-min) (1- (point))
  18996. (list 'face 'org-agenda-structure))
  18997. (setq pos (point))
  18998. (insert (or org-select-this-todo-keyword "ALL") "\n")
  18999. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19000. (setq pos (point))
  19001. (unless org-agenda-multi
  19002. (insert "Available with `N r': (0)ALL")
  19003. (let ((n 0) s)
  19004. (mapc (lambda (x)
  19005. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19006. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19007. (insert "\n "))
  19008. (insert " " s))
  19009. kwds))
  19010. (insert "\n"))
  19011. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19012. (when rtnall
  19013. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19014. (goto-char (point-min))
  19015. (org-fit-agenda-window)
  19016. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19017. (org-finalize-agenda)
  19018. (setq buffer-read-only t)))
  19019. ;;; Agenda tags match
  19020. ;;;###autoload
  19021. (defun org-tags-view (&optional todo-only match)
  19022. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19023. The prefix arg TODO-ONLY limits the search to TODO entries."
  19024. (interactive "P")
  19025. (org-compile-prefix-format 'tags)
  19026. (org-set-sorting-strategy 'tags)
  19027. (let* ((org-tags-match-list-sublevels
  19028. (if todo-only t org-tags-match-list-sublevels))
  19029. (completion-ignore-case t)
  19030. rtn rtnall files file pos matcher
  19031. buffer)
  19032. (setq matcher (org-make-tags-matcher match)
  19033. match (car matcher) matcher (cdr matcher))
  19034. (org-prepare-agenda (concat "TAGS " match))
  19035. (setq org-agenda-redo-command
  19036. (list 'org-tags-view (list 'quote todo-only)
  19037. (list 'if 'current-prefix-arg nil match)))
  19038. (setq files (org-agenda-files)
  19039. rtnall nil)
  19040. (while (setq file (pop files))
  19041. (catch 'nextfile
  19042. (org-check-agenda-file file)
  19043. (setq buffer (if (file-exists-p file)
  19044. (org-get-agenda-file-buffer file)
  19045. (error "No such file %s" file)))
  19046. (if (not buffer)
  19047. ;; If file does not exist, merror message to agenda
  19048. (setq rtn (list
  19049. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19050. rtnall (append rtnall rtn))
  19051. (with-current-buffer buffer
  19052. (unless (org-mode-p)
  19053. (error "Agenda file %s is not in `org-mode'" file))
  19054. (save-excursion
  19055. (save-restriction
  19056. (if org-agenda-restrict
  19057. (narrow-to-region org-agenda-restrict-begin
  19058. org-agenda-restrict-end)
  19059. (widen))
  19060. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19061. (setq rtnall (append rtnall rtn))))))))
  19062. (if org-agenda-overriding-header
  19063. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19064. nil 'face 'org-agenda-structure) "\n")
  19065. (insert "Headlines with TAGS match: ")
  19066. (add-text-properties (point-min) (1- (point))
  19067. (list 'face 'org-agenda-structure))
  19068. (setq pos (point))
  19069. (insert match "\n")
  19070. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19071. (setq pos (point))
  19072. (unless org-agenda-multi
  19073. (insert "Press `C-u r' to search again with new search string\n"))
  19074. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19075. (when rtnall
  19076. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19077. (goto-char (point-min))
  19078. (org-fit-agenda-window)
  19079. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19080. (org-finalize-agenda)
  19081. (setq buffer-read-only t)))
  19082. ;;; Agenda Finding stuck projects
  19083. (defvar org-agenda-skip-regexp nil
  19084. "Regular expression used in skipping subtrees for the agenda.
  19085. This is basically a temporary global variable that can be set and then
  19086. used by user-defined selections using `org-agenda-skip-function'.")
  19087. (defvar org-agenda-overriding-header nil
  19088. "When this is set during todo and tags searches, will replace header.")
  19089. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19090. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19091. If yes, it returns the end position of this tree, causing agenda commands
  19092. to skip this subtree. This is a function that can be put into
  19093. `org-agenda-skip-function' for the duration of a command."
  19094. (let ((end (save-excursion (org-end-of-subtree t)))
  19095. skip)
  19096. (save-excursion
  19097. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19098. (and skip end)))
  19099. (defun org-agenda-skip-entry-if (&rest conditions)
  19100. "Skip entry if any of CONDITIONS is true.
  19101. See `org-agenda-skip-if' for details."
  19102. (org-agenda-skip-if nil conditions))
  19103. (defun org-agenda-skip-subtree-if (&rest conditions)
  19104. "Skip entry if any of CONDITIONS is true.
  19105. See `org-agenda-skip-if' for details."
  19106. (org-agenda-skip-if t conditions))
  19107. (defun org-agenda-skip-if (subtree conditions)
  19108. "Checks current entity for CONDITIONS.
  19109. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19110. the entry, i.e. the text before the next heading is checked.
  19111. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19112. from different tests. Valid conditions are:
  19113. scheduled Check if there is a scheduled cookie
  19114. notscheduled Check if there is no scheduled cookie
  19115. deadline Check if there is a deadline
  19116. notdeadline Check if there is no deadline
  19117. regexp Check if regexp matches
  19118. notregexp Check if regexp does not match.
  19119. The regexp is taken from the conditions list, it must come right after
  19120. the `regexp' or `notregexp' element.
  19121. If any of these conditions is met, this function returns the end point of
  19122. the entity, causing the search to continue from there. This is a function
  19123. that can be put into `org-agenda-skip-function' for the duration of a command."
  19124. (let (beg end m)
  19125. (org-back-to-heading t)
  19126. (setq beg (point)
  19127. end (if subtree
  19128. (progn (org-end-of-subtree t) (point))
  19129. (progn (outline-next-heading) (1- (point)))))
  19130. (goto-char beg)
  19131. (and
  19132. (or
  19133. (and (memq 'scheduled conditions)
  19134. (re-search-forward org-scheduled-time-regexp end t))
  19135. (and (memq 'notscheduled conditions)
  19136. (not (re-search-forward org-scheduled-time-regexp end t)))
  19137. (and (memq 'deadline conditions)
  19138. (re-search-forward org-deadline-time-regexp end t))
  19139. (and (memq 'notdeadline conditions)
  19140. (not (re-search-forward org-deadline-time-regexp end t)))
  19141. (and (setq m (memq 'regexp conditions))
  19142. (stringp (nth 1 m))
  19143. (re-search-forward (nth 1 m) end t))
  19144. (and (setq m (memq 'notregexp conditions))
  19145. (stringp (nth 1 m))
  19146. (not (re-search-forward (nth 1 m) end t))))
  19147. end)))
  19148. ;;;###autoload
  19149. (defun org-agenda-list-stuck-projects (&rest ignore)
  19150. "Create agenda view for projects that are stuck.
  19151. Stuck projects are project that have no next actions. For the definitions
  19152. of what a project is and how to check if it stuck, customize the variable
  19153. `org-stuck-projects'.
  19154. MATCH is being ignored."
  19155. (interactive)
  19156. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19157. ;; FIXME: we could have used org-agenda-skip-if here.
  19158. (org-agenda-overriding-header "List of stuck projects: ")
  19159. (matcher (nth 0 org-stuck-projects))
  19160. (todo (nth 1 org-stuck-projects))
  19161. (todo-wds (if (member "*" todo)
  19162. (progn
  19163. (org-prepare-agenda-buffers (org-agenda-files))
  19164. (org-delete-all
  19165. org-done-keywords-for-agenda
  19166. (copy-sequence org-todo-keywords-for-agenda)))
  19167. todo))
  19168. (todo-re (concat "^\\*+[ \t]+\\("
  19169. (mapconcat 'identity todo-wds "\\|")
  19170. "\\)\\>"))
  19171. (tags (nth 2 org-stuck-projects))
  19172. (tags-re (if (member "*" tags)
  19173. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19174. (concat "^\\*+ .*:\\("
  19175. (mapconcat 'identity tags "\\|")
  19176. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19177. (gen-re (nth 3 org-stuck-projects))
  19178. (re-list
  19179. (delq nil
  19180. (list
  19181. (if todo todo-re)
  19182. (if tags tags-re)
  19183. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19184. gen-re)))))
  19185. (setq org-agenda-skip-regexp
  19186. (if re-list
  19187. (mapconcat 'identity re-list "\\|")
  19188. (error "No information how to identify unstuck projects")))
  19189. (org-tags-view nil matcher)
  19190. (with-current-buffer org-agenda-buffer-name
  19191. (setq org-agenda-redo-command
  19192. '(org-agenda-list-stuck-projects
  19193. (or current-prefix-arg org-last-arg))))))
  19194. ;;; Diary integration
  19195. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19196. (defun org-get-entries-from-diary (date)
  19197. "Get the (Emacs Calendar) diary entries for DATE."
  19198. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19199. (diary-display-hook '(fancy-diary-display))
  19200. (pop-up-frames nil)
  19201. (list-diary-entries-hook
  19202. (cons 'org-diary-default-entry list-diary-entries-hook))
  19203. (diary-file-name-prefix-function nil) ; turn this feature off
  19204. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19205. entries
  19206. (org-disable-agenda-to-diary t))
  19207. (save-excursion
  19208. (save-window-excursion
  19209. (funcall (if (fboundp 'diary-list-entries)
  19210. 'diary-list-entries 'list-diary-entries)
  19211. date 1)))
  19212. (if (not (get-buffer fancy-diary-buffer))
  19213. (setq entries nil)
  19214. (with-current-buffer fancy-diary-buffer
  19215. (setq buffer-read-only nil)
  19216. (if (zerop (buffer-size))
  19217. ;; No entries
  19218. (setq entries nil)
  19219. ;; Omit the date and other unnecessary stuff
  19220. (org-agenda-cleanup-fancy-diary)
  19221. ;; Add prefix to each line and extend the text properties
  19222. (if (zerop (buffer-size))
  19223. (setq entries nil)
  19224. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19225. (set-buffer-modified-p nil)
  19226. (kill-buffer fancy-diary-buffer)))
  19227. (when entries
  19228. (setq entries (org-split-string entries "\n"))
  19229. (setq entries
  19230. (mapcar
  19231. (lambda (x)
  19232. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19233. ;; Extend the text properties to the beginning of the line
  19234. (org-add-props x (text-properties-at (1- (length x)) x)
  19235. 'type "diary" 'date date))
  19236. entries)))))
  19237. (defun org-agenda-cleanup-fancy-diary ()
  19238. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19239. This gets rid of the date, the underline under the date, and
  19240. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19241. date. It also removes lines that contain only whitespace."
  19242. (goto-char (point-min))
  19243. (if (looking-at ".*?:[ \t]*")
  19244. (progn
  19245. (replace-match "")
  19246. (re-search-forward "\n=+$" nil t)
  19247. (replace-match "")
  19248. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19249. (re-search-forward "\n=+$" nil t)
  19250. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19251. (goto-char (point-min))
  19252. (while (re-search-forward "^ +\n" nil t)
  19253. (replace-match ""))
  19254. (goto-char (point-min))
  19255. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19256. (replace-match "")))
  19257. ;; Make sure entries from the diary have the right text properties.
  19258. (eval-after-load "diary-lib"
  19259. '(if (boundp 'diary-modify-entry-list-string-function)
  19260. ;; We can rely on the hook, nothing to do
  19261. nil
  19262. ;; Hook not avaiable, must use advice to make this work
  19263. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19264. "Make the position visible."
  19265. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19266. (stringp string)
  19267. buffer-file-name)
  19268. (setq string (org-modify-diary-entry-string string))))))
  19269. (defun org-modify-diary-entry-string (string)
  19270. "Add text properties to string, allowing org-mode to act on it."
  19271. (org-add-props string nil
  19272. 'mouse-face 'highlight
  19273. 'keymap org-agenda-keymap
  19274. 'help-echo (if buffer-file-name
  19275. (format "mouse-2 or RET jump to diary file %s"
  19276. (abbreviate-file-name buffer-file-name))
  19277. "")
  19278. 'org-agenda-diary-link t
  19279. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19280. (defun org-diary-default-entry ()
  19281. "Add a dummy entry to the diary.
  19282. Needed to avoid empty dates which mess up holiday display."
  19283. ;; Catch the error if dealing with the new add-to-diary-alist
  19284. (when org-disable-agenda-to-diary
  19285. (condition-case nil
  19286. (add-to-diary-list original-date "Org-mode dummy" "")
  19287. (error
  19288. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19289. ;;;###autoload
  19290. (defun org-diary (&rest args)
  19291. "Return diary information from org-files.
  19292. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19293. It accesses org files and extracts information from those files to be
  19294. listed in the diary. The function accepts arguments specifying what
  19295. items should be listed. The following arguments are allowed:
  19296. :timestamp List the headlines of items containing a date stamp or
  19297. date range matching the selected date. Deadlines will
  19298. also be listed, on the expiration day.
  19299. :sexp List entries resulting from diary-like sexps.
  19300. :deadline List any deadlines past due, or due within
  19301. `org-deadline-warning-days'. The listing occurs only
  19302. in the diary for *today*, not at any other date. If
  19303. an entry is marked DONE, it is no longer listed.
  19304. :scheduled List all items which are scheduled for the given date.
  19305. The diary for *today* also contains items which were
  19306. scheduled earlier and are not yet marked DONE.
  19307. :todo List all TODO items from the org-file. This may be a
  19308. long list - so this is not turned on by default.
  19309. Like deadlines, these entries only show up in the
  19310. diary for *today*, not at any other date.
  19311. The call in the diary file should look like this:
  19312. &%%(org-diary) ~/path/to/some/orgfile.org
  19313. Use a separate line for each org file to check. Or, if you omit the file name,
  19314. all files listed in `org-agenda-files' will be checked automatically:
  19315. &%%(org-diary)
  19316. If you don't give any arguments (as in the example above), the default
  19317. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19318. So the example above may also be written as
  19319. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19320. The function expects the lisp variables `entry' and `date' to be provided
  19321. by the caller, because this is how the calendar works. Don't use this
  19322. function from a program - use `org-agenda-get-day-entries' instead."
  19323. (org-agenda-maybe-reset-markers)
  19324. (org-compile-prefix-format 'agenda)
  19325. (org-set-sorting-strategy 'agenda)
  19326. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19327. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19328. (list entry)
  19329. (org-agenda-files t)))
  19330. file rtn results)
  19331. (org-prepare-agenda-buffers files)
  19332. ;; If this is called during org-agenda, don't return any entries to
  19333. ;; the calendar. Org Agenda will list these entries itself.
  19334. (if org-disable-agenda-to-diary (setq files nil))
  19335. (while (setq file (pop files))
  19336. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19337. (setq results (append results rtn)))
  19338. (if results
  19339. (concat (org-finalize-agenda-entries results) "\n"))))
  19340. ;;; Agenda entry finders
  19341. (defun org-agenda-get-day-entries (file date &rest args)
  19342. "Does the work for `org-diary' and `org-agenda'.
  19343. FILE is the path to a file to be checked for entries. DATE is date like
  19344. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19345. which kind of entries should be extracted. For details about these, see
  19346. the documentation of `org-diary'."
  19347. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19348. (let* ((org-startup-folded nil)
  19349. (org-startup-align-all-tables nil)
  19350. (buffer (if (file-exists-p file)
  19351. (org-get-agenda-file-buffer file)
  19352. (error "No such file %s" file)))
  19353. arg results rtn)
  19354. (if (not buffer)
  19355. ;; If file does not exist, make sure an error message ends up in diary
  19356. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19357. (with-current-buffer buffer
  19358. (unless (org-mode-p)
  19359. (error "Agenda file %s is not in `org-mode'" file))
  19360. (let ((case-fold-search nil))
  19361. (save-excursion
  19362. (save-restriction
  19363. (if org-agenda-restrict
  19364. (narrow-to-region org-agenda-restrict-begin
  19365. org-agenda-restrict-end)
  19366. (widen))
  19367. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19368. (while (setq arg (pop args))
  19369. (cond
  19370. ((and (eq arg :todo)
  19371. (equal date (calendar-current-date)))
  19372. (setq rtn (org-agenda-get-todos))
  19373. (setq results (append results rtn)))
  19374. ((eq arg :timestamp)
  19375. (setq rtn (org-agenda-get-blocks))
  19376. (setq results (append results rtn))
  19377. (setq rtn (org-agenda-get-timestamps))
  19378. (setq results (append results rtn)))
  19379. ((eq arg :sexp)
  19380. (setq rtn (org-agenda-get-sexps))
  19381. (setq results (append results rtn)))
  19382. ((eq arg :scheduled)
  19383. (setq rtn (org-agenda-get-scheduled))
  19384. (setq results (append results rtn)))
  19385. ((eq arg :closed)
  19386. (setq rtn (org-agenda-get-closed))
  19387. (setq results (append results rtn)))
  19388. ((eq arg :deadline)
  19389. (setq rtn (org-agenda-get-deadlines))
  19390. (setq results (append results rtn))))))))
  19391. results))))
  19392. (defun org-entry-is-todo-p ()
  19393. (member (org-get-todo-state) org-not-done-keywords))
  19394. (defun org-entry-is-done-p ()
  19395. (member (org-get-todo-state) org-done-keywords))
  19396. (defun org-get-todo-state ()
  19397. (save-excursion
  19398. (org-back-to-heading t)
  19399. (and (looking-at org-todo-line-regexp)
  19400. (match-end 2)
  19401. (match-string 2))))
  19402. (defun org-at-date-range-p (&optional inactive-ok)
  19403. "Is the cursor inside a date range?"
  19404. (interactive)
  19405. (save-excursion
  19406. (catch 'exit
  19407. (let ((pos (point)))
  19408. (skip-chars-backward "^[<\r\n")
  19409. (skip-chars-backward "<[")
  19410. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19411. (>= (match-end 0) pos)
  19412. (throw 'exit t))
  19413. (skip-chars-backward "^<[\r\n")
  19414. (skip-chars-backward "<[")
  19415. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19416. (>= (match-end 0) pos)
  19417. (throw 'exit t)))
  19418. nil)))
  19419. (defun org-agenda-get-todos ()
  19420. "Return the TODO information for agenda display."
  19421. (let* ((props (list 'face nil
  19422. 'done-face 'org-done
  19423. 'org-not-done-regexp org-not-done-regexp
  19424. 'org-todo-regexp org-todo-regexp
  19425. 'mouse-face 'highlight
  19426. 'keymap org-agenda-keymap
  19427. 'help-echo
  19428. (format "mouse-2 or RET jump to org file %s"
  19429. (abbreviate-file-name buffer-file-name))))
  19430. ;; FIXME: get rid of the \n at some point but watch out
  19431. (regexp (concat "^\\*+[ \t]+\\("
  19432. (if org-select-this-todo-keyword
  19433. (if (equal org-select-this-todo-keyword "*")
  19434. org-todo-regexp
  19435. (concat "\\<\\("
  19436. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19437. "\\)\\>"))
  19438. org-not-done-regexp)
  19439. "[^\n\r]*\\)"))
  19440. marker priority category tags
  19441. ee txt beg end)
  19442. (goto-char (point-min))
  19443. (while (re-search-forward regexp nil t)
  19444. (catch :skip
  19445. (save-match-data
  19446. (beginning-of-line)
  19447. (setq beg (point) end (progn (outline-next-heading) (point)))
  19448. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19449. (re-search-forward org-ts-regexp end t))
  19450. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19451. (re-search-forward org-scheduled-time-regexp end t))
  19452. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19453. (re-search-forward org-deadline-time-regexp end t)
  19454. (org-deadline-close (match-string 1))))
  19455. (goto-char (1+ beg))
  19456. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19457. (throw :skip nil)))
  19458. (goto-char beg)
  19459. (org-agenda-skip)
  19460. (goto-char (match-beginning 1))
  19461. (setq marker (org-agenda-new-marker (match-beginning 0))
  19462. category (org-get-category)
  19463. tags (org-get-tags-at (point))
  19464. txt (org-format-agenda-item "" (match-string 1) category tags)
  19465. priority (1+ (org-get-priority txt)))
  19466. (org-add-props txt props
  19467. 'org-marker marker 'org-hd-marker marker
  19468. 'priority priority 'org-category category
  19469. 'type "todo")
  19470. (push txt ee)
  19471. (if org-agenda-todo-list-sublevels
  19472. (goto-char (match-end 1))
  19473. (org-end-of-subtree 'invisible))))
  19474. (nreverse ee)))
  19475. (defconst org-agenda-no-heading-message
  19476. "No heading for this item in buffer or region.")
  19477. (defun org-agenda-get-timestamps ()
  19478. "Return the date stamp information for agenda display."
  19479. (let* ((props (list 'face nil
  19480. 'org-not-done-regexp org-not-done-regexp
  19481. 'org-todo-regexp org-todo-regexp
  19482. 'mouse-face 'highlight
  19483. 'keymap org-agenda-keymap
  19484. 'help-echo
  19485. (format "mouse-2 or RET jump to org file %s"
  19486. (abbreviate-file-name buffer-file-name))))
  19487. (d1 (calendar-absolute-from-gregorian date))
  19488. (remove-re
  19489. (concat
  19490. (regexp-quote
  19491. (format-time-string
  19492. "<%Y-%m-%d"
  19493. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19494. ".*?>"))
  19495. (regexp
  19496. (concat
  19497. (regexp-quote
  19498. (substring
  19499. (format-time-string
  19500. (car org-time-stamp-formats)
  19501. (apply 'encode-time ; DATE bound by calendar
  19502. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19503. 0 11))
  19504. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19505. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19506. marker hdmarker deadlinep scheduledp donep tmp priority category
  19507. ee txt timestr tags b0 b3 e3 head)
  19508. (goto-char (point-min))
  19509. (while (re-search-forward regexp nil t)
  19510. (setq b0 (match-beginning 0)
  19511. b3 (match-beginning 3) e3 (match-end 3))
  19512. (catch :skip
  19513. (and (org-at-date-range-p) (throw :skip nil))
  19514. (org-agenda-skip)
  19515. (if (and (match-end 1)
  19516. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19517. (throw :skip nil))
  19518. (if (and e3
  19519. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19520. (throw :skip nil))
  19521. (setq marker (org-agenda-new-marker b0)
  19522. category (org-get-category b0)
  19523. tmp (buffer-substring (max (point-min)
  19524. (- b0 org-ds-keyword-length))
  19525. b0)
  19526. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19527. deadlinep (string-match org-deadline-regexp tmp)
  19528. scheduledp (string-match org-scheduled-regexp tmp)
  19529. donep (org-entry-is-done-p))
  19530. (if (or scheduledp deadlinep) (throw :skip t))
  19531. (if (string-match ">" timestr)
  19532. ;; substring should only run to end of time stamp
  19533. (setq timestr (substring timestr 0 (match-end 0))))
  19534. (save-excursion
  19535. (if (re-search-backward "^\\*+ " nil t)
  19536. (progn
  19537. (goto-char (match-beginning 0))
  19538. (setq hdmarker (org-agenda-new-marker)
  19539. tags (org-get-tags-at))
  19540. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19541. (setq head (match-string 1))
  19542. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19543. (setq txt (org-format-agenda-item
  19544. nil head category tags timestr nil
  19545. remove-re)))
  19546. (setq txt org-agenda-no-heading-message))
  19547. (setq priority (org-get-priority txt))
  19548. (org-add-props txt props
  19549. 'org-marker marker 'org-hd-marker hdmarker)
  19550. (org-add-props txt nil 'priority priority
  19551. 'org-category category 'date date
  19552. 'type "timestamp")
  19553. (push txt ee))
  19554. (outline-next-heading)))
  19555. (nreverse ee)))
  19556. (defun org-agenda-get-sexps ()
  19557. "Return the sexp information for agenda display."
  19558. (require 'diary-lib)
  19559. (let* ((props (list 'face nil
  19560. 'mouse-face 'highlight
  19561. 'keymap org-agenda-keymap
  19562. 'help-echo
  19563. (format "mouse-2 or RET jump to org file %s"
  19564. (abbreviate-file-name buffer-file-name))))
  19565. (regexp "^&?%%(")
  19566. marker category ee txt tags entry result beg b sexp sexp-entry)
  19567. (goto-char (point-min))
  19568. (while (re-search-forward regexp nil t)
  19569. (catch :skip
  19570. (org-agenda-skip)
  19571. (setq beg (match-beginning 0))
  19572. (goto-char (1- (match-end 0)))
  19573. (setq b (point))
  19574. (forward-sexp 1)
  19575. (setq sexp (buffer-substring b (point)))
  19576. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19577. (org-trim (match-string 1))
  19578. ""))
  19579. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19580. (when result
  19581. (setq marker (org-agenda-new-marker beg)
  19582. category (org-get-category beg))
  19583. (if (string-match "\\S-" result)
  19584. (setq txt result)
  19585. (setq txt "SEXP entry returned empty string"))
  19586. (setq txt (org-format-agenda-item
  19587. "" txt category tags 'time))
  19588. (org-add-props txt props 'org-marker marker)
  19589. (org-add-props txt nil
  19590. 'org-category category 'date date
  19591. 'type "sexp")
  19592. (push txt ee))))
  19593. (nreverse ee)))
  19594. (defun org-agenda-get-closed ()
  19595. "Return the logged TODO entries for agenda display."
  19596. (let* ((props (list 'mouse-face 'highlight
  19597. 'org-not-done-regexp org-not-done-regexp
  19598. 'org-todo-regexp org-todo-regexp
  19599. 'keymap org-agenda-keymap
  19600. 'help-echo
  19601. (format "mouse-2 or RET jump to org file %s"
  19602. (abbreviate-file-name buffer-file-name))))
  19603. (regexp (concat
  19604. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19605. (regexp-quote
  19606. (substring
  19607. (format-time-string
  19608. (car org-time-stamp-formats)
  19609. (apply 'encode-time ; DATE bound by calendar
  19610. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19611. 1 11))))
  19612. marker hdmarker priority category tags closedp
  19613. ee txt timestr)
  19614. (goto-char (point-min))
  19615. (while (re-search-forward regexp nil t)
  19616. (catch :skip
  19617. (org-agenda-skip)
  19618. (setq marker (org-agenda-new-marker (match-beginning 0))
  19619. closedp (equal (match-string 1) org-closed-string)
  19620. category (org-get-category (match-beginning 0))
  19621. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19622. ;; donep (org-entry-is-done-p)
  19623. )
  19624. (if (string-match "\\]" timestr)
  19625. ;; substring should only run to end of time stamp
  19626. (setq timestr (substring timestr 0 (match-end 0))))
  19627. (save-excursion
  19628. (if (re-search-backward "^\\*+ " nil t)
  19629. (progn
  19630. (goto-char (match-beginning 0))
  19631. (setq hdmarker (org-agenda-new-marker)
  19632. tags (org-get-tags-at))
  19633. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19634. (setq txt (org-format-agenda-item
  19635. (if closedp "Closed: " "Clocked: ")
  19636. (match-string 1) category tags timestr)))
  19637. (setq txt org-agenda-no-heading-message))
  19638. (setq priority 100000)
  19639. (org-add-props txt props
  19640. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19641. 'priority priority 'org-category category
  19642. 'type "closed" 'date date
  19643. 'undone-face 'org-warning 'done-face 'org-done)
  19644. (push txt ee))
  19645. (goto-char (point-at-eol))))
  19646. ; (outline-next-heading)))
  19647. (nreverse ee)))
  19648. (defun org-agenda-get-deadlines ()
  19649. "Return the deadline information for agenda display."
  19650. (let* ((props (list 'mouse-face 'highlight
  19651. 'org-not-done-regexp org-not-done-regexp
  19652. 'org-todo-regexp org-todo-regexp
  19653. 'keymap org-agenda-keymap
  19654. 'help-echo
  19655. (format "mouse-2 or RET jump to org file %s"
  19656. (abbreviate-file-name buffer-file-name))))
  19657. (regexp org-deadline-time-regexp)
  19658. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19659. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19660. d2 diff dfrac wdays pos pos1 category tags
  19661. ee txt head face s upcomingp donep timestr)
  19662. (goto-char (point-min))
  19663. (while (re-search-forward regexp nil t)
  19664. (catch :skip
  19665. (org-agenda-skip)
  19666. (setq s (match-string 1)
  19667. pos (1- (match-beginning 1))
  19668. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19669. diff (- d2 d1)
  19670. wdays (org-get-wdays s)
  19671. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  19672. upcomingp (and todayp (> diff 0)))
  19673. ;; When to show a deadline in the calendar:
  19674. ;; If the expiration is within wdays warning time.
  19675. ;; Past-due deadlines are only shown on the current date
  19676. (if (or (and (<= diff wdays)
  19677. (and todayp (not org-agenda-only-exact-dates)))
  19678. (= diff 0))
  19679. (save-excursion
  19680. (setq category (org-get-category))
  19681. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19682. (progn
  19683. (goto-char (match-end 0))
  19684. (setq pos1 (match-beginning 0))
  19685. (setq tags (org-get-tags-at pos1))
  19686. (setq head (buffer-substring-no-properties
  19687. (point)
  19688. (progn (skip-chars-forward "^\r\n")
  19689. (point))))
  19690. (setq donep (string-match org-looking-at-done-regexp head))
  19691. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19692. (setq timestr
  19693. (concat (substring s (match-beginning 1)) " "))
  19694. (setq timestr 'time))
  19695. (if (and donep
  19696. (or org-agenda-skip-deadline-if-done
  19697. (not (= diff 0))))
  19698. (setq txt nil)
  19699. (setq txt (org-format-agenda-item
  19700. (if (= diff 0)
  19701. (car org-agenda-deadline-leaders)
  19702. (format (nth 1 org-agenda-deadline-leaders)
  19703. diff))
  19704. head category tags timestr))))
  19705. (setq txt org-agenda-no-heading-message))
  19706. (when txt
  19707. (setq face (org-agenda-deadline-face dfrac))
  19708. (org-add-props txt props
  19709. 'org-marker (org-agenda-new-marker pos)
  19710. 'org-hd-marker (org-agenda-new-marker pos1)
  19711. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  19712. (org-get-priority txt))
  19713. 'org-category category
  19714. 'type (if upcomingp "upcoming-deadline" "deadline")
  19715. 'date (if upcomingp date d2)
  19716. 'face (if donep 'org-done face)
  19717. 'undone-face face 'done-face 'org-done)
  19718. (push txt ee))))))
  19719. (nreverse ee)))
  19720. (defun org-agenda-deadline-face (fraction)
  19721. "Return the face to displaying a deadline item.
  19722. FRACTION is what fraction of the head-warning time has passed."
  19723. (let ((faces org-agenda-deadline-faces) f)
  19724. (catch 'exit
  19725. (while (setq f (pop faces))
  19726. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19727. (defun org-agenda-get-scheduled ()
  19728. "Return the scheduled information for agenda display."
  19729. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19730. 'org-todo-regexp org-todo-regexp
  19731. 'done-face 'org-done
  19732. 'mouse-face 'highlight
  19733. 'keymap org-agenda-keymap
  19734. 'help-echo
  19735. (format "mouse-2 or RET jump to org file %s"
  19736. (abbreviate-file-name buffer-file-name))))
  19737. (regexp org-scheduled-time-regexp)
  19738. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19739. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19740. d2 diff pos pos1 category tags
  19741. ee txt head pastschedp donep face timestr s)
  19742. (goto-char (point-min))
  19743. (while (re-search-forward regexp nil t)
  19744. (catch :skip
  19745. (org-agenda-skip)
  19746. (setq s (match-string 1)
  19747. pos (1- (match-beginning 1))
  19748. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19749. ;;; is this right?
  19750. ;;; do we need to do this for deadleine too????
  19751. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  19752. diff (- d2 d1))
  19753. (setq pastschedp (and todayp (< diff 0)))
  19754. ;; When to show a scheduled item in the calendar:
  19755. ;; If it is on or past the date.
  19756. (if (or (and (< diff 0)
  19757. (and todayp (not org-agenda-only-exact-dates)))
  19758. (= diff 0))
  19759. (save-excursion
  19760. (setq category (org-get-category))
  19761. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19762. (progn
  19763. (goto-char (match-end 0))
  19764. (setq pos1 (match-beginning 0))
  19765. (setq tags (org-get-tags-at))
  19766. (setq head (buffer-substring-no-properties
  19767. (point)
  19768. (progn (skip-chars-forward "^\r\n") (point))))
  19769. (setq donep (string-match org-looking-at-done-regexp head))
  19770. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19771. (setq timestr
  19772. (concat (substring s (match-beginning 1)) " "))
  19773. (setq timestr 'time))
  19774. (if (and donep
  19775. (or org-agenda-skip-scheduled-if-done
  19776. (not (= diff 0))))
  19777. (setq txt nil)
  19778. (setq txt (org-format-agenda-item
  19779. (if (= diff 0)
  19780. (car org-agenda-scheduled-leaders)
  19781. (format (nth 1 org-agenda-scheduled-leaders)
  19782. (- 1 diff)))
  19783. head category tags timestr))))
  19784. (setq txt org-agenda-no-heading-message))
  19785. (when txt
  19786. (setq face (if pastschedp
  19787. 'org-scheduled-previously
  19788. 'org-scheduled-today))
  19789. (org-add-props txt props
  19790. 'undone-face face
  19791. 'face (if donep 'org-done face)
  19792. 'org-marker (org-agenda-new-marker pos)
  19793. 'org-hd-marker (org-agenda-new-marker pos1)
  19794. 'type (if pastschedp "past-scheduled" "scheduled")
  19795. 'date (if pastschedp d2 date)
  19796. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19797. 'org-category category)
  19798. (push txt ee))))))
  19799. (nreverse ee)))
  19800. (defun org-agenda-get-blocks ()
  19801. "Return the date-range information for agenda display."
  19802. (let* ((props (list 'face nil
  19803. 'org-not-done-regexp org-not-done-regexp
  19804. 'org-todo-regexp org-todo-regexp
  19805. 'mouse-face 'highlight
  19806. 'keymap org-agenda-keymap
  19807. 'help-echo
  19808. (format "mouse-2 or RET jump to org file %s"
  19809. (abbreviate-file-name buffer-file-name))))
  19810. (regexp org-tr-regexp)
  19811. (d0 (calendar-absolute-from-gregorian date))
  19812. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19813. donep head)
  19814. (goto-char (point-min))
  19815. (while (re-search-forward regexp nil t)
  19816. (catch :skip
  19817. (org-agenda-skip)
  19818. (setq pos (point))
  19819. (setq timestr (match-string 0)
  19820. s1 (match-string 1)
  19821. s2 (match-string 2)
  19822. d1 (time-to-days (org-time-string-to-time s1))
  19823. d2 (time-to-days (org-time-string-to-time s2)))
  19824. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19825. ;; Only allow days between the limits, because the normal
  19826. ;; date stamps will catch the limits.
  19827. (save-excursion
  19828. (setq marker (org-agenda-new-marker (point)))
  19829. (setq category (org-get-category))
  19830. (if (re-search-backward "^\\*+ " nil t)
  19831. (progn
  19832. (goto-char (match-beginning 0))
  19833. (setq hdmarker (org-agenda-new-marker (point)))
  19834. (setq tags (org-get-tags-at))
  19835. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19836. (setq head (match-string 1))
  19837. (and org-agenda-skip-timestamp-if-done
  19838. (org-entry-is-done-p)
  19839. (throw :skip t))
  19840. (setq txt (org-format-agenda-item
  19841. (format (if (= d1 d2) "" "(%d/%d): ")
  19842. (1+ (- d0 d1)) (1+ (- d2 d1)))
  19843. head category tags
  19844. (if (= d0 d1) timestr))))
  19845. (setq txt org-agenda-no-heading-message))
  19846. (org-add-props txt props
  19847. 'org-marker marker 'org-hd-marker hdmarker
  19848. 'type "block" 'date date
  19849. 'priority (org-get-priority txt) 'org-category category)
  19850. (push txt ee)))
  19851. (goto-char pos)))
  19852. ;; Sort the entries by expiration date.
  19853. (nreverse ee)))
  19854. ;;; Agenda presentation and sorting
  19855. (defconst org-plain-time-of-day-regexp
  19856. (concat
  19857. "\\(\\<[012]?[0-9]"
  19858. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19859. "\\(--?"
  19860. "\\(\\<[012]?[0-9]"
  19861. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19862. "\\)?")
  19863. "Regular expression to match a plain time or time range.
  19864. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19865. groups carry important information:
  19866. 0 the full match
  19867. 1 the first time, range or not
  19868. 8 the second time, if it is a range.")
  19869. (defconst org-plain-time-extension-regexp
  19870. (concat
  19871. "\\(\\<[012]?[0-9]"
  19872. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19873. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  19874. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  19875. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19876. groups carry important information:
  19877. 0 the full match
  19878. 7 hours of duration
  19879. 9 minutes of duration")
  19880. (defconst org-stamp-time-of-day-regexp
  19881. (concat
  19882. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  19883. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  19884. "\\(--?"
  19885. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  19886. "Regular expression to match a timestamp time or time range.
  19887. After a match, the following groups carry important information:
  19888. 0 the full match
  19889. 1 date plus weekday, for backreferencing to make sure both times on same day
  19890. 2 the first time, range or not
  19891. 4 the second time, if it is a range.")
  19892. (defvar org-prefix-has-time nil
  19893. "A flag, set by `org-compile-prefix-format'.
  19894. The flag is set if the currently compiled format contains a `%t'.")
  19895. (defvar org-prefix-has-tag nil
  19896. "A flag, set by `org-compile-prefix-format'.
  19897. The flag is set if the currently compiled format contains a `%T'.")
  19898. (defun org-format-agenda-item (extra txt &optional category tags dotime
  19899. noprefix remove-re)
  19900. "Format TXT to be inserted into the agenda buffer.
  19901. In particular, it adds the prefix and corresponding text properties. EXTRA
  19902. must be a string and replaces the `%s' specifier in the prefix format.
  19903. CATEGORY (string, symbol or nil) may be used to overrule the default
  19904. category taken from local variable or file name. It will replace the `%c'
  19905. specifier in the format. DOTIME, when non-nil, indicates that a
  19906. time-of-day should be extracted from TXT for sorting of this entry, and for
  19907. the `%t' specifier in the format. When DOTIME is a string, this string is
  19908. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  19909. only the correctly processes TXT should be returned - this is used by
  19910. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  19911. Any match of REMOVE-RE will be removed from TXT."
  19912. (save-match-data
  19913. ;; Diary entries sometimes have extra whitespace at the beginning
  19914. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  19915. (let* ((category (or category
  19916. org-category
  19917. (if buffer-file-name
  19918. (file-name-sans-extension
  19919. (file-name-nondirectory buffer-file-name))
  19920. "")))
  19921. (tag (if tags (nth (1- (length tags)) tags) ""))
  19922. time ; time and tag are needed for the eval of the prefix format
  19923. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  19924. (time-of-day (and dotime (org-get-time-of-day ts)))
  19925. stamp plain s0 s1 s2 rtn srp)
  19926. (when (and dotime time-of-day org-prefix-has-time)
  19927. ;; Extract starting and ending time and move them to prefix
  19928. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  19929. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  19930. (setq s0 (match-string 0 ts)
  19931. srp (and stamp (match-end 3))
  19932. s1 (match-string (if plain 1 2) ts)
  19933. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  19934. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  19935. ;; them, we might want to remove them there to avoid duplication.
  19936. ;; The user can turn this off with a variable.
  19937. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  19938. (string-match (concat (regexp-quote s0) " *") txt)
  19939. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  19940. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  19941. (= (match-beginning 0) 0)
  19942. t))
  19943. (setq txt (replace-match "" nil nil txt))))
  19944. ;; Normalize the time(s) to 24 hour
  19945. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  19946. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  19947. (when (and s1 (not s2) org-agenda-default-appointment-duration
  19948. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  19949. (let ((m (+ (string-to-number (match-string 2 s1))
  19950. (* 60 (string-to-number (match-string 1 s1)))
  19951. org-agenda-default-appointment-duration))
  19952. h)
  19953. (setq h (/ m 60) m (- m (* h 60)))
  19954. (setq s2 (format "%02d:%02d" h m))))
  19955. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  19956. txt)
  19957. ;; Tags are in the string
  19958. (if (or (eq org-agenda-remove-tags t)
  19959. (and org-agenda-remove-tags
  19960. org-prefix-has-tag))
  19961. (setq txt (replace-match "" t t txt))
  19962. (setq txt (replace-match
  19963. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  19964. (match-string 2 txt))
  19965. t t txt))))
  19966. (when remove-re
  19967. (while (string-match remove-re txt)
  19968. (setq txt (replace-match "" t t txt))))
  19969. ;; Create the final string
  19970. (if noprefix
  19971. (setq rtn txt)
  19972. ;; Prepare the variables needed in the eval of the compiled format
  19973. (setq time (cond (s2 (concat s1 "-" s2))
  19974. (s1 (concat s1 "......"))
  19975. (t ""))
  19976. extra (or extra "")
  19977. category (if (symbolp category) (symbol-name category) category))
  19978. ;; Evaluate the compiled format
  19979. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  19980. ;; And finally add the text properties
  19981. (org-add-props rtn nil
  19982. 'org-category (downcase category) 'tags tags
  19983. 'org-highest-priority org-highest-priority
  19984. 'org-lowest-priority org-lowest-priority
  19985. 'prefix-length (- (length rtn) (length txt))
  19986. 'time-of-day time-of-day
  19987. 'txt txt
  19988. 'time time
  19989. 'extra extra
  19990. 'dotime dotime))))
  19991. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  19992. (defvar org-agenda-sorting-strategy-selected nil)
  19993. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  19994. (catch 'exit
  19995. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  19996. ((and todayp (member 'today (car org-agenda-time-grid))))
  19997. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  19998. ((member 'weekly (car org-agenda-time-grid)))
  19999. (t (throw 'exit list)))
  20000. (let* ((have (delq nil (mapcar
  20001. (lambda (x) (get-text-property 1 'time-of-day x))
  20002. list)))
  20003. (string (nth 1 org-agenda-time-grid))
  20004. (gridtimes (nth 2 org-agenda-time-grid))
  20005. (req (car org-agenda-time-grid))
  20006. (remove (member 'remove-match req))
  20007. new time)
  20008. (if (and (member 'require-timed req) (not have))
  20009. ;; don't show empty grid
  20010. (throw 'exit list))
  20011. (while (setq time (pop gridtimes))
  20012. (unless (and remove (member time have))
  20013. (setq time (int-to-string time))
  20014. (push (org-format-agenda-item
  20015. nil string "" nil
  20016. (concat (substring time 0 -2) ":" (substring time -2)))
  20017. new)
  20018. (put-text-property
  20019. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20020. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20021. (append new list)
  20022. (append list new)))))
  20023. (defun org-compile-prefix-format (key)
  20024. "Compile the prefix format into a Lisp form that can be evaluated.
  20025. The resulting form is returned and stored in the variable
  20026. `org-prefix-format-compiled'."
  20027. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20028. (let ((s (cond
  20029. ((stringp org-agenda-prefix-format)
  20030. org-agenda-prefix-format)
  20031. ((assq key org-agenda-prefix-format)
  20032. (cdr (assq key org-agenda-prefix-format)))
  20033. (t " %-12:c%?-12t% s")))
  20034. (start 0)
  20035. varform vars var e c f opt)
  20036. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20037. s start)
  20038. (setq var (cdr (assoc (match-string 4 s)
  20039. '(("c" . category) ("t" . time) ("s" . extra)
  20040. ("T" . tag))))
  20041. c (or (match-string 3 s) "")
  20042. opt (match-beginning 1)
  20043. start (1+ (match-beginning 0)))
  20044. (if (equal var 'time) (setq org-prefix-has-time t))
  20045. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20046. (setq f (concat "%" (match-string 2 s) "s"))
  20047. (if opt
  20048. (setq varform
  20049. `(if (equal "" ,var)
  20050. ""
  20051. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20052. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20053. (setq s (replace-match "%s" t nil s))
  20054. (push varform vars))
  20055. (setq vars (nreverse vars))
  20056. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20057. (defun org-set-sorting-strategy (key)
  20058. (if (symbolp (car org-agenda-sorting-strategy))
  20059. ;; the old format
  20060. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20061. (setq org-agenda-sorting-strategy-selected
  20062. (or (cdr (assq key org-agenda-sorting-strategy))
  20063. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20064. '(time-up category-keep priority-down)))))
  20065. (defun org-get-time-of-day (s &optional string mod24)
  20066. "Check string S for a time of day.
  20067. If found, return it as a military time number between 0 and 2400.
  20068. If not found, return nil.
  20069. The optional STRING argument forces conversion into a 5 character wide string
  20070. HH:MM."
  20071. (save-match-data
  20072. (when
  20073. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20074. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20075. (let* ((h (string-to-number (match-string 1 s)))
  20076. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20077. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20078. (am-p (equal ampm "am"))
  20079. (h1 (cond ((not ampm) h)
  20080. ((= h 12) (if am-p 0 12))
  20081. (t (+ h (if am-p 0 12)))))
  20082. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20083. (mod h1 24) h1))
  20084. (t0 (+ (* 100 h2) m))
  20085. (t1 (concat (if (>= h1 24) "+" " ")
  20086. (if (< t0 100) "0" "")
  20087. (if (< t0 10) "0" "")
  20088. (int-to-string t0))))
  20089. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20090. (defun org-finalize-agenda-entries (list &optional nosort)
  20091. "Sort and concatenate the agenda items."
  20092. (setq list (mapcar 'org-agenda-highlight-todo list))
  20093. (if nosort
  20094. list
  20095. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20096. (defun org-agenda-highlight-todo (x)
  20097. (let (re pl)
  20098. (if (eq x 'line)
  20099. (save-excursion
  20100. (beginning-of-line 1)
  20101. (setq re (get-text-property (point) 'org-todo-regexp))
  20102. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20103. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20104. (add-text-properties (match-beginning 0) (match-end 0)
  20105. (list 'face (org-get-todo-face 0)))
  20106. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20107. (delete-region (match-beginning 1) (1- (match-end 0)))
  20108. (goto-char (match-beginning 1))
  20109. (insert (format org-agenda-todo-keyword-format s)))))
  20110. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20111. pl (get-text-property 0 'prefix-length x))
  20112. ; (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  20113. ; (add-text-properties
  20114. ; (or (match-end 1) (match-end 0)) (match-end 0)
  20115. ; (list 'face (org-get-todo-face (match-string 2 x)))
  20116. ; x))
  20117. (when (and re
  20118. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20119. x (or pl 0)) pl))
  20120. (add-text-properties
  20121. (or (match-end 1) (match-end 0)) (match-end 0)
  20122. (list 'face (org-get-todo-face (match-string 2 x)))
  20123. x)
  20124. (setq x (concat (substring x 0 (match-end 1))
  20125. (format org-agenda-todo-keyword-format
  20126. (match-string 2 x))
  20127. " "
  20128. (substring x (match-end 3)))))
  20129. x)))
  20130. (defsubst org-cmp-priority (a b)
  20131. "Compare the priorities of string A and B."
  20132. (let ((pa (or (get-text-property 1 'priority a) 0))
  20133. (pb (or (get-text-property 1 'priority b) 0)))
  20134. (cond ((> pa pb) +1)
  20135. ((< pa pb) -1)
  20136. (t nil))))
  20137. (defsubst org-cmp-category (a b)
  20138. "Compare the string values of categories of strings A and B."
  20139. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20140. (cb (or (get-text-property 1 'org-category b) "")))
  20141. (cond ((string-lessp ca cb) -1)
  20142. ((string-lessp cb ca) +1)
  20143. (t nil))))
  20144. (defsubst org-cmp-tag (a b)
  20145. "Compare the string values of categories of strings A and B."
  20146. (let ((ta (car (last (get-text-property 1 'tags a))))
  20147. (tb (car (last (get-text-property 1 'tags b)))))
  20148. (cond ((not ta) +1)
  20149. ((not tb) -1)
  20150. ((string-lessp ta tb) -1)
  20151. ((string-lessp tb ta) +1)
  20152. (t nil))))
  20153. (defsubst org-cmp-time (a b)
  20154. "Compare the time-of-day values of strings A and B."
  20155. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20156. (ta (or (get-text-property 1 'time-of-day a) def))
  20157. (tb (or (get-text-property 1 'time-of-day b) def)))
  20158. (cond ((< ta tb) -1)
  20159. ((< tb ta) +1)
  20160. (t nil))))
  20161. (defun org-entries-lessp (a b)
  20162. "Predicate for sorting agenda entries."
  20163. ;; The following variables will be used when the form is evaluated.
  20164. ;; So even though the compiler complains, keep them.
  20165. (let* ((time-up (org-cmp-time a b))
  20166. (time-down (if time-up (- time-up) nil))
  20167. (priority-up (org-cmp-priority a b))
  20168. (priority-down (if priority-up (- priority-up) nil))
  20169. (category-up (org-cmp-category a b))
  20170. (category-down (if category-up (- category-up) nil))
  20171. (category-keep (if category-up +1 nil))
  20172. (tag-up (org-cmp-tag a b))
  20173. (tag-down (if tag-up (- tag-up) nil)))
  20174. (cdr (assoc
  20175. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20176. '((-1 . t) (1 . nil) (nil . nil))))))
  20177. ;;; Agenda restriction lock
  20178. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20179. "Overlay to mark the headline to which arenda commands are restricted.")
  20180. (org-overlay-put org-agenda-restriction-lock-overlay
  20181. 'face 'org-agenda-restriction-lock)
  20182. (org-overlay-put org-agenda-restriction-lock-overlay
  20183. 'help-echo "Agendas are currently limited to this subtree.")
  20184. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20185. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20186. "Overlay marking the agenda restriction line in speedbar.")
  20187. (org-overlay-put org-speedbar-restriction-lock-overlay
  20188. 'face 'org-agenda-restriction-lock)
  20189. (org-overlay-put org-speedbar-restriction-lock-overlay
  20190. 'help-echo "Agendas are currently limited to this item.")
  20191. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20192. (defun org-agenda-set-restriction-lock (&optional type)
  20193. "Set restriction lock for agenda, to current subtree or file.
  20194. Restriction will be the file if TYPE is `file', or if type is the
  20195. universal prefix '(4), or if the cursor is before the first headline
  20196. in the file. Otherwise, restriction will be to the current subtree."
  20197. (interactive "P")
  20198. (and (equal type '(4)) (setq type 'file))
  20199. (setq type (cond
  20200. (type type)
  20201. ((org-at-heading-p) 'subtree)
  20202. ((condition-case nil (org-back-to-heading t) (error nil))
  20203. 'subtree)
  20204. (t 'file)))
  20205. (if (eq type 'subtree)
  20206. (progn
  20207. (setq org-agenda-restrict t)
  20208. (setq org-agenda-overriding-restriction 'subtree)
  20209. (put 'org-agenda-files 'org-restrict
  20210. (list (buffer-file-name (buffer-base-buffer))))
  20211. (org-back-to-heading t)
  20212. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20213. (move-marker org-agenda-restrict-begin (point))
  20214. (move-marker org-agenda-restrict-end
  20215. (save-excursion (org-end-of-subtree t)))
  20216. (message "Locking agenda restriction to subtree"))
  20217. (put 'org-agenda-files 'org-restrict
  20218. (list (buffer-file-name (buffer-base-buffer))))
  20219. (setq org-agenda-restrict nil)
  20220. (setq org-agenda-overriding-restriction 'file)
  20221. (move-marker org-agenda-restrict-begin nil)
  20222. (move-marker org-agenda-restrict-end nil)
  20223. (message "Locking agenda restriction to file"))
  20224. (setq current-prefix-arg nil)
  20225. (org-agenda-maybe-redo))
  20226. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20227. "Remove the agenda restriction lock."
  20228. (interactive "P")
  20229. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20230. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20231. (setq org-agenda-overriding-restriction nil)
  20232. (setq org-agenda-restrict nil)
  20233. (put 'org-agenda-files 'org-restrict nil)
  20234. (move-marker org-agenda-restrict-begin nil)
  20235. (move-marker org-agenda-restrict-end nil)
  20236. (setq current-prefix-arg nil)
  20237. (message "Agenda restriction lock removed")
  20238. (or noupdate (org-agenda-maybe-redo)))
  20239. (defun org-agenda-maybe-redo ()
  20240. "If there is any window showing the agenda view, update it."
  20241. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20242. (w0 (selected-window)))
  20243. (when w
  20244. (select-window w)
  20245. (org-agenda-redo)
  20246. (select-window w0)
  20247. (if org-agenda-overriding-restriction
  20248. (message "Agenda view shifted to new %s restriction"
  20249. org-agenda-overriding-restriction)
  20250. (message "Agenda restriction lock removed")))))
  20251. ;;; Agenda commands
  20252. (defun org-agenda-check-type (error &rest types)
  20253. "Check if agenda buffer is of allowed type.
  20254. If ERROR is non-nil, throw an error, otherwise just return nil."
  20255. (if (memq org-agenda-type types)
  20256. t
  20257. (if error
  20258. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20259. nil)))
  20260. (defun org-agenda-quit ()
  20261. "Exit agenda by removing the window or the buffer."
  20262. (interactive)
  20263. (let ((buf (current-buffer)))
  20264. (if (not (one-window-p)) (delete-window))
  20265. (kill-buffer buf)
  20266. (org-agenda-maybe-reset-markers 'force)
  20267. (org-columns-remove-overlays))
  20268. ;; Maybe restore the pre-agenda window configuration.
  20269. (and org-agenda-restore-windows-after-quit
  20270. (not (eq org-agenda-window-setup 'other-frame))
  20271. org-pre-agenda-window-conf
  20272. (set-window-configuration org-pre-agenda-window-conf)))
  20273. (defun org-agenda-exit ()
  20274. "Exit agenda by removing the window or the buffer.
  20275. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20276. Org-mode buffers visited directly by the user will not be touched."
  20277. (interactive)
  20278. (org-release-buffers org-agenda-new-buffers)
  20279. (setq org-agenda-new-buffers nil)
  20280. (org-agenda-quit))
  20281. (defun org-agenda-execute (arg)
  20282. "Execute another agenda command, keeping same window.\\<global-map>
  20283. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20284. (interactive "P")
  20285. (let ((org-agenda-window-setup 'current-window))
  20286. (org-agenda arg)))
  20287. (defun org-save-all-org-buffers ()
  20288. "Save all Org-mode buffers without user confirmation."
  20289. (interactive)
  20290. (message "Saving all Org-mode buffers...")
  20291. (save-some-buffers t 'org-mode-p)
  20292. (message "Saving all Org-mode buffers... done"))
  20293. (defun org-agenda-redo ()
  20294. "Rebuild Agenda.
  20295. When this is the global TODO list, a prefix argument will be interpreted."
  20296. (interactive)
  20297. (let* ((org-agenda-keep-modes t)
  20298. (line (org-current-line))
  20299. (window-line (- line (org-current-line (window-start))))
  20300. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20301. (message "Rebuilding agenda buffer...")
  20302. (org-let lprops '(eval org-agenda-redo-command))
  20303. (setq org-agenda-undo-list nil
  20304. org-agenda-pending-undo-list nil)
  20305. (message "Rebuilding agenda buffer...done")
  20306. (goto-line line)
  20307. (recenter window-line)))
  20308. (defun org-agenda-goto-date (date)
  20309. "Jump to DATE in agenda."
  20310. (interactive (list (org-read-date)))
  20311. (org-agenda-list nil date))
  20312. (defun org-agenda-goto-today ()
  20313. "Go to today."
  20314. (interactive)
  20315. (org-agenda-check-type t 'timeline 'agenda)
  20316. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20317. (cond
  20318. (tdpos (goto-char tdpos))
  20319. ((eq org-agenda-type 'agenda)
  20320. (let* ((sd (time-to-days
  20321. (time-subtract (current-time)
  20322. (list 0 (* 3600 org-extend-today-until) 0))))
  20323. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20324. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20325. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20326. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20327. (org-agenda-redo)
  20328. (org-agenda-find-same-or-today-or-agenda)))
  20329. (t (error "Cannot find today")))))
  20330. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20331. (goto-char
  20332. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20333. (text-property-any (point-min) (point-max) 'org-today t)
  20334. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20335. (point-min))))
  20336. (defun org-agenda-later (arg)
  20337. "Go forward in time by thee current span.
  20338. With prefix ARG, go forward that many times the current span."
  20339. (interactive "p")
  20340. (org-agenda-check-type t 'agenda)
  20341. (let* ((span org-agenda-span)
  20342. (sd org-starting-day)
  20343. (greg (calendar-gregorian-from-absolute sd))
  20344. (cnt (get-text-property (point) 'org-day-cnt))
  20345. greg2 nd)
  20346. (cond
  20347. ((eq span 'day)
  20348. (setq sd (+ arg sd) nd 1))
  20349. ((eq span 'week)
  20350. (setq sd (+ (* 7 arg) sd) nd 7))
  20351. ((eq span 'month)
  20352. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20353. sd (calendar-absolute-from-gregorian greg2))
  20354. (setcar greg2 (1+ (car greg2)))
  20355. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20356. ((eq span 'year)
  20357. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20358. sd (calendar-absolute-from-gregorian greg2))
  20359. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20360. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20361. (let ((org-agenda-overriding-arguments
  20362. (list (car org-agenda-last-arguments) sd nd t)))
  20363. (org-agenda-redo)
  20364. (org-agenda-find-same-or-today-or-agenda cnt))))
  20365. (defun org-agenda-earlier (arg)
  20366. "Go backward in time by the current span.
  20367. With prefix ARG, go backward that many times the current span."
  20368. (interactive "p")
  20369. (org-agenda-later (- arg)))
  20370. (defun org-agenda-day-view ()
  20371. "Switch to daily view for agenda."
  20372. (interactive)
  20373. (setq org-agenda-ndays 1)
  20374. (org-agenda-change-time-span 'day))
  20375. (defun org-agenda-week-view ()
  20376. "Switch to daily view for agenda."
  20377. (interactive)
  20378. (setq org-agenda-ndays 7)
  20379. (org-agenda-change-time-span 'week))
  20380. (defun org-agenda-month-view ()
  20381. "Switch to daily view for agenda."
  20382. (interactive)
  20383. (org-agenda-change-time-span 'month))
  20384. (defun org-agenda-year-view ()
  20385. "Switch to daily view for agenda."
  20386. (interactive)
  20387. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20388. (org-agenda-change-time-span 'year)
  20389. (error "Abort")))
  20390. (defun org-agenda-change-time-span (span)
  20391. "Change the agenda view to SPAN.
  20392. SPAN may be `day', `week', `month', `year'."
  20393. (org-agenda-check-type t 'agenda)
  20394. (if (equal org-agenda-span span)
  20395. (error "Viewing span is already \"%s\"" span))
  20396. (let* ((sd (or (get-text-property (point) 'day)
  20397. org-starting-day))
  20398. (computed (org-agenda-compute-time-span sd span))
  20399. (org-agenda-overriding-arguments
  20400. (list (car org-agenda-last-arguments)
  20401. (car computed) (cdr computed) t)))
  20402. (org-agenda-redo)
  20403. (org-agenda-find-same-or-today-or-agenda))
  20404. (org-agenda-set-mode-name)
  20405. (message "Switched to %s view" span))
  20406. (defun org-agenda-compute-time-span (sd span)
  20407. "Compute starting date and number of days for agenda.
  20408. SPAN may be `day', `week', `month', `year'. The return value
  20409. is a cons cell with the starting date and the number of days,
  20410. so that the date SD will be in that range."
  20411. (let* ((greg (calendar-gregorian-from-absolute sd))
  20412. nd)
  20413. (cond
  20414. ((eq span 'day)
  20415. (setq nd 1))
  20416. ((eq span 'week)
  20417. (let* ((nt (calendar-day-of-week
  20418. (calendar-gregorian-from-absolute sd)))
  20419. (d (if org-agenda-start-on-weekday
  20420. (- nt org-agenda-start-on-weekday)
  20421. 0)))
  20422. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20423. (setq nd 7)))
  20424. ((eq span 'month)
  20425. (setq sd (calendar-absolute-from-gregorian
  20426. (list (car greg) 1 (nth 2 greg)))
  20427. nd (- (calendar-absolute-from-gregorian
  20428. (list (1+ (car greg)) 1 (nth 2 greg)))
  20429. sd)))
  20430. ((eq span 'year)
  20431. (setq sd (calendar-absolute-from-gregorian
  20432. (list 1 1 (nth 2 greg)))
  20433. nd (- (calendar-absolute-from-gregorian
  20434. (list 1 1 (1+ (nth 2 greg))))
  20435. sd))))
  20436. (cons sd nd)))
  20437. ;; FIXME: does not work if user makes date format that starts with a blank
  20438. (defun org-agenda-next-date-line (&optional arg)
  20439. "Jump to the next line indicating a date in agenda buffer."
  20440. (interactive "p")
  20441. (org-agenda-check-type t 'agenda 'timeline)
  20442. (beginning-of-line 1)
  20443. (if (looking-at "^\\S-") (forward-char 1))
  20444. (if (not (re-search-forward "^\\S-" nil t arg))
  20445. (progn
  20446. (backward-char 1)
  20447. (error "No next date after this line in this buffer")))
  20448. (goto-char (match-beginning 0)))
  20449. (defun org-agenda-previous-date-line (&optional arg)
  20450. "Jump to the previous line indicating a date in agenda buffer."
  20451. (interactive "p")
  20452. (org-agenda-check-type t 'agenda 'timeline)
  20453. (beginning-of-line 1)
  20454. (if (not (re-search-backward "^\\S-" nil t arg))
  20455. (error "No previous date before this line in this buffer")))
  20456. ;; Initialize the highlight
  20457. (defvar org-hl (org-make-overlay 1 1))
  20458. (org-overlay-put org-hl 'face 'highlight)
  20459. (defun org-highlight (begin end &optional buffer)
  20460. "Highlight a region with overlay."
  20461. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20462. org-hl begin end (or buffer (current-buffer))))
  20463. (defun org-unhighlight ()
  20464. "Detach overlay INDEX."
  20465. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20466. ;; FIXME this is currently not used.
  20467. (defun org-highlight-until-next-command (beg end &optional buffer)
  20468. (org-highlight beg end buffer)
  20469. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20470. (defun org-unhighlight-once ()
  20471. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20472. (org-unhighlight))
  20473. (defun org-agenda-follow-mode ()
  20474. "Toggle follow mode in an agenda buffer."
  20475. (interactive)
  20476. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20477. (org-agenda-set-mode-name)
  20478. (message "Follow mode is %s"
  20479. (if org-agenda-follow-mode "on" "off")))
  20480. (defun org-agenda-log-mode ()
  20481. "Toggle log mode in an agenda buffer."
  20482. (interactive)
  20483. (org-agenda-check-type t 'agenda 'timeline)
  20484. (setq org-agenda-show-log (not org-agenda-show-log))
  20485. (org-agenda-set-mode-name)
  20486. (org-agenda-redo)
  20487. (message "Log mode is %s"
  20488. (if org-agenda-show-log "on" "off")))
  20489. (defun org-agenda-toggle-diary ()
  20490. "Toggle diary inclusion in an agenda buffer."
  20491. (interactive)
  20492. (org-agenda-check-type t 'agenda)
  20493. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20494. (org-agenda-redo)
  20495. (org-agenda-set-mode-name)
  20496. (message "Diary inclusion turned %s"
  20497. (if org-agenda-include-diary "on" "off")))
  20498. (defun org-agenda-toggle-time-grid ()
  20499. "Toggle time grid in an agenda buffer."
  20500. (interactive)
  20501. (org-agenda-check-type t 'agenda)
  20502. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20503. (org-agenda-redo)
  20504. (org-agenda-set-mode-name)
  20505. (message "Time-grid turned %s"
  20506. (if org-agenda-use-time-grid "on" "off")))
  20507. (defun org-agenda-set-mode-name ()
  20508. "Set the mode name to indicate all the small mode settings."
  20509. (setq mode-name
  20510. (concat "Org-Agenda"
  20511. (if (equal org-agenda-ndays 1) " Day" "")
  20512. (if (equal org-agenda-ndays 7) " Week" "")
  20513. (if org-agenda-follow-mode " Follow" "")
  20514. (if org-agenda-include-diary " Diary" "")
  20515. (if org-agenda-use-time-grid " Grid" "")
  20516. (if org-agenda-show-log " Log" "")))
  20517. (force-mode-line-update))
  20518. (defun org-agenda-post-command-hook ()
  20519. (and (eolp) (not (bolp)) (backward-char 1))
  20520. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20521. (if (and org-agenda-follow-mode
  20522. (get-text-property (point) 'org-marker))
  20523. (org-agenda-show)))
  20524. (defun org-agenda-show-priority ()
  20525. "Show the priority of the current item.
  20526. This priority is composed of the main priority given with the [#A] cookies,
  20527. and by additional input from the age of a schedules or deadline entry."
  20528. (interactive)
  20529. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20530. (message "Priority is %d" (if pri pri -1000))))
  20531. (defun org-agenda-show-tags ()
  20532. "Show the tags applicable to the current item."
  20533. (interactive)
  20534. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20535. (if tags
  20536. (message "Tags are :%s:"
  20537. (org-no-properties (mapconcat 'identity tags ":")))
  20538. (message "No tags associated with this line"))))
  20539. (defun org-agenda-goto (&optional highlight)
  20540. "Go to the Org-mode file which contains the item at point."
  20541. (interactive)
  20542. (let* ((marker (or (get-text-property (point) 'org-marker)
  20543. (org-agenda-error)))
  20544. (buffer (marker-buffer marker))
  20545. (pos (marker-position marker)))
  20546. (switch-to-buffer-other-window buffer)
  20547. (widen)
  20548. (goto-char pos)
  20549. (when (org-mode-p)
  20550. (org-show-context 'agenda)
  20551. (save-excursion
  20552. (and (outline-next-heading)
  20553. (org-flag-heading nil)))) ; show the next heading
  20554. (run-hooks 'org-agenda-after-show-hook)
  20555. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20556. (defvar org-agenda-after-show-hook nil
  20557. "Normal hook run after an item has been shown from the agenda.
  20558. Point is in the buffer where the item originated.")
  20559. (defun org-agenda-kill ()
  20560. "Kill the entry or subtree belonging to the current agenda entry."
  20561. (interactive)
  20562. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20563. (let* ((marker (or (get-text-property (point) 'org-marker)
  20564. (org-agenda-error)))
  20565. (buffer (marker-buffer marker))
  20566. (pos (marker-position marker))
  20567. (type (get-text-property (point) 'type))
  20568. dbeg dend (n 0) conf)
  20569. (org-with-remote-undo buffer
  20570. (with-current-buffer buffer
  20571. (save-excursion
  20572. (goto-char pos)
  20573. (if (and (org-mode-p) (not (member type '("sexp"))))
  20574. (setq dbeg (progn (org-back-to-heading t) (point))
  20575. dend (org-end-of-subtree t t))
  20576. (setq dbeg (point-at-bol)
  20577. dend (min (point-max) (1+ (point-at-eol)))))
  20578. (goto-char dbeg)
  20579. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20580. (setq conf (or (eq t org-agenda-confirm-kill)
  20581. (and (numberp org-agenda-confirm-kill)
  20582. (> n org-agenda-confirm-kill))))
  20583. (and conf
  20584. (not (y-or-n-p
  20585. (format "Delete entry with %d lines in buffer \"%s\"? "
  20586. n (buffer-name buffer))))
  20587. (error "Abort"))
  20588. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20589. (with-current-buffer buffer (delete-region dbeg dend))
  20590. (message "Agenda item and source killed"))))
  20591. (defun org-agenda-archive ()
  20592. "Kill the entry or subtree belonging to the current agenda entry."
  20593. (interactive)
  20594. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20595. (let* ((marker (or (get-text-property (point) 'org-marker)
  20596. (org-agenda-error)))
  20597. (buffer (marker-buffer marker))
  20598. (pos (marker-position marker)))
  20599. (org-with-remote-undo buffer
  20600. (with-current-buffer buffer
  20601. (if (org-mode-p)
  20602. (save-excursion
  20603. (goto-char pos)
  20604. (org-remove-subtree-entries-from-agenda)
  20605. (org-back-to-heading t)
  20606. (org-archive-subtree))
  20607. (error "Archiving works only in Org-mode files"))))))
  20608. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20609. "Remove all lines in the agenda that correspond to a given subtree.
  20610. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20611. If this information is not given, the function uses the tree at point."
  20612. (let ((buf (or buf (current-buffer))) m p)
  20613. (save-excursion
  20614. (unless (and beg end)
  20615. (org-back-to-heading t)
  20616. (setq beg (point))
  20617. (org-end-of-subtree t)
  20618. (setq end (point)))
  20619. (set-buffer (get-buffer org-agenda-buffer-name))
  20620. (save-excursion
  20621. (goto-char (point-max))
  20622. (beginning-of-line 1)
  20623. (while (not (bobp))
  20624. (when (and (setq m (get-text-property (point) 'org-marker))
  20625. (equal buf (marker-buffer m))
  20626. (setq p (marker-position m))
  20627. (>= p beg)
  20628. (<= p end))
  20629. (let ((inhibit-read-only t))
  20630. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20631. (beginning-of-line 0))))))
  20632. (defun org-agenda-open-link ()
  20633. "Follow the link in the current line, if any."
  20634. (interactive)
  20635. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20636. (save-excursion
  20637. (save-restriction
  20638. (narrow-to-region (point-at-bol) (point-at-eol))
  20639. (org-open-at-point))))
  20640. (defun org-agenda-copy-local-variable (var)
  20641. "Get a variable from a referenced buffer and install it here."
  20642. (let ((m (get-text-property (point) 'org-marker)))
  20643. (when (and m (buffer-live-p (marker-buffer m)))
  20644. (org-set-local var (with-current-buffer (marker-buffer m)
  20645. (symbol-value var))))))
  20646. (defun org-agenda-switch-to (&optional delete-other-windows)
  20647. "Go to the Org-mode file which contains the item at point."
  20648. (interactive)
  20649. (let* ((marker (or (get-text-property (point) 'org-marker)
  20650. (org-agenda-error)))
  20651. (buffer (marker-buffer marker))
  20652. (pos (marker-position marker)))
  20653. (switch-to-buffer buffer)
  20654. (and delete-other-windows (delete-other-windows))
  20655. (widen)
  20656. (goto-char pos)
  20657. (when (org-mode-p)
  20658. (org-show-context 'agenda)
  20659. (save-excursion
  20660. (and (outline-next-heading)
  20661. (org-flag-heading nil)))))) ; show the next heading
  20662. (defun org-agenda-goto-mouse (ev)
  20663. "Go to the Org-mode file which contains the item at the mouse click."
  20664. (interactive "e")
  20665. (mouse-set-point ev)
  20666. (org-agenda-goto))
  20667. (defun org-agenda-show ()
  20668. "Display the Org-mode file which contains the item at point."
  20669. (interactive)
  20670. (let ((win (selected-window)))
  20671. (org-agenda-goto t)
  20672. (select-window win)))
  20673. (defun org-agenda-recenter (arg)
  20674. "Display the Org-mode file which contains the item at point and recenter."
  20675. (interactive "P")
  20676. (let ((win (selected-window)))
  20677. (org-agenda-goto t)
  20678. (recenter arg)
  20679. (select-window win)))
  20680. (defun org-agenda-show-mouse (ev)
  20681. "Display the Org-mode file which contains the item at the mouse click."
  20682. (interactive "e")
  20683. (mouse-set-point ev)
  20684. (org-agenda-show))
  20685. (defun org-agenda-check-no-diary ()
  20686. "Check if the entry is a diary link and abort if yes."
  20687. (if (get-text-property (point) 'org-agenda-diary-link)
  20688. (org-agenda-error)))
  20689. (defun org-agenda-error ()
  20690. (error "Command not allowed in this line"))
  20691. (defun org-agenda-tree-to-indirect-buffer ()
  20692. "Show the subtree corresponding to the current entry in an indirect buffer.
  20693. This calls the command `org-tree-to-indirect-buffer' from the original
  20694. Org-mode buffer.
  20695. With numerical prefix arg ARG, go up to this level and then take that tree.
  20696. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20697. dedicated frame)."
  20698. (interactive)
  20699. (org-agenda-check-no-diary)
  20700. (let* ((marker (or (get-text-property (point) 'org-marker)
  20701. (org-agenda-error)))
  20702. (buffer (marker-buffer marker))
  20703. (pos (marker-position marker)))
  20704. (with-current-buffer buffer
  20705. (save-excursion
  20706. (goto-char pos)
  20707. (call-interactively 'org-tree-to-indirect-buffer)))))
  20708. (defvar org-last-heading-marker (make-marker)
  20709. "Marker pointing to the headline that last changed its TODO state
  20710. by a remote command from the agenda.")
  20711. (defun org-agenda-todo-nextset ()
  20712. "Switch TODO entry to next sequence."
  20713. (interactive)
  20714. (org-agenda-todo 'nextset))
  20715. (defun org-agenda-todo-previousset ()
  20716. "Switch TODO entry to previous sequence."
  20717. (interactive)
  20718. (org-agenda-todo 'previousset))
  20719. (defun org-agenda-todo (&optional arg)
  20720. "Cycle TODO state of line at point, also in Org-mode file.
  20721. This changes the line at point, all other lines in the agenda referring to
  20722. the same tree node, and the headline of the tree node in the Org-mode file."
  20723. (interactive "P")
  20724. (org-agenda-check-no-diary)
  20725. (let* ((col (current-column))
  20726. (marker (or (get-text-property (point) 'org-marker)
  20727. (org-agenda-error)))
  20728. (buffer (marker-buffer marker))
  20729. (pos (marker-position marker))
  20730. (hdmarker (get-text-property (point) 'org-hd-marker))
  20731. (inhibit-read-only t)
  20732. newhead)
  20733. (org-with-remote-undo buffer
  20734. (with-current-buffer buffer
  20735. (widen)
  20736. (goto-char pos)
  20737. (org-show-context 'agenda)
  20738. (save-excursion
  20739. (and (outline-next-heading)
  20740. (org-flag-heading nil))) ; show the next heading
  20741. (org-todo arg)
  20742. (and (bolp) (forward-char 1))
  20743. (setq newhead (org-get-heading))
  20744. (save-excursion
  20745. (org-back-to-heading)
  20746. (move-marker org-last-heading-marker (point))))
  20747. (beginning-of-line 1)
  20748. (save-excursion
  20749. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20750. (move-to-column col))))
  20751. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20752. "Change all lines in the agenda buffer which match HDMARKER.
  20753. The new content of the line will be NEWHEAD (as modified by
  20754. `org-format-agenda-item'). HDMARKER is checked with
  20755. `equal' against all `org-hd-marker' text properties in the file.
  20756. If FIXFACE is non-nil, the face of each item is modified acording to
  20757. the new TODO state."
  20758. (let* ((inhibit-read-only t)
  20759. props m pl undone-face done-face finish new dotime cat tags)
  20760. (save-excursion
  20761. (goto-char (point-max))
  20762. (beginning-of-line 1)
  20763. (while (not finish)
  20764. (setq finish (bobp))
  20765. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20766. (equal m hdmarker))
  20767. (setq props (text-properties-at (point))
  20768. dotime (get-text-property (point) 'dotime)
  20769. cat (get-text-property (point) 'org-category)
  20770. tags (get-text-property (point) 'tags)
  20771. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20772. pl (get-text-property (point) 'prefix-length)
  20773. undone-face (get-text-property (point) 'undone-face)
  20774. done-face (get-text-property (point) 'done-face))
  20775. (move-to-column pl)
  20776. (cond
  20777. ((equal new "")
  20778. (beginning-of-line 1)
  20779. (and (looking-at ".*\n?") (replace-match "")))
  20780. ((looking-at ".*")
  20781. (replace-match new t t)
  20782. (beginning-of-line 1)
  20783. (add-text-properties (point-at-bol) (point-at-eol) props)
  20784. (when fixface
  20785. (add-text-properties
  20786. (point-at-bol) (point-at-eol)
  20787. (list 'face
  20788. (if org-last-todo-state-is-todo
  20789. undone-face done-face))))
  20790. (org-agenda-highlight-todo 'line)
  20791. (beginning-of-line 1))
  20792. (t (error "Line update did not work"))))
  20793. (beginning-of-line 0)))
  20794. (org-finalize-agenda)))
  20795. (defun org-agenda-align-tags (&optional line)
  20796. "Align all tags in agenda items to `org-agenda-tags-column'."
  20797. (let ((inhibit-read-only t) l c)
  20798. (save-excursion
  20799. (goto-char (if line (point-at-bol) (point-min)))
  20800. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20801. (if line (point-at-eol) nil) t)
  20802. (add-text-properties
  20803. (match-beginning 2) (match-end 2)
  20804. (list 'face (list 'org-tag (get-text-property
  20805. (match-beginning 2) 'face))))
  20806. (setq l (- (match-end 2) (match-beginning 2))
  20807. c (if (< org-agenda-tags-column 0)
  20808. (- (abs org-agenda-tags-column) l)
  20809. org-agenda-tags-column))
  20810. (delete-region (match-beginning 1) (match-end 1))
  20811. (goto-char (match-beginning 1))
  20812. (insert (org-add-props
  20813. (make-string (max 1 (- c (current-column))) ?\ )
  20814. (text-properties-at (point))))))))
  20815. (defun org-agenda-priority-up ()
  20816. "Increase the priority of line at point, also in Org-mode file."
  20817. (interactive)
  20818. (org-agenda-priority 'up))
  20819. (defun org-agenda-priority-down ()
  20820. "Decrease the priority of line at point, also in Org-mode file."
  20821. (interactive)
  20822. (org-agenda-priority 'down))
  20823. (defun org-agenda-priority (&optional force-direction)
  20824. "Set the priority of line at point, also in Org-mode file.
  20825. This changes the line at point, all other lines in the agenda referring to
  20826. the same tree node, and the headline of the tree node in the Org-mode file."
  20827. (interactive)
  20828. (org-agenda-check-no-diary)
  20829. (let* ((marker (or (get-text-property (point) 'org-marker)
  20830. (org-agenda-error)))
  20831. (hdmarker (get-text-property (point) 'org-hd-marker))
  20832. (buffer (marker-buffer hdmarker))
  20833. (pos (marker-position hdmarker))
  20834. (inhibit-read-only t)
  20835. newhead)
  20836. (org-with-remote-undo buffer
  20837. (with-current-buffer buffer
  20838. (widen)
  20839. (goto-char pos)
  20840. (org-show-context 'agenda)
  20841. (save-excursion
  20842. (and (outline-next-heading)
  20843. (org-flag-heading nil))) ; show the next heading
  20844. (funcall 'org-priority force-direction)
  20845. (end-of-line 1)
  20846. (setq newhead (org-get-heading)))
  20847. (org-agenda-change-all-lines newhead hdmarker)
  20848. (beginning-of-line 1))))
  20849. (defun org-get-tags-at (&optional pos)
  20850. "Get a list of all headline tags applicable at POS.
  20851. POS defaults to point. If tags are inherited, the list contains
  20852. the targets in the same sequence as the headlines appear, i.e.
  20853. the tags of the current headline come last."
  20854. (interactive)
  20855. (let (tags lastpos)
  20856. (save-excursion
  20857. (save-restriction
  20858. (widen)
  20859. (goto-char (or pos (point)))
  20860. (save-match-data
  20861. (org-back-to-heading t)
  20862. (condition-case nil
  20863. (while (not (equal lastpos (point)))
  20864. (setq lastpos (point))
  20865. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  20866. (setq tags (append (org-split-string
  20867. (org-match-string-no-properties 1) ":")
  20868. tags)))
  20869. (or org-use-tag-inheritance (error ""))
  20870. (org-up-heading-all 1))
  20871. (error nil))))
  20872. tags)))
  20873. ;; FIXME: should fix the tags property of the agenda line.
  20874. (defun org-agenda-set-tags ()
  20875. "Set tags for the current headline."
  20876. (interactive)
  20877. (org-agenda-check-no-diary)
  20878. (if (and (org-region-active-p) (interactive-p))
  20879. (call-interactively 'org-change-tag-in-region)
  20880. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20881. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20882. (org-agenda-error)))
  20883. (buffer (marker-buffer hdmarker))
  20884. (pos (marker-position hdmarker))
  20885. (inhibit-read-only t)
  20886. newhead)
  20887. (org-with-remote-undo buffer
  20888. (with-current-buffer buffer
  20889. (widen)
  20890. (goto-char pos)
  20891. (save-excursion
  20892. (org-show-context 'agenda))
  20893. (save-excursion
  20894. (and (outline-next-heading)
  20895. (org-flag-heading nil))) ; show the next heading
  20896. (goto-char pos)
  20897. (call-interactively 'org-set-tags)
  20898. (end-of-line 1)
  20899. (setq newhead (org-get-heading)))
  20900. (org-agenda-change-all-lines newhead hdmarker)
  20901. (beginning-of-line 1)))))
  20902. (defun org-agenda-toggle-archive-tag ()
  20903. "Toggle the archive tag for the current entry."
  20904. (interactive)
  20905. (org-agenda-check-no-diary)
  20906. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20907. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20908. (org-agenda-error)))
  20909. (buffer (marker-buffer hdmarker))
  20910. (pos (marker-position hdmarker))
  20911. (inhibit-read-only t)
  20912. newhead)
  20913. (org-with-remote-undo buffer
  20914. (with-current-buffer buffer
  20915. (widen)
  20916. (goto-char pos)
  20917. (org-show-context 'agenda)
  20918. (save-excursion
  20919. (and (outline-next-heading)
  20920. (org-flag-heading nil))) ; show the next heading
  20921. (call-interactively 'org-toggle-archive-tag)
  20922. (end-of-line 1)
  20923. (setq newhead (org-get-heading)))
  20924. (org-agenda-change-all-lines newhead hdmarker)
  20925. (beginning-of-line 1))))
  20926. (defun org-agenda-date-later (arg &optional what)
  20927. "Change the date of this item to one day later."
  20928. (interactive "p")
  20929. (org-agenda-check-type t 'agenda 'timeline)
  20930. (org-agenda-check-no-diary)
  20931. (let* ((marker (or (get-text-property (point) 'org-marker)
  20932. (org-agenda-error)))
  20933. (buffer (marker-buffer marker))
  20934. (pos (marker-position marker)))
  20935. (org-with-remote-undo buffer
  20936. (with-current-buffer buffer
  20937. (widen)
  20938. (goto-char pos)
  20939. (if (not (org-at-timestamp-p))
  20940. (error "Cannot find time stamp"))
  20941. (org-timestamp-change arg (or what 'day)))
  20942. (org-agenda-show-new-time marker org-last-changed-timestamp))
  20943. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  20944. (defun org-agenda-date-earlier (arg &optional what)
  20945. "Change the date of this item to one day earlier."
  20946. (interactive "p")
  20947. (org-agenda-date-later (- arg) what))
  20948. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  20949. "Show new date stamp via text properties."
  20950. ;; We use text properties to make this undoable
  20951. (let ((inhibit-read-only t))
  20952. (setq stamp (concat " " prefix " => " stamp))
  20953. (save-excursion
  20954. (goto-char (point-max))
  20955. (while (not (bobp))
  20956. (when (equal marker (get-text-property (point) 'org-marker))
  20957. (move-to-column (- (window-width) (length stamp)) t)
  20958. (if (featurep 'xemacs)
  20959. ;; Use `duplicable' property to trigger undo recording
  20960. (let ((ex (make-extent nil nil))
  20961. (gl (make-glyph stamp)))
  20962. (set-glyph-face gl 'secondary-selection)
  20963. (set-extent-properties
  20964. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  20965. (insert-extent ex (1- (point)) (point-at-eol)))
  20966. (add-text-properties
  20967. (1- (point)) (point-at-eol)
  20968. (list 'display (org-add-props stamp nil
  20969. 'face 'secondary-selection))))
  20970. (beginning-of-line 1))
  20971. (beginning-of-line 0)))))
  20972. (defun org-agenda-date-prompt (arg)
  20973. "Change the date of this item. Date is prompted for, with default today.
  20974. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  20975. be used to request time specification in the time stamp."
  20976. (interactive "P")
  20977. (org-agenda-check-type t 'agenda 'timeline)
  20978. (org-agenda-check-no-diary)
  20979. (let* ((marker (or (get-text-property (point) 'org-marker)
  20980. (org-agenda-error)))
  20981. (buffer (marker-buffer marker))
  20982. (pos (marker-position marker)))
  20983. (org-with-remote-undo buffer
  20984. (with-current-buffer buffer
  20985. (widen)
  20986. (goto-char pos)
  20987. (if (not (org-at-timestamp-p))
  20988. (error "Cannot find time stamp"))
  20989. (org-time-stamp arg)
  20990. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  20991. (defun org-agenda-schedule (arg)
  20992. "Schedule the item at point."
  20993. (interactive "P")
  20994. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20995. (org-agenda-check-no-diary)
  20996. (let* ((marker (or (get-text-property (point) 'org-marker)
  20997. (org-agenda-error)))
  20998. (buffer (marker-buffer marker))
  20999. (pos (marker-position marker))
  21000. (org-insert-labeled-timestamps-at-point nil)
  21001. ts)
  21002. (org-with-remote-undo buffer
  21003. (with-current-buffer buffer
  21004. (widen)
  21005. (goto-char pos)
  21006. (setq ts (org-schedule arg)))
  21007. (org-agenda-show-new-time marker ts "S"))
  21008. (message "Item scheduled for %s" ts)))
  21009. (defun org-agenda-deadline (arg)
  21010. "Schedule the item at point."
  21011. (interactive "P")
  21012. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21013. (org-agenda-check-no-diary)
  21014. (let* ((marker (or (get-text-property (point) 'org-marker)
  21015. (org-agenda-error)))
  21016. (buffer (marker-buffer marker))
  21017. (pos (marker-position marker))
  21018. (org-insert-labeled-timestamps-at-point nil)
  21019. ts)
  21020. (org-with-remote-undo buffer
  21021. (with-current-buffer buffer
  21022. (widen)
  21023. (goto-char pos)
  21024. (setq ts (org-deadline arg)))
  21025. (org-agenda-show-new-time marker ts "S"))
  21026. (message "Deadline for this item set to %s" ts)))
  21027. (defun org-get-heading (&optional no-tags)
  21028. "Return the heading of the current entry, without the stars."
  21029. (save-excursion
  21030. (org-back-to-heading t)
  21031. (if (looking-at
  21032. (if no-tags
  21033. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21034. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21035. (match-string 1) "")))
  21036. (defun org-agenda-clock-in (&optional arg)
  21037. "Start the clock on the currently selected item."
  21038. (interactive "P")
  21039. (org-agenda-check-no-diary)
  21040. (let* ((marker (or (get-text-property (point) 'org-marker)
  21041. (org-agenda-error)))
  21042. (pos (marker-position marker)))
  21043. (org-with-remote-undo (marker-buffer marker)
  21044. (with-current-buffer (marker-buffer marker)
  21045. (widen)
  21046. (goto-char pos)
  21047. (org-clock-in)))))
  21048. (defun org-agenda-clock-out (&optional arg)
  21049. "Stop the currently running clock."
  21050. (interactive "P")
  21051. (unless (marker-buffer org-clock-marker)
  21052. (error "No running clock"))
  21053. (org-with-remote-undo (marker-buffer org-clock-marker)
  21054. (org-clock-out)))
  21055. (defun org-agenda-clock-cancel (&optional arg)
  21056. "Cancel the currently running clock."
  21057. (interactive "P")
  21058. (unless (marker-buffer org-clock-marker)
  21059. (error "No running clock"))
  21060. (org-with-remote-undo (marker-buffer org-clock-marker)
  21061. (org-clock-cancel)))
  21062. (defun org-agenda-diary-entry ()
  21063. "Make a diary entry, like the `i' command from the calendar.
  21064. All the standard commands work: block, weekly etc."
  21065. (interactive)
  21066. (org-agenda-check-type t 'agenda 'timeline)
  21067. (require 'diary-lib)
  21068. (let* ((char (progn
  21069. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21070. (read-char-exclusive)))
  21071. (cmd (cdr (assoc char
  21072. '((?d . insert-diary-entry)
  21073. (?w . insert-weekly-diary-entry)
  21074. (?m . insert-monthly-diary-entry)
  21075. (?y . insert-yearly-diary-entry)
  21076. (?a . insert-anniversary-diary-entry)
  21077. (?b . insert-block-diary-entry)
  21078. (?c . insert-cyclic-diary-entry)))))
  21079. (oldf (symbol-function 'calendar-cursor-to-date))
  21080. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21081. (point (point))
  21082. (mark (or (mark t) (point))))
  21083. (unless cmd
  21084. (error "No command associated with <%c>" char))
  21085. (unless (and (get-text-property point 'day)
  21086. (or (not (equal ?b char))
  21087. (get-text-property mark 'day)))
  21088. (error "Don't know which date to use for diary entry"))
  21089. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21090. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21091. (let ((calendar-mark-ring
  21092. (list (calendar-gregorian-from-absolute
  21093. (or (get-text-property mark 'day)
  21094. (get-text-property point 'day))))))
  21095. (unwind-protect
  21096. (progn
  21097. (fset 'calendar-cursor-to-date
  21098. (lambda (&optional error)
  21099. (calendar-gregorian-from-absolute
  21100. (get-text-property point 'day))))
  21101. (call-interactively cmd))
  21102. (fset 'calendar-cursor-to-date oldf)))))
  21103. (defun org-agenda-execute-calendar-command (cmd)
  21104. "Execute a calendar command from the agenda, with the date associated to
  21105. the cursor position."
  21106. (org-agenda-check-type t 'agenda 'timeline)
  21107. (require 'diary-lib)
  21108. (unless (get-text-property (point) 'day)
  21109. (error "Don't know which date to use for calendar command"))
  21110. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21111. (point (point))
  21112. (date (calendar-gregorian-from-absolute
  21113. (get-text-property point 'day)))
  21114. ;; the following 3 vars are needed in the calendar
  21115. (displayed-day (extract-calendar-day date))
  21116. (displayed-month (extract-calendar-month date))
  21117. (displayed-year (extract-calendar-year date)))
  21118. (unwind-protect
  21119. (progn
  21120. (fset 'calendar-cursor-to-date
  21121. (lambda (&optional error)
  21122. (calendar-gregorian-from-absolute
  21123. (get-text-property point 'day))))
  21124. (call-interactively cmd))
  21125. (fset 'calendar-cursor-to-date oldf))))
  21126. (defun org-agenda-phases-of-moon ()
  21127. "Display the phases of the moon for the 3 months around the cursor date."
  21128. (interactive)
  21129. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21130. (defun org-agenda-holidays ()
  21131. "Display the holidays for the 3 months around the cursor date."
  21132. (interactive)
  21133. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21134. (defun org-agenda-sunrise-sunset (arg)
  21135. "Display sunrise and sunset for the cursor date.
  21136. Latitude and longitude can be specified with the variables
  21137. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21138. argument, latitude and longitude will be prompted for."
  21139. (interactive "P")
  21140. (let ((calendar-longitude (if arg nil calendar-longitude))
  21141. (calendar-latitude (if arg nil calendar-latitude))
  21142. (calendar-location-name
  21143. (if arg "the given coordinates" calendar-location-name)))
  21144. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21145. (defun org-agenda-goto-calendar ()
  21146. "Open the Emacs calendar with the date at the cursor."
  21147. (interactive)
  21148. (org-agenda-check-type t 'agenda 'timeline)
  21149. (let* ((day (or (get-text-property (point) 'day)
  21150. (error "Don't know which date to open in calendar")))
  21151. (date (calendar-gregorian-from-absolute day))
  21152. (calendar-move-hook nil)
  21153. (view-calendar-holidays-initially nil)
  21154. (view-diary-entries-initially nil))
  21155. (calendar)
  21156. (calendar-goto-date date)))
  21157. (defun org-calendar-goto-agenda ()
  21158. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21159. This is a command that has to be installed in `calendar-mode-map'."
  21160. (interactive)
  21161. (org-agenda-list nil (calendar-absolute-from-gregorian
  21162. (calendar-cursor-to-date))
  21163. nil))
  21164. (defun org-agenda-convert-date ()
  21165. (interactive)
  21166. (org-agenda-check-type t 'agenda 'timeline)
  21167. (let ((day (get-text-property (point) 'day))
  21168. date s)
  21169. (unless day
  21170. (error "Don't know which date to convert"))
  21171. (setq date (calendar-gregorian-from-absolute day))
  21172. (setq s (concat
  21173. "Gregorian: " (calendar-date-string date) "\n"
  21174. "ISO: " (calendar-iso-date-string date) "\n"
  21175. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21176. "Julian: " (calendar-julian-date-string date) "\n"
  21177. "Astron. JD: " (calendar-astro-date-string date)
  21178. " (Julian date number at noon UTC)\n"
  21179. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21180. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21181. "French: " (calendar-french-date-string date) "\n"
  21182. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21183. "Mayan: " (calendar-mayan-date-string date) "\n"
  21184. "Coptic: " (calendar-coptic-date-string date) "\n"
  21185. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21186. "Persian: " (calendar-persian-date-string date) "\n"
  21187. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21188. (with-output-to-temp-buffer "*Dates*"
  21189. (princ s))
  21190. (if (fboundp 'fit-window-to-buffer)
  21191. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21192. ;;;; Embedded LaTeX
  21193. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21194. "Keymap for the minor `org-cdlatex-mode'.")
  21195. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21196. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21197. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21198. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21199. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21200. (defvar org-cdlatex-texmathp-advice-is-done nil
  21201. "Flag remembering if we have applied the advice to texmathp already.")
  21202. (define-minor-mode org-cdlatex-mode
  21203. "Toggle the minor `org-cdlatex-mode'.
  21204. This mode supports entering LaTeX environment and math in LaTeX fragments
  21205. in Org-mode.
  21206. \\{org-cdlatex-mode-map}"
  21207. nil " OCDL" nil
  21208. (when org-cdlatex-mode (require 'cdlatex))
  21209. (unless org-cdlatex-texmathp-advice-is-done
  21210. (setq org-cdlatex-texmathp-advice-is-done t)
  21211. (defadvice texmathp (around org-math-always-on activate)
  21212. "Always return t in org-mode buffers.
  21213. This is because we want to insert math symbols without dollars even outside
  21214. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21215. en embedded LaTeX fragement, let texmathp do its job.
  21216. \\[org-cdlatex-mode-map]"
  21217. (interactive)
  21218. (let (p)
  21219. (cond
  21220. ((not (org-mode-p)) ad-do-it)
  21221. ((eq this-command 'cdlatex-math-symbol)
  21222. (setq ad-return-value t
  21223. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21224. (t
  21225. (let ((p (org-inside-LaTeX-fragment-p)))
  21226. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21227. (setq ad-return-value t
  21228. texmathp-why '("Org-mode embedded math" . 0))
  21229. (if p ad-do-it)))))))))
  21230. (defun turn-on-org-cdlatex ()
  21231. "Unconditionally turn on `org-cdlatex-mode'."
  21232. (org-cdlatex-mode 1))
  21233. (defun org-inside-LaTeX-fragment-p ()
  21234. "Test if point is inside a LaTeX fragment.
  21235. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21236. sequence appearing also before point.
  21237. Even though the matchers for math are configurable, this function assumes
  21238. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21239. delimiters are skipped when they have been removed by customization.
  21240. The return value is nil, or a cons cell with the delimiter and
  21241. and the position of this delimiter.
  21242. This function does a reasonably good job, but can locally be fooled by
  21243. for example currency specifications. For example it will assume being in
  21244. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21245. fragments that are properly closed, but during editing, we have to live
  21246. with the uncertainty caused by missing closing delimiters. This function
  21247. looks only before point, not after."
  21248. (catch 'exit
  21249. (let ((pos (point))
  21250. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21251. (lim (progn
  21252. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21253. (point)))
  21254. dd-on str (start 0) m re)
  21255. (goto-char pos)
  21256. (when dodollar
  21257. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21258. re (nth 1 (assoc "$" org-latex-regexps)))
  21259. (while (string-match re str start)
  21260. (cond
  21261. ((= (match-end 0) (length str))
  21262. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21263. ((= (match-end 0) (- (length str) 5))
  21264. (throw 'exit nil))
  21265. (t (setq start (match-end 0))))))
  21266. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21267. (goto-char pos)
  21268. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21269. (and (match-beginning 2) (throw 'exit nil))
  21270. ;; count $$
  21271. (while (re-search-backward "\\$\\$" lim t)
  21272. (setq dd-on (not dd-on)))
  21273. (goto-char pos)
  21274. (if dd-on (cons "$$" m))))))
  21275. (defun org-try-cdlatex-tab ()
  21276. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21277. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21278. - inside a LaTeX fragment, or
  21279. - after the first word in a line, where an abbreviation expansion could
  21280. insert a LaTeX environment."
  21281. (when org-cdlatex-mode
  21282. (cond
  21283. ((save-excursion
  21284. (skip-chars-backward "a-zA-Z0-9*")
  21285. (skip-chars-backward " \t")
  21286. (bolp))
  21287. (cdlatex-tab) t)
  21288. ((org-inside-LaTeX-fragment-p)
  21289. (cdlatex-tab) t)
  21290. (t nil))))
  21291. (defun org-cdlatex-underscore-caret (&optional arg)
  21292. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21293. Revert to the normal definition outside of these fragments."
  21294. (interactive "P")
  21295. (if (org-inside-LaTeX-fragment-p)
  21296. (call-interactively 'cdlatex-sub-superscript)
  21297. (let (org-cdlatex-mode)
  21298. (call-interactively (key-binding (vector last-input-event))))))
  21299. (defun org-cdlatex-math-modify (&optional arg)
  21300. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21301. Revert to the normal definition outside of these fragments."
  21302. (interactive "P")
  21303. (if (org-inside-LaTeX-fragment-p)
  21304. (call-interactively 'cdlatex-math-modify)
  21305. (let (org-cdlatex-mode)
  21306. (call-interactively (key-binding (vector last-input-event))))))
  21307. (defvar org-latex-fragment-image-overlays nil
  21308. "List of overlays carrying the images of latex fragments.")
  21309. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21310. (defun org-remove-latex-fragment-image-overlays ()
  21311. "Remove all overlays with LaTeX fragment images in current buffer."
  21312. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21313. (setq org-latex-fragment-image-overlays nil))
  21314. (defun org-preview-latex-fragment (&optional subtree)
  21315. "Preview the LaTeX fragment at point, or all locally or globally.
  21316. If the cursor is in a LaTeX fragment, create the image and overlay
  21317. it over the source code. If there is no fragment at point, display
  21318. all fragments in the current text, from one headline to the next. With
  21319. prefix SUBTREE, display all fragments in the current subtree. With a
  21320. double prefix `C-u C-u', or when the cursor is before the first headline,
  21321. display all fragments in the buffer.
  21322. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21323. (interactive "P")
  21324. (org-remove-latex-fragment-image-overlays)
  21325. (save-excursion
  21326. (save-restriction
  21327. (let (beg end at msg)
  21328. (cond
  21329. ((or (equal subtree '(16))
  21330. (not (save-excursion
  21331. (re-search-backward (concat "^" outline-regexp) nil t))))
  21332. (setq beg (point-min) end (point-max)
  21333. msg "Creating images for buffer...%s"))
  21334. ((equal subtree '(4))
  21335. (org-back-to-heading)
  21336. (setq beg (point) end (org-end-of-subtree t)
  21337. msg "Creating images for subtree...%s"))
  21338. (t
  21339. (if (setq at (org-inside-LaTeX-fragment-p))
  21340. (goto-char (max (point-min) (- (cdr at) 2)))
  21341. (org-back-to-heading))
  21342. (setq beg (point) end (progn (outline-next-heading) (point))
  21343. msg (if at "Creating image...%s"
  21344. "Creating images for entry...%s"))))
  21345. (message msg "")
  21346. (narrow-to-region beg end)
  21347. (goto-char beg)
  21348. (org-format-latex
  21349. (concat "ltxpng/" (file-name-sans-extension
  21350. (file-name-nondirectory
  21351. buffer-file-name)))
  21352. default-directory 'overlays msg at 'forbuffer)
  21353. (message msg "done. Use `C-c C-c' to remove images.")))))
  21354. (defvar org-latex-regexps
  21355. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21356. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21357. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21358. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21359. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21360. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21361. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21362. "Regular expressions for matching embedded LaTeX.")
  21363. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21364. "Replace LaTeX fragments with links to an image, and produce images."
  21365. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21366. (let* ((prefixnodir (file-name-nondirectory prefix))
  21367. (absprefix (expand-file-name prefix dir))
  21368. (todir (file-name-directory absprefix))
  21369. (opt org-format-latex-options)
  21370. (matchers (plist-get opt :matchers))
  21371. (re-list org-latex-regexps)
  21372. (cnt 0) txt link beg end re e checkdir
  21373. m n block linkfile movefile ov)
  21374. ;; Check if there are old images files with this prefix, and remove them
  21375. (when (file-directory-p todir)
  21376. (mapc 'delete-file
  21377. (directory-files
  21378. todir 'full
  21379. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21380. ;; Check the different regular expressions
  21381. (while (setq e (pop re-list))
  21382. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21383. block (if (nth 3 e) "\n\n" ""))
  21384. (when (member m matchers)
  21385. (goto-char (point-min))
  21386. (while (re-search-forward re nil t)
  21387. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21388. (setq txt (match-string n)
  21389. beg (match-beginning n) end (match-end n)
  21390. cnt (1+ cnt)
  21391. linkfile (format "%s_%04d.png" prefix cnt)
  21392. movefile (format "%s_%04d.png" absprefix cnt)
  21393. link (concat block "[[file:" linkfile "]]" block))
  21394. (if msg (message msg cnt))
  21395. (goto-char beg)
  21396. (unless checkdir ; make sure the directory exists
  21397. (setq checkdir t)
  21398. (or (file-directory-p todir) (make-directory todir)))
  21399. (org-create-formula-image
  21400. txt movefile opt forbuffer)
  21401. (if overlays
  21402. (progn
  21403. (setq ov (org-make-overlay beg end))
  21404. (if (featurep 'xemacs)
  21405. (progn
  21406. (org-overlay-put ov 'invisible t)
  21407. (org-overlay-put
  21408. ov 'end-glyph
  21409. (make-glyph (vector 'png :file movefile))))
  21410. (org-overlay-put
  21411. ov 'display
  21412. (list 'image :type 'png :file movefile :ascent 'center)))
  21413. (push ov org-latex-fragment-image-overlays)
  21414. (goto-char end))
  21415. (delete-region beg end)
  21416. (insert link))))))))
  21417. ;; This function borrows from Ganesh Swami's latex2png.el
  21418. (defun org-create-formula-image (string tofile options buffer)
  21419. (let* ((tmpdir (if (featurep 'xemacs)
  21420. (temp-directory)
  21421. temporary-file-directory))
  21422. (texfilebase (make-temp-name
  21423. (expand-file-name "orgtex" tmpdir)))
  21424. (texfile (concat texfilebase ".tex"))
  21425. (dvifile (concat texfilebase ".dvi"))
  21426. (pngfile (concat texfilebase ".png"))
  21427. (fnh (face-attribute 'default :height nil))
  21428. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21429. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21430. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21431. "Black"))
  21432. (bg (or (plist-get options (if buffer :background :html-background))
  21433. "Transparent")))
  21434. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21435. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21436. (with-temp-file texfile
  21437. (insert org-format-latex-header
  21438. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21439. (let ((dir default-directory))
  21440. (condition-case nil
  21441. (progn
  21442. (cd tmpdir)
  21443. (call-process "latex" nil nil nil texfile))
  21444. (error nil))
  21445. (cd dir))
  21446. (if (not (file-exists-p dvifile))
  21447. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21448. (call-process "dvipng" nil nil nil
  21449. "-E" "-fg" fg "-bg" bg
  21450. "-D" dpi
  21451. ;;"-x" scale "-y" scale
  21452. "-T" "tight"
  21453. "-o" pngfile
  21454. dvifile)
  21455. (if (not (file-exists-p pngfile))
  21456. (progn (message "Failed to create png file from %s" texfile) nil)
  21457. ;; Use the requested file name and clean up
  21458. (copy-file pngfile tofile 'replace)
  21459. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21460. (delete-file (concat texfilebase e)))
  21461. pngfile))))
  21462. (defun org-dvipng-color (attr)
  21463. "Return an rgb color specification for dvipng."
  21464. (apply 'format "rgb %s %s %s"
  21465. (mapcar 'org-normalize-color
  21466. (color-values (face-attribute 'default attr nil)))))
  21467. (defun org-normalize-color (value)
  21468. "Return string to be used as color value for an RGB component."
  21469. (format "%g" (/ value 65535.0)))
  21470. ;;;; Exporting
  21471. ;;; Variables, constants, and parameter plists
  21472. (defconst org-level-max 20)
  21473. (defvar org-export-html-preamble nil
  21474. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21475. (defvar org-export-html-postamble nil
  21476. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21477. (defvar org-export-html-auto-preamble t
  21478. "Should default preamble be inserted? Set by publishing functions.")
  21479. (defvar org-export-html-auto-postamble t
  21480. "Should default postamble be inserted? Set by publishing functions.")
  21481. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21482. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21483. (defconst org-export-plist-vars
  21484. '((:language . org-export-default-language)
  21485. (:customtime . org-display-custom-times)
  21486. (:headline-levels . org-export-headline-levels)
  21487. (:section-numbers . org-export-with-section-numbers)
  21488. (:table-of-contents . org-export-with-toc)
  21489. (:preserve-breaks . org-export-preserve-breaks)
  21490. (:archived-trees . org-export-with-archived-trees)
  21491. (:emphasize . org-export-with-emphasize)
  21492. (:sub-superscript . org-export-with-sub-superscripts)
  21493. (:special-strings . org-export-with-special-strings)
  21494. (:footnotes . org-export-with-footnotes)
  21495. (:drawers . org-export-with-drawers)
  21496. (:tags . org-export-with-tags)
  21497. (:TeX-macros . org-export-with-TeX-macros)
  21498. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21499. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21500. (:fixed-width . org-export-with-fixed-width)
  21501. (:timestamps . org-export-with-timestamps)
  21502. (:author-info . org-export-author-info)
  21503. (:time-stamp-file . org-export-time-stamp-file)
  21504. (:tables . org-export-with-tables)
  21505. (:table-auto-headline . org-export-highlight-first-table-line)
  21506. (:style . org-export-html-style)
  21507. (:agenda-style . org-agenda-export-html-style)
  21508. (:convert-org-links . org-export-html-link-org-files-as-html)
  21509. (:inline-images . org-export-html-inline-images)
  21510. (:html-extension . org-export-html-extension)
  21511. (:html-table-tag . org-export-html-table-tag)
  21512. (:expand-quoted-html . org-export-html-expand)
  21513. (:timestamp . org-export-html-with-timestamp)
  21514. (:publishing-directory . org-export-publishing-directory)
  21515. (:preamble . org-export-html-preamble)
  21516. (:postamble . org-export-html-postamble)
  21517. (:auto-preamble . org-export-html-auto-preamble)
  21518. (:auto-postamble . org-export-html-auto-postamble)
  21519. (:author . user-full-name)
  21520. (:email . user-mail-address)))
  21521. (defun org-default-export-plist ()
  21522. "Return the property list with default settings for the export variables."
  21523. (let ((l org-export-plist-vars) rtn e)
  21524. (while (setq e (pop l))
  21525. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21526. rtn))
  21527. (defun org-infile-export-plist ()
  21528. "Return the property list with file-local settings for export."
  21529. (save-excursion
  21530. (save-restriction
  21531. (widen)
  21532. (goto-char 0)
  21533. (let ((re (org-make-options-regexp
  21534. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21535. p key val text options)
  21536. (while (re-search-forward re nil t)
  21537. (setq key (org-match-string-no-properties 1)
  21538. val (org-match-string-no-properties 2))
  21539. (cond
  21540. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21541. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21542. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21543. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21544. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21545. ((string-equal key "TEXT")
  21546. (setq text (if text (concat text "\n" val) val)))
  21547. ((string-equal key "OPTIONS") (setq options val))))
  21548. (setq p (plist-put p :text text))
  21549. (when options
  21550. (let ((op '(("H" . :headline-levels)
  21551. ("num" . :section-numbers)
  21552. ("toc" . :table-of-contents)
  21553. ("\\n" . :preserve-breaks)
  21554. ("@" . :expand-quoted-html)
  21555. (":" . :fixed-width)
  21556. ("|" . :tables)
  21557. ("^" . :sub-superscript)
  21558. ("-" . :special-strings)
  21559. ("f" . :footnotes)
  21560. ("d" . :drawers)
  21561. ("tags" . :tags)
  21562. ("*" . :emphasize)
  21563. ("TeX" . :TeX-macros)
  21564. ("LaTeX" . :LaTeX-fragments)
  21565. ("skip" . :skip-before-1st-heading)
  21566. ("author" . :author-info)
  21567. ("timestamp" . :time-stamp-file)))
  21568. o)
  21569. (while (setq o (pop op))
  21570. (if (string-match (concat (regexp-quote (car o))
  21571. ":\\([^ \t\n\r;,.]*\\)")
  21572. options)
  21573. (setq p (plist-put p (cdr o)
  21574. (car (read-from-string
  21575. (match-string 1 options)))))))))
  21576. p))))
  21577. (defun org-export-directory (type plist)
  21578. (let* ((val (plist-get plist :publishing-directory))
  21579. (dir (if (listp val)
  21580. (or (cdr (assoc type val)) ".")
  21581. val)))
  21582. dir))
  21583. (defun org-skip-comments (lines)
  21584. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21585. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21586. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21587. (case-fold-search nil)
  21588. rtn line level)
  21589. (while (setq line (pop lines))
  21590. (cond
  21591. ((and (string-match re1 line)
  21592. (setq level (- (match-end 1) (match-beginning 1))))
  21593. ;; Beginning of a COMMENT subtree. Skip it.
  21594. (while (and (setq line (pop lines))
  21595. (or (not (string-match re2 line))
  21596. (> (- (match-end 1) (match-beginning 1)) level))))
  21597. (setq lines (cons line lines)))
  21598. ((string-match "^#" line)
  21599. ;; an ordinary comment line
  21600. )
  21601. ((and org-export-table-remove-special-lines
  21602. (string-match "^[ \t]*|" line)
  21603. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21604. (and (string-match "| *<[0-9]+> *|" line)
  21605. (not (string-match "| *[^ <|]" line)))))
  21606. ;; a special table line that should be removed
  21607. )
  21608. (t (setq rtn (cons line rtn)))))
  21609. (nreverse rtn)))
  21610. (defun org-export (&optional arg)
  21611. (interactive)
  21612. (let ((help "[t] insert the export option template
  21613. \[v] limit export to visible part of outline tree
  21614. \[a] export as ASCII
  21615. \[h] export as HTML
  21616. \[H] export as HTML to temporary buffer
  21617. \[R] export region as HTML
  21618. \[b] export as HTML and browse immediately
  21619. \[x] export as XOXO
  21620. \[l] export as LaTeX
  21621. \[L] export as LaTeX to temporary buffer
  21622. \[i] export current file as iCalendar file
  21623. \[I] export all agenda files as iCalendar files
  21624. \[c] export agenda files into combined iCalendar file
  21625. \[F] publish current file
  21626. \[P] publish current project
  21627. \[X] publish... (project will be prompted for)
  21628. \[A] publish all projects")
  21629. (cmds
  21630. '((?t . org-insert-export-options-template)
  21631. (?v . org-export-visible)
  21632. (?a . org-export-as-ascii)
  21633. (?h . org-export-as-html)
  21634. (?b . org-export-as-html-and-open)
  21635. (?H . org-export-as-html-to-buffer)
  21636. (?R . org-export-region-as-html)
  21637. (?x . org-export-as-xoxo)
  21638. (?l . org-export-as-latex)
  21639. (?L . org-export-as-latex-to-buffer)
  21640. (?i . org-export-icalendar-this-file)
  21641. (?I . org-export-icalendar-all-agenda-files)
  21642. (?c . org-export-icalendar-combine-agenda-files)
  21643. (?F . org-publish-current-file)
  21644. (?P . org-publish-current-project)
  21645. (?X . org-publish)
  21646. (?A . org-publish-all)))
  21647. r1 r2 ass)
  21648. (save-window-excursion
  21649. (delete-other-windows)
  21650. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21651. (princ help))
  21652. (message "Select command: ")
  21653. (setq r1 (read-char-exclusive)))
  21654. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21655. (if (setq ass (assq r2 cmds))
  21656. (call-interactively (cdr ass))
  21657. (error "No command associated with key %c" r1))))
  21658. (defconst org-html-entities
  21659. '(("nbsp")
  21660. ("iexcl")
  21661. ("cent")
  21662. ("pound")
  21663. ("curren")
  21664. ("yen")
  21665. ("brvbar")
  21666. ("vert" . "&#124;")
  21667. ("sect")
  21668. ("uml")
  21669. ("copy")
  21670. ("ordf")
  21671. ("laquo")
  21672. ("not")
  21673. ("shy")
  21674. ("reg")
  21675. ("macr")
  21676. ("deg")
  21677. ("plusmn")
  21678. ("sup2")
  21679. ("sup3")
  21680. ("acute")
  21681. ("micro")
  21682. ("para")
  21683. ("middot")
  21684. ("odot"."o")
  21685. ("star"."*")
  21686. ("cedil")
  21687. ("sup1")
  21688. ("ordm")
  21689. ("raquo")
  21690. ("frac14")
  21691. ("frac12")
  21692. ("frac34")
  21693. ("iquest")
  21694. ("Agrave")
  21695. ("Aacute")
  21696. ("Acirc")
  21697. ("Atilde")
  21698. ("Auml")
  21699. ("Aring") ("AA"."&Aring;")
  21700. ("AElig")
  21701. ("Ccedil")
  21702. ("Egrave")
  21703. ("Eacute")
  21704. ("Ecirc")
  21705. ("Euml")
  21706. ("Igrave")
  21707. ("Iacute")
  21708. ("Icirc")
  21709. ("Iuml")
  21710. ("ETH")
  21711. ("Ntilde")
  21712. ("Ograve")
  21713. ("Oacute")
  21714. ("Ocirc")
  21715. ("Otilde")
  21716. ("Ouml")
  21717. ("times")
  21718. ("Oslash")
  21719. ("Ugrave")
  21720. ("Uacute")
  21721. ("Ucirc")
  21722. ("Uuml")
  21723. ("Yacute")
  21724. ("THORN")
  21725. ("szlig")
  21726. ("agrave")
  21727. ("aacute")
  21728. ("acirc")
  21729. ("atilde")
  21730. ("auml")
  21731. ("aring")
  21732. ("aelig")
  21733. ("ccedil")
  21734. ("egrave")
  21735. ("eacute")
  21736. ("ecirc")
  21737. ("euml")
  21738. ("igrave")
  21739. ("iacute")
  21740. ("icirc")
  21741. ("iuml")
  21742. ("eth")
  21743. ("ntilde")
  21744. ("ograve")
  21745. ("oacute")
  21746. ("ocirc")
  21747. ("otilde")
  21748. ("ouml")
  21749. ("divide")
  21750. ("oslash")
  21751. ("ugrave")
  21752. ("uacute")
  21753. ("ucirc")
  21754. ("uuml")
  21755. ("yacute")
  21756. ("thorn")
  21757. ("yuml")
  21758. ("fnof")
  21759. ("Alpha")
  21760. ("Beta")
  21761. ("Gamma")
  21762. ("Delta")
  21763. ("Epsilon")
  21764. ("Zeta")
  21765. ("Eta")
  21766. ("Theta")
  21767. ("Iota")
  21768. ("Kappa")
  21769. ("Lambda")
  21770. ("Mu")
  21771. ("Nu")
  21772. ("Xi")
  21773. ("Omicron")
  21774. ("Pi")
  21775. ("Rho")
  21776. ("Sigma")
  21777. ("Tau")
  21778. ("Upsilon")
  21779. ("Phi")
  21780. ("Chi")
  21781. ("Psi")
  21782. ("Omega")
  21783. ("alpha")
  21784. ("beta")
  21785. ("gamma")
  21786. ("delta")
  21787. ("epsilon")
  21788. ("varepsilon"."&epsilon;")
  21789. ("zeta")
  21790. ("eta")
  21791. ("theta")
  21792. ("iota")
  21793. ("kappa")
  21794. ("lambda")
  21795. ("mu")
  21796. ("nu")
  21797. ("xi")
  21798. ("omicron")
  21799. ("pi")
  21800. ("rho")
  21801. ("sigmaf") ("varsigma"."&sigmaf;")
  21802. ("sigma")
  21803. ("tau")
  21804. ("upsilon")
  21805. ("phi")
  21806. ("chi")
  21807. ("psi")
  21808. ("omega")
  21809. ("thetasym") ("vartheta"."&thetasym;")
  21810. ("upsih")
  21811. ("piv")
  21812. ("bull") ("bullet"."&bull;")
  21813. ("hellip") ("dots"."&hellip;")
  21814. ("prime")
  21815. ("Prime")
  21816. ("oline")
  21817. ("frasl")
  21818. ("weierp")
  21819. ("image")
  21820. ("real")
  21821. ("trade")
  21822. ("alefsym")
  21823. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21824. ("uarr") ("uparrow"."&uarr;")
  21825. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21826. ("darr")("downarrow"."&darr;")
  21827. ("harr") ("leftrightarrow"."&harr;")
  21828. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21829. ("lArr") ("Leftarrow"."&lArr;")
  21830. ("uArr") ("Uparrow"."&uArr;")
  21831. ("rArr") ("Rightarrow"."&rArr;")
  21832. ("dArr") ("Downarrow"."&dArr;")
  21833. ("hArr") ("Leftrightarrow"."&hArr;")
  21834. ("forall")
  21835. ("part") ("partial"."&part;")
  21836. ("exist") ("exists"."&exist;")
  21837. ("empty") ("emptyset"."&empty;")
  21838. ("nabla")
  21839. ("isin") ("in"."&isin;")
  21840. ("notin")
  21841. ("ni")
  21842. ("prod")
  21843. ("sum")
  21844. ("minus")
  21845. ("lowast") ("ast"."&lowast;")
  21846. ("radic")
  21847. ("prop") ("proptp"."&prop;")
  21848. ("infin") ("infty"."&infin;")
  21849. ("ang") ("angle"."&ang;")
  21850. ("and") ("wedge"."&and;")
  21851. ("or") ("vee"."&or;")
  21852. ("cap")
  21853. ("cup")
  21854. ("int")
  21855. ("there4")
  21856. ("sim")
  21857. ("cong") ("simeq"."&cong;")
  21858. ("asymp")("approx"."&asymp;")
  21859. ("ne") ("neq"."&ne;")
  21860. ("equiv")
  21861. ("le")
  21862. ("ge")
  21863. ("sub") ("subset"."&sub;")
  21864. ("sup") ("supset"."&sup;")
  21865. ("nsub")
  21866. ("sube")
  21867. ("supe")
  21868. ("oplus")
  21869. ("otimes")
  21870. ("perp")
  21871. ("sdot") ("cdot"."&sdot;")
  21872. ("lceil")
  21873. ("rceil")
  21874. ("lfloor")
  21875. ("rfloor")
  21876. ("lang")
  21877. ("rang")
  21878. ("loz") ("Diamond"."&loz;")
  21879. ("spades") ("spadesuit"."&spades;")
  21880. ("clubs") ("clubsuit"."&clubs;")
  21881. ("hearts") ("diamondsuit"."&hearts;")
  21882. ("diams") ("diamondsuit"."&diams;")
  21883. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  21884. ("quot")
  21885. ("amp")
  21886. ("lt")
  21887. ("gt")
  21888. ("OElig")
  21889. ("oelig")
  21890. ("Scaron")
  21891. ("scaron")
  21892. ("Yuml")
  21893. ("circ")
  21894. ("tilde")
  21895. ("ensp")
  21896. ("emsp")
  21897. ("thinsp")
  21898. ("zwnj")
  21899. ("zwj")
  21900. ("lrm")
  21901. ("rlm")
  21902. ("ndash")
  21903. ("mdash")
  21904. ("lsquo")
  21905. ("rsquo")
  21906. ("sbquo")
  21907. ("ldquo")
  21908. ("rdquo")
  21909. ("bdquo")
  21910. ("dagger")
  21911. ("Dagger")
  21912. ("permil")
  21913. ("lsaquo")
  21914. ("rsaquo")
  21915. ("euro")
  21916. ("arccos"."arccos")
  21917. ("arcsin"."arcsin")
  21918. ("arctan"."arctan")
  21919. ("arg"."arg")
  21920. ("cos"."cos")
  21921. ("cosh"."cosh")
  21922. ("cot"."cot")
  21923. ("coth"."coth")
  21924. ("csc"."csc")
  21925. ("deg"."deg")
  21926. ("det"."det")
  21927. ("dim"."dim")
  21928. ("exp"."exp")
  21929. ("gcd"."gcd")
  21930. ("hom"."hom")
  21931. ("inf"."inf")
  21932. ("ker"."ker")
  21933. ("lg"."lg")
  21934. ("lim"."lim")
  21935. ("liminf"."liminf")
  21936. ("limsup"."limsup")
  21937. ("ln"."ln")
  21938. ("log"."log")
  21939. ("max"."max")
  21940. ("min"."min")
  21941. ("Pr"."Pr")
  21942. ("sec"."sec")
  21943. ("sin"."sin")
  21944. ("sinh"."sinh")
  21945. ("sup"."sup")
  21946. ("tan"."tan")
  21947. ("tanh"."tanh")
  21948. )
  21949. "Entities for TeX->HTML translation.
  21950. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  21951. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  21952. In that case, \"\\ent\" will be translated to \"&other;\".
  21953. The list contains HTML entities for Latin-1, Greek and other symbols.
  21954. It is supplemented by a number of commonly used TeX macros with appropriate
  21955. translations. There is currently no way for users to extend this.")
  21956. ;;; General functions for all backends
  21957. (defun org-cleaned-string-for-export (string &rest parameters)
  21958. "Cleanup a buffer STRING so that links can be created safely."
  21959. (interactive)
  21960. (let* ((re-radio (and org-target-link-regexp
  21961. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  21962. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  21963. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  21964. (re-archive (concat ":" org-archive-tag ":"))
  21965. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  21966. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  21967. (htmlp (plist-get parameters :for-html))
  21968. (asciip (plist-get parameters :for-ascii))
  21969. (latexp (plist-get parameters :for-LaTeX))
  21970. (commentsp (plist-get parameters :comments))
  21971. (archived-trees (plist-get parameters :archived-trees))
  21972. (inhibit-read-only t)
  21973. (drawers org-drawers)
  21974. (exp-drawers (plist-get parameters :drawers))
  21975. (outline-regexp "\\*+ ")
  21976. a b xx
  21977. rtn p)
  21978. (with-current-buffer (get-buffer-create " org-mode-tmp")
  21979. (erase-buffer)
  21980. (insert string)
  21981. ;; Remove license-to-kill stuff
  21982. (while (setq p (text-property-any (point-min) (point-max)
  21983. :org-license-to-kill t))
  21984. (delete-region p (next-single-property-change p :org-license-to-kill)))
  21985. (let ((org-inhibit-startup t)) (org-mode))
  21986. (untabify (point-min) (point-max))
  21987. ;; Get the correct stuff before the first headline
  21988. (when (plist-get parameters :skip-before-1st-heading)
  21989. (goto-char (point-min))
  21990. (when (re-search-forward "^\\*+[ \t]" nil t)
  21991. (delete-region (point-min) (match-beginning 0))
  21992. (goto-char (point-min))
  21993. (insert "\n")))
  21994. (when (plist-get parameters :add-text)
  21995. (goto-char (point-min))
  21996. (insert (plist-get parameters :add-text) "\n"))
  21997. ;; Get rid of archived trees
  21998. (when (not (eq archived-trees t))
  21999. (goto-char (point-min))
  22000. (while (re-search-forward re-archive nil t)
  22001. (if (not (org-on-heading-p t))
  22002. (org-end-of-subtree t)
  22003. (beginning-of-line 1)
  22004. (setq a (if archived-trees
  22005. (1+ (point-at-eol)) (point))
  22006. b (org-end-of-subtree t))
  22007. (if (> b a) (delete-region a b)))))
  22008. ;; Get rid of drawers
  22009. (unless (eq t exp-drawers)
  22010. (goto-char (point-min))
  22011. (let ((re (concat "^[ \t]*:\\("
  22012. (mapconcat
  22013. 'identity
  22014. (org-delete-all exp-drawers
  22015. (copy-sequence drawers))
  22016. "\\|")
  22017. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22018. (while (re-search-forward re nil t)
  22019. (replace-match ""))))
  22020. ;; Find targets in comments and move them out of comments,
  22021. ;; but mark them as targets that should be invisible
  22022. (goto-char (point-min))
  22023. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22024. (replace-match "\\1(INVISIBLE)"))
  22025. ;; Protect backend specific stuff, throw away the others.
  22026. (let ((formatters
  22027. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22028. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22029. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22030. fmt)
  22031. (goto-char (point-min))
  22032. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22033. (goto-char (match-end 0))
  22034. (while (not (looking-at "#\\+END_EXAMPLE"))
  22035. (insert ": ")
  22036. (beginning-of-line 2)))
  22037. (goto-char (point-min))
  22038. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22039. (add-text-properties (match-beginning 0) (match-end 0)
  22040. '(org-protected t)))
  22041. (while formatters
  22042. (setq fmt (pop formatters))
  22043. (when (car fmt)
  22044. (goto-char (point-min))
  22045. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22046. ":[ \t]*\\(.*\\)") nil t)
  22047. (replace-match "\\1" t)
  22048. (add-text-properties
  22049. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22050. '(org-protected t))))
  22051. (goto-char (point-min))
  22052. (while (re-search-forward
  22053. (concat "^#\\+"
  22054. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22055. (cadddr fmt) "\\>.*\n?") nil t)
  22056. (if (car fmt)
  22057. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22058. '(org-protected t))
  22059. (delete-region (match-beginning 0) (match-end 0))))))
  22060. ;; Protect quoted subtrees
  22061. (goto-char (point-min))
  22062. (while (re-search-forward re-quote nil t)
  22063. (goto-char (match-beginning 0))
  22064. (end-of-line 1)
  22065. (add-text-properties (point) (org-end-of-subtree t)
  22066. '(org-protected t)))
  22067. ;; Protect verbatim elements
  22068. (goto-char (point-min))
  22069. (while (re-search-forward org-verbatim-re nil t)
  22070. (add-text-properties (match-beginning 4) (match-end 4)
  22071. '(org-protected t))
  22072. (goto-char (1+ (match-end 4))))
  22073. ;; Remove subtrees that are commented
  22074. (goto-char (point-min))
  22075. (while (re-search-forward re-commented nil t)
  22076. (goto-char (match-beginning 0))
  22077. (delete-region (point) (org-end-of-subtree t)))
  22078. ;; Remove special table lines
  22079. (when org-export-table-remove-special-lines
  22080. (goto-char (point-min))
  22081. (while (re-search-forward "^[ \t]*|" nil t)
  22082. (beginning-of-line 1)
  22083. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22084. (and (looking-at ".*?| *<[0-9]+> *|")
  22085. (not (looking-at ".*?| *[^ <|]"))))
  22086. (delete-region (max (point-min) (1- (point-at-bol)))
  22087. (point-at-eol))
  22088. (end-of-line 1))))
  22089. ;; Specific LaTeX stuff
  22090. (when latexp
  22091. (require 'org-export-latex nil)
  22092. (org-export-latex-cleaned-string))
  22093. (when asciip
  22094. (org-export-ascii-clean-string))
  22095. ;; Specific HTML stuff
  22096. (when htmlp
  22097. ;; Convert LaTeX fragments to images
  22098. (when (plist-get parameters :LaTeX-fragments)
  22099. (org-format-latex
  22100. (concat "ltxpng/" (file-name-sans-extension
  22101. (file-name-nondirectory
  22102. org-current-export-file)))
  22103. org-current-export-dir nil "Creating LaTeX image %s"))
  22104. (message "Exporting..."))
  22105. ;; Remove or replace comments
  22106. (goto-char (point-min))
  22107. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22108. (if commentsp
  22109. (progn (add-text-properties
  22110. (match-beginning 0) (match-end 0) '(org-protected t))
  22111. (replace-match (format commentsp (match-string 1)) t t))
  22112. (replace-match "")))
  22113. ;; Find matches for radio targets and turn them into internal links
  22114. (goto-char (point-min))
  22115. (when re-radio
  22116. (while (re-search-forward re-radio nil t)
  22117. (org-if-unprotected
  22118. (replace-match "\\1[[\\2]]"))))
  22119. ;; Find all links that contain a newline and put them into a single line
  22120. (goto-char (point-min))
  22121. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22122. (org-if-unprotected
  22123. (replace-match "\\1 \\3")
  22124. (goto-char (match-beginning 0))))
  22125. ;; Normalize links: Convert angle and plain links into bracket links
  22126. ;; Expand link abbreviations
  22127. (goto-char (point-min))
  22128. (while (re-search-forward re-plain-link nil t)
  22129. (goto-char (1- (match-end 0)))
  22130. (org-if-unprotected
  22131. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22132. ":" (match-string 3) "]]")))
  22133. ;; added 'org-link face to links
  22134. (put-text-property 0 (length s) 'face 'org-link s)
  22135. (replace-match s t t))))
  22136. (goto-char (point-min))
  22137. (while (re-search-forward re-angle-link nil t)
  22138. (goto-char (1- (match-end 0)))
  22139. (org-if-unprotected
  22140. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22141. ":" (match-string 3) "]]")))
  22142. (put-text-property 0 (length s) 'face 'org-link s)
  22143. (replace-match s t t))))
  22144. (goto-char (point-min))
  22145. (while (re-search-forward org-bracket-link-regexp nil t)
  22146. (org-if-unprotected
  22147. (let* ((s (concat "[[" (setq xx (save-match-data
  22148. (org-link-expand-abbrev (match-string 1))))
  22149. "]"
  22150. (if (match-end 3)
  22151. (match-string 2)
  22152. (concat "[" xx "]"))
  22153. "]")))
  22154. (put-text-property 0 (length s) 'face 'org-link s)
  22155. (replace-match s t t))))
  22156. ;; Find multiline emphasis and put them into single line
  22157. (when (plist-get parameters :emph-multiline)
  22158. (goto-char (point-min))
  22159. (while (re-search-forward org-emph-re nil t)
  22160. (if (not (= (char-after (match-beginning 3))
  22161. (char-after (match-beginning 4))))
  22162. (org-if-unprotected
  22163. (subst-char-in-region (match-beginning 0) (match-end 0)
  22164. ?\n ?\ t)
  22165. (goto-char (1- (match-end 0))))
  22166. (goto-char (1+ (match-beginning 0))))))
  22167. (setq rtn (buffer-string)))
  22168. (kill-buffer " org-mode-tmp")
  22169. rtn))
  22170. (defun org-export-grab-title-from-buffer ()
  22171. "Get a title for the current document, from looking at the buffer."
  22172. (let ((inhibit-read-only t))
  22173. (save-excursion
  22174. (goto-char (point-min))
  22175. (let ((end (save-excursion (outline-next-heading) (point))))
  22176. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22177. ;; Mark the line so that it will not be exported as normal text.
  22178. (org-unmodified
  22179. (add-text-properties (match-beginning 0) (match-end 0)
  22180. (list :org-license-to-kill t)))
  22181. ;; Return the title string
  22182. (org-trim (match-string 0)))))))
  22183. (defun org-export-get-title-from-subtree ()
  22184. "Return subtree title and exclude it from export."
  22185. (let (title (m (mark)))
  22186. (save-excursion
  22187. (goto-char (region-beginning))
  22188. (when (and (org-at-heading-p)
  22189. (>= (org-end-of-subtree t t) (region-end)))
  22190. ;; This is a subtree, we take the title from the first heading
  22191. (goto-char (region-beginning))
  22192. (looking-at org-todo-line-regexp)
  22193. (setq title (match-string 3))
  22194. (org-unmodified
  22195. (add-text-properties (point) (1+ (point-at-eol))
  22196. (list :org-license-to-kill t)))))
  22197. title))
  22198. (defun org-solidify-link-text (s &optional alist)
  22199. "Take link text and make a safe target out of it."
  22200. (save-match-data
  22201. (let* ((rtn
  22202. (mapconcat
  22203. 'identity
  22204. (org-split-string s "[ \t\r\n]+") "--"))
  22205. (a (assoc rtn alist)))
  22206. (or (cdr a) rtn))))
  22207. (defun org-get-min-level (lines)
  22208. "Get the minimum level in LINES."
  22209. (let ((re "^\\(\\*+\\) ") l min)
  22210. (catch 'exit
  22211. (while (setq l (pop lines))
  22212. (if (string-match re l)
  22213. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22214. 1)))
  22215. ;; Variable holding the vector with section numbers
  22216. (defvar org-section-numbers (make-vector org-level-max 0))
  22217. (defun org-init-section-numbers ()
  22218. "Initialize the vector for the section numbers."
  22219. (let* ((level -1)
  22220. (numbers (nreverse (org-split-string "" "\\.")))
  22221. (depth (1- (length org-section-numbers)))
  22222. (i depth) number-string)
  22223. (while (>= i 0)
  22224. (if (> i level)
  22225. (aset org-section-numbers i 0)
  22226. (setq number-string (or (car numbers) "0"))
  22227. (if (string-match "\\`[A-Z]\\'" number-string)
  22228. (aset org-section-numbers i
  22229. (- (string-to-char number-string) ?A -1))
  22230. (aset org-section-numbers i (string-to-number number-string)))
  22231. (pop numbers))
  22232. (setq i (1- i)))))
  22233. (defun org-section-number (&optional level)
  22234. "Return a string with the current section number.
  22235. When LEVEL is non-nil, increase section numbers on that level."
  22236. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22237. (when level
  22238. (when (> level -1)
  22239. (aset org-section-numbers
  22240. level (1+ (aref org-section-numbers level))))
  22241. (setq idx (1+ level))
  22242. (while (<= idx depth)
  22243. (if (not (= idx 1))
  22244. (aset org-section-numbers idx 0))
  22245. (setq idx (1+ idx))))
  22246. (setq idx 0)
  22247. (while (<= idx depth)
  22248. (setq n (aref org-section-numbers idx))
  22249. (setq string (concat string (if (not (string= string "")) "." "")
  22250. (int-to-string n)))
  22251. (setq idx (1+ idx)))
  22252. (save-match-data
  22253. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22254. (setq string (replace-match "" t nil string)))
  22255. (if (string-match "\\(\\.0\\)+\\'" string)
  22256. (setq string (replace-match "" t nil string))))
  22257. string))
  22258. ;;; ASCII export
  22259. (defvar org-last-level nil) ; dynamically scoped variable
  22260. (defvar org-min-level nil) ; dynamically scoped variable
  22261. (defvar org-levels-open nil) ; dynamically scoped parameter
  22262. (defvar org-ascii-current-indentation nil) ; For communication
  22263. (defun org-export-as-ascii (arg)
  22264. "Export the outline as a pretty ASCII file.
  22265. If there is an active region, export only the region.
  22266. The prefix ARG specifies how many levels of the outline should become
  22267. underlined headlines. The default is 3."
  22268. (interactive "P")
  22269. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22270. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22271. (org-infile-export-plist)))
  22272. (region-p (org-region-active-p))
  22273. (subtree-p
  22274. (when region-p
  22275. (save-excursion
  22276. (goto-char (region-beginning))
  22277. (and (org-at-heading-p)
  22278. (>= (org-end-of-subtree t t) (region-end))))))
  22279. (custom-times org-display-custom-times)
  22280. (org-ascii-current-indentation '(0 . 0))
  22281. (level 0) line txt
  22282. (umax nil)
  22283. (umax-toc nil)
  22284. (case-fold-search nil)
  22285. (filename (concat (file-name-as-directory
  22286. (org-export-directory :ascii opt-plist))
  22287. (file-name-sans-extension
  22288. (or (and subtree-p
  22289. (org-entry-get (region-beginning)
  22290. "EXPORT_FILE_NAME" t))
  22291. (file-name-nondirectory buffer-file-name)))
  22292. ".txt"))
  22293. (filename (if (equal (file-truename filename)
  22294. (file-truename buffer-file-name))
  22295. (concat filename ".txt")
  22296. filename))
  22297. (buffer (find-file-noselect filename))
  22298. (org-levels-open (make-vector org-level-max nil))
  22299. (odd org-odd-levels-only)
  22300. (date (plist-get opt-plist :date))
  22301. (author (plist-get opt-plist :author))
  22302. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22303. (plist-get opt-plist :title)
  22304. (and (not
  22305. (plist-get opt-plist :skip-before-1st-heading))
  22306. (org-export-grab-title-from-buffer))
  22307. (file-name-sans-extension
  22308. (file-name-nondirectory buffer-file-name))))
  22309. (email (plist-get opt-plist :email))
  22310. (language (plist-get opt-plist :language))
  22311. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22312. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22313. (todo nil)
  22314. (lang-words nil)
  22315. (region
  22316. (buffer-substring
  22317. (if (org-region-active-p) (region-beginning) (point-min))
  22318. (if (org-region-active-p) (region-end) (point-max))))
  22319. (lines (org-split-string
  22320. (org-cleaned-string-for-export
  22321. region
  22322. :for-ascii t
  22323. :skip-before-1st-heading
  22324. (plist-get opt-plist :skip-before-1st-heading)
  22325. :drawers (plist-get opt-plist :drawers)
  22326. :verbatim-multiline t
  22327. :archived-trees
  22328. (plist-get opt-plist :archived-trees)
  22329. :add-text (plist-get opt-plist :text))
  22330. "\n"))
  22331. thetoc have-headings first-heading-pos
  22332. table-open table-buffer)
  22333. (let ((inhibit-read-only t))
  22334. (org-unmodified
  22335. (remove-text-properties (point-min) (point-max)
  22336. '(:org-license-to-kill t))))
  22337. (setq org-min-level (org-get-min-level lines))
  22338. (setq org-last-level org-min-level)
  22339. (org-init-section-numbers)
  22340. (find-file-noselect filename)
  22341. (setq lang-words (or (assoc language org-export-language-setup)
  22342. (assoc "en" org-export-language-setup)))
  22343. (switch-to-buffer-other-window buffer)
  22344. (erase-buffer)
  22345. (fundamental-mode)
  22346. ;; create local variables for all options, to make sure all called
  22347. ;; functions get the correct information
  22348. (mapc (lambda (x)
  22349. (set (make-local-variable (cdr x))
  22350. (plist-get opt-plist (car x))))
  22351. org-export-plist-vars)
  22352. (org-set-local 'org-odd-levels-only odd)
  22353. (setq umax (if arg (prefix-numeric-value arg)
  22354. org-export-headline-levels))
  22355. (setq umax-toc (if (integerp org-export-with-toc)
  22356. (min org-export-with-toc umax)
  22357. umax))
  22358. ;; File header
  22359. (if title (org-insert-centered title ?=))
  22360. (insert "\n")
  22361. (if (and (or author email)
  22362. org-export-author-info)
  22363. (insert (concat (nth 1 lang-words) ": " (or author "")
  22364. (if email (concat " <" email ">") "")
  22365. "\n")))
  22366. (cond
  22367. ((and date (string-match "%" date))
  22368. (setq date (format-time-string date (current-time))))
  22369. (date)
  22370. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22371. (if (and date org-export-time-stamp-file)
  22372. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22373. (insert "\n\n")
  22374. (if org-export-with-toc
  22375. (progn
  22376. (push (concat (nth 3 lang-words) "\n") thetoc)
  22377. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22378. (mapc '(lambda (line)
  22379. (if (string-match org-todo-line-regexp
  22380. line)
  22381. ;; This is a headline
  22382. (progn
  22383. (setq have-headings t)
  22384. (setq level (- (match-end 1) (match-beginning 1))
  22385. level (org-tr-level level)
  22386. txt (match-string 3 line)
  22387. todo
  22388. (or (and org-export-mark-todo-in-toc
  22389. (match-beginning 2)
  22390. (not (member (match-string 2 line)
  22391. org-done-keywords)))
  22392. ; TODO, not DONE
  22393. (and org-export-mark-todo-in-toc
  22394. (= level umax-toc)
  22395. (org-search-todo-below
  22396. line lines level))))
  22397. (setq txt (org-html-expand-for-ascii txt))
  22398. (while (string-match org-bracket-link-regexp txt)
  22399. (setq txt
  22400. (replace-match
  22401. (match-string (if (match-end 2) 3 1) txt)
  22402. t t txt)))
  22403. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22404. (string-match
  22405. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22406. txt))
  22407. (setq txt (replace-match "" t t txt)))
  22408. (if (string-match quote-re0 txt)
  22409. (setq txt (replace-match "" t t txt)))
  22410. (if org-export-with-section-numbers
  22411. (setq txt (concat (org-section-number level)
  22412. " " txt)))
  22413. (if (<= level umax-toc)
  22414. (progn
  22415. (push
  22416. (concat
  22417. (make-string
  22418. (* (max 0 (- level org-min-level)) 4) ?\ )
  22419. (format (if todo "%s (*)\n" "%s\n") txt))
  22420. thetoc)
  22421. (setq org-last-level level))
  22422. ))))
  22423. lines)
  22424. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22425. (org-init-section-numbers)
  22426. (while (setq line (pop lines))
  22427. ;; Remove the quoted HTML tags.
  22428. (setq line (org-html-expand-for-ascii line))
  22429. ;; Remove targets
  22430. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22431. (setq line (replace-match "" t t line)))
  22432. ;; Replace internal links
  22433. (while (string-match org-bracket-link-regexp line)
  22434. (setq line (replace-match
  22435. (if (match-end 3) "[\\3]" "[\\1]")
  22436. t nil line)))
  22437. (when custom-times
  22438. (setq line (org-translate-time line)))
  22439. (cond
  22440. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22441. ;; a Headline
  22442. (setq first-heading-pos (or first-heading-pos (point)))
  22443. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22444. txt (match-string 2 line))
  22445. (org-ascii-level-start level txt umax lines))
  22446. ((and org-export-with-tables
  22447. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22448. (if (not table-open)
  22449. ;; New table starts
  22450. (setq table-open t table-buffer nil))
  22451. ;; Accumulate lines
  22452. (setq table-buffer (cons line table-buffer))
  22453. (when (or (not lines)
  22454. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22455. (car lines))))
  22456. (setq table-open nil
  22457. table-buffer (nreverse table-buffer))
  22458. (insert (mapconcat
  22459. (lambda (x)
  22460. (org-fix-indentation x org-ascii-current-indentation))
  22461. (org-format-table-ascii table-buffer)
  22462. "\n") "\n")))
  22463. (t
  22464. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22465. (if (and org-export-with-fixed-width
  22466. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22467. (setq line (replace-match "\\1" nil nil line)))
  22468. (insert line "\n"))))
  22469. (normal-mode)
  22470. ;; insert the table of contents
  22471. (when thetoc
  22472. (goto-char (point-min))
  22473. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22474. (progn
  22475. (goto-char (match-beginning 0))
  22476. (replace-match ""))
  22477. (goto-char first-heading-pos))
  22478. (mapc 'insert thetoc)
  22479. (or (looking-at "[ \t]*\n[ \t]*\n")
  22480. (insert "\n\n")))
  22481. ;; Convert whitespace place holders
  22482. (goto-char (point-min))
  22483. (let (beg end)
  22484. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22485. (setq end (next-single-property-change beg 'org-whitespace))
  22486. (goto-char beg)
  22487. (delete-region beg end)
  22488. (insert (make-string (- end beg) ?\ ))))
  22489. (save-buffer)
  22490. ;; remove display and invisible chars
  22491. (let (beg end)
  22492. (goto-char (point-min))
  22493. (while (setq beg (next-single-property-change (point) 'display))
  22494. (setq end (next-single-property-change beg 'display))
  22495. (delete-region beg end)
  22496. (goto-char beg)
  22497. (insert "=>"))
  22498. (goto-char (point-min))
  22499. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22500. (setq end (next-single-property-change beg 'org-cwidth))
  22501. (delete-region beg end)
  22502. (goto-char beg)))
  22503. (goto-char (point-min))))
  22504. (defun org-export-ascii-clean-string ()
  22505. "Do extra work for ASCII export"
  22506. (goto-char (point-min))
  22507. (while (re-search-forward org-verbatim-re nil t)
  22508. (goto-char (match-end 2))
  22509. (backward-delete-char 1) (insert "'")
  22510. (goto-char (match-beginning 2))
  22511. (delete-char 1) (insert "`")
  22512. (goto-char (match-end 2))))
  22513. (defun org-search-todo-below (line lines level)
  22514. "Search the subtree below LINE for any TODO entries."
  22515. (let ((rest (cdr (memq line lines)))
  22516. (re org-todo-line-regexp)
  22517. line lv todo)
  22518. (catch 'exit
  22519. (while (setq line (pop rest))
  22520. (if (string-match re line)
  22521. (progn
  22522. (setq lv (- (match-end 1) (match-beginning 1))
  22523. todo (and (match-beginning 2)
  22524. (not (member (match-string 2 line)
  22525. org-done-keywords))))
  22526. ; TODO, not DONE
  22527. (if (<= lv level) (throw 'exit nil))
  22528. (if todo (throw 'exit t))))))))
  22529. (defun org-html-expand-for-ascii (line)
  22530. "Handle quoted HTML for ASCII export."
  22531. (if org-export-html-expand
  22532. (while (string-match "@<[^<>\n]*>" line)
  22533. ;; We just remove the tags for now.
  22534. (setq line (replace-match "" nil nil line))))
  22535. line)
  22536. (defun org-insert-centered (s &optional underline)
  22537. "Insert the string S centered and underline it with character UNDERLINE."
  22538. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22539. (insert (make-string ind ?\ ) s "\n")
  22540. (if underline
  22541. (insert (make-string ind ?\ )
  22542. (make-string (string-width s) underline)
  22543. "\n"))))
  22544. (defun org-ascii-level-start (level title umax &optional lines)
  22545. "Insert a new level in ASCII export."
  22546. (let (char (n (- level umax 1)) (ind 0))
  22547. (if (> level umax)
  22548. (progn
  22549. (insert (make-string (* 2 n) ?\ )
  22550. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22551. org-export-ascii-bullets))
  22552. " " title "\n")
  22553. ;; find the indentation of the next non-empty line
  22554. (catch 'stop
  22555. (while lines
  22556. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22557. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22558. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22559. (pop lines)))
  22560. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22561. (if (or (not (equal (char-before) ?\n))
  22562. (not (equal (char-before (1- (point))) ?\n)))
  22563. (insert "\n"))
  22564. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22565. (unless org-export-with-tags
  22566. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22567. (setq title (replace-match "" t t title))))
  22568. (if org-export-with-section-numbers
  22569. (setq title (concat (org-section-number level) " " title)))
  22570. (insert title "\n" (make-string (string-width title) char) "\n")
  22571. (setq org-ascii-current-indentation '(0 . 0)))))
  22572. (defun org-export-visible (type arg)
  22573. "Create a copy of the visible part of the current buffer, and export it.
  22574. The copy is created in a temporary buffer and removed after use.
  22575. TYPE is the final key (as a string) that also select the export command in
  22576. the `C-c C-e' export dispatcher.
  22577. As a special case, if the you type SPC at the prompt, the temporary
  22578. org-mode file will not be removed but presented to you so that you can
  22579. continue to use it. The prefix arg ARG is passed through to the exporting
  22580. command."
  22581. (interactive
  22582. (list (progn
  22583. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22584. (read-char-exclusive))
  22585. current-prefix-arg))
  22586. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22587. (error "Invalid export key"))
  22588. (let* ((binding (cdr (assoc type
  22589. '((?a . org-export-as-ascii)
  22590. (?\C-a . org-export-as-ascii)
  22591. (?b . org-export-as-html-and-open)
  22592. (?\C-b . org-export-as-html-and-open)
  22593. (?h . org-export-as-html)
  22594. (?H . org-export-as-html-to-buffer)
  22595. (?R . org-export-region-as-html)
  22596. (?x . org-export-as-xoxo)))))
  22597. (keepp (equal type ?\ ))
  22598. (file buffer-file-name)
  22599. (buffer (get-buffer-create "*Org Export Visible*"))
  22600. s e)
  22601. ;; Need to hack the drawers here.
  22602. (save-excursion
  22603. (goto-char (point-min))
  22604. (while (re-search-forward org-drawer-regexp nil t)
  22605. (goto-char (match-beginning 1))
  22606. (or (org-invisible-p) (org-flag-drawer nil))))
  22607. (with-current-buffer buffer (erase-buffer))
  22608. (save-excursion
  22609. (setq s (goto-char (point-min)))
  22610. (while (not (= (point) (point-max)))
  22611. (goto-char (org-find-invisible))
  22612. (append-to-buffer buffer s (point))
  22613. (setq s (goto-char (org-find-visible))))
  22614. (org-cycle-hide-drawers 'all)
  22615. (goto-char (point-min))
  22616. (unless keepp
  22617. ;; Copy all comment lines to the end, to make sure #+ settings are
  22618. ;; still available for the second export step. Kind of a hack, but
  22619. ;; does do the trick.
  22620. (if (looking-at "#[^\r\n]*")
  22621. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22622. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22623. (append-to-buffer buffer (1+ (match-beginning 0))
  22624. (min (point-max) (1+ (match-end 0))))))
  22625. (set-buffer buffer)
  22626. (let ((buffer-file-name file)
  22627. (org-inhibit-startup t))
  22628. (org-mode)
  22629. (show-all)
  22630. (unless keepp (funcall binding arg))))
  22631. (if (not keepp)
  22632. (kill-buffer buffer)
  22633. (switch-to-buffer-other-window buffer)
  22634. (goto-char (point-min)))))
  22635. (defun org-find-visible ()
  22636. (let ((s (point)))
  22637. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22638. (get-char-property s 'invisible)))
  22639. s))
  22640. (defun org-find-invisible ()
  22641. (let ((s (point)))
  22642. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22643. (not (get-char-property s 'invisible))))
  22644. s))
  22645. ;;; HTML export
  22646. (defun org-get-current-options ()
  22647. "Return a string with current options as keyword options.
  22648. Does include HTML export options as well as TODO and CATEGORY stuff."
  22649. (format
  22650. "#+TITLE: %s
  22651. #+AUTHOR: %s
  22652. #+EMAIL: %s
  22653. #+LANGUAGE: %s
  22654. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22655. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s d:%s tags:%s
  22656. #+CATEGORY: %s
  22657. #+SEQ_TODO: %s
  22658. #+TYP_TODO: %s
  22659. #+PRIORITIES: %c %c %c
  22660. #+DRAWERS: %s
  22661. #+STARTUP: %s %s %s %s %s
  22662. #+TAGS: %s
  22663. #+ARCHIVE: %s
  22664. #+LINK: %s
  22665. "
  22666. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22667. org-export-headline-levels
  22668. org-export-with-section-numbers
  22669. org-export-with-toc
  22670. org-export-preserve-breaks
  22671. org-export-html-expand
  22672. org-export-with-fixed-width
  22673. org-export-with-tables
  22674. org-export-with-sub-superscripts
  22675. org-export-with-special-strings
  22676. org-export-with-footnotes
  22677. org-export-with-emphasize
  22678. org-export-with-TeX-macros
  22679. org-export-with-LaTeX-fragments
  22680. org-export-skip-text-before-1st-heading
  22681. org-export-with-drawers
  22682. org-export-with-tags
  22683. (file-name-nondirectory buffer-file-name)
  22684. "TODO FEEDBACK VERIFY DONE"
  22685. "Me Jason Marie DONE"
  22686. org-highest-priority org-lowest-priority org-default-priority
  22687. (mapconcat 'identity org-drawers " ")
  22688. (cdr (assoc org-startup-folded
  22689. '((nil . "showall") (t . "overview") (content . "content"))))
  22690. (if org-odd-levels-only "odd" "oddeven")
  22691. (if org-hide-leading-stars "hidestars" "showstars")
  22692. (if org-startup-align-all-tables "align" "noalign")
  22693. (cond ((eq t org-log-done) "logdone")
  22694. ((not org-log-done) "nologging")
  22695. ((listp org-log-done)
  22696. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  22697. org-log-done " ")))
  22698. (or (mapconcat (lambda (x)
  22699. (cond
  22700. ((equal '(:startgroup) x) "{")
  22701. ((equal '(:endgroup) x) "}")
  22702. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22703. (t (car x))))
  22704. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22705. org-archive-location
  22706. "org file:~/org/%s.org"
  22707. ))
  22708. (defun org-insert-export-options-template ()
  22709. "Insert into the buffer a template with information for exporting."
  22710. (interactive)
  22711. (if (not (bolp)) (newline))
  22712. (let ((s (org-get-current-options)))
  22713. (and (string-match "#\\+CATEGORY" s)
  22714. (setq s (substring s 0 (match-beginning 0))))
  22715. (insert s)))
  22716. (defun org-toggle-fixed-width-section (arg)
  22717. "Toggle the fixed-width export.
  22718. If there is no active region, the QUOTE keyword at the current headline is
  22719. inserted or removed. When present, it causes the text between this headline
  22720. and the next to be exported as fixed-width text, and unmodified.
  22721. If there is an active region, this command adds or removes a colon as the
  22722. first character of this line. If the first character of a line is a colon,
  22723. this line is also exported in fixed-width font."
  22724. (interactive "P")
  22725. (let* ((cc 0)
  22726. (regionp (org-region-active-p))
  22727. (beg (if regionp (region-beginning) (point)))
  22728. (end (if regionp (region-end)))
  22729. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22730. (case-fold-search nil)
  22731. (re "[ \t]*\\(:\\)")
  22732. off)
  22733. (if regionp
  22734. (save-excursion
  22735. (goto-char beg)
  22736. (setq cc (current-column))
  22737. (beginning-of-line 1)
  22738. (setq off (looking-at re))
  22739. (while (> nlines 0)
  22740. (setq nlines (1- nlines))
  22741. (beginning-of-line 1)
  22742. (cond
  22743. (arg
  22744. (move-to-column cc t)
  22745. (insert ":\n")
  22746. (forward-line -1))
  22747. ((and off (looking-at re))
  22748. (replace-match "" t t nil 1))
  22749. ((not off) (move-to-column cc t) (insert ":")))
  22750. (forward-line 1)))
  22751. (save-excursion
  22752. (org-back-to-heading)
  22753. (if (looking-at (concat outline-regexp
  22754. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22755. (replace-match "" t t nil 1)
  22756. (if (looking-at outline-regexp)
  22757. (progn
  22758. (goto-char (match-end 0))
  22759. (insert org-quote-string " "))))))))
  22760. (defun org-export-as-html-and-open (arg)
  22761. "Export the outline as HTML and immediately open it with a browser.
  22762. If there is an active region, export only the region.
  22763. The prefix ARG specifies how many levels of the outline should become
  22764. headlines. The default is 3. Lower levels will become bulleted lists."
  22765. (interactive "P")
  22766. (org-export-as-html arg 'hidden)
  22767. (org-open-file buffer-file-name))
  22768. (defun org-export-as-html-batch ()
  22769. "Call `org-export-as-html', may be used in batch processing as
  22770. emacs --batch
  22771. --load=$HOME/lib/emacs/org.el
  22772. --eval \"(setq org-export-headline-levels 2)\"
  22773. --visit=MyFile --funcall org-export-as-html-batch"
  22774. (org-export-as-html org-export-headline-levels 'hidden))
  22775. (defun org-export-as-html-to-buffer (arg)
  22776. "Call `org-exort-as-html` with output to a temporary buffer.
  22777. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22778. (interactive "P")
  22779. (org-export-as-html arg nil nil "*Org HTML Export*")
  22780. (switch-to-buffer-other-window "*Org HTML Export*"))
  22781. (defun org-replace-region-by-html (beg end)
  22782. "Assume the current region has org-mode syntax, and convert it to HTML.
  22783. This can be used in any buffer. For example, you could write an
  22784. itemized list in org-mode syntax in an HTML buffer and then use this
  22785. command to convert it."
  22786. (interactive "r")
  22787. (let (reg html buf pop-up-frames)
  22788. (save-window-excursion
  22789. (if (org-mode-p)
  22790. (setq html (org-export-region-as-html
  22791. beg end t 'string))
  22792. (setq reg (buffer-substring beg end)
  22793. buf (get-buffer-create "*Org tmp*"))
  22794. (with-current-buffer buf
  22795. (erase-buffer)
  22796. (insert reg)
  22797. (org-mode)
  22798. (setq html (org-export-region-as-html
  22799. (point-min) (point-max) t 'string)))
  22800. (kill-buffer buf)))
  22801. (delete-region beg end)
  22802. (insert html)))
  22803. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22804. "Convert region from BEG to END in org-mode buffer to HTML.
  22805. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22806. contents, and only produce the region of converted text, useful for
  22807. cut-and-paste operations.
  22808. If BUFFER is a buffer or a string, use/create that buffer as a target
  22809. of the converted HTML. If BUFFER is the symbol `string', return the
  22810. produced HTML as a string and leave not buffer behind. For example,
  22811. a Lisp program could call this function in the following way:
  22812. (setq html (org-export-region-as-html beg end t 'string))
  22813. When called interactively, the output buffer is selected, and shown
  22814. in a window. A non-interactive call will only retunr the buffer."
  22815. (interactive "r\nP")
  22816. (when (interactive-p)
  22817. (setq buffer "*Org HTML Export*"))
  22818. (let ((transient-mark-mode t) (zmacs-regions t)
  22819. rtn)
  22820. (goto-char end)
  22821. (set-mark (point)) ;; to activate the region
  22822. (goto-char beg)
  22823. (setq rtn (org-export-as-html
  22824. nil nil nil
  22825. buffer body-only))
  22826. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22827. (if (and (interactive-p) (bufferp rtn))
  22828. (switch-to-buffer-other-window rtn)
  22829. rtn)))
  22830. (defvar html-table-tag nil) ; dynamically scoped into this.
  22831. (defun org-export-as-html (arg &optional hidden ext-plist
  22832. to-buffer body-only)
  22833. "Export the outline as a pretty HTML file.
  22834. If there is an active region, export only the region. The prefix
  22835. ARG specifies how many levels of the outline should become
  22836. headlines. The default is 3. Lower levels will become bulleted
  22837. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22838. EXT-PLIST is a property list with external parameters overriding
  22839. org-mode's default settings, but still inferior to file-local
  22840. settings. When TO-BUFFER is non-nil, create a buffer with that
  22841. name and export to that buffer. If TO-BUFFER is the symbol `string',
  22842. don't leave any buffer behind but just return the resulting HTML as
  22843. a string. When BODY-ONLY is set, don't produce the file header and footer,
  22844. simply return the content of <body>...</body>, without even
  22845. the body tags themselves."
  22846. (interactive "P")
  22847. ;; Make sure we have a file name when we need it.
  22848. (when (and (not (or to-buffer body-only))
  22849. (not buffer-file-name))
  22850. (if (buffer-base-buffer)
  22851. (org-set-local 'buffer-file-name
  22852. (with-current-buffer (buffer-base-buffer)
  22853. buffer-file-name))
  22854. (error "Need a file name to be able to export.")))
  22855. (message "Exporting...")
  22856. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22857. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  22858. (setq-default org-done-keywords org-done-keywords)
  22859. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  22860. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22861. ext-plist
  22862. (org-infile-export-plist)))
  22863. (style (plist-get opt-plist :style))
  22864. (link-validate (plist-get opt-plist :link-validation-function))
  22865. valid thetoc have-headings first-heading-pos
  22866. (odd org-odd-levels-only)
  22867. (region-p (org-region-active-p))
  22868. (subtree-p
  22869. (when region-p
  22870. (save-excursion
  22871. (goto-char (region-beginning))
  22872. (and (org-at-heading-p)
  22873. (>= (org-end-of-subtree t t) (region-end))))))
  22874. ;; The following two are dynamically scoped into other
  22875. ;; routines below.
  22876. (org-current-export-dir (org-export-directory :html opt-plist))
  22877. (org-current-export-file buffer-file-name)
  22878. (level 0) (line "") (origline "") txt todo
  22879. (umax nil)
  22880. (umax-toc nil)
  22881. (filename (if to-buffer nil
  22882. (expand-file-name
  22883. (concat
  22884. (file-name-sans-extension
  22885. (or (and subtree-p
  22886. (org-entry-get (region-beginning)
  22887. "EXPORT_FILE_NAME" t))
  22888. (file-name-nondirectory buffer-file-name)))
  22889. "." org-export-html-extension)
  22890. (file-name-as-directory
  22891. (org-export-directory :html opt-plist)))))
  22892. (current-dir (if buffer-file-name
  22893. (file-name-directory buffer-file-name)
  22894. default-directory))
  22895. (buffer (if to-buffer
  22896. (cond
  22897. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  22898. (t (get-buffer-create to-buffer)))
  22899. (find-file-noselect filename)))
  22900. (org-levels-open (make-vector org-level-max nil))
  22901. (date (plist-get opt-plist :date))
  22902. (author (plist-get opt-plist :author))
  22903. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22904. (plist-get opt-plist :title)
  22905. (and (not
  22906. (plist-get opt-plist :skip-before-1st-heading))
  22907. (org-export-grab-title-from-buffer))
  22908. (and buffer-file-name
  22909. (file-name-sans-extension
  22910. (file-name-nondirectory buffer-file-name)))
  22911. "UNTITLED"))
  22912. (html-table-tag (plist-get opt-plist :html-table-tag))
  22913. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22914. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  22915. (inquote nil)
  22916. (infixed nil)
  22917. (in-local-list nil)
  22918. (local-list-num nil)
  22919. (local-list-indent nil)
  22920. (llt org-plain-list-ordered-item-terminator)
  22921. (email (plist-get opt-plist :email))
  22922. (language (plist-get opt-plist :language))
  22923. (lang-words nil)
  22924. (target-alist nil) tg
  22925. (head-count 0) cnt
  22926. (start 0)
  22927. (coding-system (and (boundp 'buffer-file-coding-system)
  22928. buffer-file-coding-system))
  22929. (coding-system-for-write (or org-export-html-coding-system
  22930. coding-system))
  22931. (save-buffer-coding-system (or org-export-html-coding-system
  22932. coding-system))
  22933. (charset (and coding-system-for-write
  22934. (fboundp 'coding-system-get)
  22935. (coding-system-get coding-system-for-write
  22936. 'mime-charset)))
  22937. (region
  22938. (buffer-substring
  22939. (if region-p (region-beginning) (point-min))
  22940. (if region-p (region-end) (point-max))))
  22941. (lines
  22942. (org-split-string
  22943. (org-cleaned-string-for-export
  22944. region
  22945. :emph-multiline t
  22946. :for-html t
  22947. :skip-before-1st-heading
  22948. (plist-get opt-plist :skip-before-1st-heading)
  22949. :drawers (plist-get opt-plist :drawers)
  22950. :archived-trees
  22951. (plist-get opt-plist :archived-trees)
  22952. :add-text
  22953. (plist-get opt-plist :text)
  22954. :LaTeX-fragments
  22955. (plist-get opt-plist :LaTeX-fragments))
  22956. "[\r\n]"))
  22957. table-open type
  22958. table-buffer table-orig-buffer
  22959. ind start-is-num starter didclose
  22960. rpl path desc descp desc1 desc2 link
  22961. )
  22962. (let ((inhibit-read-only t))
  22963. (org-unmodified
  22964. (remove-text-properties (point-min) (point-max)
  22965. '(:org-license-to-kill t))))
  22966. (message "Exporting...")
  22967. (setq org-min-level (org-get-min-level lines))
  22968. (setq org-last-level org-min-level)
  22969. (org-init-section-numbers)
  22970. (cond
  22971. ((and date (string-match "%" date))
  22972. (setq date (format-time-string date (current-time))))
  22973. (date)
  22974. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22975. ;; Get the language-dependent settings
  22976. (setq lang-words (or (assoc language org-export-language-setup)
  22977. (assoc "en" org-export-language-setup)))
  22978. ;; Switch to the output buffer
  22979. (set-buffer buffer)
  22980. (let ((inhibit-read-only t)) (erase-buffer))
  22981. (fundamental-mode)
  22982. (and (fboundp 'set-buffer-file-coding-system)
  22983. (set-buffer-file-coding-system coding-system-for-write))
  22984. (let ((case-fold-search nil)
  22985. (org-odd-levels-only odd))
  22986. ;; create local variables for all options, to make sure all called
  22987. ;; functions get the correct information
  22988. (mapc (lambda (x)
  22989. (set (make-local-variable (cdr x))
  22990. (plist-get opt-plist (car x))))
  22991. org-export-plist-vars)
  22992. (setq umax (if arg (prefix-numeric-value arg)
  22993. org-export-headline-levels))
  22994. (setq umax-toc (if (integerp org-export-with-toc)
  22995. (min org-export-with-toc umax)
  22996. umax))
  22997. (unless body-only
  22998. ;; File header
  22999. (insert (format
  23000. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23001. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23002. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23003. lang=\"%s\" xml:lang=\"%s\">
  23004. <head>
  23005. <title>%s</title>
  23006. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23007. <meta name=\"generator\" content=\"Org-mode\"/>
  23008. <meta name=\"generated\" content=\"%s\"/>
  23009. <meta name=\"author\" content=\"%s\"/>
  23010. %s
  23011. </head><body>
  23012. "
  23013. language language (org-html-expand title)
  23014. (or charset "iso-8859-1") date author style))
  23015. (insert (or (plist-get opt-plist :preamble) ""))
  23016. (when (plist-get opt-plist :auto-preamble)
  23017. (if title (insert (format org-export-html-title-format
  23018. (org-html-expand title))))))
  23019. (if (and org-export-with-toc (not body-only))
  23020. (progn
  23021. (push (format "<h%d>%s</h%d>\n"
  23022. org-export-html-toplevel-hlevel
  23023. (nth 3 lang-words)
  23024. org-export-html-toplevel-hlevel)
  23025. thetoc)
  23026. (push "<ul>\n<li>" thetoc)
  23027. (setq lines
  23028. (mapcar '(lambda (line)
  23029. (if (string-match org-todo-line-regexp line)
  23030. ;; This is a headline
  23031. (progn
  23032. (setq have-headings t)
  23033. (setq level (- (match-end 1) (match-beginning 1))
  23034. level (org-tr-level level)
  23035. txt (save-match-data
  23036. (org-html-expand
  23037. (org-export-cleanup-toc-line
  23038. (match-string 3 line))))
  23039. todo
  23040. (or (and org-export-mark-todo-in-toc
  23041. (match-beginning 2)
  23042. (not (member (match-string 2 line)
  23043. org-done-keywords)))
  23044. ; TODO, not DONE
  23045. (and org-export-mark-todo-in-toc
  23046. (= level umax-toc)
  23047. (org-search-todo-below
  23048. line lines level))))
  23049. (if (string-match
  23050. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23051. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23052. (if (string-match quote-re0 txt)
  23053. (setq txt (replace-match "" t t txt)))
  23054. (if org-export-with-section-numbers
  23055. (setq txt (concat (org-section-number level)
  23056. " " txt)))
  23057. (if (<= level (max umax umax-toc))
  23058. (setq head-count (+ head-count 1)))
  23059. (if (<= level umax-toc)
  23060. (progn
  23061. (if (> level org-last-level)
  23062. (progn
  23063. (setq cnt (- level org-last-level))
  23064. (while (>= (setq cnt (1- cnt)) 0)
  23065. (push "\n<ul>\n<li>" thetoc))
  23066. (push "\n" thetoc)))
  23067. (if (< level org-last-level)
  23068. (progn
  23069. (setq cnt (- org-last-level level))
  23070. (while (>= (setq cnt (1- cnt)) 0)
  23071. (push "</li>\n</ul>" thetoc))
  23072. (push "\n" thetoc)))
  23073. ;; Check for targets
  23074. (while (string-match org-target-regexp line)
  23075. (setq tg (match-string 1 line)
  23076. line (replace-match
  23077. (concat "@<span class=\"target\">" tg "@</span> ")
  23078. t t line))
  23079. (push (cons (org-solidify-link-text tg)
  23080. (format "sec-%d" head-count))
  23081. target-alist))
  23082. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23083. (setq txt (replace-match "" t t txt)))
  23084. (push
  23085. (format
  23086. (if todo
  23087. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23088. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23089. head-count txt) thetoc)
  23090. (setq org-last-level level))
  23091. )))
  23092. line)
  23093. lines))
  23094. (while (> org-last-level (1- org-min-level))
  23095. (setq org-last-level (1- org-last-level))
  23096. (push "</li>\n</ul>\n" thetoc))
  23097. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23098. (setq head-count 0)
  23099. (org-init-section-numbers)
  23100. (while (setq line (pop lines) origline line)
  23101. (catch 'nextline
  23102. ;; end of quote section?
  23103. (when (and inquote (string-match "^\\*+ " line))
  23104. (insert "</pre>\n")
  23105. (setq inquote nil))
  23106. ;; inside a quote section?
  23107. (when inquote
  23108. (insert (org-html-protect line) "\n")
  23109. (throw 'nextline nil))
  23110. ;; verbatim lines
  23111. (when (and org-export-with-fixed-width
  23112. (string-match "^[ \t]*:\\(.*\\)" line))
  23113. (when (not infixed)
  23114. (setq infixed t)
  23115. (insert "<pre>\n"))
  23116. (insert (org-html-protect (match-string 1 line)) "\n")
  23117. (when (and lines
  23118. (not (string-match "^[ \t]*\\(:.*\\)"
  23119. (car lines))))
  23120. (setq infixed nil)
  23121. (insert "</pre>\n"))
  23122. (throw 'nextline nil))
  23123. ;; Protected HTML
  23124. (when (get-text-property 0 'org-protected line)
  23125. (let (par)
  23126. (when (re-search-backward
  23127. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23128. (setq par (match-string 1))
  23129. (replace-match "\\2\n"))
  23130. (insert line "\n")
  23131. (while (and lines
  23132. (or (= (length (car lines)) 0)
  23133. (get-text-property 0 'org-protected (car lines))))
  23134. (insert (pop lines) "\n"))
  23135. (and par (insert "<p>\n")))
  23136. (throw 'nextline nil))
  23137. ;; Horizontal line
  23138. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23139. (insert "\n<hr/>\n")
  23140. (throw 'nextline nil))
  23141. ;; make targets to anchors
  23142. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23143. (cond
  23144. ((match-end 2)
  23145. (setq line (replace-match
  23146. (concat "@<a name=\""
  23147. (org-solidify-link-text (match-string 1 line))
  23148. "\">\\nbsp@</a>")
  23149. t t line)))
  23150. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23151. (setq line (replace-match
  23152. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23153. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23154. t t line)))
  23155. (t
  23156. (setq line (replace-match
  23157. (concat "@<a name=\""
  23158. (org-solidify-link-text (match-string 1 line))
  23159. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23160. t t line)))))
  23161. (setq line (org-html-handle-time-stamps line))
  23162. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23163. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23164. ;; Also handle sub_superscripts and checkboxes
  23165. (or (string-match org-table-hline-regexp line)
  23166. (setq line (org-html-expand line)))
  23167. ;; Format the links
  23168. (setq start 0)
  23169. (while (string-match org-bracket-link-analytic-regexp line start)
  23170. (setq start (match-beginning 0))
  23171. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23172. (setq path (match-string 3 line))
  23173. (setq desc1 (if (match-end 5) (match-string 5 line))
  23174. desc2 (if (match-end 2) (concat type ":" path) path)
  23175. descp (and desc1 (not (equal desc1 desc2)))
  23176. desc (or desc1 desc2))
  23177. ;; Make an image out of the description if that is so wanted
  23178. (when (and descp (org-file-image-p desc))
  23179. (save-match-data
  23180. (if (string-match "^file:" desc)
  23181. (setq desc (substring desc (match-end 0)))))
  23182. (setq desc (concat "<img src=\"" desc "\"/>")))
  23183. ;; FIXME: do we need to unescape here somewhere?
  23184. (cond
  23185. ((equal type "internal")
  23186. (setq rpl
  23187. (concat
  23188. "<a href=\"#"
  23189. (org-solidify-link-text
  23190. (save-match-data (org-link-unescape path)) target-alist)
  23191. "\">" desc "</a>")))
  23192. ((member type '("http" "https"))
  23193. ;; standard URL, just check if we need to inline an image
  23194. (if (and (or (eq t org-export-html-inline-images)
  23195. (and org-export-html-inline-images (not descp)))
  23196. (org-file-image-p path))
  23197. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23198. (setq link (concat type ":" path))
  23199. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23200. ((member type '("ftp" "mailto" "news"))
  23201. ;; standard URL
  23202. (setq link (concat type ":" path))
  23203. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23204. ((string= type "file")
  23205. ;; FILE link
  23206. (let* ((filename path)
  23207. (abs-p (file-name-absolute-p filename))
  23208. thefile file-is-image-p search)
  23209. (save-match-data
  23210. (if (string-match "::\\(.*\\)" filename)
  23211. (setq search (match-string 1 filename)
  23212. filename (replace-match "" t nil filename)))
  23213. (setq valid
  23214. (if (functionp link-validate)
  23215. (funcall link-validate filename current-dir)
  23216. t))
  23217. (setq file-is-image-p (org-file-image-p filename))
  23218. (setq thefile (if abs-p (expand-file-name filename) filename))
  23219. (when (and org-export-html-link-org-files-as-html
  23220. (string-match "\\.org$" thefile))
  23221. (setq thefile (concat (substring thefile 0
  23222. (match-beginning 0))
  23223. "." org-export-html-extension))
  23224. (if (and search
  23225. ;; make sure this is can be used as target search
  23226. (not (string-match "^[0-9]*$" search))
  23227. (not (string-match "^\\*" search))
  23228. (not (string-match "^/.*/$" search)))
  23229. (setq thefile (concat thefile "#"
  23230. (org-solidify-link-text
  23231. (org-link-unescape search)))))
  23232. (when (string-match "^file:" desc)
  23233. (setq desc (replace-match "" t t desc))
  23234. (if (string-match "\\.org$" desc)
  23235. (setq desc (replace-match "" t t desc))))))
  23236. (setq rpl (if (and file-is-image-p
  23237. (or (eq t org-export-html-inline-images)
  23238. (and org-export-html-inline-images
  23239. (not descp))))
  23240. (concat "<img src=\"" thefile "\"/>")
  23241. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23242. (if (not valid) (setq rpl desc))))
  23243. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23244. (setq rpl (concat "<i>&lt;" type ":"
  23245. (save-match-data (org-link-unescape path))
  23246. "&gt;</i>"))))
  23247. (setq line (replace-match rpl t t line)
  23248. start (+ start (length rpl))))
  23249. ;; TODO items
  23250. (if (and (string-match org-todo-line-regexp line)
  23251. (match-beginning 2))
  23252. (setq line
  23253. (concat (substring line 0 (match-beginning 2))
  23254. "<span class=\""
  23255. (if (member (match-string 2 line)
  23256. org-done-keywords)
  23257. "done" "todo")
  23258. "\">" (match-string 2 line)
  23259. "</span>" (substring line (match-end 2)))))
  23260. ;; Does this contain a reference to a footnote?
  23261. (when org-export-with-footnotes
  23262. (setq start 0)
  23263. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23264. (if (get-text-property (match-beginning 2) 'org-protected line)
  23265. (setq start (match-end 2))
  23266. (let ((n (match-string 2 line)))
  23267. (setq line
  23268. (replace-match
  23269. (format
  23270. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23271. (match-string 1 line) n n n)
  23272. t t line))))))
  23273. (cond
  23274. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23275. ;; This is a headline
  23276. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23277. txt (match-string 2 line))
  23278. (if (string-match quote-re0 txt)
  23279. (setq txt (replace-match "" t t txt)))
  23280. (if (<= level (max umax umax-toc))
  23281. (setq head-count (+ head-count 1)))
  23282. (when in-local-list
  23283. ;; Close any local lists before inserting a new header line
  23284. (while local-list-num
  23285. (org-close-li)
  23286. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23287. (pop local-list-num))
  23288. (setq local-list-indent nil
  23289. in-local-list nil))
  23290. (setq first-heading-pos (or first-heading-pos (point)))
  23291. (org-html-level-start level txt umax
  23292. (and org-export-with-toc (<= level umax))
  23293. head-count)
  23294. ;; QUOTES
  23295. (when (string-match quote-re line)
  23296. (insert "<pre>")
  23297. (setq inquote t)))
  23298. ((and org-export-with-tables
  23299. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23300. (if (not table-open)
  23301. ;; New table starts
  23302. (setq table-open t table-buffer nil table-orig-buffer nil))
  23303. ;; Accumulate lines
  23304. (setq table-buffer (cons line table-buffer)
  23305. table-orig-buffer (cons origline table-orig-buffer))
  23306. (when (or (not lines)
  23307. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23308. (car lines))))
  23309. (setq table-open nil
  23310. table-buffer (nreverse table-buffer)
  23311. table-orig-buffer (nreverse table-orig-buffer))
  23312. (org-close-par-maybe)
  23313. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23314. (t
  23315. ;; Normal lines
  23316. (when (string-match
  23317. (cond
  23318. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23319. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23320. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23321. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23322. line)
  23323. (setq ind (org-get-string-indentation line)
  23324. start-is-num (match-beginning 4)
  23325. starter (if (match-beginning 2)
  23326. (substring (match-string 2 line) 0 -1))
  23327. line (substring line (match-beginning 5)))
  23328. (unless (string-match "[^ \t]" line)
  23329. ;; empty line. Pretend indentation is large.
  23330. (setq ind (if org-empty-line-terminates-plain-lists
  23331. 0
  23332. (1+ (or (car local-list-indent) 1)))))
  23333. (setq didclose nil)
  23334. (while (and in-local-list
  23335. (or (and (= ind (car local-list-indent))
  23336. (not starter))
  23337. (< ind (car local-list-indent))))
  23338. (setq didclose t)
  23339. (org-close-li)
  23340. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23341. (pop local-list-num) (pop local-list-indent)
  23342. (setq in-local-list local-list-indent))
  23343. (cond
  23344. ((and starter
  23345. (or (not in-local-list)
  23346. (> ind (car local-list-indent))))
  23347. ;; Start new (level of) list
  23348. (org-close-par-maybe)
  23349. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23350. (push start-is-num local-list-num)
  23351. (push ind local-list-indent)
  23352. (setq in-local-list t))
  23353. (starter
  23354. ;; continue current list
  23355. (org-close-li)
  23356. (insert "<li>\n"))
  23357. (didclose
  23358. ;; we did close a list, normal text follows: need <p>
  23359. (org-open-par)))
  23360. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23361. (setq line
  23362. (replace-match
  23363. (if (equal (match-string 1 line) "X")
  23364. "<b>[X]</b>"
  23365. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23366. t t line))))
  23367. ;; Empty lines start a new paragraph. If hand-formatted lists
  23368. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23369. ;; also start a new paragraph.
  23370. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23371. ;; Is this the start of a footnote?
  23372. (when org-export-with-footnotes
  23373. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23374. (org-close-par-maybe)
  23375. (let ((n (match-string 1 line)))
  23376. (setq line (replace-match
  23377. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23378. ;; Check if the line break needs to be conserved
  23379. (cond
  23380. ((string-match "\\\\\\\\[ \t]*$" line)
  23381. (setq line (replace-match "<br/>" t t line)))
  23382. (org-export-preserve-breaks
  23383. (setq line (concat line "<br/>"))))
  23384. (insert line "\n")))))
  23385. ;; Properly close all local lists and other lists
  23386. (when inquote (insert "</pre>\n"))
  23387. (when in-local-list
  23388. ;; Close any local lists before inserting a new header line
  23389. (while local-list-num
  23390. (org-close-li)
  23391. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23392. (pop local-list-num))
  23393. (setq local-list-indent nil
  23394. in-local-list nil))
  23395. (org-html-level-start 1 nil umax
  23396. (and org-export-with-toc (<= level umax))
  23397. head-count)
  23398. (unless body-only
  23399. (when (plist-get opt-plist :auto-postamble)
  23400. (insert "<div id=\"postamble\">")
  23401. (when (and org-export-author-info author)
  23402. (insert "<p class=\"author\"> "
  23403. (nth 1 lang-words) ": " author "\n")
  23404. (when email
  23405. (if (listp (split-string email ",+ *"))
  23406. (mapc (lambda(e)
  23407. (insert "<a href=\"mailto:" e "\">&lt;"
  23408. e "&gt;</a>\n"))
  23409. (split-string email ",+ *"))
  23410. (insert "<a href=\"mailto:" email "\">&lt;"
  23411. email "&gt;</a>\n")))
  23412. (insert "</p>\n"))
  23413. (when (and date org-export-time-stamp-file)
  23414. (insert "<p class=\"date\"> "
  23415. (nth 2 lang-words) ": "
  23416. date "</p>\n"))
  23417. (insert "</div>"))
  23418. (if org-export-html-with-timestamp
  23419. (insert org-export-html-html-helper-timestamp))
  23420. (insert (or (plist-get opt-plist :postamble) ""))
  23421. (insert "</body>\n</html>\n"))
  23422. (normal-mode)
  23423. (if (eq major-mode default-major-mode) (html-mode))
  23424. ;; insert the table of contents
  23425. (goto-char (point-min))
  23426. (when thetoc
  23427. (if (or (re-search-forward
  23428. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23429. (re-search-forward
  23430. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23431. (progn
  23432. (goto-char (match-beginning 0))
  23433. (replace-match ""))
  23434. (goto-char first-heading-pos)
  23435. (when (looking-at "\\s-*</p>")
  23436. (goto-char (match-end 0))
  23437. (insert "\n")))
  23438. (insert "<div id=\"table-of-contents\">\n")
  23439. (mapc 'insert thetoc)
  23440. (insert "</div>\n"))
  23441. ;; remove empty paragraphs and lists
  23442. (goto-char (point-min))
  23443. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23444. (replace-match ""))
  23445. (goto-char (point-min))
  23446. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23447. (replace-match ""))
  23448. ;; Convert whitespace place holders
  23449. (goto-char (point-min))
  23450. (let (beg end n)
  23451. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23452. (setq n (get-text-property beg 'org-whitespace)
  23453. end (next-single-property-change beg 'org-whitespace))
  23454. (goto-char beg)
  23455. (delete-region beg end)
  23456. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23457. (make-string n ?x)))))
  23458. (or to-buffer (save-buffer))
  23459. (goto-char (point-min))
  23460. (message "Exporting... done")
  23461. (if (eq to-buffer 'string)
  23462. (prog1 (buffer-substring (point-min) (point-max))
  23463. (kill-buffer (current-buffer)))
  23464. (current-buffer)))))
  23465. (defvar org-table-colgroup-info nil)
  23466. (defun org-format-table-ascii (lines)
  23467. "Format a table for ascii export."
  23468. (if (stringp lines)
  23469. (setq lines (org-split-string lines "\n")))
  23470. (if (not (string-match "^[ \t]*|" (car lines)))
  23471. ;; Table made by table.el - test for spanning
  23472. lines
  23473. ;; A normal org table
  23474. ;; Get rid of hlines at beginning and end
  23475. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23476. (setq lines (nreverse lines))
  23477. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23478. (setq lines (nreverse lines))
  23479. (when org-export-table-remove-special-lines
  23480. ;; Check if the table has a marking column. If yes remove the
  23481. ;; column and the special lines
  23482. (setq lines (org-table-clean-before-export lines)))
  23483. ;; Get rid of the vertical lines except for grouping
  23484. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23485. rtn line vl1 start)
  23486. (while (setq line (pop lines))
  23487. (if (string-match org-table-hline-regexp line)
  23488. (and (string-match "|\\(.*\\)|" line)
  23489. (setq line (replace-match " \\1" t nil line)))
  23490. (setq start 0 vl1 vl)
  23491. (while (string-match "|" line start)
  23492. (setq start (match-end 0))
  23493. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23494. (push line rtn))
  23495. (nreverse rtn))))
  23496. (defun org-colgroup-info-to-vline-list (info)
  23497. (let (vl new last)
  23498. (while info
  23499. (setq last new new (pop info))
  23500. (if (or (memq last '(:end :startend))
  23501. (memq new '(:start :startend)))
  23502. (push t vl)
  23503. (push nil vl)))
  23504. (setq vl (nreverse vl))
  23505. (and vl (setcar vl nil))
  23506. vl))
  23507. (defun org-format-table-html (lines olines)
  23508. "Find out which HTML converter to use and return the HTML code."
  23509. (if (stringp lines)
  23510. (setq lines (org-split-string lines "\n")))
  23511. (if (string-match "^[ \t]*|" (car lines))
  23512. ;; A normal org table
  23513. (org-format-org-table-html lines)
  23514. ;; Table made by table.el - test for spanning
  23515. (let* ((hlines (delq nil (mapcar
  23516. (lambda (x)
  23517. (if (string-match "^[ \t]*\\+-" x) x
  23518. nil))
  23519. lines)))
  23520. (first (car hlines))
  23521. (ll (and (string-match "\\S-+" first)
  23522. (match-string 0 first)))
  23523. (re (concat "^[ \t]*" (regexp-quote ll)))
  23524. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23525. hlines))))
  23526. (if (and (not spanning)
  23527. (not org-export-prefer-native-exporter-for-tables))
  23528. ;; We can use my own converter with HTML conversions
  23529. (org-format-table-table-html lines)
  23530. ;; Need to use the code generator in table.el, with the original text.
  23531. (org-format-table-table-html-using-table-generate-source olines)))))
  23532. (defun org-format-org-table-html (lines &optional splice)
  23533. "Format a table into HTML."
  23534. ;; Get rid of hlines at beginning and end
  23535. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23536. (setq lines (nreverse lines))
  23537. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23538. (setq lines (nreverse lines))
  23539. (when org-export-table-remove-special-lines
  23540. ;; Check if the table has a marking column. If yes remove the
  23541. ;; column and the special lines
  23542. (setq lines (org-table-clean-before-export lines)))
  23543. (let ((head (and org-export-highlight-first-table-line
  23544. (delq nil (mapcar
  23545. (lambda (x) (string-match "^[ \t]*|-" x))
  23546. (cdr lines)))))
  23547. (nlines 0) fnum i
  23548. tbopen line fields html gr colgropen)
  23549. (if splice (setq head nil))
  23550. (unless splice (push (if head "<thead>" "<tbody>") html))
  23551. (setq tbopen t)
  23552. (while (setq line (pop lines))
  23553. (catch 'next-line
  23554. (if (string-match "^[ \t]*|-" line)
  23555. (progn
  23556. (unless splice
  23557. (push (if head "</thead>" "</tbody>") html)
  23558. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23559. (setq head nil) ;; head ends here, first time around
  23560. ;; ignore this line
  23561. (throw 'next-line t)))
  23562. ;; Break the line into fields
  23563. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23564. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23565. (setq nlines (1+ nlines) i -1)
  23566. (push (concat "<tr>"
  23567. (mapconcat
  23568. (lambda (x)
  23569. (setq i (1+ i))
  23570. (if (and (< i nlines)
  23571. (string-match org-table-number-regexp x))
  23572. (incf (aref fnum i)))
  23573. (if head
  23574. (concat (car org-export-table-header-tags) x
  23575. (cdr org-export-table-header-tags))
  23576. (concat (car org-export-table-data-tags) x
  23577. (cdr org-export-table-data-tags))))
  23578. fields "")
  23579. "</tr>")
  23580. html)))
  23581. (unless splice (if tbopen (push "</tbody>" html)))
  23582. (unless splice (push "</table>\n" html))
  23583. (setq html (nreverse html))
  23584. (unless splice
  23585. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23586. (push (mapconcat
  23587. (lambda (x)
  23588. (setq gr (pop org-table-colgroup-info))
  23589. (format "%s<col align=\"%s\"></col>%s"
  23590. (if (memq gr '(:start :startend))
  23591. (prog1
  23592. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23593. (setq colgropen t))
  23594. "")
  23595. (if (> (/ (float x) nlines) org-table-number-fraction)
  23596. "right" "left")
  23597. (if (memq gr '(:end :startend))
  23598. (progn (setq colgropen nil) "</colgroup>")
  23599. "")))
  23600. fnum "")
  23601. html)
  23602. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23603. (push html-table-tag html))
  23604. (concat (mapconcat 'identity html "\n") "\n")))
  23605. (defun org-table-clean-before-export (lines)
  23606. "Check if the table has a marking column.
  23607. If yes remove the column and the special lines."
  23608. (setq org-table-colgroup-info nil)
  23609. (if (memq nil
  23610. (mapcar
  23611. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23612. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23613. lines))
  23614. (progn
  23615. (setq org-table-clean-did-remove-column nil)
  23616. (delq nil
  23617. (mapcar
  23618. (lambda (x)
  23619. (cond
  23620. ((string-match "^[ \t]*| */ *|" x)
  23621. (setq org-table-colgroup-info
  23622. (mapcar (lambda (x)
  23623. (cond ((member x '("<" "&lt;")) :start)
  23624. ((member x '(">" "&gt;")) :end)
  23625. ((member x '("<>" "&lt;&gt;")) :startend)
  23626. (t nil)))
  23627. (org-split-string x "[ \t]*|[ \t]*")))
  23628. nil)
  23629. (t x)))
  23630. lines)))
  23631. (setq org-table-clean-did-remove-column t)
  23632. (delq nil
  23633. (mapcar
  23634. (lambda (x)
  23635. (cond
  23636. ((string-match "^[ \t]*| */ *|" x)
  23637. (setq org-table-colgroup-info
  23638. (mapcar (lambda (x)
  23639. (cond ((member x '("<" "&lt;")) :start)
  23640. ((member x '(">" "&gt;")) :end)
  23641. ((member x '("<>" "&lt;&gt;")) :startend)
  23642. (t nil)))
  23643. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23644. nil)
  23645. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23646. nil) ; ignore this line
  23647. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23648. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23649. ;; remove the first column
  23650. (replace-match "\\1|" t nil x))))
  23651. lines))))
  23652. (defun org-format-table-table-html (lines)
  23653. "Format a table generated by table.el into HTML.
  23654. This conversion does *not* use `table-generate-source' from table.el.
  23655. This has the advantage that Org-mode's HTML conversions can be used.
  23656. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23657. (let (line field-buffer
  23658. (head org-export-highlight-first-table-line)
  23659. fields html empty)
  23660. (setq html (concat html-table-tag "\n"))
  23661. (while (setq line (pop lines))
  23662. (setq empty "&nbsp;")
  23663. (catch 'next-line
  23664. (if (string-match "^[ \t]*\\+-" line)
  23665. (progn
  23666. (if field-buffer
  23667. (progn
  23668. (setq
  23669. html
  23670. (concat
  23671. html
  23672. "<tr>"
  23673. (mapconcat
  23674. (lambda (x)
  23675. (if (equal x "") (setq x empty))
  23676. (if head
  23677. (concat (car org-export-table-header-tags) x
  23678. (cdr org-export-table-header-tags))
  23679. (concat (car org-export-table-data-tags) x
  23680. (cdr org-export-table-data-tags))))
  23681. field-buffer "\n")
  23682. "</tr>\n"))
  23683. (setq head nil)
  23684. (setq field-buffer nil)))
  23685. ;; Ignore this line
  23686. (throw 'next-line t)))
  23687. ;; Break the line into fields and store the fields
  23688. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23689. (if field-buffer
  23690. (setq field-buffer (mapcar
  23691. (lambda (x)
  23692. (concat x "<br/>" (pop fields)))
  23693. field-buffer))
  23694. (setq field-buffer fields))))
  23695. (setq html (concat html "</table>\n"))
  23696. html))
  23697. (defun org-format-table-table-html-using-table-generate-source (lines)
  23698. "Format a table into html, using `table-generate-source' from table.el.
  23699. This has the advantage that cell- or row-spanning is allowed.
  23700. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23701. (require 'table)
  23702. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23703. (erase-buffer)
  23704. (insert (mapconcat 'identity lines "\n"))
  23705. (goto-char (point-min))
  23706. (if (not (re-search-forward "|[^+]" nil t))
  23707. (error "Error processing table"))
  23708. (table-recognize-table)
  23709. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23710. (table-generate-source 'html " org-tmp2 ")
  23711. (set-buffer " org-tmp2 ")
  23712. (buffer-substring (point-min) (point-max))))
  23713. (defun org-html-handle-time-stamps (s)
  23714. "Format time stamps in string S, or remove them."
  23715. (catch 'exit
  23716. (let (r b)
  23717. (while (string-match org-maybe-keyword-time-regexp s)
  23718. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23719. ;; never export CLOCK
  23720. (throw 'exit ""))
  23721. (or b (setq b (substring s 0 (match-beginning 0))))
  23722. (if (not org-export-with-timestamps)
  23723. (setq r (concat r (substring s 0 (match-beginning 0)))
  23724. s (substring s (match-end 0)))
  23725. (setq r (concat
  23726. r (substring s 0 (match-beginning 0))
  23727. (if (match-end 1)
  23728. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23729. (match-string 1 s)))
  23730. (format " @<span class=\"timestamp\">%s@</span>"
  23731. (substring
  23732. (org-translate-time (match-string 3 s)) 1 -1)))
  23733. s (substring s (match-end 0)))))
  23734. ;; Line break if line started and ended with time stamp stuff
  23735. (if (not r)
  23736. s
  23737. (setq r (concat r s))
  23738. (unless (string-match "\\S-" (concat b s))
  23739. (setq r (concat r "@<br/>")))
  23740. r))))
  23741. (defun org-html-protect (s)
  23742. ;; convert & to &amp;, < to &lt; and > to &gt;
  23743. (let ((start 0))
  23744. (while (string-match "&" s start)
  23745. (setq s (replace-match "&amp;" t t s)
  23746. start (1+ (match-beginning 0))))
  23747. (while (string-match "<" s)
  23748. (setq s (replace-match "&lt;" t t s)))
  23749. (while (string-match ">" s)
  23750. (setq s (replace-match "&gt;" t t s))))
  23751. s)
  23752. (defun org-export-cleanup-toc-line (s)
  23753. "Remove tags and time staps from lines going into the toc."
  23754. (when (memq org-export-with-tags '(not-in-toc nil))
  23755. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23756. (setq s (replace-match "" t t s))))
  23757. (when org-export-remove-timestamps-from-toc
  23758. (while (string-match org-maybe-keyword-time-regexp s)
  23759. (setq s (replace-match "" t t s))))
  23760. (while (string-match org-bracket-link-regexp s)
  23761. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23762. t t s)))
  23763. s)
  23764. (defun org-html-expand (string)
  23765. "Prepare STRING for HTML export. Applies all active conversions.
  23766. If there are links in the string, don't modify these."
  23767. (let* ((re (concat org-bracket-link-regexp "\\|"
  23768. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23769. m s l res)
  23770. (while (setq m (string-match re string))
  23771. (setq s (substring string 0 m)
  23772. l (match-string 0 string)
  23773. string (substring string (match-end 0)))
  23774. (push (org-html-do-expand s) res)
  23775. (push l res))
  23776. (push (org-html-do-expand string) res)
  23777. (apply 'concat (nreverse res))))
  23778. (defun org-html-do-expand (s)
  23779. "Apply all active conversions to translate special ASCII to HTML."
  23780. (setq s (org-html-protect s))
  23781. (if org-export-html-expand
  23782. (let ((start 0))
  23783. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23784. (setq s (replace-match "<\\1>" t nil s)))))
  23785. (if org-export-with-emphasize
  23786. (setq s (org-export-html-convert-emphasize s)))
  23787. (if org-export-with-special-strings
  23788. (setq s (org-export-html-convert-special-strings s)))
  23789. (if org-export-with-sub-superscripts
  23790. (setq s (org-export-html-convert-sub-super s)))
  23791. (if org-export-with-TeX-macros
  23792. (let ((start 0) wd ass)
  23793. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23794. (if (get-text-property (match-beginning 0) 'org-protected s)
  23795. (setq start (match-end 0))
  23796. (setq wd (match-string 1 s))
  23797. (if (setq ass (assoc wd org-html-entities))
  23798. (setq s (replace-match (or (cdr ass)
  23799. (concat "&" (car ass) ";"))
  23800. t t s))
  23801. (setq start (+ start (length wd))))))))
  23802. s)
  23803. (defun org-create-multibrace-regexp (left right n)
  23804. "Create a regular expression which will match a balanced sexp.
  23805. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23806. as single character strings.
  23807. The regexp returned will match the entire expression including the
  23808. delimiters. It will also define a single group which contains the
  23809. match except for the outermost delimiters. The maximum depth of
  23810. stacked delimiters is N. Escaping delimiters is not possible."
  23811. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23812. (or "\\|")
  23813. (re nothing)
  23814. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23815. (while (> n 1)
  23816. (setq n (1- n)
  23817. re (concat re or next)
  23818. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23819. (concat left "\\(" re "\\)" right)))
  23820. (defvar org-match-substring-regexp
  23821. (concat
  23822. "\\([^\\]\\)\\([_^]\\)\\("
  23823. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23824. "\\|"
  23825. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23826. "\\|"
  23827. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23828. "The regular expression matching a sub- or superscript.")
  23829. (defvar org-match-substring-with-braces-regexp
  23830. (concat
  23831. "\\([^\\]\\)\\([_^]\\)\\("
  23832. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23833. "\\)")
  23834. "The regular expression matching a sub- or superscript, forcing braces.")
  23835. (defconst org-export-html-special-string-regexps
  23836. '(("\\\\-" . "&shy;")
  23837. ("---\\([^-]\\)" . "&mdash;\\1")
  23838. ("--\\([^-]\\)" . "&ndash;\\1")
  23839. ("\\.\\.\\." . "&hellip;"))
  23840. "Regular expressions for special string conversion.")
  23841. (defun org-export-html-convert-special-strings (string)
  23842. "Convert special characters in STRING to HTML."
  23843. (let ((all org-export-html-special-string-regexps)
  23844. e a re rpl start)
  23845. (while (setq a (pop all))
  23846. (setq re (car a) rpl (cdr a) start 0)
  23847. (while (string-match re string start)
  23848. (if (get-text-property (match-beginning 0) 'org-protected string)
  23849. (setq start (match-end 0))
  23850. (setq string (replace-match rpl t nil string)))))
  23851. string))
  23852. (defun org-export-html-convert-sub-super (string)
  23853. "Convert sub- and superscripts in STRING to HTML."
  23854. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  23855. (while (string-match org-match-substring-regexp string s)
  23856. (cond
  23857. ((and requireb (match-end 8)) (setq s (match-end 2)))
  23858. ((get-text-property (match-beginning 2) 'org-protected string)
  23859. (setq s (match-end 2)))
  23860. (t
  23861. (setq s (match-end 1)
  23862. key (if (string= (match-string 2 string) "_") "sub" "sup")
  23863. c (or (match-string 8 string)
  23864. (match-string 6 string)
  23865. (match-string 5 string))
  23866. string (replace-match
  23867. (concat (match-string 1 string)
  23868. "<" key ">" c "</" key ">")
  23869. t t string)))))
  23870. (while (string-match "\\\\\\([_^]\\)" string)
  23871. (setq string (replace-match (match-string 1 string) t t string)))
  23872. string))
  23873. (defun org-export-html-convert-emphasize (string)
  23874. "Apply emphasis."
  23875. (let ((s 0) rpl)
  23876. (while (string-match org-emph-re string s)
  23877. (if (not (equal
  23878. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  23879. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  23880. (setq s (match-beginning 0)
  23881. rpl
  23882. (concat
  23883. (match-string 1 string)
  23884. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  23885. (match-string 4 string)
  23886. (nth 3 (assoc (match-string 3 string)
  23887. org-emphasis-alist))
  23888. (match-string 5 string))
  23889. string (replace-match rpl t t string)
  23890. s (+ s (- (length rpl) 2)))
  23891. (setq s (1+ s))))
  23892. string))
  23893. (defvar org-par-open nil)
  23894. (defun org-open-par ()
  23895. "Insert <p>, but first close previous paragraph if any."
  23896. (org-close-par-maybe)
  23897. (insert "\n<p>")
  23898. (setq org-par-open t))
  23899. (defun org-close-par-maybe ()
  23900. "Close paragraph if there is one open."
  23901. (when org-par-open
  23902. (insert "</p>")
  23903. (setq org-par-open nil)))
  23904. (defun org-close-li ()
  23905. "Close <li> if necessary."
  23906. (org-close-par-maybe)
  23907. (insert "</li>\n"))
  23908. (defvar body-only) ; dynamically scoped into this.
  23909. (defun org-html-level-start (level title umax with-toc head-count)
  23910. "Insert a new level in HTML export.
  23911. When TITLE is nil, just close all open levels."
  23912. (org-close-par-maybe)
  23913. (let ((l org-level-max))
  23914. (while (>= l level)
  23915. (if (aref org-levels-open (1- l))
  23916. (progn
  23917. (org-html-level-close l umax)
  23918. (aset org-levels-open (1- l) nil)))
  23919. (setq l (1- l)))
  23920. (when title
  23921. ;; If title is nil, this means this function is called to close
  23922. ;; all levels, so the rest is done only if title is given
  23923. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23924. (setq title (replace-match
  23925. (if org-export-with-tags
  23926. (save-match-data
  23927. (concat
  23928. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  23929. (mapconcat 'identity (org-split-string
  23930. (match-string 1 title) ":")
  23931. "&nbsp;")
  23932. "</span>"))
  23933. "")
  23934. t t title)))
  23935. (if (> level umax)
  23936. (progn
  23937. (if (aref org-levels-open (1- level))
  23938. (progn
  23939. (org-close-li)
  23940. (insert "<li>" title "<br/>\n"))
  23941. (aset org-levels-open (1- level) t)
  23942. (org-close-par-maybe)
  23943. (insert "<ul>\n<li>" title "<br/>\n")))
  23944. (aset org-levels-open (1- level) t)
  23945. (if (and org-export-with-section-numbers (not body-only))
  23946. (setq title (concat (org-section-number level) " " title)))
  23947. (setq level (+ level org-export-html-toplevel-hlevel -1))
  23948. (if with-toc
  23949. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  23950. level level head-count title level))
  23951. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  23952. (org-open-par)))))
  23953. (defun org-html-level-close (level max-outline-level)
  23954. "Terminate one level in HTML export."
  23955. (if (<= level max-outline-level)
  23956. (insert "</div>\n")
  23957. (org-close-li)
  23958. (insert "</ul>\n")))
  23959. ;;; iCalendar export
  23960. ;;;###autoload
  23961. (defun org-export-icalendar-this-file ()
  23962. "Export current file as an iCalendar file.
  23963. The iCalendar file will be located in the same directory as the Org-mode
  23964. file, but with extension `.ics'."
  23965. (interactive)
  23966. (org-export-icalendar nil buffer-file-name))
  23967. ;;;###autoload
  23968. (defun org-export-icalendar-all-agenda-files ()
  23969. "Export all files in `org-agenda-files' to iCalendar .ics files.
  23970. Each iCalendar file will be located in the same directory as the Org-mode
  23971. file, but with extension `.ics'."
  23972. (interactive)
  23973. (apply 'org-export-icalendar nil (org-agenda-files t)))
  23974. ;;;###autoload
  23975. (defun org-export-icalendar-combine-agenda-files ()
  23976. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  23977. The file is stored under the name `org-combined-agenda-icalendar-file'."
  23978. (interactive)
  23979. (apply 'org-export-icalendar t (org-agenda-files t)))
  23980. (defun org-export-icalendar (combine &rest files)
  23981. "Create iCalendar files for all elements of FILES.
  23982. If COMBINE is non-nil, combine all calendar entries into a single large
  23983. file and store it under the name `org-combined-agenda-icalendar-file'."
  23984. (save-excursion
  23985. (org-prepare-agenda-buffers files)
  23986. (let* ((dir (org-export-directory
  23987. :ical (list :publishing-directory
  23988. org-export-publishing-directory)))
  23989. file ical-file ical-buffer category started org-agenda-new-buffers)
  23990. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  23991. (when combine
  23992. (setq ical-file
  23993. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  23994. org-combined-agenda-icalendar-file
  23995. (expand-file-name org-combined-agenda-icalendar-file dir))
  23996. ical-buffer (org-get-agenda-file-buffer ical-file))
  23997. (set-buffer ical-buffer) (erase-buffer))
  23998. (while (setq file (pop files))
  23999. (catch 'nextfile
  24000. (org-check-agenda-file file)
  24001. (set-buffer (org-get-agenda-file-buffer file))
  24002. (unless combine
  24003. (setq ical-file (concat (file-name-as-directory dir)
  24004. (file-name-sans-extension
  24005. (file-name-nondirectory buffer-file-name))
  24006. ".ics"))
  24007. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24008. (with-current-buffer ical-buffer (erase-buffer)))
  24009. (setq category (or org-category
  24010. (file-name-sans-extension
  24011. (file-name-nondirectory buffer-file-name))))
  24012. (if (symbolp category) (setq category (symbol-name category)))
  24013. (let ((standard-output ical-buffer))
  24014. (if combine
  24015. (and (not started) (setq started t)
  24016. (org-start-icalendar-file org-icalendar-combined-name))
  24017. (org-start-icalendar-file category))
  24018. (org-print-icalendar-entries combine)
  24019. (when (or (and combine (not files)) (not combine))
  24020. (org-finish-icalendar-file)
  24021. (set-buffer ical-buffer)
  24022. (save-buffer)
  24023. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24024. (org-release-buffers org-agenda-new-buffers))))
  24025. (defvar org-after-save-iCalendar-file-hook nil
  24026. "Hook run after an iCalendar file has been saved.
  24027. The iCalendar buffer is still current when this hook is run.
  24028. A good way to use this is to tell a desktop calenndar application to re-read
  24029. the iCalendar file.")
  24030. (defun org-print-icalendar-entries (&optional combine)
  24031. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24032. When COMBINE is non nil, add the category to each line."
  24033. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24034. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24035. (dts (org-ical-ts-to-string
  24036. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24037. "DTSTART"))
  24038. hd ts ts2 state status (inc t) pos b sexp rrule
  24039. scheduledp deadlinep tmp pri category entry location summary desc
  24040. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24041. (org-refresh-category-properties)
  24042. (save-excursion
  24043. (goto-char (point-min))
  24044. (while (re-search-forward re1 nil t)
  24045. (catch :skip
  24046. (org-agenda-skip)
  24047. (setq pos (match-beginning 0)
  24048. ts (match-string 0)
  24049. inc t
  24050. hd (org-get-heading)
  24051. summary (org-icalendar-cleanup-string
  24052. (org-entry-get nil "SUMMARY"))
  24053. desc (org-icalendar-cleanup-string
  24054. (or (org-entry-get nil "DESCRIPTION")
  24055. (and org-icalendar-include-body (org-get-entry)))
  24056. t org-icalendar-include-body)
  24057. location (org-icalendar-cleanup-string
  24058. (org-entry-get nil "LOCATION"))
  24059. category (org-get-category))
  24060. (if (looking-at re2)
  24061. (progn
  24062. (goto-char (match-end 0))
  24063. (setq ts2 (match-string 1) inc nil))
  24064. (setq tmp (buffer-substring (max (point-min)
  24065. (- pos org-ds-keyword-length))
  24066. pos)
  24067. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24068. (progn
  24069. (setq inc nil)
  24070. (replace-match "\\1" t nil ts))
  24071. ts)
  24072. deadlinep (string-match org-deadline-regexp tmp)
  24073. scheduledp (string-match org-scheduled-regexp tmp)
  24074. ;; donep (org-entry-is-done-p)
  24075. ))
  24076. (if (or (string-match org-tr-regexp hd)
  24077. (string-match org-ts-regexp hd))
  24078. (setq hd (replace-match "" t t hd)))
  24079. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24080. (setq rrule
  24081. (concat "\nRRULE:FREQ="
  24082. (cdr (assoc
  24083. (match-string 2 ts)
  24084. '(("d" . "DAILY")("w" . "WEEKLY")
  24085. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24086. ";INTERVAL=" (match-string 1 ts)))
  24087. (setq rrule ""))
  24088. (setq summary (or summary hd))
  24089. (if (string-match org-bracket-link-regexp summary)
  24090. (setq summary
  24091. (replace-match (if (match-end 3)
  24092. (match-string 3 summary)
  24093. (match-string 1 summary))
  24094. t t summary)))
  24095. (if deadlinep (setq summary (concat "DL: " summary)))
  24096. (if scheduledp (setq summary (concat "S: " summary)))
  24097. (if (string-match "\\`<%%" ts)
  24098. (with-current-buffer sexp-buffer
  24099. (insert (substring ts 1 -1) " " summary "\n"))
  24100. (princ (format "BEGIN:VEVENT
  24101. %s
  24102. %s%s
  24103. SUMMARY:%s%s%s
  24104. CATEGORIES:%s
  24105. END:VEVENT\n"
  24106. (org-ical-ts-to-string ts "DTSTART")
  24107. (org-ical-ts-to-string ts2 "DTEND" inc)
  24108. rrule summary
  24109. (if (and desc (string-match "\\S-" desc))
  24110. (concat "\nDESCRIPTION: " desc) "")
  24111. (if (and location (string-match "\\S-" location))
  24112. (concat "\nLOCATION: " location) "")
  24113. category)))))
  24114. (when (and org-icalendar-include-sexps
  24115. (condition-case nil (require 'icalendar) (error nil))
  24116. (fboundp 'icalendar-export-region))
  24117. ;; Get all the literal sexps
  24118. (goto-char (point-min))
  24119. (while (re-search-forward "^&?%%(" nil t)
  24120. (catch :skip
  24121. (org-agenda-skip)
  24122. (setq b (match-beginning 0))
  24123. (goto-char (1- (match-end 0)))
  24124. (forward-sexp 1)
  24125. (end-of-line 1)
  24126. (setq sexp (buffer-substring b (point)))
  24127. (with-current-buffer sexp-buffer
  24128. (insert sexp "\n"))
  24129. (princ (org-diary-to-ical-string sexp-buffer)))))
  24130. (when org-icalendar-include-todo
  24131. (goto-char (point-min))
  24132. (while (re-search-forward org-todo-line-regexp nil t)
  24133. (catch :skip
  24134. (org-agenda-skip)
  24135. (setq state (match-string 2))
  24136. (setq status (if (member state org-done-keywords)
  24137. "COMPLETED" "NEEDS-ACTION"))
  24138. (when (and state
  24139. (or (not (member state org-done-keywords))
  24140. (eq org-icalendar-include-todo 'all))
  24141. (not (member org-archive-tag (org-get-tags-at)))
  24142. )
  24143. (setq hd (match-string 3)
  24144. summary (org-icalendar-cleanup-string
  24145. (org-entry-get nil "SUMMARY"))
  24146. desc (org-icalendar-cleanup-string
  24147. (or (org-entry-get nil "DESCRIPTION")
  24148. (and org-icalendar-include-body (org-get-entry)))
  24149. t org-icalendar-include-body)
  24150. location (org-icalendar-cleanup-string
  24151. (org-entry-get nil "LOCATION")))
  24152. (if (string-match org-bracket-link-regexp hd)
  24153. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24154. (match-string 1 hd))
  24155. t t hd)))
  24156. (if (string-match org-priority-regexp hd)
  24157. (setq pri (string-to-char (match-string 2 hd))
  24158. hd (concat (substring hd 0 (match-beginning 1))
  24159. (substring hd (match-end 1))))
  24160. (setq pri org-default-priority))
  24161. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24162. (- org-lowest-priority org-highest-priority))))))
  24163. (princ (format "BEGIN:VTODO
  24164. %s
  24165. SUMMARY:%s%s%s
  24166. CATEGORIES:%s
  24167. SEQUENCE:1
  24168. PRIORITY:%d
  24169. STATUS:%s
  24170. END:VTODO\n"
  24171. dts
  24172. (or summary hd)
  24173. (if (and location (string-match "\\S-" location))
  24174. (concat "\nLOCATION: " location) "")
  24175. (if (and desc (string-match "\\S-" desc))
  24176. (concat "\nDESCRIPTION: " desc) "")
  24177. category pri status)))))))))
  24178. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24179. "Take out stuff and quote what needs to be quoted.
  24180. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24181. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24182. characters."
  24183. (if (not s)
  24184. nil
  24185. (when is-body
  24186. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24187. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24188. (while (string-match re s) (setq s (replace-match "" t t s)))
  24189. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24190. (let ((start 0))
  24191. (while (string-match "\\([,;\\]\\)" s start)
  24192. (setq start (+ (match-beginning 0) 2)
  24193. s (replace-match "\\\\\\1" nil nil s))))
  24194. (when is-body
  24195. (while (string-match "[ \t]*\n[ \t]*" s)
  24196. (setq s (replace-match "\\n" t t s))))
  24197. (setq s (org-trim s))
  24198. (if is-body
  24199. (if maxlength
  24200. (if (and (numberp maxlength)
  24201. (> (length s) maxlength))
  24202. (setq s (substring s 0 maxlength)))))
  24203. s))
  24204. (defun org-get-entry ()
  24205. "Clean-up description string."
  24206. (save-excursion
  24207. (org-back-to-heading t)
  24208. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24209. (defun org-start-icalendar-file (name)
  24210. "Start an iCalendar file by inserting the header."
  24211. (let ((user user-full-name)
  24212. (name (or name "unknown"))
  24213. (timezone (cadr (current-time-zone))))
  24214. (princ
  24215. (format "BEGIN:VCALENDAR
  24216. VERSION:2.0
  24217. X-WR-CALNAME:%s
  24218. PRODID:-//%s//Emacs with Org-mode//EN
  24219. X-WR-TIMEZONE:%s
  24220. CALSCALE:GREGORIAN\n" name user timezone))))
  24221. (defun org-finish-icalendar-file ()
  24222. "Finish an iCalendar file by inserting the END statement."
  24223. (princ "END:VCALENDAR\n"))
  24224. (defun org-ical-ts-to-string (s keyword &optional inc)
  24225. "Take a time string S and convert it to iCalendar format.
  24226. KEYWORD is added in front, to make a complete line like DTSTART....
  24227. When INC is non-nil, increase the hour by two (if time string contains
  24228. a time), or the day by one (if it does not contain a time)."
  24229. (let ((t1 (org-parse-time-string s 'nodefault))
  24230. t2 fmt have-time time)
  24231. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24232. (setq t2 t1 have-time t)
  24233. (setq t2 (org-parse-time-string s)))
  24234. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24235. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24236. (when inc
  24237. (if have-time
  24238. (if org-agenda-default-appointment-duration
  24239. (setq mi (+ org-agenda-default-appointment-duration mi))
  24240. (setq h (+ 2 h)))
  24241. (setq d (1+ d))))
  24242. (setq time (encode-time s mi h d m y)))
  24243. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24244. (concat keyword (format-time-string fmt time))))
  24245. ;;; XOXO export
  24246. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24247. (with-current-buffer buffer
  24248. (apply 'insert output)))
  24249. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24250. (defun org-export-as-xoxo (&optional buffer)
  24251. "Export the org buffer as XOXO.
  24252. The XOXO buffer is named *xoxo-<source buffer name>*"
  24253. (interactive (list (current-buffer)))
  24254. ;; A quickie abstraction
  24255. ;; Output everything as XOXO
  24256. (with-current-buffer (get-buffer buffer)
  24257. (let* ((pos (point))
  24258. (opt-plist (org-combine-plists (org-default-export-plist)
  24259. (org-infile-export-plist)))
  24260. (filename (concat (file-name-as-directory
  24261. (org-export-directory :xoxo opt-plist))
  24262. (file-name-sans-extension
  24263. (file-name-nondirectory buffer-file-name))
  24264. ".html"))
  24265. (out (find-file-noselect filename))
  24266. (last-level 1)
  24267. (hanging-li nil))
  24268. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24269. ;; Check the output buffer is empty.
  24270. (with-current-buffer out (erase-buffer))
  24271. ;; Kick off the output
  24272. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24273. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24274. (let* ((hd (match-string-no-properties 1))
  24275. (level (length hd))
  24276. (text (concat
  24277. (match-string-no-properties 2)
  24278. (save-excursion
  24279. (goto-char (match-end 0))
  24280. (let ((str ""))
  24281. (catch 'loop
  24282. (while 't
  24283. (forward-line)
  24284. (if (looking-at "^[ \t]\\(.*\\)")
  24285. (setq str (concat str (match-string-no-properties 1)))
  24286. (throw 'loop str)))))))))
  24287. ;; Handle level rendering
  24288. (cond
  24289. ((> level last-level)
  24290. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24291. ((< level last-level)
  24292. (dotimes (- (- last-level level) 1)
  24293. (if hanging-li
  24294. (org-export-as-xoxo-insert-into out "</li>\n"))
  24295. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24296. (when hanging-li
  24297. (org-export-as-xoxo-insert-into out "</li>\n")
  24298. (setq hanging-li nil)))
  24299. ((equal level last-level)
  24300. (if hanging-li
  24301. (org-export-as-xoxo-insert-into out "</li>\n")))
  24302. )
  24303. (setq last-level level)
  24304. ;; And output the new li
  24305. (setq hanging-li 't)
  24306. (if (equal ?+ (elt text 0))
  24307. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24308. (org-export-as-xoxo-insert-into out "<li>" text))))
  24309. ;; Finally finish off the ol
  24310. (dotimes (- last-level 1)
  24311. (if hanging-li
  24312. (org-export-as-xoxo-insert-into out "</li>\n"))
  24313. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24314. (goto-char pos)
  24315. ;; Finish the buffer off and clean it up.
  24316. (switch-to-buffer-other-window out)
  24317. (indent-region (point-min) (point-max) nil)
  24318. (save-buffer)
  24319. (goto-char (point-min))
  24320. )))
  24321. ;;;; Key bindings
  24322. ;; Make `C-c C-x' a prefix key
  24323. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24324. ;; TAB key with modifiers
  24325. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24326. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24327. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24328. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24329. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24330. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24331. ;; The following line is necessary under Suse GNU/Linux
  24332. (unless (featurep 'xemacs)
  24333. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24334. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24335. (define-key org-mode-map [backtab] 'org-shifttab)
  24336. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24337. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24338. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24339. ;; Cursor keys with modifiers
  24340. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24341. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24342. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24343. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24344. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24345. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24346. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24347. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24348. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24349. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24350. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24351. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24352. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24353. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24354. ;;; Extra keys for tty access.
  24355. ;; We only set them when really needed because otherwise the
  24356. ;; menus don't show the simple keys
  24357. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24358. (not window-system))
  24359. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24360. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24361. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24362. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24363. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24364. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24365. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24366. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24367. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24368. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24369. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24370. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24371. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24372. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24373. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24374. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24375. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24376. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24377. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24378. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24379. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24380. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24381. ;; All the other keys
  24382. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24383. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24384. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24385. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24386. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24387. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24388. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24389. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24390. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24391. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24392. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24393. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24394. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24395. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24396. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24397. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24398. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24399. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24400. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24401. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24402. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24403. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24404. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24405. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24406. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24407. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24408. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24409. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24410. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24411. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24412. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24413. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24414. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24415. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24416. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24417. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24418. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24419. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24420. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24421. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24422. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24423. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24424. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24425. (org-defkey org-mode-map "\C-m" 'org-return)
  24426. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24427. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24428. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24429. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24430. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24431. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24432. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24433. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24434. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24435. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24436. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24437. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24438. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24439. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24440. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24441. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24442. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24443. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24444. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24445. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24446. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24447. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24448. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24449. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24450. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24451. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24452. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24453. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24454. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24455. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24456. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24457. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24458. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24459. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24460. (when (featurep 'xemacs)
  24461. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24462. (defsubst org-table-p () (org-at-table-p))
  24463. (defun org-self-insert-command (N)
  24464. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24465. If the cursor is in a table looking at whitespace, the whitespace is
  24466. overwritten, and the table is not marked as requiring realignment."
  24467. (interactive "p")
  24468. (if (and (org-table-p)
  24469. (progn
  24470. ;; check if we blank the field, and if that triggers align
  24471. (and org-table-auto-blank-field
  24472. (member last-command
  24473. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24474. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24475. ;; got extra space, this field does not determine column width
  24476. (let (org-table-may-need-update) (org-table-blank-field))
  24477. ;; no extra space, this field may determine column width
  24478. (org-table-blank-field)))
  24479. t)
  24480. (eq N 1)
  24481. (looking-at "[^|\n]* |"))
  24482. (let (org-table-may-need-update)
  24483. (goto-char (1- (match-end 0)))
  24484. (delete-backward-char 1)
  24485. (goto-char (match-beginning 0))
  24486. (self-insert-command N))
  24487. (setq org-table-may-need-update t)
  24488. (self-insert-command N)
  24489. (org-fix-tags-on-the-fly)))
  24490. (defun org-fix-tags-on-the-fly ()
  24491. (when (and (equal (char-after (point-at-bol)) ?*)
  24492. (org-on-heading-p))
  24493. (org-align-tags-here org-tags-column)))
  24494. (defun org-delete-backward-char (N)
  24495. "Like `delete-backward-char', insert whitespace at field end in tables.
  24496. When deleting backwards, in tables this function will insert whitespace in
  24497. front of the next \"|\" separator, to keep the table aligned. The table will
  24498. still be marked for re-alignment if the field did fill the entire column,
  24499. because, in this case the deletion might narrow the column."
  24500. (interactive "p")
  24501. (if (and (org-table-p)
  24502. (eq N 1)
  24503. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24504. (looking-at ".*?|"))
  24505. (let ((pos (point))
  24506. (noalign (looking-at "[^|\n\r]* |"))
  24507. (c org-table-may-need-update))
  24508. (backward-delete-char N)
  24509. (skip-chars-forward "^|")
  24510. (insert " ")
  24511. (goto-char (1- pos))
  24512. ;; noalign: if there were two spaces at the end, this field
  24513. ;; does not determine the width of the column.
  24514. (if noalign (setq org-table-may-need-update c)))
  24515. (backward-delete-char N)
  24516. (org-fix-tags-on-the-fly)))
  24517. (defun org-delete-char (N)
  24518. "Like `delete-char', but insert whitespace at field end in tables.
  24519. When deleting characters, in tables this function will insert whitespace in
  24520. front of the next \"|\" separator, to keep the table aligned. The table will
  24521. still be marked for re-alignment if the field did fill the entire column,
  24522. because, in this case the deletion might narrow the column."
  24523. (interactive "p")
  24524. (if (and (org-table-p)
  24525. (not (bolp))
  24526. (not (= (char-after) ?|))
  24527. (eq N 1))
  24528. (if (looking-at ".*?|")
  24529. (let ((pos (point))
  24530. (noalign (looking-at "[^|\n\r]* |"))
  24531. (c org-table-may-need-update))
  24532. (replace-match (concat
  24533. (substring (match-string 0) 1 -1)
  24534. " |"))
  24535. (goto-char pos)
  24536. ;; noalign: if there were two spaces at the end, this field
  24537. ;; does not determine the width of the column.
  24538. (if noalign (setq org-table-may-need-update c)))
  24539. (delete-char N))
  24540. (delete-char N)
  24541. (org-fix-tags-on-the-fly)))
  24542. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24543. (put 'org-self-insert-command 'delete-selection t)
  24544. (put 'orgtbl-self-insert-command 'delete-selection t)
  24545. (put 'org-delete-char 'delete-selection 'supersede)
  24546. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24547. ;; Make `flyspell-mode' delay after some commands
  24548. (put 'org-self-insert-command 'flyspell-delayed t)
  24549. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24550. (put 'org-delete-char 'flyspell-delayed t)
  24551. (put 'org-delete-backward-char 'flyspell-delayed t)
  24552. ;; Make pabbrev-mode expand after org-mode commands
  24553. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24554. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24555. ;; How to do this: Measure non-white length of current string
  24556. ;; If equal to column width, we should realign.
  24557. (defun org-remap (map &rest commands)
  24558. "In MAP, remap the functions given in COMMANDS.
  24559. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24560. (let (new old)
  24561. (while commands
  24562. (setq old (pop commands) new (pop commands))
  24563. (if (fboundp 'command-remapping)
  24564. (org-defkey map (vector 'remap old) new)
  24565. (substitute-key-definition old new map global-map)))))
  24566. (when (eq org-enable-table-editor 'optimized)
  24567. ;; If the user wants maximum table support, we need to hijack
  24568. ;; some standard editing functions
  24569. (org-remap org-mode-map
  24570. 'self-insert-command 'org-self-insert-command
  24571. 'delete-char 'org-delete-char
  24572. 'delete-backward-char 'org-delete-backward-char)
  24573. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24574. (defun org-shiftcursor-error ()
  24575. "Throw an error because Shift-Cursor command was applied in wrong context."
  24576. (error "This command is active in special context like tables, headlines or timestamps"))
  24577. (defun org-shifttab (&optional arg)
  24578. "Global visibility cycling or move to previous table field.
  24579. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24580. on context.
  24581. See the individual commands for more information."
  24582. (interactive "P")
  24583. (cond
  24584. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24585. (arg (message "Content view to level: ")
  24586. (org-content (prefix-numeric-value arg))
  24587. (setq org-cycle-global-status 'overview))
  24588. (t (call-interactively 'org-global-cycle))))
  24589. (defun org-shiftmetaleft ()
  24590. "Promote subtree or delete table column.
  24591. Calls `org-promote-subtree', `org-outdent-item',
  24592. or `org-table-delete-column', depending on context.
  24593. See the individual commands for more information."
  24594. (interactive)
  24595. (cond
  24596. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24597. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24598. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24599. (t (org-shiftcursor-error))))
  24600. (defun org-shiftmetaright ()
  24601. "Demote subtree or insert table column.
  24602. Calls `org-demote-subtree', `org-indent-item',
  24603. or `org-table-insert-column', depending on context.
  24604. See the individual commands for more information."
  24605. (interactive)
  24606. (cond
  24607. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24608. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24609. ((org-at-item-p) (call-interactively 'org-indent-item))
  24610. (t (org-shiftcursor-error))))
  24611. (defun org-shiftmetaup (&optional arg)
  24612. "Move subtree up or kill table row.
  24613. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24614. `org-move-item-up' depending on context. See the individual commands
  24615. for more information."
  24616. (interactive "P")
  24617. (cond
  24618. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24619. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24620. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24621. (t (org-shiftcursor-error))))
  24622. (defun org-shiftmetadown (&optional arg)
  24623. "Move subtree down or insert table row.
  24624. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24625. `org-move-item-down', depending on context. See the individual
  24626. commands for more information."
  24627. (interactive "P")
  24628. (cond
  24629. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24630. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24631. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24632. (t (org-shiftcursor-error))))
  24633. (defun org-metaleft (&optional arg)
  24634. "Promote heading or move table column to left.
  24635. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24636. With no specific context, calls the Emacs default `backward-word'.
  24637. See the individual commands for more information."
  24638. (interactive "P")
  24639. (cond
  24640. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24641. ((or (org-on-heading-p) (org-region-active-p))
  24642. (call-interactively 'org-do-promote))
  24643. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24644. (t (call-interactively 'backward-word))))
  24645. (defun org-metaright (&optional arg)
  24646. "Demote subtree or move table column to right.
  24647. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24648. With no specific context, calls the Emacs default `forward-word'.
  24649. See the individual commands for more information."
  24650. (interactive "P")
  24651. (cond
  24652. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24653. ((or (org-on-heading-p) (org-region-active-p))
  24654. (call-interactively 'org-do-demote))
  24655. ((org-at-item-p) (call-interactively 'org-indent-item))
  24656. (t (call-interactively 'forward-word))))
  24657. (defun org-metaup (&optional arg)
  24658. "Move subtree up or move table row up.
  24659. Calls `org-move-subtree-up' or `org-table-move-row' or
  24660. `org-move-item-up', depending on context. See the individual commands
  24661. for more information."
  24662. (interactive "P")
  24663. (cond
  24664. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24665. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24666. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24667. (t (transpose-lines 1) (beginning-of-line -1))))
  24668. (defun org-metadown (&optional arg)
  24669. "Move subtree down or move table row down.
  24670. Calls `org-move-subtree-down' or `org-table-move-row' or
  24671. `org-move-item-down', depending on context. See the individual
  24672. commands for more information."
  24673. (interactive "P")
  24674. (cond
  24675. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24676. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24677. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24678. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24679. (defun org-shiftup (&optional arg)
  24680. "Increase item in timestamp or increase priority of current headline.
  24681. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24682. depending on context. See the individual commands for more information."
  24683. (interactive "P")
  24684. (cond
  24685. ((org-at-timestamp-p t)
  24686. (call-interactively (if org-edit-timestamp-down-means-later
  24687. 'org-timestamp-down 'org-timestamp-up)))
  24688. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24689. ((org-at-item-p) (call-interactively 'org-previous-item))
  24690. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24691. (defun org-shiftdown (&optional arg)
  24692. "Decrease item in timestamp or decrease priority of current headline.
  24693. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24694. depending on context. See the individual commands for more information."
  24695. (interactive "P")
  24696. (cond
  24697. ((org-at-timestamp-p t)
  24698. (call-interactively (if org-edit-timestamp-down-means-later
  24699. 'org-timestamp-up 'org-timestamp-down)))
  24700. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24701. (t (call-interactively 'org-next-item))))
  24702. (defun org-shiftright ()
  24703. "Next TODO keyword or timestamp one day later, depending on context."
  24704. (interactive)
  24705. (cond
  24706. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24707. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24708. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24709. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24710. (t (org-shiftcursor-error))))
  24711. (defun org-shiftleft ()
  24712. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24713. (interactive)
  24714. (cond
  24715. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24716. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24717. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24718. ((org-at-property-p)
  24719. (call-interactively 'org-property-previous-allowed-value))
  24720. (t (org-shiftcursor-error))))
  24721. (defun org-shiftcontrolright ()
  24722. "Switch to next TODO set."
  24723. (interactive)
  24724. (cond
  24725. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24726. (t (org-shiftcursor-error))))
  24727. (defun org-shiftcontrolleft ()
  24728. "Switch to previous TODO set."
  24729. (interactive)
  24730. (cond
  24731. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24732. (t (org-shiftcursor-error))))
  24733. (defun org-ctrl-c-ret ()
  24734. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24735. (interactive)
  24736. (cond
  24737. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24738. (t (call-interactively 'org-insert-heading))))
  24739. (defun org-copy-special ()
  24740. "Copy region in table or copy current subtree.
  24741. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24742. See the individual commands for more information."
  24743. (interactive)
  24744. (call-interactively
  24745. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24746. (defun org-cut-special ()
  24747. "Cut region in table or cut current subtree.
  24748. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24749. See the individual commands for more information."
  24750. (interactive)
  24751. (call-interactively
  24752. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24753. (defun org-paste-special (arg)
  24754. "Paste rectangular region into table, or past subtree relative to level.
  24755. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24756. See the individual commands for more information."
  24757. (interactive "P")
  24758. (if (org-at-table-p)
  24759. (org-table-paste-rectangle)
  24760. (org-paste-subtree arg)))
  24761. (defun org-ctrl-c-ctrl-c (&optional arg)
  24762. "Set tags in headline, or update according to changed information at point.
  24763. This command does many different things, depending on context:
  24764. - If the cursor is in a headline, prompt for tags and insert them
  24765. into the current line, aligned to `org-tags-column'. When called
  24766. with prefix arg, realign all tags in the current buffer.
  24767. - If the cursor is in one of the special #+KEYWORD lines, this
  24768. triggers scanning the buffer for these lines and updating the
  24769. information.
  24770. - If the cursor is inside a table, realign the table. This command
  24771. works even if the automatic table editor has been turned off.
  24772. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24773. the entire table.
  24774. - If the cursor is a the beginning of a dynamic block, update it.
  24775. - If the cursor is inside a table created by the table.el package,
  24776. activate that table.
  24777. - If the current buffer is a remember buffer, close note and file it.
  24778. with a prefix argument, file it without further interaction to the default
  24779. location.
  24780. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24781. links in this buffer.
  24782. - If the cursor is on a numbered item in a plain list, renumber the
  24783. ordered list.
  24784. - If the cursor is on a checkbox, toggle it."
  24785. (interactive "P")
  24786. (let ((org-enable-table-editor t))
  24787. (cond
  24788. ((or org-clock-overlays
  24789. org-occur-highlights
  24790. org-latex-fragment-image-overlays)
  24791. (org-remove-clock-overlays)
  24792. (org-remove-occur-highlights)
  24793. (org-remove-latex-fragment-image-overlays)
  24794. (message "Temporary highlights/overlays removed from current buffer"))
  24795. ((and (local-variable-p 'org-finish-function (current-buffer))
  24796. (fboundp org-finish-function))
  24797. (funcall org-finish-function))
  24798. ((org-at-property-p)
  24799. (call-interactively 'org-property-action))
  24800. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24801. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24802. ((org-at-table.el-p)
  24803. (require 'table)
  24804. (beginning-of-line 1)
  24805. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24806. (call-interactively 'table-recognize-table))
  24807. ((org-at-table-p)
  24808. (org-table-maybe-eval-formula)
  24809. (if arg
  24810. (call-interactively 'org-table-recalculate)
  24811. (org-table-maybe-recalculate-line))
  24812. (call-interactively 'org-table-align))
  24813. ((org-at-item-checkbox-p)
  24814. (call-interactively 'org-toggle-checkbox))
  24815. ((org-at-item-p)
  24816. (call-interactively 'org-maybe-renumber-ordered-list))
  24817. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24818. ;; Dynamic block
  24819. (beginning-of-line 1)
  24820. (org-update-dblock))
  24821. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24822. (cond
  24823. ((equal (match-string 1) "TBLFM")
  24824. ;; Recalculate the table before this line
  24825. (save-excursion
  24826. (beginning-of-line 1)
  24827. (skip-chars-backward " \r\n\t")
  24828. (if (org-at-table-p)
  24829. (org-call-with-arg 'org-table-recalculate t))))
  24830. (t
  24831. (call-interactively 'org-mode-restart))))
  24832. (t (error "C-c C-c can do nothing useful at this location.")))))
  24833. (defun org-mode-restart ()
  24834. "Restart Org-mode, to scan again for special lines.
  24835. Also updates the keyword regular expressions."
  24836. (interactive)
  24837. (let ((org-inhibit-startup t)) (org-mode))
  24838. (message "Org-mode restarted to refresh keyword and special line setup"))
  24839. (defun org-kill-note-or-show-branches ()
  24840. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  24841. (interactive)
  24842. (if (not org-finish-function)
  24843. (call-interactively 'show-branches)
  24844. (let ((org-note-abort t))
  24845. (funcall org-finish-function))))
  24846. (defun org-return (&optional indent)
  24847. "Goto next table row or insert a newline.
  24848. Calls `org-table-next-row' or `newline', depending on context.
  24849. See the individual commands for more information."
  24850. (interactive)
  24851. (cond
  24852. ((bobp) (if indent (newline-and-indent) (newline)))
  24853. ((org-at-table-p)
  24854. (org-table-justify-field-maybe)
  24855. (call-interactively 'org-table-next-row))
  24856. (t (if indent (newline-and-indent) (newline)))))
  24857. (defun org-return-indent ()
  24858. (interactive)
  24859. "Goto next table row or insert a newline and indent.
  24860. Calls `org-table-next-row' or `newline-and-indent', depending on
  24861. context. See the individual commands for more information."
  24862. (org-return t))
  24863. (defun org-ctrl-c-minus ()
  24864. "Insert separator line in table or modify bullet type in list.
  24865. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  24866. depending on context."
  24867. (interactive)
  24868. (cond
  24869. ((org-at-table-p)
  24870. (call-interactively 'org-table-insert-hline))
  24871. ((org-on-heading-p)
  24872. ;; Convert to item
  24873. (save-excursion
  24874. (beginning-of-line 1)
  24875. (if (looking-at "\\*+ ")
  24876. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  24877. ((org-in-item-p)
  24878. (call-interactively 'org-cycle-list-bullet))
  24879. (t (error "`C-c -' does have no function here."))))
  24880. (defun org-meta-return (&optional arg)
  24881. "Insert a new heading or wrap a region in a table.
  24882. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  24883. See the individual commands for more information."
  24884. (interactive "P")
  24885. (cond
  24886. ((org-at-table-p)
  24887. (call-interactively 'org-table-wrap-region))
  24888. (t (call-interactively 'org-insert-heading))))
  24889. ;;; Menu entries
  24890. ;; Define the Org-mode menus
  24891. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  24892. '("Tbl"
  24893. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  24894. ["Next Field" org-cycle (org-at-table-p)]
  24895. ["Previous Field" org-shifttab (org-at-table-p)]
  24896. ["Next Row" org-return (org-at-table-p)]
  24897. "--"
  24898. ["Blank Field" org-table-blank-field (org-at-table-p)]
  24899. ["Edit Field" org-table-edit-field (org-at-table-p)]
  24900. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  24901. "--"
  24902. ("Column"
  24903. ["Move Column Left" org-metaleft (org-at-table-p)]
  24904. ["Move Column Right" org-metaright (org-at-table-p)]
  24905. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  24906. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  24907. ("Row"
  24908. ["Move Row Up" org-metaup (org-at-table-p)]
  24909. ["Move Row Down" org-metadown (org-at-table-p)]
  24910. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  24911. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  24912. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  24913. "--"
  24914. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  24915. ("Rectangle"
  24916. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  24917. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  24918. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  24919. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  24920. "--"
  24921. ("Calculate"
  24922. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  24923. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  24924. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  24925. "--"
  24926. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  24927. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  24928. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  24929. "--"
  24930. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  24931. "--"
  24932. ["Sum Column/Rectangle" org-table-sum
  24933. (or (org-at-table-p) (org-region-active-p))]
  24934. ["Which Column?" org-table-current-column (org-at-table-p)])
  24935. ["Debug Formulas"
  24936. org-table-toggle-formula-debugger
  24937. :style toggle :selected org-table-formula-debug]
  24938. ["Show Col/Row Numbers"
  24939. org-table-toggle-coordinate-overlays
  24940. :style toggle :selected org-table-overlay-coordinates]
  24941. "--"
  24942. ["Create" org-table-create (and (not (org-at-table-p))
  24943. org-enable-table-editor)]
  24944. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  24945. ["Import from File" org-table-import (not (org-at-table-p))]
  24946. ["Export to File" org-table-export (org-at-table-p)]
  24947. "--"
  24948. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  24949. (easy-menu-define org-org-menu org-mode-map "Org menu"
  24950. '("Org"
  24951. ("Show/Hide"
  24952. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  24953. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  24954. ["Sparse Tree" org-occur t]
  24955. ["Reveal Context" org-reveal t]
  24956. ["Show All" show-all t]
  24957. "--"
  24958. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  24959. "--"
  24960. ["New Heading" org-insert-heading t]
  24961. ("Navigate Headings"
  24962. ["Up" outline-up-heading t]
  24963. ["Next" outline-next-visible-heading t]
  24964. ["Previous" outline-previous-visible-heading t]
  24965. ["Next Same Level" outline-forward-same-level t]
  24966. ["Previous Same Level" outline-backward-same-level t]
  24967. "--"
  24968. ["Jump" org-goto t])
  24969. ("Edit Structure"
  24970. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  24971. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  24972. "--"
  24973. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  24974. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  24975. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  24976. "--"
  24977. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  24978. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  24979. ["Demote Heading" org-metaright (not (org-at-table-p))]
  24980. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  24981. "--"
  24982. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  24983. "--"
  24984. ["Convert to odd levels" org-convert-to-odd-levels t]
  24985. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  24986. ("Editing"
  24987. ["Emphasis..." org-emphasize t])
  24988. ("Archive"
  24989. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  24990. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  24991. ; :active t :keys "C-u C-c C-x C-a"]
  24992. ["Sparse trees open ARCHIVE trees"
  24993. (setq org-sparse-tree-open-archived-trees
  24994. (not org-sparse-tree-open-archived-trees))
  24995. :style toggle :selected org-sparse-tree-open-archived-trees]
  24996. ["Cycling opens ARCHIVE trees"
  24997. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  24998. :style toggle :selected org-cycle-open-archived-trees]
  24999. ["Agenda includes ARCHIVE trees"
  25000. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25001. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25002. "--"
  25003. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25004. ; ["Check and Move Children" (org-archive-subtree '(4))
  25005. ; :active t :keys "C-u C-c C-x C-s"]
  25006. )
  25007. "--"
  25008. ("TODO Lists"
  25009. ["TODO/DONE/-" org-todo t]
  25010. ("Select keyword"
  25011. ["Next keyword" org-shiftright (org-on-heading-p)]
  25012. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25013. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25014. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25015. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25016. ["Show TODO Tree" org-show-todo-tree t]
  25017. ["Global TODO list" org-todo-list t]
  25018. "--"
  25019. ["Set Priority" org-priority t]
  25020. ["Priority Up" org-shiftup t]
  25021. ["Priority Down" org-shiftdown t])
  25022. ("TAGS and Properties"
  25023. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25024. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25025. "--"
  25026. ["Set property" 'org-set-property t]
  25027. ["Column view of properties" org-columns t]
  25028. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25029. ("Dates and Scheduling"
  25030. ["Timestamp" org-time-stamp t]
  25031. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25032. ("Change Date"
  25033. ["1 Day Later" org-shiftright t]
  25034. ["1 Day Earlier" org-shiftleft t]
  25035. ["1 ... Later" org-shiftup t]
  25036. ["1 ... Earlier" org-shiftdown t])
  25037. ["Compute Time Range" org-evaluate-time-range t]
  25038. ["Schedule Item" org-schedule t]
  25039. ["Deadline" org-deadline t]
  25040. "--"
  25041. ["Custom time format" org-toggle-time-stamp-overlays
  25042. :style radio :selected org-display-custom-times]
  25043. "--"
  25044. ["Goto Calendar" org-goto-calendar t]
  25045. ["Date from Calendar" org-date-from-calendar t])
  25046. ("Logging work"
  25047. ["Clock in" org-clock-in t]
  25048. ["Clock out" org-clock-out t]
  25049. ["Clock cancel" org-clock-cancel t]
  25050. ["Goto running clock" org-clock-goto t]
  25051. ["Display times" org-clock-display t]
  25052. ["Create clock table" org-clock-report t]
  25053. "--"
  25054. ["Record DONE time"
  25055. (progn (setq org-log-done (not org-log-done))
  25056. (message "Switching to %s will %s record a timestamp"
  25057. (car org-done-keywords)
  25058. (if org-log-done "automatically" "not")))
  25059. :style toggle :selected org-log-done])
  25060. "--"
  25061. ["Agenda Command..." org-agenda t]
  25062. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25063. ("File List for Agenda")
  25064. ("Special views current file"
  25065. ["TODO Tree" org-show-todo-tree t]
  25066. ["Check Deadlines" org-check-deadlines t]
  25067. ["Timeline" org-timeline t]
  25068. ["Tags Tree" org-tags-sparse-tree t])
  25069. "--"
  25070. ("Hyperlinks"
  25071. ["Store Link (Global)" org-store-link t]
  25072. ["Insert Link" org-insert-link t]
  25073. ["Follow Link" org-open-at-point t]
  25074. "--"
  25075. ["Next link" org-next-link t]
  25076. ["Previous link" org-previous-link t]
  25077. "--"
  25078. ["Descriptive Links"
  25079. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25080. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25081. ["Literal Links"
  25082. (progn
  25083. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25084. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25085. "--"
  25086. ["Export/Publish..." org-export t]
  25087. ("LaTeX"
  25088. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25089. :selected org-cdlatex-mode]
  25090. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25091. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25092. ["Modify math symbol" org-cdlatex-math-modify
  25093. (org-inside-LaTeX-fragment-p)]
  25094. ["Export LaTeX fragments as images"
  25095. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25096. :style toggle :selected org-export-with-LaTeX-fragments])
  25097. "--"
  25098. ("Documentation"
  25099. ["Show Version" org-version t]
  25100. ["Info Documentation" org-info t])
  25101. ("Customize"
  25102. ["Browse Org Group" org-customize t]
  25103. "--"
  25104. ["Expand This Menu" org-create-customize-menu
  25105. (fboundp 'customize-menu-create)])
  25106. "--"
  25107. ["Refresh setup" org-mode-restart t]
  25108. ))
  25109. (defun org-info (&optional node)
  25110. "Read documentation for Org-mode in the info system.
  25111. With optional NODE, go directly to that node."
  25112. (interactive)
  25113. (require 'info)
  25114. (Info-goto-node (format "(org)%s" (or node ""))))
  25115. (defun org-install-agenda-files-menu ()
  25116. (let ((bl (buffer-list)))
  25117. (save-excursion
  25118. (while bl
  25119. (set-buffer (pop bl))
  25120. (if (org-mode-p) (setq bl nil)))
  25121. (when (org-mode-p)
  25122. (easy-menu-change
  25123. '("Org") "File List for Agenda"
  25124. (append
  25125. (list
  25126. ["Edit File List" (org-edit-agenda-file-list) t]
  25127. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25128. ["Remove Current File from List" org-remove-file t]
  25129. ["Cycle through agenda files" org-cycle-agenda-files t]
  25130. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25131. "--")
  25132. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25133. ;;;; Documentation
  25134. (defun org-customize ()
  25135. "Call the customize function with org as argument."
  25136. (interactive)
  25137. (customize-browse 'org))
  25138. (defun org-create-customize-menu ()
  25139. "Create a full customization menu for Org-mode, insert it into the menu."
  25140. (interactive)
  25141. (if (fboundp 'customize-menu-create)
  25142. (progn
  25143. (easy-menu-change
  25144. '("Org") "Customize"
  25145. `(["Browse Org group" org-customize t]
  25146. "--"
  25147. ,(customize-menu-create 'org)
  25148. ["Set" Custom-set t]
  25149. ["Save" Custom-save t]
  25150. ["Reset to Current" Custom-reset-current t]
  25151. ["Reset to Saved" Custom-reset-saved t]
  25152. ["Reset to Standard Settings" Custom-reset-standard t]))
  25153. (message "\"Org\"-menu now contains full customization menu"))
  25154. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25155. ;;;; Miscellaneous stuff
  25156. ;;; Generally useful functions
  25157. (defun org-context ()
  25158. "Return a list of contexts of the current cursor position.
  25159. If several contexts apply, all are returned.
  25160. Each context entry is a list with a symbol naming the context, and
  25161. two positions indicating start and end of the context. Possible
  25162. contexts are:
  25163. :headline anywhere in a headline
  25164. :headline-stars on the leading stars in a headline
  25165. :todo-keyword on a TODO keyword (including DONE) in a headline
  25166. :tags on the TAGS in a headline
  25167. :priority on the priority cookie in a headline
  25168. :item on the first line of a plain list item
  25169. :item-bullet on the bullet/number of a plain list item
  25170. :checkbox on the checkbox in a plain list item
  25171. :table in an org-mode table
  25172. :table-special on a special filed in a table
  25173. :table-table in a table.el table
  25174. :link on a hyperlink
  25175. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25176. :target on a <<target>>
  25177. :radio-target on a <<<radio-target>>>
  25178. :latex-fragment on a LaTeX fragment
  25179. :latex-preview on a LaTeX fragment with overlayed preview image
  25180. This function expects the position to be visible because it uses font-lock
  25181. faces as a help to recognize the following contexts: :table-special, :link,
  25182. and :keyword."
  25183. (let* ((f (get-text-property (point) 'face))
  25184. (faces (if (listp f) f (list f)))
  25185. (p (point)) clist o)
  25186. ;; First the large context
  25187. (cond
  25188. ((org-on-heading-p t)
  25189. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25190. (when (progn
  25191. (beginning-of-line 1)
  25192. (looking-at org-todo-line-tags-regexp))
  25193. (push (org-point-in-group p 1 :headline-stars) clist)
  25194. (push (org-point-in-group p 2 :todo-keyword) clist)
  25195. (push (org-point-in-group p 4 :tags) clist))
  25196. (goto-char p)
  25197. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25198. (if (looking-at "\\[#[A-Z0-9]\\]")
  25199. (push (org-point-in-group p 0 :priority) clist)))
  25200. ((org-at-item-p)
  25201. (push (org-point-in-group p 2 :item-bullet) clist)
  25202. (push (list :item (point-at-bol)
  25203. (save-excursion (org-end-of-item) (point)))
  25204. clist)
  25205. (and (org-at-item-checkbox-p)
  25206. (push (org-point-in-group p 0 :checkbox) clist)))
  25207. ((org-at-table-p)
  25208. (push (list :table (org-table-begin) (org-table-end)) clist)
  25209. (if (memq 'org-formula faces)
  25210. (push (list :table-special
  25211. (previous-single-property-change p 'face)
  25212. (next-single-property-change p 'face)) clist)))
  25213. ((org-at-table-p 'any)
  25214. (push (list :table-table) clist)))
  25215. (goto-char p)
  25216. ;; Now the small context
  25217. (cond
  25218. ((org-at-timestamp-p)
  25219. (push (org-point-in-group p 0 :timestamp) clist))
  25220. ((memq 'org-link faces)
  25221. (push (list :link
  25222. (previous-single-property-change p 'face)
  25223. (next-single-property-change p 'face)) clist))
  25224. ((memq 'org-special-keyword faces)
  25225. (push (list :keyword
  25226. (previous-single-property-change p 'face)
  25227. (next-single-property-change p 'face)) clist))
  25228. ((org-on-target-p)
  25229. (push (org-point-in-group p 0 :target) clist)
  25230. (goto-char (1- (match-beginning 0)))
  25231. (if (looking-at org-radio-target-regexp)
  25232. (push (org-point-in-group p 0 :radio-target) clist))
  25233. (goto-char p))
  25234. ((setq o (car (delq nil
  25235. (mapcar
  25236. (lambda (x)
  25237. (if (memq x org-latex-fragment-image-overlays) x))
  25238. (org-overlays-at (point))))))
  25239. (push (list :latex-fragment
  25240. (org-overlay-start o) (org-overlay-end o)) clist)
  25241. (push (list :latex-preview
  25242. (org-overlay-start o) (org-overlay-end o)) clist))
  25243. ((org-inside-LaTeX-fragment-p)
  25244. ;; FIXME: positions wrong.
  25245. (push (list :latex-fragment (point) (point)) clist)))
  25246. (setq clist (nreverse (delq nil clist)))
  25247. clist))
  25248. ;; FIXME: Compare with at-regexp-p Do we need both?
  25249. (defun org-in-regexp (re &optional nlines visually)
  25250. "Check if point is inside a match of regexp.
  25251. Normally only the current line is checked, but you can include NLINES extra
  25252. lines both before and after point into the search.
  25253. If VISUALLY is set, require that the cursor is not after the match but
  25254. really on, so that the block visually is on the match."
  25255. (catch 'exit
  25256. (let ((pos (point))
  25257. (eol (point-at-eol (+ 1 (or nlines 0))))
  25258. (inc (if visually 1 0)))
  25259. (save-excursion
  25260. (beginning-of-line (- 1 (or nlines 0)))
  25261. (while (re-search-forward re eol t)
  25262. (if (and (<= (match-beginning 0) pos)
  25263. (>= (+ inc (match-end 0)) pos))
  25264. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25265. (defun org-at-regexp-p (regexp)
  25266. "Is point inside a match of REGEXP in the current line?"
  25267. (catch 'exit
  25268. (save-excursion
  25269. (let ((pos (point)) (end (point-at-eol)))
  25270. (beginning-of-line 1)
  25271. (while (re-search-forward regexp end t)
  25272. (if (and (<= (match-beginning 0) pos)
  25273. (>= (match-end 0) pos))
  25274. (throw 'exit t)))
  25275. nil))))
  25276. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25277. "Call `multi-occur' with buffers for all agenda files."
  25278. (interactive "sOrg-files matching: \np")
  25279. (let* ((files (org-agenda-files))
  25280. (tnames (mapcar 'file-truename files))
  25281. (extra org-agenda-multi-occur-extra-files)
  25282. f)
  25283. (while (setq f (pop extra))
  25284. (unless (member (file-truename f) tnames)
  25285. (add-to-list 'files f 'append)
  25286. (add-to-list 'tnames (file-truename f) 'append)))
  25287. (multi-occur
  25288. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25289. regexp)))
  25290. (if (boundp 'occur-mode-find-occurrence-hook)
  25291. ;; Emacs 23
  25292. (add-hook 'occur-mode-find-occurrence-hook
  25293. (lambda ()
  25294. (when (org-mode-p)
  25295. (org-reveal))))
  25296. ;; Emacs 22
  25297. (defadvice occur-mode-goto-occurrence
  25298. (after org-occur-reveal activate)
  25299. (and (org-mode-p) (org-reveal)))
  25300. (defadvice occur-mode-goto-occurrence-other-window
  25301. (after org-occur-reveal activate)
  25302. (and (org-mode-p) (org-reveal)))
  25303. (defadvice occur-mode-display-occurrence
  25304. (after org-occur-reveal activate)
  25305. (when (org-mode-p)
  25306. (let ((pos (occur-mode-find-occurrence)))
  25307. (with-current-buffer (marker-buffer pos)
  25308. (save-excursion
  25309. (goto-char pos)
  25310. (org-reveal)))))))
  25311. (defun org-uniquify (list)
  25312. "Remove duplicate elements from LIST."
  25313. (let (res)
  25314. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25315. res))
  25316. (defun org-delete-all (elts list)
  25317. "Remove all elements in ELTS from LIST."
  25318. (while elts
  25319. (setq list (delete (pop elts) list)))
  25320. list)
  25321. (defun org-back-over-empty-lines ()
  25322. "Move backwards over witespace, to the beginning of the first empty line.
  25323. Returns the number o empty lines passed."
  25324. (let ((pos (point)))
  25325. (skip-chars-backward " \t\n\r")
  25326. (beginning-of-line 2)
  25327. (goto-char (min (point) pos))
  25328. (count-lines (point) pos)))
  25329. (defun org-skip-whitespace ()
  25330. (skip-chars-forward " \t\n\r"))
  25331. (defun org-point-in-group (point group &optional context)
  25332. "Check if POINT is in match-group GROUP.
  25333. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25334. match. If the match group does ot exist or point is not inside it,
  25335. return nil."
  25336. (and (match-beginning group)
  25337. (>= point (match-beginning group))
  25338. (<= point (match-end group))
  25339. (if context
  25340. (list context (match-beginning group) (match-end group))
  25341. t)))
  25342. (defun org-switch-to-buffer-other-window (&rest args)
  25343. "Switch to buffer in a second window on the current frame.
  25344. In particular, do not allow pop-up frames."
  25345. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25346. special-display-function)
  25347. (apply 'switch-to-buffer-other-window args)))
  25348. (defun org-combine-plists (&rest plists)
  25349. "Create a single property list from all plists in PLISTS.
  25350. The process starts by copying the first list, and then setting properties
  25351. from the other lists. Settings in the last list are the most significant
  25352. ones and overrule settings in the other lists."
  25353. (let ((rtn (copy-sequence (pop plists)))
  25354. p v ls)
  25355. (while plists
  25356. (setq ls (pop plists))
  25357. (while ls
  25358. (setq p (pop ls) v (pop ls))
  25359. (setq rtn (plist-put rtn p v))))
  25360. rtn))
  25361. (defun org-move-line-down (arg)
  25362. "Move the current line down. With prefix argument, move it past ARG lines."
  25363. (interactive "p")
  25364. (let ((col (current-column))
  25365. beg end pos)
  25366. (beginning-of-line 1) (setq beg (point))
  25367. (beginning-of-line 2) (setq end (point))
  25368. (beginning-of-line (+ 1 arg))
  25369. (setq pos (move-marker (make-marker) (point)))
  25370. (insert (delete-and-extract-region beg end))
  25371. (goto-char pos)
  25372. (move-to-column col)))
  25373. (defun org-move-line-up (arg)
  25374. "Move the current line up. With prefix argument, move it past ARG lines."
  25375. (interactive "p")
  25376. (let ((col (current-column))
  25377. beg end pos)
  25378. (beginning-of-line 1) (setq beg (point))
  25379. (beginning-of-line 2) (setq end (point))
  25380. (beginning-of-line (- arg))
  25381. (setq pos (move-marker (make-marker) (point)))
  25382. (insert (delete-and-extract-region beg end))
  25383. (goto-char pos)
  25384. (move-to-column col)))
  25385. (defun org-replace-escapes (string table)
  25386. "Replace %-escapes in STRING with values in TABLE.
  25387. TABLE is an association list with keys like \"%a\" and string values.
  25388. The sequences in STRING may contain normal field width and padding information,
  25389. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25390. so values can contain further %-escapes if they are define later in TABLE."
  25391. (let ((case-fold-search nil)
  25392. e re rpl)
  25393. (while (setq e (pop table))
  25394. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25395. (while (string-match re string)
  25396. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25397. (cdr e)))
  25398. (setq string (replace-match rpl t t string))))
  25399. string))
  25400. (defun org-sublist (list start end)
  25401. "Return a section of LIST, from START to END.
  25402. Counting starts at 1."
  25403. (let (rtn (c start))
  25404. (setq list (nthcdr (1- start) list))
  25405. (while (and list (<= c end))
  25406. (push (pop list) rtn)
  25407. (setq c (1+ c)))
  25408. (nreverse rtn)))
  25409. (defun org-find-base-buffer-visiting (file)
  25410. "Like `find-buffer-visiting' but alway return the base buffer and
  25411. not an indirect buffer"
  25412. (let ((buf (find-buffer-visiting file)))
  25413. (if buf
  25414. (or (buffer-base-buffer buf) buf)
  25415. nil)))
  25416. (defun org-image-file-name-regexp ()
  25417. "Return regexp matching the file names of images."
  25418. (if (fboundp 'image-file-name-regexp)
  25419. (image-file-name-regexp)
  25420. (let ((image-file-name-extensions
  25421. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25422. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25423. (concat "\\."
  25424. (regexp-opt (nconc (mapcar 'upcase
  25425. image-file-name-extensions)
  25426. image-file-name-extensions)
  25427. t)
  25428. "\\'"))))
  25429. (defun org-file-image-p (file)
  25430. "Return non-nil if FILE is an image."
  25431. (save-match-data
  25432. (string-match (org-image-file-name-regexp) file)))
  25433. ;;; Paragraph filling stuff.
  25434. ;; We want this to be just right, so use the full arsenal.
  25435. (defun org-indent-line-function ()
  25436. "Indent line like previous, but further if previous was headline or item."
  25437. (interactive)
  25438. (let* ((pos (point))
  25439. (itemp (org-at-item-p))
  25440. column bpos bcol tpos tcol bullet btype bullet-type)
  25441. ;; Find the previous relevant line
  25442. (beginning-of-line 1)
  25443. (cond
  25444. ((looking-at "#") (setq column 0))
  25445. ((looking-at "\\*+ ") (setq column 0))
  25446. (t
  25447. (beginning-of-line 0)
  25448. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25449. (beginning-of-line 0))
  25450. (cond
  25451. ((looking-at "\\*+[ \t]+")
  25452. (goto-char (match-end 0))
  25453. (setq column (current-column)))
  25454. ((org-in-item-p)
  25455. (org-beginning-of-item)
  25456. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25457. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25458. (setq bpos (match-beginning 1) tpos (match-end 0)
  25459. bcol (progn (goto-char bpos) (current-column))
  25460. tcol (progn (goto-char tpos) (current-column))
  25461. bullet (match-string 1)
  25462. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25463. (if (not itemp)
  25464. (setq column tcol)
  25465. (goto-char pos)
  25466. (beginning-of-line 1)
  25467. (if (looking-at "\\S-")
  25468. (progn
  25469. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25470. (setq bullet (match-string 1)
  25471. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25472. (setq column (if (equal btype bullet-type) bcol tcol)))
  25473. (setq column (org-get-indentation)))))
  25474. (t (setq column (org-get-indentation))))))
  25475. (goto-char pos)
  25476. (if (<= (current-column) (current-indentation))
  25477. (indent-line-to column)
  25478. (save-excursion (indent-line-to column)))
  25479. (setq column (current-column))
  25480. (beginning-of-line 1)
  25481. (if (looking-at
  25482. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25483. (replace-match (concat "\\1" (format org-property-format
  25484. (match-string 2) (match-string 3)))
  25485. t nil))
  25486. (move-to-column column)))
  25487. (defun org-set-autofill-regexps ()
  25488. (interactive)
  25489. ;; In the paragraph separator we include headlines, because filling
  25490. ;; text in a line directly attached to a headline would otherwise
  25491. ;; fill the headline as well.
  25492. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25493. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25494. ;; The paragraph starter includes hand-formatted lists.
  25495. (org-set-local 'paragraph-start
  25496. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25497. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25498. ;; But only if the user has not turned off tables or fixed-width regions
  25499. (org-set-local
  25500. 'auto-fill-inhibit-regexp
  25501. (concat "\\*+ \\|#\\+"
  25502. "\\|[ \t]*" org-keyword-time-regexp
  25503. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25504. (concat
  25505. "\\|[ \t]*["
  25506. (if org-enable-table-editor "|" "")
  25507. (if org-enable-fixed-width-editor ":" "")
  25508. "]"))))
  25509. ;; We use our own fill-paragraph function, to make sure that tables
  25510. ;; and fixed-width regions are not wrapped. That function will pass
  25511. ;; through to `fill-paragraph' when appropriate.
  25512. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25513. ; Adaptive filling: To get full control, first make sure that
  25514. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25515. (org-set-local 'adaptive-fill-regexp "\000")
  25516. (org-set-local 'adaptive-fill-function
  25517. 'org-adaptive-fill-function)
  25518. (org-set-local
  25519. 'align-mode-rules-list
  25520. '((org-in-buffer-settings
  25521. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25522. (modes . '(org-mode))))))
  25523. (defun org-fill-paragraph (&optional justify)
  25524. "Re-align a table, pass through to fill-paragraph if no table."
  25525. (let ((table-p (org-at-table-p))
  25526. (table.el-p (org-at-table.el-p)))
  25527. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25528. (save-excursion (goto-char (point-at-bol))
  25529. (looking-at outline-regexp)))
  25530. t) ; skip headlines
  25531. (table.el-p t) ; skip table.el tables
  25532. (table-p (org-table-align) t) ; align org-mode tables
  25533. (t nil)))) ; call paragraph-fill
  25534. ;; For reference, this is the default value of adaptive-fill-regexp
  25535. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25536. (defun org-adaptive-fill-function ()
  25537. "Return a fill prefix for org-mode files.
  25538. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25539. work correctly."
  25540. (cond ((looking-at "#[ \t]+")
  25541. (match-string 0))
  25542. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25543. (save-excursion
  25544. (goto-char (match-end 0))
  25545. (make-string (current-column) ?\ )))
  25546. (t nil)))
  25547. ;;;; Functions extending outline functionality
  25548. (defun org-beginning-of-line (&optional arg)
  25549. "Go to the beginning of the current line. If that is invisible, continue
  25550. to a visible line beginning. This makes the function of C-a more intuitive.
  25551. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25552. first attempt, and only move to after the tags when the cursor is already
  25553. beyond the end of the headline."
  25554. (interactive "P")
  25555. (let ((pos (point)))
  25556. (beginning-of-line 1)
  25557. (if (bobp)
  25558. nil
  25559. (backward-char 1)
  25560. (if (org-invisible-p)
  25561. (while (and (not (bobp)) (org-invisible-p))
  25562. (backward-char 1)
  25563. (beginning-of-line 1))
  25564. (forward-char 1)))
  25565. (when org-special-ctrl-a/e
  25566. (cond
  25567. ((and (looking-at org-todo-line-regexp)
  25568. (= (char-after (match-end 1)) ?\ ))
  25569. (goto-char
  25570. (if (eq org-special-ctrl-a/e t)
  25571. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25572. ((= pos (point)) (match-beginning 3))
  25573. (t (point)))
  25574. (cond ((> pos (point)) (point))
  25575. ((not (eq last-command this-command)) (point))
  25576. (t (match-beginning 3))))))
  25577. ((org-at-item-p)
  25578. (goto-char
  25579. (if (eq org-special-ctrl-a/e t)
  25580. (cond ((> pos (match-end 4)) (match-end 4))
  25581. ((= pos (point)) (match-end 4))
  25582. (t (point)))
  25583. (cond ((> pos (point)) (point))
  25584. ((not (eq last-command this-command)) (point))
  25585. (t (match-end 4))))))))))
  25586. (defun org-end-of-line (&optional arg)
  25587. "Go to the end of the line.
  25588. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25589. first attempt, and only move to after the tags when the cursor is already
  25590. beyond the end of the headline."
  25591. (interactive "P")
  25592. (if (or (not org-special-ctrl-a/e)
  25593. (not (org-on-heading-p)))
  25594. (end-of-line arg)
  25595. (let ((pos (point)))
  25596. (beginning-of-line 1)
  25597. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25598. (if (eq org-special-ctrl-a/e t)
  25599. (if (or (< pos (match-beginning 1))
  25600. (= pos (match-end 0)))
  25601. (goto-char (match-beginning 1))
  25602. (goto-char (match-end 0)))
  25603. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25604. (goto-char (match-end 0))
  25605. (goto-char (match-beginning 1))))
  25606. (end-of-line arg)))))
  25607. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25608. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25609. (defun org-invisible-p ()
  25610. "Check if point is at a character currently not visible."
  25611. ;; Early versions of noutline don't have `outline-invisible-p'.
  25612. (if (fboundp 'outline-invisible-p)
  25613. (outline-invisible-p)
  25614. (get-char-property (point) 'invisible)))
  25615. (defun org-invisible-p2 ()
  25616. "Check if point is at a character currently not visible."
  25617. (save-excursion
  25618. (if (and (eolp) (not (bobp))) (backward-char 1))
  25619. ;; Early versions of noutline don't have `outline-invisible-p'.
  25620. (if (fboundp 'outline-invisible-p)
  25621. (outline-invisible-p)
  25622. (get-char-property (point) 'invisible))))
  25623. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25624. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25625. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25626. (defun org-at-heading-or-item-p ()
  25627. (or (org-on-heading-p) (org-at-item-p)))
  25628. (defun org-on-target-p ()
  25629. (or (org-in-regexp org-radio-target-regexp)
  25630. (org-in-regexp org-target-regexp)))
  25631. (defun org-up-heading-all (arg)
  25632. "Move to the heading line of which the present line is a subheading.
  25633. This function considers both visible and invisible heading lines.
  25634. With argument, move up ARG levels."
  25635. (if (fboundp 'outline-up-heading-all)
  25636. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25637. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25638. (defun org-up-heading-safe ()
  25639. "Move to the heading line of which the present line is a subheading.
  25640. This version will not throw an error. It will return the level of the
  25641. headline found, or nil if no higher level is found."
  25642. (let ((pos (point)) start-level level
  25643. (re (concat "^" outline-regexp)))
  25644. (catch 'exit
  25645. (outline-back-to-heading t)
  25646. (setq start-level (funcall outline-level))
  25647. (if (equal start-level 1) (throw 'exit nil))
  25648. (while (re-search-backward re nil t)
  25649. (setq level (funcall outline-level))
  25650. (if (< level start-level) (throw 'exit level)))
  25651. nil)))
  25652. (defun org-first-sibling-p ()
  25653. "Is this heading the first child of its parents?"
  25654. (interactive)
  25655. (let ((re (concat "^" outline-regexp))
  25656. level l)
  25657. (unless (org-at-heading-p t)
  25658. (error "Not at a heading"))
  25659. (setq level (funcall outline-level))
  25660. (save-excursion
  25661. (if (not (re-search-backward re nil t))
  25662. t
  25663. (setq l (funcall outline-level))
  25664. (< l level)))))
  25665. (defun org-goto-sibling (&optional previous)
  25666. "Goto the next sibling, even if it is invisible.
  25667. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25668. when a sibling was found. When none is found, return nil and don't
  25669. move point."
  25670. (let ((fun (if previous 're-search-backward 're-search-forward))
  25671. (pos (point))
  25672. (re (concat "^" outline-regexp))
  25673. level l)
  25674. (when (condition-case nil (org-back-to-heading t) (error nil))
  25675. (setq level (funcall outline-level))
  25676. (catch 'exit
  25677. (or previous (forward-char 1))
  25678. (while (funcall fun re nil t)
  25679. (setq l (funcall outline-level))
  25680. (when (< l level) (goto-char pos) (throw 'exit nil))
  25681. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25682. (goto-char pos)
  25683. nil))))
  25684. (defun org-show-siblings ()
  25685. "Show all siblings of the current headline."
  25686. (save-excursion
  25687. (while (org-goto-sibling) (org-flag-heading nil)))
  25688. (save-excursion
  25689. (while (org-goto-sibling 'previous)
  25690. (org-flag-heading nil))))
  25691. (defun org-show-hidden-entry ()
  25692. "Show an entry where even the heading is hidden."
  25693. (save-excursion
  25694. (org-show-entry)))
  25695. (defun org-flag-heading (flag &optional entry)
  25696. "Flag the current heading. FLAG non-nil means make invisible.
  25697. When ENTRY is non-nil, show the entire entry."
  25698. (save-excursion
  25699. (org-back-to-heading t)
  25700. ;; Check if we should show the entire entry
  25701. (if entry
  25702. (progn
  25703. (org-show-entry)
  25704. (save-excursion
  25705. (and (outline-next-heading)
  25706. (org-flag-heading nil))))
  25707. (outline-flag-region (max (point-min) (1- (point)))
  25708. (save-excursion (outline-end-of-heading) (point))
  25709. flag))))
  25710. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25711. ;; This is an exact copy of the original function, but it uses
  25712. ;; `org-back-to-heading', to make it work also in invisible
  25713. ;; trees. And is uses an invisible-OK argument.
  25714. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25715. (org-back-to-heading invisible-OK)
  25716. (let ((first t)
  25717. (level (funcall outline-level)))
  25718. (while (and (not (eobp))
  25719. (or first (> (funcall outline-level) level)))
  25720. (setq first nil)
  25721. (outline-next-heading))
  25722. (unless to-heading
  25723. (if (memq (preceding-char) '(?\n ?\^M))
  25724. (progn
  25725. ;; Go to end of line before heading
  25726. (forward-char -1)
  25727. (if (memq (preceding-char) '(?\n ?\^M))
  25728. ;; leave blank line before heading
  25729. (forward-char -1))))))
  25730. (point))
  25731. (defun org-show-subtree ()
  25732. "Show everything after this heading at deeper levels."
  25733. (outline-flag-region
  25734. (point)
  25735. (save-excursion
  25736. (outline-end-of-subtree) (outline-next-heading) (point))
  25737. nil))
  25738. (defun org-show-entry ()
  25739. "Show the body directly following this heading.
  25740. Show the heading too, if it is currently invisible."
  25741. (interactive)
  25742. (save-excursion
  25743. (condition-case nil
  25744. (progn
  25745. (org-back-to-heading t)
  25746. (outline-flag-region
  25747. (max (point-min) (1- (point)))
  25748. (save-excursion
  25749. (re-search-forward
  25750. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25751. (or (match-beginning 1) (point-max)))
  25752. nil))
  25753. (error nil))))
  25754. (defun org-make-options-regexp (kwds)
  25755. "Make a regular expression for keyword lines."
  25756. (concat
  25757. "^"
  25758. "#?[ \t]*\\+\\("
  25759. (mapconcat 'regexp-quote kwds "\\|")
  25760. "\\):[ \t]*"
  25761. "\\(.+\\)"))
  25762. ;; Make isearch reveal the necessary context
  25763. (defun org-isearch-end ()
  25764. "Reveal context after isearch exits."
  25765. (when isearch-success ; only if search was successful
  25766. (if (featurep 'xemacs)
  25767. ;; Under XEmacs, the hook is run in the correct place,
  25768. ;; we directly show the context.
  25769. (org-show-context 'isearch)
  25770. ;; In Emacs the hook runs *before* restoring the overlays.
  25771. ;; So we have to use a one-time post-command-hook to do this.
  25772. ;; (Emacs 22 has a special variable, see function `org-mode')
  25773. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25774. isearch-mode-end-hook-quit)
  25775. ;; Only when the isearch was not quitted.
  25776. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25777. 'append 'local)))))
  25778. (defun org-isearch-post-command ()
  25779. "Remove self from hook, and show context."
  25780. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25781. (org-show-context 'isearch))
  25782. ;;;; Integration with and fixes for other packages
  25783. ;;; Imenu support
  25784. (defvar org-imenu-markers nil
  25785. "All markers currently used by Imenu.")
  25786. (make-variable-buffer-local 'org-imenu-markers)
  25787. (defun org-imenu-new-marker (&optional pos)
  25788. "Return a new marker for use by Imenu, and remember the marker."
  25789. (let ((m (make-marker)))
  25790. (move-marker m (or pos (point)))
  25791. (push m org-imenu-markers)
  25792. m))
  25793. (defun org-imenu-get-tree ()
  25794. "Produce the index for Imenu."
  25795. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25796. (setq org-imenu-markers nil)
  25797. (let* ((n org-imenu-depth)
  25798. (re (concat "^" outline-regexp))
  25799. (subs (make-vector (1+ n) nil))
  25800. (last-level 0)
  25801. m tree level head)
  25802. (save-excursion
  25803. (save-restriction
  25804. (widen)
  25805. (goto-char (point-max))
  25806. (while (re-search-backward re nil t)
  25807. (setq level (org-reduced-level (funcall outline-level)))
  25808. (when (<= level n)
  25809. (looking-at org-complex-heading-regexp)
  25810. (setq head (org-match-string-no-properties 4)
  25811. m (org-imenu-new-marker))
  25812. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25813. (if (>= level last-level)
  25814. (push (cons head m) (aref subs level))
  25815. (push (cons head (aref subs (1+ level))) (aref subs level))
  25816. (loop for i from (1+ level) to n do (aset subs i nil)))
  25817. (setq last-level level)))))
  25818. (aref subs 1)))
  25819. (eval-after-load "imenu"
  25820. '(progn
  25821. (add-hook 'imenu-after-jump-hook
  25822. (lambda () (org-show-context 'org-goto)))))
  25823. ;; Speedbar support
  25824. (defun org-speedbar-set-agenda-restriction ()
  25825. "Restrict future agenda commands to the location at point in speedbar.
  25826. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  25827. (interactive)
  25828. (let (p m tp np dir txt w)
  25829. (cond
  25830. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25831. 'org-imenu t))
  25832. (setq m (get-text-property p 'org-imenu-marker))
  25833. (save-excursion
  25834. (save-restriction
  25835. (set-buffer (marker-buffer m))
  25836. (goto-char m)
  25837. (org-agenda-set-restriction-lock 'subtree))))
  25838. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25839. 'speedbar-function 'speedbar-find-file))
  25840. (setq tp (previous-single-property-change
  25841. (1+ p) 'speedbar-function)
  25842. np (next-single-property-change
  25843. tp 'speedbar-function)
  25844. dir (speedbar-line-directory)
  25845. txt (buffer-substring-no-properties (or tp (point-min))
  25846. (or np (point-max))))
  25847. (save-excursion
  25848. (save-restriction
  25849. (set-buffer (find-file-noselect
  25850. (let ((default-directory dir))
  25851. (expand-file-name txt))))
  25852. (unless (org-mode-p)
  25853. (error "Cannot restrict to non-Org-mode file"))
  25854. (org-agenda-set-restriction-lock 'file))))
  25855. (t (error "Don't know how to restrict Org-mode's agenda")))
  25856. (org-move-overlay org-speedbar-restriction-lock-overlay
  25857. (point-at-bol) (point-at-eol))
  25858. (setq current-prefix-arg nil)
  25859. (org-agenda-maybe-redo)))
  25860. (eval-after-load "speedbar"
  25861. '(progn
  25862. (speedbar-add-supported-extension ".org")
  25863. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  25864. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  25865. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  25866. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  25867. (add-hook 'speedbar-visiting-tag-hook
  25868. (lambda () (org-show-context 'org-goto)))))
  25869. ;;; Fixes and Hacks
  25870. ;; Make flyspell not check words in links, to not mess up our keymap
  25871. (defun org-mode-flyspell-verify ()
  25872. "Don't let flyspell put overlays at active buttons."
  25873. (not (get-text-property (point) 'keymap)))
  25874. ;; Make `bookmark-jump' show the jump location if it was hidden.
  25875. (eval-after-load "bookmark"
  25876. '(if (boundp 'bookmark-after-jump-hook)
  25877. ;; We can use the hook
  25878. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  25879. ;; Hook not available, use advice
  25880. (defadvice bookmark-jump (after org-make-visible activate)
  25881. "Make the position visible."
  25882. (org-bookmark-jump-unhide))))
  25883. (defun org-bookmark-jump-unhide ()
  25884. "Unhide the current position, to show the bookmark location."
  25885. (and (org-mode-p)
  25886. (or (org-invisible-p)
  25887. (save-excursion (goto-char (max (point-min) (1- (point))))
  25888. (org-invisible-p)))
  25889. (org-show-context 'bookmark-jump)))
  25890. ;; Fix a bug in htmlize where there are text properties (face nil)
  25891. (eval-after-load "htmlize"
  25892. '(progn
  25893. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  25894. "Make sure there are no nil faces"
  25895. (setq ad-return-value (delq nil ad-return-value)))))
  25896. ;; Make session.el ignore our circular variable
  25897. (eval-after-load "session"
  25898. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  25899. ;;;; Experimental code
  25900. (defun org-closed-in-range ()
  25901. "Sparse tree of items closed in a certain time range.
  25902. Still experimental, may disappear in the future."
  25903. (interactive)
  25904. ;; Get the time interval from the user.
  25905. (let* ((time1 (time-to-seconds
  25906. (org-read-date nil 'to-time nil "Starting date: ")))
  25907. (time2 (time-to-seconds
  25908. (org-read-date nil 'to-time nil "End date:")))
  25909. ;; callback function
  25910. (callback (lambda ()
  25911. (let ((time
  25912. (time-to-seconds
  25913. (apply 'encode-time
  25914. (org-parse-time-string
  25915. (match-string 1))))))
  25916. ;; check if time in interval
  25917. (and (>= time time1) (<= time time2))))))
  25918. ;; make tree, check each match with the callback
  25919. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  25920. ;(let ((org-refile-targets org-refile-targets)
  25921. ; (org-refile-use-outline-path org-refile-use-outline-path))
  25922. ; (when (equal just-goto '(16))
  25923. ; (setq org-refile-targets '((nil . (:maxlevel . 10))))
  25924. ; (setq org-refile-use-outline-path t))
  25925. ; (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  25926. ;;;; Finish up
  25927. (provide 'org)
  25928. (run-hooks 'org-load-hook)
  25929. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  25930. ;;; org.el ends here