org.el 1.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712
  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.22a+
  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. (require 'cl)
  66. (eval-when-compile
  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.22a+"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. (defcustom org-load-hook '(org-load-default-extensions)
  138. "Hook that is run after org.el has been loaded.
  139. This happens also after `org' has been provided, so
  140. requiring something in this hook that does a (require 'org) is ok."
  141. :group 'org
  142. :type 'hook)
  143. (defcustom org-default-extensions '(org-irc)
  144. "Extensions that should always be loaded together with org.el.
  145. If the description starts with <A>, this means the extension
  146. will be autoloaded when needed, preloading is not necessary."
  147. :group 'org
  148. :type
  149. '(set :greedy t
  150. (const :tag " Mouse support (org-mouse.el)" org-mouse)
  151. (const :tag "<A> Publishing (org-publish.el)" org-publish)
  152. (const :tag "<A> LaTeX export (org-export-latex.el)" org-export-latex)
  153. (const :tag " IRC/ERC links (org-irc.el)" org-irc)
  154. (const :tag " Apple Mail message links under OS X (org-mac-message.el)" org-mac-message)))
  155. (defun org-load-default-extensions ()
  156. "Load all extensions listed in `org-default-extensions'."
  157. (mapc (lambda (ext)
  158. (condition-case nil (require ext)
  159. (error (message "Problems while trying to load feature `%s'" ext))))
  160. org-default-extensions))
  161. ;; FIXME: Needs a separate group...
  162. (defcustom org-completion-fallback-command 'hippie-expand
  163. "The expansion command called by \\[org-complete] in normal context.
  164. Normal means, no org-mode-specific context."
  165. :group 'org
  166. :type 'function)
  167. (defgroup org-startup nil
  168. "Options concerning startup of Org-mode."
  169. :tag "Org Startup"
  170. :group 'org)
  171. (defcustom org-startup-folded t
  172. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  173. This can also be configured on a per-file basis by adding one of
  174. the following lines anywhere in the buffer:
  175. #+STARTUP: fold
  176. #+STARTUP: nofold
  177. #+STARTUP: content"
  178. :group 'org-startup
  179. :type '(choice
  180. (const :tag "nofold: show all" nil)
  181. (const :tag "fold: overview" t)
  182. (const :tag "content: all headlines" content)))
  183. (defcustom org-startup-truncated t
  184. "Non-nil means, entering Org-mode will set `truncate-lines'.
  185. This is useful since some lines containing links can be very long and
  186. uninteresting. Also tables look terrible when wrapped."
  187. :group 'org-startup
  188. :type 'boolean)
  189. (defcustom org-startup-align-all-tables nil
  190. "Non-nil means, align all tables when visiting a file.
  191. This is useful when the column width in tables is forced with <N> cookies
  192. in table fields. Such tables will look correct only after the first re-align.
  193. This can also be configured on a per-file basis by adding one of
  194. the following lines anywhere in the buffer:
  195. #+STARTUP: align
  196. #+STARTUP: noalign"
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-insert-mode-line-in-empty-file nil
  200. "Non-nil means insert the first line setting Org-mode in empty files.
  201. When the function `org-mode' is called interactively in an empty file, this
  202. normally means that the file name does not automatically trigger Org-mode.
  203. To ensure that the file will always be in Org-mode in the future, a
  204. line enforcing Org-mode will be inserted into the buffer, if this option
  205. has been set."
  206. :group 'org-startup
  207. :type 'boolean)
  208. (defcustom org-replace-disputed-keys nil
  209. "Non-nil means use alternative key bindings for some keys.
  210. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  211. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  212. If you want to use Org-mode together with one of these other modes,
  213. or more generally if you would like to move some Org-mode commands to
  214. other keys, set this variable and configure the keys with the variable
  215. `org-disputed-keys'.
  216. This option is only relevant at load-time of Org-mode, and must be set
  217. *before* org.el is loaded. Changing it requires a restart of Emacs to
  218. become effective."
  219. :group 'org-startup
  220. :type 'boolean)
  221. (if (fboundp 'defvaralias)
  222. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  223. (defcustom org-disputed-keys
  224. '(([(shift up)] . [(meta p)])
  225. ([(shift down)] . [(meta n)])
  226. ([(shift left)] . [(meta -)])
  227. ([(shift right)] . [(meta +)])
  228. ([(control shift right)] . [(meta shift +)])
  229. ([(control shift left)] . [(meta shift -)]))
  230. "Keys for which Org-mode and other modes compete.
  231. This is an alist, cars are the default keys, second element specifies
  232. the alternative to use when `org-replace-disputed-keys' is t.
  233. Keys can be specified in any syntax supported by `define-key'.
  234. The value of this option takes effect only at Org-mode's startup,
  235. therefore you'll have to restart Emacs to apply it after changing."
  236. :group 'org-startup
  237. :type 'alist)
  238. (defun org-key (key)
  239. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  240. Or return the original if not disputed."
  241. (if org-replace-disputed-keys
  242. (let* ((nkey (key-description key))
  243. (x (org-find-if (lambda (x)
  244. (equal (key-description (car x)) nkey))
  245. org-disputed-keys)))
  246. (if x (cdr x) key))
  247. key))
  248. (defun org-find-if (predicate seq)
  249. (catch 'exit
  250. (while seq
  251. (if (funcall predicate (car seq))
  252. (throw 'exit (car seq))
  253. (pop seq)))))
  254. (defun org-defkey (keymap key def)
  255. "Define a key, possibly translated, as returned by `org-key'."
  256. (define-key keymap (org-key key) def))
  257. (defcustom org-ellipsis nil
  258. "The ellipsis to use in the Org-mode outline.
  259. When nil, just use the standard three dots. When a string, use that instead,
  260. When a face, use the standart 3 dots, but with the specified face.
  261. The change affects only Org-mode (which will then use its own display table).
  262. Changing this requires executing `M-x org-mode' in a buffer to become
  263. effective."
  264. :group 'org-startup
  265. :type '(choice (const :tag "Default" nil)
  266. (face :tag "Face" :value org-warning)
  267. (string :tag "String" :value "...#")))
  268. (defvar org-display-table nil
  269. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  270. (defgroup org-keywords nil
  271. "Keywords in Org-mode."
  272. :tag "Org Keywords"
  273. :group 'org)
  274. (defcustom org-deadline-string "DEADLINE:"
  275. "String to mark deadline entries.
  276. A deadline is this string, followed by a time stamp. Should be a word,
  277. terminated by a colon. You can insert a schedule keyword and
  278. a timestamp with \\[org-deadline].
  279. Changes become only effective after restarting Emacs."
  280. :group 'org-keywords
  281. :type 'string)
  282. (defcustom org-scheduled-string "SCHEDULED:"
  283. "String to mark scheduled TODO entries.
  284. A schedule is this string, followed by a time stamp. Should be a word,
  285. terminated by a colon. You can insert a schedule keyword and
  286. a timestamp with \\[org-schedule].
  287. Changes become only effective after restarting Emacs."
  288. :group 'org-keywords
  289. :type 'string)
  290. (defcustom org-closed-string "CLOSED:"
  291. "String used as the prefix for timestamps logging closing a TODO entry."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-clock-string "CLOCK:"
  295. "String used as prefix for timestamps clocking work hours on an item."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-comment-string "COMMENT"
  299. "Entries starting with this keyword will never be exported.
  300. An entry can be toggled between COMMENT and normal with
  301. \\[org-toggle-comment].
  302. Changes become only effective after restarting Emacs."
  303. :group 'org-keywords
  304. :type 'string)
  305. (defcustom org-quote-string "QUOTE"
  306. "Entries starting with this keyword will be exported in fixed-width font.
  307. Quoting applies only to the text in the entry following the headline, and does
  308. not extend beyond the next headline, even if that is lower level.
  309. An entry can be toggled between QUOTE and normal with
  310. \\[org-toggle-fixed-width-section]."
  311. :group 'org-keywords
  312. :type 'string)
  313. (defconst org-repeat-re
  314. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  315. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  316. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  317. "Regular expression for specifying repeated events.
  318. After a match, group 1 contains the repeat expression.")
  319. (defgroup org-structure nil
  320. "Options concerning the general structure of Org-mode files."
  321. :tag "Org Structure"
  322. :group 'org)
  323. (defgroup org-reveal-location nil
  324. "Options about how to make context of a location visible."
  325. :tag "Org Reveal Location"
  326. :group 'org-structure)
  327. (defconst org-context-choice
  328. '(choice
  329. (const :tag "Always" t)
  330. (const :tag "Never" nil)
  331. (repeat :greedy t :tag "Individual contexts"
  332. (cons
  333. (choice :tag "Context"
  334. (const agenda)
  335. (const org-goto)
  336. (const occur-tree)
  337. (const tags-tree)
  338. (const link-search)
  339. (const mark-goto)
  340. (const bookmark-jump)
  341. (const isearch)
  342. (const default))
  343. (boolean))))
  344. "Contexts for the reveal options.")
  345. (defcustom org-show-hierarchy-above '((default . t))
  346. "Non-nil means, show full hierarchy when revealing a location.
  347. Org-mode often shows locations in an org-mode file which might have
  348. been invisible before. When this is set, the hierarchy of headings
  349. above the exposed location is shown.
  350. Turning this off for example for sparse trees makes them very compact.
  351. Instead of t, this can also be an alist specifying this option for different
  352. contexts. Valid contexts are
  353. agenda when exposing an entry from the agenda
  354. org-goto when using the command `org-goto' on key C-c C-j
  355. occur-tree when using the command `org-occur' on key C-c /
  356. tags-tree when constructing a sparse tree based on tags matches
  357. link-search when exposing search matches associated with a link
  358. mark-goto when exposing the jump goal of a mark
  359. bookmark-jump when exposing a bookmark location
  360. isearch when exiting from an incremental search
  361. default default for all contexts not set explicitly"
  362. :group 'org-reveal-location
  363. :type org-context-choice)
  364. (defcustom org-show-following-heading '((default . nil))
  365. "Non-nil means, show following heading when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the heading following the
  368. match is shown.
  369. Turning this off for example for sparse trees makes them very compact,
  370. but makes it harder to edit the location of the match. In such a case,
  371. use the command \\[org-reveal] to show more context.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defcustom org-show-siblings '((default . nil) (isearch t))
  377. "Non-nil means, show all sibling heading when revealing a location.
  378. Org-mode often shows locations in an org-mode file which might have
  379. been invisible before. When this is set, the sibling of the current entry
  380. heading are all made visible. If `org-show-hierarchy-above' is t,
  381. the same happens on each level of the hierarchy above the current entry.
  382. By default this is on for the isearch context, off for all other contexts.
  383. Turning this off for example for sparse trees makes them very compact,
  384. but makes it harder to edit the location of the match. In such a case,
  385. use the command \\[org-reveal] to show more context.
  386. Instead of t, this can also be an alist specifying this option for different
  387. contexts. See `org-show-hierarchy-above' for valid contexts."
  388. :group 'org-reveal-location
  389. :type org-context-choice)
  390. (defcustom org-show-entry-below '((default . nil))
  391. "Non-nil means, show the entry below a headline when revealing a location.
  392. Org-mode often shows locations in an org-mode file which might have
  393. been invisible before. When this is set, the text below the headline that is
  394. exposed is also shown.
  395. By default this is off for all contexts.
  396. Instead of t, this can also be an alist specifying this option for different
  397. contexts. See `org-show-hierarchy-above' for valid contexts."
  398. :group 'org-reveal-location
  399. :type org-context-choice)
  400. (defgroup org-cycle nil
  401. "Options concerning visibility cycling in Org-mode."
  402. :tag "Org Cycle"
  403. :group 'org-structure)
  404. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  405. "Names of drawers. Drawers are not opened by cycling on the headline above.
  406. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  407. this:
  408. :DRAWERNAME:
  409. .....
  410. :END:
  411. The drawer \"PROPERTIES\" is special for capturing properties through
  412. the property API.
  413. Drawers can be defined on the per-file basis with a line like:
  414. #+DRAWERS: HIDDEN STATE PROPERTIES"
  415. :group 'org-structure
  416. :type '(repeat (string :tag "Drawer Name")))
  417. (defcustom org-cycle-global-at-bob nil
  418. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  419. This makes it possible to do global cycling without having to use S-TAB or
  420. C-u TAB. For this special case to work, the first line of the buffer
  421. must not be a headline - it may be empty ot some other text. When used in
  422. this way, `org-cycle-hook' is disables temporarily, to make sure the
  423. cursor stays at the beginning of the buffer.
  424. When this option is nil, don't do anything special at the beginning
  425. of the buffer."
  426. :group 'org-cycle
  427. :type 'boolean)
  428. (defcustom org-cycle-emulate-tab t
  429. "Where should `org-cycle' emulate TAB.
  430. nil Never
  431. white Only in completely white lines
  432. whitestart Only at the beginning of lines, before the first non-white char
  433. t Everywhere except in headlines
  434. exc-hl-bol Everywhere except at the start of a headline
  435. If TAB is used in a place where it does not emulate TAB, the current subtree
  436. visibility is cycled."
  437. :group 'org-cycle
  438. :type '(choice (const :tag "Never" nil)
  439. (const :tag "Only in completely white lines" white)
  440. (const :tag "Before first char in a line" whitestart)
  441. (const :tag "Everywhere except in headlines" t)
  442. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  443. ))
  444. (defcustom org-cycle-separator-lines 2
  445. "Number of empty lines needed to keep an empty line between collapsed trees.
  446. If you leave an empty line between the end of a subtree and the following
  447. headline, this empty line is hidden when the subtree is folded.
  448. Org-mode will leave (exactly) one empty line visible if the number of
  449. empty lines is equal or larger to the number given in this variable.
  450. So the default 2 means, at least 2 empty lines after the end of a subtree
  451. are needed to produce free space between a collapsed subtree and the
  452. following headline.
  453. Special case: when 0, never leave empty lines in collapsed view."
  454. :group 'org-cycle
  455. :type 'integer)
  456. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  457. org-cycle-hide-drawers
  458. org-cycle-show-empty-lines
  459. org-optimize-window-after-visibility-change)
  460. "Hook that is run after `org-cycle' has changed the buffer visibility.
  461. The function(s) in this hook must accept a single argument which indicates
  462. the new state that was set by the most recent `org-cycle' command. The
  463. argument is a symbol. After a global state change, it can have the values
  464. `overview', `content', or `all'. After a local state change, it can have
  465. the values `folded', `children', or `subtree'."
  466. :group 'org-cycle
  467. :type 'hook)
  468. (defgroup org-edit-structure nil
  469. "Options concerning structure editing in Org-mode."
  470. :tag "Org Edit Structure"
  471. :group 'org-structure)
  472. (defcustom org-odd-levels-only nil
  473. "Non-nil means, skip even levels and only use odd levels for the outline.
  474. This has the effect that two stars are being added/taken away in
  475. promotion/demotion commands. It also influences how levels are
  476. handled by the exporters.
  477. Changing it requires restart of `font-lock-mode' to become effective
  478. for fontification also in regions already fontified.
  479. You may also set this on a per-file basis by adding one of the following
  480. lines to the buffer:
  481. #+STARTUP: odd
  482. #+STARTUP: oddeven"
  483. :group 'org-edit-structure
  484. :group 'org-font-lock
  485. :type 'boolean)
  486. (defcustom org-adapt-indentation t
  487. "Non-nil means, adapt indentation when promoting and demoting.
  488. When this is set and the *entire* text in an entry is indented, the
  489. indentation is increased by one space in a demotion command, and
  490. decreased by one in a promotion command. If any line in the entry
  491. body starts at column 0, indentation is not changed at all."
  492. :group 'org-edit-structure
  493. :type 'boolean)
  494. (defcustom org-special-ctrl-a/e nil
  495. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  496. When t, `C-a' will bring back the cursor to the beginning of the
  497. headline text, i.e. after the stars and after a possible TODO keyword.
  498. In an item, this will be the position after the bullet.
  499. When the cursor is already at that position, another `C-a' will bring
  500. it to the beginning of the line.
  501. `C-e' will jump to the end of the headline, ignoring the presence of tags
  502. in the headline. A second `C-e' will then jump to the true end of the
  503. line, after any tags.
  504. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  505. and only a directly following, identical keypress will bring the cursor
  506. to the special positions."
  507. :group 'org-edit-structure
  508. :type '(choice
  509. (const :tag "off" nil)
  510. (const :tag "after bullet first" t)
  511. (const :tag "border first" reversed)))
  512. (if (fboundp 'defvaralias)
  513. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  514. (defcustom org-special-ctrl-k nil
  515. "Non-nil means `C-k' will behave specially in headlines.
  516. When nil, `C-k' will call the default `kill-line' command.
  517. When t, the following will happen while the cursor is in the headline:
  518. - When the cursor is at the beginning of a headline, kill the entire
  519. line and possible the folded subtree below the line.
  520. - When in the middle of the headline text, kill the headline up to the tags.
  521. - When after the headline text, kill the tags."
  522. :group 'org-edit-structure
  523. :type 'boolean)
  524. (defcustom org-M-RET-may-split-line '((default . t))
  525. "Non-nil means, M-RET will split the line at the cursor position.
  526. When nil, it will go to the end of the line before making a
  527. new line.
  528. You may also set this option in a different way for different
  529. contexts. Valid contexts are:
  530. headline when creating a new headline
  531. item when creating a new item
  532. table in a table field
  533. default the value to be used for all contexts not explicitly
  534. customized"
  535. :group 'org-structure
  536. :group 'org-table
  537. :type '(choice
  538. (const :tag "Always" t)
  539. (const :tag "Never" nil)
  540. (repeat :greedy t :tag "Individual contexts"
  541. (cons
  542. (choice :tag "Context"
  543. (const headline)
  544. (const item)
  545. (const table)
  546. (const default))
  547. (boolean)))))
  548. (defcustom org-blank-before-new-entry '((heading . nil)
  549. (plain-list-item . nil))
  550. "Should `org-insert-heading' leave a blank line before new heading/item?
  551. The value is an alist, with `heading' and `plain-list-item' as car,
  552. and a boolean flag as cdr."
  553. :group 'org-edit-structure
  554. :type '(list
  555. (cons (const heading) (boolean))
  556. (cons (const plain-list-item) (boolean))))
  557. (defcustom org-insert-heading-hook nil
  558. "Hook being run after inserting a new heading."
  559. :group 'org-edit-structure
  560. :type 'hook)
  561. (defcustom org-enable-fixed-width-editor t
  562. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  563. This currently only means, they are never auto-wrapped.
  564. When nil, such lines will be treated like ordinary lines.
  565. See also the QUOTE keyword."
  566. :group 'org-edit-structure
  567. :type 'boolean)
  568. (defcustom org-goto-auto-isearch t
  569. "Non-nil means, typing characters in org-goto starts incremental search."
  570. :group 'org-edit-structure
  571. :type 'boolean)
  572. (defgroup org-sparse-trees nil
  573. "Options concerning sparse trees in Org-mode."
  574. :tag "Org Sparse Trees"
  575. :group 'org-structure)
  576. (defcustom org-highlight-sparse-tree-matches t
  577. "Non-nil means, highlight all matches that define a sparse tree.
  578. The highlights will automatically disappear the next time the buffer is
  579. changed by an edit command."
  580. :group 'org-sparse-trees
  581. :type 'boolean)
  582. (defcustom org-remove-highlights-with-change t
  583. "Non-nil means, any change to the buffer will remove temporary highlights.
  584. Such highlights are created by `org-occur' and `org-clock-display'.
  585. When nil, `C-c C-c needs to be used to get rid of the highlights.
  586. The highlights created by `org-preview-latex-fragment' always need
  587. `C-c C-c' to be removed."
  588. :group 'org-sparse-trees
  589. :group 'org-time
  590. :type 'boolean)
  591. (defcustom org-occur-hook '(org-first-headline-recenter)
  592. "Hook that is run after `org-occur' has constructed a sparse tree.
  593. This can be used to recenter the window to show as much of the structure
  594. as possible."
  595. :group 'org-sparse-trees
  596. :type 'hook)
  597. (defgroup org-plain-lists nil
  598. "Options concerning plain lists in Org-mode."
  599. :tag "Org Plain lists"
  600. :group 'org-structure)
  601. (defcustom org-cycle-include-plain-lists nil
  602. "Non-nil means, include plain lists into visibility cycling.
  603. This means that during cycling, plain list items will *temporarily* be
  604. interpreted as outline headlines with a level given by 1000+i where i is the
  605. indentation of the bullet. In all other operations, plain list items are
  606. not seen as headlines. For example, you cannot assign a TODO keyword to
  607. such an item."
  608. :group 'org-plain-lists
  609. :type 'boolean)
  610. (defcustom org-plain-list-ordered-item-terminator t
  611. "The character that makes a line with leading number an ordered list item.
  612. Valid values are ?. and ?\). To get both terminators, use t. While
  613. ?. may look nicer, it creates the danger that a line with leading
  614. number may be incorrectly interpreted as an item. ?\) therefore is
  615. the safe choice."
  616. :group 'org-plain-lists
  617. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  618. (const :tag "paren like in \"2)\"" ?\))
  619. (const :tab "both" t)))
  620. (defcustom org-auto-renumber-ordered-lists t
  621. "Non-nil means, automatically renumber ordered plain lists.
  622. Renumbering happens when the sequence have been changed with
  623. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  624. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  625. :group 'org-plain-lists
  626. :type 'boolean)
  627. (defcustom org-provide-checkbox-statistics t
  628. "Non-nil means, update checkbox statistics after insert and toggle.
  629. When this is set, checkbox statistics is updated each time you either insert
  630. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  631. with \\[org-ctrl-c-ctrl-c\\]."
  632. :group 'org-plain-lists
  633. :type 'boolean)
  634. (defgroup org-archive nil
  635. "Options concerning archiving in Org-mode."
  636. :tag "Org Archive"
  637. :group 'org-structure)
  638. (defcustom org-archive-tag "ARCHIVE"
  639. "The tag that marks a subtree as archived.
  640. An archived subtree does not open during visibility cycling, and does
  641. not contribute to the agenda listings.
  642. After changing this, font-lock must be restarted in the relevant buffers to
  643. get the proper fontification."
  644. :group 'org-archive
  645. :group 'org-keywords
  646. :type 'string)
  647. (defcustom org-agenda-skip-archived-trees t
  648. "Non-nil means, the agenda will skip any items located in archived trees.
  649. An archived tree is a tree marked with the tag ARCHIVE."
  650. :group 'org-archive
  651. :group 'org-agenda-skip
  652. :type 'boolean)
  653. (defcustom org-cycle-open-archived-trees nil
  654. "Non-nil means, `org-cycle' will open archived trees.
  655. An archived tree is a tree marked with the tag ARCHIVE.
  656. When nil, archived trees will stay folded. You can still open them with
  657. normal outline commands like `show-all', but not with the cycling commands."
  658. :group 'org-archive
  659. :group 'org-cycle
  660. :type 'boolean)
  661. (defcustom org-sparse-tree-open-archived-trees nil
  662. "Non-nil means sparse tree construction shows matches in archived trees.
  663. When nil, matches in these trees are highlighted, but the trees are kept in
  664. collapsed state."
  665. :group 'org-archive
  666. :group 'org-sparse-trees
  667. :type 'boolean)
  668. (defcustom org-archive-location "%s_archive::"
  669. "The location where subtrees should be archived.
  670. This string consists of two parts, separated by a double-colon.
  671. The first part is a file name - when omitted, archiving happens in the same
  672. file. %s will be replaced by the current file name (without directory part).
  673. Archiving to a different file is useful to keep archived entries from
  674. contributing to the Org-mode Agenda.
  675. The part after the double colon is a headline. The archived entries will be
  676. filed under that headline. When omitted, the subtrees are simply filed away
  677. at the end of the file, as top-level entries.
  678. Here are a few examples:
  679. \"%s_archive::\"
  680. If the current file is Projects.org, archive in file
  681. Projects.org_archive, as top-level trees. This is the default.
  682. \"::* Archived Tasks\"
  683. Archive in the current file, under the top-level headline
  684. \"* Archived Tasks\".
  685. \"~/org/archive.org::\"
  686. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  687. \"basement::** Finished Tasks\"
  688. Archive in file ./basement (relative path), as level 3 trees
  689. below the level 2 heading \"** Finished Tasks\".
  690. You may set this option on a per-file basis by adding to the buffer a
  691. line like
  692. #+ARCHIVE: basement::** Finished Tasks"
  693. :group 'org-archive
  694. :type 'string)
  695. (defcustom org-archive-mark-done t
  696. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  697. This can be a string to set the keyword to use. When t, Org-mode will
  698. use the first keyword in its list that means done."
  699. :group 'org-archive
  700. :type '(choice
  701. (const :tag "No" nil)
  702. (const :tag "Yes" t)
  703. (string :tag "Use this keyword")))
  704. (defcustom org-archive-stamp-time t
  705. "Non-nil means, add a time stamp to entries moved to an archive file.
  706. This variable is obsolete and has no effect anymore, instead add ot remove
  707. `time' from the variablle `org-archive-save-context-info'."
  708. :group 'org-archive
  709. :type 'boolean)
  710. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  711. "Parts of context info that should be stored as properties when archiving.
  712. When a subtree is moved to an archive file, it looses information given by
  713. context, like inherited tags, the category, and possibly also the TODO
  714. state (depending on the variable `org-archive-mark-done').
  715. This variable can be a list of any of the following symbols:
  716. time The time of archiving.
  717. file The file where the entry originates.
  718. itags The local tags, in the headline of the subtree.
  719. ltags The tags the subtree inherits from further up the hierarchy.
  720. todo The pre-archive TODO state.
  721. category The category, taken from file name or #+CATEGORY lines.
  722. olpath The outline path to the item. These are all headlines above
  723. the current item, separated by /, like a file path.
  724. For each symbol present in the list, a property will be created in
  725. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  726. information."
  727. :group 'org-archive
  728. :type '(set :greedy t
  729. (const :tag "Time" time)
  730. (const :tag "File" file)
  731. (const :tag "Category" category)
  732. (const :tag "TODO state" todo)
  733. (const :tag "TODO state" priority)
  734. (const :tag "Inherited tags" itags)
  735. (const :tag "Outline path" olpath)
  736. (const :tag "Local tags" ltags)))
  737. (defgroup org-imenu-and-speedbar nil
  738. "Options concerning imenu and speedbar in Org-mode."
  739. :tag "Org Imenu and Speedbar"
  740. :group 'org-structure)
  741. (defcustom org-imenu-depth 2
  742. "The maximum level for Imenu access to Org-mode headlines.
  743. This also applied for speedbar access."
  744. :group 'org-imenu-and-speedbar
  745. :type 'number)
  746. (defgroup org-table nil
  747. "Options concerning tables in Org-mode."
  748. :tag "Org Table"
  749. :group 'org)
  750. (defcustom org-enable-table-editor 'optimized
  751. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  752. When nil, such lines will be treated like ordinary lines.
  753. When equal to the symbol `optimized', the table editor will be optimized to
  754. do the following:
  755. - Automatic overwrite mode in front of whitespace in table fields.
  756. This makes the structure of the table stay in tact as long as the edited
  757. field does not exceed the column width.
  758. - Minimize the number of realigns. Normally, the table is aligned each time
  759. TAB or RET are pressed to move to another field. With optimization this
  760. happens only if changes to a field might have changed the column width.
  761. Optimization requires replacing the functions `self-insert-command',
  762. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  763. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  764. very good at guessing when a re-align will be necessary, but you can always
  765. force one with \\[org-ctrl-c-ctrl-c].
  766. If you would like to use the optimized version in Org-mode, but the
  767. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  768. This variable can be used to turn on and off the table editor during a session,
  769. but in order to toggle optimization, a restart is required.
  770. See also the variable `org-table-auto-blank-field'."
  771. :group 'org-table
  772. :type '(choice
  773. (const :tag "off" nil)
  774. (const :tag "on" t)
  775. (const :tag "on, optimized" optimized)))
  776. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  777. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  778. In the optimized version, the table editor takes over all simple keys that
  779. normally just insert a character. In tables, the characters are inserted
  780. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  781. for empty fields). Outside tables, the correct binding of the keys is
  782. restored.
  783. The default for this option is t if the optimized version is also used in
  784. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  785. this variable requires a restart of Emacs to become effective."
  786. :group 'org-table
  787. :type 'boolean)
  788. (defcustom orgtbl-radio-table-templates
  789. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  790. % END RECEIVE ORGTBL %n
  791. \\begin{comment}
  792. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  793. | | |
  794. \\end{comment}\n")
  795. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  796. @c END RECEIVE ORGTBL %n
  797. @ignore
  798. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  799. | | |
  800. @end ignore\n")
  801. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  802. <!-- END RECEIVE ORGTBL %n -->
  803. <!--
  804. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  805. | | |
  806. -->\n"))
  807. "Templates for radio tables in different major modes.
  808. All occurrences of %n in a template will be replaced with the name of the
  809. table, obtained by prompting the user."
  810. :group 'org-table
  811. :type '(repeat
  812. (list (symbol :tag "Major mode")
  813. (string :tag "Format"))))
  814. (defgroup org-table-settings nil
  815. "Settings for tables in Org-mode."
  816. :tag "Org Table Settings"
  817. :group 'org-table)
  818. (defcustom org-table-default-size "5x2"
  819. "The default size for newly created tables, Columns x Rows."
  820. :group 'org-table-settings
  821. :type 'string)
  822. (defcustom org-table-number-regexp
  823. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  824. "Regular expression for recognizing numbers in table columns.
  825. If a table column contains mostly numbers, it will be aligned to the
  826. right. If not, it will be aligned to the left.
  827. The default value of this option is a regular expression which allows
  828. anything which looks remotely like a number as used in scientific
  829. context. For example, all of the following will be considered a
  830. number:
  831. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  832. Other options offered by the customize interface are more restrictive."
  833. :group 'org-table-settings
  834. :type '(choice
  835. (const :tag "Positive Integers"
  836. "^[0-9]+$")
  837. (const :tag "Integers"
  838. "^[-+]?[0-9]+$")
  839. (const :tag "Floating Point Numbers"
  840. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  841. (const :tag "Floating Point Number or Integer"
  842. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  843. (const :tag "Exponential, Floating point, Integer"
  844. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  845. (const :tag "Very General Number-Like, including hex"
  846. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  847. (string :tag "Regexp:")))
  848. (defcustom org-table-number-fraction 0.5
  849. "Fraction of numbers in a column required to make the column align right.
  850. In a column all non-white fields are considered. If at least this
  851. fraction of fields is matched by `org-table-number-fraction',
  852. alignment to the right border applies."
  853. :group 'org-table-settings
  854. :type 'number)
  855. (defgroup org-table-editing nil
  856. "Behavior of tables during editing in Org-mode."
  857. :tag "Org Table Editing"
  858. :group 'org-table)
  859. (defcustom org-table-automatic-realign t
  860. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  861. When nil, aligning is only done with \\[org-table-align], or after column
  862. removal/insertion."
  863. :group 'org-table-editing
  864. :type 'boolean)
  865. (defcustom org-table-auto-blank-field t
  866. "Non-nil means, automatically blank table field when starting to type into it.
  867. This only happens when typing immediately after a field motion
  868. command (TAB, S-TAB or RET).
  869. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  870. :group 'org-table-editing
  871. :type 'boolean)
  872. (defcustom org-table-tab-jumps-over-hlines t
  873. "Non-nil means, tab in the last column of a table with jump over a hline.
  874. If a horizontal separator line is following the current line,
  875. `org-table-next-field' can either create a new row before that line, or jump
  876. over the line. When this option is nil, a new line will be created before
  877. this line."
  878. :group 'org-table-editing
  879. :type 'boolean)
  880. (defcustom org-table-tab-recognizes-table.el t
  881. "Non-nil means, TAB will automatically notice a table.el table.
  882. When it sees such a table, it moves point into it and - if necessary -
  883. calls `table-recognize-table'."
  884. :group 'org-table-editing
  885. :type 'boolean)
  886. (defgroup org-table-calculation nil
  887. "Options concerning tables in Org-mode."
  888. :tag "Org Table Calculation"
  889. :group 'org-table)
  890. (defcustom org-table-use-standard-references t
  891. "Should org-mode work with table refrences like B3 instead of @3$2?
  892. Possible values are:
  893. nil never use them
  894. from accept as input, do not present for editing
  895. t: accept as input and present for editing"
  896. :group 'org-table-calculation
  897. :type '(choice
  898. (const :tag "Never, don't even check unser input for them" nil)
  899. (const :tag "Always, both as user input, and when editing" t)
  900. (const :tag "Convert user input, don't offer during editing" 'from)))
  901. (defcustom org-table-copy-increment t
  902. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  903. :group 'org-table-calculation
  904. :type 'boolean)
  905. (defcustom org-calc-default-modes
  906. '(calc-internal-prec 12
  907. calc-float-format (float 5)
  908. calc-angle-mode deg
  909. calc-prefer-frac nil
  910. calc-symbolic-mode nil
  911. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  912. calc-display-working-message t
  913. )
  914. "List with Calc mode settings for use in calc-eval for table formulas.
  915. The list must contain alternating symbols (Calc modes variables and values).
  916. Don't remove any of the default settings, just change the values. Org-mode
  917. relies on the variables to be present in the list."
  918. :group 'org-table-calculation
  919. :type 'plist)
  920. (defcustom org-table-formula-evaluate-inline t
  921. "Non-nil means, TAB and RET evaluate a formula in current table field.
  922. If the current field starts with an equal sign, it is assumed to be a formula
  923. which should be evaluated as described in the manual and in the documentation
  924. string of the command `org-table-eval-formula'. This feature requires the
  925. Emacs calc package.
  926. When this variable is nil, formula calculation is only available through
  927. the command \\[org-table-eval-formula]."
  928. :group 'org-table-calculation
  929. :type 'boolean)
  930. (defcustom org-table-formula-use-constants t
  931. "Non-nil means, interpret constants in formulas in tables.
  932. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  933. by the value given in `org-table-formula-constants', or by a value obtained
  934. from the `constants.el' package."
  935. :group 'org-table-calculation
  936. :type 'boolean)
  937. (defcustom org-table-formula-constants nil
  938. "Alist with constant names and values, for use in table formulas.
  939. The car of each element is a name of a constant, without the `$' before it.
  940. The cdr is the value as a string. For example, if you'd like to use the
  941. speed of light in a formula, you would configure
  942. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  943. and then use it in an equation like `$1*$c'.
  944. Constants can also be defined on a per-file basis using a line like
  945. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  946. :group 'org-table-calculation
  947. :type '(repeat
  948. (cons (string :tag "name")
  949. (string :tag "value"))))
  950. (defvar org-table-formula-constants-local nil
  951. "Local version of `org-table-formula-constants'.")
  952. (make-variable-buffer-local 'org-table-formula-constants-local)
  953. (defcustom org-table-allow-automatic-line-recalculation t
  954. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  955. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  956. :group 'org-table-calculation
  957. :type 'boolean)
  958. (defgroup org-link nil
  959. "Options concerning links in Org-mode."
  960. :tag "Org Link"
  961. :group 'org)
  962. (defvar org-link-abbrev-alist-local nil
  963. "Buffer-local version of `org-link-abbrev-alist', which see.
  964. The value of this is taken from the #+LINK lines.")
  965. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  966. (defcustom org-link-abbrev-alist nil
  967. "Alist of link abbreviations.
  968. The car of each element is a string, to be replaced at the start of a link.
  969. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  970. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  971. [[linkkey:tag][description]]
  972. If REPLACE is a string, the tag will simply be appended to create the link.
  973. If the string contains \"%s\", the tag will be inserted there.
  974. REPLACE may also be a function that will be called with the tag as the
  975. only argument to create the link, which should be returned as a string.
  976. See the manual for examples."
  977. :group 'org-link
  978. :type 'alist)
  979. (defcustom org-descriptive-links t
  980. "Non-nil means, hide link part and only show description of bracket links.
  981. Bracket links are like [[link][descritpion]]. This variable sets the initial
  982. state in new org-mode buffers. The setting can then be toggled on a
  983. per-buffer basis from the Org->Hyperlinks menu."
  984. :group 'org-link
  985. :type 'boolean)
  986. (defcustom org-link-file-path-type 'adaptive
  987. "How the path name in file links should be stored.
  988. Valid values are:
  989. relative Relative to the current directory, i.e. the directory of the file
  990. into which the link is being inserted.
  991. absolute Absolute path, if possible with ~ for home directory.
  992. noabbrev Absolute path, no abbreviation of home directory.
  993. adaptive Use relative path for files in the current directory and sub-
  994. directories of it. For other files, use an absolute path."
  995. :group 'org-link
  996. :type '(choice
  997. (const relative)
  998. (const absolute)
  999. (const noabbrev)
  1000. (const adaptive)))
  1001. (defcustom org-activate-links '(bracket angle plain radio tag date)
  1002. "Types of links that should be activated in Org-mode files.
  1003. This is a list of symbols, each leading to the activation of a certain link
  1004. type. In principle, it does not hurt to turn on most link types - there may
  1005. be a small gain when turning off unused link types. The types are:
  1006. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1007. angular Links in angular brackes that may contain whitespace like
  1008. <bbdb:Carsten Dominik>.
  1009. plain Plain links in normal text, no whitespace, like http://google.com.
  1010. radio Text that is matched by a radio target, see manual for details.
  1011. tag Tag settings in a headline (link to tag search).
  1012. date Time stamps (link to calendar).
  1013. Changing this variable requires a restart of Emacs to become effective."
  1014. :group 'org-link
  1015. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1016. (const :tag "Angular bracket links (old style)" angular)
  1017. (const :tag "Plain text links" plain)
  1018. (const :tag "Radio target matches" radio)
  1019. (const :tag "Tags" tag)
  1020. (const :tag "Timestamps" date)))
  1021. (defgroup org-link-store nil
  1022. "Options concerning storing links in Org-mode"
  1023. :tag "Org Store Link"
  1024. :group 'org-link)
  1025. (defcustom org-email-link-description-format "Email %c: %.30s"
  1026. "Format of the description part of a link to an email or usenet message.
  1027. The following %-excapes will be replaced by corresponding information:
  1028. %F full \"From\" field
  1029. %f name, taken from \"From\" field, address if no name
  1030. %T full \"To\" field
  1031. %t first name in \"To\" field, address if no name
  1032. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1033. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1034. %s subject
  1035. %m message-id.
  1036. You may use normal field width specification between the % and the letter.
  1037. This is for example useful to limit the length of the subject.
  1038. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1039. :group 'org-link-store
  1040. :type 'string)
  1041. (defcustom org-from-is-user-regexp
  1042. (let (r1 r2)
  1043. (when (and user-mail-address (not (string= user-mail-address "")))
  1044. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1045. (when (and user-full-name (not (string= user-full-name "")))
  1046. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1047. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1048. "Regexp mached against the \"From:\" header of an email or usenet message.
  1049. It should match if the message is from the user him/herself."
  1050. :group 'org-link-store
  1051. :type 'regexp)
  1052. (defcustom org-context-in-file-links t
  1053. "Non-nil means, file links from `org-store-link' contain context.
  1054. A search string will be added to the file name with :: as separator and
  1055. used to find the context when the link is activated by the command
  1056. `org-open-at-point'.
  1057. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1058. negates this setting for the duration of the command."
  1059. :group 'org-link-store
  1060. :type 'boolean)
  1061. (defcustom org-keep-stored-link-after-insertion nil
  1062. "Non-nil means, keep link in list for entire session.
  1063. The command `org-store-link' adds a link pointing to the current
  1064. location to an internal list. These links accumulate during a session.
  1065. The command `org-insert-link' can be used to insert links into any
  1066. Org-mode file (offering completion for all stored links). When this
  1067. option is nil, every link which has been inserted once using \\[org-insert-link]
  1068. will be removed from the list, to make completing the unused links
  1069. more efficient."
  1070. :group 'org-link-store
  1071. :type 'boolean)
  1072. (defcustom org-usenet-links-prefer-google nil
  1073. "Non-nil means, `org-store-link' will create web links to Google groups.
  1074. When nil, Gnus will be used for such links.
  1075. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1076. negates this setting for the duration of the command."
  1077. :group 'org-link-store
  1078. :type 'boolean)
  1079. (defgroup org-link-follow nil
  1080. "Options concerning following links in Org-mode"
  1081. :tag "Org Follow Link"
  1082. :group 'org-link)
  1083. (defcustom org-tab-follows-link nil
  1084. "Non-nil means, on links TAB will follow the link.
  1085. Needs to be set before org.el is loaded."
  1086. :group 'org-link-follow
  1087. :type 'boolean)
  1088. (defcustom org-return-follows-link nil
  1089. "Non-nil means, on links RET will follow the link.
  1090. Needs to be set before org.el is loaded."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-mouse-1-follows-link
  1094. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1095. "Non-nil means, mouse-1 on a link will follow the link.
  1096. A longer mouse click will still set point. Does not work on XEmacs.
  1097. Needs to be set before org.el is loaded."
  1098. :group 'org-link-follow
  1099. :type 'boolean)
  1100. (defcustom org-mark-ring-length 4
  1101. "Number of different positions to be recorded in the ring
  1102. Changing this requires a restart of Emacs to work correctly."
  1103. :group 'org-link-follow
  1104. :type 'interger)
  1105. (defcustom org-link-frame-setup
  1106. '((vm . vm-visit-folder-other-frame)
  1107. (gnus . gnus-other-frame)
  1108. (file . find-file-other-window))
  1109. "Setup the frame configuration for following links.
  1110. When following a link with Emacs, it may often be useful to display
  1111. this link in another window or frame. This variable can be used to
  1112. set this up for the different types of links.
  1113. For VM, use any of
  1114. `vm-visit-folder'
  1115. `vm-visit-folder-other-frame'
  1116. For Gnus, use any of
  1117. `gnus'
  1118. `gnus-other-frame'
  1119. For FILE, use any of
  1120. `find-file'
  1121. `find-file-other-window'
  1122. `find-file-other-frame'
  1123. For the calendar, use the variable `calendar-setup'.
  1124. For BBDB, it is currently only possible to display the matches in
  1125. another window."
  1126. :group 'org-link-follow
  1127. :type '(list
  1128. (cons (const vm)
  1129. (choice
  1130. (const vm-visit-folder)
  1131. (const vm-visit-folder-other-window)
  1132. (const vm-visit-folder-other-frame)))
  1133. (cons (const gnus)
  1134. (choice
  1135. (const gnus)
  1136. (const gnus-other-frame)))
  1137. (cons (const file)
  1138. (choice
  1139. (const find-file)
  1140. (const find-file-other-window)
  1141. (const find-file-other-frame)))))
  1142. (defcustom org-display-internal-link-with-indirect-buffer nil
  1143. "Non-nil means, use indirect buffer to display infile links.
  1144. Activating internal links (from one location in a file to another location
  1145. in the same file) normally just jumps to the location. When the link is
  1146. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1147. another window. When this option is set, the other window actually displays
  1148. an indirect buffer clone of the current buffer, to avoid any visibility
  1149. changes to the current buffer."
  1150. :group 'org-link-follow
  1151. :type 'boolean)
  1152. (defcustom org-open-non-existing-files nil
  1153. "Non-nil means, `org-open-file' will open non-existing files.
  1154. When nil, an error will be generated."
  1155. :group 'org-link-follow
  1156. :type 'boolean)
  1157. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1158. "Function and arguments to call for following mailto links.
  1159. This is a list with the first element being a lisp function, and the
  1160. remaining elements being arguments to the function. In string arguments,
  1161. %a will be replaced by the address, and %s will be replaced by the subject
  1162. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1163. :group 'org-link-follow
  1164. :type '(choice
  1165. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1166. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1167. (const :tag "message-mail" (message-mail "%a" "%s"))
  1168. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1169. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1170. "Non-nil means, ask for confirmation before executing shell links.
  1171. Shell links can be dangerous: just think about a link
  1172. [[shell:rm -rf ~/*][Google Search]]
  1173. This link would show up in your Org-mode document as \"Google Search\",
  1174. but really it would remove your entire home directory.
  1175. Therefore we advise against setting this variable to nil.
  1176. Just change it to `y-or-n-p' of you want to confirm with a
  1177. single keystroke rather than having to type \"yes\"."
  1178. :group 'org-link-follow
  1179. :type '(choice
  1180. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1181. (const :tag "with y-or-n (faster)" y-or-n-p)
  1182. (const :tag "no confirmation (dangerous)" nil)))
  1183. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1184. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1185. Elisp links can be dangerous: just think about a link
  1186. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1187. This link would show up in your Org-mode document as \"Google Search\",
  1188. but really it would remove your entire home directory.
  1189. Therefore we advise against setting this variable to nil.
  1190. Just change it to `y-or-n-p' of you want to confirm with a
  1191. single keystroke rather than having to type \"yes\"."
  1192. :group 'org-link-follow
  1193. :type '(choice
  1194. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1195. (const :tag "with y-or-n (faster)" y-or-n-p)
  1196. (const :tag "no confirmation (dangerous)" nil)))
  1197. (defconst org-file-apps-defaults-gnu
  1198. '((remote . emacs)
  1199. (t . mailcap))
  1200. "Default file applications on a UNIX or GNU/Linux system.
  1201. See `org-file-apps'.")
  1202. (defconst org-file-apps-defaults-macosx
  1203. '((remote . emacs)
  1204. (t . "open %s")
  1205. ("ps" . "gv %s")
  1206. ("ps.gz" . "gv %s")
  1207. ("eps" . "gv %s")
  1208. ("eps.gz" . "gv %s")
  1209. ("dvi" . "xdvi %s")
  1210. ("fig" . "xfig %s"))
  1211. "Default file applications on a MacOS X system.
  1212. The system \"open\" is known as a default, but we use X11 applications
  1213. for some files for which the OS does not have a good default.
  1214. See `org-file-apps'.")
  1215. (defconst org-file-apps-defaults-windowsnt
  1216. (list
  1217. '(remote . emacs)
  1218. (cons t
  1219. (list (if (featurep 'xemacs)
  1220. 'mswindows-shell-execute
  1221. 'w32-shell-execute)
  1222. "open" 'file)))
  1223. "Default file applications on a Windows NT system.
  1224. The system \"open\" is used for most files.
  1225. See `org-file-apps'.")
  1226. (defcustom org-file-apps
  1227. '(
  1228. ("txt" . emacs)
  1229. ("tex" . emacs)
  1230. ("ltx" . emacs)
  1231. ("org" . emacs)
  1232. ("el" . emacs)
  1233. ("bib" . emacs)
  1234. )
  1235. "External applications for opening `file:path' items in a document.
  1236. Org-mode uses system defaults for different file types, but
  1237. you can use this variable to set the application for a given file
  1238. extension. The entries in this list are cons cells where the car identifies
  1239. files and the cdr the corresponding command. Possible values for the
  1240. file identifier are
  1241. \"ext\" A string identifying an extension
  1242. `directory' Matches a directory
  1243. `remote' Matches a remote file, accessible through tramp or efs.
  1244. Remote files most likely should be visited through Emacs
  1245. because external applications cannot handle such paths.
  1246. t Default for all remaining files
  1247. Possible values for the command are:
  1248. `emacs' The file will be visited by the current Emacs process.
  1249. `default' Use the default application for this file type.
  1250. string A command to be executed by a shell; %s will be replaced
  1251. by the path to the file.
  1252. sexp A Lisp form which will be evaluated. The file path will
  1253. be available in the Lisp variable `file'.
  1254. For more examples, see the system specific constants
  1255. `org-file-apps-defaults-macosx'
  1256. `org-file-apps-defaults-windowsnt'
  1257. `org-file-apps-defaults-gnu'."
  1258. :group 'org-link-follow
  1259. :type '(repeat
  1260. (cons (choice :value ""
  1261. (string :tag "Extension")
  1262. (const :tag "Default for unrecognized files" t)
  1263. (const :tag "Remote file" remote)
  1264. (const :tag "Links to a directory" directory))
  1265. (choice :value ""
  1266. (const :tag "Visit with Emacs" emacs)
  1267. (const :tag "Use system default" default)
  1268. (string :tag "Command")
  1269. (sexp :tag "Lisp form")))))
  1270. (defcustom org-mhe-search-all-folders nil
  1271. "Non-nil means, that the search for the mh-message will be extended to
  1272. all folders if the message cannot be found in the folder given in the link.
  1273. Searching all folders is very efficient with one of the search engines
  1274. supported by MH-E, but will be slow with pick."
  1275. :group 'org-link-follow
  1276. :type 'boolean)
  1277. (defgroup org-remember nil
  1278. "Options concerning interaction with remember.el."
  1279. :tag "Org Remember"
  1280. :group 'org)
  1281. (defcustom org-directory "~/org"
  1282. "Directory with org files.
  1283. This directory will be used as default to prompt for org files.
  1284. Used by the hooks for remember.el."
  1285. :group 'org-remember
  1286. :type 'directory)
  1287. (defcustom org-default-notes-file "~/.notes"
  1288. "Default target for storing notes.
  1289. Used by the hooks for remember.el. This can be a string, or nil to mean
  1290. the value of `remember-data-file'.
  1291. You can set this on a per-template basis with the variable
  1292. `org-remember-templates'."
  1293. :group 'org-remember
  1294. :type '(choice
  1295. (const :tag "Default from remember-data-file" nil)
  1296. file))
  1297. (defcustom org-remember-store-without-prompt t
  1298. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1299. In this case, you need `C-u C-c C-c' to get the prompts for
  1300. note file and headline.
  1301. When this variable is nil, `C-c C-c' give you the prompts, and
  1302. `C-u C-c C-c' trigger the fasttrack."
  1303. :group 'org-remember
  1304. :type 'boolean)
  1305. (defcustom org-remember-interactive-interface 'refile
  1306. "The interface to be used for interactive filing of remember notes.
  1307. This is only used when the interactive mode for selecting a filing
  1308. location is used (see the variable `org-remember-store-without-prompt').
  1309. Allowed vaues are:
  1310. outline The interface shows an outline of the relevant file
  1311. and the correct heading is found by moving through
  1312. the outline or by searching with incremental search.
  1313. outline-path-completion Headlines in the current buffer are offered via
  1314. completion.
  1315. refile Use the refile interface, and offer headlines,
  1316. possibly from different buffers."
  1317. :group 'org-remember
  1318. :type '(choice
  1319. (const :tag "Refile" refile)
  1320. (const :tag "Outline" outline)
  1321. (const :tag "Outline-path-completion" outline-path-completion)))
  1322. (defcustom org-goto-interface 'outline
  1323. "The default interface to be used for `org-goto'.
  1324. Allowed vaues are:
  1325. outline The interface shows an outline of the relevant file
  1326. and the correct heading is found by moving through
  1327. the outline or by searching with incremental search.
  1328. outline-path-completion Headlines in the current buffer are offered via
  1329. completion."
  1330. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1331. :type '(choice
  1332. (const :tag "Outline" outline)
  1333. (const :tag "Outline-path-completion" outline-path-completion)))
  1334. (defcustom org-remember-default-headline ""
  1335. "The headline that should be the default location in the notes file.
  1336. When filing remember notes, the cursor will start at that position.
  1337. You can set this on a per-template basis with the variable
  1338. `org-remember-templates'."
  1339. :group 'org-remember
  1340. :type 'string)
  1341. (defcustom org-remember-templates nil
  1342. "Templates for the creation of remember buffers.
  1343. When nil, just let remember make the buffer.
  1344. When not nil, this is a list of 5-element lists. In each entry, the first
  1345. element is the name of the template, which should be a single short word.
  1346. The second element is a character, a unique key to select this template.
  1347. The third element is the template. The fourth element is optional and can
  1348. specify a destination file for remember items created with this template.
  1349. The default file is given by `org-default-notes-file'. An optional fifth
  1350. element can specify the headline in that file that should be offered
  1351. first when the user is asked to file the entry. The default headline is
  1352. given in the variable `org-remember-default-headline'.
  1353. The template specifies the structure of the remember buffer. It should have
  1354. a first line starting with a star, to act as the org-mode headline.
  1355. Furthermore, the following %-escapes will be replaced with content:
  1356. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1357. A default value and a completion table ca be specified like this:
  1358. %^{prompt|default|completion2|completion3|...}
  1359. %t time stamp, date only
  1360. %T time stamp with date and time
  1361. %u, %U like the above, but inactive time stamps
  1362. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1363. You may define a prompt like %^{Please specify birthday}t
  1364. %n user name (taken from `user-full-name')
  1365. %a annotation, normally the link created with org-store-link
  1366. %i initial content, the region active. If %i is indented,
  1367. the entire inserted text will be indented as well.
  1368. %c content of the clipboard, or current kill ring head
  1369. %^g prompt for tags, with completion on tags in target file
  1370. %^G prompt for tags, with completion all tags in all agenda files
  1371. %:keyword specific information for certain link types, see below
  1372. %[pathname] insert the contents of the file given by `pathname'
  1373. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1374. %! Store this note immediately after filling the template
  1375. %? After completing the template, position cursor here.
  1376. Apart from these general escapes, you can access information specific to the
  1377. link type that is created. For example, calling `remember' in emails or gnus
  1378. will record the author and the subject of the message, which you can access
  1379. with %:author and %:subject, respectively. Here is a complete list of what
  1380. is recorded for each link type.
  1381. Link type | Available information
  1382. -------------------+------------------------------------------------------
  1383. bbdb | %:type %:name %:company
  1384. vm, wl, mh, rmail | %:type %:subject %:message-id
  1385. | %:from %:fromname %:fromaddress
  1386. | %:to %:toname %:toaddress
  1387. | %:fromto (either \"to NAME\" or \"from NAME\")
  1388. gnus | %:group, for messages also all email fields
  1389. w3, w3m | %:type %:url
  1390. info | %:type %:file %:node
  1391. calendar | %:type %:date"
  1392. :group 'org-remember
  1393. :get (lambda (var) ; Make sure all entries have 5 elements
  1394. (mapcar (lambda (x)
  1395. (if (not (stringp (car x))) (setq x (cons "" x)))
  1396. (cond ((= (length x) 4) (append x '("")))
  1397. ((= (length x) 3) (append x '("" "")))
  1398. (t x)))
  1399. (default-value var)))
  1400. :type '(repeat
  1401. :tag "enabled"
  1402. (list :value ("" ?a "\n" nil nil)
  1403. (string :tag "Name")
  1404. (character :tag "Selection Key")
  1405. (string :tag "Template")
  1406. (choice
  1407. (file :tag "Destination file")
  1408. (const :tag "Prompt for file" nil))
  1409. (choice
  1410. (string :tag "Destination headline")
  1411. (const :tag "Selection interface for heading")))))
  1412. (defcustom org-reverse-note-order nil
  1413. "Non-nil means, store new notes at the beginning of a file or entry.
  1414. When nil, new notes will be filed to the end of a file or entry.
  1415. This can also be a list with cons cells of regular expressions that
  1416. are matched against file names, and values."
  1417. :group 'org-remember
  1418. :type '(choice
  1419. (const :tag "Reverse always" t)
  1420. (const :tag "Reverse never" nil)
  1421. (repeat :tag "By file name regexp"
  1422. (cons regexp boolean))))
  1423. (defcustom org-refile-targets nil
  1424. "Targets for refiling entries with \\[org-refile].
  1425. This is list of cons cells. Each cell contains:
  1426. - a specification of the files to be considered, either a list of files,
  1427. or a symbol whose function or value fields will be used to retrieve
  1428. a file name or a list of file names. Nil means, refile to a different
  1429. heading in the current buffer.
  1430. - A specification of how to find candidate refile targets. This may be
  1431. any of
  1432. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1433. This tag has to be present in all target headlines, inheritance will
  1434. not be considered.
  1435. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1436. todo keyword.
  1437. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1438. headlines that are refiling targets.
  1439. - a cons cell (:level . N). Any headline of level N is considered a target.
  1440. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1441. ;; FIXME: what if there are a var and func with same name???
  1442. :group 'org-remember
  1443. :type '(repeat
  1444. (cons
  1445. (choice :value org-agenda-files
  1446. (const :tag "All agenda files" org-agenda-files)
  1447. (const :tag "Current buffer" nil)
  1448. (function) (variable) (file))
  1449. (choice :tag "Identify target headline by"
  1450. (cons :tag "Specific tag" (const :tag) (string))
  1451. (cons :tag "TODO keyword" (const :todo) (string))
  1452. (cons :tag "Regular expression" (const :regexp) (regexp))
  1453. (cons :tag "Level number" (const :level) (integer))
  1454. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1455. (defcustom org-refile-use-outline-path nil
  1456. "Non-nil means, provide refile targets as paths.
  1457. So a level 3 headline will be available as level1/level2/level3.
  1458. When the value is `file', also include the file name (without directory)
  1459. into the path. When `full-file-path', include the full file path."
  1460. :group 'org-remember
  1461. :type '(choice
  1462. (const :tag "Not" nil)
  1463. (const :tag "Yes" t)
  1464. (const :tag "Start with file name" file)
  1465. (const :tag "Start with full file path" full-file-path)))
  1466. (defgroup org-todo nil
  1467. "Options concerning TODO items in Org-mode."
  1468. :tag "Org TODO"
  1469. :group 'org)
  1470. (defgroup org-progress nil
  1471. "Options concerning Progress logging in Org-mode."
  1472. :tag "Org Progress"
  1473. :group 'org-time)
  1474. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1475. "List of TODO entry keyword sequences and their interpretation.
  1476. \\<org-mode-map>This is a list of sequences.
  1477. Each sequence starts with a symbol, either `sequence' or `type',
  1478. indicating if the keywords should be interpreted as a sequence of
  1479. action steps, or as different types of TODO items. The first
  1480. keywords are states requiring action - these states will select a headline
  1481. for inclusion into the global TODO list Org-mode produces. If one of
  1482. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1483. signify that no further action is necessary. If \"|\" is not found,
  1484. the last keyword is treated as the only DONE state of the sequence.
  1485. The command \\[org-todo] cycles an entry through these states, and one
  1486. additional state where no keyword is present. For details about this
  1487. cycling, see the manual.
  1488. TODO keywords and interpretation can also be set on a per-file basis with
  1489. the special #+SEQ_TODO and #+TYP_TODO lines.
  1490. Each keyword can optionally specify a character for fast state selection
  1491. \(in combination with the variable `org-use-fast-todo-selection')
  1492. and specifiers for state change logging, using the same syntax
  1493. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1494. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1495. indicates to record a time stamp each time this state is selected.
  1496. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1497. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1498. entering and when leaving this state.
  1499. For backward compatibility, this variable may also be just a list
  1500. of keywords - in this case the interptetation (sequence or type) will be
  1501. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1502. :group 'org-todo
  1503. :group 'org-keywords
  1504. :type '(choice
  1505. (repeat :tag "Old syntax, just keywords"
  1506. (string :tag "Keyword"))
  1507. (repeat :tag "New syntax"
  1508. (cons
  1509. (choice
  1510. :tag "Interpretation"
  1511. (const :tag "Sequence (cycling hits every state)" sequence)
  1512. (const :tag "Type (cycling directly to DONE)" type))
  1513. (repeat
  1514. (string :tag "Keyword"))))))
  1515. (defvar org-todo-keywords-1 nil
  1516. "All TODO and DONE keywords active in a buffer.")
  1517. (make-variable-buffer-local 'org-todo-keywords-1)
  1518. (defvar org-todo-keywords-for-agenda nil)
  1519. (defvar org-done-keywords-for-agenda nil)
  1520. (defvar org-not-done-keywords nil)
  1521. (make-variable-buffer-local 'org-not-done-keywords)
  1522. (defvar org-done-keywords nil)
  1523. (make-variable-buffer-local 'org-done-keywords)
  1524. (defvar org-todo-heads nil)
  1525. (make-variable-buffer-local 'org-todo-heads)
  1526. (defvar org-todo-sets nil)
  1527. (make-variable-buffer-local 'org-todo-sets)
  1528. (defvar org-todo-log-states nil)
  1529. (make-variable-buffer-local 'org-todo-log-states)
  1530. (defvar org-todo-kwd-alist nil)
  1531. (make-variable-buffer-local 'org-todo-kwd-alist)
  1532. (defvar org-todo-key-alist nil)
  1533. (make-variable-buffer-local 'org-todo-key-alist)
  1534. (defvar org-todo-key-trigger nil)
  1535. (make-variable-buffer-local 'org-todo-key-trigger)
  1536. (defcustom org-todo-interpretation 'sequence
  1537. "Controls how TODO keywords are interpreted.
  1538. This variable is in principle obsolete and is only used for
  1539. backward compatibility, if the interpretation of todo keywords is
  1540. not given already in `org-todo-keywords'. See that variable for
  1541. more information."
  1542. :group 'org-todo
  1543. :group 'org-keywords
  1544. :type '(choice (const sequence)
  1545. (const type)))
  1546. (defcustom org-use-fast-todo-selection 'prefix
  1547. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1548. This variable describes if and under what circumstances the cycling
  1549. mechanism for TODO keywords will be replaced by a single-key, direct
  1550. selection scheme.
  1551. When nil, fast selection is never used.
  1552. When the symbol `prefix', it will be used when `org-todo' is called with
  1553. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1554. in an agenda buffer.
  1555. When t, fast selection is used by default. In this case, the prefix
  1556. argument forces cycling instead.
  1557. In all cases, the special interface is only used if access keys have actually
  1558. been assigned by the user, i.e. if keywords in the configuration are followed
  1559. by a letter in parenthesis, like TODO(t)."
  1560. :group 'org-todo
  1561. :type '(choice
  1562. (const :tag "Never" nil)
  1563. (const :tag "By default" t)
  1564. (const :tag "Only with C-u C-c C-t" prefix)))
  1565. (defcustom org-after-todo-state-change-hook nil
  1566. "Hook which is run after the state of a TODO item was changed.
  1567. The new state (a string with a TODO keyword, or nil) is available in the
  1568. Lisp variable `state'."
  1569. :group 'org-todo
  1570. :type 'hook)
  1571. (defcustom org-log-done nil
  1572. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1573. When equal to the list (done), also prompt for a closing note.
  1574. This can also be configured on a per-file basis by adding one of
  1575. the following lines anywhere in the buffer:
  1576. #+STARTUP: logdone
  1577. #+STARTUP: lognotedone
  1578. #+STARTUP: nologdone"
  1579. :group 'org-todo
  1580. :group 'org-progress
  1581. :type '(choice
  1582. (const :tag "No logging" nil)
  1583. (const :tag "Record CLOSED timestamp" time)
  1584. (const :tag "Record CLOSED timestamp with closing note." note)))
  1585. ;; Normalize old uses of org-log-done.
  1586. (cond
  1587. ((eq org-log-done t) (setq org-log-done 'time))
  1588. ((and (listp org-log-done) (memq 'done org-log-done))
  1589. (setq org-log-done 'note)))
  1590. ;; FIXME: document
  1591. (defcustom org-log-note-clock-out nil
  1592. "Non-nil means, recored a note when clocking out of an item.
  1593. This can also be configured on a per-file basis by adding one of
  1594. the following lines anywhere in the buffer:
  1595. #+STARTUP: lognoteclock-out
  1596. #+STARTUP: nolognoteclock-out"
  1597. :group 'org-todo
  1598. :group 'org-progress
  1599. :type 'boolean)
  1600. (defcustom org-log-done-with-time t
  1601. "Non-nil means, the CLOSED time stamp will contain date and time.
  1602. When nil, only the date will be recorded."
  1603. :group 'org-progress
  1604. :type 'boolean)
  1605. (defcustom org-log-note-headings
  1606. '((done . "CLOSING NOTE %t")
  1607. (state . "State %-12s %t")
  1608. (clock-out . ""))
  1609. "Headings for notes added when clocking out or closing TODO items.
  1610. The value is an alist, with the car being a symbol indicating the note
  1611. context, and the cdr is the heading to be used. The heading may also be the
  1612. empty string.
  1613. %t in the heading will be replaced by a time stamp.
  1614. %s will be replaced by the new TODO state, in double quotes.
  1615. %u will be replaced by the user name.
  1616. %U will be replaced by the full user name."
  1617. :group 'org-todo
  1618. :group 'org-progress
  1619. :type '(list :greedy t
  1620. (cons (const :tag "Heading when closing an item" done) string)
  1621. (cons (const :tag
  1622. "Heading when changing todo state (todo sequence only)"
  1623. state) string)
  1624. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1625. (defcustom org-log-states-order-reversed t
  1626. "Non-nil means, the latest state change note will be directly after heading.
  1627. When nil, the notes will be orderer according to time."
  1628. :group 'org-todo
  1629. :group 'org-progress
  1630. :type 'boolean)
  1631. (defcustom org-log-repeat 'time
  1632. "Non-nil means, record moving through the DONE state when triggering repeat.
  1633. An auto-repeating tasks is immediately switched back to TODO when marked
  1634. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1635. the TODO keyword definition, or recording a cloing note by setting
  1636. `org-log-done', there will be no record of the task moving trhough DONE.
  1637. This variable forces taking a note anyway. Possible values are:
  1638. nil Don't force a record
  1639. time Record a time stamp
  1640. note Record a note
  1641. This option can also be set with on a per-file-basis with
  1642. #+STARTUP: logrepeat
  1643. #+STARTUP: lognoterepeat
  1644. #+STARTUP: nologrepeat
  1645. You can have local logging settings for a subtree by setting the LOGGING
  1646. property to one or more of these keywords."
  1647. :group 'org-todo
  1648. :group 'org-progress
  1649. :type '(choice
  1650. (const :tag "Don't force a record" nil)
  1651. (const :tag "Force recording the DONE state" time)
  1652. (const :tag "Force recording a note with the DONE state" note)))
  1653. (defcustom org-clock-into-drawer 2
  1654. "Should clocking info be wrapped into a drawer?
  1655. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1656. If necessary, the drawer will be created.
  1657. When nil, the drawer will not be created, but used when present.
  1658. When an integer and the number of clocking entries in an item
  1659. reaches or exceeds this number, a drawer will be created."
  1660. :group 'org-todo
  1661. :group 'org-progress
  1662. :type '(choice
  1663. (const :tag "Always" t)
  1664. (const :tag "Only when drawer exists" nil)
  1665. (integer :tag "When at least N clock entries")))
  1666. (defcustom org-clock-out-when-done t
  1667. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1668. Nil means, clock will keep running until stopped explicitly with
  1669. `C-c C-x C-o', or until the clock is started in a different item."
  1670. :group 'org-progress
  1671. :type 'boolean)
  1672. (defcustom org-clock-in-switch-to-state nil
  1673. "Set task to a special todo state while clocking it.
  1674. The value should be the state to which the entry should be switched."
  1675. :group 'org-progress
  1676. :group 'org-todo
  1677. :type '(choice
  1678. (const :tag "Don't force a state" nil)
  1679. (string :tag "State")))
  1680. (defgroup org-priorities nil
  1681. "Priorities in Org-mode."
  1682. :tag "Org Priorities"
  1683. :group 'org-todo)
  1684. (defcustom org-highest-priority ?A
  1685. "The highest priority of TODO items. A character like ?A, ?B etc.
  1686. Must have a smaller ASCII number than `org-lowest-priority'."
  1687. :group 'org-priorities
  1688. :type 'character)
  1689. (defcustom org-lowest-priority ?C
  1690. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1691. Must have a larger ASCII number than `org-highest-priority'."
  1692. :group 'org-priorities
  1693. :type 'character)
  1694. (defcustom org-default-priority ?B
  1695. "The default priority of TODO items.
  1696. This is the priority an item get if no explicit priority is given."
  1697. :group 'org-priorities
  1698. :type 'character)
  1699. (defcustom org-priority-start-cycle-with-default t
  1700. "Non-nil means, start with default priority when starting to cycle.
  1701. When this is nil, the first step in the cycle will be (depending on the
  1702. command used) one higher or lower that the default priority."
  1703. :group 'org-priorities
  1704. :type 'boolean)
  1705. (defgroup org-time nil
  1706. "Options concerning time stamps and deadlines in Org-mode."
  1707. :tag "Org Time"
  1708. :group 'org)
  1709. (defcustom org-insert-labeled-timestamps-at-point nil
  1710. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1711. When nil, these labeled time stamps are forces into the second line of an
  1712. entry, just after the headline. When scheduling from the global TODO list,
  1713. the time stamp will always be forced into the second line."
  1714. :group 'org-time
  1715. :type 'boolean)
  1716. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1717. "Formats for `format-time-string' which are used for time stamps.
  1718. It is not recommended to change this constant.")
  1719. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1720. "Number of minutes to round time stamps to.
  1721. These are two values, the first applies when first creating a time stamp.
  1722. The second applies when changing it with the commands `S-up' and `S-down'.
  1723. When changing the time stamp, this means that it will change in steps
  1724. of N minues, as given by the second value.
  1725. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1726. numbers should be factors of 60, so for example 5, 10, 15.
  1727. When this is larger than 1, you can still force an exact time-stamp by using
  1728. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1729. and by using a prefix arg to `S-up/down' to specify the exact number
  1730. of minutes to shift."
  1731. :group 'org-time
  1732. :get '(lambda (var) ; Make sure all entries have 5 elements
  1733. (if (integerp (default-value var))
  1734. (list (default-value var) 5)
  1735. (default-value var)))
  1736. :type '(list
  1737. (integer :tag "when inserting times")
  1738. (integer :tag "when modifying times")))
  1739. (defcustom org-display-custom-times nil
  1740. "Non-nil means, overlay custom formats over all time stamps.
  1741. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1742. To turn this on on a per-file basis, insert anywhere in the file:
  1743. #+STARTUP: customtime"
  1744. :group 'org-time
  1745. :set 'set-default
  1746. :type 'sexp)
  1747. (make-variable-buffer-local 'org-display-custom-times)
  1748. (defcustom org-time-stamp-custom-formats
  1749. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1750. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1751. These are overlayed over the default ISO format if the variable
  1752. `org-display-custom-times' is set. Time like %H:%M should be at the
  1753. end of the second format."
  1754. :group 'org-time
  1755. :type 'sexp)
  1756. (defun org-time-stamp-format (&optional long inactive)
  1757. "Get the right format for a time string."
  1758. (let ((f (if long (cdr org-time-stamp-formats)
  1759. (car org-time-stamp-formats))))
  1760. (if inactive
  1761. (concat "[" (substring f 1 -1) "]")
  1762. f)))
  1763. (defcustom org-read-date-prefer-future t
  1764. "Non-nil means, assume future for incomplete date input from user.
  1765. This affects the following situations:
  1766. 1. The user gives a day, but no month.
  1767. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1768. read this as the third of *next* month. However, if you enter \"17\",
  1769. it will be considered as *this* month.
  1770. 2. The user gives a month but not a year.
  1771. For example, if it is april and you enter \"feb 2\", this will be read
  1772. as feb 2, *next* year. \"May 5\", however, will be this year.
  1773. When this option is nil, the current month and year will always be used
  1774. as defaults."
  1775. :group 'org-time
  1776. :type 'boolean)
  1777. (defcustom org-read-date-display-live t
  1778. "Non-nil means, display current interpretation of date prompt live.
  1779. This display will be in an overlay, in the minibuffer."
  1780. :group 'org-time
  1781. :type 'boolean)
  1782. (defcustom org-read-date-popup-calendar t
  1783. "Non-nil means, pop up a calendar when prompting for a date.
  1784. In the calendar, the date can be selected with mouse-1. However, the
  1785. minibuffer will also be active, and you can simply enter the date as well.
  1786. When nil, only the minibuffer will be available."
  1787. :group 'org-time
  1788. :type 'boolean)
  1789. (if (fboundp 'defvaralias)
  1790. (defvaralias 'org-popup-calendar-for-date-prompt
  1791. 'org-read-date-popup-calendar))
  1792. (defcustom org-extend-today-until 0
  1793. "The hour when your day really ends.
  1794. This has influence for the following applications:
  1795. - When switching the agenda to \"today\". It it is still earlier than
  1796. the time given here, the day recognized as TODAY is actually yesterday.
  1797. - When a date is read from the user and it is still before the time given
  1798. here, the current date and time will be assumed to be yesterday, 23:59.
  1799. FIXME:
  1800. IMPORTANT: This is still a very experimental feature, it may disappear
  1801. again or it may be extended to mean more things."
  1802. :group 'org-time
  1803. :type 'number)
  1804. (defcustom org-edit-timestamp-down-means-later nil
  1805. "Non-nil means, S-down will increase the time in a time stamp.
  1806. When nil, S-up will increase."
  1807. :group 'org-time
  1808. :type 'boolean)
  1809. (defcustom org-calendar-follow-timestamp-change t
  1810. "Non-nil means, make the calendar window follow timestamp changes.
  1811. When a timestamp is modified and the calendar window is visible, it will be
  1812. moved to the new date."
  1813. :group 'org-time
  1814. :type 'boolean)
  1815. (defcustom org-clock-heading-function nil
  1816. "When non-nil, should be a function to create `org-clock-heading'.
  1817. This is the string shown in the mode line when a clock is running.
  1818. The function is called with point at the beginning of the headline."
  1819. :group 'org-time ; FIXME: Should we have a separate group????
  1820. :type 'function)
  1821. (defgroup org-tags nil
  1822. "Options concerning tags in Org-mode."
  1823. :tag "Org Tags"
  1824. :group 'org)
  1825. (defcustom org-tag-alist nil
  1826. "List of tags allowed in Org-mode files.
  1827. When this list is nil, Org-mode will base TAG input on what is already in the
  1828. buffer.
  1829. The value of this variable is an alist, the car of each entry must be a
  1830. keyword as a string, the cdr may be a character that is used to select
  1831. that tag through the fast-tag-selection interface.
  1832. See the manual for details."
  1833. :group 'org-tags
  1834. :type '(repeat
  1835. (choice
  1836. (cons (string :tag "Tag name")
  1837. (character :tag "Access char"))
  1838. (const :tag "Start radio group" (:startgroup))
  1839. (const :tag "End radio group" (:endgroup)))))
  1840. (defcustom org-use-fast-tag-selection 'auto
  1841. "Non-nil means, use fast tag selection scheme.
  1842. This is a special interface to select and deselect tags with single keys.
  1843. When nil, fast selection is never used.
  1844. When the symbol `auto', fast selection is used if and only if selection
  1845. characters for tags have been configured, either through the variable
  1846. `org-tag-alist' or through a #+TAGS line in the buffer.
  1847. When t, fast selection is always used and selection keys are assigned
  1848. automatically if necessary."
  1849. :group 'org-tags
  1850. :type '(choice
  1851. (const :tag "Always" t)
  1852. (const :tag "Never" nil)
  1853. (const :tag "When selection characters are configured" 'auto)))
  1854. (defcustom org-fast-tag-selection-single-key nil
  1855. "Non-nil means, fast tag selection exits after first change.
  1856. When nil, you have to press RET to exit it.
  1857. During fast tag selection, you can toggle this flag with `C-c'.
  1858. This variable can also have the value `expert'. In this case, the window
  1859. displaying the tags menu is not even shown, until you press C-c again."
  1860. :group 'org-tags
  1861. :type '(choice
  1862. (const :tag "No" nil)
  1863. (const :tag "Yes" t)
  1864. (const :tag "Expert" expert)))
  1865. (defvar org-fast-tag-selection-include-todo nil
  1866. "Non-nil means, fast tags selection interface will also offer TODO states.
  1867. This is an undocumented feature, you should not rely on it.")
  1868. (defcustom org-tags-column -80
  1869. "The column to which tags should be indented in a headline.
  1870. If this number is positive, it specifies the column. If it is negative,
  1871. it means that the tags should be flushright to that column. For example,
  1872. -80 works well for a normal 80 character screen."
  1873. :group 'org-tags
  1874. :type 'integer)
  1875. (defcustom org-auto-align-tags t
  1876. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1877. These operations change the length of a headline and therefore shift
  1878. the tags around. With this options turned on, after each such operation
  1879. the tags are again aligned to `org-tags-column'."
  1880. :group 'org-tags
  1881. :type 'boolean)
  1882. (defcustom org-use-tag-inheritance t
  1883. "Non-nil means, tags in levels apply also for sublevels.
  1884. When nil, only the tags directly given in a specific line apply there.
  1885. If you turn off this option, you very likely want to turn on the
  1886. companion option `org-tags-match-list-sublevels'."
  1887. :group 'org-tags
  1888. :type 'boolean)
  1889. (defcustom org-tags-match-list-sublevels nil
  1890. "Non-nil means list also sublevels of headlines matching tag search.
  1891. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1892. the sublevels of a headline matching a tag search often also match
  1893. the same search. Listing all of them can create very long lists.
  1894. Setting this variable to nil causes subtrees of a match to be skipped.
  1895. This option is off by default, because inheritance in on. If you turn
  1896. inheritance off, you very likely want to turn this option on.
  1897. As a special case, if the tag search is restricted to TODO items, the
  1898. value of this variable is ignored and sublevels are always checked, to
  1899. make sure all corresponding TODO items find their way into the list."
  1900. :group 'org-tags
  1901. :type 'boolean)
  1902. (defvar org-tags-history nil
  1903. "History of minibuffer reads for tags.")
  1904. (defvar org-last-tags-completion-table nil
  1905. "The last used completion table for tags.")
  1906. (defvar org-after-tags-change-hook nil
  1907. "Hook that is run after the tags in a line have changed.")
  1908. (defgroup org-properties nil
  1909. "Options concerning properties in Org-mode."
  1910. :tag "Org Properties"
  1911. :group 'org)
  1912. (defcustom org-property-format "%-10s %s"
  1913. "How property key/value pairs should be formatted by `indent-line'.
  1914. When `indent-line' hits a property definition, it will format the line
  1915. according to this format, mainly to make sure that the values are
  1916. lined-up with respect to each other."
  1917. :group 'org-properties
  1918. :type 'string)
  1919. (defcustom org-use-property-inheritance nil
  1920. "Non-nil means, properties apply also for sublevels.
  1921. This setting is only relevant during property searches, not when querying
  1922. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1923. you need to call `org-entry-get' with the inheritance flag.
  1924. Turning this on can cause significant overhead when doing a search, so
  1925. this is turned off by default.
  1926. When nil, only the properties directly given in the current entry count.
  1927. The value may also be a list of properties that shouldhave inheritance.
  1928. However, note that some special properties use inheritance under special
  1929. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1930. and the properties ending in \"_ALL\" when they are used as descriptor
  1931. for valid values of a property."
  1932. :group 'org-properties
  1933. :type '(choice
  1934. (const :tag "Not" nil)
  1935. (const :tag "Always" nil)
  1936. (repeat :tag "Specific properties" (string :tag "Property"))))
  1937. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1938. "The default column format, if no other format has been defined.
  1939. This variable can be set on the per-file basis by inserting a line
  1940. #+COLUMNS: %25ITEM ....."
  1941. :group 'org-properties
  1942. :type 'string)
  1943. (defcustom org-global-properties nil
  1944. "List of property/value pairs that can be inherited by any entry.
  1945. You can set buffer-local values for this by adding lines like
  1946. #+PROPERTY: NAME VALUE"
  1947. :group 'org-properties
  1948. :type '(repeat
  1949. (cons (string :tag "Property")
  1950. (string :tag "Value"))))
  1951. (defvar org-local-properties nil
  1952. "List of property/value pairs that can be inherited by any entry.
  1953. Valid for the current buffer.
  1954. This variable is populated from #+PROPERTY lines.")
  1955. (defgroup org-agenda nil
  1956. "Options concerning agenda views in Org-mode."
  1957. :tag "Org Agenda"
  1958. :group 'org)
  1959. (defvar org-category nil
  1960. "Variable used by org files to set a category for agenda display.
  1961. Such files should use a file variable to set it, for example
  1962. # -*- mode: org; org-category: \"ELisp\"
  1963. or contain a special line
  1964. #+CATEGORY: ELisp
  1965. If the file does not specify a category, then file's base name
  1966. is used instead.")
  1967. (make-variable-buffer-local 'org-category)
  1968. (defcustom org-agenda-files nil
  1969. "The files to be used for agenda display.
  1970. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1971. \\[org-remove-file]. You can also use customize to edit the list.
  1972. If an entry is a directory, all files in that directory that are matched by
  1973. `org-agenda-file-regexp' will be part of the file list.
  1974. If the value of the variable is not a list but a single file name, then
  1975. the list of agenda files is actually stored and maintained in that file, one
  1976. agenda file per line."
  1977. :group 'org-agenda
  1978. :type '(choice
  1979. (repeat :tag "List of files and directories" file)
  1980. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1981. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1982. "Regular expression to match files for `org-agenda-files'.
  1983. If any element in the list in that variable contains a directory instead
  1984. of a normal file, all files in that directory that are matched by this
  1985. regular expression will be included."
  1986. :group 'org-agenda
  1987. :type 'regexp)
  1988. (defcustom org-agenda-skip-unavailable-files nil
  1989. "t means to just skip non-reachable files in `org-agenda-files'.
  1990. Nil means to remove them, after a query, from the list."
  1991. :group 'org-agenda
  1992. :type 'boolean)
  1993. (defcustom org-agenda-text-search-extra-files nil
  1994. "List of extra files to be searched by text search commands.
  1995. These files will be search in addition to the agenda files bu the
  1996. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1997. Note that these files will only be searched for text search commands,
  1998. not for the other agenda views like todo lists, tag earches or the weekly
  1999. agenda. This variable is intended to list notes and possibly archive files
  2000. that should also be searched by these two commands."
  2001. :group 'org-agenda
  2002. :type '(repeat file))
  2003. (if (fboundp 'defvaralias)
  2004. (defvaralias 'org-agenda-multi-occur-extra-files
  2005. 'org-agenda-text-search-extra-files))
  2006. (defcustom org-agenda-confirm-kill 1
  2007. "When set, remote killing from the agenda buffer needs confirmation.
  2008. When t, a confirmation is always needed. When a number N, confirmation is
  2009. only needed when the text to be killed contains more than N non-white lines."
  2010. :group 'org-agenda
  2011. :type '(choice
  2012. (const :tag "Never" nil)
  2013. (const :tag "Always" t)
  2014. (number :tag "When more than N lines")))
  2015. (defcustom org-calendar-to-agenda-key [?c]
  2016. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2017. The command `org-calendar-goto-agenda' will be bound to this key. The
  2018. default is the character `c' because then `c' can be used to switch back and
  2019. forth between agenda and calendar."
  2020. :group 'org-agenda
  2021. :type 'sexp)
  2022. (defcustom org-agenda-compact-blocks nil
  2023. "Non-nil means, make the block agenda more compact.
  2024. This is done by leaving out unnecessary lines."
  2025. :group 'org-agenda
  2026. :type nil)
  2027. (defgroup org-agenda-export nil
  2028. "Options concerning exporting agenda views in Org-mode."
  2029. :tag "Org Agenda Export"
  2030. :group 'org-agenda)
  2031. (defcustom org-agenda-with-colors t
  2032. "Non-nil means, use colors in agenda views."
  2033. :group 'org-agenda-export
  2034. :type 'boolean)
  2035. (defcustom org-agenda-exporter-settings nil
  2036. "Alist of variable/value pairs that should be active during agenda export.
  2037. This is a good place to set uptions for ps-print and for htmlize."
  2038. :group 'org-agenda-export
  2039. :type '(repeat
  2040. (list
  2041. (variable)
  2042. (sexp :tag "Value"))))
  2043. (defcustom org-agenda-export-html-style ""
  2044. "The style specification for exported HTML Agenda files.
  2045. If this variable contains a string, it will replace the default <style>
  2046. section as produced by `htmlize'.
  2047. Since there are different ways of setting style information, this variable
  2048. needs to contain the full HTML structure to provide a style, including the
  2049. surrounding HTML tags. The style specifications should include definitions
  2050. the fonts used by the agenda, here is an example:
  2051. <style type=\"text/css\">
  2052. p { font-weight: normal; color: gray; }
  2053. .org-agenda-structure {
  2054. font-size: 110%;
  2055. color: #003399;
  2056. font-weight: 600;
  2057. }
  2058. .org-todo {
  2059. color: #cc6666;Week-agenda:
  2060. font-weight: bold;
  2061. }
  2062. .org-done {
  2063. color: #339933;
  2064. }
  2065. .title { text-align: center; }
  2066. .todo, .deadline { color: red; }
  2067. .done { color: green; }
  2068. </style>
  2069. or, if you want to keep the style in a file,
  2070. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2071. As the value of this option simply gets inserted into the HTML <head> header,
  2072. you can \"misuse\" it to also add other text to the header. However,
  2073. <style>...</style> is required, if not present the variable will be ignored."
  2074. :group 'org-agenda-export
  2075. :group 'org-export-html
  2076. :type 'string)
  2077. (defgroup org-agenda-custom-commands nil
  2078. "Options concerning agenda views in Org-mode."
  2079. :tag "Org Agenda Custom Commands"
  2080. :group 'org-agenda)
  2081. (defcustom org-agenda-custom-commands nil
  2082. "Custom commands for the agenda.
  2083. These commands will be offered on the splash screen displayed by the
  2084. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2085. (key desc type match options files)
  2086. key The key (one or more characters as a string) to be associated
  2087. with the command.
  2088. desc A description of the commend, when omitted or nil, a default
  2089. description is built using MATCH.
  2090. type The command type, any of the following symbols:
  2091. agenda The daily/weekly agenda.
  2092. todo Entries with a specific TODO keyword, in all agenda files.
  2093. search Entries containing search words entry or headline.
  2094. tags Tags/Property/TODO match in all agenda files.
  2095. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2096. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2097. tags-tree Sparse tree with all tags matches in *current* file.
  2098. occur-tree Occur sparse tree for *current* file.
  2099. ... A user-defined function.
  2100. match What to search for:
  2101. - a single keyword for TODO keyword searches
  2102. - a tags match expression for tags searches
  2103. - a regular expression for occur searches
  2104. options A list of option settings, similar to that in a let form, so like
  2105. this: ((opt1 val1) (opt2 val2) ...)
  2106. files A list of files file to write the produced agenda buffer to
  2107. with the command `org-store-agenda-views'.
  2108. If a file name ends in \".html\", an HTML version of the buffer
  2109. is written out. If it ends in \".ps\", a postscript version is
  2110. produced. Otherwide, only the plain text is written to the file.
  2111. You can also define a set of commands, to create a composite agenda buffer.
  2112. In this case, an entry looks like this:
  2113. (key desc (cmd1 cmd2 ...) general-options file)
  2114. where
  2115. desc A description string to be displayed in the dispatcher menu.
  2116. cmd An agenda command, similar to the above. However, tree commands
  2117. are no allowed, but instead you can get agenda and global todo list.
  2118. So valid commands for a set are:
  2119. (agenda)
  2120. (alltodo)
  2121. (stuck)
  2122. (todo \"match\" options files)
  2123. (search \"match\" options files)
  2124. (tags \"match\" options files)
  2125. (tags-todo \"match\" options files)
  2126. Each command can carry a list of options, and another set of options can be
  2127. given for the whole set of commands. Individual command options take
  2128. precedence over the general options.
  2129. When using several characters as key to a command, the first characters
  2130. are prefix commands. For the dispatcher to display useful information, you
  2131. should provide a description for the prefix, like
  2132. (setq org-agenda-custom-commands
  2133. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2134. (\"hl\" tags \"+HOME+Lisa\")
  2135. (\"hp\" tags \"+HOME+Peter\")
  2136. (\"hk\" tags \"+HOME+Kim\")))"
  2137. :group 'org-agenda-custom-commands
  2138. :type '(repeat
  2139. (choice :value ("a" "" tags "" nil)
  2140. (list :tag "Single command"
  2141. (string :tag "Access Key(s) ")
  2142. (option (string :tag "Description"))
  2143. (choice
  2144. (const :tag "Agenda" agenda)
  2145. (const :tag "TODO list" alltodo)
  2146. (const :tag "Search words" search)
  2147. (const :tag "Stuck projects" stuck)
  2148. (const :tag "Tags search (all agenda files)" tags)
  2149. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2150. (const :tag "TODO keyword search (all agenda files)" todo)
  2151. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2152. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2153. (const :tag "Occur tree (current buffer)" occur-tree)
  2154. (sexp :tag "Other, user-defined function"))
  2155. (string :tag "Match")
  2156. (repeat :tag "Local options"
  2157. (list (variable :tag "Option") (sexp :tag "Value")))
  2158. (option (repeat :tag "Export" (file :tag "Export to"))))
  2159. (list :tag "Command series, all agenda files"
  2160. (string :tag "Access Key(s)")
  2161. (string :tag "Description ")
  2162. (repeat
  2163. (choice
  2164. (const :tag "Agenda" (agenda))
  2165. (const :tag "TODO list" (alltodo))
  2166. (list :tag "Search words"
  2167. (const :format "" search)
  2168. (string :tag "Match")
  2169. (repeat :tag "Local options"
  2170. (list (variable :tag "Option")
  2171. (sexp :tag "Value"))))
  2172. (const :tag "Stuck projects" (stuck))
  2173. (list :tag "Tags search"
  2174. (const :format "" tags)
  2175. (string :tag "Match")
  2176. (repeat :tag "Local options"
  2177. (list (variable :tag "Option")
  2178. (sexp :tag "Value"))))
  2179. (list :tag "Tags search, TODO entries only"
  2180. (const :format "" tags-todo)
  2181. (string :tag "Match")
  2182. (repeat :tag "Local options"
  2183. (list (variable :tag "Option")
  2184. (sexp :tag "Value"))))
  2185. (list :tag "TODO keyword search"
  2186. (const :format "" todo)
  2187. (string :tag "Match")
  2188. (repeat :tag "Local options"
  2189. (list (variable :tag "Option")
  2190. (sexp :tag "Value"))))
  2191. (list :tag "Other, user-defined function"
  2192. (symbol :tag "function")
  2193. (string :tag "Match")
  2194. (repeat :tag "Local options"
  2195. (list (variable :tag "Option")
  2196. (sexp :tag "Value"))))))
  2197. (repeat :tag "General options"
  2198. (list (variable :tag "Option")
  2199. (sexp :tag "Value")))
  2200. (option (repeat :tag "Export" (file :tag "Export to"))))
  2201. (cons :tag "Prefix key documentation"
  2202. (string :tag "Access Key(s)")
  2203. (string :tag "Description ")))))
  2204. (defcustom org-agenda-query-register ?o
  2205. "The register holding the current query string.
  2206. The prupose of this is that if you construct a query string interactively,
  2207. you can then use it to define a custom command."
  2208. :group 'org-agenda-custom-commands
  2209. :type 'character)
  2210. (defcustom org-stuck-projects
  2211. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2212. "How to identify stuck projects.
  2213. This is a list of four items:
  2214. 1. A tags/todo matcher string that is used to identify a project.
  2215. The entire tree below a headline matched by this is considered one project.
  2216. 2. A list of TODO keywords identifying non-stuck projects.
  2217. If the project subtree contains any headline with one of these todo
  2218. keywords, the project is considered to be not stuck. If you specify
  2219. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2220. 3. A list of tags identifying non-stuck projects.
  2221. If the project subtree contains any headline with one of these tags,
  2222. the project is considered to be not stuck. If you specify \"*\" as
  2223. a tag, any tag will mark the project unstuck.
  2224. 4. An arbitrary regular expression matching non-stuck projects.
  2225. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2226. or `C-c a #' to produce the list."
  2227. :group 'org-agenda-custom-commands
  2228. :type '(list
  2229. (string :tag "Tags/TODO match to identify a project")
  2230. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2231. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2232. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2233. (defgroup org-agenda-skip nil
  2234. "Options concerning skipping parts of agenda files."
  2235. :tag "Org Agenda Skip"
  2236. :group 'org-agenda)
  2237. (defcustom org-agenda-todo-list-sublevels t
  2238. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2239. When nil, the sublevels of a TODO entry are not checked, resulting in
  2240. potentially much shorter TODO lists."
  2241. :group 'org-agenda-skip
  2242. :group 'org-todo
  2243. :type 'boolean)
  2244. (defcustom org-agenda-todo-ignore-with-date nil
  2245. "Non-nil means, don't show entries with a date in the global todo list.
  2246. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2247. but don't want them to show up in the TODO list.
  2248. When this is set, it also covers deadlines and scheduled items, the settings
  2249. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2250. will be ignored."
  2251. :group 'org-agenda-skip
  2252. :group 'org-todo
  2253. :type 'boolean)
  2254. (defcustom org-agenda-todo-ignore-scheduled nil
  2255. "Non-nil means, don't show scheduled entries in the global todo list.
  2256. The idea behind this is that by scheduling it, you have already taken care
  2257. of this item.
  2258. See also `org-agenda-todo-ignore-with-date'."
  2259. :group 'org-agenda-skip
  2260. :group 'org-todo
  2261. :type 'boolean)
  2262. (defcustom org-agenda-todo-ignore-deadlines nil
  2263. "Non-nil means, don't show near deadline entries in the global todo list.
  2264. Near means closer than `org-deadline-warning-days' days.
  2265. The idea behind this is that such items will appear in the agenda anyway.
  2266. See also `org-agenda-todo-ignore-with-date'."
  2267. :group 'org-agenda-skip
  2268. :group 'org-todo
  2269. :type 'boolean)
  2270. (defcustom org-agenda-skip-scheduled-if-done nil
  2271. "Non-nil means don't show scheduled items in agenda when they are done.
  2272. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2273. it applies only to the actual date of the scheduling. Warnings about
  2274. an item with a past scheduling dates are always turned off when the item
  2275. is DONE."
  2276. :group 'org-agenda-skip
  2277. :type 'boolean)
  2278. (defcustom org-agenda-skip-deadline-if-done nil
  2279. "Non-nil means don't show deadines when the corresponding item is done.
  2280. When nil, the deadline is still shown and should give you a happy feeling.
  2281. This is relevant for the daily/weekly agenda. And it applied only to the
  2282. actualy date of the deadline. Warnings about approching and past-due
  2283. deadlines are always turned off when the item is DONE."
  2284. :group 'org-agenda-skip
  2285. :type 'boolean)
  2286. (defcustom org-agenda-skip-timestamp-if-done nil
  2287. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2288. :group 'org-agenda-skip
  2289. :type 'boolean)
  2290. (defcustom org-timeline-show-empty-dates 3
  2291. "Non-nil means, `org-timeline' also shows dates without an entry.
  2292. When nil, only the days which actually have entries are shown.
  2293. When t, all days between the first and the last date are shown.
  2294. When an integer, show also empty dates, but if there is a gap of more than
  2295. N days, just insert a special line indicating the size of the gap."
  2296. :group 'org-agenda-skip
  2297. :type '(choice
  2298. (const :tag "None" nil)
  2299. (const :tag "All" t)
  2300. (number :tag "at most")))
  2301. (defgroup org-agenda-startup nil
  2302. "Options concerning initial settings in the Agenda in Org Mode."
  2303. :tag "Org Agenda Startup"
  2304. :group 'org-agenda)
  2305. (defcustom org-finalize-agenda-hook nil
  2306. "Hook run just before displaying an agenda buffer."
  2307. :group 'org-agenda-startup
  2308. :type 'hook)
  2309. (defcustom org-agenda-mouse-1-follows-link nil
  2310. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2311. A longer mouse click will still set point. Does not work on XEmacs.
  2312. Needs to be set before org.el is loaded."
  2313. :group 'org-agenda-startup
  2314. :type 'boolean)
  2315. (defcustom org-agenda-start-with-follow-mode nil
  2316. "The initial value of follow-mode in a newly created agenda window."
  2317. :group 'org-agenda-startup
  2318. :type 'boolean)
  2319. (defgroup org-agenda-windows nil
  2320. "Options concerning the windows used by the Agenda in Org Mode."
  2321. :tag "Org Agenda Windows"
  2322. :group 'org-agenda)
  2323. (defcustom org-agenda-window-setup 'reorganize-frame
  2324. "How the agenda buffer should be displayed.
  2325. Possible values for this option are:
  2326. current-window Show agenda in the current window, keeping all other windows.
  2327. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2328. other-window Use `switch-to-buffer-other-window' to display agenda.
  2329. reorganize-frame Show only two windows on the current frame, the current
  2330. window and the agenda.
  2331. See also the variable `org-agenda-restore-windows-after-quit'."
  2332. :group 'org-agenda-windows
  2333. :type '(choice
  2334. (const current-window)
  2335. (const other-frame)
  2336. (const other-window)
  2337. (const reorganize-frame)))
  2338. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2339. "The min and max height of the agenda window as a fraction of frame height.
  2340. The value of the variable is a cons cell with two numbers between 0 and 1.
  2341. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2342. :group 'org-agenda-windows
  2343. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2344. (defcustom org-agenda-restore-windows-after-quit nil
  2345. "Non-nil means, restore window configuration open exiting agenda.
  2346. Before the window configuration is changed for displaying the agenda,
  2347. the current status is recorded. When the agenda is exited with
  2348. `q' or `x' and this option is set, the old state is restored. If
  2349. `org-agenda-window-setup' is `other-frame', the value of this
  2350. option will be ignored.."
  2351. :group 'org-agenda-windows
  2352. :type 'boolean)
  2353. (defcustom org-indirect-buffer-display 'other-window
  2354. "How should indirect tree buffers be displayed?
  2355. This applies to indirect buffers created with the commands
  2356. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2357. Valid values are:
  2358. current-window Display in the current window
  2359. other-window Just display in another window.
  2360. dedicated-frame Create one new frame, and re-use it each time.
  2361. new-frame Make a new frame each time. Note that in this case
  2362. previously-made indirect buffers are kept, and you need to
  2363. kill these buffers yourself."
  2364. :group 'org-structure
  2365. :group 'org-agenda-windows
  2366. :type '(choice
  2367. (const :tag "In current window" current-window)
  2368. (const :tag "In current frame, other window" other-window)
  2369. (const :tag "Each time a new frame" new-frame)
  2370. (const :tag "One dedicated frame" dedicated-frame)))
  2371. (defgroup org-agenda-daily/weekly nil
  2372. "Options concerning the daily/weekly agenda."
  2373. :tag "Org Agenda Daily/Weekly"
  2374. :group 'org-agenda)
  2375. (defcustom org-agenda-ndays 7
  2376. "Number of days to include in overview display.
  2377. Should be 1 or 7."
  2378. :group 'org-agenda-daily/weekly
  2379. :type 'number)
  2380. (defcustom org-agenda-start-on-weekday 1
  2381. "Non-nil means, start the overview always on the specified weekday.
  2382. 0 denotes Sunday, 1 denotes Monday etc.
  2383. When nil, always start on the current day."
  2384. :group 'org-agenda-daily/weekly
  2385. :type '(choice (const :tag "Today" nil)
  2386. (number :tag "Weekday No.")))
  2387. (defcustom org-agenda-show-all-dates t
  2388. "Non-nil means, `org-agenda' shows every day in the selected range.
  2389. When nil, only the days which actually have entries are shown."
  2390. :group 'org-agenda-daily/weekly
  2391. :type 'boolean)
  2392. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2393. "Format string for displaying dates in the agenda.
  2394. Used by the daily/weekly agenda and by the timeline. This should be
  2395. a format string understood by `format-time-string', or a function returning
  2396. the formatted date as a string. The function must take a single argument,
  2397. a calendar-style date list like (month day year)."
  2398. :group 'org-agenda-daily/weekly
  2399. :type '(choice
  2400. (string :tag "Format string")
  2401. (function :tag "Function")))
  2402. (defun org-agenda-format-date-aligned (date)
  2403. "Format a date string for display in the daily/weekly agenda, or timeline.
  2404. This function makes sure that dates are aligned for easy reading."
  2405. (format "%-9s %2d %s %4d"
  2406. (calendar-day-name date)
  2407. (extract-calendar-day date)
  2408. (calendar-month-name (extract-calendar-month date))
  2409. (extract-calendar-year date)))
  2410. (defcustom org-agenda-include-diary nil
  2411. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2412. :group 'org-agenda-daily/weekly
  2413. :type 'boolean)
  2414. (defcustom org-agenda-include-all-todo nil
  2415. "Set means weekly/daily agenda will always contain all TODO entries.
  2416. The TODO entries will be listed at the top of the agenda, before
  2417. the entries for specific days."
  2418. :group 'org-agenda-daily/weekly
  2419. :type 'boolean)
  2420. (defcustom org-agenda-repeating-timestamp-show-all t
  2421. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2422. When nil, only one occurence is shown, either today or the
  2423. nearest into the future."
  2424. :group 'org-agenda-daily/weekly
  2425. :type 'boolean)
  2426. (defcustom org-deadline-warning-days 14
  2427. "No. of days before expiration during which a deadline becomes active.
  2428. This variable governs the display in sparse trees and in the agenda.
  2429. When 0 or negative, it means use this number (the absolute value of it)
  2430. even if a deadline has a different individual lead time specified."
  2431. :group 'org-time
  2432. :group 'org-agenda-daily/weekly
  2433. :type 'number)
  2434. (defcustom org-scheduled-past-days 10000
  2435. "No. of days to continue listing scheduled items that are not marked DONE.
  2436. When an item is scheduled on a date, it shows up in the agenda on this
  2437. day and will be listed until it is marked done for the number of days
  2438. given here."
  2439. :group 'org-agenda-daily/weekly
  2440. :type 'number)
  2441. (defgroup org-agenda-time-grid nil
  2442. "Options concerning the time grid in the Org-mode Agenda."
  2443. :tag "Org Agenda Time Grid"
  2444. :group 'org-agenda)
  2445. (defcustom org-agenda-use-time-grid t
  2446. "Non-nil means, show a time grid in the agenda schedule.
  2447. A time grid is a set of lines for specific times (like every two hours between
  2448. 8:00 and 20:00). The items scheduled for a day at specific times are
  2449. sorted in between these lines.
  2450. For details about when the grid will be shown, and what it will look like, see
  2451. the variable `org-agenda-time-grid'."
  2452. :group 'org-agenda-time-grid
  2453. :type 'boolean)
  2454. (defcustom org-agenda-time-grid
  2455. '((daily today require-timed)
  2456. "----------------"
  2457. (800 1000 1200 1400 1600 1800 2000))
  2458. "The settings for time grid for agenda display.
  2459. This is a list of three items. The first item is again a list. It contains
  2460. symbols specifying conditions when the grid should be displayed:
  2461. daily if the agenda shows a single day
  2462. weekly if the agenda shows an entire week
  2463. today show grid on current date, independent of daily/weekly display
  2464. require-timed show grid only if at least one item has a time specification
  2465. The second item is a string which will be places behing the grid time.
  2466. The third item is a list of integers, indicating the times that should have
  2467. a grid line."
  2468. :group 'org-agenda-time-grid
  2469. :type
  2470. '(list
  2471. (set :greedy t :tag "Grid Display Options"
  2472. (const :tag "Show grid in single day agenda display" daily)
  2473. (const :tag "Show grid in weekly agenda display" weekly)
  2474. (const :tag "Always show grid for today" today)
  2475. (const :tag "Show grid only if any timed entries are present"
  2476. require-timed)
  2477. (const :tag "Skip grid times already present in an entry"
  2478. remove-match))
  2479. (string :tag "Grid String")
  2480. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2481. (defgroup org-agenda-sorting nil
  2482. "Options concerning sorting in the Org-mode Agenda."
  2483. :tag "Org Agenda Sorting"
  2484. :group 'org-agenda)
  2485. (defconst org-sorting-choice
  2486. '(choice
  2487. (const time-up) (const time-down)
  2488. (const category-keep) (const category-up) (const category-down)
  2489. (const tag-down) (const tag-up)
  2490. (const priority-up) (const priority-down))
  2491. "Sorting choices.")
  2492. (defcustom org-agenda-sorting-strategy
  2493. '((agenda time-up category-keep priority-down)
  2494. (todo category-keep priority-down)
  2495. (tags category-keep priority-down)
  2496. (search category-keep))
  2497. "Sorting structure for the agenda items of a single day.
  2498. This is a list of symbols which will be used in sequence to determine
  2499. if an entry should be listed before another entry. The following
  2500. symbols are recognized:
  2501. time-up Put entries with time-of-day indications first, early first
  2502. time-down Put entries with time-of-day indications first, late first
  2503. category-keep Keep the default order of categories, corresponding to the
  2504. sequence in `org-agenda-files'.
  2505. category-up Sort alphabetically by category, A-Z.
  2506. category-down Sort alphabetically by category, Z-A.
  2507. tag-up Sort alphabetically by last tag, A-Z.
  2508. tag-down Sort alphabetically by last tag, Z-A.
  2509. priority-up Sort numerically by priority, high priority last.
  2510. priority-down Sort numerically by priority, high priority first.
  2511. The different possibilities will be tried in sequence, and testing stops
  2512. if one comparison returns a \"not-equal\". For example, the default
  2513. '(time-up category-keep priority-down)
  2514. means: Pull out all entries having a specified time of day and sort them,
  2515. in order to make a time schedule for the current day the first thing in the
  2516. agenda listing for the day. Of the entries without a time indication, keep
  2517. the grouped in categories, don't sort the categories, but keep them in
  2518. the sequence given in `org-agenda-files'. Within each category sort by
  2519. priority.
  2520. Leaving out `category-keep' would mean that items will be sorted across
  2521. categories by priority.
  2522. Instead of a single list, this can also be a set of list for specific
  2523. contents, with a context symbol in the car of the list, any of
  2524. `agenda', `todo', `tags' for the corresponding agenda views."
  2525. :group 'org-agenda-sorting
  2526. :type `(choice
  2527. (repeat :tag "General" ,org-sorting-choice)
  2528. (list :tag "Individually"
  2529. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2530. (repeat ,org-sorting-choice))
  2531. (cons (const :tag "Strategy for TODO lists" todo)
  2532. (repeat ,org-sorting-choice))
  2533. (cons (const :tag "Strategy for Tags matches" tags)
  2534. (repeat ,org-sorting-choice)))))
  2535. (defcustom org-sort-agenda-notime-is-late t
  2536. "Non-nil means, items without time are considered late.
  2537. This is only relevant for sorting. When t, items which have no explicit
  2538. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2539. do have a time. When nil, the default time is before 0:00. You can use this
  2540. option to decide if the schedule for today should come before or after timeless
  2541. agenda entries."
  2542. :group 'org-agenda-sorting
  2543. :type 'boolean)
  2544. (defgroup org-agenda-line-format nil
  2545. "Options concerning the entry prefix in the Org-mode agenda display."
  2546. :tag "Org Agenda Line Format"
  2547. :group 'org-agenda)
  2548. (defcustom org-agenda-prefix-format
  2549. '((agenda . " %-12:c%?-12t% s")
  2550. (timeline . " % s")
  2551. (todo . " %-12:c")
  2552. (tags . " %-12:c")
  2553. (search . " %-12:c"))
  2554. "Format specifications for the prefix of items in the agenda views.
  2555. An alist with four entries, for the different agenda types. The keys to the
  2556. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2557. are format strings.
  2558. This format works similar to a printf format, with the following meaning:
  2559. %c the category of the item, \"Diary\" for entries from the diary, or
  2560. as given by the CATEGORY keyword or derived from the file name.
  2561. %T the *last* tag of the item. Last because inherited tags come
  2562. first in the list.
  2563. %t the time-of-day specification if one applies to the entry, in the
  2564. format HH:MM
  2565. %s Scheduling/Deadline information, a short string
  2566. All specifiers work basically like the standard `%s' of printf, but may
  2567. contain two additional characters: A question mark just after the `%' and
  2568. a whitespace/punctuation character just before the final letter.
  2569. If the first character after `%' is a question mark, the entire field
  2570. will only be included if the corresponding value applies to the
  2571. current entry. This is useful for fields which should have fixed
  2572. width when present, but zero width when absent. For example,
  2573. \"%?-12t\" will result in a 12 character time field if a time of the
  2574. day is specified, but will completely disappear in entries which do
  2575. not contain a time.
  2576. If there is punctuation or whitespace character just before the final
  2577. format letter, this character will be appended to the field value if
  2578. the value is not empty. For example, the format \"%-12:c\" leads to
  2579. \"Diary: \" if the category is \"Diary\". If the category were be
  2580. empty, no additional colon would be interted.
  2581. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2582. - Indent the line with two space characters
  2583. - Give the category in a 12 chars wide field, padded with whitespace on
  2584. the right (because of `-'). Append a colon if there is a category
  2585. (because of `:').
  2586. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2587. time, don't put in an empty field, just skip it (because of '?').
  2588. - Finally, put the scheduling information and append a whitespace.
  2589. As another example, if you don't want the time-of-day of entries in
  2590. the prefix, you could use:
  2591. (setq org-agenda-prefix-format \" %-11:c% s\")
  2592. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2593. `org-agenda-remove-tags'."
  2594. :type '(choice
  2595. (string :tag "General format")
  2596. (list :greedy t :tag "View dependent"
  2597. (cons (const agenda) (string :tag "Format"))
  2598. (cons (const timeline) (string :tag "Format"))
  2599. (cons (const todo) (string :tag "Format"))
  2600. (cons (const tags) (string :tag "Format"))
  2601. (cons (const search) (string :tag "Format"))))
  2602. :group 'org-agenda-line-format)
  2603. (defvar org-prefix-format-compiled nil
  2604. "The compiled version of the most recently used prefix format.
  2605. See the variable `org-agenda-prefix-format'.")
  2606. (defcustom org-agenda-todo-keyword-format "%-1s"
  2607. "Format for the TODO keyword in agenda lines.
  2608. Set this to something like \"%-12s\" if you want all TODO keywords
  2609. to occupy a fixed space in the agenda display."
  2610. :group 'org-agenda-line-format
  2611. :type 'string)
  2612. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2613. "Text preceeding scheduled items in the agenda view.
  2614. This is a list with two strings. The first applies when the item is
  2615. scheduled on the current day. The second applies when it has been scheduled
  2616. previously, it may contain a %d to capture how many days ago the item was
  2617. scheduled."
  2618. :group 'org-agenda-line-format
  2619. :type '(list
  2620. (string :tag "Scheduled today ")
  2621. (string :tag "Scheduled previously")))
  2622. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2623. "Text preceeding deadline items in the agenda view.
  2624. This is a list with two strings. The first applies when the item has its
  2625. deadline on the current day. The second applies when it is in the past or
  2626. in the future, it may contain %d to capture how many days away the deadline
  2627. is (was)."
  2628. :group 'org-agenda-line-format
  2629. :type '(list
  2630. (string :tag "Deadline today ")
  2631. (string :tag "Deadline relative")))
  2632. (defcustom org-agenda-remove-times-when-in-prefix t
  2633. "Non-nil means, remove duplicate time specifications in agenda items.
  2634. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2635. time-of-day specification in a headline or diary entry is extracted and
  2636. placed into the prefix. If this option is non-nil, the original specification
  2637. \(a timestamp or -range, or just a plain time(range) specification like
  2638. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2639. cluttered.
  2640. The option can be t or nil. It may also be the symbol `beg', indicating
  2641. that the time should only be removed what it is located at the beginning of
  2642. the headline/diary entry."
  2643. :group 'org-agenda-line-format
  2644. :type '(choice
  2645. (const :tag "Always" t)
  2646. (const :tag "Never" nil)
  2647. (const :tag "When at beginning of entry" beg)))
  2648. (defcustom org-agenda-default-appointment-duration nil
  2649. "Default duration for appointments that only have a starting time.
  2650. When nil, no duration is specified in such cases.
  2651. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2652. :group 'org-agenda-line-format
  2653. :type '(choice
  2654. (integer :tag "Minutes")
  2655. (const :tag "No default duration")))
  2656. (defcustom org-agenda-remove-tags nil
  2657. "Non-nil means, remove the tags from the headline copy in the agenda.
  2658. When this is the symbol `prefix', only remove tags when
  2659. `org-agenda-prefix-format' contains a `%T' specifier."
  2660. :group 'org-agenda-line-format
  2661. :type '(choice
  2662. (const :tag "Always" t)
  2663. (const :tag "Never" nil)
  2664. (const :tag "When prefix format contains %T" prefix)))
  2665. (if (fboundp 'defvaralias)
  2666. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2667. 'org-agenda-remove-tags))
  2668. (defcustom org-agenda-tags-column -80
  2669. "Shift tags in agenda items to this column.
  2670. If this number is positive, it specifies the column. If it is negative,
  2671. it means that the tags should be flushright to that column. For example,
  2672. -80 works well for a normal 80 character screen."
  2673. :group 'org-agenda-line-format
  2674. :type 'integer)
  2675. (if (fboundp 'defvaralias)
  2676. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2677. (defcustom org-agenda-fontify-priorities t
  2678. "Non-nil means, highlight low and high priorities in agenda.
  2679. When t, the highest priority entries are bold, lowest priority italic.
  2680. This may also be an association list of priority faces. The face may be
  2681. a names face, or a list like `(:background \"Red\")'."
  2682. :group 'org-agenda-line-format
  2683. :type '(choice
  2684. (const :tag "Never" nil)
  2685. (const :tag "Defaults" t)
  2686. (repeat :tag "Specify"
  2687. (list (character :tag "Priority" :value ?A)
  2688. (sexp :tag "face")))))
  2689. (defgroup org-latex nil
  2690. "Options for embedding LaTeX code into Org-mode"
  2691. :tag "Org LaTeX"
  2692. :group 'org)
  2693. (defcustom org-format-latex-options
  2694. '(:foreground default :background default :scale 1.0
  2695. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2696. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2697. "Options for creating images from LaTeX fragments.
  2698. This is a property list with the following properties:
  2699. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2700. `default' means use the forground of the default face.
  2701. :background the background color, or \"Transparent\".
  2702. `default' means use the background of the default face.
  2703. :scale a scaling factor for the size of the images
  2704. :html-foreground, :html-background, :html-scale
  2705. The same numbers for HTML export.
  2706. :matchers a list indicating which matchers should be used to
  2707. find LaTeX fragments. Valid members of this list are:
  2708. \"begin\" find environments
  2709. \"$\" find math expressions surrounded by $...$
  2710. \"$$\" find math expressions surrounded by $$....$$
  2711. \"\\(\" find math expressions surrounded by \\(...\\)
  2712. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2713. :group 'org-latex
  2714. :type 'plist)
  2715. (defcustom org-format-latex-header "\\documentclass{article}
  2716. \\usepackage{fullpage} % do not remove
  2717. \\usepackage{amssymb}
  2718. \\usepackage[usenames]{color}
  2719. \\usepackage{amsmath}
  2720. \\usepackage{latexsym}
  2721. \\usepackage[mathscr]{eucal}
  2722. \\pagestyle{empty} % do not remove"
  2723. "The document header used for processing LaTeX fragments."
  2724. :group 'org-latex
  2725. :type 'string)
  2726. (defgroup org-export nil
  2727. "Options for exporting org-listings."
  2728. :tag "Org Export"
  2729. :group 'org)
  2730. (defgroup org-export-general nil
  2731. "General options for exporting Org-mode files."
  2732. :tag "Org Export General"
  2733. :group 'org-export)
  2734. ;; FIXME
  2735. (defvar org-export-publishing-directory nil)
  2736. (defcustom org-export-with-special-strings t
  2737. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2738. When this option is turned on, these strings will be exported as:
  2739. Org HTML LaTeX
  2740. -----+----------+--------
  2741. \\- &shy; \\-
  2742. -- &ndash; --
  2743. --- &mdash; ---
  2744. ... &hellip; \ldots
  2745. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2746. :group 'org-export-translation
  2747. :type 'boolean)
  2748. (defcustom org-export-language-setup
  2749. '(("en" "Author" "Date" "Table of Contents")
  2750. ("cs" "Autor" "Datum" "Obsah")
  2751. ("da" "Ophavsmand" "Dato" "Indhold")
  2752. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2753. ("es" "Autor" "Fecha" "\xcdndice")
  2754. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2755. ("it" "Autore" "Data" "Indice")
  2756. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2757. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2758. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2759. "Terms used in export text, translated to different languages.
  2760. Use the variable `org-export-default-language' to set the language,
  2761. or use the +OPTION lines for a per-file setting."
  2762. :group 'org-export-general
  2763. :type '(repeat
  2764. (list
  2765. (string :tag "HTML language tag")
  2766. (string :tag "Author")
  2767. (string :tag "Date")
  2768. (string :tag "Table of Contents"))))
  2769. (defcustom org-export-default-language "en"
  2770. "The default language of HTML export, as a string.
  2771. This should have an association in `org-export-language-setup'."
  2772. :group 'org-export-general
  2773. :type 'string)
  2774. (defcustom org-export-skip-text-before-1st-heading t
  2775. "Non-nil means, skip all text before the first headline when exporting.
  2776. When nil, that text is exported as well."
  2777. :group 'org-export-general
  2778. :type 'boolean)
  2779. (defcustom org-export-headline-levels 3
  2780. "The last level which is still exported as a headline.
  2781. Inferior levels will produce itemize lists when exported.
  2782. Note that a numeric prefix argument to an exporter function overrides
  2783. this setting.
  2784. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2785. :group 'org-export-general
  2786. :type 'number)
  2787. (defcustom org-export-with-section-numbers t
  2788. "Non-nil means, add section numbers to headlines when exporting.
  2789. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2790. :group 'org-export-general
  2791. :type 'boolean)
  2792. (defcustom org-export-with-toc t
  2793. "Non-nil means, create a table of contents in exported files.
  2794. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2795. When an integer, include levels up to N in the toc, this may then be
  2796. different from `org-export-headline-levels', but it will not be allowed
  2797. to be larger than the number of headline levels.
  2798. When nil, no table of contents is made.
  2799. Headlines which contain any TODO items will be marked with \"(*)\" in
  2800. ASCII export, and with red color in HTML output, if the option
  2801. `org-export-mark-todo-in-toc' is set.
  2802. In HTML output, the TOC will be clickable.
  2803. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2804. or \"toc:3\"."
  2805. :group 'org-export-general
  2806. :type '(choice
  2807. (const :tag "No Table of Contents" nil)
  2808. (const :tag "Full Table of Contents" t)
  2809. (integer :tag "TOC to level")))
  2810. (defcustom org-export-mark-todo-in-toc nil
  2811. "Non-nil means, mark TOC lines that contain any open TODO items."
  2812. :group 'org-export-general
  2813. :type 'boolean)
  2814. (defcustom org-export-preserve-breaks nil
  2815. "Non-nil means, preserve all line breaks when exporting.
  2816. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2817. export, line breaks will always be preserved, regardless of this variable.
  2818. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2819. :group 'org-export-general
  2820. :type 'boolean)
  2821. (defcustom org-export-with-archived-trees 'headline
  2822. "Whether subtrees with the ARCHIVE tag should be exported.
  2823. This can have three different values
  2824. nil Do not export, pretend this tree is not present
  2825. t Do export the entire tree
  2826. headline Only export the headline, but skip the tree below it."
  2827. :group 'org-export-general
  2828. :group 'org-archive
  2829. :type '(choice
  2830. (const :tag "not at all" nil)
  2831. (const :tag "headline only" 'headline)
  2832. (const :tag "entirely" t)))
  2833. (defcustom org-export-author-info t
  2834. "Non-nil means, insert author name and email into the exported file.
  2835. This option can also be set with the +OPTIONS line,
  2836. e.g. \"author-info:nil\"."
  2837. :group 'org-export-general
  2838. :type 'boolean)
  2839. (defcustom org-export-time-stamp-file t
  2840. "Non-nil means, insert a time stamp into the exported file.
  2841. The time stamp shows when the file was created.
  2842. This option can also be set with the +OPTIONS line,
  2843. e.g. \"timestamp:nil\"."
  2844. :group 'org-export-general
  2845. :type 'boolean)
  2846. (defcustom org-export-with-timestamps t
  2847. "If nil, do not export time stamps and associated keywords."
  2848. :group 'org-export-general
  2849. :type 'boolean)
  2850. (defcustom org-export-remove-timestamps-from-toc t
  2851. "If nil, remove timestamps from the table of contents entries."
  2852. :group 'org-export-general
  2853. :type 'boolean)
  2854. (defcustom org-export-with-tags 'not-in-toc
  2855. "If nil, do not export tags, just remove them from headlines.
  2856. If this is the symbol `not-in-toc', tags will be removed from table of
  2857. contents entries, but still be shown in the headlines of the document.
  2858. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2859. :group 'org-export-general
  2860. :type '(choice
  2861. (const :tag "Off" nil)
  2862. (const :tag "Not in TOC" not-in-toc)
  2863. (const :tag "On" t)))
  2864. (defcustom org-export-with-drawers nil
  2865. "Non-nil means, export with drawers like the property drawer.
  2866. When t, all drawers are exported. This may also be a list of
  2867. drawer names to export."
  2868. :group 'org-export-general
  2869. :type '(choice
  2870. (const :tag "All drawers" t)
  2871. (const :tag "None" nil)
  2872. (repeat :tag "Selected drawers"
  2873. (string :tag "Drawer name"))))
  2874. (defgroup org-export-translation nil
  2875. "Options for translating special ascii sequences for the export backends."
  2876. :tag "Org Export Translation"
  2877. :group 'org-export)
  2878. (defcustom org-export-with-emphasize t
  2879. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2880. If the export target supports emphasizing text, the word will be
  2881. typeset in bold, italic, or underlined, respectively. Works only for
  2882. single words, but you can say: I *really* *mean* *this*.
  2883. Not all export backends support this.
  2884. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2885. :group 'org-export-translation
  2886. :type 'boolean)
  2887. (defcustom org-export-with-footnotes t
  2888. "If nil, export [1] as a footnote marker.
  2889. Lines starting with [1] will be formatted as footnotes.
  2890. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2891. :group 'org-export-translation
  2892. :type 'boolean)
  2893. (defcustom org-export-with-sub-superscripts t
  2894. "Non-nil means, interpret \"_\" and \"^\" for export.
  2895. When this option is turned on, you can use TeX-like syntax for sub- and
  2896. superscripts. Several characters after \"_\" or \"^\" will be
  2897. considered as a single item - so grouping with {} is normally not
  2898. needed. For example, the following things will be parsed as single
  2899. sub- or superscripts.
  2900. 10^24 or 10^tau several digits will be considered 1 item.
  2901. 10^-12 or 10^-tau a leading sign with digits or a word
  2902. x^2-y^3 will be read as x^2 - y^3, because items are
  2903. terminated by almost any nonword/nondigit char.
  2904. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2905. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2906. sub/superscript. If you set this variable to the symbol `{}',
  2907. the braces are *required* in order to trigger interpretations as
  2908. sub/superscript. This can be helpful in documents that need \"_\"
  2909. frequently in plain text.
  2910. Not all export backends support this, but HTML does.
  2911. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2912. :group 'org-export-translation
  2913. :type '(choice
  2914. (const :tag "Always interpret" t)
  2915. (const :tag "Only with braces" {})
  2916. (const :tag "Never interpret" nil)))
  2917. (defcustom org-export-with-special-strings t
  2918. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2919. When this option is turned on, these strings will be exported as:
  2920. \\- : &shy;
  2921. -- : &ndash;
  2922. --- : &mdash;
  2923. Not all export backends support this, but HTML does.
  2924. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2925. :group 'org-export-translation
  2926. :type 'boolean)
  2927. (defcustom org-export-with-TeX-macros t
  2928. "Non-nil means, interpret simple TeX-like macros when exporting.
  2929. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2930. No only real TeX macros will work here, but the standard HTML entities
  2931. for math can be used as macro names as well. For a list of supported
  2932. names in HTML export, see the constant `org-html-entities'.
  2933. Not all export backends support this.
  2934. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2935. :group 'org-export-translation
  2936. :group 'org-export-latex
  2937. :type 'boolean)
  2938. (defcustom org-export-with-LaTeX-fragments nil
  2939. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2940. When set, the exporter will find LaTeX environments if the \\begin line is
  2941. the first non-white thing on a line. It will also find the math delimiters
  2942. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2943. display math.
  2944. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2945. :group 'org-export-translation
  2946. :group 'org-export-latex
  2947. :type 'boolean)
  2948. (defcustom org-export-with-fixed-width t
  2949. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2950. This can be used to have pre-formatted text, fragments of code etc. For
  2951. example:
  2952. : ;; Some Lisp examples
  2953. : (while (defc cnt)
  2954. : (ding))
  2955. will be looking just like this in also HTML. See also the QUOTE keyword.
  2956. Not all export backends support this.
  2957. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2958. :group 'org-export-translation
  2959. :type 'boolean)
  2960. (defcustom org-match-sexp-depth 3
  2961. "Number of stacked braces for sub/superscript matching.
  2962. This has to be set before loading org.el to be effective."
  2963. :group 'org-export-translation
  2964. :type 'integer)
  2965. (defgroup org-export-tables nil
  2966. "Options for exporting tables in Org-mode."
  2967. :tag "Org Export Tables"
  2968. :group 'org-export)
  2969. (defcustom org-export-with-tables t
  2970. "If non-nil, lines starting with \"|\" define a table.
  2971. For example:
  2972. | Name | Address | Birthday |
  2973. |-------------+----------+-----------|
  2974. | Arthur Dent | England | 29.2.2100 |
  2975. Not all export backends support this.
  2976. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2977. :group 'org-export-tables
  2978. :type 'boolean)
  2979. (defcustom org-export-highlight-first-table-line t
  2980. "Non-nil means, highlight the first table line.
  2981. In HTML export, this means use <th> instead of <td>.
  2982. In tables created with table.el, this applies to the first table line.
  2983. In Org-mode tables, all lines before the first horizontal separator
  2984. line will be formatted with <th> tags."
  2985. :group 'org-export-tables
  2986. :type 'boolean)
  2987. (defcustom org-export-table-remove-special-lines t
  2988. "Remove special lines and marking characters in calculating tables.
  2989. This removes the special marking character column from tables that are set
  2990. up for spreadsheet calculations. It also removes the entire lines
  2991. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2992. the values of constants may be useful to have."
  2993. :group 'org-export-tables
  2994. :type 'boolean)
  2995. (defcustom org-export-prefer-native-exporter-for-tables nil
  2996. "Non-nil means, always export tables created with table.el natively.
  2997. Natively means, use the HTML code generator in table.el.
  2998. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2999. the table does not use row- or column-spanning). This has the
  3000. advantage, that the automatic HTML conversions for math symbols and
  3001. sub/superscripts can be applied. Org-mode's HTML generator is also
  3002. much faster."
  3003. :group 'org-export-tables
  3004. :type 'boolean)
  3005. (defgroup org-export-ascii nil
  3006. "Options specific for ASCII export of Org-mode files."
  3007. :tag "Org Export ASCII"
  3008. :group 'org-export)
  3009. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  3010. "Characters for underlining headings in ASCII export.
  3011. In the given sequence, these characters will be used for level 1, 2, ..."
  3012. :group 'org-export-ascii
  3013. :type '(repeat character))
  3014. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  3015. "Bullet characters for headlines converted to lists in ASCII export.
  3016. The first character is used for the first lest level generated in this
  3017. way, and so on. If there are more levels than characters given here,
  3018. the list will be repeated.
  3019. Note that plain lists will keep the same bullets as the have in the
  3020. Org-mode file."
  3021. :group 'org-export-ascii
  3022. :type '(repeat character))
  3023. (defgroup org-export-xml nil
  3024. "Options specific for XML export of Org-mode files."
  3025. :tag "Org Export XML"
  3026. :group 'org-export)
  3027. (defgroup org-export-html nil
  3028. "Options specific for HTML export of Org-mode files."
  3029. :tag "Org Export HTML"
  3030. :group 'org-export)
  3031. (defcustom org-export-html-coding-system nil
  3032. ""
  3033. :group 'org-export-html
  3034. :type 'coding-system)
  3035. (defcustom org-export-html-extension "html"
  3036. "The extension for exported HTML files."
  3037. :group 'org-export-html
  3038. :type 'string)
  3039. (defcustom org-export-html-style
  3040. "<style type=\"text/css\">
  3041. html {
  3042. font-family: Times, serif;
  3043. font-size: 12pt;
  3044. }
  3045. .title { text-align: center; }
  3046. .todo { color: red; }
  3047. .done { color: green; }
  3048. .timestamp { color: grey }
  3049. .timestamp-kwd { color: CadetBlue }
  3050. .tag { background-color:lightblue; font-weight:normal }
  3051. .target { background-color: lavender; }
  3052. pre {
  3053. border: 1pt solid #AEBDCC;
  3054. background-color: #F3F5F7;
  3055. padding: 5pt;
  3056. font-family: courier, monospace;
  3057. }
  3058. table { border-collapse: collapse; }
  3059. td, th {
  3060. vertical-align: top;
  3061. <!--border: 1pt solid #ADB9CC;-->
  3062. }
  3063. </style>"
  3064. "The default style specification for exported HTML files.
  3065. Since there are different ways of setting style information, this variable
  3066. needs to contain the full HTML structure to provide a style, including the
  3067. surrounding HTML tags. The style specifications should include definitions
  3068. for new classes todo, done, title, and deadline. For example, legal values
  3069. would be:
  3070. <style type=\"text/css\">
  3071. p { font-weight: normal; color: gray; }
  3072. h1 { color: black; }
  3073. .title { text-align: center; }
  3074. .todo, .deadline { color: red; }
  3075. .done { color: green; }
  3076. </style>
  3077. or, if you want to keep the style in a file,
  3078. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3079. As the value of this option simply gets inserted into the HTML <head> header,
  3080. you can \"misuse\" it to add arbitrary text to the header."
  3081. :group 'org-export-html
  3082. :type 'string)
  3083. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3084. "Format for typesetting the document title in HTML export."
  3085. :group 'org-export-html
  3086. :type 'string)
  3087. (defcustom org-export-html-toplevel-hlevel 2
  3088. "The <H> level for level 1 headings in HTML export."
  3089. :group 'org-export-html
  3090. :type 'string)
  3091. (defcustom org-export-html-link-org-files-as-html t
  3092. "Non-nil means, make file links to `file.org' point to `file.html'.
  3093. When org-mode is exporting an org-mode file to HTML, links to
  3094. non-html files are directly put into a href tag in HTML.
  3095. However, links to other Org-mode files (recognized by the
  3096. extension `.org.) should become links to the corresponding html
  3097. file, assuming that the linked org-mode file will also be
  3098. converted to HTML.
  3099. When nil, the links still point to the plain `.org' file."
  3100. :group 'org-export-html
  3101. :type 'boolean)
  3102. (defcustom org-export-html-inline-images 'maybe
  3103. "Non-nil means, inline images into exported HTML pages.
  3104. This is done using an <img> tag. When nil, an anchor with href is used to
  3105. link to the image. If this option is `maybe', then images in links with
  3106. an empty description will be inlined, while images with a description will
  3107. be linked only."
  3108. :group 'org-export-html
  3109. :type '(choice (const :tag "Never" nil)
  3110. (const :tag "Always" t)
  3111. (const :tag "When there is no description" maybe)))
  3112. ;; FIXME: rename
  3113. (defcustom org-export-html-expand t
  3114. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3115. When nil, these tags will be exported as plain text and therefore
  3116. not be interpreted by a browser.
  3117. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3118. :group 'org-export-html
  3119. :type 'boolean)
  3120. (defcustom org-export-html-table-tag
  3121. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3122. "The HTML tag that is used to start a table.
  3123. This must be a <table> tag, but you may change the options like
  3124. borders and spacing."
  3125. :group 'org-export-html
  3126. :type 'string)
  3127. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3128. "The opening tag for table header fields.
  3129. This is customizable so that alignment options can be specified."
  3130. :group 'org-export-tables
  3131. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3132. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3133. "The opening tag for table data fields.
  3134. This is customizable so that alignment options can be specified."
  3135. :group 'org-export-tables
  3136. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3137. (defcustom org-export-html-with-timestamp nil
  3138. "If non-nil, write `org-export-html-html-helper-timestamp'
  3139. into the exported HTML text. Otherwise, the buffer will just be saved
  3140. to a file."
  3141. :group 'org-export-html
  3142. :type 'boolean)
  3143. (defcustom org-export-html-html-helper-timestamp
  3144. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3145. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3146. :group 'org-export-html
  3147. :type 'string)
  3148. (defgroup org-export-icalendar nil
  3149. "Options specific for iCalendar export of Org-mode files."
  3150. :tag "Org Export iCalendar"
  3151. :group 'org-export)
  3152. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3153. "The file name for the iCalendar file covering all agenda files.
  3154. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3155. The file name should be absolute, the file will be overwritten without warning."
  3156. :group 'org-export-icalendar
  3157. :type 'file)
  3158. (defcustom org-icalendar-include-todo nil
  3159. "Non-nil means, export to iCalendar files should also cover TODO items."
  3160. :group 'org-export-icalendar
  3161. :type '(choice
  3162. (const :tag "None" nil)
  3163. (const :tag "Unfinished" t)
  3164. (const :tag "All" all)))
  3165. (defcustom org-icalendar-include-sexps t
  3166. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3167. These are entries like in the diary, but directly in an Org-mode file."
  3168. :group 'org-export-icalendar
  3169. :type 'boolean)
  3170. (defcustom org-icalendar-include-body 100
  3171. "Amount of text below headline to be included in iCalendar export.
  3172. This is a number of characters that should maximally be included.
  3173. Properties, scheduling and clocking lines will always be removed.
  3174. The text will be inserted into the DESCRIPTION field."
  3175. :group 'org-export-icalendar
  3176. :type '(choice
  3177. (const :tag "Nothing" nil)
  3178. (const :tag "Everything" t)
  3179. (integer :tag "Max characters")))
  3180. (defcustom org-icalendar-combined-name "OrgMode"
  3181. "Calendar name for the combined iCalendar representing all agenda files."
  3182. :group 'org-export-icalendar
  3183. :type 'string)
  3184. (defgroup org-font-lock nil
  3185. "Font-lock settings for highlighting in Org-mode."
  3186. :tag "Org Font Lock"
  3187. :group 'org)
  3188. (defcustom org-level-color-stars-only nil
  3189. "Non-nil means fontify only the stars in each headline.
  3190. When nil, the entire headline is fontified.
  3191. Changing it requires restart of `font-lock-mode' to become effective
  3192. also in regions already fontified."
  3193. :group 'org-font-lock
  3194. :type 'boolean)
  3195. (defcustom org-hide-leading-stars nil
  3196. "Non-nil means, hide the first N-1 stars in a headline.
  3197. This works by using the face `org-hide' for these stars. This
  3198. face is white for a light background, and black for a dark
  3199. background. You may have to customize the face `org-hide' to
  3200. make this work.
  3201. Changing it requires restart of `font-lock-mode' to become effective
  3202. also in regions already fontified.
  3203. You may also set this on a per-file basis by adding one of the following
  3204. lines to the buffer:
  3205. #+STARTUP: hidestars
  3206. #+STARTUP: showstars"
  3207. :group 'org-font-lock
  3208. :type 'boolean)
  3209. (defcustom org-fontify-done-headline nil
  3210. "Non-nil means, change the face of a headline if it is marked DONE.
  3211. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3212. When this is non-nil, the headline after the keyword is set to the
  3213. `org-headline-done' as an additional indication."
  3214. :group 'org-font-lock
  3215. :type 'boolean)
  3216. (defcustom org-fontify-emphasized-text t
  3217. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3218. Changing this variable requires a restart of Emacs to take effect."
  3219. :group 'org-font-lock
  3220. :type 'boolean)
  3221. (defcustom org-highlight-latex-fragments-and-specials nil
  3222. "Non-nil means, fontify what is treated specially by the exporters."
  3223. :group 'org-font-lock
  3224. :type 'boolean)
  3225. (defcustom org-hide-emphasis-markers nil
  3226. "Non-nil mean font-lock should hide the emphasis marker characters."
  3227. :group 'org-font-lock
  3228. :type 'boolean)
  3229. (defvar org-emph-re nil
  3230. "Regular expression for matching emphasis.")
  3231. (defvar org-verbatim-re nil
  3232. "Regular expression for matching verbatim text.")
  3233. (defvar org-emphasis-regexp-components) ; defined just below
  3234. (defvar org-emphasis-alist) ; defined just below
  3235. (defun org-set-emph-re (var val)
  3236. "Set variable and compute the emphasis regular expression."
  3237. (set var val)
  3238. (when (and (boundp 'org-emphasis-alist)
  3239. (boundp 'org-emphasis-regexp-components)
  3240. org-emphasis-alist org-emphasis-regexp-components)
  3241. (let* ((e org-emphasis-regexp-components)
  3242. (pre (car e))
  3243. (post (nth 1 e))
  3244. (border (nth 2 e))
  3245. (body (nth 3 e))
  3246. (nl (nth 4 e))
  3247. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3248. (body1 (concat body "*?"))
  3249. (markers (mapconcat 'car org-emphasis-alist ""))
  3250. (vmarkers (mapconcat
  3251. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3252. org-emphasis-alist "")))
  3253. ;; make sure special characters appear at the right position in the class
  3254. (if (string-match "\\^" markers)
  3255. (setq markers (concat (replace-match "" t t markers) "^")))
  3256. (if (string-match "-" markers)
  3257. (setq markers (concat (replace-match "" t t markers) "-")))
  3258. (if (string-match "\\^" vmarkers)
  3259. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3260. (if (string-match "-" vmarkers)
  3261. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3262. (if (> nl 0)
  3263. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3264. (int-to-string nl) "\\}")))
  3265. ;; Make the regexp
  3266. (setq org-emph-re
  3267. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3268. "\\("
  3269. "\\([" markers "]\\)"
  3270. "\\("
  3271. "[^" border "]\\|"
  3272. "[^" border (if (and nil stacked) markers) "]"
  3273. body1
  3274. "[^" border (if (and nil stacked) markers) "]"
  3275. "\\)"
  3276. "\\3\\)"
  3277. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3278. (setq org-verbatim-re
  3279. (concat "\\([" pre "]\\|^\\)"
  3280. "\\("
  3281. "\\([" vmarkers "]\\)"
  3282. "\\("
  3283. "[^" border "]\\|"
  3284. "[^" border "]"
  3285. body1
  3286. "[^" border "]"
  3287. "\\)"
  3288. "\\3\\)"
  3289. "\\([" post "]\\|$\\)")))))
  3290. (defcustom org-emphasis-regexp-components
  3291. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3292. "Components used to build the regular expression for emphasis.
  3293. This is a list with 6 entries. Terminology: In an emphasis string
  3294. like \" *strong word* \", we call the initial space PREMATCH, the final
  3295. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3296. and \"trong wor\" is the body. The different components in this variable
  3297. specify what is allowed/forbidden in each part:
  3298. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3299. post Chars allowed as postmatch. End of line will be allowed too.
  3300. border The chars *forbidden* as border characters.
  3301. body-regexp A regexp like \".\" to match a body character. Don't use
  3302. non-shy groups here, and don't allow newline here.
  3303. newline The maximum number of newlines allowed in an emphasis exp.
  3304. Use customize to modify this, or restart Emacs after changing it."
  3305. :group 'org-font-lock
  3306. :set 'org-set-emph-re
  3307. :type '(list
  3308. (sexp :tag "Allowed chars in pre ")
  3309. (sexp :tag "Allowed chars in post ")
  3310. (sexp :tag "Forbidden chars in border ")
  3311. (sexp :tag "Regexp for body ")
  3312. (integer :tag "number of newlines allowed")
  3313. (option (boolean :tag "Stacking (DISABLED) "))))
  3314. (defcustom org-emphasis-alist
  3315. '(("*" bold "<b>" "</b>")
  3316. ("/" italic "<i>" "</i>")
  3317. ("_" underline "<u>" "</u>")
  3318. ("=" org-code "<code>" "</code>" verbatim)
  3319. ("~" org-verbatim "" "" verbatim)
  3320. ("+" (:strike-through t) "<del>" "</del>")
  3321. )
  3322. "Special syntax for emphasized text.
  3323. Text starting and ending with a special character will be emphasized, for
  3324. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3325. characters, the face to be used by font-lock for highlighting in Org-mode
  3326. Emacs buffers, and the HTML tags to be used for this.
  3327. Use customize to modify this, or restart Emacs after changing it."
  3328. :group 'org-font-lock
  3329. :set 'org-set-emph-re
  3330. :type '(repeat
  3331. (list
  3332. (string :tag "Marker character")
  3333. (choice
  3334. (face :tag "Font-lock-face")
  3335. (plist :tag "Face property list"))
  3336. (string :tag "HTML start tag")
  3337. (string :tag "HTML end tag")
  3338. (option (const verbatim)))))
  3339. ;;; The faces
  3340. (defgroup org-faces nil
  3341. "Faces in Org-mode."
  3342. :tag "Org Faces"
  3343. :group 'org-font-lock)
  3344. (defun org-compatible-face (inherits specs)
  3345. "Make a compatible face specification.
  3346. If INHERITS is an existing face and if the Emacs version supports it,
  3347. just inherit the face. If not, use SPECS to define the face.
  3348. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3349. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3350. to the top of the list. The `min-colors' attribute will be removed from
  3351. any other entries, and any resulting duplicates will be removed entirely."
  3352. (cond
  3353. ((and inherits (facep inherits)
  3354. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3355. ;; In Emacs 23, we use inheritance where possible.
  3356. ;; We only do this in Emacs 23, because only there the outline
  3357. ;; faces have been changed to the original org-mode-level-faces.
  3358. (list (list t :inherit inherits)))
  3359. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3360. ;; These do not understand the `min-colors' attribute.
  3361. (let (r e a)
  3362. (while (setq e (pop specs))
  3363. (cond
  3364. ((memq (car e) '(t default)) (push e r))
  3365. ((setq a (member '(min-colors 8) (car e)))
  3366. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3367. (cdr e)))))
  3368. ((setq a (assq 'min-colors (car e)))
  3369. (setq e (cons (delq a (car e)) (cdr e)))
  3370. (or (assoc (car e) r) (push e r)))
  3371. (t (or (assoc (car e) r) (push e r)))))
  3372. (nreverse r)))
  3373. (t specs)))
  3374. (put 'org-compatible-face 'lisp-indent-function 1)
  3375. (defface org-hide
  3376. '((((background light)) (:foreground "white"))
  3377. (((background dark)) (:foreground "black")))
  3378. "Face used to hide leading stars in headlines.
  3379. The forground color of this face should be equal to the background
  3380. color of the frame."
  3381. :group 'org-faces)
  3382. (defface org-level-1 ;; font-lock-function-name-face
  3383. (org-compatible-face 'outline-1
  3384. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3385. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3386. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3387. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3388. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3389. (t (:bold t))))
  3390. "Face used for level 1 headlines."
  3391. :group 'org-faces)
  3392. (defface org-level-2 ;; font-lock-variable-name-face
  3393. (org-compatible-face 'outline-2
  3394. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3395. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3396. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3397. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3398. (t (:bold t))))
  3399. "Face used for level 2 headlines."
  3400. :group 'org-faces)
  3401. (defface org-level-3 ;; font-lock-keyword-face
  3402. (org-compatible-face 'outline-3
  3403. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3404. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3405. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3406. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3407. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3408. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3409. (t (:bold t))))
  3410. "Face used for level 3 headlines."
  3411. :group 'org-faces)
  3412. (defface org-level-4 ;; font-lock-comment-face
  3413. (org-compatible-face 'outline-4
  3414. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3415. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3416. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3417. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3418. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3419. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3420. (t (:bold t))))
  3421. "Face used for level 4 headlines."
  3422. :group 'org-faces)
  3423. (defface org-level-5 ;; font-lock-type-face
  3424. (org-compatible-face 'outline-5
  3425. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3426. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3427. (((class color) (min-colors 8)) (:foreground "green"))))
  3428. "Face used for level 5 headlines."
  3429. :group 'org-faces)
  3430. (defface org-level-6 ;; font-lock-constant-face
  3431. (org-compatible-face 'outline-6
  3432. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3433. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3434. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3435. "Face used for level 6 headlines."
  3436. :group 'org-faces)
  3437. (defface org-level-7 ;; font-lock-builtin-face
  3438. (org-compatible-face 'outline-7
  3439. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3440. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3441. (((class color) (min-colors 8)) (:foreground "blue"))))
  3442. "Face used for level 7 headlines."
  3443. :group 'org-faces)
  3444. (defface org-level-8 ;; font-lock-string-face
  3445. (org-compatible-face 'outline-8
  3446. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3447. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3448. (((class color) (min-colors 8)) (:foreground "green"))))
  3449. "Face used for level 8 headlines."
  3450. :group 'org-faces)
  3451. (defface org-special-keyword ;; font-lock-string-face
  3452. (org-compatible-face nil
  3453. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3454. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3455. (t (:italic t))))
  3456. "Face used for special keywords."
  3457. :group 'org-faces)
  3458. (defface org-drawer ;; font-lock-function-name-face
  3459. (org-compatible-face nil
  3460. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3461. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3462. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3463. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3464. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3465. (t (:bold t))))
  3466. "Face used for drawers."
  3467. :group 'org-faces)
  3468. (defface org-property-value nil
  3469. "Face used for the value of a property."
  3470. :group 'org-faces)
  3471. (defface org-column
  3472. (org-compatible-face nil
  3473. '((((class color) (min-colors 16) (background light))
  3474. (:background "grey90"))
  3475. (((class color) (min-colors 16) (background dark))
  3476. (:background "grey30"))
  3477. (((class color) (min-colors 8))
  3478. (:background "cyan" :foreground "black"))
  3479. (t (:inverse-video t))))
  3480. "Face for column display of entry properties."
  3481. :group 'org-faces)
  3482. (when (fboundp 'set-face-attribute)
  3483. ;; Make sure that a fixed-width face is used when we have a column table.
  3484. (set-face-attribute 'org-column nil
  3485. :height (face-attribute 'default :height)
  3486. :family (face-attribute 'default :family)))
  3487. (defface org-warning
  3488. (org-compatible-face 'font-lock-warning-face
  3489. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3490. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3491. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3492. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3493. (t (:bold t))))
  3494. "Face for deadlines and TODO keywords."
  3495. :group 'org-faces)
  3496. (defface org-archived ; similar to shadow
  3497. (org-compatible-face 'shadow
  3498. '((((class color grayscale) (min-colors 88) (background light))
  3499. (:foreground "grey50"))
  3500. (((class color grayscale) (min-colors 88) (background dark))
  3501. (:foreground "grey70"))
  3502. (((class color) (min-colors 8) (background light))
  3503. (:foreground "green"))
  3504. (((class color) (min-colors 8) (background dark))
  3505. (:foreground "yellow"))))
  3506. "Face for headline with the ARCHIVE tag."
  3507. :group 'org-faces)
  3508. (defface org-link
  3509. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3510. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3511. (t (:underline t)))
  3512. "Face for links."
  3513. :group 'org-faces)
  3514. (defface org-ellipsis
  3515. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3516. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3517. (t (:strike-through t)))
  3518. "Face for the ellipsis in folded text."
  3519. :group 'org-faces)
  3520. (defface org-target
  3521. '((((class color) (background light)) (:underline t))
  3522. (((class color) (background dark)) (:underline t))
  3523. (t (:underline t)))
  3524. "Face for links."
  3525. :group 'org-faces)
  3526. (defface org-date
  3527. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3528. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3529. (t (:underline t)))
  3530. "Face for links."
  3531. :group 'org-faces)
  3532. (defface org-sexp-date
  3533. '((((class color) (background light)) (:foreground "Purple"))
  3534. (((class color) (background dark)) (:foreground "Cyan"))
  3535. (t (:underline t)))
  3536. "Face for links."
  3537. :group 'org-faces)
  3538. (defface org-tag
  3539. '((t (:bold t)))
  3540. "Face for tags."
  3541. :group 'org-faces)
  3542. (defface org-todo ; font-lock-warning-face
  3543. (org-compatible-face nil
  3544. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3545. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3546. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3547. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3548. (t (:inverse-video t :bold t))))
  3549. "Face for TODO keywords."
  3550. :group 'org-faces)
  3551. (defface org-done ;; font-lock-type-face
  3552. (org-compatible-face nil
  3553. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3554. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3555. (((class color) (min-colors 8)) (:foreground "green"))
  3556. (t (:bold t))))
  3557. "Face used for todo keywords that indicate DONE items."
  3558. :group 'org-faces)
  3559. (defface org-headline-done ;; font-lock-string-face
  3560. (org-compatible-face nil
  3561. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3562. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3563. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3564. "Face used to indicate that a headline is DONE.
  3565. This face is only used if `org-fontify-done-headline' is set. If applies
  3566. to the part of the headline after the DONE keyword."
  3567. :group 'org-faces)
  3568. (defcustom org-todo-keyword-faces nil
  3569. "Faces for specific TODO keywords.
  3570. This is a list of cons cells, with TODO keywords in the car
  3571. and faces in the cdr. The face can be a symbol, or a property
  3572. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3573. :group 'org-faces
  3574. :group 'org-todo
  3575. :type '(repeat
  3576. (cons
  3577. (string :tag "keyword")
  3578. (sexp :tag "face"))))
  3579. (defface org-table ;; font-lock-function-name-face
  3580. (org-compatible-face nil
  3581. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3582. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3583. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3584. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3585. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3586. (((class color) (min-colors 8) (background dark)))))
  3587. "Face used for tables."
  3588. :group 'org-faces)
  3589. (defface org-formula
  3590. (org-compatible-face nil
  3591. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3592. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3593. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3594. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3595. (t (:bold t :italic t))))
  3596. "Face for formulas."
  3597. :group 'org-faces)
  3598. (defface org-code
  3599. (org-compatible-face nil
  3600. '((((class color grayscale) (min-colors 88) (background light))
  3601. (:foreground "grey50"))
  3602. (((class color grayscale) (min-colors 88) (background dark))
  3603. (:foreground "grey70"))
  3604. (((class color) (min-colors 8) (background light))
  3605. (:foreground "green"))
  3606. (((class color) (min-colors 8) (background dark))
  3607. (:foreground "yellow"))))
  3608. "Face for fixed-with text like code snippets."
  3609. :group 'org-faces
  3610. :version "22.1")
  3611. (defface org-verbatim
  3612. (org-compatible-face nil
  3613. '((((class color grayscale) (min-colors 88) (background light))
  3614. (:foreground "grey50" :underline t))
  3615. (((class color grayscale) (min-colors 88) (background dark))
  3616. (:foreground "grey70" :underline t))
  3617. (((class color) (min-colors 8) (background light))
  3618. (:foreground "green" :underline t))
  3619. (((class color) (min-colors 8) (background dark))
  3620. (:foreground "yellow" :underline t))))
  3621. "Face for fixed-with text like code snippets."
  3622. :group 'org-faces
  3623. :version "22.1")
  3624. (defface org-agenda-structure ;; font-lock-function-name-face
  3625. (org-compatible-face nil
  3626. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3627. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3628. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3629. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3630. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3631. (t (:bold t))))
  3632. "Face used in agenda for captions and dates."
  3633. :group 'org-faces)
  3634. (defface org-scheduled-today
  3635. (org-compatible-face nil
  3636. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3637. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3638. (((class color) (min-colors 8)) (:foreground "green"))
  3639. (t (:bold t :italic t))))
  3640. "Face for items scheduled for a certain day."
  3641. :group 'org-faces)
  3642. (defface org-scheduled-previously
  3643. (org-compatible-face nil
  3644. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3645. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3646. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3647. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3648. (t (:bold t))))
  3649. "Face for items scheduled previously, and not yet done."
  3650. :group 'org-faces)
  3651. (defface org-upcoming-deadline
  3652. (org-compatible-face nil
  3653. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3654. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3655. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3656. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3657. (t (:bold t))))
  3658. "Face for items scheduled previously, and not yet done."
  3659. :group 'org-faces)
  3660. (defcustom org-agenda-deadline-faces
  3661. '((1.0 . org-warning)
  3662. (0.5 . org-upcoming-deadline)
  3663. (0.0 . default))
  3664. "Faces for showing deadlines in the agenda.
  3665. This is a list of cons cells. The cdr of each cell is a face to be used,
  3666. and it can also just be like '(:foreground \"yellow\").
  3667. Each car is a fraction of the head-warning time that must have passed for
  3668. this the face in the cdr to be used for display. The numbers must be
  3669. given in descending order. The head-warning time is normally taken
  3670. from `org-deadline-warning-days', but can also be specified in the deadline
  3671. timestamp itself, like this:
  3672. DEADLINE: <2007-08-13 Mon -8d>
  3673. You may use d for days, w for weeks, m for months and y for years. Months
  3674. and years will only be treated in an approximate fashion (30.4 days for a
  3675. month and 365.24 days for a year)."
  3676. :group 'org-faces
  3677. :group 'org-agenda-daily/weekly
  3678. :type '(repeat
  3679. (cons
  3680. (number :tag "Fraction of head-warning time passed")
  3681. (sexp :tag "Face"))))
  3682. ;; FIXME: this is not a good face yet.
  3683. (defface org-agenda-restriction-lock
  3684. (org-compatible-face nil
  3685. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3686. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3687. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3688. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3689. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3690. (t (:inverse-video t))))
  3691. "Face for showing the agenda restriction lock."
  3692. :group 'org-faces)
  3693. (defface org-time-grid ;; font-lock-variable-name-face
  3694. (org-compatible-face nil
  3695. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3696. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3697. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3698. "Face used for time grids."
  3699. :group 'org-faces)
  3700. (defconst org-level-faces
  3701. '(org-level-1 org-level-2 org-level-3 org-level-4
  3702. org-level-5 org-level-6 org-level-7 org-level-8
  3703. ))
  3704. (defcustom org-n-level-faces (length org-level-faces)
  3705. "The number of different faces to be used for headlines.
  3706. Org-mode defines 8 different headline faces, so this can be at most 8.
  3707. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3708. :type 'number
  3709. :group 'org-faces)
  3710. ;;; Functions and variables from ther packages
  3711. ;; Declared here to avoid compiler warnings
  3712. (eval-and-compile
  3713. (unless (fboundp 'declare-function)
  3714. (defmacro declare-function (fn file &optional arglist fileonly))))
  3715. ;; XEmacs only
  3716. (defvar outline-mode-menu-heading)
  3717. (defvar outline-mode-menu-show)
  3718. (defvar outline-mode-menu-hide)
  3719. (defvar zmacs-regions) ; XEmacs regions
  3720. ;; Emacs only
  3721. (defvar mark-active)
  3722. ;; Various packages
  3723. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3724. (declare-function add-to-diary-list "diary-lib"
  3725. (date string specifier &optional marker globcolor literal))
  3726. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3727. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3728. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3729. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3730. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3731. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3732. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3733. (declare-function bbdb-record-name "ext:bbdb" (record))
  3734. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3735. (declare-function bibtex-generate-autokey "bibtex" ())
  3736. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3737. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3738. (defvar calc-embedded-close-formula)
  3739. (defvar calc-embedded-open-formula)
  3740. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3741. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3742. (declare-function calendar-check-holidays "holidays" (date))
  3743. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3744. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3745. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3746. (declare-function calendar-forward-day "cal-move" (arg))
  3747. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3748. (declare-function calendar-goto-date "cal-move" (date))
  3749. (declare-function calendar-goto-today "cal-move" ())
  3750. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3751. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3752. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3753. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3754. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3755. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3756. (defvar calendar-mode-map)
  3757. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3758. (declare-function cdlatex-tab "ext:cdlatex" ())
  3759. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3760. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3761. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3762. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3763. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3764. (defvar font-lock-unfontify-region-function)
  3765. (declare-function gnus-article-show-summary "gnus-art" ())
  3766. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3767. (defvar gnus-other-frame-object)
  3768. (defvar gnus-group-name)
  3769. (defvar gnus-article-current)
  3770. (defvar Info-current-file)
  3771. (defvar Info-current-node)
  3772. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3773. (declare-function mh-find-path "mh-utils" ())
  3774. (declare-function mh-get-header-field "mh-utils" (field))
  3775. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3776. (declare-function mh-header-display "mh-show" ())
  3777. (declare-function mh-index-previous-folder "mh-search" ())
  3778. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3779. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3780. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3781. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3782. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3783. (declare-function mh-show-header-display "mh-show" t t)
  3784. (declare-function mh-show-msg "mh-show" (msg))
  3785. (declare-function mh-show-show "mh-show" t t)
  3786. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3787. (defvar mh-progs)
  3788. (defvar mh-current-folder)
  3789. (defvar mh-show-folder-buffer)
  3790. (defvar mh-index-folder)
  3791. (defvar mh-searcher)
  3792. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3793. (declare-function parse-time-string "parse-time" (string))
  3794. (declare-function remember "remember" (&optional initial))
  3795. (declare-function remember-buffer-desc "remember" ())
  3796. (declare-function remember-finalize "remember" ())
  3797. (defvar remember-save-after-remembering)
  3798. (defvar remember-data-file)
  3799. (defvar remember-register)
  3800. (defvar remember-buffer)
  3801. (defvar remember-handler-functions)
  3802. (defvar remember-annotation-functions)
  3803. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3804. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3805. (declare-function rmail-what-message "rmail" ())
  3806. (defvar rmail-current-message)
  3807. (defvar texmathp-why)
  3808. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3809. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3810. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3811. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3812. (declare-function vm-isearch-update "ext:vm-search" ())
  3813. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3814. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3815. (declare-function vm-su-subject "ext:vm-summary" (m))
  3816. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3817. (defvar vm-message-pointer)
  3818. (defvar vm-folder-directory)
  3819. (defvar w3m-current-url)
  3820. (defvar w3m-current-title)
  3821. ;; backward compatibility to old version of wl
  3822. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3823. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3824. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3825. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3826. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3827. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3828. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3829. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3830. (defvar wl-summary-buffer-elmo-folder)
  3831. (defvar wl-summary-buffer-folder-name)
  3832. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3833. (defvar org-latex-regexps)
  3834. (defvar constants-unit-system)
  3835. ;;; Variables for pre-computed regular expressions, all buffer local
  3836. (defvar org-drawer-regexp nil
  3837. "Matches first line of a hidden block.")
  3838. (make-variable-buffer-local 'org-drawer-regexp)
  3839. (defvar org-todo-regexp nil
  3840. "Matches any of the TODO state keywords.")
  3841. (make-variable-buffer-local 'org-todo-regexp)
  3842. (defvar org-not-done-regexp nil
  3843. "Matches any of the TODO state keywords except the last one.")
  3844. (make-variable-buffer-local 'org-not-done-regexp)
  3845. (defvar org-todo-line-regexp nil
  3846. "Matches a headline and puts TODO state into group 2 if present.")
  3847. (make-variable-buffer-local 'org-todo-line-regexp)
  3848. (defvar org-complex-heading-regexp nil
  3849. "Matches a headline and puts everything into groups:
  3850. group 1: the stars
  3851. group 2: The todo keyword, maybe
  3852. group 3: Priority cookie
  3853. group 4: True headline
  3854. group 5: Tags")
  3855. (make-variable-buffer-local 'org-complex-heading-regexp)
  3856. (defvar org-todo-line-tags-regexp nil
  3857. "Matches a headline and puts TODO state into group 2 if present.
  3858. Also put tags into group 4 if tags are present.")
  3859. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3860. (defvar org-nl-done-regexp nil
  3861. "Matches newline followed by a headline with the DONE keyword.")
  3862. (make-variable-buffer-local 'org-nl-done-regexp)
  3863. (defvar org-looking-at-done-regexp nil
  3864. "Matches the DONE keyword a point.")
  3865. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3866. (defvar org-ds-keyword-length 12
  3867. "Maximum length of the Deadline and SCHEDULED keywords.")
  3868. (make-variable-buffer-local 'org-ds-keyword-length)
  3869. (defvar org-deadline-regexp nil
  3870. "Matches the DEADLINE keyword.")
  3871. (make-variable-buffer-local 'org-deadline-regexp)
  3872. (defvar org-deadline-time-regexp nil
  3873. "Matches the DEADLINE keyword together with a time stamp.")
  3874. (make-variable-buffer-local 'org-deadline-time-regexp)
  3875. (defvar org-deadline-line-regexp nil
  3876. "Matches the DEADLINE keyword and the rest of the line.")
  3877. (make-variable-buffer-local 'org-deadline-line-regexp)
  3878. (defvar org-scheduled-regexp nil
  3879. "Matches the SCHEDULED keyword.")
  3880. (make-variable-buffer-local 'org-scheduled-regexp)
  3881. (defvar org-scheduled-time-regexp nil
  3882. "Matches the SCHEDULED keyword together with a time stamp.")
  3883. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3884. (defvar org-closed-time-regexp nil
  3885. "Matches the CLOSED keyword together with a time stamp.")
  3886. (make-variable-buffer-local 'org-closed-time-regexp)
  3887. (defvar org-keyword-time-regexp nil
  3888. "Matches any of the 4 keywords, together with the time stamp.")
  3889. (make-variable-buffer-local 'org-keyword-time-regexp)
  3890. (defvar org-keyword-time-not-clock-regexp nil
  3891. "Matches any of the 3 keywords, together with the time stamp.")
  3892. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3893. (defvar org-maybe-keyword-time-regexp nil
  3894. "Matches a timestamp, possibly preceeded by a keyword.")
  3895. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3896. (defvar org-planning-or-clock-line-re nil
  3897. "Matches a line with planning or clock info.")
  3898. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3899. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3900. rear-nonsticky t mouse-map t fontified t)
  3901. "Properties to remove when a string without properties is wanted.")
  3902. (defsubst org-match-string-no-properties (num &optional string)
  3903. (if (featurep 'xemacs)
  3904. (let ((s (match-string num string)))
  3905. (remove-text-properties 0 (length s) org-rm-props s)
  3906. s)
  3907. (match-string-no-properties num string)))
  3908. (defsubst org-no-properties (s)
  3909. (if (fboundp 'set-text-properties)
  3910. (set-text-properties 0 (length s) nil s)
  3911. (remove-text-properties 0 (length s) org-rm-props s))
  3912. s)
  3913. (defsubst org-get-alist-option (option key)
  3914. (cond ((eq key t) t)
  3915. ((eq option t) t)
  3916. ((assoc key option) (cdr (assoc key option)))
  3917. (t (cdr (assq 'default option)))))
  3918. (defsubst org-inhibit-invisibility ()
  3919. "Modified `buffer-invisibility-spec' for Emacs 21.
  3920. Some ops with invisible text do not work correctly on Emacs 21. For these
  3921. we turn off invisibility temporarily. Use this in a `let' form."
  3922. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3923. (defsubst org-set-local (var value)
  3924. "Make VAR local in current buffer and set it to VALUE."
  3925. (set (make-variable-buffer-local var) value))
  3926. (defsubst org-mode-p ()
  3927. "Check if the current buffer is in Org-mode."
  3928. (eq major-mode 'org-mode))
  3929. (defsubst org-last (list)
  3930. "Return the last element of LIST."
  3931. (car (last list)))
  3932. (defun org-let (list &rest body)
  3933. (eval (cons 'let (cons list body))))
  3934. (put 'org-let 'lisp-indent-function 1)
  3935. (defun org-let2 (list1 list2 &rest body)
  3936. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3937. (put 'org-let2 'lisp-indent-function 2)
  3938. (defconst org-startup-options
  3939. '(("fold" org-startup-folded t)
  3940. ("overview" org-startup-folded t)
  3941. ("nofold" org-startup-folded nil)
  3942. ("showall" org-startup-folded nil)
  3943. ("content" org-startup-folded content)
  3944. ("hidestars" org-hide-leading-stars t)
  3945. ("showstars" org-hide-leading-stars nil)
  3946. ("odd" org-odd-levels-only t)
  3947. ("oddeven" org-odd-levels-only nil)
  3948. ("align" org-startup-align-all-tables t)
  3949. ("noalign" org-startup-align-all-tables nil)
  3950. ("customtime" org-display-custom-times t)
  3951. ("logdone" org-log-done time)
  3952. ("lognotedone" org-log-done note)
  3953. ("nologdone" org-log-done nil)
  3954. ("lognoteclock-out" org-log-note-clock-out t)
  3955. ("nolognoteclock-out" org-log-note-clock-out nil)
  3956. ("logrepeat" org-log-repeat state)
  3957. ("lognoterepeat" org-log-repeat note)
  3958. ("nologrepeat" org-log-repeat nil)
  3959. ("constcgs" constants-unit-system cgs)
  3960. ("constSI" constants-unit-system SI))
  3961. "Variable associated with STARTUP options for org-mode.
  3962. Each element is a list of three items: The startup options as written
  3963. in the #+STARTUP line, the corresponding variable, and the value to
  3964. set this variable to if the option is found. An optional forth element PUSH
  3965. means to push this value onto the list in the variable.")
  3966. (defun org-set-regexps-and-options ()
  3967. "Precompute regular expressions for current buffer."
  3968. (when (org-mode-p)
  3969. (org-set-local 'org-todo-kwd-alist nil)
  3970. (org-set-local 'org-todo-key-alist nil)
  3971. (org-set-local 'org-todo-key-trigger nil)
  3972. (org-set-local 'org-todo-keywords-1 nil)
  3973. (org-set-local 'org-done-keywords nil)
  3974. (org-set-local 'org-todo-heads nil)
  3975. (org-set-local 'org-todo-sets nil)
  3976. (org-set-local 'org-todo-log-states nil)
  3977. (let ((re (org-make-options-regexp
  3978. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3979. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3980. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3981. (splitre "[ \t]+")
  3982. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3983. tail sep kws1 prio props drawers)
  3984. (save-excursion
  3985. (save-restriction
  3986. (widen)
  3987. (goto-char (point-min))
  3988. (while (re-search-forward re nil t)
  3989. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3990. (cond
  3991. ((equal key "CATEGORY")
  3992. (if (string-match "[ \t]+$" value)
  3993. (setq value (replace-match "" t t value)))
  3994. (setq cat value))
  3995. ((member key '("SEQ_TODO" "TODO"))
  3996. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3997. ((equal key "TYP_TODO")
  3998. (push (cons 'type (org-split-string value splitre)) kwds))
  3999. ((equal key "TAGS")
  4000. (setq tags (append tags (org-split-string value splitre))))
  4001. ((equal key "COLUMNS")
  4002. (org-set-local 'org-columns-default-format value))
  4003. ((equal key "LINK")
  4004. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4005. (push (cons (match-string 1 value)
  4006. (org-trim (match-string 2 value)))
  4007. links)))
  4008. ((equal key "PRIORITIES")
  4009. (setq prio (org-split-string value " +")))
  4010. ((equal key "PROPERTY")
  4011. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4012. (push (cons (match-string 1 value) (match-string 2 value))
  4013. props)))
  4014. ((equal key "DRAWERS")
  4015. (setq drawers (org-split-string value splitre)))
  4016. ((equal key "CONSTANTS")
  4017. (setq const (append const (org-split-string value splitre))))
  4018. ((equal key "STARTUP")
  4019. (let ((opts (org-split-string value splitre))
  4020. l var val)
  4021. (while (setq l (pop opts))
  4022. (when (setq l (assoc l org-startup-options))
  4023. (setq var (nth 1 l) val (nth 2 l))
  4024. (if (not (nth 3 l))
  4025. (set (make-local-variable var) val)
  4026. (if (not (listp (symbol-value var)))
  4027. (set (make-local-variable var) nil))
  4028. (set (make-local-variable var) (symbol-value var))
  4029. (add-to-list var val))))))
  4030. ((equal key "ARCHIVE")
  4031. (string-match " *$" value)
  4032. (setq arch (replace-match "" t t value))
  4033. (remove-text-properties 0 (length arch)
  4034. '(face t fontified t) arch)))
  4035. )))
  4036. (when cat
  4037. (org-set-local 'org-category (intern cat))
  4038. (push (cons "CATEGORY" cat) props))
  4039. (when prio
  4040. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4041. (setq prio (mapcar 'string-to-char prio))
  4042. (org-set-local 'org-highest-priority (nth 0 prio))
  4043. (org-set-local 'org-lowest-priority (nth 1 prio))
  4044. (org-set-local 'org-default-priority (nth 2 prio)))
  4045. (and props (org-set-local 'org-local-properties (nreverse props)))
  4046. (and drawers (org-set-local 'org-drawers drawers))
  4047. (and arch (org-set-local 'org-archive-location arch))
  4048. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4049. ;; Process the TODO keywords
  4050. (unless kwds
  4051. ;; Use the global values as if they had been given locally.
  4052. (setq kwds (default-value 'org-todo-keywords))
  4053. (if (stringp (car kwds))
  4054. (setq kwds (list (cons org-todo-interpretation
  4055. (default-value 'org-todo-keywords)))))
  4056. (setq kwds (reverse kwds)))
  4057. (setq kwds (nreverse kwds))
  4058. (let (inter kws kw)
  4059. (while (setq kws (pop kwds))
  4060. (setq inter (pop kws) sep (member "|" kws)
  4061. kws0 (delete "|" (copy-sequence kws))
  4062. kwsa nil
  4063. kws1 (mapcar
  4064. (lambda (x)
  4065. ;; 1 2
  4066. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4067. (progn
  4068. (setq kw (match-string 1 x)
  4069. key (and (match-end 2) (match-string 2 x))
  4070. log (org-extract-log-state-settings x))
  4071. (push (cons kw (and key (string-to-char key))) kwsa)
  4072. (and log (push log org-todo-log-states))
  4073. kw)
  4074. (error "Invalid TODO keyword %s" x)))
  4075. kws0)
  4076. kwsa (if kwsa (append '((:startgroup))
  4077. (nreverse kwsa)
  4078. '((:endgroup))))
  4079. hw (car kws1)
  4080. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4081. tail (list inter hw (car dws) (org-last dws)))
  4082. (add-to-list 'org-todo-heads hw 'append)
  4083. (push kws1 org-todo-sets)
  4084. (setq org-done-keywords (append org-done-keywords dws nil))
  4085. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4086. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4087. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4088. (setq org-todo-sets (nreverse org-todo-sets)
  4089. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4090. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4091. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4092. ;; Process the constants
  4093. (when const
  4094. (let (e cst)
  4095. (while (setq e (pop const))
  4096. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4097. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4098. (setq org-table-formula-constants-local cst)))
  4099. ;; Process the tags.
  4100. (when tags
  4101. (let (e tgs)
  4102. (while (setq e (pop tags))
  4103. (cond
  4104. ((equal e "{") (push '(:startgroup) tgs))
  4105. ((equal e "}") (push '(:endgroup) tgs))
  4106. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4107. (push (cons (match-string 1 e)
  4108. (string-to-char (match-string 2 e)))
  4109. tgs))
  4110. (t (push (list e) tgs))))
  4111. (org-set-local 'org-tag-alist nil)
  4112. (while (setq e (pop tgs))
  4113. (or (and (stringp (car e))
  4114. (assoc (car e) org-tag-alist))
  4115. (push e org-tag-alist))))))
  4116. ;; Compute the regular expressions and other local variables
  4117. (if (not org-done-keywords)
  4118. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4119. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4120. (length org-scheduled-string)))
  4121. org-drawer-regexp
  4122. (concat "^[ \t]*:\\("
  4123. (mapconcat 'regexp-quote org-drawers "\\|")
  4124. "\\):[ \t]*$")
  4125. org-not-done-keywords
  4126. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4127. org-todo-regexp
  4128. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4129. "\\|") "\\)\\>")
  4130. org-not-done-regexp
  4131. (concat "\\<\\("
  4132. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4133. "\\)\\>")
  4134. org-todo-line-regexp
  4135. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4136. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4137. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4138. org-complex-heading-regexp
  4139. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4140. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4141. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4142. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4143. org-nl-done-regexp
  4144. (concat "\n\\*+[ \t]+"
  4145. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4146. "\\)" "\\>")
  4147. org-todo-line-tags-regexp
  4148. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4149. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4150. (org-re
  4151. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4152. org-looking-at-done-regexp
  4153. (concat "^" "\\(?:"
  4154. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4155. "\\>")
  4156. org-deadline-regexp (concat "\\<" org-deadline-string)
  4157. org-deadline-time-regexp
  4158. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4159. org-deadline-line-regexp
  4160. (concat "\\<\\(" org-deadline-string "\\).*")
  4161. org-scheduled-regexp
  4162. (concat "\\<" org-scheduled-string)
  4163. org-scheduled-time-regexp
  4164. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4165. org-closed-time-regexp
  4166. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4167. org-keyword-time-regexp
  4168. (concat "\\<\\(" org-scheduled-string
  4169. "\\|" org-deadline-string
  4170. "\\|" org-closed-string
  4171. "\\|" org-clock-string "\\)"
  4172. " *[[<]\\([^]>]+\\)[]>]")
  4173. org-keyword-time-not-clock-regexp
  4174. (concat "\\<\\(" org-scheduled-string
  4175. "\\|" org-deadline-string
  4176. "\\|" org-closed-string
  4177. "\\)"
  4178. " *[[<]\\([^]>]+\\)[]>]")
  4179. org-maybe-keyword-time-regexp
  4180. (concat "\\(\\<\\(" org-scheduled-string
  4181. "\\|" org-deadline-string
  4182. "\\|" org-closed-string
  4183. "\\|" org-clock-string "\\)\\)?"
  4184. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4185. org-planning-or-clock-line-re
  4186. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4187. "\\|" org-deadline-string
  4188. "\\|" org-closed-string "\\|" org-clock-string
  4189. "\\)\\>\\)")
  4190. )
  4191. (org-compute-latex-and-specials-regexp)
  4192. (org-set-font-lock-defaults)))
  4193. (defun org-extract-log-state-settings (x)
  4194. "Extract the log state setting from a TODO keyword string.
  4195. This will extract info from a string like \"WAIT(w@/!)\"."
  4196. (let (kw key log1 log2)
  4197. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4198. (setq kw (match-string 1 x)
  4199. key (and (match-end 2) (match-string 2 x))
  4200. log1 (and (match-end 3) (match-string 3 x))
  4201. log2 (and (match-end 4) (match-string 4 x)))
  4202. (and (or log1 log2)
  4203. (list kw
  4204. (and log1 (if (equal log1 "!") 'time 'note))
  4205. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4206. (defun org-remove-keyword-keys (list)
  4207. "Remove a pair of parenthesis at the end of each string in LIST."
  4208. (mapcar (lambda (x)
  4209. (if (string-match "(.*)$" x)
  4210. (substring x 0 (match-beginning 0))
  4211. x))
  4212. list))
  4213. ;; FIXME: this could be done much better, using second characters etc.
  4214. (defun org-assign-fast-keys (alist)
  4215. "Assign fast keys to a keyword-key alist.
  4216. Respect keys that are already there."
  4217. (let (new e k c c1 c2 (char ?a))
  4218. (while (setq e (pop alist))
  4219. (cond
  4220. ((equal e '(:startgroup)) (push e new))
  4221. ((equal e '(:endgroup)) (push e new))
  4222. (t
  4223. (setq k (car e) c2 nil)
  4224. (if (cdr e)
  4225. (setq c (cdr e))
  4226. ;; automatically assign a character.
  4227. (setq c1 (string-to-char
  4228. (downcase (substring
  4229. k (if (= (string-to-char k) ?@) 1 0)))))
  4230. (if (or (rassoc c1 new) (rassoc c1 alist))
  4231. (while (or (rassoc char new) (rassoc char alist))
  4232. (setq char (1+ char)))
  4233. (setq c2 c1))
  4234. (setq c (or c2 char)))
  4235. (push (cons k c) new))))
  4236. (nreverse new)))
  4237. ;;; Some variables ujsed in various places
  4238. (defvar org-window-configuration nil
  4239. "Used in various places to store a window configuration.")
  4240. (defvar org-finish-function nil
  4241. "Function to be called when `C-c C-c' is used.
  4242. This is for getting out of special buffers like remember.")
  4243. ;; FIXME: Occasionally check by commenting these, to make sure
  4244. ;; no other functions uses these, forgetting to let-bind them.
  4245. (defvar entry)
  4246. (defvar state)
  4247. (defvar last-state)
  4248. (defvar date)
  4249. (defvar description)
  4250. ;; Defined somewhere in this file, but used before definition.
  4251. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4252. (defvar org-agenda-buffer-name)
  4253. (defvar org-agenda-undo-list)
  4254. (defvar org-agenda-pending-undo-list)
  4255. (defvar org-agenda-overriding-header)
  4256. (defvar orgtbl-mode)
  4257. (defvar org-html-entities)
  4258. (defvar org-struct-menu)
  4259. (defvar org-org-menu)
  4260. (defvar org-tbl-menu)
  4261. (defvar org-agenda-keymap)
  4262. ;;;; Emacs/XEmacs compatibility
  4263. ;; Overlay compatibility functions
  4264. (defun org-make-overlay (beg end &optional buffer)
  4265. (if (featurep 'xemacs)
  4266. (make-extent beg end buffer)
  4267. (make-overlay beg end buffer)))
  4268. (defun org-delete-overlay (ovl)
  4269. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4270. (defun org-detach-overlay (ovl)
  4271. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4272. (defun org-move-overlay (ovl beg end &optional buffer)
  4273. (if (featurep 'xemacs)
  4274. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4275. (move-overlay ovl beg end buffer)))
  4276. (defun org-overlay-put (ovl prop value)
  4277. (if (featurep 'xemacs)
  4278. (set-extent-property ovl prop value)
  4279. (overlay-put ovl prop value)))
  4280. (defun org-overlay-display (ovl text &optional face evap)
  4281. "Make overlay OVL display TEXT with face FACE."
  4282. (if (featurep 'xemacs)
  4283. (let ((gl (make-glyph text)))
  4284. (and face (set-glyph-face gl face))
  4285. (set-extent-property ovl 'invisible t)
  4286. (set-extent-property ovl 'end-glyph gl))
  4287. (overlay-put ovl 'display text)
  4288. (if face (overlay-put ovl 'face face))
  4289. (if evap (overlay-put ovl 'evaporate t))))
  4290. (defun org-overlay-before-string (ovl text &optional face evap)
  4291. "Make overlay OVL display TEXT with face FACE."
  4292. (if (featurep 'xemacs)
  4293. (let ((gl (make-glyph text)))
  4294. (and face (set-glyph-face gl face))
  4295. (set-extent-property ovl 'begin-glyph gl))
  4296. (if face (org-add-props text nil 'face face))
  4297. (overlay-put ovl 'before-string text)
  4298. (if evap (overlay-put ovl 'evaporate t))))
  4299. (defun org-overlay-get (ovl prop)
  4300. (if (featurep 'xemacs)
  4301. (extent-property ovl prop)
  4302. (overlay-get ovl prop)))
  4303. (defun org-overlays-at (pos)
  4304. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4305. (defun org-overlays-in (&optional start end)
  4306. (if (featurep 'xemacs)
  4307. (extent-list nil start end)
  4308. (overlays-in start end)))
  4309. (defun org-overlay-start (o)
  4310. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4311. (defun org-overlay-end (o)
  4312. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4313. (defun org-find-overlays (prop &optional pos delete)
  4314. "Find all overlays specifying PROP at POS or point.
  4315. If DELETE is non-nil, delete all those overlays."
  4316. (let ((overlays (org-overlays-at (or pos (point))))
  4317. ov found)
  4318. (while (setq ov (pop overlays))
  4319. (if (org-overlay-get ov prop)
  4320. (if delete (org-delete-overlay ov) (push ov found))))
  4321. found))
  4322. ;; Region compatibility
  4323. (defun org-add-hook (hook function &optional append local)
  4324. "Add-hook, compatible with both Emacsen."
  4325. (if (and local (featurep 'xemacs))
  4326. (add-local-hook hook function append)
  4327. (add-hook hook function append local)))
  4328. (defvar org-ignore-region nil
  4329. "To temporarily disable the active region.")
  4330. (defun org-region-active-p ()
  4331. "Is `transient-mark-mode' on and the region active?
  4332. Works on both Emacs and XEmacs."
  4333. (if org-ignore-region
  4334. nil
  4335. (if (featurep 'xemacs)
  4336. (and zmacs-regions (region-active-p))
  4337. (if (fboundp 'use-region-p)
  4338. (use-region-p)
  4339. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4340. ;; Invisibility compatibility
  4341. (defun org-add-to-invisibility-spec (arg)
  4342. "Add elements to `buffer-invisibility-spec'.
  4343. See documentation for `buffer-invisibility-spec' for the kind of elements
  4344. that can be added."
  4345. (cond
  4346. ((fboundp 'add-to-invisibility-spec)
  4347. (add-to-invisibility-spec arg))
  4348. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4349. (setq buffer-invisibility-spec (list arg)))
  4350. (t
  4351. (setq buffer-invisibility-spec
  4352. (cons arg buffer-invisibility-spec)))))
  4353. (defun org-remove-from-invisibility-spec (arg)
  4354. "Remove elements from `buffer-invisibility-spec'."
  4355. (if (fboundp 'remove-from-invisibility-spec)
  4356. (remove-from-invisibility-spec arg)
  4357. (if (consp buffer-invisibility-spec)
  4358. (setq buffer-invisibility-spec
  4359. (delete arg buffer-invisibility-spec)))))
  4360. (defun org-in-invisibility-spec-p (arg)
  4361. "Is ARG a member of `buffer-invisibility-spec'?"
  4362. (if (consp buffer-invisibility-spec)
  4363. (member arg buffer-invisibility-spec)
  4364. nil))
  4365. ;;;; Define the Org-mode
  4366. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4367. (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."))
  4368. ;; We use a before-change function to check if a table might need
  4369. ;; an update.
  4370. (defvar org-table-may-need-update t
  4371. "Indicates that a table might need an update.
  4372. This variable is set by `org-before-change-function'.
  4373. `org-table-align' sets it back to nil.")
  4374. (defvar org-mode-map)
  4375. (defvar org-mode-hook nil)
  4376. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4377. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4378. (defvar org-table-buffer-is-an nil)
  4379. (defconst org-outline-regexp "\\*+ ")
  4380. ;;;###autoload
  4381. (define-derived-mode org-mode outline-mode "Org"
  4382. "Outline-based notes management and organizer, alias
  4383. \"Carsten's outline-mode for keeping track of everything.\"
  4384. Org-mode develops organizational tasks around a NOTES file which
  4385. contains information about projects as plain text. Org-mode is
  4386. implemented on top of outline-mode, which is ideal to keep the content
  4387. of large files well structured. It supports ToDo items, deadlines and
  4388. time stamps, which magically appear in the diary listing of the Emacs
  4389. calendar. Tables are easily created with a built-in table editor.
  4390. Plain text URL-like links connect to websites, emails (VM), Usenet
  4391. messages (Gnus), BBDB entries, and any files related to the project.
  4392. For printing and sharing of notes, an Org-mode file (or a part of it)
  4393. can be exported as a structured ASCII or HTML file.
  4394. The following commands are available:
  4395. \\{org-mode-map}"
  4396. ;; Get rid of Outline menus, they are not needed
  4397. ;; Need to do this here because define-derived-mode sets up
  4398. ;; the keymap so late. Still, it is a waste to call this each time
  4399. ;; we switch another buffer into org-mode.
  4400. (if (featurep 'xemacs)
  4401. (when (boundp 'outline-mode-menu-heading)
  4402. ;; Assume this is Greg's port, it used easymenu
  4403. (easy-menu-remove outline-mode-menu-heading)
  4404. (easy-menu-remove outline-mode-menu-show)
  4405. (easy-menu-remove outline-mode-menu-hide))
  4406. (define-key org-mode-map [menu-bar headings] 'undefined)
  4407. (define-key org-mode-map [menu-bar hide] 'undefined)
  4408. (define-key org-mode-map [menu-bar show] 'undefined))
  4409. (easy-menu-add org-org-menu)
  4410. (easy-menu-add org-tbl-menu)
  4411. (org-install-agenda-files-menu)
  4412. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4413. (org-add-to-invisibility-spec '(org-cwidth))
  4414. (when (featurep 'xemacs)
  4415. (org-set-local 'line-move-ignore-invisible t))
  4416. (org-set-local 'outline-regexp org-outline-regexp)
  4417. (org-set-local 'outline-level 'org-outline-level)
  4418. (when (and org-ellipsis
  4419. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4420. (fboundp 'make-glyph-code))
  4421. (unless org-display-table
  4422. (setq org-display-table (make-display-table)))
  4423. (set-display-table-slot
  4424. org-display-table 4
  4425. (vconcat (mapcar
  4426. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4427. org-ellipsis)))
  4428. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4429. (setq buffer-display-table org-display-table))
  4430. (org-set-regexps-and-options)
  4431. ;; Calc embedded
  4432. (org-set-local 'calc-embedded-open-mode "# ")
  4433. (modify-syntax-entry ?# "<")
  4434. (modify-syntax-entry ?@ "w")
  4435. (if org-startup-truncated (setq truncate-lines t))
  4436. (org-set-local 'font-lock-unfontify-region-function
  4437. 'org-unfontify-region)
  4438. ;; Activate before-change-function
  4439. (org-set-local 'org-table-may-need-update t)
  4440. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4441. 'local)
  4442. ;; Check for running clock before killing a buffer
  4443. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4444. ;; Paragraphs and auto-filling
  4445. (org-set-autofill-regexps)
  4446. (setq indent-line-function 'org-indent-line-function)
  4447. (org-update-radio-target-regexp)
  4448. ;; Comment characters
  4449. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4450. (org-set-local 'comment-padding " ")
  4451. ;; Align options lines
  4452. (org-set-local
  4453. 'align-mode-rules-list
  4454. '((org-in-buffer-settings
  4455. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4456. (modes . '(org-mode)))))
  4457. ;; Imenu
  4458. (org-set-local 'imenu-create-index-function
  4459. 'org-imenu-get-tree)
  4460. ;; Make isearch reveal context
  4461. (if (or (featurep 'xemacs)
  4462. (not (boundp 'outline-isearch-open-invisible-function)))
  4463. ;; Emacs 21 and XEmacs make use of the hook
  4464. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4465. ;; Emacs 22 deals with this through a special variable
  4466. (org-set-local 'outline-isearch-open-invisible-function
  4467. (lambda (&rest ignore) (org-show-context 'isearch))))
  4468. ;; If empty file that did not turn on org-mode automatically, make it to.
  4469. (if (and org-insert-mode-line-in-empty-file
  4470. (interactive-p)
  4471. (= (point-min) (point-max)))
  4472. (insert "# -*- mode: org -*-\n\n"))
  4473. (unless org-inhibit-startup
  4474. (when org-startup-align-all-tables
  4475. (let ((bmp (buffer-modified-p)))
  4476. (org-table-map-tables 'org-table-align)
  4477. (set-buffer-modified-p bmp)))
  4478. (org-cycle-hide-drawers 'all)
  4479. (cond
  4480. ((eq org-startup-folded t)
  4481. (org-cycle '(4)))
  4482. ((eq org-startup-folded 'content)
  4483. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4484. (org-cycle '(4)) (org-cycle '(4)))))))
  4485. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4486. (defsubst org-call-with-arg (command arg)
  4487. "Call COMMAND interactively, but pretend prefix are was ARG."
  4488. (let ((current-prefix-arg arg)) (call-interactively command)))
  4489. (defsubst org-current-line (&optional pos)
  4490. (save-excursion
  4491. (and pos (goto-char pos))
  4492. ;; works also in narrowed buffer, because we start at 1, not point-min
  4493. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4494. (defun org-current-time ()
  4495. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4496. (if (> (car org-time-stamp-rounding-minutes) 1)
  4497. (let ((r (car org-time-stamp-rounding-minutes))
  4498. (time (decode-time)))
  4499. (apply 'encode-time
  4500. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4501. (nthcdr 2 time))))
  4502. (current-time)))
  4503. (defun org-add-props (string plist &rest props)
  4504. "Add text properties to entire string, from beginning to end.
  4505. PLIST may be a list of properties, PROPS are individual properties and values
  4506. that will be added to PLIST. Returns the string that was modified."
  4507. (add-text-properties
  4508. 0 (length string) (if props (append plist props) plist) string)
  4509. string)
  4510. (put 'org-add-props 'lisp-indent-function 2)
  4511. ;;;; Font-Lock stuff, including the activators
  4512. (defvar org-mouse-map (make-sparse-keymap))
  4513. (org-defkey org-mouse-map
  4514. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4515. (org-defkey org-mouse-map
  4516. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4517. (when org-mouse-1-follows-link
  4518. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4519. (when org-tab-follows-link
  4520. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4521. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4522. (when org-return-follows-link
  4523. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4524. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4525. (require 'font-lock)
  4526. (defconst org-non-link-chars "]\t\n\r<>")
  4527. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4528. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4529. (defvar org-link-re-with-space nil
  4530. "Matches a link with spaces, optional angular brackets around it.")
  4531. (defvar org-link-re-with-space2 nil
  4532. "Matches a link with spaces, optional angular brackets around it.")
  4533. (defvar org-angle-link-re nil
  4534. "Matches link with angular brackets, spaces are allowed.")
  4535. (defvar org-plain-link-re nil
  4536. "Matches plain link, without spaces.")
  4537. (defvar org-bracket-link-regexp nil
  4538. "Matches a link in double brackets.")
  4539. (defvar org-bracket-link-analytic-regexp nil
  4540. "Regular expression used to analyze links.
  4541. Here is what the match groups contain after a match:
  4542. 1: http:
  4543. 2: http
  4544. 3: path
  4545. 4: [desc]
  4546. 5: desc")
  4547. (defvar org-any-link-re nil
  4548. "Regular expression matching any link.")
  4549. (defun org-make-link-regexps ()
  4550. "Update the link regular expressions.
  4551. This should be called after the variable `org-link-types' has changed."
  4552. (setq org-link-re-with-space
  4553. (concat
  4554. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4555. "\\([^" org-non-link-chars " ]"
  4556. "[^" org-non-link-chars "]*"
  4557. "[^" org-non-link-chars " ]\\)>?")
  4558. org-link-re-with-space2
  4559. (concat
  4560. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4561. "\\([^" org-non-link-chars " ]"
  4562. "[^]\t\n\r]*"
  4563. "[^" org-non-link-chars " ]\\)>?")
  4564. org-angle-link-re
  4565. (concat
  4566. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4567. "\\([^" org-non-link-chars " ]"
  4568. "[^" org-non-link-chars "]*"
  4569. "\\)>")
  4570. org-plain-link-re
  4571. (concat
  4572. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4573. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4574. org-bracket-link-regexp
  4575. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4576. org-bracket-link-analytic-regexp
  4577. (concat
  4578. "\\[\\["
  4579. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4580. "\\([^]]+\\)"
  4581. "\\]"
  4582. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4583. "\\]")
  4584. org-any-link-re
  4585. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4586. org-angle-link-re "\\)\\|\\("
  4587. org-plain-link-re "\\)")))
  4588. (org-make-link-regexps)
  4589. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4590. "Regular expression for fast time stamp matching.")
  4591. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4592. "Regular expression for fast time stamp matching.")
  4593. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4594. "Regular expression matching time strings for analysis.
  4595. This one does not require the space after the date.")
  4596. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4597. "Regular expression matching time strings for analysis.")
  4598. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4599. "Regular expression matching time stamps, with groups.")
  4600. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4601. "Regular expression matching time stamps (also [..]), with groups.")
  4602. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4603. "Regular expression matching a time stamp range.")
  4604. (defconst org-tr-regexp-both
  4605. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4606. "Regular expression matching a time stamp range.")
  4607. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4608. org-ts-regexp "\\)?")
  4609. "Regular expression matching a time stamp or time stamp range.")
  4610. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4611. org-ts-regexp-both "\\)?")
  4612. "Regular expression matching a time stamp or time stamp range.
  4613. The time stamps may be either active or inactive.")
  4614. (defvar org-emph-face nil)
  4615. (defun org-do-emphasis-faces (limit)
  4616. "Run through the buffer and add overlays to links."
  4617. (let (rtn)
  4618. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4619. (if (not (= (char-after (match-beginning 3))
  4620. (char-after (match-beginning 4))))
  4621. (progn
  4622. (setq rtn t)
  4623. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4624. 'face
  4625. (nth 1 (assoc (match-string 3)
  4626. org-emphasis-alist)))
  4627. (add-text-properties (match-beginning 2) (match-end 2)
  4628. '(font-lock-multiline t))
  4629. (when org-hide-emphasis-markers
  4630. (add-text-properties (match-end 4) (match-beginning 5)
  4631. '(invisible org-link))
  4632. (add-text-properties (match-beginning 3) (match-end 3)
  4633. '(invisible org-link)))))
  4634. (backward-char 1))
  4635. rtn))
  4636. (defun org-emphasize (&optional char)
  4637. "Insert or change an emphasis, i.e. a font like bold or italic.
  4638. If there is an active region, change that region to a new emphasis.
  4639. If there is no region, just insert the marker characters and position
  4640. the cursor between them.
  4641. CHAR should be either the marker character, or the first character of the
  4642. HTML tag associated with that emphasis. If CHAR is a space, the means
  4643. to remove the emphasis of the selected region.
  4644. If char is not given (for example in an interactive call) it
  4645. will be prompted for."
  4646. (interactive)
  4647. (let ((eal org-emphasis-alist) e det
  4648. (erc org-emphasis-regexp-components)
  4649. (prompt "")
  4650. (string "") beg end move tag c s)
  4651. (if (org-region-active-p)
  4652. (setq beg (region-beginning) end (region-end)
  4653. string (buffer-substring beg end))
  4654. (setq move t))
  4655. (while (setq e (pop eal))
  4656. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4657. c (aref tag 0))
  4658. (push (cons c (string-to-char (car e))) det)
  4659. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4660. (substring tag 1)))))
  4661. (unless char
  4662. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4663. (setq char (read-char-exclusive)))
  4664. (setq char (or (cdr (assoc char det)) char))
  4665. (if (equal char ?\ )
  4666. (setq s "" move nil)
  4667. (unless (assoc (char-to-string char) org-emphasis-alist)
  4668. (error "No such emphasis marker: \"%c\"" char))
  4669. (setq s (char-to-string char)))
  4670. (while (and (> (length string) 1)
  4671. (equal (substring string 0 1) (substring string -1))
  4672. (assoc (substring string 0 1) org-emphasis-alist))
  4673. (setq string (substring string 1 -1)))
  4674. (setq string (concat s string s))
  4675. (if beg (delete-region beg end))
  4676. (unless (or (bolp)
  4677. (string-match (concat "[" (nth 0 erc) "\n]")
  4678. (char-to-string (char-before (point)))))
  4679. (insert " "))
  4680. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4681. (char-to-string (char-after (point))))
  4682. (insert " ") (backward-char 1))
  4683. (insert string)
  4684. (and move (backward-char 1))))
  4685. (defconst org-nonsticky-props
  4686. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4687. (defun org-activate-plain-links (limit)
  4688. "Run through the buffer and add overlays to links."
  4689. (catch 'exit
  4690. (let (f)
  4691. (while (re-search-forward org-plain-link-re limit t)
  4692. (setq f (get-text-property (match-beginning 0) 'face))
  4693. (if (or (eq f 'org-tag)
  4694. (and (listp f) (memq 'org-tag f)))
  4695. nil
  4696. (add-text-properties (match-beginning 0) (match-end 0)
  4697. (list 'mouse-face 'highlight
  4698. 'rear-nonsticky org-nonsticky-props
  4699. 'keymap org-mouse-map
  4700. ))
  4701. (throw 'exit t))))))
  4702. (defun org-activate-code (limit)
  4703. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4704. (unless (get-text-property (match-beginning 1) 'face)
  4705. (remove-text-properties (match-beginning 0) (match-end 0)
  4706. '(display t invisible t intangible t))
  4707. t)))
  4708. (defun org-activate-angle-links (limit)
  4709. "Run through the buffer and add overlays to links."
  4710. (if (re-search-forward org-angle-link-re limit t)
  4711. (progn
  4712. (add-text-properties (match-beginning 0) (match-end 0)
  4713. (list 'mouse-face 'highlight
  4714. 'rear-nonsticky org-nonsticky-props
  4715. 'keymap org-mouse-map
  4716. ))
  4717. t)))
  4718. (defmacro org-maybe-intangible (props)
  4719. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4720. In emacs 21, invisible text is not avoided by the command loop, so the
  4721. intangible property is needed to make sure point skips this text.
  4722. In Emacs 22, this is not necessary. The intangible text property has
  4723. led to problems with flyspell. These problems are fixed in flyspell.el,
  4724. but we still avoid setting the property in Emacs 22 and later.
  4725. We use a macro so that the test can happen at compilation time."
  4726. (if (< emacs-major-version 22)
  4727. `(append '(intangible t) ,props)
  4728. props))
  4729. (defun org-activate-bracket-links (limit)
  4730. "Run through the buffer and add overlays to bracketed links."
  4731. (if (re-search-forward org-bracket-link-regexp limit t)
  4732. (let* ((help (concat "LINK: "
  4733. (org-match-string-no-properties 1)))
  4734. ;; FIXME: above we should remove the escapes.
  4735. ;; but that requires another match, protecting match data,
  4736. ;; a lot of overhead for font-lock.
  4737. (ip (org-maybe-intangible
  4738. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4739. 'keymap org-mouse-map 'mouse-face 'highlight
  4740. 'font-lock-multiline t 'help-echo help)))
  4741. (vp (list 'rear-nonsticky org-nonsticky-props
  4742. 'keymap org-mouse-map 'mouse-face 'highlight
  4743. ' font-lock-multiline t 'help-echo help)))
  4744. ;; We need to remove the invisible property here. Table narrowing
  4745. ;; may have made some of this invisible.
  4746. (remove-text-properties (match-beginning 0) (match-end 0)
  4747. '(invisible nil))
  4748. (if (match-end 3)
  4749. (progn
  4750. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4751. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4752. (add-text-properties (match-end 3) (match-end 0) ip))
  4753. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4754. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4755. (add-text-properties (match-end 1) (match-end 0) ip))
  4756. t)))
  4757. (defun org-activate-dates (limit)
  4758. "Run through the buffer and add overlays to dates."
  4759. (if (re-search-forward org-tsr-regexp-both limit t)
  4760. (progn
  4761. (add-text-properties (match-beginning 0) (match-end 0)
  4762. (list 'mouse-face 'highlight
  4763. 'rear-nonsticky org-nonsticky-props
  4764. 'keymap org-mouse-map))
  4765. (when org-display-custom-times
  4766. (if (match-end 3)
  4767. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4768. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4769. t)))
  4770. (defvar org-target-link-regexp nil
  4771. "Regular expression matching radio targets in plain text.")
  4772. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4773. "Regular expression matching a link target.")
  4774. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4775. "Regular expression matching a radio target.")
  4776. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4777. "Regular expression matching any target.")
  4778. (defun org-activate-target-links (limit)
  4779. "Run through the buffer and add overlays to target matches."
  4780. (when org-target-link-regexp
  4781. (let ((case-fold-search t))
  4782. (if (re-search-forward org-target-link-regexp limit t)
  4783. (progn
  4784. (add-text-properties (match-beginning 0) (match-end 0)
  4785. (list 'mouse-face 'highlight
  4786. 'rear-nonsticky org-nonsticky-props
  4787. 'keymap org-mouse-map
  4788. 'help-echo "Radio target link"
  4789. 'org-linked-text t))
  4790. t)))))
  4791. (defun org-update-radio-target-regexp ()
  4792. "Find all radio targets in this file and update the regular expression."
  4793. (interactive)
  4794. (when (memq 'radio org-activate-links)
  4795. (setq org-target-link-regexp
  4796. (org-make-target-link-regexp (org-all-targets 'radio)))
  4797. (org-restart-font-lock)))
  4798. (defun org-hide-wide-columns (limit)
  4799. (let (s e)
  4800. (setq s (text-property-any (point) (or limit (point-max))
  4801. 'org-cwidth t))
  4802. (when s
  4803. (setq e (next-single-property-change s 'org-cwidth))
  4804. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4805. (goto-char e)
  4806. t)))
  4807. (defvar org-latex-and-specials-regexp nil
  4808. "Regular expression for highlighting export special stuff.")
  4809. (defvar org-match-substring-regexp)
  4810. (defvar org-match-substring-with-braces-regexp)
  4811. (defvar org-export-html-special-string-regexps)
  4812. (defun org-compute-latex-and-specials-regexp ()
  4813. "Compute regular expression for stuff treated specially by exporters."
  4814. (if (not org-highlight-latex-fragments-and-specials)
  4815. (org-set-local 'org-latex-and-specials-regexp nil)
  4816. (let*
  4817. ((matchers (plist-get org-format-latex-options :matchers))
  4818. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4819. org-latex-regexps)))
  4820. (options (org-combine-plists (org-default-export-plist)
  4821. (org-infile-export-plist)))
  4822. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4823. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4824. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4825. (org-export-html-expand (plist-get options :expand-quoted-html))
  4826. (org-export-with-special-strings (plist-get options :special-strings))
  4827. (re-sub
  4828. (cond
  4829. ((equal org-export-with-sub-superscripts '{})
  4830. (list org-match-substring-with-braces-regexp))
  4831. (org-export-with-sub-superscripts
  4832. (list org-match-substring-regexp))
  4833. (t nil)))
  4834. (re-latex
  4835. (if org-export-with-LaTeX-fragments
  4836. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4837. (re-macros
  4838. (if org-export-with-TeX-macros
  4839. (list (concat "\\\\"
  4840. (regexp-opt
  4841. (append (mapcar 'car org-html-entities)
  4842. (if (boundp 'org-latex-entities)
  4843. org-latex-entities nil))
  4844. 'words))) ; FIXME
  4845. ))
  4846. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4847. (re-special (if org-export-with-special-strings
  4848. (mapcar (lambda (x) (car x))
  4849. org-export-html-special-string-regexps)))
  4850. (re-rest
  4851. (delq nil
  4852. (list
  4853. (if org-export-html-expand "@<[^>\n]+>")
  4854. ))))
  4855. (org-set-local
  4856. 'org-latex-and-specials-regexp
  4857. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4858. re-rest) "\\|")))))
  4859. (defface org-latex-and-export-specials
  4860. (let ((font (cond ((assq :inherit custom-face-attributes)
  4861. '(:inherit underline))
  4862. (t '(:underline t)))))
  4863. `((((class grayscale) (background light))
  4864. (:foreground "DimGray" ,@font))
  4865. (((class grayscale) (background dark))
  4866. (:foreground "LightGray" ,@font))
  4867. (((class color) (background light))
  4868. (:foreground "SaddleBrown"))
  4869. (((class color) (background dark))
  4870. (:foreground "burlywood"))
  4871. (t (,@font))))
  4872. "Face used to highlight math latex and other special exporter stuff."
  4873. :group 'org-faces)
  4874. (defun org-do-latex-and-special-faces (limit)
  4875. "Run through the buffer and add overlays to links."
  4876. (when org-latex-and-specials-regexp
  4877. (let (rtn d)
  4878. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4879. limit t))
  4880. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4881. 'face))
  4882. '(org-code org-verbatim underline)))
  4883. (progn
  4884. (setq rtn t
  4885. d (cond ((member (char-after (1+ (match-beginning 0)))
  4886. '(?_ ?^)) 1)
  4887. (t 0)))
  4888. (font-lock-prepend-text-property
  4889. (+ d (match-beginning 0)) (match-end 0)
  4890. 'face 'org-latex-and-export-specials)
  4891. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4892. '(font-lock-multiline t)))))
  4893. rtn)))
  4894. (defun org-restart-font-lock ()
  4895. "Restart font-lock-mode, to force refontification."
  4896. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4897. (font-lock-mode -1)
  4898. (font-lock-mode 1)))
  4899. (defun org-all-targets (&optional radio)
  4900. "Return a list of all targets in this file.
  4901. With optional argument RADIO, only find radio targets."
  4902. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4903. rtn)
  4904. (save-excursion
  4905. (goto-char (point-min))
  4906. (while (re-search-forward re nil t)
  4907. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4908. rtn)))
  4909. (defun org-make-target-link-regexp (targets)
  4910. "Make regular expression matching all strings in TARGETS.
  4911. The regular expression finds the targets also if there is a line break
  4912. between words."
  4913. (and targets
  4914. (concat
  4915. "\\<\\("
  4916. (mapconcat
  4917. (lambda (x)
  4918. (while (string-match " +" x)
  4919. (setq x (replace-match "\\s-+" t t x)))
  4920. x)
  4921. targets
  4922. "\\|")
  4923. "\\)\\>")))
  4924. (defun org-activate-tags (limit)
  4925. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4926. (progn
  4927. (add-text-properties (match-beginning 1) (match-end 1)
  4928. (list 'mouse-face 'highlight
  4929. 'rear-nonsticky org-nonsticky-props
  4930. 'keymap org-mouse-map))
  4931. t)))
  4932. (defun org-outline-level ()
  4933. (save-excursion
  4934. (looking-at outline-regexp)
  4935. (if (match-beginning 1)
  4936. (+ (org-get-string-indentation (match-string 1)) 1000)
  4937. (1- (- (match-end 0) (match-beginning 0))))))
  4938. (defvar org-font-lock-keywords nil)
  4939. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4940. "Regular expression matching a property line.")
  4941. (defun org-set-font-lock-defaults ()
  4942. (let* ((em org-fontify-emphasized-text)
  4943. (lk org-activate-links)
  4944. (org-font-lock-extra-keywords
  4945. (list
  4946. ;; Headlines
  4947. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4948. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4949. ;; Table lines
  4950. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4951. (1 'org-table t))
  4952. ;; Table internals
  4953. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4954. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4955. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4956. ;; Drawers
  4957. (list org-drawer-regexp '(0 'org-special-keyword t))
  4958. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4959. ;; Properties
  4960. (list org-property-re
  4961. '(1 'org-special-keyword t)
  4962. '(3 'org-property-value t))
  4963. (if org-format-transports-properties-p
  4964. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4965. ;; Links
  4966. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4967. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4968. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4969. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4970. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4971. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4972. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4973. '(org-hide-wide-columns (0 nil append))
  4974. ;; TODO lines
  4975. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4976. '(1 (org-get-todo-face 1) t))
  4977. ;; DONE
  4978. (if org-fontify-done-headline
  4979. (list (concat "^[*]+ +\\<\\("
  4980. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4981. "\\)\\(.*\\)")
  4982. '(2 'org-headline-done t))
  4983. nil)
  4984. ;; Priorities
  4985. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4986. ;; Special keywords
  4987. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4988. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4989. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4990. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4991. ;; Emphasis
  4992. (if em
  4993. (if (featurep 'xemacs)
  4994. '(org-do-emphasis-faces (0 nil append))
  4995. '(org-do-emphasis-faces)))
  4996. ;; Checkboxes
  4997. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4998. 2 'bold prepend)
  4999. (if org-provide-checkbox-statistics
  5000. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5001. (0 (org-get-checkbox-statistics-face) t)))
  5002. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  5003. '(1 'org-archived prepend))
  5004. ;; Specials
  5005. '(org-do-latex-and-special-faces)
  5006. ;; Code
  5007. '(org-activate-code (1 'org-code t))
  5008. ;; COMMENT
  5009. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5010. "\\|" org-quote-string "\\)\\>")
  5011. '(1 'org-special-keyword t))
  5012. '("^#.*" (0 'font-lock-comment-face t))
  5013. )))
  5014. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5015. ;; Now set the full font-lock-keywords
  5016. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5017. (org-set-local 'font-lock-defaults
  5018. '(org-font-lock-keywords t nil nil backward-paragraph))
  5019. (kill-local-variable 'font-lock-keywords) nil))
  5020. (defvar org-m nil)
  5021. (defvar org-l nil)
  5022. (defvar org-f nil)
  5023. (defun org-get-level-face (n)
  5024. "Get the right face for match N in font-lock matching of healdines."
  5025. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5026. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5027. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5028. (cond
  5029. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5030. ((eq n 2) org-f)
  5031. (t (if org-level-color-stars-only nil org-f))))
  5032. (defun org-get-todo-face (kwd)
  5033. "Get the right face for a TODO keyword KWD.
  5034. If KWD is a number, get the corresponding match group."
  5035. (if (numberp kwd) (setq kwd (match-string kwd)))
  5036. (or (cdr (assoc kwd org-todo-keyword-faces))
  5037. (and (member kwd org-done-keywords) 'org-done)
  5038. 'org-todo))
  5039. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5040. "Remove fontification and activation overlays from links."
  5041. (font-lock-default-unfontify-region beg end)
  5042. (let* ((buffer-undo-list t)
  5043. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5044. (inhibit-modification-hooks t)
  5045. deactivate-mark buffer-file-name buffer-file-truename)
  5046. (remove-text-properties beg end
  5047. '(mouse-face t keymap t org-linked-text t
  5048. invisible t intangible t))))
  5049. ;;;; Visibility cycling, including org-goto and indirect buffer
  5050. ;;; Cycling
  5051. (defvar org-cycle-global-status nil)
  5052. (make-variable-buffer-local 'org-cycle-global-status)
  5053. (defvar org-cycle-subtree-status nil)
  5054. (make-variable-buffer-local 'org-cycle-subtree-status)
  5055. ;;;###autoload
  5056. (defun org-cycle (&optional arg)
  5057. "Visibility cycling for Org-mode.
  5058. - When this function is called with a prefix argument, rotate the entire
  5059. buffer through 3 states (global cycling)
  5060. 1. OVERVIEW: Show only top-level headlines.
  5061. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5062. 3. SHOW ALL: Show everything.
  5063. - When point is at the beginning of a headline, rotate the subtree started
  5064. by this line through 3 different states (local cycling)
  5065. 1. FOLDED: Only the main headline is shown.
  5066. 2. CHILDREN: The main headline and the direct children are shown.
  5067. From this state, you can move to one of the children
  5068. and zoom in further.
  5069. 3. SUBTREE: Show the entire subtree, including body text.
  5070. - When there is a numeric prefix, go up to a heading with level ARG, do
  5071. a `show-subtree' and return to the previous cursor position. If ARG
  5072. is negative, go up that many levels.
  5073. - When point is not at the beginning of a headline, execute
  5074. `indent-relative', like TAB normally does. See the option
  5075. `org-cycle-emulate-tab' for details.
  5076. - Special case: if point is at the beginning of the buffer and there is
  5077. no headline in line 1, this function will act as if called with prefix arg.
  5078. But only if also the variable `org-cycle-global-at-bob' is t."
  5079. (interactive "P")
  5080. (let* ((outline-regexp
  5081. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5082. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5083. outline-regexp))
  5084. (bob-special (and org-cycle-global-at-bob (bobp)
  5085. (not (looking-at outline-regexp))))
  5086. (org-cycle-hook
  5087. (if bob-special
  5088. (delq 'org-optimize-window-after-visibility-change
  5089. (copy-sequence org-cycle-hook))
  5090. org-cycle-hook))
  5091. (pos (point)))
  5092. (if (or bob-special (equal arg '(4)))
  5093. ;; special case: use global cycling
  5094. (setq arg t))
  5095. (cond
  5096. ((org-at-table-p 'any)
  5097. ;; Enter the table or move to the next field in the table
  5098. (or (org-table-recognize-table.el)
  5099. (progn
  5100. (if arg (org-table-edit-field t)
  5101. (org-table-justify-field-maybe)
  5102. (call-interactively 'org-table-next-field)))))
  5103. ((eq arg t) ;; Global cycling
  5104. (cond
  5105. ((and (eq last-command this-command)
  5106. (eq org-cycle-global-status 'overview))
  5107. ;; We just created the overview - now do table of contents
  5108. ;; This can be slow in very large buffers, so indicate action
  5109. (message "CONTENTS...")
  5110. (org-content)
  5111. (message "CONTENTS...done")
  5112. (setq org-cycle-global-status 'contents)
  5113. (run-hook-with-args 'org-cycle-hook 'contents))
  5114. ((and (eq last-command this-command)
  5115. (eq org-cycle-global-status 'contents))
  5116. ;; We just showed the table of contents - now show everything
  5117. (show-all)
  5118. (message "SHOW ALL")
  5119. (setq org-cycle-global-status 'all)
  5120. (run-hook-with-args 'org-cycle-hook 'all))
  5121. (t
  5122. ;; Default action: go to overview
  5123. (org-overview)
  5124. (message "OVERVIEW")
  5125. (setq org-cycle-global-status 'overview)
  5126. (run-hook-with-args 'org-cycle-hook 'overview))))
  5127. ((and org-drawers org-drawer-regexp
  5128. (save-excursion
  5129. (beginning-of-line 1)
  5130. (looking-at org-drawer-regexp)))
  5131. ;; Toggle block visibility
  5132. (org-flag-drawer
  5133. (not (get-char-property (match-end 0) 'invisible))))
  5134. ((integerp arg)
  5135. ;; Show-subtree, ARG levels up from here.
  5136. (save-excursion
  5137. (org-back-to-heading)
  5138. (outline-up-heading (if (< arg 0) (- arg)
  5139. (- (funcall outline-level) arg)))
  5140. (org-show-subtree)))
  5141. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5142. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5143. ;; At a heading: rotate between three different views
  5144. (org-back-to-heading)
  5145. (let ((goal-column 0) eoh eol eos)
  5146. ;; First, some boundaries
  5147. (save-excursion
  5148. (org-back-to-heading)
  5149. (save-excursion
  5150. (beginning-of-line 2)
  5151. (while (and (not (eobp)) ;; this is like `next-line'
  5152. (get-char-property (1- (point)) 'invisible))
  5153. (beginning-of-line 2)) (setq eol (point)))
  5154. (outline-end-of-heading) (setq eoh (point))
  5155. (org-end-of-subtree t)
  5156. (unless (eobp)
  5157. (skip-chars-forward " \t\n")
  5158. (beginning-of-line 1) ; in case this is an item
  5159. )
  5160. (setq eos (1- (point))))
  5161. ;; Find out what to do next and set `this-command'
  5162. (cond
  5163. ((= eos eoh)
  5164. ;; Nothing is hidden behind this heading
  5165. (message "EMPTY ENTRY")
  5166. (setq org-cycle-subtree-status nil)
  5167. (save-excursion
  5168. (goto-char eos)
  5169. (outline-next-heading)
  5170. (if (org-invisible-p) (org-flag-heading nil))))
  5171. ((or (>= eol eos)
  5172. (not (string-match "\\S-" (buffer-substring eol eos))))
  5173. ;; Entire subtree is hidden in one line: open it
  5174. (org-show-entry)
  5175. (show-children)
  5176. (message "CHILDREN")
  5177. (save-excursion
  5178. (goto-char eos)
  5179. (outline-next-heading)
  5180. (if (org-invisible-p) (org-flag-heading nil)))
  5181. (setq org-cycle-subtree-status 'children)
  5182. (run-hook-with-args 'org-cycle-hook 'children))
  5183. ((and (eq last-command this-command)
  5184. (eq org-cycle-subtree-status 'children))
  5185. ;; We just showed the children, now show everything.
  5186. (org-show-subtree)
  5187. (message "SUBTREE")
  5188. (setq org-cycle-subtree-status 'subtree)
  5189. (run-hook-with-args 'org-cycle-hook 'subtree))
  5190. (t
  5191. ;; Default action: hide the subtree.
  5192. (hide-subtree)
  5193. (message "FOLDED")
  5194. (setq org-cycle-subtree-status 'folded)
  5195. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5196. ;; TAB emulation
  5197. (buffer-read-only (org-back-to-heading))
  5198. ((org-try-cdlatex-tab))
  5199. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5200. (or (not (bolp))
  5201. (not (looking-at outline-regexp))))
  5202. (call-interactively (global-key-binding "\t")))
  5203. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5204. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5205. (or (and (eq org-cycle-emulate-tab 'white)
  5206. (= (match-end 0) (point-at-eol)))
  5207. (and (eq org-cycle-emulate-tab 'whitestart)
  5208. (>= (match-end 0) pos))))
  5209. t
  5210. (eq org-cycle-emulate-tab t))
  5211. ; (if (and (looking-at "[ \n\r\t]")
  5212. ; (string-match "^[ \t]*$" (buffer-substring
  5213. ; (point-at-bol) (point))))
  5214. ; (progn
  5215. ; (beginning-of-line 1)
  5216. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5217. (call-interactively (global-key-binding "\t")))
  5218. (t (save-excursion
  5219. (org-back-to-heading)
  5220. (org-cycle))))))
  5221. ;;;###autoload
  5222. (defun org-global-cycle (&optional arg)
  5223. "Cycle the global visibility. For details see `org-cycle'."
  5224. (interactive "P")
  5225. (let ((org-cycle-include-plain-lists
  5226. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5227. (if (integerp arg)
  5228. (progn
  5229. (show-all)
  5230. (hide-sublevels arg)
  5231. (setq org-cycle-global-status 'contents))
  5232. (org-cycle '(4)))))
  5233. (defun org-overview ()
  5234. "Switch to overview mode, shoing only top-level headlines.
  5235. Really, this shows all headlines with level equal or greater than the level
  5236. of the first headline in the buffer. This is important, because if the
  5237. first headline is not level one, then (hide-sublevels 1) gives confusing
  5238. results."
  5239. (interactive)
  5240. (let ((level (save-excursion
  5241. (goto-char (point-min))
  5242. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5243. (progn
  5244. (goto-char (match-beginning 0))
  5245. (funcall outline-level))))))
  5246. (and level (hide-sublevels level))))
  5247. (defun org-content (&optional arg)
  5248. "Show all headlines in the buffer, like a table of contents.
  5249. With numerical argument N, show content up to level N."
  5250. (interactive "P")
  5251. (save-excursion
  5252. ;; Visit all headings and show their offspring
  5253. (and (integerp arg) (org-overview))
  5254. (goto-char (point-max))
  5255. (catch 'exit
  5256. (while (and (progn (condition-case nil
  5257. (outline-previous-visible-heading 1)
  5258. (error (goto-char (point-min))))
  5259. t)
  5260. (looking-at outline-regexp))
  5261. (if (integerp arg)
  5262. (show-children (1- arg))
  5263. (show-branches))
  5264. (if (bobp) (throw 'exit nil))))))
  5265. (defun org-optimize-window-after-visibility-change (state)
  5266. "Adjust the window after a change in outline visibility.
  5267. This function is the default value of the hook `org-cycle-hook'."
  5268. (when (get-buffer-window (current-buffer))
  5269. (cond
  5270. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5271. ; ((eq state 'overview) (org-beginning-of-line))
  5272. ((eq state 'content) nil)
  5273. ((eq state 'all) nil)
  5274. ((eq state 'folded) nil)
  5275. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5276. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5277. (defun org-compact-display-after-subtree-move ()
  5278. (let (beg end)
  5279. (save-excursion
  5280. (if (org-up-heading-safe)
  5281. (progn
  5282. (hide-subtree)
  5283. (show-entry)
  5284. (show-children)
  5285. (org-cycle-show-empty-lines 'children)
  5286. (org-cycle-hide-drawers 'children))
  5287. (org-overview)))))
  5288. (defun org-cycle-show-empty-lines (state)
  5289. "Show empty lines above all visible headlines.
  5290. The region to be covered depends on STATE when called through
  5291. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5292. entire buffer covered. Note that an empty line is only shown if there
  5293. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5294. (when (> org-cycle-separator-lines 0)
  5295. (save-excursion
  5296. (let* ((n org-cycle-separator-lines)
  5297. (re (cond
  5298. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5299. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5300. (t (let ((ns (number-to-string (- n 2))))
  5301. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5302. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5303. beg end)
  5304. (cond
  5305. ((memq state '(overview contents t))
  5306. (setq beg (point-min) end (point-max)))
  5307. ((memq state '(children folded))
  5308. (setq beg (point) end (progn (org-end-of-subtree t t)
  5309. (beginning-of-line 2)
  5310. (point)))))
  5311. (when beg
  5312. (goto-char beg)
  5313. (while (re-search-forward re end t)
  5314. (if (not (get-char-property (match-end 1) 'invisible))
  5315. (outline-flag-region
  5316. (match-beginning 1) (match-end 1) nil)))))))
  5317. ;; Never hide empty lines at the end of the file.
  5318. (save-excursion
  5319. (goto-char (point-max))
  5320. (outline-previous-heading)
  5321. (outline-end-of-heading)
  5322. (if (and (looking-at "[ \t\n]+")
  5323. (= (match-end 0) (point-max)))
  5324. (outline-flag-region (point) (match-end 0) nil))))
  5325. (defun org-subtree-end-visible-p ()
  5326. "Is the end of the current subtree visible?"
  5327. (pos-visible-in-window-p
  5328. (save-excursion (org-end-of-subtree t) (point))))
  5329. (defun org-first-headline-recenter (&optional N)
  5330. "Move cursor to the first headline and recenter the headline.
  5331. Optional argument N means, put the headline into the Nth line of the window."
  5332. (goto-char (point-min))
  5333. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5334. (beginning-of-line)
  5335. (recenter (prefix-numeric-value N))))
  5336. ;;; Org-goto
  5337. (defvar org-goto-window-configuration nil)
  5338. (defvar org-goto-marker nil)
  5339. (defvar org-goto-map
  5340. (let ((map (make-sparse-keymap)))
  5341. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5342. (while (setq cmd (pop cmds))
  5343. (substitute-key-definition cmd cmd map global-map)))
  5344. (suppress-keymap map)
  5345. (org-defkey map "\C-m" 'org-goto-ret)
  5346. (org-defkey map [(return)] 'org-goto-ret)
  5347. (org-defkey map [(left)] 'org-goto-left)
  5348. (org-defkey map [(right)] 'org-goto-right)
  5349. (org-defkey map [(control ?g)] 'org-goto-quit)
  5350. (org-defkey map "\C-i" 'org-cycle)
  5351. (org-defkey map [(tab)] 'org-cycle)
  5352. (org-defkey map [(down)] 'outline-next-visible-heading)
  5353. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5354. (if org-goto-auto-isearch
  5355. (if (fboundp 'define-key-after)
  5356. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5357. nil)
  5358. (org-defkey map "q" 'org-goto-quit)
  5359. (org-defkey map "n" 'outline-next-visible-heading)
  5360. (org-defkey map "p" 'outline-previous-visible-heading)
  5361. (org-defkey map "f" 'outline-forward-same-level)
  5362. (org-defkey map "b" 'outline-backward-same-level)
  5363. (org-defkey map "u" 'outline-up-heading))
  5364. (org-defkey map "/" 'org-occur)
  5365. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5366. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5367. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5368. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5369. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5370. map))
  5371. (defconst org-goto-help
  5372. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5373. RET=jump to location [Q]uit and return to previous location
  5374. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5375. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5376. (defun org-goto (&optional alternative-interface)
  5377. "Look up a different location in the current file, keeping current visibility.
  5378. When you want look-up or go to a different location in a document, the
  5379. fastest way is often to fold the entire buffer and then dive into the tree.
  5380. This method has the disadvantage, that the previous location will be folded,
  5381. which may not be what you want.
  5382. This command works around this by showing a copy of the current buffer
  5383. in an indirect buffer, in overview mode. You can dive into the tree in
  5384. that copy, use org-occur and incremental search to find a location.
  5385. When pressing RET or `Q', the command returns to the original buffer in
  5386. which the visibility is still unchanged. After RET is will also jump to
  5387. the location selected in the indirect buffer and expose the
  5388. the headline hierarchy above."
  5389. (interactive "P")
  5390. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5391. (org-refile-use-outline-path t)
  5392. (interface
  5393. (if (not alternative-interface)
  5394. org-goto-interface
  5395. (if (eq org-goto-interface 'outline)
  5396. 'outline-path-completion
  5397. 'outline)))
  5398. (org-goto-start-pos (point))
  5399. (selected-point
  5400. (if (eq interface 'outline)
  5401. (car (org-get-location (current-buffer) org-goto-help))
  5402. (nth 3 (org-refile-get-location "Goto: ")))))
  5403. (if selected-point
  5404. (progn
  5405. (org-mark-ring-push org-goto-start-pos)
  5406. (goto-char selected-point)
  5407. (if (or (org-invisible-p) (org-invisible-p2))
  5408. (org-show-context 'org-goto)))
  5409. (message "Quit"))))
  5410. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5411. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5412. (defvar org-goto-local-auto-isearch-map) ; defined below
  5413. (defun org-get-location (buf help)
  5414. "Let the user select a location in the Org-mode buffer BUF.
  5415. This function uses a recursive edit. It returns the selected position
  5416. or nil."
  5417. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5418. (isearch-hide-immediately nil)
  5419. (isearch-search-fun-function
  5420. (lambda () 'org-goto-local-search-forward-headings))
  5421. (org-goto-selected-point org-goto-exit-command))
  5422. (save-excursion
  5423. (save-window-excursion
  5424. (delete-other-windows)
  5425. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5426. (switch-to-buffer
  5427. (condition-case nil
  5428. (make-indirect-buffer (current-buffer) "*org-goto*")
  5429. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5430. (with-output-to-temp-buffer "*Help*"
  5431. (princ help))
  5432. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5433. (setq buffer-read-only nil)
  5434. (let ((org-startup-truncated t)
  5435. (org-startup-folded nil)
  5436. (org-startup-align-all-tables nil))
  5437. (org-mode)
  5438. (org-overview))
  5439. (setq buffer-read-only t)
  5440. (if (and (boundp 'org-goto-start-pos)
  5441. (integer-or-marker-p org-goto-start-pos))
  5442. (let ((org-show-hierarchy-above t)
  5443. (org-show-siblings t)
  5444. (org-show-following-heading t))
  5445. (goto-char org-goto-start-pos)
  5446. (and (org-invisible-p) (org-show-context)))
  5447. (goto-char (point-min)))
  5448. (org-beginning-of-line)
  5449. (message "Select location and press RET")
  5450. (use-local-map org-goto-map)
  5451. (recursive-edit)
  5452. ))
  5453. (kill-buffer "*org-goto*")
  5454. (cons org-goto-selected-point org-goto-exit-command)))
  5455. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5456. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5457. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5458. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5459. (defun org-goto-local-search-forward-headings (string bound noerror)
  5460. "Search and make sure that anu matches are in headlines."
  5461. (catch 'return
  5462. (while (search-forward string bound noerror)
  5463. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5464. (and (member :headline context)
  5465. (not (member :tags context))))
  5466. (throw 'return (point))))))
  5467. (defun org-goto-local-auto-isearch ()
  5468. "Start isearch."
  5469. (interactive)
  5470. (goto-char (point-min))
  5471. (let ((keys (this-command-keys)))
  5472. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5473. (isearch-mode t)
  5474. (isearch-process-search-char (string-to-char keys)))))
  5475. (defun org-goto-ret (&optional arg)
  5476. "Finish `org-goto' by going to the new location."
  5477. (interactive "P")
  5478. (setq org-goto-selected-point (point)
  5479. org-goto-exit-command 'return)
  5480. (throw 'exit nil))
  5481. (defun org-goto-left ()
  5482. "Finish `org-goto' by going to the new location."
  5483. (interactive)
  5484. (if (org-on-heading-p)
  5485. (progn
  5486. (beginning-of-line 1)
  5487. (setq org-goto-selected-point (point)
  5488. org-goto-exit-command 'left)
  5489. (throw 'exit nil))
  5490. (error "Not on a heading")))
  5491. (defun org-goto-right ()
  5492. "Finish `org-goto' by going to the new location."
  5493. (interactive)
  5494. (if (org-on-heading-p)
  5495. (progn
  5496. (setq org-goto-selected-point (point)
  5497. org-goto-exit-command 'right)
  5498. (throw 'exit nil))
  5499. (error "Not on a heading")))
  5500. (defun org-goto-quit ()
  5501. "Finish `org-goto' without cursor motion."
  5502. (interactive)
  5503. (setq org-goto-selected-point nil)
  5504. (setq org-goto-exit-command 'quit)
  5505. (throw 'exit nil))
  5506. ;;; Indirect buffer display of subtrees
  5507. (defvar org-indirect-dedicated-frame nil
  5508. "This is the frame being used for indirect tree display.")
  5509. (defvar org-last-indirect-buffer nil)
  5510. (defun org-tree-to-indirect-buffer (&optional arg)
  5511. "Create indirect buffer and narrow it to current subtree.
  5512. With numerical prefix ARG, go up to this level and then take that tree.
  5513. If ARG is negative, go up that many levels.
  5514. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5515. indirect buffer previously made with this command, to avoid proliferation of
  5516. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5517. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5518. is kept so that you can work with several indirect buffers at the same time.
  5519. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5520. requests that a new frame be made for the new buffer, so that the dedicated
  5521. frame is not changed."
  5522. (interactive "P")
  5523. (let ((cbuf (current-buffer))
  5524. (cwin (selected-window))
  5525. (pos (point))
  5526. beg end level heading ibuf)
  5527. (save-excursion
  5528. (org-back-to-heading t)
  5529. (when (numberp arg)
  5530. (setq level (org-outline-level))
  5531. (if (< arg 0) (setq arg (+ level arg)))
  5532. (while (> (setq level (org-outline-level)) arg)
  5533. (outline-up-heading 1 t)))
  5534. (setq beg (point)
  5535. heading (org-get-heading))
  5536. (org-end-of-subtree t) (setq end (point)))
  5537. (if (and (buffer-live-p org-last-indirect-buffer)
  5538. (not (eq org-indirect-buffer-display 'new-frame))
  5539. (not arg))
  5540. (kill-buffer org-last-indirect-buffer))
  5541. (setq ibuf (org-get-indirect-buffer cbuf)
  5542. org-last-indirect-buffer ibuf)
  5543. (cond
  5544. ((or (eq org-indirect-buffer-display 'new-frame)
  5545. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5546. (select-frame (make-frame))
  5547. (delete-other-windows)
  5548. (switch-to-buffer ibuf)
  5549. (org-set-frame-title heading))
  5550. ((eq org-indirect-buffer-display 'dedicated-frame)
  5551. (raise-frame
  5552. (select-frame (or (and org-indirect-dedicated-frame
  5553. (frame-live-p org-indirect-dedicated-frame)
  5554. org-indirect-dedicated-frame)
  5555. (setq org-indirect-dedicated-frame (make-frame)))))
  5556. (delete-other-windows)
  5557. (switch-to-buffer ibuf)
  5558. (org-set-frame-title (concat "Indirect: " heading)))
  5559. ((eq org-indirect-buffer-display 'current-window)
  5560. (switch-to-buffer ibuf))
  5561. ((eq org-indirect-buffer-display 'other-window)
  5562. (pop-to-buffer ibuf))
  5563. (t (error "Invalid value.")))
  5564. (if (featurep 'xemacs)
  5565. (save-excursion (org-mode) (turn-on-font-lock)))
  5566. (narrow-to-region beg end)
  5567. (show-all)
  5568. (goto-char pos)
  5569. (and (window-live-p cwin) (select-window cwin))))
  5570. (defun org-get-indirect-buffer (&optional buffer)
  5571. (setq buffer (or buffer (current-buffer)))
  5572. (let ((n 1) (base (buffer-name buffer)) bname)
  5573. (while (buffer-live-p
  5574. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5575. (setq n (1+ n)))
  5576. (condition-case nil
  5577. (make-indirect-buffer buffer bname 'clone)
  5578. (error (make-indirect-buffer buffer bname)))))
  5579. (defun org-set-frame-title (title)
  5580. "Set the title of the current frame to the string TITLE."
  5581. ;; FIXME: how to name a single frame in XEmacs???
  5582. (unless (featurep 'xemacs)
  5583. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5584. ;;;; Structure editing
  5585. ;;; Inserting headlines
  5586. (defun org-insert-heading (&optional force-heading)
  5587. "Insert a new heading or item with same depth at point.
  5588. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5589. If point is at the beginning of a headline, insert a sibling before the
  5590. current headline. If point is not at the beginning, do not split the line,
  5591. but create the new hedline after the current line."
  5592. (interactive "P")
  5593. (if (= (buffer-size) 0)
  5594. (insert "\n* ")
  5595. (when (or force-heading (not (org-insert-item)))
  5596. (let* ((head (save-excursion
  5597. (condition-case nil
  5598. (progn
  5599. (org-back-to-heading)
  5600. (match-string 0))
  5601. (error "*"))))
  5602. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5603. pos)
  5604. (cond
  5605. ((and (org-on-heading-p) (bolp)
  5606. (or (bobp)
  5607. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5608. ;; insert before the current line
  5609. (open-line (if blank 2 1)))
  5610. ((and (bolp)
  5611. (or (bobp)
  5612. (save-excursion
  5613. (backward-char 1) (not (org-invisible-p)))))
  5614. ;; insert right here
  5615. nil)
  5616. (t
  5617. ; ;; in the middle of the line
  5618. ; (org-show-entry)
  5619. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5620. ; (if (and
  5621. ; (org-on-heading-p)
  5622. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5623. ; ;; protect the tags
  5624. ;; (let ((tags (match-string 2)) pos)
  5625. ; (delete-region (match-beginning 1) (match-end 1))
  5626. ; (setq pos (point-at-bol))
  5627. ; (newline (if blank 2 1))
  5628. ; (save-excursion
  5629. ; (goto-char pos)
  5630. ; (end-of-line 1)
  5631. ; (insert " " tags)
  5632. ; (org-set-tags nil 'align)))
  5633. ; (newline (if blank 2 1)))
  5634. ; (newline (if blank 2 1))))
  5635. ;; in the middle of the line
  5636. (org-show-entry)
  5637. (let ((split
  5638. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5639. tags pos)
  5640. (if (org-on-heading-p)
  5641. (progn
  5642. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5643. (setq tags (and (match-end 2) (match-string 2)))
  5644. (and (match-end 1)
  5645. (delete-region (match-beginning 1) (match-end 1)))
  5646. (setq pos (point-at-bol))
  5647. (or split (end-of-line 1))
  5648. (delete-horizontal-space)
  5649. (newline (if blank 2 1))
  5650. (when tags
  5651. (save-excursion
  5652. (goto-char pos)
  5653. (end-of-line 1)
  5654. (insert " " tags)
  5655. (org-set-tags nil 'align))))
  5656. (or split (end-of-line 1))
  5657. (newline (if blank 2 1))))))
  5658. (insert head) (just-one-space)
  5659. (setq pos (point))
  5660. (end-of-line 1)
  5661. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5662. (run-hooks 'org-insert-heading-hook)))))
  5663. (defun org-insert-heading-after-current ()
  5664. "Insert a new heading with same level as current, after current subtree."
  5665. (interactive)
  5666. (org-back-to-heading)
  5667. (org-insert-heading)
  5668. (org-move-subtree-down)
  5669. (end-of-line 1))
  5670. (defun org-insert-todo-heading (arg)
  5671. "Insert a new heading with the same level and TODO state as current heading.
  5672. If the heading has no TODO state, or if the state is DONE, use the first
  5673. state (TODO by default). Also with prefix arg, force first state."
  5674. (interactive "P")
  5675. (when (not (org-insert-item 'checkbox))
  5676. (org-insert-heading)
  5677. (save-excursion
  5678. (org-back-to-heading)
  5679. (outline-previous-heading)
  5680. (looking-at org-todo-line-regexp))
  5681. (if (or arg
  5682. (not (match-beginning 2))
  5683. (member (match-string 2) org-done-keywords))
  5684. (insert (car org-todo-keywords-1) " ")
  5685. (insert (match-string 2) " "))))
  5686. (defun org-insert-subheading (arg)
  5687. "Insert a new subheading and demote it.
  5688. Works for outline headings and for plain lists alike."
  5689. (interactive "P")
  5690. (org-insert-heading arg)
  5691. (cond
  5692. ((org-on-heading-p) (org-do-demote))
  5693. ((org-at-item-p) (org-indent-item 1))))
  5694. (defun org-insert-todo-subheading (arg)
  5695. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5696. Works for outline headings and for plain lists alike."
  5697. (interactive "P")
  5698. (org-insert-todo-heading arg)
  5699. (cond
  5700. ((org-on-heading-p) (org-do-demote))
  5701. ((org-at-item-p) (org-indent-item 1))))
  5702. ;;; Promotion and Demotion
  5703. (defun org-promote-subtree ()
  5704. "Promote the entire subtree.
  5705. See also `org-promote'."
  5706. (interactive)
  5707. (save-excursion
  5708. (org-map-tree 'org-promote))
  5709. (org-fix-position-after-promote))
  5710. (defun org-demote-subtree ()
  5711. "Demote the entire subtree. See `org-demote'.
  5712. See also `org-promote'."
  5713. (interactive)
  5714. (save-excursion
  5715. (org-map-tree 'org-demote))
  5716. (org-fix-position-after-promote))
  5717. (defun org-do-promote ()
  5718. "Promote the current heading higher up the tree.
  5719. If the region is active in `transient-mark-mode', promote all headings
  5720. in the region."
  5721. (interactive)
  5722. (save-excursion
  5723. (if (org-region-active-p)
  5724. (org-map-region 'org-promote (region-beginning) (region-end))
  5725. (org-promote)))
  5726. (org-fix-position-after-promote))
  5727. (defun org-do-demote ()
  5728. "Demote the current heading lower down the tree.
  5729. If the region is active in `transient-mark-mode', demote all headings
  5730. in the region."
  5731. (interactive)
  5732. (save-excursion
  5733. (if (org-region-active-p)
  5734. (org-map-region 'org-demote (region-beginning) (region-end))
  5735. (org-demote)))
  5736. (org-fix-position-after-promote))
  5737. (defun org-fix-position-after-promote ()
  5738. "Make sure that after pro/demotion cursor position is right."
  5739. (let ((pos (point)))
  5740. (when (save-excursion
  5741. (beginning-of-line 1)
  5742. (looking-at org-todo-line-regexp)
  5743. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5744. (cond ((eobp) (insert " "))
  5745. ((eolp) (insert " "))
  5746. ((equal (char-after) ?\ ) (forward-char 1))))))
  5747. (defun org-reduced-level (l)
  5748. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5749. (defun org-get-legal-level (level &optional change)
  5750. "Rectify a level change under the influence of `org-odd-levels-only'
  5751. LEVEL is a current level, CHANGE is by how much the level should be
  5752. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5753. even level numbers will become the next higher odd number."
  5754. (if org-odd-levels-only
  5755. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5756. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5757. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5758. (max 1 (+ level change))))
  5759. (defun org-promote ()
  5760. "Promote the current heading higher up the tree.
  5761. If the region is active in `transient-mark-mode', promote all headings
  5762. in the region."
  5763. (org-back-to-heading t)
  5764. (let* ((level (save-match-data (funcall outline-level)))
  5765. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5766. (diff (abs (- level (length up-head) -1))))
  5767. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5768. (replace-match up-head nil t)
  5769. ;; Fixup tag positioning
  5770. (and org-auto-align-tags (org-set-tags nil t))
  5771. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5772. (defun org-demote ()
  5773. "Demote the current heading lower down the tree.
  5774. If the region is active in `transient-mark-mode', demote all headings
  5775. in the region."
  5776. (org-back-to-heading t)
  5777. (let* ((level (save-match-data (funcall outline-level)))
  5778. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5779. (diff (abs (- level (length down-head) -1))))
  5780. (replace-match down-head nil t)
  5781. ;; Fixup tag positioning
  5782. (and org-auto-align-tags (org-set-tags nil t))
  5783. (if org-adapt-indentation (org-fixup-indentation diff))))
  5784. (defun org-map-tree (fun)
  5785. "Call FUN for every heading underneath the current one."
  5786. (org-back-to-heading)
  5787. (let ((level (funcall outline-level)))
  5788. (save-excursion
  5789. (funcall fun)
  5790. (while (and (progn
  5791. (outline-next-heading)
  5792. (> (funcall outline-level) level))
  5793. (not (eobp)))
  5794. (funcall fun)))))
  5795. (defun org-map-region (fun beg end)
  5796. "Call FUN for every heading between BEG and END."
  5797. (let ((org-ignore-region t))
  5798. (save-excursion
  5799. (setq end (copy-marker end))
  5800. (goto-char beg)
  5801. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5802. (< (point) end))
  5803. (funcall fun))
  5804. (while (and (progn
  5805. (outline-next-heading)
  5806. (< (point) end))
  5807. (not (eobp)))
  5808. (funcall fun)))))
  5809. (defun org-fixup-indentation (diff)
  5810. "Change the indentation in the current entry by DIFF
  5811. However, if any line in the current entry has no indentation, or if it
  5812. would end up with no indentation after the change, nothing at all is done."
  5813. (save-excursion
  5814. (let ((end (save-excursion (outline-next-heading)
  5815. (point-marker)))
  5816. (prohibit (if (> diff 0)
  5817. "^\\S-"
  5818. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5819. col)
  5820. (unless (save-excursion (end-of-line 1)
  5821. (re-search-forward prohibit end t))
  5822. (while (and (< (point) end)
  5823. (re-search-forward "^[ \t]+" end t))
  5824. (goto-char (match-end 0))
  5825. (setq col (current-column))
  5826. (if (< diff 0) (replace-match ""))
  5827. (indent-to (+ diff col))))
  5828. (move-marker end nil))))
  5829. (defun org-convert-to-odd-levels ()
  5830. "Convert an org-mode file with all levels allowed to one with odd levels.
  5831. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5832. level 5 etc."
  5833. (interactive)
  5834. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5835. (let ((org-odd-levels-only nil) n)
  5836. (save-excursion
  5837. (goto-char (point-min))
  5838. (while (re-search-forward "^\\*\\*+ " nil t)
  5839. (setq n (- (length (match-string 0)) 2))
  5840. (while (>= (setq n (1- n)) 0)
  5841. (org-demote))
  5842. (end-of-line 1))))))
  5843. (defun org-convert-to-oddeven-levels ()
  5844. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5845. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5846. section with an even level, conversion would destroy the structure of the file. An error
  5847. is signaled in this case."
  5848. (interactive)
  5849. (goto-char (point-min))
  5850. ;; First check if there are no even levels
  5851. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5852. (org-show-context t)
  5853. (error "Not all levels are odd in this file. Conversion not possible."))
  5854. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5855. (let ((org-odd-levels-only nil) n)
  5856. (save-excursion
  5857. (goto-char (point-min))
  5858. (while (re-search-forward "^\\*\\*+ " nil t)
  5859. (setq n (/ (1- (length (match-string 0))) 2))
  5860. (while (>= (setq n (1- n)) 0)
  5861. (org-promote))
  5862. (end-of-line 1))))))
  5863. (defun org-tr-level (n)
  5864. "Make N odd if required."
  5865. (if org-odd-levels-only (1+ (/ n 2)) n))
  5866. ;;; Vertical tree motion, cutting and pasting of subtrees
  5867. (defun org-move-subtree-up (&optional arg)
  5868. "Move the current subtree up past ARG headlines of the same level."
  5869. (interactive "p")
  5870. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5871. (defun org-move-subtree-down (&optional arg)
  5872. "Move the current subtree down past ARG headlines of the same level."
  5873. (interactive "p")
  5874. (setq arg (prefix-numeric-value arg))
  5875. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5876. 'outline-get-last-sibling))
  5877. (ins-point (make-marker))
  5878. (cnt (abs arg))
  5879. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5880. ;; Select the tree
  5881. (org-back-to-heading)
  5882. (setq beg0 (point))
  5883. (save-excursion
  5884. (setq ne-beg (org-back-over-empty-lines))
  5885. (setq beg (point)))
  5886. (save-match-data
  5887. (save-excursion (outline-end-of-heading)
  5888. (setq folded (org-invisible-p)))
  5889. (outline-end-of-subtree))
  5890. (outline-next-heading)
  5891. (setq ne-end (org-back-over-empty-lines))
  5892. (setq end (point))
  5893. (goto-char beg0)
  5894. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5895. ;; include less whitespace
  5896. (save-excursion
  5897. (goto-char beg)
  5898. (forward-line (- ne-beg ne-end))
  5899. (setq beg (point))))
  5900. ;; Find insertion point, with error handling
  5901. (while (> cnt 0)
  5902. (or (and (funcall movfunc) (looking-at outline-regexp))
  5903. (progn (goto-char beg0)
  5904. (error "Cannot move past superior level or buffer limit")))
  5905. (setq cnt (1- cnt)))
  5906. (if (> arg 0)
  5907. ;; Moving forward - still need to move over subtree
  5908. (progn (org-end-of-subtree t t)
  5909. (save-excursion
  5910. (org-back-over-empty-lines)
  5911. (or (bolp) (newline)))))
  5912. (setq ne-ins (org-back-over-empty-lines))
  5913. (move-marker ins-point (point))
  5914. (setq txt (buffer-substring beg end))
  5915. (delete-region beg end)
  5916. (outline-flag-region (1- beg) beg nil)
  5917. (outline-flag-region (1- (point)) (point) nil)
  5918. (insert txt)
  5919. (or (bolp) (insert "\n"))
  5920. (setq ins-end (point))
  5921. (goto-char ins-point)
  5922. (org-skip-whitespace)
  5923. (when (and (< arg 0)
  5924. (org-first-sibling-p)
  5925. (> ne-ins ne-beg))
  5926. ;; Move whitespace back to beginning
  5927. (save-excursion
  5928. (goto-char ins-end)
  5929. (let ((kill-whole-line t))
  5930. (kill-line (- ne-ins ne-beg)) (point)))
  5931. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5932. (move-marker ins-point nil)
  5933. (org-compact-display-after-subtree-move)
  5934. (unless folded
  5935. (org-show-entry)
  5936. (show-children)
  5937. (org-cycle-hide-drawers 'children))))
  5938. (defvar org-subtree-clip ""
  5939. "Clipboard for cut and paste of subtrees.
  5940. This is actually only a copy of the kill, because we use the normal kill
  5941. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5942. (defvar org-subtree-clip-folded nil
  5943. "Was the last copied subtree folded?
  5944. This is used to fold the tree back after pasting.")
  5945. (defun org-cut-subtree (&optional n)
  5946. "Cut the current subtree into the clipboard.
  5947. With prefix arg N, cut this many sequential subtrees.
  5948. This is a short-hand for marking the subtree and then cutting it."
  5949. (interactive "p")
  5950. (org-copy-subtree n 'cut))
  5951. (defun org-copy-subtree (&optional n cut)
  5952. "Cut the current subtree into the clipboard.
  5953. With prefix arg N, cut this many sequential subtrees.
  5954. This is a short-hand for marking the subtree and then copying it.
  5955. If CUT is non-nil, actually cut the subtree."
  5956. (interactive "p")
  5957. (let (beg end folded (beg0 (point)))
  5958. (if (interactive-p)
  5959. (org-back-to-heading nil) ; take what looks like a subtree
  5960. (org-back-to-heading t)) ; take what is really there
  5961. (org-back-over-empty-lines)
  5962. (setq beg (point))
  5963. (skip-chars-forward " \t\r\n")
  5964. (save-match-data
  5965. (save-excursion (outline-end-of-heading)
  5966. (setq folded (org-invisible-p)))
  5967. (condition-case nil
  5968. (outline-forward-same-level (1- n))
  5969. (error nil))
  5970. (org-end-of-subtree t t))
  5971. (org-back-over-empty-lines)
  5972. (setq end (point))
  5973. (goto-char beg0)
  5974. (when (> end beg)
  5975. (setq org-subtree-clip-folded folded)
  5976. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5977. (setq org-subtree-clip (current-kill 0))
  5978. (message "%s: Subtree(s) with %d characters"
  5979. (if cut "Cut" "Copied")
  5980. (length org-subtree-clip)))))
  5981. (defun org-paste-subtree (&optional level tree)
  5982. "Paste the clipboard as a subtree, with modification of headline level.
  5983. The entire subtree is promoted or demoted in order to match a new headline
  5984. level. By default, the new level is derived from the visible headings
  5985. before and after the insertion point, and taken to be the inferior headline
  5986. level of the two. So if the previous visible heading is level 3 and the
  5987. next is level 4 (or vice versa), level 4 will be used for insertion.
  5988. This makes sure that the subtree remains an independent subtree and does
  5989. not swallow low level entries.
  5990. You can also force a different level, either by using a numeric prefix
  5991. argument, or by inserting the heading marker by hand. For example, if the
  5992. cursor is after \"*****\", then the tree will be shifted to level 5.
  5993. If you want to insert the tree as is, just use \\[yank].
  5994. If optional TREE is given, use this text instead of the kill ring."
  5995. (interactive "P")
  5996. (unless (org-kill-is-subtree-p tree)
  5997. (error "%s"
  5998. (substitute-command-keys
  5999. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6000. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  6001. (^re (concat "^\\(" outline-regexp "\\)"))
  6002. (re (concat "\\(" outline-regexp "\\)"))
  6003. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6004. (old-level (if (string-match ^re txt)
  6005. (- (match-end 0) (match-beginning 0) 1)
  6006. -1))
  6007. (force-level (cond (level (prefix-numeric-value level))
  6008. ((string-match
  6009. ^re_ (buffer-substring (point-at-bol) (point)))
  6010. (- (match-end 1) (match-beginning 1)))
  6011. (t nil)))
  6012. (previous-level (save-excursion
  6013. (condition-case nil
  6014. (progn
  6015. (outline-previous-visible-heading 1)
  6016. (if (looking-at re)
  6017. (- (match-end 0) (match-beginning 0) 1)
  6018. 1))
  6019. (error 1))))
  6020. (next-level (save-excursion
  6021. (condition-case nil
  6022. (progn
  6023. (or (looking-at outline-regexp)
  6024. (outline-next-visible-heading 1))
  6025. (if (looking-at re)
  6026. (- (match-end 0) (match-beginning 0) 1)
  6027. 1))
  6028. (error 1))))
  6029. (new-level (or force-level (max previous-level next-level)))
  6030. (shift (if (or (= old-level -1)
  6031. (= new-level -1)
  6032. (= old-level new-level))
  6033. 0
  6034. (- new-level old-level)))
  6035. (delta (if (> shift 0) -1 1))
  6036. (func (if (> shift 0) 'org-demote 'org-promote))
  6037. (org-odd-levels-only nil)
  6038. beg end)
  6039. ;; Remove the forced level indicator
  6040. (if force-level
  6041. (delete-region (point-at-bol) (point)))
  6042. ;; Paste
  6043. (beginning-of-line 1)
  6044. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6045. (setq beg (point))
  6046. (insert-before-markers txt) ;; FIXME: correct fix????
  6047. (unless (string-match "\n\\'" txt) (insert "\n"))
  6048. (setq end (point))
  6049. (goto-char beg)
  6050. (skip-chars-forward " \t\n\r")
  6051. (setq beg (point))
  6052. ;; Shift if necessary
  6053. (unless (= shift 0)
  6054. (save-restriction
  6055. (narrow-to-region beg end)
  6056. (while (not (= shift 0))
  6057. (org-map-region func (point-min) (point-max))
  6058. (setq shift (+ delta shift)))
  6059. (goto-char (point-min))))
  6060. (when (interactive-p)
  6061. (message "Clipboard pasted as level %d subtree" new-level))
  6062. (if (and kill-ring
  6063. (eq org-subtree-clip (current-kill 0))
  6064. org-subtree-clip-folded)
  6065. ;; The tree was folded before it was killed/copied
  6066. (hide-subtree))))
  6067. (defun org-kill-is-subtree-p (&optional txt)
  6068. "Check if the current kill is an outline subtree, or a set of trees.
  6069. Returns nil if kill does not start with a headline, or if the first
  6070. headline level is not the largest headline level in the tree.
  6071. So this will actually accept several entries of equal levels as well,
  6072. which is OK for `org-paste-subtree'.
  6073. If optional TXT is given, check this string instead of the current kill."
  6074. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6075. (start-level (and kill
  6076. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6077. org-outline-regexp "\\)")
  6078. kill)
  6079. (- (match-end 2) (match-beginning 2) 1)))
  6080. (re (concat "^" org-outline-regexp))
  6081. (start (1+ (match-beginning 2))))
  6082. (if (not start-level)
  6083. (progn
  6084. nil) ;; does not even start with a heading
  6085. (catch 'exit
  6086. (while (setq start (string-match re kill (1+ start)))
  6087. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6088. (throw 'exit nil)))
  6089. t))))
  6090. (defun org-narrow-to-subtree ()
  6091. "Narrow buffer to the current subtree."
  6092. (interactive)
  6093. (save-excursion
  6094. (save-match-data
  6095. (narrow-to-region
  6096. (progn (org-back-to-heading) (point))
  6097. (progn (org-end-of-subtree t t) (point))))))
  6098. ;;; Outline Sorting
  6099. (defun org-sort (with-case)
  6100. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6101. Optional argument WITH-CASE means sort case-sensitively."
  6102. (interactive "P")
  6103. (if (org-at-table-p)
  6104. (org-call-with-arg 'org-table-sort-lines with-case)
  6105. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6106. (defvar org-priority-regexp) ; defined later in the file
  6107. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6108. "Sort entries on a certain level of an outline tree.
  6109. If there is an active region, the entries in the region are sorted.
  6110. Else, if the cursor is before the first entry, sort the top-level items.
  6111. Else, the children of the entry at point are sorted.
  6112. Sorting can be alphabetically, numerically, and by date/time as given by
  6113. the first time stamp in the entry. The command prompts for the sorting
  6114. type unless it has been given to the function through the SORTING-TYPE
  6115. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6116. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6117. called with point at the beginning of the record. It must return either
  6118. a string or a number that should serve as the sorting key for that record.
  6119. Comparing entries ignores case by default. However, with an optional argument
  6120. WITH-CASE, the sorting considers case as well."
  6121. (interactive "P")
  6122. (let ((case-func (if with-case 'identity 'downcase))
  6123. start beg end stars re re2
  6124. txt what tmp plain-list-p)
  6125. ;; Find beginning and end of region to sort
  6126. (cond
  6127. ((org-region-active-p)
  6128. ;; we will sort the region
  6129. (setq end (region-end)
  6130. what "region")
  6131. (goto-char (region-beginning))
  6132. (if (not (org-on-heading-p)) (outline-next-heading))
  6133. (setq start (point)))
  6134. ((org-at-item-p)
  6135. ;; we will sort this plain list
  6136. (org-beginning-of-item-list) (setq start (point))
  6137. (org-end-of-item-list) (setq end (point))
  6138. (goto-char start)
  6139. (setq plain-list-p t
  6140. what "plain list"))
  6141. ((or (org-on-heading-p)
  6142. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6143. ;; we will sort the children of the current headline
  6144. (org-back-to-heading)
  6145. (setq start (point)
  6146. end (progn (org-end-of-subtree t t)
  6147. (org-back-over-empty-lines)
  6148. (point))
  6149. what "children")
  6150. (goto-char start)
  6151. (show-subtree)
  6152. (outline-next-heading))
  6153. (t
  6154. ;; we will sort the top-level entries in this file
  6155. (goto-char (point-min))
  6156. (or (org-on-heading-p) (outline-next-heading))
  6157. (setq start (point) end (point-max) what "top-level")
  6158. (goto-char start)
  6159. (show-all)))
  6160. (setq beg (point))
  6161. (if (>= beg end) (error "Nothing to sort"))
  6162. (unless plain-list-p
  6163. (looking-at "\\(\\*+\\)")
  6164. (setq stars (match-string 1)
  6165. re (concat "^" (regexp-quote stars) " +")
  6166. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6167. txt (buffer-substring beg end))
  6168. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6169. (if (and (not (equal stars "*")) (string-match re2 txt))
  6170. (error "Region to sort contains a level above the first entry")))
  6171. (unless sorting-type
  6172. (message
  6173. (if plain-list-p
  6174. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6175. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6176. what)
  6177. (setq sorting-type (read-char-exclusive))
  6178. (and (= (downcase sorting-type) ?f)
  6179. (setq getkey-func
  6180. (completing-read "Sort using function: "
  6181. obarray 'fboundp t nil nil))
  6182. (setq getkey-func (intern getkey-func)))
  6183. (and (= (downcase sorting-type) ?r)
  6184. (setq property
  6185. (completing-read "Property: "
  6186. (mapcar 'list (org-buffer-property-keys t))
  6187. nil t))))
  6188. (message "Sorting entries...")
  6189. (save-restriction
  6190. (narrow-to-region start end)
  6191. (let ((dcst (downcase sorting-type))
  6192. (now (current-time)))
  6193. (sort-subr
  6194. (/= dcst sorting-type)
  6195. ;; This function moves to the beginning character of the "record" to
  6196. ;; be sorted.
  6197. (if plain-list-p
  6198. (lambda nil
  6199. (if (org-at-item-p) t (goto-char (point-max))))
  6200. (lambda nil
  6201. (if (re-search-forward re nil t)
  6202. (goto-char (match-beginning 0))
  6203. (goto-char (point-max)))))
  6204. ;; This function moves to the last character of the "record" being
  6205. ;; sorted.
  6206. (if plain-list-p
  6207. 'org-end-of-item
  6208. (lambda nil
  6209. (save-match-data
  6210. (condition-case nil
  6211. (outline-forward-same-level 1)
  6212. (error
  6213. (goto-char (point-max)))))))
  6214. ;; This function returns the value that gets sorted against.
  6215. (if plain-list-p
  6216. (lambda nil
  6217. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6218. (cond
  6219. ((= dcst ?n)
  6220. (string-to-number (buffer-substring (match-end 0)
  6221. (point-at-eol))))
  6222. ((= dcst ?a)
  6223. (buffer-substring (match-end 0) (point-at-eol)))
  6224. ((= dcst ?t)
  6225. (if (re-search-forward org-ts-regexp
  6226. (point-at-eol) t)
  6227. (org-time-string-to-time (match-string 0))
  6228. now))
  6229. ((= dcst ?f)
  6230. (if getkey-func
  6231. (progn
  6232. (setq tmp (funcall getkey-func))
  6233. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6234. tmp)
  6235. (error "Invalid key function `%s'" getkey-func)))
  6236. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6237. (lambda nil
  6238. (cond
  6239. ((= dcst ?n)
  6240. (if (looking-at outline-regexp)
  6241. (string-to-number (buffer-substring (match-end 0)
  6242. (point-at-eol)))
  6243. nil))
  6244. ((= dcst ?a)
  6245. (funcall case-func (buffer-substring (point-at-bol)
  6246. (point-at-eol))))
  6247. ((= dcst ?t)
  6248. (if (re-search-forward org-ts-regexp
  6249. (save-excursion
  6250. (forward-line 2)
  6251. (point)) t)
  6252. (org-time-string-to-time (match-string 0))
  6253. now))
  6254. ((= dcst ?p)
  6255. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6256. (string-to-char (match-string 2))
  6257. org-default-priority))
  6258. ((= dcst ?r)
  6259. (or (org-entry-get nil property) ""))
  6260. ((= dcst ?f)
  6261. (if getkey-func
  6262. (progn
  6263. (setq tmp (funcall getkey-func))
  6264. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6265. tmp)
  6266. (error "Invalid key function `%s'" getkey-func)))
  6267. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6268. nil
  6269. (cond
  6270. ((= dcst ?a) 'string<)
  6271. ((= dcst ?t) 'time-less-p)
  6272. (t nil)))))
  6273. (message "Sorting entries...done")))
  6274. (defun org-do-sort (table what &optional with-case sorting-type)
  6275. "Sort TABLE of WHAT according to SORTING-TYPE.
  6276. The user will be prompted for the SORTING-TYPE if the call to this
  6277. function does not specify it. WHAT is only for the prompt, to indicate
  6278. what is being sorted. The sorting key will be extracted from
  6279. the car of the elements of the table.
  6280. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6281. (unless sorting-type
  6282. (message
  6283. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6284. what)
  6285. (setq sorting-type (read-char-exclusive)))
  6286. (let ((dcst (downcase sorting-type))
  6287. extractfun comparefun)
  6288. ;; Define the appropriate functions
  6289. (cond
  6290. ((= dcst ?n)
  6291. (setq extractfun 'string-to-number
  6292. comparefun (if (= dcst sorting-type) '< '>)))
  6293. ((= dcst ?a)
  6294. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6295. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6296. comparefun (if (= dcst sorting-type)
  6297. 'string<
  6298. (lambda (a b) (and (not (string< a b))
  6299. (not (string= a b)))))))
  6300. ((= dcst ?t)
  6301. (setq extractfun
  6302. (lambda (x)
  6303. (if (string-match org-ts-regexp x)
  6304. (time-to-seconds
  6305. (org-time-string-to-time (match-string 0 x)))
  6306. 0))
  6307. comparefun (if (= dcst sorting-type) '< '>)))
  6308. (t (error "Invalid sorting type `%c'" sorting-type)))
  6309. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6310. table)
  6311. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6312. ;;;; Plain list items, including checkboxes
  6313. ;;; Plain list items
  6314. (defun org-at-item-p ()
  6315. "Is point in a line starting a hand-formatted item?"
  6316. (let ((llt org-plain-list-ordered-item-terminator))
  6317. (save-excursion
  6318. (goto-char (point-at-bol))
  6319. (looking-at
  6320. (cond
  6321. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6322. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6323. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6324. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6325. (defun org-in-item-p ()
  6326. "It the cursor inside a plain list item.
  6327. Does not have to be the first line."
  6328. (save-excursion
  6329. (condition-case nil
  6330. (progn
  6331. (org-beginning-of-item)
  6332. (org-at-item-p)
  6333. t)
  6334. (error nil))))
  6335. (defun org-insert-item (&optional checkbox)
  6336. "Insert a new item at the current level.
  6337. Return t when things worked, nil when we are not in an item."
  6338. (when (save-excursion
  6339. (condition-case nil
  6340. (progn
  6341. (org-beginning-of-item)
  6342. (org-at-item-p)
  6343. (if (org-invisible-p) (error "Invisible item"))
  6344. t)
  6345. (error nil)))
  6346. (let* ((bul (match-string 0))
  6347. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6348. (match-end 0)))
  6349. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6350. pos)
  6351. (cond
  6352. ((and (org-at-item-p) (<= (point) eow))
  6353. ;; before the bullet
  6354. (beginning-of-line 1)
  6355. (open-line (if blank 2 1)))
  6356. ((<= (point) eow)
  6357. (beginning-of-line 1))
  6358. (t
  6359. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6360. (end-of-line 1)
  6361. (delete-horizontal-space))
  6362. (newline (if blank 2 1))))
  6363. (insert bul (if checkbox "[ ]" ""))
  6364. (just-one-space)
  6365. (setq pos (point))
  6366. (end-of-line 1)
  6367. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6368. (org-maybe-renumber-ordered-list)
  6369. (and checkbox (org-update-checkbox-count-maybe))
  6370. t))
  6371. ;;; Checkboxes
  6372. (defun org-at-item-checkbox-p ()
  6373. "Is point at a line starting a plain-list item with a checklet?"
  6374. (and (org-at-item-p)
  6375. (save-excursion
  6376. (goto-char (match-end 0))
  6377. (skip-chars-forward " \t")
  6378. (looking-at "\\[[- X]\\]"))))
  6379. (defun org-toggle-checkbox (&optional arg)
  6380. "Toggle the checkbox in the current line."
  6381. (interactive "P")
  6382. (catch 'exit
  6383. (let (beg end status (firstnew 'unknown))
  6384. (cond
  6385. ((org-region-active-p)
  6386. (setq beg (region-beginning) end (region-end)))
  6387. ((org-on-heading-p)
  6388. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6389. ((org-at-item-checkbox-p)
  6390. (let ((pos (point)))
  6391. (replace-match
  6392. (cond (arg "[-]")
  6393. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6394. (t "[ ]"))
  6395. t t)
  6396. (goto-char pos))
  6397. (throw 'exit t))
  6398. (t (error "Not at a checkbox or heading, and no active region")))
  6399. (save-excursion
  6400. (goto-char beg)
  6401. (while (< (point) end)
  6402. (when (org-at-item-checkbox-p)
  6403. (setq status (equal (match-string 0) "[X]"))
  6404. (when (eq firstnew 'unknown)
  6405. (setq firstnew (not status)))
  6406. (replace-match
  6407. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6408. (beginning-of-line 2)))))
  6409. (org-update-checkbox-count-maybe))
  6410. (defun org-update-checkbox-count-maybe ()
  6411. "Update checkbox statistics unless turned off by user."
  6412. (when org-provide-checkbox-statistics
  6413. (org-update-checkbox-count)))
  6414. (defun org-update-checkbox-count (&optional all)
  6415. "Update the checkbox statistics in the current section.
  6416. This will find all statistic cookies like [57%] and [6/12] and update them
  6417. with the current numbers. With optional prefix argument ALL, do this for
  6418. the whole buffer."
  6419. (interactive "P")
  6420. (save-excursion
  6421. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6422. (beg (condition-case nil
  6423. (progn (outline-back-to-heading) (point))
  6424. (error (point-min))))
  6425. (end (move-marker (make-marker)
  6426. (progn (outline-next-heading) (point))))
  6427. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6428. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6429. (re-find (concat re "\\|" re-box))
  6430. beg-cookie end-cookie is-percent c-on c-off lim
  6431. eline curr-ind next-ind continue-from startsearch
  6432. (cstat 0)
  6433. )
  6434. (when all
  6435. (goto-char (point-min))
  6436. (outline-next-heading)
  6437. (setq beg (point) end (point-max)))
  6438. (goto-char end)
  6439. ;; find each statistic cookie
  6440. (while (re-search-backward re-find beg t)
  6441. (setq beg-cookie (match-beginning 1)
  6442. end-cookie (match-end 1)
  6443. cstat (+ cstat (if end-cookie 1 0))
  6444. startsearch (point-at-eol)
  6445. continue-from (point-at-bol)
  6446. is-percent (match-beginning 2)
  6447. lim (cond
  6448. ((org-on-heading-p) (outline-next-heading) (point))
  6449. ((org-at-item-p) (org-end-of-item) (point))
  6450. (t nil))
  6451. c-on 0
  6452. c-off 0)
  6453. (when lim
  6454. ;; find first checkbox for this cookie and gather
  6455. ;; statistics from all that are at this indentation level
  6456. (goto-char startsearch)
  6457. (if (re-search-forward re-box lim t)
  6458. (progn
  6459. (org-beginning-of-item)
  6460. (setq curr-ind (org-get-indentation))
  6461. (setq next-ind curr-ind)
  6462. (while (= curr-ind next-ind)
  6463. (save-excursion (end-of-line) (setq eline (point)))
  6464. (if (re-search-forward re-box eline t)
  6465. (if (member (match-string 2) '("[ ]" "[-]"))
  6466. (setq c-off (1+ c-off))
  6467. (setq c-on (1+ c-on))
  6468. )
  6469. )
  6470. (org-end-of-item)
  6471. (setq next-ind (org-get-indentation))
  6472. )))
  6473. (goto-char continue-from)
  6474. ;; update cookie
  6475. (when end-cookie
  6476. (delete-region beg-cookie end-cookie)
  6477. (goto-char beg-cookie)
  6478. (insert
  6479. (if is-percent
  6480. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6481. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6482. ;; update items checkbox if it has one
  6483. (when (org-at-item-p)
  6484. (org-beginning-of-item)
  6485. (when (and (> (+ c-on c-off) 0)
  6486. (re-search-forward re-box (point-at-eol) t))
  6487. (setq beg-cookie (match-beginning 2)
  6488. end-cookie (match-end 2))
  6489. (delete-region beg-cookie end-cookie)
  6490. (goto-char beg-cookie)
  6491. (cond ((= c-off 0) (insert "[X]"))
  6492. ((= c-on 0) (insert "[ ]"))
  6493. (t (insert "[-]")))
  6494. )))
  6495. (goto-char continue-from))
  6496. (when (interactive-p)
  6497. (message "Checkbox satistics updated %s (%d places)"
  6498. (if all "in entire file" "in current outline entry") cstat)))))
  6499. (defun org-get-checkbox-statistics-face ()
  6500. "Select the face for checkbox statistics.
  6501. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6502. it will be `org-todo'."
  6503. (if (match-end 1)
  6504. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6505. (if (and (> (match-end 2) (match-beginning 2))
  6506. (equal (match-string 2) (match-string 3)))
  6507. 'org-done
  6508. 'org-todo)))
  6509. (defun org-get-indentation (&optional line)
  6510. "Get the indentation of the current line, interpreting tabs.
  6511. When LINE is given, assume it represents a line and compute its indentation."
  6512. (if line
  6513. (if (string-match "^ *" (org-remove-tabs line))
  6514. (match-end 0))
  6515. (save-excursion
  6516. (beginning-of-line 1)
  6517. (skip-chars-forward " \t")
  6518. (current-column))))
  6519. (defun org-remove-tabs (s &optional width)
  6520. "Replace tabulators in S with spaces.
  6521. Assumes that s is a single line, starting in column 0."
  6522. (setq width (or width tab-width))
  6523. (while (string-match "\t" s)
  6524. (setq s (replace-match
  6525. (make-string
  6526. (- (* width (/ (+ (match-beginning 0) width) width))
  6527. (match-beginning 0)) ?\ )
  6528. t t s)))
  6529. s)
  6530. (defun org-fix-indentation (line ind)
  6531. "Fix indentation in LINE.
  6532. IND is a cons cell with target and minimum indentation.
  6533. If the current indenation in LINE is smaller than the minimum,
  6534. leave it alone. If it is larger than ind, set it to the target."
  6535. (let* ((l (org-remove-tabs line))
  6536. (i (org-get-indentation l))
  6537. (i1 (car ind)) (i2 (cdr ind)))
  6538. (if (>= i i2) (setq l (substring line i2)))
  6539. (if (> i1 0)
  6540. (concat (make-string i1 ?\ ) l)
  6541. l)))
  6542. (defcustom org-empty-line-terminates-plain-lists nil
  6543. "Non-nil means, an empty line ends all plain list levels.
  6544. When nil, empty lines are part of the preceeding item."
  6545. :group 'org-plain-lists
  6546. :type 'boolean)
  6547. (defun org-beginning-of-item ()
  6548. "Go to the beginning of the current hand-formatted item.
  6549. If the cursor is not in an item, throw an error."
  6550. (interactive)
  6551. (let ((pos (point))
  6552. (limit (save-excursion
  6553. (condition-case nil
  6554. (progn
  6555. (org-back-to-heading)
  6556. (beginning-of-line 2) (point))
  6557. (error (point-min)))))
  6558. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6559. ind ind1)
  6560. (if (org-at-item-p)
  6561. (beginning-of-line 1)
  6562. (beginning-of-line 1)
  6563. (skip-chars-forward " \t")
  6564. (setq ind (current-column))
  6565. (if (catch 'exit
  6566. (while t
  6567. (beginning-of-line 0)
  6568. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6569. (if (looking-at "[ \t]*$")
  6570. (setq ind1 ind-empty)
  6571. (skip-chars-forward " \t")
  6572. (setq ind1 (current-column)))
  6573. (if (< ind1 ind)
  6574. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6575. nil
  6576. (goto-char pos)
  6577. (error "Not in an item")))))
  6578. (defun org-end-of-item ()
  6579. "Go to the end of the current hand-formatted item.
  6580. If the cursor is not in an item, throw an error."
  6581. (interactive)
  6582. (let* ((pos (point))
  6583. ind1
  6584. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6585. (limit (save-excursion (outline-next-heading) (point)))
  6586. (ind (save-excursion
  6587. (org-beginning-of-item)
  6588. (skip-chars-forward " \t")
  6589. (current-column)))
  6590. (end (catch 'exit
  6591. (while t
  6592. (beginning-of-line 2)
  6593. (if (eobp) (throw 'exit (point)))
  6594. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6595. (if (looking-at "[ \t]*$")
  6596. (setq ind1 ind-empty)
  6597. (skip-chars-forward " \t")
  6598. (setq ind1 (current-column)))
  6599. (if (<= ind1 ind)
  6600. (throw 'exit (point-at-bol)))))))
  6601. (if end
  6602. (goto-char end)
  6603. (goto-char pos)
  6604. (error "Not in an item"))))
  6605. (defun org-next-item ()
  6606. "Move to the beginning of the next item in the current plain list.
  6607. Error if not at a plain list, or if this is the last item in the list."
  6608. (interactive)
  6609. (let (ind ind1 (pos (point)))
  6610. (org-beginning-of-item)
  6611. (setq ind (org-get-indentation))
  6612. (org-end-of-item)
  6613. (setq ind1 (org-get-indentation))
  6614. (unless (and (org-at-item-p) (= ind ind1))
  6615. (goto-char pos)
  6616. (error "On last item"))))
  6617. (defun org-previous-item ()
  6618. "Move to the beginning of the previous item in the current plain list.
  6619. Error if not at a plain list, or if this is the first item in the list."
  6620. (interactive)
  6621. (let (beg ind ind1 (pos (point)))
  6622. (org-beginning-of-item)
  6623. (setq beg (point))
  6624. (setq ind (org-get-indentation))
  6625. (goto-char beg)
  6626. (catch 'exit
  6627. (while t
  6628. (beginning-of-line 0)
  6629. (if (looking-at "[ \t]*$")
  6630. nil
  6631. (if (<= (setq ind1 (org-get-indentation)) ind)
  6632. (throw 'exit t)))))
  6633. (condition-case nil
  6634. (if (or (not (org-at-item-p))
  6635. (< ind1 (1- ind)))
  6636. (error "")
  6637. (org-beginning-of-item))
  6638. (error (goto-char pos)
  6639. (error "On first item")))))
  6640. (defun org-first-list-item-p ()
  6641. "Is this heading the item in a plain list?"
  6642. (unless (org-at-item-p)
  6643. (error "Not at a plain list item"))
  6644. (org-beginning-of-item)
  6645. (= (point) (save-excursion (org-beginning-of-item-list))))
  6646. (defun org-move-item-down ()
  6647. "Move the plain list item at point down, i.e. swap with following item.
  6648. Subitems (items with larger indentation) are considered part of the item,
  6649. so this really moves item trees."
  6650. (interactive)
  6651. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6652. (org-beginning-of-item)
  6653. (setq beg0 (point))
  6654. (save-excursion
  6655. (setq ne-beg (org-back-over-empty-lines))
  6656. (setq beg (point)))
  6657. (goto-char beg0)
  6658. (setq ind (org-get-indentation))
  6659. (org-end-of-item)
  6660. (setq end0 (point))
  6661. (setq ind1 (org-get-indentation))
  6662. (setq ne-end (org-back-over-empty-lines))
  6663. (setq end (point))
  6664. (goto-char beg0)
  6665. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6666. ;; include less whitespace
  6667. (save-excursion
  6668. (goto-char beg)
  6669. (forward-line (- ne-beg ne-end))
  6670. (setq beg (point))))
  6671. (goto-char end0)
  6672. (if (and (org-at-item-p) (= ind ind1))
  6673. (progn
  6674. (org-end-of-item)
  6675. (org-back-over-empty-lines)
  6676. (setq txt (buffer-substring beg end))
  6677. (save-excursion
  6678. (delete-region beg end))
  6679. (setq pos (point))
  6680. (insert txt)
  6681. (goto-char pos) (org-skip-whitespace)
  6682. (org-maybe-renumber-ordered-list))
  6683. (goto-char pos)
  6684. (error "Cannot move this item further down"))))
  6685. (defun org-move-item-up (arg)
  6686. "Move the plain list item at point up, i.e. swap with previous item.
  6687. Subitems (items with larger indentation) are considered part of the item,
  6688. so this really moves item trees."
  6689. (interactive "p")
  6690. (let (beg beg0 end ind ind1 (pos (point)) txt
  6691. ne-beg ne-ins ins-end)
  6692. (org-beginning-of-item)
  6693. (setq beg0 (point))
  6694. (setq ind (org-get-indentation))
  6695. (save-excursion
  6696. (setq ne-beg (org-back-over-empty-lines))
  6697. (setq beg (point)))
  6698. (goto-char beg0)
  6699. (org-end-of-item)
  6700. (setq end (point))
  6701. (goto-char beg0)
  6702. (catch 'exit
  6703. (while t
  6704. (beginning-of-line 0)
  6705. (if (looking-at "[ \t]*$")
  6706. (if org-empty-line-terminates-plain-lists
  6707. (progn
  6708. (goto-char pos)
  6709. (error "Cannot move this item further up"))
  6710. nil)
  6711. (if (<= (setq ind1 (org-get-indentation)) ind)
  6712. (throw 'exit t)))))
  6713. (condition-case nil
  6714. (org-beginning-of-item)
  6715. (error (goto-char beg)
  6716. (error "Cannot move this item further up")))
  6717. (setq ind1 (org-get-indentation))
  6718. (if (and (org-at-item-p) (= ind ind1))
  6719. (progn
  6720. (setq ne-ins (org-back-over-empty-lines))
  6721. (setq txt (buffer-substring beg end))
  6722. (save-excursion
  6723. (delete-region beg end))
  6724. (setq pos (point))
  6725. (insert txt)
  6726. (setq ins-end (point))
  6727. (goto-char pos) (org-skip-whitespace)
  6728. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6729. ;; Move whitespace back to beginning
  6730. (save-excursion
  6731. (goto-char ins-end)
  6732. (let ((kill-whole-line t))
  6733. (kill-line (- ne-ins ne-beg)) (point)))
  6734. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6735. (org-maybe-renumber-ordered-list))
  6736. (goto-char pos)
  6737. (error "Cannot move this item further up"))))
  6738. (defun org-maybe-renumber-ordered-list ()
  6739. "Renumber the ordered list at point if setup allows it.
  6740. This tests the user option `org-auto-renumber-ordered-lists' before
  6741. doing the renumbering."
  6742. (interactive)
  6743. (when (and org-auto-renumber-ordered-lists
  6744. (org-at-item-p))
  6745. (if (match-beginning 3)
  6746. (org-renumber-ordered-list 1)
  6747. (org-fix-bullet-type))))
  6748. (defun org-maybe-renumber-ordered-list-safe ()
  6749. (condition-case nil
  6750. (save-excursion
  6751. (org-maybe-renumber-ordered-list))
  6752. (error nil)))
  6753. (defun org-cycle-list-bullet (&optional which)
  6754. "Cycle through the different itemize/enumerate bullets.
  6755. This cycle the entire list level through the sequence:
  6756. `-' -> `+' -> `*' -> `1.' -> `1)'
  6757. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6758. 0 meand `-', 1 means `+' etc."
  6759. (interactive "P")
  6760. (org-preserve-lc
  6761. (org-beginning-of-item-list)
  6762. (org-at-item-p)
  6763. (beginning-of-line 1)
  6764. (let ((current (match-string 0))
  6765. (prevp (eq which 'previous))
  6766. new)
  6767. (setq new (cond
  6768. ((and (numberp which)
  6769. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6770. ((string-match "-" current) (if prevp "1)" "+"))
  6771. ((string-match "\\+" current)
  6772. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6773. ((string-match "\\*" current) (if prevp "+" "1."))
  6774. ((string-match "\\." current) (if prevp "*" "1)"))
  6775. ((string-match ")" current) (if prevp "1." "-"))
  6776. (t (error "This should not happen"))))
  6777. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6778. (org-fix-bullet-type)
  6779. (org-maybe-renumber-ordered-list))))
  6780. (defun org-get-string-indentation (s)
  6781. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6782. (let ((n -1) (i 0) (w tab-width) c)
  6783. (catch 'exit
  6784. (while (< (setq n (1+ n)) (length s))
  6785. (setq c (aref s n))
  6786. (cond ((= c ?\ ) (setq i (1+ i)))
  6787. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6788. (t (throw 'exit t)))))
  6789. i))
  6790. (defun org-renumber-ordered-list (arg)
  6791. "Renumber an ordered plain list.
  6792. Cursor needs to be in the first line of an item, the line that starts
  6793. with something like \"1.\" or \"2)\"."
  6794. (interactive "p")
  6795. (unless (and (org-at-item-p)
  6796. (match-beginning 3))
  6797. (error "This is not an ordered list"))
  6798. (let ((line (org-current-line))
  6799. (col (current-column))
  6800. (ind (org-get-string-indentation
  6801. (buffer-substring (point-at-bol) (match-beginning 3))))
  6802. ;; (term (substring (match-string 3) -1))
  6803. ind1 (n (1- arg))
  6804. fmt)
  6805. ;; find where this list begins
  6806. (org-beginning-of-item-list)
  6807. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6808. (setq fmt (concat "%d" (match-string 1)))
  6809. (beginning-of-line 0)
  6810. ;; walk forward and replace these numbers
  6811. (catch 'exit
  6812. (while t
  6813. (catch 'next
  6814. (beginning-of-line 2)
  6815. (if (eobp) (throw 'exit nil))
  6816. (if (looking-at "[ \t]*$") (throw 'next nil))
  6817. (skip-chars-forward " \t") (setq ind1 (current-column))
  6818. (if (> ind1 ind) (throw 'next t))
  6819. (if (< ind1 ind) (throw 'exit t))
  6820. (if (not (org-at-item-p)) (throw 'exit nil))
  6821. (delete-region (match-beginning 2) (match-end 2))
  6822. (goto-char (match-beginning 2))
  6823. (insert (format fmt (setq n (1+ n)))))))
  6824. (goto-line line)
  6825. (move-to-column col)))
  6826. (defun org-fix-bullet-type ()
  6827. "Make sure all items in this list have the same bullet as the firsst item."
  6828. (interactive)
  6829. (unless (org-at-item-p) (error "This is not a list"))
  6830. (let ((line (org-current-line))
  6831. (col (current-column))
  6832. (ind (current-indentation))
  6833. ind1 bullet)
  6834. ;; find where this list begins
  6835. (org-beginning-of-item-list)
  6836. (beginning-of-line 1)
  6837. ;; find out what the bullet type is
  6838. (looking-at "[ \t]*\\(\\S-+\\)")
  6839. (setq bullet (match-string 1))
  6840. ;; walk forward and replace these numbers
  6841. (beginning-of-line 0)
  6842. (catch 'exit
  6843. (while t
  6844. (catch 'next
  6845. (beginning-of-line 2)
  6846. (if (eobp) (throw 'exit nil))
  6847. (if (looking-at "[ \t]*$") (throw 'next nil))
  6848. (skip-chars-forward " \t") (setq ind1 (current-column))
  6849. (if (> ind1 ind) (throw 'next t))
  6850. (if (< ind1 ind) (throw 'exit t))
  6851. (if (not (org-at-item-p)) (throw 'exit nil))
  6852. (skip-chars-forward " \t")
  6853. (looking-at "\\S-+")
  6854. (replace-match bullet))))
  6855. (goto-line line)
  6856. (move-to-column col)
  6857. (if (string-match "[0-9]" bullet)
  6858. (org-renumber-ordered-list 1))))
  6859. (defun org-beginning-of-item-list ()
  6860. "Go to the beginning of the current item list.
  6861. I.e. to the first item in this list."
  6862. (interactive)
  6863. (org-beginning-of-item)
  6864. (let ((pos (point-at-bol))
  6865. (ind (org-get-indentation))
  6866. ind1)
  6867. ;; find where this list begins
  6868. (catch 'exit
  6869. (while t
  6870. (catch 'next
  6871. (beginning-of-line 0)
  6872. (if (looking-at "[ \t]*$")
  6873. (throw (if (bobp) 'exit 'next) t))
  6874. (skip-chars-forward " \t") (setq ind1 (current-column))
  6875. (if (or (< ind1 ind)
  6876. (and (= ind1 ind)
  6877. (not (org-at-item-p)))
  6878. (bobp))
  6879. (throw 'exit t)
  6880. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6881. (goto-char pos)))
  6882. (defun org-end-of-item-list ()
  6883. "Go to the end of the current item list.
  6884. I.e. to the text after the last item."
  6885. (interactive)
  6886. (org-beginning-of-item)
  6887. (let ((pos (point-at-bol))
  6888. (ind (org-get-indentation))
  6889. ind1)
  6890. ;; find where this list begins
  6891. (catch 'exit
  6892. (while t
  6893. (catch 'next
  6894. (beginning-of-line 2)
  6895. (if (looking-at "[ \t]*$")
  6896. (throw (if (eobp) 'exit 'next) t))
  6897. (skip-chars-forward " \t") (setq ind1 (current-column))
  6898. (if (or (< ind1 ind)
  6899. (and (= ind1 ind)
  6900. (not (org-at-item-p)))
  6901. (eobp))
  6902. (progn
  6903. (setq pos (point-at-bol))
  6904. (throw 'exit t))))))
  6905. (goto-char pos)))
  6906. (defvar org-last-indent-begin-marker (make-marker))
  6907. (defvar org-last-indent-end-marker (make-marker))
  6908. (defun org-outdent-item (arg)
  6909. "Outdent a local list item."
  6910. (interactive "p")
  6911. (org-indent-item (- arg)))
  6912. (defun org-indent-item (arg)
  6913. "Indent a local list item."
  6914. (interactive "p")
  6915. (unless (org-at-item-p)
  6916. (error "Not on an item"))
  6917. (save-excursion
  6918. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6919. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6920. (setq beg org-last-indent-begin-marker
  6921. end org-last-indent-end-marker)
  6922. (org-beginning-of-item)
  6923. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6924. (org-end-of-item)
  6925. (setq end (move-marker org-last-indent-end-marker (point))))
  6926. (goto-char beg)
  6927. (setq tmp (org-item-indent-positions)
  6928. ind (car tmp)
  6929. ind-down (nth 2 tmp)
  6930. ind-up (nth 1 tmp)
  6931. delta (if (> arg 0)
  6932. (if ind-down (- ind-down ind) 2)
  6933. (if ind-up (- ind-up ind) -2)))
  6934. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6935. (while (< (point) end)
  6936. (beginning-of-line 1)
  6937. (skip-chars-forward " \t") (setq ind1 (current-column))
  6938. (delete-region (point-at-bol) (point))
  6939. (or (eolp) (indent-to-column (+ ind1 delta)))
  6940. (beginning-of-line 2))))
  6941. (org-fix-bullet-type)
  6942. (org-maybe-renumber-ordered-list-safe)
  6943. (save-excursion
  6944. (beginning-of-line 0)
  6945. (condition-case nil (org-beginning-of-item) (error nil))
  6946. (org-maybe-renumber-ordered-list-safe)))
  6947. (defun org-item-indent-positions ()
  6948. "Return indentation for plain list items.
  6949. This returns a list with three values: The current indentation, the
  6950. parent indentation and the indentation a child should habe.
  6951. Assumes cursor in item line."
  6952. (let* ((bolpos (point-at-bol))
  6953. (ind (org-get-indentation))
  6954. ind-down ind-up pos)
  6955. (save-excursion
  6956. (org-beginning-of-item-list)
  6957. (skip-chars-backward "\n\r \t")
  6958. (when (org-in-item-p)
  6959. (org-beginning-of-item)
  6960. (setq ind-up (org-get-indentation))))
  6961. (setq pos (point))
  6962. (save-excursion
  6963. (cond
  6964. ((and (condition-case nil (progn (org-previous-item) t)
  6965. (error nil))
  6966. (or (forward-char 1) t)
  6967. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6968. (setq ind-down (org-get-indentation)))
  6969. ((and (goto-char pos)
  6970. (org-at-item-p))
  6971. (goto-char (match-end 0))
  6972. (skip-chars-forward " \t")
  6973. (setq ind-down (current-column)))))
  6974. (list ind ind-up ind-down)))
  6975. ;;; The orgstruct minor mode
  6976. ;; Define a minor mode which can be used in other modes in order to
  6977. ;; integrate the org-mode structure editing commands.
  6978. ;; This is really a hack, because the org-mode structure commands use
  6979. ;; keys which normally belong to the major mode. Here is how it
  6980. ;; works: The minor mode defines all the keys necessary to operate the
  6981. ;; structure commands, but wraps the commands into a function which
  6982. ;; tests if the cursor is currently at a headline or a plain list
  6983. ;; item. If that is the case, the structure command is used,
  6984. ;; temporarily setting many Org-mode variables like regular
  6985. ;; expressions for filling etc. However, when any of those keys is
  6986. ;; used at a different location, function uses `key-binding' to look
  6987. ;; up if the key has an associated command in another currently active
  6988. ;; keymap (minor modes, major mode, global), and executes that
  6989. ;; command. There might be problems if any of the keys is otherwise
  6990. ;; used as a prefix key.
  6991. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6992. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6993. ;; addresses this by checking explicitly for both bindings.
  6994. (defvar orgstruct-mode-map (make-sparse-keymap)
  6995. "Keymap for the minor `orgstruct-mode'.")
  6996. (defvar org-local-vars nil
  6997. "List of local variables, for use by `orgstruct-mode'")
  6998. ;;;###autoload
  6999. (define-minor-mode orgstruct-mode
  7000. "Toggle the minor more `orgstruct-mode'.
  7001. This mode is for using Org-mode structure commands in other modes.
  7002. The following key behave as if Org-mode was active, if the cursor
  7003. is on a headline, or on a plain list item (both in the definition
  7004. of Org-mode).
  7005. M-up Move entry/item up
  7006. M-down Move entry/item down
  7007. M-left Promote
  7008. M-right Demote
  7009. M-S-up Move entry/item up
  7010. M-S-down Move entry/item down
  7011. M-S-left Promote subtree
  7012. M-S-right Demote subtree
  7013. M-q Fill paragraph and items like in Org-mode
  7014. C-c ^ Sort entries
  7015. C-c - Cycle list bullet
  7016. TAB Cycle item visibility
  7017. M-RET Insert new heading/item
  7018. S-M-RET Insert new TODO heading / Chekbox item
  7019. C-c C-c Set tags / toggle checkbox"
  7020. nil " OrgStruct" nil
  7021. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7022. ;;;###autoload
  7023. (defun turn-on-orgstruct ()
  7024. "Unconditionally turn on `orgstruct-mode'."
  7025. (orgstruct-mode 1))
  7026. ;;;###autoload
  7027. (defun turn-on-orgstruct++ ()
  7028. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7029. In addition to setting orgstruct-mode, this also exports all indentation and
  7030. autofilling variables from org-mode into the buffer. Note that turning
  7031. off orgstruct-mode will *not* remove these additional settings."
  7032. (orgstruct-mode 1)
  7033. (let (var val)
  7034. (mapc
  7035. (lambda (x)
  7036. (when (string-match
  7037. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7038. (symbol-name (car x)))
  7039. (setq var (car x) val (nth 1 x))
  7040. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7041. org-local-vars)))
  7042. (defun orgstruct-error ()
  7043. "Error when there is no default binding for a structure key."
  7044. (interactive)
  7045. (error "This key has no function outside structure elements"))
  7046. (defun orgstruct-setup ()
  7047. "Setup orgstruct keymaps."
  7048. (let ((nfunc 0)
  7049. (bindings
  7050. (list
  7051. '([(meta up)] org-metaup)
  7052. '([(meta down)] org-metadown)
  7053. '([(meta left)] org-metaleft)
  7054. '([(meta right)] org-metaright)
  7055. '([(meta shift up)] org-shiftmetaup)
  7056. '([(meta shift down)] org-shiftmetadown)
  7057. '([(meta shift left)] org-shiftmetaleft)
  7058. '([(meta shift right)] org-shiftmetaright)
  7059. '([(shift up)] org-shiftup)
  7060. '([(shift down)] org-shiftdown)
  7061. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7062. '("\M-q" fill-paragraph)
  7063. '("\C-c^" org-sort)
  7064. '("\C-c-" org-cycle-list-bullet)))
  7065. elt key fun cmd)
  7066. (while (setq elt (pop bindings))
  7067. (setq nfunc (1+ nfunc))
  7068. (setq key (org-key (car elt))
  7069. fun (nth 1 elt)
  7070. cmd (orgstruct-make-binding fun nfunc key))
  7071. (org-defkey orgstruct-mode-map key cmd))
  7072. ;; Special treatment needed for TAB and RET
  7073. (org-defkey orgstruct-mode-map [(tab)]
  7074. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7075. (org-defkey orgstruct-mode-map "\C-i"
  7076. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7077. (org-defkey orgstruct-mode-map "\M-\C-m"
  7078. (orgstruct-make-binding 'org-insert-heading 105
  7079. "\M-\C-m" [(meta return)]))
  7080. (org-defkey orgstruct-mode-map [(meta return)]
  7081. (orgstruct-make-binding 'org-insert-heading 106
  7082. [(meta return)] "\M-\C-m"))
  7083. (org-defkey orgstruct-mode-map [(shift meta return)]
  7084. (orgstruct-make-binding 'org-insert-todo-heading 107
  7085. [(meta return)] "\M-\C-m"))
  7086. (unless org-local-vars
  7087. (setq org-local-vars (org-get-local-variables)))
  7088. t))
  7089. (defun orgstruct-make-binding (fun n &rest keys)
  7090. "Create a function for binding in the structure minor mode.
  7091. FUN is the command to call inside a table. N is used to create a unique
  7092. command name. KEYS are keys that should be checked in for a command
  7093. to execute outside of tables."
  7094. (eval
  7095. (list 'defun
  7096. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7097. '(arg)
  7098. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7099. "Outside of structure, run the binding of `"
  7100. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7101. "'.")
  7102. '(interactive "p")
  7103. (list 'if
  7104. '(org-context-p 'headline 'item)
  7105. (list 'org-run-like-in-org-mode (list 'quote fun))
  7106. (list 'let '(orgstruct-mode)
  7107. (list 'call-interactively
  7108. (append '(or)
  7109. (mapcar (lambda (k)
  7110. (list 'key-binding k))
  7111. keys)
  7112. '('orgstruct-error))))))))
  7113. (defun org-context-p (&rest contexts)
  7114. "Check if local context is and of CONTEXTS.
  7115. Possible values in the list of contexts are `table', `headline', and `item'."
  7116. (let ((pos (point)))
  7117. (goto-char (point-at-bol))
  7118. (prog1 (or (and (memq 'table contexts)
  7119. (looking-at "[ \t]*|"))
  7120. (and (memq 'headline contexts)
  7121. (looking-at "\\*+"))
  7122. (and (memq 'item contexts)
  7123. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7124. (goto-char pos))))
  7125. (defun org-get-local-variables ()
  7126. "Return a list of all local variables in an org-mode buffer."
  7127. (let (varlist)
  7128. (with-current-buffer (get-buffer-create "*Org tmp*")
  7129. (erase-buffer)
  7130. (org-mode)
  7131. (setq varlist (buffer-local-variables)))
  7132. (kill-buffer "*Org tmp*")
  7133. (delq nil
  7134. (mapcar
  7135. (lambda (x)
  7136. (setq x
  7137. (if (symbolp x)
  7138. (list x)
  7139. (list (car x) (list 'quote (cdr x)))))
  7140. (if (string-match
  7141. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7142. (symbol-name (car x)))
  7143. x nil))
  7144. varlist))))
  7145. ;;;###autoload
  7146. (defun org-run-like-in-org-mode (cmd)
  7147. (unless org-local-vars
  7148. (setq org-local-vars (org-get-local-variables)))
  7149. (eval (list 'let org-local-vars
  7150. (list 'call-interactively (list 'quote cmd)))))
  7151. ;;;; Archiving
  7152. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7153. (defun org-archive-subtree (&optional find-done)
  7154. "Move the current subtree to the archive.
  7155. The archive can be a certain top-level heading in the current file, or in
  7156. a different file. The tree will be moved to that location, the subtree
  7157. heading be marked DONE, and the current time will be added.
  7158. When called with prefix argument FIND-DONE, find whole trees without any
  7159. open TODO items and archive them (after getting confirmation from the user).
  7160. If the cursor is not at a headline when this comand is called, try all level
  7161. 1 trees. If the cursor is on a headline, only try the direct children of
  7162. this heading."
  7163. (interactive "P")
  7164. (if find-done
  7165. (org-archive-all-done)
  7166. ;; Save all relevant TODO keyword-relatex variables
  7167. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7168. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7169. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7170. (tr-org-done-keywords org-done-keywords)
  7171. (tr-org-todo-regexp org-todo-regexp)
  7172. (tr-org-todo-line-regexp org-todo-line-regexp)
  7173. (tr-org-odd-levels-only org-odd-levels-only)
  7174. (this-buffer (current-buffer))
  7175. (org-archive-location org-archive-location)
  7176. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7177. ;; start of variables that will be used for saving context
  7178. ;; The compiler complains about them - keep them anyway!
  7179. (file (abbreviate-file-name (buffer-file-name)))
  7180. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7181. (time (format-time-string
  7182. (substring (cdr org-time-stamp-formats) 1 -1)
  7183. (current-time)))
  7184. afile heading buffer level newfile-p
  7185. category todo priority
  7186. ;; start of variables that will be used for savind context
  7187. ltags itags prop)
  7188. ;; Try to find a local archive location
  7189. (save-excursion
  7190. (save-restriction
  7191. (widen)
  7192. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7193. (if (and prop (string-match "\\S-" prop))
  7194. (setq org-archive-location prop)
  7195. (if (or (re-search-backward re nil t)
  7196. (re-search-forward re nil t))
  7197. (setq org-archive-location (match-string 1))))))
  7198. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7199. (progn
  7200. (setq afile (format (match-string 1 org-archive-location)
  7201. (file-name-nondirectory buffer-file-name))
  7202. heading (match-string 2 org-archive-location)))
  7203. (error "Invalid `org-archive-location'"))
  7204. (if (> (length afile) 0)
  7205. (setq newfile-p (not (file-exists-p afile))
  7206. buffer (find-file-noselect afile))
  7207. (setq buffer (current-buffer)))
  7208. (unless buffer
  7209. (error "Cannot access file \"%s\"" afile))
  7210. (if (and (> (length heading) 0)
  7211. (string-match "^\\*+" heading))
  7212. (setq level (match-end 0))
  7213. (setq heading nil level 0))
  7214. (save-excursion
  7215. (org-back-to-heading t)
  7216. ;; Get context information that will be lost by moving the tree
  7217. (org-refresh-category-properties)
  7218. (setq category (org-get-category)
  7219. todo (and (looking-at org-todo-line-regexp)
  7220. (match-string 2))
  7221. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7222. ltags (org-get-tags)
  7223. itags (org-delete-all ltags (org-get-tags-at)))
  7224. (setq ltags (mapconcat 'identity ltags " ")
  7225. itags (mapconcat 'identity itags " "))
  7226. ;; We first only copy, in case something goes wrong
  7227. ;; we need to protect this-command, to avoid kill-region sets it,
  7228. ;; which would lead to duplication of subtrees
  7229. (let (this-command) (org-copy-subtree))
  7230. (set-buffer buffer)
  7231. ;; Enforce org-mode for the archive buffer
  7232. (if (not (org-mode-p))
  7233. ;; Force the mode for future visits.
  7234. (let ((org-insert-mode-line-in-empty-file t)
  7235. (org-inhibit-startup t))
  7236. (call-interactively 'org-mode)))
  7237. (when newfile-p
  7238. (goto-char (point-max))
  7239. (insert (format "\nArchived entries from file %s\n\n"
  7240. (buffer-file-name this-buffer))))
  7241. ;; Force the TODO keywords of the original buffer
  7242. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7243. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7244. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7245. (org-done-keywords tr-org-done-keywords)
  7246. (org-todo-regexp tr-org-todo-regexp)
  7247. (org-todo-line-regexp tr-org-todo-line-regexp)
  7248. (org-odd-levels-only
  7249. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7250. org-odd-levels-only
  7251. tr-org-odd-levels-only)))
  7252. (goto-char (point-min))
  7253. (show-all)
  7254. (if heading
  7255. (progn
  7256. (if (re-search-forward
  7257. (concat "^" (regexp-quote heading)
  7258. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7259. nil t)
  7260. (goto-char (match-end 0))
  7261. ;; Heading not found, just insert it at the end
  7262. (goto-char (point-max))
  7263. (or (bolp) (insert "\n"))
  7264. (insert "\n" heading "\n")
  7265. (end-of-line 0))
  7266. ;; Make the subtree visible
  7267. (show-subtree)
  7268. (org-end-of-subtree t)
  7269. (skip-chars-backward " \t\r\n")
  7270. (and (looking-at "[ \t\r\n]*")
  7271. (replace-match "\n\n")))
  7272. ;; No specific heading, just go to end of file.
  7273. (goto-char (point-max)) (insert "\n"))
  7274. ;; Paste
  7275. (org-paste-subtree (org-get-legal-level level 1))
  7276. ;; Mark the entry as done
  7277. (when (and org-archive-mark-done
  7278. (looking-at org-todo-line-regexp)
  7279. (or (not (match-end 2))
  7280. (not (member (match-string 2) org-done-keywords))))
  7281. (let (org-log-done org-todo-log-states)
  7282. (org-todo
  7283. (car (or (member org-archive-mark-done org-done-keywords)
  7284. org-done-keywords)))))
  7285. ;; Add the context info
  7286. (when org-archive-save-context-info
  7287. (let ((l org-archive-save-context-info) e n v)
  7288. (while (setq e (pop l))
  7289. (when (and (setq v (symbol-value e))
  7290. (stringp v) (string-match "\\S-" v))
  7291. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7292. (org-entry-put (point) n v)))))
  7293. ;; Save and kill the buffer, if it is not the same buffer.
  7294. (if (not (eq this-buffer buffer))
  7295. (progn (save-buffer) (kill-buffer buffer)))))
  7296. ;; Here we are back in the original buffer. Everything seems to have
  7297. ;; worked. So now cut the tree and finish up.
  7298. (let (this-command) (org-cut-subtree))
  7299. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7300. (message "Subtree archived %s"
  7301. (if (eq this-buffer buffer)
  7302. (concat "under heading: " heading)
  7303. (concat "in file: " (abbreviate-file-name afile)))))))
  7304. (defun org-refresh-category-properties ()
  7305. "Refresh category text properties in teh buffer."
  7306. (let ((def-cat (cond
  7307. ((null org-category)
  7308. (if buffer-file-name
  7309. (file-name-sans-extension
  7310. (file-name-nondirectory buffer-file-name))
  7311. "???"))
  7312. ((symbolp org-category) (symbol-name org-category))
  7313. (t org-category)))
  7314. beg end cat pos optionp)
  7315. (org-unmodified
  7316. (save-excursion
  7317. (save-restriction
  7318. (widen)
  7319. (goto-char (point-min))
  7320. (put-text-property (point) (point-max) 'org-category def-cat)
  7321. (while (re-search-forward
  7322. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7323. (setq pos (match-end 0)
  7324. optionp (equal (char-after (match-beginning 0)) ?#)
  7325. cat (org-trim (match-string 2)))
  7326. (if optionp
  7327. (setq beg (point-at-bol) end (point-max))
  7328. (org-back-to-heading t)
  7329. (setq beg (point) end (org-end-of-subtree t t)))
  7330. (put-text-property beg end 'org-category cat)
  7331. (goto-char pos)))))))
  7332. (defun org-archive-all-done (&optional tag)
  7333. "Archive sublevels of the current tree without open TODO items.
  7334. If the cursor is not on a headline, try all level 1 trees. If
  7335. it is on a headline, try all direct children.
  7336. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7337. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7338. (rea (concat ".*:" org-archive-tag ":"))
  7339. (begm (make-marker))
  7340. (endm (make-marker))
  7341. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7342. "Move subtree to archive (no open TODO items)? "))
  7343. beg end (cntarch 0))
  7344. (if (org-on-heading-p)
  7345. (progn
  7346. (setq re1 (concat "^" (regexp-quote
  7347. (make-string
  7348. (1+ (- (match-end 0) (match-beginning 0) 1))
  7349. ?*))
  7350. " "))
  7351. (move-marker begm (point))
  7352. (move-marker endm (org-end-of-subtree t)))
  7353. (setq re1 "^* ")
  7354. (move-marker begm (point-min))
  7355. (move-marker endm (point-max)))
  7356. (save-excursion
  7357. (goto-char begm)
  7358. (while (re-search-forward re1 endm t)
  7359. (setq beg (match-beginning 0)
  7360. end (save-excursion (org-end-of-subtree t) (point)))
  7361. (goto-char beg)
  7362. (if (re-search-forward re end t)
  7363. (goto-char end)
  7364. (goto-char beg)
  7365. (if (and (or (not tag) (not (looking-at rea)))
  7366. (y-or-n-p question))
  7367. (progn
  7368. (if tag
  7369. (org-toggle-tag org-archive-tag 'on)
  7370. (org-archive-subtree))
  7371. (setq cntarch (1+ cntarch)))
  7372. (goto-char end)))))
  7373. (message "%d trees archived" cntarch)))
  7374. (defun org-cycle-hide-drawers (state)
  7375. "Re-hide all drawers after a visibility state change."
  7376. (when (and (org-mode-p)
  7377. (not (memq state '(overview folded))))
  7378. (save-excursion
  7379. (let* ((globalp (memq state '(contents all)))
  7380. (beg (if globalp (point-min) (point)))
  7381. (end (if globalp (point-max) (org-end-of-subtree t))))
  7382. (goto-char beg)
  7383. (while (re-search-forward org-drawer-regexp end t)
  7384. (org-flag-drawer t))))))
  7385. (defun org-flag-drawer (flag)
  7386. (save-excursion
  7387. (beginning-of-line 1)
  7388. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7389. (let ((b (match-end 0))
  7390. (outline-regexp org-outline-regexp))
  7391. (if (re-search-forward
  7392. "^[ \t]*:END:"
  7393. (save-excursion (outline-next-heading) (point)) t)
  7394. (outline-flag-region b (point-at-eol) flag)
  7395. (error ":END: line missing"))))))
  7396. (defun org-cycle-hide-archived-subtrees (state)
  7397. "Re-hide all archived subtrees after a visibility state change."
  7398. (when (and (not org-cycle-open-archived-trees)
  7399. (not (memq state '(overview folded))))
  7400. (save-excursion
  7401. (let* ((globalp (memq state '(contents all)))
  7402. (beg (if globalp (point-min) (point)))
  7403. (end (if globalp (point-max) (org-end-of-subtree t))))
  7404. (org-hide-archived-subtrees beg end)
  7405. (goto-char beg)
  7406. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7407. (message "%s" (substitute-command-keys
  7408. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7409. (defun org-force-cycle-archived ()
  7410. "Cycle subtree even if it is archived."
  7411. (interactive)
  7412. (setq this-command 'org-cycle)
  7413. (let ((org-cycle-open-archived-trees t))
  7414. (call-interactively 'org-cycle)))
  7415. (defun org-hide-archived-subtrees (beg end)
  7416. "Re-hide all archived subtrees after a visibility state change."
  7417. (save-excursion
  7418. (let* ((re (concat ":" org-archive-tag ":")))
  7419. (goto-char beg)
  7420. (while (re-search-forward re end t)
  7421. (and (org-on-heading-p) (hide-subtree))
  7422. (org-end-of-subtree t)))))
  7423. (defun org-toggle-tag (tag &optional onoff)
  7424. "Toggle the tag TAG for the current line.
  7425. If ONOFF is `on' or `off', don't toggle but set to this state."
  7426. (unless (org-on-heading-p t) (error "Not on headling"))
  7427. (let (res current)
  7428. (save-excursion
  7429. (beginning-of-line)
  7430. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7431. (point-at-eol) t)
  7432. (progn
  7433. (setq current (match-string 1))
  7434. (replace-match ""))
  7435. (setq current ""))
  7436. (setq current (nreverse (org-split-string current ":")))
  7437. (cond
  7438. ((eq onoff 'on)
  7439. (setq res t)
  7440. (or (member tag current) (push tag current)))
  7441. ((eq onoff 'off)
  7442. (or (not (member tag current)) (setq current (delete tag current))))
  7443. (t (if (member tag current)
  7444. (setq current (delete tag current))
  7445. (setq res t)
  7446. (push tag current))))
  7447. (end-of-line 1)
  7448. (if current
  7449. (progn
  7450. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7451. (org-set-tags nil t))
  7452. (delete-horizontal-space))
  7453. (run-hooks 'org-after-tags-change-hook))
  7454. res))
  7455. (defun org-toggle-archive-tag (&optional arg)
  7456. "Toggle the archive tag for the current headline.
  7457. With prefix ARG, check all children of current headline and offer tagging
  7458. the children that do not contain any open TODO items."
  7459. (interactive "P")
  7460. (if arg
  7461. (org-archive-all-done 'tag)
  7462. (let (set)
  7463. (save-excursion
  7464. (org-back-to-heading t)
  7465. (setq set (org-toggle-tag org-archive-tag))
  7466. (when set (hide-subtree)))
  7467. (and set (beginning-of-line 1))
  7468. (message "Subtree %s" (if set "archived" "unarchived")))))
  7469. ;;;; Tables
  7470. ;;; The table editor
  7471. ;; Watch out: Here we are talking about two different kind of tables.
  7472. ;; Most of the code is for the tables created with the Org-mode table editor.
  7473. ;; Sometimes, we talk about tables created and edited with the table.el
  7474. ;; Emacs package. We call the former org-type tables, and the latter
  7475. ;; table.el-type tables.
  7476. (defun org-before-change-function (beg end)
  7477. "Every change indicates that a table might need an update."
  7478. (setq org-table-may-need-update t))
  7479. (defconst org-table-line-regexp "^[ \t]*|"
  7480. "Detects an org-type table line.")
  7481. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7482. "Detects an org-type table line.")
  7483. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7484. "Detects a table line marked for automatic recalculation.")
  7485. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7486. "Detects a table line marked for automatic recalculation.")
  7487. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7488. "Detects a table line marked for automatic recalculation.")
  7489. (defconst org-table-hline-regexp "^[ \t]*|-"
  7490. "Detects an org-type table hline.")
  7491. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7492. "Detects a table-type table hline.")
  7493. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7494. "Detects an org-type or table-type table.")
  7495. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7496. "Searching from within a table (any type) this finds the first line
  7497. outside the table.")
  7498. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7499. "Searching from within a table (any type) this finds the first line
  7500. outside the table.")
  7501. (defvar org-table-last-highlighted-reference nil)
  7502. (defvar org-table-formula-history nil)
  7503. (defvar org-table-column-names nil
  7504. "Alist with column names, derived from the `!' line.")
  7505. (defvar org-table-column-name-regexp nil
  7506. "Regular expression matching the current column names.")
  7507. (defvar org-table-local-parameters nil
  7508. "Alist with parameter names, derived from the `$' line.")
  7509. (defvar org-table-named-field-locations nil
  7510. "Alist with locations of named fields.")
  7511. (defvar org-table-current-line-types nil
  7512. "Table row types, non-nil only for the duration of a comand.")
  7513. (defvar org-table-current-begin-line nil
  7514. "Table begin line, non-nil only for the duration of a comand.")
  7515. (defvar org-table-current-begin-pos nil
  7516. "Table begin position, non-nil only for the duration of a comand.")
  7517. (defvar org-table-dlines nil
  7518. "Vector of data line line numbers in the current table.")
  7519. (defvar org-table-hlines nil
  7520. "Vector of hline line numbers in the current table.")
  7521. (defconst org-table-range-regexp
  7522. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7523. ;; 1 2 3 4 5
  7524. "Regular expression for matching ranges in formulas.")
  7525. (defconst org-table-range-regexp2
  7526. (concat
  7527. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7528. "\\.\\."
  7529. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7530. "Match a range for reference display.")
  7531. (defconst org-table-translate-regexp
  7532. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7533. "Match a reference that needs translation, for reference display.")
  7534. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7535. (defun org-table-create-with-table.el ()
  7536. "Use the table.el package to insert a new table.
  7537. If there is already a table at point, convert between Org-mode tables
  7538. and table.el tables."
  7539. (interactive)
  7540. (require 'table)
  7541. (cond
  7542. ((org-at-table.el-p)
  7543. (if (y-or-n-p "Convert table to Org-mode table? ")
  7544. (org-table-convert)))
  7545. ((org-at-table-p)
  7546. (if (y-or-n-p "Convert table to table.el table? ")
  7547. (org-table-convert)))
  7548. (t (call-interactively 'table-insert))))
  7549. (defun org-table-create-or-convert-from-region (arg)
  7550. "Convert region to table, or create an empty table.
  7551. If there is an active region, convert it to a table, using the function
  7552. `org-table-convert-region'. See the documentation of that function
  7553. to learn how the prefix argument is interpreted to determine the field
  7554. separator.
  7555. If there is no such region, create an empty table with `org-table-create'."
  7556. (interactive "P")
  7557. (if (org-region-active-p)
  7558. (org-table-convert-region (region-beginning) (region-end) arg)
  7559. (org-table-create arg)))
  7560. (defun org-table-create (&optional size)
  7561. "Query for a size and insert a table skeleton.
  7562. SIZE is a string Columns x Rows like for example \"3x2\"."
  7563. (interactive "P")
  7564. (unless size
  7565. (setq size (read-string
  7566. (concat "Table size Columns x Rows [e.g. "
  7567. org-table-default-size "]: ")
  7568. "" nil org-table-default-size)))
  7569. (let* ((pos (point))
  7570. (indent (make-string (current-column) ?\ ))
  7571. (split (org-split-string size " *x *"))
  7572. (rows (string-to-number (nth 1 split)))
  7573. (columns (string-to-number (car split)))
  7574. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7575. "\n")))
  7576. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7577. (point-at-bol) (point)))
  7578. (beginning-of-line 1)
  7579. (newline))
  7580. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7581. (dotimes (i rows) (insert line))
  7582. (goto-char pos)
  7583. (if (> rows 1)
  7584. ;; Insert a hline after the first row.
  7585. (progn
  7586. (end-of-line 1)
  7587. (insert "\n|-")
  7588. (goto-char pos)))
  7589. (org-table-align)))
  7590. (defun org-table-convert-region (beg0 end0 &optional separator)
  7591. "Convert region to a table.
  7592. The region goes from BEG0 to END0, but these borders will be moved
  7593. slightly, to make sure a beginning of line in the first line is included.
  7594. SEPARATOR specifies the field separator in the lines. It can have the
  7595. following values:
  7596. '(4) Use the comma as a field separator
  7597. '(16) Use a TAB as field separator
  7598. integer When a number, use that many spaces as field separator
  7599. nil When nil, the command tries to be smart and figure out the
  7600. separator in the following way:
  7601. - when each line contains a TAB, assume TAB-separated material
  7602. - when each line contains a comme, assume CSV material
  7603. - else, assume one or more SPACE charcters as separator."
  7604. (interactive "rP")
  7605. (let* ((beg (min beg0 end0))
  7606. (end (max beg0 end0))
  7607. re)
  7608. (goto-char beg)
  7609. (beginning-of-line 1)
  7610. (setq beg (move-marker (make-marker) (point)))
  7611. (goto-char end)
  7612. (if (bolp) (backward-char 1) (end-of-line 1))
  7613. (setq end (move-marker (make-marker) (point)))
  7614. ;; Get the right field separator
  7615. (unless separator
  7616. (goto-char beg)
  7617. (setq separator
  7618. (cond
  7619. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7620. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7621. (t 1))))
  7622. (setq re (cond
  7623. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7624. ((equal separator '(16)) "^\\|\t")
  7625. ((integerp separator)
  7626. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7627. (t (error "This should not happen"))))
  7628. (goto-char beg)
  7629. (while (re-search-forward re end t)
  7630. (replace-match "| " t t))
  7631. (goto-char beg)
  7632. (insert " ")
  7633. (org-table-align)))
  7634. (defun org-table-import (file arg)
  7635. "Import FILE as a table.
  7636. The file is assumed to be tab-separated. Such files can be produced by most
  7637. spreadsheet and database applications. If no tabs (at least one per line)
  7638. are found, lines will be split on whitespace into fields."
  7639. (interactive "f\nP")
  7640. (or (bolp) (newline))
  7641. (let ((beg (point))
  7642. (pm (point-max)))
  7643. (insert-file-contents file)
  7644. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7645. (defun org-table-export ()
  7646. "Export table as a tab-separated file.
  7647. Such a file can be imported into a spreadsheet program like Excel."
  7648. (interactive)
  7649. (let* ((beg (org-table-begin))
  7650. (end (org-table-end))
  7651. (table (buffer-substring beg end))
  7652. (file (read-file-name "Export table to: "))
  7653. buf)
  7654. (unless (or (not (file-exists-p file))
  7655. (y-or-n-p (format "Overwrite file %s? " file)))
  7656. (error "Abort"))
  7657. (with-current-buffer (find-file-noselect file)
  7658. (setq buf (current-buffer))
  7659. (erase-buffer)
  7660. (fundamental-mode)
  7661. (insert table)
  7662. (goto-char (point-min))
  7663. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7664. (replace-match "" t t)
  7665. (end-of-line 1))
  7666. (goto-char (point-min))
  7667. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7668. (replace-match "" t t)
  7669. (goto-char (min (1+ (point)) (point-max))))
  7670. (goto-char (point-min))
  7671. (while (re-search-forward "^-[-+]*$" nil t)
  7672. (replace-match "")
  7673. (if (looking-at "\n")
  7674. (delete-char 1)))
  7675. (goto-char (point-min))
  7676. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7677. (replace-match "\t" t t))
  7678. (save-buffer))
  7679. (kill-buffer buf)))
  7680. (defvar org-table-aligned-begin-marker (make-marker)
  7681. "Marker at the beginning of the table last aligned.
  7682. Used to check if cursor still is in that table, to minimize realignment.")
  7683. (defvar org-table-aligned-end-marker (make-marker)
  7684. "Marker at the end of the table last aligned.
  7685. Used to check if cursor still is in that table, to minimize realignment.")
  7686. (defvar org-table-last-alignment nil
  7687. "List of flags for flushright alignment, from the last re-alignment.
  7688. This is being used to correctly align a single field after TAB or RET.")
  7689. (defvar org-table-last-column-widths nil
  7690. "List of max width of fields in each column.
  7691. This is being used to correctly align a single field after TAB or RET.")
  7692. (defvar org-table-overlay-coordinates nil
  7693. "Overlay coordinates after each align of a table.")
  7694. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7695. (defvar org-last-recalc-line nil)
  7696. (defconst org-narrow-column-arrow "=>"
  7697. "Used as display property in narrowed table columns.")
  7698. (defun org-table-align ()
  7699. "Align the table at point by aligning all vertical bars."
  7700. (interactive)
  7701. (let* (
  7702. ;; Limits of table
  7703. (beg (org-table-begin))
  7704. (end (org-table-end))
  7705. ;; Current cursor position
  7706. (linepos (org-current-line))
  7707. (colpos (org-table-current-column))
  7708. (winstart (window-start))
  7709. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7710. lines (new "") lengths l typenums ty fields maxfields i
  7711. column
  7712. (indent "") cnt frac
  7713. rfmt hfmt
  7714. (spaces '(1 . 1))
  7715. (sp1 (car spaces))
  7716. (sp2 (cdr spaces))
  7717. (rfmt1 (concat
  7718. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7719. (hfmt1 (concat
  7720. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7721. emptystrings links dates emph narrow fmax f1 len c e)
  7722. (untabify beg end)
  7723. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7724. ;; Check if we have links or dates
  7725. (goto-char beg)
  7726. (setq links (re-search-forward org-bracket-link-regexp end t))
  7727. (goto-char beg)
  7728. (setq emph (and org-hide-emphasis-markers
  7729. (re-search-forward org-emph-re end t)))
  7730. (goto-char beg)
  7731. (setq dates (and org-display-custom-times
  7732. (re-search-forward org-ts-regexp-both end t)))
  7733. ;; Make sure the link properties are right
  7734. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7735. ;; Make sure the date properties are right
  7736. (when dates (goto-char beg) (while (org-activate-dates end)))
  7737. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7738. ;; Check if we are narrowing any columns
  7739. (goto-char beg)
  7740. (setq narrow (and org-format-transports-properties-p
  7741. (re-search-forward "<[0-9]+>" end t)))
  7742. ;; Get the rows
  7743. (setq lines (org-split-string
  7744. (buffer-substring beg end) "\n"))
  7745. ;; Store the indentation of the first line
  7746. (if (string-match "^ *" (car lines))
  7747. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7748. ;; Mark the hlines by setting the corresponding element to nil
  7749. ;; At the same time, we remove trailing space.
  7750. (setq lines (mapcar (lambda (l)
  7751. (if (string-match "^ *|-" l)
  7752. nil
  7753. (if (string-match "[ \t]+$" l)
  7754. (substring l 0 (match-beginning 0))
  7755. l)))
  7756. lines))
  7757. ;; Get the data fields by splitting the lines.
  7758. (setq fields (mapcar
  7759. (lambda (l)
  7760. (org-split-string l " *| *"))
  7761. (delq nil (copy-sequence lines))))
  7762. ;; How many fields in the longest line?
  7763. (condition-case nil
  7764. (setq maxfields (apply 'max (mapcar 'length fields)))
  7765. (error
  7766. (kill-region beg end)
  7767. (org-table-create org-table-default-size)
  7768. (error "Empty table - created default table")))
  7769. ;; A list of empty strings to fill any short rows on output
  7770. (setq emptystrings (make-list maxfields ""))
  7771. ;; Check for special formatting.
  7772. (setq i -1)
  7773. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7774. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7775. ;; Check if there is an explicit width specified
  7776. (when narrow
  7777. (setq c column fmax nil)
  7778. (while c
  7779. (setq e (pop c))
  7780. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7781. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7782. ;; Find fields that are wider than fmax, and shorten them
  7783. (when fmax
  7784. (loop for xx in column do
  7785. (when (and (stringp xx)
  7786. (> (org-string-width xx) fmax))
  7787. (org-add-props xx nil
  7788. 'help-echo
  7789. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7790. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7791. (unless (> f1 1)
  7792. (error "Cannot narrow field starting with wide link \"%s\""
  7793. (match-string 0 xx)))
  7794. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7795. (add-text-properties (- f1 2) f1
  7796. (list 'display org-narrow-column-arrow)
  7797. xx)))))
  7798. ;; Get the maximum width for each column
  7799. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7800. ;; Get the fraction of numbers, to decide about alignment of the column
  7801. (setq cnt 0 frac 0.0)
  7802. (loop for x in column do
  7803. (if (equal x "")
  7804. nil
  7805. (setq frac ( / (+ (* frac cnt)
  7806. (if (string-match org-table-number-regexp x) 1 0))
  7807. (setq cnt (1+ cnt))))))
  7808. (push (>= frac org-table-number-fraction) typenums))
  7809. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7810. ;; Store the alignment of this table, for later editing of single fields
  7811. (setq org-table-last-alignment typenums
  7812. org-table-last-column-widths lengths)
  7813. ;; With invisible characters, `format' does not get the field width right
  7814. ;; So we need to make these fields wide by hand.
  7815. (when (or links emph)
  7816. (loop for i from 0 upto (1- maxfields) do
  7817. (setq len (nth i lengths))
  7818. (loop for j from 0 upto (1- (length fields)) do
  7819. (setq c (nthcdr i (car (nthcdr j fields))))
  7820. (if (and (stringp (car c))
  7821. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7822. ; (string-match org-bracket-link-regexp (car c))
  7823. (< (org-string-width (car c)) len))
  7824. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7825. ;; Compute the formats needed for output of the table
  7826. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7827. (while (setq l (pop lengths))
  7828. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7829. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7830. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7831. (setq rfmt (concat rfmt "\n")
  7832. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7833. (setq new (mapconcat
  7834. (lambda (l)
  7835. (if l (apply 'format rfmt
  7836. (append (pop fields) emptystrings))
  7837. hfmt))
  7838. lines ""))
  7839. ;; Replace the old one
  7840. (delete-region beg end)
  7841. (move-marker end nil)
  7842. (move-marker org-table-aligned-begin-marker (point))
  7843. (insert new)
  7844. (move-marker org-table-aligned-end-marker (point))
  7845. (when (and orgtbl-mode (not (org-mode-p)))
  7846. (goto-char org-table-aligned-begin-marker)
  7847. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7848. ;; Try to move to the old location
  7849. (goto-line winstartline)
  7850. (setq winstart (point-at-bol))
  7851. (goto-line linepos)
  7852. (set-window-start (selected-window) winstart 'noforce)
  7853. (org-table-goto-column colpos)
  7854. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7855. (setq org-table-may-need-update nil)
  7856. ))
  7857. (defun org-string-width (s)
  7858. "Compute width of string, ignoring invisible characters.
  7859. This ignores character with invisibility property `org-link', and also
  7860. characters with property `org-cwidth', because these will become invisible
  7861. upon the next fontification round."
  7862. (let (b l)
  7863. (when (or (eq t buffer-invisibility-spec)
  7864. (assq 'org-link buffer-invisibility-spec))
  7865. (while (setq b (text-property-any 0 (length s)
  7866. 'invisible 'org-link s))
  7867. (setq s (concat (substring s 0 b)
  7868. (substring s (or (next-single-property-change
  7869. b 'invisible s) (length s)))))))
  7870. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7871. (setq s (concat (substring s 0 b)
  7872. (substring s (or (next-single-property-change
  7873. b 'org-cwidth s) (length s))))))
  7874. (setq l (string-width s) b -1)
  7875. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7876. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7877. l))
  7878. (defun org-table-begin (&optional table-type)
  7879. "Find the beginning of the table and return its position.
  7880. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7881. (save-excursion
  7882. (if (not (re-search-backward
  7883. (if table-type org-table-any-border-regexp
  7884. org-table-border-regexp)
  7885. nil t))
  7886. (progn (goto-char (point-min)) (point))
  7887. (goto-char (match-beginning 0))
  7888. (beginning-of-line 2)
  7889. (point))))
  7890. (defun org-table-end (&optional table-type)
  7891. "Find the end of the table and return its position.
  7892. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7893. (save-excursion
  7894. (if (not (re-search-forward
  7895. (if table-type org-table-any-border-regexp
  7896. org-table-border-regexp)
  7897. nil t))
  7898. (goto-char (point-max))
  7899. (goto-char (match-beginning 0)))
  7900. (point-marker)))
  7901. (defun org-table-justify-field-maybe (&optional new)
  7902. "Justify the current field, text to left, number to right.
  7903. Optional argument NEW may specify text to replace the current field content."
  7904. (cond
  7905. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7906. ((org-at-table-hline-p))
  7907. ((and (not new)
  7908. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7909. (current-buffer)))
  7910. (< (point) org-table-aligned-begin-marker)
  7911. (>= (point) org-table-aligned-end-marker)))
  7912. ;; This is not the same table, force a full re-align
  7913. (setq org-table-may-need-update t))
  7914. (t ;; realign the current field, based on previous full realign
  7915. (let* ((pos (point)) s
  7916. (col (org-table-current-column))
  7917. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7918. l f n o e)
  7919. (when (> col 0)
  7920. (skip-chars-backward "^|\n")
  7921. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7922. (progn
  7923. (setq s (match-string 1)
  7924. o (match-string 0)
  7925. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7926. e (not (= (match-beginning 2) (match-end 2))))
  7927. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7928. l (if e "|" (setq org-table-may-need-update t) ""))
  7929. n (format f s))
  7930. (if new
  7931. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7932. (setq n (format f new))
  7933. (setq n (concat new "|") org-table-may-need-update t)))
  7934. (or (equal n o)
  7935. (let (org-table-may-need-update)
  7936. (replace-match n t t))))
  7937. (setq org-table-may-need-update t))
  7938. (goto-char pos))))))
  7939. (defun org-table-next-field ()
  7940. "Go to the next field in the current table, creating new lines as needed.
  7941. Before doing so, re-align the table if necessary."
  7942. (interactive)
  7943. (org-table-maybe-eval-formula)
  7944. (org-table-maybe-recalculate-line)
  7945. (if (and org-table-automatic-realign
  7946. org-table-may-need-update)
  7947. (org-table-align))
  7948. (let ((end (org-table-end)))
  7949. (if (org-at-table-hline-p)
  7950. (end-of-line 1))
  7951. (condition-case nil
  7952. (progn
  7953. (re-search-forward "|" end)
  7954. (if (looking-at "[ \t]*$")
  7955. (re-search-forward "|" end))
  7956. (if (and (looking-at "-")
  7957. org-table-tab-jumps-over-hlines
  7958. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7959. (goto-char (match-beginning 1)))
  7960. (if (looking-at "-")
  7961. (progn
  7962. (beginning-of-line 0)
  7963. (org-table-insert-row 'below))
  7964. (if (looking-at " ") (forward-char 1))))
  7965. (error
  7966. (org-table-insert-row 'below)))))
  7967. (defun org-table-previous-field ()
  7968. "Go to the previous field in the table.
  7969. Before doing so, re-align the table if necessary."
  7970. (interactive)
  7971. (org-table-justify-field-maybe)
  7972. (org-table-maybe-recalculate-line)
  7973. (if (and org-table-automatic-realign
  7974. org-table-may-need-update)
  7975. (org-table-align))
  7976. (if (org-at-table-hline-p)
  7977. (end-of-line 1))
  7978. (re-search-backward "|" (org-table-begin))
  7979. (re-search-backward "|" (org-table-begin))
  7980. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7981. (re-search-backward "|" (org-table-begin)))
  7982. (if (looking-at "| ?")
  7983. (goto-char (match-end 0))))
  7984. (defun org-table-next-row ()
  7985. "Go to the next row (same column) in the current table.
  7986. Before doing so, re-align the table if necessary."
  7987. (interactive)
  7988. (org-table-maybe-eval-formula)
  7989. (org-table-maybe-recalculate-line)
  7990. (if (or (looking-at "[ \t]*$")
  7991. (save-excursion (skip-chars-backward " \t") (bolp)))
  7992. (newline)
  7993. (if (and org-table-automatic-realign
  7994. org-table-may-need-update)
  7995. (org-table-align))
  7996. (let ((col (org-table-current-column)))
  7997. (beginning-of-line 2)
  7998. (if (or (not (org-at-table-p))
  7999. (org-at-table-hline-p))
  8000. (progn
  8001. (beginning-of-line 0)
  8002. (org-table-insert-row 'below)))
  8003. (org-table-goto-column col)
  8004. (skip-chars-backward "^|\n\r")
  8005. (if (looking-at " ") (forward-char 1)))))
  8006. (defun org-table-copy-down (n)
  8007. "Copy a field down in the current column.
  8008. If the field at the cursor is empty, copy into it the content of the nearest
  8009. non-empty field above. With argument N, use the Nth non-empty field.
  8010. If the current field is not empty, it is copied down to the next row, and
  8011. the cursor is moved with it. Therefore, repeating this command causes the
  8012. column to be filled row-by-row.
  8013. If the variable `org-table-copy-increment' is non-nil and the field is an
  8014. integer or a timestamp, it will be incremented while copying. In the case of
  8015. a timestamp, if the cursor is on the year, change the year. If it is on the
  8016. month or the day, change that. Point will stay on the current date field
  8017. in order to easily repeat the interval."
  8018. (interactive "p")
  8019. (let* ((colpos (org-table-current-column))
  8020. (col (current-column))
  8021. (field (org-table-get-field))
  8022. (non-empty (string-match "[^ \t]" field))
  8023. (beg (org-table-begin))
  8024. txt)
  8025. (org-table-check-inside-data-field)
  8026. (if non-empty
  8027. (progn
  8028. (setq txt (org-trim field))
  8029. (org-table-next-row)
  8030. (org-table-blank-field))
  8031. (save-excursion
  8032. (setq txt
  8033. (catch 'exit
  8034. (while (progn (beginning-of-line 1)
  8035. (re-search-backward org-table-dataline-regexp
  8036. beg t))
  8037. (org-table-goto-column colpos t)
  8038. (if (and (looking-at
  8039. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8040. (= (setq n (1- n)) 0))
  8041. (throw 'exit (match-string 1))))))))
  8042. (if txt
  8043. (progn
  8044. (if (and org-table-copy-increment
  8045. (string-match "^[0-9]+$" txt))
  8046. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8047. (insert txt)
  8048. (move-to-column col)
  8049. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8050. (org-timestamp-up 1)
  8051. (org-table-maybe-recalculate-line))
  8052. (org-table-align)
  8053. (move-to-column col))
  8054. (error "No non-empty field found"))))
  8055. (defun org-table-check-inside-data-field ()
  8056. "Is point inside a table data field?
  8057. I.e. not on a hline or before the first or after the last column?
  8058. This actually throws an error, so it aborts the current command."
  8059. (if (or (not (org-at-table-p))
  8060. (= (org-table-current-column) 0)
  8061. (org-at-table-hline-p)
  8062. (looking-at "[ \t]*$"))
  8063. (error "Not in table data field")))
  8064. (defvar org-table-clip nil
  8065. "Clipboard for table regions.")
  8066. (defun org-table-blank-field ()
  8067. "Blank the current table field or active region."
  8068. (interactive)
  8069. (org-table-check-inside-data-field)
  8070. (if (and (interactive-p) (org-region-active-p))
  8071. (let (org-table-clip)
  8072. (org-table-cut-region (region-beginning) (region-end)))
  8073. (skip-chars-backward "^|")
  8074. (backward-char 1)
  8075. (if (looking-at "|[^|\n]+")
  8076. (let* ((pos (match-beginning 0))
  8077. (match (match-string 0))
  8078. (len (org-string-width match)))
  8079. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8080. (goto-char (+ 2 pos))
  8081. (substring match 1)))))
  8082. (defun org-table-get-field (&optional n replace)
  8083. "Return the value of the field in column N of current row.
  8084. N defaults to current field.
  8085. If REPLACE is a string, replace field with this value. The return value
  8086. is always the old value."
  8087. (and n (org-table-goto-column n))
  8088. (skip-chars-backward "^|\n")
  8089. (backward-char 1)
  8090. (if (looking-at "|[^|\r\n]*")
  8091. (let* ((pos (match-beginning 0))
  8092. (val (buffer-substring (1+ pos) (match-end 0))))
  8093. (if replace
  8094. (replace-match (concat "|" replace) t t))
  8095. (goto-char (min (point-at-eol) (+ 2 pos)))
  8096. val)
  8097. (forward-char 1) ""))
  8098. (defun org-table-field-info (arg)
  8099. "Show info about the current field, and highlight any reference at point."
  8100. (interactive "P")
  8101. (org-table-get-specials)
  8102. (save-excursion
  8103. (let* ((pos (point))
  8104. (col (org-table-current-column))
  8105. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8106. (name (car (rassoc (list (org-current-line) col)
  8107. org-table-named-field-locations)))
  8108. (eql (org-table-get-stored-formulas))
  8109. (dline (org-table-current-dline))
  8110. (ref (format "@%d$%d" dline col))
  8111. (ref1 (org-table-convert-refs-to-an ref))
  8112. (fequation (or (assoc name eql) (assoc ref eql)))
  8113. (cequation (assoc (int-to-string col) eql))
  8114. (eqn (or fequation cequation)))
  8115. (goto-char pos)
  8116. (condition-case nil
  8117. (org-table-show-reference 'local)
  8118. (error nil))
  8119. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8120. dline col
  8121. (if cname (concat " or $" cname) "")
  8122. dline col ref1
  8123. (if name (concat " or $" name) "")
  8124. ;; FIXME: formula info not correct if special table line
  8125. (if eqn
  8126. (concat ", formula: "
  8127. (org-table-formula-to-user
  8128. (concat
  8129. (if (string-match "^[$@]"(car eqn)) "" "$")
  8130. (car eqn) "=" (cdr eqn))))
  8131. "")))))
  8132. (defun org-table-current-column ()
  8133. "Find out which column we are in.
  8134. When called interactively, column is also displayed in echo area."
  8135. (interactive)
  8136. (if (interactive-p) (org-table-check-inside-data-field))
  8137. (save-excursion
  8138. (let ((cnt 0) (pos (point)))
  8139. (beginning-of-line 1)
  8140. (while (search-forward "|" pos t)
  8141. (setq cnt (1+ cnt)))
  8142. (if (interactive-p) (message "This is table column %d" cnt))
  8143. cnt)))
  8144. (defun org-table-current-dline ()
  8145. "Find out what table data line we are in.
  8146. Only datalins count for this."
  8147. (interactive)
  8148. (if (interactive-p) (org-table-check-inside-data-field))
  8149. (save-excursion
  8150. (let ((cnt 0) (pos (point)))
  8151. (goto-char (org-table-begin))
  8152. (while (<= (point) pos)
  8153. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8154. (beginning-of-line 2))
  8155. (if (interactive-p) (message "This is table line %d" cnt))
  8156. cnt)))
  8157. (defun org-table-goto-column (n &optional on-delim force)
  8158. "Move the cursor to the Nth column in the current table line.
  8159. With optional argument ON-DELIM, stop with point before the left delimiter
  8160. of the field.
  8161. If there are less than N fields, just go to after the last delimiter.
  8162. However, when FORCE is non-nil, create new columns if necessary."
  8163. (interactive "p")
  8164. (let ((pos (point-at-eol)))
  8165. (beginning-of-line 1)
  8166. (when (> n 0)
  8167. (while (and (> (setq n (1- n)) -1)
  8168. (or (search-forward "|" pos t)
  8169. (and force
  8170. (progn (end-of-line 1)
  8171. (skip-chars-backward "^|")
  8172. (insert " | "))))))
  8173. ; (backward-char 2) t)))))
  8174. (when (and force (not (looking-at ".*|")))
  8175. (save-excursion (end-of-line 1) (insert " | ")))
  8176. (if on-delim
  8177. (backward-char 1)
  8178. (if (looking-at " ") (forward-char 1))))))
  8179. (defun org-at-table-p (&optional table-type)
  8180. "Return t if the cursor is inside an org-type table.
  8181. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8182. (if org-enable-table-editor
  8183. (save-excursion
  8184. (beginning-of-line 1)
  8185. (looking-at (if table-type org-table-any-line-regexp
  8186. org-table-line-regexp)))
  8187. nil))
  8188. (defun org-at-table.el-p ()
  8189. "Return t if and only if we are at a table.el table."
  8190. (and (org-at-table-p 'any)
  8191. (save-excursion
  8192. (goto-char (org-table-begin 'any))
  8193. (looking-at org-table1-hline-regexp))))
  8194. (defun org-table-recognize-table.el ()
  8195. "If there is a table.el table nearby, recognize it and move into it."
  8196. (if org-table-tab-recognizes-table.el
  8197. (if (org-at-table.el-p)
  8198. (progn
  8199. (beginning-of-line 1)
  8200. (if (looking-at org-table-dataline-regexp)
  8201. nil
  8202. (if (looking-at org-table1-hline-regexp)
  8203. (progn
  8204. (beginning-of-line 2)
  8205. (if (looking-at org-table-any-border-regexp)
  8206. (beginning-of-line -1)))))
  8207. (if (re-search-forward "|" (org-table-end t) t)
  8208. (progn
  8209. (require 'table)
  8210. (if (table--at-cell-p (point))
  8211. t
  8212. (message "recognizing table.el table...")
  8213. (table-recognize-table)
  8214. (message "recognizing table.el table...done")))
  8215. (error "This should not happen..."))
  8216. t)
  8217. nil)
  8218. nil))
  8219. (defun org-at-table-hline-p ()
  8220. "Return t if the cursor is inside a hline in a table."
  8221. (if org-enable-table-editor
  8222. (save-excursion
  8223. (beginning-of-line 1)
  8224. (looking-at org-table-hline-regexp))
  8225. nil))
  8226. (defun org-table-insert-column ()
  8227. "Insert a new column into the table."
  8228. (interactive)
  8229. (if (not (org-at-table-p))
  8230. (error "Not at a table"))
  8231. (org-table-find-dataline)
  8232. (let* ((col (max 1 (org-table-current-column)))
  8233. (beg (org-table-begin))
  8234. (end (org-table-end))
  8235. ;; Current cursor position
  8236. (linepos (org-current-line))
  8237. (colpos col))
  8238. (goto-char beg)
  8239. (while (< (point) end)
  8240. (if (org-at-table-hline-p)
  8241. nil
  8242. (org-table-goto-column col t)
  8243. (insert "| "))
  8244. (beginning-of-line 2))
  8245. (move-marker end nil)
  8246. (goto-line linepos)
  8247. (org-table-goto-column colpos)
  8248. (org-table-align)
  8249. (org-table-fix-formulas "$" nil (1- col) 1)))
  8250. (defun org-table-find-dataline ()
  8251. "Find a dataline in the current table, which is needed for column commands."
  8252. (if (and (org-at-table-p)
  8253. (not (org-at-table-hline-p)))
  8254. t
  8255. (let ((col (current-column))
  8256. (end (org-table-end)))
  8257. (move-to-column col)
  8258. (while (and (< (point) end)
  8259. (or (not (= (current-column) col))
  8260. (org-at-table-hline-p)))
  8261. (beginning-of-line 2)
  8262. (move-to-column col))
  8263. (if (and (org-at-table-p)
  8264. (not (org-at-table-hline-p)))
  8265. t
  8266. (error
  8267. "Please position cursor in a data line for column operations")))))
  8268. (defun org-table-delete-column ()
  8269. "Delete a column from the table."
  8270. (interactive)
  8271. (if (not (org-at-table-p))
  8272. (error "Not at a table"))
  8273. (org-table-find-dataline)
  8274. (org-table-check-inside-data-field)
  8275. (let* ((col (org-table-current-column))
  8276. (beg (org-table-begin))
  8277. (end (org-table-end))
  8278. ;; Current cursor position
  8279. (linepos (org-current-line))
  8280. (colpos col))
  8281. (goto-char beg)
  8282. (while (< (point) end)
  8283. (if (org-at-table-hline-p)
  8284. nil
  8285. (org-table-goto-column col t)
  8286. (and (looking-at "|[^|\n]+|")
  8287. (replace-match "|")))
  8288. (beginning-of-line 2))
  8289. (move-marker end nil)
  8290. (goto-line linepos)
  8291. (org-table-goto-column colpos)
  8292. (org-table-align)
  8293. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8294. col -1 col)))
  8295. (defun org-table-move-column-right ()
  8296. "Move column to the right."
  8297. (interactive)
  8298. (org-table-move-column nil))
  8299. (defun org-table-move-column-left ()
  8300. "Move column to the left."
  8301. (interactive)
  8302. (org-table-move-column 'left))
  8303. (defun org-table-move-column (&optional left)
  8304. "Move the current column to the right. With arg LEFT, move to the left."
  8305. (interactive "P")
  8306. (if (not (org-at-table-p))
  8307. (error "Not at a table"))
  8308. (org-table-find-dataline)
  8309. (org-table-check-inside-data-field)
  8310. (let* ((col (org-table-current-column))
  8311. (col1 (if left (1- col) col))
  8312. (beg (org-table-begin))
  8313. (end (org-table-end))
  8314. ;; Current cursor position
  8315. (linepos (org-current-line))
  8316. (colpos (if left (1- col) (1+ col))))
  8317. (if (and left (= col 1))
  8318. (error "Cannot move column further left"))
  8319. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8320. (error "Cannot move column further right"))
  8321. (goto-char beg)
  8322. (while (< (point) end)
  8323. (if (org-at-table-hline-p)
  8324. nil
  8325. (org-table-goto-column col1 t)
  8326. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8327. (replace-match "|\\2|\\1|")))
  8328. (beginning-of-line 2))
  8329. (move-marker end nil)
  8330. (goto-line linepos)
  8331. (org-table-goto-column colpos)
  8332. (org-table-align)
  8333. (org-table-fix-formulas
  8334. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8335. (cons (number-to-string colpos) (number-to-string col))))))
  8336. (defun org-table-move-row-down ()
  8337. "Move table row down."
  8338. (interactive)
  8339. (org-table-move-row nil))
  8340. (defun org-table-move-row-up ()
  8341. "Move table row up."
  8342. (interactive)
  8343. (org-table-move-row 'up))
  8344. (defun org-table-move-row (&optional up)
  8345. "Move the current table line down. With arg UP, move it up."
  8346. (interactive "P")
  8347. (let* ((col (current-column))
  8348. (pos (point))
  8349. (hline1p (save-excursion (beginning-of-line 1)
  8350. (looking-at org-table-hline-regexp)))
  8351. (dline1 (org-table-current-dline))
  8352. (dline2 (+ dline1 (if up -1 1)))
  8353. (tonew (if up 0 2))
  8354. txt hline2p)
  8355. (beginning-of-line tonew)
  8356. (unless (org-at-table-p)
  8357. (goto-char pos)
  8358. (error "Cannot move row further"))
  8359. (setq hline2p (looking-at org-table-hline-regexp))
  8360. (goto-char pos)
  8361. (beginning-of-line 1)
  8362. (setq pos (point))
  8363. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8364. (delete-region (point) (1+ (point-at-eol)))
  8365. (beginning-of-line tonew)
  8366. (insert txt)
  8367. (beginning-of-line 0)
  8368. (move-to-column col)
  8369. (unless (or hline1p hline2p)
  8370. (org-table-fix-formulas
  8371. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8372. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8373. (defun org-table-insert-row (&optional arg)
  8374. "Insert a new row above the current line into the table.
  8375. With prefix ARG, insert below the current line."
  8376. (interactive "P")
  8377. (if (not (org-at-table-p))
  8378. (error "Not at a table"))
  8379. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8380. (new (org-table-clean-line line)))
  8381. ;; Fix the first field if necessary
  8382. (if (string-match "^[ \t]*| *[#$] *|" line)
  8383. (setq new (replace-match (match-string 0 line) t t new)))
  8384. (beginning-of-line (if arg 2 1))
  8385. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8386. (beginning-of-line 0)
  8387. (re-search-forward "| ?" (point-at-eol) t)
  8388. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8389. (org-table-align))
  8390. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8391. (defun org-table-insert-hline (&optional above)
  8392. "Insert a horizontal-line below the current line into the table.
  8393. With prefix ABOVE, insert above the current line."
  8394. (interactive "P")
  8395. (if (not (org-at-table-p))
  8396. (error "Not at a table"))
  8397. (let ((line (org-table-clean-line
  8398. (buffer-substring (point-at-bol) (point-at-eol))))
  8399. (col (current-column)))
  8400. (while (string-match "|\\( +\\)|" line)
  8401. (setq line (replace-match
  8402. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8403. ?-) "|") t t line)))
  8404. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8405. (beginning-of-line (if above 1 2))
  8406. (insert line "\n")
  8407. (beginning-of-line (if above 1 -1))
  8408. (move-to-column col)
  8409. (and org-table-overlay-coordinates (org-table-align))))
  8410. (defun org-table-hline-and-move (&optional same-column)
  8411. "Insert a hline and move to the row below that line."
  8412. (interactive "P")
  8413. (let ((col (org-table-current-column)))
  8414. (org-table-maybe-eval-formula)
  8415. (org-table-maybe-recalculate-line)
  8416. (org-table-insert-hline)
  8417. (end-of-line 2)
  8418. (if (looking-at "\n[ \t]*|-")
  8419. (progn (insert "\n|") (org-table-align))
  8420. (org-table-next-field))
  8421. (if same-column (org-table-goto-column col))))
  8422. (defun org-table-clean-line (s)
  8423. "Convert a table line S into a string with only \"|\" and space.
  8424. In particular, this does handle wide and invisible characters."
  8425. (if (string-match "^[ \t]*|-" s)
  8426. ;; It's a hline, just map the characters
  8427. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8428. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8429. (setq s (replace-match
  8430. (concat "|" (make-string (org-string-width (match-string 1 s))
  8431. ?\ ) "|")
  8432. t t s)))
  8433. s))
  8434. (defun org-table-kill-row ()
  8435. "Delete the current row or horizontal line from the table."
  8436. (interactive)
  8437. (if (not (org-at-table-p))
  8438. (error "Not at a table"))
  8439. (let ((col (current-column))
  8440. (dline (org-table-current-dline)))
  8441. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8442. (if (not (org-at-table-p)) (beginning-of-line 0))
  8443. (move-to-column col)
  8444. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8445. dline -1 dline)))
  8446. (defun org-table-sort-lines (with-case &optional sorting-type)
  8447. "Sort table lines according to the column at point.
  8448. The position of point indicates the column to be used for
  8449. sorting, and the range of lines is the range between the nearest
  8450. horizontal separator lines, or the entire table of no such lines
  8451. exist. If point is before the first column, you will be prompted
  8452. for the sorting column. If there is an active region, the mark
  8453. specifies the first line and the sorting column, while point
  8454. should be in the last line to be included into the sorting.
  8455. The command then prompts for the sorting type which can be
  8456. alphabetically, numerically, or by time (as given in a time stamp
  8457. in the field). Sorting in reverse order is also possible.
  8458. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8459. If SORTING-TYPE is specified when this function is called from a Lisp
  8460. program, no prompting will take place. SORTING-TYPE must be a character,
  8461. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8462. should be done in reverse order."
  8463. (interactive "P")
  8464. (let* ((thisline (org-current-line))
  8465. (thiscol (org-table-current-column))
  8466. beg end bcol ecol tend tbeg column lns pos)
  8467. (when (equal thiscol 0)
  8468. (if (interactive-p)
  8469. (setq thiscol
  8470. (string-to-number
  8471. (read-string "Use column N for sorting: ")))
  8472. (setq thiscol 1))
  8473. (org-table-goto-column thiscol))
  8474. (org-table-check-inside-data-field)
  8475. (if (org-region-active-p)
  8476. (progn
  8477. (setq beg (region-beginning) end (region-end))
  8478. (goto-char beg)
  8479. (setq column (org-table-current-column)
  8480. beg (point-at-bol))
  8481. (goto-char end)
  8482. (setq end (point-at-bol 2)))
  8483. (setq column (org-table-current-column)
  8484. pos (point)
  8485. tbeg (org-table-begin)
  8486. tend (org-table-end))
  8487. (if (re-search-backward org-table-hline-regexp tbeg t)
  8488. (setq beg (point-at-bol 2))
  8489. (goto-char tbeg)
  8490. (setq beg (point-at-bol 1)))
  8491. (goto-char pos)
  8492. (if (re-search-forward org-table-hline-regexp tend t)
  8493. (setq end (point-at-bol 1))
  8494. (goto-char tend)
  8495. (setq end (point-at-bol))))
  8496. (setq beg (move-marker (make-marker) beg)
  8497. end (move-marker (make-marker) end))
  8498. (untabify beg end)
  8499. (goto-char beg)
  8500. (org-table-goto-column column)
  8501. (skip-chars-backward "^|")
  8502. (setq bcol (current-column))
  8503. (org-table-goto-column (1+ column))
  8504. (skip-chars-backward "^|")
  8505. (setq ecol (1- (current-column)))
  8506. (org-table-goto-column column)
  8507. (setq lns (mapcar (lambda(x) (cons
  8508. (org-sort-remove-invisible
  8509. (nth (1- column)
  8510. (org-split-string x "[ \t]*|[ \t]*")))
  8511. x))
  8512. (org-split-string (buffer-substring beg end) "\n")))
  8513. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8514. (delete-region beg end)
  8515. (move-marker beg nil)
  8516. (move-marker end nil)
  8517. (insert (mapconcat 'cdr lns "\n") "\n")
  8518. (goto-line thisline)
  8519. (org-table-goto-column thiscol)
  8520. (message "%d lines sorted, based on column %d" (length lns) column)))
  8521. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8522. (defun org-sort-remove-invisible (s)
  8523. (remove-text-properties 0 (length s) org-rm-props s)
  8524. (while (string-match org-bracket-link-regexp s)
  8525. (setq s (replace-match (if (match-end 2)
  8526. (match-string 3 s)
  8527. (match-string 1 s)) t t s)))
  8528. s)
  8529. (defun org-table-cut-region (beg end)
  8530. "Copy region in table to the clipboard and blank all relevant fields."
  8531. (interactive "r")
  8532. (org-table-copy-region beg end 'cut))
  8533. (defun org-table-copy-region (beg end &optional cut)
  8534. "Copy rectangular region in table to clipboard.
  8535. A special clipboard is used which can only be accessed
  8536. with `org-table-paste-rectangle'."
  8537. (interactive "rP")
  8538. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8539. region cols
  8540. (rpl (if cut " " nil)))
  8541. (goto-char beg)
  8542. (org-table-check-inside-data-field)
  8543. (setq l01 (org-current-line)
  8544. c01 (org-table-current-column))
  8545. (goto-char end)
  8546. (org-table-check-inside-data-field)
  8547. (setq l02 (org-current-line)
  8548. c02 (org-table-current-column))
  8549. (setq l1 (min l01 l02) l2 (max l01 l02)
  8550. c1 (min c01 c02) c2 (max c01 c02))
  8551. (catch 'exit
  8552. (while t
  8553. (catch 'nextline
  8554. (if (> l1 l2) (throw 'exit t))
  8555. (goto-line l1)
  8556. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8557. (setq cols nil ic1 c1 ic2 c2)
  8558. (while (< ic1 (1+ ic2))
  8559. (push (org-table-get-field ic1 rpl) cols)
  8560. (setq ic1 (1+ ic1)))
  8561. (push (nreverse cols) region)
  8562. (setq l1 (1+ l1)))))
  8563. (setq org-table-clip (nreverse region))
  8564. (if cut (org-table-align))
  8565. org-table-clip))
  8566. (defun org-table-paste-rectangle ()
  8567. "Paste a rectangular region into a table.
  8568. The upper right corner ends up in the current field. All involved fields
  8569. will be overwritten. If the rectangle does not fit into the present table,
  8570. the table is enlarged as needed. The process ignores horizontal separator
  8571. lines."
  8572. (interactive)
  8573. (unless (and org-table-clip (listp org-table-clip))
  8574. (error "First cut/copy a region to paste!"))
  8575. (org-table-check-inside-data-field)
  8576. (let* ((clip org-table-clip)
  8577. (line (org-current-line))
  8578. (col (org-table-current-column))
  8579. (org-enable-table-editor t)
  8580. (org-table-automatic-realign nil)
  8581. c cols field)
  8582. (while (setq cols (pop clip))
  8583. (while (org-at-table-hline-p) (beginning-of-line 2))
  8584. (if (not (org-at-table-p))
  8585. (progn (end-of-line 0) (org-table-next-field)))
  8586. (setq c col)
  8587. (while (setq field (pop cols))
  8588. (org-table-goto-column c nil 'force)
  8589. (org-table-get-field nil field)
  8590. (setq c (1+ c)))
  8591. (beginning-of-line 2))
  8592. (goto-line line)
  8593. (org-table-goto-column col)
  8594. (org-table-align)))
  8595. (defun org-table-convert ()
  8596. "Convert from `org-mode' table to table.el and back.
  8597. Obviously, this only works within limits. When an Org-mode table is
  8598. converted to table.el, all horizontal separator lines get lost, because
  8599. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8600. A table.el table can be converted to an Org-mode table only if it does not
  8601. do row or column spanning. Multiline cells will become multiple cells.
  8602. Beware, Org-mode does not test if the table can be successfully converted - it
  8603. blindly applies a recipe that works for simple tables."
  8604. (interactive)
  8605. (require 'table)
  8606. (if (org-at-table.el-p)
  8607. ;; convert to Org-mode table
  8608. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8609. (end (move-marker (make-marker) (org-table-end t))))
  8610. (table-unrecognize-region beg end)
  8611. (goto-char beg)
  8612. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8613. (replace-match ""))
  8614. (goto-char beg))
  8615. (if (org-at-table-p)
  8616. ;; convert to table.el table
  8617. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8618. (end (move-marker (make-marker) (org-table-end))))
  8619. ;; first, get rid of all horizontal lines
  8620. (goto-char beg)
  8621. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8622. (replace-match ""))
  8623. ;; insert a hline before first
  8624. (goto-char beg)
  8625. (org-table-insert-hline 'above)
  8626. (beginning-of-line -1)
  8627. ;; insert a hline after each line
  8628. (while (progn (beginning-of-line 3) (< (point) end))
  8629. (org-table-insert-hline))
  8630. (goto-char beg)
  8631. (setq end (move-marker end (org-table-end)))
  8632. ;; replace "+" at beginning and ending of hlines
  8633. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8634. (replace-match "\\1+-"))
  8635. (goto-char beg)
  8636. (while (re-search-forward "-|[ \t]*$" end t)
  8637. (replace-match "-+"))
  8638. (goto-char beg)))))
  8639. (defun org-table-wrap-region (arg)
  8640. "Wrap several fields in a column like a paragraph.
  8641. This is useful if you'd like to spread the contents of a field over several
  8642. lines, in order to keep the table compact.
  8643. If there is an active region, and both point and mark are in the same column,
  8644. the text in the column is wrapped to minimum width for the given number of
  8645. lines. Generally, this makes the table more compact. A prefix ARG may be
  8646. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8647. formats the selected text to two lines. If the region was longer than two
  8648. lines, the remaining lines remain empty. A negative prefix argument reduces
  8649. the current number of lines by that amount. The wrapped text is pasted back
  8650. into the table. If you formatted it to more lines than it was before, fields
  8651. further down in the table get overwritten - so you might need to make space in
  8652. the table first.
  8653. If there is no region, the current field is split at the cursor position and
  8654. the text fragment to the right of the cursor is prepended to the field one
  8655. line down.
  8656. If there is no region, but you specify a prefix ARG, the current field gets
  8657. blank, and the content is appended to the field above."
  8658. (interactive "P")
  8659. (org-table-check-inside-data-field)
  8660. (if (org-region-active-p)
  8661. ;; There is a region: fill as a paragraph
  8662. (let* ((beg (region-beginning))
  8663. (cline (save-excursion (goto-char beg) (org-current-line)))
  8664. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8665. nlines)
  8666. (org-table-cut-region (region-beginning) (region-end))
  8667. (if (> (length (car org-table-clip)) 1)
  8668. (error "Region must be limited to single column"))
  8669. (setq nlines (if arg
  8670. (if (< arg 1)
  8671. (+ (length org-table-clip) arg)
  8672. arg)
  8673. (length org-table-clip)))
  8674. (setq org-table-clip
  8675. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8676. nil nlines)))
  8677. (goto-line cline)
  8678. (org-table-goto-column ccol)
  8679. (org-table-paste-rectangle))
  8680. ;; No region, split the current field at point
  8681. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8682. (skip-chars-forward "^\r\n|"))
  8683. (if arg
  8684. ;; combine with field above
  8685. (let ((s (org-table-blank-field))
  8686. (col (org-table-current-column)))
  8687. (beginning-of-line 0)
  8688. (while (org-at-table-hline-p) (beginning-of-line 0))
  8689. (org-table-goto-column col)
  8690. (skip-chars-forward "^|")
  8691. (skip-chars-backward " ")
  8692. (insert " " (org-trim s))
  8693. (org-table-align))
  8694. ;; split field
  8695. (if (looking-at "\\([^|]+\\)+|")
  8696. (let ((s (match-string 1)))
  8697. (replace-match " |")
  8698. (goto-char (match-beginning 0))
  8699. (org-table-next-row)
  8700. (insert (org-trim s) " ")
  8701. (org-table-align))
  8702. (org-table-next-row)))))
  8703. (defvar org-field-marker nil)
  8704. (defun org-table-edit-field (arg)
  8705. "Edit table field in a different window.
  8706. This is mainly useful for fields that contain hidden parts.
  8707. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8708. it can be edited in place."
  8709. (interactive "P")
  8710. (if arg
  8711. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8712. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8713. (remove-text-properties b e '(org-cwidth t invisible t
  8714. display t intangible t))
  8715. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8716. (font-lock-fontify-block)))
  8717. (let ((pos (move-marker (make-marker) (point)))
  8718. (field (org-table-get-field))
  8719. (cw (current-window-configuration))
  8720. p)
  8721. (org-switch-to-buffer-other-window "*Org tmp*")
  8722. (erase-buffer)
  8723. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8724. (let ((org-inhibit-startup t)) (org-mode))
  8725. (goto-char (setq p (point-max)))
  8726. (insert (org-trim field))
  8727. (remove-text-properties p (point-max)
  8728. '(invisible t org-cwidth t display t
  8729. intangible t))
  8730. (goto-char p)
  8731. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8732. (org-set-local 'org-window-configuration cw)
  8733. (org-set-local 'org-field-marker pos)
  8734. (message "Edit and finish with C-c C-c"))))
  8735. (defun org-table-finish-edit-field ()
  8736. "Finish editing a table data field.
  8737. Remove all newline characters, insert the result into the table, realign
  8738. the table and kill the editing buffer."
  8739. (let ((pos org-field-marker)
  8740. (cw org-window-configuration)
  8741. (cb (current-buffer))
  8742. text)
  8743. (goto-char (point-min))
  8744. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8745. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8746. (replace-match " "))
  8747. (setq text (org-trim (buffer-string)))
  8748. (set-window-configuration cw)
  8749. (kill-buffer cb)
  8750. (select-window (get-buffer-window (marker-buffer pos)))
  8751. (goto-char pos)
  8752. (move-marker pos nil)
  8753. (org-table-check-inside-data-field)
  8754. (org-table-get-field nil text)
  8755. (org-table-align)
  8756. (message "New field value inserted")))
  8757. (defun org-trim (s)
  8758. "Remove whitespace at beginning and end of string."
  8759. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8760. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8761. s)
  8762. (defun org-wrap (string &optional width lines)
  8763. "Wrap string to either a number of lines, or a width in characters.
  8764. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8765. that costs. If there is a word longer than WIDTH, the text is actually
  8766. wrapped to the length of that word.
  8767. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8768. many lines, whatever width that takes.
  8769. The return value is a list of lines, without newlines at the end."
  8770. (let* ((words (org-split-string string "[ \t\n]+"))
  8771. (maxword (apply 'max (mapcar 'org-string-width words)))
  8772. w ll)
  8773. (cond (width
  8774. (org-do-wrap words (max maxword width)))
  8775. (lines
  8776. (setq w maxword)
  8777. (setq ll (org-do-wrap words maxword))
  8778. (if (<= (length ll) lines)
  8779. ll
  8780. (setq ll words)
  8781. (while (> (length ll) lines)
  8782. (setq w (1+ w))
  8783. (setq ll (org-do-wrap words w)))
  8784. ll))
  8785. (t (error "Cannot wrap this")))))
  8786. (defun org-do-wrap (words width)
  8787. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8788. (let (lines line)
  8789. (while words
  8790. (setq line (pop words))
  8791. (while (and words (< (+ (length line) (length (car words))) width))
  8792. (setq line (concat line " " (pop words))))
  8793. (setq lines (push line lines)))
  8794. (nreverse lines)))
  8795. (defun org-split-string (string &optional separators)
  8796. "Splits STRING into substrings at SEPARATORS.
  8797. No empty strings are returned if there are matches at the beginning
  8798. and end of string."
  8799. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8800. (start 0)
  8801. notfirst
  8802. (list nil))
  8803. (while (and (string-match rexp string
  8804. (if (and notfirst
  8805. (= start (match-beginning 0))
  8806. (< start (length string)))
  8807. (1+ start) start))
  8808. (< (match-beginning 0) (length string)))
  8809. (setq notfirst t)
  8810. (or (eq (match-beginning 0) 0)
  8811. (and (eq (match-beginning 0) (match-end 0))
  8812. (eq (match-beginning 0) start))
  8813. (setq list
  8814. (cons (substring string start (match-beginning 0))
  8815. list)))
  8816. (setq start (match-end 0)))
  8817. (or (eq start (length string))
  8818. (setq list
  8819. (cons (substring string start)
  8820. list)))
  8821. (nreverse list)))
  8822. (defun org-table-map-tables (function)
  8823. "Apply FUNCTION to the start of all tables in the buffer."
  8824. (save-excursion
  8825. (save-restriction
  8826. (widen)
  8827. (goto-char (point-min))
  8828. (while (re-search-forward org-table-any-line-regexp nil t)
  8829. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8830. (beginning-of-line 1)
  8831. (if (looking-at org-table-line-regexp)
  8832. (save-excursion (funcall function)))
  8833. (re-search-forward org-table-any-border-regexp nil 1))))
  8834. (message "Mapping tables: done"))
  8835. (defvar org-timecnt) ; dynamically scoped parameter
  8836. (defun org-table-sum (&optional beg end nlast)
  8837. "Sum numbers in region of current table column.
  8838. The result will be displayed in the echo area, and will be available
  8839. as kill to be inserted with \\[yank].
  8840. If there is an active region, it is interpreted as a rectangle and all
  8841. numbers in that rectangle will be summed. If there is no active
  8842. region and point is located in a table column, sum all numbers in that
  8843. column.
  8844. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8845. numbers are assumed to be times as well (in decimal hours) and the
  8846. numbers are added as such.
  8847. If NLAST is a number, only the NLAST fields will actually be summed."
  8848. (interactive)
  8849. (save-excursion
  8850. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8851. (cond
  8852. ((and beg end)) ; beg and end given explicitly
  8853. ((org-region-active-p)
  8854. (setq beg (region-beginning) end (region-end)))
  8855. (t
  8856. (setq col (org-table-current-column))
  8857. (goto-char (org-table-begin))
  8858. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8859. (error "No table data"))
  8860. (org-table-goto-column col)
  8861. (setq beg (point))
  8862. (goto-char (org-table-end))
  8863. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8864. (error "No table data"))
  8865. (org-table-goto-column col)
  8866. (setq end (point))))
  8867. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8868. (items1 (cond ((not nlast) items)
  8869. ((>= nlast (length items)) items)
  8870. (t (setq items (reverse items))
  8871. (setcdr (nthcdr (1- nlast) items) nil)
  8872. (nreverse items))))
  8873. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8874. items1)))
  8875. (res (apply '+ numbers))
  8876. (sres (if (= org-timecnt 0)
  8877. (format "%g" res)
  8878. (setq diff (* 3600 res)
  8879. h (floor (/ diff 3600)) diff (mod diff 3600)
  8880. m (floor (/ diff 60)) diff (mod diff 60)
  8881. s diff)
  8882. (format "%d:%02d:%02d" h m s))))
  8883. (kill-new sres)
  8884. (if (interactive-p)
  8885. (message "%s"
  8886. (substitute-command-keys
  8887. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8888. (length numbers) sres))))
  8889. sres))))
  8890. (defun org-table-get-number-for-summing (s)
  8891. (let (n)
  8892. (if (string-match "^ *|? *" s)
  8893. (setq s (replace-match "" nil nil s)))
  8894. (if (string-match " *|? *$" s)
  8895. (setq s (replace-match "" nil nil s)))
  8896. (setq n (string-to-number s))
  8897. (cond
  8898. ((and (string-match "0" s)
  8899. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8900. ((string-match "\\`[ \t]+\\'" s) nil)
  8901. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8902. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8903. (m (string-to-number (or (match-string 2 s) "0")))
  8904. (s (string-to-number (or (match-string 4 s) "0"))))
  8905. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8906. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8907. ((equal n 0) nil)
  8908. (t n))))
  8909. (defun org-table-current-field-formula (&optional key noerror)
  8910. "Return the formula active for the current field.
  8911. Assumes that specials are in place.
  8912. If KEY is given, return the key to this formula.
  8913. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8914. (let* ((name (car (rassoc (list (org-current-line)
  8915. (org-table-current-column))
  8916. org-table-named-field-locations)))
  8917. (col (org-table-current-column))
  8918. (scol (int-to-string col))
  8919. (ref (format "@%d$%d" (org-table-current-dline) col))
  8920. (stored-list (org-table-get-stored-formulas noerror))
  8921. (ass (or (assoc name stored-list)
  8922. (assoc ref stored-list)
  8923. (assoc scol stored-list))))
  8924. (if key
  8925. (car ass)
  8926. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8927. (cdr ass))))))
  8928. (defun org-table-get-formula (&optional equation named)
  8929. "Read a formula from the minibuffer, offer stored formula as default.
  8930. When NAMED is non-nil, look for a named equation."
  8931. (let* ((stored-list (org-table-get-stored-formulas))
  8932. (name (car (rassoc (list (org-current-line)
  8933. (org-table-current-column))
  8934. org-table-named-field-locations)))
  8935. (ref (format "@%d$%d" (org-table-current-dline)
  8936. (org-table-current-column)))
  8937. (refass (assoc ref stored-list))
  8938. (scol (if named
  8939. (if name name ref)
  8940. (int-to-string (org-table-current-column))))
  8941. (dummy (and (or name refass) (not named)
  8942. (not (y-or-n-p "Replace field formula with column formula? " ))
  8943. (error "Abort")))
  8944. (name (or name ref))
  8945. (org-table-may-need-update nil)
  8946. (stored (cdr (assoc scol stored-list)))
  8947. (eq (cond
  8948. ((and stored equation (string-match "^ *=? *$" equation))
  8949. stored)
  8950. ((stringp equation)
  8951. equation)
  8952. (t (org-table-formula-from-user
  8953. (read-string
  8954. (org-table-formula-to-user
  8955. (format "%s formula %s%s="
  8956. (if named "Field" "Column")
  8957. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8958. scol))
  8959. (if stored (org-table-formula-to-user stored) "")
  8960. 'org-table-formula-history
  8961. )))))
  8962. mustsave)
  8963. (when (not (string-match "\\S-" eq))
  8964. ;; remove formula
  8965. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8966. (org-table-store-formulas stored-list)
  8967. (error "Formula removed"))
  8968. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8969. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8970. (if (and name (not named))
  8971. ;; We set the column equation, delete the named one.
  8972. (setq stored-list (delq (assoc name stored-list) stored-list)
  8973. mustsave t))
  8974. (if stored
  8975. (setcdr (assoc scol stored-list) eq)
  8976. (setq stored-list (cons (cons scol eq) stored-list)))
  8977. (if (or mustsave (not (equal stored eq)))
  8978. (org-table-store-formulas stored-list))
  8979. eq))
  8980. (defun org-table-store-formulas (alist)
  8981. "Store the list of formulas below the current table."
  8982. (setq alist (sort alist 'org-table-formula-less-p))
  8983. (save-excursion
  8984. (goto-char (org-table-end))
  8985. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8986. (progn
  8987. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8988. (goto-char (match-beginning 2))
  8989. (delete-region (match-beginning 2) (match-end 0)))
  8990. (insert "#+TBLFM:"))
  8991. (insert " "
  8992. (mapconcat (lambda (x)
  8993. (concat
  8994. (if (equal (string-to-char (car x)) ?@) "" "$")
  8995. (car x) "=" (cdr x)))
  8996. alist "::")
  8997. "\n")))
  8998. (defsubst org-table-formula-make-cmp-string (a)
  8999. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  9000. (concat
  9001. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  9002. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  9003. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  9004. (defun org-table-formula-less-p (a b)
  9005. "Compare two formulas for sorting."
  9006. (let ((as (org-table-formula-make-cmp-string (car a)))
  9007. (bs (org-table-formula-make-cmp-string (car b))))
  9008. (and as bs (string< as bs))))
  9009. (defun org-table-get-stored-formulas (&optional noerror)
  9010. "Return an alist with the stored formulas directly after current table."
  9011. (interactive)
  9012. (let (scol eq eq-alist strings string seen)
  9013. (save-excursion
  9014. (goto-char (org-table-end))
  9015. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9016. (setq strings (org-split-string (match-string 2) " *:: *"))
  9017. (while (setq string (pop strings))
  9018. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  9019. (setq scol (if (match-end 2)
  9020. (match-string 2 string)
  9021. (match-string 1 string))
  9022. eq (match-string 3 string)
  9023. eq-alist (cons (cons scol eq) eq-alist))
  9024. (if (member scol seen)
  9025. (if noerror
  9026. (progn
  9027. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9028. (ding)
  9029. (sit-for 2))
  9030. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9031. (push scol seen))))))
  9032. (nreverse eq-alist)))
  9033. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9034. "Modify the equations after the table structure has been edited.
  9035. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9036. For all numbers larger than LIMIT, shift them by DELTA."
  9037. (save-excursion
  9038. (goto-char (org-table-end))
  9039. (when (looking-at "#\\+TBLFM:")
  9040. (let ((re (concat key "\\([0-9]+\\)"))
  9041. (re2
  9042. (when remove
  9043. (if (equal key "$")
  9044. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9045. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9046. s n a)
  9047. (when remove
  9048. (while (re-search-forward re2 (point-at-eol) t)
  9049. (replace-match "")))
  9050. (while (re-search-forward re (point-at-eol) t)
  9051. (setq s (match-string 1) n (string-to-number s))
  9052. (cond
  9053. ((setq a (assoc s replace))
  9054. (replace-match (concat key (cdr a)) t t))
  9055. ((and limit (> n limit))
  9056. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9057. (defun org-table-get-specials ()
  9058. "Get the column names and local parameters for this table."
  9059. (save-excursion
  9060. (let ((beg (org-table-begin)) (end (org-table-end))
  9061. names name fields fields1 field cnt
  9062. c v l line col types dlines hlines)
  9063. (setq org-table-column-names nil
  9064. org-table-local-parameters nil
  9065. org-table-named-field-locations nil
  9066. org-table-current-begin-line nil
  9067. org-table-current-begin-pos nil
  9068. org-table-current-line-types nil)
  9069. (goto-char beg)
  9070. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9071. (setq names (org-split-string (match-string 1) " *| *")
  9072. cnt 1)
  9073. (while (setq name (pop names))
  9074. (setq cnt (1+ cnt))
  9075. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9076. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9077. (setq org-table-column-names (nreverse org-table-column-names))
  9078. (setq org-table-column-name-regexp
  9079. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9080. (goto-char beg)
  9081. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9082. (setq fields (org-split-string (match-string 1) " *| *"))
  9083. (while (setq field (pop fields))
  9084. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9085. (push (cons (match-string 1 field) (match-string 2 field))
  9086. org-table-local-parameters))))
  9087. (goto-char beg)
  9088. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9089. (setq c (match-string 1)
  9090. fields (org-split-string (match-string 2) " *| *"))
  9091. (save-excursion
  9092. (beginning-of-line (if (equal c "_") 2 0))
  9093. (setq line (org-current-line) col 1)
  9094. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9095. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9096. (while (and fields1 (setq field (pop fields)))
  9097. (setq v (pop fields1) col (1+ col))
  9098. (when (and (stringp field) (stringp v)
  9099. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9100. (push (cons field v) org-table-local-parameters)
  9101. (push (list field line col) org-table-named-field-locations))))
  9102. ;; Analyse the line types
  9103. (goto-char beg)
  9104. (setq org-table-current-begin-line (org-current-line)
  9105. org-table-current-begin-pos (point)
  9106. l org-table-current-begin-line)
  9107. (while (looking-at "[ \t]*|\\(-\\)?")
  9108. (push (if (match-end 1) 'hline 'dline) types)
  9109. (if (match-end 1) (push l hlines) (push l dlines))
  9110. (beginning-of-line 2)
  9111. (setq l (1+ l)))
  9112. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9113. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9114. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9115. (defun org-table-maybe-eval-formula ()
  9116. "Check if the current field starts with \"=\" or \":=\".
  9117. If yes, store the formula and apply it."
  9118. ;; We already know we are in a table. Get field will only return a formula
  9119. ;; when appropriate. It might return a separator line, but no problem.
  9120. (when org-table-formula-evaluate-inline
  9121. (let* ((field (org-trim (or (org-table-get-field) "")))
  9122. named eq)
  9123. (when (string-match "^:?=\\(.*\\)" field)
  9124. (setq named (equal (string-to-char field) ?:)
  9125. eq (match-string 1 field))
  9126. (if (or (fboundp 'calc-eval)
  9127. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9128. (org-table-eval-formula (if named '(4) nil)
  9129. (org-table-formula-from-user eq))
  9130. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9131. (defvar org-recalc-commands nil
  9132. "List of commands triggering the recalculation of a line.
  9133. Will be filled automatically during use.")
  9134. (defvar org-recalc-marks
  9135. '((" " . "Unmarked: no special line, no automatic recalculation")
  9136. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9137. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9138. ("!" . "Column name definition line. Reference in formula as $name.")
  9139. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9140. ("_" . "Names for values in row below this one.")
  9141. ("^" . "Names for values in row above this one.")))
  9142. (defun org-table-rotate-recalc-marks (&optional newchar)
  9143. "Rotate the recalculation mark in the first column.
  9144. If in any row, the first field is not consistent with a mark,
  9145. insert a new column for the markers.
  9146. When there is an active region, change all the lines in the region,
  9147. after prompting for the marking character.
  9148. After each change, a message will be displayed indicating the meaning
  9149. of the new mark."
  9150. (interactive)
  9151. (unless (org-at-table-p) (error "Not at a table"))
  9152. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9153. (beg (org-table-begin))
  9154. (end (org-table-end))
  9155. (l (org-current-line))
  9156. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9157. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9158. (have-col
  9159. (save-excursion
  9160. (goto-char beg)
  9161. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9162. (col (org-table-current-column))
  9163. (forcenew (car (assoc newchar org-recalc-marks)))
  9164. epos new)
  9165. (when l1
  9166. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9167. (setq newchar (char-to-string (read-char-exclusive))
  9168. forcenew (car (assoc newchar org-recalc-marks))))
  9169. (if (and newchar (not forcenew))
  9170. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9171. newchar))
  9172. (if l1 (goto-line l1))
  9173. (save-excursion
  9174. (beginning-of-line 1)
  9175. (unless (looking-at org-table-dataline-regexp)
  9176. (error "Not at a table data line")))
  9177. (unless have-col
  9178. (org-table-goto-column 1)
  9179. (org-table-insert-column)
  9180. (org-table-goto-column (1+ col)))
  9181. (setq epos (point-at-eol))
  9182. (save-excursion
  9183. (beginning-of-line 1)
  9184. (org-table-get-field
  9185. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9186. (concat " "
  9187. (setq new (or forcenew
  9188. (cadr (member (match-string 1) marks))))
  9189. " ")
  9190. " # ")))
  9191. (if (and l1 l2)
  9192. (progn
  9193. (goto-line l1)
  9194. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9195. (and (looking-at org-table-dataline-regexp)
  9196. (org-table-get-field 1 (concat " " new " "))))
  9197. (goto-line l1)))
  9198. (if (not (= epos (point-at-eol))) (org-table-align))
  9199. (goto-line l)
  9200. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9201. (defun org-table-maybe-recalculate-line ()
  9202. "Recompute the current line if marked for it, and if we haven't just done it."
  9203. (interactive)
  9204. (and org-table-allow-automatic-line-recalculation
  9205. (not (and (memq last-command org-recalc-commands)
  9206. (equal org-last-recalc-line (org-current-line))))
  9207. (save-excursion (beginning-of-line 1)
  9208. (looking-at org-table-auto-recalculate-regexp))
  9209. (org-table-recalculate) t))
  9210. (defvar org-table-formula-debug nil
  9211. "Non-nil means, debug table formulas.
  9212. When nil, simply write \"#ERROR\" in corrupted fields.")
  9213. (make-variable-buffer-local 'org-table-formula-debug)
  9214. (defvar modes)
  9215. (defsubst org-set-calc-mode (var &optional value)
  9216. (if (stringp var)
  9217. (setq var (assoc var '(("D" calc-angle-mode deg)
  9218. ("R" calc-angle-mode rad)
  9219. ("F" calc-prefer-frac t)
  9220. ("S" calc-symbolic-mode t)))
  9221. value (nth 2 var) var (nth 1 var)))
  9222. (if (memq var modes)
  9223. (setcar (cdr (memq var modes)) value)
  9224. (cons var (cons value modes)))
  9225. modes)
  9226. (defun org-table-eval-formula (&optional arg equation
  9227. suppress-align suppress-const
  9228. suppress-store suppress-analysis)
  9229. "Replace the table field value at the cursor by the result of a calculation.
  9230. This function makes use of Dave Gillespie's Calc package, in my view the
  9231. most exciting program ever written for GNU Emacs. So you need to have Calc
  9232. installed in order to use this function.
  9233. In a table, this command replaces the value in the current field with the
  9234. result of a formula. It also installs the formula as the \"current\" column
  9235. formula, by storing it in a special line below the table. When called
  9236. with a `C-u' prefix, the current field must ba a named field, and the
  9237. formula is installed as valid in only this specific field.
  9238. When called with two `C-u' prefixes, insert the active equation
  9239. for the field back into the current field, so that it can be
  9240. edited there. This is useful in order to use \\[org-table-show-reference]
  9241. to check the referenced fields.
  9242. When called, the command first prompts for a formula, which is read in
  9243. the minibuffer. Previously entered formulas are available through the
  9244. history list, and the last used formula is offered as a default.
  9245. These stored formulas are adapted correctly when moving, inserting, or
  9246. deleting columns with the corresponding commands.
  9247. The formula can be any algebraic expression understood by the Calc package.
  9248. For details, see the Org-mode manual.
  9249. This function can also be called from Lisp programs and offers
  9250. additional arguments: EQUATION can be the formula to apply. If this
  9251. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9252. used to speed-up recursive calls by by-passing unnecessary aligns.
  9253. SUPPRESS-CONST suppresses the interpretation of constants in the
  9254. formula, assuming that this has been done already outside the function.
  9255. SUPPRESS-STORE means the formula should not be stored, either because
  9256. it is already stored, or because it is a modified equation that should
  9257. not overwrite the stored one."
  9258. (interactive "P")
  9259. (org-table-check-inside-data-field)
  9260. (or suppress-analysis (org-table-get-specials))
  9261. (if (equal arg '(16))
  9262. (let ((eq (org-table-current-field-formula)))
  9263. (or eq (error "No equation active for current field"))
  9264. (org-table-get-field nil eq)
  9265. (org-table-align)
  9266. (setq org-table-may-need-update t))
  9267. (let* (fields
  9268. (ndown (if (integerp arg) arg 1))
  9269. (org-table-automatic-realign nil)
  9270. (case-fold-search nil)
  9271. (down (> ndown 1))
  9272. (formula (if (and equation suppress-store)
  9273. equation
  9274. (org-table-get-formula equation (equal arg '(4)))))
  9275. (n0 (org-table-current-column))
  9276. (modes (copy-sequence org-calc-default-modes))
  9277. (numbers nil) ; was a variable, now fixed default
  9278. (keep-empty nil)
  9279. n form form0 bw fmt x ev orig c lispp literal)
  9280. ;; Parse the format string. Since we have a lot of modes, this is
  9281. ;; a lot of work. However, I think calc still uses most of the time.
  9282. (if (string-match ";" formula)
  9283. (let ((tmp (org-split-string formula ";")))
  9284. (setq formula (car tmp)
  9285. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9286. (nth 1 tmp)))
  9287. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9288. (setq c (string-to-char (match-string 1 fmt))
  9289. n (string-to-number (match-string 2 fmt)))
  9290. (if (= c ?p)
  9291. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9292. (setq modes (org-set-calc-mode
  9293. 'calc-float-format
  9294. (list (cdr (assoc c '((?n . float) (?f . fix)
  9295. (?s . sci) (?e . eng))))
  9296. n))))
  9297. (setq fmt (replace-match "" t t fmt)))
  9298. (if (string-match "[NT]" fmt)
  9299. (setq numbers (equal (match-string 0 fmt) "N")
  9300. fmt (replace-match "" t t fmt)))
  9301. (if (string-match "L" fmt)
  9302. (setq literal t
  9303. fmt (replace-match "" t t fmt)))
  9304. (if (string-match "E" fmt)
  9305. (setq keep-empty t
  9306. fmt (replace-match "" t t fmt)))
  9307. (while (string-match "[DRFS]" fmt)
  9308. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9309. (setq fmt (replace-match "" t t fmt)))
  9310. (unless (string-match "\\S-" fmt)
  9311. (setq fmt nil))))
  9312. (if (and (not suppress-const) org-table-formula-use-constants)
  9313. (setq formula (org-table-formula-substitute-names formula)))
  9314. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9315. (while (> ndown 0)
  9316. (setq fields (org-split-string
  9317. (org-no-properties
  9318. (buffer-substring (point-at-bol) (point-at-eol)))
  9319. " *| *"))
  9320. (if (eq numbers t)
  9321. (setq fields (mapcar
  9322. (lambda (x) (number-to-string (string-to-number x)))
  9323. fields)))
  9324. (setq ndown (1- ndown))
  9325. (setq form (copy-sequence formula)
  9326. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9327. (if (and lispp literal) (setq lispp 'literal))
  9328. ;; Check for old vertical references
  9329. (setq form (org-rewrite-old-row-references form))
  9330. ;; Insert complex ranges
  9331. (while (string-match org-table-range-regexp form)
  9332. (setq form
  9333. (replace-match
  9334. (save-match-data
  9335. (org-table-make-reference
  9336. (org-table-get-range (match-string 0 form) nil n0)
  9337. keep-empty numbers lispp))
  9338. t t form)))
  9339. ;; Insert simple ranges
  9340. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9341. (setq form
  9342. (replace-match
  9343. (save-match-data
  9344. (org-table-make-reference
  9345. (org-sublist
  9346. fields (string-to-number (match-string 1 form))
  9347. (string-to-number (match-string 2 form)))
  9348. keep-empty numbers lispp))
  9349. t t form)))
  9350. (setq form0 form)
  9351. ;; Insert the references to fields in same row
  9352. (while (string-match "\\$\\([0-9]+\\)" form)
  9353. (setq n (string-to-number (match-string 1 form))
  9354. x (nth (1- (if (= n 0) n0 n)) fields))
  9355. (unless x (error "Invalid field specifier \"%s\""
  9356. (match-string 0 form)))
  9357. (setq form (replace-match
  9358. (save-match-data
  9359. (org-table-make-reference x nil numbers lispp))
  9360. t t form)))
  9361. (if lispp
  9362. (setq ev (condition-case nil
  9363. (eval (eval (read form)))
  9364. (error "#ERROR"))
  9365. ev (if (numberp ev) (number-to-string ev) ev))
  9366. (or (fboundp 'calc-eval)
  9367. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9368. (setq ev (calc-eval (cons form modes)
  9369. (if numbers 'num))))
  9370. (when org-table-formula-debug
  9371. (with-output-to-temp-buffer "*Substitution History*"
  9372. (princ (format "Substitution history of formula
  9373. Orig: %s
  9374. $xyz-> %s
  9375. @r$c-> %s
  9376. $1-> %s\n" orig formula form0 form))
  9377. (if (listp ev)
  9378. (princ (format " %s^\nError: %s"
  9379. (make-string (car ev) ?\-) (nth 1 ev)))
  9380. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9381. ev (or fmt "NONE")
  9382. (if fmt (format fmt (string-to-number ev)) ev)))))
  9383. (setq bw (get-buffer-window "*Substitution History*"))
  9384. (shrink-window-if-larger-than-buffer bw)
  9385. (unless (and (interactive-p) (not ndown))
  9386. (unless (let (inhibit-redisplay)
  9387. (y-or-n-p "Debugging Formula. Continue to next? "))
  9388. (org-table-align)
  9389. (error "Abort"))
  9390. (delete-window bw)
  9391. (message "")))
  9392. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9393. (org-table-justify-field-maybe
  9394. (if fmt (format fmt (string-to-number ev)) ev))
  9395. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9396. (call-interactively 'org-return)
  9397. (setq ndown 0)))
  9398. (and down (org-table-maybe-recalculate-line))
  9399. (or suppress-align (and org-table-may-need-update
  9400. (org-table-align))))))
  9401. (defun org-table-put-field-property (prop value)
  9402. (save-excursion
  9403. (put-text-property (progn (skip-chars-backward "^|") (point))
  9404. (progn (skip-chars-forward "^|") (point))
  9405. prop value)))
  9406. (defun org-table-get-range (desc &optional tbeg col highlight)
  9407. "Get a calc vector from a column, accorting to descriptor DESC.
  9408. Optional arguments TBEG and COL can give the beginning of the table and
  9409. the current column, to avoid unnecessary parsing.
  9410. HIGHLIGHT means, just highlight the range."
  9411. (if (not (equal (string-to-char desc) ?@))
  9412. (setq desc (concat "@" desc)))
  9413. (save-excursion
  9414. (or tbeg (setq tbeg (org-table-begin)))
  9415. (or col (setq col (org-table-current-column)))
  9416. (let ((thisline (org-current-line))
  9417. beg end c1 c2 r1 r2 rangep tmp)
  9418. (unless (string-match org-table-range-regexp desc)
  9419. (error "Invalid table range specifier `%s'" desc))
  9420. (setq rangep (match-end 3)
  9421. r1 (and (match-end 1) (match-string 1 desc))
  9422. r2 (and (match-end 4) (match-string 4 desc))
  9423. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9424. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9425. (and c1 (setq c1 (+ (string-to-number c1)
  9426. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9427. (and c2 (setq c2 (+ (string-to-number c2)
  9428. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9429. (if (equal r1 "") (setq r1 nil))
  9430. (if (equal r2 "") (setq r2 nil))
  9431. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9432. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9433. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9434. (if (not r1) (setq r1 thisline))
  9435. (if (not r2) (setq r2 thisline))
  9436. (if (not c1) (setq c1 col))
  9437. (if (not c2) (setq c2 col))
  9438. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9439. ;; just one field
  9440. (progn
  9441. (goto-line r1)
  9442. (while (not (looking-at org-table-dataline-regexp))
  9443. (beginning-of-line 2))
  9444. (prog1 (org-trim (org-table-get-field c1))
  9445. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9446. ;; A range, return a vector
  9447. ;; First sort the numbers to get a regular ractangle
  9448. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9449. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9450. (goto-line r1)
  9451. (while (not (looking-at org-table-dataline-regexp))
  9452. (beginning-of-line 2))
  9453. (org-table-goto-column c1)
  9454. (setq beg (point))
  9455. (goto-line r2)
  9456. (while (not (looking-at org-table-dataline-regexp))
  9457. (beginning-of-line 0))
  9458. (org-table-goto-column c2)
  9459. (setq end (point))
  9460. (if highlight
  9461. (org-table-highlight-rectangle
  9462. beg (progn (skip-chars-forward "^|\n") (point))))
  9463. ;; return string representation of calc vector
  9464. (mapcar 'org-trim
  9465. (apply 'append (org-table-copy-region beg end)))))))
  9466. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9467. "Analyze descriptor DESC and retrieve the corresponding line number.
  9468. The cursor is currently in line CLINE, the table begins in line BLINE,
  9469. and TABLE is a vector with line types."
  9470. (if (string-match "^[0-9]+$" desc)
  9471. (aref org-table-dlines (string-to-number desc))
  9472. (setq cline (or cline (org-current-line))
  9473. bline (or bline org-table-current-begin-line)
  9474. table (or table org-table-current-line-types))
  9475. (if (or
  9476. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9477. ;; 1 2 3 4 5 6
  9478. (and (not (match-end 3)) (not (match-end 6)))
  9479. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9480. (error "invalid row descriptor `%s'" desc))
  9481. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9482. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9483. (odir (and (match-end 5) (match-string 5 desc)))
  9484. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9485. (i (- cline bline))
  9486. (rel (and (match-end 6)
  9487. (or (and (match-end 1) (not (match-end 3)))
  9488. (match-end 5)))))
  9489. (if (and hn (not hdir))
  9490. (progn
  9491. (setq i 0 hdir "+")
  9492. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9493. (if (and (not hn) on (not odir))
  9494. (error "should never happen");;(aref org-table-dlines on)
  9495. (if (and hn (> hn 0))
  9496. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9497. (if on
  9498. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9499. (+ bline i)))))
  9500. (defun org-find-row-type (table i type backwards relative n)
  9501. (let ((l (length table)))
  9502. (while (> n 0)
  9503. (while (and (setq i (+ i (if backwards -1 1)))
  9504. (>= i 0) (< i l)
  9505. (not (eq (aref table i) type))
  9506. (if (and relative (eq (aref table i) 'hline))
  9507. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9508. t)))
  9509. (setq n (1- n)))
  9510. (if (or (< i 0) (>= i l))
  9511. (error "Row descriptior leads outside table")
  9512. i)))
  9513. (defun org-rewrite-old-row-references (s)
  9514. (if (string-match "&[-+0-9I]" s)
  9515. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9516. s))
  9517. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9518. "Convert list ELEMENTS to something appropriate to insert into formula.
  9519. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9520. NUMBERS indicates that everything should be converted to numbers.
  9521. LISPP means to return something appropriate for a Lisp list."
  9522. (if (stringp elements) ; just a single val
  9523. (if lispp
  9524. (if (eq lispp 'literal)
  9525. elements
  9526. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9527. (if (equal elements "") (setq elements "0"))
  9528. (if numbers (number-to-string (string-to-number elements)) elements))
  9529. (unless keep-empty
  9530. (setq elements
  9531. (delq nil
  9532. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9533. elements))))
  9534. (setq elements (or elements '("0")))
  9535. (if lispp
  9536. (mapconcat
  9537. (lambda (x)
  9538. (if (eq lispp 'literal)
  9539. x
  9540. (prin1-to-string (if numbers (string-to-number x) x))))
  9541. elements " ")
  9542. (concat "[" (mapconcat
  9543. (lambda (x)
  9544. (if numbers (number-to-string (string-to-number x)) x))
  9545. elements
  9546. ",") "]"))))
  9547. (defun org-table-recalculate (&optional all noalign)
  9548. "Recalculate the current table line by applying all stored formulas.
  9549. With prefix arg ALL, do this for all lines in the table."
  9550. (interactive "P")
  9551. (or (memq this-command org-recalc-commands)
  9552. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9553. (unless (org-at-table-p) (error "Not at a table"))
  9554. (if (equal all '(16))
  9555. (org-table-iterate)
  9556. (org-table-get-specials)
  9557. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9558. (lambda (a b) (string< (car a) (car b)))))
  9559. (inhibit-redisplay (not debug-on-error))
  9560. (line-re org-table-dataline-regexp)
  9561. (thisline (org-current-line))
  9562. (thiscol (org-table-current-column))
  9563. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9564. ;; Insert constants in all formulas
  9565. (setq eqlist
  9566. (mapcar (lambda (x)
  9567. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9568. x)
  9569. eqlist))
  9570. ;; Split the equation list
  9571. (while (setq eq (pop eqlist))
  9572. (if (<= (string-to-char (car eq)) ?9)
  9573. (push eq eqlnum)
  9574. (push eq eqlname)))
  9575. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9576. (if all
  9577. (progn
  9578. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9579. (goto-char (setq beg (org-table-begin)))
  9580. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9581. ;; This is a table with marked lines, compute selected lines
  9582. (setq line-re org-table-recalculate-regexp)
  9583. ;; Move forward to the first non-header line
  9584. (if (and (re-search-forward org-table-dataline-regexp end t)
  9585. (re-search-forward org-table-hline-regexp end t)
  9586. (re-search-forward org-table-dataline-regexp end t))
  9587. (setq beg (match-beginning 0))
  9588. nil))) ;; just leave beg where it is
  9589. (setq beg (point-at-bol)
  9590. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9591. (goto-char beg)
  9592. (and all (message "Re-applying formulas to full table..."))
  9593. ;; First find the named fields, and mark them untouchanble
  9594. (remove-text-properties beg end '(org-untouchable t))
  9595. (while (setq eq (pop eqlname))
  9596. (setq name (car eq)
  9597. a (assoc name org-table-named-field-locations))
  9598. (and (not a)
  9599. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9600. (setq a (list name
  9601. (aref org-table-dlines
  9602. (string-to-number (match-string 1 name)))
  9603. (string-to-number (match-string 2 name)))))
  9604. (when (and a (or all (equal (nth 1 a) thisline)))
  9605. (message "Re-applying formula to field: %s" name)
  9606. (goto-line (nth 1 a))
  9607. (org-table-goto-column (nth 2 a))
  9608. (push (append a (list (cdr eq))) eqlname1)
  9609. (org-table-put-field-property :org-untouchable t)))
  9610. ;; Now evauluate the column formulas, but skip fields covered by
  9611. ;; field formulas
  9612. (goto-char beg)
  9613. (while (re-search-forward line-re end t)
  9614. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9615. ;; Unprotected line, recalculate
  9616. (and all (message "Re-applying formulas to full table...(line %d)"
  9617. (setq cnt (1+ cnt))))
  9618. (setq org-last-recalc-line (org-current-line))
  9619. (setq eql eqlnum)
  9620. (while (setq entry (pop eql))
  9621. (goto-line org-last-recalc-line)
  9622. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9623. (unless (get-text-property (point) :org-untouchable)
  9624. (org-table-eval-formula nil (cdr entry)
  9625. 'noalign 'nocst 'nostore 'noanalysis)))))
  9626. ;; Now evaluate the field formulas
  9627. (while (setq eq (pop eqlname1))
  9628. (message "Re-applying formula to field: %s" (car eq))
  9629. (goto-line (nth 1 eq))
  9630. (org-table-goto-column (nth 2 eq))
  9631. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9632. 'nostore 'noanalysis))
  9633. (goto-line thisline)
  9634. (org-table-goto-column thiscol)
  9635. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9636. (or noalign (and org-table-may-need-update (org-table-align))
  9637. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9638. ;; back to initial position
  9639. (message "Re-applying formulas...done")
  9640. (goto-line thisline)
  9641. (org-table-goto-column thiscol)
  9642. (or noalign (and org-table-may-need-update (org-table-align))
  9643. (and all (message "Re-applying formulas...done"))))))
  9644. (defun org-table-iterate (&optional arg)
  9645. "Recalculate the table until it does not change anymore."
  9646. (interactive "P")
  9647. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9648. (i 0)
  9649. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9650. thistbl)
  9651. (catch 'exit
  9652. (while (< i imax)
  9653. (setq i (1+ i))
  9654. (org-table-recalculate 'all)
  9655. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9656. (if (not (string= lasttbl thistbl))
  9657. (setq lasttbl thistbl)
  9658. (if (> i 1)
  9659. (message "Convergence after %d iterations" i)
  9660. (message "Table was already stable"))
  9661. (throw 'exit t)))
  9662. (error "No convergence after %d iterations" i))))
  9663. (defun org-table-formula-substitute-names (f)
  9664. "Replace $const with values in string F."
  9665. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9666. ;; First, check for column names
  9667. (while (setq start (string-match org-table-column-name-regexp f start))
  9668. (setq start (1+ start))
  9669. (setq a (assoc (match-string 1 f) org-table-column-names))
  9670. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9671. ;; Parameters and constants
  9672. (setq start 0)
  9673. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9674. (setq start (1+ start))
  9675. (if (setq a (save-match-data
  9676. (org-table-get-constant (match-string 1 f))))
  9677. (setq f (replace-match
  9678. (concat (if pp "(") a (if pp ")")) t t f))))
  9679. (if org-table-formula-debug
  9680. (put-text-property 0 (length f) :orig-formula f1 f))
  9681. f))
  9682. (defun org-table-get-constant (const)
  9683. "Find the value for a parameter or constant in a formula.
  9684. Parameters get priority."
  9685. (or (cdr (assoc const org-table-local-parameters))
  9686. (cdr (assoc const org-table-formula-constants-local))
  9687. (cdr (assoc const org-table-formula-constants))
  9688. (and (fboundp 'constants-get) (constants-get const))
  9689. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9690. (org-entry-get nil (substring const 5) 'inherit))
  9691. "#UNDEFINED_NAME"))
  9692. (defvar org-table-fedit-map
  9693. (let ((map (make-sparse-keymap)))
  9694. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9695. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9696. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9697. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9698. (org-defkey map "\C-c?" 'org-table-show-reference)
  9699. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9700. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9701. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9702. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9703. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9704. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9705. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9706. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9707. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9708. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9709. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9710. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9711. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9712. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9713. map))
  9714. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9715. '("Edit-Formulas"
  9716. ["Finish and Install" org-table-fedit-finish t]
  9717. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9718. ["Abort" org-table-fedit-abort t]
  9719. "--"
  9720. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9721. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9722. "--"
  9723. "Shift Reference at Point"
  9724. ["Up" org-table-fedit-ref-up t]
  9725. ["Down" org-table-fedit-ref-down t]
  9726. ["Left" org-table-fedit-ref-left t]
  9727. ["Right" org-table-fedit-ref-right t]
  9728. "-"
  9729. "Change Test Row for Column Formulas"
  9730. ["Up" org-table-fedit-line-up t]
  9731. ["Down" org-table-fedit-line-down t]
  9732. "--"
  9733. ["Scroll Table Window" org-table-fedit-scroll t]
  9734. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9735. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9736. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9737. org-table-overlay-coordinates)]
  9738. "--"
  9739. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9740. :style toggle :selected org-table-buffer-is-an]))
  9741. (defvar org-pos)
  9742. (defun org-table-edit-formulas ()
  9743. "Edit the formulas of the current table in a separate buffer."
  9744. (interactive)
  9745. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9746. (beginning-of-line 0))
  9747. (unless (org-at-table-p) (error "Not at a table"))
  9748. (org-table-get-specials)
  9749. (let ((key (org-table-current-field-formula 'key 'noerror))
  9750. (eql (sort (org-table-get-stored-formulas 'noerror)
  9751. 'org-table-formula-less-p))
  9752. (pos (move-marker (make-marker) (point)))
  9753. (startline 1)
  9754. (wc (current-window-configuration))
  9755. (titles '((column . "# Column Formulas\n")
  9756. (field . "# Field Formulas\n")
  9757. (named . "# Named Field Formulas\n")))
  9758. entry s type title)
  9759. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9760. (erase-buffer)
  9761. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9762. (let ((font-lock-global-modes '(not fundamental-mode)))
  9763. (fundamental-mode))
  9764. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9765. (org-set-local 'org-pos pos)
  9766. (org-set-local 'org-window-configuration wc)
  9767. (use-local-map org-table-fedit-map)
  9768. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9769. (easy-menu-add org-table-fedit-menu)
  9770. (setq startline (org-current-line))
  9771. (while (setq entry (pop eql))
  9772. (setq type (cond
  9773. ((equal (string-to-char (car entry)) ?@) 'field)
  9774. ((string-match "^[0-9]" (car entry)) 'column)
  9775. (t 'named)))
  9776. (when (setq title (assq type titles))
  9777. (or (bobp) (insert "\n"))
  9778. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9779. (setq titles (delq title titles)))
  9780. (if (equal key (car entry)) (setq startline (org-current-line)))
  9781. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9782. (car entry) " = " (cdr entry) "\n"))
  9783. (remove-text-properties 0 (length s) '(face nil) s)
  9784. (insert s))
  9785. (if (eq org-table-use-standard-references t)
  9786. (org-table-fedit-toggle-ref-type))
  9787. (goto-line startline)
  9788. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9789. (defun org-table-fedit-post-command ()
  9790. (when (not (memq this-command '(lisp-complete-symbol)))
  9791. (let ((win (selected-window)))
  9792. (save-excursion
  9793. (condition-case nil
  9794. (org-table-show-reference)
  9795. (error nil))
  9796. (select-window win)))))
  9797. (defun org-table-formula-to-user (s)
  9798. "Convert a formula from internal to user representation."
  9799. (if (eq org-table-use-standard-references t)
  9800. (org-table-convert-refs-to-an s)
  9801. s))
  9802. (defun org-table-formula-from-user (s)
  9803. "Convert a formula from user to internal representation."
  9804. (if org-table-use-standard-references
  9805. (org-table-convert-refs-to-rc s)
  9806. s))
  9807. (defun org-table-convert-refs-to-rc (s)
  9808. "Convert spreadsheet references from AB7 to @7$28.
  9809. Works for single references, but also for entire formulas and even the
  9810. full TBLFM line."
  9811. (let ((start 0))
  9812. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9813. (cond
  9814. ((match-end 3)
  9815. ;; format match, just advance
  9816. (setq start (match-end 0)))
  9817. ((and (> (match-beginning 0) 0)
  9818. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9819. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9820. ;; 3.e5 or something like this.
  9821. (setq start (match-end 0)))
  9822. (t
  9823. (setq start (match-beginning 0)
  9824. s (replace-match
  9825. (if (equal (match-string 2 s) "&")
  9826. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9827. (format "@%d$%d"
  9828. (string-to-number (match-string 2 s))
  9829. (org-letters-to-number (match-string 1 s))))
  9830. t t s)))))
  9831. s))
  9832. (defun org-table-convert-refs-to-an (s)
  9833. "Convert spreadsheet references from to @7$28 to AB7.
  9834. Works for single references, but also for entire formulas and even the
  9835. full TBLFM line."
  9836. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9837. (setq s (replace-match
  9838. (format "%s%d"
  9839. (org-number-to-letters
  9840. (string-to-number (match-string 2 s)))
  9841. (string-to-number (match-string 1 s)))
  9842. t t s)))
  9843. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9844. (setq s (replace-match (concat "\\1"
  9845. (org-number-to-letters
  9846. (string-to-number (match-string 2 s))) "&")
  9847. t nil s)))
  9848. s)
  9849. (defun org-letters-to-number (s)
  9850. "Convert a base 26 number represented by letters into an integer.
  9851. For example: AB -> 28."
  9852. (let ((n 0))
  9853. (setq s (upcase s))
  9854. (while (> (length s) 0)
  9855. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9856. s (substring s 1)))
  9857. n))
  9858. (defun org-number-to-letters (n)
  9859. "Convert an integer into a base 26 number represented by letters.
  9860. For example: 28 -> AB."
  9861. (let ((s ""))
  9862. (while (> n 0)
  9863. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9864. n (/ (1- n) 26)))
  9865. s))
  9866. (defun org-table-fedit-convert-buffer (function)
  9867. "Convert all references in this buffer, using FUNTION."
  9868. (let ((line (org-current-line)))
  9869. (goto-char (point-min))
  9870. (while (not (eobp))
  9871. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9872. (delete-region (point) (point-at-eol))
  9873. (or (eobp) (forward-char 1)))
  9874. (goto-line line)))
  9875. (defun org-table-fedit-toggle-ref-type ()
  9876. "Convert all references in the buffer from B3 to @3$2 and back."
  9877. (interactive)
  9878. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9879. (org-table-fedit-convert-buffer
  9880. (if org-table-buffer-is-an
  9881. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9882. (message "Reference type switched to %s"
  9883. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9884. (defun org-table-fedit-ref-up ()
  9885. "Shift the reference at point one row/hline up."
  9886. (interactive)
  9887. (org-table-fedit-shift-reference 'up))
  9888. (defun org-table-fedit-ref-down ()
  9889. "Shift the reference at point one row/hline down."
  9890. (interactive)
  9891. (org-table-fedit-shift-reference 'down))
  9892. (defun org-table-fedit-ref-left ()
  9893. "Shift the reference at point one field to the left."
  9894. (interactive)
  9895. (org-table-fedit-shift-reference 'left))
  9896. (defun org-table-fedit-ref-right ()
  9897. "Shift the reference at point one field to the right."
  9898. (interactive)
  9899. (org-table-fedit-shift-reference 'right))
  9900. (defun org-table-fedit-shift-reference (dir)
  9901. (cond
  9902. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9903. (if (memq dir '(left right))
  9904. (org-rematch-and-replace 1 (eq dir 'left))
  9905. (error "Cannot shift reference in this direction")))
  9906. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9907. ;; A B3-like reference
  9908. (if (memq dir '(up down))
  9909. (org-rematch-and-replace 2 (eq dir 'up))
  9910. (org-rematch-and-replace 1 (eq dir 'left))))
  9911. ((org-at-regexp-p
  9912. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9913. ;; An internal reference
  9914. (if (memq dir '(up down))
  9915. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9916. (org-rematch-and-replace 5 (eq dir 'left))))))
  9917. (defun org-rematch-and-replace (n &optional decr hline)
  9918. "Re-match the group N, and replace it with the shifted refrence."
  9919. (or (match-end n) (error "Cannot shift reference in this direction"))
  9920. (goto-char (match-beginning n))
  9921. (and (looking-at (regexp-quote (match-string n)))
  9922. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9923. t t)))
  9924. (defun org-shift-refpart (ref &optional decr hline)
  9925. "Shift a refrence part REF.
  9926. If DECR is set, decrease the references row/column, else increase.
  9927. If HLINE is set, this may be a hline reference, it certainly is not
  9928. a translation reference."
  9929. (save-match-data
  9930. (let* ((sign (string-match "^[-+]" ref)) n)
  9931. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9932. (cond
  9933. ((and hline (string-match "^I+" ref))
  9934. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9935. (setq n (+ n (if decr -1 1)))
  9936. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9937. (if sign
  9938. (setq sign (if (< n 0) "-" "+") n (abs n))
  9939. (setq n (max 1 n)))
  9940. (concat sign (make-string n ?I)))
  9941. ((string-match "^[0-9]+" ref)
  9942. (setq n (string-to-number (concat sign ref)))
  9943. (setq n (+ n (if decr -1 1)))
  9944. (if sign
  9945. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9946. (number-to-string (max 1 n))))
  9947. ((string-match "^[a-zA-Z]+" ref)
  9948. (org-number-to-letters
  9949. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9950. (t (error "Cannot shift reference"))))))
  9951. (defun org-table-fedit-toggle-coordinates ()
  9952. "Toggle the display of coordinates in the refrenced table."
  9953. (interactive)
  9954. (let ((pos (marker-position org-pos)))
  9955. (with-current-buffer (marker-buffer org-pos)
  9956. (save-excursion
  9957. (goto-char pos)
  9958. (org-table-toggle-coordinate-overlays)))))
  9959. (defun org-table-fedit-finish (&optional arg)
  9960. "Parse the buffer for formula definitions and install them.
  9961. With prefix ARG, apply the new formulas to the table."
  9962. (interactive "P")
  9963. (org-table-remove-rectangle-highlight)
  9964. (if org-table-use-standard-references
  9965. (progn
  9966. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9967. (setq org-table-buffer-is-an nil)))
  9968. (let ((pos org-pos) eql var form)
  9969. (goto-char (point-min))
  9970. (while (re-search-forward
  9971. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9972. nil t)
  9973. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9974. form (match-string 3))
  9975. (setq form (org-trim form))
  9976. (when (not (equal form ""))
  9977. (while (string-match "[ \t]*\n[ \t]*" form)
  9978. (setq form (replace-match " " t t form)))
  9979. (when (assoc var eql)
  9980. (error "Double formulas for %s" var))
  9981. (push (cons var form) eql)))
  9982. (setq org-pos nil)
  9983. (set-window-configuration org-window-configuration)
  9984. (select-window (get-buffer-window (marker-buffer pos)))
  9985. (goto-char pos)
  9986. (unless (org-at-table-p)
  9987. (error "Lost table position - cannot install formulae"))
  9988. (org-table-store-formulas eql)
  9989. (move-marker pos nil)
  9990. (kill-buffer "*Edit Formulas*")
  9991. (if arg
  9992. (org-table-recalculate 'all)
  9993. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9994. (defun org-table-fedit-abort ()
  9995. "Abort editing formulas, without installing the changes."
  9996. (interactive)
  9997. (org-table-remove-rectangle-highlight)
  9998. (let ((pos org-pos))
  9999. (set-window-configuration org-window-configuration)
  10000. (select-window (get-buffer-window (marker-buffer pos)))
  10001. (goto-char pos)
  10002. (move-marker pos nil)
  10003. (message "Formula editing aborted without installing changes")))
  10004. (defun org-table-fedit-lisp-indent ()
  10005. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  10006. (interactive)
  10007. (let ((pos (point)) beg end ind)
  10008. (beginning-of-line 1)
  10009. (cond
  10010. ((looking-at "[ \t]")
  10011. (goto-char pos)
  10012. (call-interactively 'lisp-indent-line))
  10013. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  10014. ((not (fboundp 'pp-buffer))
  10015. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  10016. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  10017. (goto-char (- (match-end 0) 2))
  10018. (setq beg (point))
  10019. (setq ind (make-string (current-column) ?\ ))
  10020. (condition-case nil (forward-sexp 1)
  10021. (error
  10022. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  10023. (setq end (point))
  10024. (save-restriction
  10025. (narrow-to-region beg end)
  10026. (if (eq last-command this-command)
  10027. (progn
  10028. (goto-char (point-min))
  10029. (setq this-command nil)
  10030. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10031. (replace-match " ")))
  10032. (pp-buffer)
  10033. (untabify (point-min) (point-max))
  10034. (goto-char (1+ (point-min)))
  10035. (while (re-search-forward "^." nil t)
  10036. (beginning-of-line 1)
  10037. (insert ind))
  10038. (goto-char (point-max))
  10039. (backward-delete-char 1)))
  10040. (goto-char beg))
  10041. (t nil))))
  10042. (defvar org-show-positions nil)
  10043. (defun org-table-show-reference (&optional local)
  10044. "Show the location/value of the $ expression at point."
  10045. (interactive)
  10046. (org-table-remove-rectangle-highlight)
  10047. (catch 'exit
  10048. (let ((pos (if local (point) org-pos))
  10049. (face2 'highlight)
  10050. (org-inhibit-highlight-removal t)
  10051. (win (selected-window))
  10052. (org-show-positions nil)
  10053. var name e what match dest)
  10054. (if local (org-table-get-specials))
  10055. (setq what (cond
  10056. ((or (org-at-regexp-p org-table-range-regexp2)
  10057. (org-at-regexp-p org-table-translate-regexp)
  10058. (org-at-regexp-p org-table-range-regexp))
  10059. (setq match
  10060. (save-match-data
  10061. (org-table-convert-refs-to-rc (match-string 0))))
  10062. 'range)
  10063. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10064. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10065. ((not local) nil)
  10066. (t (error "No reference at point")))
  10067. match (and what (or match (match-string 0))))
  10068. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10069. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10070. 'secondary-selection))
  10071. (org-add-hook 'before-change-functions
  10072. 'org-table-remove-rectangle-highlight)
  10073. (if (eq what 'name) (setq var (substring match 1)))
  10074. (when (eq what 'range)
  10075. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10076. (setq match (org-table-formula-substitute-names match)))
  10077. (unless local
  10078. (save-excursion
  10079. (end-of-line 1)
  10080. (re-search-backward "^\\S-" nil t)
  10081. (beginning-of-line 1)
  10082. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10083. (setq dest
  10084. (save-match-data
  10085. (org-table-convert-refs-to-rc (match-string 1))))
  10086. (org-table-add-rectangle-overlay
  10087. (match-beginning 1) (match-end 1) face2))))
  10088. (if (and (markerp pos) (marker-buffer pos))
  10089. (if (get-buffer-window (marker-buffer pos))
  10090. (select-window (get-buffer-window (marker-buffer pos)))
  10091. (org-switch-to-buffer-other-window (get-buffer-window
  10092. (marker-buffer pos)))))
  10093. (goto-char pos)
  10094. (org-table-force-dataline)
  10095. (when dest
  10096. (setq name (substring dest 1))
  10097. (cond
  10098. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10099. (setq e (assoc name org-table-named-field-locations))
  10100. (goto-line (nth 1 e))
  10101. (org-table-goto-column (nth 2 e)))
  10102. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10103. (let ((l (string-to-number (match-string 1 dest)))
  10104. (c (string-to-number (match-string 2 dest))))
  10105. (goto-line (aref org-table-dlines l))
  10106. (org-table-goto-column c)))
  10107. (t (org-table-goto-column (string-to-number name))))
  10108. (move-marker pos (point))
  10109. (org-table-highlight-rectangle nil nil face2))
  10110. (cond
  10111. ((equal dest match))
  10112. ((not match))
  10113. ((eq what 'range)
  10114. (condition-case nil
  10115. (save-excursion
  10116. (org-table-get-range match nil nil 'highlight))
  10117. (error nil)))
  10118. ((setq e (assoc var org-table-named-field-locations))
  10119. (goto-line (nth 1 e))
  10120. (org-table-goto-column (nth 2 e))
  10121. (org-table-highlight-rectangle (point) (point))
  10122. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10123. ((setq e (assoc var org-table-column-names))
  10124. (org-table-goto-column (string-to-number (cdr e)))
  10125. (org-table-highlight-rectangle (point) (point))
  10126. (goto-char (org-table-begin))
  10127. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10128. (org-table-end) t)
  10129. (progn
  10130. (goto-char (match-beginning 1))
  10131. (org-table-highlight-rectangle)
  10132. (message "Named column (column %s)" (cdr e)))
  10133. (error "Column name not found")))
  10134. ((eq what 'column)
  10135. ;; column number
  10136. (org-table-goto-column (string-to-number (substring match 1)))
  10137. (org-table-highlight-rectangle (point) (point))
  10138. (message "Column %s" (substring match 1)))
  10139. ((setq e (assoc var org-table-local-parameters))
  10140. (goto-char (org-table-begin))
  10141. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10142. (progn
  10143. (goto-char (match-beginning 1))
  10144. (org-table-highlight-rectangle)
  10145. (message "Local parameter."))
  10146. (error "Parameter not found")))
  10147. (t
  10148. (cond
  10149. ((not var) (error "No reference at point"))
  10150. ((setq e (assoc var org-table-formula-constants-local))
  10151. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10152. var (cdr e)))
  10153. ((setq e (assoc var org-table-formula-constants))
  10154. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10155. var (cdr e)))
  10156. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10157. (message "Constant: $%s=%s, from `constants.el'%s."
  10158. var e (format " (%s units)" constants-unit-system)))
  10159. (t (error "Undefined name $%s" var)))))
  10160. (goto-char pos)
  10161. (when (and org-show-positions
  10162. (not (memq this-command '(org-table-fedit-scroll
  10163. org-table-fedit-scroll-down))))
  10164. (push pos org-show-positions)
  10165. (push org-table-current-begin-pos org-show-positions)
  10166. (let ((min (apply 'min org-show-positions))
  10167. (max (apply 'max org-show-positions)))
  10168. (goto-char min) (recenter 0)
  10169. (goto-char max)
  10170. (or (pos-visible-in-window-p max) (recenter -1))))
  10171. (select-window win))))
  10172. (defun org-table-force-dataline ()
  10173. "Make sure the cursor is in a dataline in a table."
  10174. (unless (save-excursion
  10175. (beginning-of-line 1)
  10176. (looking-at org-table-dataline-regexp))
  10177. (let* ((re org-table-dataline-regexp)
  10178. (p1 (save-excursion (re-search-forward re nil 'move)))
  10179. (p2 (save-excursion (re-search-backward re nil 'move))))
  10180. (cond ((and p1 p2)
  10181. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10182. p1 p2)))
  10183. ((or p1 p2) (goto-char (or p1 p2)))
  10184. (t (error "No table dataline around here"))))))
  10185. (defun org-table-fedit-line-up ()
  10186. "Move cursor one line up in the window showing the table."
  10187. (interactive)
  10188. (org-table-fedit-move 'previous-line))
  10189. (defun org-table-fedit-line-down ()
  10190. "Move cursor one line down in the window showing the table."
  10191. (interactive)
  10192. (org-table-fedit-move 'next-line))
  10193. (defun org-table-fedit-move (command)
  10194. "Move the cursor in the window shoinw the table.
  10195. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10196. (let ((org-table-allow-automatic-line-recalculation nil)
  10197. (pos org-pos) (win (selected-window)) p)
  10198. (select-window (get-buffer-window (marker-buffer org-pos)))
  10199. (setq p (point))
  10200. (call-interactively command)
  10201. (while (and (org-at-table-p)
  10202. (org-at-table-hline-p))
  10203. (call-interactively command))
  10204. (or (org-at-table-p) (goto-char p))
  10205. (move-marker pos (point))
  10206. (select-window win)))
  10207. (defun org-table-fedit-scroll (N)
  10208. (interactive "p")
  10209. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10210. (scroll-other-window N)))
  10211. (defun org-table-fedit-scroll-down (N)
  10212. (interactive "p")
  10213. (org-table-fedit-scroll (- N)))
  10214. (defvar org-table-rectangle-overlays nil)
  10215. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10216. "Add a new overlay."
  10217. (let ((ov (org-make-overlay beg end)))
  10218. (org-overlay-put ov 'face (or face 'secondary-selection))
  10219. (push ov org-table-rectangle-overlays)))
  10220. (defun org-table-highlight-rectangle (&optional beg end face)
  10221. "Highlight rectangular region in a table."
  10222. (setq beg (or beg (point)) end (or end (point)))
  10223. (let ((b (min beg end))
  10224. (e (max beg end))
  10225. l1 c1 l2 c2 tmp)
  10226. (and (boundp 'org-show-positions)
  10227. (setq org-show-positions (cons b (cons e org-show-positions))))
  10228. (goto-char (min beg end))
  10229. (setq l1 (org-current-line)
  10230. c1 (org-table-current-column))
  10231. (goto-char (max beg end))
  10232. (setq l2 (org-current-line)
  10233. c2 (org-table-current-column))
  10234. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10235. (goto-line l1)
  10236. (beginning-of-line 1)
  10237. (loop for line from l1 to l2 do
  10238. (when (looking-at org-table-dataline-regexp)
  10239. (org-table-goto-column c1)
  10240. (skip-chars-backward "^|\n") (setq beg (point))
  10241. (org-table-goto-column c2)
  10242. (skip-chars-forward "^|\n") (setq end (point))
  10243. (org-table-add-rectangle-overlay beg end face))
  10244. (beginning-of-line 2))
  10245. (goto-char b))
  10246. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10247. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10248. "Remove the rectangle overlays."
  10249. (unless org-inhibit-highlight-removal
  10250. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10251. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10252. (setq org-table-rectangle-overlays nil)))
  10253. (defvar org-table-coordinate-overlays nil
  10254. "Collects the cooordinate grid overlays, so that they can be removed.")
  10255. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10256. (defun org-table-overlay-coordinates ()
  10257. "Add overlays to the table at point, to show row/column coordinates."
  10258. (interactive)
  10259. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10260. (setq org-table-coordinate-overlays nil)
  10261. (save-excursion
  10262. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10263. (goto-char (org-table-begin))
  10264. (while (org-at-table-p)
  10265. (setq eol (point-at-eol))
  10266. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10267. (push ov org-table-coordinate-overlays)
  10268. (setq hline (looking-at org-table-hline-regexp))
  10269. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10270. (format "%4d" (setq id (1+ id)))))
  10271. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10272. (when hline
  10273. (setq ic 0)
  10274. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10275. (setq beg (1+ (match-beginning 0))
  10276. ic (1+ ic)
  10277. s1 (concat "$" (int-to-string ic))
  10278. s2 (org-number-to-letters ic)
  10279. str (if (eq org-table-use-standard-references t) s2 s1))
  10280. (setq ov (org-make-overlay beg (+ beg (length str))))
  10281. (push ov org-table-coordinate-overlays)
  10282. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10283. (beginning-of-line 2)))))
  10284. (defun org-table-toggle-coordinate-overlays ()
  10285. "Toggle the display of Row/Column numbers in tables."
  10286. (interactive)
  10287. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10288. (message "Row/Column number display turned %s"
  10289. (if org-table-overlay-coordinates "on" "off"))
  10290. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10291. (org-table-align))
  10292. (unless org-table-overlay-coordinates
  10293. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10294. (setq org-table-coordinate-overlays nil)))
  10295. (defun org-table-toggle-formula-debugger ()
  10296. "Toggle the formula debugger in tables."
  10297. (interactive)
  10298. (setq org-table-formula-debug (not org-table-formula-debug))
  10299. (message "Formula debugging has been turned %s"
  10300. (if org-table-formula-debug "on" "off")))
  10301. ;;; The orgtbl minor mode
  10302. ;; Define a minor mode which can be used in other modes in order to
  10303. ;; integrate the org-mode table editor.
  10304. ;; This is really a hack, because the org-mode table editor uses several
  10305. ;; keys which normally belong to the major mode, for example the TAB and
  10306. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10307. ;; necessary to operate the table editor, but wraps the commands into a
  10308. ;; function which tests if the cursor is currently inside a table. If that
  10309. ;; is the case, the table editor command is executed. However, when any of
  10310. ;; those keys is used outside a table, the function uses `key-binding' to
  10311. ;; look up if the key has an associated command in another currently active
  10312. ;; keymap (minor modes, major mode, global), and executes that command.
  10313. ;; There might be problems if any of the keys used by the table editor is
  10314. ;; otherwise used as a prefix key.
  10315. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10316. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10317. ;; addresses this by checking explicitly for both bindings.
  10318. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10319. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10320. ;; Some modes bind other commands to simple characters, for example
  10321. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10322. ;; active, this binding is ignored inside tables and replaced with a
  10323. ;; modified self-insert.
  10324. (defvar orgtbl-mode nil
  10325. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10326. table editor in arbitrary modes.")
  10327. (make-variable-buffer-local 'orgtbl-mode)
  10328. (defvar orgtbl-mode-map (make-keymap)
  10329. "Keymap for `orgtbl-mode'.")
  10330. ;;;###autoload
  10331. (defun turn-on-orgtbl ()
  10332. "Unconditionally turn on `orgtbl-mode'."
  10333. (orgtbl-mode 1))
  10334. (defvar org-old-auto-fill-inhibit-regexp nil
  10335. "Local variable used by `orgtbl-mode'")
  10336. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10337. "Matches a line belonging to an orgtbl.")
  10338. (defconst orgtbl-extra-font-lock-keywords
  10339. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10340. 0 (quote 'org-table) 'prepend))
  10341. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10342. ;;;###autoload
  10343. (defun orgtbl-mode (&optional arg)
  10344. "The `org-mode' table editor as a minor mode for use in other modes."
  10345. (interactive)
  10346. (if (org-mode-p)
  10347. ;; Exit without error, in case some hook functions calls this
  10348. ;; by accident in org-mode.
  10349. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10350. (setq orgtbl-mode
  10351. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10352. (if orgtbl-mode
  10353. (progn
  10354. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10355. ;; Make sure we are first in minor-mode-map-alist
  10356. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10357. (and c (setq minor-mode-map-alist
  10358. (cons c (delq c minor-mode-map-alist)))))
  10359. (org-set-local (quote org-table-may-need-update) t)
  10360. (org-add-hook 'before-change-functions 'org-before-change-function
  10361. nil 'local)
  10362. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10363. auto-fill-inhibit-regexp)
  10364. (org-set-local 'auto-fill-inhibit-regexp
  10365. (if auto-fill-inhibit-regexp
  10366. (concat orgtbl-line-start-regexp "\\|"
  10367. auto-fill-inhibit-regexp)
  10368. orgtbl-line-start-regexp))
  10369. (org-add-to-invisibility-spec '(org-cwidth))
  10370. (when (fboundp 'font-lock-add-keywords)
  10371. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10372. (org-restart-font-lock))
  10373. (easy-menu-add orgtbl-mode-menu)
  10374. (run-hooks 'orgtbl-mode-hook))
  10375. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10376. (org-cleanup-narrow-column-properties)
  10377. (org-remove-from-invisibility-spec '(org-cwidth))
  10378. (remove-hook 'before-change-functions 'org-before-change-function t)
  10379. (when (fboundp 'font-lock-remove-keywords)
  10380. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10381. (org-restart-font-lock))
  10382. (easy-menu-remove orgtbl-mode-menu)
  10383. (force-mode-line-update 'all))))
  10384. (defun org-cleanup-narrow-column-properties ()
  10385. "Remove all properties related to narrow-column invisibility."
  10386. (let ((s 1))
  10387. (while (setq s (text-property-any s (point-max)
  10388. 'display org-narrow-column-arrow))
  10389. (remove-text-properties s (1+ s) '(display t)))
  10390. (setq s 1)
  10391. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10392. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10393. (setq s 1)
  10394. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10395. (remove-text-properties s (1+ s) '(invisible t)))))
  10396. ;; Install it as a minor mode.
  10397. (put 'orgtbl-mode :included t)
  10398. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10399. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10400. (defun orgtbl-make-binding (fun n &rest keys)
  10401. "Create a function for binding in the table minor mode.
  10402. FUN is the command to call inside a table. N is used to create a unique
  10403. command name. KEYS are keys that should be checked in for a command
  10404. to execute outside of tables."
  10405. (eval
  10406. (list 'defun
  10407. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10408. '(arg)
  10409. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10410. "Outside of tables, run the binding of `"
  10411. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10412. "'.")
  10413. '(interactive "p")
  10414. (list 'if
  10415. '(org-at-table-p)
  10416. (list 'call-interactively (list 'quote fun))
  10417. (list 'let '(orgtbl-mode)
  10418. (list 'call-interactively
  10419. (append '(or)
  10420. (mapcar (lambda (k)
  10421. (list 'key-binding k))
  10422. keys)
  10423. '('orgtbl-error))))))))
  10424. (defun orgtbl-error ()
  10425. "Error when there is no default binding for a table key."
  10426. (interactive)
  10427. (error "This key has no function outside tables"))
  10428. (defun orgtbl-setup ()
  10429. "Setup orgtbl keymaps."
  10430. (let ((nfunc 0)
  10431. (bindings
  10432. (list
  10433. '([(meta shift left)] org-table-delete-column)
  10434. '([(meta left)] org-table-move-column-left)
  10435. '([(meta right)] org-table-move-column-right)
  10436. '([(meta shift right)] org-table-insert-column)
  10437. '([(meta shift up)] org-table-kill-row)
  10438. '([(meta shift down)] org-table-insert-row)
  10439. '([(meta up)] org-table-move-row-up)
  10440. '([(meta down)] org-table-move-row-down)
  10441. '("\C-c\C-w" org-table-cut-region)
  10442. '("\C-c\M-w" org-table-copy-region)
  10443. '("\C-c\C-y" org-table-paste-rectangle)
  10444. '("\C-c-" org-table-insert-hline)
  10445. '("\C-c}" org-table-toggle-coordinate-overlays)
  10446. '("\C-c{" org-table-toggle-formula-debugger)
  10447. '("\C-m" org-table-next-row)
  10448. '([(shift return)] org-table-copy-down)
  10449. '("\C-c\C-q" org-table-wrap-region)
  10450. '("\C-c?" org-table-field-info)
  10451. '("\C-c " org-table-blank-field)
  10452. '("\C-c+" org-table-sum)
  10453. '("\C-c=" org-table-eval-formula)
  10454. '("\C-c'" org-table-edit-formulas)
  10455. '("\C-c`" org-table-edit-field)
  10456. '("\C-c*" org-table-recalculate)
  10457. '("\C-c|" org-table-create-or-convert-from-region)
  10458. '("\C-c^" org-table-sort-lines)
  10459. '([(control ?#)] org-table-rotate-recalc-marks)))
  10460. elt key fun cmd)
  10461. (while (setq elt (pop bindings))
  10462. (setq nfunc (1+ nfunc))
  10463. (setq key (org-key (car elt))
  10464. fun (nth 1 elt)
  10465. cmd (orgtbl-make-binding fun nfunc key))
  10466. (org-defkey orgtbl-mode-map key cmd))
  10467. ;; Special treatment needed for TAB and RET
  10468. (org-defkey orgtbl-mode-map [(return)]
  10469. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10470. (org-defkey orgtbl-mode-map "\C-m"
  10471. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10472. (org-defkey orgtbl-mode-map [(tab)]
  10473. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10474. (org-defkey orgtbl-mode-map "\C-i"
  10475. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10476. (org-defkey orgtbl-mode-map [(shift tab)]
  10477. (orgtbl-make-binding 'org-table-previous-field 104
  10478. [(shift tab)] [(tab)] "\C-i"))
  10479. (org-defkey orgtbl-mode-map "\M-\C-m"
  10480. (orgtbl-make-binding 'org-table-wrap-region 105
  10481. "\M-\C-m" [(meta return)]))
  10482. (org-defkey orgtbl-mode-map [(meta return)]
  10483. (orgtbl-make-binding 'org-table-wrap-region 106
  10484. [(meta return)] "\M-\C-m"))
  10485. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10486. (when orgtbl-optimized
  10487. ;; If the user wants maximum table support, we need to hijack
  10488. ;; some standard editing functions
  10489. (org-remap orgtbl-mode-map
  10490. 'self-insert-command 'orgtbl-self-insert-command
  10491. 'delete-char 'org-delete-char
  10492. 'delete-backward-char 'org-delete-backward-char)
  10493. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10494. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10495. '("OrgTbl"
  10496. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10497. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10498. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10499. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10500. "--"
  10501. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10502. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10503. ["Copy Field from Above"
  10504. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10505. "--"
  10506. ("Column"
  10507. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10508. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10509. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10510. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10511. ("Row"
  10512. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10513. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10514. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10515. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10516. ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
  10517. "--"
  10518. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10519. ("Rectangle"
  10520. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10521. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10522. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10523. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10524. "--"
  10525. ("Radio tables"
  10526. ["Insert table template" orgtbl-insert-radio-table
  10527. (assq major-mode orgtbl-radio-table-templates)]
  10528. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10529. "--"
  10530. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10531. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10532. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10533. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10534. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10535. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10536. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10537. ["Sum Column/Rectangle" org-table-sum
  10538. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10539. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10540. ["Debug Formulas"
  10541. org-table-toggle-formula-debugger :active (org-at-table-p)
  10542. :keys "C-c {"
  10543. :style toggle :selected org-table-formula-debug]
  10544. ["Show Col/Row Numbers"
  10545. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10546. :keys "C-c }"
  10547. :style toggle :selected org-table-overlay-coordinates]
  10548. ))
  10549. t))
  10550. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10551. "If the cursor is inside a table, realign the table.
  10552. It it is a table to be sent away to a receiver, do it.
  10553. With prefix arg, also recompute table."
  10554. (interactive "P")
  10555. (let ((pos (point)) action)
  10556. (save-excursion
  10557. (beginning-of-line 1)
  10558. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10559. ((looking-at "[ \t]*|") pos)
  10560. ((looking-at "#\\+TBLFM:") 'recalc))))
  10561. (cond
  10562. ((integerp action)
  10563. (goto-char action)
  10564. (org-table-maybe-eval-formula)
  10565. (if arg
  10566. (call-interactively 'org-table-recalculate)
  10567. (org-table-maybe-recalculate-line))
  10568. (call-interactively 'org-table-align)
  10569. (orgtbl-send-table 'maybe))
  10570. ((eq action 'recalc)
  10571. (save-excursion
  10572. (beginning-of-line 1)
  10573. (skip-chars-backward " \r\n\t")
  10574. (if (org-at-table-p)
  10575. (org-call-with-arg 'org-table-recalculate t))))
  10576. (t (let (orgtbl-mode)
  10577. (call-interactively (key-binding "\C-c\C-c")))))))
  10578. (defun orgtbl-tab (arg)
  10579. "Justification and field motion for `orgtbl-mode'."
  10580. (interactive "P")
  10581. (if arg (org-table-edit-field t)
  10582. (org-table-justify-field-maybe)
  10583. (org-table-next-field)))
  10584. (defun orgtbl-ret ()
  10585. "Justification and field motion for `orgtbl-mode'."
  10586. (interactive)
  10587. (org-table-justify-field-maybe)
  10588. (org-table-next-row))
  10589. (defun orgtbl-self-insert-command (N)
  10590. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10591. If the cursor is in a table looking at whitespace, the whitespace is
  10592. overwritten, and the table is not marked as requiring realignment."
  10593. (interactive "p")
  10594. (if (and (org-at-table-p)
  10595. (or
  10596. (and org-table-auto-blank-field
  10597. (member last-command
  10598. '(orgtbl-hijacker-command-100
  10599. orgtbl-hijacker-command-101
  10600. orgtbl-hijacker-command-102
  10601. orgtbl-hijacker-command-103
  10602. orgtbl-hijacker-command-104
  10603. orgtbl-hijacker-command-105))
  10604. (org-table-blank-field))
  10605. t)
  10606. (eq N 1)
  10607. (looking-at "[^|\n]* +|"))
  10608. (let (org-table-may-need-update)
  10609. (goto-char (1- (match-end 0)))
  10610. (delete-backward-char 1)
  10611. (goto-char (match-beginning 0))
  10612. (self-insert-command N))
  10613. (setq org-table-may-need-update t)
  10614. (let (orgtbl-mode)
  10615. (call-interactively (key-binding (vector last-input-event))))))
  10616. (defun org-force-self-insert (N)
  10617. "Needed to enforce self-insert under remapping."
  10618. (interactive "p")
  10619. (self-insert-command N))
  10620. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10621. "Regula expression matching exponentials as produced by calc.")
  10622. (defvar org-table-clean-did-remove-column nil)
  10623. (defun orgtbl-export (table target)
  10624. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10625. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10626. org-table-last-alignment org-table-last-column-widths
  10627. maxcol column)
  10628. (if (not (fboundp func))
  10629. (error "Cannot export orgtbl table to %s" target))
  10630. (setq lines (org-table-clean-before-export lines))
  10631. (setq table
  10632. (mapcar
  10633. (lambda (x)
  10634. (if (string-match org-table-hline-regexp x)
  10635. 'hline
  10636. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10637. lines))
  10638. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10639. table)))
  10640. (loop for i from (1- maxcol) downto 0 do
  10641. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10642. (setq column (delq nil column))
  10643. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10644. (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))
  10645. (funcall func table nil)))
  10646. (defun orgtbl-send-table (&optional maybe)
  10647. "Send a tranformed version of this table to the receiver position.
  10648. With argument MAYBE, fail quietly if no transformation is defined for
  10649. this table."
  10650. (interactive)
  10651. (catch 'exit
  10652. (unless (org-at-table-p) (error "Not at a table"))
  10653. ;; when non-interactive, we assume align has just happened.
  10654. (when (interactive-p) (org-table-align))
  10655. (save-excursion
  10656. (goto-char (org-table-begin))
  10657. (beginning-of-line 0)
  10658. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10659. (if maybe
  10660. (throw 'exit nil)
  10661. (error "Don't know how to transform this table."))))
  10662. (let* ((name (match-string 1))
  10663. beg
  10664. (transform (intern (match-string 2)))
  10665. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10666. (skip (plist-get params :skip))
  10667. (skipcols (plist-get params :skipcols))
  10668. (txt (buffer-substring-no-properties
  10669. (org-table-begin) (org-table-end)))
  10670. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10671. (lines (org-table-clean-before-export lines))
  10672. (i0 (if org-table-clean-did-remove-column 2 1))
  10673. (table (mapcar
  10674. (lambda (x)
  10675. (if (string-match org-table-hline-regexp x)
  10676. 'hline
  10677. (org-remove-by-index
  10678. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10679. skipcols i0)))
  10680. lines))
  10681. (fun (if (= i0 2) 'cdr 'identity))
  10682. (org-table-last-alignment
  10683. (org-remove-by-index (funcall fun org-table-last-alignment)
  10684. skipcols i0))
  10685. (org-table-last-column-widths
  10686. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10687. skipcols i0)))
  10688. (unless (fboundp transform)
  10689. (error "No such transformation function %s" transform))
  10690. (setq txt (funcall transform table params))
  10691. ;; Find the insertion place
  10692. (save-excursion
  10693. (goto-char (point-min))
  10694. (unless (re-search-forward
  10695. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10696. (error "Don't know where to insert translated table"))
  10697. (goto-char (match-beginning 0))
  10698. (beginning-of-line 2)
  10699. (setq beg (point))
  10700. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10701. (error "Cannot find end of insertion region"))
  10702. (beginning-of-line 1)
  10703. (delete-region beg (point))
  10704. (goto-char beg)
  10705. (insert txt "\n"))
  10706. (message "Table converted and installed at receiver location"))))
  10707. (defun org-remove-by-index (list indices &optional i0)
  10708. "Remove the elements in LIST with indices in INDICES.
  10709. First element has index 0, or I0 if given."
  10710. (if (not indices)
  10711. list
  10712. (if (integerp indices) (setq indices (list indices)))
  10713. (setq i0 (1- (or i0 0)))
  10714. (delq :rm (mapcar (lambda (x)
  10715. (setq i0 (1+ i0))
  10716. (if (memq i0 indices) :rm x))
  10717. list))))
  10718. (defun orgtbl-toggle-comment ()
  10719. "Comment or uncomment the orgtbl at point."
  10720. (interactive)
  10721. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10722. (re2 (concat "^" orgtbl-line-start-regexp))
  10723. (commented (save-excursion (beginning-of-line 1)
  10724. (cond ((looking-at re1) t)
  10725. ((looking-at re2) nil)
  10726. (t (error "Not at an org table")))))
  10727. (re (if commented re1 re2))
  10728. beg end)
  10729. (save-excursion
  10730. (beginning-of-line 1)
  10731. (while (looking-at re) (beginning-of-line 0))
  10732. (beginning-of-line 2)
  10733. (setq beg (point))
  10734. (while (looking-at re) (beginning-of-line 2))
  10735. (setq end (point)))
  10736. (comment-region beg end (if commented '(4) nil))))
  10737. (defun orgtbl-insert-radio-table ()
  10738. "Insert a radio table template appropriate for this major mode."
  10739. (interactive)
  10740. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10741. (txt (nth 1 e))
  10742. name pos)
  10743. (unless e (error "No radio table setup defined for %s" major-mode))
  10744. (setq name (read-string "Table name: "))
  10745. (while (string-match "%n" txt)
  10746. (setq txt (replace-match name t t txt)))
  10747. (or (bolp) (insert "\n"))
  10748. (setq pos (point))
  10749. (insert txt)
  10750. (goto-char pos)))
  10751. (defun org-get-param (params header i sym &optional hsym)
  10752. "Get parameter value for symbol SYM.
  10753. If this is a header line, actually get the value for the symbol with an
  10754. additional \"h\" inserted after the colon.
  10755. If the value is a protperty list, get the element for the current column.
  10756. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10757. (let ((val (plist-get params sym)))
  10758. (and hsym header (setq val (or (plist-get params hsym) val)))
  10759. (if (consp val) (plist-get val i) val)))
  10760. (defun orgtbl-to-generic (table params)
  10761. "Convert the orgtbl-mode TABLE to some other format.
  10762. This generic routine can be used for many standard cases.
  10763. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10764. separator line, or a list of fields for that line.
  10765. PARAMS is a property list of parameters that can influence the conversion.
  10766. For the generic converter, some parameters are obligatory: You need to
  10767. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10768. :splice, you must have :tstart and :tend.
  10769. Valid parameters are
  10770. :tstart String to start the table. Ignored when :splice is t.
  10771. :tend String to end the table. Ignored when :splice is t.
  10772. :splice When set to t, return only table body lines, don't wrap
  10773. them into :tstart and :tend. Default is nil.
  10774. :hline String to be inserted on horizontal separation lines.
  10775. May be nil to ignore hlines.
  10776. :lstart String to start a new table line.
  10777. :lend String to end a table line
  10778. :sep Separator between two fields
  10779. :lfmt Format for entire line, with enough %s to capture all fields.
  10780. If this is present, :lstart, :lend, and :sep are ignored.
  10781. :fmt A format to be used to wrap the field, should contain
  10782. %s for the original field value. For example, to wrap
  10783. everything in dollars, you could use :fmt \"$%s$\".
  10784. This may also be a property list with column numbers and
  10785. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10786. :hlstart :hlend :hlsep :hlfmt :hfmt
  10787. Same as above, specific for the header lines in the table.
  10788. All lines before the first hline are treated as header.
  10789. If any of these is not present, the data line value is used.
  10790. :efmt Use this format to print numbers with exponentials.
  10791. The format should have %s twice for inserting mantissa
  10792. and exponent, for example \"%s\\\\times10^{%s}\". This
  10793. may also be a property list with column numbers and
  10794. formats. :fmt will still be applied after :efmt.
  10795. In addition to this, the parameters :skip and :skipcols are always handled
  10796. directly by `orgtbl-send-table'. See manual."
  10797. (interactive)
  10798. (let* ((p params)
  10799. (splicep (plist-get p :splice))
  10800. (hline (plist-get p :hline))
  10801. rtn line i fm efm lfmt h)
  10802. ;; Do we have a header?
  10803. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10804. (setq h t))
  10805. ;; Put header
  10806. (unless splicep
  10807. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10808. ;; Now loop over all lines
  10809. (while (setq line (pop table))
  10810. (if (eq line 'hline)
  10811. ;; A horizontal separator line
  10812. (progn (if hline (push hline rtn))
  10813. (setq h nil)) ; no longer in header
  10814. ;; A normal line. Convert the fields, push line onto the result list
  10815. (setq i 0)
  10816. (setq line
  10817. (mapcar
  10818. (lambda (f)
  10819. (setq i (1+ i)
  10820. fm (org-get-param p h i :fmt :hfmt)
  10821. efm (org-get-param p h i :efmt))
  10822. (if (and efm (string-match orgtbl-exp-regexp f))
  10823. (setq f (format
  10824. efm (match-string 1 f) (match-string 2 f))))
  10825. (if fm (setq f (format fm f)))
  10826. f)
  10827. line))
  10828. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10829. (push (apply 'format lfmt line) rtn)
  10830. (push (concat
  10831. (org-get-param p h i :lstart :hlstart)
  10832. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10833. (org-get-param p h i :lend :hlend))
  10834. rtn))))
  10835. (unless splicep
  10836. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10837. (mapconcat 'identity (nreverse rtn) "\n")))
  10838. (defun orgtbl-to-latex (table params)
  10839. "Convert the orgtbl-mode TABLE to LaTeX.
  10840. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10841. separator line, or a list of fields for that line.
  10842. PARAMS is a property list of parameters that can influence the conversion.
  10843. Supports all parameters from `orgtbl-to-generic'. Most important for
  10844. LaTeX are:
  10845. :splice When set to t, return only table body lines, don't wrap
  10846. them into a tabular environment. Default is nil.
  10847. :fmt A format to be used to wrap the field, should contain %s for the
  10848. original field value. For example, to wrap everything in dollars,
  10849. use :fmt \"$%s$\". This may also be a property list with column
  10850. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10851. :efmt Format for transforming numbers with exponentials. The format
  10852. should have %s twice for inserting mantissa and exponent, for
  10853. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10854. This may also be a property list with column numbers and formats.
  10855. The general parameters :skip and :skipcols have already been applied when
  10856. this function is called."
  10857. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10858. org-table-last-alignment ""))
  10859. (params2
  10860. (list
  10861. :tstart (concat "\\begin{tabular}{" alignment "}")
  10862. :tend "\\end{tabular}"
  10863. :lstart "" :lend " \\\\" :sep " & "
  10864. :efmt "%s\\,(%s)" :hline "\\hline")))
  10865. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10866. (defun orgtbl-to-html (table params)
  10867. "Convert the orgtbl-mode TABLE to LaTeX.
  10868. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10869. separator line, or a list of fields for that line.
  10870. PARAMS is a property list of parameters that can influence the conversion.
  10871. Currently this function recognizes the following parameters:
  10872. :splice When set to t, return only table body lines, don't wrap
  10873. them into a <table> environment. Default is nil.
  10874. The general parameters :skip and :skipcols have already been applied when
  10875. this function is called. The function does *not* use `orgtbl-to-generic',
  10876. so you cannot specify parameters for it."
  10877. (let* ((splicep (plist-get params :splice))
  10878. html)
  10879. ;; Just call the formatter we already have
  10880. ;; We need to make text lines for it, so put the fields back together.
  10881. (setq html (org-format-org-table-html
  10882. (mapcar
  10883. (lambda (x)
  10884. (if (eq x 'hline)
  10885. "|----+----|"
  10886. (concat "| " (mapconcat 'identity x " | ") " |")))
  10887. table)
  10888. splicep))
  10889. (if (string-match "\n+\\'" html)
  10890. (setq html (replace-match "" t t html)))
  10891. html))
  10892. (defun orgtbl-to-texinfo (table params)
  10893. "Convert the orgtbl-mode TABLE to TeXInfo.
  10894. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10895. separator line, or a list of fields for that line.
  10896. PARAMS is a property list of parameters that can influence the conversion.
  10897. Supports all parameters from `orgtbl-to-generic'. Most important for
  10898. TeXInfo are:
  10899. :splice nil/t When set to t, return only table body lines, don't wrap
  10900. them into a multitable environment. Default is nil.
  10901. :fmt fmt A format to be used to wrap the field, should contain
  10902. %s for the original field value. For example, to wrap
  10903. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10904. This may also be a property list with column numbers and
  10905. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10906. :cf \"f1 f2..\" The column fractions for the table. By default these
  10907. are computed automatically from the width of the columns
  10908. under org-mode.
  10909. The general parameters :skip and :skipcols have already been applied when
  10910. this function is called."
  10911. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10912. (colfrac (or (plist-get params :cf)
  10913. (mapconcat
  10914. (lambda (x) (format "%.3f" (/ (float x) total)))
  10915. org-table-last-column-widths " ")))
  10916. (params2
  10917. (list
  10918. :tstart (concat "@multitable @columnfractions " colfrac)
  10919. :tend "@end multitable"
  10920. :lstart "@item " :lend "" :sep " @tab "
  10921. :hlstart "@headitem ")))
  10922. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10923. ;;;; Link Stuff
  10924. ;;; Link abbreviations
  10925. (defun org-link-expand-abbrev (link)
  10926. "Apply replacements as defined in `org-link-abbrev-alist."
  10927. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10928. (let* ((key (match-string 1 link))
  10929. (as (or (assoc key org-link-abbrev-alist-local)
  10930. (assoc key org-link-abbrev-alist)))
  10931. (tag (and (match-end 2) (match-string 3 link)))
  10932. rpl)
  10933. (if (not as)
  10934. link
  10935. (setq rpl (cdr as))
  10936. (cond
  10937. ((symbolp rpl) (funcall rpl tag))
  10938. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10939. (t (concat rpl tag)))))
  10940. link))
  10941. ;;; Storing and inserting links
  10942. (defvar org-insert-link-history nil
  10943. "Minibuffer history for links inserted with `org-insert-link'.")
  10944. (defvar org-stored-links nil
  10945. "Contains the links stored with `org-store-link'.")
  10946. (defvar org-store-link-plist nil
  10947. "Plist with info about the most recently link created with `org-store-link'.")
  10948. (defvar org-link-protocols nil
  10949. "Link protocols added to Org-mode using `org-add-link-type'.")
  10950. (defvar org-store-link-functions nil
  10951. "List of functions that are called to create and store a link.
  10952. Each function will be called in turn until one returns a non-nil
  10953. value. Each function should check if it is responsible for creating
  10954. this link (for example by looking at the major mode).
  10955. If not, it must exit and return nil.
  10956. If yes, it should return a non-nil value after a calling
  10957. `org-store-link-props' with a list of properties and values.
  10958. Special properties are:
  10959. :type The link prefix. like \"http\". This must be given.
  10960. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10961. This is obligatory as well.
  10962. :description Optional default description for the second pair
  10963. of brackets in an Org-mode link. The user can still change
  10964. this when inserting this link into an Org-mode buffer.
  10965. In addition to these, any additional properties can be specified
  10966. and then used in remember templates.")
  10967. (defun org-add-link-type (type &optional follow publish)
  10968. "Add TYPE to the list of `org-link-types'.
  10969. Re-compute all regular expressions depending on `org-link-types'
  10970. FOLLOW and PUBLISH are two functions. Both take the link path as
  10971. an argument.
  10972. FOLLOW should do whatever is necessary to follow the link, for example
  10973. to find a file or display a mail message.
  10974. PUBLISH takes the path and retuns the string that should be used when
  10975. this document is published. FIMXE: This is actually not yet implemented."
  10976. (add-to-list 'org-link-types type t)
  10977. (org-make-link-regexps)
  10978. (add-to-list 'org-link-protocols
  10979. (list type follow publish)))
  10980. (defun org-add-agenda-custom-command (entry)
  10981. "Replace or add a command in `org-agenda-custom-commands'.
  10982. This is mostly for hacking and trying a new command - once the command
  10983. works you probably want to add it to `org-agenda-custom-commands' for good."
  10984. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10985. (if ass
  10986. (setcdr ass (cdr entry))
  10987. (push entry org-agenda-custom-commands))))
  10988. ;;;###autoload
  10989. (defun org-store-link (arg)
  10990. "\\<org-mode-map>Store an org-link to the current location.
  10991. This link is added to `org-stored-links' and can later be inserted
  10992. into an org-buffer with \\[org-insert-link].
  10993. For some link types, a prefix arg is interpreted:
  10994. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10995. For file links, arg negates `org-context-in-file-links'."
  10996. (interactive "P")
  10997. (setq org-store-link-plist nil) ; reset
  10998. (let (link cpltxt desc description search txt)
  10999. (cond
  11000. ((run-hook-with-args-until-success 'org-store-link-functions)
  11001. (setq link (plist-get org-store-link-plist :link)
  11002. desc (or (plist-get org-store-link-plist :description) link)))
  11003. ((eq major-mode 'bbdb-mode)
  11004. (let ((name (bbdb-record-name (bbdb-current-record)))
  11005. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  11006. (setq cpltxt (concat "bbdb:" (or name company))
  11007. link (org-make-link cpltxt))
  11008. (org-store-link-props :type "bbdb" :name name :company company)))
  11009. ((eq major-mode 'Info-mode)
  11010. (setq link (org-make-link "info:"
  11011. (file-name-nondirectory Info-current-file)
  11012. ":" Info-current-node))
  11013. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  11014. ":" Info-current-node))
  11015. (org-store-link-props :type "info" :file Info-current-file
  11016. :node Info-current-node))
  11017. ((eq major-mode 'calendar-mode)
  11018. (let ((cd (calendar-cursor-to-date)))
  11019. (setq link
  11020. (format-time-string
  11021. (car org-time-stamp-formats)
  11022. (apply 'encode-time
  11023. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  11024. nil nil nil))))
  11025. (org-store-link-props :type "calendar" :date cd)))
  11026. ((or (eq major-mode 'vm-summary-mode)
  11027. (eq major-mode 'vm-presentation-mode))
  11028. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  11029. (vm-follow-summary-cursor)
  11030. (save-excursion
  11031. (vm-select-folder-buffer)
  11032. (let* ((message (car vm-message-pointer))
  11033. (folder buffer-file-name)
  11034. (subject (vm-su-subject message))
  11035. (to (vm-get-header-contents message "To"))
  11036. (from (vm-get-header-contents message "From"))
  11037. (message-id (vm-su-message-id message)))
  11038. (org-store-link-props :type "vm" :from from :to to :subject subject
  11039. :message-id message-id)
  11040. (setq message-id (org-remove-angle-brackets message-id))
  11041. (setq folder (abbreviate-file-name folder))
  11042. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  11043. folder)
  11044. (setq folder (replace-match "" t t folder)))
  11045. (setq cpltxt (org-email-link-description))
  11046. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11047. ((eq major-mode 'wl-summary-mode)
  11048. (let* ((msgnum (wl-summary-message-number))
  11049. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11050. msgnum 'message-id))
  11051. (wl-message-entity
  11052. (if (fboundp 'elmo-message-entity)
  11053. (elmo-message-entity
  11054. wl-summary-buffer-elmo-folder msgnum)
  11055. (elmo-msgdb-overview-get-entity
  11056. msgnum (wl-summary-buffer-msgdb))))
  11057. (from (wl-summary-line-from))
  11058. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11059. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11060. (wl-summary-line-subject))))
  11061. (org-store-link-props :type "wl" :from from :to to
  11062. :subject subject :message-id message-id)
  11063. (setq message-id (org-remove-angle-brackets message-id))
  11064. (setq cpltxt (org-email-link-description))
  11065. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11066. "#" message-id))))
  11067. ((or (equal major-mode 'mh-folder-mode)
  11068. (equal major-mode 'mh-show-mode))
  11069. (let ((from (org-mhe-get-header "From:"))
  11070. (to (org-mhe-get-header "To:"))
  11071. (message-id (org-mhe-get-header "Message-Id:"))
  11072. (subject (org-mhe-get-header "Subject:")))
  11073. (org-store-link-props :type "mh" :from from :to to
  11074. :subject subject :message-id message-id)
  11075. (setq cpltxt (org-email-link-description))
  11076. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11077. (org-remove-angle-brackets message-id)))))
  11078. ((or (eq major-mode 'rmail-mode)
  11079. (eq major-mode 'rmail-summary-mode))
  11080. (save-window-excursion
  11081. (save-restriction
  11082. (when (eq major-mode 'rmail-summary-mode)
  11083. (rmail-show-message rmail-current-message))
  11084. (rmail-narrow-to-non-pruned-header)
  11085. (let ((folder buffer-file-name)
  11086. (message-id (mail-fetch-field "message-id"))
  11087. (from (mail-fetch-field "from"))
  11088. (to (mail-fetch-field "to"))
  11089. (subject (mail-fetch-field "subject")))
  11090. (org-store-link-props
  11091. :type "rmail" :from from :to to
  11092. :subject subject :message-id message-id)
  11093. (setq message-id (org-remove-angle-brackets message-id))
  11094. (setq cpltxt (org-email-link-description))
  11095. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11096. (rmail-show-message rmail-current-message))))
  11097. ((eq major-mode 'gnus-group-mode)
  11098. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11099. (gnus-group-group-name)) ; version
  11100. ((fboundp 'gnus-group-name)
  11101. (gnus-group-name))
  11102. (t "???"))))
  11103. (unless group (error "Not on a group"))
  11104. (org-store-link-props :type "gnus" :group group)
  11105. (setq cpltxt (concat
  11106. (if (org-xor arg org-usenet-links-prefer-google)
  11107. "http://groups.google.com/groups?group="
  11108. "gnus:")
  11109. group)
  11110. link (org-make-link cpltxt))))
  11111. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11112. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11113. (let* ((group gnus-newsgroup-name)
  11114. (article (gnus-summary-article-number))
  11115. (header (gnus-summary-article-header article))
  11116. (from (mail-header-from header))
  11117. (message-id (mail-header-id header))
  11118. (date (mail-header-date header))
  11119. (subject (gnus-summary-subject-string)))
  11120. (org-store-link-props :type "gnus" :from from :subject subject
  11121. :message-id message-id :group group)
  11122. (setq cpltxt (org-email-link-description))
  11123. (if (org-xor arg org-usenet-links-prefer-google)
  11124. (setq link
  11125. (concat
  11126. cpltxt "\n "
  11127. (format "http://groups.google.com/groups?as_umsgid=%s"
  11128. (org-fixup-message-id-for-http message-id))))
  11129. (setq link (org-make-link "gnus:" group
  11130. "#" (number-to-string article))))))
  11131. ((eq major-mode 'w3-mode)
  11132. (setq cpltxt (url-view-url t)
  11133. link (org-make-link cpltxt))
  11134. (org-store-link-props :type "w3" :url (url-view-url t)))
  11135. ((eq major-mode 'w3m-mode)
  11136. (setq cpltxt (or w3m-current-title w3m-current-url)
  11137. link (org-make-link w3m-current-url))
  11138. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11139. ((setq search (run-hook-with-args-until-success
  11140. 'org-create-file-search-functions))
  11141. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11142. "::" search))
  11143. (setq cpltxt (or description link)))
  11144. ((eq major-mode 'image-mode)
  11145. (setq cpltxt (concat "file:"
  11146. (abbreviate-file-name buffer-file-name))
  11147. link (org-make-link cpltxt))
  11148. (org-store-link-props :type "image" :file buffer-file-name))
  11149. ((eq major-mode 'dired-mode)
  11150. ;; link to the file in the current line
  11151. (setq cpltxt (concat "file:"
  11152. (abbreviate-file-name
  11153. (expand-file-name
  11154. (dired-get-filename nil t))))
  11155. link (org-make-link cpltxt)))
  11156. ((and buffer-file-name (org-mode-p))
  11157. ;; Just link to current headline
  11158. (setq cpltxt (concat "file:"
  11159. (abbreviate-file-name buffer-file-name)))
  11160. ;; Add a context search string
  11161. (when (org-xor org-context-in-file-links arg)
  11162. ;; Check if we are on a target
  11163. (if (org-in-regexp "<<\\(.*?\\)>>")
  11164. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11165. (setq txt (cond
  11166. ((org-on-heading-p) nil)
  11167. ((org-region-active-p)
  11168. (buffer-substring (region-beginning) (region-end)))
  11169. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11170. (when (or (null txt) (string-match "\\S-" txt))
  11171. (setq cpltxt
  11172. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11173. desc "NONE"))))
  11174. (if (string-match "::\\'" cpltxt)
  11175. (setq cpltxt (substring cpltxt 0 -2)))
  11176. (setq link (org-make-link cpltxt)))
  11177. ((buffer-file-name (buffer-base-buffer))
  11178. ;; Just link to this file here.
  11179. (setq cpltxt (concat "file:"
  11180. (abbreviate-file-name
  11181. (buffer-file-name (buffer-base-buffer)))))
  11182. ;; Add a context string
  11183. (when (org-xor org-context-in-file-links arg)
  11184. (setq txt (if (org-region-active-p)
  11185. (buffer-substring (region-beginning) (region-end))
  11186. (buffer-substring (point-at-bol) (point-at-eol))))
  11187. ;; Only use search option if there is some text.
  11188. (when (string-match "\\S-" txt)
  11189. (setq cpltxt
  11190. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11191. desc "NONE")))
  11192. (setq link (org-make-link cpltxt)))
  11193. ((interactive-p)
  11194. (error "Cannot link to a buffer which is not visiting a file"))
  11195. (t (setq link nil)))
  11196. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11197. (setq link (or link cpltxt)
  11198. desc (or desc cpltxt))
  11199. (if (equal desc "NONE") (setq desc nil))
  11200. (if (and (interactive-p) link)
  11201. (progn
  11202. (setq org-stored-links
  11203. (cons (list link desc) org-stored-links))
  11204. (message "Stored: %s" (or desc link)))
  11205. (and link (org-make-link-string link desc)))))
  11206. (defun org-store-link-props (&rest plist)
  11207. "Store link properties, extract names and addresses."
  11208. (let (x adr)
  11209. (when (setq x (plist-get plist :from))
  11210. (setq adr (mail-extract-address-components x))
  11211. (plist-put plist :fromname (car adr))
  11212. (plist-put plist :fromaddress (nth 1 adr)))
  11213. (when (setq x (plist-get plist :to))
  11214. (setq adr (mail-extract-address-components x))
  11215. (plist-put plist :toname (car adr))
  11216. (plist-put plist :toaddress (nth 1 adr))))
  11217. (let ((from (plist-get plist :from))
  11218. (to (plist-get plist :to)))
  11219. (when (and from to org-from-is-user-regexp)
  11220. (plist-put plist :fromto
  11221. (if (string-match org-from-is-user-regexp from)
  11222. (concat "to %t")
  11223. (concat "from %f")))))
  11224. (setq org-store-link-plist plist))
  11225. (defun org-email-link-description (&optional fmt)
  11226. "Return the description part of an email link.
  11227. This takes information from `org-store-link-plist' and formats it
  11228. according to FMT (default from `org-email-link-description-format')."
  11229. (setq fmt (or fmt org-email-link-description-format))
  11230. (let* ((p org-store-link-plist)
  11231. (to (plist-get p :toaddress))
  11232. (from (plist-get p :fromaddress))
  11233. (table
  11234. (list
  11235. (cons "%c" (plist-get p :fromto))
  11236. (cons "%F" (plist-get p :from))
  11237. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11238. (cons "%T" (plist-get p :to))
  11239. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11240. (cons "%s" (plist-get p :subject))
  11241. (cons "%m" (plist-get p :message-id)))))
  11242. (when (string-match "%c" fmt)
  11243. ;; Check if the user wrote this message
  11244. (if (and org-from-is-user-regexp from to
  11245. (save-match-data (string-match org-from-is-user-regexp from)))
  11246. (setq fmt (replace-match "to %t" t t fmt))
  11247. (setq fmt (replace-match "from %f" t t fmt))))
  11248. (org-replace-escapes fmt table)))
  11249. (defun org-make-org-heading-search-string (&optional string heading)
  11250. "Make search string for STRING or current headline."
  11251. (interactive)
  11252. (let ((s (or string (org-get-heading))))
  11253. (unless (and string (not heading))
  11254. ;; We are using a headline, clean up garbage in there.
  11255. (if (string-match org-todo-regexp s)
  11256. (setq s (replace-match "" t t s)))
  11257. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11258. (setq s (replace-match "" t t s)))
  11259. (setq s (org-trim s))
  11260. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11261. org-comment-string "\\)") s)
  11262. (setq s (replace-match "" t t s)))
  11263. (while (string-match org-ts-regexp s)
  11264. (setq s (replace-match "" t t s))))
  11265. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11266. (setq s (replace-match " " t t s)))
  11267. (or string (setq s (concat "*" s))) ; Add * for headlines
  11268. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11269. (defun org-make-link (&rest strings)
  11270. "Concatenate STRINGS."
  11271. (apply 'concat strings))
  11272. (defun org-make-link-string (link &optional description)
  11273. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11274. (unless (string-match "\\S-" link)
  11275. (error "Empty link"))
  11276. (when (stringp description)
  11277. ;; Remove brackets from the description, they are fatal.
  11278. (while (string-match "\\[" description)
  11279. (setq description (replace-match "{" t t description)))
  11280. (while (string-match "\\]" description)
  11281. (setq description (replace-match "}" t t description))))
  11282. (when (equal (org-link-escape link) description)
  11283. ;; No description needed, it is identical
  11284. (setq description nil))
  11285. (when (and (not description)
  11286. (not (equal link (org-link-escape link))))
  11287. (setq description link))
  11288. (concat "[[" (org-link-escape link) "]"
  11289. (if description (concat "[" description "]") "")
  11290. "]"))
  11291. (defconst org-link-escape-chars
  11292. '((?\ . "%20")
  11293. (?\[ . "%5B")
  11294. (?\] . "%5D")
  11295. (?\340 . "%E0") ; `a
  11296. (?\342 . "%E2") ; ^a
  11297. (?\347 . "%E7") ; ,c
  11298. (?\350 . "%E8") ; `e
  11299. (?\351 . "%E9") ; 'e
  11300. (?\352 . "%EA") ; ^e
  11301. (?\356 . "%EE") ; ^i
  11302. (?\364 . "%F4") ; ^o
  11303. (?\371 . "%F9") ; `u
  11304. (?\373 . "%FB") ; ^u
  11305. (?\; . "%3B")
  11306. (?? . "%3F")
  11307. (?= . "%3D")
  11308. (?+ . "%2B")
  11309. )
  11310. "Association list of escapes for some characters problematic in links.
  11311. This is the list that is used for internal purposes.")
  11312. (defconst org-link-escape-chars-browser
  11313. '((?\ . "%20")) ; 32 for the SPC char
  11314. "Association list of escapes for some characters problematic in links.
  11315. This is the list that is used before handing over to the browser.")
  11316. (defun org-link-escape (text &optional table)
  11317. "Escape charaters in TEXT that are problematic for links."
  11318. (setq table (or table org-link-escape-chars))
  11319. (when text
  11320. (let ((re (mapconcat (lambda (x) (regexp-quote
  11321. (char-to-string (car x))))
  11322. table "\\|")))
  11323. (while (string-match re text)
  11324. (setq text
  11325. (replace-match
  11326. (cdr (assoc (string-to-char (match-string 0 text))
  11327. table))
  11328. t t text)))
  11329. text)))
  11330. (defun org-link-unescape (text &optional table)
  11331. "Reverse the action of `org-link-escape'."
  11332. (setq table (or table org-link-escape-chars))
  11333. (when text
  11334. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11335. table "\\|")))
  11336. (while (string-match re text)
  11337. (setq text
  11338. (replace-match
  11339. (char-to-string (car (rassoc (match-string 0 text) table)))
  11340. t t text)))
  11341. text)))
  11342. (defun org-xor (a b)
  11343. "Exclusive or."
  11344. (if a (not b) b))
  11345. (defun org-get-header (header)
  11346. "Find a header field in the current buffer."
  11347. (save-excursion
  11348. (goto-char (point-min))
  11349. (let ((case-fold-search t) s)
  11350. (cond
  11351. ((eq header 'from)
  11352. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11353. (setq s (match-string 1)))
  11354. (while (string-match "\"" s)
  11355. (setq s (replace-match "" t t s)))
  11356. (if (string-match "[<(].*" s)
  11357. (setq s (replace-match "" t t s))))
  11358. ((eq header 'message-id)
  11359. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11360. (setq s (match-string 1))))
  11361. ((eq header 'subject)
  11362. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11363. (setq s (match-string 1)))))
  11364. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11365. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11366. s)))
  11367. (defun org-fixup-message-id-for-http (s)
  11368. "Replace special characters in a message id, so it can be used in an http query."
  11369. (while (string-match "<" s)
  11370. (setq s (replace-match "%3C" t t s)))
  11371. (while (string-match ">" s)
  11372. (setq s (replace-match "%3E" t t s)))
  11373. (while (string-match "@" s)
  11374. (setq s (replace-match "%40" t t s)))
  11375. s)
  11376. ;;;###autoload
  11377. (defun org-insert-link-global ()
  11378. "Insert a link like Org-mode does.
  11379. This command can be called in any mode to insert a link in Org-mode syntax."
  11380. (interactive)
  11381. (org-run-like-in-org-mode 'org-insert-link))
  11382. (defun org-insert-link (&optional complete-file)
  11383. "Insert a link. At the prompt, enter the link.
  11384. Completion can be used to select a link previously stored with
  11385. `org-store-link'. When the empty string is entered (i.e. if you just
  11386. press RET at the prompt), the link defaults to the most recently
  11387. stored link. As SPC triggers completion in the minibuffer, you need to
  11388. use M-SPC or C-q SPC to force the insertion of a space character.
  11389. You will also be prompted for a description, and if one is given, it will
  11390. be displayed in the buffer instead of the link.
  11391. If there is already a link at point, this command will allow you to edit link
  11392. and description parts.
  11393. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11394. selected using completion. The path to the file will be relative to
  11395. the current directory if the file is in the current directory or a
  11396. subdirectory. Otherwise, the link will be the absolute path as
  11397. completed in the minibuffer (i.e. normally ~/path/to/file).
  11398. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11399. is in the current directory or below.
  11400. With three \\[universal-argument] prefixes, negate the meaning of
  11401. `org-keep-stored-link-after-insertion'."
  11402. (interactive "P")
  11403. (let* ((wcf (current-window-configuration))
  11404. (region (if (org-region-active-p)
  11405. (buffer-substring (region-beginning) (region-end))))
  11406. (remove (and region (list (region-beginning) (region-end))))
  11407. (desc region)
  11408. tmphist ; byte-compile incorrectly complains about this
  11409. link entry file)
  11410. (cond
  11411. ((org-in-regexp org-bracket-link-regexp 1)
  11412. ;; We do have a link at point, and we are going to edit it.
  11413. (setq remove (list (match-beginning 0) (match-end 0)))
  11414. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11415. (setq link (read-string "Link: "
  11416. (org-link-unescape
  11417. (org-match-string-no-properties 1)))))
  11418. ((or (org-in-regexp org-angle-link-re)
  11419. (org-in-regexp org-plain-link-re))
  11420. ;; Convert to bracket link
  11421. (setq remove (list (match-beginning 0) (match-end 0))
  11422. link (read-string "Link: "
  11423. (org-remove-angle-brackets (match-string 0)))))
  11424. ((equal complete-file '(4))
  11425. ;; Completing read for file names.
  11426. (setq file (read-file-name "File: "))
  11427. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11428. (pwd1 (file-name-as-directory (abbreviate-file-name
  11429. (expand-file-name ".")))))
  11430. (cond
  11431. ((equal complete-file '(16))
  11432. (setq link (org-make-link
  11433. "file:"
  11434. (abbreviate-file-name (expand-file-name file)))))
  11435. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11436. (setq link (org-make-link "file:" (match-string 1 file))))
  11437. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11438. (expand-file-name file))
  11439. (setq link (org-make-link
  11440. "file:" (match-string 1 (expand-file-name file)))))
  11441. (t (setq link (org-make-link "file:" file))))))
  11442. (t
  11443. ;; Read link, with completion for stored links.
  11444. (with-output-to-temp-buffer "*Org Links*"
  11445. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11446. (when org-stored-links
  11447. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11448. (princ (mapconcat
  11449. (lambda (x)
  11450. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11451. (reverse org-stored-links) "\n"))))
  11452. (let ((cw (selected-window)))
  11453. (select-window (get-buffer-window "*Org Links*"))
  11454. (shrink-window-if-larger-than-buffer)
  11455. (setq truncate-lines t)
  11456. (select-window cw))
  11457. ;; Fake a link history, containing the stored links.
  11458. (setq tmphist (append (mapcar 'car org-stored-links)
  11459. org-insert-link-history))
  11460. (unwind-protect
  11461. (setq link (org-completing-read
  11462. "Link: "
  11463. (append
  11464. (mapcar (lambda (x) (list (concat (car x) ":")))
  11465. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11466. (mapcar (lambda (x) (list (concat x ":")))
  11467. org-link-types))
  11468. nil nil nil
  11469. 'tmphist
  11470. (or (car (car org-stored-links)))))
  11471. (set-window-configuration wcf)
  11472. (kill-buffer "*Org Links*"))
  11473. (setq entry (assoc link org-stored-links))
  11474. (or entry (push link org-insert-link-history))
  11475. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11476. (not org-keep-stored-link-after-insertion))
  11477. (setq org-stored-links (delq (assoc link org-stored-links)
  11478. org-stored-links)))
  11479. (setq desc (or desc (nth 1 entry)))))
  11480. (if (string-match org-plain-link-re link)
  11481. ;; URL-like link, normalize the use of angular brackets.
  11482. (setq link (org-make-link (org-remove-angle-brackets link))))
  11483. ;; Check if we are linking to the current file with a search option
  11484. ;; If yes, simplify the link by using only the search option.
  11485. (when (and buffer-file-name
  11486. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11487. (let* ((path (match-string 1 link))
  11488. (case-fold-search nil)
  11489. (search (match-string 2 link)))
  11490. (save-match-data
  11491. (if (equal (file-truename buffer-file-name) (file-truename path))
  11492. ;; We are linking to this same file, with a search option
  11493. (setq link search)))))
  11494. ;; Check if we can/should use a relative path. If yes, simplify the link
  11495. (when (string-match "\\<file:\\(.*\\)" link)
  11496. (let* ((path (match-string 1 link))
  11497. (origpath path)
  11498. (case-fold-search nil))
  11499. (cond
  11500. ((eq org-link-file-path-type 'absolute)
  11501. (setq path (abbreviate-file-name (expand-file-name path))))
  11502. ((eq org-link-file-path-type 'noabbrev)
  11503. (setq path (expand-file-name path)))
  11504. ((eq org-link-file-path-type 'relative)
  11505. (setq path (file-relative-name path)))
  11506. (t
  11507. (save-match-data
  11508. (if (string-match (concat "^" (regexp-quote
  11509. (file-name-as-directory
  11510. (expand-file-name "."))))
  11511. (expand-file-name path))
  11512. ;; We are linking a file with relative path name.
  11513. (setq path (substring (expand-file-name path)
  11514. (match-end 0)))))))
  11515. (setq link (concat "file:" path))
  11516. (if (equal desc origpath)
  11517. (setq desc path))))
  11518. (setq desc (read-string "Description: " desc))
  11519. (unless (string-match "\\S-" desc) (setq desc nil))
  11520. (if remove (apply 'delete-region remove))
  11521. (insert (org-make-link-string link desc))))
  11522. (defun org-completing-read (&rest args)
  11523. (let ((minibuffer-local-completion-map
  11524. (copy-keymap minibuffer-local-completion-map)))
  11525. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11526. (apply 'completing-read args)))
  11527. ;;; Opening/following a link
  11528. (defvar org-link-search-failed nil)
  11529. (defun org-next-link ()
  11530. "Move forward to the next link.
  11531. If the link is in hidden text, expose it."
  11532. (interactive)
  11533. (when (and org-link-search-failed (eq this-command last-command))
  11534. (goto-char (point-min))
  11535. (message "Link search wrapped back to beginning of buffer"))
  11536. (setq org-link-search-failed nil)
  11537. (let* ((pos (point))
  11538. (ct (org-context))
  11539. (a (assoc :link ct)))
  11540. (if a (goto-char (nth 2 a)))
  11541. (if (re-search-forward org-any-link-re nil t)
  11542. (progn
  11543. (goto-char (match-beginning 0))
  11544. (if (org-invisible-p) (org-show-context)))
  11545. (goto-char pos)
  11546. (setq org-link-search-failed t)
  11547. (error "No further link found"))))
  11548. (defun org-previous-link ()
  11549. "Move backward to the previous link.
  11550. If the link is in hidden text, expose it."
  11551. (interactive)
  11552. (when (and org-link-search-failed (eq this-command last-command))
  11553. (goto-char (point-max))
  11554. (message "Link search wrapped back to end of buffer"))
  11555. (setq org-link-search-failed nil)
  11556. (let* ((pos (point))
  11557. (ct (org-context))
  11558. (a (assoc :link ct)))
  11559. (if a (goto-char (nth 1 a)))
  11560. (if (re-search-backward org-any-link-re nil t)
  11561. (progn
  11562. (goto-char (match-beginning 0))
  11563. (if (org-invisible-p) (org-show-context)))
  11564. (goto-char pos)
  11565. (setq org-link-search-failed t)
  11566. (error "No further link found"))))
  11567. (defun org-find-file-at-mouse (ev)
  11568. "Open file link or URL at mouse."
  11569. (interactive "e")
  11570. (mouse-set-point ev)
  11571. (org-open-at-point 'in-emacs))
  11572. (defun org-open-at-mouse (ev)
  11573. "Open file link or URL at mouse."
  11574. (interactive "e")
  11575. (mouse-set-point ev)
  11576. (org-open-at-point))
  11577. (defvar org-window-config-before-follow-link nil
  11578. "The window configuration before following a link.
  11579. This is saved in case the need arises to restore it.")
  11580. (defvar org-open-link-marker (make-marker)
  11581. "Marker pointing to the location where `org-open-at-point; was called.")
  11582. ;;;###autoload
  11583. (defun org-open-at-point-global ()
  11584. "Follow a link like Org-mode does.
  11585. This command can be called in any mode to follow a link that has
  11586. Org-mode syntax."
  11587. (interactive)
  11588. (org-run-like-in-org-mode 'org-open-at-point))
  11589. (defun org-open-at-point (&optional in-emacs)
  11590. "Open link at or after point.
  11591. If there is no link at point, this function will search forward up to
  11592. the end of the current subtree.
  11593. Normally, files will be opened by an appropriate application. If the
  11594. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11595. (interactive "P")
  11596. (move-marker org-open-link-marker (point))
  11597. (setq org-window-config-before-follow-link (current-window-configuration))
  11598. (org-remove-occur-highlights nil nil t)
  11599. (if (org-at-timestamp-p t)
  11600. (org-follow-timestamp-link)
  11601. (let (type path link line search (pos (point)))
  11602. (catch 'match
  11603. (save-excursion
  11604. (skip-chars-forward "^]\n\r")
  11605. (when (org-in-regexp org-bracket-link-regexp)
  11606. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11607. (while (string-match " *\n *" link)
  11608. (setq link (replace-match " " t t link)))
  11609. (setq link (org-link-expand-abbrev link))
  11610. (if (string-match org-link-re-with-space2 link)
  11611. (setq type (match-string 1 link) path (match-string 2 link))
  11612. (setq type "thisfile" path link))
  11613. (throw 'match t)))
  11614. (when (get-text-property (point) 'org-linked-text)
  11615. (setq type "thisfile"
  11616. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11617. (1+ (point)) (point))
  11618. path (buffer-substring
  11619. (previous-single-property-change pos 'org-linked-text)
  11620. (next-single-property-change pos 'org-linked-text)))
  11621. (throw 'match t))
  11622. (save-excursion
  11623. (when (or (org-in-regexp org-angle-link-re)
  11624. (org-in-regexp org-plain-link-re))
  11625. (setq type (match-string 1) path (match-string 2))
  11626. (throw 'match t)))
  11627. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11628. (setq type "tree-match"
  11629. path (match-string 1))
  11630. (throw 'match t))
  11631. (save-excursion
  11632. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11633. (setq type "tags"
  11634. path (match-string 1))
  11635. (while (string-match ":" path)
  11636. (setq path (replace-match "+" t t path)))
  11637. (throw 'match t))))
  11638. (unless path
  11639. (error "No link found"))
  11640. ;; Remove any trailing spaces in path
  11641. (if (string-match " +\\'" path)
  11642. (setq path (replace-match "" t t path)))
  11643. (cond
  11644. ((assoc type org-link-protocols)
  11645. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11646. ((equal type "mailto")
  11647. (let ((cmd (car org-link-mailto-program))
  11648. (args (cdr org-link-mailto-program)) args1
  11649. (address path) (subject "") a)
  11650. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11651. (setq address (match-string 1 path)
  11652. subject (org-link-escape (match-string 2 path))))
  11653. (while args
  11654. (cond
  11655. ((not (stringp (car args))) (push (pop args) args1))
  11656. (t (setq a (pop args))
  11657. (if (string-match "%a" a)
  11658. (setq a (replace-match address t t a)))
  11659. (if (string-match "%s" a)
  11660. (setq a (replace-match subject t t a)))
  11661. (push a args1))))
  11662. (apply cmd (nreverse args1))))
  11663. ((member type '("http" "https" "ftp" "news"))
  11664. (browse-url (concat type ":" (org-link-escape
  11665. path org-link-escape-chars-browser))))
  11666. ((member type '("message"))
  11667. (browse-url (concat type ":" path)))
  11668. ((string= type "tags")
  11669. (org-tags-view in-emacs path))
  11670. ((string= type "thisfile")
  11671. (if in-emacs
  11672. (switch-to-buffer-other-window
  11673. (org-get-buffer-for-internal-link (current-buffer)))
  11674. (org-mark-ring-push))
  11675. (let ((cmd `(org-link-search
  11676. ,path
  11677. ,(cond ((equal in-emacs '(4)) 'occur)
  11678. ((equal in-emacs '(16)) 'org-occur)
  11679. (t nil))
  11680. ,pos)))
  11681. (condition-case nil (eval cmd)
  11682. (error (progn (widen) (eval cmd))))))
  11683. ((string= type "tree-match")
  11684. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11685. ((string= type "file")
  11686. (if (string-match "::\\([0-9]+\\)\\'" path)
  11687. (setq line (string-to-number (match-string 1 path))
  11688. path (substring path 0 (match-beginning 0)))
  11689. (if (string-match "::\\(.+\\)\\'" path)
  11690. (setq search (match-string 1 path)
  11691. path (substring path 0 (match-beginning 0)))))
  11692. (if (string-match "[*?{]" (file-name-nondirectory path))
  11693. (dired path)
  11694. (org-open-file path in-emacs line search)))
  11695. ((string= type "news")
  11696. (org-follow-gnus-link path))
  11697. ((string= type "bbdb")
  11698. (org-follow-bbdb-link path))
  11699. ((string= type "info")
  11700. (org-follow-info-link path))
  11701. ((string= type "gnus")
  11702. (let (group article)
  11703. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11704. (error "Error in Gnus link"))
  11705. (setq group (match-string 1 path)
  11706. article (match-string 3 path))
  11707. (org-follow-gnus-link group article)))
  11708. ((string= type "vm")
  11709. (let (folder article)
  11710. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11711. (error "Error in VM link"))
  11712. (setq folder (match-string 1 path)
  11713. article (match-string 3 path))
  11714. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11715. (org-follow-vm-link folder article in-emacs)))
  11716. ((string= type "wl")
  11717. (let (folder article)
  11718. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11719. (error "Error in Wanderlust link"))
  11720. (setq folder (match-string 1 path)
  11721. article (match-string 3 path))
  11722. (org-follow-wl-link folder article)))
  11723. ((string= type "mhe")
  11724. (let (folder article)
  11725. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11726. (error "Error in MHE link"))
  11727. (setq folder (match-string 1 path)
  11728. article (match-string 3 path))
  11729. (org-follow-mhe-link folder article)))
  11730. ((string= type "rmail")
  11731. (let (folder article)
  11732. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11733. (error "Error in RMAIL link"))
  11734. (setq folder (match-string 1 path)
  11735. article (match-string 3 path))
  11736. (org-follow-rmail-link folder article)))
  11737. ((string= type "shell")
  11738. (let ((cmd path))
  11739. (if (or (not org-confirm-shell-link-function)
  11740. (funcall org-confirm-shell-link-function
  11741. (format "Execute \"%s\" in shell? "
  11742. (org-add-props cmd nil
  11743. 'face 'org-warning))))
  11744. (progn
  11745. (message "Executing %s" cmd)
  11746. (shell-command cmd))
  11747. (error "Abort"))))
  11748. ((string= type "elisp")
  11749. (let ((cmd path))
  11750. (if (or (not org-confirm-elisp-link-function)
  11751. (funcall org-confirm-elisp-link-function
  11752. (format "Execute \"%s\" as elisp? "
  11753. (org-add-props cmd nil
  11754. 'face 'org-warning))))
  11755. (message "%s => %s" cmd (eval (read cmd)))
  11756. (error "Abort"))))
  11757. (t
  11758. (browse-url-at-point)))))
  11759. (move-marker org-open-link-marker nil))
  11760. ;;; File search
  11761. (defvar org-create-file-search-functions nil
  11762. "List of functions to construct the right search string for a file link.
  11763. These functions are called in turn with point at the location to
  11764. which the link should point.
  11765. A function in the hook should first test if it would like to
  11766. handle this file type, for example by checking the major-mode or
  11767. the file extension. If it decides not to handle this file, it
  11768. should just return nil to give other functions a chance. If it
  11769. does handle the file, it must return the search string to be used
  11770. when following the link. The search string will be part of the
  11771. file link, given after a double colon, and `org-open-at-point'
  11772. will automatically search for it. If special measures must be
  11773. taken to make the search successful, another function should be
  11774. added to the companion hook `org-execute-file-search-functions',
  11775. which see.
  11776. A function in this hook may also use `setq' to set the variable
  11777. `description' to provide a suggestion for the descriptive text to
  11778. be used for this link when it gets inserted into an Org-mode
  11779. buffer with \\[org-insert-link].")
  11780. (defvar org-execute-file-search-functions nil
  11781. "List of functions to execute a file search triggered by a link.
  11782. Functions added to this hook must accept a single argument, the
  11783. search string that was part of the file link, the part after the
  11784. double colon. The function must first check if it would like to
  11785. handle this search, for example by checking the major-mode or the
  11786. file extension. If it decides not to handle this search, it
  11787. should just return nil to give other functions a chance. If it
  11788. does handle the search, it must return a non-nil value to keep
  11789. other functions from trying.
  11790. Each function can access the current prefix argument through the
  11791. variable `current-prefix-argument'. Note that a single prefix is
  11792. used to force opening a link in Emacs, so it may be good to only
  11793. use a numeric or double prefix to guide the search function.
  11794. In case this is needed, a function in this hook can also restore
  11795. the window configuration before `org-open-at-point' was called using:
  11796. (set-window-configuration org-window-config-before-follow-link)")
  11797. (defun org-link-search (s &optional type avoid-pos)
  11798. "Search for a link search option.
  11799. If S is surrounded by forward slashes, it is interpreted as a
  11800. regular expression. In org-mode files, this will create an `org-occur'
  11801. sparse tree. In ordinary files, `occur' will be used to list matches.
  11802. If the current buffer is in `dired-mode', grep will be used to search
  11803. in all files. If AVOID-POS is given, ignore matches near that position."
  11804. (let ((case-fold-search t)
  11805. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11806. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11807. (append '(("") (" ") ("\t") ("\n"))
  11808. org-emphasis-alist)
  11809. "\\|") "\\)"))
  11810. (pos (point))
  11811. (pre "") (post "")
  11812. words re0 re1 re2 re3 re4 re5 re2a reall)
  11813. (cond
  11814. ;; First check if there are any special
  11815. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11816. ;; Now try the builtin stuff
  11817. ((save-excursion
  11818. (goto-char (point-min))
  11819. (and
  11820. (re-search-forward
  11821. (concat "<<" (regexp-quote s0) ">>") nil t)
  11822. (setq pos (match-beginning 0))))
  11823. ;; There is an exact target for this
  11824. (goto-char pos))
  11825. ((string-match "^/\\(.*\\)/$" s)
  11826. ;; A regular expression
  11827. (cond
  11828. ((org-mode-p)
  11829. (org-occur (match-string 1 s)))
  11830. ;;((eq major-mode 'dired-mode)
  11831. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11832. (t (org-do-occur (match-string 1 s)))))
  11833. (t
  11834. ;; A normal search strings
  11835. (when (equal (string-to-char s) ?*)
  11836. ;; Anchor on headlines, post may include tags.
  11837. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11838. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11839. s (substring s 1)))
  11840. (remove-text-properties
  11841. 0 (length s)
  11842. '(face nil mouse-face nil keymap nil fontified nil) s)
  11843. ;; Make a series of regular expressions to find a match
  11844. (setq words (org-split-string s "[ \n\r\t]+")
  11845. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11846. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11847. "\\)" markers)
  11848. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11849. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11850. re1 (concat pre re2 post)
  11851. re3 (concat pre re4 post)
  11852. re5 (concat pre ".*" re4)
  11853. re2 (concat pre re2)
  11854. re2a (concat pre re2a)
  11855. re4 (concat pre re4)
  11856. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11857. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11858. re5 "\\)"
  11859. ))
  11860. (cond
  11861. ((eq type 'org-occur) (org-occur reall))
  11862. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11863. (t (goto-char (point-min))
  11864. (if (or (org-search-not-self 1 re0 nil t)
  11865. (org-search-not-self 1 re1 nil t)
  11866. (org-search-not-self 1 re2 nil t)
  11867. (org-search-not-self 1 re2a nil t)
  11868. (org-search-not-self 1 re3 nil t)
  11869. (org-search-not-self 1 re4 nil t)
  11870. (org-search-not-self 1 re5 nil t)
  11871. )
  11872. (goto-char (match-beginning 1))
  11873. (goto-char pos)
  11874. (error "No match")))))
  11875. (t
  11876. ;; Normal string-search
  11877. (goto-char (point-min))
  11878. (if (search-forward s nil t)
  11879. (goto-char (match-beginning 0))
  11880. (error "No match"))))
  11881. (and (org-mode-p) (org-show-context 'link-search))))
  11882. (defun org-search-not-self (group &rest args)
  11883. "Execute `re-search-forward', but only accept matches that do not
  11884. enclose the position of `org-open-link-marker'."
  11885. (let ((m org-open-link-marker))
  11886. (catch 'exit
  11887. (while (apply 're-search-forward args)
  11888. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11889. (goto-char (match-end group))
  11890. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11891. (> (match-beginning 0) (marker-position m))
  11892. (< (match-end 0) (marker-position m)))
  11893. (save-match-data
  11894. (or (not (org-in-regexp
  11895. org-bracket-link-analytic-regexp 1))
  11896. (not (match-end 4)) ; no description
  11897. (and (<= (match-beginning 4) (point))
  11898. (>= (match-end 4) (point))))))
  11899. (throw 'exit (point))))))))
  11900. (defun org-get-buffer-for-internal-link (buffer)
  11901. "Return a buffer to be used for displaying the link target of internal links."
  11902. (cond
  11903. ((not org-display-internal-link-with-indirect-buffer)
  11904. buffer)
  11905. ((string-match "(Clone)$" (buffer-name buffer))
  11906. (message "Buffer is already a clone, not making another one")
  11907. ;; we also do not modify visibility in this case
  11908. buffer)
  11909. (t ; make a new indirect buffer for displaying the link
  11910. (let* ((bn (buffer-name buffer))
  11911. (ibn (concat bn "(Clone)"))
  11912. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11913. (with-current-buffer ib (org-overview))
  11914. ib))))
  11915. (defun org-do-occur (regexp &optional cleanup)
  11916. "Call the Emacs command `occur'.
  11917. If CLEANUP is non-nil, remove the printout of the regular expression
  11918. in the *Occur* buffer. This is useful if the regex is long and not useful
  11919. to read."
  11920. (occur regexp)
  11921. (when cleanup
  11922. (let ((cwin (selected-window)) win beg end)
  11923. (when (setq win (get-buffer-window "*Occur*"))
  11924. (select-window win))
  11925. (goto-char (point-min))
  11926. (when (re-search-forward "match[a-z]+" nil t)
  11927. (setq beg (match-end 0))
  11928. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11929. (setq end (1- (match-beginning 0)))))
  11930. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11931. (goto-char (point-min))
  11932. (select-window cwin))))
  11933. ;;; The mark ring for links jumps
  11934. (defvar org-mark-ring nil
  11935. "Mark ring for positions before jumps in Org-mode.")
  11936. (defvar org-mark-ring-last-goto nil
  11937. "Last position in the mark ring used to go back.")
  11938. ;; Fill and close the ring
  11939. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11940. (loop for i from 1 to org-mark-ring-length do
  11941. (push (make-marker) org-mark-ring))
  11942. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11943. org-mark-ring)
  11944. (defun org-mark-ring-push (&optional pos buffer)
  11945. "Put the current position or POS into the mark ring and rotate it."
  11946. (interactive)
  11947. (setq pos (or pos (point)))
  11948. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11949. (move-marker (car org-mark-ring)
  11950. (or pos (point))
  11951. (or buffer (current-buffer)))
  11952. (message "%s"
  11953. (substitute-command-keys
  11954. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11955. (defun org-mark-ring-goto (&optional n)
  11956. "Jump to the previous position in the mark ring.
  11957. With prefix arg N, jump back that many stored positions. When
  11958. called several times in succession, walk through the entire ring.
  11959. Org-mode commands jumping to a different position in the current file,
  11960. or to another Org-mode file, automatically push the old position
  11961. onto the ring."
  11962. (interactive "p")
  11963. (let (p m)
  11964. (if (eq last-command this-command)
  11965. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11966. (setq p org-mark-ring))
  11967. (setq org-mark-ring-last-goto p)
  11968. (setq m (car p))
  11969. (switch-to-buffer (marker-buffer m))
  11970. (goto-char m)
  11971. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11972. (defun org-remove-angle-brackets (s)
  11973. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11974. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11975. s)
  11976. (defun org-add-angle-brackets (s)
  11977. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11978. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11979. s)
  11980. ;;; Following specific links
  11981. (defun org-follow-timestamp-link ()
  11982. (cond
  11983. ((org-at-date-range-p t)
  11984. (let ((org-agenda-start-on-weekday)
  11985. (t1 (match-string 1))
  11986. (t2 (match-string 2)))
  11987. (setq t1 (time-to-days (org-time-string-to-time t1))
  11988. t2 (time-to-days (org-time-string-to-time t2)))
  11989. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11990. ((org-at-timestamp-p t)
  11991. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11992. (substring (match-string 1) 0 10)))
  11993. 1))
  11994. (t (error "This should not happen"))))
  11995. (defun org-follow-bbdb-link (name)
  11996. "Follow a BBDB link to NAME."
  11997. (require 'bbdb)
  11998. (let ((inhibit-redisplay (not debug-on-error))
  11999. (bbdb-electric-p nil))
  12000. (catch 'exit
  12001. ;; Exact match on name
  12002. (bbdb-name (concat "\\`" name "\\'") nil)
  12003. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12004. ;; Exact match on name
  12005. (bbdb-company (concat "\\`" name "\\'") nil)
  12006. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12007. ;; Partial match on name
  12008. (bbdb-name name nil)
  12009. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12010. ;; Partial match on company
  12011. (bbdb-company name nil)
  12012. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12013. ;; General match including network address and notes
  12014. (bbdb name nil)
  12015. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  12016. (delete-window (get-buffer-window "*BBDB*"))
  12017. (error "No matching BBDB record")))))
  12018. (defun org-follow-info-link (name)
  12019. "Follow an info file & node link to NAME."
  12020. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  12021. (string-match "\\(.*\\)" name))
  12022. (progn
  12023. (require 'info)
  12024. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  12025. (Info-find-node (match-string 1 name) (match-string 2 name))
  12026. (Info-find-node (match-string 1 name) "Top")))
  12027. (message "Could not open: %s" name)))
  12028. (defun org-follow-gnus-link (&optional group article)
  12029. "Follow a Gnus link to GROUP and ARTICLE."
  12030. (require 'gnus)
  12031. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  12032. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  12033. (cond ((and group article)
  12034. (gnus-group-read-group 1 nil group)
  12035. (gnus-summary-goto-article (string-to-number article) nil t))
  12036. (group (gnus-group-jump-to-group group))))
  12037. (defun org-follow-vm-link (&optional folder article readonly)
  12038. "Follow a VM link to FOLDER and ARTICLE."
  12039. (require 'vm)
  12040. (setq article (org-add-angle-brackets article))
  12041. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  12042. ;; ange-ftp or efs or tramp access
  12043. (let ((user (or (match-string 1 folder) (user-login-name)))
  12044. (host (match-string 2 folder))
  12045. (file (match-string 3 folder)))
  12046. (cond
  12047. ((featurep 'tramp)
  12048. ;; use tramp to access the file
  12049. (if (featurep 'xemacs)
  12050. (setq folder (format "[%s@%s]%s" user host file))
  12051. (setq folder (format "/%s@%s:%s" user host file))))
  12052. (t
  12053. ;; use ange-ftp or efs
  12054. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12055. (setq folder (format "/%s@%s:%s" user host file))))))
  12056. (when folder
  12057. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12058. (sit-for 0.1)
  12059. (when article
  12060. (vm-select-folder-buffer)
  12061. (widen)
  12062. (let ((case-fold-search t))
  12063. (goto-char (point-min))
  12064. (if (not (re-search-forward
  12065. (concat "^" "message-id: *" (regexp-quote article))))
  12066. (error "Could not find the specified message in this folder"))
  12067. (vm-isearch-update)
  12068. (vm-isearch-narrow)
  12069. (vm-beginning-of-message)
  12070. (vm-summarize)))))
  12071. (defun org-follow-wl-link (folder article)
  12072. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12073. (if (and (string= folder "%")
  12074. article
  12075. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12076. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12077. ;; Thus, we recompose folder and article ids.
  12078. (setq folder (format "%s#%s" folder (match-string 1 article))
  12079. article (match-string 3 article)))
  12080. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12081. (error "No such folder: %s" folder))
  12082. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12083. (and article
  12084. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12085. (wl-summary-redisplay)))
  12086. (defun org-follow-rmail-link (folder article)
  12087. "Follow an RMAIL link to FOLDER and ARTICLE."
  12088. (setq article (org-add-angle-brackets article))
  12089. (let (message-number)
  12090. (save-excursion
  12091. (save-window-excursion
  12092. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12093. (setq message-number
  12094. (save-restriction
  12095. (widen)
  12096. (goto-char (point-max))
  12097. (if (re-search-backward
  12098. (concat "^Message-ID:\\s-+" (regexp-quote
  12099. (or article "")))
  12100. nil t)
  12101. (rmail-what-message))))))
  12102. (if message-number
  12103. (progn
  12104. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12105. (rmail-show-message message-number)
  12106. message-number)
  12107. (error "Message not found"))))
  12108. ;;; mh-e integration based on planner-mode
  12109. (defun org-mhe-get-message-real-folder ()
  12110. "Return the name of the current message real folder, so if you use
  12111. sequences, it will now work."
  12112. (save-excursion
  12113. (let* ((folder
  12114. (if (equal major-mode 'mh-folder-mode)
  12115. mh-current-folder
  12116. ;; Refer to the show buffer
  12117. mh-show-folder-buffer))
  12118. (end-index
  12119. (if (boundp 'mh-index-folder)
  12120. (min (length mh-index-folder) (length folder))))
  12121. )
  12122. ;; a simple test on mh-index-data does not work, because
  12123. ;; mh-index-data is always nil in a show buffer.
  12124. (if (and (boundp 'mh-index-folder)
  12125. (string= mh-index-folder (substring folder 0 end-index)))
  12126. (if (equal major-mode 'mh-show-mode)
  12127. (save-window-excursion
  12128. (let (pop-up-frames)
  12129. (when (buffer-live-p (get-buffer folder))
  12130. (progn
  12131. (pop-to-buffer folder)
  12132. (org-mhe-get-message-folder-from-index)
  12133. )
  12134. )))
  12135. (org-mhe-get-message-folder-from-index)
  12136. )
  12137. folder
  12138. )
  12139. )))
  12140. (defun org-mhe-get-message-folder-from-index ()
  12141. "Returns the name of the message folder in a index folder buffer."
  12142. (save-excursion
  12143. (mh-index-previous-folder)
  12144. (re-search-forward "^\\(+.*\\)$" nil t)
  12145. (message "%s" (match-string 1))))
  12146. (defun org-mhe-get-message-folder ()
  12147. "Return the name of the current message folder. Be careful if you
  12148. use sequences."
  12149. (save-excursion
  12150. (if (equal major-mode 'mh-folder-mode)
  12151. mh-current-folder
  12152. ;; Refer to the show buffer
  12153. mh-show-folder-buffer)))
  12154. (defun org-mhe-get-message-num ()
  12155. "Return the number of the current message. Be careful if you
  12156. use sequences."
  12157. (save-excursion
  12158. (if (equal major-mode 'mh-folder-mode)
  12159. (mh-get-msg-num nil)
  12160. ;; Refer to the show buffer
  12161. (mh-show-buffer-message-number))))
  12162. (defun org-mhe-get-header (header)
  12163. "Return a header of the message in folder mode. This will create a
  12164. show buffer for the corresponding message. If you have a more clever
  12165. idea..."
  12166. (let* ((folder (org-mhe-get-message-folder))
  12167. (num (org-mhe-get-message-num))
  12168. (buffer (get-buffer-create (concat "show-" folder)))
  12169. (header-field))
  12170. (with-current-buffer buffer
  12171. (mh-display-msg num folder)
  12172. (if (equal major-mode 'mh-folder-mode)
  12173. (mh-header-display)
  12174. (mh-show-header-display))
  12175. (set-buffer buffer)
  12176. (setq header-field (mh-get-header-field header))
  12177. (if (equal major-mode 'mh-folder-mode)
  12178. (mh-show)
  12179. (mh-show-show))
  12180. header-field)))
  12181. (defun org-follow-mhe-link (folder article)
  12182. "Follow an MHE link to FOLDER and ARTICLE.
  12183. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12184. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12185. ARTICLE is searched in all folders. Indexed searches (swish++,
  12186. namazu, and others supported by MH-E) will always search in all
  12187. folders."
  12188. (require 'mh-e)
  12189. (require 'mh-search)
  12190. (require 'mh-utils)
  12191. (mh-find-path)
  12192. (if (not article)
  12193. (mh-visit-folder (mh-normalize-folder-name folder))
  12194. (setq article (org-add-angle-brackets article))
  12195. (mh-search-choose)
  12196. (if (equal mh-searcher 'pick)
  12197. (progn
  12198. (mh-search folder (list "--message-id" article))
  12199. (when (and org-mhe-search-all-folders
  12200. (not (org-mhe-get-message-real-folder)))
  12201. (kill-this-buffer)
  12202. (mh-search "+" (list "--message-id" article))))
  12203. (mh-search "+" article))
  12204. (if (org-mhe-get-message-real-folder)
  12205. (mh-show-msg 1)
  12206. (kill-this-buffer)
  12207. (error "Message not found"))))
  12208. ;;; BibTeX links
  12209. ;; Use the custom search meachnism to construct and use search strings for
  12210. ;; file links to BibTeX database entries.
  12211. (defun org-create-file-search-in-bibtex ()
  12212. "Create the search string and description for a BibTeX database entry."
  12213. (when (eq major-mode 'bibtex-mode)
  12214. ;; yes, we want to construct this search string.
  12215. ;; Make a good description for this entry, using names, year and the title
  12216. ;; Put it into the `description' variable which is dynamically scoped.
  12217. (let ((bibtex-autokey-names 1)
  12218. (bibtex-autokey-names-stretch 1)
  12219. (bibtex-autokey-name-case-convert-function 'identity)
  12220. (bibtex-autokey-name-separator " & ")
  12221. (bibtex-autokey-additional-names " et al.")
  12222. (bibtex-autokey-year-length 4)
  12223. (bibtex-autokey-name-year-separator " ")
  12224. (bibtex-autokey-titlewords 3)
  12225. (bibtex-autokey-titleword-separator " ")
  12226. (bibtex-autokey-titleword-case-convert-function 'identity)
  12227. (bibtex-autokey-titleword-length 'infty)
  12228. (bibtex-autokey-year-title-separator ": "))
  12229. (setq description (bibtex-generate-autokey)))
  12230. ;; Now parse the entry, get the key and return it.
  12231. (save-excursion
  12232. (bibtex-beginning-of-entry)
  12233. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12234. (defun org-execute-file-search-in-bibtex (s)
  12235. "Find the link search string S as a key for a database entry."
  12236. (when (eq major-mode 'bibtex-mode)
  12237. ;; Yes, we want to do the search in this file.
  12238. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12239. (goto-char (point-min))
  12240. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12241. (regexp-quote s) "[ \t\n]*,") nil t)
  12242. (goto-char (match-beginning 0)))
  12243. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12244. ;; Use double prefix to indicate that any web link should be browsed
  12245. (let ((b (current-buffer)) (p (point)))
  12246. ;; Restore the window configuration because we just use the web link
  12247. (set-window-configuration org-window-config-before-follow-link)
  12248. (save-excursion (set-buffer b) (goto-char p)
  12249. (bibtex-url)))
  12250. (recenter 0)) ; Move entry start to beginning of window
  12251. ;; return t to indicate that the search is done.
  12252. t))
  12253. ;; Finally add the functions to the right hooks.
  12254. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12255. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12256. ;; end of Bibtex link setup
  12257. ;;; Following file links
  12258. (defun org-open-file (path &optional in-emacs line search)
  12259. "Open the file at PATH.
  12260. First, this expands any special file name abbreviations. Then the
  12261. configuration variable `org-file-apps' is checked if it contains an
  12262. entry for this file type, and if yes, the corresponding command is launched.
  12263. If no application is found, Emacs simply visits the file.
  12264. With optional argument IN-EMACS, Emacs will visit the file.
  12265. Optional LINE specifies a line to go to, optional SEARCH a string to
  12266. search for. If LINE or SEARCH is given, the file will always be
  12267. opened in Emacs.
  12268. If the file does not exist, an error is thrown."
  12269. (setq in-emacs (or in-emacs line search))
  12270. (let* ((file (if (equal path "")
  12271. buffer-file-name
  12272. (substitute-in-file-name (expand-file-name path))))
  12273. (apps (append org-file-apps (org-default-apps)))
  12274. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12275. (dirp (if remp nil (file-directory-p file)))
  12276. (dfile (downcase file))
  12277. (old-buffer (current-buffer))
  12278. (old-pos (point))
  12279. (old-mode major-mode)
  12280. ext cmd)
  12281. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12282. (setq ext (match-string 1 dfile))
  12283. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12284. (setq ext (match-string 1 dfile))))
  12285. (if in-emacs
  12286. (setq cmd 'emacs)
  12287. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12288. (and dirp (cdr (assoc 'directory apps)))
  12289. (cdr (assoc ext apps))
  12290. (cdr (assoc t apps)))))
  12291. (when (eq cmd 'mailcap)
  12292. (require 'mailcap)
  12293. (mailcap-parse-mailcaps)
  12294. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12295. (command (mailcap-mime-info mime-type)))
  12296. (if (stringp command)
  12297. (setq cmd command)
  12298. (setq cmd 'emacs))))
  12299. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12300. (not (file-exists-p file))
  12301. (not org-open-non-existing-files))
  12302. (error "No such file: %s" file))
  12303. (cond
  12304. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12305. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12306. (while (string-match "['\"]%s['\"]" cmd)
  12307. (setq cmd (replace-match "%s" t t cmd)))
  12308. (while (string-match "%s" cmd)
  12309. (setq cmd (replace-match
  12310. (save-match-data (shell-quote-argument file))
  12311. t t cmd)))
  12312. (save-window-excursion
  12313. (start-process-shell-command cmd nil cmd)))
  12314. ((or (stringp cmd)
  12315. (eq cmd 'emacs))
  12316. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12317. (widen)
  12318. (if line (goto-line line)
  12319. (if search (org-link-search search))))
  12320. ((consp cmd)
  12321. (eval cmd))
  12322. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12323. (and (org-mode-p) (eq old-mode 'org-mode)
  12324. (or (not (equal old-buffer (current-buffer)))
  12325. (not (equal old-pos (point))))
  12326. (org-mark-ring-push old-pos old-buffer))))
  12327. (defun org-default-apps ()
  12328. "Return the default applications for this operating system."
  12329. (cond
  12330. ((eq system-type 'darwin)
  12331. org-file-apps-defaults-macosx)
  12332. ((eq system-type 'windows-nt)
  12333. org-file-apps-defaults-windowsnt)
  12334. (t org-file-apps-defaults-gnu)))
  12335. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12336. (defun org-file-remote-p (file)
  12337. "Test whether FILE specifies a location on a remote system.
  12338. Return non-nil if the location is indeed remote.
  12339. For example, the filename \"/user@host:/foo\" specifies a location
  12340. on the system \"/user@host:\"."
  12341. (cond ((fboundp 'file-remote-p)
  12342. (file-remote-p file))
  12343. ((fboundp 'tramp-handle-file-remote-p)
  12344. (tramp-handle-file-remote-p file))
  12345. ((and (boundp 'ange-ftp-name-format)
  12346. (string-match (car ange-ftp-name-format) file))
  12347. t)
  12348. (t nil)))
  12349. ;;;; Hooks for remember.el, and refiling
  12350. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12351. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12352. ;;;###autoload
  12353. (defun org-remember-insinuate ()
  12354. "Setup remember.el for use wiht Org-mode."
  12355. (require 'remember)
  12356. (setq remember-annotation-functions '(org-remember-annotation))
  12357. (setq remember-handler-functions '(org-remember-handler))
  12358. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12359. ;;;###autoload
  12360. (defun org-remember-annotation ()
  12361. "Return a link to the current location as an annotation for remember.el.
  12362. If you are using Org-mode files as target for data storage with
  12363. remember.el, then the annotations should include a link compatible with the
  12364. conventions in Org-mode. This function returns such a link."
  12365. (org-store-link nil))
  12366. (defconst org-remember-help
  12367. "Select a destination location for the note.
  12368. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12369. RET on headline -> Store as sublevel entry to current headline
  12370. RET at beg-of-buf -> Append to file as level 2 headline
  12371. <left>/<right> -> before/after current headline, same headings level")
  12372. (defvar org-remember-previous-location nil)
  12373. (defvar org-force-remember-template-char) ;; dynamically scoped
  12374. (defun org-select-remember-template (&optional use-char)
  12375. (when org-remember-templates
  12376. (let* ((templates (mapcar (lambda (x)
  12377. (if (stringp (car x))
  12378. (append (list (nth 1 x) (car x)) (cddr x))
  12379. (append (list (car x) "") (cdr x))))
  12380. org-remember-templates))
  12381. (char (or use-char
  12382. (cond
  12383. ((= (length templates) 1)
  12384. (caar templates))
  12385. ((and (boundp 'org-force-remember-template-char)
  12386. org-force-remember-template-char)
  12387. (if (stringp org-force-remember-template-char)
  12388. (string-to-char org-force-remember-template-char)
  12389. org-force-remember-template-char))
  12390. (t
  12391. (message "Select template: %s"
  12392. (mapconcat
  12393. (lambda (x)
  12394. (cond
  12395. ((not (string-match "\\S-" (nth 1 x)))
  12396. (format "[%c]" (car x)))
  12397. ((equal (downcase (car x))
  12398. (downcase (aref (nth 1 x) 0)))
  12399. (format "[%c]%s" (car x)
  12400. (substring (nth 1 x) 1)))
  12401. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12402. templates " "))
  12403. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12404. (when (equal char0 ?\C-g)
  12405. (jump-to-register remember-register)
  12406. (kill-buffer remember-buffer))
  12407. char0))))))
  12408. (cddr (assoc char templates)))))
  12409. (defvar x-last-selected-text)
  12410. (defvar x-last-selected-text-primary)
  12411. ;;;###autoload
  12412. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12413. "Initialize *remember* buffer with template, invoke `org-mode'.
  12414. This function should be placed into `remember-mode-hook' and in fact requires
  12415. to be run from that hook to function properly."
  12416. (if org-remember-templates
  12417. (let* ((entry (org-select-remember-template use-char))
  12418. (tpl (car entry))
  12419. (plist-p (if org-store-link-plist t nil))
  12420. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12421. (string-match "\\S-" (nth 1 entry)))
  12422. (nth 1 entry)
  12423. org-default-notes-file))
  12424. (headline (nth 2 entry))
  12425. (v-c (or (and (eq window-system 'x)
  12426. (fboundp 'x-cut-buffer-or-selection-value)
  12427. (x-cut-buffer-or-selection-value))
  12428. (org-bound-and-true-p x-last-selected-text)
  12429. (org-bound-and-true-p x-last-selected-text-primary)
  12430. (and (> (length kill-ring) 0) (current-kill 0))))
  12431. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12432. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12433. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12434. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12435. ;; `initial' and `annotation' are bound in `remember'
  12436. (v-i (if (boundp 'initial) initial))
  12437. (v-a (if (and (boundp 'annotation) annotation)
  12438. (if (equal annotation "[[]]") "" annotation)
  12439. ""))
  12440. (v-A (if (and v-a
  12441. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12442. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12443. v-a))
  12444. (v-n user-full-name)
  12445. (org-startup-folded nil)
  12446. org-time-was-given org-end-time-was-given x
  12447. prompt completions char time pos default histvar)
  12448. (setq org-store-link-plist
  12449. (append (list :annotation v-a :initial v-i)
  12450. org-store-link-plist))
  12451. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12452. (erase-buffer)
  12453. (insert (substitute-command-keys
  12454. (format
  12455. "## Filing location: Select interactively, default, or last used:
  12456. ## %s to select file and header location interactively.
  12457. ## %s \"%s\" -> \"* %s\"
  12458. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12459. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12460. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12461. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12462. (abbreviate-file-name (or file org-default-notes-file))
  12463. (or headline "")
  12464. (or (car org-remember-previous-location) "???")
  12465. (or (cdr org-remember-previous-location) "???"))))
  12466. (insert tpl) (goto-char (point-min))
  12467. ;; Simple %-escapes
  12468. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12469. (when (and initial (equal (match-string 0) "%i"))
  12470. (save-match-data
  12471. (let* ((lead (buffer-substring
  12472. (point-at-bol) (match-beginning 0))))
  12473. (setq v-i (mapconcat 'identity
  12474. (org-split-string initial "\n")
  12475. (concat "\n" lead))))))
  12476. (replace-match
  12477. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12478. t t))
  12479. ;; %[] Insert contents of a file.
  12480. (goto-char (point-min))
  12481. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12482. (let ((start (match-beginning 0))
  12483. (end (match-end 0))
  12484. (filename (expand-file-name (match-string 1))))
  12485. (goto-char start)
  12486. (delete-region start end)
  12487. (condition-case error
  12488. (insert-file-contents filename)
  12489. (error (insert (format "%%![Couldn't insert %s: %s]"
  12490. filename error))))))
  12491. ;; %() embedded elisp
  12492. (goto-char (point-min))
  12493. (while (re-search-forward "%\\((.+)\\)" nil t)
  12494. (goto-char (match-beginning 0))
  12495. (let ((template-start (point)))
  12496. (forward-char 1)
  12497. (let ((result
  12498. (condition-case error
  12499. (eval (read (current-buffer)))
  12500. (error (format "%%![Error: %s]" error)))))
  12501. (delete-region template-start (point))
  12502. (insert result))))
  12503. ;; From the property list
  12504. (when plist-p
  12505. (goto-char (point-min))
  12506. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12507. (and (setq x (or (plist-get org-store-link-plist
  12508. (intern (match-string 1))) ""))
  12509. (replace-match x t t))))
  12510. ;; Turn on org-mode in the remember buffer, set local variables
  12511. (org-mode)
  12512. (org-set-local 'org-finish-function 'org-remember-finalize)
  12513. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12514. (org-set-local 'org-default-notes-file file))
  12515. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12516. (org-set-local 'org-remember-default-headline headline))
  12517. ;; Interactive template entries
  12518. (goto-char (point-min))
  12519. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12520. (setq char (if (match-end 3) (match-string 3))
  12521. prompt (if (match-end 2) (match-string 2)))
  12522. (goto-char (match-beginning 0))
  12523. (replace-match "")
  12524. (setq completions nil default nil)
  12525. (when prompt
  12526. (setq completions (org-split-string prompt "|")
  12527. prompt (pop completions)
  12528. default (car completions)
  12529. histvar (intern (concat
  12530. "org-remember-template-prompt-history::"
  12531. (or prompt "")))
  12532. completions (mapcar 'list completions)))
  12533. (cond
  12534. ((member char '("G" "g"))
  12535. (let* ((org-last-tags-completion-table
  12536. (org-global-tags-completion-table
  12537. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12538. (org-add-colon-after-tag-completion t)
  12539. (ins (completing-read
  12540. (if prompt (concat prompt ": ") "Tags: ")
  12541. 'org-tags-completion-function nil nil nil
  12542. 'org-tags-history)))
  12543. (setq ins (mapconcat 'identity
  12544. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12545. ":"))
  12546. (when (string-match "\\S-" ins)
  12547. (or (equal (char-before) ?:) (insert ":"))
  12548. (insert ins)
  12549. (or (equal (char-after) ?:) (insert ":")))))
  12550. (char
  12551. (setq org-time-was-given (equal (upcase char) char))
  12552. (setq time (org-read-date (equal (upcase char) "U") t nil
  12553. prompt))
  12554. (org-insert-time-stamp time org-time-was-given
  12555. (member char '("u" "U"))
  12556. nil nil (list org-end-time-was-given)))
  12557. (t
  12558. (insert (org-completing-read
  12559. (concat (if prompt prompt "Enter string")
  12560. (if default (concat " [" default "]"))
  12561. ": ")
  12562. completions nil nil nil histvar default)))))
  12563. (goto-char (point-min))
  12564. (if (re-search-forward "%\\?" nil t)
  12565. (replace-match "")
  12566. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12567. (org-mode)
  12568. (org-set-local 'org-finish-function 'org-remember-finalize))
  12569. (when (save-excursion
  12570. (goto-char (point-min))
  12571. (re-search-forward "%!" nil t))
  12572. (replace-match "")
  12573. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12574. (defun org-remember-finish-immediately ()
  12575. "File remember note immediately.
  12576. This should be run in `post-command-hook' and will remove itself
  12577. from that hook."
  12578. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12579. (when org-finish-function
  12580. (funcall org-finish-function)))
  12581. (defvar org-clock-marker) ; Defined below
  12582. (defun org-remember-finalize ()
  12583. "Finalize the remember process."
  12584. (unless (fboundp 'remember-finalize)
  12585. (defalias 'remember-finalize 'remember-buffer))
  12586. (when (and org-clock-marker
  12587. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12588. ;; FIXME: test this, this is w/o notetaking!
  12589. (let (org-log-note-clock-out) (org-clock-out)))
  12590. (when buffer-file-name
  12591. (save-buffer)
  12592. (setq buffer-file-name nil))
  12593. (remember-finalize))
  12594. ;;;###autoload
  12595. (defun org-remember (&optional goto org-force-remember-template-char)
  12596. "Call `remember'. If this is already a remember buffer, re-apply template.
  12597. If there is an active region, make sure remember uses it as initial content
  12598. of the remember buffer.
  12599. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12600. anything, just go to the file/headline where the selected template usually
  12601. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12602. note stored by remember.
  12603. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12604. associated with a template in `org-remember-templates'."
  12605. (interactive "P")
  12606. (cond
  12607. ((equal goto '(4)) (org-go-to-remember-target))
  12608. ((equal goto '(16)) (org-remember-goto-last-stored))
  12609. (t
  12610. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12611. (progn
  12612. (when (< (length org-remember-templates) 2)
  12613. (error "No other template available"))
  12614. (erase-buffer)
  12615. (let ((annotation (plist-get org-store-link-plist :annotation))
  12616. (initial (plist-get org-store-link-plist :initial)))
  12617. (org-remember-apply-template))
  12618. (message "Press C-c C-c to remember data"))
  12619. (if (org-region-active-p)
  12620. (remember (buffer-substring (point) (mark)))
  12621. (call-interactively 'remember))))))
  12622. (defun org-remember-goto-last-stored ()
  12623. "Go to the location where the last remember note was stored."
  12624. (interactive)
  12625. (bookmark-jump "org-remember-last-stored")
  12626. (message "This is the last note stored by remember"))
  12627. (defun org-go-to-remember-target (&optional template-key)
  12628. "Go to the target location of a remember template.
  12629. The user is queried for the template."
  12630. (interactive)
  12631. (let* ((entry (org-select-remember-template template-key))
  12632. (file (nth 1 entry))
  12633. (heading (nth 2 entry))
  12634. visiting)
  12635. (unless (and file (stringp file) (string-match "\\S-" file))
  12636. (setq file org-default-notes-file))
  12637. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12638. (setq heading org-remember-default-headline))
  12639. (setq visiting (org-find-base-buffer-visiting file))
  12640. (if (not visiting) (find-file-noselect file))
  12641. (switch-to-buffer (or visiting (get-file-buffer file)))
  12642. (widen)
  12643. (goto-char (point-min))
  12644. (if (re-search-forward
  12645. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12646. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12647. nil t)
  12648. (goto-char (match-beginning 0))
  12649. (error "Target headline not found: %s" heading))))
  12650. (defvar org-note-abort nil) ; dynamically scoped
  12651. ;;;###autoload
  12652. (defun org-remember-handler ()
  12653. "Store stuff from remember.el into an org file.
  12654. First prompts for an org file. If the user just presses return, the value
  12655. of `org-default-notes-file' is used.
  12656. Then the command offers the headings tree of the selected file in order to
  12657. file the text at a specific location.
  12658. You can either immediately press RET to get the note appended to the
  12659. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12660. find a better place. Then press RET or <left> or <right> in insert the note.
  12661. Key Cursor position Note gets inserted
  12662. -----------------------------------------------------------------------------
  12663. RET buffer-start as level 1 heading at end of file
  12664. RET on headline as sublevel of the heading at cursor
  12665. RET no heading at cursor position, level taken from context.
  12666. Or use prefix arg to specify level manually.
  12667. <left> on headline as same level, before current heading
  12668. <right> on headline as same level, after current heading
  12669. So the fastest way to store the note is to press RET RET to append it to
  12670. the default file. This way your current train of thought is not
  12671. interrupted, in accordance with the principles of remember.el.
  12672. You can also get the fast execution without prompting by using
  12673. C-u C-c C-c to exit the remember buffer. See also the variable
  12674. `org-remember-store-without-prompt'.
  12675. Before being stored away, the function ensures that the text has a
  12676. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12677. is constructed from the current date and some additional data.
  12678. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12679. also indented so that it starts in the same column as the headline
  12680. \(i.e. after the stars).
  12681. See also the variable `org-reverse-note-order'."
  12682. (goto-char (point-min))
  12683. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12684. (replace-match ""))
  12685. (goto-char (point-max))
  12686. (beginning-of-line 1)
  12687. (while (looking-at "[ \t]*$\\|##.*")
  12688. (delete-region (1- (point)) (point-max))
  12689. (beginning-of-line 1))
  12690. (catch 'quit
  12691. (if org-note-abort (throw 'quit nil))
  12692. (let* ((txt (buffer-substring (point-min) (point-max)))
  12693. (fastp (org-xor (equal current-prefix-arg '(4))
  12694. org-remember-store-without-prompt))
  12695. (file (cond
  12696. (fastp org-default-notes-file)
  12697. ((and (eq org-remember-interactive-interface 'refile)
  12698. org-refile-targets)
  12699. org-default-notes-file)
  12700. ((not (and (equal current-prefix-arg '(16))
  12701. org-remember-previous-location))
  12702. (org-get-org-file))))
  12703. (heading org-remember-default-headline)
  12704. (visiting (and file (org-find-base-buffer-visiting file)))
  12705. (org-startup-folded nil)
  12706. (org-startup-align-all-tables nil)
  12707. (org-goto-start-pos 1)
  12708. spos exitcmd level indent reversed)
  12709. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12710. (setq file (car org-remember-previous-location)
  12711. heading (cdr org-remember-previous-location)
  12712. fastp t))
  12713. (setq current-prefix-arg nil)
  12714. (if (string-match "[ \t\n]+\\'" txt)
  12715. (setq txt (replace-match "" t t txt)))
  12716. ;; Modify text so that it becomes a nice subtree which can be inserted
  12717. ;; into an org tree.
  12718. (let* ((lines (split-string txt "\n"))
  12719. first)
  12720. (setq first (car lines) lines (cdr lines))
  12721. (if (string-match "^\\*+ " first)
  12722. ;; Is already a headline
  12723. (setq indent nil)
  12724. ;; We need to add a headline: Use time and first buffer line
  12725. (setq lines (cons first lines)
  12726. first (concat "* " (current-time-string)
  12727. " (" (remember-buffer-desc) ")")
  12728. indent " "))
  12729. (if (and org-adapt-indentation indent)
  12730. (setq lines (mapcar
  12731. (lambda (x)
  12732. (if (string-match "\\S-" x)
  12733. (concat indent x) x))
  12734. lines)))
  12735. (setq txt (concat first "\n"
  12736. (mapconcat 'identity lines "\n"))))
  12737. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12738. (setq txt (replace-match "\n\n" t t txt))
  12739. (if (string-match "[ \t\n]*\\'" txt)
  12740. (setq txt (replace-match "\n" t t txt))))
  12741. ;; Put the modified text back into the remember buffer, for refile.
  12742. (erase-buffer)
  12743. (insert txt)
  12744. (goto-char (point-min))
  12745. (when (and (eq org-remember-interactive-interface 'refile)
  12746. (not fastp))
  12747. (org-refile nil (or visiting (find-file-noselect file)))
  12748. (throw 'quit t))
  12749. ;; Find the file
  12750. (if (not visiting) (find-file-noselect file))
  12751. (with-current-buffer (or visiting (get-file-buffer file))
  12752. (unless (org-mode-p)
  12753. (error "Target files for remember notes must be in Org-mode"))
  12754. (save-excursion
  12755. (save-restriction
  12756. (widen)
  12757. (and (goto-char (point-min))
  12758. (not (re-search-forward "^\\* " nil t))
  12759. (insert "\n* " (or heading "Notes") "\n"))
  12760. (setq reversed (org-notes-order-reversed-p))
  12761. ;; Find the default location
  12762. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12763. (goto-char (point-min))
  12764. (if (re-search-forward
  12765. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12766. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12767. nil t)
  12768. (setq org-goto-start-pos (match-beginning 0))
  12769. (when fastp
  12770. (goto-char (point-max))
  12771. (unless (bolp) (newline))
  12772. (insert "* " heading "\n")
  12773. (setq org-goto-start-pos (point-at-bol 0)))))
  12774. ;; Ask the User for a location, using the appropriate interface
  12775. (cond
  12776. (fastp (setq spos org-goto-start-pos
  12777. exitcmd 'return))
  12778. ((eq org-remember-interactive-interface 'outline)
  12779. (setq spos (org-get-location (current-buffer)
  12780. org-remember-help)
  12781. exitcmd (cdr spos)
  12782. spos (car spos)))
  12783. ((eq org-remember-interactive-interface 'outline-path-completion)
  12784. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12785. (org-refile-use-outline-path t))
  12786. (setq spos (org-refile-get-location "Heading: ")
  12787. exitcmd 'return
  12788. spos (nth 3 spos))))
  12789. (t (error "this should not hapen")))
  12790. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12791. ; not handle this note
  12792. (goto-char spos)
  12793. (cond ((org-on-heading-p t)
  12794. (org-back-to-heading t)
  12795. (setq level (funcall outline-level))
  12796. (cond
  12797. ((eq exitcmd 'return)
  12798. ;; sublevel of current
  12799. (setq org-remember-previous-location
  12800. (cons (abbreviate-file-name file)
  12801. (org-get-heading 'notags)))
  12802. (if reversed
  12803. (outline-next-heading)
  12804. (org-end-of-subtree t)
  12805. (if (not (bolp))
  12806. (if (looking-at "[ \t]*\n")
  12807. (beginning-of-line 2)
  12808. (end-of-line 1)
  12809. (insert "\n"))))
  12810. (bookmark-set "org-remember-last-stored")
  12811. (org-paste-subtree (org-get-legal-level level 1) txt))
  12812. ((eq exitcmd 'left)
  12813. ;; before current
  12814. (bookmark-set "org-remember-last-stored")
  12815. (org-paste-subtree level txt))
  12816. ((eq exitcmd 'right)
  12817. ;; after current
  12818. (org-end-of-subtree t)
  12819. (bookmark-set "org-remember-last-stored")
  12820. (org-paste-subtree level txt))
  12821. (t (error "This should not happen"))))
  12822. ((and (bobp) (not reversed))
  12823. ;; Put it at the end, one level below level 1
  12824. (save-restriction
  12825. (widen)
  12826. (goto-char (point-max))
  12827. (if (not (bolp)) (newline))
  12828. (bookmark-set "org-remember-last-stored")
  12829. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12830. ((and (bobp) reversed)
  12831. ;; Put it at the start, as level 1
  12832. (save-restriction
  12833. (widen)
  12834. (goto-char (point-min))
  12835. (re-search-forward "^\\*+ " nil t)
  12836. (beginning-of-line 1)
  12837. (bookmark-set "org-remember-last-stored")
  12838. (org-paste-subtree 1 txt)))
  12839. (t
  12840. ;; Put it right there, with automatic level determined by
  12841. ;; org-paste-subtree or from prefix arg
  12842. (bookmark-set "org-remember-last-stored")
  12843. (org-paste-subtree
  12844. (if (numberp current-prefix-arg) current-prefix-arg)
  12845. txt)))
  12846. (when remember-save-after-remembering
  12847. (save-buffer)
  12848. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12849. t) ;; return t to indicate that we took care of this note.
  12850. (defun org-get-org-file ()
  12851. "Read a filename, with default directory `org-directory'."
  12852. (let ((default (or org-default-notes-file remember-data-file)))
  12853. (read-file-name (format "File name [%s]: " default)
  12854. (file-name-as-directory org-directory)
  12855. default)))
  12856. (defun org-notes-order-reversed-p ()
  12857. "Check if the current file should receive notes in reversed order."
  12858. (cond
  12859. ((not org-reverse-note-order) nil)
  12860. ((eq t org-reverse-note-order) t)
  12861. ((not (listp org-reverse-note-order)) nil)
  12862. (t (catch 'exit
  12863. (let ((all org-reverse-note-order)
  12864. entry)
  12865. (while (setq entry (pop all))
  12866. (if (string-match (car entry) buffer-file-name)
  12867. (throw 'exit (cdr entry))))
  12868. nil)))))
  12869. ;;; Refiling
  12870. (defvar org-refile-target-table nil
  12871. "The list of refile targets, created by `org-refile'.")
  12872. (defvar org-agenda-new-buffers nil
  12873. "Buffers created to visit agenda files.")
  12874. (defun org-get-refile-targets (&optional default-buffer)
  12875. "Produce a table with refile targets."
  12876. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12877. targets txt re files f desc descre)
  12878. (with-current-buffer (or default-buffer (current-buffer))
  12879. (while (setq entry (pop entries))
  12880. (setq files (car entry) desc (cdr entry))
  12881. (cond
  12882. ((null files) (setq files (list (current-buffer))))
  12883. ((eq files 'org-agenda-files)
  12884. (setq files (org-agenda-files 'unrestricted)))
  12885. ((and (symbolp files) (fboundp files))
  12886. (setq files (funcall files)))
  12887. ((and (symbolp files) (boundp files))
  12888. (setq files (symbol-value files))))
  12889. (if (stringp files) (setq files (list files)))
  12890. (cond
  12891. ((eq (car desc) :tag)
  12892. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12893. ((eq (car desc) :todo)
  12894. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12895. ((eq (car desc) :regexp)
  12896. (setq descre (cdr desc)))
  12897. ((eq (car desc) :level)
  12898. (setq descre (concat "^\\*\\{" (number-to-string
  12899. (if org-odd-levels-only
  12900. (1- (* 2 (cdr desc)))
  12901. (cdr desc)))
  12902. "\\}[ \t]")))
  12903. ((eq (car desc) :maxlevel)
  12904. (setq descre (concat "^\\*\\{1," (number-to-string
  12905. (if org-odd-levels-only
  12906. (1- (* 2 (cdr desc)))
  12907. (cdr desc)))
  12908. "\\}[ \t]")))
  12909. (t (error "Bad refiling target description %s" desc)))
  12910. (while (setq f (pop files))
  12911. (save-excursion
  12912. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12913. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12914. (save-excursion
  12915. (save-restriction
  12916. (widen)
  12917. (goto-char (point-min))
  12918. (while (re-search-forward descre nil t)
  12919. (goto-char (point-at-bol))
  12920. (when (looking-at org-complex-heading-regexp)
  12921. (setq txt (match-string 4)
  12922. re (concat "^" (regexp-quote
  12923. (buffer-substring (match-beginning 1)
  12924. (match-end 4)))))
  12925. (if (match-end 5) (setq re (concat re "[ \t]+"
  12926. (regexp-quote
  12927. (match-string 5)))))
  12928. (setq re (concat re "[ \t]*$"))
  12929. (when org-refile-use-outline-path
  12930. (setq txt (mapconcat 'identity
  12931. (append
  12932. (if (eq org-refile-use-outline-path 'file)
  12933. (list (file-name-nondirectory
  12934. (buffer-file-name (buffer-base-buffer))))
  12935. (if (eq org-refile-use-outline-path 'full-file-path)
  12936. (list (buffer-file-name (buffer-base-buffer)))))
  12937. (org-get-outline-path)
  12938. (list txt))
  12939. "/")))
  12940. (push (list txt f re (point)) targets))
  12941. (goto-char (point-at-eol))))))))
  12942. (nreverse targets))))
  12943. (defun org-get-outline-path ()
  12944. "Return the outline path to the current entry, as a list."
  12945. (let (rtn)
  12946. (save-excursion
  12947. (while (org-up-heading-safe)
  12948. (when (looking-at org-complex-heading-regexp)
  12949. (push (org-match-string-no-properties 4) rtn)))
  12950. rtn)))
  12951. (defvar org-refile-history nil
  12952. "History for refiling operations.")
  12953. (defun org-refile (&optional goto default-buffer)
  12954. "Move the entry at point to another heading.
  12955. The list of target headings is compiled using the information in
  12956. `org-refile-targets', which see. This list is created upon first use, and
  12957. you can update it by calling this command with a double prefix (`C-u C-u').
  12958. FIXME: Can we find a better way of updating?
  12959. At the target location, the entry is filed as a subitem of the target heading.
  12960. Depending on `org-reverse-note-order', the new subitem will either be the
  12961. first of the last subitem.
  12962. With prefix arg GOTO, the command will only visit the target location,
  12963. not actually move anything.
  12964. With a double prefix `C-c C-c', go to the location where the last refiling
  12965. operation has put the subtree.
  12966. With a double prefix argument, the command can be used to jump to any
  12967. heading in the current buffer."
  12968. (interactive "P")
  12969. (let* ((cbuf (current-buffer))
  12970. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12971. pos it nbuf file re level reversed)
  12972. (if (equal goto '(16))
  12973. (org-refile-goto-last-stored)
  12974. (when (setq it (org-refile-get-location
  12975. (if goto "Goto: " "Refile to: ") default-buffer))
  12976. (setq file (nth 1 it)
  12977. re (nth 2 it)
  12978. pos (nth 3 it))
  12979. (setq nbuf (or (find-buffer-visiting file)
  12980. (find-file-noselect file)))
  12981. (if goto
  12982. (progn
  12983. (switch-to-buffer nbuf)
  12984. (goto-char pos)
  12985. (org-show-context 'org-goto))
  12986. (org-copy-special)
  12987. (save-excursion
  12988. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12989. (find-file-noselect file))))
  12990. (setq reversed (org-notes-order-reversed-p))
  12991. (save-excursion
  12992. (save-restriction
  12993. (widen)
  12994. (goto-char pos)
  12995. (looking-at outline-regexp)
  12996. (setq level (org-get-legal-level (funcall outline-level) 1))
  12997. (goto-char
  12998. (if reversed
  12999. (outline-next-heading)
  13000. (or (save-excursion (outline-get-next-sibling))
  13001. (org-end-of-subtree t t)
  13002. (point-max))))
  13003. (bookmark-set "org-refile-last-stored")
  13004. (org-paste-subtree level))))
  13005. (org-cut-special)
  13006. (message "Entry refiled to \"%s\"" (car it)))))))
  13007. (defun org-refile-goto-last-stored ()
  13008. "Go to the location where the last refile was stored."
  13009. (interactive)
  13010. (bookmark-jump "org-refile-last-stored")
  13011. (message "This is the location of the last refile"))
  13012. (defun org-refile-get-location (&optional prompt default-buffer)
  13013. "Prompt the user for a refile location, using PROMPT."
  13014. (let ((org-refile-targets org-refile-targets)
  13015. (org-refile-use-outline-path org-refile-use-outline-path))
  13016. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  13017. (unless org-refile-target-table
  13018. (error "No refile targets"))
  13019. (let* ((cbuf (current-buffer))
  13020. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  13021. (fname (and filename (file-truename filename)))
  13022. (tbl (mapcar
  13023. (lambda (x)
  13024. (if (not (equal fname (file-truename (nth 1 x))))
  13025. (cons (concat (car x) " (" (file-name-nondirectory
  13026. (nth 1 x)) ")")
  13027. (cdr x))
  13028. x))
  13029. org-refile-target-table))
  13030. (completion-ignore-case t))
  13031. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  13032. tbl)))
  13033. ;;;; Dynamic blocks
  13034. (defun org-find-dblock (name)
  13035. "Find the first dynamic block with name NAME in the buffer.
  13036. If not found, stay at current position and return nil."
  13037. (let (pos)
  13038. (save-excursion
  13039. (goto-char (point-min))
  13040. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  13041. nil t)
  13042. (match-beginning 0))))
  13043. (if pos (goto-char pos))
  13044. pos))
  13045. (defconst org-dblock-start-re
  13046. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13047. "Matches the startline of a dynamic block, with parameters.")
  13048. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13049. "Matches the end of a dyhamic block.")
  13050. (defun org-create-dblock (plist)
  13051. "Create a dynamic block section, with parameters taken from PLIST.
  13052. PLIST must containe a :name entry which is used as name of the block."
  13053. (unless (bolp) (newline))
  13054. (let ((name (plist-get plist :name)))
  13055. (insert "#+BEGIN: " name)
  13056. (while plist
  13057. (if (eq (car plist) :name)
  13058. (setq plist (cddr plist))
  13059. (insert " " (prin1-to-string (pop plist)))))
  13060. (insert "\n\n#+END:\n")
  13061. (beginning-of-line -2)))
  13062. (defun org-prepare-dblock ()
  13063. "Prepare dynamic block for refresh.
  13064. This empties the block, puts the cursor at the insert position and returns
  13065. the property list including an extra property :name with the block name."
  13066. (unless (looking-at org-dblock-start-re)
  13067. (error "Not at a dynamic block"))
  13068. (let* ((begdel (1+ (match-end 0)))
  13069. (name (org-no-properties (match-string 1)))
  13070. (params (append (list :name name)
  13071. (read (concat "(" (match-string 3) ")")))))
  13072. (unless (re-search-forward org-dblock-end-re nil t)
  13073. (error "Dynamic block not terminated"))
  13074. (delete-region begdel (match-beginning 0))
  13075. (goto-char begdel)
  13076. (open-line 1)
  13077. params))
  13078. (defun org-map-dblocks (&optional command)
  13079. "Apply COMMAND to all dynamic blocks in the current buffer.
  13080. If COMMAND is not given, use `org-update-dblock'."
  13081. (let ((cmd (or command 'org-update-dblock))
  13082. pos)
  13083. (save-excursion
  13084. (goto-char (point-min))
  13085. (while (re-search-forward org-dblock-start-re nil t)
  13086. (goto-char (setq pos (match-beginning 0)))
  13087. (condition-case nil
  13088. (funcall cmd)
  13089. (error (message "Error during update of dynamic block")))
  13090. (goto-char pos)
  13091. (unless (re-search-forward org-dblock-end-re nil t)
  13092. (error "Dynamic block not terminated"))))))
  13093. (defun org-dblock-update (&optional arg)
  13094. "User command for updating dynamic blocks.
  13095. Update the dynamic block at point. With prefix ARG, update all dynamic
  13096. blocks in the buffer."
  13097. (interactive "P")
  13098. (if arg
  13099. (org-update-all-dblocks)
  13100. (or (looking-at org-dblock-start-re)
  13101. (org-beginning-of-dblock))
  13102. (org-update-dblock)))
  13103. (defun org-update-dblock ()
  13104. "Update the dynamic block at point
  13105. This means to empty the block, parse for parameters and then call
  13106. the correct writing function."
  13107. (save-window-excursion
  13108. (let* ((pos (point))
  13109. (line (org-current-line))
  13110. (params (org-prepare-dblock))
  13111. (name (plist-get params :name))
  13112. (cmd (intern (concat "org-dblock-write:" name))))
  13113. (message "Updating dynamic block `%s' at line %d..." name line)
  13114. (funcall cmd params)
  13115. (message "Updating dynamic block `%s' at line %d...done" name line)
  13116. (goto-char pos))))
  13117. (defun org-beginning-of-dblock ()
  13118. "Find the beginning of the dynamic block at point.
  13119. Error if there is no scuh block at point."
  13120. (let ((pos (point))
  13121. beg)
  13122. (end-of-line 1)
  13123. (if (and (re-search-backward org-dblock-start-re nil t)
  13124. (setq beg (match-beginning 0))
  13125. (re-search-forward org-dblock-end-re nil t)
  13126. (> (match-end 0) pos))
  13127. (goto-char beg)
  13128. (goto-char pos)
  13129. (error "Not in a dynamic block"))))
  13130. (defun org-update-all-dblocks ()
  13131. "Update all dynamic blocks in the buffer.
  13132. This function can be used in a hook."
  13133. (when (org-mode-p)
  13134. (org-map-dblocks 'org-update-dblock)))
  13135. ;;;; Completion
  13136. (defconst org-additional-option-like-keywords
  13137. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13138. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13139. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13140. (defun org-complete (&optional arg)
  13141. "Perform completion on word at point.
  13142. At the beginning of a headline, this completes TODO keywords as given in
  13143. `org-todo-keywords'.
  13144. If the current word is preceded by a backslash, completes the TeX symbols
  13145. that are supported for HTML support.
  13146. If the current word is preceded by \"#+\", completes special words for
  13147. setting file options.
  13148. In the line after \"#+STARTUP:, complete valid keywords.\"
  13149. At all other locations, this simply calls the value of
  13150. `org-completion-fallback-command'."
  13151. (interactive "P")
  13152. (org-without-partial-completion
  13153. (catch 'exit
  13154. (let* ((end (point))
  13155. (beg1 (save-excursion
  13156. (skip-chars-backward (org-re "[:alnum:]_@"))
  13157. (point)))
  13158. (beg (save-excursion
  13159. (skip-chars-backward "a-zA-Z0-9_:$")
  13160. (point)))
  13161. (confirm (lambda (x) (stringp (car x))))
  13162. (searchhead (equal (char-before beg) ?*))
  13163. (tag (and (equal (char-before beg1) ?:)
  13164. (equal (char-after (point-at-bol)) ?*)))
  13165. (prop (and (equal (char-before beg1) ?:)
  13166. (not (equal (char-after (point-at-bol)) ?*))))
  13167. (texp (equal (char-before beg) ?\\))
  13168. (link (equal (char-before beg) ?\[))
  13169. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13170. beg)
  13171. "#+"))
  13172. (startup (string-match "^#\\+STARTUP:.*"
  13173. (buffer-substring (point-at-bol) (point))))
  13174. (completion-ignore-case opt)
  13175. (type nil)
  13176. (tbl nil)
  13177. (table (cond
  13178. (opt
  13179. (setq type :opt)
  13180. (append
  13181. (mapcar
  13182. (lambda (x)
  13183. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13184. (cons (match-string 2 x) (match-string 1 x)))
  13185. (org-split-string (org-get-current-options) "\n"))
  13186. (mapcar 'list org-additional-option-like-keywords)))
  13187. (startup
  13188. (setq type :startup)
  13189. org-startup-options)
  13190. (link (append org-link-abbrev-alist-local
  13191. org-link-abbrev-alist))
  13192. (texp
  13193. (setq type :tex)
  13194. org-html-entities)
  13195. ((string-match "\\`\\*+[ \t]+\\'"
  13196. (buffer-substring (point-at-bol) beg))
  13197. (setq type :todo)
  13198. (mapcar 'list org-todo-keywords-1))
  13199. (searchhead
  13200. (setq type :searchhead)
  13201. (save-excursion
  13202. (goto-char (point-min))
  13203. (while (re-search-forward org-todo-line-regexp nil t)
  13204. (push (list
  13205. (org-make-org-heading-search-string
  13206. (match-string 3) t))
  13207. tbl)))
  13208. tbl)
  13209. (tag (setq type :tag beg beg1)
  13210. (or org-tag-alist (org-get-buffer-tags)))
  13211. (prop (setq type :prop beg beg1)
  13212. (mapcar 'list (org-buffer-property-keys nil t t)))
  13213. (t (progn
  13214. (call-interactively org-completion-fallback-command)
  13215. (throw 'exit nil)))))
  13216. (pattern (buffer-substring-no-properties beg end))
  13217. (completion (try-completion pattern table confirm)))
  13218. (cond ((eq completion t)
  13219. (if (not (assoc (upcase pattern) table))
  13220. (message "Already complete")
  13221. (if (equal type :opt)
  13222. (insert (substring (cdr (assoc (upcase pattern) table))
  13223. (length pattern)))
  13224. (if (memq type '(:tag :prop)) (insert ":")))))
  13225. ((null completion)
  13226. (message "Can't find completion for \"%s\"" pattern)
  13227. (ding))
  13228. ((not (string= pattern completion))
  13229. (delete-region beg end)
  13230. (if (string-match " +$" completion)
  13231. (setq completion (replace-match "" t t completion)))
  13232. (insert completion)
  13233. (if (get-buffer-window "*Completions*")
  13234. (delete-window (get-buffer-window "*Completions*")))
  13235. (if (assoc completion table)
  13236. (if (eq type :todo) (insert " ")
  13237. (if (memq type '(:tag :prop)) (insert ":"))))
  13238. (if (and (equal type :opt) (assoc completion table))
  13239. (message "%s" (substitute-command-keys
  13240. "Press \\[org-complete] again to insert example settings"))))
  13241. (t
  13242. (message "Making completion list...")
  13243. (let ((list (sort (all-completions pattern table confirm)
  13244. 'string<)))
  13245. (with-output-to-temp-buffer "*Completions*"
  13246. (condition-case nil
  13247. ;; Protection needed for XEmacs and emacs 21
  13248. (display-completion-list list pattern)
  13249. (error (display-completion-list list)))))
  13250. (message "Making completion list...%s" "done")))))))
  13251. ;;;; TODO, DEADLINE, Comments
  13252. (defun org-toggle-comment ()
  13253. "Change the COMMENT state of an entry."
  13254. (interactive)
  13255. (save-excursion
  13256. (org-back-to-heading)
  13257. (let (case-fold-search)
  13258. (if (looking-at (concat outline-regexp
  13259. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13260. (replace-match "" t t nil 1)
  13261. (if (looking-at outline-regexp)
  13262. (progn
  13263. (goto-char (match-end 0))
  13264. (insert org-comment-string " ")))))))
  13265. (defvar org-last-todo-state-is-todo nil
  13266. "This is non-nil when the last TODO state change led to a TODO state.
  13267. If the last change removed the TODO tag or switched to DONE, then
  13268. this is nil.")
  13269. (defvar org-setting-tags nil) ; dynamically skiped
  13270. ;; FIXME: better place
  13271. (defun org-property-or-variable-value (var &optional inherit)
  13272. "Check if there is a property fixing the value of VAR.
  13273. If yes, return this value. If not, return the current value of the variable."
  13274. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13275. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13276. (read prop)
  13277. (symbol-value var))))
  13278. (defun org-parse-local-options (string var)
  13279. "Parse STRING for startup setting relevant for variable VAR."
  13280. (let ((rtn (symbol-value var))
  13281. e opts)
  13282. (save-match-data
  13283. (if (or (not string) (not (string-match "\\S-" string)))
  13284. rtn
  13285. (setq opts (delq nil (mapcar (lambda (x)
  13286. (setq e (assoc x org-startup-options))
  13287. (if (eq (nth 1 e) var) e nil))
  13288. (org-split-string string "[ \t]+"))))
  13289. (if (not opts)
  13290. rtn
  13291. (setq rtn nil)
  13292. (while (setq e (pop opts))
  13293. (if (not (nth 3 e))
  13294. (setq rtn (nth 2 e))
  13295. (if (not (listp rtn)) (setq rtn nil))
  13296. (push (nth 2 e) rtn)))
  13297. rtn)))))
  13298. (defvar org-blocker-hook nil
  13299. "Hook for functions that are allowed to block a state change.
  13300. Each function gets as its single argument a property list, see
  13301. `org-trigger-hook' for more information about this list.
  13302. If any of the functions in this hook returns nil, the state change
  13303. is blocked.")
  13304. (defvar org-trigger-hook nil
  13305. "Hook for functions that are triggered by a state change.
  13306. Each function gets as its single argument a property list with at least
  13307. the following elements:
  13308. (:type type-of-change :position pos-at-entry-start
  13309. :from old-state :to new-state)
  13310. Depending on the type, more properties may be present.
  13311. This mechanism is currently implemented for:
  13312. TODO state changes
  13313. ------------------
  13314. :type todo-state-change
  13315. :from previous state (keyword as a string), or nil
  13316. :to new state (keyword as a string), or nil")
  13317. (defun org-todo (&optional arg)
  13318. "Change the TODO state of an item.
  13319. The state of an item is given by a keyword at the start of the heading,
  13320. like
  13321. *** TODO Write paper
  13322. *** DONE Call mom
  13323. The different keywords are specified in the variable `org-todo-keywords'.
  13324. By default the available states are \"TODO\" and \"DONE\".
  13325. So for this example: when the item starts with TODO, it is changed to DONE.
  13326. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13327. DONE are present, add TODO at the beginning of the heading.
  13328. With C-u prefix arg, use completion to determine the new state.
  13329. With numeric prefix arg, switch to that state.
  13330. For calling through lisp, arg is also interpreted in the following way:
  13331. 'none -> empty state
  13332. \"\"(empty string) -> switch to empty state
  13333. 'done -> switch to DONE
  13334. 'nextset -> switch to the next set of keywords
  13335. 'previousset -> switch to the previous set of keywords
  13336. \"WAITING\" -> switch to the specified keyword, but only if it
  13337. really is a member of `org-todo-keywords'."
  13338. (interactive "P")
  13339. (save-excursion
  13340. (catch 'exit
  13341. (org-back-to-heading)
  13342. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13343. (or (looking-at (concat " +" org-todo-regexp " *"))
  13344. (looking-at " *"))
  13345. (let* ((match-data (match-data))
  13346. (startpos (point-at-bol))
  13347. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13348. (org-log-done org-log-done)
  13349. (org-log-repeat org-log-repeat)
  13350. (org-todo-log-states org-todo-log-states)
  13351. (this (match-string 1))
  13352. (hl-pos (match-beginning 0))
  13353. (head (org-get-todo-sequence-head this))
  13354. (ass (assoc head org-todo-kwd-alist))
  13355. (interpret (nth 1 ass))
  13356. (done-word (nth 3 ass))
  13357. (final-done-word (nth 4 ass))
  13358. (last-state (or this ""))
  13359. (completion-ignore-case t)
  13360. (member (member this org-todo-keywords-1))
  13361. (tail (cdr member))
  13362. (state (cond
  13363. ((and org-todo-key-trigger
  13364. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13365. (and (not arg) org-use-fast-todo-selection
  13366. (not (eq org-use-fast-todo-selection 'prefix)))))
  13367. ;; Use fast selection
  13368. (org-fast-todo-selection))
  13369. ((and (equal arg '(4))
  13370. (or (not org-use-fast-todo-selection)
  13371. (not org-todo-key-trigger)))
  13372. ;; Read a state with completion
  13373. (completing-read "State: " (mapcar (lambda(x) (list x))
  13374. org-todo-keywords-1)
  13375. nil t))
  13376. ((eq arg 'right)
  13377. (if this
  13378. (if tail (car tail) nil)
  13379. (car org-todo-keywords-1)))
  13380. ((eq arg 'left)
  13381. (if (equal member org-todo-keywords-1)
  13382. nil
  13383. (if this
  13384. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13385. org-todo-keywords-1)
  13386. (org-last org-todo-keywords-1))))
  13387. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13388. (setq arg nil))) ; hack to fall back to cycling
  13389. (arg
  13390. ;; user or caller requests a specific state
  13391. (cond
  13392. ((equal arg "") nil)
  13393. ((eq arg 'none) nil)
  13394. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13395. ((eq arg 'nextset)
  13396. (or (car (cdr (member head org-todo-heads)))
  13397. (car org-todo-heads)))
  13398. ((eq arg 'previousset)
  13399. (let ((org-todo-heads (reverse org-todo-heads)))
  13400. (or (car (cdr (member head org-todo-heads)))
  13401. (car org-todo-heads))))
  13402. ((car (member arg org-todo-keywords-1)))
  13403. ((nth (1- (prefix-numeric-value arg))
  13404. org-todo-keywords-1))))
  13405. ((null member) (or head (car org-todo-keywords-1)))
  13406. ((equal this final-done-word) nil) ;; -> make empty
  13407. ((null tail) nil) ;; -> first entry
  13408. ((eq interpret 'sequence)
  13409. (car tail))
  13410. ((memq interpret '(type priority))
  13411. (if (eq this-command last-command)
  13412. (car tail)
  13413. (if (> (length tail) 0)
  13414. (or done-word (car org-done-keywords))
  13415. nil)))
  13416. (t nil)))
  13417. (next (if state (concat " " state " ") " "))
  13418. (change-plist (list :type 'todo-state-change :from this :to state
  13419. :position startpos))
  13420. dolog now-done-p)
  13421. (when org-blocker-hook
  13422. (unless (save-excursion
  13423. (save-match-data
  13424. (run-hook-with-args-until-failure
  13425. 'org-blocker-hook change-plist)))
  13426. (if (interactive-p)
  13427. (error "TODO state change from %s to %s blocked" this state)
  13428. ;; fail silently
  13429. (message "TODO state change from %s to %s blocked" this state)
  13430. (throw 'exit nil))))
  13431. (store-match-data match-data)
  13432. (replace-match next t t)
  13433. (unless (pos-visible-in-window-p hl-pos)
  13434. (message "TODO state changed to %s" (org-trim next)))
  13435. (unless head
  13436. (setq head (org-get-todo-sequence-head state)
  13437. ass (assoc head org-todo-kwd-alist)
  13438. interpret (nth 1 ass)
  13439. done-word (nth 3 ass)
  13440. final-done-word (nth 4 ass)))
  13441. (when (memq arg '(nextset previousset))
  13442. (message "Keyword-Set %d/%d: %s"
  13443. (- (length org-todo-sets) -1
  13444. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13445. (length org-todo-sets)
  13446. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13447. (setq org-last-todo-state-is-todo
  13448. (not (member state org-done-keywords)))
  13449. (setq now-done-p (and (member state org-done-keywords)
  13450. (not (member this org-done-keywords))))
  13451. (and logging (org-local-logging logging))
  13452. (when (and (or org-todo-log-states org-log-done)
  13453. (not (memq arg '(nextset previousset))))
  13454. ;; we need to look at recording a time and note
  13455. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13456. (nth 2 (assoc this org-todo-log-states))))
  13457. (when (and state
  13458. (member state org-not-done-keywords)
  13459. (not (member this org-not-done-keywords)))
  13460. ;; This is now a todo state and was not one before
  13461. ;; If there was a CLOSED time stamp, get rid of it.
  13462. (org-add-planning-info nil nil 'closed))
  13463. (when (and now-done-p org-log-done)
  13464. ;; It is now done, and it was not done before
  13465. (org-add-planning-info 'closed (org-current-time))
  13466. (if (and (not dolog) (eq 'note org-log-done))
  13467. (org-add-log-maybe 'done state 'findpos 'note)))
  13468. (when (and state dolog)
  13469. ;; This is a non-nil state, and we need to log it
  13470. (org-add-log-maybe 'state state 'findpos dolog)))
  13471. ;; Fixup tag positioning
  13472. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13473. (run-hooks 'org-after-todo-state-change-hook)
  13474. (if (and arg (not (member state org-done-keywords)))
  13475. (setq head (org-get-todo-sequence-head state)))
  13476. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13477. ;; Do we need to trigger a repeat?
  13478. (when now-done-p (org-auto-repeat-maybe state))
  13479. ;; Fixup cursor location if close to the keyword
  13480. (if (and (outline-on-heading-p)
  13481. (not (bolp))
  13482. (save-excursion (beginning-of-line 1)
  13483. (looking-at org-todo-line-regexp))
  13484. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13485. (progn
  13486. (goto-char (or (match-end 2) (match-end 1)))
  13487. (just-one-space)))
  13488. (when org-trigger-hook
  13489. (save-excursion
  13490. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13491. (defun org-local-logging (value)
  13492. "Get logging settings from a property VALUE."
  13493. (let* (words w a)
  13494. ;; directly set the variables, they are already local.
  13495. (setq org-log-done nil
  13496. org-log-repeat nil
  13497. org-todo-log-states nil)
  13498. (setq words (org-split-string value))
  13499. (while (setq w (pop words))
  13500. (cond
  13501. ((setq a (assoc w org-startup-options))
  13502. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13503. (set (nth 1 a) (nth 2 a))))
  13504. ((setq a (org-extract-log-state-settings w))
  13505. (and (member (car a) org-todo-keywords-1)
  13506. (push a org-todo-log-states)))))))
  13507. (defun org-get-todo-sequence-head (kwd)
  13508. "Return the head of the TODO sequence to which KWD belongs.
  13509. If KWD is not set, check if there is a text property remembering the
  13510. right sequence."
  13511. (let (p)
  13512. (cond
  13513. ((not kwd)
  13514. (or (get-text-property (point-at-bol) 'org-todo-head)
  13515. (progn
  13516. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13517. nil (point-at-eol)))
  13518. (get-text-property p 'org-todo-head))))
  13519. ((not (member kwd org-todo-keywords-1))
  13520. (car org-todo-keywords-1))
  13521. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13522. (defun org-fast-todo-selection ()
  13523. "Fast TODO keyword selection with single keys.
  13524. Returns the new TODO keyword, or nil if no state change should occur."
  13525. (let* ((fulltable org-todo-key-alist)
  13526. (done-keywords org-done-keywords) ;; needed for the faces.
  13527. (maxlen (apply 'max (mapcar
  13528. (lambda (x)
  13529. (if (stringp (car x)) (string-width (car x)) 0))
  13530. fulltable)))
  13531. (expert nil)
  13532. (fwidth (+ maxlen 3 1 3))
  13533. (ncol (/ (- (window-width) 4) fwidth))
  13534. tg cnt e c tbl
  13535. groups ingroup)
  13536. (save-window-excursion
  13537. (if expert
  13538. (set-buffer (get-buffer-create " *Org todo*"))
  13539. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13540. (erase-buffer)
  13541. (org-set-local 'org-done-keywords done-keywords)
  13542. (setq tbl fulltable cnt 0)
  13543. (while (setq e (pop tbl))
  13544. (cond
  13545. ((equal e '(:startgroup))
  13546. (push '() groups) (setq ingroup t)
  13547. (when (not (= cnt 0))
  13548. (setq cnt 0)
  13549. (insert "\n"))
  13550. (insert "{ "))
  13551. ((equal e '(:endgroup))
  13552. (setq ingroup nil cnt 0)
  13553. (insert "}\n"))
  13554. (t
  13555. (setq tg (car e) c (cdr e))
  13556. (if ingroup (push tg (car groups)))
  13557. (setq tg (org-add-props tg nil 'face
  13558. (org-get-todo-face tg)))
  13559. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13560. (insert "[" c "] " tg (make-string
  13561. (- fwidth 4 (length tg)) ?\ ))
  13562. (when (= (setq cnt (1+ cnt)) ncol)
  13563. (insert "\n")
  13564. (if ingroup (insert " "))
  13565. (setq cnt 0)))))
  13566. (insert "\n")
  13567. (goto-char (point-min))
  13568. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13569. (fit-window-to-buffer))
  13570. (message "[a-z..]:Set [SPC]:clear")
  13571. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13572. (cond
  13573. ((or (= c ?\C-g)
  13574. (and (= c ?q) (not (rassoc c fulltable))))
  13575. (setq quit-flag t))
  13576. ((= c ?\ ) nil)
  13577. ((setq e (rassoc c fulltable) tg (car e))
  13578. tg)
  13579. (t (setq quit-flag t))))))
  13580. (defun org-get-repeat ()
  13581. "Check if tere is a deadline/schedule with repeater in this entry."
  13582. (save-match-data
  13583. (save-excursion
  13584. (org-back-to-heading t)
  13585. (if (re-search-forward
  13586. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13587. (match-string 1)))))
  13588. (defvar org-last-changed-timestamp)
  13589. (defvar org-log-post-message)
  13590. (defvar org-log-note-purpose)
  13591. (defun org-auto-repeat-maybe (done-word)
  13592. "Check if the current headline contains a repeated deadline/schedule.
  13593. If yes, set TODO state back to what it was and change the base date
  13594. of repeating deadline/scheduled time stamps to new date.
  13595. This function is run automatically after each state change to a DONE state."
  13596. ;; last-state is dynamically scoped into this function
  13597. (let* ((repeat (org-get-repeat))
  13598. (aa (assoc last-state org-todo-kwd-alist))
  13599. (interpret (nth 1 aa))
  13600. (head (nth 2 aa))
  13601. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13602. (msg "Entry repeats: ")
  13603. (org-log-done nil)
  13604. (org-todo-log-states nil)
  13605. re type n what ts)
  13606. (when repeat
  13607. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13608. (org-todo (if (eq interpret 'type) last-state head))
  13609. (when (and org-log-repeat
  13610. (or (not (memq 'org-add-log-note
  13611. (default-value 'post-command-hook)))
  13612. (eq org-log-note-purpose 'done)))
  13613. ;; Make sure a note is taken;
  13614. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13615. 'findpos org-log-repeat))
  13616. (org-back-to-heading t)
  13617. (org-add-planning-info nil nil 'closed)
  13618. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13619. org-deadline-time-regexp "\\)\\|\\("
  13620. org-ts-regexp "\\)"))
  13621. (while (re-search-forward
  13622. re (save-excursion (outline-next-heading) (point)) t)
  13623. (setq type (if (match-end 1) org-scheduled-string
  13624. (if (match-end 3) org-deadline-string "Plain:"))
  13625. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13626. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13627. (setq n (string-to-number (match-string 1 ts))
  13628. what (match-string 2 ts))
  13629. (if (equal what "w") (setq n (* n 7) what "d"))
  13630. (org-timestamp-change n (cdr (assoc what whata)))
  13631. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13632. (setq org-log-post-message msg)
  13633. (message "%s" msg))))
  13634. (defun org-show-todo-tree (arg)
  13635. "Make a compact tree which shows all headlines marked with TODO.
  13636. The tree will show the lines where the regexp matches, and all higher
  13637. headlines above the match.
  13638. With a \\[universal-argument] prefix, also show the DONE entries.
  13639. With a numeric prefix N, construct a sparse tree for the Nth element
  13640. of `org-todo-keywords-1'."
  13641. (interactive "P")
  13642. (let ((case-fold-search nil)
  13643. (kwd-re
  13644. (cond ((null arg) org-not-done-regexp)
  13645. ((equal arg '(4))
  13646. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13647. (mapcar 'list org-todo-keywords-1))))
  13648. (concat "\\("
  13649. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13650. "\\)\\>")))
  13651. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13652. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13653. org-todo-keywords-1)))
  13654. (t (error "Invalid prefix argument: %s" arg)))))
  13655. (message "%d TODO entries found"
  13656. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13657. (defun org-deadline (&optional remove)
  13658. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13659. With argument REMOVE, remove any deadline from the item."
  13660. (interactive "P")
  13661. (if remove
  13662. (progn
  13663. (org-remove-timestamp-with-keyword org-deadline-string)
  13664. (message "Item no longer has a deadline."))
  13665. (org-add-planning-info 'deadline nil 'closed)))
  13666. (defun org-schedule (&optional remove)
  13667. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13668. With argument REMOVE, remove any scheduling date from the item."
  13669. (interactive "P")
  13670. (if remove
  13671. (progn
  13672. (org-remove-timestamp-with-keyword org-scheduled-string)
  13673. (message "Item is no longer scheduled."))
  13674. (org-add-planning-info 'scheduled nil 'closed)))
  13675. (defun org-remove-timestamp-with-keyword (keyword)
  13676. "Remove all time stamps with KEYWORD in the current entry."
  13677. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13678. beg)
  13679. (save-excursion
  13680. (org-back-to-heading t)
  13681. (setq beg (point))
  13682. (org-end-of-subtree t t)
  13683. (while (re-search-backward re beg t)
  13684. (replace-match "")
  13685. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13686. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13687. (defun org-add-planning-info (what &optional time &rest remove)
  13688. "Insert new timestamp with keyword in the line directly after the headline.
  13689. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13690. If non is given, the user is prompted for a date.
  13691. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13692. be removed."
  13693. (interactive)
  13694. (let (org-time-was-given org-end-time-was-given)
  13695. (when what (setq time (or time (org-read-date nil 'to-time))))
  13696. (when (and org-insert-labeled-timestamps-at-point
  13697. (member what '(scheduled deadline)))
  13698. (insert
  13699. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13700. (org-insert-time-stamp time org-time-was-given
  13701. nil nil nil (list org-end-time-was-given))
  13702. (setq what nil))
  13703. (save-excursion
  13704. (save-restriction
  13705. (let (col list elt ts buffer-invisibility-spec)
  13706. (org-back-to-heading t)
  13707. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13708. (goto-char (match-end 1))
  13709. (setq col (current-column))
  13710. (goto-char (match-end 0))
  13711. (if (eobp) (insert "\n") (forward-char 1))
  13712. (if (and (not (looking-at outline-regexp))
  13713. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13714. "[^\r\n]*"))
  13715. (not (equal (match-string 1) org-clock-string)))
  13716. (narrow-to-region (match-beginning 0) (match-end 0))
  13717. (insert-before-markers "\n")
  13718. (backward-char 1)
  13719. (narrow-to-region (point) (point))
  13720. (indent-to-column col))
  13721. ;; Check if we have to remove something.
  13722. (setq list (cons what remove))
  13723. (while list
  13724. (setq elt (pop list))
  13725. (goto-char (point-min))
  13726. (when (or (and (eq elt 'scheduled)
  13727. (re-search-forward org-scheduled-time-regexp nil t))
  13728. (and (eq elt 'deadline)
  13729. (re-search-forward org-deadline-time-regexp nil t))
  13730. (and (eq elt 'closed)
  13731. (re-search-forward org-closed-time-regexp nil t)))
  13732. (replace-match "")
  13733. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13734. (if (looking-at " +") (replace-match ""))))
  13735. (goto-char (point-max))
  13736. (when what
  13737. (insert
  13738. (if (not (equal (char-before) ?\ )) " " "")
  13739. (cond ((eq what 'scheduled) org-scheduled-string)
  13740. ((eq what 'deadline) org-deadline-string)
  13741. ((eq what 'closed) org-closed-string))
  13742. " ")
  13743. (setq ts (org-insert-time-stamp
  13744. time
  13745. (or org-time-was-given
  13746. (and (eq what 'closed) org-log-done-with-time))
  13747. (eq what 'closed)
  13748. nil nil (list org-end-time-was-given)))
  13749. (end-of-line 1))
  13750. (goto-char (point-min))
  13751. (widen)
  13752. (if (looking-at "[ \t]+\r?\n")
  13753. (replace-match ""))
  13754. ts)))))
  13755. (defvar org-log-note-marker (make-marker))
  13756. (defvar org-log-note-purpose nil)
  13757. (defvar org-log-note-state nil)
  13758. (defvar org-log-note-how nil)
  13759. (defvar org-log-note-window-configuration nil)
  13760. (defvar org-log-note-return-to (make-marker))
  13761. (defvar org-log-post-message nil
  13762. "Message to be displayed after a log note has been stored.
  13763. The auto-repeater uses this.")
  13764. (defun org-add-log-maybe (&optional purpose state findpos how)
  13765. "Set up the post command hook to take a note.
  13766. If this is about to TODO state change, the new state is expected in STATE.
  13767. When FINDPOS is non-nil, find the correct position for the note in
  13768. the current entry. If not, assume that it can be inserted at point."
  13769. (save-excursion
  13770. (when findpos
  13771. (org-back-to-heading t)
  13772. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13773. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13774. "[^\r\n]*\\)?"))
  13775. (goto-char (match-end 0))
  13776. (unless org-log-states-order-reversed
  13777. (and (= (char-after) ?\n) (forward-char 1))
  13778. (org-skip-over-state-notes)
  13779. (skip-chars-backward " \t\n\r")))
  13780. (move-marker org-log-note-marker (point))
  13781. (setq org-log-note-purpose purpose
  13782. org-log-note-state state
  13783. org-log-note-how how)
  13784. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13785. (defun org-skip-over-state-notes ()
  13786. "Skip past the list of State notes in an entry."
  13787. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13788. (while (looking-at "[ \t]*- State")
  13789. (condition-case nil
  13790. (org-next-item)
  13791. (error (org-end-of-item)))))
  13792. (defun org-add-log-note (&optional purpose)
  13793. "Pop up a window for taking a note, and add this note later at point."
  13794. (remove-hook 'post-command-hook 'org-add-log-note)
  13795. (setq org-log-note-window-configuration (current-window-configuration))
  13796. (delete-other-windows)
  13797. (move-marker org-log-note-return-to (point))
  13798. (switch-to-buffer (marker-buffer org-log-note-marker))
  13799. (goto-char org-log-note-marker)
  13800. (org-switch-to-buffer-other-window "*Org Note*")
  13801. (erase-buffer)
  13802. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13803. (org-store-log-note)
  13804. (let ((org-inhibit-startup t)) (org-mode))
  13805. (insert (format "# Insert note for %s.
  13806. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13807. (cond
  13808. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13809. ((eq org-log-note-purpose 'done) "closed todo item")
  13810. ((eq org-log-note-purpose 'state)
  13811. (format "state change to \"%s\"" org-log-note-state))
  13812. (t (error "This should not happen")))))
  13813. (org-set-local 'org-finish-function 'org-store-log-note)))
  13814. (defun org-store-log-note ()
  13815. "Finish taking a log note, and insert it to where it belongs."
  13816. (let ((txt (buffer-string))
  13817. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13818. lines ind)
  13819. (kill-buffer (current-buffer))
  13820. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13821. (setq txt (replace-match "" t t txt)))
  13822. (if (string-match "\\s-+\\'" txt)
  13823. (setq txt (replace-match "" t t txt)))
  13824. (setq lines (org-split-string txt "\n"))
  13825. (when (and note (string-match "\\S-" note))
  13826. (setq note
  13827. (org-replace-escapes
  13828. note
  13829. (list (cons "%u" (user-login-name))
  13830. (cons "%U" user-full-name)
  13831. (cons "%t" (format-time-string
  13832. (org-time-stamp-format 'long 'inactive)
  13833. (current-time)))
  13834. (cons "%s" (if org-log-note-state
  13835. (concat "\"" org-log-note-state "\"")
  13836. "")))))
  13837. (if lines (setq note (concat note " \\\\")))
  13838. (push note lines))
  13839. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13840. (when lines
  13841. (save-excursion
  13842. (set-buffer (marker-buffer org-log-note-marker))
  13843. (save-excursion
  13844. (goto-char org-log-note-marker)
  13845. (move-marker org-log-note-marker nil)
  13846. (end-of-line 1)
  13847. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13848. (indent-relative nil)
  13849. (insert "- " (pop lines))
  13850. (org-indent-line-function)
  13851. (beginning-of-line 1)
  13852. (looking-at "[ \t]*")
  13853. (setq ind (concat (match-string 0) " "))
  13854. (end-of-line 1)
  13855. (while lines (insert "\n" ind (pop lines)))))))
  13856. (set-window-configuration org-log-note-window-configuration)
  13857. (with-current-buffer (marker-buffer org-log-note-return-to)
  13858. (goto-char org-log-note-return-to))
  13859. (move-marker org-log-note-return-to nil)
  13860. (and org-log-post-message (message "%s" org-log-post-message)))
  13861. ;; FIXME: what else would be useful?
  13862. ;; - priority
  13863. ;; - date
  13864. (defun org-sparse-tree (&optional arg)
  13865. "Create a sparse tree, prompt for the details.
  13866. This command can create sparse trees. You first need to select the type
  13867. of match used to create the tree:
  13868. t Show entries with a specific TODO keyword.
  13869. T Show entries selected by a tags match.
  13870. p Enter a property name and its value (both with completion on existing
  13871. names/values) and show entries with that property.
  13872. r Show entries matching a regular expression
  13873. d Show deadlines due within `org-deadline-warning-days'."
  13874. (interactive "P")
  13875. (let (ans kwd value)
  13876. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13877. (setq ans (read-char-exclusive))
  13878. (cond
  13879. ((equal ans ?d)
  13880. (call-interactively 'org-check-deadlines))
  13881. ((equal ans ?b)
  13882. (call-interactively 'org-check-before-date))
  13883. ((equal ans ?t)
  13884. (org-show-todo-tree '(4)))
  13885. ((equal ans ?T)
  13886. (call-interactively 'org-tags-sparse-tree))
  13887. ((member ans '(?p ?P))
  13888. (setq kwd (completing-read "Property: "
  13889. (mapcar 'list (org-buffer-property-keys))))
  13890. (setq value (completing-read "Value: "
  13891. (mapcar 'list (org-property-values kwd))))
  13892. (unless (string-match "\\`{.*}\\'" value)
  13893. (setq value (concat "\"" value "\"")))
  13894. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13895. ((member ans '(?r ?R ?/))
  13896. (call-interactively 'org-occur))
  13897. (t (error "No such sparse tree command \"%c\"" ans)))))
  13898. (defvar org-occur-highlights nil)
  13899. (make-variable-buffer-local 'org-occur-highlights)
  13900. (defun org-occur (regexp &optional keep-previous callback)
  13901. "Make a compact tree which shows all matches of REGEXP.
  13902. The tree will show the lines where the regexp matches, and all higher
  13903. headlines above the match. It will also show the heading after the match,
  13904. to make sure editing the matching entry is easy.
  13905. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13906. call to `org-occur' will be kept, to allow stacking of calls to this
  13907. command.
  13908. If CALLBACK is non-nil, it is a function which is called to confirm
  13909. that the match should indeed be shown."
  13910. (interactive "sRegexp: \nP")
  13911. (or keep-previous (org-remove-occur-highlights nil nil t))
  13912. (let ((cnt 0))
  13913. (save-excursion
  13914. (goto-char (point-min))
  13915. (if (or (not keep-previous) ; do not want to keep
  13916. (not org-occur-highlights)) ; no previous matches
  13917. ;; hide everything
  13918. (org-overview))
  13919. (while (re-search-forward regexp nil t)
  13920. (when (or (not callback)
  13921. (save-match-data (funcall callback)))
  13922. (setq cnt (1+ cnt))
  13923. (when org-highlight-sparse-tree-matches
  13924. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13925. (org-show-context 'occur-tree))))
  13926. (when org-remove-highlights-with-change
  13927. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13928. nil 'local))
  13929. (unless org-sparse-tree-open-archived-trees
  13930. (org-hide-archived-subtrees (point-min) (point-max)))
  13931. (run-hooks 'org-occur-hook)
  13932. (if (interactive-p)
  13933. (message "%d match(es) for regexp %s" cnt regexp))
  13934. cnt))
  13935. (defun org-show-context (&optional key)
  13936. "Make sure point and context and visible.
  13937. How much context is shown depends upon the variables
  13938. `org-show-hierarchy-above', `org-show-following-heading'. and
  13939. `org-show-siblings'."
  13940. (let ((heading-p (org-on-heading-p t))
  13941. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13942. (following-p (org-get-alist-option org-show-following-heading key))
  13943. (entry-p (org-get-alist-option org-show-entry-below key))
  13944. (siblings-p (org-get-alist-option org-show-siblings key)))
  13945. (catch 'exit
  13946. ;; Show heading or entry text
  13947. (if (and heading-p (not entry-p))
  13948. (org-flag-heading nil) ; only show the heading
  13949. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13950. (org-show-hidden-entry))) ; show entire entry
  13951. (when following-p
  13952. ;; Show next sibling, or heading below text
  13953. (save-excursion
  13954. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13955. (org-flag-heading nil))))
  13956. (when siblings-p (org-show-siblings))
  13957. (when hierarchy-p
  13958. ;; show all higher headings, possibly with siblings
  13959. (save-excursion
  13960. (while (and (condition-case nil
  13961. (progn (org-up-heading-all 1) t)
  13962. (error nil))
  13963. (not (bobp)))
  13964. (org-flag-heading nil)
  13965. (when siblings-p (org-show-siblings))))))))
  13966. (defun org-reveal (&optional siblings)
  13967. "Show current entry, hierarchy above it, and the following headline.
  13968. This can be used to show a consistent set of context around locations
  13969. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13970. not t for the search context.
  13971. With optional argument SIBLINGS, on each level of the hierarchy all
  13972. siblings are shown. This repairs the tree structure to what it would
  13973. look like when opened with hierarchical calls to `org-cycle'."
  13974. (interactive "P")
  13975. (let ((org-show-hierarchy-above t)
  13976. (org-show-following-heading t)
  13977. (org-show-siblings (if siblings t org-show-siblings)))
  13978. (org-show-context nil)))
  13979. (defun org-highlight-new-match (beg end)
  13980. "Highlight from BEG to END and mark the highlight is an occur headline."
  13981. (let ((ov (org-make-overlay beg end)))
  13982. (org-overlay-put ov 'face 'secondary-selection)
  13983. (push ov org-occur-highlights)))
  13984. (defun org-remove-occur-highlights (&optional beg end noremove)
  13985. "Remove the occur highlights from the buffer.
  13986. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13987. from the `before-change-functions' in the current buffer."
  13988. (interactive)
  13989. (unless org-inhibit-highlight-removal
  13990. (mapc 'org-delete-overlay org-occur-highlights)
  13991. (setq org-occur-highlights nil)
  13992. (unless noremove
  13993. (remove-hook 'before-change-functions
  13994. 'org-remove-occur-highlights 'local))))
  13995. ;;;; Priorities
  13996. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13997. "Regular expression matching the priority indicator.")
  13998. (defvar org-remove-priority-next-time nil)
  13999. (defun org-priority-up ()
  14000. "Increase the priority of the current item."
  14001. (interactive)
  14002. (org-priority 'up))
  14003. (defun org-priority-down ()
  14004. "Decrease the priority of the current item."
  14005. (interactive)
  14006. (org-priority 'down))
  14007. (defun org-priority (&optional action)
  14008. "Change the priority of an item by ARG.
  14009. ACTION can be `set', `up', `down', or a character."
  14010. (interactive)
  14011. (setq action (or action 'set))
  14012. (let (current new news have remove)
  14013. (save-excursion
  14014. (org-back-to-heading)
  14015. (if (looking-at org-priority-regexp)
  14016. (setq current (string-to-char (match-string 2))
  14017. have t)
  14018. (setq current org-default-priority))
  14019. (cond
  14020. ((or (eq action 'set) (integerp action))
  14021. (if (integerp action)
  14022. (setq new action)
  14023. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  14024. (setq new (read-char-exclusive)))
  14025. (if (and (= (upcase org-highest-priority) org-highest-priority)
  14026. (= (upcase org-lowest-priority) org-lowest-priority))
  14027. (setq new (upcase new)))
  14028. (cond ((equal new ?\ ) (setq remove t))
  14029. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  14030. (error "Priority must be between `%c' and `%c'"
  14031. org-highest-priority org-lowest-priority))))
  14032. ((eq action 'up)
  14033. (if (and (not have) (eq last-command this-command))
  14034. (setq new org-lowest-priority)
  14035. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14036. org-default-priority (1- current)))))
  14037. ((eq action 'down)
  14038. (if (and (not have) (eq last-command this-command))
  14039. (setq new org-highest-priority)
  14040. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14041. org-default-priority (1+ current)))))
  14042. (t (error "Invalid action")))
  14043. (if (or (< (upcase new) org-highest-priority)
  14044. (> (upcase new) org-lowest-priority))
  14045. (setq remove t))
  14046. (setq news (format "%c" new))
  14047. (if have
  14048. (if remove
  14049. (replace-match "" t t nil 1)
  14050. (replace-match news t t nil 2))
  14051. (if remove
  14052. (error "No priority cookie found in line")
  14053. (looking-at org-todo-line-regexp)
  14054. (if (match-end 2)
  14055. (progn
  14056. (goto-char (match-end 2))
  14057. (insert " [#" news "]"))
  14058. (goto-char (match-beginning 3))
  14059. (insert "[#" news "] ")))))
  14060. (org-preserve-lc (org-set-tags nil 'align))
  14061. (if remove
  14062. (message "Priority removed")
  14063. (message "Priority of current item set to %s" news))))
  14064. (defun org-get-priority (s)
  14065. "Find priority cookie and return priority."
  14066. (save-match-data
  14067. (if (not (string-match org-priority-regexp s))
  14068. (* 1000 (- org-lowest-priority org-default-priority))
  14069. (* 1000 (- org-lowest-priority
  14070. (string-to-char (match-string 2 s)))))))
  14071. ;;;; Tags
  14072. (defun org-scan-tags (action matcher &optional todo-only)
  14073. "Scan headline tags with inheritance and produce output ACTION.
  14074. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14075. evaluated, testing if a given set of tags qualifies a headline for
  14076. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14077. are included in the output."
  14078. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14079. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14080. (org-re
  14081. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14082. (props (list 'face nil
  14083. 'done-face 'org-done
  14084. 'undone-face nil
  14085. 'mouse-face 'highlight
  14086. 'org-not-done-regexp org-not-done-regexp
  14087. 'org-todo-regexp org-todo-regexp
  14088. 'keymap org-agenda-keymap
  14089. 'help-echo
  14090. (format "mouse-2 or RET jump to org file %s"
  14091. (abbreviate-file-name
  14092. (or (buffer-file-name (buffer-base-buffer))
  14093. (buffer-name (buffer-base-buffer)))))))
  14094. (case-fold-search nil)
  14095. lspos
  14096. tags tags-list tags-alist (llast 0) rtn level category i txt
  14097. todo marker entry priority)
  14098. (save-excursion
  14099. (goto-char (point-min))
  14100. (when (eq action 'sparse-tree)
  14101. (org-overview)
  14102. (org-remove-occur-highlights))
  14103. (while (re-search-forward re nil t)
  14104. (catch :skip
  14105. (setq todo (if (match-end 1) (match-string 2))
  14106. tags (if (match-end 4) (match-string 4)))
  14107. (goto-char (setq lspos (1+ (match-beginning 0))))
  14108. (setq level (org-reduced-level (funcall outline-level))
  14109. category (org-get-category))
  14110. (setq i llast llast level)
  14111. ;; remove tag lists from same and sublevels
  14112. (while (>= i level)
  14113. (when (setq entry (assoc i tags-alist))
  14114. (setq tags-alist (delete entry tags-alist)))
  14115. (setq i (1- i)))
  14116. ;; add the nex tags
  14117. (when tags
  14118. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14119. tags-alist
  14120. (cons (cons level tags) tags-alist)))
  14121. ;; compile tags for current headline
  14122. (setq tags-list
  14123. (if org-use-tag-inheritance
  14124. (apply 'append (mapcar 'cdr tags-alist))
  14125. tags))
  14126. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14127. (eval matcher)
  14128. (or (not org-agenda-skip-archived-trees)
  14129. (not (member org-archive-tag tags-list))))
  14130. (and (eq action 'agenda) (org-agenda-skip))
  14131. ;; list this headline
  14132. (if (eq action 'sparse-tree)
  14133. (progn
  14134. (and org-highlight-sparse-tree-matches
  14135. (org-get-heading) (match-end 0)
  14136. (org-highlight-new-match
  14137. (match-beginning 0) (match-beginning 1)))
  14138. (org-show-context 'tags-tree))
  14139. (setq txt (org-format-agenda-item
  14140. ""
  14141. (concat
  14142. (if org-tags-match-list-sublevels
  14143. (make-string (1- level) ?.) "")
  14144. (org-get-heading))
  14145. category tags-list)
  14146. priority (org-get-priority txt))
  14147. (goto-char lspos)
  14148. (setq marker (org-agenda-new-marker))
  14149. (org-add-props txt props
  14150. 'org-marker marker 'org-hd-marker marker 'org-category category
  14151. 'priority priority 'type "tagsmatch")
  14152. (push txt rtn))
  14153. ;; if we are to skip sublevels, jump to end of subtree
  14154. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14155. (when (and (eq action 'sparse-tree)
  14156. (not org-sparse-tree-open-archived-trees))
  14157. (org-hide-archived-subtrees (point-min) (point-max)))
  14158. (nreverse rtn)))
  14159. (defvar todo-only) ;; dynamically scoped
  14160. (defun org-tags-sparse-tree (&optional todo-only match)
  14161. "Create a sparse tree according to tags string MATCH.
  14162. MATCH can contain positive and negative selection of tags, like
  14163. \"+WORK+URGENT-WITHBOSS\".
  14164. If optional argument TODO_ONLY is non-nil, only select lines that are
  14165. also TODO lines."
  14166. (interactive "P")
  14167. (org-prepare-agenda-buffers (list (current-buffer)))
  14168. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14169. (defvar org-cached-props nil)
  14170. (defun org-cached-entry-get (pom property)
  14171. (if (or (eq t org-use-property-inheritance)
  14172. (member property org-use-property-inheritance))
  14173. ;; Caching is not possible, check it directly
  14174. (org-entry-get pom property 'inherit)
  14175. ;; Get all properties, so that we can do complicated checks easily
  14176. (cdr (assoc property (or org-cached-props
  14177. (setq org-cached-props
  14178. (org-entry-properties pom)))))))
  14179. (defun org-global-tags-completion-table (&optional files)
  14180. "Return the list of all tags in all agenda buffer/files."
  14181. (save-excursion
  14182. (org-uniquify
  14183. (delq nil
  14184. (apply 'append
  14185. (mapcar
  14186. (lambda (file)
  14187. (set-buffer (find-file-noselect file))
  14188. (append (org-get-buffer-tags)
  14189. (mapcar (lambda (x) (if (stringp (car-safe x))
  14190. (list (car-safe x)) nil))
  14191. org-tag-alist)))
  14192. (if (and files (car files))
  14193. files
  14194. (org-agenda-files))))))))
  14195. (defun org-make-tags-matcher (match)
  14196. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14197. ;; todo-only is scoped dynamically into this function, and the function
  14198. ;; may change it it the matcher asksk for it.
  14199. (unless match
  14200. ;; Get a new match request, with completion
  14201. (let ((org-last-tags-completion-table
  14202. (org-global-tags-completion-table)))
  14203. (setq match (completing-read
  14204. "Match: " 'org-tags-completion-function nil nil nil
  14205. 'org-tags-history))))
  14206. ;; Parse the string and create a lisp form
  14207. (let ((match0 match)
  14208. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14209. minus tag mm
  14210. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14211. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14212. (if (string-match "/+" match)
  14213. ;; match contains also a todo-matching request
  14214. (progn
  14215. (setq tagsmatch (substring match 0 (match-beginning 0))
  14216. todomatch (substring match (match-end 0)))
  14217. (if (string-match "^!" todomatch)
  14218. (setq todo-only t todomatch (substring todomatch 1)))
  14219. (if (string-match "^\\s-*$" todomatch)
  14220. (setq todomatch nil)))
  14221. ;; only matching tags
  14222. (setq tagsmatch match todomatch nil))
  14223. ;; Make the tags matcher
  14224. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14225. (setq tagsmatcher t)
  14226. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14227. (while (setq term (pop orterms))
  14228. (while (and (equal (substring term -1) "\\") orterms)
  14229. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14230. (while (string-match re term)
  14231. (setq minus (and (match-end 1)
  14232. (equal (match-string 1 term) "-"))
  14233. tag (match-string 2 term)
  14234. re-p (equal (string-to-char tag) ?{)
  14235. level-p (match-end 3)
  14236. prop-p (match-end 4)
  14237. mm (cond
  14238. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14239. (level-p `(= level ,(string-to-number
  14240. (match-string 3 term))))
  14241. (prop-p
  14242. (setq pn (match-string 4 term)
  14243. pv (match-string 5 term)
  14244. cat-p (equal pn "CATEGORY")
  14245. re-p (equal (string-to-char pv) ?{)
  14246. pv (substring pv 1 -1))
  14247. (if (equal pn "CATEGORY")
  14248. (setq gv '(get-text-property (point) 'org-category))
  14249. (setq gv `(org-cached-entry-get nil ,pn)))
  14250. (if re-p
  14251. `(string-match ,pv (or ,gv ""))
  14252. `(equal ,pv (or ,gv ""))))
  14253. (t `(member ,(downcase tag) tags-list)))
  14254. mm (if minus (list 'not mm) mm)
  14255. term (substring term (match-end 0)))
  14256. (push mm tagsmatcher))
  14257. (push (if (> (length tagsmatcher) 1)
  14258. (cons 'and tagsmatcher)
  14259. (car tagsmatcher))
  14260. orlist)
  14261. (setq tagsmatcher nil))
  14262. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14263. (setq tagsmatcher
  14264. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14265. ;; Make the todo matcher
  14266. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14267. (setq todomatcher t)
  14268. (setq orterms (org-split-string todomatch "|") orlist nil)
  14269. (while (setq term (pop orterms))
  14270. (while (string-match re term)
  14271. (setq minus (and (match-end 1)
  14272. (equal (match-string 1 term) "-"))
  14273. kwd (match-string 2 term)
  14274. re-p (equal (string-to-char kwd) ?{)
  14275. term (substring term (match-end 0))
  14276. mm (if re-p
  14277. `(string-match ,(substring kwd 1 -1) todo)
  14278. (list 'equal 'todo kwd))
  14279. mm (if minus (list 'not mm) mm))
  14280. (push mm todomatcher))
  14281. (push (if (> (length todomatcher) 1)
  14282. (cons 'and todomatcher)
  14283. (car todomatcher))
  14284. orlist)
  14285. (setq todomatcher nil))
  14286. (setq todomatcher (if (> (length orlist) 1)
  14287. (cons 'or orlist) (car orlist))))
  14288. ;; Return the string and lisp forms of the matcher
  14289. (setq matcher (if todomatcher
  14290. (list 'and tagsmatcher todomatcher)
  14291. tagsmatcher))
  14292. (cons match0 matcher)))
  14293. (defun org-match-any-p (re list)
  14294. "Does re match any element of list?"
  14295. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14296. (delq nil list))
  14297. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14298. (defvar org-tags-overlay (org-make-overlay 1 1))
  14299. (org-detach-overlay org-tags-overlay)
  14300. (defun org-align-tags-here (to-col)
  14301. ;; Assumes that this is a headline
  14302. (let ((pos (point)) (col (current-column)) tags)
  14303. (beginning-of-line 1)
  14304. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14305. (< pos (match-beginning 2)))
  14306. (progn
  14307. (setq tags (match-string 2))
  14308. (goto-char (match-beginning 1))
  14309. (insert " ")
  14310. (delete-region (point) (1+ (match-end 0)))
  14311. (backward-char 1)
  14312. (move-to-column
  14313. (max (1+ (current-column))
  14314. (1+ col)
  14315. (if (> to-col 0)
  14316. to-col
  14317. (- (abs to-col) (length tags))))
  14318. t)
  14319. (insert tags)
  14320. (move-to-column (min (current-column) col) t))
  14321. (goto-char pos))))
  14322. (defun org-set-tags (&optional arg just-align)
  14323. "Set the tags for the current headline.
  14324. With prefix ARG, realign all tags in headings in the current buffer."
  14325. (interactive "P")
  14326. (let* ((re (concat "^" outline-regexp))
  14327. (current (org-get-tags-string))
  14328. (col (current-column))
  14329. (org-setting-tags t)
  14330. table current-tags inherited-tags ; computed below when needed
  14331. tags p0 c0 c1 rpl)
  14332. (if arg
  14333. (save-excursion
  14334. (goto-char (point-min))
  14335. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14336. (while (re-search-forward re nil t)
  14337. (org-set-tags nil t)
  14338. (end-of-line 1)))
  14339. (message "All tags realigned to column %d" org-tags-column))
  14340. (if just-align
  14341. (setq tags current)
  14342. ;; Get a new set of tags from the user
  14343. (save-excursion
  14344. (setq table (or org-tag-alist (org-get-buffer-tags))
  14345. org-last-tags-completion-table table
  14346. current-tags (org-split-string current ":")
  14347. inherited-tags (nreverse
  14348. (nthcdr (length current-tags)
  14349. (nreverse (org-get-tags-at))))
  14350. tags
  14351. (if (or (eq t org-use-fast-tag-selection)
  14352. (and org-use-fast-tag-selection
  14353. (delq nil (mapcar 'cdr table))))
  14354. (org-fast-tag-selection
  14355. current-tags inherited-tags table
  14356. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14357. (let ((org-add-colon-after-tag-completion t))
  14358. (org-trim
  14359. (org-without-partial-completion
  14360. (completing-read "Tags: " 'org-tags-completion-function
  14361. nil nil current 'org-tags-history)))))))
  14362. (while (string-match "[-+&]+" tags)
  14363. ;; No boolean logic, just a list
  14364. (setq tags (replace-match ":" t t tags))))
  14365. (if (string-match "\\`[\t ]*\\'" tags)
  14366. (setq tags "")
  14367. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14368. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14369. ;; Insert new tags at the correct column
  14370. (beginning-of-line 1)
  14371. (cond
  14372. ((and (equal current "") (equal tags "")))
  14373. ((re-search-forward
  14374. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14375. (point-at-eol) t)
  14376. (if (equal tags "")
  14377. (setq rpl "")
  14378. (goto-char (match-beginning 0))
  14379. (setq c0 (current-column) p0 (point)
  14380. c1 (max (1+ c0) (if (> org-tags-column 0)
  14381. org-tags-column
  14382. (- (- org-tags-column) (length tags))))
  14383. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14384. (replace-match rpl t t)
  14385. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14386. tags)
  14387. (t (error "Tags alignment failed")))
  14388. (move-to-column col)
  14389. (unless just-align
  14390. (run-hooks 'org-after-tags-change-hook)))))
  14391. (defun org-change-tag-in-region (beg end tag off)
  14392. "Add or remove TAG for each entry in the region.
  14393. This works in the agenda, and also in an org-mode buffer."
  14394. (interactive
  14395. (list (region-beginning) (region-end)
  14396. (let ((org-last-tags-completion-table
  14397. (if (org-mode-p)
  14398. (org-get-buffer-tags)
  14399. (org-global-tags-completion-table))))
  14400. (completing-read
  14401. "Tag: " 'org-tags-completion-function nil nil nil
  14402. 'org-tags-history))
  14403. (progn
  14404. (message "[s]et or [r]emove? ")
  14405. (equal (read-char-exclusive) ?r))))
  14406. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14407. (let ((agendap (equal major-mode 'org-agenda-mode))
  14408. l1 l2 m buf pos newhead (cnt 0))
  14409. (goto-char end)
  14410. (setq l2 (1- (org-current-line)))
  14411. (goto-char beg)
  14412. (setq l1 (org-current-line))
  14413. (loop for l from l1 to l2 do
  14414. (goto-line l)
  14415. (setq m (get-text-property (point) 'org-hd-marker))
  14416. (when (or (and (org-mode-p) (org-on-heading-p))
  14417. (and agendap m))
  14418. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14419. pos (if agendap m (point)))
  14420. (with-current-buffer buf
  14421. (save-excursion
  14422. (save-restriction
  14423. (goto-char pos)
  14424. (setq cnt (1+ cnt))
  14425. (org-toggle-tag tag (if off 'off 'on))
  14426. (setq newhead (org-get-heading)))))
  14427. (and agendap (org-agenda-change-all-lines newhead m))))
  14428. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14429. (defun org-tags-completion-function (string predicate &optional flag)
  14430. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14431. (confirm (lambda (x) (stringp (car x)))))
  14432. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14433. (setq s1 (match-string 1 string)
  14434. s2 (match-string 2 string))
  14435. (setq s1 "" s2 string))
  14436. (cond
  14437. ((eq flag nil)
  14438. ;; try completion
  14439. (setq rtn (try-completion s2 ctable confirm))
  14440. (if (stringp rtn)
  14441. (setq rtn
  14442. (concat s1 s2 (substring rtn (length s2))
  14443. (if (and org-add-colon-after-tag-completion
  14444. (assoc rtn ctable))
  14445. ":" ""))))
  14446. rtn)
  14447. ((eq flag t)
  14448. ;; all-completions
  14449. (all-completions s2 ctable confirm)
  14450. )
  14451. ((eq flag 'lambda)
  14452. ;; exact match?
  14453. (assoc s2 ctable)))
  14454. ))
  14455. (defun org-fast-tag-insert (kwd tags face &optional end)
  14456. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14457. (insert (format "%-12s" (concat kwd ":"))
  14458. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14459. (or end "")))
  14460. (defun org-fast-tag-show-exit (flag)
  14461. (save-excursion
  14462. (goto-line 3)
  14463. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14464. (replace-match ""))
  14465. (when flag
  14466. (end-of-line 1)
  14467. (move-to-column (- (window-width) 19) t)
  14468. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14469. (defun org-set-current-tags-overlay (current prefix)
  14470. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14471. (if (featurep 'xemacs)
  14472. (org-overlay-display org-tags-overlay (concat prefix s)
  14473. 'secondary-selection)
  14474. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14475. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14476. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14477. "Fast tag selection with single keys.
  14478. CURRENT is the current list of tags in the headline, INHERITED is the
  14479. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14480. possibly with grouping information. TODO-TABLE is a similar table with
  14481. TODO keywords, should these have keys assigned to them.
  14482. If the keys are nil, a-z are automatically assigned.
  14483. Returns the new tags string, or nil to not change the current settings."
  14484. (let* ((fulltable (append table todo-table))
  14485. (maxlen (apply 'max (mapcar
  14486. (lambda (x)
  14487. (if (stringp (car x)) (string-width (car x)) 0))
  14488. fulltable)))
  14489. (buf (current-buffer))
  14490. (expert (eq org-fast-tag-selection-single-key 'expert))
  14491. (buffer-tags nil)
  14492. (fwidth (+ maxlen 3 1 3))
  14493. (ncol (/ (- (window-width) 4) fwidth))
  14494. (i-face 'org-done)
  14495. (c-face 'org-todo)
  14496. tg cnt e c char c1 c2 ntable tbl rtn
  14497. ov-start ov-end ov-prefix
  14498. (exit-after-next org-fast-tag-selection-single-key)
  14499. (done-keywords org-done-keywords)
  14500. groups ingroup)
  14501. (save-excursion
  14502. (beginning-of-line 1)
  14503. (if (looking-at
  14504. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14505. (setq ov-start (match-beginning 1)
  14506. ov-end (match-end 1)
  14507. ov-prefix "")
  14508. (setq ov-start (1- (point-at-eol))
  14509. ov-end (1+ ov-start))
  14510. (skip-chars-forward "^\n\r")
  14511. (setq ov-prefix
  14512. (concat
  14513. (buffer-substring (1- (point)) (point))
  14514. (if (> (current-column) org-tags-column)
  14515. " "
  14516. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14517. (org-move-overlay org-tags-overlay ov-start ov-end)
  14518. (save-window-excursion
  14519. (if expert
  14520. (set-buffer (get-buffer-create " *Org tags*"))
  14521. (delete-other-windows)
  14522. (split-window-vertically)
  14523. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14524. (erase-buffer)
  14525. (org-set-local 'org-done-keywords done-keywords)
  14526. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14527. (org-fast-tag-insert "Current" current c-face "\n\n")
  14528. (org-fast-tag-show-exit exit-after-next)
  14529. (org-set-current-tags-overlay current ov-prefix)
  14530. (setq tbl fulltable char ?a cnt 0)
  14531. (while (setq e (pop tbl))
  14532. (cond
  14533. ((equal e '(:startgroup))
  14534. (push '() groups) (setq ingroup t)
  14535. (when (not (= cnt 0))
  14536. (setq cnt 0)
  14537. (insert "\n"))
  14538. (insert "{ "))
  14539. ((equal e '(:endgroup))
  14540. (setq ingroup nil cnt 0)
  14541. (insert "}\n"))
  14542. (t
  14543. (setq tg (car e) c2 nil)
  14544. (if (cdr e)
  14545. (setq c (cdr e))
  14546. ;; automatically assign a character.
  14547. (setq c1 (string-to-char
  14548. (downcase (substring
  14549. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14550. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14551. (while (or (rassoc char ntable) (rassoc char table))
  14552. (setq char (1+ char)))
  14553. (setq c2 c1))
  14554. (setq c (or c2 char)))
  14555. (if ingroup (push tg (car groups)))
  14556. (setq tg (org-add-props tg nil 'face
  14557. (cond
  14558. ((not (assoc tg table))
  14559. (org-get-todo-face tg))
  14560. ((member tg current) c-face)
  14561. ((member tg inherited) i-face)
  14562. (t nil))))
  14563. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14564. (insert "[" c "] " tg (make-string
  14565. (- fwidth 4 (length tg)) ?\ ))
  14566. (push (cons tg c) ntable)
  14567. (when (= (setq cnt (1+ cnt)) ncol)
  14568. (insert "\n")
  14569. (if ingroup (insert " "))
  14570. (setq cnt 0)))))
  14571. (setq ntable (nreverse ntable))
  14572. (insert "\n")
  14573. (goto-char (point-min))
  14574. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14575. (fit-window-to-buffer))
  14576. (setq rtn
  14577. (catch 'exit
  14578. (while t
  14579. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14580. (if groups " [!] no groups" " [!]groups")
  14581. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14582. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14583. (cond
  14584. ((= c ?\r) (throw 'exit t))
  14585. ((= c ?!)
  14586. (setq groups (not groups))
  14587. (goto-char (point-min))
  14588. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14589. ((= c ?\C-c)
  14590. (if (not expert)
  14591. (org-fast-tag-show-exit
  14592. (setq exit-after-next (not exit-after-next)))
  14593. (setq expert nil)
  14594. (delete-other-windows)
  14595. (split-window-vertically)
  14596. (org-switch-to-buffer-other-window " *Org tags*")
  14597. (and (fboundp 'fit-window-to-buffer)
  14598. (fit-window-to-buffer))))
  14599. ((or (= c ?\C-g)
  14600. (and (= c ?q) (not (rassoc c ntable))))
  14601. (org-detach-overlay org-tags-overlay)
  14602. (setq quit-flag t))
  14603. ((= c ?\ )
  14604. (setq current nil)
  14605. (if exit-after-next (setq exit-after-next 'now)))
  14606. ((= c ?\t)
  14607. (condition-case nil
  14608. (setq tg (completing-read
  14609. "Tag: "
  14610. (or buffer-tags
  14611. (with-current-buffer buf
  14612. (org-get-buffer-tags)))))
  14613. (quit (setq tg "")))
  14614. (when (string-match "\\S-" tg)
  14615. (add-to-list 'buffer-tags (list tg))
  14616. (if (member tg current)
  14617. (setq current (delete tg current))
  14618. (push tg current)))
  14619. (if exit-after-next (setq exit-after-next 'now)))
  14620. ((setq e (rassoc c todo-table) tg (car e))
  14621. (with-current-buffer buf
  14622. (save-excursion (org-todo tg)))
  14623. (if exit-after-next (setq exit-after-next 'now)))
  14624. ((setq e (rassoc c ntable) tg (car e))
  14625. (if (member tg current)
  14626. (setq current (delete tg current))
  14627. (loop for g in groups do
  14628. (if (member tg g)
  14629. (mapc (lambda (x)
  14630. (setq current (delete x current)))
  14631. g)))
  14632. (push tg current))
  14633. (if exit-after-next (setq exit-after-next 'now))))
  14634. ;; Create a sorted list
  14635. (setq current
  14636. (sort current
  14637. (lambda (a b)
  14638. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14639. (if (eq exit-after-next 'now) (throw 'exit t))
  14640. (goto-char (point-min))
  14641. (beginning-of-line 2)
  14642. (delete-region (point) (point-at-eol))
  14643. (org-fast-tag-insert "Current" current c-face)
  14644. (org-set-current-tags-overlay current ov-prefix)
  14645. (while (re-search-forward
  14646. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14647. (setq tg (match-string 1))
  14648. (add-text-properties
  14649. (match-beginning 1) (match-end 1)
  14650. (list 'face
  14651. (cond
  14652. ((member tg current) c-face)
  14653. ((member tg inherited) i-face)
  14654. (t (get-text-property (match-beginning 1) 'face))))))
  14655. (goto-char (point-min)))))
  14656. (org-detach-overlay org-tags-overlay)
  14657. (if rtn
  14658. (mapconcat 'identity current ":")
  14659. nil))))
  14660. (defun org-get-tags-string ()
  14661. "Get the TAGS string in the current headline."
  14662. (unless (org-on-heading-p t)
  14663. (error "Not on a heading"))
  14664. (save-excursion
  14665. (beginning-of-line 1)
  14666. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14667. (org-match-string-no-properties 1)
  14668. "")))
  14669. (defun org-get-tags ()
  14670. "Get the list of tags specified in the current headline."
  14671. (org-split-string (org-get-tags-string) ":"))
  14672. (defun org-get-buffer-tags ()
  14673. "Get a table of all tags used in the buffer, for completion."
  14674. (let (tags)
  14675. (save-excursion
  14676. (goto-char (point-min))
  14677. (while (re-search-forward
  14678. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14679. (when (equal (char-after (point-at-bol 0)) ?*)
  14680. (mapc (lambda (x) (add-to-list 'tags x))
  14681. (org-split-string (org-match-string-no-properties 1) ":")))))
  14682. (mapcar 'list tags)))
  14683. ;;;; Properties
  14684. ;;; Setting and retrieving properties
  14685. (defconst org-special-properties
  14686. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14687. "TIMESTAMP" "TIMESTAMP_IA")
  14688. "The special properties valid in Org-mode.
  14689. These are properties that are not defined in the property drawer,
  14690. but in some other way.")
  14691. (defconst org-default-properties
  14692. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14693. "LOCATION" "LOGGING" "COLUMNS")
  14694. "Some properties that are used by Org-mode for various purposes.
  14695. Being in this list makes sure that they are offered for completion.")
  14696. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14697. "Regular expression matching the first line of a property drawer.")
  14698. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14699. "Regular expression matching the first line of a property drawer.")
  14700. (defun org-property-action ()
  14701. "Do an action on properties."
  14702. (interactive)
  14703. (let (c)
  14704. (org-at-property-p)
  14705. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14706. (setq c (read-char-exclusive))
  14707. (cond
  14708. ((equal c ?s)
  14709. (call-interactively 'org-set-property))
  14710. ((equal c ?d)
  14711. (call-interactively 'org-delete-property))
  14712. ((equal c ?D)
  14713. (call-interactively 'org-delete-property-globally))
  14714. ((equal c ?c)
  14715. (call-interactively 'org-compute-property-at-point))
  14716. (t (error "No such property action %c" c)))))
  14717. (defun org-at-property-p ()
  14718. "Is the cursor in a property line?"
  14719. ;; FIXME: Does not check if we are actually in the drawer.
  14720. ;; FIXME: also returns true on any drawers.....
  14721. ;; This is used by C-c C-c for property action.
  14722. (save-excursion
  14723. (beginning-of-line 1)
  14724. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14725. (defmacro org-with-point-at (pom &rest body)
  14726. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14727. (declare (indent 1) (debug t))
  14728. `(save-excursion
  14729. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14730. (save-excursion
  14731. (goto-char (or pom (point)))
  14732. ,@body)))
  14733. (defun org-get-property-block (&optional beg end force)
  14734. "Return the (beg . end) range of the body of the property drawer.
  14735. BEG and END can be beginning and end of subtree, if not given
  14736. they will be found.
  14737. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14738. (catch 'exit
  14739. (save-excursion
  14740. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14741. (end (or end (progn (outline-next-heading) (point)))))
  14742. (goto-char beg)
  14743. (if (re-search-forward org-property-start-re end t)
  14744. (setq beg (1+ (match-end 0)))
  14745. (if force
  14746. (save-excursion
  14747. (org-insert-property-drawer)
  14748. (setq end (progn (outline-next-heading) (point))))
  14749. (throw 'exit nil))
  14750. (goto-char beg)
  14751. (if (re-search-forward org-property-start-re end t)
  14752. (setq beg (1+ (match-end 0)))))
  14753. (if (re-search-forward org-property-end-re end t)
  14754. (setq end (match-beginning 0))
  14755. (or force (throw 'exit nil))
  14756. (goto-char beg)
  14757. (setq end beg)
  14758. (org-indent-line-function)
  14759. (insert ":END:\n"))
  14760. (cons beg end)))))
  14761. (defun org-entry-properties (&optional pom which)
  14762. "Get all properties of the entry at point-or-marker POM.
  14763. This includes the TODO keyword, the tags, time strings for deadline,
  14764. scheduled, and clocking, and any additional properties defined in the
  14765. entry. The return value is an alist, keys may occur multiple times
  14766. if the property key was used several times.
  14767. POM may also be nil, in which case the current entry is used.
  14768. If WHICH is nil or `all', get all properties. If WHICH is
  14769. `special' or `standard', only get that subclass."
  14770. (setq which (or which 'all))
  14771. (org-with-point-at pom
  14772. (let ((clockstr (substring org-clock-string 0 -1))
  14773. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14774. beg end range props sum-props key value string clocksum)
  14775. (save-excursion
  14776. (when (condition-case nil (org-back-to-heading t) (error nil))
  14777. (setq beg (point))
  14778. (setq sum-props (get-text-property (point) 'org-summaries))
  14779. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14780. (outline-next-heading)
  14781. (setq end (point))
  14782. (when (memq which '(all special))
  14783. ;; Get the special properties, like TODO and tags
  14784. (goto-char beg)
  14785. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14786. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14787. (when (looking-at org-priority-regexp)
  14788. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14789. (when (and (setq value (org-get-tags-string))
  14790. (string-match "\\S-" value))
  14791. (push (cons "TAGS" value) props))
  14792. (when (setq value (org-get-tags-at))
  14793. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14794. props))
  14795. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14796. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14797. string (if (equal key clockstr)
  14798. (org-no-properties
  14799. (org-trim
  14800. (buffer-substring
  14801. (match-beginning 3) (goto-char (point-at-eol)))))
  14802. (substring (org-match-string-no-properties 3) 1 -1)))
  14803. (unless key
  14804. (if (= (char-after (match-beginning 3)) ?\[)
  14805. (setq key "TIMESTAMP_IA")
  14806. (setq key "TIMESTAMP")))
  14807. (when (or (equal key clockstr) (not (assoc key props)))
  14808. (push (cons key string) props)))
  14809. )
  14810. (when (memq which '(all standard))
  14811. ;; Get the standard properties, like :PORP: ...
  14812. (setq range (org-get-property-block beg end))
  14813. (when range
  14814. (goto-char (car range))
  14815. (while (re-search-forward
  14816. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14817. (cdr range) t)
  14818. (setq key (org-match-string-no-properties 1)
  14819. value (org-trim (or (org-match-string-no-properties 2) "")))
  14820. (unless (member key excluded)
  14821. (push (cons key (or value "")) props)))))
  14822. (if clocksum
  14823. (push (cons "CLOCKSUM"
  14824. (org-column-number-to-string (/ (float clocksum) 60.)
  14825. 'add_times))
  14826. props))
  14827. (append sum-props (nreverse props)))))))
  14828. (defun org-entry-get (pom property &optional inherit)
  14829. "Get value of PROPERTY for entry at point-or-marker POM.
  14830. If INHERIT is non-nil and the entry does not have the property,
  14831. then also check higher levels of the hierarchy.
  14832. If the property is present but empty, the return value is the empty string.
  14833. If the property is not present at all, nil is returned."
  14834. (org-with-point-at pom
  14835. (if inherit
  14836. (org-entry-get-with-inheritance property)
  14837. (if (member property org-special-properties)
  14838. ;; We need a special property. Use brute force, get all properties.
  14839. (cdr (assoc property (org-entry-properties nil 'special)))
  14840. (let ((range (org-get-property-block)))
  14841. (if (and range
  14842. (goto-char (car range))
  14843. (re-search-forward
  14844. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14845. (cdr range) t))
  14846. ;; Found the property, return it.
  14847. (if (match-end 1)
  14848. (org-match-string-no-properties 1)
  14849. "")))))))
  14850. (defun org-entry-delete (pom property)
  14851. "Delete the property PROPERTY from entry at point-or-marker POM."
  14852. (org-with-point-at pom
  14853. (if (member property org-special-properties)
  14854. nil ; cannot delete these properties.
  14855. (let ((range (org-get-property-block)))
  14856. (if (and range
  14857. (goto-char (car range))
  14858. (re-search-forward
  14859. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14860. (cdr range) t))
  14861. (progn
  14862. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14863. t)
  14864. nil)))))
  14865. ;; Multi-values properties are properties that contain multiple values
  14866. ;; These values are assumed to be single words, separated by whitespace.
  14867. (defun org-entry-add-to-multivalued-property (pom property value)
  14868. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14869. (let* ((old (org-entry-get pom property))
  14870. (values (and old (org-split-string old "[ \t]"))))
  14871. (unless (member value values)
  14872. (setq values (cons value values))
  14873. (org-entry-put pom property
  14874. (mapconcat 'identity values " ")))))
  14875. (defun org-entry-remove-from-multivalued-property (pom property value)
  14876. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14877. (let* ((old (org-entry-get pom property))
  14878. (values (and old (org-split-string old "[ \t]"))))
  14879. (when (member value values)
  14880. (setq values (delete value values))
  14881. (org-entry-put pom property
  14882. (mapconcat 'identity values " ")))))
  14883. (defun org-entry-member-in-multivalued-property (pom property value)
  14884. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14885. (let* ((old (org-entry-get pom property))
  14886. (values (and old (org-split-string old "[ \t]"))))
  14887. (member value values)))
  14888. (defvar org-entry-property-inherited-from (make-marker))
  14889. (defun org-entry-get-with-inheritance (property)
  14890. "Get entry property, and search higher levels if not present."
  14891. (let (tmp)
  14892. (save-excursion
  14893. (save-restriction
  14894. (widen)
  14895. (catch 'ex
  14896. (while t
  14897. (when (setq tmp (org-entry-get nil property))
  14898. (org-back-to-heading t)
  14899. (move-marker org-entry-property-inherited-from (point))
  14900. (throw 'ex tmp))
  14901. (or (org-up-heading-safe) (throw 'ex nil)))))
  14902. (or tmp (cdr (assoc property org-local-properties))
  14903. (cdr (assoc property org-global-properties))))))
  14904. (defun org-entry-put (pom property value)
  14905. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14906. (org-with-point-at pom
  14907. (org-back-to-heading t)
  14908. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14909. range)
  14910. (cond
  14911. ((equal property "TODO")
  14912. (when (and (stringp value) (string-match "\\S-" value)
  14913. (not (member value org-todo-keywords-1)))
  14914. (error "\"%s\" is not a valid TODO state" value))
  14915. (if (or (not value)
  14916. (not (string-match "\\S-" value)))
  14917. (setq value 'none))
  14918. (org-todo value)
  14919. (org-set-tags nil 'align))
  14920. ((equal property "PRIORITY")
  14921. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14922. (string-to-char value) ?\ ))
  14923. (org-set-tags nil 'align))
  14924. ((equal property "SCHEDULED")
  14925. (if (re-search-forward org-scheduled-time-regexp end t)
  14926. (cond
  14927. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14928. ((eq value 'later) (org-timestamp-change 1 'day))
  14929. (t (call-interactively 'org-schedule)))
  14930. (call-interactively 'org-schedule)))
  14931. ((equal property "DEADLINE")
  14932. (if (re-search-forward org-deadline-time-regexp end t)
  14933. (cond
  14934. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14935. ((eq value 'later) (org-timestamp-change 1 'day))
  14936. (t (call-interactively 'org-deadline)))
  14937. (call-interactively 'org-deadline)))
  14938. ((member property org-special-properties)
  14939. (error "The %s property can not yet be set with `org-entry-put'"
  14940. property))
  14941. (t ; a non-special property
  14942. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14943. (setq range (org-get-property-block beg end 'force))
  14944. (goto-char (car range))
  14945. (if (re-search-forward
  14946. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14947. (progn
  14948. (delete-region (match-beginning 1) (match-end 1))
  14949. (goto-char (match-beginning 1)))
  14950. (goto-char (cdr range))
  14951. (insert "\n")
  14952. (backward-char 1)
  14953. (org-indent-line-function)
  14954. (insert ":" property ":"))
  14955. (and value (insert " " value))
  14956. (org-indent-line-function)))))))
  14957. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14958. "Get all property keys in the current buffer.
  14959. With INCLUDE-SPECIALS, also list the special properties that relect things
  14960. like tags and TODO state.
  14961. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14962. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14963. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14964. formats in the current buffer."
  14965. (let (rtn range cfmt cols s p)
  14966. (save-excursion
  14967. (save-restriction
  14968. (widen)
  14969. (goto-char (point-min))
  14970. (while (re-search-forward org-property-start-re nil t)
  14971. (setq range (org-get-property-block))
  14972. (goto-char (car range))
  14973. (while (re-search-forward
  14974. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14975. (cdr range) t)
  14976. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14977. (outline-next-heading))))
  14978. (when include-specials
  14979. (setq rtn (append org-special-properties rtn)))
  14980. (when include-defaults
  14981. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14982. (when include-columns
  14983. (save-excursion
  14984. (save-restriction
  14985. (widen)
  14986. (goto-char (point-min))
  14987. (while (re-search-forward
  14988. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14989. nil t)
  14990. (setq cfmt (match-string 2) s 0)
  14991. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14992. cfmt s)
  14993. (setq s (match-end 0)
  14994. p (match-string 1 cfmt))
  14995. (unless (or (equal p "ITEM")
  14996. (member p org-special-properties))
  14997. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14998. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14999. (defun org-property-values (key)
  15000. "Return a list of all values of property KEY."
  15001. (save-excursion
  15002. (save-restriction
  15003. (widen)
  15004. (goto-char (point-min))
  15005. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  15006. values)
  15007. (while (re-search-forward re nil t)
  15008. (add-to-list 'values (org-trim (match-string 1))))
  15009. (delete "" values)))))
  15010. (defun org-insert-property-drawer ()
  15011. "Insert a property drawer into the current entry."
  15012. (interactive)
  15013. (org-back-to-heading t)
  15014. (looking-at outline-regexp)
  15015. (let ((indent (- (match-end 0)(match-beginning 0)))
  15016. (beg (point))
  15017. (re (concat "^[ \t]*" org-keyword-time-regexp))
  15018. end hiddenp)
  15019. (outline-next-heading)
  15020. (setq end (point))
  15021. (goto-char beg)
  15022. (while (re-search-forward re end t))
  15023. (setq hiddenp (org-invisible-p))
  15024. (end-of-line 1)
  15025. (and (equal (char-after) ?\n) (forward-char 1))
  15026. (org-skip-over-state-notes)
  15027. (skip-chars-backward " \t\n\r")
  15028. (if (eq (char-before) ?*) (forward-char 1))
  15029. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  15030. (beginning-of-line 0)
  15031. (indent-to-column indent)
  15032. (beginning-of-line 2)
  15033. (indent-to-column indent)
  15034. (beginning-of-line 0)
  15035. (if hiddenp
  15036. (save-excursion
  15037. (org-back-to-heading t)
  15038. (hide-entry))
  15039. (org-flag-drawer t))))
  15040. (defun org-set-property (property value)
  15041. "In the current entry, set PROPERTY to VALUE.
  15042. When called interactively, this will prompt for a property name, offering
  15043. completion on existing and default properties. And then it will prompt
  15044. for a value, offering competion either on allowed values (via an inherited
  15045. xxx_ALL property) or on existing values in other instances of this property
  15046. in the current file."
  15047. (interactive
  15048. (let* ((prop (completing-read
  15049. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15050. (cur (org-entry-get nil prop))
  15051. (allowed (org-property-get-allowed-values nil prop 'table))
  15052. (existing (mapcar 'list (org-property-values prop)))
  15053. (val (if allowed
  15054. (completing-read "Value: " allowed nil 'req-match)
  15055. (completing-read
  15056. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15057. (concat "[" cur "]") "")
  15058. ": ")
  15059. existing nil nil "" nil cur))))
  15060. (list prop (if (equal val "") cur val))))
  15061. (unless (equal (org-entry-get nil property) value)
  15062. (org-entry-put nil property value)))
  15063. (defun org-delete-property (property)
  15064. "In the current entry, delete PROPERTY."
  15065. (interactive
  15066. (let* ((prop (completing-read
  15067. "Property: " (org-entry-properties nil 'standard))))
  15068. (list prop)))
  15069. (message "Property %s %s" property
  15070. (if (org-entry-delete nil property)
  15071. "deleted"
  15072. "was not present in the entry")))
  15073. (defun org-delete-property-globally (property)
  15074. "Remove PROPERTY globally, from all entries."
  15075. (interactive
  15076. (let* ((prop (completing-read
  15077. "Globally remove property: "
  15078. (mapcar 'list (org-buffer-property-keys)))))
  15079. (list prop)))
  15080. (save-excursion
  15081. (save-restriction
  15082. (widen)
  15083. (goto-char (point-min))
  15084. (let ((cnt 0))
  15085. (while (re-search-forward
  15086. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15087. nil t)
  15088. (setq cnt (1+ cnt))
  15089. (replace-match ""))
  15090. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15091. (defvar org-columns-current-fmt-compiled) ; defined below
  15092. (defun org-compute-property-at-point ()
  15093. "Compute the property at point.
  15094. This looks for an enclosing column format, extracts the operator and
  15095. then applies it to the proerty in the column format's scope."
  15096. (interactive)
  15097. (unless (org-at-property-p)
  15098. (error "Not at a property"))
  15099. (let ((prop (org-match-string-no-properties 2)))
  15100. (org-columns-get-format-and-top-level)
  15101. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15102. (error "No operator defined for property %s" prop))
  15103. (org-columns-compute prop)))
  15104. (defun org-property-get-allowed-values (pom property &optional table)
  15105. "Get allowed values for the property PROPERTY.
  15106. When TABLE is non-nil, return an alist that can directly be used for
  15107. completion."
  15108. (let (vals)
  15109. (cond
  15110. ((equal property "TODO")
  15111. (setq vals (org-with-point-at pom
  15112. (append org-todo-keywords-1 '("")))))
  15113. ((equal property "PRIORITY")
  15114. (let ((n org-lowest-priority))
  15115. (while (>= n org-highest-priority)
  15116. (push (char-to-string n) vals)
  15117. (setq n (1- n)))))
  15118. ((member property org-special-properties))
  15119. (t
  15120. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15121. (when (and vals (string-match "\\S-" vals))
  15122. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15123. (setq vals (mapcar (lambda (x)
  15124. (cond ((stringp x) x)
  15125. ((numberp x) (number-to-string x))
  15126. ((symbolp x) (symbol-name x))
  15127. (t "???")))
  15128. vals)))))
  15129. (if table (mapcar 'list vals) vals)))
  15130. (defun org-property-previous-allowed-value (&optional previous)
  15131. "Switch to the next allowed value for this property."
  15132. (interactive)
  15133. (org-property-next-allowed-value t))
  15134. (defun org-property-next-allowed-value (&optional previous)
  15135. "Switch to the next allowed value for this property."
  15136. (interactive)
  15137. (unless (org-at-property-p)
  15138. (error "Not at a property"))
  15139. (let* ((key (match-string 2))
  15140. (value (match-string 3))
  15141. (allowed (or (org-property-get-allowed-values (point) key)
  15142. (and (member value '("[ ]" "[-]" "[X]"))
  15143. '("[ ]" "[X]"))))
  15144. nval)
  15145. (unless allowed
  15146. (error "Allowed values for this property have not been defined"))
  15147. (if previous (setq allowed (reverse allowed)))
  15148. (if (member value allowed)
  15149. (setq nval (car (cdr (member value allowed)))))
  15150. (setq nval (or nval (car allowed)))
  15151. (if (equal nval value)
  15152. (error "Only one allowed value for this property"))
  15153. (org-at-property-p)
  15154. (replace-match (concat " :" key ": " nval) t t)
  15155. (org-indent-line-function)
  15156. (beginning-of-line 1)
  15157. (skip-chars-forward " \t")))
  15158. (defun org-find-entry-with-id (ident)
  15159. "Locate the entry that contains the ID property with exact value IDENT.
  15160. IDENT can be a string, a symbol or a number, this function will search for
  15161. the string representation of it.
  15162. Return the position where this entry starts, or nil if there is no such entry."
  15163. (let ((id (cond
  15164. ((stringp ident) ident)
  15165. ((symbol-name ident) (symbol-name ident))
  15166. ((numberp ident) (number-to-string ident))
  15167. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15168. (case-fold-search nil))
  15169. (save-excursion
  15170. (save-restriction
  15171. (widen)
  15172. (goto-char (point-min))
  15173. (when (re-search-forward
  15174. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15175. nil t)
  15176. (org-back-to-heading)
  15177. (point))))))
  15178. ;;; Column View
  15179. (defvar org-columns-overlays nil
  15180. "Holds the list of current column overlays.")
  15181. (defvar org-columns-current-fmt nil
  15182. "Local variable, holds the currently active column format.")
  15183. (defvar org-columns-current-fmt-compiled nil
  15184. "Local variable, holds the currently active column format.
  15185. This is the compiled version of the format.")
  15186. (defvar org-columns-current-widths nil
  15187. "Loval variable, holds the currently widths of fields.")
  15188. (defvar org-columns-current-maxwidths nil
  15189. "Loval variable, holds the currently active maximum column widths.")
  15190. (defvar org-columns-begin-marker (make-marker)
  15191. "Points to the position where last a column creation command was called.")
  15192. (defvar org-columns-top-level-marker (make-marker)
  15193. "Points to the position where current columns region starts.")
  15194. (defvar org-columns-map (make-sparse-keymap)
  15195. "The keymap valid in column display.")
  15196. (defun org-columns-content ()
  15197. "Switch to contents view while in columns view."
  15198. (interactive)
  15199. (org-overview)
  15200. (org-content))
  15201. (org-defkey org-columns-map "c" 'org-columns-content)
  15202. (org-defkey org-columns-map "o" 'org-overview)
  15203. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15204. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15205. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15206. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15207. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15208. (org-defkey org-columns-map "q" 'org-columns-quit)
  15209. (org-defkey org-columns-map "r" 'org-columns-redo)
  15210. (org-defkey org-columns-map "g" 'org-columns-redo)
  15211. (org-defkey org-columns-map [left] 'backward-char)
  15212. (org-defkey org-columns-map "\M-b" 'backward-char)
  15213. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15214. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15215. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15216. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15217. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15218. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15219. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15220. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15221. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15222. (org-defkey org-columns-map ">" 'org-columns-widen)
  15223. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15224. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15225. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15226. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15227. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15228. '("Column"
  15229. ["Edit property" org-columns-edit-value t]
  15230. ["Next allowed value" org-columns-next-allowed-value t]
  15231. ["Previous allowed value" org-columns-previous-allowed-value t]
  15232. ["Show full value" org-columns-show-value t]
  15233. ["Edit allowed values" org-columns-edit-allowed t]
  15234. "--"
  15235. ["Edit column attributes" org-columns-edit-attributes t]
  15236. ["Increase column width" org-columns-widen t]
  15237. ["Decrease column width" org-columns-narrow t]
  15238. "--"
  15239. ["Move column right" org-columns-move-right t]
  15240. ["Move column left" org-columns-move-left t]
  15241. ["Add column" org-columns-new t]
  15242. ["Delete column" org-columns-delete t]
  15243. "--"
  15244. ["CONTENTS" org-columns-content t]
  15245. ["OVERVIEW" org-overview t]
  15246. ["Refresh columns display" org-columns-redo t]
  15247. "--"
  15248. ["Open link" org-columns-open-link t]
  15249. "--"
  15250. ["Quit" org-columns-quit t]))
  15251. (defun org-columns-new-overlay (beg end &optional string face)
  15252. "Create a new column overlay and add it to the list."
  15253. (let ((ov (org-make-overlay beg end)))
  15254. (org-overlay-put ov 'face (or face 'secondary-selection))
  15255. (org-overlay-display ov string face)
  15256. (push ov org-columns-overlays)
  15257. ov))
  15258. (defun org-columns-display-here (&optional props)
  15259. "Overlay the current line with column display."
  15260. (interactive)
  15261. (let* ((fmt org-columns-current-fmt-compiled)
  15262. (beg (point-at-bol))
  15263. (level-face (save-excursion
  15264. (beginning-of-line 1)
  15265. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15266. (org-get-level-face 2))))
  15267. (color (list :foreground
  15268. (face-attribute (or level-face 'default) :foreground)))
  15269. props pom property ass width f string ov column val modval)
  15270. ;; Check if the entry is in another buffer.
  15271. (unless props
  15272. (if (eq major-mode 'org-agenda-mode)
  15273. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15274. (get-text-property (point) 'org-marker))
  15275. props (if pom (org-entry-properties pom) nil))
  15276. (setq props (org-entry-properties nil))))
  15277. ;; Walk the format
  15278. (while (setq column (pop fmt))
  15279. (setq property (car column)
  15280. ass (if (equal property "ITEM")
  15281. (cons "ITEM"
  15282. (save-match-data
  15283. (org-no-properties
  15284. (org-remove-tabs
  15285. (buffer-substring-no-properties
  15286. (point-at-bol) (point-at-eol))))))
  15287. (assoc property props))
  15288. width (or (cdr (assoc property org-columns-current-maxwidths))
  15289. (nth 2 column)
  15290. (length property))
  15291. f (format "%%-%d.%ds | " width width)
  15292. val (or (cdr ass) "")
  15293. modval (if (equal property "ITEM")
  15294. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15295. string (format f (or modval val)))
  15296. ;; Create the overlay
  15297. (org-unmodified
  15298. (setq ov (org-columns-new-overlay
  15299. beg (setq beg (1+ beg)) string
  15300. (list color 'org-column)))
  15301. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15302. (org-overlay-put ov 'keymap org-columns-map)
  15303. (org-overlay-put ov 'org-columns-key property)
  15304. (org-overlay-put ov 'org-columns-value (cdr ass))
  15305. (org-overlay-put ov 'org-columns-value-modified modval)
  15306. (org-overlay-put ov 'org-columns-pom pom)
  15307. (org-overlay-put ov 'org-columns-format f))
  15308. (if (or (not (char-after beg))
  15309. (equal (char-after beg) ?\n))
  15310. (let ((inhibit-read-only t))
  15311. (save-excursion
  15312. (goto-char beg)
  15313. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15314. ;; Make the rest of the line disappear.
  15315. (org-unmodified
  15316. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15317. (org-overlay-put ov 'invisible t)
  15318. (org-overlay-put ov 'keymap org-columns-map)
  15319. (org-overlay-put ov 'intangible t)
  15320. (push ov org-columns-overlays)
  15321. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15322. (org-overlay-put ov 'keymap org-columns-map)
  15323. (push ov org-columns-overlays)
  15324. (let ((inhibit-read-only t))
  15325. (put-text-property (max (point-min) (1- (point-at-bol)))
  15326. (min (point-max) (1+ (point-at-eol)))
  15327. 'read-only "Type `e' to edit property")))))
  15328. (defvar org-previous-header-line-format nil
  15329. "The header line format before column view was turned on.")
  15330. (defvar org-columns-inhibit-recalculation nil
  15331. "Inhibit recomputing of columns on column view startup.")
  15332. (defvar header-line-format)
  15333. (defun org-columns-display-here-title ()
  15334. "Overlay the newline before the current line with the table title."
  15335. (interactive)
  15336. (let ((fmt org-columns-current-fmt-compiled)
  15337. string (title "")
  15338. property width f column str widths)
  15339. (while (setq column (pop fmt))
  15340. (setq property (car column)
  15341. str (or (nth 1 column) property)
  15342. width (or (cdr (assoc property org-columns-current-maxwidths))
  15343. (nth 2 column)
  15344. (length str))
  15345. widths (push width widths)
  15346. f (format "%%-%d.%ds | " width width)
  15347. string (format f str)
  15348. title (concat title string)))
  15349. (setq title (concat
  15350. (org-add-props " " nil 'display '(space :align-to 0))
  15351. (org-add-props title nil 'face '(:weight bold :underline t))))
  15352. (org-set-local 'org-previous-header-line-format header-line-format)
  15353. (org-set-local 'org-columns-current-widths (nreverse widths))
  15354. (setq header-line-format title)))
  15355. (defun org-columns-remove-overlays ()
  15356. "Remove all currently active column overlays."
  15357. (interactive)
  15358. (when (marker-buffer org-columns-begin-marker)
  15359. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15360. (when (local-variable-p 'org-previous-header-line-format)
  15361. (setq header-line-format org-previous-header-line-format)
  15362. (kill-local-variable 'org-previous-header-line-format))
  15363. (move-marker org-columns-begin-marker nil)
  15364. (move-marker org-columns-top-level-marker nil)
  15365. (org-unmodified
  15366. (mapc 'org-delete-overlay org-columns-overlays)
  15367. (setq org-columns-overlays nil)
  15368. (let ((inhibit-read-only t))
  15369. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15370. (defun org-columns-cleanup-item (item fmt)
  15371. "Remove from ITEM what is a column in the format FMT."
  15372. (if (not org-complex-heading-regexp)
  15373. item
  15374. (when (string-match org-complex-heading-regexp item)
  15375. (concat
  15376. (org-add-props (concat (match-string 1 item) " ") nil
  15377. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15378. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15379. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15380. " " (match-string 4 item)
  15381. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15382. (defun org-columns-show-value ()
  15383. "Show the full value of the property."
  15384. (interactive)
  15385. (let ((value (get-char-property (point) 'org-columns-value)))
  15386. (message "Value is: %s" (or value ""))))
  15387. (defun org-columns-quit ()
  15388. "Remove the column overlays and in this way exit column editing."
  15389. (interactive)
  15390. (org-unmodified
  15391. (org-columns-remove-overlays)
  15392. (let ((inhibit-read-only t))
  15393. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15394. (when (eq major-mode 'org-agenda-mode)
  15395. (message
  15396. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15397. (defun org-columns-check-computed ()
  15398. "Check if this column value is computed.
  15399. If yes, throw an error indicating that changing it does not make sense."
  15400. (let ((val (get-char-property (point) 'org-columns-value)))
  15401. (when (and (stringp val)
  15402. (get-char-property 0 'org-computed val))
  15403. (error "This value is computed from the entry's children"))))
  15404. (defun org-columns-todo (&optional arg)
  15405. "Change the TODO state during column view."
  15406. (interactive "P")
  15407. (org-columns-edit-value "TODO"))
  15408. (defun org-columns-set-tags-or-toggle (&optional arg)
  15409. "Toggle checkbox at point, or set tags for current headline."
  15410. (interactive "P")
  15411. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15412. (get-char-property (point) 'org-columns-value))
  15413. (org-columns-next-allowed-value)
  15414. (org-columns-edit-value "TAGS")))
  15415. (defun org-columns-edit-value (&optional key)
  15416. "Edit the value of the property at point in column view.
  15417. Where possible, use the standard interface for changing this line."
  15418. (interactive)
  15419. (org-columns-check-computed)
  15420. (let* ((external-key key)
  15421. (col (current-column))
  15422. (key (or key (get-char-property (point) 'org-columns-key)))
  15423. (value (get-char-property (point) 'org-columns-value))
  15424. (bol (point-at-bol)) (eol (point-at-eol))
  15425. (pom (or (get-text-property bol 'org-hd-marker)
  15426. (point))) ; keep despite of compiler waring
  15427. (line-overlays
  15428. (delq nil (mapcar (lambda (x)
  15429. (and (eq (overlay-buffer x) (current-buffer))
  15430. (>= (overlay-start x) bol)
  15431. (<= (overlay-start x) eol)
  15432. x))
  15433. org-columns-overlays)))
  15434. nval eval allowed)
  15435. (cond
  15436. ((equal key "CLOCKSUM")
  15437. (error "This special column cannot be edited"))
  15438. ((equal key "ITEM")
  15439. (setq eval '(org-with-point-at pom
  15440. (org-edit-headline))))
  15441. ((equal key "TODO")
  15442. (setq eval '(org-with-point-at pom
  15443. (let ((current-prefix-arg
  15444. (if external-key current-prefix-arg '(4))))
  15445. (call-interactively 'org-todo)))))
  15446. ((equal key "PRIORITY")
  15447. (setq eval '(org-with-point-at pom
  15448. (call-interactively 'org-priority))))
  15449. ((equal key "TAGS")
  15450. (setq eval '(org-with-point-at pom
  15451. (let ((org-fast-tag-selection-single-key
  15452. (if (eq org-fast-tag-selection-single-key 'expert)
  15453. t org-fast-tag-selection-single-key)))
  15454. (call-interactively 'org-set-tags)))))
  15455. ((equal key "DEADLINE")
  15456. (setq eval '(org-with-point-at pom
  15457. (call-interactively 'org-deadline))))
  15458. ((equal key "SCHEDULED")
  15459. (setq eval '(org-with-point-at pom
  15460. (call-interactively 'org-schedule))))
  15461. (t
  15462. (setq allowed (org-property-get-allowed-values pom key 'table))
  15463. (if allowed
  15464. (setq nval (completing-read "Value: " allowed nil t))
  15465. (setq nval (read-string "Edit: " value)))
  15466. (setq nval (org-trim nval))
  15467. (when (not (equal nval value))
  15468. (setq eval '(org-entry-put pom key nval)))))
  15469. (when eval
  15470. (let ((inhibit-read-only t))
  15471. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15472. (unwind-protect
  15473. (progn
  15474. (setq org-columns-overlays
  15475. (org-delete-all line-overlays org-columns-overlays))
  15476. (mapc 'org-delete-overlay line-overlays)
  15477. (org-columns-eval eval))
  15478. (org-columns-display-here))))
  15479. (move-to-column col)
  15480. (if (and (org-mode-p)
  15481. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15482. (org-columns-update key))))
  15483. (defun org-edit-headline () ; FIXME: this is not columns specific
  15484. "Edit the current headline, the part without TODO keyword, TAGS."
  15485. (org-back-to-heading)
  15486. (when (looking-at org-todo-line-regexp)
  15487. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15488. (txt (match-string 3))
  15489. (post "")
  15490. txt2)
  15491. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15492. (setq post (match-string 0 txt)
  15493. txt (substring txt 0 (match-beginning 0))))
  15494. (setq txt2 (read-string "Edit: " txt))
  15495. (when (not (equal txt txt2))
  15496. (beginning-of-line 1)
  15497. (insert pre txt2 post)
  15498. (delete-region (point) (point-at-eol))
  15499. (org-set-tags nil t)))))
  15500. (defun org-columns-edit-allowed ()
  15501. "Edit the list of allowed values for the current property."
  15502. (interactive)
  15503. (let* ((key (get-char-property (point) 'org-columns-key))
  15504. (key1 (concat key "_ALL"))
  15505. (allowed (org-entry-get (point) key1 t))
  15506. nval)
  15507. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15508. (setq nval (read-string "Allowed: " allowed))
  15509. (org-entry-put
  15510. (cond ((marker-position org-entry-property-inherited-from)
  15511. org-entry-property-inherited-from)
  15512. ((marker-position org-columns-top-level-marker)
  15513. org-columns-top-level-marker))
  15514. key1 nval)))
  15515. (defmacro org-no-warnings (&rest body)
  15516. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15517. (defun org-columns-eval (form)
  15518. (let (hidep)
  15519. (save-excursion
  15520. (beginning-of-line 1)
  15521. ;; `next-line' is needed here, because it skips invisible line.
  15522. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15523. (setq hidep (org-on-heading-p 1)))
  15524. (eval form)
  15525. (and hidep (hide-entry))))
  15526. (defun org-columns-previous-allowed-value ()
  15527. "Switch to the previous allowed value for this column."
  15528. (interactive)
  15529. (org-columns-next-allowed-value t))
  15530. (defun org-columns-next-allowed-value (&optional previous)
  15531. "Switch to the next allowed value for this column."
  15532. (interactive)
  15533. (org-columns-check-computed)
  15534. (let* ((col (current-column))
  15535. (key (get-char-property (point) 'org-columns-key))
  15536. (value (get-char-property (point) 'org-columns-value))
  15537. (bol (point-at-bol)) (eol (point-at-eol))
  15538. (pom (or (get-text-property bol 'org-hd-marker)
  15539. (point))) ; keep despite of compiler waring
  15540. (line-overlays
  15541. (delq nil (mapcar (lambda (x)
  15542. (and (eq (overlay-buffer x) (current-buffer))
  15543. (>= (overlay-start x) bol)
  15544. (<= (overlay-start x) eol)
  15545. x))
  15546. org-columns-overlays)))
  15547. (allowed (or (org-property-get-allowed-values pom key)
  15548. (and (memq
  15549. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15550. '(checkbox checkbox-n-of-m checkbox-percent))
  15551. '("[ ]" "[X]"))))
  15552. nval)
  15553. (when (equal key "ITEM")
  15554. (error "Cannot edit item headline from here"))
  15555. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15556. (error "Allowed values for this property have not been defined"))
  15557. (if (member key '("SCHEDULED" "DEADLINE"))
  15558. (setq nval (if previous 'earlier 'later))
  15559. (if previous (setq allowed (reverse allowed)))
  15560. (if (member value allowed)
  15561. (setq nval (car (cdr (member value allowed)))))
  15562. (setq nval (or nval (car allowed)))
  15563. (if (equal nval value)
  15564. (error "Only one allowed value for this property")))
  15565. (let ((inhibit-read-only t))
  15566. (remove-text-properties (1- bol) eol '(read-only t))
  15567. (unwind-protect
  15568. (progn
  15569. (setq org-columns-overlays
  15570. (org-delete-all line-overlays org-columns-overlays))
  15571. (mapc 'org-delete-overlay line-overlays)
  15572. (org-columns-eval '(org-entry-put pom key nval)))
  15573. (org-columns-display-here)))
  15574. (move-to-column col)
  15575. (if (and (org-mode-p)
  15576. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15577. (org-columns-update key))))
  15578. (defun org-verify-version (task)
  15579. (cond
  15580. ((eq task 'columns)
  15581. (if (or (featurep 'xemacs)
  15582. (< emacs-major-version 22))
  15583. (error "Emacs 22 is required for the columns feature")))))
  15584. (defun org-columns-open-link (&optional arg)
  15585. (interactive "P")
  15586. (let ((value (get-char-property (point) 'org-columns-value)))
  15587. (org-open-link-from-string value arg)))
  15588. (defun org-open-link-from-string (s &optional arg)
  15589. "Open a link in the string S, as if it was in Org-mode."
  15590. (interactive)
  15591. (with-temp-buffer
  15592. (let ((org-inhibit-startup t))
  15593. (org-mode)
  15594. (insert s)
  15595. (goto-char (point-min))
  15596. (org-open-at-point arg))))
  15597. (defun org-columns-get-format-and-top-level ()
  15598. (let (fmt)
  15599. (when (condition-case nil (org-back-to-heading) (error nil))
  15600. (move-marker org-entry-property-inherited-from nil)
  15601. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15602. (setq fmt (or fmt org-columns-default-format))
  15603. (org-set-local 'org-columns-current-fmt fmt)
  15604. (org-columns-compile-format fmt)
  15605. (if (marker-position org-entry-property-inherited-from)
  15606. (move-marker org-columns-top-level-marker
  15607. org-entry-property-inherited-from)
  15608. (move-marker org-columns-top-level-marker (point)))
  15609. fmt))
  15610. (defun org-columns ()
  15611. "Turn on column view on an org-mode file."
  15612. (interactive)
  15613. (org-verify-version 'columns)
  15614. (org-columns-remove-overlays)
  15615. (move-marker org-columns-begin-marker (point))
  15616. (let (beg end fmt cache maxwidths)
  15617. (setq fmt (org-columns-get-format-and-top-level))
  15618. (save-excursion
  15619. (goto-char org-columns-top-level-marker)
  15620. (setq beg (point))
  15621. (unless org-columns-inhibit-recalculation
  15622. (org-columns-compute-all))
  15623. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15624. (point-max)))
  15625. ;; Get and cache the properties
  15626. (goto-char beg)
  15627. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15628. (save-excursion
  15629. (save-restriction
  15630. (narrow-to-region beg end)
  15631. (org-clock-sum))))
  15632. (while (re-search-forward (concat "^" outline-regexp) end t)
  15633. (push (cons (org-current-line) (org-entry-properties)) cache))
  15634. (when cache
  15635. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15636. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15637. (org-columns-display-here-title)
  15638. (mapc (lambda (x)
  15639. (goto-line (car x))
  15640. (org-columns-display-here (cdr x)))
  15641. cache)))))
  15642. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15643. "Insert a new column, to the leeft o the current column."
  15644. (interactive)
  15645. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15646. cell)
  15647. (setq prop (completing-read
  15648. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15649. nil nil prop))
  15650. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15651. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15652. (if (string-match "\\S-" width)
  15653. (setq width (string-to-number width))
  15654. (setq width nil))
  15655. (setq fmt (completing-read "Summary [none]: "
  15656. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15657. nil t))
  15658. (if (string-match "\\S-" fmt)
  15659. (setq fmt (intern fmt))
  15660. (setq fmt nil))
  15661. (if (eq fmt 'none) (setq fmt nil))
  15662. (if editp
  15663. (progn
  15664. (setcar editp prop)
  15665. (setcdr editp (list title width nil fmt)))
  15666. (setq cell (nthcdr (1- (current-column))
  15667. org-columns-current-fmt-compiled))
  15668. (setcdr cell (cons (list prop title width nil fmt)
  15669. (cdr cell))))
  15670. (org-columns-store-format)
  15671. (org-columns-redo)))
  15672. (defun org-columns-delete ()
  15673. "Delete the column at point from columns view."
  15674. (interactive)
  15675. (let* ((n (current-column))
  15676. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15677. (when (y-or-n-p
  15678. (format "Are you sure you want to remove column \"%s\"? " title))
  15679. (setq org-columns-current-fmt-compiled
  15680. (delq (nth n org-columns-current-fmt-compiled)
  15681. org-columns-current-fmt-compiled))
  15682. (org-columns-store-format)
  15683. (org-columns-redo)
  15684. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15685. (backward-char 1)))))
  15686. (defun org-columns-edit-attributes ()
  15687. "Edit the attributes of the current column."
  15688. (interactive)
  15689. (let* ((n (current-column))
  15690. (info (nth n org-columns-current-fmt-compiled)))
  15691. (apply 'org-columns-new info)))
  15692. (defun org-columns-widen (arg)
  15693. "Make the column wider by ARG characters."
  15694. (interactive "p")
  15695. (let* ((n (current-column))
  15696. (entry (nth n org-columns-current-fmt-compiled))
  15697. (width (or (nth 2 entry)
  15698. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15699. (setq width (max 1 (+ width arg)))
  15700. (setcar (nthcdr 2 entry) width)
  15701. (org-columns-store-format)
  15702. (org-columns-redo)))
  15703. (defun org-columns-narrow (arg)
  15704. "Make the column nrrower by ARG characters."
  15705. (interactive "p")
  15706. (org-columns-widen (- arg)))
  15707. (defun org-columns-move-right ()
  15708. "Swap this column with the one to the right."
  15709. (interactive)
  15710. (let* ((n (current-column))
  15711. (cell (nthcdr n org-columns-current-fmt-compiled))
  15712. e)
  15713. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15714. (error "Cannot shift this column further to the right"))
  15715. (setq e (car cell))
  15716. (setcar cell (car (cdr cell)))
  15717. (setcdr cell (cons e (cdr (cdr cell))))
  15718. (org-columns-store-format)
  15719. (org-columns-redo)
  15720. (forward-char 1)))
  15721. (defun org-columns-move-left ()
  15722. "Swap this column with the one to the left."
  15723. (interactive)
  15724. (let* ((n (current-column)))
  15725. (when (= n 0)
  15726. (error "Cannot shift this column further to the left"))
  15727. (backward-char 1)
  15728. (org-columns-move-right)
  15729. (backward-char 1)))
  15730. (defun org-columns-store-format ()
  15731. "Store the text version of the current columns format in appropriate place.
  15732. This is either in the COLUMNS property of the node starting the current column
  15733. display, or in the #+COLUMNS line of the current buffer."
  15734. (let (fmt (cnt 0))
  15735. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15736. (org-set-local 'org-columns-current-fmt fmt)
  15737. (if (marker-position org-columns-top-level-marker)
  15738. (save-excursion
  15739. (goto-char org-columns-top-level-marker)
  15740. (if (and (org-at-heading-p)
  15741. (org-entry-get nil "COLUMNS"))
  15742. (org-entry-put nil "COLUMNS" fmt)
  15743. (goto-char (point-min))
  15744. ;; Overwrite all #+COLUMNS lines....
  15745. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15746. (setq cnt (1+ cnt))
  15747. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15748. (unless (> cnt 0)
  15749. (goto-char (point-min))
  15750. (or (org-on-heading-p t) (outline-next-heading))
  15751. (let ((inhibit-read-only t))
  15752. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15753. (org-set-local 'org-columns-default-format fmt))))))
  15754. (defvar org-overriding-columns-format nil
  15755. "When set, overrides any other definition.")
  15756. (defvar org-agenda-view-columns-initially nil
  15757. "When set, switch to columns view immediately after creating the agenda.")
  15758. (defun org-agenda-columns ()
  15759. "Turn on column view in the agenda."
  15760. (interactive)
  15761. (org-verify-version 'columns)
  15762. (org-columns-remove-overlays)
  15763. (move-marker org-columns-begin-marker (point))
  15764. (let (fmt cache maxwidths m)
  15765. (cond
  15766. ((and (local-variable-p 'org-overriding-columns-format)
  15767. org-overriding-columns-format)
  15768. (setq fmt org-overriding-columns-format))
  15769. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15770. (setq fmt (org-entry-get m "COLUMNS" t)))
  15771. ((and (boundp 'org-columns-current-fmt)
  15772. (local-variable-p 'org-columns-current-fmt)
  15773. org-columns-current-fmt)
  15774. (setq fmt org-columns-current-fmt))
  15775. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15776. (setq m (get-text-property m 'org-hd-marker))
  15777. (setq fmt (org-entry-get m "COLUMNS" t))))
  15778. (setq fmt (or fmt org-columns-default-format))
  15779. (org-set-local 'org-columns-current-fmt fmt)
  15780. (org-columns-compile-format fmt)
  15781. (save-excursion
  15782. ;; Get and cache the properties
  15783. (goto-char (point-min))
  15784. (while (not (eobp))
  15785. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15786. (get-text-property (point) 'org-marker)))
  15787. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15788. (beginning-of-line 2))
  15789. (when cache
  15790. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15791. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15792. (org-columns-display-here-title)
  15793. (mapc (lambda (x)
  15794. (goto-line (car x))
  15795. (org-columns-display-here (cdr x)))
  15796. cache)))))
  15797. (defun org-columns-get-autowidth-alist (s cache)
  15798. "Derive the maximum column widths from the format and the cache."
  15799. (let ((start 0) rtn)
  15800. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15801. (push (cons (match-string 1 s) 1) rtn)
  15802. (setq start (match-end 0)))
  15803. (mapc (lambda (x)
  15804. (setcdr x (apply 'max
  15805. (mapcar
  15806. (lambda (y)
  15807. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15808. cache))))
  15809. rtn)
  15810. rtn))
  15811. (defun org-columns-compute-all ()
  15812. "Compute all columns that have operators defined."
  15813. (org-unmodified
  15814. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15815. (let ((columns org-columns-current-fmt-compiled) col)
  15816. (while (setq col (pop columns))
  15817. (when (nth 3 col)
  15818. (save-excursion
  15819. (org-columns-compute (car col)))))))
  15820. (defun org-columns-update (property)
  15821. "Recompute PROPERTY, and update the columns display for it."
  15822. (org-columns-compute property)
  15823. (let (fmt val pos)
  15824. (save-excursion
  15825. (mapc (lambda (ov)
  15826. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15827. (setq pos (org-overlay-start ov))
  15828. (goto-char pos)
  15829. (when (setq val (cdr (assoc property
  15830. (get-text-property
  15831. (point-at-bol) 'org-summaries))))
  15832. (setq fmt (org-overlay-get ov 'org-columns-format))
  15833. (org-overlay-put ov 'org-columns-value val)
  15834. (org-overlay-put ov 'display (format fmt val)))))
  15835. org-columns-overlays))))
  15836. (defun org-columns-compute (property)
  15837. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15838. (interactive)
  15839. (let* ((re (concat "^" outline-regexp))
  15840. (lmax 30) ; Does anyone use deeper levels???
  15841. (lsum (make-vector lmax 0))
  15842. (lflag (make-vector lmax nil))
  15843. (level 0)
  15844. (ass (assoc property org-columns-current-fmt-compiled))
  15845. (format (nth 4 ass))
  15846. (printf (nth 5 ass))
  15847. (beg org-columns-top-level-marker)
  15848. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15849. (save-excursion
  15850. ;; Find the region to compute
  15851. (goto-char beg)
  15852. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15853. (goto-char end)
  15854. ;; Walk the tree from the back and do the computations
  15855. (while (re-search-backward re beg t)
  15856. (setq sumpos (match-beginning 0)
  15857. last-level level
  15858. level (org-outline-level)
  15859. val (org-entry-get nil property)
  15860. valflag (and val (string-match "\\S-" val)))
  15861. (cond
  15862. ((< level last-level)
  15863. ;; put the sum of lower levels here as a property
  15864. (setq sum (aref lsum last-level) ; current sum
  15865. flag (aref lflag last-level) ; any valid entries from children?
  15866. str (org-column-number-to-string sum format printf)
  15867. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15868. useval (if flag str1 (if valflag val ""))
  15869. sum-alist (get-text-property sumpos 'org-summaries))
  15870. (if (assoc property sum-alist)
  15871. (setcdr (assoc property sum-alist) useval)
  15872. (push (cons property useval) sum-alist)
  15873. (org-unmodified
  15874. (add-text-properties sumpos (1+ sumpos)
  15875. (list 'org-summaries sum-alist))))
  15876. (when val
  15877. (org-entry-put nil property (if flag str val)))
  15878. ;; add current to current level accumulator
  15879. (when (or flag valflag)
  15880. (aset lsum level (+ (aref lsum level)
  15881. (if flag sum (org-column-string-to-number
  15882. (if flag str val) format))))
  15883. (aset lflag level t))
  15884. ;; clear accumulators for deeper levels
  15885. (loop for l from (1+ level) to (1- lmax) do
  15886. (aset lsum l 0)
  15887. (aset lflag l nil)))
  15888. ((>= level last-level)
  15889. ;; add what we have here to the accumulator for this level
  15890. (aset lsum level (+ (aref lsum level)
  15891. (org-column-string-to-number (or val "0") format)))
  15892. (and valflag (aset lflag level t)))
  15893. (t (error "This should not happen")))))))
  15894. (defun org-columns-redo ()
  15895. "Construct the column display again."
  15896. (interactive)
  15897. (message "Recomputing columns...")
  15898. (save-excursion
  15899. (if (marker-position org-columns-begin-marker)
  15900. (goto-char org-columns-begin-marker))
  15901. (org-columns-remove-overlays)
  15902. (if (org-mode-p)
  15903. (call-interactively 'org-columns)
  15904. (call-interactively 'org-agenda-columns)))
  15905. (message "Recomputing columns...done"))
  15906. (defun org-columns-not-in-agenda ()
  15907. (if (eq major-mode 'org-agenda-mode)
  15908. (error "This command is only allowed in Org-mode buffers")))
  15909. (defun org-string-to-number (s)
  15910. "Convert string to number, and interpret hh:mm:ss."
  15911. (if (not (string-match ":" s))
  15912. (string-to-number s)
  15913. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15914. (while l
  15915. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15916. sum)))
  15917. (defun org-column-number-to-string (n fmt &optional printf)
  15918. "Convert a computed column number to a string value, according to FMT."
  15919. (cond
  15920. ((eq fmt 'add_times)
  15921. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15922. (format "%d:%02d" h m)))
  15923. ((eq fmt 'checkbox)
  15924. (cond ((= n (floor n)) "[X]")
  15925. ((> n 1.) "[-]")
  15926. (t "[ ]")))
  15927. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15928. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15929. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15930. (printf (format printf n))
  15931. ((eq fmt 'currency)
  15932. (format "%.2f" n))
  15933. (t (number-to-string n))))
  15934. (defun org-nofm-to-completion (n m &optional percent)
  15935. (if (not percent)
  15936. (format "[%d/%d]" n m)
  15937. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15938. (defun org-column-string-to-number (s fmt)
  15939. "Convert a column value to a number that can be used for column computing."
  15940. (cond
  15941. ((string-match ":" s)
  15942. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15943. (while l
  15944. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15945. sum))
  15946. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15947. (if (equal s "[X]") 1. 0.000001))
  15948. (t (string-to-number s))))
  15949. (defun org-columns-uncompile-format (cfmt)
  15950. "Turn the compiled columns format back into a string representation."
  15951. (let ((rtn "") e s prop title op width fmt printf)
  15952. (while (setq e (pop cfmt))
  15953. (setq prop (car e)
  15954. title (nth 1 e)
  15955. width (nth 2 e)
  15956. op (nth 3 e)
  15957. fmt (nth 4 e)
  15958. printf (nth 5 e))
  15959. (cond
  15960. ((eq fmt 'add_times) (setq op ":"))
  15961. ((eq fmt 'checkbox) (setq op "X"))
  15962. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15963. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15964. ((eq fmt 'add_numbers) (setq op "+"))
  15965. ((eq fmt 'currency) (setq op "$")))
  15966. (if (and op printf) (setq op (concat op ";" printf)))
  15967. (if (equal title prop) (setq title nil))
  15968. (setq s (concat "%" (if width (number-to-string width))
  15969. prop
  15970. (if title (concat "(" title ")"))
  15971. (if op (concat "{" op "}"))))
  15972. (setq rtn (concat rtn " " s)))
  15973. (org-trim rtn)))
  15974. (defun org-columns-compile-format (fmt)
  15975. "Turn a column format string into an alist of specifications.
  15976. The alist has one entry for each column in the format. The elements of
  15977. that list are:
  15978. property the property
  15979. title the title field for the columns
  15980. width the column width in characters, can be nil for automatic
  15981. operator the operator if any
  15982. format the output format for computed results, derived from operator
  15983. printf a printf format for computed values"
  15984. (let ((start 0) width prop title op f printf)
  15985. (setq org-columns-current-fmt-compiled nil)
  15986. (while (string-match
  15987. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15988. fmt start)
  15989. (setq start (match-end 0)
  15990. width (match-string 1 fmt)
  15991. prop (match-string 2 fmt)
  15992. title (or (match-string 3 fmt) prop)
  15993. op (match-string 4 fmt)
  15994. f nil
  15995. printf nil)
  15996. (if width (setq width (string-to-number width)))
  15997. (when (and op (string-match ";" op))
  15998. (setq printf (substring op (match-end 0))
  15999. op (substring op 0 (match-beginning 0))))
  16000. (cond
  16001. ((equal op "+") (setq f 'add_numbers))
  16002. ((equal op "$") (setq f 'currency))
  16003. ((equal op ":") (setq f 'add_times))
  16004. ((equal op "X") (setq f 'checkbox))
  16005. ((equal op "X/") (setq f 'checkbox-n-of-m))
  16006. ((equal op "X%") (setq f 'checkbox-percent))
  16007. )
  16008. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  16009. (setq org-columns-current-fmt-compiled
  16010. (nreverse org-columns-current-fmt-compiled))))
  16011. ;;; Dynamic block for Column view
  16012. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  16013. "Get the column view of the current buffer or subtree.
  16014. The first optional argument MAXLEVEL sets the level limit. A
  16015. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  16016. empty rows, an empty row being one where all the column view
  16017. specifiers except ITEM are empty. This function returns a list
  16018. containing the title row and all other rows. Each row is a list
  16019. of fields."
  16020. (save-excursion
  16021. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  16022. (n (length title)) row tbl)
  16023. (goto-char (point-min))
  16024. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  16025. (or (null maxlevel)
  16026. (>= maxlevel
  16027. (if org-odd-levels-only
  16028. (/ (1+ (length (match-string 1))) 2)
  16029. (length (match-string 1))))))
  16030. (when (get-char-property (match-beginning 0) 'org-columns-key)
  16031. (setq row nil)
  16032. (loop for i from 0 to (1- n) do
  16033. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  16034. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  16035. "")
  16036. row))
  16037. (setq row (nreverse row))
  16038. (unless (and skip-empty-rows
  16039. (eq 1 (length (delete "" (delete-dups row)))))
  16040. (push row tbl))))
  16041. (append (list title 'hline) (nreverse tbl)))))
  16042. (defun org-dblock-write:columnview (params)
  16043. "Write the column view table.
  16044. PARAMS is a property list of parameters:
  16045. :width enforce same column widths with <N> specifiers.
  16046. :id the :ID: property of the entry where the columns view
  16047. should be built, as a string. When `local', call locally.
  16048. When `global' call column view with the cursor at the beginning
  16049. of the buffer (usually this means that the whole buffer switches
  16050. to column view).
  16051. :hlines When t, insert a hline before each item. When a number, insert
  16052. a hline before each level <= that number.
  16053. :vlines When t, make each column a colgroup to enforce vertical lines.
  16054. :maxlevel When set to a number, don't capture headlines below this level.
  16055. :skip-empty-rows
  16056. When t, skip rows where all specifiers other than ITEM are empty."
  16057. (let ((pos (move-marker (make-marker) (point)))
  16058. (hlines (plist-get params :hlines))
  16059. (vlines (plist-get params :vlines))
  16060. (maxlevel (plist-get params :maxlevel))
  16061. (skip-empty-rows (plist-get params :skip-empty-rows))
  16062. tbl id idpos nfields tmp)
  16063. (save-excursion
  16064. (save-restriction
  16065. (when (setq id (plist-get params :id))
  16066. (cond ((not id) nil)
  16067. ((eq id 'global) (goto-char (point-min)))
  16068. ((eq id 'local) nil)
  16069. ((setq idpos (org-find-entry-with-id id))
  16070. (goto-char idpos))
  16071. (t (error "Cannot find entry with :ID: %s" id))))
  16072. (org-columns)
  16073. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  16074. (setq nfields (length (car tbl)))
  16075. (org-columns-quit)))
  16076. (goto-char pos)
  16077. (move-marker pos nil)
  16078. (when tbl
  16079. (when (plist-get params :hlines)
  16080. (setq tmp nil)
  16081. (while tbl
  16082. (if (eq (car tbl) 'hline)
  16083. (push (pop tbl) tmp)
  16084. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16085. (if (and (not (eq (car tmp) 'hline))
  16086. (or (eq hlines t)
  16087. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16088. (push 'hline tmp)))
  16089. (push (pop tbl) tmp)))
  16090. (setq tbl (nreverse tmp)))
  16091. (when vlines
  16092. (setq tbl (mapcar (lambda (x)
  16093. (if (eq 'hline x) x (cons "" x)))
  16094. tbl))
  16095. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16096. (setq pos (point))
  16097. (insert (org-listtable-to-string tbl))
  16098. (when (plist-get params :width)
  16099. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16100. org-columns-current-widths "|")))
  16101. (goto-char pos)
  16102. (org-table-align))))
  16103. (defun org-listtable-to-string (tbl)
  16104. "Convert a listtable TBL to a string that contains the Org-mode table.
  16105. The table still need to be alligned. The resulting string has no leading
  16106. and tailing newline characters."
  16107. (mapconcat
  16108. (lambda (x)
  16109. (cond
  16110. ((listp x)
  16111. (concat "|" (mapconcat 'identity x "|") "|"))
  16112. ((eq x 'hline) "|-|")
  16113. (t (error "Garbage in listtable: %s" x))))
  16114. tbl "\n"))
  16115. (defun org-insert-columns-dblock ()
  16116. "Create a dynamic block capturing a column view table."
  16117. (interactive)
  16118. (let ((defaults '(:name "columnview" :hlines 1))
  16119. (id (completing-read
  16120. "Capture columns (local, global, entry with :ID: property) [local]: "
  16121. (append '(("global") ("local"))
  16122. (mapcar 'list (org-property-values "ID"))))))
  16123. (if (equal id "") (setq id 'local))
  16124. (if (equal id "global") (setq id 'global))
  16125. (setq defaults (append defaults (list :id id)))
  16126. (org-create-dblock defaults)
  16127. (org-update-dblock)))
  16128. ;;;; Timestamps
  16129. (defvar org-last-changed-timestamp nil)
  16130. (defvar org-time-was-given) ; dynamically scoped parameter
  16131. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16132. (defvar org-ts-what) ; dynamically scoped parameter
  16133. (defun org-time-stamp (arg)
  16134. "Prompt for a date/time and insert a time stamp.
  16135. If the user specifies a time like HH:MM, or if this command is called
  16136. with a prefix argument, the time stamp will contain date and time.
  16137. Otherwise, only the date will be included. All parts of a date not
  16138. specified by the user will be filled in from the current date/time.
  16139. So if you press just return without typing anything, the time stamp
  16140. will represent the current date/time. If there is already a timestamp
  16141. at the cursor, it will be modified."
  16142. (interactive "P")
  16143. (let* ((ts nil)
  16144. (default-time
  16145. ;; Default time is either today, or, when entering a range,
  16146. ;; the range start.
  16147. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16148. (save-excursion
  16149. (re-search-backward
  16150. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16151. (- (point) 20) t)))
  16152. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16153. (current-time)))
  16154. (default-input (and ts (org-get-compact-tod ts)))
  16155. org-time-was-given org-end-time-was-given time)
  16156. (cond
  16157. ((and (org-at-timestamp-p)
  16158. (eq last-command 'org-time-stamp)
  16159. (eq this-command 'org-time-stamp))
  16160. (insert "--")
  16161. (setq time (let ((this-command this-command))
  16162. (org-read-date arg 'totime nil nil default-time default-input)))
  16163. (org-insert-time-stamp time (or org-time-was-given arg)))
  16164. ((org-at-timestamp-p)
  16165. (setq time (let ((this-command this-command))
  16166. (org-read-date arg 'totime nil nil default-time default-input)))
  16167. (when (org-at-timestamp-p) ; just to get the match data
  16168. (replace-match "")
  16169. (setq org-last-changed-timestamp
  16170. (org-insert-time-stamp
  16171. time (or org-time-was-given arg)
  16172. nil nil nil (list org-end-time-was-given))))
  16173. (message "Timestamp updated"))
  16174. (t
  16175. (setq time (let ((this-command this-command))
  16176. (org-read-date arg 'totime nil nil default-time default-input)))
  16177. (org-insert-time-stamp time (or org-time-was-given arg)
  16178. nil nil nil (list org-end-time-was-given))))))
  16179. ;; FIXME: can we use this for something else????
  16180. ;; like computing time differences?????
  16181. (defun org-get-compact-tod (s)
  16182. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16183. (let* ((t1 (match-string 1 s))
  16184. (h1 (string-to-number (match-string 2 s)))
  16185. (m1 (string-to-number (match-string 3 s)))
  16186. (t2 (and (match-end 4) (match-string 5 s)))
  16187. (h2 (and t2 (string-to-number (match-string 6 s))))
  16188. (m2 (and t2 (string-to-number (match-string 7 s))))
  16189. dh dm)
  16190. (if (not t2)
  16191. t1
  16192. (setq dh (- h2 h1) dm (- m2 m1))
  16193. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16194. (concat t1 "+" (number-to-string dh)
  16195. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16196. (defun org-time-stamp-inactive (&optional arg)
  16197. "Insert an inactive time stamp.
  16198. An inactive time stamp is enclosed in square brackets instead of angle
  16199. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16200. does not link to the calendar and cannot be changed with the S-cursor keys.
  16201. So these are more for recording a certain time/date."
  16202. (interactive "P")
  16203. (let (org-time-was-given org-end-time-was-given time)
  16204. (setq time (org-read-date arg 'totime))
  16205. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16206. nil nil (list org-end-time-was-given))))
  16207. (defvar org-date-ovl (org-make-overlay 1 1))
  16208. (org-overlay-put org-date-ovl 'face 'org-warning)
  16209. (org-detach-overlay org-date-ovl)
  16210. (defvar org-ans1) ; dynamically scoped parameter
  16211. (defvar org-ans2) ; dynamically scoped parameter
  16212. (defvar org-plain-time-of-day-regexp) ; defined below
  16213. (defvar org-read-date-overlay nil)
  16214. (defvar org-dcst nil) ; dynamically scoped
  16215. (defun org-read-date (&optional with-time to-time from-string prompt
  16216. default-time default-input)
  16217. "Read a date, possibly a time, and make things smooth for the user.
  16218. The prompt will suggest to enter an ISO date, but you can also enter anything
  16219. which will at least partially be understood by `parse-time-string'.
  16220. Unrecognized parts of the date will default to the current day, month, year,
  16221. hour and minute. If this command is called to replace a timestamp at point,
  16222. of to enter the second timestamp of a range, the default time is taken from the
  16223. existing stamp. For example,
  16224. 3-2-5 --> 2003-02-05
  16225. feb 15 --> currentyear-02-15
  16226. sep 12 9 --> 2009-09-12
  16227. 12:45 --> today 12:45
  16228. 22 sept 0:34 --> currentyear-09-22 0:34
  16229. 12 --> currentyear-currentmonth-12
  16230. Fri --> nearest Friday (today or later)
  16231. etc.
  16232. Furthermore you can specify a relative date by giving, as the *first* thing
  16233. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16234. change in days weeks, months, years.
  16235. With a single plus or minus, the date is relative to today. With a double
  16236. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16237. +4d --> four days from today
  16238. +4 --> same as above
  16239. +2w --> two weeks from today
  16240. ++5 --> five days from default date
  16241. The function understands only English month and weekday abbreviations,
  16242. but this can be configured with the variables `parse-time-months' and
  16243. `parse-time-weekdays'.
  16244. While prompting, a calendar is popped up - you can also select the
  16245. date with the mouse (button 1). The calendar shows a period of three
  16246. months. To scroll it to other months, use the keys `>' and `<'.
  16247. If you don't like the calendar, turn it off with
  16248. \(setq org-read-date-popup-calendar nil)
  16249. With optional argument TO-TIME, the date will immediately be converted
  16250. to an internal time.
  16251. With an optional argument WITH-TIME, the prompt will suggest to also
  16252. insert a time. Note that when WITH-TIME is not set, you can still
  16253. enter a time, and this function will inform the calling routine about
  16254. this change. The calling routine may then choose to change the format
  16255. used to insert the time stamp into the buffer to include the time.
  16256. With optional argument FROM-STRING, read from this string instead from
  16257. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16258. the time/date that is used for everything that is not specified by the
  16259. user."
  16260. (require 'parse-time)
  16261. (let* ((org-time-stamp-rounding-minutes
  16262. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  16263. (org-dcst org-display-custom-times)
  16264. (ct (org-current-time))
  16265. (def (or default-time ct))
  16266. (defdecode (decode-time def))
  16267. (dummy (progn
  16268. (when (< (nth 2 defdecode) org-extend-today-until)
  16269. (setcar (nthcdr 2 defdecode) -1)
  16270. (setcar (nthcdr 1 defdecode) 59)
  16271. (setq def (apply 'encode-time defdecode)
  16272. defdecode (decode-time def)))))
  16273. (calendar-move-hook nil)
  16274. (view-diary-entries-initially nil)
  16275. (view-calendar-holidays-initially nil)
  16276. (timestr (format-time-string
  16277. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16278. (prompt (concat (if prompt (concat prompt " ") "")
  16279. (format "Date+time [%s]: " timestr)))
  16280. ans (org-ans0 "") org-ans1 org-ans2 final)
  16281. (cond
  16282. (from-string (setq ans from-string))
  16283. (org-read-date-popup-calendar
  16284. (save-excursion
  16285. (save-window-excursion
  16286. (calendar)
  16287. (calendar-forward-day (- (time-to-days def)
  16288. (calendar-absolute-from-gregorian
  16289. (calendar-current-date))))
  16290. (org-eval-in-calendar nil t)
  16291. (let* ((old-map (current-local-map))
  16292. (map (copy-keymap calendar-mode-map))
  16293. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16294. (org-defkey map (kbd "RET") 'org-calendar-select)
  16295. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16296. 'org-calendar-select-mouse)
  16297. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16298. 'org-calendar-select-mouse)
  16299. (org-defkey minibuffer-local-map [(meta shift left)]
  16300. (lambda () (interactive)
  16301. (org-eval-in-calendar '(calendar-backward-month 1))))
  16302. (org-defkey minibuffer-local-map [(meta shift right)]
  16303. (lambda () (interactive)
  16304. (org-eval-in-calendar '(calendar-forward-month 1))))
  16305. (org-defkey minibuffer-local-map [(meta shift up)]
  16306. (lambda () (interactive)
  16307. (org-eval-in-calendar '(calendar-backward-year 1))))
  16308. (org-defkey minibuffer-local-map [(meta shift down)]
  16309. (lambda () (interactive)
  16310. (org-eval-in-calendar '(calendar-forward-year 1))))
  16311. (org-defkey minibuffer-local-map [(shift up)]
  16312. (lambda () (interactive)
  16313. (org-eval-in-calendar '(calendar-backward-week 1))))
  16314. (org-defkey minibuffer-local-map [(shift down)]
  16315. (lambda () (interactive)
  16316. (org-eval-in-calendar '(calendar-forward-week 1))))
  16317. (org-defkey minibuffer-local-map [(shift left)]
  16318. (lambda () (interactive)
  16319. (org-eval-in-calendar '(calendar-backward-day 1))))
  16320. (org-defkey minibuffer-local-map [(shift right)]
  16321. (lambda () (interactive)
  16322. (org-eval-in-calendar '(calendar-forward-day 1))))
  16323. (org-defkey minibuffer-local-map ">"
  16324. (lambda () (interactive)
  16325. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16326. (org-defkey minibuffer-local-map "<"
  16327. (lambda () (interactive)
  16328. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16329. (unwind-protect
  16330. (progn
  16331. (use-local-map map)
  16332. (add-hook 'post-command-hook 'org-read-date-display)
  16333. (setq org-ans0 (read-string prompt default-input nil nil))
  16334. ;; org-ans0: from prompt
  16335. ;; org-ans1: from mouse click
  16336. ;; org-ans2: from calendar motion
  16337. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16338. (remove-hook 'post-command-hook 'org-read-date-display)
  16339. (use-local-map old-map)
  16340. (when org-read-date-overlay
  16341. (org-delete-overlay org-read-date-overlay)
  16342. (setq org-read-date-overlay nil)))))))
  16343. (t ; Naked prompt only
  16344. (unwind-protect
  16345. (setq ans (read-string prompt default-input nil timestr))
  16346. (when org-read-date-overlay
  16347. (org-delete-overlay org-read-date-overlay)
  16348. (setq org-read-date-overlay nil)))))
  16349. (setq final (org-read-date-analyze ans def defdecode))
  16350. (if to-time
  16351. (apply 'encode-time final)
  16352. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16353. (format "%04d-%02d-%02d %02d:%02d"
  16354. (nth 5 final) (nth 4 final) (nth 3 final)
  16355. (nth 2 final) (nth 1 final))
  16356. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16357. (defvar def)
  16358. (defvar defdecode)
  16359. (defvar with-time)
  16360. (defun org-read-date-display ()
  16361. "Display the currrent date prompt interpretation in the minibuffer."
  16362. (when org-read-date-display-live
  16363. (when org-read-date-overlay
  16364. (org-delete-overlay org-read-date-overlay))
  16365. (let ((p (point)))
  16366. (end-of-line 1)
  16367. (while (not (equal (buffer-substring
  16368. (max (point-min) (- (point) 4)) (point))
  16369. " "))
  16370. (insert " "))
  16371. (goto-char p))
  16372. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16373. " " (or org-ans1 org-ans2)))
  16374. (org-end-time-was-given nil)
  16375. (f (org-read-date-analyze ans def defdecode))
  16376. (fmts (if org-dcst
  16377. org-time-stamp-custom-formats
  16378. org-time-stamp-formats))
  16379. (fmt (if (or with-time
  16380. (and (boundp 'org-time-was-given) org-time-was-given))
  16381. (cdr fmts)
  16382. (car fmts)))
  16383. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16384. (when (and org-end-time-was-given
  16385. (string-match org-plain-time-of-day-regexp txt))
  16386. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16387. org-end-time-was-given
  16388. (substring txt (match-end 0)))))
  16389. (setq org-read-date-overlay
  16390. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16391. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16392. (defun org-read-date-analyze (ans def defdecode)
  16393. "Analyze the combined answer of the date prompt."
  16394. ;; FIXME: cleanup and comment
  16395. (let (delta deltan deltaw deltadef year month day
  16396. hour minute second wday pm h2 m2 tl wday1)
  16397. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16398. (setq ans (replace-match "" t t ans)
  16399. deltan (car delta)
  16400. deltaw (nth 1 delta)
  16401. deltadef (nth 2 delta)))
  16402. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16403. (when (string-match
  16404. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16405. (setq year (if (match-end 2)
  16406. (string-to-number (match-string 2 ans))
  16407. (string-to-number (format-time-string "%Y")))
  16408. month (string-to-number (match-string 3 ans))
  16409. day (string-to-number (match-string 4 ans)))
  16410. (if (< year 100) (setq year (+ 2000 year)))
  16411. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16412. t nil ans)))
  16413. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16414. ;; If there is a time with am/pm, and *no* time without it, we convert
  16415. ;; so that matching will be successful.
  16416. (loop for i from 1 to 2 do ; twice, for end time as well
  16417. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16418. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16419. (setq hour (string-to-number (match-string 1 ans))
  16420. minute (if (match-end 3)
  16421. (string-to-number (match-string 3 ans))
  16422. 0)
  16423. pm (equal ?p
  16424. (string-to-char (downcase (match-string 4 ans)))))
  16425. (if (and (= hour 12) (not pm))
  16426. (setq hour 0)
  16427. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16428. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16429. t t ans))))
  16430. ;; Check if a time range is given as a duration
  16431. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16432. (setq hour (string-to-number (match-string 1 ans))
  16433. h2 (+ hour (string-to-number (match-string 3 ans)))
  16434. minute (string-to-number (match-string 2 ans))
  16435. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16436. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16437. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16438. ;; Check if there is a time range
  16439. (when (boundp 'org-end-time-was-given)
  16440. (setq org-time-was-given nil)
  16441. (when (and (string-match org-plain-time-of-day-regexp ans)
  16442. (match-end 8))
  16443. (setq org-end-time-was-given (match-string 8 ans))
  16444. (setq ans (concat (substring ans 0 (match-beginning 7))
  16445. (substring ans (match-end 7))))))
  16446. (setq tl (parse-time-string ans)
  16447. day (or (nth 3 tl) (nth 3 defdecode))
  16448. month (or (nth 4 tl)
  16449. (if (and org-read-date-prefer-future
  16450. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16451. (1+ (nth 4 defdecode))
  16452. (nth 4 defdecode)))
  16453. year (or (nth 5 tl)
  16454. (if (and org-read-date-prefer-future
  16455. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16456. (1+ (nth 5 defdecode))
  16457. (nth 5 defdecode)))
  16458. hour (or (nth 2 tl) (nth 2 defdecode))
  16459. minute (or (nth 1 tl) (nth 1 defdecode))
  16460. second (or (nth 0 tl) 0)
  16461. wday (nth 6 tl))
  16462. (when deltan
  16463. (unless deltadef
  16464. (let ((now (decode-time (current-time))))
  16465. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16466. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16467. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16468. ((equal deltaw "m") (setq month (+ month deltan)))
  16469. ((equal deltaw "y") (setq year (+ year deltan)))))
  16470. (when (and wday (not (nth 3 tl)))
  16471. ;; Weekday was given, but no day, so pick that day in the week
  16472. ;; on or after the derived date.
  16473. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16474. (unless (equal wday wday1)
  16475. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16476. (if (and (boundp 'org-time-was-given)
  16477. (nth 2 tl))
  16478. (setq org-time-was-given t))
  16479. (if (< year 100) (setq year (+ 2000 year)))
  16480. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16481. (list second minute hour day month year)))
  16482. (defvar parse-time-weekdays)
  16483. (defun org-read-date-get-relative (s today default)
  16484. "Check string S for special relative date string.
  16485. TODAY and DEFAULT are internal times, for today and for a default.
  16486. Return shift list (N what def-flag)
  16487. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16488. N is the number of WHATs to shift.
  16489. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16490. the DEFAULT date rather than TODAY."
  16491. (when (string-match
  16492. (concat
  16493. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16494. "\\([0-9]+\\)?"
  16495. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16496. "\\([ \t]\\|$\\)") s)
  16497. (let* ((dir (if (match-end 1)
  16498. (string-to-char (substring (match-string 1 s) -1))
  16499. ?+))
  16500. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16501. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16502. (what (if (match-end 3) (match-string 3 s) "d"))
  16503. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16504. (date (if rel default today))
  16505. (wday (nth 6 (decode-time date)))
  16506. delta)
  16507. (if wday1
  16508. (progn
  16509. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16510. (if (= dir ?-) (setq delta (- delta 7)))
  16511. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16512. (list delta "d" rel))
  16513. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16514. (defun org-eval-in-calendar (form &optional keepdate)
  16515. "Eval FORM in the calendar window and return to current window.
  16516. Also, store the cursor date in variable org-ans2."
  16517. (let ((sw (selected-window)))
  16518. (select-window (get-buffer-window "*Calendar*"))
  16519. (eval form)
  16520. (when (and (not keepdate) (calendar-cursor-to-date))
  16521. (let* ((date (calendar-cursor-to-date))
  16522. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16523. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16524. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16525. (select-window sw)))
  16526. ; ;; Update the prompt to show new default date
  16527. ; (save-excursion
  16528. ; (goto-char (point-min))
  16529. ; (when (and org-ans2
  16530. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16531. ; (get-text-property (match-end 0) 'field))
  16532. ; (let ((inhibit-read-only t))
  16533. ; (replace-match (concat "[" org-ans2 "]") t t)
  16534. ; (add-text-properties (point-min) (1+ (match-end 0))
  16535. ; (text-properties-at (1+ (point-min)))))))))
  16536. (defun org-calendar-select ()
  16537. "Return to `org-read-date' with the date currently selected.
  16538. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16539. (interactive)
  16540. (when (calendar-cursor-to-date)
  16541. (let* ((date (calendar-cursor-to-date))
  16542. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16543. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16544. (if (active-minibuffer-window) (exit-minibuffer))))
  16545. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16546. "Insert a date stamp for the date given by the internal TIME.
  16547. WITH-HM means, use the stamp format that includes the time of the day.
  16548. INACTIVE means use square brackets instead of angular ones, so that the
  16549. stamp will not contribute to the agenda.
  16550. PRE and POST are optional strings to be inserted before and after the
  16551. stamp.
  16552. The command returns the inserted time stamp."
  16553. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16554. stamp)
  16555. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16556. (insert-before-markers (or pre ""))
  16557. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16558. (when (listp extra)
  16559. (setq extra (car extra))
  16560. (if (and (stringp extra)
  16561. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16562. (setq extra (format "-%02d:%02d"
  16563. (string-to-number (match-string 1 extra))
  16564. (string-to-number (match-string 2 extra))))
  16565. (setq extra nil)))
  16566. (when extra
  16567. (backward-char 1)
  16568. (insert-before-markers extra)
  16569. (forward-char 1))
  16570. (insert-before-markers (or post ""))
  16571. stamp))
  16572. (defun org-toggle-time-stamp-overlays ()
  16573. "Toggle the use of custom time stamp formats."
  16574. (interactive)
  16575. (setq org-display-custom-times (not org-display-custom-times))
  16576. (unless org-display-custom-times
  16577. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16578. (while (setq p (next-single-property-change p 'display))
  16579. (if (and (get-text-property p 'display)
  16580. (eq (get-text-property p 'face) 'org-date))
  16581. (remove-text-properties
  16582. p (setq p (next-single-property-change p 'display))
  16583. '(display t))))
  16584. (set-buffer-modified-p bmp)))
  16585. (if (featurep 'xemacs)
  16586. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16587. (org-restart-font-lock)
  16588. (setq org-table-may-need-update t)
  16589. (if org-display-custom-times
  16590. (message "Time stamps are overlayed with custom format")
  16591. (message "Time stamp overlays removed")))
  16592. (defun org-display-custom-time (beg end)
  16593. "Overlay modified time stamp format over timestamp between BED and END."
  16594. (let* ((ts (buffer-substring beg end))
  16595. t1 w1 with-hm tf time str w2 (off 0))
  16596. (save-match-data
  16597. (setq t1 (org-parse-time-string ts t))
  16598. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16599. (setq off (- (match-end 0) (match-beginning 0)))))
  16600. (setq end (- end off))
  16601. (setq w1 (- end beg)
  16602. with-hm (and (nth 1 t1) (nth 2 t1))
  16603. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16604. time (org-fix-decoded-time t1)
  16605. str (org-add-props
  16606. (format-time-string
  16607. (substring tf 1 -1) (apply 'encode-time time))
  16608. nil 'mouse-face 'highlight)
  16609. w2 (length str))
  16610. (if (not (= w2 w1))
  16611. (add-text-properties (1+ beg) (+ 2 beg)
  16612. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16613. (if (featurep 'xemacs)
  16614. (progn
  16615. (put-text-property beg end 'invisible t)
  16616. (put-text-property beg end 'end-glyph (make-glyph str)))
  16617. (put-text-property beg end 'display str))))
  16618. (defun org-translate-time (string)
  16619. "Translate all timestamps in STRING to custom format.
  16620. But do this only if the variable `org-display-custom-times' is set."
  16621. (when org-display-custom-times
  16622. (save-match-data
  16623. (let* ((start 0)
  16624. (re org-ts-regexp-both)
  16625. t1 with-hm inactive tf time str beg end)
  16626. (while (setq start (string-match re string start))
  16627. (setq beg (match-beginning 0)
  16628. end (match-end 0)
  16629. t1 (save-match-data
  16630. (org-parse-time-string (substring string beg end) t))
  16631. with-hm (and (nth 1 t1) (nth 2 t1))
  16632. inactive (equal (substring string beg (1+ beg)) "[")
  16633. tf (funcall (if with-hm 'cdr 'car)
  16634. org-time-stamp-custom-formats)
  16635. time (org-fix-decoded-time t1)
  16636. str (format-time-string
  16637. (concat
  16638. (if inactive "[" "<") (substring tf 1 -1)
  16639. (if inactive "]" ">"))
  16640. (apply 'encode-time time))
  16641. string (replace-match str t t string)
  16642. start (+ start (length str)))))))
  16643. string)
  16644. (defun org-fix-decoded-time (time)
  16645. "Set 0 instead of nil for the first 6 elements of time.
  16646. Don't touch the rest."
  16647. (let ((n 0))
  16648. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16649. (defun org-days-to-time (timestamp-string)
  16650. "Difference between TIMESTAMP-STRING and now in days."
  16651. (- (time-to-days (org-time-string-to-time timestamp-string))
  16652. (time-to-days (current-time))))
  16653. (defun org-deadline-close (timestamp-string &optional ndays)
  16654. "Is the time in TIMESTAMP-STRING close to the current date?"
  16655. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16656. (and (< (org-days-to-time timestamp-string) ndays)
  16657. (not (org-entry-is-done-p))))
  16658. (defun org-get-wdays (ts)
  16659. "Get the deadline lead time appropriate for timestring TS."
  16660. (cond
  16661. ((<= org-deadline-warning-days 0)
  16662. ;; 0 or negative, enforce this value no matter what
  16663. (- org-deadline-warning-days))
  16664. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16665. ;; lead time is specified.
  16666. (floor (* (string-to-number (match-string 1 ts))
  16667. (cdr (assoc (match-string 2 ts)
  16668. '(("d" . 1) ("w" . 7)
  16669. ("m" . 30.4) ("y" . 365.25)))))))
  16670. ;; go for the default.
  16671. (t org-deadline-warning-days)))
  16672. (defun org-calendar-select-mouse (ev)
  16673. "Return to `org-read-date' with the date currently selected.
  16674. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16675. (interactive "e")
  16676. (mouse-set-point ev)
  16677. (when (calendar-cursor-to-date)
  16678. (let* ((date (calendar-cursor-to-date))
  16679. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16680. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16681. (if (active-minibuffer-window) (exit-minibuffer))))
  16682. (defun org-check-deadlines (ndays)
  16683. "Check if there are any deadlines due or past due.
  16684. A deadline is considered due if it happens within `org-deadline-warning-days'
  16685. days from today's date. If the deadline appears in an entry marked DONE,
  16686. it is not shown. The prefix arg NDAYS can be used to test that many
  16687. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16688. (interactive "P")
  16689. (let* ((org-warn-days
  16690. (cond
  16691. ((equal ndays '(4)) 100000)
  16692. (ndays (prefix-numeric-value ndays))
  16693. (t (abs org-deadline-warning-days))))
  16694. (case-fold-search nil)
  16695. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16696. (callback
  16697. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16698. (message "%d deadlines past-due or due within %d days"
  16699. (org-occur regexp nil callback)
  16700. org-warn-days)))
  16701. (defun org-check-before-date (date)
  16702. "Check if there are deadlines or scheduled entries before DATE."
  16703. (interactive (list (org-read-date)))
  16704. (let ((case-fold-search nil)
  16705. (regexp (concat "\\<\\(" org-deadline-string
  16706. "\\|" org-scheduled-string
  16707. "\\) *<\\([^>]+\\)>"))
  16708. (callback
  16709. (lambda () (time-less-p
  16710. (org-time-string-to-time (match-string 2))
  16711. (org-time-string-to-time date)))))
  16712. (message "%d entries before %s"
  16713. (org-occur regexp nil callback) date)))
  16714. (defun org-evaluate-time-range (&optional to-buffer)
  16715. "Evaluate a time range by computing the difference between start and end.
  16716. Normally the result is just printed in the echo area, but with prefix arg
  16717. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16718. If the time range is actually in a table, the result is inserted into the
  16719. next column.
  16720. For time difference computation, a year is assumed to be exactly 365
  16721. days in order to avoid rounding problems."
  16722. (interactive "P")
  16723. (or
  16724. (org-clock-update-time-maybe)
  16725. (save-excursion
  16726. (unless (org-at-date-range-p t)
  16727. (goto-char (point-at-bol))
  16728. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16729. (if (not (org-at-date-range-p t))
  16730. (error "Not at a time-stamp range, and none found in current line")))
  16731. (let* ((ts1 (match-string 1))
  16732. (ts2 (match-string 2))
  16733. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16734. (match-end (match-end 0))
  16735. (time1 (org-time-string-to-time ts1))
  16736. (time2 (org-time-string-to-time ts2))
  16737. (t1 (time-to-seconds time1))
  16738. (t2 (time-to-seconds time2))
  16739. (diff (abs (- t2 t1)))
  16740. (negative (< (- t2 t1) 0))
  16741. ;; (ys (floor (* 365 24 60 60)))
  16742. (ds (* 24 60 60))
  16743. (hs (* 60 60))
  16744. (fy "%dy %dd %02d:%02d")
  16745. (fy1 "%dy %dd")
  16746. (fd "%dd %02d:%02d")
  16747. (fd1 "%dd")
  16748. (fh "%02d:%02d")
  16749. y d h m align)
  16750. (if havetime
  16751. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16752. y 0
  16753. d (floor (/ diff ds)) diff (mod diff ds)
  16754. h (floor (/ diff hs)) diff (mod diff hs)
  16755. m (floor (/ diff 60)))
  16756. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16757. y 0
  16758. d (floor (+ (/ diff ds) 0.5))
  16759. h 0 m 0))
  16760. (if (not to-buffer)
  16761. (message "%s" (org-make-tdiff-string y d h m))
  16762. (if (org-at-table-p)
  16763. (progn
  16764. (goto-char match-end)
  16765. (setq align t)
  16766. (and (looking-at " *|") (goto-char (match-end 0))))
  16767. (goto-char match-end))
  16768. (if (looking-at
  16769. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16770. (replace-match ""))
  16771. (if negative (insert " -"))
  16772. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16773. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16774. (insert " " (format fh h m))))
  16775. (if align (org-table-align))
  16776. (message "Time difference inserted")))))
  16777. (defun org-make-tdiff-string (y d h m)
  16778. (let ((fmt "")
  16779. (l nil))
  16780. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16781. l (push y l)))
  16782. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16783. l (push d l)))
  16784. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16785. l (push h l)))
  16786. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16787. l (push m l)))
  16788. (apply 'format fmt (nreverse l))))
  16789. (defun org-time-string-to-time (s)
  16790. (apply 'encode-time (org-parse-time-string s)))
  16791. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16792. "Convert a time stamp to an absolute day number.
  16793. If there is a specifyer for a cyclic time stamp, get the closest date to
  16794. DAYNR."
  16795. (cond
  16796. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16797. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16798. daynr
  16799. (+ daynr 1000)))
  16800. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16801. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16802. (time-to-days (current-time))) (match-string 0 s)
  16803. prefer))
  16804. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16805. (defun org-time-from-absolute (d)
  16806. "Return the time corresponding to date D.
  16807. D may be an absolute day number, or a calendar-type list (month day year)."
  16808. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16809. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16810. (defun org-calendar-holiday ()
  16811. "List of holidays, for Diary display in Org-mode."
  16812. (require 'holidays)
  16813. (let ((hl (funcall
  16814. (if (fboundp 'calendar-check-holidays)
  16815. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16816. (if hl (mapconcat 'identity hl "; "))))
  16817. (defun org-diary-sexp-entry (sexp entry date)
  16818. "Process a SEXP diary ENTRY for DATE."
  16819. (require 'diary-lib)
  16820. (let ((result (if calendar-debug-sexp
  16821. (let ((stack-trace-on-error t))
  16822. (eval (car (read-from-string sexp))))
  16823. (condition-case nil
  16824. (eval (car (read-from-string sexp)))
  16825. (error
  16826. (beep)
  16827. (message "Bad sexp at line %d in %s: %s"
  16828. (org-current-line)
  16829. (buffer-file-name) sexp)
  16830. (sleep-for 2))))))
  16831. (cond ((stringp result) result)
  16832. ((and (consp result)
  16833. (stringp (cdr result))) (cdr result))
  16834. (result entry)
  16835. (t nil))))
  16836. (defun org-diary-to-ical-string (frombuf)
  16837. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16838. This uses the icalendar.el library."
  16839. (let* ((tmpdir (if (featurep 'xemacs)
  16840. (temp-directory)
  16841. temporary-file-directory))
  16842. (tmpfile (make-temp-name
  16843. (expand-file-name "orgics" tmpdir)))
  16844. buf rtn b e)
  16845. (save-excursion
  16846. (set-buffer frombuf)
  16847. (icalendar-export-region (point-min) (point-max) tmpfile)
  16848. (setq buf (find-buffer-visiting tmpfile))
  16849. (set-buffer buf)
  16850. (goto-char (point-min))
  16851. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16852. (setq b (match-beginning 0)))
  16853. (goto-char (point-max))
  16854. (if (re-search-backward "^END:VEVENT" nil t)
  16855. (setq e (match-end 0)))
  16856. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16857. (kill-buffer buf)
  16858. (kill-buffer frombuf)
  16859. (delete-file tmpfile)
  16860. rtn))
  16861. (defun org-closest-date (start current change prefer)
  16862. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16863. When PREFER is `past' return a date that is either CURRENT or past.
  16864. When PREFER is `future', return a date that is either CURRENT or future."
  16865. ;; Make the proper lists from the dates
  16866. (catch 'exit
  16867. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16868. dn dw sday cday n1 n2
  16869. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16870. (setq start (org-date-to-gregorian start)
  16871. current (org-date-to-gregorian
  16872. (if org-agenda-repeating-timestamp-show-all
  16873. current
  16874. (time-to-days (current-time))))
  16875. sday (calendar-absolute-from-gregorian start)
  16876. cday (calendar-absolute-from-gregorian current))
  16877. (if (<= cday sday) (throw 'exit sday))
  16878. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16879. (setq dn (string-to-number (match-string 1 change))
  16880. dw (cdr (assoc (match-string 2 change) a1)))
  16881. (error "Invalid change specifyer: %s" change))
  16882. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16883. (cond
  16884. ((eq dw 'day)
  16885. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16886. n2 (+ n1 dn)))
  16887. ((eq dw 'year)
  16888. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16889. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16890. (setq date1 (list m d y1)
  16891. n1 (calendar-absolute-from-gregorian date1)
  16892. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16893. n2 (calendar-absolute-from-gregorian date2)))
  16894. ((eq dw 'month)
  16895. ;; approx number of month between the tow dates
  16896. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16897. ;; How often does dn fit in there?
  16898. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16899. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16900. m (+ m nm)
  16901. ny (floor (/ m 12))
  16902. y (+ y ny)
  16903. m (- m (* ny 12)))
  16904. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16905. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16906. (setq m2 (+ m dn) y2 y)
  16907. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16908. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16909. (while (< n2 cday)
  16910. (setq n1 n2 m m2 y y2)
  16911. (setq m2 (+ m dn) y2 y)
  16912. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16913. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16914. (if org-agenda-repeating-timestamp-show-all
  16915. (cond
  16916. ((eq prefer 'past) n1)
  16917. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16918. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16919. (cond
  16920. ((eq prefer 'past) n1)
  16921. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16922. (t (if (= cday n1) n1 n2)))))))
  16923. (defun org-date-to-gregorian (date)
  16924. "Turn any specification of DATE into a gregorian date for the calendar."
  16925. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16926. ((and (listp date) (= (length date) 3)) date)
  16927. ((stringp date)
  16928. (setq date (org-parse-time-string date))
  16929. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16930. ((listp date)
  16931. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16932. (defun org-parse-time-string (s &optional nodefault)
  16933. "Parse the standard Org-mode time string.
  16934. This should be a lot faster than the normal `parse-time-string'.
  16935. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16936. hour and minute fields will be nil if not given."
  16937. (if (string-match org-ts-regexp0 s)
  16938. (list 0
  16939. (if (or (match-beginning 8) (not nodefault))
  16940. (string-to-number (or (match-string 8 s) "0")))
  16941. (if (or (match-beginning 7) (not nodefault))
  16942. (string-to-number (or (match-string 7 s) "0")))
  16943. (string-to-number (match-string 4 s))
  16944. (string-to-number (match-string 3 s))
  16945. (string-to-number (match-string 2 s))
  16946. nil nil nil)
  16947. (make-list 9 0)))
  16948. (defun org-timestamp-up (&optional arg)
  16949. "Increase the date item at the cursor by one.
  16950. If the cursor is on the year, change the year. If it is on the month or
  16951. the day, change that.
  16952. With prefix ARG, change by that many units."
  16953. (interactive "p")
  16954. (org-timestamp-change (prefix-numeric-value arg)))
  16955. (defun org-timestamp-down (&optional arg)
  16956. "Decrease the date item at the cursor by one.
  16957. If the cursor is on the year, change the year. If it is on the month or
  16958. the day, change that.
  16959. With prefix ARG, change by that many units."
  16960. (interactive "p")
  16961. (org-timestamp-change (- (prefix-numeric-value arg))))
  16962. (defun org-timestamp-up-day (&optional arg)
  16963. "Increase the date in the time stamp by one day.
  16964. With prefix ARG, change that many days."
  16965. (interactive "p")
  16966. (if (and (not (org-at-timestamp-p t))
  16967. (org-on-heading-p))
  16968. (org-todo 'up)
  16969. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16970. (defun org-timestamp-down-day (&optional arg)
  16971. "Decrease the date in the time stamp by one day.
  16972. With prefix ARG, change that many days."
  16973. (interactive "p")
  16974. (if (and (not (org-at-timestamp-p t))
  16975. (org-on-heading-p))
  16976. (org-todo 'down)
  16977. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16978. (defsubst org-pos-in-match-range (pos n)
  16979. (and (match-beginning n)
  16980. (<= (match-beginning n) pos)
  16981. (>= (match-end n) pos)))
  16982. (defun org-at-timestamp-p (&optional inactive-ok)
  16983. "Determine if the cursor is in or at a timestamp."
  16984. (interactive)
  16985. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16986. (pos (point))
  16987. (ans (or (looking-at tsr)
  16988. (save-excursion
  16989. (skip-chars-backward "^[<\n\r\t")
  16990. (if (> (point) (point-min)) (backward-char 1))
  16991. (and (looking-at tsr)
  16992. (> (- (match-end 0) pos) -1))))))
  16993. (and ans
  16994. (boundp 'org-ts-what)
  16995. (setq org-ts-what
  16996. (cond
  16997. ((= pos (match-beginning 0)) 'bracket)
  16998. ((= pos (1- (match-end 0))) 'bracket)
  16999. ((org-pos-in-match-range pos 2) 'year)
  17000. ((org-pos-in-match-range pos 3) 'month)
  17001. ((org-pos-in-match-range pos 7) 'hour)
  17002. ((org-pos-in-match-range pos 8) 'minute)
  17003. ((or (org-pos-in-match-range pos 4)
  17004. (org-pos-in-match-range pos 5)) 'day)
  17005. ((and (> pos (or (match-end 8) (match-end 5)))
  17006. (< pos (match-end 0)))
  17007. (- pos (or (match-end 8) (match-end 5))))
  17008. (t 'day))))
  17009. ans))
  17010. (defun org-toggle-timestamp-type ()
  17011. "Toggle the type (<active> or [inactive]) of a time stamp."
  17012. (interactive)
  17013. (when (org-at-timestamp-p t)
  17014. (save-excursion
  17015. (goto-char (match-beginning 0))
  17016. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  17017. (goto-char (1- (match-end 0)))
  17018. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  17019. (message "Timestamp is now %sactive"
  17020. (if (equal (char-before) ?>) "in" ""))))
  17021. (defun org-timestamp-change (n &optional what)
  17022. "Change the date in the time stamp at point.
  17023. The date will be changed by N times WHAT. WHAT can be `day', `month',
  17024. `year', `minute', `second'. If WHAT is not given, the cursor position
  17025. in the timestamp determines what will be changed."
  17026. (let ((pos (point))
  17027. with-hm inactive
  17028. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  17029. org-ts-what
  17030. extra rem
  17031. ts time time0)
  17032. (if (not (org-at-timestamp-p t))
  17033. (error "Not at a timestamp"))
  17034. (if (and (not what) (eq org-ts-what 'bracket))
  17035. (org-toggle-timestamp-type)
  17036. (if (and (not what) (not (eq org-ts-what 'day))
  17037. org-display-custom-times
  17038. (get-text-property (point) 'display)
  17039. (not (get-text-property (1- (point)) 'display)))
  17040. (setq org-ts-what 'day))
  17041. (setq org-ts-what (or what org-ts-what)
  17042. inactive (= (char-after (match-beginning 0)) ?\[)
  17043. ts (match-string 0))
  17044. (replace-match "")
  17045. (if (string-match
  17046. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  17047. ts)
  17048. (setq extra (match-string 1 ts)))
  17049. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  17050. (setq with-hm t))
  17051. (setq time0 (org-parse-time-string ts))
  17052. (when (and (eq org-ts-what 'minute)
  17053. (eq current-prefix-arg nil))
  17054. (setq n (* dm (signum n)))
  17055. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  17056. (setcar (cdr time0) (+ (nth 1 time0)
  17057. (if (> n 0) (- rem) (- dm rem))))))
  17058. (setq time
  17059. (encode-time (or (car time0) 0)
  17060. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  17061. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  17062. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  17063. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  17064. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  17065. (nthcdr 6 time0)))
  17066. (when (integerp org-ts-what)
  17067. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  17068. (if (eq what 'calendar)
  17069. (let ((cal-date (org-get-date-from-calendar)))
  17070. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17071. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17072. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17073. (setcar time0 (or (car time0) 0))
  17074. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17075. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17076. (setq time (apply 'encode-time time0))))
  17077. (setq org-last-changed-timestamp
  17078. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17079. (org-clock-update-time-maybe)
  17080. (goto-char pos)
  17081. ;; Try to recenter the calendar window, if any
  17082. (if (and org-calendar-follow-timestamp-change
  17083. (get-buffer-window "*Calendar*" t)
  17084. (memq org-ts-what '(day month year)))
  17085. (org-recenter-calendar (time-to-days time))))))
  17086. ;; FIXME: does not yet work for lead times
  17087. (defun org-modify-ts-extra (s pos n)
  17088. "Change the different parts of the lead-time and repeat fields in timestamp."
  17089. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17090. ng h m new)
  17091. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17092. (cond
  17093. ((or (org-pos-in-match-range pos 2)
  17094. (org-pos-in-match-range pos 3))
  17095. (setq m (string-to-number (match-string 3 s))
  17096. h (string-to-number (match-string 2 s)))
  17097. (if (org-pos-in-match-range pos 2)
  17098. (setq h (+ h n))
  17099. (setq m (+ m n)))
  17100. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17101. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17102. (setq h (min 24 (max 0 h)))
  17103. (setq ng 1 new (format "-%02d:%02d" h m)))
  17104. ((org-pos-in-match-range pos 6)
  17105. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17106. ((org-pos-in-match-range pos 5)
  17107. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  17108. (when ng
  17109. (setq s (concat
  17110. (substring s 0 (match-beginning ng))
  17111. new
  17112. (substring s (match-end ng))))))
  17113. s))
  17114. (defun org-recenter-calendar (date)
  17115. "If the calendar is visible, recenter it to DATE."
  17116. (let* ((win (selected-window))
  17117. (cwin (get-buffer-window "*Calendar*" t))
  17118. (calendar-move-hook nil))
  17119. (when cwin
  17120. (select-window cwin)
  17121. (calendar-goto-date (if (listp date) date
  17122. (calendar-gregorian-from-absolute date)))
  17123. (select-window win))))
  17124. (defun org-goto-calendar (&optional arg)
  17125. "Go to the Emacs calendar at the current date.
  17126. If there is a time stamp in the current line, go to that date.
  17127. A prefix ARG can be used to force the current date."
  17128. (interactive "P")
  17129. (let ((tsr org-ts-regexp) diff
  17130. (calendar-move-hook nil)
  17131. (view-calendar-holidays-initially nil)
  17132. (view-diary-entries-initially nil))
  17133. (if (or (org-at-timestamp-p)
  17134. (save-excursion
  17135. (beginning-of-line 1)
  17136. (looking-at (concat ".*" tsr))))
  17137. (let ((d1 (time-to-days (current-time)))
  17138. (d2 (time-to-days
  17139. (org-time-string-to-time (match-string 1)))))
  17140. (setq diff (- d2 d1))))
  17141. (calendar)
  17142. (calendar-goto-today)
  17143. (if (and diff (not arg)) (calendar-forward-day diff))))
  17144. (defun org-get-date-from-calendar ()
  17145. "Return a list (month day year) of date at point in calendar."
  17146. (with-current-buffer "*Calendar*"
  17147. (save-match-data
  17148. (calendar-cursor-to-date))))
  17149. (defun org-date-from-calendar ()
  17150. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17151. If there is already a time stamp at the cursor position, update it."
  17152. (interactive)
  17153. (if (org-at-timestamp-p t)
  17154. (org-timestamp-change 0 'calendar)
  17155. (let ((cal-date (org-get-date-from-calendar)))
  17156. (org-insert-time-stamp
  17157. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17158. (defvar appt-time-msg-list)
  17159. ;;;###autoload
  17160. (defun org-agenda-to-appt (&optional refresh filter)
  17161. "Activate appointments found in `org-agenda-files'.
  17162. With a \\[universal-argument] prefix, refresh the list of
  17163. appointements.
  17164. If FILTER is t, interactively prompt the user for a regular
  17165. expression, and filter out entries that don't match it.
  17166. If FILTER is a string, use this string as a regular expression
  17167. for filtering entries out.
  17168. FILTER can also be an alist with the car of each cell being
  17169. either 'headline or 'category. For example:
  17170. '((headline \"IMPORTANT\")
  17171. (category \"Work\"))
  17172. will only add headlines containing IMPORTANT or headlines
  17173. belonging to the \"Work\" category."
  17174. (interactive "P")
  17175. (require 'calendar)
  17176. (if refresh (setq appt-time-msg-list nil))
  17177. (if (eq filter t)
  17178. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17179. (let* ((cnt 0) ; count added events
  17180. (org-agenda-new-buffers nil)
  17181. (org-deadline-warning-days 0)
  17182. (today (org-date-to-gregorian
  17183. (time-to-days (current-time))))
  17184. (files (org-agenda-files)) entries file)
  17185. ;; Get all entries which may contain an appt
  17186. (while (setq file (pop files))
  17187. (setq entries
  17188. (append entries
  17189. (org-agenda-get-day-entries
  17190. file today :timestamp :scheduled :deadline))))
  17191. (setq entries (delq nil entries))
  17192. ;; Map thru entries and find if we should filter them out
  17193. (mapc
  17194. (lambda(x)
  17195. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17196. (cat (get-text-property 1 'org-category x))
  17197. (tod (get-text-property 1 'time-of-day x))
  17198. (ok (or (null filter)
  17199. (and (stringp filter) (string-match filter evt))
  17200. (and (listp filter)
  17201. (or (string-match
  17202. (cadr (assoc 'category filter)) cat)
  17203. (string-match
  17204. (cadr (assoc 'headline filter)) evt))))))
  17205. ;; FIXME: Shall we remove text-properties for the appt text?
  17206. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17207. (when (and ok tod)
  17208. (setq tod (number-to-string tod)
  17209. tod (when (string-match
  17210. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17211. (concat (match-string 1 tod) ":"
  17212. (match-string 2 tod))))
  17213. (appt-add tod evt)
  17214. (setq cnt (1+ cnt))))) entries)
  17215. (org-release-buffers org-agenda-new-buffers)
  17216. (if (eq cnt 0)
  17217. (message "No event to add")
  17218. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17219. ;;; The clock for measuring work time.
  17220. (defvar org-mode-line-string "")
  17221. (put 'org-mode-line-string 'risky-local-variable t)
  17222. (defvar org-mode-line-timer nil)
  17223. (defvar org-clock-heading "")
  17224. (defvar org-clock-start-time "")
  17225. (defun org-update-mode-line ()
  17226. (let* ((delta (- (time-to-seconds (current-time))
  17227. (time-to-seconds org-clock-start-time)))
  17228. (h (floor delta 3600))
  17229. (m (floor (- delta (* 3600 h)) 60)))
  17230. (setq org-mode-line-string
  17231. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17232. 'help-echo "Org-mode clock is running"))
  17233. (force-mode-line-update)))
  17234. (defvar org-clock-marker (make-marker)
  17235. "Marker recording the last clock-in.")
  17236. (defvar org-clock-mode-line-entry nil
  17237. "Information for the modeline about the running clock.")
  17238. (defun org-clock-in ()
  17239. "Start the clock on the current item.
  17240. If necessary, clock-out of the currently active clock."
  17241. (interactive)
  17242. (org-clock-out t)
  17243. (let (ts)
  17244. (save-excursion
  17245. (org-back-to-heading t)
  17246. (when (and org-clock-in-switch-to-state
  17247. (not (looking-at (concat outline-regexp "[ \t]*"
  17248. org-clock-in-switch-to-state
  17249. "\\>"))))
  17250. (org-todo org-clock-in-switch-to-state))
  17251. (if (and org-clock-heading-function
  17252. (functionp org-clock-heading-function))
  17253. (setq org-clock-heading (funcall org-clock-heading-function))
  17254. (if (looking-at org-complex-heading-regexp)
  17255. (setq org-clock-heading (match-string 4))
  17256. (setq org-clock-heading "???")))
  17257. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17258. (org-clock-find-position)
  17259. (insert "\n") (backward-char 1)
  17260. (indent-relative)
  17261. (insert org-clock-string " ")
  17262. (setq org-clock-start-time (current-time))
  17263. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17264. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17265. (or global-mode-string (setq global-mode-string '("")))
  17266. (or (memq 'org-mode-line-string global-mode-string)
  17267. (setq global-mode-string
  17268. (append global-mode-string '(org-mode-line-string))))
  17269. (org-update-mode-line)
  17270. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17271. (message "Clock started at %s" ts))))
  17272. (defun org-clock-find-position ()
  17273. "Find the location where the next clock line should be inserted."
  17274. (org-back-to-heading t)
  17275. (catch 'exit
  17276. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17277. (re (concat "^[ \t]*" org-clock-string))
  17278. (cnt 0)
  17279. first last)
  17280. (goto-char beg)
  17281. (when (eobp) (newline) (setq end (max (point) end)))
  17282. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17283. ;; we seem to have a CLOCK drawer, so go there.
  17284. (beginning-of-line 2)
  17285. (throw 'exit t))
  17286. ;; Lets count the CLOCK lines
  17287. (goto-char beg)
  17288. (while (re-search-forward re end t)
  17289. (setq first (or first (match-beginning 0))
  17290. last (match-beginning 0)
  17291. cnt (1+ cnt)))
  17292. (when (and (integerp org-clock-into-drawer)
  17293. (>= (1+ cnt) org-clock-into-drawer))
  17294. ;; Wrap current entries into a new drawer
  17295. (goto-char last)
  17296. (beginning-of-line 2)
  17297. (if (org-at-item-p) (org-end-of-item))
  17298. (insert ":END:\n")
  17299. (beginning-of-line 0)
  17300. (org-indent-line-function)
  17301. (goto-char first)
  17302. (insert ":CLOCK:\n")
  17303. (beginning-of-line 0)
  17304. (org-indent-line-function)
  17305. (org-flag-drawer t)
  17306. (beginning-of-line 2)
  17307. (throw 'exit nil))
  17308. (goto-char beg)
  17309. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17310. (not (equal (match-string 1) org-clock-string)))
  17311. ;; Planning info, skip to after it
  17312. (beginning-of-line 2)
  17313. (or (bolp) (newline)))
  17314. (when (eq t org-clock-into-drawer)
  17315. (insert ":CLOCK:\n:END:\n")
  17316. (beginning-of-line -1)
  17317. (org-indent-line-function)
  17318. (org-flag-drawer t)
  17319. (beginning-of-line 2)
  17320. (org-indent-line-function)))))
  17321. (defun org-clock-out (&optional fail-quietly)
  17322. "Stop the currently running clock.
  17323. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17324. (interactive)
  17325. (catch 'exit
  17326. (if (not (marker-buffer org-clock-marker))
  17327. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17328. (let (ts te s h m)
  17329. (save-excursion
  17330. (set-buffer (marker-buffer org-clock-marker))
  17331. (goto-char org-clock-marker)
  17332. (beginning-of-line 1)
  17333. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17334. (equal (match-string 1) org-clock-string))
  17335. (setq ts (match-string 2))
  17336. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17337. (goto-char (match-end 0))
  17338. (delete-region (point) (point-at-eol))
  17339. (insert "--")
  17340. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17341. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17342. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17343. h (floor (/ s 3600))
  17344. s (- s (* 3600 h))
  17345. m (floor (/ s 60))
  17346. s (- s (* 60 s)))
  17347. (insert " => " (format "%2d:%02d" h m))
  17348. (move-marker org-clock-marker nil)
  17349. (when org-log-note-clock-out
  17350. (org-add-log-maybe 'clock-out))
  17351. (when org-mode-line-timer
  17352. (cancel-timer org-mode-line-timer)
  17353. (setq org-mode-line-timer nil))
  17354. (setq global-mode-string
  17355. (delq 'org-mode-line-string global-mode-string))
  17356. (force-mode-line-update)
  17357. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17358. (defun org-clock-cancel ()
  17359. "Cancel the running clock be removing the start timestamp."
  17360. (interactive)
  17361. (if (not (marker-buffer org-clock-marker))
  17362. (error "No active clock"))
  17363. (save-excursion
  17364. (set-buffer (marker-buffer org-clock-marker))
  17365. (goto-char org-clock-marker)
  17366. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17367. (setq global-mode-string
  17368. (delq 'org-mode-line-string global-mode-string))
  17369. (force-mode-line-update)
  17370. (message "Clock canceled"))
  17371. (defun org-clock-goto (&optional delete-windows)
  17372. "Go to the currently clocked-in entry."
  17373. (interactive "P")
  17374. (if (not (marker-buffer org-clock-marker))
  17375. (error "No active clock"))
  17376. (switch-to-buffer-other-window
  17377. (marker-buffer org-clock-marker))
  17378. (if delete-windows (delete-other-windows))
  17379. (goto-char org-clock-marker)
  17380. (org-show-entry)
  17381. (org-back-to-heading)
  17382. (recenter))
  17383. (defvar org-clock-file-total-minutes nil
  17384. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17385. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17386. (defun org-clock-sum (&optional tstart tend)
  17387. "Sum the times for each subtree.
  17388. Puts the resulting times in minutes as a text property on each headline."
  17389. (interactive)
  17390. (let* ((bmp (buffer-modified-p))
  17391. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17392. org-clock-string
  17393. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17394. (lmax 30)
  17395. (ltimes (make-vector lmax 0))
  17396. (t1 0)
  17397. (level 0)
  17398. ts te dt
  17399. time)
  17400. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17401. (save-excursion
  17402. (goto-char (point-max))
  17403. (while (re-search-backward re nil t)
  17404. (cond
  17405. ((match-end 2)
  17406. ;; Two time stamps
  17407. (setq ts (match-string 2)
  17408. te (match-string 3)
  17409. ts (time-to-seconds
  17410. (apply 'encode-time (org-parse-time-string ts)))
  17411. te (time-to-seconds
  17412. (apply 'encode-time (org-parse-time-string te)))
  17413. ts (if tstart (max ts tstart) ts)
  17414. te (if tend (min te tend) te)
  17415. dt (- te ts)
  17416. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17417. ((match-end 4)
  17418. ;; A naket time
  17419. (setq t1 (+ t1 (string-to-number (match-string 5))
  17420. (* 60 (string-to-number (match-string 4))))))
  17421. (t ;; A headline
  17422. (setq level (- (match-end 1) (match-beginning 1)))
  17423. (when (or (> t1 0) (> (aref ltimes level) 0))
  17424. (loop for l from 0 to level do
  17425. (aset ltimes l (+ (aref ltimes l) t1)))
  17426. (setq t1 0 time (aref ltimes level))
  17427. (loop for l from level to (1- lmax) do
  17428. (aset ltimes l 0))
  17429. (goto-char (match-beginning 0))
  17430. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17431. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17432. (set-buffer-modified-p bmp)))
  17433. (defun org-clock-display (&optional total-only)
  17434. "Show subtree times in the entire buffer.
  17435. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17436. in the echo area."
  17437. (interactive)
  17438. (org-remove-clock-overlays)
  17439. (let (time h m p)
  17440. (org-clock-sum)
  17441. (unless total-only
  17442. (save-excursion
  17443. (goto-char (point-min))
  17444. (while (or (and (equal (setq p (point)) (point-min))
  17445. (get-text-property p :org-clock-minutes))
  17446. (setq p (next-single-property-change
  17447. (point) :org-clock-minutes)))
  17448. (goto-char p)
  17449. (when (setq time (get-text-property p :org-clock-minutes))
  17450. (org-put-clock-overlay time (funcall outline-level))))
  17451. (setq h (/ org-clock-file-total-minutes 60)
  17452. m (- org-clock-file-total-minutes (* 60 h)))
  17453. ;; Arrange to remove the overlays upon next change.
  17454. (when org-remove-highlights-with-change
  17455. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17456. nil 'local))))
  17457. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17458. (defvar org-clock-overlays nil)
  17459. (make-variable-buffer-local 'org-clock-overlays)
  17460. (defun org-put-clock-overlay (time &optional level)
  17461. "Put an overlays on the current line, displaying TIME.
  17462. If LEVEL is given, prefix time with a corresponding number of stars.
  17463. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17464. will be easy to remove."
  17465. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17466. (l (if level (org-get-legal-level level 0) 0))
  17467. (off 0)
  17468. ov tx)
  17469. (move-to-column c)
  17470. (unless (eolp) (skip-chars-backward "^ \t"))
  17471. (skip-chars-backward " \t")
  17472. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17473. tx (concat (buffer-substring (1- (point)) (point))
  17474. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17475. (org-add-props (format "%s %2d:%02d%s"
  17476. (make-string l ?*) h m
  17477. (make-string (- 16 l) ?\ ))
  17478. '(face secondary-selection))
  17479. ""))
  17480. (if (not (featurep 'xemacs))
  17481. (org-overlay-put ov 'display tx)
  17482. (org-overlay-put ov 'invisible t)
  17483. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17484. (push ov org-clock-overlays)))
  17485. (defun org-remove-clock-overlays (&optional beg end noremove)
  17486. "Remove the occur highlights from the buffer.
  17487. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17488. from the `before-change-functions' in the current buffer."
  17489. (interactive)
  17490. (unless org-inhibit-highlight-removal
  17491. (mapc 'org-delete-overlay org-clock-overlays)
  17492. (setq org-clock-overlays nil)
  17493. (unless noremove
  17494. (remove-hook 'before-change-functions
  17495. 'org-remove-clock-overlays 'local))))
  17496. (defun org-clock-out-if-current ()
  17497. "Clock out if the current entry contains the running clock.
  17498. This is used to stop the clock after a TODO entry is marked DONE,
  17499. and is only done if the variable `org-clock-out-when-done' is not nil."
  17500. (when (and org-clock-out-when-done
  17501. (member state org-done-keywords)
  17502. (equal (marker-buffer org-clock-marker) (current-buffer))
  17503. (< (point) org-clock-marker)
  17504. (> (save-excursion (outline-next-heading) (point))
  17505. org-clock-marker))
  17506. ;; Clock out, but don't accept a logging message for this.
  17507. (let ((org-log-note-clock-out nil))
  17508. (org-clock-out))))
  17509. (add-hook 'org-after-todo-state-change-hook
  17510. 'org-clock-out-if-current)
  17511. (defun org-check-running-clock ()
  17512. "Check if the current buffer contains the running clock.
  17513. If yes, offer to stop it and to save the buffer with the changes."
  17514. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17515. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17516. (buffer-name))))
  17517. (org-clock-out)
  17518. (when (y-or-n-p "Save changed buffer?")
  17519. (save-buffer))))
  17520. (defun org-clock-report (&optional arg)
  17521. "Create a table containing a report about clocked time.
  17522. If the cursor is inside an existing clocktable block, then the table
  17523. will be updated. If not, a new clocktable will be inserted.
  17524. When called with a prefix argument, move to the first clock table in the
  17525. buffer and update it."
  17526. (interactive "P")
  17527. (org-remove-clock-overlays)
  17528. (when arg
  17529. (org-find-dblock "clocktable")
  17530. (org-show-entry))
  17531. (if (org-in-clocktable-p)
  17532. (goto-char (org-in-clocktable-p))
  17533. (org-create-dblock (list :name "clocktable"
  17534. :maxlevel 2 :scope 'file)))
  17535. (org-update-dblock))
  17536. (defun org-in-clocktable-p ()
  17537. "Check if the cursor is in a clocktable."
  17538. (let ((pos (point)) start)
  17539. (save-excursion
  17540. (end-of-line 1)
  17541. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17542. (setq start (match-beginning 0))
  17543. (re-search-forward "^#\\+END:.*" nil t)
  17544. (>= (match-end 0) pos)
  17545. start))))
  17546. (defun org-clock-update-time-maybe ()
  17547. "If this is a CLOCK line, update it and return t.
  17548. Otherwise, return nil."
  17549. (interactive)
  17550. (save-excursion
  17551. (beginning-of-line 1)
  17552. (skip-chars-forward " \t")
  17553. (when (looking-at org-clock-string)
  17554. (let ((re (concat "[ \t]*" org-clock-string
  17555. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17556. "\\([ \t]*=>.*\\)?"))
  17557. ts te h m s)
  17558. (if (not (looking-at re))
  17559. nil
  17560. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17561. (end-of-line 1)
  17562. (setq ts (match-string 1)
  17563. te (match-string 2))
  17564. (setq s (- (time-to-seconds
  17565. (apply 'encode-time (org-parse-time-string te)))
  17566. (time-to-seconds
  17567. (apply 'encode-time (org-parse-time-string ts))))
  17568. h (floor (/ s 3600))
  17569. s (- s (* 3600 h))
  17570. m (floor (/ s 60))
  17571. s (- s (* 60 s)))
  17572. (insert " => " (format "%2d:%02d" h m))
  17573. t)))))
  17574. (defun org-clock-special-range (key &optional time as-strings)
  17575. "Return two times bordering a special time range.
  17576. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17577. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17578. A week starts Monday 0:00 and ends Sunday 24:00.
  17579. The range is determined relative to TIME. TIME defaults to the current time.
  17580. The return value is a cons cell with two internal times like the ones
  17581. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17582. the returned times will be formatted strings."
  17583. (let* ((tm (decode-time (or time (current-time))))
  17584. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17585. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17586. (dow (nth 6 tm))
  17587. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17588. (cond
  17589. ((eq key 'today)
  17590. (setq h 0 m 0 h1 24 m1 0))
  17591. ((eq key 'yesterday)
  17592. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17593. ((eq key 'thisweek)
  17594. (setq diff (if (= dow 0) 6 (1- dow))
  17595. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17596. ((eq key 'lastweek)
  17597. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17598. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17599. ((eq key 'thismonth)
  17600. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17601. ((eq key 'lastmonth)
  17602. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17603. ((eq key 'thisyear)
  17604. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17605. ((eq key 'lastyear)
  17606. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17607. (t (error "No such time block %s" key)))
  17608. (setq ts (encode-time s m h d month y)
  17609. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17610. (or d1 d) (or month1 month) (or y1 y)))
  17611. (setq fm (cdr org-time-stamp-formats))
  17612. (if as-strings
  17613. (cons (format-time-string fm ts) (format-time-string fm te))
  17614. (cons ts te))))
  17615. (defun org-dblock-write:clocktable (params)
  17616. "Write the standard clocktable."
  17617. (catch 'exit
  17618. (let* ((hlchars '((1 . "*") (2 . "/")))
  17619. (ins (make-marker))
  17620. (total-time nil)
  17621. (scope (plist-get params :scope))
  17622. (tostring (plist-get params :tostring))
  17623. (multifile (plist-get params :multifile))
  17624. (header (plist-get params :header))
  17625. (maxlevel (or (plist-get params :maxlevel) 3))
  17626. (step (plist-get params :step))
  17627. (emph (plist-get params :emphasize))
  17628. (ts (plist-get params :tstart))
  17629. (te (plist-get params :tend))
  17630. (block (plist-get params :block))
  17631. ipos time h m p level hlc hdl
  17632. cc beg end pos tbl)
  17633. (when step
  17634. (org-clocktable-steps params)
  17635. (throw 'exit nil))
  17636. (when block
  17637. (setq cc (org-clock-special-range block nil t)
  17638. ts (car cc) te (cdr cc)))
  17639. (if ts (setq ts (time-to-seconds
  17640. (apply 'encode-time (org-parse-time-string ts)))))
  17641. (if te (setq te (time-to-seconds
  17642. (apply 'encode-time (org-parse-time-string te)))))
  17643. (move-marker ins (point))
  17644. (setq ipos (point))
  17645. ;; Get the right scope
  17646. (setq pos (point))
  17647. (save-restriction
  17648. (cond
  17649. ((not scope))
  17650. ((eq scope 'file) (widen))
  17651. ((eq scope 'subtree) (org-narrow-to-subtree))
  17652. ((eq scope 'tree)
  17653. (while (org-up-heading-safe))
  17654. (org-narrow-to-subtree))
  17655. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17656. (symbol-name scope)))
  17657. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17658. (catch 'exit
  17659. (while (org-up-heading-safe)
  17660. (looking-at outline-regexp)
  17661. (if (<= (org-reduced-level (funcall outline-level)) level)
  17662. (throw 'exit nil))))
  17663. (org-narrow-to-subtree))
  17664. ((or (listp scope) (eq scope 'agenda))
  17665. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17666. (scope 'agenda)
  17667. (p1 (copy-sequence params))
  17668. file)
  17669. (plist-put p1 :tostring t)
  17670. (plist-put p1 :multifile t)
  17671. (plist-put p1 :scope 'file)
  17672. (org-prepare-agenda-buffers files)
  17673. (while (setq file (pop files))
  17674. (with-current-buffer (find-buffer-visiting file)
  17675. (push (org-clocktable-add-file
  17676. file (org-dblock-write:clocktable p1)) tbl)
  17677. (setq total-time (+ (or total-time 0)
  17678. org-clock-file-total-minutes)))))))
  17679. (goto-char pos)
  17680. (unless (eq scope 'agenda)
  17681. (org-clock-sum ts te)
  17682. (goto-char (point-min))
  17683. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17684. (goto-char p)
  17685. (when (setq time (get-text-property p :org-clock-minutes))
  17686. (save-excursion
  17687. (beginning-of-line 1)
  17688. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17689. (setq level (org-reduced-level
  17690. (- (match-end 1) (match-beginning 1))))
  17691. (<= level maxlevel))
  17692. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17693. hdl (match-string 2)
  17694. h (/ time 60)
  17695. m (- time (* 60 h)))
  17696. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17697. (push (concat
  17698. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17699. (make-string (1- level) ?|)
  17700. hlc (format "%d:%02d" h m) hlc
  17701. " |") tbl))))))
  17702. (setq tbl (nreverse tbl))
  17703. (if tostring
  17704. (if tbl (mapconcat 'identity tbl "\n") nil)
  17705. (goto-char ins)
  17706. (insert-before-markers
  17707. (or header
  17708. (concat
  17709. "Clock summary at ["
  17710. (substring
  17711. (format-time-string (cdr org-time-stamp-formats))
  17712. 1 -1)
  17713. "]."
  17714. (if block
  17715. (format " Considered range is /%s/." block)
  17716. "")
  17717. "\n\n"))
  17718. (if (eq scope 'agenda) "|File" "")
  17719. "|L|Headline|Time|\n")
  17720. (setq total-time (or total-time org-clock-file-total-minutes)
  17721. h (/ total-time 60)
  17722. m (- total-time (* 60 h)))
  17723. (insert-before-markers
  17724. "|-\n|"
  17725. (if (eq scope 'agenda) "|" "")
  17726. "|"
  17727. "*Total time*| "
  17728. (format "*%d:%02d*" h m)
  17729. "|\n|-\n")
  17730. (setq tbl (delq nil tbl))
  17731. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17732. (equal (substring (car tbl) 0 2) "|-"))
  17733. (pop tbl))
  17734. (insert-before-markers (mapconcat
  17735. 'identity (delq nil tbl)
  17736. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17737. (backward-delete-char 1)
  17738. (goto-char ipos)
  17739. (skip-chars-forward "^|")
  17740. (org-table-align))))))
  17741. (defun org-clocktable-steps (params)
  17742. (let* ((p1 (copy-sequence params))
  17743. (ts (plist-get p1 :tstart))
  17744. (te (plist-get p1 :tend))
  17745. (step0 (plist-get p1 :step))
  17746. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17747. (block (plist-get p1 :block))
  17748. cc)
  17749. (when block
  17750. (setq cc (org-clock-special-range block nil t)
  17751. ts (car cc) te (cdr cc)))
  17752. (if ts (setq ts (time-to-seconds
  17753. (apply 'encode-time (org-parse-time-string ts)))))
  17754. (if te (setq te (time-to-seconds
  17755. (apply 'encode-time (org-parse-time-string te)))))
  17756. (plist-put p1 :header "")
  17757. (plist-put p1 :step nil)
  17758. (plist-put p1 :block nil)
  17759. (while (< ts te)
  17760. (or (bolp) (insert "\n"))
  17761. (plist-put p1 :tstart (format-time-string
  17762. (car org-time-stamp-formats)
  17763. (seconds-to-time ts)))
  17764. (plist-put p1 :tend (format-time-string
  17765. (car org-time-stamp-formats)
  17766. (seconds-to-time (setq ts (+ ts step)))))
  17767. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17768. (plist-get p1 :tstart) "\n")
  17769. (org-dblock-write:clocktable p1)
  17770. (re-search-forward "#\\+END:")
  17771. (end-of-line 0))))
  17772. (defun org-clocktable-add-file (file table)
  17773. (if table
  17774. (let ((lines (org-split-string table "\n"))
  17775. (ff (file-name-nondirectory file)))
  17776. (mapconcat 'identity
  17777. (mapcar (lambda (x)
  17778. (if (string-match org-table-dataline-regexp x)
  17779. (concat "|" ff x)
  17780. x))
  17781. lines)
  17782. "\n"))))
  17783. ;; FIXME: I don't think anybody uses this, ask David
  17784. (defun org-collect-clock-time-entries ()
  17785. "Return an internal list with clocking information.
  17786. This list has one entry for each CLOCK interval.
  17787. FIXME: describe the elements."
  17788. (interactive)
  17789. (let ((re (concat "^[ \t]*" org-clock-string
  17790. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17791. rtn beg end next cont level title total closedp leafp
  17792. clockpos titlepos h m donep)
  17793. (save-excursion
  17794. (org-clock-sum)
  17795. (goto-char (point-min))
  17796. (while (re-search-forward re nil t)
  17797. (setq clockpos (match-beginning 0)
  17798. beg (match-string 1) end (match-string 2)
  17799. cont (match-end 0))
  17800. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17801. end (apply 'encode-time (org-parse-time-string end)))
  17802. (org-back-to-heading t)
  17803. (setq donep (org-entry-is-done-p))
  17804. (setq titlepos (point)
  17805. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17806. h (/ total 60) m (- total (* 60 h))
  17807. total (cons h m))
  17808. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17809. (setq level (- (match-end 1) (match-beginning 1))
  17810. title (org-match-string-no-properties 2))
  17811. (save-excursion (outline-next-heading) (setq next (point)))
  17812. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17813. (goto-char next)
  17814. (setq leafp (and (looking-at "^\\*+ ")
  17815. (<= (- (match-end 0) (point)) level)))
  17816. (push (list beg end clockpos closedp donep
  17817. total title titlepos level leafp)
  17818. rtn)
  17819. (goto-char cont)))
  17820. (nreverse rtn)))
  17821. ;;;; Agenda, and Diary Integration
  17822. ;;; Define the Org-agenda-mode
  17823. (defvar org-agenda-mode-map (make-sparse-keymap)
  17824. "Keymap for `org-agenda-mode'.")
  17825. (defvar org-agenda-menu) ; defined later in this file.
  17826. (defvar org-agenda-follow-mode nil)
  17827. (defvar org-agenda-show-log nil)
  17828. (defvar org-agenda-redo-command nil)
  17829. (defvar org-agenda-query-string nil)
  17830. (defvar org-agenda-mode-hook nil)
  17831. (defvar org-agenda-type nil)
  17832. (defvar org-agenda-force-single-file nil)
  17833. (defun org-agenda-mode ()
  17834. "Mode for time-sorted view on action items in Org-mode files.
  17835. The following commands are available:
  17836. \\{org-agenda-mode-map}"
  17837. (interactive)
  17838. (kill-all-local-variables)
  17839. (setq org-agenda-undo-list nil
  17840. org-agenda-pending-undo-list nil)
  17841. (setq major-mode 'org-agenda-mode)
  17842. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17843. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17844. (setq mode-name "Org-Agenda")
  17845. (use-local-map org-agenda-mode-map)
  17846. (easy-menu-add org-agenda-menu)
  17847. (if org-startup-truncated (setq truncate-lines t))
  17848. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17849. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17850. ;; Make sure properties are removed when copying text
  17851. (when (boundp 'buffer-substring-filters)
  17852. (org-set-local 'buffer-substring-filters
  17853. (cons (lambda (x)
  17854. (set-text-properties 0 (length x) nil x) x)
  17855. buffer-substring-filters)))
  17856. (unless org-agenda-keep-modes
  17857. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17858. org-agenda-show-log nil))
  17859. (easy-menu-change
  17860. '("Agenda") "Agenda Files"
  17861. (append
  17862. (list
  17863. (vector
  17864. (if (get 'org-agenda-files 'org-restrict)
  17865. "Restricted to single file"
  17866. "Edit File List")
  17867. '(org-edit-agenda-file-list)
  17868. (not (get 'org-agenda-files 'org-restrict)))
  17869. "--")
  17870. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17871. (org-agenda-set-mode-name)
  17872. (apply
  17873. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17874. (list 'org-agenda-mode-hook)))
  17875. (substitute-key-definition 'undo 'org-agenda-undo
  17876. org-agenda-mode-map global-map)
  17877. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17878. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17879. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17880. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17881. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17882. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17883. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17884. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17885. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17886. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17887. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17888. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17889. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17890. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17891. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17892. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17893. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17894. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17895. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17896. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17897. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17898. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17899. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17900. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17901. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17902. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17903. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17904. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17905. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17906. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17907. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17908. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17909. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17910. (while l (org-defkey org-agenda-mode-map
  17911. (int-to-string (pop l)) 'digit-argument)))
  17912. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17913. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17914. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17915. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17916. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17917. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17918. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17919. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17920. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17921. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17922. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17923. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17924. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17925. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17926. (org-defkey org-agenda-mode-map "n" 'next-line)
  17927. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17928. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17929. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17930. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17931. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17932. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17933. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17934. (eval-after-load "calendar"
  17935. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17936. 'org-calendar-goto-agenda))
  17937. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17938. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17939. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17940. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17941. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17942. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17943. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17944. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17945. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17946. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17947. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17948. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17949. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17950. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17951. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17952. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17953. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17954. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17955. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17956. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17957. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17958. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17959. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  17960. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  17961. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  17962. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  17963. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17964. "Local keymap for agenda entries from Org-mode.")
  17965. (org-defkey org-agenda-keymap
  17966. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17967. (org-defkey org-agenda-keymap
  17968. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17969. (when org-agenda-mouse-1-follows-link
  17970. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17971. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17972. '("Agenda"
  17973. ("Agenda Files")
  17974. "--"
  17975. ["Show" org-agenda-show t]
  17976. ["Go To (other window)" org-agenda-goto t]
  17977. ["Go To (this window)" org-agenda-switch-to t]
  17978. ["Follow Mode" org-agenda-follow-mode
  17979. :style toggle :selected org-agenda-follow-mode :active t]
  17980. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17981. "--"
  17982. ["Cycle TODO" org-agenda-todo t]
  17983. ["Archive subtree" org-agenda-archive t]
  17984. ["Delete subtree" org-agenda-kill t]
  17985. "--"
  17986. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17987. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17988. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17989. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17990. "--"
  17991. ("Tags and Properties"
  17992. ["Show all Tags" org-agenda-show-tags t]
  17993. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17994. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17995. "--"
  17996. ["Column View" org-columns t])
  17997. ("Date/Schedule"
  17998. ["Schedule" org-agenda-schedule t]
  17999. ["Set Deadline" org-agenda-deadline t]
  18000. "--"
  18001. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  18002. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  18003. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  18004. ("Clock"
  18005. ["Clock in" org-agenda-clock-in t]
  18006. ["Clock out" org-agenda-clock-out t]
  18007. ["Clock cancel" org-agenda-clock-cancel t]
  18008. ["Goto running clock" org-clock-goto t])
  18009. ("Priority"
  18010. ["Set Priority" org-agenda-priority t]
  18011. ["Increase Priority" org-agenda-priority-up t]
  18012. ["Decrease Priority" org-agenda-priority-down t]
  18013. ["Show Priority" org-agenda-show-priority t])
  18014. ("Calendar/Diary"
  18015. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  18016. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  18017. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  18018. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  18019. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  18020. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  18021. "--"
  18022. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  18023. "--"
  18024. ("View"
  18025. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  18026. :style radio :selected (equal org-agenda-ndays 1)]
  18027. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  18028. :style radio :selected (equal org-agenda-ndays 7)]
  18029. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  18030. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  18031. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  18032. :style radio :selected (member org-agenda-ndays '(365 366))]
  18033. "--"
  18034. ["Show Logbook entries" org-agenda-log-mode
  18035. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  18036. ["Include Diary" org-agenda-toggle-diary
  18037. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  18038. ["Use Time Grid" org-agenda-toggle-time-grid
  18039. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  18040. ["Write view to file" org-write-agenda t]
  18041. ["Rebuild buffer" org-agenda-redo t]
  18042. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  18043. "--"
  18044. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  18045. "--"
  18046. ["Quit" org-agenda-quit t]
  18047. ["Exit and Release Buffers" org-agenda-exit t]
  18048. ))
  18049. ;;; Agenda undo
  18050. (defvar org-agenda-allow-remote-undo t
  18051. "Non-nil means, allow remote undo from the agenda buffer.")
  18052. (defvar org-agenda-undo-list nil
  18053. "List of undoable operations in the agenda since last refresh.")
  18054. (defvar org-agenda-undo-has-started-in nil
  18055. "Buffers that have already seen `undo-start' in the current undo sequence.")
  18056. (defvar org-agenda-pending-undo-list nil
  18057. "In a series of undo commands, this is the list of remaning undo items.")
  18058. (defmacro org-if-unprotected (&rest body)
  18059. "Execute BODY if there is no `org-protected' text property at point."
  18060. (declare (debug t))
  18061. `(unless (get-text-property (point) 'org-protected)
  18062. ,@body))
  18063. (defmacro org-with-remote-undo (_buffer &rest _body)
  18064. "Execute BODY while recording undo information in two buffers."
  18065. (declare (indent 1) (debug t))
  18066. `(let ((_cline (org-current-line))
  18067. (_cmd this-command)
  18068. (_buf1 (current-buffer))
  18069. (_buf2 ,_buffer)
  18070. (_undo1 buffer-undo-list)
  18071. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18072. _c1 _c2)
  18073. ,@_body
  18074. (when org-agenda-allow-remote-undo
  18075. (setq _c1 (org-verify-change-for-undo
  18076. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18077. _c2 (org-verify-change-for-undo
  18078. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18079. (when (or _c1 _c2)
  18080. ;; make sure there are undo boundaries
  18081. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18082. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18083. ;; remember which buffer to undo
  18084. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18085. org-agenda-undo-list)))))
  18086. (defun org-agenda-undo ()
  18087. "Undo a remote editing step in the agenda.
  18088. This undoes changes both in the agenda buffer and in the remote buffer
  18089. that have been changed along."
  18090. (interactive)
  18091. (or org-agenda-allow-remote-undo
  18092. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18093. (if (not (eq this-command last-command))
  18094. (setq org-agenda-undo-has-started-in nil
  18095. org-agenda-pending-undo-list org-agenda-undo-list))
  18096. (if (not org-agenda-pending-undo-list)
  18097. (error "No further undo information"))
  18098. (let* ((entry (pop org-agenda-pending-undo-list))
  18099. buf line cmd rembuf)
  18100. (setq cmd (pop entry) line (pop entry))
  18101. (setq rembuf (nth 2 entry))
  18102. (org-with-remote-undo rembuf
  18103. (while (bufferp (setq buf (pop entry)))
  18104. (if (pop entry)
  18105. (with-current-buffer buf
  18106. (let ((last-undo-buffer buf)
  18107. (inhibit-read-only t))
  18108. (unless (memq buf org-agenda-undo-has-started-in)
  18109. (push buf org-agenda-undo-has-started-in)
  18110. (make-local-variable 'pending-undo-list)
  18111. (undo-start))
  18112. (while (and pending-undo-list
  18113. (listp pending-undo-list)
  18114. (not (car pending-undo-list)))
  18115. (pop pending-undo-list))
  18116. (undo-more 1))))))
  18117. (goto-line line)
  18118. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18119. (defun org-verify-change-for-undo (l1 l2)
  18120. "Verify that a real change occurred between the undo lists L1 and L2."
  18121. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18122. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18123. (not (eq l1 l2)))
  18124. ;;; Agenda dispatch
  18125. (defvar org-agenda-restrict nil)
  18126. (defvar org-agenda-restrict-begin (make-marker))
  18127. (defvar org-agenda-restrict-end (make-marker))
  18128. (defvar org-agenda-last-dispatch-buffer nil)
  18129. (defvar org-agenda-overriding-restriction nil)
  18130. ;;;###autoload
  18131. (defun org-agenda (arg &optional keys restriction)
  18132. "Dispatch agenda commands to collect entries to the agenda buffer.
  18133. Prompts for a command to execute. Any prefix arg will be passed
  18134. on to the selected command. The default selections are:
  18135. a Call `org-agenda-list' to display the agenda for current day or week.
  18136. t Call `org-todo-list' to display the global todo list.
  18137. T Call `org-todo-list' to display the global todo list, select only
  18138. entries with a specific TODO keyword (the user gets a prompt).
  18139. m Call `org-tags-view' to display headlines with tags matching
  18140. a condition (the user is prompted for the condition).
  18141. M Like `m', but select only TODO entries, no ordinary headlines.
  18142. L Create a timeline for the current buffer.
  18143. e Export views to associated files.
  18144. More commands can be added by configuring the variable
  18145. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18146. searches can be pre-defined in this way.
  18147. If the current buffer is in Org-mode and visiting a file, you can also
  18148. first press `<' once to indicate that the agenda should be temporarily
  18149. \(until the next use of \\[org-agenda]) restricted to the current file.
  18150. Pressing `<' twice means to restrict to the current subtree or region
  18151. \(if active)."
  18152. (interactive "P")
  18153. (catch 'exit
  18154. (let* ((prefix-descriptions nil)
  18155. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18156. (org-agenda-custom-commands
  18157. ;; normalize different versions
  18158. (delq nil
  18159. (mapcar
  18160. (lambda (x)
  18161. (cond ((stringp (cdr x))
  18162. (push x prefix-descriptions)
  18163. nil)
  18164. ((stringp (nth 1 x)) x)
  18165. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18166. (t (cons (car x) (cons "" (cdr x))))))
  18167. org-agenda-custom-commands)))
  18168. (buf (current-buffer))
  18169. (bfn (buffer-file-name (buffer-base-buffer)))
  18170. entry key type match lprops ans)
  18171. ;; Turn off restriction unless there is an overriding one
  18172. (unless org-agenda-overriding-restriction
  18173. (put 'org-agenda-files 'org-restrict nil)
  18174. (setq org-agenda-restrict nil)
  18175. (move-marker org-agenda-restrict-begin nil)
  18176. (move-marker org-agenda-restrict-end nil))
  18177. ;; Delete old local properties
  18178. (put 'org-agenda-redo-command 'org-lprops nil)
  18179. ;; Remember where this call originated
  18180. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18181. (unless keys
  18182. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18183. keys (car ans)
  18184. restriction (cdr ans)))
  18185. ;; Estabish the restriction, if any
  18186. (when (and (not org-agenda-overriding-restriction) restriction)
  18187. (put 'org-agenda-files 'org-restrict (list bfn))
  18188. (cond
  18189. ((eq restriction 'region)
  18190. (setq org-agenda-restrict t)
  18191. (move-marker org-agenda-restrict-begin (region-beginning))
  18192. (move-marker org-agenda-restrict-end (region-end)))
  18193. ((eq restriction 'subtree)
  18194. (save-excursion
  18195. (setq org-agenda-restrict t)
  18196. (org-back-to-heading t)
  18197. (move-marker org-agenda-restrict-begin (point))
  18198. (move-marker org-agenda-restrict-end
  18199. (progn (org-end-of-subtree t)))))))
  18200. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18201. ;; For example the todo list should not need it (but does...)
  18202. (cond
  18203. ((setq entry (assoc keys org-agenda-custom-commands))
  18204. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18205. (progn
  18206. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18207. (put 'org-agenda-redo-command 'org-lprops lprops)
  18208. (cond
  18209. ((eq type 'agenda)
  18210. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18211. ((eq type 'alltodo)
  18212. (org-let lprops '(org-todo-list current-prefix-arg)))
  18213. ((eq type 'search)
  18214. (org-let lprops '(org-search-view current-prefix-arg match)))
  18215. ((eq type 'stuck)
  18216. (org-let lprops '(org-agenda-list-stuck-projects
  18217. current-prefix-arg)))
  18218. ((eq type 'tags)
  18219. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18220. ((eq type 'tags-todo)
  18221. (org-let lprops '(org-tags-view '(4) match)))
  18222. ((eq type 'todo)
  18223. (org-let lprops '(org-todo-list match)))
  18224. ((eq type 'tags-tree)
  18225. (org-check-for-org-mode)
  18226. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18227. ((eq type 'todo-tree)
  18228. (org-check-for-org-mode)
  18229. (org-let lprops
  18230. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18231. (regexp-quote match) "\\>"))))
  18232. ((eq type 'occur-tree)
  18233. (org-check-for-org-mode)
  18234. (org-let lprops '(org-occur match)))
  18235. ((functionp type)
  18236. (org-let lprops '(funcall type match)))
  18237. ((fboundp type)
  18238. (org-let lprops '(funcall type match)))
  18239. (t (error "Invalid custom agenda command type %s" type))))
  18240. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18241. ((equal keys "C")
  18242. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18243. (customize-variable 'org-agenda-custom-commands))
  18244. ((equal keys "a") (call-interactively 'org-agenda-list))
  18245. ((equal keys "s") (call-interactively 'org-search-view))
  18246. ((equal keys "t") (call-interactively 'org-todo-list))
  18247. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18248. ((equal keys "m") (call-interactively 'org-tags-view))
  18249. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18250. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18251. ((equal keys "L")
  18252. (unless (org-mode-p)
  18253. (error "This is not an Org-mode file"))
  18254. (unless restriction
  18255. (put 'org-agenda-files 'org-restrict (list bfn))
  18256. (org-call-with-arg 'org-timeline arg)))
  18257. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18258. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18259. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18260. (t (error "Invalid agenda key"))))))
  18261. (defun org-agenda-normalize-custom-commands (cmds)
  18262. (delq nil
  18263. (mapcar
  18264. (lambda (x)
  18265. (cond ((stringp (cdr x)) nil)
  18266. ((stringp (nth 1 x)) x)
  18267. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18268. (t (cons (car x) (cons "" (cdr x))))))
  18269. cmds)))
  18270. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18271. "The user interface for selecting an agenda command."
  18272. (catch 'exit
  18273. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18274. (restrict-ok (and bfn (org-mode-p)))
  18275. (region-p (org-region-active-p))
  18276. (custom org-agenda-custom-commands)
  18277. (selstring "")
  18278. restriction second-time
  18279. c entry key type match prefixes rmheader header-end custom1 desc)
  18280. (save-window-excursion
  18281. (delete-other-windows)
  18282. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18283. (erase-buffer)
  18284. (insert (eval-when-compile
  18285. (let ((header
  18286. "
  18287. Press key for an agenda command: < Buffer,subtree/region restriction
  18288. -------------------------------- > Remove restriction
  18289. a Agenda for current week or day e Export agenda views
  18290. t List of all TODO entries T Entries with special TODO kwd
  18291. m Match a TAGS query M Like m, but only TODO entries
  18292. L Timeline for current buffer # List stuck projects (!=configure)
  18293. s Search for keywords C Configure custom agenda commands
  18294. / Multi-occur
  18295. ")
  18296. (start 0))
  18297. (while (string-match
  18298. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18299. header start)
  18300. (setq start (match-end 0))
  18301. (add-text-properties (match-beginning 2) (match-end 2)
  18302. '(face bold) header))
  18303. header)))
  18304. (setq header-end (move-marker (make-marker) (point)))
  18305. (while t
  18306. (setq custom1 custom)
  18307. (when (eq rmheader t)
  18308. (goto-line 1)
  18309. (re-search-forward ":" nil t)
  18310. (delete-region (match-end 0) (point-at-eol))
  18311. (forward-char 1)
  18312. (looking-at "-+")
  18313. (delete-region (match-end 0) (point-at-eol))
  18314. (move-marker header-end (match-end 0)))
  18315. (goto-char header-end)
  18316. (delete-region (point) (point-max))
  18317. (while (setq entry (pop custom1))
  18318. (setq key (car entry) desc (nth 1 entry)
  18319. type (nth 2 entry) match (nth 3 entry))
  18320. (if (> (length key) 1)
  18321. (add-to-list 'prefixes (string-to-char key))
  18322. (insert
  18323. (format
  18324. "\n%-4s%-14s: %s"
  18325. (org-add-props (copy-sequence key)
  18326. '(face bold))
  18327. (cond
  18328. ((string-match "\\S-" desc) desc)
  18329. ((eq type 'agenda) "Agenda for current week or day")
  18330. ((eq type 'alltodo) "List of all TODO entries")
  18331. ((eq type 'search) "Word search")
  18332. ((eq type 'stuck) "List of stuck projects")
  18333. ((eq type 'todo) "TODO keyword")
  18334. ((eq type 'tags) "Tags query")
  18335. ((eq type 'tags-todo) "Tags (TODO)")
  18336. ((eq type 'tags-tree) "Tags tree")
  18337. ((eq type 'todo-tree) "TODO kwd tree")
  18338. ((eq type 'occur-tree) "Occur tree")
  18339. ((functionp type) (if (symbolp type)
  18340. (symbol-name type)
  18341. "Lambda expression"))
  18342. (t "???"))
  18343. (cond
  18344. ((stringp match)
  18345. (org-add-props match nil 'face 'org-warning))
  18346. (match
  18347. (format "set of %d commands" (length match)))
  18348. (t ""))))))
  18349. (when prefixes
  18350. (mapc (lambda (x)
  18351. (insert
  18352. (format "\n%s %s"
  18353. (org-add-props (char-to-string x)
  18354. nil 'face 'bold)
  18355. (or (cdr (assoc (concat selstring (char-to-string x))
  18356. prefix-descriptions))
  18357. "Prefix key"))))
  18358. prefixes))
  18359. (goto-char (point-min))
  18360. (when (fboundp 'fit-window-to-buffer)
  18361. (if second-time
  18362. (if (not (pos-visible-in-window-p (point-max)))
  18363. (fit-window-to-buffer))
  18364. (setq second-time t)
  18365. (fit-window-to-buffer)))
  18366. (message "Press key for agenda command%s:"
  18367. (if (or restrict-ok org-agenda-overriding-restriction)
  18368. (if org-agenda-overriding-restriction
  18369. " (restriction lock active)"
  18370. (if restriction
  18371. (format " (restricted to %s)" restriction)
  18372. " (unrestricted)"))
  18373. ""))
  18374. (setq c (read-char-exclusive))
  18375. (message "")
  18376. (cond
  18377. ((assoc (char-to-string c) custom)
  18378. (setq selstring (concat selstring (char-to-string c)))
  18379. (throw 'exit (cons selstring restriction)))
  18380. ((memq c prefixes)
  18381. (setq selstring (concat selstring (char-to-string c))
  18382. prefixes nil
  18383. rmheader (or rmheader t)
  18384. custom (delq nil (mapcar
  18385. (lambda (x)
  18386. (if (or (= (length (car x)) 1)
  18387. (/= (string-to-char (car x)) c))
  18388. nil
  18389. (cons (substring (car x) 1) (cdr x))))
  18390. custom))))
  18391. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18392. (message "Restriction is only possible in Org-mode buffers")
  18393. (ding) (sit-for 1))
  18394. ((eq c ?1)
  18395. (org-agenda-remove-restriction-lock 'noupdate)
  18396. (setq restriction 'buffer))
  18397. ((eq c ?0)
  18398. (org-agenda-remove-restriction-lock 'noupdate)
  18399. (setq restriction (if region-p 'region 'subtree)))
  18400. ((eq c ?<)
  18401. (org-agenda-remove-restriction-lock 'noupdate)
  18402. (setq restriction
  18403. (cond
  18404. ((eq restriction 'buffer)
  18405. (if region-p 'region 'subtree))
  18406. ((memq restriction '(subtree region))
  18407. nil)
  18408. (t 'buffer))))
  18409. ((eq c ?>)
  18410. (org-agenda-remove-restriction-lock 'noupdate)
  18411. (setq restriction nil))
  18412. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18413. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18414. ((and (> (length selstring) 0) (eq c ?\d))
  18415. (delete-window)
  18416. (org-agenda-get-restriction-and-command prefix-descriptions))
  18417. ((equal c ?q) (error "Abort"))
  18418. (t (error "Invalid key %c" c))))))))
  18419. (defun org-run-agenda-series (name series)
  18420. (org-prepare-agenda name)
  18421. (let* ((org-agenda-multi t)
  18422. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18423. (cmds (car series))
  18424. (gprops (nth 1 series))
  18425. match ;; The byte compiler incorrectly complains about this. Keep it!
  18426. cmd type lprops)
  18427. (while (setq cmd (pop cmds))
  18428. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18429. (cond
  18430. ((eq type 'agenda)
  18431. (org-let2 gprops lprops
  18432. '(call-interactively 'org-agenda-list)))
  18433. ((eq type 'alltodo)
  18434. (org-let2 gprops lprops
  18435. '(call-interactively 'org-todo-list)))
  18436. ((eq type 'search)
  18437. (org-let2 gprops lprops
  18438. '(org-search-view current-prefix-arg match)))
  18439. ((eq type 'stuck)
  18440. (org-let2 gprops lprops
  18441. '(call-interactively 'org-agenda-list-stuck-projects)))
  18442. ((eq type 'tags)
  18443. (org-let2 gprops lprops
  18444. '(org-tags-view current-prefix-arg match)))
  18445. ((eq type 'tags-todo)
  18446. (org-let2 gprops lprops
  18447. '(org-tags-view '(4) match)))
  18448. ((eq type 'todo)
  18449. (org-let2 gprops lprops
  18450. '(org-todo-list match)))
  18451. ((fboundp type)
  18452. (org-let2 gprops lprops
  18453. '(funcall type match)))
  18454. (t (error "Invalid type in command series"))))
  18455. (widen)
  18456. (setq org-agenda-redo-command redo)
  18457. (goto-char (point-min)))
  18458. (org-finalize-agenda))
  18459. ;;;###autoload
  18460. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18461. "Run an agenda command in batch mode and send the result to STDOUT.
  18462. If CMD-KEY is a string of length 1, it is used as a key in
  18463. `org-agenda-custom-commands' and triggers this command. If it is a
  18464. longer string it is used as a tags/todo match string.
  18465. Paramters are alternating variable names and values that will be bound
  18466. before running the agenda command."
  18467. (let (pars)
  18468. (while parameters
  18469. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18470. (if (> (length cmd-key) 2)
  18471. (eval (list 'let (nreverse pars)
  18472. (list 'org-tags-view nil cmd-key)))
  18473. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18474. (set-buffer org-agenda-buffer-name)
  18475. (princ (org-encode-for-stdout (buffer-string)))))
  18476. (defun org-encode-for-stdout (string)
  18477. (if (fboundp 'encode-coding-string)
  18478. (encode-coding-string string buffer-file-coding-system)
  18479. string))
  18480. (defvar org-agenda-info nil)
  18481. ;;;###autoload
  18482. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18483. "Run an agenda command in batch mode and send the result to STDOUT.
  18484. If CMD-KEY is a string of length 1, it is used as a key in
  18485. `org-agenda-custom-commands' and triggers this command. If it is a
  18486. longer string it is used as a tags/todo match string.
  18487. Paramters are alternating variable names and values that will be bound
  18488. before running the agenda command.
  18489. The output gives a line for each selected agenda item. Each
  18490. item is a list of comma-separated values, like this:
  18491. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18492. category The category of the item
  18493. head The headline, without TODO kwd, TAGS and PRIORITY
  18494. type The type of the agenda entry, can be
  18495. todo selected in TODO match
  18496. tagsmatch selected in tags match
  18497. diary imported from diary
  18498. deadline a deadline on given date
  18499. scheduled scheduled on given date
  18500. timestamp entry has timestamp on given date
  18501. closed entry was closed on given date
  18502. upcoming-deadline warning about deadline
  18503. past-scheduled forwarded scheduled item
  18504. block entry has date block including g. date
  18505. todo The todo keyword, if any
  18506. tags All tags including inherited ones, separated by colons
  18507. date The relevant date, like 2007-2-14
  18508. time The time, like 15:00-16:50
  18509. extra Sting with extra planning info
  18510. priority-l The priority letter if any was given
  18511. priority-n The computed numerical priority
  18512. agenda-day The day in the agenda where this is listed"
  18513. (let (pars)
  18514. (while parameters
  18515. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18516. (push (list 'org-agenda-remove-tags t) pars)
  18517. (if (> (length cmd-key) 2)
  18518. (eval (list 'let (nreverse pars)
  18519. (list 'org-tags-view nil cmd-key)))
  18520. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18521. (set-buffer org-agenda-buffer-name)
  18522. (let* ((lines (org-split-string (buffer-string) "\n"))
  18523. line)
  18524. (while (setq line (pop lines))
  18525. (catch 'next
  18526. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18527. (setq org-agenda-info
  18528. (org-fix-agenda-info (text-properties-at 0 line)))
  18529. (princ
  18530. (org-encode-for-stdout
  18531. (mapconcat 'org-agenda-export-csv-mapper
  18532. '(org-category txt type todo tags date time-of-day extra
  18533. priority-letter priority agenda-day)
  18534. ",")))
  18535. (princ "\n"))))))
  18536. (defun org-fix-agenda-info (props)
  18537. "Make sure all properties on an agenda item have a canonical form,
  18538. so the export commands can easily use it."
  18539. (let (tmp re)
  18540. (when (setq tmp (plist-get props 'tags))
  18541. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18542. (when (setq tmp (plist-get props 'date))
  18543. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18544. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18545. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18546. (setq tmp (calendar-date-string tmp)))
  18547. (setq props (plist-put props 'date tmp)))
  18548. (when (setq tmp (plist-get props 'day))
  18549. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18550. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18551. (setq tmp (calendar-date-string tmp)))
  18552. (setq props (plist-put props 'day tmp))
  18553. (setq props (plist-put props 'agenda-day tmp)))
  18554. (when (setq tmp (plist-get props 'txt))
  18555. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18556. (plist-put props 'priority-letter (match-string 1 tmp))
  18557. (setq tmp (replace-match "" t t tmp)))
  18558. (when (and (setq re (plist-get props 'org-todo-regexp))
  18559. (setq re (concat "\\`\\.*" re " ?"))
  18560. (string-match re tmp))
  18561. (plist-put props 'todo (match-string 1 tmp))
  18562. (setq tmp (replace-match "" t t tmp)))
  18563. (plist-put props 'txt tmp)))
  18564. props)
  18565. (defun org-agenda-export-csv-mapper (prop)
  18566. (let ((res (plist-get org-agenda-info prop)))
  18567. (setq res
  18568. (cond
  18569. ((not res) "")
  18570. ((stringp res) res)
  18571. (t (prin1-to-string res))))
  18572. (while (string-match "," res)
  18573. (setq res (replace-match ";" t t res)))
  18574. (org-trim res)))
  18575. ;;;###autoload
  18576. (defun org-store-agenda-views (&rest parameters)
  18577. (interactive)
  18578. (eval (list 'org-batch-store-agenda-views)))
  18579. ;; FIXME, why is this a macro?????
  18580. ;;;###autoload
  18581. (defmacro org-batch-store-agenda-views (&rest parameters)
  18582. "Run all custom agenda commands that have a file argument."
  18583. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18584. (pop-up-frames nil)
  18585. (dir default-directory)
  18586. pars cmd thiscmdkey files opts)
  18587. (while parameters
  18588. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18589. (setq pars (reverse pars))
  18590. (save-window-excursion
  18591. (while cmds
  18592. (setq cmd (pop cmds)
  18593. thiscmdkey (car cmd)
  18594. opts (nth 4 cmd)
  18595. files (nth 5 cmd))
  18596. (if (stringp files) (setq files (list files)))
  18597. (when files
  18598. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18599. (list 'org-agenda nil thiscmdkey)))
  18600. (set-buffer org-agenda-buffer-name)
  18601. (while files
  18602. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18603. (list 'org-write-agenda
  18604. (expand-file-name (pop files) dir) t))))
  18605. (and (get-buffer org-agenda-buffer-name)
  18606. (kill-buffer org-agenda-buffer-name)))))))
  18607. (defun org-write-agenda (file &optional nosettings)
  18608. "Write the current buffer (an agenda view) as a file.
  18609. Depending on the extension of the file name, plain text (.txt),
  18610. HTML (.html or .htm) or Postscript (.ps) is produced.
  18611. If NOSETTINGS is given, do not scope the settings of
  18612. `org-agenda-exporter-settings' into the export commands. This is used when
  18613. the settings have already been scoped and we do not wish to overrule other,
  18614. higher priority settings."
  18615. (interactive "FWrite agenda to file: ")
  18616. (if (not (file-writable-p file))
  18617. (error "Cannot write agenda to file %s" file))
  18618. (cond
  18619. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18620. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18621. (org-let (if nosettings nil org-agenda-exporter-settings)
  18622. '(save-excursion
  18623. (save-window-excursion
  18624. (cond
  18625. ((string-match "\\.html?\\'" file)
  18626. (set-buffer (htmlize-buffer (current-buffer)))
  18627. (when (and org-agenda-export-html-style
  18628. (string-match "<style>" org-agenda-export-html-style))
  18629. ;; replace <style> section with org-agenda-export-html-style
  18630. (goto-char (point-min))
  18631. (kill-region (- (search-forward "<style") 6)
  18632. (search-forward "</style>"))
  18633. (insert org-agenda-export-html-style))
  18634. (write-file file)
  18635. (kill-buffer (current-buffer))
  18636. (message "HTML written to %s" file))
  18637. ((string-match "\\.ps\\'" file)
  18638. (ps-print-buffer-with-faces file)
  18639. (message "Postscript written to %s" file))
  18640. (t
  18641. (let ((bs (buffer-string)))
  18642. (find-file file)
  18643. (insert bs)
  18644. (save-buffer 0)
  18645. (kill-buffer (current-buffer))
  18646. (message "Plain text written to %s" file))))))
  18647. (set-buffer org-agenda-buffer-name)))
  18648. (defmacro org-no-read-only (&rest body)
  18649. "Inhibit read-only for BODY."
  18650. `(let ((inhibit-read-only t)) ,@body))
  18651. (defun org-check-for-org-mode ()
  18652. "Make sure current buffer is in org-mode. Error if not."
  18653. (or (org-mode-p)
  18654. (error "Cannot execute org-mode agenda command on buffer in %s."
  18655. major-mode)))
  18656. (defun org-fit-agenda-window ()
  18657. "Fit the window to the buffer size."
  18658. (and (memq org-agenda-window-setup '(reorganize-frame))
  18659. (fboundp 'fit-window-to-buffer)
  18660. (fit-window-to-buffer
  18661. nil
  18662. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18663. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18664. ;;; Agenda file list
  18665. (defun org-agenda-files (&optional unrestricted)
  18666. "Get the list of agenda files.
  18667. Optional UNRESTRICTED means return the full list even if a restriction
  18668. is currently in place."
  18669. (let ((files
  18670. (cond
  18671. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18672. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18673. ((listp org-agenda-files) org-agenda-files)
  18674. (t (error "Invalid value of `org-agenda-files'")))))
  18675. (setq files (apply 'append
  18676. (mapcar (lambda (f)
  18677. (if (file-directory-p f)
  18678. (directory-files f t
  18679. org-agenda-file-regexp)
  18680. (list f)))
  18681. files)))
  18682. (if org-agenda-skip-unavailable-files
  18683. (delq nil
  18684. (mapcar (function
  18685. (lambda (file)
  18686. (and (file-readable-p file) file)))
  18687. files))
  18688. files))) ; `org-check-agenda-file' will remove them from the list
  18689. (defun org-edit-agenda-file-list ()
  18690. "Edit the list of agenda files.
  18691. Depending on setup, this either uses customize to edit the variable
  18692. `org-agenda-files', or it visits the file that is holding the list. In the
  18693. latter case, the buffer is set up in a way that saving it automatically kills
  18694. the buffer and restores the previous window configuration."
  18695. (interactive)
  18696. (if (stringp org-agenda-files)
  18697. (let ((cw (current-window-configuration)))
  18698. (find-file org-agenda-files)
  18699. (org-set-local 'org-window-configuration cw)
  18700. (org-add-hook 'after-save-hook
  18701. (lambda ()
  18702. (set-window-configuration
  18703. (prog1 org-window-configuration
  18704. (kill-buffer (current-buffer))))
  18705. (org-install-agenda-files-menu)
  18706. (message "New agenda file list installed"))
  18707. nil 'local)
  18708. (message "%s" (substitute-command-keys
  18709. "Edit list and finish with \\[save-buffer]")))
  18710. (customize-variable 'org-agenda-files)))
  18711. (defun org-store-new-agenda-file-list (list)
  18712. "Set new value for the agenda file list and save it correcly."
  18713. (if (stringp org-agenda-files)
  18714. (let ((f org-agenda-files) b)
  18715. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18716. (with-temp-file f
  18717. (insert (mapconcat 'identity list "\n") "\n")))
  18718. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18719. (setq org-agenda-files list)
  18720. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18721. (defun org-read-agenda-file-list ()
  18722. "Read the list of agenda files from a file."
  18723. (when (stringp org-agenda-files)
  18724. (with-temp-buffer
  18725. (insert-file-contents org-agenda-files)
  18726. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18727. ;;;###autoload
  18728. (defun org-cycle-agenda-files ()
  18729. "Cycle through the files in `org-agenda-files'.
  18730. If the current buffer visits an agenda file, find the next one in the list.
  18731. If the current buffer does not, find the first agenda file."
  18732. (interactive)
  18733. (let* ((fs (org-agenda-files t))
  18734. (files (append fs (list (car fs))))
  18735. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18736. file)
  18737. (unless files (error "No agenda files"))
  18738. (catch 'exit
  18739. (while (setq file (pop files))
  18740. (if (equal (file-truename file) tcf)
  18741. (when (car files)
  18742. (find-file (car files))
  18743. (throw 'exit t))))
  18744. (find-file (car fs)))
  18745. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18746. (defun org-agenda-file-to-front (&optional to-end)
  18747. "Move/add the current file to the top of the agenda file list.
  18748. If the file is not present in the list, it is added to the front. If it is
  18749. present, it is moved there. With optional argument TO-END, add/move to the
  18750. end of the list."
  18751. (interactive "P")
  18752. (let ((org-agenda-skip-unavailable-files nil)
  18753. (file-alist (mapcar (lambda (x)
  18754. (cons (file-truename x) x))
  18755. (org-agenda-files t)))
  18756. (ctf (file-truename buffer-file-name))
  18757. x had)
  18758. (setq x (assoc ctf file-alist) had x)
  18759. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18760. (if to-end
  18761. (setq file-alist (append (delq x file-alist) (list x)))
  18762. (setq file-alist (cons x (delq x file-alist))))
  18763. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18764. (org-install-agenda-files-menu)
  18765. (message "File %s to %s of agenda file list"
  18766. (if had "moved" "added") (if to-end "end" "front"))))
  18767. (defun org-remove-file (&optional file)
  18768. "Remove current file from the list of files in variable `org-agenda-files'.
  18769. These are the files which are being checked for agenda entries.
  18770. Optional argument FILE means, use this file instead of the current."
  18771. (interactive)
  18772. (let* ((org-agenda-skip-unavailable-files nil)
  18773. (file (or file buffer-file-name))
  18774. (true-file (file-truename file))
  18775. (afile (abbreviate-file-name file))
  18776. (files (delq nil (mapcar
  18777. (lambda (x)
  18778. (if (equal true-file
  18779. (file-truename x))
  18780. nil x))
  18781. (org-agenda-files t)))))
  18782. (if (not (= (length files) (length (org-agenda-files t))))
  18783. (progn
  18784. (org-store-new-agenda-file-list files)
  18785. (org-install-agenda-files-menu)
  18786. (message "Removed file: %s" afile))
  18787. (message "File was not in list: %s (not removed)" afile))))
  18788. (defun org-file-menu-entry (file)
  18789. (vector file (list 'find-file file) t))
  18790. (defun org-check-agenda-file (file)
  18791. "Make sure FILE exists. If not, ask user what to do."
  18792. (when (not (file-exists-p file))
  18793. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18794. (abbreviate-file-name file))
  18795. (let ((r (downcase (read-char-exclusive))))
  18796. (cond
  18797. ((equal r ?r)
  18798. (org-remove-file file)
  18799. (throw 'nextfile t))
  18800. (t (error "Abort"))))))
  18801. ;;; Agenda prepare and finalize
  18802. (defvar org-agenda-multi nil) ; dynammically scoped
  18803. (defvar org-agenda-buffer-name "*Org Agenda*")
  18804. (defvar org-pre-agenda-window-conf nil)
  18805. (defvar org-agenda-name nil)
  18806. (defun org-prepare-agenda (&optional name)
  18807. (setq org-todo-keywords-for-agenda nil)
  18808. (setq org-done-keywords-for-agenda nil)
  18809. (if org-agenda-multi
  18810. (progn
  18811. (setq buffer-read-only nil)
  18812. (goto-char (point-max))
  18813. (unless (or (bobp) org-agenda-compact-blocks)
  18814. (insert "\n" (make-string (window-width) ?=) "\n"))
  18815. (narrow-to-region (point) (point-max)))
  18816. (org-agenda-reset-markers)
  18817. (org-prepare-agenda-buffers (org-agenda-files))
  18818. (setq org-todo-keywords-for-agenda
  18819. (org-uniquify org-todo-keywords-for-agenda))
  18820. (setq org-done-keywords-for-agenda
  18821. (org-uniquify org-done-keywords-for-agenda))
  18822. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18823. (awin (get-buffer-window abuf)))
  18824. (cond
  18825. ((equal (current-buffer) abuf) nil)
  18826. (awin (select-window awin))
  18827. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18828. ((equal org-agenda-window-setup 'current-window)
  18829. (switch-to-buffer abuf))
  18830. ((equal org-agenda-window-setup 'other-window)
  18831. (org-switch-to-buffer-other-window abuf))
  18832. ((equal org-agenda-window-setup 'other-frame)
  18833. (switch-to-buffer-other-frame abuf))
  18834. ((equal org-agenda-window-setup 'reorganize-frame)
  18835. (delete-other-windows)
  18836. (org-switch-to-buffer-other-window abuf))))
  18837. (setq buffer-read-only nil)
  18838. (erase-buffer)
  18839. (org-agenda-mode)
  18840. (and name (not org-agenda-name)
  18841. (org-set-local 'org-agenda-name name)))
  18842. (setq buffer-read-only nil))
  18843. (defun org-finalize-agenda ()
  18844. "Finishing touch for the agenda buffer, called just before displaying it."
  18845. (unless org-agenda-multi
  18846. (save-excursion
  18847. (let ((inhibit-read-only t))
  18848. (goto-char (point-min))
  18849. (while (org-activate-bracket-links (point-max))
  18850. (add-text-properties (match-beginning 0) (match-end 0)
  18851. '(face org-link)))
  18852. (org-agenda-align-tags)
  18853. (unless org-agenda-with-colors
  18854. (remove-text-properties (point-min) (point-max) '(face nil))))
  18855. (if (and (boundp 'org-overriding-columns-format)
  18856. org-overriding-columns-format)
  18857. (org-set-local 'org-overriding-columns-format
  18858. org-overriding-columns-format))
  18859. (if (and (boundp 'org-agenda-view-columns-initially)
  18860. org-agenda-view-columns-initially)
  18861. (org-agenda-columns))
  18862. (when org-agenda-fontify-priorities
  18863. (org-fontify-priorities))
  18864. (run-hooks 'org-finalize-agenda-hook)
  18865. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18866. )))
  18867. (defun org-fontify-priorities ()
  18868. "Make highest priority lines bold, and lowest italic."
  18869. (interactive)
  18870. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18871. (org-delete-overlay o)))
  18872. (org-overlays-in (point-min) (point-max)))
  18873. (save-excursion
  18874. (let ((inhibit-read-only t)
  18875. b e p ov h l)
  18876. (goto-char (point-min))
  18877. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18878. (setq h (or (get-char-property (point) 'org-highest-priority)
  18879. org-highest-priority)
  18880. l (or (get-char-property (point) 'org-lowest-priority)
  18881. org-lowest-priority)
  18882. p (string-to-char (match-string 1))
  18883. b (match-beginning 0) e (point-at-eol)
  18884. ov (org-make-overlay b e))
  18885. (org-overlay-put
  18886. ov 'face
  18887. (cond ((listp org-agenda-fontify-priorities)
  18888. (cdr (assoc p org-agenda-fontify-priorities)))
  18889. ((equal p l) 'italic)
  18890. ((equal p h) 'bold)))
  18891. (org-overlay-put ov 'org-type 'org-priority)))))
  18892. (defun org-prepare-agenda-buffers (files)
  18893. "Create buffers for all agenda files, protect archived trees and comments."
  18894. (interactive)
  18895. (let ((pa '(:org-archived t))
  18896. (pc '(:org-comment t))
  18897. (pall '(:org-archived t :org-comment t))
  18898. (inhibit-read-only t)
  18899. (rea (concat ":" org-archive-tag ":"))
  18900. bmp file re)
  18901. (save-excursion
  18902. (save-restriction
  18903. (while (setq file (pop files))
  18904. (if (bufferp file)
  18905. (set-buffer file)
  18906. (org-check-agenda-file file)
  18907. (set-buffer (org-get-agenda-file-buffer file)))
  18908. (widen)
  18909. (setq bmp (buffer-modified-p))
  18910. (org-refresh-category-properties)
  18911. (setq org-todo-keywords-for-agenda
  18912. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18913. (setq org-done-keywords-for-agenda
  18914. (append org-done-keywords-for-agenda org-done-keywords))
  18915. (save-excursion
  18916. (remove-text-properties (point-min) (point-max) pall)
  18917. (when org-agenda-skip-archived-trees
  18918. (goto-char (point-min))
  18919. (while (re-search-forward rea nil t)
  18920. (if (org-on-heading-p t)
  18921. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18922. (goto-char (point-min))
  18923. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18924. (while (re-search-forward re nil t)
  18925. (add-text-properties
  18926. (match-beginning 0) (org-end-of-subtree t) pc)))
  18927. (set-buffer-modified-p bmp))))))
  18928. (defvar org-agenda-skip-function nil
  18929. "Function to be called at each match during agenda construction.
  18930. If this function returns nil, the current match should not be skipped.
  18931. Otherwise, the function must return a position from where the search
  18932. should be continued.
  18933. This may also be a Lisp form, it will be evaluated.
  18934. Never set this variable using `setq' or so, because then it will apply
  18935. to all future agenda commands. Instead, bind it with `let' to scope
  18936. it dynamically into the agenda-constructing command. A good way to set
  18937. it is through options in org-agenda-custom-commands.")
  18938. (defun org-agenda-skip ()
  18939. "Throw to `:skip' in places that should be skipped.
  18940. Also moves point to the end of the skipped region, so that search can
  18941. continue from there."
  18942. (let ((p (point-at-bol)) to fp)
  18943. (and org-agenda-skip-archived-trees
  18944. (get-text-property p :org-archived)
  18945. (org-end-of-subtree t)
  18946. (throw :skip t))
  18947. (and (get-text-property p :org-comment)
  18948. (org-end-of-subtree t)
  18949. (throw :skip t))
  18950. (if (equal (char-after p) ?#) (throw :skip t))
  18951. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18952. (consp org-agenda-skip-function))
  18953. (setq to (save-excursion
  18954. (save-match-data
  18955. (if fp
  18956. (funcall org-agenda-skip-function)
  18957. (eval org-agenda-skip-function))))))
  18958. (goto-char to)
  18959. (throw :skip t))))
  18960. (defvar org-agenda-markers nil
  18961. "List of all currently active markers created by `org-agenda'.")
  18962. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18963. "Creation time of the last agenda marker.")
  18964. (defun org-agenda-new-marker (&optional pos)
  18965. "Return a new agenda marker.
  18966. Org-mode keeps a list of these markers and resets them when they are
  18967. no longer in use."
  18968. (let ((m (copy-marker (or pos (point)))))
  18969. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18970. (push m org-agenda-markers)
  18971. m))
  18972. (defun org-agenda-reset-markers ()
  18973. "Reset markers created by `org-agenda'."
  18974. (while org-agenda-markers
  18975. (move-marker (pop org-agenda-markers) nil)))
  18976. (defun org-get-agenda-file-buffer (file)
  18977. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18978. it to the list of buffers which might be released later."
  18979. (let ((buf (org-find-base-buffer-visiting file)))
  18980. (if buf
  18981. buf ; just return it
  18982. ;; Make a new buffer and remember it
  18983. (setq buf (find-file-noselect file))
  18984. (if buf (push buf org-agenda-new-buffers))
  18985. buf)))
  18986. (defun org-release-buffers (blist)
  18987. "Release all buffers in list, asking the user for confirmation when needed.
  18988. When a buffer is unmodified, it is just killed. When modified, it is saved
  18989. \(if the user agrees) and then killed."
  18990. (let (buf file)
  18991. (while (setq buf (pop blist))
  18992. (setq file (buffer-file-name buf))
  18993. (when (and (buffer-modified-p buf)
  18994. file
  18995. (y-or-n-p (format "Save file %s? " file)))
  18996. (with-current-buffer buf (save-buffer)))
  18997. (kill-buffer buf))))
  18998. (defun org-get-category (&optional pos)
  18999. "Get the category applying to position POS."
  19000. (get-text-property (or pos (point)) 'org-category))
  19001. ;;; Agenda timeline
  19002. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  19003. (defun org-timeline (&optional include-all)
  19004. "Show a time-sorted view of the entries in the current org file.
  19005. Only entries with a time stamp of today or later will be listed. With
  19006. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  19007. under the current date.
  19008. If the buffer contains an active region, only check the region for
  19009. dates."
  19010. (interactive "P")
  19011. (require 'calendar)
  19012. (org-compile-prefix-format 'timeline)
  19013. (org-set-sorting-strategy 'timeline)
  19014. (let* ((dopast t)
  19015. (dotodo include-all)
  19016. (doclosed org-agenda-show-log)
  19017. (entry buffer-file-name)
  19018. (date (calendar-current-date))
  19019. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  19020. (end (if (org-region-active-p) (region-end) (point-max)))
  19021. (day-numbers (org-get-all-dates beg end 'no-ranges
  19022. t doclosed ; always include today
  19023. org-timeline-show-empty-dates))
  19024. (org-deadline-warning-days 0)
  19025. (org-agenda-only-exact-dates t)
  19026. (today (time-to-days (current-time)))
  19027. (past t)
  19028. args
  19029. s e rtn d emptyp)
  19030. (setq org-agenda-redo-command
  19031. (list 'progn
  19032. (list 'org-switch-to-buffer-other-window (current-buffer))
  19033. (list 'org-timeline (list 'quote include-all))))
  19034. (if (not dopast)
  19035. ;; Remove past dates from the list of dates.
  19036. (setq day-numbers (delq nil (mapcar (lambda(x)
  19037. (if (>= x today) x nil))
  19038. day-numbers))))
  19039. (org-prepare-agenda (concat "Timeline "
  19040. (file-name-nondirectory buffer-file-name)))
  19041. (if doclosed (push :closed args))
  19042. (push :timestamp args)
  19043. (push :deadline args)
  19044. (push :scheduled args)
  19045. (push :sexp args)
  19046. (if dotodo (push :todo args))
  19047. (while (setq d (pop day-numbers))
  19048. (if (and (listp d) (eq (car d) :omitted))
  19049. (progn
  19050. (setq s (point))
  19051. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  19052. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  19053. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  19054. (if (and (>= d today)
  19055. dopast
  19056. past)
  19057. (progn
  19058. (setq past nil)
  19059. (insert (make-string 79 ?-) "\n")))
  19060. (setq date (calendar-gregorian-from-absolute d))
  19061. (setq s (point))
  19062. (setq rtn (and (not emptyp)
  19063. (apply 'org-agenda-get-day-entries entry
  19064. date args)))
  19065. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  19066. (progn
  19067. (insert
  19068. (if (stringp org-agenda-format-date)
  19069. (format-time-string org-agenda-format-date
  19070. (org-time-from-absolute date))
  19071. (funcall org-agenda-format-date date))
  19072. "\n")
  19073. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19074. (put-text-property s (1- (point)) 'org-date-line t)
  19075. (if (equal d today)
  19076. (put-text-property s (1- (point)) 'org-today t))
  19077. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  19078. (put-text-property s (1- (point)) 'day d)))))
  19079. (goto-char (point-min))
  19080. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19081. (point-min)))
  19082. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19083. (org-finalize-agenda)
  19084. (setq buffer-read-only t)))
  19085. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19086. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19087. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19088. not every single day in the range. If FORCE-TODAY is non-nil, make
  19089. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19090. inactive time stamps (those in square brackets) are included.
  19091. When EMPTY is non-nil, also include days without any entries."
  19092. (let ((re (concat
  19093. (if pre-re pre-re "")
  19094. (if inactive org-ts-regexp-both org-ts-regexp)))
  19095. dates dates1 date day day1 day2 ts1 ts2)
  19096. (if force-today
  19097. (setq dates (list (time-to-days (current-time)))))
  19098. (save-excursion
  19099. (goto-char beg)
  19100. (while (re-search-forward re end t)
  19101. (setq day (time-to-days (org-time-string-to-time
  19102. (substring (match-string 1) 0 10))))
  19103. (or (memq day dates) (push day dates)))
  19104. (unless no-ranges
  19105. (goto-char beg)
  19106. (while (re-search-forward org-tr-regexp end t)
  19107. (setq ts1 (substring (match-string 1) 0 10)
  19108. ts2 (substring (match-string 2) 0 10)
  19109. day1 (time-to-days (org-time-string-to-time ts1))
  19110. day2 (time-to-days (org-time-string-to-time ts2)))
  19111. (while (< (setq day1 (1+ day1)) day2)
  19112. (or (memq day1 dates) (push day1 dates)))))
  19113. (setq dates (sort dates '<))
  19114. (when empty
  19115. (while (setq day (pop dates))
  19116. (setq day2 (car dates))
  19117. (push day dates1)
  19118. (when (and day2 empty)
  19119. (if (or (eq empty t)
  19120. (and (numberp empty) (<= (- day2 day) empty)))
  19121. (while (< (setq day (1+ day)) day2)
  19122. (push (list day) dates1))
  19123. (push (cons :omitted (- day2 day)) dates1))))
  19124. (setq dates (nreverse dates1)))
  19125. dates)))
  19126. ;;; Agenda Daily/Weekly
  19127. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19128. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19129. (defvar org-agenda-last-arguments nil
  19130. "The arguments of the previous call to org-agenda")
  19131. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19132. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19133. (defvar org-include-all-loc nil) ; local variable
  19134. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19135. ;;;###autoload
  19136. (defun org-agenda-list (&optional include-all start-day ndays)
  19137. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19138. The view will be for the current day or week, but from the overview buffer
  19139. you will be able to go to other days/weeks.
  19140. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19141. all unfinished TODO items will also be shown, before the agenda.
  19142. This feature is considered obsolete, please use the TODO list or a block
  19143. agenda instead.
  19144. With a numeric prefix argument in an interactive call, the agenda will
  19145. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19146. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19147. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19148. given in `org-agenda-start-on-weekday'."
  19149. (interactive "P")
  19150. (if (and (integerp include-all) (> include-all 0))
  19151. (setq ndays include-all include-all nil))
  19152. (setq ndays (or ndays org-agenda-ndays)
  19153. start-day (or start-day org-agenda-start-day))
  19154. (if org-agenda-overriding-arguments
  19155. (setq include-all (car org-agenda-overriding-arguments)
  19156. start-day (nth 1 org-agenda-overriding-arguments)
  19157. ndays (nth 2 org-agenda-overriding-arguments)))
  19158. (if (stringp start-day)
  19159. ;; Convert to an absolute day number
  19160. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19161. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19162. (org-compile-prefix-format 'agenda)
  19163. (org-set-sorting-strategy 'agenda)
  19164. (require 'calendar)
  19165. (let* ((org-agenda-start-on-weekday
  19166. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19167. org-agenda-start-on-weekday nil))
  19168. (thefiles (org-agenda-files))
  19169. (files thefiles)
  19170. (today (time-to-days
  19171. (time-subtract (current-time)
  19172. (list 0 (* 3600 org-extend-today-until) 0))))
  19173. (sd (or start-day today))
  19174. (start (if (or (null org-agenda-start-on-weekday)
  19175. (< org-agenda-ndays 7))
  19176. sd
  19177. (let* ((nt (calendar-day-of-week
  19178. (calendar-gregorian-from-absolute sd)))
  19179. (n1 org-agenda-start-on-weekday)
  19180. (d (- nt n1)))
  19181. (- sd (+ (if (< d 0) 7 0) d)))))
  19182. (day-numbers (list start))
  19183. (day-cnt 0)
  19184. (inhibit-redisplay (not debug-on-error))
  19185. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19186. (setq org-agenda-redo-command
  19187. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19188. ;; Make the list of days
  19189. (setq ndays (or ndays org-agenda-ndays)
  19190. nd ndays)
  19191. (while (> ndays 1)
  19192. (push (1+ (car day-numbers)) day-numbers)
  19193. (setq ndays (1- ndays)))
  19194. (setq day-numbers (nreverse day-numbers))
  19195. (org-prepare-agenda "Day/Week")
  19196. (org-set-local 'org-starting-day (car day-numbers))
  19197. (org-set-local 'org-include-all-loc include-all)
  19198. (org-set-local 'org-agenda-span
  19199. (org-agenda-ndays-to-span nd))
  19200. (when (and (or include-all org-agenda-include-all-todo)
  19201. (member today day-numbers))
  19202. (setq files thefiles
  19203. rtnall nil)
  19204. (while (setq file (pop files))
  19205. (catch 'nextfile
  19206. (org-check-agenda-file file)
  19207. (setq date (calendar-gregorian-from-absolute today)
  19208. rtn (org-agenda-get-day-entries
  19209. file date :todo))
  19210. (setq rtnall (append rtnall rtn))))
  19211. (when rtnall
  19212. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19213. (add-text-properties (point-min) (1- (point))
  19214. (list 'face 'org-agenda-structure))
  19215. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19216. (unless org-agenda-compact-blocks
  19217. (setq s (point))
  19218. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19219. "-agenda:\n")
  19220. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19221. 'org-date-line t)))
  19222. (while (setq d (pop day-numbers))
  19223. (setq date (calendar-gregorian-from-absolute d)
  19224. s (point))
  19225. (if (or (setq todayp (= d today))
  19226. (and (not start-pos) (= d sd)))
  19227. (setq start-pos (point))
  19228. (if (and start-pos (not end-pos))
  19229. (setq end-pos (point))))
  19230. (setq files thefiles
  19231. rtnall nil)
  19232. (while (setq file (pop files))
  19233. (catch 'nextfile
  19234. (org-check-agenda-file file)
  19235. (if org-agenda-show-log
  19236. (setq rtn (org-agenda-get-day-entries
  19237. file date
  19238. :deadline :scheduled :timestamp :sexp :closed))
  19239. (setq rtn (org-agenda-get-day-entries
  19240. file date
  19241. :deadline :scheduled :sexp :timestamp)))
  19242. (setq rtnall (append rtnall rtn))))
  19243. (if org-agenda-include-diary
  19244. (progn
  19245. (require 'diary-lib)
  19246. (setq rtn (org-get-entries-from-diary date))
  19247. (setq rtnall (append rtnall rtn))))
  19248. (if (or rtnall org-agenda-show-all-dates)
  19249. (progn
  19250. (setq day-cnt (1+ day-cnt))
  19251. (insert
  19252. (if (stringp org-agenda-format-date)
  19253. (format-time-string org-agenda-format-date
  19254. (org-time-from-absolute date))
  19255. (funcall org-agenda-format-date date))
  19256. "\n")
  19257. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19258. (put-text-property s (1- (point)) 'org-date-line t)
  19259. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19260. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19261. (if rtnall (insert
  19262. (org-finalize-agenda-entries
  19263. (org-agenda-add-time-grid-maybe
  19264. rtnall nd todayp))
  19265. "\n"))
  19266. (put-text-property s (1- (point)) 'day d)
  19267. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19268. (goto-char (point-min))
  19269. (org-fit-agenda-window)
  19270. (unless (and (pos-visible-in-window-p (point-min))
  19271. (pos-visible-in-window-p (point-max)))
  19272. (goto-char (1- (point-max)))
  19273. (recenter -1)
  19274. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19275. (progn
  19276. (goto-char (or start-pos 1))
  19277. (recenter 1))))
  19278. (goto-char (or start-pos 1))
  19279. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19280. (org-finalize-agenda)
  19281. (setq buffer-read-only t)
  19282. (message "")))
  19283. (defun org-agenda-ndays-to-span (n)
  19284. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19285. ;;; Agenda word search
  19286. (defvar org-agenda-search-history nil)
  19287. ;;;###autoload
  19288. (defun org-search-view (&optional arg string)
  19289. "Show all entries that contain words or regular expressions.
  19290. If the first character of the search string is an asterisks,
  19291. search only the headlines.
  19292. The search string is broken into \"words\" by splitting at whitespace.
  19293. The individual words are then interpreted as a boolean expression with
  19294. logical AND. Words prefixed with a minus must not occur in the entry.
  19295. Words without a prefix or prefixed with a plus must occur in the entry.
  19296. Matching is case-insensitive and the words are enclosed by word delimiters.
  19297. Words enclosed by curly braces are interpreted as regular expressions
  19298. that must or must not match in the entry.
  19299. This command searches the agenda files, and in addition the files listed
  19300. in `org-agenda-text-search-extra-files'."
  19301. (interactive "P")
  19302. (org-compile-prefix-format 'search)
  19303. (org-set-sorting-strategy 'search)
  19304. (org-prepare-agenda "SEARCH")
  19305. (let* ((props (list 'face nil
  19306. 'done-face 'org-done
  19307. 'org-not-done-regexp org-not-done-regexp
  19308. 'org-todo-regexp org-todo-regexp
  19309. 'mouse-face 'highlight
  19310. 'keymap org-agenda-keymap
  19311. 'help-echo (format "mouse-2 or RET jump to location")))
  19312. regexp rtn rtnall files file pos
  19313. marker priority category tags c neg re
  19314. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  19315. (unless (and (not arg)
  19316. (stringp string)
  19317. (string-match "\\S-" string))
  19318. (setq string (read-string "[+-]Word/{Regexp} ...: "
  19319. (cond
  19320. ((integerp arg) (cons string arg))
  19321. (arg string))
  19322. 'org-agenda-search-history)))
  19323. (setq org-agenda-redo-command
  19324. (list 'org-search-view 'current-prefix-arg string))
  19325. (setq org-agenda-query-string string)
  19326. (if (equal (string-to-char string) ?*)
  19327. (setq hdl-only t
  19328. words (substring string 1))
  19329. (setq words string))
  19330. (setq words (org-split-string words))
  19331. (mapc (lambda (w)
  19332. (setq c (string-to-char w))
  19333. (if (equal c ?-)
  19334. (setq neg t w (substring w 1))
  19335. (if (equal c ?+)
  19336. (setq neg nil w (substring w 1))
  19337. (setq neg nil)))
  19338. (if (string-match "\\`{.*}\\'" w)
  19339. (setq re (substring w 1 -1))
  19340. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  19341. (if neg (push re regexps-) (push re regexps+)))
  19342. words)
  19343. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  19344. (if (not regexps+)
  19345. (setq regexp (concat "^" org-outline-regexp))
  19346. (setq regexp (pop regexps+))
  19347. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  19348. regexp))))
  19349. (setq files (append (org-agenda-files) org-agenda-text-search-extra-files)
  19350. rtnall nil)
  19351. (while (setq file (pop files))
  19352. (setq ee nil)
  19353. (catch 'nextfile
  19354. (org-check-agenda-file file)
  19355. (setq buffer (if (file-exists-p file)
  19356. (org-get-agenda-file-buffer file)
  19357. (error "No such file %s" file)))
  19358. (if (not buffer)
  19359. ;; If file does not exist, make sure an error message is sent
  19360. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19361. file))))
  19362. (with-current-buffer buffer
  19363. (unless (org-mode-p)
  19364. (error "Agenda file %s is not in `org-mode'" file))
  19365. (let ((case-fold-search t))
  19366. (save-excursion
  19367. (save-restriction
  19368. (if org-agenda-restrict
  19369. (narrow-to-region org-agenda-restrict-begin
  19370. org-agenda-restrict-end)
  19371. (widen))
  19372. (goto-char (point-min))
  19373. (unless (or (org-on-heading-p)
  19374. (outline-next-heading))
  19375. (throw 'nextfile t))
  19376. (goto-char (max (point-min) (1- (point))))
  19377. (while (re-search-forward regexp nil t)
  19378. (org-back-to-heading t)
  19379. (skip-chars-forward "* ")
  19380. (setq beg (point-at-bol)
  19381. beg1 (point)
  19382. end (progn (outline-next-heading) (point)))
  19383. (catch :skip
  19384. (goto-char beg)
  19385. (org-agenda-skip)
  19386. (setq str (buffer-substring-no-properties
  19387. (point-at-bol)
  19388. (if hdl-only (point-at-eol) end)))
  19389. (mapc (lambda (wr) (when (string-match wr str)
  19390. (goto-char (1- end))
  19391. (throw :skip t)))
  19392. regexps-)
  19393. (mapc (lambda (wr) (unless (string-match wr str)
  19394. (goto-char (1- end))
  19395. (throw :skip t)))
  19396. regexps+)
  19397. (goto-char beg)
  19398. (setq marker (org-agenda-new-marker (point))
  19399. category (org-get-category)
  19400. tags (org-get-tags-at (point))
  19401. txt (org-format-agenda-item
  19402. ""
  19403. (buffer-substring-no-properties
  19404. beg1 (point-at-eol))
  19405. category tags))
  19406. (org-add-props txt props
  19407. 'org-marker marker 'org-hd-marker marker
  19408. 'priority 1000 'org-category category
  19409. 'type "search")
  19410. (push txt ee)
  19411. (goto-char (1- end)))))))))
  19412. (setq rtn (nreverse ee))
  19413. (setq rtnall (append rtnall rtn)))
  19414. (if org-agenda-overriding-header
  19415. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19416. nil 'face 'org-agenda-structure) "\n")
  19417. (insert "Search words: ")
  19418. (add-text-properties (point-min) (1- (point))
  19419. (list 'face 'org-agenda-structure))
  19420. (setq pos (point))
  19421. (insert string "\n")
  19422. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19423. (setq pos (point))
  19424. (unless org-agenda-multi
  19425. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  19426. (add-text-properties pos (1- (point))
  19427. (list 'face 'org-agenda-structure))))
  19428. (when rtnall
  19429. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19430. (goto-char (point-min))
  19431. (org-fit-agenda-window)
  19432. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19433. (org-finalize-agenda)
  19434. (setq buffer-read-only t)))
  19435. ;;; Agenda TODO list
  19436. (defvar org-select-this-todo-keyword nil)
  19437. (defvar org-last-arg nil)
  19438. ;;;###autoload
  19439. (defun org-todo-list (arg)
  19440. "Show all TODO entries from all agenda file in a single list.
  19441. The prefix arg can be used to select a specific TODO keyword and limit
  19442. the list to these. When using \\[universal-argument], you will be prompted
  19443. for a keyword. A numeric prefix directly selects the Nth keyword in
  19444. `org-todo-keywords-1'."
  19445. (interactive "P")
  19446. (require 'calendar)
  19447. (org-compile-prefix-format 'todo)
  19448. (org-set-sorting-strategy 'todo)
  19449. (org-prepare-agenda "TODO")
  19450. (let* ((today (time-to-days (current-time)))
  19451. (date (calendar-gregorian-from-absolute today))
  19452. (kwds org-todo-keywords-for-agenda)
  19453. (completion-ignore-case t)
  19454. (org-select-this-todo-keyword
  19455. (if (stringp arg) arg
  19456. (and arg (integerp arg) (> arg 0)
  19457. (nth (1- arg) kwds))))
  19458. rtn rtnall files file pos)
  19459. (when (equal arg '(4))
  19460. (setq org-select-this-todo-keyword
  19461. (completing-read "Keyword (or KWD1|K2D2|...): "
  19462. (mapcar 'list kwds) nil nil)))
  19463. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19464. (org-set-local 'org-last-arg arg)
  19465. (setq org-agenda-redo-command
  19466. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19467. (setq files (org-agenda-files)
  19468. rtnall nil)
  19469. (while (setq file (pop files))
  19470. (catch 'nextfile
  19471. (org-check-agenda-file file)
  19472. (setq rtn (org-agenda-get-day-entries file date :todo))
  19473. (setq rtnall (append rtnall rtn))))
  19474. (if org-agenda-overriding-header
  19475. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19476. nil 'face 'org-agenda-structure) "\n")
  19477. (insert "Global list of TODO items of type: ")
  19478. (add-text-properties (point-min) (1- (point))
  19479. (list 'face 'org-agenda-structure))
  19480. (setq pos (point))
  19481. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19482. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19483. (setq pos (point))
  19484. (unless org-agenda-multi
  19485. (insert "Available with `N r': (0)ALL")
  19486. (let ((n 0) s)
  19487. (mapc (lambda (x)
  19488. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19489. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19490. (insert "\n "))
  19491. (insert " " s))
  19492. kwds))
  19493. (insert "\n"))
  19494. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19495. (when rtnall
  19496. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19497. (goto-char (point-min))
  19498. (org-fit-agenda-window)
  19499. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19500. (org-finalize-agenda)
  19501. (setq buffer-read-only t)))
  19502. ;;; Agenda tags match
  19503. ;;;###autoload
  19504. (defun org-tags-view (&optional todo-only match)
  19505. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19506. The prefix arg TODO-ONLY limits the search to TODO entries."
  19507. (interactive "P")
  19508. (org-compile-prefix-format 'tags)
  19509. (org-set-sorting-strategy 'tags)
  19510. (let* ((org-tags-match-list-sublevels
  19511. (if todo-only t org-tags-match-list-sublevels))
  19512. (completion-ignore-case t)
  19513. rtn rtnall files file pos matcher
  19514. buffer)
  19515. (setq matcher (org-make-tags-matcher match)
  19516. match (car matcher) matcher (cdr matcher))
  19517. (org-prepare-agenda (concat "TAGS " match))
  19518. (setq org-agenda-redo-command
  19519. (list 'org-tags-view (list 'quote todo-only)
  19520. (list 'if 'current-prefix-arg nil match)))
  19521. (setq files (org-agenda-files)
  19522. rtnall nil)
  19523. (while (setq file (pop files))
  19524. (catch 'nextfile
  19525. (org-check-agenda-file file)
  19526. (setq buffer (if (file-exists-p file)
  19527. (org-get-agenda-file-buffer file)
  19528. (error "No such file %s" file)))
  19529. (if (not buffer)
  19530. ;; If file does not exist, merror message to agenda
  19531. (setq rtn (list
  19532. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19533. rtnall (append rtnall rtn))
  19534. (with-current-buffer buffer
  19535. (unless (org-mode-p)
  19536. (error "Agenda file %s is not in `org-mode'" file))
  19537. (save-excursion
  19538. (save-restriction
  19539. (if org-agenda-restrict
  19540. (narrow-to-region org-agenda-restrict-begin
  19541. org-agenda-restrict-end)
  19542. (widen))
  19543. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19544. (setq rtnall (append rtnall rtn))))))))
  19545. (if org-agenda-overriding-header
  19546. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19547. nil 'face 'org-agenda-structure) "\n")
  19548. (insert "Headlines with TAGS match: ")
  19549. (add-text-properties (point-min) (1- (point))
  19550. (list 'face 'org-agenda-structure))
  19551. (setq pos (point))
  19552. (insert match "\n")
  19553. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19554. (setq pos (point))
  19555. (unless org-agenda-multi
  19556. (insert "Press `C-u r' to search again with new search string\n"))
  19557. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19558. (when rtnall
  19559. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19560. (goto-char (point-min))
  19561. (org-fit-agenda-window)
  19562. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19563. (org-finalize-agenda)
  19564. (setq buffer-read-only t)))
  19565. ;;; Agenda Finding stuck projects
  19566. (defvar org-agenda-skip-regexp nil
  19567. "Regular expression used in skipping subtrees for the agenda.
  19568. This is basically a temporary global variable that can be set and then
  19569. used by user-defined selections using `org-agenda-skip-function'.")
  19570. (defvar org-agenda-overriding-header nil
  19571. "When this is set during todo and tags searches, will replace header.")
  19572. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19573. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19574. If yes, it returns the end position of this tree, causing agenda commands
  19575. to skip this subtree. This is a function that can be put into
  19576. `org-agenda-skip-function' for the duration of a command."
  19577. (let ((end (save-excursion (org-end-of-subtree t)))
  19578. skip)
  19579. (save-excursion
  19580. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19581. (and skip end)))
  19582. (defun org-agenda-skip-entry-if (&rest conditions)
  19583. "Skip entry if any of CONDITIONS is true.
  19584. See `org-agenda-skip-if' for details."
  19585. (org-agenda-skip-if nil conditions))
  19586. (defun org-agenda-skip-subtree-if (&rest conditions)
  19587. "Skip entry if any of CONDITIONS is true.
  19588. See `org-agenda-skip-if' for details."
  19589. (org-agenda-skip-if t conditions))
  19590. (defun org-agenda-skip-if (subtree conditions)
  19591. "Checks current entity for CONDITIONS.
  19592. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19593. the entry, i.e. the text before the next heading is checked.
  19594. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19595. from different tests. Valid conditions are:
  19596. scheduled Check if there is a scheduled cookie
  19597. notscheduled Check if there is no scheduled cookie
  19598. deadline Check if there is a deadline
  19599. notdeadline Check if there is no deadline
  19600. regexp Check if regexp matches
  19601. notregexp Check if regexp does not match.
  19602. The regexp is taken from the conditions list, it must come right after
  19603. the `regexp' or `notregexp' element.
  19604. If any of these conditions is met, this function returns the end point of
  19605. the entity, causing the search to continue from there. This is a function
  19606. that can be put into `org-agenda-skip-function' for the duration of a command."
  19607. (let (beg end m)
  19608. (org-back-to-heading t)
  19609. (setq beg (point)
  19610. end (if subtree
  19611. (progn (org-end-of-subtree t) (point))
  19612. (progn (outline-next-heading) (1- (point)))))
  19613. (goto-char beg)
  19614. (and
  19615. (or
  19616. (and (memq 'scheduled conditions)
  19617. (re-search-forward org-scheduled-time-regexp end t))
  19618. (and (memq 'notscheduled conditions)
  19619. (not (re-search-forward org-scheduled-time-regexp end t)))
  19620. (and (memq 'deadline conditions)
  19621. (re-search-forward org-deadline-time-regexp end t))
  19622. (and (memq 'notdeadline conditions)
  19623. (not (re-search-forward org-deadline-time-regexp end t)))
  19624. (and (setq m (memq 'regexp conditions))
  19625. (stringp (nth 1 m))
  19626. (re-search-forward (nth 1 m) end t))
  19627. (and (setq m (memq 'notregexp conditions))
  19628. (stringp (nth 1 m))
  19629. (not (re-search-forward (nth 1 m) end t))))
  19630. end)))
  19631. ;;;###autoload
  19632. (defun org-agenda-list-stuck-projects (&rest ignore)
  19633. "Create agenda view for projects that are stuck.
  19634. Stuck projects are project that have no next actions. For the definitions
  19635. of what a project is and how to check if it stuck, customize the variable
  19636. `org-stuck-projects'.
  19637. MATCH is being ignored."
  19638. (interactive)
  19639. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19640. ;; FIXME: we could have used org-agenda-skip-if here.
  19641. (org-agenda-overriding-header "List of stuck projects: ")
  19642. (matcher (nth 0 org-stuck-projects))
  19643. (todo (nth 1 org-stuck-projects))
  19644. (todo-wds (if (member "*" todo)
  19645. (progn
  19646. (org-prepare-agenda-buffers (org-agenda-files))
  19647. (org-delete-all
  19648. org-done-keywords-for-agenda
  19649. (copy-sequence org-todo-keywords-for-agenda)))
  19650. todo))
  19651. (todo-re (concat "^\\*+[ \t]+\\("
  19652. (mapconcat 'identity todo-wds "\\|")
  19653. "\\)\\>"))
  19654. (tags (nth 2 org-stuck-projects))
  19655. (tags-re (if (member "*" tags)
  19656. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19657. (concat "^\\*+ .*:\\("
  19658. (mapconcat 'identity tags "\\|")
  19659. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19660. (gen-re (nth 3 org-stuck-projects))
  19661. (re-list
  19662. (delq nil
  19663. (list
  19664. (if todo todo-re)
  19665. (if tags tags-re)
  19666. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19667. gen-re)))))
  19668. (setq org-agenda-skip-regexp
  19669. (if re-list
  19670. (mapconcat 'identity re-list "\\|")
  19671. (error "No information how to identify unstuck projects")))
  19672. (org-tags-view nil matcher)
  19673. (with-current-buffer org-agenda-buffer-name
  19674. (setq org-agenda-redo-command
  19675. '(org-agenda-list-stuck-projects
  19676. (or current-prefix-arg org-last-arg))))))
  19677. ;;; Diary integration
  19678. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19679. (defun org-get-entries-from-diary (date)
  19680. "Get the (Emacs Calendar) diary entries for DATE."
  19681. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19682. (diary-display-hook '(fancy-diary-display))
  19683. (pop-up-frames nil)
  19684. (list-diary-entries-hook
  19685. (cons 'org-diary-default-entry list-diary-entries-hook))
  19686. (diary-file-name-prefix-function nil) ; turn this feature off
  19687. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19688. entries
  19689. (org-disable-agenda-to-diary t))
  19690. (save-excursion
  19691. (save-window-excursion
  19692. (funcall (if (fboundp 'diary-list-entries)
  19693. 'diary-list-entries 'list-diary-entries)
  19694. date 1)))
  19695. (if (not (get-buffer fancy-diary-buffer))
  19696. (setq entries nil)
  19697. (with-current-buffer fancy-diary-buffer
  19698. (setq buffer-read-only nil)
  19699. (if (zerop (buffer-size))
  19700. ;; No entries
  19701. (setq entries nil)
  19702. ;; Omit the date and other unnecessary stuff
  19703. (org-agenda-cleanup-fancy-diary)
  19704. ;; Add prefix to each line and extend the text properties
  19705. (if (zerop (buffer-size))
  19706. (setq entries nil)
  19707. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19708. (set-buffer-modified-p nil)
  19709. (kill-buffer fancy-diary-buffer)))
  19710. (when entries
  19711. (setq entries (org-split-string entries "\n"))
  19712. (setq entries
  19713. (mapcar
  19714. (lambda (x)
  19715. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19716. ;; Extend the text properties to the beginning of the line
  19717. (org-add-props x (text-properties-at (1- (length x)) x)
  19718. 'type "diary" 'date date))
  19719. entries)))))
  19720. (defun org-agenda-cleanup-fancy-diary ()
  19721. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19722. This gets rid of the date, the underline under the date, and
  19723. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19724. date. It also removes lines that contain only whitespace."
  19725. (goto-char (point-min))
  19726. (if (looking-at ".*?:[ \t]*")
  19727. (progn
  19728. (replace-match "")
  19729. (re-search-forward "\n=+$" nil t)
  19730. (replace-match "")
  19731. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19732. (re-search-forward "\n=+$" nil t)
  19733. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19734. (goto-char (point-min))
  19735. (while (re-search-forward "^ +\n" nil t)
  19736. (replace-match ""))
  19737. (goto-char (point-min))
  19738. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19739. (replace-match "")))
  19740. ;; Make sure entries from the diary have the right text properties.
  19741. (eval-after-load "diary-lib"
  19742. '(if (boundp 'diary-modify-entry-list-string-function)
  19743. ;; We can rely on the hook, nothing to do
  19744. nil
  19745. ;; Hook not avaiable, must use advice to make this work
  19746. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19747. "Make the position visible."
  19748. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19749. (stringp string)
  19750. buffer-file-name)
  19751. (setq string (org-modify-diary-entry-string string))))))
  19752. (defun org-modify-diary-entry-string (string)
  19753. "Add text properties to string, allowing org-mode to act on it."
  19754. (org-add-props string nil
  19755. 'mouse-face 'highlight
  19756. 'keymap org-agenda-keymap
  19757. 'help-echo (if buffer-file-name
  19758. (format "mouse-2 or RET jump to diary file %s"
  19759. (abbreviate-file-name buffer-file-name))
  19760. "")
  19761. 'org-agenda-diary-link t
  19762. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19763. (defun org-diary-default-entry ()
  19764. "Add a dummy entry to the diary.
  19765. Needed to avoid empty dates which mess up holiday display."
  19766. ;; Catch the error if dealing with the new add-to-diary-alist
  19767. (when org-disable-agenda-to-diary
  19768. (condition-case nil
  19769. (add-to-diary-list original-date "Org-mode dummy" "")
  19770. (error
  19771. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19772. ;;;###autoload
  19773. (defun org-diary (&rest args)
  19774. "Return diary information from org-files.
  19775. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19776. It accesses org files and extracts information from those files to be
  19777. listed in the diary. The function accepts arguments specifying what
  19778. items should be listed. The following arguments are allowed:
  19779. :timestamp List the headlines of items containing a date stamp or
  19780. date range matching the selected date. Deadlines will
  19781. also be listed, on the expiration day.
  19782. :sexp List entries resulting from diary-like sexps.
  19783. :deadline List any deadlines past due, or due within
  19784. `org-deadline-warning-days'. The listing occurs only
  19785. in the diary for *today*, not at any other date. If
  19786. an entry is marked DONE, it is no longer listed.
  19787. :scheduled List all items which are scheduled for the given date.
  19788. The diary for *today* also contains items which were
  19789. scheduled earlier and are not yet marked DONE.
  19790. :todo List all TODO items from the org-file. This may be a
  19791. long list - so this is not turned on by default.
  19792. Like deadlines, these entries only show up in the
  19793. diary for *today*, not at any other date.
  19794. The call in the diary file should look like this:
  19795. &%%(org-diary) ~/path/to/some/orgfile.org
  19796. Use a separate line for each org file to check. Or, if you omit the file name,
  19797. all files listed in `org-agenda-files' will be checked automatically:
  19798. &%%(org-diary)
  19799. If you don't give any arguments (as in the example above), the default
  19800. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19801. So the example above may also be written as
  19802. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19803. The function expects the lisp variables `entry' and `date' to be provided
  19804. by the caller, because this is how the calendar works. Don't use this
  19805. function from a program - use `org-agenda-get-day-entries' instead."
  19806. (when (> (- (time-to-seconds (current-time))
  19807. org-agenda-last-marker-time)
  19808. 5)
  19809. (org-agenda-reset-markers))
  19810. (org-compile-prefix-format 'agenda)
  19811. (org-set-sorting-strategy 'agenda)
  19812. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19813. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19814. (list entry)
  19815. (org-agenda-files t)))
  19816. file rtn results)
  19817. (org-prepare-agenda-buffers files)
  19818. ;; If this is called during org-agenda, don't return any entries to
  19819. ;; the calendar. Org Agenda will list these entries itself.
  19820. (if org-disable-agenda-to-diary (setq files nil))
  19821. (while (setq file (pop files))
  19822. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19823. (setq results (append results rtn)))
  19824. (if results
  19825. (concat (org-finalize-agenda-entries results) "\n"))))
  19826. ;;; Agenda entry finders
  19827. (defun org-agenda-get-day-entries (file date &rest args)
  19828. "Does the work for `org-diary' and `org-agenda'.
  19829. FILE is the path to a file to be checked for entries. DATE is date like
  19830. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19831. which kind of entries should be extracted. For details about these, see
  19832. the documentation of `org-diary'."
  19833. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19834. (let* ((org-startup-folded nil)
  19835. (org-startup-align-all-tables nil)
  19836. (buffer (if (file-exists-p file)
  19837. (org-get-agenda-file-buffer file)
  19838. (error "No such file %s" file)))
  19839. arg results rtn)
  19840. (if (not buffer)
  19841. ;; If file does not exist, make sure an error message ends up in diary
  19842. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19843. (with-current-buffer buffer
  19844. (unless (org-mode-p)
  19845. (error "Agenda file %s is not in `org-mode'" file))
  19846. (let ((case-fold-search nil))
  19847. (save-excursion
  19848. (save-restriction
  19849. (if org-agenda-restrict
  19850. (narrow-to-region org-agenda-restrict-begin
  19851. org-agenda-restrict-end)
  19852. (widen))
  19853. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19854. (while (setq arg (pop args))
  19855. (cond
  19856. ((and (eq arg :todo)
  19857. (equal date (calendar-current-date)))
  19858. (setq rtn (org-agenda-get-todos))
  19859. (setq results (append results rtn)))
  19860. ((eq arg :timestamp)
  19861. (setq rtn (org-agenda-get-blocks))
  19862. (setq results (append results rtn))
  19863. (setq rtn (org-agenda-get-timestamps))
  19864. (setq results (append results rtn)))
  19865. ((eq arg :sexp)
  19866. (setq rtn (org-agenda-get-sexps))
  19867. (setq results (append results rtn)))
  19868. ((eq arg :scheduled)
  19869. (setq rtn (org-agenda-get-scheduled))
  19870. (setq results (append results rtn)))
  19871. ((eq arg :closed)
  19872. (setq rtn (org-agenda-get-closed))
  19873. (setq results (append results rtn)))
  19874. ((eq arg :deadline)
  19875. (setq rtn (org-agenda-get-deadlines))
  19876. (setq results (append results rtn))))))))
  19877. results))))
  19878. (defun org-entry-is-todo-p ()
  19879. (member (org-get-todo-state) org-not-done-keywords))
  19880. (defun org-entry-is-done-p ()
  19881. (member (org-get-todo-state) org-done-keywords))
  19882. (defun org-get-todo-state ()
  19883. (save-excursion
  19884. (org-back-to-heading t)
  19885. (and (looking-at org-todo-line-regexp)
  19886. (match-end 2)
  19887. (match-string 2))))
  19888. (defun org-at-date-range-p (&optional inactive-ok)
  19889. "Is the cursor inside a date range?"
  19890. (interactive)
  19891. (save-excursion
  19892. (catch 'exit
  19893. (let ((pos (point)))
  19894. (skip-chars-backward "^[<\r\n")
  19895. (skip-chars-backward "<[")
  19896. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19897. (>= (match-end 0) pos)
  19898. (throw 'exit t))
  19899. (skip-chars-backward "^<[\r\n")
  19900. (skip-chars-backward "<[")
  19901. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19902. (>= (match-end 0) pos)
  19903. (throw 'exit t)))
  19904. nil)))
  19905. (defun org-agenda-get-todos ()
  19906. "Return the TODO information for agenda display."
  19907. (let* ((props (list 'face nil
  19908. 'done-face 'org-done
  19909. 'org-not-done-regexp org-not-done-regexp
  19910. 'org-todo-regexp org-todo-regexp
  19911. 'mouse-face 'highlight
  19912. 'keymap org-agenda-keymap
  19913. 'help-echo
  19914. (format "mouse-2 or RET jump to org file %s"
  19915. (abbreviate-file-name buffer-file-name))))
  19916. ;; FIXME: get rid of the \n at some point but watch out
  19917. (regexp (concat "^\\*+[ \t]+\\("
  19918. (if org-select-this-todo-keyword
  19919. (if (equal org-select-this-todo-keyword "*")
  19920. org-todo-regexp
  19921. (concat "\\<\\("
  19922. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19923. "\\)\\>"))
  19924. org-not-done-regexp)
  19925. "[^\n\r]*\\)"))
  19926. marker priority category tags
  19927. ee txt beg end)
  19928. (goto-char (point-min))
  19929. (while (re-search-forward regexp nil t)
  19930. (catch :skip
  19931. (save-match-data
  19932. (beginning-of-line)
  19933. (setq beg (point) end (progn (outline-next-heading) (point)))
  19934. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19935. (re-search-forward org-ts-regexp end t))
  19936. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19937. (re-search-forward org-scheduled-time-regexp end t))
  19938. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19939. (re-search-forward org-deadline-time-regexp end t)
  19940. (org-deadline-close (match-string 1))))
  19941. (goto-char (1+ beg))
  19942. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19943. (throw :skip nil)))
  19944. (goto-char beg)
  19945. (org-agenda-skip)
  19946. (goto-char (match-beginning 1))
  19947. (setq marker (org-agenda-new-marker (match-beginning 0))
  19948. category (org-get-category)
  19949. tags (org-get-tags-at (point))
  19950. txt (org-format-agenda-item "" (match-string 1) category tags)
  19951. priority (1+ (org-get-priority txt)))
  19952. (org-add-props txt props
  19953. 'org-marker marker 'org-hd-marker marker
  19954. 'priority priority 'org-category category
  19955. 'type "todo")
  19956. (push txt ee)
  19957. (if org-agenda-todo-list-sublevels
  19958. (goto-char (match-end 1))
  19959. (org-end-of-subtree 'invisible))))
  19960. (nreverse ee)))
  19961. (defconst org-agenda-no-heading-message
  19962. "No heading for this item in buffer or region.")
  19963. (defun org-agenda-get-timestamps ()
  19964. "Return the date stamp information for agenda display."
  19965. (let* ((props (list 'face nil
  19966. 'org-not-done-regexp org-not-done-regexp
  19967. 'org-todo-regexp org-todo-regexp
  19968. 'mouse-face 'highlight
  19969. 'keymap org-agenda-keymap
  19970. 'help-echo
  19971. (format "mouse-2 or RET jump to org file %s"
  19972. (abbreviate-file-name buffer-file-name))))
  19973. (d1 (calendar-absolute-from-gregorian date))
  19974. (remove-re
  19975. (concat
  19976. (regexp-quote
  19977. (format-time-string
  19978. "<%Y-%m-%d"
  19979. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19980. ".*?>"))
  19981. (regexp
  19982. (concat
  19983. (regexp-quote
  19984. (substring
  19985. (format-time-string
  19986. (car org-time-stamp-formats)
  19987. (apply 'encode-time ; DATE bound by calendar
  19988. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19989. 0 11))
  19990. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19991. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19992. marker hdmarker deadlinep scheduledp donep tmp priority category
  19993. ee txt timestr tags b0 b3 e3 head)
  19994. (goto-char (point-min))
  19995. (while (re-search-forward regexp nil t)
  19996. (setq b0 (match-beginning 0)
  19997. b3 (match-beginning 3) e3 (match-end 3))
  19998. (catch :skip
  19999. (and (org-at-date-range-p) (throw :skip nil))
  20000. (org-agenda-skip)
  20001. (if (and (match-end 1)
  20002. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  20003. (throw :skip nil))
  20004. (if (and e3
  20005. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  20006. (throw :skip nil))
  20007. (setq marker (org-agenda-new-marker b0)
  20008. category (org-get-category b0)
  20009. tmp (buffer-substring (max (point-min)
  20010. (- b0 org-ds-keyword-length))
  20011. b0)
  20012. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  20013. deadlinep (string-match org-deadline-regexp tmp)
  20014. scheduledp (string-match org-scheduled-regexp tmp)
  20015. donep (org-entry-is-done-p))
  20016. (if (or scheduledp deadlinep) (throw :skip t))
  20017. (if (string-match ">" timestr)
  20018. ;; substring should only run to end of time stamp
  20019. (setq timestr (substring timestr 0 (match-end 0))))
  20020. (save-excursion
  20021. (if (re-search-backward "^\\*+ " nil t)
  20022. (progn
  20023. (goto-char (match-beginning 0))
  20024. (setq hdmarker (org-agenda-new-marker)
  20025. tags (org-get-tags-at))
  20026. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20027. (setq head (match-string 1))
  20028. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  20029. (setq txt (org-format-agenda-item
  20030. nil head category tags timestr nil
  20031. remove-re)))
  20032. (setq txt org-agenda-no-heading-message))
  20033. (setq priority (org-get-priority txt))
  20034. (org-add-props txt props
  20035. 'org-marker marker 'org-hd-marker hdmarker)
  20036. (org-add-props txt nil 'priority priority
  20037. 'org-category category 'date date
  20038. 'type "timestamp")
  20039. (push txt ee))
  20040. (outline-next-heading)))
  20041. (nreverse ee)))
  20042. (defun org-agenda-get-sexps ()
  20043. "Return the sexp information for agenda display."
  20044. (require 'diary-lib)
  20045. (let* ((props (list 'face nil
  20046. 'mouse-face 'highlight
  20047. 'keymap org-agenda-keymap
  20048. 'help-echo
  20049. (format "mouse-2 or RET jump to org file %s"
  20050. (abbreviate-file-name buffer-file-name))))
  20051. (regexp "^&?%%(")
  20052. marker category ee txt tags entry result beg b sexp sexp-entry)
  20053. (goto-char (point-min))
  20054. (while (re-search-forward regexp nil t)
  20055. (catch :skip
  20056. (org-agenda-skip)
  20057. (setq beg (match-beginning 0))
  20058. (goto-char (1- (match-end 0)))
  20059. (setq b (point))
  20060. (forward-sexp 1)
  20061. (setq sexp (buffer-substring b (point)))
  20062. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  20063. (org-trim (match-string 1))
  20064. ""))
  20065. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  20066. (when result
  20067. (setq marker (org-agenda-new-marker beg)
  20068. category (org-get-category beg))
  20069. (if (string-match "\\S-" result)
  20070. (setq txt result)
  20071. (setq txt "SEXP entry returned empty string"))
  20072. (setq txt (org-format-agenda-item
  20073. "" txt category tags 'time))
  20074. (org-add-props txt props 'org-marker marker)
  20075. (org-add-props txt nil
  20076. 'org-category category 'date date
  20077. 'type "sexp")
  20078. (push txt ee))))
  20079. (nreverse ee)))
  20080. (defun org-agenda-get-closed ()
  20081. "Return the logged TODO entries for agenda display."
  20082. (let* ((props (list 'mouse-face 'highlight
  20083. 'org-not-done-regexp org-not-done-regexp
  20084. 'org-todo-regexp org-todo-regexp
  20085. 'keymap org-agenda-keymap
  20086. 'help-echo
  20087. (format "mouse-2 or RET jump to org file %s"
  20088. (abbreviate-file-name buffer-file-name))))
  20089. (regexp (concat
  20090. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  20091. (regexp-quote
  20092. (substring
  20093. (format-time-string
  20094. (car org-time-stamp-formats)
  20095. (apply 'encode-time ; DATE bound by calendar
  20096. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20097. 1 11))))
  20098. marker hdmarker priority category tags closedp
  20099. ee txt timestr)
  20100. (goto-char (point-min))
  20101. (while (re-search-forward regexp nil t)
  20102. (catch :skip
  20103. (org-agenda-skip)
  20104. (setq marker (org-agenda-new-marker (match-beginning 0))
  20105. closedp (equal (match-string 1) org-closed-string)
  20106. category (org-get-category (match-beginning 0))
  20107. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20108. ;; donep (org-entry-is-done-p)
  20109. )
  20110. (if (string-match "\\]" timestr)
  20111. ;; substring should only run to end of time stamp
  20112. (setq timestr (substring timestr 0 (match-end 0))))
  20113. (save-excursion
  20114. (if (re-search-backward "^\\*+ " nil t)
  20115. (progn
  20116. (goto-char (match-beginning 0))
  20117. (setq hdmarker (org-agenda-new-marker)
  20118. tags (org-get-tags-at))
  20119. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20120. (setq txt (org-format-agenda-item
  20121. (if closedp "Closed: " "Clocked: ")
  20122. (match-string 1) category tags timestr)))
  20123. (setq txt org-agenda-no-heading-message))
  20124. (setq priority 100000)
  20125. (org-add-props txt props
  20126. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20127. 'priority priority 'org-category category
  20128. 'type "closed" 'date date
  20129. 'undone-face 'org-warning 'done-face 'org-done)
  20130. (push txt ee))
  20131. (goto-char (point-at-eol))))
  20132. (nreverse ee)))
  20133. (defun org-agenda-get-deadlines ()
  20134. "Return the deadline information for agenda display."
  20135. (let* ((props (list 'mouse-face 'highlight
  20136. 'org-not-done-regexp org-not-done-regexp
  20137. 'org-todo-regexp org-todo-regexp
  20138. 'keymap org-agenda-keymap
  20139. 'help-echo
  20140. (format "mouse-2 or RET jump to org file %s"
  20141. (abbreviate-file-name buffer-file-name))))
  20142. (regexp org-deadline-time-regexp)
  20143. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20144. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20145. d2 diff dfrac wdays pos pos1 category tags
  20146. ee txt head face s upcomingp donep timestr)
  20147. (goto-char (point-min))
  20148. (while (re-search-forward regexp nil t)
  20149. (catch :skip
  20150. (org-agenda-skip)
  20151. (setq s (match-string 1)
  20152. pos (1- (match-beginning 1))
  20153. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20154. diff (- d2 d1)
  20155. wdays (org-get-wdays s)
  20156. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20157. upcomingp (and todayp (> diff 0)))
  20158. ;; When to show a deadline in the calendar:
  20159. ;; If the expiration is within wdays warning time.
  20160. ;; Past-due deadlines are only shown on the current date
  20161. (if (or (and (<= diff wdays)
  20162. (and todayp (not org-agenda-only-exact-dates)))
  20163. (= diff 0))
  20164. (save-excursion
  20165. (setq category (org-get-category))
  20166. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20167. (progn
  20168. (goto-char (match-end 0))
  20169. (setq pos1 (match-beginning 0))
  20170. (setq tags (org-get-tags-at pos1))
  20171. (setq head (buffer-substring-no-properties
  20172. (point)
  20173. (progn (skip-chars-forward "^\r\n")
  20174. (point))))
  20175. (setq donep (string-match org-looking-at-done-regexp head))
  20176. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20177. (setq timestr
  20178. (concat (substring s (match-beginning 1)) " "))
  20179. (setq timestr 'time))
  20180. (if (and donep
  20181. (or org-agenda-skip-deadline-if-done
  20182. (not (= diff 0))))
  20183. (setq txt nil)
  20184. (setq txt (org-format-agenda-item
  20185. (if (= diff 0)
  20186. (car org-agenda-deadline-leaders)
  20187. (format (nth 1 org-agenda-deadline-leaders)
  20188. diff))
  20189. head category tags timestr))))
  20190. (setq txt org-agenda-no-heading-message))
  20191. (when txt
  20192. (setq face (org-agenda-deadline-face dfrac wdays))
  20193. (org-add-props txt props
  20194. 'org-marker (org-agenda-new-marker pos)
  20195. 'org-hd-marker (org-agenda-new-marker pos1)
  20196. 'priority (+ (- diff)
  20197. (org-get-priority txt))
  20198. 'org-category category
  20199. 'type (if upcomingp "upcoming-deadline" "deadline")
  20200. 'date (if upcomingp date d2)
  20201. 'face (if donep 'org-done face)
  20202. 'undone-face face 'done-face 'org-done)
  20203. (push txt ee))))))
  20204. (nreverse ee)))
  20205. (defun org-agenda-deadline-face (fraction &optional wdays)
  20206. "Return the face to displaying a deadline item.
  20207. FRACTION is what fraction of the head-warning time has passed."
  20208. (if (equal wdays 0) (setq fraction 1.))
  20209. (let ((faces org-agenda-deadline-faces) f)
  20210. (catch 'exit
  20211. (while (setq f (pop faces))
  20212. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20213. (defun org-agenda-get-scheduled ()
  20214. "Return the scheduled information for agenda display."
  20215. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20216. 'org-todo-regexp org-todo-regexp
  20217. 'done-face 'org-done
  20218. 'mouse-face 'highlight
  20219. 'keymap org-agenda-keymap
  20220. 'help-echo
  20221. (format "mouse-2 or RET jump to org file %s"
  20222. (abbreviate-file-name buffer-file-name))))
  20223. (regexp org-scheduled-time-regexp)
  20224. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20225. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20226. d2 diff pos pos1 category tags
  20227. ee txt head pastschedp donep face timestr s)
  20228. (goto-char (point-min))
  20229. (while (re-search-forward regexp nil t)
  20230. (catch :skip
  20231. (org-agenda-skip)
  20232. (setq s (match-string 1)
  20233. pos (1- (match-beginning 1))
  20234. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20235. ;;; is this right?
  20236. ;;; do we need to do this for deadleine too????
  20237. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20238. diff (- d2 d1))
  20239. (setq pastschedp (and todayp (< diff 0)))
  20240. ;; When to show a scheduled item in the calendar:
  20241. ;; If it is on or past the date.
  20242. (if (or (and (< diff 0)
  20243. (and todayp (not org-agenda-only-exact-dates)))
  20244. (= diff 0))
  20245. (save-excursion
  20246. (setq category (org-get-category))
  20247. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20248. (progn
  20249. (goto-char (match-end 0))
  20250. (setq pos1 (match-beginning 0))
  20251. (setq tags (org-get-tags-at))
  20252. (setq head (buffer-substring-no-properties
  20253. (point)
  20254. (progn (skip-chars-forward "^\r\n") (point))))
  20255. (setq donep (string-match org-looking-at-done-regexp head))
  20256. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20257. (setq timestr
  20258. (concat (substring s (match-beginning 1)) " "))
  20259. (setq timestr 'time))
  20260. (if (and donep
  20261. (or org-agenda-skip-scheduled-if-done
  20262. (not (= diff 0))))
  20263. (setq txt nil)
  20264. (setq txt (org-format-agenda-item
  20265. (if (= diff 0)
  20266. (car org-agenda-scheduled-leaders)
  20267. (format (nth 1 org-agenda-scheduled-leaders)
  20268. (- 1 diff)))
  20269. head category tags timestr))))
  20270. (setq txt org-agenda-no-heading-message))
  20271. (when txt
  20272. (setq face (if pastschedp
  20273. 'org-scheduled-previously
  20274. 'org-scheduled-today))
  20275. (org-add-props txt props
  20276. 'undone-face face
  20277. 'face (if donep 'org-done face)
  20278. 'org-marker (org-agenda-new-marker pos)
  20279. 'org-hd-marker (org-agenda-new-marker pos1)
  20280. 'type (if pastschedp "past-scheduled" "scheduled")
  20281. 'date (if pastschedp d2 date)
  20282. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20283. 'org-category category)
  20284. (push txt ee))))))
  20285. (nreverse ee)))
  20286. (defun org-agenda-get-blocks ()
  20287. "Return the date-range information for agenda display."
  20288. (let* ((props (list 'face nil
  20289. 'org-not-done-regexp org-not-done-regexp
  20290. 'org-todo-regexp org-todo-regexp
  20291. 'mouse-face 'highlight
  20292. 'keymap org-agenda-keymap
  20293. 'help-echo
  20294. (format "mouse-2 or RET jump to org file %s"
  20295. (abbreviate-file-name buffer-file-name))))
  20296. (regexp org-tr-regexp)
  20297. (d0 (calendar-absolute-from-gregorian date))
  20298. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20299. donep head)
  20300. (goto-char (point-min))
  20301. (while (re-search-forward regexp nil t)
  20302. (catch :skip
  20303. (org-agenda-skip)
  20304. (setq pos (point))
  20305. (setq timestr (match-string 0)
  20306. s1 (match-string 1)
  20307. s2 (match-string 2)
  20308. d1 (time-to-days (org-time-string-to-time s1))
  20309. d2 (time-to-days (org-time-string-to-time s2)))
  20310. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20311. ;; Only allow days between the limits, because the normal
  20312. ;; date stamps will catch the limits.
  20313. (save-excursion
  20314. (setq marker (org-agenda-new-marker (point)))
  20315. (setq category (org-get-category))
  20316. (if (re-search-backward "^\\*+ " nil t)
  20317. (progn
  20318. (goto-char (match-beginning 0))
  20319. (setq hdmarker (org-agenda-new-marker (point)))
  20320. (setq tags (org-get-tags-at))
  20321. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20322. (setq head (match-string 1))
  20323. (and org-agenda-skip-timestamp-if-done
  20324. (org-entry-is-done-p)
  20325. (throw :skip t))
  20326. (setq txt (org-format-agenda-item
  20327. (format (if (= d1 d2) "" "(%d/%d): ")
  20328. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20329. head category tags
  20330. (if (= d0 d1) timestr))))
  20331. (setq txt org-agenda-no-heading-message))
  20332. (org-add-props txt props
  20333. 'org-marker marker 'org-hd-marker hdmarker
  20334. 'type "block" 'date date
  20335. 'priority (org-get-priority txt) 'org-category category)
  20336. (push txt ee)))
  20337. (goto-char pos)))
  20338. ;; Sort the entries by expiration date.
  20339. (nreverse ee)))
  20340. ;;; Agenda presentation and sorting
  20341. (defconst org-plain-time-of-day-regexp
  20342. (concat
  20343. "\\(\\<[012]?[0-9]"
  20344. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20345. "\\(--?"
  20346. "\\(\\<[012]?[0-9]"
  20347. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20348. "\\)?")
  20349. "Regular expression to match a plain time or time range.
  20350. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20351. groups carry important information:
  20352. 0 the full match
  20353. 1 the first time, range or not
  20354. 8 the second time, if it is a range.")
  20355. (defconst org-plain-time-extension-regexp
  20356. (concat
  20357. "\\(\\<[012]?[0-9]"
  20358. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20359. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20360. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20361. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20362. groups carry important information:
  20363. 0 the full match
  20364. 7 hours of duration
  20365. 9 minutes of duration")
  20366. (defconst org-stamp-time-of-day-regexp
  20367. (concat
  20368. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20369. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20370. "\\(--?"
  20371. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20372. "Regular expression to match a timestamp time or time range.
  20373. After a match, the following groups carry important information:
  20374. 0 the full match
  20375. 1 date plus weekday, for backreferencing to make sure both times on same day
  20376. 2 the first time, range or not
  20377. 4 the second time, if it is a range.")
  20378. (defvar org-prefix-has-time nil
  20379. "A flag, set by `org-compile-prefix-format'.
  20380. The flag is set if the currently compiled format contains a `%t'.")
  20381. (defvar org-prefix-has-tag nil
  20382. "A flag, set by `org-compile-prefix-format'.
  20383. The flag is set if the currently compiled format contains a `%T'.")
  20384. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20385. noprefix remove-re)
  20386. "Format TXT to be inserted into the agenda buffer.
  20387. In particular, it adds the prefix and corresponding text properties. EXTRA
  20388. must be a string and replaces the `%s' specifier in the prefix format.
  20389. CATEGORY (string, symbol or nil) may be used to overrule the default
  20390. category taken from local variable or file name. It will replace the `%c'
  20391. specifier in the format. DOTIME, when non-nil, indicates that a
  20392. time-of-day should be extracted from TXT for sorting of this entry, and for
  20393. the `%t' specifier in the format. When DOTIME is a string, this string is
  20394. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20395. only the correctly processes TXT should be returned - this is used by
  20396. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20397. Any match of REMOVE-RE will be removed from TXT."
  20398. (save-match-data
  20399. ;; Diary entries sometimes have extra whitespace at the beginning
  20400. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20401. (let* ((category (or category
  20402. org-category
  20403. (if buffer-file-name
  20404. (file-name-sans-extension
  20405. (file-name-nondirectory buffer-file-name))
  20406. "")))
  20407. (tag (if tags (nth (1- (length tags)) tags) ""))
  20408. time ; time and tag are needed for the eval of the prefix format
  20409. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20410. (time-of-day (and dotime (org-get-time-of-day ts)))
  20411. stamp plain s0 s1 s2 rtn srp)
  20412. (when (and dotime time-of-day org-prefix-has-time)
  20413. ;; Extract starting and ending time and move them to prefix
  20414. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20415. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20416. (setq s0 (match-string 0 ts)
  20417. srp (and stamp (match-end 3))
  20418. s1 (match-string (if plain 1 2) ts)
  20419. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20420. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20421. ;; them, we might want to remove them there to avoid duplication.
  20422. ;; The user can turn this off with a variable.
  20423. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20424. (string-match (concat (regexp-quote s0) " *") txt)
  20425. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20426. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20427. (= (match-beginning 0) 0)
  20428. t))
  20429. (setq txt (replace-match "" nil nil txt))))
  20430. ;; Normalize the time(s) to 24 hour
  20431. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20432. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20433. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20434. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20435. (let ((m (+ (string-to-number (match-string 2 s1))
  20436. (* 60 (string-to-number (match-string 1 s1)))
  20437. org-agenda-default-appointment-duration))
  20438. h)
  20439. (setq h (/ m 60) m (- m (* h 60)))
  20440. (setq s2 (format "%02d:%02d" h m))))
  20441. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20442. txt)
  20443. ;; Tags are in the string
  20444. (if (or (eq org-agenda-remove-tags t)
  20445. (and org-agenda-remove-tags
  20446. org-prefix-has-tag))
  20447. (setq txt (replace-match "" t t txt))
  20448. (setq txt (replace-match
  20449. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20450. (match-string 2 txt))
  20451. t t txt))))
  20452. (when remove-re
  20453. (while (string-match remove-re txt)
  20454. (setq txt (replace-match "" t t txt))))
  20455. ;; Create the final string
  20456. (if noprefix
  20457. (setq rtn txt)
  20458. ;; Prepare the variables needed in the eval of the compiled format
  20459. (setq time (cond (s2 (concat s1 "-" s2))
  20460. (s1 (concat s1 "......"))
  20461. (t ""))
  20462. extra (or extra "")
  20463. category (if (symbolp category) (symbol-name category) category))
  20464. ;; Evaluate the compiled format
  20465. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20466. ;; And finally add the text properties
  20467. (org-add-props rtn nil
  20468. 'org-category (downcase category) 'tags tags
  20469. 'org-highest-priority org-highest-priority
  20470. 'org-lowest-priority org-lowest-priority
  20471. 'prefix-length (- (length rtn) (length txt))
  20472. 'time-of-day time-of-day
  20473. 'txt txt
  20474. 'time time
  20475. 'extra extra
  20476. 'dotime dotime))))
  20477. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20478. (defvar org-agenda-sorting-strategy-selected nil)
  20479. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20480. (catch 'exit
  20481. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20482. ((and todayp (member 'today (car org-agenda-time-grid))))
  20483. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20484. ((member 'weekly (car org-agenda-time-grid)))
  20485. (t (throw 'exit list)))
  20486. (let* ((have (delq nil (mapcar
  20487. (lambda (x) (get-text-property 1 'time-of-day x))
  20488. list)))
  20489. (string (nth 1 org-agenda-time-grid))
  20490. (gridtimes (nth 2 org-agenda-time-grid))
  20491. (req (car org-agenda-time-grid))
  20492. (remove (member 'remove-match req))
  20493. new time)
  20494. (if (and (member 'require-timed req) (not have))
  20495. ;; don't show empty grid
  20496. (throw 'exit list))
  20497. (while (setq time (pop gridtimes))
  20498. (unless (and remove (member time have))
  20499. (setq time (int-to-string time))
  20500. (push (org-format-agenda-item
  20501. nil string "" nil
  20502. (concat (substring time 0 -2) ":" (substring time -2)))
  20503. new)
  20504. (put-text-property
  20505. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20506. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20507. (append new list)
  20508. (append list new)))))
  20509. (defun org-compile-prefix-format (key)
  20510. "Compile the prefix format into a Lisp form that can be evaluated.
  20511. The resulting form is returned and stored in the variable
  20512. `org-prefix-format-compiled'."
  20513. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20514. (let ((s (cond
  20515. ((stringp org-agenda-prefix-format)
  20516. org-agenda-prefix-format)
  20517. ((assq key org-agenda-prefix-format)
  20518. (cdr (assq key org-agenda-prefix-format)))
  20519. (t " %-12:c%?-12t% s")))
  20520. (start 0)
  20521. varform vars var e c f opt)
  20522. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20523. s start)
  20524. (setq var (cdr (assoc (match-string 4 s)
  20525. '(("c" . category) ("t" . time) ("s" . extra)
  20526. ("T" . tag))))
  20527. c (or (match-string 3 s) "")
  20528. opt (match-beginning 1)
  20529. start (1+ (match-beginning 0)))
  20530. (if (equal var 'time) (setq org-prefix-has-time t))
  20531. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20532. (setq f (concat "%" (match-string 2 s) "s"))
  20533. (if opt
  20534. (setq varform
  20535. `(if (equal "" ,var)
  20536. ""
  20537. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20538. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20539. (setq s (replace-match "%s" t nil s))
  20540. (push varform vars))
  20541. (setq vars (nreverse vars))
  20542. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20543. (defun org-set-sorting-strategy (key)
  20544. (if (symbolp (car org-agenda-sorting-strategy))
  20545. ;; the old format
  20546. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20547. (setq org-agenda-sorting-strategy-selected
  20548. (or (cdr (assq key org-agenda-sorting-strategy))
  20549. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20550. '(time-up category-keep priority-down)))))
  20551. (defun org-get-time-of-day (s &optional string mod24)
  20552. "Check string S for a time of day.
  20553. If found, return it as a military time number between 0 and 2400.
  20554. If not found, return nil.
  20555. The optional STRING argument forces conversion into a 5 character wide string
  20556. HH:MM."
  20557. (save-match-data
  20558. (when
  20559. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20560. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20561. (let* ((h (string-to-number (match-string 1 s)))
  20562. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20563. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20564. (am-p (equal ampm "am"))
  20565. (h1 (cond ((not ampm) h)
  20566. ((= h 12) (if am-p 0 12))
  20567. (t (+ h (if am-p 0 12)))))
  20568. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20569. (mod h1 24) h1))
  20570. (t0 (+ (* 100 h2) m))
  20571. (t1 (concat (if (>= h1 24) "+" " ")
  20572. (if (< t0 100) "0" "")
  20573. (if (< t0 10) "0" "")
  20574. (int-to-string t0))))
  20575. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20576. (defun org-finalize-agenda-entries (list &optional nosort)
  20577. "Sort and concatenate the agenda items."
  20578. (setq list (mapcar 'org-agenda-highlight-todo list))
  20579. (if nosort
  20580. list
  20581. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20582. (defun org-agenda-highlight-todo (x)
  20583. (let (re pl)
  20584. (if (eq x 'line)
  20585. (save-excursion
  20586. (beginning-of-line 1)
  20587. (setq re (get-text-property (point) 'org-todo-regexp))
  20588. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20589. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20590. (add-text-properties (match-beginning 0) (match-end 0)
  20591. (list 'face (org-get-todo-face 0)))
  20592. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20593. (delete-region (match-beginning 1) (1- (match-end 0)))
  20594. (goto-char (match-beginning 1))
  20595. (insert (format org-agenda-todo-keyword-format s)))))
  20596. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20597. pl (get-text-property 0 'prefix-length x))
  20598. (when (and re
  20599. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20600. x (or pl 0)) pl))
  20601. (add-text-properties
  20602. (or (match-end 1) (match-end 0)) (match-end 0)
  20603. (list 'face (org-get-todo-face (match-string 2 x)))
  20604. x)
  20605. (setq x (concat (substring x 0 (match-end 1))
  20606. (format org-agenda-todo-keyword-format
  20607. (match-string 2 x))
  20608. " "
  20609. (substring x (match-end 3)))))
  20610. x)))
  20611. (defsubst org-cmp-priority (a b)
  20612. "Compare the priorities of string A and B."
  20613. (let ((pa (or (get-text-property 1 'priority a) 0))
  20614. (pb (or (get-text-property 1 'priority b) 0)))
  20615. (cond ((> pa pb) +1)
  20616. ((< pa pb) -1)
  20617. (t nil))))
  20618. (defsubst org-cmp-category (a b)
  20619. "Compare the string values of categories of strings A and B."
  20620. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20621. (cb (or (get-text-property 1 'org-category b) "")))
  20622. (cond ((string-lessp ca cb) -1)
  20623. ((string-lessp cb ca) +1)
  20624. (t nil))))
  20625. (defsubst org-cmp-tag (a b)
  20626. "Compare the string values of categories of strings A and B."
  20627. (let ((ta (car (last (get-text-property 1 'tags a))))
  20628. (tb (car (last (get-text-property 1 'tags b)))))
  20629. (cond ((not ta) +1)
  20630. ((not tb) -1)
  20631. ((string-lessp ta tb) -1)
  20632. ((string-lessp tb ta) +1)
  20633. (t nil))))
  20634. (defsubst org-cmp-time (a b)
  20635. "Compare the time-of-day values of strings A and B."
  20636. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20637. (ta (or (get-text-property 1 'time-of-day a) def))
  20638. (tb (or (get-text-property 1 'time-of-day b) def)))
  20639. (cond ((< ta tb) -1)
  20640. ((< tb ta) +1)
  20641. (t nil))))
  20642. (defun org-entries-lessp (a b)
  20643. "Predicate for sorting agenda entries."
  20644. ;; The following variables will be used when the form is evaluated.
  20645. ;; So even though the compiler complains, keep them.
  20646. (let* ((time-up (org-cmp-time a b))
  20647. (time-down (if time-up (- time-up) nil))
  20648. (priority-up (org-cmp-priority a b))
  20649. (priority-down (if priority-up (- priority-up) nil))
  20650. (category-up (org-cmp-category a b))
  20651. (category-down (if category-up (- category-up) nil))
  20652. (category-keep (if category-up +1 nil))
  20653. (tag-up (org-cmp-tag a b))
  20654. (tag-down (if tag-up (- tag-up) nil)))
  20655. (cdr (assoc
  20656. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20657. '((-1 . t) (1 . nil) (nil . nil))))))
  20658. ;;; Agenda restriction lock
  20659. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20660. "Overlay to mark the headline to which arenda commands are restricted.")
  20661. (org-overlay-put org-agenda-restriction-lock-overlay
  20662. 'face 'org-agenda-restriction-lock)
  20663. (org-overlay-put org-agenda-restriction-lock-overlay
  20664. 'help-echo "Agendas are currently limited to this subtree.")
  20665. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20666. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20667. "Overlay marking the agenda restriction line in speedbar.")
  20668. (org-overlay-put org-speedbar-restriction-lock-overlay
  20669. 'face 'org-agenda-restriction-lock)
  20670. (org-overlay-put org-speedbar-restriction-lock-overlay
  20671. 'help-echo "Agendas are currently limited to this item.")
  20672. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20673. (defun org-agenda-set-restriction-lock (&optional type)
  20674. "Set restriction lock for agenda, to current subtree or file.
  20675. Restriction will be the file if TYPE is `file', or if type is the
  20676. universal prefix '(4), or if the cursor is before the first headline
  20677. in the file. Otherwise, restriction will be to the current subtree."
  20678. (interactive "P")
  20679. (and (equal type '(4)) (setq type 'file))
  20680. (setq type (cond
  20681. (type type)
  20682. ((org-at-heading-p) 'subtree)
  20683. ((condition-case nil (org-back-to-heading t) (error nil))
  20684. 'subtree)
  20685. (t 'file)))
  20686. (if (eq type 'subtree)
  20687. (progn
  20688. (setq org-agenda-restrict t)
  20689. (setq org-agenda-overriding-restriction 'subtree)
  20690. (put 'org-agenda-files 'org-restrict
  20691. (list (buffer-file-name (buffer-base-buffer))))
  20692. (org-back-to-heading t)
  20693. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20694. (move-marker org-agenda-restrict-begin (point))
  20695. (move-marker org-agenda-restrict-end
  20696. (save-excursion (org-end-of-subtree t)))
  20697. (message "Locking agenda restriction to subtree"))
  20698. (put 'org-agenda-files 'org-restrict
  20699. (list (buffer-file-name (buffer-base-buffer))))
  20700. (setq org-agenda-restrict nil)
  20701. (setq org-agenda-overriding-restriction 'file)
  20702. (move-marker org-agenda-restrict-begin nil)
  20703. (move-marker org-agenda-restrict-end nil)
  20704. (message "Locking agenda restriction to file"))
  20705. (setq current-prefix-arg nil)
  20706. (org-agenda-maybe-redo))
  20707. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20708. "Remove the agenda restriction lock."
  20709. (interactive "P")
  20710. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20711. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20712. (setq org-agenda-overriding-restriction nil)
  20713. (setq org-agenda-restrict nil)
  20714. (put 'org-agenda-files 'org-restrict nil)
  20715. (move-marker org-agenda-restrict-begin nil)
  20716. (move-marker org-agenda-restrict-end nil)
  20717. (setq current-prefix-arg nil)
  20718. (message "Agenda restriction lock removed")
  20719. (or noupdate (org-agenda-maybe-redo)))
  20720. (defun org-agenda-maybe-redo ()
  20721. "If there is any window showing the agenda view, update it."
  20722. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20723. (w0 (selected-window)))
  20724. (when w
  20725. (select-window w)
  20726. (org-agenda-redo)
  20727. (select-window w0)
  20728. (if org-agenda-overriding-restriction
  20729. (message "Agenda view shifted to new %s restriction"
  20730. org-agenda-overriding-restriction)
  20731. (message "Agenda restriction lock removed")))))
  20732. ;;; Agenda commands
  20733. (defun org-agenda-check-type (error &rest types)
  20734. "Check if agenda buffer is of allowed type.
  20735. If ERROR is non-nil, throw an error, otherwise just return nil."
  20736. (if (memq org-agenda-type types)
  20737. t
  20738. (if error
  20739. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20740. nil)))
  20741. (defun org-agenda-quit ()
  20742. "Exit agenda by removing the window or the buffer."
  20743. (interactive)
  20744. (let ((buf (current-buffer)))
  20745. (if (not (one-window-p)) (delete-window))
  20746. (kill-buffer buf)
  20747. (org-agenda-reset-markers)
  20748. (org-columns-remove-overlays))
  20749. ;; Maybe restore the pre-agenda window configuration.
  20750. (and org-agenda-restore-windows-after-quit
  20751. (not (eq org-agenda-window-setup 'other-frame))
  20752. org-pre-agenda-window-conf
  20753. (set-window-configuration org-pre-agenda-window-conf)))
  20754. (defun org-agenda-exit ()
  20755. "Exit agenda by removing the window or the buffer.
  20756. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20757. Org-mode buffers visited directly by the user will not be touched."
  20758. (interactive)
  20759. (org-release-buffers org-agenda-new-buffers)
  20760. (setq org-agenda-new-buffers nil)
  20761. (org-agenda-quit))
  20762. (defun org-agenda-execute (arg)
  20763. "Execute another agenda command, keeping same window.\\<global-map>
  20764. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20765. (interactive "P")
  20766. (let ((org-agenda-window-setup 'current-window))
  20767. (org-agenda arg)))
  20768. (defun org-save-all-org-buffers ()
  20769. "Save all Org-mode buffers without user confirmation."
  20770. (interactive)
  20771. (message "Saving all Org-mode buffers...")
  20772. (save-some-buffers t 'org-mode-p)
  20773. (message "Saving all Org-mode buffers... done"))
  20774. (defun org-agenda-redo ()
  20775. "Rebuild Agenda.
  20776. When this is the global TODO list, a prefix argument will be interpreted."
  20777. (interactive)
  20778. (let* ((org-agenda-keep-modes t)
  20779. (line (org-current-line))
  20780. (window-line (- line (org-current-line (window-start))))
  20781. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20782. (message "Rebuilding agenda buffer...")
  20783. (org-let lprops '(eval org-agenda-redo-command))
  20784. (setq org-agenda-undo-list nil
  20785. org-agenda-pending-undo-list nil)
  20786. (message "Rebuilding agenda buffer...done")
  20787. (goto-line line)
  20788. (recenter window-line)))
  20789. (defun org-agenda-manipulate-query-add ()
  20790. "Manipulate the query by adding a search term with positive selection.
  20791. Positive selection means, the term must be matched for selection of an entry."
  20792. (interactive)
  20793. (org-agenda-manipulate-query ?\[))
  20794. (defun org-agenda-manipulate-query-subtract ()
  20795. "Manipulate the query by adding a search term with negative selection.
  20796. Negative selection means, term must not be matched for selection of an entry."
  20797. (interactive)
  20798. (org-agenda-manipulate-query ?\]))
  20799. (defun org-agenda-manipulate-query-add-re ()
  20800. "Manipulate the query by adding a search regexp with positive selection.
  20801. Positive selection means, the regexp must match for selection of an entry."
  20802. (interactive)
  20803. (org-agenda-manipulate-query ?\{))
  20804. (defun org-agenda-manipulate-query-subtract-re ()
  20805. "Manipulate the query by adding a search regexp with negative selection.
  20806. Negative selection means, regexp must not match for selection of an entry."
  20807. (interactive)
  20808. (org-agenda-manipulate-query ?\}))
  20809. (defun org-agenda-manipulate-query (char)
  20810. (cond
  20811. ((eq org-agenda-type 'search)
  20812. (org-add-to-string
  20813. 'org-agenda-query-string
  20814. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  20815. (?\{ . " +{}") (?\} . " -{}")))))
  20816. (setq org-agenda-redo-command
  20817. (list 'org-search-view
  20818. (+ (length org-agenda-query-string)
  20819. (if (member char '(?\{ ?\})) 0 1))
  20820. org-agenda-query-string))
  20821. (set-register org-agenda-query-register org-agenda-query-string)
  20822. (org-agenda-redo))
  20823. (t (error "Canot manipulate query for %s-type agenda buffers"
  20824. org-agenda-type))))
  20825. (defun org-add-to-string (var string)
  20826. (set var (concat (symbol-value var) string)))
  20827. (defun org-agenda-goto-date (date)
  20828. "Jump to DATE in agenda."
  20829. (interactive (list (org-read-date)))
  20830. (org-agenda-list nil date))
  20831. (defun org-agenda-goto-today ()
  20832. "Go to today."
  20833. (interactive)
  20834. (org-agenda-check-type t 'timeline 'agenda)
  20835. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20836. (cond
  20837. (tdpos (goto-char tdpos))
  20838. ((eq org-agenda-type 'agenda)
  20839. (let* ((sd (time-to-days
  20840. (time-subtract (current-time)
  20841. (list 0 (* 3600 org-extend-today-until) 0))))
  20842. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20843. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20844. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20845. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20846. (org-agenda-redo)
  20847. (org-agenda-find-same-or-today-or-agenda)))
  20848. (t (error "Cannot find today")))))
  20849. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20850. (goto-char
  20851. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20852. (text-property-any (point-min) (point-max) 'org-today t)
  20853. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20854. (point-min))))
  20855. (defun org-agenda-later (arg)
  20856. "Go forward in time by thee current span.
  20857. With prefix ARG, go forward that many times the current span."
  20858. (interactive "p")
  20859. (org-agenda-check-type t 'agenda)
  20860. (let* ((span org-agenda-span)
  20861. (sd org-starting-day)
  20862. (greg (calendar-gregorian-from-absolute sd))
  20863. (cnt (get-text-property (point) 'org-day-cnt))
  20864. greg2 nd)
  20865. (cond
  20866. ((eq span 'day)
  20867. (setq sd (+ arg sd) nd 1))
  20868. ((eq span 'week)
  20869. (setq sd (+ (* 7 arg) sd) nd 7))
  20870. ((eq span 'month)
  20871. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20872. sd (calendar-absolute-from-gregorian greg2))
  20873. (setcar greg2 (1+ (car greg2)))
  20874. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20875. ((eq span 'year)
  20876. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20877. sd (calendar-absolute-from-gregorian greg2))
  20878. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20879. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20880. (let ((org-agenda-overriding-arguments
  20881. (list (car org-agenda-last-arguments) sd nd t)))
  20882. (org-agenda-redo)
  20883. (org-agenda-find-same-or-today-or-agenda cnt))))
  20884. (defun org-agenda-earlier (arg)
  20885. "Go backward in time by the current span.
  20886. With prefix ARG, go backward that many times the current span."
  20887. (interactive "p")
  20888. (org-agenda-later (- arg)))
  20889. (defun org-agenda-day-view ()
  20890. "Switch to daily view for agenda."
  20891. (interactive)
  20892. (setq org-agenda-ndays 1)
  20893. (org-agenda-change-time-span 'day))
  20894. (defun org-agenda-week-view ()
  20895. "Switch to daily view for agenda."
  20896. (interactive)
  20897. (setq org-agenda-ndays 7)
  20898. (org-agenda-change-time-span 'week))
  20899. (defun org-agenda-month-view ()
  20900. "Switch to daily view for agenda."
  20901. (interactive)
  20902. (org-agenda-change-time-span 'month))
  20903. (defun org-agenda-year-view ()
  20904. "Switch to daily view for agenda."
  20905. (interactive)
  20906. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20907. (org-agenda-change-time-span 'year)
  20908. (error "Abort")))
  20909. (defun org-agenda-change-time-span (span)
  20910. "Change the agenda view to SPAN.
  20911. SPAN may be `day', `week', `month', `year'."
  20912. (org-agenda-check-type t 'agenda)
  20913. (if (equal org-agenda-span span)
  20914. (error "Viewing span is already \"%s\"" span))
  20915. (let* ((sd (or (get-text-property (point) 'day)
  20916. org-starting-day))
  20917. (computed (org-agenda-compute-time-span sd span))
  20918. (org-agenda-overriding-arguments
  20919. (list (car org-agenda-last-arguments)
  20920. (car computed) (cdr computed) t)))
  20921. (org-agenda-redo)
  20922. (org-agenda-find-same-or-today-or-agenda))
  20923. (org-agenda-set-mode-name)
  20924. (message "Switched to %s view" span))
  20925. (defun org-agenda-compute-time-span (sd span)
  20926. "Compute starting date and number of days for agenda.
  20927. SPAN may be `day', `week', `month', `year'. The return value
  20928. is a cons cell with the starting date and the number of days,
  20929. so that the date SD will be in that range."
  20930. (let* ((greg (calendar-gregorian-from-absolute sd))
  20931. nd)
  20932. (cond
  20933. ((eq span 'day)
  20934. (setq nd 1))
  20935. ((eq span 'week)
  20936. (let* ((nt (calendar-day-of-week
  20937. (calendar-gregorian-from-absolute sd)))
  20938. (d (if org-agenda-start-on-weekday
  20939. (- nt org-agenda-start-on-weekday)
  20940. 0)))
  20941. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20942. (setq nd 7)))
  20943. ((eq span 'month)
  20944. (setq sd (calendar-absolute-from-gregorian
  20945. (list (car greg) 1 (nth 2 greg)))
  20946. nd (- (calendar-absolute-from-gregorian
  20947. (list (1+ (car greg)) 1 (nth 2 greg)))
  20948. sd)))
  20949. ((eq span 'year)
  20950. (setq sd (calendar-absolute-from-gregorian
  20951. (list 1 1 (nth 2 greg)))
  20952. nd (- (calendar-absolute-from-gregorian
  20953. (list 1 1 (1+ (nth 2 greg))))
  20954. sd))))
  20955. (cons sd nd)))
  20956. ;; FIXME: does not work if user makes date format that starts with a blank
  20957. (defun org-agenda-next-date-line (&optional arg)
  20958. "Jump to the next line indicating a date in agenda buffer."
  20959. (interactive "p")
  20960. (org-agenda-check-type t 'agenda 'timeline)
  20961. (beginning-of-line 1)
  20962. (if (looking-at "^\\S-") (forward-char 1))
  20963. (if (not (re-search-forward "^\\S-" nil t arg))
  20964. (progn
  20965. (backward-char 1)
  20966. (error "No next date after this line in this buffer")))
  20967. (goto-char (match-beginning 0)))
  20968. (defun org-agenda-previous-date-line (&optional arg)
  20969. "Jump to the previous line indicating a date in agenda buffer."
  20970. (interactive "p")
  20971. (org-agenda-check-type t 'agenda 'timeline)
  20972. (beginning-of-line 1)
  20973. (if (not (re-search-backward "^\\S-" nil t arg))
  20974. (error "No previous date before this line in this buffer")))
  20975. ;; Initialize the highlight
  20976. (defvar org-hl (org-make-overlay 1 1))
  20977. (org-overlay-put org-hl 'face 'highlight)
  20978. (defun org-highlight (begin end &optional buffer)
  20979. "Highlight a region with overlay."
  20980. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20981. org-hl begin end (or buffer (current-buffer))))
  20982. (defun org-unhighlight ()
  20983. "Detach overlay INDEX."
  20984. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20985. ;; FIXME this is currently not used.
  20986. (defun org-highlight-until-next-command (beg end &optional buffer)
  20987. (org-highlight beg end buffer)
  20988. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20989. (defun org-unhighlight-once ()
  20990. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20991. (org-unhighlight))
  20992. (defun org-agenda-follow-mode ()
  20993. "Toggle follow mode in an agenda buffer."
  20994. (interactive)
  20995. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20996. (org-agenda-set-mode-name)
  20997. (message "Follow mode is %s"
  20998. (if org-agenda-follow-mode "on" "off")))
  20999. (defun org-agenda-log-mode ()
  21000. "Toggle log mode in an agenda buffer."
  21001. (interactive)
  21002. (org-agenda-check-type t 'agenda 'timeline)
  21003. (setq org-agenda-show-log (not org-agenda-show-log))
  21004. (org-agenda-set-mode-name)
  21005. (org-agenda-redo)
  21006. (message "Log mode is %s"
  21007. (if org-agenda-show-log "on" "off")))
  21008. (defun org-agenda-toggle-diary ()
  21009. "Toggle diary inclusion in an agenda buffer."
  21010. (interactive)
  21011. (org-agenda-check-type t 'agenda)
  21012. (setq org-agenda-include-diary (not org-agenda-include-diary))
  21013. (org-agenda-redo)
  21014. (org-agenda-set-mode-name)
  21015. (message "Diary inclusion turned %s"
  21016. (if org-agenda-include-diary "on" "off")))
  21017. (defun org-agenda-toggle-time-grid ()
  21018. "Toggle time grid in an agenda buffer."
  21019. (interactive)
  21020. (org-agenda-check-type t 'agenda)
  21021. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  21022. (org-agenda-redo)
  21023. (org-agenda-set-mode-name)
  21024. (message "Time-grid turned %s"
  21025. (if org-agenda-use-time-grid "on" "off")))
  21026. (defun org-agenda-set-mode-name ()
  21027. "Set the mode name to indicate all the small mode settings."
  21028. (setq mode-name
  21029. (concat "Org-Agenda"
  21030. (if (equal org-agenda-ndays 1) " Day" "")
  21031. (if (equal org-agenda-ndays 7) " Week" "")
  21032. (if org-agenda-follow-mode " Follow" "")
  21033. (if org-agenda-include-diary " Diary" "")
  21034. (if org-agenda-use-time-grid " Grid" "")
  21035. (if org-agenda-show-log " Log" "")))
  21036. (force-mode-line-update))
  21037. (defun org-agenda-post-command-hook ()
  21038. (and (eolp) (not (bolp)) (backward-char 1))
  21039. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  21040. (if (and org-agenda-follow-mode
  21041. (get-text-property (point) 'org-marker))
  21042. (org-agenda-show)))
  21043. (defun org-agenda-show-priority ()
  21044. "Show the priority of the current item.
  21045. This priority is composed of the main priority given with the [#A] cookies,
  21046. and by additional input from the age of a schedules or deadline entry."
  21047. (interactive)
  21048. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  21049. (message "Priority is %d" (if pri pri -1000))))
  21050. (defun org-agenda-show-tags ()
  21051. "Show the tags applicable to the current item."
  21052. (interactive)
  21053. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  21054. (if tags
  21055. (message "Tags are :%s:"
  21056. (org-no-properties (mapconcat 'identity tags ":")))
  21057. (message "No tags associated with this line"))))
  21058. (defun org-agenda-goto (&optional highlight)
  21059. "Go to the Org-mode file which contains the item at point."
  21060. (interactive)
  21061. (let* ((marker (or (get-text-property (point) 'org-marker)
  21062. (org-agenda-error)))
  21063. (buffer (marker-buffer marker))
  21064. (pos (marker-position marker)))
  21065. (switch-to-buffer-other-window buffer)
  21066. (widen)
  21067. (goto-char pos)
  21068. (when (org-mode-p)
  21069. (org-show-context 'agenda)
  21070. (save-excursion
  21071. (and (outline-next-heading)
  21072. (org-flag-heading nil)))) ; show the next heading
  21073. (recenter (/ (window-height) 2))
  21074. (run-hooks 'org-agenda-after-show-hook)
  21075. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  21076. (defvar org-agenda-after-show-hook nil
  21077. "Normal hook run after an item has been shown from the agenda.
  21078. Point is in the buffer where the item originated.")
  21079. (defun org-agenda-kill ()
  21080. "Kill the entry or subtree belonging to the current agenda entry."
  21081. (interactive)
  21082. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21083. (let* ((marker (or (get-text-property (point) 'org-marker)
  21084. (org-agenda-error)))
  21085. (buffer (marker-buffer marker))
  21086. (pos (marker-position marker))
  21087. (type (get-text-property (point) 'type))
  21088. dbeg dend (n 0) conf)
  21089. (org-with-remote-undo buffer
  21090. (with-current-buffer buffer
  21091. (save-excursion
  21092. (goto-char pos)
  21093. (if (and (org-mode-p) (not (member type '("sexp"))))
  21094. (setq dbeg (progn (org-back-to-heading t) (point))
  21095. dend (org-end-of-subtree t t))
  21096. (setq dbeg (point-at-bol)
  21097. dend (min (point-max) (1+ (point-at-eol)))))
  21098. (goto-char dbeg)
  21099. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  21100. (setq conf (or (eq t org-agenda-confirm-kill)
  21101. (and (numberp org-agenda-confirm-kill)
  21102. (> n org-agenda-confirm-kill))))
  21103. (and conf
  21104. (not (y-or-n-p
  21105. (format "Delete entry with %d lines in buffer \"%s\"? "
  21106. n (buffer-name buffer))))
  21107. (error "Abort"))
  21108. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  21109. (with-current-buffer buffer (delete-region dbeg dend))
  21110. (message "Agenda item and source killed"))))
  21111. (defun org-agenda-archive ()
  21112. "Kill the entry or subtree belonging to the current agenda entry."
  21113. (interactive)
  21114. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21115. (let* ((marker (or (get-text-property (point) 'org-marker)
  21116. (org-agenda-error)))
  21117. (buffer (marker-buffer marker))
  21118. (pos (marker-position marker)))
  21119. (org-with-remote-undo buffer
  21120. (with-current-buffer buffer
  21121. (if (org-mode-p)
  21122. (save-excursion
  21123. (goto-char pos)
  21124. (org-remove-subtree-entries-from-agenda)
  21125. (org-back-to-heading t)
  21126. (org-archive-subtree))
  21127. (error "Archiving works only in Org-mode files"))))))
  21128. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  21129. "Remove all lines in the agenda that correspond to a given subtree.
  21130. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  21131. If this information is not given, the function uses the tree at point."
  21132. (let ((buf (or buf (current-buffer))) m p)
  21133. (save-excursion
  21134. (unless (and beg end)
  21135. (org-back-to-heading t)
  21136. (setq beg (point))
  21137. (org-end-of-subtree t)
  21138. (setq end (point)))
  21139. (set-buffer (get-buffer org-agenda-buffer-name))
  21140. (save-excursion
  21141. (goto-char (point-max))
  21142. (beginning-of-line 1)
  21143. (while (not (bobp))
  21144. (when (and (setq m (get-text-property (point) 'org-marker))
  21145. (equal buf (marker-buffer m))
  21146. (setq p (marker-position m))
  21147. (>= p beg)
  21148. (<= p end))
  21149. (let ((inhibit-read-only t))
  21150. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21151. (beginning-of-line 0))))))
  21152. (defun org-agenda-open-link ()
  21153. "Follow the link in the current line, if any."
  21154. (interactive)
  21155. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21156. (save-excursion
  21157. (save-restriction
  21158. (narrow-to-region (point-at-bol) (point-at-eol))
  21159. (org-open-at-point))))
  21160. (defun org-agenda-copy-local-variable (var)
  21161. "Get a variable from a referenced buffer and install it here."
  21162. (let ((m (get-text-property (point) 'org-marker)))
  21163. (when (and m (buffer-live-p (marker-buffer m)))
  21164. (org-set-local var (with-current-buffer (marker-buffer m)
  21165. (symbol-value var))))))
  21166. (defun org-agenda-switch-to (&optional delete-other-windows)
  21167. "Go to the Org-mode file which contains the item at point."
  21168. (interactive)
  21169. (let* ((marker (or (get-text-property (point) 'org-marker)
  21170. (org-agenda-error)))
  21171. (buffer (marker-buffer marker))
  21172. (pos (marker-position marker)))
  21173. (switch-to-buffer buffer)
  21174. (and delete-other-windows (delete-other-windows))
  21175. (widen)
  21176. (goto-char pos)
  21177. (when (org-mode-p)
  21178. (org-show-context 'agenda)
  21179. (save-excursion
  21180. (and (outline-next-heading)
  21181. (org-flag-heading nil)))))) ; show the next heading
  21182. (defun org-agenda-goto-mouse (ev)
  21183. "Go to the Org-mode file which contains the item at the mouse click."
  21184. (interactive "e")
  21185. (mouse-set-point ev)
  21186. (org-agenda-goto))
  21187. (defun org-agenda-show ()
  21188. "Display the Org-mode file which contains the item at point."
  21189. (interactive)
  21190. (let ((win (selected-window)))
  21191. (org-agenda-goto t)
  21192. (select-window win)))
  21193. (defun org-agenda-recenter (arg)
  21194. "Display the Org-mode file which contains the item at point and recenter."
  21195. (interactive "P")
  21196. (let ((win (selected-window)))
  21197. (org-agenda-goto t)
  21198. (recenter arg)
  21199. (select-window win)))
  21200. (defun org-agenda-show-mouse (ev)
  21201. "Display the Org-mode file which contains the item at the mouse click."
  21202. (interactive "e")
  21203. (mouse-set-point ev)
  21204. (org-agenda-show))
  21205. (defun org-agenda-check-no-diary ()
  21206. "Check if the entry is a diary link and abort if yes."
  21207. (if (get-text-property (point) 'org-agenda-diary-link)
  21208. (org-agenda-error)))
  21209. (defun org-agenda-error ()
  21210. (error "Command not allowed in this line"))
  21211. (defun org-agenda-tree-to-indirect-buffer ()
  21212. "Show the subtree corresponding to the current entry in an indirect buffer.
  21213. This calls the command `org-tree-to-indirect-buffer' from the original
  21214. Org-mode buffer.
  21215. With numerical prefix arg ARG, go up to this level and then take that tree.
  21216. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21217. dedicated frame)."
  21218. (interactive)
  21219. (org-agenda-check-no-diary)
  21220. (let* ((marker (or (get-text-property (point) 'org-marker)
  21221. (org-agenda-error)))
  21222. (buffer (marker-buffer marker))
  21223. (pos (marker-position marker)))
  21224. (with-current-buffer buffer
  21225. (save-excursion
  21226. (goto-char pos)
  21227. (call-interactively 'org-tree-to-indirect-buffer)))))
  21228. (defvar org-last-heading-marker (make-marker)
  21229. "Marker pointing to the headline that last changed its TODO state
  21230. by a remote command from the agenda.")
  21231. (defun org-agenda-todo-nextset ()
  21232. "Switch TODO entry to next sequence."
  21233. (interactive)
  21234. (org-agenda-todo 'nextset))
  21235. (defun org-agenda-todo-previousset ()
  21236. "Switch TODO entry to previous sequence."
  21237. (interactive)
  21238. (org-agenda-todo 'previousset))
  21239. (defun org-agenda-todo (&optional arg)
  21240. "Cycle TODO state of line at point, also in Org-mode file.
  21241. This changes the line at point, all other lines in the agenda referring to
  21242. the same tree node, and the headline of the tree node in the Org-mode file."
  21243. (interactive "P")
  21244. (org-agenda-check-no-diary)
  21245. (let* ((col (current-column))
  21246. (marker (or (get-text-property (point) 'org-marker)
  21247. (org-agenda-error)))
  21248. (buffer (marker-buffer marker))
  21249. (pos (marker-position marker))
  21250. (hdmarker (get-text-property (point) 'org-hd-marker))
  21251. (inhibit-read-only t)
  21252. newhead)
  21253. (org-with-remote-undo buffer
  21254. (with-current-buffer buffer
  21255. (widen)
  21256. (goto-char pos)
  21257. (org-show-context 'agenda)
  21258. (save-excursion
  21259. (and (outline-next-heading)
  21260. (org-flag-heading nil))) ; show the next heading
  21261. (org-todo arg)
  21262. (and (bolp) (forward-char 1))
  21263. (setq newhead (org-get-heading))
  21264. (save-excursion
  21265. (org-back-to-heading)
  21266. (move-marker org-last-heading-marker (point))))
  21267. (beginning-of-line 1)
  21268. (save-excursion
  21269. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21270. (move-to-column col))))
  21271. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21272. "Change all lines in the agenda buffer which match HDMARKER.
  21273. The new content of the line will be NEWHEAD (as modified by
  21274. `org-format-agenda-item'). HDMARKER is checked with
  21275. `equal' against all `org-hd-marker' text properties in the file.
  21276. If FIXFACE is non-nil, the face of each item is modified acording to
  21277. the new TODO state."
  21278. (let* ((inhibit-read-only t)
  21279. props m pl undone-face done-face finish new dotime cat tags)
  21280. (save-excursion
  21281. (goto-char (point-max))
  21282. (beginning-of-line 1)
  21283. (while (not finish)
  21284. (setq finish (bobp))
  21285. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21286. (equal m hdmarker))
  21287. (setq props (text-properties-at (point))
  21288. dotime (get-text-property (point) 'dotime)
  21289. cat (get-text-property (point) 'org-category)
  21290. tags (get-text-property (point) 'tags)
  21291. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21292. pl (get-text-property (point) 'prefix-length)
  21293. undone-face (get-text-property (point) 'undone-face)
  21294. done-face (get-text-property (point) 'done-face))
  21295. (move-to-column pl)
  21296. (cond
  21297. ((equal new "")
  21298. (beginning-of-line 1)
  21299. (and (looking-at ".*\n?") (replace-match "")))
  21300. ((looking-at ".*")
  21301. (replace-match new t t)
  21302. (beginning-of-line 1)
  21303. (add-text-properties (point-at-bol) (point-at-eol) props)
  21304. (when fixface
  21305. (add-text-properties
  21306. (point-at-bol) (point-at-eol)
  21307. (list 'face
  21308. (if org-last-todo-state-is-todo
  21309. undone-face done-face))))
  21310. (org-agenda-highlight-todo 'line)
  21311. (beginning-of-line 1))
  21312. (t (error "Line update did not work"))))
  21313. (beginning-of-line 0)))
  21314. (org-finalize-agenda)))
  21315. (defun org-agenda-align-tags (&optional line)
  21316. "Align all tags in agenda items to `org-agenda-tags-column'."
  21317. (let ((inhibit-read-only t) l c)
  21318. (save-excursion
  21319. (goto-char (if line (point-at-bol) (point-min)))
  21320. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21321. (if line (point-at-eol) nil) t)
  21322. (add-text-properties
  21323. (match-beginning 2) (match-end 2)
  21324. (list 'face (delq nil (list 'org-tag (get-text-property
  21325. (match-beginning 2) 'face)))))
  21326. (setq l (- (match-end 2) (match-beginning 2))
  21327. c (if (< org-agenda-tags-column 0)
  21328. (- (abs org-agenda-tags-column) l)
  21329. org-agenda-tags-column))
  21330. (delete-region (match-beginning 1) (match-end 1))
  21331. (goto-char (match-beginning 1))
  21332. (insert (org-add-props
  21333. (make-string (max 1 (- c (current-column))) ?\ )
  21334. (text-properties-at (point))))))))
  21335. (defun org-agenda-priority-up ()
  21336. "Increase the priority of line at point, also in Org-mode file."
  21337. (interactive)
  21338. (org-agenda-priority 'up))
  21339. (defun org-agenda-priority-down ()
  21340. "Decrease the priority of line at point, also in Org-mode file."
  21341. (interactive)
  21342. (org-agenda-priority 'down))
  21343. (defun org-agenda-priority (&optional force-direction)
  21344. "Set the priority of line at point, also in Org-mode file.
  21345. This changes the line at point, all other lines in the agenda referring to
  21346. the same tree node, and the headline of the tree node in the Org-mode file."
  21347. (interactive)
  21348. (org-agenda-check-no-diary)
  21349. (let* ((marker (or (get-text-property (point) 'org-marker)
  21350. (org-agenda-error)))
  21351. (hdmarker (get-text-property (point) 'org-hd-marker))
  21352. (buffer (marker-buffer hdmarker))
  21353. (pos (marker-position hdmarker))
  21354. (inhibit-read-only t)
  21355. newhead)
  21356. (org-with-remote-undo buffer
  21357. (with-current-buffer buffer
  21358. (widen)
  21359. (goto-char pos)
  21360. (org-show-context 'agenda)
  21361. (save-excursion
  21362. (and (outline-next-heading)
  21363. (org-flag-heading nil))) ; show the next heading
  21364. (funcall 'org-priority force-direction)
  21365. (end-of-line 1)
  21366. (setq newhead (org-get-heading)))
  21367. (org-agenda-change-all-lines newhead hdmarker)
  21368. (beginning-of-line 1))))
  21369. (defun org-get-tags-at (&optional pos)
  21370. "Get a list of all headline tags applicable at POS.
  21371. POS defaults to point. If tags are inherited, the list contains
  21372. the targets in the same sequence as the headlines appear, i.e.
  21373. the tags of the current headline come last."
  21374. (interactive)
  21375. (let (tags lastpos)
  21376. (save-excursion
  21377. (save-restriction
  21378. (widen)
  21379. (goto-char (or pos (point)))
  21380. (save-match-data
  21381. (org-back-to-heading t)
  21382. (condition-case nil
  21383. (while (not (equal lastpos (point)))
  21384. (setq lastpos (point))
  21385. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21386. (setq tags (append (org-split-string
  21387. (org-match-string-no-properties 1) ":")
  21388. tags)))
  21389. (or org-use-tag-inheritance (error ""))
  21390. (org-up-heading-all 1))
  21391. (error nil))))
  21392. tags)))
  21393. ;; FIXME: should fix the tags property of the agenda line.
  21394. (defun org-agenda-set-tags ()
  21395. "Set tags for the current headline."
  21396. (interactive)
  21397. (org-agenda-check-no-diary)
  21398. (if (and (org-region-active-p) (interactive-p))
  21399. (call-interactively 'org-change-tag-in-region)
  21400. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21401. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21402. (org-agenda-error)))
  21403. (buffer (marker-buffer hdmarker))
  21404. (pos (marker-position hdmarker))
  21405. (inhibit-read-only t)
  21406. newhead)
  21407. (org-with-remote-undo buffer
  21408. (with-current-buffer buffer
  21409. (widen)
  21410. (goto-char pos)
  21411. (save-excursion
  21412. (org-show-context 'agenda))
  21413. (save-excursion
  21414. (and (outline-next-heading)
  21415. (org-flag-heading nil))) ; show the next heading
  21416. (goto-char pos)
  21417. (call-interactively 'org-set-tags)
  21418. (end-of-line 1)
  21419. (setq newhead (org-get-heading)))
  21420. (org-agenda-change-all-lines newhead hdmarker)
  21421. (beginning-of-line 1)))))
  21422. (defun org-agenda-toggle-archive-tag ()
  21423. "Toggle the archive tag for the current entry."
  21424. (interactive)
  21425. (org-agenda-check-no-diary)
  21426. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21427. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21428. (org-agenda-error)))
  21429. (buffer (marker-buffer hdmarker))
  21430. (pos (marker-position hdmarker))
  21431. (inhibit-read-only t)
  21432. newhead)
  21433. (org-with-remote-undo buffer
  21434. (with-current-buffer buffer
  21435. (widen)
  21436. (goto-char pos)
  21437. (org-show-context 'agenda)
  21438. (save-excursion
  21439. (and (outline-next-heading)
  21440. (org-flag-heading nil))) ; show the next heading
  21441. (call-interactively 'org-toggle-archive-tag)
  21442. (end-of-line 1)
  21443. (setq newhead (org-get-heading)))
  21444. (org-agenda-change-all-lines newhead hdmarker)
  21445. (beginning-of-line 1))))
  21446. (defun org-agenda-date-later (arg &optional what)
  21447. "Change the date of this item to one day later."
  21448. (interactive "p")
  21449. (org-agenda-check-type t 'agenda 'timeline)
  21450. (org-agenda-check-no-diary)
  21451. (let* ((marker (or (get-text-property (point) 'org-marker)
  21452. (org-agenda-error)))
  21453. (buffer (marker-buffer marker))
  21454. (pos (marker-position marker)))
  21455. (org-with-remote-undo buffer
  21456. (with-current-buffer buffer
  21457. (widen)
  21458. (goto-char pos)
  21459. (if (not (org-at-timestamp-p))
  21460. (error "Cannot find time stamp"))
  21461. (org-timestamp-change arg (or what 'day)))
  21462. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21463. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21464. (defun org-agenda-date-earlier (arg &optional what)
  21465. "Change the date of this item to one day earlier."
  21466. (interactive "p")
  21467. (org-agenda-date-later (- arg) what))
  21468. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21469. "Show new date stamp via text properties."
  21470. ;; We use text properties to make this undoable
  21471. (let ((inhibit-read-only t))
  21472. (setq stamp (concat " " prefix " => " stamp))
  21473. (save-excursion
  21474. (goto-char (point-max))
  21475. (while (not (bobp))
  21476. (when (equal marker (get-text-property (point) 'org-marker))
  21477. (move-to-column (- (window-width) (length stamp)) t)
  21478. (if (featurep 'xemacs)
  21479. ;; Use `duplicable' property to trigger undo recording
  21480. (let ((ex (make-extent nil nil))
  21481. (gl (make-glyph stamp)))
  21482. (set-glyph-face gl 'secondary-selection)
  21483. (set-extent-properties
  21484. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21485. (insert-extent ex (1- (point)) (point-at-eol)))
  21486. (add-text-properties
  21487. (1- (point)) (point-at-eol)
  21488. (list 'display (org-add-props stamp nil
  21489. 'face 'secondary-selection))))
  21490. (beginning-of-line 1))
  21491. (beginning-of-line 0)))))
  21492. (defun org-agenda-date-prompt (arg)
  21493. "Change the date of this item. Date is prompted for, with default today.
  21494. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21495. be used to request time specification in the time stamp."
  21496. (interactive "P")
  21497. (org-agenda-check-type t 'agenda 'timeline)
  21498. (org-agenda-check-no-diary)
  21499. (let* ((marker (or (get-text-property (point) 'org-marker)
  21500. (org-agenda-error)))
  21501. (buffer (marker-buffer marker))
  21502. (pos (marker-position marker)))
  21503. (org-with-remote-undo buffer
  21504. (with-current-buffer buffer
  21505. (widen)
  21506. (goto-char pos)
  21507. (if (not (org-at-timestamp-p))
  21508. (error "Cannot find time stamp"))
  21509. (org-time-stamp arg)
  21510. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21511. (defun org-agenda-schedule (arg)
  21512. "Schedule the item at point."
  21513. (interactive "P")
  21514. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21515. (org-agenda-check-no-diary)
  21516. (let* ((marker (or (get-text-property (point) 'org-marker)
  21517. (org-agenda-error)))
  21518. (buffer (marker-buffer marker))
  21519. (pos (marker-position marker))
  21520. (org-insert-labeled-timestamps-at-point nil)
  21521. ts)
  21522. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21523. (set-marker-insertion-type marker t)
  21524. (org-with-remote-undo buffer
  21525. (with-current-buffer buffer
  21526. (widen)
  21527. (goto-char pos)
  21528. (setq ts (org-schedule arg)))
  21529. (org-agenda-show-new-time marker ts "S"))
  21530. (message "Item scheduled for %s" ts)))
  21531. (defun org-agenda-deadline (arg)
  21532. "Schedule the item at point."
  21533. (interactive "P")
  21534. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21535. (org-agenda-check-no-diary)
  21536. (let* ((marker (or (get-text-property (point) 'org-marker)
  21537. (org-agenda-error)))
  21538. (buffer (marker-buffer marker))
  21539. (pos (marker-position marker))
  21540. (org-insert-labeled-timestamps-at-point nil)
  21541. ts)
  21542. (org-with-remote-undo buffer
  21543. (with-current-buffer buffer
  21544. (widen)
  21545. (goto-char pos)
  21546. (setq ts (org-deadline arg)))
  21547. (org-agenda-show-new-time marker ts "S"))
  21548. (message "Deadline for this item set to %s" ts)))
  21549. (defun org-get-heading (&optional no-tags)
  21550. "Return the heading of the current entry, without the stars."
  21551. (save-excursion
  21552. (org-back-to-heading t)
  21553. (if (looking-at
  21554. (if no-tags
  21555. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21556. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21557. (match-string 1) "")))
  21558. (defun org-agenda-clock-in (&optional arg)
  21559. "Start the clock on the currently selected item."
  21560. (interactive "P")
  21561. (org-agenda-check-no-diary)
  21562. (let* ((marker (or (get-text-property (point) 'org-marker)
  21563. (org-agenda-error)))
  21564. (pos (marker-position marker)))
  21565. (org-with-remote-undo (marker-buffer marker)
  21566. (with-current-buffer (marker-buffer marker)
  21567. (widen)
  21568. (goto-char pos)
  21569. (org-clock-in)))))
  21570. (defun org-agenda-clock-out (&optional arg)
  21571. "Stop the currently running clock."
  21572. (interactive "P")
  21573. (unless (marker-buffer org-clock-marker)
  21574. (error "No running clock"))
  21575. (org-with-remote-undo (marker-buffer org-clock-marker)
  21576. (org-clock-out)))
  21577. (defun org-agenda-clock-cancel (&optional arg)
  21578. "Cancel the currently running clock."
  21579. (interactive "P")
  21580. (unless (marker-buffer org-clock-marker)
  21581. (error "No running clock"))
  21582. (org-with-remote-undo (marker-buffer org-clock-marker)
  21583. (org-clock-cancel)))
  21584. (defun org-agenda-diary-entry ()
  21585. "Make a diary entry, like the `i' command from the calendar.
  21586. All the standard commands work: block, weekly etc."
  21587. (interactive)
  21588. (org-agenda-check-type t 'agenda 'timeline)
  21589. (require 'diary-lib)
  21590. (let* ((char (progn
  21591. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21592. (read-char-exclusive)))
  21593. (cmd (cdr (assoc char
  21594. '((?d . insert-diary-entry)
  21595. (?w . insert-weekly-diary-entry)
  21596. (?m . insert-monthly-diary-entry)
  21597. (?y . insert-yearly-diary-entry)
  21598. (?a . insert-anniversary-diary-entry)
  21599. (?b . insert-block-diary-entry)
  21600. (?c . insert-cyclic-diary-entry)))))
  21601. (oldf (symbol-function 'calendar-cursor-to-date))
  21602. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21603. (point (point))
  21604. (mark (or (mark t) (point))))
  21605. (unless cmd
  21606. (error "No command associated with <%c>" char))
  21607. (unless (and (get-text-property point 'day)
  21608. (or (not (equal ?b char))
  21609. (get-text-property mark 'day)))
  21610. (error "Don't know which date to use for diary entry"))
  21611. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21612. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21613. (let ((calendar-mark-ring
  21614. (list (calendar-gregorian-from-absolute
  21615. (or (get-text-property mark 'day)
  21616. (get-text-property point 'day))))))
  21617. (unwind-protect
  21618. (progn
  21619. (fset 'calendar-cursor-to-date
  21620. (lambda (&optional error)
  21621. (calendar-gregorian-from-absolute
  21622. (get-text-property point 'day))))
  21623. (call-interactively cmd))
  21624. (fset 'calendar-cursor-to-date oldf)))))
  21625. (defun org-agenda-execute-calendar-command (cmd)
  21626. "Execute a calendar command from the agenda, with the date associated to
  21627. the cursor position."
  21628. (org-agenda-check-type t 'agenda 'timeline)
  21629. (require 'diary-lib)
  21630. (unless (get-text-property (point) 'day)
  21631. (error "Don't know which date to use for calendar command"))
  21632. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21633. (point (point))
  21634. (date (calendar-gregorian-from-absolute
  21635. (get-text-property point 'day)))
  21636. ;; the following 3 vars are needed in the calendar
  21637. (displayed-day (extract-calendar-day date))
  21638. (displayed-month (extract-calendar-month date))
  21639. (displayed-year (extract-calendar-year date)))
  21640. (unwind-protect
  21641. (progn
  21642. (fset 'calendar-cursor-to-date
  21643. (lambda (&optional error)
  21644. (calendar-gregorian-from-absolute
  21645. (get-text-property point 'day))))
  21646. (call-interactively cmd))
  21647. (fset 'calendar-cursor-to-date oldf))))
  21648. (defun org-agenda-phases-of-moon ()
  21649. "Display the phases of the moon for the 3 months around the cursor date."
  21650. (interactive)
  21651. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21652. (defun org-agenda-holidays ()
  21653. "Display the holidays for the 3 months around the cursor date."
  21654. (interactive)
  21655. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21656. (defun org-agenda-sunrise-sunset (arg)
  21657. "Display sunrise and sunset for the cursor date.
  21658. Latitude and longitude can be specified with the variables
  21659. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21660. argument, latitude and longitude will be prompted for."
  21661. (interactive "P")
  21662. (let ((calendar-longitude (if arg nil calendar-longitude))
  21663. (calendar-latitude (if arg nil calendar-latitude))
  21664. (calendar-location-name
  21665. (if arg "the given coordinates" calendar-location-name)))
  21666. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21667. (defun org-agenda-goto-calendar ()
  21668. "Open the Emacs calendar with the date at the cursor."
  21669. (interactive)
  21670. (org-agenda-check-type t 'agenda 'timeline)
  21671. (let* ((day (or (get-text-property (point) 'day)
  21672. (error "Don't know which date to open in calendar")))
  21673. (date (calendar-gregorian-from-absolute day))
  21674. (calendar-move-hook nil)
  21675. (view-calendar-holidays-initially nil)
  21676. (view-diary-entries-initially nil))
  21677. (calendar)
  21678. (calendar-goto-date date)))
  21679. (defun org-calendar-goto-agenda ()
  21680. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21681. This is a command that has to be installed in `calendar-mode-map'."
  21682. (interactive)
  21683. (org-agenda-list nil (calendar-absolute-from-gregorian
  21684. (calendar-cursor-to-date))
  21685. nil))
  21686. (defun org-agenda-convert-date ()
  21687. (interactive)
  21688. (org-agenda-check-type t 'agenda 'timeline)
  21689. (let ((day (get-text-property (point) 'day))
  21690. date s)
  21691. (unless day
  21692. (error "Don't know which date to convert"))
  21693. (setq date (calendar-gregorian-from-absolute day))
  21694. (setq s (concat
  21695. "Gregorian: " (calendar-date-string date) "\n"
  21696. "ISO: " (calendar-iso-date-string date) "\n"
  21697. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21698. "Julian: " (calendar-julian-date-string date) "\n"
  21699. "Astron. JD: " (calendar-astro-date-string date)
  21700. " (Julian date number at noon UTC)\n"
  21701. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21702. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21703. "French: " (calendar-french-date-string date) "\n"
  21704. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21705. "Mayan: " (calendar-mayan-date-string date) "\n"
  21706. "Coptic: " (calendar-coptic-date-string date) "\n"
  21707. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21708. "Persian: " (calendar-persian-date-string date) "\n"
  21709. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21710. (with-output-to-temp-buffer "*Dates*"
  21711. (princ s))
  21712. (if (fboundp 'fit-window-to-buffer)
  21713. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21714. ;;;; Embedded LaTeX
  21715. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21716. "Keymap for the minor `org-cdlatex-mode'.")
  21717. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21718. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21719. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21720. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21721. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21722. (defvar org-cdlatex-texmathp-advice-is-done nil
  21723. "Flag remembering if we have applied the advice to texmathp already.")
  21724. (define-minor-mode org-cdlatex-mode
  21725. "Toggle the minor `org-cdlatex-mode'.
  21726. This mode supports entering LaTeX environment and math in LaTeX fragments
  21727. in Org-mode.
  21728. \\{org-cdlatex-mode-map}"
  21729. nil " OCDL" nil
  21730. (when org-cdlatex-mode (require 'cdlatex))
  21731. (unless org-cdlatex-texmathp-advice-is-done
  21732. (setq org-cdlatex-texmathp-advice-is-done t)
  21733. (defadvice texmathp (around org-math-always-on activate)
  21734. "Always return t in org-mode buffers.
  21735. This is because we want to insert math symbols without dollars even outside
  21736. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21737. en embedded LaTeX fragement, let texmathp do its job.
  21738. \\[org-cdlatex-mode-map]"
  21739. (interactive)
  21740. (let (p)
  21741. (cond
  21742. ((not (org-mode-p)) ad-do-it)
  21743. ((eq this-command 'cdlatex-math-symbol)
  21744. (setq ad-return-value t
  21745. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21746. (t
  21747. (let ((p (org-inside-LaTeX-fragment-p)))
  21748. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21749. (setq ad-return-value t
  21750. texmathp-why '("Org-mode embedded math" . 0))
  21751. (if p ad-do-it)))))))))
  21752. (defun turn-on-org-cdlatex ()
  21753. "Unconditionally turn on `org-cdlatex-mode'."
  21754. (org-cdlatex-mode 1))
  21755. (defun org-inside-LaTeX-fragment-p ()
  21756. "Test if point is inside a LaTeX fragment.
  21757. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21758. sequence appearing also before point.
  21759. Even though the matchers for math are configurable, this function assumes
  21760. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21761. delimiters are skipped when they have been removed by customization.
  21762. The return value is nil, or a cons cell with the delimiter and
  21763. and the position of this delimiter.
  21764. This function does a reasonably good job, but can locally be fooled by
  21765. for example currency specifications. For example it will assume being in
  21766. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21767. fragments that are properly closed, but during editing, we have to live
  21768. with the uncertainty caused by missing closing delimiters. This function
  21769. looks only before point, not after."
  21770. (catch 'exit
  21771. (let ((pos (point))
  21772. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21773. (lim (progn
  21774. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21775. (point)))
  21776. dd-on str (start 0) m re)
  21777. (goto-char pos)
  21778. (when dodollar
  21779. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21780. re (nth 1 (assoc "$" org-latex-regexps)))
  21781. (while (string-match re str start)
  21782. (cond
  21783. ((= (match-end 0) (length str))
  21784. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21785. ((= (match-end 0) (- (length str) 5))
  21786. (throw 'exit nil))
  21787. (t (setq start (match-end 0))))))
  21788. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21789. (goto-char pos)
  21790. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21791. (and (match-beginning 2) (throw 'exit nil))
  21792. ;; count $$
  21793. (while (re-search-backward "\\$\\$" lim t)
  21794. (setq dd-on (not dd-on)))
  21795. (goto-char pos)
  21796. (if dd-on (cons "$$" m))))))
  21797. (defun org-try-cdlatex-tab ()
  21798. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21799. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21800. - inside a LaTeX fragment, or
  21801. - after the first word in a line, where an abbreviation expansion could
  21802. insert a LaTeX environment."
  21803. (when org-cdlatex-mode
  21804. (cond
  21805. ((save-excursion
  21806. (skip-chars-backward "a-zA-Z0-9*")
  21807. (skip-chars-backward " \t")
  21808. (bolp))
  21809. (cdlatex-tab) t)
  21810. ((org-inside-LaTeX-fragment-p)
  21811. (cdlatex-tab) t)
  21812. (t nil))))
  21813. (defun org-cdlatex-underscore-caret (&optional arg)
  21814. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21815. Revert to the normal definition outside of these fragments."
  21816. (interactive "P")
  21817. (if (org-inside-LaTeX-fragment-p)
  21818. (call-interactively 'cdlatex-sub-superscript)
  21819. (let (org-cdlatex-mode)
  21820. (call-interactively (key-binding (vector last-input-event))))))
  21821. (defun org-cdlatex-math-modify (&optional arg)
  21822. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21823. Revert to the normal definition outside of these fragments."
  21824. (interactive "P")
  21825. (if (org-inside-LaTeX-fragment-p)
  21826. (call-interactively 'cdlatex-math-modify)
  21827. (let (org-cdlatex-mode)
  21828. (call-interactively (key-binding (vector last-input-event))))))
  21829. (defvar org-latex-fragment-image-overlays nil
  21830. "List of overlays carrying the images of latex fragments.")
  21831. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21832. (defun org-remove-latex-fragment-image-overlays ()
  21833. "Remove all overlays with LaTeX fragment images in current buffer."
  21834. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21835. (setq org-latex-fragment-image-overlays nil))
  21836. (defun org-preview-latex-fragment (&optional subtree)
  21837. "Preview the LaTeX fragment at point, or all locally or globally.
  21838. If the cursor is in a LaTeX fragment, create the image and overlay
  21839. it over the source code. If there is no fragment at point, display
  21840. all fragments in the current text, from one headline to the next. With
  21841. prefix SUBTREE, display all fragments in the current subtree. With a
  21842. double prefix `C-u C-u', or when the cursor is before the first headline,
  21843. display all fragments in the buffer.
  21844. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21845. (interactive "P")
  21846. (org-remove-latex-fragment-image-overlays)
  21847. (save-excursion
  21848. (save-restriction
  21849. (let (beg end at msg)
  21850. (cond
  21851. ((or (equal subtree '(16))
  21852. (not (save-excursion
  21853. (re-search-backward (concat "^" outline-regexp) nil t))))
  21854. (setq beg (point-min) end (point-max)
  21855. msg "Creating images for buffer...%s"))
  21856. ((equal subtree '(4))
  21857. (org-back-to-heading)
  21858. (setq beg (point) end (org-end-of-subtree t)
  21859. msg "Creating images for subtree...%s"))
  21860. (t
  21861. (if (setq at (org-inside-LaTeX-fragment-p))
  21862. (goto-char (max (point-min) (- (cdr at) 2)))
  21863. (org-back-to-heading))
  21864. (setq beg (point) end (progn (outline-next-heading) (point))
  21865. msg (if at "Creating image...%s"
  21866. "Creating images for entry...%s"))))
  21867. (message msg "")
  21868. (narrow-to-region beg end)
  21869. (goto-char beg)
  21870. (org-format-latex
  21871. (concat "ltxpng/" (file-name-sans-extension
  21872. (file-name-nondirectory
  21873. buffer-file-name)))
  21874. default-directory 'overlays msg at 'forbuffer)
  21875. (message msg "done. Use `C-c C-c' to remove images.")))))
  21876. (defvar org-latex-regexps
  21877. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21878. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21879. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21880. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21881. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21882. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21883. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21884. "Regular expressions for matching embedded LaTeX.")
  21885. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21886. "Replace LaTeX fragments with links to an image, and produce images."
  21887. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21888. (let* ((prefixnodir (file-name-nondirectory prefix))
  21889. (absprefix (expand-file-name prefix dir))
  21890. (todir (file-name-directory absprefix))
  21891. (opt org-format-latex-options)
  21892. (matchers (plist-get opt :matchers))
  21893. (re-list org-latex-regexps)
  21894. (cnt 0) txt link beg end re e checkdir
  21895. m n block linkfile movefile ov)
  21896. ;; Check if there are old images files with this prefix, and remove them
  21897. (when (file-directory-p todir)
  21898. (mapc 'delete-file
  21899. (directory-files
  21900. todir 'full
  21901. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21902. ;; Check the different regular expressions
  21903. (while (setq e (pop re-list))
  21904. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21905. block (if (nth 3 e) "\n\n" ""))
  21906. (when (member m matchers)
  21907. (goto-char (point-min))
  21908. (while (re-search-forward re nil t)
  21909. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21910. (setq txt (match-string n)
  21911. beg (match-beginning n) end (match-end n)
  21912. cnt (1+ cnt)
  21913. linkfile (format "%s_%04d.png" prefix cnt)
  21914. movefile (format "%s_%04d.png" absprefix cnt)
  21915. link (concat block "[[file:" linkfile "]]" block))
  21916. (if msg (message msg cnt))
  21917. (goto-char beg)
  21918. (unless checkdir ; make sure the directory exists
  21919. (setq checkdir t)
  21920. (or (file-directory-p todir) (make-directory todir)))
  21921. (org-create-formula-image
  21922. txt movefile opt forbuffer)
  21923. (if overlays
  21924. (progn
  21925. (setq ov (org-make-overlay beg end))
  21926. (if (featurep 'xemacs)
  21927. (progn
  21928. (org-overlay-put ov 'invisible t)
  21929. (org-overlay-put
  21930. ov 'end-glyph
  21931. (make-glyph (vector 'png :file movefile))))
  21932. (org-overlay-put
  21933. ov 'display
  21934. (list 'image :type 'png :file movefile :ascent 'center)))
  21935. (push ov org-latex-fragment-image-overlays)
  21936. (goto-char end))
  21937. (delete-region beg end)
  21938. (insert link))))))))
  21939. ;; This function borrows from Ganesh Swami's latex2png.el
  21940. (defun org-create-formula-image (string tofile options buffer)
  21941. (let* ((tmpdir (if (featurep 'xemacs)
  21942. (temp-directory)
  21943. temporary-file-directory))
  21944. (texfilebase (make-temp-name
  21945. (expand-file-name "orgtex" tmpdir)))
  21946. (texfile (concat texfilebase ".tex"))
  21947. (dvifile (concat texfilebase ".dvi"))
  21948. (pngfile (concat texfilebase ".png"))
  21949. (fnh (face-attribute 'default :height nil))
  21950. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21951. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21952. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21953. "Black"))
  21954. (bg (or (plist-get options (if buffer :background :html-background))
  21955. "Transparent")))
  21956. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21957. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21958. (with-temp-file texfile
  21959. (insert org-format-latex-header
  21960. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21961. (let ((dir default-directory))
  21962. (condition-case nil
  21963. (progn
  21964. (cd tmpdir)
  21965. (call-process "latex" nil nil nil texfile))
  21966. (error nil))
  21967. (cd dir))
  21968. (if (not (file-exists-p dvifile))
  21969. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21970. (call-process "dvipng" nil nil nil
  21971. "-E" "-fg" fg "-bg" bg
  21972. "-D" dpi
  21973. ;;"-x" scale "-y" scale
  21974. "-T" "tight"
  21975. "-o" pngfile
  21976. dvifile)
  21977. (if (not (file-exists-p pngfile))
  21978. (progn (message "Failed to create png file from %s" texfile) nil)
  21979. ;; Use the requested file name and clean up
  21980. (copy-file pngfile tofile 'replace)
  21981. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21982. (delete-file (concat texfilebase e)))
  21983. pngfile))))
  21984. (defun org-dvipng-color (attr)
  21985. "Return an rgb color specification for dvipng."
  21986. (apply 'format "rgb %s %s %s"
  21987. (mapcar 'org-normalize-color
  21988. (color-values (face-attribute 'default attr nil)))))
  21989. (defun org-normalize-color (value)
  21990. "Return string to be used as color value for an RGB component."
  21991. (format "%g" (/ value 65535.0)))
  21992. ;;;; Exporting
  21993. ;;; Variables, constants, and parameter plists
  21994. (defconst org-level-max 20)
  21995. (defvar org-export-html-preamble nil
  21996. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21997. (defvar org-export-html-postamble nil
  21998. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21999. (defvar org-export-html-auto-preamble t
  22000. "Should default preamble be inserted? Set by publishing functions.")
  22001. (defvar org-export-html-auto-postamble t
  22002. "Should default postamble be inserted? Set by publishing functions.")
  22003. (defvar org-current-export-file nil) ; dynamically scoped parameter
  22004. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  22005. (defconst org-export-plist-vars
  22006. '((:language . org-export-default-language)
  22007. (:customtime . org-display-custom-times)
  22008. (:headline-levels . org-export-headline-levels)
  22009. (:section-numbers . org-export-with-section-numbers)
  22010. (:table-of-contents . org-export-with-toc)
  22011. (:preserve-breaks . org-export-preserve-breaks)
  22012. (:archived-trees . org-export-with-archived-trees)
  22013. (:emphasize . org-export-with-emphasize)
  22014. (:sub-superscript . org-export-with-sub-superscripts)
  22015. (:special-strings . org-export-with-special-strings)
  22016. (:footnotes . org-export-with-footnotes)
  22017. (:drawers . org-export-with-drawers)
  22018. (:tags . org-export-with-tags)
  22019. (:TeX-macros . org-export-with-TeX-macros)
  22020. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  22021. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  22022. (:fixed-width . org-export-with-fixed-width)
  22023. (:timestamps . org-export-with-timestamps)
  22024. (:author-info . org-export-author-info)
  22025. (:time-stamp-file . org-export-time-stamp-file)
  22026. (:tables . org-export-with-tables)
  22027. (:table-auto-headline . org-export-highlight-first-table-line)
  22028. (:style . org-export-html-style)
  22029. (:agenda-style . org-agenda-export-html-style)
  22030. (:convert-org-links . org-export-html-link-org-files-as-html)
  22031. (:inline-images . org-export-html-inline-images)
  22032. (:html-extension . org-export-html-extension)
  22033. (:html-table-tag . org-export-html-table-tag)
  22034. (:expand-quoted-html . org-export-html-expand)
  22035. (:timestamp . org-export-html-with-timestamp)
  22036. (:publishing-directory . org-export-publishing-directory)
  22037. (:preamble . org-export-html-preamble)
  22038. (:postamble . org-export-html-postamble)
  22039. (:auto-preamble . org-export-html-auto-preamble)
  22040. (:auto-postamble . org-export-html-auto-postamble)
  22041. (:author . user-full-name)
  22042. (:email . user-mail-address)))
  22043. (defun org-default-export-plist ()
  22044. "Return the property list with default settings for the export variables."
  22045. (let ((l org-export-plist-vars) rtn e)
  22046. (while (setq e (pop l))
  22047. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  22048. rtn))
  22049. (defun org-infile-export-plist ()
  22050. "Return the property list with file-local settings for export."
  22051. (save-excursion
  22052. (save-restriction
  22053. (widen)
  22054. (goto-char 0)
  22055. (let ((re (org-make-options-regexp
  22056. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  22057. p key val text options)
  22058. (while (re-search-forward re nil t)
  22059. (setq key (org-match-string-no-properties 1)
  22060. val (org-match-string-no-properties 2))
  22061. (cond
  22062. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  22063. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  22064. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  22065. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  22066. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  22067. ((string-equal key "TEXT")
  22068. (setq text (if text (concat text "\n" val) val)))
  22069. ((string-equal key "OPTIONS") (setq options val))))
  22070. (setq p (plist-put p :text text))
  22071. (when options
  22072. (let ((op '(("H" . :headline-levels)
  22073. ("num" . :section-numbers)
  22074. ("toc" . :table-of-contents)
  22075. ("\\n" . :preserve-breaks)
  22076. ("@" . :expand-quoted-html)
  22077. (":" . :fixed-width)
  22078. ("|" . :tables)
  22079. ("^" . :sub-superscript)
  22080. ("-" . :special-strings)
  22081. ("f" . :footnotes)
  22082. ("d" . :drawers)
  22083. ("tags" . :tags)
  22084. ("*" . :emphasize)
  22085. ("TeX" . :TeX-macros)
  22086. ("LaTeX" . :LaTeX-fragments)
  22087. ("skip" . :skip-before-1st-heading)
  22088. ("author" . :author-info)
  22089. ("timestamp" . :time-stamp-file)))
  22090. o)
  22091. (while (setq o (pop op))
  22092. (if (string-match (concat (regexp-quote (car o))
  22093. ":\\([^ \t\n\r;,.]*\\)")
  22094. options)
  22095. (setq p (plist-put p (cdr o)
  22096. (car (read-from-string
  22097. (match-string 1 options)))))))))
  22098. p))))
  22099. (defun org-export-directory (type plist)
  22100. (let* ((val (plist-get plist :publishing-directory))
  22101. (dir (if (listp val)
  22102. (or (cdr (assoc type val)) ".")
  22103. val)))
  22104. dir))
  22105. (defun org-skip-comments (lines)
  22106. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  22107. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  22108. (re2 "^\\(\\*+\\)[ \t\n\r]")
  22109. (case-fold-search nil)
  22110. rtn line level)
  22111. (while (setq line (pop lines))
  22112. (cond
  22113. ((and (string-match re1 line)
  22114. (setq level (- (match-end 1) (match-beginning 1))))
  22115. ;; Beginning of a COMMENT subtree. Skip it.
  22116. (while (and (setq line (pop lines))
  22117. (or (not (string-match re2 line))
  22118. (> (- (match-end 1) (match-beginning 1)) level))))
  22119. (setq lines (cons line lines)))
  22120. ((string-match "^#" line)
  22121. ;; an ordinary comment line
  22122. )
  22123. ((and org-export-table-remove-special-lines
  22124. (string-match "^[ \t]*|" line)
  22125. (or (string-match "^[ \t]*| *[!_^] *|" line)
  22126. (and (string-match "| *<[0-9]+> *|" line)
  22127. (not (string-match "| *[^ <|]" line)))))
  22128. ;; a special table line that should be removed
  22129. )
  22130. (t (setq rtn (cons line rtn)))))
  22131. (nreverse rtn)))
  22132. (defun org-export (&optional arg)
  22133. (interactive)
  22134. (let ((help "[t] insert the export option template
  22135. \[v] limit export to visible part of outline tree
  22136. \[a] export as ASCII
  22137. \[h] export as HTML
  22138. \[H] export as HTML to temporary buffer
  22139. \[R] export region as HTML
  22140. \[b] export as HTML and browse immediately
  22141. \[x] export as XOXO
  22142. \[l] export as LaTeX
  22143. \[L] export as LaTeX to temporary buffer
  22144. \[i] export current file as iCalendar file
  22145. \[I] export all agenda files as iCalendar files
  22146. \[c] export agenda files into combined iCalendar file
  22147. \[F] publish current file
  22148. \[P] publish current project
  22149. \[X] publish... (project will be prompted for)
  22150. \[A] publish all projects")
  22151. (cmds
  22152. '((?t . org-insert-export-options-template)
  22153. (?v . org-export-visible)
  22154. (?a . org-export-as-ascii)
  22155. (?h . org-export-as-html)
  22156. (?b . org-export-as-html-and-open)
  22157. (?H . org-export-as-html-to-buffer)
  22158. (?R . org-export-region-as-html)
  22159. (?x . org-export-as-xoxo)
  22160. (?l . org-export-as-latex)
  22161. (?L . org-export-as-latex-to-buffer)
  22162. (?i . org-export-icalendar-this-file)
  22163. (?I . org-export-icalendar-all-agenda-files)
  22164. (?c . org-export-icalendar-combine-agenda-files)
  22165. (?F . org-publish-current-file)
  22166. (?P . org-publish-current-project)
  22167. (?X . org-publish)
  22168. (?A . org-publish-all)))
  22169. r1 r2 ass)
  22170. (save-window-excursion
  22171. (delete-other-windows)
  22172. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22173. (princ help))
  22174. (message "Select command: ")
  22175. (setq r1 (read-char-exclusive)))
  22176. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22177. (if (setq ass (assq r2 cmds))
  22178. (call-interactively (cdr ass))
  22179. (error "No command associated with key %c" r1))))
  22180. (defconst org-html-entities
  22181. '(("nbsp")
  22182. ("iexcl")
  22183. ("cent")
  22184. ("pound")
  22185. ("curren")
  22186. ("yen")
  22187. ("brvbar")
  22188. ("vert" . "&#124;")
  22189. ("sect")
  22190. ("uml")
  22191. ("copy")
  22192. ("ordf")
  22193. ("laquo")
  22194. ("not")
  22195. ("shy")
  22196. ("reg")
  22197. ("macr")
  22198. ("deg")
  22199. ("plusmn")
  22200. ("sup2")
  22201. ("sup3")
  22202. ("acute")
  22203. ("micro")
  22204. ("para")
  22205. ("middot")
  22206. ("odot"."o")
  22207. ("star"."*")
  22208. ("cedil")
  22209. ("sup1")
  22210. ("ordm")
  22211. ("raquo")
  22212. ("frac14")
  22213. ("frac12")
  22214. ("frac34")
  22215. ("iquest")
  22216. ("Agrave")
  22217. ("Aacute")
  22218. ("Acirc")
  22219. ("Atilde")
  22220. ("Auml")
  22221. ("Aring") ("AA"."&Aring;")
  22222. ("AElig")
  22223. ("Ccedil")
  22224. ("Egrave")
  22225. ("Eacute")
  22226. ("Ecirc")
  22227. ("Euml")
  22228. ("Igrave")
  22229. ("Iacute")
  22230. ("Icirc")
  22231. ("Iuml")
  22232. ("ETH")
  22233. ("Ntilde")
  22234. ("Ograve")
  22235. ("Oacute")
  22236. ("Ocirc")
  22237. ("Otilde")
  22238. ("Ouml")
  22239. ("times")
  22240. ("Oslash")
  22241. ("Ugrave")
  22242. ("Uacute")
  22243. ("Ucirc")
  22244. ("Uuml")
  22245. ("Yacute")
  22246. ("THORN")
  22247. ("szlig")
  22248. ("agrave")
  22249. ("aacute")
  22250. ("acirc")
  22251. ("atilde")
  22252. ("auml")
  22253. ("aring")
  22254. ("aelig")
  22255. ("ccedil")
  22256. ("egrave")
  22257. ("eacute")
  22258. ("ecirc")
  22259. ("euml")
  22260. ("igrave")
  22261. ("iacute")
  22262. ("icirc")
  22263. ("iuml")
  22264. ("eth")
  22265. ("ntilde")
  22266. ("ograve")
  22267. ("oacute")
  22268. ("ocirc")
  22269. ("otilde")
  22270. ("ouml")
  22271. ("divide")
  22272. ("oslash")
  22273. ("ugrave")
  22274. ("uacute")
  22275. ("ucirc")
  22276. ("uuml")
  22277. ("yacute")
  22278. ("thorn")
  22279. ("yuml")
  22280. ("fnof")
  22281. ("Alpha")
  22282. ("Beta")
  22283. ("Gamma")
  22284. ("Delta")
  22285. ("Epsilon")
  22286. ("Zeta")
  22287. ("Eta")
  22288. ("Theta")
  22289. ("Iota")
  22290. ("Kappa")
  22291. ("Lambda")
  22292. ("Mu")
  22293. ("Nu")
  22294. ("Xi")
  22295. ("Omicron")
  22296. ("Pi")
  22297. ("Rho")
  22298. ("Sigma")
  22299. ("Tau")
  22300. ("Upsilon")
  22301. ("Phi")
  22302. ("Chi")
  22303. ("Psi")
  22304. ("Omega")
  22305. ("alpha")
  22306. ("beta")
  22307. ("gamma")
  22308. ("delta")
  22309. ("epsilon")
  22310. ("varepsilon"."&epsilon;")
  22311. ("zeta")
  22312. ("eta")
  22313. ("theta")
  22314. ("iota")
  22315. ("kappa")
  22316. ("lambda")
  22317. ("mu")
  22318. ("nu")
  22319. ("xi")
  22320. ("omicron")
  22321. ("pi")
  22322. ("rho")
  22323. ("sigmaf") ("varsigma"."&sigmaf;")
  22324. ("sigma")
  22325. ("tau")
  22326. ("upsilon")
  22327. ("phi")
  22328. ("chi")
  22329. ("psi")
  22330. ("omega")
  22331. ("thetasym") ("vartheta"."&thetasym;")
  22332. ("upsih")
  22333. ("piv")
  22334. ("bull") ("bullet"."&bull;")
  22335. ("hellip") ("dots"."&hellip;")
  22336. ("prime")
  22337. ("Prime")
  22338. ("oline")
  22339. ("frasl")
  22340. ("weierp")
  22341. ("image")
  22342. ("real")
  22343. ("trade")
  22344. ("alefsym")
  22345. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22346. ("uarr") ("uparrow"."&uarr;")
  22347. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22348. ("darr")("downarrow"."&darr;")
  22349. ("harr") ("leftrightarrow"."&harr;")
  22350. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22351. ("lArr") ("Leftarrow"."&lArr;")
  22352. ("uArr") ("Uparrow"."&uArr;")
  22353. ("rArr") ("Rightarrow"."&rArr;")
  22354. ("dArr") ("Downarrow"."&dArr;")
  22355. ("hArr") ("Leftrightarrow"."&hArr;")
  22356. ("forall")
  22357. ("part") ("partial"."&part;")
  22358. ("exist") ("exists"."&exist;")
  22359. ("empty") ("emptyset"."&empty;")
  22360. ("nabla")
  22361. ("isin") ("in"."&isin;")
  22362. ("notin")
  22363. ("ni")
  22364. ("prod")
  22365. ("sum")
  22366. ("minus")
  22367. ("lowast") ("ast"."&lowast;")
  22368. ("radic")
  22369. ("prop") ("proptp"."&prop;")
  22370. ("infin") ("infty"."&infin;")
  22371. ("ang") ("angle"."&ang;")
  22372. ("and") ("wedge"."&and;")
  22373. ("or") ("vee"."&or;")
  22374. ("cap")
  22375. ("cup")
  22376. ("int")
  22377. ("there4")
  22378. ("sim")
  22379. ("cong") ("simeq"."&cong;")
  22380. ("asymp")("approx"."&asymp;")
  22381. ("ne") ("neq"."&ne;")
  22382. ("equiv")
  22383. ("le")
  22384. ("ge")
  22385. ("sub") ("subset"."&sub;")
  22386. ("sup") ("supset"."&sup;")
  22387. ("nsub")
  22388. ("sube")
  22389. ("supe")
  22390. ("oplus")
  22391. ("otimes")
  22392. ("perp")
  22393. ("sdot") ("cdot"."&sdot;")
  22394. ("lceil")
  22395. ("rceil")
  22396. ("lfloor")
  22397. ("rfloor")
  22398. ("lang")
  22399. ("rang")
  22400. ("loz") ("Diamond"."&loz;")
  22401. ("spades") ("spadesuit"."&spades;")
  22402. ("clubs") ("clubsuit"."&clubs;")
  22403. ("hearts") ("diamondsuit"."&hearts;")
  22404. ("diams") ("diamondsuit"."&diams;")
  22405. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22406. ("quot")
  22407. ("amp")
  22408. ("lt")
  22409. ("gt")
  22410. ("OElig")
  22411. ("oelig")
  22412. ("Scaron")
  22413. ("scaron")
  22414. ("Yuml")
  22415. ("circ")
  22416. ("tilde")
  22417. ("ensp")
  22418. ("emsp")
  22419. ("thinsp")
  22420. ("zwnj")
  22421. ("zwj")
  22422. ("lrm")
  22423. ("rlm")
  22424. ("ndash")
  22425. ("mdash")
  22426. ("lsquo")
  22427. ("rsquo")
  22428. ("sbquo")
  22429. ("ldquo")
  22430. ("rdquo")
  22431. ("bdquo")
  22432. ("dagger")
  22433. ("Dagger")
  22434. ("permil")
  22435. ("lsaquo")
  22436. ("rsaquo")
  22437. ("euro")
  22438. ("arccos"."arccos")
  22439. ("arcsin"."arcsin")
  22440. ("arctan"."arctan")
  22441. ("arg"."arg")
  22442. ("cos"."cos")
  22443. ("cosh"."cosh")
  22444. ("cot"."cot")
  22445. ("coth"."coth")
  22446. ("csc"."csc")
  22447. ("deg"."deg")
  22448. ("det"."det")
  22449. ("dim"."dim")
  22450. ("exp"."exp")
  22451. ("gcd"."gcd")
  22452. ("hom"."hom")
  22453. ("inf"."inf")
  22454. ("ker"."ker")
  22455. ("lg"."lg")
  22456. ("lim"."lim")
  22457. ("liminf"."liminf")
  22458. ("limsup"."limsup")
  22459. ("ln"."ln")
  22460. ("log"."log")
  22461. ("max"."max")
  22462. ("min"."min")
  22463. ("Pr"."Pr")
  22464. ("sec"."sec")
  22465. ("sin"."sin")
  22466. ("sinh"."sinh")
  22467. ("sup"."sup")
  22468. ("tan"."tan")
  22469. ("tanh"."tanh")
  22470. )
  22471. "Entities for TeX->HTML translation.
  22472. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22473. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22474. In that case, \"\\ent\" will be translated to \"&other;\".
  22475. The list contains HTML entities for Latin-1, Greek and other symbols.
  22476. It is supplemented by a number of commonly used TeX macros with appropriate
  22477. translations. There is currently no way for users to extend this.")
  22478. ;;; General functions for all backends
  22479. (defun org-cleaned-string-for-export (string &rest parameters)
  22480. "Cleanup a buffer STRING so that links can be created safely."
  22481. (interactive)
  22482. (let* ((re-radio (and org-target-link-regexp
  22483. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22484. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22485. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22486. (re-archive (concat ":" org-archive-tag ":"))
  22487. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22488. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22489. (htmlp (plist-get parameters :for-html))
  22490. (asciip (plist-get parameters :for-ascii))
  22491. (latexp (plist-get parameters :for-LaTeX))
  22492. (commentsp (plist-get parameters :comments))
  22493. (archived-trees (plist-get parameters :archived-trees))
  22494. (inhibit-read-only t)
  22495. (drawers org-drawers)
  22496. (exp-drawers (plist-get parameters :drawers))
  22497. (outline-regexp "\\*+ ")
  22498. a b xx
  22499. rtn p)
  22500. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22501. (erase-buffer)
  22502. (insert string)
  22503. ;; Remove license-to-kill stuff
  22504. (while (setq p (text-property-any (point-min) (point-max)
  22505. :org-license-to-kill t))
  22506. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22507. (let ((org-inhibit-startup t)) (org-mode))
  22508. (untabify (point-min) (point-max))
  22509. ;; Get rid of drawers
  22510. (unless (eq t exp-drawers)
  22511. (goto-char (point-min))
  22512. (let ((re (concat "^[ \t]*:\\("
  22513. (mapconcat
  22514. 'identity
  22515. (org-delete-all exp-drawers
  22516. (copy-sequence drawers))
  22517. "\\|")
  22518. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22519. (while (re-search-forward re nil t)
  22520. (replace-match ""))))
  22521. ;; Get the correct stuff before the first headline
  22522. (when (plist-get parameters :skip-before-1st-heading)
  22523. (goto-char (point-min))
  22524. (when (re-search-forward "^\\*+[ \t]" nil t)
  22525. (delete-region (point-min) (match-beginning 0))
  22526. (goto-char (point-min))
  22527. (insert "\n")))
  22528. (when (plist-get parameters :add-text)
  22529. (goto-char (point-min))
  22530. (insert (plist-get parameters :add-text) "\n"))
  22531. ;; Get rid of archived trees
  22532. (when (not (eq archived-trees t))
  22533. (goto-char (point-min))
  22534. (while (re-search-forward re-archive nil t)
  22535. (if (not (org-on-heading-p t))
  22536. (org-end-of-subtree t)
  22537. (beginning-of-line 1)
  22538. (setq a (if archived-trees
  22539. (1+ (point-at-eol)) (point))
  22540. b (org-end-of-subtree t))
  22541. (if (> b a) (delete-region a b)))))
  22542. ;; Find targets in comments and move them out of comments,
  22543. ;; but mark them as targets that should be invisible
  22544. (goto-char (point-min))
  22545. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22546. (replace-match "\\1(INVISIBLE)"))
  22547. ;; Protect backend specific stuff, throw away the others.
  22548. (let ((formatters
  22549. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22550. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22551. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22552. fmt)
  22553. (goto-char (point-min))
  22554. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22555. (goto-char (match-end 0))
  22556. (while (not (looking-at "#\\+END_EXAMPLE"))
  22557. (insert ": ")
  22558. (beginning-of-line 2)))
  22559. (goto-char (point-min))
  22560. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22561. (add-text-properties (match-beginning 0) (match-end 0)
  22562. '(org-protected t)))
  22563. (while formatters
  22564. (setq fmt (pop formatters))
  22565. (when (car fmt)
  22566. (goto-char (point-min))
  22567. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22568. ":[ \t]*\\(.*\\)") nil t)
  22569. (replace-match "\\1" t)
  22570. (add-text-properties
  22571. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22572. '(org-protected t))))
  22573. (goto-char (point-min))
  22574. (while (re-search-forward
  22575. (concat "^#\\+"
  22576. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22577. (cadddr fmt) "\\>.*\n?") nil t)
  22578. (if (car fmt)
  22579. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22580. '(org-protected t))
  22581. (delete-region (match-beginning 0) (match-end 0))))))
  22582. ;; Protect quoted subtrees
  22583. (goto-char (point-min))
  22584. (while (re-search-forward re-quote nil t)
  22585. (goto-char (match-beginning 0))
  22586. (end-of-line 1)
  22587. (add-text-properties (point) (org-end-of-subtree t)
  22588. '(org-protected t)))
  22589. ;; Protect verbatim elements
  22590. (goto-char (point-min))
  22591. (while (re-search-forward org-verbatim-re nil t)
  22592. (add-text-properties (match-beginning 4) (match-end 4)
  22593. '(org-protected t))
  22594. (goto-char (1+ (match-end 4))))
  22595. ;; Remove subtrees that are commented
  22596. (goto-char (point-min))
  22597. (while (re-search-forward re-commented nil t)
  22598. (goto-char (match-beginning 0))
  22599. (delete-region (point) (org-end-of-subtree t)))
  22600. ;; Remove special table lines
  22601. (when org-export-table-remove-special-lines
  22602. (goto-char (point-min))
  22603. (while (re-search-forward "^[ \t]*|" nil t)
  22604. (beginning-of-line 1)
  22605. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22606. (and (looking-at ".*?| *<[0-9]+> *|")
  22607. (not (looking-at ".*?| *[^ <|]"))))
  22608. (delete-region (max (point-min) (1- (point-at-bol)))
  22609. (point-at-eol))
  22610. (end-of-line 1))))
  22611. ;; Specific LaTeX stuff
  22612. (when latexp
  22613. (require 'org-export-latex nil)
  22614. (org-export-latex-cleaned-string))
  22615. (when asciip
  22616. (org-export-ascii-clean-string))
  22617. ;; Specific HTML stuff
  22618. (when htmlp
  22619. ;; Convert LaTeX fragments to images
  22620. (when (plist-get parameters :LaTeX-fragments)
  22621. (org-format-latex
  22622. (concat "ltxpng/" (file-name-sans-extension
  22623. (file-name-nondirectory
  22624. org-current-export-file)))
  22625. org-current-export-dir nil "Creating LaTeX image %s"))
  22626. (message "Exporting..."))
  22627. ;; Remove or replace comments
  22628. (goto-char (point-min))
  22629. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22630. (if commentsp
  22631. (progn (add-text-properties
  22632. (match-beginning 0) (match-end 0) '(org-protected t))
  22633. (replace-match (format commentsp (match-string 1)) t t))
  22634. (replace-match "")))
  22635. ;; Find matches for radio targets and turn them into internal links
  22636. (goto-char (point-min))
  22637. (when re-radio
  22638. (while (re-search-forward re-radio nil t)
  22639. (org-if-unprotected
  22640. (replace-match "\\1[[\\2]]"))))
  22641. ;; Find all links that contain a newline and put them into a single line
  22642. (goto-char (point-min))
  22643. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22644. (org-if-unprotected
  22645. (replace-match "\\1 \\3")
  22646. (goto-char (match-beginning 0))))
  22647. ;; Normalize links: Convert angle and plain links into bracket links
  22648. ;; Expand link abbreviations
  22649. (goto-char (point-min))
  22650. (while (re-search-forward re-plain-link nil t)
  22651. (goto-char (1- (match-end 0)))
  22652. (org-if-unprotected
  22653. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22654. ":" (match-string 3) "]]")))
  22655. ;; added 'org-link face to links
  22656. (put-text-property 0 (length s) 'face 'org-link s)
  22657. (replace-match s t t))))
  22658. (goto-char (point-min))
  22659. (while (re-search-forward re-angle-link nil t)
  22660. (goto-char (1- (match-end 0)))
  22661. (org-if-unprotected
  22662. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22663. ":" (match-string 3) "]]")))
  22664. (put-text-property 0 (length s) 'face 'org-link s)
  22665. (replace-match s t t))))
  22666. (goto-char (point-min))
  22667. (while (re-search-forward org-bracket-link-regexp nil t)
  22668. (org-if-unprotected
  22669. (let* ((s (concat "[[" (setq xx (save-match-data
  22670. (org-link-expand-abbrev (match-string 1))))
  22671. "]"
  22672. (if (match-end 3)
  22673. (match-string 2)
  22674. (concat "[" xx "]"))
  22675. "]")))
  22676. (put-text-property 0 (length s) 'face 'org-link s)
  22677. (replace-match s t t))))
  22678. ;; Find multiline emphasis and put them into single line
  22679. (when (plist-get parameters :emph-multiline)
  22680. (goto-char (point-min))
  22681. (while (re-search-forward org-emph-re nil t)
  22682. (if (not (= (char-after (match-beginning 3))
  22683. (char-after (match-beginning 4))))
  22684. (org-if-unprotected
  22685. (subst-char-in-region (match-beginning 0) (match-end 0)
  22686. ?\n ?\ t)
  22687. (goto-char (1- (match-end 0))))
  22688. (goto-char (1+ (match-beginning 0))))))
  22689. (setq rtn (buffer-string)))
  22690. (kill-buffer " org-mode-tmp")
  22691. rtn))
  22692. (defun org-export-grab-title-from-buffer ()
  22693. "Get a title for the current document, from looking at the buffer."
  22694. (let ((inhibit-read-only t))
  22695. (save-excursion
  22696. (goto-char (point-min))
  22697. (let ((end (save-excursion (outline-next-heading) (point))))
  22698. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22699. ;; Mark the line so that it will not be exported as normal text.
  22700. (org-unmodified
  22701. (add-text-properties (match-beginning 0) (match-end 0)
  22702. (list :org-license-to-kill t)))
  22703. ;; Return the title string
  22704. (org-trim (match-string 0)))))))
  22705. (defun org-export-get-title-from-subtree ()
  22706. "Return subtree title and exclude it from export."
  22707. (let (title (m (mark)))
  22708. (save-excursion
  22709. (goto-char (region-beginning))
  22710. (when (and (org-at-heading-p)
  22711. (>= (org-end-of-subtree t t) (region-end)))
  22712. ;; This is a subtree, we take the title from the first heading
  22713. (goto-char (region-beginning))
  22714. (looking-at org-todo-line-regexp)
  22715. (setq title (match-string 3))
  22716. (org-unmodified
  22717. (add-text-properties (point) (1+ (point-at-eol))
  22718. (list :org-license-to-kill t)))))
  22719. title))
  22720. (defun org-solidify-link-text (s &optional alist)
  22721. "Take link text and make a safe target out of it."
  22722. (save-match-data
  22723. (let* ((rtn
  22724. (mapconcat
  22725. 'identity
  22726. (org-split-string s "[ \t\r\n]+") "--"))
  22727. (a (assoc rtn alist)))
  22728. (or (cdr a) rtn))))
  22729. (defun org-get-min-level (lines)
  22730. "Get the minimum level in LINES."
  22731. (let ((re "^\\(\\*+\\) ") l min)
  22732. (catch 'exit
  22733. (while (setq l (pop lines))
  22734. (if (string-match re l)
  22735. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22736. 1)))
  22737. ;; Variable holding the vector with section numbers
  22738. (defvar org-section-numbers (make-vector org-level-max 0))
  22739. (defun org-init-section-numbers ()
  22740. "Initialize the vector for the section numbers."
  22741. (let* ((level -1)
  22742. (numbers (nreverse (org-split-string "" "\\.")))
  22743. (depth (1- (length org-section-numbers)))
  22744. (i depth) number-string)
  22745. (while (>= i 0)
  22746. (if (> i level)
  22747. (aset org-section-numbers i 0)
  22748. (setq number-string (or (car numbers) "0"))
  22749. (if (string-match "\\`[A-Z]\\'" number-string)
  22750. (aset org-section-numbers i
  22751. (- (string-to-char number-string) ?A -1))
  22752. (aset org-section-numbers i (string-to-number number-string)))
  22753. (pop numbers))
  22754. (setq i (1- i)))))
  22755. (defun org-section-number (&optional level)
  22756. "Return a string with the current section number.
  22757. When LEVEL is non-nil, increase section numbers on that level."
  22758. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22759. (when level
  22760. (when (> level -1)
  22761. (aset org-section-numbers
  22762. level (1+ (aref org-section-numbers level))))
  22763. (setq idx (1+ level))
  22764. (while (<= idx depth)
  22765. (if (not (= idx 1))
  22766. (aset org-section-numbers idx 0))
  22767. (setq idx (1+ idx))))
  22768. (setq idx 0)
  22769. (while (<= idx depth)
  22770. (setq n (aref org-section-numbers idx))
  22771. (setq string (concat string (if (not (string= string "")) "." "")
  22772. (int-to-string n)))
  22773. (setq idx (1+ idx)))
  22774. (save-match-data
  22775. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22776. (setq string (replace-match "" t nil string)))
  22777. (if (string-match "\\(\\.0\\)+\\'" string)
  22778. (setq string (replace-match "" t nil string))))
  22779. string))
  22780. ;;; ASCII export
  22781. (defvar org-last-level nil) ; dynamically scoped variable
  22782. (defvar org-min-level nil) ; dynamically scoped variable
  22783. (defvar org-levels-open nil) ; dynamically scoped parameter
  22784. (defvar org-ascii-current-indentation nil) ; For communication
  22785. (defun org-export-as-ascii (arg)
  22786. "Export the outline as a pretty ASCII file.
  22787. If there is an active region, export only the region.
  22788. The prefix ARG specifies how many levels of the outline should become
  22789. underlined headlines. The default is 3."
  22790. (interactive "P")
  22791. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22792. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22793. (org-infile-export-plist)))
  22794. (region-p (org-region-active-p))
  22795. (subtree-p
  22796. (when region-p
  22797. (save-excursion
  22798. (goto-char (region-beginning))
  22799. (and (org-at-heading-p)
  22800. (>= (org-end-of-subtree t t) (region-end))))))
  22801. (custom-times org-display-custom-times)
  22802. (org-ascii-current-indentation '(0 . 0))
  22803. (level 0) line txt
  22804. (umax nil)
  22805. (umax-toc nil)
  22806. (case-fold-search nil)
  22807. (filename (concat (file-name-as-directory
  22808. (org-export-directory :ascii opt-plist))
  22809. (file-name-sans-extension
  22810. (or (and subtree-p
  22811. (org-entry-get (region-beginning)
  22812. "EXPORT_FILE_NAME" t))
  22813. (file-name-nondirectory buffer-file-name)))
  22814. ".txt"))
  22815. (filename (if (equal (file-truename filename)
  22816. (file-truename buffer-file-name))
  22817. (concat filename ".txt")
  22818. filename))
  22819. (buffer (find-file-noselect filename))
  22820. (org-levels-open (make-vector org-level-max nil))
  22821. (odd org-odd-levels-only)
  22822. (date (plist-get opt-plist :date))
  22823. (author (plist-get opt-plist :author))
  22824. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22825. (plist-get opt-plist :title)
  22826. (and (not
  22827. (plist-get opt-plist :skip-before-1st-heading))
  22828. (org-export-grab-title-from-buffer))
  22829. (file-name-sans-extension
  22830. (file-name-nondirectory buffer-file-name))))
  22831. (email (plist-get opt-plist :email))
  22832. (language (plist-get opt-plist :language))
  22833. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22834. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22835. (todo nil)
  22836. (lang-words nil)
  22837. (region
  22838. (buffer-substring
  22839. (if (org-region-active-p) (region-beginning) (point-min))
  22840. (if (org-region-active-p) (region-end) (point-max))))
  22841. (lines (org-split-string
  22842. (org-cleaned-string-for-export
  22843. region
  22844. :for-ascii t
  22845. :skip-before-1st-heading
  22846. (plist-get opt-plist :skip-before-1st-heading)
  22847. :drawers (plist-get opt-plist :drawers)
  22848. :verbatim-multiline t
  22849. :archived-trees
  22850. (plist-get opt-plist :archived-trees)
  22851. :add-text (plist-get opt-plist :text))
  22852. "\n"))
  22853. thetoc have-headings first-heading-pos
  22854. table-open table-buffer)
  22855. (let ((inhibit-read-only t))
  22856. (org-unmodified
  22857. (remove-text-properties (point-min) (point-max)
  22858. '(:org-license-to-kill t))))
  22859. (setq org-min-level (org-get-min-level lines))
  22860. (setq org-last-level org-min-level)
  22861. (org-init-section-numbers)
  22862. (find-file-noselect filename)
  22863. (setq lang-words (or (assoc language org-export-language-setup)
  22864. (assoc "en" org-export-language-setup)))
  22865. (switch-to-buffer-other-window buffer)
  22866. (erase-buffer)
  22867. (fundamental-mode)
  22868. ;; create local variables for all options, to make sure all called
  22869. ;; functions get the correct information
  22870. (mapc (lambda (x)
  22871. (set (make-local-variable (cdr x))
  22872. (plist-get opt-plist (car x))))
  22873. org-export-plist-vars)
  22874. (org-set-local 'org-odd-levels-only odd)
  22875. (setq umax (if arg (prefix-numeric-value arg)
  22876. org-export-headline-levels))
  22877. (setq umax-toc (if (integerp org-export-with-toc)
  22878. (min org-export-with-toc umax)
  22879. umax))
  22880. ;; File header
  22881. (if title (org-insert-centered title ?=))
  22882. (insert "\n")
  22883. (if (and (or author email)
  22884. org-export-author-info)
  22885. (insert (concat (nth 1 lang-words) ": " (or author "")
  22886. (if email (concat " <" email ">") "")
  22887. "\n")))
  22888. (cond
  22889. ((and date (string-match "%" date))
  22890. (setq date (format-time-string date (current-time))))
  22891. (date)
  22892. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22893. (if (and date org-export-time-stamp-file)
  22894. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22895. (insert "\n\n")
  22896. (if org-export-with-toc
  22897. (progn
  22898. (push (concat (nth 3 lang-words) "\n") thetoc)
  22899. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22900. (mapc '(lambda (line)
  22901. (if (string-match org-todo-line-regexp
  22902. line)
  22903. ;; This is a headline
  22904. (progn
  22905. (setq have-headings t)
  22906. (setq level (- (match-end 1) (match-beginning 1))
  22907. level (org-tr-level level)
  22908. txt (match-string 3 line)
  22909. todo
  22910. (or (and org-export-mark-todo-in-toc
  22911. (match-beginning 2)
  22912. (not (member (match-string 2 line)
  22913. org-done-keywords)))
  22914. ; TODO, not DONE
  22915. (and org-export-mark-todo-in-toc
  22916. (= level umax-toc)
  22917. (org-search-todo-below
  22918. line lines level))))
  22919. (setq txt (org-html-expand-for-ascii txt))
  22920. (while (string-match org-bracket-link-regexp txt)
  22921. (setq txt
  22922. (replace-match
  22923. (match-string (if (match-end 2) 3 1) txt)
  22924. t t txt)))
  22925. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22926. (string-match
  22927. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22928. txt))
  22929. (setq txt (replace-match "" t t txt)))
  22930. (if (string-match quote-re0 txt)
  22931. (setq txt (replace-match "" t t txt)))
  22932. (if org-export-with-section-numbers
  22933. (setq txt (concat (org-section-number level)
  22934. " " txt)))
  22935. (if (<= level umax-toc)
  22936. (progn
  22937. (push
  22938. (concat
  22939. (make-string
  22940. (* (max 0 (- level org-min-level)) 4) ?\ )
  22941. (format (if todo "%s (*)\n" "%s\n") txt))
  22942. thetoc)
  22943. (setq org-last-level level))
  22944. ))))
  22945. lines)
  22946. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22947. (org-init-section-numbers)
  22948. (while (setq line (pop lines))
  22949. ;; Remove the quoted HTML tags.
  22950. (setq line (org-html-expand-for-ascii line))
  22951. ;; Remove targets
  22952. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22953. (setq line (replace-match "" t t line)))
  22954. ;; Replace internal links
  22955. (while (string-match org-bracket-link-regexp line)
  22956. (setq line (replace-match
  22957. (if (match-end 3) "[\\3]" "[\\1]")
  22958. t nil line)))
  22959. (when custom-times
  22960. (setq line (org-translate-time line)))
  22961. (cond
  22962. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22963. ;; a Headline
  22964. (setq first-heading-pos (or first-heading-pos (point)))
  22965. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22966. txt (match-string 2 line))
  22967. (org-ascii-level-start level txt umax lines))
  22968. ((and org-export-with-tables
  22969. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22970. (if (not table-open)
  22971. ;; New table starts
  22972. (setq table-open t table-buffer nil))
  22973. ;; Accumulate lines
  22974. (setq table-buffer (cons line table-buffer))
  22975. (when (or (not lines)
  22976. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22977. (car lines))))
  22978. (setq table-open nil
  22979. table-buffer (nreverse table-buffer))
  22980. (insert (mapconcat
  22981. (lambda (x)
  22982. (org-fix-indentation x org-ascii-current-indentation))
  22983. (org-format-table-ascii table-buffer)
  22984. "\n") "\n")))
  22985. (t
  22986. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22987. (if (and org-export-with-fixed-width
  22988. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22989. (setq line (replace-match "\\1" nil nil line)))
  22990. (insert line "\n"))))
  22991. (normal-mode)
  22992. ;; insert the table of contents
  22993. (when thetoc
  22994. (goto-char (point-min))
  22995. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22996. (progn
  22997. (goto-char (match-beginning 0))
  22998. (replace-match ""))
  22999. (goto-char first-heading-pos))
  23000. (mapc 'insert thetoc)
  23001. (or (looking-at "[ \t]*\n[ \t]*\n")
  23002. (insert "\n\n")))
  23003. ;; Convert whitespace place holders
  23004. (goto-char (point-min))
  23005. (let (beg end)
  23006. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23007. (setq end (next-single-property-change beg 'org-whitespace))
  23008. (goto-char beg)
  23009. (delete-region beg end)
  23010. (insert (make-string (- end beg) ?\ ))))
  23011. (save-buffer)
  23012. ;; remove display and invisible chars
  23013. (let (beg end)
  23014. (goto-char (point-min))
  23015. (while (setq beg (next-single-property-change (point) 'display))
  23016. (setq end (next-single-property-change beg 'display))
  23017. (delete-region beg end)
  23018. (goto-char beg)
  23019. (insert "=>"))
  23020. (goto-char (point-min))
  23021. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  23022. (setq end (next-single-property-change beg 'org-cwidth))
  23023. (delete-region beg end)
  23024. (goto-char beg)))
  23025. (goto-char (point-min))))
  23026. (defun org-export-ascii-clean-string ()
  23027. "Do extra work for ASCII export"
  23028. (goto-char (point-min))
  23029. (while (re-search-forward org-verbatim-re nil t)
  23030. (goto-char (match-end 2))
  23031. (backward-delete-char 1) (insert "'")
  23032. (goto-char (match-beginning 2))
  23033. (delete-char 1) (insert "`")
  23034. (goto-char (match-end 2))))
  23035. (defun org-search-todo-below (line lines level)
  23036. "Search the subtree below LINE for any TODO entries."
  23037. (let ((rest (cdr (memq line lines)))
  23038. (re org-todo-line-regexp)
  23039. line lv todo)
  23040. (catch 'exit
  23041. (while (setq line (pop rest))
  23042. (if (string-match re line)
  23043. (progn
  23044. (setq lv (- (match-end 1) (match-beginning 1))
  23045. todo (and (match-beginning 2)
  23046. (not (member (match-string 2 line)
  23047. org-done-keywords))))
  23048. ; TODO, not DONE
  23049. (if (<= lv level) (throw 'exit nil))
  23050. (if todo (throw 'exit t))))))))
  23051. (defun org-html-expand-for-ascii (line)
  23052. "Handle quoted HTML for ASCII export."
  23053. (if org-export-html-expand
  23054. (while (string-match "@<[^<>\n]*>" line)
  23055. ;; We just remove the tags for now.
  23056. (setq line (replace-match "" nil nil line))))
  23057. line)
  23058. (defun org-insert-centered (s &optional underline)
  23059. "Insert the string S centered and underline it with character UNDERLINE."
  23060. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  23061. (insert (make-string ind ?\ ) s "\n")
  23062. (if underline
  23063. (insert (make-string ind ?\ )
  23064. (make-string (string-width s) underline)
  23065. "\n"))))
  23066. (defun org-ascii-level-start (level title umax &optional lines)
  23067. "Insert a new level in ASCII export."
  23068. (let (char (n (- level umax 1)) (ind 0))
  23069. (if (> level umax)
  23070. (progn
  23071. (insert (make-string (* 2 n) ?\ )
  23072. (char-to-string (nth (% n (length org-export-ascii-bullets))
  23073. org-export-ascii-bullets))
  23074. " " title "\n")
  23075. ;; find the indentation of the next non-empty line
  23076. (catch 'stop
  23077. (while lines
  23078. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  23079. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  23080. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  23081. (pop lines)))
  23082. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  23083. (if (or (not (equal (char-before) ?\n))
  23084. (not (equal (char-before (1- (point))) ?\n)))
  23085. (insert "\n"))
  23086. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  23087. (unless org-export-with-tags
  23088. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23089. (setq title (replace-match "" t t title))))
  23090. (if org-export-with-section-numbers
  23091. (setq title (concat (org-section-number level) " " title)))
  23092. (insert title "\n" (make-string (string-width title) char) "\n")
  23093. (setq org-ascii-current-indentation '(0 . 0)))))
  23094. (defun org-export-visible (type arg)
  23095. "Create a copy of the visible part of the current buffer, and export it.
  23096. The copy is created in a temporary buffer and removed after use.
  23097. TYPE is the final key (as a string) that also select the export command in
  23098. the `C-c C-e' export dispatcher.
  23099. As a special case, if the you type SPC at the prompt, the temporary
  23100. org-mode file will not be removed but presented to you so that you can
  23101. continue to use it. The prefix arg ARG is passed through to the exporting
  23102. command."
  23103. (interactive
  23104. (list (progn
  23105. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  23106. (read-char-exclusive))
  23107. current-prefix-arg))
  23108. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  23109. (error "Invalid export key"))
  23110. (let* ((binding (cdr (assoc type
  23111. '((?a . org-export-as-ascii)
  23112. (?\C-a . org-export-as-ascii)
  23113. (?b . org-export-as-html-and-open)
  23114. (?\C-b . org-export-as-html-and-open)
  23115. (?h . org-export-as-html)
  23116. (?H . org-export-as-html-to-buffer)
  23117. (?R . org-export-region-as-html)
  23118. (?x . org-export-as-xoxo)))))
  23119. (keepp (equal type ?\ ))
  23120. (file buffer-file-name)
  23121. (buffer (get-buffer-create "*Org Export Visible*"))
  23122. s e)
  23123. ;; Need to hack the drawers here.
  23124. (save-excursion
  23125. (goto-char (point-min))
  23126. (while (re-search-forward org-drawer-regexp nil t)
  23127. (goto-char (match-beginning 1))
  23128. (or (org-invisible-p) (org-flag-drawer nil))))
  23129. (with-current-buffer buffer (erase-buffer))
  23130. (save-excursion
  23131. (setq s (goto-char (point-min)))
  23132. (while (not (= (point) (point-max)))
  23133. (goto-char (org-find-invisible))
  23134. (append-to-buffer buffer s (point))
  23135. (setq s (goto-char (org-find-visible))))
  23136. (org-cycle-hide-drawers 'all)
  23137. (goto-char (point-min))
  23138. (unless keepp
  23139. ;; Copy all comment lines to the end, to make sure #+ settings are
  23140. ;; still available for the second export step. Kind of a hack, but
  23141. ;; does do the trick.
  23142. (if (looking-at "#[^\r\n]*")
  23143. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23144. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23145. (append-to-buffer buffer (1+ (match-beginning 0))
  23146. (min (point-max) (1+ (match-end 0))))))
  23147. (set-buffer buffer)
  23148. (let ((buffer-file-name file)
  23149. (org-inhibit-startup t))
  23150. (org-mode)
  23151. (show-all)
  23152. (unless keepp (funcall binding arg))))
  23153. (if (not keepp)
  23154. (kill-buffer buffer)
  23155. (switch-to-buffer-other-window buffer)
  23156. (goto-char (point-min)))))
  23157. (defun org-find-visible ()
  23158. (let ((s (point)))
  23159. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23160. (get-char-property s 'invisible)))
  23161. s))
  23162. (defun org-find-invisible ()
  23163. (let ((s (point)))
  23164. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23165. (not (get-char-property s 'invisible))))
  23166. s))
  23167. ;;; HTML export
  23168. (defun org-get-current-options ()
  23169. "Return a string with current options as keyword options.
  23170. Does include HTML export options as well as TODO and CATEGORY stuff."
  23171. (format
  23172. "#+TITLE: %s
  23173. #+AUTHOR: %s
  23174. #+EMAIL: %s
  23175. #+LANGUAGE: %s
  23176. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23177. #+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
  23178. #+CATEGORY: %s
  23179. #+SEQ_TODO: %s
  23180. #+TYP_TODO: %s
  23181. #+PRIORITIES: %c %c %c
  23182. #+DRAWERS: %s
  23183. #+STARTUP: %s %s %s %s %s
  23184. #+TAGS: %s
  23185. #+ARCHIVE: %s
  23186. #+LINK: %s
  23187. "
  23188. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  23189. org-export-headline-levels
  23190. org-export-with-section-numbers
  23191. org-export-with-toc
  23192. org-export-preserve-breaks
  23193. org-export-html-expand
  23194. org-export-with-fixed-width
  23195. org-export-with-tables
  23196. org-export-with-sub-superscripts
  23197. org-export-with-special-strings
  23198. org-export-with-footnotes
  23199. org-export-with-emphasize
  23200. org-export-with-TeX-macros
  23201. org-export-with-LaTeX-fragments
  23202. org-export-skip-text-before-1st-heading
  23203. org-export-with-drawers
  23204. org-export-with-tags
  23205. (file-name-nondirectory buffer-file-name)
  23206. "TODO FEEDBACK VERIFY DONE"
  23207. "Me Jason Marie DONE"
  23208. org-highest-priority org-lowest-priority org-default-priority
  23209. (mapconcat 'identity org-drawers " ")
  23210. (cdr (assoc org-startup-folded
  23211. '((nil . "showall") (t . "overview") (content . "content"))))
  23212. (if org-odd-levels-only "odd" "oddeven")
  23213. (if org-hide-leading-stars "hidestars" "showstars")
  23214. (if org-startup-align-all-tables "align" "noalign")
  23215. (cond ((eq org-log-done t) "logdone")
  23216. ((equal org-log-done 'note) "lognotedone")
  23217. ((not org-log-done) "nologdone"))
  23218. (or (mapconcat (lambda (x)
  23219. (cond
  23220. ((equal '(:startgroup) x) "{")
  23221. ((equal '(:endgroup) x) "}")
  23222. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23223. (t (car x))))
  23224. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23225. org-archive-location
  23226. "org file:~/org/%s.org"
  23227. ))
  23228. (defun org-insert-export-options-template ()
  23229. "Insert into the buffer a template with information for exporting."
  23230. (interactive)
  23231. (if (not (bolp)) (newline))
  23232. (let ((s (org-get-current-options)))
  23233. (and (string-match "#\\+CATEGORY" s)
  23234. (setq s (substring s 0 (match-beginning 0))))
  23235. (insert s)))
  23236. (defun org-toggle-fixed-width-section (arg)
  23237. "Toggle the fixed-width export.
  23238. If there is no active region, the QUOTE keyword at the current headline is
  23239. inserted or removed. When present, it causes the text between this headline
  23240. and the next to be exported as fixed-width text, and unmodified.
  23241. If there is an active region, this command adds or removes a colon as the
  23242. first character of this line. If the first character of a line is a colon,
  23243. this line is also exported in fixed-width font."
  23244. (interactive "P")
  23245. (let* ((cc 0)
  23246. (regionp (org-region-active-p))
  23247. (beg (if regionp (region-beginning) (point)))
  23248. (end (if regionp (region-end)))
  23249. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23250. (case-fold-search nil)
  23251. (re "[ \t]*\\(:\\)")
  23252. off)
  23253. (if regionp
  23254. (save-excursion
  23255. (goto-char beg)
  23256. (setq cc (current-column))
  23257. (beginning-of-line 1)
  23258. (setq off (looking-at re))
  23259. (while (> nlines 0)
  23260. (setq nlines (1- nlines))
  23261. (beginning-of-line 1)
  23262. (cond
  23263. (arg
  23264. (move-to-column cc t)
  23265. (insert ":\n")
  23266. (forward-line -1))
  23267. ((and off (looking-at re))
  23268. (replace-match "" t t nil 1))
  23269. ((not off) (move-to-column cc t) (insert ":")))
  23270. (forward-line 1)))
  23271. (save-excursion
  23272. (org-back-to-heading)
  23273. (if (looking-at (concat outline-regexp
  23274. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23275. (replace-match "" t t nil 1)
  23276. (if (looking-at outline-regexp)
  23277. (progn
  23278. (goto-char (match-end 0))
  23279. (insert org-quote-string " "))))))))
  23280. (defun org-export-as-html-and-open (arg)
  23281. "Export the outline as HTML and immediately open it with a browser.
  23282. If there is an active region, export only the region.
  23283. The prefix ARG specifies how many levels of the outline should become
  23284. headlines. The default is 3. Lower levels will become bulleted lists."
  23285. (interactive "P")
  23286. (org-export-as-html arg 'hidden)
  23287. (org-open-file buffer-file-name))
  23288. (defun org-export-as-html-batch ()
  23289. "Call `org-export-as-html', may be used in batch processing as
  23290. emacs --batch
  23291. --load=$HOME/lib/emacs/org.el
  23292. --eval \"(setq org-export-headline-levels 2)\"
  23293. --visit=MyFile --funcall org-export-as-html-batch"
  23294. (org-export-as-html org-export-headline-levels 'hidden))
  23295. (defun org-export-as-html-to-buffer (arg)
  23296. "Call `org-exort-as-html` with output to a temporary buffer.
  23297. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23298. (interactive "P")
  23299. (org-export-as-html arg nil nil "*Org HTML Export*")
  23300. (switch-to-buffer-other-window "*Org HTML Export*"))
  23301. (defun org-replace-region-by-html (beg end)
  23302. "Assume the current region has org-mode syntax, and convert it to HTML.
  23303. This can be used in any buffer. For example, you could write an
  23304. itemized list in org-mode syntax in an HTML buffer and then use this
  23305. command to convert it."
  23306. (interactive "r")
  23307. (let (reg html buf pop-up-frames)
  23308. (save-window-excursion
  23309. (if (org-mode-p)
  23310. (setq html (org-export-region-as-html
  23311. beg end t 'string))
  23312. (setq reg (buffer-substring beg end)
  23313. buf (get-buffer-create "*Org tmp*"))
  23314. (with-current-buffer buf
  23315. (erase-buffer)
  23316. (insert reg)
  23317. (org-mode)
  23318. (setq html (org-export-region-as-html
  23319. (point-min) (point-max) t 'string)))
  23320. (kill-buffer buf)))
  23321. (delete-region beg end)
  23322. (insert html)))
  23323. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23324. "Convert region from BEG to END in org-mode buffer to HTML.
  23325. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23326. contents, and only produce the region of converted text, useful for
  23327. cut-and-paste operations.
  23328. If BUFFER is a buffer or a string, use/create that buffer as a target
  23329. of the converted HTML. If BUFFER is the symbol `string', return the
  23330. produced HTML as a string and leave not buffer behind. For example,
  23331. a Lisp program could call this function in the following way:
  23332. (setq html (org-export-region-as-html beg end t 'string))
  23333. When called interactively, the output buffer is selected, and shown
  23334. in a window. A non-interactive call will only retunr the buffer."
  23335. (interactive "r\nP")
  23336. (when (interactive-p)
  23337. (setq buffer "*Org HTML Export*"))
  23338. (let ((transient-mark-mode t) (zmacs-regions t)
  23339. rtn)
  23340. (goto-char end)
  23341. (set-mark (point)) ;; to activate the region
  23342. (goto-char beg)
  23343. (setq rtn (org-export-as-html
  23344. nil nil nil
  23345. buffer body-only))
  23346. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23347. (if (and (interactive-p) (bufferp rtn))
  23348. (switch-to-buffer-other-window rtn)
  23349. rtn)))
  23350. (defvar html-table-tag nil) ; dynamically scoped into this.
  23351. (defun org-export-as-html (arg &optional hidden ext-plist
  23352. to-buffer body-only pub-dir)
  23353. "Export the outline as a pretty HTML file.
  23354. If there is an active region, export only the region. The prefix
  23355. ARG specifies how many levels of the outline should become
  23356. headlines. The default is 3. Lower levels will become bulleted
  23357. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23358. EXT-PLIST is a property list with external parameters overriding
  23359. org-mode's default settings, but still inferior to file-local
  23360. settings. When TO-BUFFER is non-nil, create a buffer with that
  23361. name and export to that buffer. If TO-BUFFER is the symbol
  23362. `string', don't leave any buffer behind but just return the
  23363. resulting HTML as a string. When BODY-ONLY is set, don't produce
  23364. the file header and footer, simply return the content of
  23365. <body>...</body>, without even the body tags themselves. When
  23366. PUB-DIR is set, use this as the publishing directory."
  23367. (interactive "P")
  23368. ;; Make sure we have a file name when we need it.
  23369. (when (and (not (or to-buffer body-only))
  23370. (not buffer-file-name))
  23371. (if (buffer-base-buffer)
  23372. (org-set-local 'buffer-file-name
  23373. (with-current-buffer (buffer-base-buffer)
  23374. buffer-file-name))
  23375. (error "Need a file name to be able to export.")))
  23376. (message "Exporting...")
  23377. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23378. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23379. (setq-default org-done-keywords org-done-keywords)
  23380. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23381. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23382. ext-plist
  23383. (org-infile-export-plist)))
  23384. (style (plist-get opt-plist :style))
  23385. (html-extension (plist-get opt-plist :html-extension))
  23386. (link-validate (plist-get opt-plist :link-validation-function))
  23387. valid thetoc have-headings first-heading-pos
  23388. (odd org-odd-levels-only)
  23389. (region-p (org-region-active-p))
  23390. (subtree-p
  23391. (when region-p
  23392. (save-excursion
  23393. (goto-char (region-beginning))
  23394. (and (org-at-heading-p)
  23395. (>= (org-end-of-subtree t t) (region-end))))))
  23396. ;; The following two are dynamically scoped into other
  23397. ;; routines below.
  23398. (org-current-export-dir
  23399. (or pub-dir (org-export-directory :html opt-plist)))
  23400. (org-current-export-file buffer-file-name)
  23401. (level 0) (line "") (origline "") txt todo
  23402. (umax nil)
  23403. (umax-toc nil)
  23404. (filename (if to-buffer nil
  23405. (expand-file-name
  23406. (concat
  23407. (file-name-sans-extension
  23408. (or (and subtree-p
  23409. (org-entry-get (region-beginning)
  23410. "EXPORT_FILE_NAME" t))
  23411. (file-name-nondirectory buffer-file-name)))
  23412. "." html-extension)
  23413. (file-name-as-directory
  23414. (or pub-dir (org-export-directory :html opt-plist))))))
  23415. (current-dir (if buffer-file-name
  23416. (file-name-directory buffer-file-name)
  23417. default-directory))
  23418. (buffer (if to-buffer
  23419. (cond
  23420. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23421. (t (get-buffer-create to-buffer)))
  23422. (find-file-noselect filename)))
  23423. (org-levels-open (make-vector org-level-max nil))
  23424. (date (plist-get opt-plist :date))
  23425. (author (plist-get opt-plist :author))
  23426. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23427. (plist-get opt-plist :title)
  23428. (and (not
  23429. (plist-get opt-plist :skip-before-1st-heading))
  23430. (org-export-grab-title-from-buffer))
  23431. (and buffer-file-name
  23432. (file-name-sans-extension
  23433. (file-name-nondirectory buffer-file-name)))
  23434. "UNTITLED"))
  23435. (html-table-tag (plist-get opt-plist :html-table-tag))
  23436. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23437. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23438. (inquote nil)
  23439. (infixed nil)
  23440. (in-local-list nil)
  23441. (local-list-num nil)
  23442. (local-list-indent nil)
  23443. (llt org-plain-list-ordered-item-terminator)
  23444. (email (plist-get opt-plist :email))
  23445. (language (plist-get opt-plist :language))
  23446. (lang-words nil)
  23447. (target-alist nil) tg
  23448. (head-count 0) cnt
  23449. (start 0)
  23450. (coding-system (and (boundp 'buffer-file-coding-system)
  23451. buffer-file-coding-system))
  23452. (coding-system-for-write (or org-export-html-coding-system
  23453. coding-system))
  23454. (save-buffer-coding-system (or org-export-html-coding-system
  23455. coding-system))
  23456. (charset (and coding-system-for-write
  23457. (fboundp 'coding-system-get)
  23458. (coding-system-get coding-system-for-write
  23459. 'mime-charset)))
  23460. (region
  23461. (buffer-substring
  23462. (if region-p (region-beginning) (point-min))
  23463. (if region-p (region-end) (point-max))))
  23464. (lines
  23465. (org-split-string
  23466. (org-cleaned-string-for-export
  23467. region
  23468. :emph-multiline t
  23469. :for-html t
  23470. :skip-before-1st-heading
  23471. (plist-get opt-plist :skip-before-1st-heading)
  23472. :drawers (plist-get opt-plist :drawers)
  23473. :archived-trees
  23474. (plist-get opt-plist :archived-trees)
  23475. :add-text
  23476. (plist-get opt-plist :text)
  23477. :LaTeX-fragments
  23478. (plist-get opt-plist :LaTeX-fragments))
  23479. "[\r\n]"))
  23480. table-open type
  23481. table-buffer table-orig-buffer
  23482. ind start-is-num starter didclose
  23483. rpl path desc descp desc1 desc2 link
  23484. )
  23485. (let ((inhibit-read-only t))
  23486. (org-unmodified
  23487. (remove-text-properties (point-min) (point-max)
  23488. '(:org-license-to-kill t))))
  23489. (message "Exporting...")
  23490. (setq org-min-level (org-get-min-level lines))
  23491. (setq org-last-level org-min-level)
  23492. (org-init-section-numbers)
  23493. (cond
  23494. ((and date (string-match "%" date))
  23495. (setq date (format-time-string date (current-time))))
  23496. (date)
  23497. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23498. ;; Get the language-dependent settings
  23499. (setq lang-words (or (assoc language org-export-language-setup)
  23500. (assoc "en" org-export-language-setup)))
  23501. ;; Switch to the output buffer
  23502. (set-buffer buffer)
  23503. (let ((inhibit-read-only t)) (erase-buffer))
  23504. (fundamental-mode)
  23505. (and (fboundp 'set-buffer-file-coding-system)
  23506. (set-buffer-file-coding-system coding-system-for-write))
  23507. (let ((case-fold-search nil)
  23508. (org-odd-levels-only odd))
  23509. ;; create local variables for all options, to make sure all called
  23510. ;; functions get the correct information
  23511. (mapc (lambda (x)
  23512. (set (make-local-variable (cdr x))
  23513. (plist-get opt-plist (car x))))
  23514. org-export-plist-vars)
  23515. (setq umax (if arg (prefix-numeric-value arg)
  23516. org-export-headline-levels))
  23517. (setq umax-toc (if (integerp org-export-with-toc)
  23518. (min org-export-with-toc umax)
  23519. umax))
  23520. (unless body-only
  23521. ;; File header
  23522. (insert (format
  23523. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23524. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23525. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23526. lang=\"%s\" xml:lang=\"%s\">
  23527. <head>
  23528. <title>%s</title>
  23529. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23530. <meta name=\"generator\" content=\"Org-mode\"/>
  23531. <meta name=\"generated\" content=\"%s\"/>
  23532. <meta name=\"author\" content=\"%s\"/>
  23533. %s
  23534. </head><body>
  23535. "
  23536. language language (org-html-expand title)
  23537. (or charset "iso-8859-1") date author style))
  23538. (insert (or (plist-get opt-plist :preamble) ""))
  23539. (when (plist-get opt-plist :auto-preamble)
  23540. (if title (insert (format org-export-html-title-format
  23541. (org-html-expand title))))))
  23542. (if (and org-export-with-toc (not body-only))
  23543. (progn
  23544. (push (format "<h%d>%s</h%d>\n"
  23545. org-export-html-toplevel-hlevel
  23546. (nth 3 lang-words)
  23547. org-export-html-toplevel-hlevel)
  23548. thetoc)
  23549. (push "<ul>\n<li>" thetoc)
  23550. (setq lines
  23551. (mapcar '(lambda (line)
  23552. (if (string-match org-todo-line-regexp line)
  23553. ;; This is a headline
  23554. (progn
  23555. (setq have-headings t)
  23556. (setq level (- (match-end 1) (match-beginning 1))
  23557. level (org-tr-level level)
  23558. txt (save-match-data
  23559. (org-html-expand
  23560. (org-export-cleanup-toc-line
  23561. (match-string 3 line))))
  23562. todo
  23563. (or (and org-export-mark-todo-in-toc
  23564. (match-beginning 2)
  23565. (not (member (match-string 2 line)
  23566. org-done-keywords)))
  23567. ; TODO, not DONE
  23568. (and org-export-mark-todo-in-toc
  23569. (= level umax-toc)
  23570. (org-search-todo-below
  23571. line lines level))))
  23572. (if (string-match
  23573. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23574. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23575. (if (string-match quote-re0 txt)
  23576. (setq txt (replace-match "" t t txt)))
  23577. (if org-export-with-section-numbers
  23578. (setq txt (concat (org-section-number level)
  23579. " " txt)))
  23580. (if (<= level (max umax umax-toc))
  23581. (setq head-count (+ head-count 1)))
  23582. (if (<= level umax-toc)
  23583. (progn
  23584. (if (> level org-last-level)
  23585. (progn
  23586. (setq cnt (- level org-last-level))
  23587. (while (>= (setq cnt (1- cnt)) 0)
  23588. (push "\n<ul>\n<li>" thetoc))
  23589. (push "\n" thetoc)))
  23590. (if (< level org-last-level)
  23591. (progn
  23592. (setq cnt (- org-last-level level))
  23593. (while (>= (setq cnt (1- cnt)) 0)
  23594. (push "</li>\n</ul>" thetoc))
  23595. (push "\n" thetoc)))
  23596. ;; Check for targets
  23597. (while (string-match org-target-regexp line)
  23598. (setq tg (match-string 1 line)
  23599. line (replace-match
  23600. (concat "@<span class=\"target\">" tg "@</span> ")
  23601. t t line))
  23602. (push (cons (org-solidify-link-text tg)
  23603. (format "sec-%d" head-count))
  23604. target-alist))
  23605. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23606. (setq txt (replace-match "" t t txt)))
  23607. (push
  23608. (format
  23609. (if todo
  23610. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23611. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23612. head-count txt) thetoc)
  23613. (setq org-last-level level))
  23614. )))
  23615. line)
  23616. lines))
  23617. (while (> org-last-level (1- org-min-level))
  23618. (setq org-last-level (1- org-last-level))
  23619. (push "</li>\n</ul>\n" thetoc))
  23620. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23621. (setq head-count 0)
  23622. (org-init-section-numbers)
  23623. (while (setq line (pop lines) origline line)
  23624. (catch 'nextline
  23625. ;; end of quote section?
  23626. (when (and inquote (string-match "^\\*+ " line))
  23627. (insert "</pre>\n")
  23628. (setq inquote nil))
  23629. ;; inside a quote section?
  23630. (when inquote
  23631. (insert (org-html-protect line) "\n")
  23632. (throw 'nextline nil))
  23633. ;; verbatim lines
  23634. (when (and org-export-with-fixed-width
  23635. (string-match "^[ \t]*:\\(.*\\)" line))
  23636. (when (not infixed)
  23637. (setq infixed t)
  23638. (insert "<pre>\n"))
  23639. (insert (org-html-protect (match-string 1 line)) "\n")
  23640. (when (and lines
  23641. (not (string-match "^[ \t]*\\(:.*\\)"
  23642. (car lines))))
  23643. (setq infixed nil)
  23644. (insert "</pre>\n"))
  23645. (throw 'nextline nil))
  23646. ;; Protected HTML
  23647. (when (get-text-property 0 'org-protected line)
  23648. (let (par)
  23649. (when (re-search-backward
  23650. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23651. (setq par (match-string 1))
  23652. (replace-match "\\2\n"))
  23653. (insert line "\n")
  23654. (while (and lines
  23655. (or (= (length (car lines)) 0)
  23656. (get-text-property 0 'org-protected (car lines))))
  23657. (insert (pop lines) "\n"))
  23658. (and par (insert "<p>\n")))
  23659. (throw 'nextline nil))
  23660. ;; Horizontal line
  23661. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23662. (insert "\n<hr/>\n")
  23663. (throw 'nextline nil))
  23664. ;; make targets to anchors
  23665. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23666. (cond
  23667. ((match-end 2)
  23668. (setq line (replace-match
  23669. (concat "@<a name=\""
  23670. (org-solidify-link-text (match-string 1 line))
  23671. "\">\\nbsp@</a>")
  23672. t t line)))
  23673. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23674. (setq line (replace-match
  23675. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23676. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23677. t t line)))
  23678. (t
  23679. (setq line (replace-match
  23680. (concat "@<a name=\""
  23681. (org-solidify-link-text (match-string 1 line))
  23682. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23683. t t line)))))
  23684. (setq line (org-html-handle-time-stamps line))
  23685. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23686. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23687. ;; Also handle sub_superscripts and checkboxes
  23688. (or (string-match org-table-hline-regexp line)
  23689. (setq line (org-html-expand line)))
  23690. ;; Format the links
  23691. (setq start 0)
  23692. (while (string-match org-bracket-link-analytic-regexp line start)
  23693. (setq start (match-beginning 0))
  23694. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23695. (setq path (match-string 3 line))
  23696. (setq desc1 (if (match-end 5) (match-string 5 line))
  23697. desc2 (if (match-end 2) (concat type ":" path) path)
  23698. descp (and desc1 (not (equal desc1 desc2)))
  23699. desc (or desc1 desc2))
  23700. ;; Make an image out of the description if that is so wanted
  23701. (when (and descp (org-file-image-p desc))
  23702. (save-match-data
  23703. (if (string-match "^file:" desc)
  23704. (setq desc (substring desc (match-end 0)))))
  23705. (setq desc (concat "<img src=\"" desc "\"/>")))
  23706. ;; FIXME: do we need to unescape here somewhere?
  23707. (cond
  23708. ((equal type "internal")
  23709. (setq rpl
  23710. (concat
  23711. "<a href=\"#"
  23712. (org-solidify-link-text
  23713. (save-match-data (org-link-unescape path)) target-alist)
  23714. "\">" desc "</a>")))
  23715. ((member type '("http" "https"))
  23716. ;; standard URL, just check if we need to inline an image
  23717. (if (and (or (eq t org-export-html-inline-images)
  23718. (and org-export-html-inline-images (not descp)))
  23719. (org-file-image-p path))
  23720. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23721. (setq link (concat type ":" path))
  23722. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23723. ((member type '("ftp" "mailto" "news"))
  23724. ;; standard URL
  23725. (setq link (concat type ":" path))
  23726. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23727. ((string= type "file")
  23728. ;; FILE link
  23729. (let* ((filename path)
  23730. (abs-p (file-name-absolute-p filename))
  23731. thefile file-is-image-p search)
  23732. (save-match-data
  23733. (if (string-match "::\\(.*\\)" filename)
  23734. (setq search (match-string 1 filename)
  23735. filename (replace-match "" t nil filename)))
  23736. (setq valid
  23737. (if (functionp link-validate)
  23738. (funcall link-validate filename current-dir)
  23739. t))
  23740. (setq file-is-image-p (org-file-image-p filename))
  23741. (setq thefile (if abs-p (expand-file-name filename) filename))
  23742. (when (and org-export-html-link-org-files-as-html
  23743. (string-match "\\.org$" thefile))
  23744. (setq thefile (concat (substring thefile 0
  23745. (match-beginning 0))
  23746. "." html-extension))
  23747. (if (and search
  23748. ;; make sure this is can be used as target search
  23749. (not (string-match "^[0-9]*$" search))
  23750. (not (string-match "^\\*" search))
  23751. (not (string-match "^/.*/$" search)))
  23752. (setq thefile (concat thefile "#"
  23753. (org-solidify-link-text
  23754. (org-link-unescape search)))))
  23755. (when (string-match "^file:" desc)
  23756. (setq desc (replace-match "" t t desc))
  23757. (if (string-match "\\.org$" desc)
  23758. (setq desc (replace-match "" t t desc))))))
  23759. (setq rpl (if (and file-is-image-p
  23760. (or (eq t org-export-html-inline-images)
  23761. (and org-export-html-inline-images
  23762. (not descp))))
  23763. (concat "<img src=\"" thefile "\"/>")
  23764. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23765. (if (not valid) (setq rpl desc))))
  23766. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23767. (setq rpl (concat "<i>&lt;" type ":"
  23768. (save-match-data (org-link-unescape path))
  23769. "&gt;</i>"))))
  23770. (setq line (replace-match rpl t t line)
  23771. start (+ start (length rpl))))
  23772. ;; TODO items
  23773. (if (and (string-match org-todo-line-regexp line)
  23774. (match-beginning 2))
  23775. (setq line
  23776. (concat (substring line 0 (match-beginning 2))
  23777. "<span class=\""
  23778. (if (member (match-string 2 line)
  23779. org-done-keywords)
  23780. "done" "todo")
  23781. "\">" (match-string 2 line)
  23782. "</span>" (substring line (match-end 2)))))
  23783. ;; Does this contain a reference to a footnote?
  23784. (when org-export-with-footnotes
  23785. (setq start 0)
  23786. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23787. (if (get-text-property (match-beginning 2) 'org-protected line)
  23788. (setq start (match-end 2))
  23789. (let ((n (match-string 2 line)))
  23790. (setq line
  23791. (replace-match
  23792. (format
  23793. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23794. (match-string 1 line) n n n)
  23795. t t line))))))
  23796. (cond
  23797. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23798. ;; This is a headline
  23799. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23800. txt (match-string 2 line))
  23801. (if (string-match quote-re0 txt)
  23802. (setq txt (replace-match "" t t txt)))
  23803. (if (<= level (max umax umax-toc))
  23804. (setq head-count (+ head-count 1)))
  23805. (when in-local-list
  23806. ;; Close any local lists before inserting a new header line
  23807. (while local-list-num
  23808. (org-close-li)
  23809. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23810. (pop local-list-num))
  23811. (setq local-list-indent nil
  23812. in-local-list nil))
  23813. (setq first-heading-pos (or first-heading-pos (point)))
  23814. (org-html-level-start level txt umax
  23815. (and org-export-with-toc (<= level umax))
  23816. head-count)
  23817. ;; QUOTES
  23818. (when (string-match quote-re line)
  23819. (insert "<pre>")
  23820. (setq inquote t)))
  23821. ((and org-export-with-tables
  23822. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23823. (if (not table-open)
  23824. ;; New table starts
  23825. (setq table-open t table-buffer nil table-orig-buffer nil))
  23826. ;; Accumulate lines
  23827. (setq table-buffer (cons line table-buffer)
  23828. table-orig-buffer (cons origline table-orig-buffer))
  23829. (when (or (not lines)
  23830. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23831. (car lines))))
  23832. (setq table-open nil
  23833. table-buffer (nreverse table-buffer)
  23834. table-orig-buffer (nreverse table-orig-buffer))
  23835. (org-close-par-maybe)
  23836. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23837. (t
  23838. ;; Normal lines
  23839. (when (string-match
  23840. (cond
  23841. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23842. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23843. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23844. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23845. line)
  23846. (setq ind (org-get-string-indentation line)
  23847. start-is-num (match-beginning 4)
  23848. starter (if (match-beginning 2)
  23849. (substring (match-string 2 line) 0 -1))
  23850. line (substring line (match-beginning 5)))
  23851. (unless (string-match "[^ \t]" line)
  23852. ;; empty line. Pretend indentation is large.
  23853. (setq ind (if org-empty-line-terminates-plain-lists
  23854. 0
  23855. (1+ (or (car local-list-indent) 1)))))
  23856. (setq didclose nil)
  23857. (while (and in-local-list
  23858. (or (and (= ind (car local-list-indent))
  23859. (not starter))
  23860. (< ind (car local-list-indent))))
  23861. (setq didclose t)
  23862. (org-close-li)
  23863. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23864. (pop local-list-num) (pop local-list-indent)
  23865. (setq in-local-list local-list-indent))
  23866. (cond
  23867. ((and starter
  23868. (or (not in-local-list)
  23869. (> ind (car local-list-indent))))
  23870. ;; Start new (level of) list
  23871. (org-close-par-maybe)
  23872. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23873. (push start-is-num local-list-num)
  23874. (push ind local-list-indent)
  23875. (setq in-local-list t))
  23876. (starter
  23877. ;; continue current list
  23878. (org-close-li)
  23879. (insert "<li>\n"))
  23880. (didclose
  23881. ;; we did close a list, normal text follows: need <p>
  23882. (org-open-par)))
  23883. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23884. (setq line
  23885. (replace-match
  23886. (if (equal (match-string 1 line) "X")
  23887. "<b>[X]</b>"
  23888. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23889. t t line))))
  23890. ;; Empty lines start a new paragraph. If hand-formatted lists
  23891. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23892. ;; also start a new paragraph.
  23893. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23894. ;; Is this the start of a footnote?
  23895. (when org-export-with-footnotes
  23896. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23897. (org-close-par-maybe)
  23898. (let ((n (match-string 1 line)))
  23899. (setq line (replace-match
  23900. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23901. ;; Check if the line break needs to be conserved
  23902. (cond
  23903. ((string-match "\\\\\\\\[ \t]*$" line)
  23904. (setq line (replace-match "<br/>" t t line)))
  23905. (org-export-preserve-breaks
  23906. (setq line (concat line "<br/>"))))
  23907. (insert line "\n")))))
  23908. ;; Properly close all local lists and other lists
  23909. (when inquote (insert "</pre>\n"))
  23910. (when in-local-list
  23911. ;; Close any local lists before inserting a new header line
  23912. (while local-list-num
  23913. (org-close-li)
  23914. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23915. (pop local-list-num))
  23916. (setq local-list-indent nil
  23917. in-local-list nil))
  23918. (org-html-level-start 1 nil umax
  23919. (and org-export-with-toc (<= level umax))
  23920. head-count)
  23921. (unless body-only
  23922. (when (plist-get opt-plist :auto-postamble)
  23923. (insert "<div id=\"postamble\">")
  23924. (when (and org-export-author-info author)
  23925. (insert "<p class=\"author\"> "
  23926. (nth 1 lang-words) ": " author "\n")
  23927. (when email
  23928. (if (listp (split-string email ",+ *"))
  23929. (mapc (lambda(e)
  23930. (insert "<a href=\"mailto:" e "\">&lt;"
  23931. e "&gt;</a>\n"))
  23932. (split-string email ",+ *"))
  23933. (insert "<a href=\"mailto:" email "\">&lt;"
  23934. email "&gt;</a>\n")))
  23935. (insert "</p>\n"))
  23936. (when (and date org-export-time-stamp-file)
  23937. (insert "<p class=\"date\"> "
  23938. (nth 2 lang-words) ": "
  23939. date "</p>\n"))
  23940. (insert "</div>"))
  23941. (if org-export-html-with-timestamp
  23942. (insert org-export-html-html-helper-timestamp))
  23943. (insert (or (plist-get opt-plist :postamble) ""))
  23944. (insert "</body>\n</html>\n"))
  23945. (normal-mode)
  23946. (if (eq major-mode default-major-mode) (html-mode))
  23947. ;; insert the table of contents
  23948. (goto-char (point-min))
  23949. (when thetoc
  23950. (if (or (re-search-forward
  23951. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23952. (re-search-forward
  23953. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23954. (progn
  23955. (goto-char (match-beginning 0))
  23956. (replace-match ""))
  23957. (goto-char first-heading-pos)
  23958. (when (looking-at "\\s-*</p>")
  23959. (goto-char (match-end 0))
  23960. (insert "\n")))
  23961. (insert "<div id=\"table-of-contents\">\n")
  23962. (mapc 'insert thetoc)
  23963. (insert "</div>\n"))
  23964. ;; remove empty paragraphs and lists
  23965. (goto-char (point-min))
  23966. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23967. (replace-match ""))
  23968. (goto-char (point-min))
  23969. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23970. (replace-match ""))
  23971. (goto-char (point-min))
  23972. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23973. (replace-match ""))
  23974. ;; Convert whitespace place holders
  23975. (goto-char (point-min))
  23976. (let (beg end n)
  23977. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23978. (setq n (get-text-property beg 'org-whitespace)
  23979. end (next-single-property-change beg 'org-whitespace))
  23980. (goto-char beg)
  23981. (delete-region beg end)
  23982. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23983. (make-string n ?x)))))
  23984. (or to-buffer (progn (save-buffer) (kill-buffer (current-buffer))))
  23985. (goto-char (point-min))
  23986. (message "Exporting... done")
  23987. (if (eq to-buffer 'string)
  23988. (prog1 (buffer-substring (point-min) (point-max))
  23989. (kill-buffer (current-buffer)))
  23990. (current-buffer)))))
  23991. (defvar org-table-colgroup-info nil)
  23992. (defun org-format-table-ascii (lines)
  23993. "Format a table for ascii export."
  23994. (if (stringp lines)
  23995. (setq lines (org-split-string lines "\n")))
  23996. (if (not (string-match "^[ \t]*|" (car lines)))
  23997. ;; Table made by table.el - test for spanning
  23998. lines
  23999. ;; A normal org table
  24000. ;; Get rid of hlines at beginning and end
  24001. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24002. (setq lines (nreverse lines))
  24003. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24004. (setq lines (nreverse lines))
  24005. (when org-export-table-remove-special-lines
  24006. ;; Check if the table has a marking column. If yes remove the
  24007. ;; column and the special lines
  24008. (setq lines (org-table-clean-before-export lines)))
  24009. ;; Get rid of the vertical lines except for grouping
  24010. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  24011. rtn line vl1 start)
  24012. (while (setq line (pop lines))
  24013. (if (string-match org-table-hline-regexp line)
  24014. (and (string-match "|\\(.*\\)|" line)
  24015. (setq line (replace-match " \\1" t nil line)))
  24016. (setq start 0 vl1 vl)
  24017. (while (string-match "|" line start)
  24018. (setq start (match-end 0))
  24019. (or (pop vl1) (setq line (replace-match " " t t line)))))
  24020. (push line rtn))
  24021. (nreverse rtn))))
  24022. (defun org-colgroup-info-to-vline-list (info)
  24023. (let (vl new last)
  24024. (while info
  24025. (setq last new new (pop info))
  24026. (if (or (memq last '(:end :startend))
  24027. (memq new '(:start :startend)))
  24028. (push t vl)
  24029. (push nil vl)))
  24030. (setq vl (nreverse vl))
  24031. (and vl (setcar vl nil))
  24032. vl))
  24033. (defun org-format-table-html (lines olines)
  24034. "Find out which HTML converter to use and return the HTML code."
  24035. (if (stringp lines)
  24036. (setq lines (org-split-string lines "\n")))
  24037. (if (string-match "^[ \t]*|" (car lines))
  24038. ;; A normal org table
  24039. (org-format-org-table-html lines)
  24040. ;; Table made by table.el - test for spanning
  24041. (let* ((hlines (delq nil (mapcar
  24042. (lambda (x)
  24043. (if (string-match "^[ \t]*\\+-" x) x
  24044. nil))
  24045. lines)))
  24046. (first (car hlines))
  24047. (ll (and (string-match "\\S-+" first)
  24048. (match-string 0 first)))
  24049. (re (concat "^[ \t]*" (regexp-quote ll)))
  24050. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  24051. hlines))))
  24052. (if (and (not spanning)
  24053. (not org-export-prefer-native-exporter-for-tables))
  24054. ;; We can use my own converter with HTML conversions
  24055. (org-format-table-table-html lines)
  24056. ;; Need to use the code generator in table.el, with the original text.
  24057. (org-format-table-table-html-using-table-generate-source olines)))))
  24058. (defun org-format-org-table-html (lines &optional splice)
  24059. "Format a table into HTML."
  24060. ;; Get rid of hlines at beginning and end
  24061. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24062. (setq lines (nreverse lines))
  24063. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24064. (setq lines (nreverse lines))
  24065. (when org-export-table-remove-special-lines
  24066. ;; Check if the table has a marking column. If yes remove the
  24067. ;; column and the special lines
  24068. (setq lines (org-table-clean-before-export lines)))
  24069. (let ((head (and org-export-highlight-first-table-line
  24070. (delq nil (mapcar
  24071. (lambda (x) (string-match "^[ \t]*|-" x))
  24072. (cdr lines)))))
  24073. (nlines 0) fnum i
  24074. tbopen line fields html gr colgropen)
  24075. (if splice (setq head nil))
  24076. (unless splice (push (if head "<thead>" "<tbody>") html))
  24077. (setq tbopen t)
  24078. (while (setq line (pop lines))
  24079. (catch 'next-line
  24080. (if (string-match "^[ \t]*|-" line)
  24081. (progn
  24082. (unless splice
  24083. (push (if head "</thead>" "</tbody>") html)
  24084. (if lines (push "<tbody>" html) (setq tbopen nil)))
  24085. (setq head nil) ;; head ends here, first time around
  24086. ;; ignore this line
  24087. (throw 'next-line t)))
  24088. ;; Break the line into fields
  24089. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24090. (unless fnum (setq fnum (make-vector (length fields) 0)))
  24091. (setq nlines (1+ nlines) i -1)
  24092. (push (concat "<tr>"
  24093. (mapconcat
  24094. (lambda (x)
  24095. (setq i (1+ i))
  24096. (if (and (< i nlines)
  24097. (string-match org-table-number-regexp x))
  24098. (incf (aref fnum i)))
  24099. (if head
  24100. (concat (car org-export-table-header-tags) x
  24101. (cdr org-export-table-header-tags))
  24102. (concat (car org-export-table-data-tags) x
  24103. (cdr org-export-table-data-tags))))
  24104. fields "")
  24105. "</tr>")
  24106. html)))
  24107. (unless splice (if tbopen (push "</tbody>" html)))
  24108. (unless splice (push "</table>\n" html))
  24109. (setq html (nreverse html))
  24110. (unless splice
  24111. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  24112. (push (mapconcat
  24113. (lambda (x)
  24114. (setq gr (pop org-table-colgroup-info))
  24115. (format "%s<col align=\"%s\"></col>%s"
  24116. (if (memq gr '(:start :startend))
  24117. (prog1
  24118. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  24119. (setq colgropen t))
  24120. "")
  24121. (if (> (/ (float x) nlines) org-table-number-fraction)
  24122. "right" "left")
  24123. (if (memq gr '(:end :startend))
  24124. (progn (setq colgropen nil) "</colgroup>")
  24125. "")))
  24126. fnum "")
  24127. html)
  24128. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  24129. (push html-table-tag html))
  24130. (concat (mapconcat 'identity html "\n") "\n")))
  24131. (defun org-table-clean-before-export (lines)
  24132. "Check if the table has a marking column.
  24133. If yes remove the column and the special lines."
  24134. (setq org-table-colgroup-info nil)
  24135. (if (memq nil
  24136. (mapcar
  24137. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24138. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24139. lines))
  24140. (progn
  24141. (setq org-table-clean-did-remove-column nil)
  24142. (delq nil
  24143. (mapcar
  24144. (lambda (x)
  24145. (cond
  24146. ((string-match "^[ \t]*| */ *|" x)
  24147. (setq org-table-colgroup-info
  24148. (mapcar (lambda (x)
  24149. (cond ((member x '("<" "&lt;")) :start)
  24150. ((member x '(">" "&gt;")) :end)
  24151. ((member x '("<>" "&lt;&gt;")) :startend)
  24152. (t nil)))
  24153. (org-split-string x "[ \t]*|[ \t]*")))
  24154. nil)
  24155. (t x)))
  24156. lines)))
  24157. (setq org-table-clean-did-remove-column t)
  24158. (delq nil
  24159. (mapcar
  24160. (lambda (x)
  24161. (cond
  24162. ((string-match "^[ \t]*| */ *|" x)
  24163. (setq org-table-colgroup-info
  24164. (mapcar (lambda (x)
  24165. (cond ((member x '("<" "&lt;")) :start)
  24166. ((member x '(">" "&gt;")) :end)
  24167. ((member x '("<>" "&lt;&gt;")) :startend)
  24168. (t nil)))
  24169. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24170. nil)
  24171. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24172. nil) ; ignore this line
  24173. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24174. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24175. ;; remove the first column
  24176. (replace-match "\\1|" t nil x))))
  24177. lines))))
  24178. (defun org-format-table-table-html (lines)
  24179. "Format a table generated by table.el into HTML.
  24180. This conversion does *not* use `table-generate-source' from table.el.
  24181. This has the advantage that Org-mode's HTML conversions can be used.
  24182. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24183. (let (line field-buffer
  24184. (head org-export-highlight-first-table-line)
  24185. fields html empty)
  24186. (setq html (concat html-table-tag "\n"))
  24187. (while (setq line (pop lines))
  24188. (setq empty "&nbsp;")
  24189. (catch 'next-line
  24190. (if (string-match "^[ \t]*\\+-" line)
  24191. (progn
  24192. (if field-buffer
  24193. (progn
  24194. (setq
  24195. html
  24196. (concat
  24197. html
  24198. "<tr>"
  24199. (mapconcat
  24200. (lambda (x)
  24201. (if (equal x "") (setq x empty))
  24202. (if head
  24203. (concat (car org-export-table-header-tags) x
  24204. (cdr org-export-table-header-tags))
  24205. (concat (car org-export-table-data-tags) x
  24206. (cdr org-export-table-data-tags))))
  24207. field-buffer "\n")
  24208. "</tr>\n"))
  24209. (setq head nil)
  24210. (setq field-buffer nil)))
  24211. ;; Ignore this line
  24212. (throw 'next-line t)))
  24213. ;; Break the line into fields and store the fields
  24214. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24215. (if field-buffer
  24216. (setq field-buffer (mapcar
  24217. (lambda (x)
  24218. (concat x "<br/>" (pop fields)))
  24219. field-buffer))
  24220. (setq field-buffer fields))))
  24221. (setq html (concat html "</table>\n"))
  24222. html))
  24223. (defun org-format-table-table-html-using-table-generate-source (lines)
  24224. "Format a table into html, using `table-generate-source' from table.el.
  24225. This has the advantage that cell- or row-spanning is allowed.
  24226. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24227. (require 'table)
  24228. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24229. (erase-buffer)
  24230. (insert (mapconcat 'identity lines "\n"))
  24231. (goto-char (point-min))
  24232. (if (not (re-search-forward "|[^+]" nil t))
  24233. (error "Error processing table"))
  24234. (table-recognize-table)
  24235. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24236. (table-generate-source 'html " org-tmp2 ")
  24237. (set-buffer " org-tmp2 ")
  24238. (buffer-substring (point-min) (point-max))))
  24239. (defun org-html-handle-time-stamps (s)
  24240. "Format time stamps in string S, or remove them."
  24241. (catch 'exit
  24242. (let (r b)
  24243. (while (string-match org-maybe-keyword-time-regexp s)
  24244. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24245. ;; never export CLOCK
  24246. (throw 'exit ""))
  24247. (or b (setq b (substring s 0 (match-beginning 0))))
  24248. (if (not org-export-with-timestamps)
  24249. (setq r (concat r (substring s 0 (match-beginning 0)))
  24250. s (substring s (match-end 0)))
  24251. (setq r (concat
  24252. r (substring s 0 (match-beginning 0))
  24253. (if (match-end 1)
  24254. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24255. (match-string 1 s)))
  24256. (format " @<span class=\"timestamp\">%s@</span>"
  24257. (substring
  24258. (org-translate-time (match-string 3 s)) 1 -1)))
  24259. s (substring s (match-end 0)))))
  24260. ;; Line break if line started and ended with time stamp stuff
  24261. (if (not r)
  24262. s
  24263. (setq r (concat r s))
  24264. (unless (string-match "\\S-" (concat b s))
  24265. (setq r (concat r "@<br/>")))
  24266. r))))
  24267. (defun org-html-protect (s)
  24268. ;; convert & to &amp;, < to &lt; and > to &gt;
  24269. (let ((start 0))
  24270. (while (string-match "&" s start)
  24271. (setq s (replace-match "&amp;" t t s)
  24272. start (1+ (match-beginning 0))))
  24273. (while (string-match "<" s)
  24274. (setq s (replace-match "&lt;" t t s)))
  24275. (while (string-match ">" s)
  24276. (setq s (replace-match "&gt;" t t s))))
  24277. s)
  24278. (defun org-export-cleanup-toc-line (s)
  24279. "Remove tags and time staps from lines going into the toc."
  24280. (when (memq org-export-with-tags '(not-in-toc nil))
  24281. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24282. (setq s (replace-match "" t t s))))
  24283. (when org-export-remove-timestamps-from-toc
  24284. (while (string-match org-maybe-keyword-time-regexp s)
  24285. (setq s (replace-match "" t t s))))
  24286. (while (string-match org-bracket-link-regexp s)
  24287. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24288. t t s)))
  24289. s)
  24290. (defun org-html-expand (string)
  24291. "Prepare STRING for HTML export. Applies all active conversions.
  24292. If there are links in the string, don't modify these."
  24293. (let* ((re (concat org-bracket-link-regexp "\\|"
  24294. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24295. m s l res)
  24296. (while (setq m (string-match re string))
  24297. (setq s (substring string 0 m)
  24298. l (match-string 0 string)
  24299. string (substring string (match-end 0)))
  24300. (push (org-html-do-expand s) res)
  24301. (push l res))
  24302. (push (org-html-do-expand string) res)
  24303. (apply 'concat (nreverse res))))
  24304. (defun org-html-do-expand (s)
  24305. "Apply all active conversions to translate special ASCII to HTML."
  24306. (setq s (org-html-protect s))
  24307. (if org-export-html-expand
  24308. (let ((start 0))
  24309. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24310. (setq s (replace-match "<\\1>" t nil s)))))
  24311. (if org-export-with-emphasize
  24312. (setq s (org-export-html-convert-emphasize s)))
  24313. (if org-export-with-special-strings
  24314. (setq s (org-export-html-convert-special-strings s)))
  24315. (if org-export-with-sub-superscripts
  24316. (setq s (org-export-html-convert-sub-super s)))
  24317. (if org-export-with-TeX-macros
  24318. (let ((start 0) wd ass)
  24319. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24320. (if (get-text-property (match-beginning 0) 'org-protected s)
  24321. (setq start (match-end 0))
  24322. (setq wd (match-string 1 s))
  24323. (if (setq ass (assoc wd org-html-entities))
  24324. (setq s (replace-match (or (cdr ass)
  24325. (concat "&" (car ass) ";"))
  24326. t t s))
  24327. (setq start (+ start (length wd))))))))
  24328. s)
  24329. (defun org-create-multibrace-regexp (left right n)
  24330. "Create a regular expression which will match a balanced sexp.
  24331. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24332. as single character strings.
  24333. The regexp returned will match the entire expression including the
  24334. delimiters. It will also define a single group which contains the
  24335. match except for the outermost delimiters. The maximum depth of
  24336. stacked delimiters is N. Escaping delimiters is not possible."
  24337. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24338. (or "\\|")
  24339. (re nothing)
  24340. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24341. (while (> n 1)
  24342. (setq n (1- n)
  24343. re (concat re or next)
  24344. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24345. (concat left "\\(" re "\\)" right)))
  24346. (defvar org-match-substring-regexp
  24347. (concat
  24348. "\\([^\\]\\)\\([_^]\\)\\("
  24349. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24350. "\\|"
  24351. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24352. "\\|"
  24353. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24354. "The regular expression matching a sub- or superscript.")
  24355. (defvar org-match-substring-with-braces-regexp
  24356. (concat
  24357. "\\([^\\]\\)\\([_^]\\)\\("
  24358. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24359. "\\)")
  24360. "The regular expression matching a sub- or superscript, forcing braces.")
  24361. (defconst org-export-html-special-string-regexps
  24362. '(("\\\\-" . "&shy;")
  24363. ("---\\([^-]\\)" . "&mdash;\\1")
  24364. ("--\\([^-]\\)" . "&ndash;\\1")
  24365. ("\\.\\.\\." . "&hellip;"))
  24366. "Regular expressions for special string conversion.")
  24367. (defun org-export-html-convert-special-strings (string)
  24368. "Convert special characters in STRING to HTML."
  24369. (let ((all org-export-html-special-string-regexps)
  24370. e a re rpl start)
  24371. (while (setq a (pop all))
  24372. (setq re (car a) rpl (cdr a) start 0)
  24373. (while (string-match re string start)
  24374. (if (get-text-property (match-beginning 0) 'org-protected string)
  24375. (setq start (match-end 0))
  24376. (setq string (replace-match rpl t nil string)))))
  24377. string))
  24378. (defun org-export-html-convert-sub-super (string)
  24379. "Convert sub- and superscripts in STRING to HTML."
  24380. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24381. (while (string-match org-match-substring-regexp string s)
  24382. (cond
  24383. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24384. ((get-text-property (match-beginning 2) 'org-protected string)
  24385. (setq s (match-end 2)))
  24386. (t
  24387. (setq s (match-end 1)
  24388. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24389. c (or (match-string 8 string)
  24390. (match-string 6 string)
  24391. (match-string 5 string))
  24392. string (replace-match
  24393. (concat (match-string 1 string)
  24394. "<" key ">" c "</" key ">")
  24395. t t string)))))
  24396. (while (string-match "\\\\\\([_^]\\)" string)
  24397. (setq string (replace-match (match-string 1 string) t t string)))
  24398. string))
  24399. (defun org-export-html-convert-emphasize (string)
  24400. "Apply emphasis."
  24401. (let ((s 0) rpl)
  24402. (while (string-match org-emph-re string s)
  24403. (if (not (equal
  24404. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24405. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24406. (setq s (match-beginning 0)
  24407. rpl
  24408. (concat
  24409. (match-string 1 string)
  24410. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24411. (match-string 4 string)
  24412. (nth 3 (assoc (match-string 3 string)
  24413. org-emphasis-alist))
  24414. (match-string 5 string))
  24415. string (replace-match rpl t t string)
  24416. s (+ s (- (length rpl) 2)))
  24417. (setq s (1+ s))))
  24418. string))
  24419. (defvar org-par-open nil)
  24420. (defun org-open-par ()
  24421. "Insert <p>, but first close previous paragraph if any."
  24422. (org-close-par-maybe)
  24423. (insert "\n<p>")
  24424. (setq org-par-open t))
  24425. (defun org-close-par-maybe ()
  24426. "Close paragraph if there is one open."
  24427. (when org-par-open
  24428. (insert "</p>")
  24429. (setq org-par-open nil)))
  24430. (defun org-close-li ()
  24431. "Close <li> if necessary."
  24432. (org-close-par-maybe)
  24433. (insert "</li>\n"))
  24434. (defvar body-only) ; dynamically scoped into this.
  24435. (defun org-html-level-start (level title umax with-toc head-count)
  24436. "Insert a new level in HTML export.
  24437. When TITLE is nil, just close all open levels."
  24438. (org-close-par-maybe)
  24439. (let ((l org-level-max))
  24440. (while (>= l level)
  24441. (if (aref org-levels-open (1- l))
  24442. (progn
  24443. (org-html-level-close l umax)
  24444. (aset org-levels-open (1- l) nil)))
  24445. (setq l (1- l)))
  24446. (when title
  24447. ;; If title is nil, this means this function is called to close
  24448. ;; all levels, so the rest is done only if title is given
  24449. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24450. (setq title (replace-match
  24451. (if org-export-with-tags
  24452. (save-match-data
  24453. (concat
  24454. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24455. (mapconcat 'identity (org-split-string
  24456. (match-string 1 title) ":")
  24457. "&nbsp;")
  24458. "</span>"))
  24459. "")
  24460. t t title)))
  24461. (if (> level umax)
  24462. (progn
  24463. (if (aref org-levels-open (1- level))
  24464. (progn
  24465. (org-close-li)
  24466. (insert "<li>" title "<br/>\n"))
  24467. (aset org-levels-open (1- level) t)
  24468. (org-close-par-maybe)
  24469. (insert "<ul>\n<li>" title "<br/>\n")))
  24470. (aset org-levels-open (1- level) t)
  24471. (if (and org-export-with-section-numbers (not body-only))
  24472. (setq title (concat (org-section-number level) " " title)))
  24473. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24474. (if with-toc
  24475. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24476. level level head-count title level))
  24477. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24478. (org-open-par)))))
  24479. (defun org-html-level-close (level max-outline-level)
  24480. "Terminate one level in HTML export."
  24481. (if (<= level max-outline-level)
  24482. (insert "</div>\n")
  24483. (org-close-li)
  24484. (insert "</ul>\n")))
  24485. ;;; iCalendar export
  24486. ;;;###autoload
  24487. (defun org-export-icalendar-this-file ()
  24488. "Export current file as an iCalendar file.
  24489. The iCalendar file will be located in the same directory as the Org-mode
  24490. file, but with extension `.ics'."
  24491. (interactive)
  24492. (org-export-icalendar nil buffer-file-name))
  24493. ;;;###autoload
  24494. (defun org-export-icalendar-all-agenda-files ()
  24495. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24496. Each iCalendar file will be located in the same directory as the Org-mode
  24497. file, but with extension `.ics'."
  24498. (interactive)
  24499. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24500. ;;;###autoload
  24501. (defun org-export-icalendar-combine-agenda-files ()
  24502. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24503. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24504. (interactive)
  24505. (apply 'org-export-icalendar t (org-agenda-files t)))
  24506. (defun org-export-icalendar (combine &rest files)
  24507. "Create iCalendar files for all elements of FILES.
  24508. If COMBINE is non-nil, combine all calendar entries into a single large
  24509. file and store it under the name `org-combined-agenda-icalendar-file'."
  24510. (save-excursion
  24511. (org-prepare-agenda-buffers files)
  24512. (let* ((dir (org-export-directory
  24513. :ical (list :publishing-directory
  24514. org-export-publishing-directory)))
  24515. file ical-file ical-buffer category started org-agenda-new-buffers)
  24516. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24517. (when combine
  24518. (setq ical-file
  24519. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24520. org-combined-agenda-icalendar-file
  24521. (expand-file-name org-combined-agenda-icalendar-file dir))
  24522. ical-buffer (org-get-agenda-file-buffer ical-file))
  24523. (set-buffer ical-buffer) (erase-buffer))
  24524. (while (setq file (pop files))
  24525. (catch 'nextfile
  24526. (org-check-agenda-file file)
  24527. (set-buffer (org-get-agenda-file-buffer file))
  24528. (unless combine
  24529. (setq ical-file (concat (file-name-as-directory dir)
  24530. (file-name-sans-extension
  24531. (file-name-nondirectory buffer-file-name))
  24532. ".ics"))
  24533. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24534. (with-current-buffer ical-buffer (erase-buffer)))
  24535. (setq category (or org-category
  24536. (file-name-sans-extension
  24537. (file-name-nondirectory buffer-file-name))))
  24538. (if (symbolp category) (setq category (symbol-name category)))
  24539. (let ((standard-output ical-buffer))
  24540. (if combine
  24541. (and (not started) (setq started t)
  24542. (org-start-icalendar-file org-icalendar-combined-name))
  24543. (org-start-icalendar-file category))
  24544. (org-print-icalendar-entries combine)
  24545. (when (or (and combine (not files)) (not combine))
  24546. (org-finish-icalendar-file)
  24547. (set-buffer ical-buffer)
  24548. (save-buffer)
  24549. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24550. (org-release-buffers org-agenda-new-buffers))))
  24551. (defvar org-after-save-iCalendar-file-hook nil
  24552. "Hook run after an iCalendar file has been saved.
  24553. The iCalendar buffer is still current when this hook is run.
  24554. A good way to use this is to tell a desktop calenndar application to re-read
  24555. the iCalendar file.")
  24556. (defun org-print-icalendar-entries (&optional combine)
  24557. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24558. When COMBINE is non nil, add the category to each line."
  24559. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24560. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24561. (dts (org-ical-ts-to-string
  24562. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24563. "DTSTART"))
  24564. hd ts ts2 state status (inc t) pos b sexp rrule
  24565. scheduledp deadlinep tmp pri category entry location summary desc
  24566. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24567. (org-refresh-category-properties)
  24568. (save-excursion
  24569. (goto-char (point-min))
  24570. (while (re-search-forward re1 nil t)
  24571. (catch :skip
  24572. (org-agenda-skip)
  24573. (setq pos (match-beginning 0)
  24574. ts (match-string 0)
  24575. inc t
  24576. hd (org-get-heading)
  24577. summary (org-icalendar-cleanup-string
  24578. (org-entry-get nil "SUMMARY"))
  24579. desc (org-icalendar-cleanup-string
  24580. (or (org-entry-get nil "DESCRIPTION")
  24581. (and org-icalendar-include-body (org-get-entry)))
  24582. t org-icalendar-include-body)
  24583. location (org-icalendar-cleanup-string
  24584. (org-entry-get nil "LOCATION"))
  24585. category (org-get-category))
  24586. (if (looking-at re2)
  24587. (progn
  24588. (goto-char (match-end 0))
  24589. (setq ts2 (match-string 1) inc nil))
  24590. (setq tmp (buffer-substring (max (point-min)
  24591. (- pos org-ds-keyword-length))
  24592. pos)
  24593. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24594. (progn
  24595. (setq inc nil)
  24596. (replace-match "\\1" t nil ts))
  24597. ts)
  24598. deadlinep (string-match org-deadline-regexp tmp)
  24599. scheduledp (string-match org-scheduled-regexp tmp)
  24600. ;; donep (org-entry-is-done-p)
  24601. ))
  24602. (if (or (string-match org-tr-regexp hd)
  24603. (string-match org-ts-regexp hd))
  24604. (setq hd (replace-match "" t t hd)))
  24605. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24606. (setq rrule
  24607. (concat "\nRRULE:FREQ="
  24608. (cdr (assoc
  24609. (match-string 2 ts)
  24610. '(("d" . "DAILY")("w" . "WEEKLY")
  24611. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24612. ";INTERVAL=" (match-string 1 ts)))
  24613. (setq rrule ""))
  24614. (setq summary (or summary hd))
  24615. (if (string-match org-bracket-link-regexp summary)
  24616. (setq summary
  24617. (replace-match (if (match-end 3)
  24618. (match-string 3 summary)
  24619. (match-string 1 summary))
  24620. t t summary)))
  24621. (if deadlinep (setq summary (concat "DL: " summary)))
  24622. (if scheduledp (setq summary (concat "S: " summary)))
  24623. (if (string-match "\\`<%%" ts)
  24624. (with-current-buffer sexp-buffer
  24625. (insert (substring ts 1 -1) " " summary "\n"))
  24626. (princ (format "BEGIN:VEVENT
  24627. %s
  24628. %s%s
  24629. SUMMARY:%s%s%s
  24630. CATEGORIES:%s
  24631. END:VEVENT\n"
  24632. (org-ical-ts-to-string ts "DTSTART")
  24633. (org-ical-ts-to-string ts2 "DTEND" inc)
  24634. rrule summary
  24635. (if (and desc (string-match "\\S-" desc))
  24636. (concat "\nDESCRIPTION: " desc) "")
  24637. (if (and location (string-match "\\S-" location))
  24638. (concat "\nLOCATION: " location) "")
  24639. category)))))
  24640. (when (and org-icalendar-include-sexps
  24641. (condition-case nil (require 'icalendar) (error nil))
  24642. (fboundp 'icalendar-export-region))
  24643. ;; Get all the literal sexps
  24644. (goto-char (point-min))
  24645. (while (re-search-forward "^&?%%(" nil t)
  24646. (catch :skip
  24647. (org-agenda-skip)
  24648. (setq b (match-beginning 0))
  24649. (goto-char (1- (match-end 0)))
  24650. (forward-sexp 1)
  24651. (end-of-line 1)
  24652. (setq sexp (buffer-substring b (point)))
  24653. (with-current-buffer sexp-buffer
  24654. (insert sexp "\n"))
  24655. (princ (org-diary-to-ical-string sexp-buffer)))))
  24656. (when org-icalendar-include-todo
  24657. (goto-char (point-min))
  24658. (while (re-search-forward org-todo-line-regexp nil t)
  24659. (catch :skip
  24660. (org-agenda-skip)
  24661. (setq state (match-string 2))
  24662. (setq status (if (member state org-done-keywords)
  24663. "COMPLETED" "NEEDS-ACTION"))
  24664. (when (and state
  24665. (or (not (member state org-done-keywords))
  24666. (eq org-icalendar-include-todo 'all))
  24667. (not (member org-archive-tag (org-get-tags-at)))
  24668. )
  24669. (setq hd (match-string 3)
  24670. summary (org-icalendar-cleanup-string
  24671. (org-entry-get nil "SUMMARY"))
  24672. desc (org-icalendar-cleanup-string
  24673. (or (org-entry-get nil "DESCRIPTION")
  24674. (and org-icalendar-include-body (org-get-entry)))
  24675. t org-icalendar-include-body)
  24676. location (org-icalendar-cleanup-string
  24677. (org-entry-get nil "LOCATION")))
  24678. (if (string-match org-bracket-link-regexp hd)
  24679. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24680. (match-string 1 hd))
  24681. t t hd)))
  24682. (if (string-match org-priority-regexp hd)
  24683. (setq pri (string-to-char (match-string 2 hd))
  24684. hd (concat (substring hd 0 (match-beginning 1))
  24685. (substring hd (match-end 1))))
  24686. (setq pri org-default-priority))
  24687. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24688. (- org-lowest-priority org-highest-priority))))))
  24689. (princ (format "BEGIN:VTODO
  24690. %s
  24691. SUMMARY:%s%s%s
  24692. CATEGORIES:%s
  24693. SEQUENCE:1
  24694. PRIORITY:%d
  24695. STATUS:%s
  24696. END:VTODO\n"
  24697. dts
  24698. (or summary hd)
  24699. (if (and location (string-match "\\S-" location))
  24700. (concat "\nLOCATION: " location) "")
  24701. (if (and desc (string-match "\\S-" desc))
  24702. (concat "\nDESCRIPTION: " desc) "")
  24703. category pri status)))))))))
  24704. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24705. "Take out stuff and quote what needs to be quoted.
  24706. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24707. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24708. characters."
  24709. (if (not s)
  24710. nil
  24711. (when is-body
  24712. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24713. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24714. (while (string-match re s) (setq s (replace-match "" t t s)))
  24715. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24716. (let ((start 0))
  24717. (while (string-match "\\([,;\\]\\)" s start)
  24718. (setq start (+ (match-beginning 0) 2)
  24719. s (replace-match "\\\\\\1" nil nil s))))
  24720. (when is-body
  24721. (while (string-match "[ \t]*\n[ \t]*" s)
  24722. (setq s (replace-match "\\n" t t s))))
  24723. (setq s (org-trim s))
  24724. (if is-body
  24725. (if maxlength
  24726. (if (and (numberp maxlength)
  24727. (> (length s) maxlength))
  24728. (setq s (substring s 0 maxlength)))))
  24729. s))
  24730. (defun org-get-entry ()
  24731. "Clean-up description string."
  24732. (save-excursion
  24733. (org-back-to-heading t)
  24734. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24735. (defun org-start-icalendar-file (name)
  24736. "Start an iCalendar file by inserting the header."
  24737. (let ((user user-full-name)
  24738. (name (or name "unknown"))
  24739. (timezone (cadr (current-time-zone))))
  24740. (princ
  24741. (format "BEGIN:VCALENDAR
  24742. VERSION:2.0
  24743. X-WR-CALNAME:%s
  24744. PRODID:-//%s//Emacs with Org-mode//EN
  24745. X-WR-TIMEZONE:%s
  24746. CALSCALE:GREGORIAN\n" name user timezone))))
  24747. (defun org-finish-icalendar-file ()
  24748. "Finish an iCalendar file by inserting the END statement."
  24749. (princ "END:VCALENDAR\n"))
  24750. (defun org-ical-ts-to-string (s keyword &optional inc)
  24751. "Take a time string S and convert it to iCalendar format.
  24752. KEYWORD is added in front, to make a complete line like DTSTART....
  24753. When INC is non-nil, increase the hour by two (if time string contains
  24754. a time), or the day by one (if it does not contain a time)."
  24755. (let ((t1 (org-parse-time-string s 'nodefault))
  24756. t2 fmt have-time time)
  24757. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24758. (setq t2 t1 have-time t)
  24759. (setq t2 (org-parse-time-string s)))
  24760. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24761. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24762. (when inc
  24763. (if have-time
  24764. (if org-agenda-default-appointment-duration
  24765. (setq mi (+ org-agenda-default-appointment-duration mi))
  24766. (setq h (+ 2 h)))
  24767. (setq d (1+ d))))
  24768. (setq time (encode-time s mi h d m y)))
  24769. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24770. (concat keyword (format-time-string fmt time))))
  24771. ;;; XOXO export
  24772. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24773. (with-current-buffer buffer
  24774. (apply 'insert output)))
  24775. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24776. (defun org-export-as-xoxo (&optional buffer)
  24777. "Export the org buffer as XOXO.
  24778. The XOXO buffer is named *xoxo-<source buffer name>*"
  24779. (interactive (list (current-buffer)))
  24780. ;; A quickie abstraction
  24781. ;; Output everything as XOXO
  24782. (with-current-buffer (get-buffer buffer)
  24783. (let* ((pos (point))
  24784. (opt-plist (org-combine-plists (org-default-export-plist)
  24785. (org-infile-export-plist)))
  24786. (filename (concat (file-name-as-directory
  24787. (org-export-directory :xoxo opt-plist))
  24788. (file-name-sans-extension
  24789. (file-name-nondirectory buffer-file-name))
  24790. ".html"))
  24791. (out (find-file-noselect filename))
  24792. (last-level 1)
  24793. (hanging-li nil))
  24794. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24795. ;; Check the output buffer is empty.
  24796. (with-current-buffer out (erase-buffer))
  24797. ;; Kick off the output
  24798. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24799. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24800. (let* ((hd (match-string-no-properties 1))
  24801. (level (length hd))
  24802. (text (concat
  24803. (match-string-no-properties 2)
  24804. (save-excursion
  24805. (goto-char (match-end 0))
  24806. (let ((str ""))
  24807. (catch 'loop
  24808. (while 't
  24809. (forward-line)
  24810. (if (looking-at "^[ \t]\\(.*\\)")
  24811. (setq str (concat str (match-string-no-properties 1)))
  24812. (throw 'loop str)))))))))
  24813. ;; Handle level rendering
  24814. (cond
  24815. ((> level last-level)
  24816. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24817. ((< level last-level)
  24818. (dotimes (- (- last-level level) 1)
  24819. (if hanging-li
  24820. (org-export-as-xoxo-insert-into out "</li>\n"))
  24821. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24822. (when hanging-li
  24823. (org-export-as-xoxo-insert-into out "</li>\n")
  24824. (setq hanging-li nil)))
  24825. ((equal level last-level)
  24826. (if hanging-li
  24827. (org-export-as-xoxo-insert-into out "</li>\n")))
  24828. )
  24829. (setq last-level level)
  24830. ;; And output the new li
  24831. (setq hanging-li 't)
  24832. (if (equal ?+ (elt text 0))
  24833. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24834. (org-export-as-xoxo-insert-into out "<li>" text))))
  24835. ;; Finally finish off the ol
  24836. (dotimes (- last-level 1)
  24837. (if hanging-li
  24838. (org-export-as-xoxo-insert-into out "</li>\n"))
  24839. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24840. (goto-char pos)
  24841. ;; Finish the buffer off and clean it up.
  24842. (switch-to-buffer-other-window out)
  24843. (indent-region (point-min) (point-max) nil)
  24844. (save-buffer)
  24845. (goto-char (point-min))
  24846. )))
  24847. ;;;; Key bindings
  24848. ;; Make `C-c C-x' a prefix key
  24849. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24850. ;; TAB key with modifiers
  24851. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24852. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24853. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24854. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24855. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24856. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24857. ;; The following line is necessary under Suse GNU/Linux
  24858. (unless (featurep 'xemacs)
  24859. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24860. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24861. (define-key org-mode-map [backtab] 'org-shifttab)
  24862. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24863. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24864. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24865. ;; Cursor keys with modifiers
  24866. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24867. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24868. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24869. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24870. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24871. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24872. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24873. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24874. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24875. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24876. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24877. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24878. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24879. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24880. ;;; Extra keys for tty access.
  24881. ;; We only set them when really needed because otherwise the
  24882. ;; menus don't show the simple keys
  24883. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24884. (not window-system))
  24885. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24886. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24887. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24888. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24889. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24890. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24891. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24892. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24893. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24894. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24895. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24896. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24897. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24898. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24899. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24900. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24901. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24902. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24903. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24904. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24905. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24906. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24907. ;; All the other keys
  24908. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24909. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24910. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24911. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24912. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24913. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24914. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24915. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24916. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24917. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24918. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24919. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24920. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24921. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24922. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24923. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24924. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24925. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24926. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24927. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24928. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24929. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24930. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24931. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24932. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24933. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24934. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24935. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24936. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24937. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24938. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24939. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24940. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24941. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24942. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24943. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24944. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24945. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24946. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24947. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  24948. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24949. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24950. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24951. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24952. (org-defkey org-mode-map "\C-m" 'org-return)
  24953. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24954. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24955. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24956. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24957. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24958. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24959. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24960. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24961. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24962. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24963. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24964. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24965. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24966. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24967. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24968. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24969. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24970. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24971. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24972. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24973. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24974. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24975. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24976. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24977. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24978. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24979. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24980. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24981. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24982. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24983. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24984. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24985. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24986. (when (featurep 'xemacs)
  24987. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24988. (defsubst org-table-p () (org-at-table-p))
  24989. (defun org-self-insert-command (N)
  24990. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24991. If the cursor is in a table looking at whitespace, the whitespace is
  24992. overwritten, and the table is not marked as requiring realignment."
  24993. (interactive "p")
  24994. (if (and (org-table-p)
  24995. (progn
  24996. ;; check if we blank the field, and if that triggers align
  24997. (and org-table-auto-blank-field
  24998. (member last-command
  24999. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  25000. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  25001. ;; got extra space, this field does not determine column width
  25002. (let (org-table-may-need-update) (org-table-blank-field))
  25003. ;; no extra space, this field may determine column width
  25004. (org-table-blank-field)))
  25005. t)
  25006. (eq N 1)
  25007. (looking-at "[^|\n]* |"))
  25008. (let (org-table-may-need-update)
  25009. (goto-char (1- (match-end 0)))
  25010. (delete-backward-char 1)
  25011. (goto-char (match-beginning 0))
  25012. (self-insert-command N))
  25013. (setq org-table-may-need-update t)
  25014. (self-insert-command N)
  25015. (org-fix-tags-on-the-fly)))
  25016. (defun org-fix-tags-on-the-fly ()
  25017. (when (and (equal (char-after (point-at-bol)) ?*)
  25018. (org-on-heading-p))
  25019. (org-align-tags-here org-tags-column)))
  25020. (defun org-delete-backward-char (N)
  25021. "Like `delete-backward-char', insert whitespace at field end in tables.
  25022. When deleting backwards, in tables this function will insert whitespace in
  25023. front of the next \"|\" separator, to keep the table aligned. The table will
  25024. still be marked for re-alignment if the field did fill the entire column,
  25025. because, in this case the deletion might narrow the column."
  25026. (interactive "p")
  25027. (if (and (org-table-p)
  25028. (eq N 1)
  25029. (string-match "|" (buffer-substring (point-at-bol) (point)))
  25030. (looking-at ".*?|"))
  25031. (let ((pos (point))
  25032. (noalign (looking-at "[^|\n\r]* |"))
  25033. (c org-table-may-need-update))
  25034. (backward-delete-char N)
  25035. (skip-chars-forward "^|")
  25036. (insert " ")
  25037. (goto-char (1- pos))
  25038. ;; noalign: if there were two spaces at the end, this field
  25039. ;; does not determine the width of the column.
  25040. (if noalign (setq org-table-may-need-update c)))
  25041. (backward-delete-char N)
  25042. (org-fix-tags-on-the-fly)))
  25043. (defun org-delete-char (N)
  25044. "Like `delete-char', but insert whitespace at field end in tables.
  25045. When deleting characters, in tables this function will insert whitespace in
  25046. front of the next \"|\" separator, to keep the table aligned. The table will
  25047. still be marked for re-alignment if the field did fill the entire column,
  25048. because, in this case the deletion might narrow the column."
  25049. (interactive "p")
  25050. (if (and (org-table-p)
  25051. (not (bolp))
  25052. (not (= (char-after) ?|))
  25053. (eq N 1))
  25054. (if (looking-at ".*?|")
  25055. (let ((pos (point))
  25056. (noalign (looking-at "[^|\n\r]* |"))
  25057. (c org-table-may-need-update))
  25058. (replace-match (concat
  25059. (substring (match-string 0) 1 -1)
  25060. " |"))
  25061. (goto-char pos)
  25062. ;; noalign: if there were two spaces at the end, this field
  25063. ;; does not determine the width of the column.
  25064. (if noalign (setq org-table-may-need-update c)))
  25065. (delete-char N))
  25066. (delete-char N)
  25067. (org-fix-tags-on-the-fly)))
  25068. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  25069. (put 'org-self-insert-command 'delete-selection t)
  25070. (put 'orgtbl-self-insert-command 'delete-selection t)
  25071. (put 'org-delete-char 'delete-selection 'supersede)
  25072. (put 'org-delete-backward-char 'delete-selection 'supersede)
  25073. ;; Make `flyspell-mode' delay after some commands
  25074. (put 'org-self-insert-command 'flyspell-delayed t)
  25075. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  25076. (put 'org-delete-char 'flyspell-delayed t)
  25077. (put 'org-delete-backward-char 'flyspell-delayed t)
  25078. ;; Make pabbrev-mode expand after org-mode commands
  25079. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  25080. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  25081. ;; How to do this: Measure non-white length of current string
  25082. ;; If equal to column width, we should realign.
  25083. (defun org-remap (map &rest commands)
  25084. "In MAP, remap the functions given in COMMANDS.
  25085. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  25086. (let (new old)
  25087. (while commands
  25088. (setq old (pop commands) new (pop commands))
  25089. (if (fboundp 'command-remapping)
  25090. (org-defkey map (vector 'remap old) new)
  25091. (substitute-key-definition old new map global-map)))))
  25092. (when (eq org-enable-table-editor 'optimized)
  25093. ;; If the user wants maximum table support, we need to hijack
  25094. ;; some standard editing functions
  25095. (org-remap org-mode-map
  25096. 'self-insert-command 'org-self-insert-command
  25097. 'delete-char 'org-delete-char
  25098. 'delete-backward-char 'org-delete-backward-char)
  25099. (org-defkey org-mode-map "|" 'org-force-self-insert))
  25100. (defun org-shiftcursor-error ()
  25101. "Throw an error because Shift-Cursor command was applied in wrong context."
  25102. (error "This command is active in special context like tables, headlines or timestamps"))
  25103. (defun org-shifttab (&optional arg)
  25104. "Global visibility cycling or move to previous table field.
  25105. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  25106. on context.
  25107. See the individual commands for more information."
  25108. (interactive "P")
  25109. (cond
  25110. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  25111. (arg (message "Content view to level: ")
  25112. (org-content (prefix-numeric-value arg))
  25113. (setq org-cycle-global-status 'overview))
  25114. (t (call-interactively 'org-global-cycle))))
  25115. (defun org-shiftmetaleft ()
  25116. "Promote subtree or delete table column.
  25117. Calls `org-promote-subtree', `org-outdent-item',
  25118. or `org-table-delete-column', depending on context.
  25119. See the individual commands for more information."
  25120. (interactive)
  25121. (cond
  25122. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  25123. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  25124. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25125. (t (org-shiftcursor-error))))
  25126. (defun org-shiftmetaright ()
  25127. "Demote subtree or insert table column.
  25128. Calls `org-demote-subtree', `org-indent-item',
  25129. or `org-table-insert-column', depending on context.
  25130. See the individual commands for more information."
  25131. (interactive)
  25132. (cond
  25133. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  25134. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  25135. ((org-at-item-p) (call-interactively 'org-indent-item))
  25136. (t (org-shiftcursor-error))))
  25137. (defun org-shiftmetaup (&optional arg)
  25138. "Move subtree up or kill table row.
  25139. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25140. `org-move-item-up' depending on context. See the individual commands
  25141. for more information."
  25142. (interactive "P")
  25143. (cond
  25144. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25145. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25146. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25147. (t (org-shiftcursor-error))))
  25148. (defun org-shiftmetadown (&optional arg)
  25149. "Move subtree down or insert table row.
  25150. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25151. `org-move-item-down', depending on context. See the individual
  25152. commands for more information."
  25153. (interactive "P")
  25154. (cond
  25155. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25156. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25157. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25158. (t (org-shiftcursor-error))))
  25159. (defun org-metaleft (&optional arg)
  25160. "Promote heading or move table column to left.
  25161. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25162. With no specific context, calls the Emacs default `backward-word'.
  25163. See the individual commands for more information."
  25164. (interactive "P")
  25165. (cond
  25166. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25167. ((or (org-on-heading-p) (org-region-active-p))
  25168. (call-interactively 'org-do-promote))
  25169. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25170. (t (call-interactively 'backward-word))))
  25171. (defun org-metaright (&optional arg)
  25172. "Demote subtree or move table column to right.
  25173. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25174. With no specific context, calls the Emacs default `forward-word'.
  25175. See the individual commands for more information."
  25176. (interactive "P")
  25177. (cond
  25178. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25179. ((or (org-on-heading-p) (org-region-active-p))
  25180. (call-interactively 'org-do-demote))
  25181. ((org-at-item-p) (call-interactively 'org-indent-item))
  25182. (t (call-interactively 'forward-word))))
  25183. (defun org-metaup (&optional arg)
  25184. "Move subtree up or move table row up.
  25185. Calls `org-move-subtree-up' or `org-table-move-row' or
  25186. `org-move-item-up', depending on context. See the individual commands
  25187. for more information."
  25188. (interactive "P")
  25189. (cond
  25190. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25191. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25192. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25193. (t (transpose-lines 1) (beginning-of-line -1))))
  25194. (defun org-metadown (&optional arg)
  25195. "Move subtree down or move table row down.
  25196. Calls `org-move-subtree-down' or `org-table-move-row' or
  25197. `org-move-item-down', depending on context. See the individual
  25198. commands for more information."
  25199. (interactive "P")
  25200. (cond
  25201. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25202. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25203. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25204. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25205. (defun org-shiftup (&optional arg)
  25206. "Increase item in timestamp or increase priority of current headline.
  25207. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25208. depending on context. See the individual commands for more information."
  25209. (interactive "P")
  25210. (cond
  25211. ((org-at-timestamp-p t)
  25212. (call-interactively (if org-edit-timestamp-down-means-later
  25213. 'org-timestamp-down 'org-timestamp-up)))
  25214. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25215. ((org-at-item-p) (call-interactively 'org-previous-item))
  25216. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25217. (defun org-shiftdown (&optional arg)
  25218. "Decrease item in timestamp or decrease priority of current headline.
  25219. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25220. depending on context. See the individual commands for more information."
  25221. (interactive "P")
  25222. (cond
  25223. ((org-at-timestamp-p t)
  25224. (call-interactively (if org-edit-timestamp-down-means-later
  25225. 'org-timestamp-up 'org-timestamp-down)))
  25226. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25227. (t (call-interactively 'org-next-item))))
  25228. (defun org-shiftright ()
  25229. "Next TODO keyword or timestamp one day later, depending on context."
  25230. (interactive)
  25231. (cond
  25232. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25233. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25234. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25235. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25236. (t (org-shiftcursor-error))))
  25237. (defun org-shiftleft ()
  25238. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25239. (interactive)
  25240. (cond
  25241. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25242. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25243. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25244. ((org-at-property-p)
  25245. (call-interactively 'org-property-previous-allowed-value))
  25246. (t (org-shiftcursor-error))))
  25247. (defun org-shiftcontrolright ()
  25248. "Switch to next TODO set."
  25249. (interactive)
  25250. (cond
  25251. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25252. (t (org-shiftcursor-error))))
  25253. (defun org-shiftcontrolleft ()
  25254. "Switch to previous TODO set."
  25255. (interactive)
  25256. (cond
  25257. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25258. (t (org-shiftcursor-error))))
  25259. (defun org-ctrl-c-ret ()
  25260. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25261. (interactive)
  25262. (cond
  25263. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25264. (t (call-interactively 'org-insert-heading))))
  25265. (defun org-copy-special ()
  25266. "Copy region in table or copy current subtree.
  25267. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25268. See the individual commands for more information."
  25269. (interactive)
  25270. (call-interactively
  25271. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25272. (defun org-cut-special ()
  25273. "Cut region in table or cut current subtree.
  25274. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25275. See the individual commands for more information."
  25276. (interactive)
  25277. (call-interactively
  25278. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25279. (defun org-paste-special (arg)
  25280. "Paste rectangular region into table, or past subtree relative to level.
  25281. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25282. See the individual commands for more information."
  25283. (interactive "P")
  25284. (if (org-at-table-p)
  25285. (org-table-paste-rectangle)
  25286. (org-paste-subtree arg)))
  25287. (defun org-ctrl-c-ctrl-c (&optional arg)
  25288. "Set tags in headline, or update according to changed information at point.
  25289. This command does many different things, depending on context:
  25290. - If the cursor is in a headline, prompt for tags and insert them
  25291. into the current line, aligned to `org-tags-column'. When called
  25292. with prefix arg, realign all tags in the current buffer.
  25293. - If the cursor is in one of the special #+KEYWORD lines, this
  25294. triggers scanning the buffer for these lines and updating the
  25295. information.
  25296. - If the cursor is inside a table, realign the table. This command
  25297. works even if the automatic table editor has been turned off.
  25298. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25299. the entire table.
  25300. - If the cursor is a the beginning of a dynamic block, update it.
  25301. - If the cursor is inside a table created by the table.el package,
  25302. activate that table.
  25303. - If the current buffer is a remember buffer, close note and file it.
  25304. with a prefix argument, file it without further interaction to the default
  25305. location.
  25306. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25307. links in this buffer.
  25308. - If the cursor is on a numbered item in a plain list, renumber the
  25309. ordered list.
  25310. - If the cursor is on a checkbox, toggle it."
  25311. (interactive "P")
  25312. (let ((org-enable-table-editor t))
  25313. (cond
  25314. ((or org-clock-overlays
  25315. org-occur-highlights
  25316. org-latex-fragment-image-overlays)
  25317. (org-remove-clock-overlays)
  25318. (org-remove-occur-highlights)
  25319. (org-remove-latex-fragment-image-overlays)
  25320. (message "Temporary highlights/overlays removed from current buffer"))
  25321. ((and (local-variable-p 'org-finish-function (current-buffer))
  25322. (fboundp org-finish-function))
  25323. (funcall org-finish-function))
  25324. ((org-at-property-p)
  25325. (call-interactively 'org-property-action))
  25326. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25327. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25328. ((org-at-table.el-p)
  25329. (require 'table)
  25330. (beginning-of-line 1)
  25331. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25332. (call-interactively 'table-recognize-table))
  25333. ((org-at-table-p)
  25334. (org-table-maybe-eval-formula)
  25335. (if arg
  25336. (call-interactively 'org-table-recalculate)
  25337. (org-table-maybe-recalculate-line))
  25338. (call-interactively 'org-table-align))
  25339. ((org-at-item-checkbox-p)
  25340. (call-interactively 'org-toggle-checkbox))
  25341. ((org-at-item-p)
  25342. (call-interactively 'org-maybe-renumber-ordered-list))
  25343. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25344. ;; Dynamic block
  25345. (beginning-of-line 1)
  25346. (org-update-dblock))
  25347. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25348. (cond
  25349. ((equal (match-string 1) "TBLFM")
  25350. ;; Recalculate the table before this line
  25351. (save-excursion
  25352. (beginning-of-line 1)
  25353. (skip-chars-backward " \r\n\t")
  25354. (if (org-at-table-p)
  25355. (org-call-with-arg 'org-table-recalculate t))))
  25356. (t
  25357. (call-interactively 'org-mode-restart))))
  25358. (t (error "C-c C-c can do nothing useful at this location.")))))
  25359. (defun org-mode-restart ()
  25360. "Restart Org-mode, to scan again for special lines.
  25361. Also updates the keyword regular expressions."
  25362. (interactive)
  25363. (let ((org-inhibit-startup t)) (org-mode))
  25364. (message "Org-mode restarted to refresh keyword and special line setup"))
  25365. (defun org-kill-note-or-show-branches ()
  25366. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25367. (interactive)
  25368. (if (not org-finish-function)
  25369. (call-interactively 'show-branches)
  25370. (let ((org-note-abort t))
  25371. (funcall org-finish-function))))
  25372. (defun org-return (&optional indent)
  25373. "Goto next table row or insert a newline.
  25374. Calls `org-table-next-row' or `newline', depending on context.
  25375. See the individual commands for more information."
  25376. (interactive)
  25377. (cond
  25378. ((bobp) (if indent (newline-and-indent) (newline)))
  25379. ((and (org-at-heading-p)
  25380. (looking-at
  25381. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25382. (org-show-entry)
  25383. (end-of-line 1)
  25384. (newline))
  25385. ((org-at-table-p)
  25386. (org-table-justify-field-maybe)
  25387. (call-interactively 'org-table-next-row))
  25388. (t (if indent (newline-and-indent) (newline)))))
  25389. (defun org-return-indent ()
  25390. "Goto next table row or insert a newline and indent.
  25391. Calls `org-table-next-row' or `newline-and-indent', depending on
  25392. context. See the individual commands for more information."
  25393. (interactive)
  25394. (org-return t))
  25395. (defun org-ctrl-c-star ()
  25396. "Compute table, or change heading status of lines.
  25397. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  25398. depending on context. This will also turn a plain list item or a normal
  25399. line into a subheading."
  25400. (interactive)
  25401. (cond
  25402. ((org-at-table-p)
  25403. (call-interactively 'org-table-recalculate))
  25404. ((org-region-active-p)
  25405. ;; Convert all lines in region to list items
  25406. (call-interactively 'org-toggle-region-headings))
  25407. ((org-on-heading-p)
  25408. (org-toggle-region-headings (point-at-bol)
  25409. (min (1+ (point-at-eol)) (point-max))))
  25410. ((org-at-item-p)
  25411. ;; Convert to heading
  25412. ;; FIXME: not yet implemented
  25413. )
  25414. (t (org-toggle-region-headings (point-at-bol)
  25415. (min (1+ (point-at-eol)) (point-max))))))
  25416. (defun org-ctrl-c-minus ()
  25417. "Insert separator line in table or modify bullet status of line.
  25418. Also turns a plain line or a region of lines into list items.
  25419. Calls `org-table-insert-hline', `org-toggle-region-items', or
  25420. `org-cycle-list-bullet', depending on context."
  25421. (interactive)
  25422. (cond
  25423. ((org-at-table-p)
  25424. (call-interactively 'org-table-insert-hline))
  25425. ((org-on-heading-p)
  25426. ;; Convert to item
  25427. (save-excursion
  25428. (beginning-of-line 1)
  25429. (if (looking-at "\\*+ ")
  25430. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25431. ((org-region-active-p)
  25432. ;; Convert all lines in region to list items
  25433. (call-interactively 'org-toggle-region-items))
  25434. ((org-in-item-p)
  25435. (call-interactively 'org-cycle-list-bullet))
  25436. (t (org-toggle-region-items (point-at-bol)
  25437. (min (1+ (point-at-eol)) (point-max))))))
  25438. (defun org-toggle-region-items (beg end)
  25439. "Convert all lines in region to list items.
  25440. If the first line is already an item, convert all list items in the region
  25441. to normal lines."
  25442. (interactive "r")
  25443. (let (l2 l)
  25444. (save-excursion
  25445. (goto-char end)
  25446. (setq l2 (org-current-line))
  25447. (goto-char beg)
  25448. (beginning-of-line 1)
  25449. (setq l (1- (org-current-line)))
  25450. (if (org-at-item-p)
  25451. ;; We already have items, de-itemize
  25452. (while (< (setq l (1+ l)) l2)
  25453. (when (org-at-item-p)
  25454. (goto-char (match-beginning 2))
  25455. (delete-region (match-beginning 2) (match-end 2))
  25456. (and (looking-at "[ \t]+") (replace-match "")))
  25457. (beginning-of-line 2))
  25458. (while (< (setq l (1+ l)) l2)
  25459. (unless (org-at-item-p)
  25460. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25461. (replace-match "\\1- \\2")))
  25462. (beginning-of-line 2))))))
  25463. (defun org-toggle-region-headings (beg end)
  25464. "Convert all lines in region to list items.
  25465. If the first line is already an item, convert all list items in the region
  25466. to normal lines."
  25467. (interactive "r")
  25468. (let (l2 l)
  25469. (save-excursion
  25470. (goto-char end)
  25471. (setq l2 (org-current-line))
  25472. (goto-char beg)
  25473. (beginning-of-line 1)
  25474. (setq l (1- (org-current-line)))
  25475. (if (org-on-heading-p)
  25476. ;; We already have headlines, de-star them
  25477. (while (< (setq l (1+ l)) l2)
  25478. (when (org-on-heading-p t)
  25479. (and (looking-at outline-regexp) (replace-match "")))
  25480. (beginning-of-line 2))
  25481. (let* ((stars (save-excursion
  25482. (re-search-backward org-complex-heading-regexp nil t)
  25483. (or (match-string 1) "*")))
  25484. (add-stars (if org-odd-levels-only "**" "*"))
  25485. (rpl (concat stars add-stars " \\2")))
  25486. (while (< (setq l (1+ l)) l2)
  25487. (unless (org-on-heading-p)
  25488. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25489. (replace-match rpl)))
  25490. (beginning-of-line 2)))))))
  25491. (defun org-meta-return (&optional arg)
  25492. "Insert a new heading or wrap a region in a table.
  25493. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25494. See the individual commands for more information."
  25495. (interactive "P")
  25496. (cond
  25497. ((org-at-table-p)
  25498. (call-interactively 'org-table-wrap-region))
  25499. (t (call-interactively 'org-insert-heading))))
  25500. ;;; Menu entries
  25501. ;; Define the Org-mode menus
  25502. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25503. '("Tbl"
  25504. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25505. ["Next Field" org-cycle (org-at-table-p)]
  25506. ["Previous Field" org-shifttab (org-at-table-p)]
  25507. ["Next Row" org-return (org-at-table-p)]
  25508. "--"
  25509. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25510. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25511. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25512. "--"
  25513. ("Column"
  25514. ["Move Column Left" org-metaleft (org-at-table-p)]
  25515. ["Move Column Right" org-metaright (org-at-table-p)]
  25516. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25517. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25518. ("Row"
  25519. ["Move Row Up" org-metaup (org-at-table-p)]
  25520. ["Move Row Down" org-metadown (org-at-table-p)]
  25521. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25522. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25523. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25524. "--"
  25525. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25526. ("Rectangle"
  25527. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25528. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25529. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25530. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25531. "--"
  25532. ("Calculate"
  25533. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25534. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25535. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25536. "--"
  25537. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25538. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25539. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25540. "--"
  25541. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25542. "--"
  25543. ["Sum Column/Rectangle" org-table-sum
  25544. (or (org-at-table-p) (org-region-active-p))]
  25545. ["Which Column?" org-table-current-column (org-at-table-p)])
  25546. ["Debug Formulas"
  25547. org-table-toggle-formula-debugger
  25548. :style toggle :selected org-table-formula-debug]
  25549. ["Show Col/Row Numbers"
  25550. org-table-toggle-coordinate-overlays
  25551. :style toggle :selected org-table-overlay-coordinates]
  25552. "--"
  25553. ["Create" org-table-create (and (not (org-at-table-p))
  25554. org-enable-table-editor)]
  25555. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25556. ["Import from File" org-table-import (not (org-at-table-p))]
  25557. ["Export to File" org-table-export (org-at-table-p)]
  25558. "--"
  25559. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25560. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25561. '("Org"
  25562. ("Show/Hide"
  25563. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25564. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25565. ["Sparse Tree" org-occur t]
  25566. ["Reveal Context" org-reveal t]
  25567. ["Show All" show-all t]
  25568. "--"
  25569. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25570. "--"
  25571. ["New Heading" org-insert-heading t]
  25572. ("Navigate Headings"
  25573. ["Up" outline-up-heading t]
  25574. ["Next" outline-next-visible-heading t]
  25575. ["Previous" outline-previous-visible-heading t]
  25576. ["Next Same Level" outline-forward-same-level t]
  25577. ["Previous Same Level" outline-backward-same-level t]
  25578. "--"
  25579. ["Jump" org-goto t])
  25580. ("Edit Structure"
  25581. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25582. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25583. "--"
  25584. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25585. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25586. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25587. "--"
  25588. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25589. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25590. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25591. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25592. "--"
  25593. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25594. "--"
  25595. ["Convert to odd levels" org-convert-to-odd-levels t]
  25596. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25597. ("Editing"
  25598. ["Emphasis..." org-emphasize t])
  25599. ("Archive"
  25600. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25601. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25602. ; :active t :keys "C-u C-c C-x C-a"]
  25603. ["Sparse trees open ARCHIVE trees"
  25604. (setq org-sparse-tree-open-archived-trees
  25605. (not org-sparse-tree-open-archived-trees))
  25606. :style toggle :selected org-sparse-tree-open-archived-trees]
  25607. ["Cycling opens ARCHIVE trees"
  25608. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25609. :style toggle :selected org-cycle-open-archived-trees]
  25610. ["Agenda includes ARCHIVE trees"
  25611. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25612. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25613. "--"
  25614. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25615. ; ["Check and Move Children" (org-archive-subtree '(4))
  25616. ; :active t :keys "C-u C-c C-x C-s"]
  25617. )
  25618. "--"
  25619. ("TODO Lists"
  25620. ["TODO/DONE/-" org-todo t]
  25621. ("Select keyword"
  25622. ["Next keyword" org-shiftright (org-on-heading-p)]
  25623. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25624. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25625. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25626. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25627. ["Show TODO Tree" org-show-todo-tree t]
  25628. ["Global TODO list" org-todo-list t]
  25629. "--"
  25630. ["Set Priority" org-priority t]
  25631. ["Priority Up" org-shiftup t]
  25632. ["Priority Down" org-shiftdown t])
  25633. ("TAGS and Properties"
  25634. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25635. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25636. "--"
  25637. ["Set property" 'org-set-property t]
  25638. ["Column view of properties" org-columns t]
  25639. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25640. ("Dates and Scheduling"
  25641. ["Timestamp" org-time-stamp t]
  25642. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25643. ("Change Date"
  25644. ["1 Day Later" org-shiftright t]
  25645. ["1 Day Earlier" org-shiftleft t]
  25646. ["1 ... Later" org-shiftup t]
  25647. ["1 ... Earlier" org-shiftdown t])
  25648. ["Compute Time Range" org-evaluate-time-range t]
  25649. ["Schedule Item" org-schedule t]
  25650. ["Deadline" org-deadline t]
  25651. "--"
  25652. ["Custom time format" org-toggle-time-stamp-overlays
  25653. :style radio :selected org-display-custom-times]
  25654. "--"
  25655. ["Goto Calendar" org-goto-calendar t]
  25656. ["Date from Calendar" org-date-from-calendar t])
  25657. ("Logging work"
  25658. ["Clock in" org-clock-in t]
  25659. ["Clock out" org-clock-out t]
  25660. ["Clock cancel" org-clock-cancel t]
  25661. ["Goto running clock" org-clock-goto t]
  25662. ["Display times" org-clock-display t]
  25663. ["Create clock table" org-clock-report t]
  25664. "--"
  25665. ["Record DONE time"
  25666. (progn (setq org-log-done (not org-log-done))
  25667. (message "Switching to %s will %s record a timestamp"
  25668. (car org-done-keywords)
  25669. (if org-log-done "automatically" "not")))
  25670. :style toggle :selected org-log-done])
  25671. "--"
  25672. ["Agenda Command..." org-agenda t]
  25673. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25674. ("File List for Agenda")
  25675. ("Special views current file"
  25676. ["TODO Tree" org-show-todo-tree t]
  25677. ["Check Deadlines" org-check-deadlines t]
  25678. ["Timeline" org-timeline t]
  25679. ["Tags Tree" org-tags-sparse-tree t])
  25680. "--"
  25681. ("Hyperlinks"
  25682. ["Store Link (Global)" org-store-link t]
  25683. ["Insert Link" org-insert-link t]
  25684. ["Follow Link" org-open-at-point t]
  25685. "--"
  25686. ["Next link" org-next-link t]
  25687. ["Previous link" org-previous-link t]
  25688. "--"
  25689. ["Descriptive Links"
  25690. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25691. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25692. ["Literal Links"
  25693. (progn
  25694. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25695. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25696. "--"
  25697. ["Export/Publish..." org-export t]
  25698. ("LaTeX"
  25699. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25700. :selected org-cdlatex-mode]
  25701. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25702. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25703. ["Modify math symbol" org-cdlatex-math-modify
  25704. (org-inside-LaTeX-fragment-p)]
  25705. ["Export LaTeX fragments as images"
  25706. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25707. :style toggle :selected org-export-with-LaTeX-fragments])
  25708. "--"
  25709. ("Documentation"
  25710. ["Show Version" org-version t]
  25711. ["Info Documentation" org-info t])
  25712. ("Customize"
  25713. ["Browse Org Group" org-customize t]
  25714. "--"
  25715. ["Expand This Menu" org-create-customize-menu
  25716. (fboundp 'customize-menu-create)])
  25717. "--"
  25718. ["Refresh setup" org-mode-restart t]
  25719. ))
  25720. (defun org-info (&optional node)
  25721. "Read documentation for Org-mode in the info system.
  25722. With optional NODE, go directly to that node."
  25723. (interactive)
  25724. (require 'info)
  25725. (info (format "(org)%s" (or node ""))))
  25726. (defun org-install-agenda-files-menu ()
  25727. (let ((bl (buffer-list)))
  25728. (save-excursion
  25729. (while bl
  25730. (set-buffer (pop bl))
  25731. (if (org-mode-p) (setq bl nil)))
  25732. (when (org-mode-p)
  25733. (easy-menu-change
  25734. '("Org") "File List for Agenda"
  25735. (append
  25736. (list
  25737. ["Edit File List" (org-edit-agenda-file-list) t]
  25738. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25739. ["Remove Current File from List" org-remove-file t]
  25740. ["Cycle through agenda files" org-cycle-agenda-files t]
  25741. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25742. "--")
  25743. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25744. ;;;; Documentation
  25745. (defun org-customize ()
  25746. "Call the customize function with org as argument."
  25747. (interactive)
  25748. (customize-browse 'org))
  25749. (defun org-create-customize-menu ()
  25750. "Create a full customization menu for Org-mode, insert it into the menu."
  25751. (interactive)
  25752. (if (fboundp 'customize-menu-create)
  25753. (progn
  25754. (easy-menu-change
  25755. '("Org") "Customize"
  25756. `(["Browse Org group" org-customize t]
  25757. "--"
  25758. ,(customize-menu-create 'org)
  25759. ["Set" Custom-set t]
  25760. ["Save" Custom-save t]
  25761. ["Reset to Current" Custom-reset-current t]
  25762. ["Reset to Saved" Custom-reset-saved t]
  25763. ["Reset to Standard Settings" Custom-reset-standard t]))
  25764. (message "\"Org\"-menu now contains full customization menu"))
  25765. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25766. ;;;; Miscellaneous stuff
  25767. ;;; Generally useful functions
  25768. (defun org-context ()
  25769. "Return a list of contexts of the current cursor position.
  25770. If several contexts apply, all are returned.
  25771. Each context entry is a list with a symbol naming the context, and
  25772. two positions indicating start and end of the context. Possible
  25773. contexts are:
  25774. :headline anywhere in a headline
  25775. :headline-stars on the leading stars in a headline
  25776. :todo-keyword on a TODO keyword (including DONE) in a headline
  25777. :tags on the TAGS in a headline
  25778. :priority on the priority cookie in a headline
  25779. :item on the first line of a plain list item
  25780. :item-bullet on the bullet/number of a plain list item
  25781. :checkbox on the checkbox in a plain list item
  25782. :table in an org-mode table
  25783. :table-special on a special filed in a table
  25784. :table-table in a table.el table
  25785. :link on a hyperlink
  25786. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25787. :target on a <<target>>
  25788. :radio-target on a <<<radio-target>>>
  25789. :latex-fragment on a LaTeX fragment
  25790. :latex-preview on a LaTeX fragment with overlayed preview image
  25791. This function expects the position to be visible because it uses font-lock
  25792. faces as a help to recognize the following contexts: :table-special, :link,
  25793. and :keyword."
  25794. (let* ((f (get-text-property (point) 'face))
  25795. (faces (if (listp f) f (list f)))
  25796. (p (point)) clist o)
  25797. ;; First the large context
  25798. (cond
  25799. ((org-on-heading-p t)
  25800. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25801. (when (progn
  25802. (beginning-of-line 1)
  25803. (looking-at org-todo-line-tags-regexp))
  25804. (push (org-point-in-group p 1 :headline-stars) clist)
  25805. (push (org-point-in-group p 2 :todo-keyword) clist)
  25806. (push (org-point-in-group p 4 :tags) clist))
  25807. (goto-char p)
  25808. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25809. (if (looking-at "\\[#[A-Z0-9]\\]")
  25810. (push (org-point-in-group p 0 :priority) clist)))
  25811. ((org-at-item-p)
  25812. (push (org-point-in-group p 2 :item-bullet) clist)
  25813. (push (list :item (point-at-bol)
  25814. (save-excursion (org-end-of-item) (point)))
  25815. clist)
  25816. (and (org-at-item-checkbox-p)
  25817. (push (org-point-in-group p 0 :checkbox) clist)))
  25818. ((org-at-table-p)
  25819. (push (list :table (org-table-begin) (org-table-end)) clist)
  25820. (if (memq 'org-formula faces)
  25821. (push (list :table-special
  25822. (previous-single-property-change p 'face)
  25823. (next-single-property-change p 'face)) clist)))
  25824. ((org-at-table-p 'any)
  25825. (push (list :table-table) clist)))
  25826. (goto-char p)
  25827. ;; Now the small context
  25828. (cond
  25829. ((org-at-timestamp-p)
  25830. (push (org-point-in-group p 0 :timestamp) clist))
  25831. ((memq 'org-link faces)
  25832. (push (list :link
  25833. (previous-single-property-change p 'face)
  25834. (next-single-property-change p 'face)) clist))
  25835. ((memq 'org-special-keyword faces)
  25836. (push (list :keyword
  25837. (previous-single-property-change p 'face)
  25838. (next-single-property-change p 'face)) clist))
  25839. ((org-on-target-p)
  25840. (push (org-point-in-group p 0 :target) clist)
  25841. (goto-char (1- (match-beginning 0)))
  25842. (if (looking-at org-radio-target-regexp)
  25843. (push (org-point-in-group p 0 :radio-target) clist))
  25844. (goto-char p))
  25845. ((setq o (car (delq nil
  25846. (mapcar
  25847. (lambda (x)
  25848. (if (memq x org-latex-fragment-image-overlays) x))
  25849. (org-overlays-at (point))))))
  25850. (push (list :latex-fragment
  25851. (org-overlay-start o) (org-overlay-end o)) clist)
  25852. (push (list :latex-preview
  25853. (org-overlay-start o) (org-overlay-end o)) clist))
  25854. ((org-inside-LaTeX-fragment-p)
  25855. ;; FIXME: positions wrong.
  25856. (push (list :latex-fragment (point) (point)) clist)))
  25857. (setq clist (nreverse (delq nil clist)))
  25858. clist))
  25859. ;; FIXME: Compare with at-regexp-p Do we need both?
  25860. (defun org-in-regexp (re &optional nlines visually)
  25861. "Check if point is inside a match of regexp.
  25862. Normally only the current line is checked, but you can include NLINES extra
  25863. lines both before and after point into the search.
  25864. If VISUALLY is set, require that the cursor is not after the match but
  25865. really on, so that the block visually is on the match."
  25866. (catch 'exit
  25867. (let ((pos (point))
  25868. (eol (point-at-eol (+ 1 (or nlines 0))))
  25869. (inc (if visually 1 0)))
  25870. (save-excursion
  25871. (beginning-of-line (- 1 (or nlines 0)))
  25872. (while (re-search-forward re eol t)
  25873. (if (and (<= (match-beginning 0) pos)
  25874. (>= (+ inc (match-end 0)) pos))
  25875. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25876. (defun org-at-regexp-p (regexp)
  25877. "Is point inside a match of REGEXP in the current line?"
  25878. (catch 'exit
  25879. (save-excursion
  25880. (let ((pos (point)) (end (point-at-eol)))
  25881. (beginning-of-line 1)
  25882. (while (re-search-forward regexp end t)
  25883. (if (and (<= (match-beginning 0) pos)
  25884. (>= (match-end 0) pos))
  25885. (throw 'exit t)))
  25886. nil))))
  25887. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25888. "Call `multi-occur' with buffers for all agenda files."
  25889. (interactive "sOrg-files matching: \np")
  25890. (let* ((files (org-agenda-files))
  25891. (tnames (mapcar 'file-truename files))
  25892. (extra org-agenda-text-search-extra-files)
  25893. f)
  25894. (while (setq f (pop extra))
  25895. (unless (member (file-truename f) tnames)
  25896. (add-to-list 'files f 'append)
  25897. (add-to-list 'tnames (file-truename f) 'append)))
  25898. (multi-occur
  25899. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25900. regexp)))
  25901. (if (boundp 'occur-mode-find-occurrence-hook)
  25902. ;; Emacs 23
  25903. (add-hook 'occur-mode-find-occurrence-hook
  25904. (lambda ()
  25905. (when (org-mode-p)
  25906. (org-reveal))))
  25907. ;; Emacs 22
  25908. (defadvice occur-mode-goto-occurrence
  25909. (after org-occur-reveal activate)
  25910. (and (org-mode-p) (org-reveal)))
  25911. (defadvice occur-mode-goto-occurrence-other-window
  25912. (after org-occur-reveal activate)
  25913. (and (org-mode-p) (org-reveal)))
  25914. (defadvice occur-mode-display-occurrence
  25915. (after org-occur-reveal activate)
  25916. (when (org-mode-p)
  25917. (let ((pos (occur-mode-find-occurrence)))
  25918. (with-current-buffer (marker-buffer pos)
  25919. (save-excursion
  25920. (goto-char pos)
  25921. (org-reveal)))))))
  25922. (defun org-uniquify (list)
  25923. "Remove duplicate elements from LIST."
  25924. (let (res)
  25925. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25926. res))
  25927. (defun org-delete-all (elts list)
  25928. "Remove all elements in ELTS from LIST."
  25929. (while elts
  25930. (setq list (delete (pop elts) list)))
  25931. list)
  25932. (defun org-back-over-empty-lines ()
  25933. "Move backwards over witespace, to the beginning of the first empty line.
  25934. Returns the number o empty lines passed."
  25935. (let ((pos (point)))
  25936. (skip-chars-backward " \t\n\r")
  25937. (beginning-of-line 2)
  25938. (goto-char (min (point) pos))
  25939. (count-lines (point) pos)))
  25940. (defun org-skip-whitespace ()
  25941. (skip-chars-forward " \t\n\r"))
  25942. (defun org-point-in-group (point group &optional context)
  25943. "Check if POINT is in match-group GROUP.
  25944. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25945. match. If the match group does ot exist or point is not inside it,
  25946. return nil."
  25947. (and (match-beginning group)
  25948. (>= point (match-beginning group))
  25949. (<= point (match-end group))
  25950. (if context
  25951. (list context (match-beginning group) (match-end group))
  25952. t)))
  25953. (defun org-switch-to-buffer-other-window (&rest args)
  25954. "Switch to buffer in a second window on the current frame.
  25955. In particular, do not allow pop-up frames."
  25956. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25957. special-display-function)
  25958. (apply 'switch-to-buffer-other-window args)))
  25959. (defun org-combine-plists (&rest plists)
  25960. "Create a single property list from all plists in PLISTS.
  25961. The process starts by copying the first list, and then setting properties
  25962. from the other lists. Settings in the last list are the most significant
  25963. ones and overrule settings in the other lists."
  25964. (let ((rtn (copy-sequence (pop plists)))
  25965. p v ls)
  25966. (while plists
  25967. (setq ls (pop plists))
  25968. (while ls
  25969. (setq p (pop ls) v (pop ls))
  25970. (setq rtn (plist-put rtn p v))))
  25971. rtn))
  25972. (defun org-move-line-down (arg)
  25973. "Move the current line down. With prefix argument, move it past ARG lines."
  25974. (interactive "p")
  25975. (let ((col (current-column))
  25976. beg end pos)
  25977. (beginning-of-line 1) (setq beg (point))
  25978. (beginning-of-line 2) (setq end (point))
  25979. (beginning-of-line (+ 1 arg))
  25980. (setq pos (move-marker (make-marker) (point)))
  25981. (insert (delete-and-extract-region beg end))
  25982. (goto-char pos)
  25983. (move-to-column col)))
  25984. (defun org-move-line-up (arg)
  25985. "Move the current line up. With prefix argument, move it past ARG lines."
  25986. (interactive "p")
  25987. (let ((col (current-column))
  25988. beg end pos)
  25989. (beginning-of-line 1) (setq beg (point))
  25990. (beginning-of-line 2) (setq end (point))
  25991. (beginning-of-line (- arg))
  25992. (setq pos (move-marker (make-marker) (point)))
  25993. (insert (delete-and-extract-region beg end))
  25994. (goto-char pos)
  25995. (move-to-column col)))
  25996. (defun org-replace-escapes (string table)
  25997. "Replace %-escapes in STRING with values in TABLE.
  25998. TABLE is an association list with keys like \"%a\" and string values.
  25999. The sequences in STRING may contain normal field width and padding information,
  26000. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  26001. so values can contain further %-escapes if they are define later in TABLE."
  26002. (let ((case-fold-search nil)
  26003. e re rpl)
  26004. (while (setq e (pop table))
  26005. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  26006. (while (string-match re string)
  26007. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  26008. (cdr e)))
  26009. (setq string (replace-match rpl t t string))))
  26010. string))
  26011. (defun org-sublist (list start end)
  26012. "Return a section of LIST, from START to END.
  26013. Counting starts at 1."
  26014. (let (rtn (c start))
  26015. (setq list (nthcdr (1- start) list))
  26016. (while (and list (<= c end))
  26017. (push (pop list) rtn)
  26018. (setq c (1+ c)))
  26019. (nreverse rtn)))
  26020. (defun org-find-base-buffer-visiting (file)
  26021. "Like `find-buffer-visiting' but alway return the base buffer and
  26022. not an indirect buffer"
  26023. (let ((buf (find-buffer-visiting file)))
  26024. (if buf
  26025. (or (buffer-base-buffer buf) buf)
  26026. nil)))
  26027. (defun org-image-file-name-regexp ()
  26028. "Return regexp matching the file names of images."
  26029. (if (fboundp 'image-file-name-regexp)
  26030. (image-file-name-regexp)
  26031. (let ((image-file-name-extensions
  26032. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  26033. "xbm" "xpm" "pbm" "pgm" "ppm")))
  26034. (concat "\\."
  26035. (regexp-opt (nconc (mapcar 'upcase
  26036. image-file-name-extensions)
  26037. image-file-name-extensions)
  26038. t)
  26039. "\\'"))))
  26040. (defun org-file-image-p (file)
  26041. "Return non-nil if FILE is an image."
  26042. (save-match-data
  26043. (string-match (org-image-file-name-regexp) file)))
  26044. ;;; Paragraph filling stuff.
  26045. ;; We want this to be just right, so use the full arsenal.
  26046. (defun org-indent-line-function ()
  26047. "Indent line like previous, but further if previous was headline or item."
  26048. (interactive)
  26049. (let* ((pos (point))
  26050. (itemp (org-at-item-p))
  26051. column bpos bcol tpos tcol bullet btype bullet-type)
  26052. ;; Find the previous relevant line
  26053. (beginning-of-line 1)
  26054. (cond
  26055. ((looking-at "#") (setq column 0))
  26056. ((looking-at "\\*+ ") (setq column 0))
  26057. (t
  26058. (beginning-of-line 0)
  26059. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  26060. (beginning-of-line 0))
  26061. (cond
  26062. ((looking-at "\\*+[ \t]+")
  26063. (goto-char (match-end 0))
  26064. (setq column (current-column)))
  26065. ((org-in-item-p)
  26066. (org-beginning-of-item)
  26067. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26068. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  26069. (setq bpos (match-beginning 1) tpos (match-end 0)
  26070. bcol (progn (goto-char bpos) (current-column))
  26071. tcol (progn (goto-char tpos) (current-column))
  26072. bullet (match-string 1)
  26073. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  26074. (if (not itemp)
  26075. (setq column tcol)
  26076. (goto-char pos)
  26077. (beginning-of-line 1)
  26078. (if (looking-at "\\S-")
  26079. (progn
  26080. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26081. (setq bullet (match-string 1)
  26082. btype (if (string-match "[0-9]" bullet) "n" bullet))
  26083. (setq column (if (equal btype bullet-type) bcol tcol)))
  26084. (setq column (org-get-indentation)))))
  26085. (t (setq column (org-get-indentation))))))
  26086. (goto-char pos)
  26087. (if (<= (current-column) (current-indentation))
  26088. (indent-line-to column)
  26089. (save-excursion (indent-line-to column)))
  26090. (setq column (current-column))
  26091. (beginning-of-line 1)
  26092. (if (looking-at
  26093. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  26094. (replace-match (concat "\\1" (format org-property-format
  26095. (match-string 2) (match-string 3)))
  26096. t nil))
  26097. (move-to-column column)))
  26098. (defun org-set-autofill-regexps ()
  26099. (interactive)
  26100. ;; In the paragraph separator we include headlines, because filling
  26101. ;; text in a line directly attached to a headline would otherwise
  26102. ;; fill the headline as well.
  26103. (org-set-local 'comment-start-skip "^#+[ \t]*")
  26104. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  26105. ;; The paragraph starter includes hand-formatted lists.
  26106. (org-set-local 'paragraph-start
  26107. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  26108. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  26109. ;; But only if the user has not turned off tables or fixed-width regions
  26110. (org-set-local
  26111. 'auto-fill-inhibit-regexp
  26112. (concat "\\*+ \\|#\\+"
  26113. "\\|[ \t]*" org-keyword-time-regexp
  26114. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  26115. (concat
  26116. "\\|[ \t]*["
  26117. (if org-enable-table-editor "|" "")
  26118. (if org-enable-fixed-width-editor ":" "")
  26119. "]"))))
  26120. ;; We use our own fill-paragraph function, to make sure that tables
  26121. ;; and fixed-width regions are not wrapped. That function will pass
  26122. ;; through to `fill-paragraph' when appropriate.
  26123. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  26124. ; Adaptive filling: To get full control, first make sure that
  26125. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  26126. (org-set-local 'adaptive-fill-regexp "\000")
  26127. (org-set-local 'adaptive-fill-function
  26128. 'org-adaptive-fill-function)
  26129. (org-set-local
  26130. 'align-mode-rules-list
  26131. '((org-in-buffer-settings
  26132. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  26133. (modes . '(org-mode))))))
  26134. (defun org-fill-paragraph (&optional justify)
  26135. "Re-align a table, pass through to fill-paragraph if no table."
  26136. (let ((table-p (org-at-table-p))
  26137. (table.el-p (org-at-table.el-p)))
  26138. (cond ((and (equal (char-after (point-at-bol)) ?*)
  26139. (save-excursion (goto-char (point-at-bol))
  26140. (looking-at outline-regexp)))
  26141. t) ; skip headlines
  26142. (table.el-p t) ; skip table.el tables
  26143. (table-p (org-table-align) t) ; align org-mode tables
  26144. (t nil)))) ; call paragraph-fill
  26145. ;; For reference, this is the default value of adaptive-fill-regexp
  26146. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  26147. (defun org-adaptive-fill-function ()
  26148. "Return a fill prefix for org-mode files.
  26149. In particular, this makes sure hanging paragraphs for hand-formatted lists
  26150. work correctly."
  26151. (cond ((looking-at "#[ \t]+")
  26152. (match-string 0))
  26153. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  26154. (save-excursion
  26155. (goto-char (match-end 0))
  26156. (make-string (current-column) ?\ )))
  26157. (t nil)))
  26158. ;;;; Functions extending outline functionality
  26159. (defun org-beginning-of-line (&optional arg)
  26160. "Go to the beginning of the current line. If that is invisible, continue
  26161. to a visible line beginning. This makes the function of C-a more intuitive.
  26162. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26163. first attempt, and only move to after the tags when the cursor is already
  26164. beyond the end of the headline."
  26165. (interactive "P")
  26166. (let ((pos (point)))
  26167. (beginning-of-line 1)
  26168. (if (bobp)
  26169. nil
  26170. (backward-char 1)
  26171. (if (org-invisible-p)
  26172. (while (and (not (bobp)) (org-invisible-p))
  26173. (backward-char 1)
  26174. (beginning-of-line 1))
  26175. (forward-char 1)))
  26176. (when org-special-ctrl-a/e
  26177. (cond
  26178. ((and (looking-at org-todo-line-regexp)
  26179. (= (char-after (match-end 1)) ?\ ))
  26180. (goto-char
  26181. (if (eq org-special-ctrl-a/e t)
  26182. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  26183. ((= pos (point)) (match-beginning 3))
  26184. (t (point)))
  26185. (cond ((> pos (point)) (point))
  26186. ((not (eq last-command this-command)) (point))
  26187. (t (match-beginning 3))))))
  26188. ((org-at-item-p)
  26189. (goto-char
  26190. (if (eq org-special-ctrl-a/e t)
  26191. (cond ((> pos (match-end 4)) (match-end 4))
  26192. ((= pos (point)) (match-end 4))
  26193. (t (point)))
  26194. (cond ((> pos (point)) (point))
  26195. ((not (eq last-command this-command)) (point))
  26196. (t (match-end 4))))))))))
  26197. (defun org-end-of-line (&optional arg)
  26198. "Go to the end of the line.
  26199. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26200. first attempt, and only move to after the tags when the cursor is already
  26201. beyond the end of the headline."
  26202. (interactive "P")
  26203. (if (or (not org-special-ctrl-a/e)
  26204. (not (org-on-heading-p)))
  26205. (end-of-line arg)
  26206. (let ((pos (point)))
  26207. (beginning-of-line 1)
  26208. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  26209. (if (eq org-special-ctrl-a/e t)
  26210. (if (or (< pos (match-beginning 1))
  26211. (= pos (match-end 0)))
  26212. (goto-char (match-beginning 1))
  26213. (goto-char (match-end 0)))
  26214. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  26215. (goto-char (match-end 0))
  26216. (goto-char (match-beginning 1))))
  26217. (end-of-line arg)))))
  26218. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26219. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26220. (defun org-kill-line (&optional arg)
  26221. "Kill line, to tags or end of line."
  26222. (interactive "P")
  26223. (cond
  26224. ((or (not org-special-ctrl-k)
  26225. (bolp)
  26226. (not (org-on-heading-p)))
  26227. (call-interactively 'kill-line))
  26228. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26229. (kill-region (point) (match-beginning 1))
  26230. (org-set-tags nil t))
  26231. (t (kill-region (point) (point-at-eol)))))
  26232. (define-key org-mode-map "\C-k" 'org-kill-line)
  26233. (defun org-invisible-p ()
  26234. "Check if point is at a character currently not visible."
  26235. ;; Early versions of noutline don't have `outline-invisible-p'.
  26236. (if (fboundp 'outline-invisible-p)
  26237. (outline-invisible-p)
  26238. (get-char-property (point) 'invisible)))
  26239. (defun org-invisible-p2 ()
  26240. "Check if point is at a character currently not visible."
  26241. (save-excursion
  26242. (if (and (eolp) (not (bobp))) (backward-char 1))
  26243. ;; Early versions of noutline don't have `outline-invisible-p'.
  26244. (if (fboundp 'outline-invisible-p)
  26245. (outline-invisible-p)
  26246. (get-char-property (point) 'invisible))))
  26247. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26248. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26249. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26250. (defun org-at-heading-or-item-p ()
  26251. (or (org-on-heading-p) (org-at-item-p)))
  26252. (defun org-on-target-p ()
  26253. (or (org-in-regexp org-radio-target-regexp)
  26254. (org-in-regexp org-target-regexp)))
  26255. (defun org-up-heading-all (arg)
  26256. "Move to the heading line of which the present line is a subheading.
  26257. This function considers both visible and invisible heading lines.
  26258. With argument, move up ARG levels."
  26259. (if (fboundp 'outline-up-heading-all)
  26260. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26261. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26262. (defun org-up-heading-safe ()
  26263. "Move to the heading line of which the present line is a subheading.
  26264. This version will not throw an error. It will return the level of the
  26265. headline found, or nil if no higher level is found."
  26266. (let ((pos (point)) start-level level
  26267. (re (concat "^" outline-regexp)))
  26268. (catch 'exit
  26269. (outline-back-to-heading t)
  26270. (setq start-level (funcall outline-level))
  26271. (if (equal start-level 1) (throw 'exit nil))
  26272. (while (re-search-backward re nil t)
  26273. (setq level (funcall outline-level))
  26274. (if (< level start-level) (throw 'exit level)))
  26275. nil)))
  26276. (defun org-first-sibling-p ()
  26277. "Is this heading the first child of its parents?"
  26278. (interactive)
  26279. (let ((re (concat "^" outline-regexp))
  26280. level l)
  26281. (unless (org-at-heading-p t)
  26282. (error "Not at a heading"))
  26283. (setq level (funcall outline-level))
  26284. (save-excursion
  26285. (if (not (re-search-backward re nil t))
  26286. t
  26287. (setq l (funcall outline-level))
  26288. (< l level)))))
  26289. (defun org-goto-sibling (&optional previous)
  26290. "Goto the next sibling, even if it is invisible.
  26291. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26292. when a sibling was found. When none is found, return nil and don't
  26293. move point."
  26294. (let ((fun (if previous 're-search-backward 're-search-forward))
  26295. (pos (point))
  26296. (re (concat "^" outline-regexp))
  26297. level l)
  26298. (when (condition-case nil (org-back-to-heading t) (error nil))
  26299. (setq level (funcall outline-level))
  26300. (catch 'exit
  26301. (or previous (forward-char 1))
  26302. (while (funcall fun re nil t)
  26303. (setq l (funcall outline-level))
  26304. (when (< l level) (goto-char pos) (throw 'exit nil))
  26305. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26306. (goto-char pos)
  26307. nil))))
  26308. (defun org-show-siblings ()
  26309. "Show all siblings of the current headline."
  26310. (save-excursion
  26311. (while (org-goto-sibling) (org-flag-heading nil)))
  26312. (save-excursion
  26313. (while (org-goto-sibling 'previous)
  26314. (org-flag-heading nil))))
  26315. (defun org-show-hidden-entry ()
  26316. "Show an entry where even the heading is hidden."
  26317. (save-excursion
  26318. (org-show-entry)))
  26319. (defun org-flag-heading (flag &optional entry)
  26320. "Flag the current heading. FLAG non-nil means make invisible.
  26321. When ENTRY is non-nil, show the entire entry."
  26322. (save-excursion
  26323. (org-back-to-heading t)
  26324. ;; Check if we should show the entire entry
  26325. (if entry
  26326. (progn
  26327. (org-show-entry)
  26328. (save-excursion
  26329. (and (outline-next-heading)
  26330. (org-flag-heading nil))))
  26331. (outline-flag-region (max (point-min) (1- (point)))
  26332. (save-excursion (outline-end-of-heading) (point))
  26333. flag))))
  26334. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26335. ;; This is an exact copy of the original function, but it uses
  26336. ;; `org-back-to-heading', to make it work also in invisible
  26337. ;; trees. And is uses an invisible-OK argument.
  26338. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26339. (org-back-to-heading invisible-OK)
  26340. (let ((first t)
  26341. (level (funcall outline-level)))
  26342. (while (and (not (eobp))
  26343. (or first (> (funcall outline-level) level)))
  26344. (setq first nil)
  26345. (outline-next-heading))
  26346. (unless to-heading
  26347. (if (memq (preceding-char) '(?\n ?\^M))
  26348. (progn
  26349. ;; Go to end of line before heading
  26350. (forward-char -1)
  26351. (if (memq (preceding-char) '(?\n ?\^M))
  26352. ;; leave blank line before heading
  26353. (forward-char -1))))))
  26354. (point))
  26355. (defun org-show-subtree ()
  26356. "Show everything after this heading at deeper levels."
  26357. (outline-flag-region
  26358. (point)
  26359. (save-excursion
  26360. (outline-end-of-subtree) (outline-next-heading) (point))
  26361. nil))
  26362. (defun org-show-entry ()
  26363. "Show the body directly following this heading.
  26364. Show the heading too, if it is currently invisible."
  26365. (interactive)
  26366. (save-excursion
  26367. (condition-case nil
  26368. (progn
  26369. (org-back-to-heading t)
  26370. (outline-flag-region
  26371. (max (point-min) (1- (point)))
  26372. (save-excursion
  26373. (re-search-forward
  26374. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26375. (or (match-beginning 1) (point-max)))
  26376. nil))
  26377. (error nil))))
  26378. (defun org-make-options-regexp (kwds)
  26379. "Make a regular expression for keyword lines."
  26380. (concat
  26381. "^"
  26382. "#?[ \t]*\\+\\("
  26383. (mapconcat 'regexp-quote kwds "\\|")
  26384. "\\):[ \t]*"
  26385. "\\(.+\\)"))
  26386. ;; Make isearch reveal the necessary context
  26387. (defun org-isearch-end ()
  26388. "Reveal context after isearch exits."
  26389. (when isearch-success ; only if search was successful
  26390. (if (featurep 'xemacs)
  26391. ;; Under XEmacs, the hook is run in the correct place,
  26392. ;; we directly show the context.
  26393. (org-show-context 'isearch)
  26394. ;; In Emacs the hook runs *before* restoring the overlays.
  26395. ;; So we have to use a one-time post-command-hook to do this.
  26396. ;; (Emacs 22 has a special variable, see function `org-mode')
  26397. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26398. isearch-mode-end-hook-quit)
  26399. ;; Only when the isearch was not quitted.
  26400. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26401. 'append 'local)))))
  26402. (defun org-isearch-post-command ()
  26403. "Remove self from hook, and show context."
  26404. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26405. (org-show-context 'isearch))
  26406. ;;;; Integration with and fixes for other packages
  26407. ;;; Imenu support
  26408. (defvar org-imenu-markers nil
  26409. "All markers currently used by Imenu.")
  26410. (make-variable-buffer-local 'org-imenu-markers)
  26411. (defun org-imenu-new-marker (&optional pos)
  26412. "Return a new marker for use by Imenu, and remember the marker."
  26413. (let ((m (make-marker)))
  26414. (move-marker m (or pos (point)))
  26415. (push m org-imenu-markers)
  26416. m))
  26417. (defun org-imenu-get-tree ()
  26418. "Produce the index for Imenu."
  26419. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26420. (setq org-imenu-markers nil)
  26421. (let* ((n org-imenu-depth)
  26422. (re (concat "^" outline-regexp))
  26423. (subs (make-vector (1+ n) nil))
  26424. (last-level 0)
  26425. m tree level head)
  26426. (save-excursion
  26427. (save-restriction
  26428. (widen)
  26429. (goto-char (point-max))
  26430. (while (re-search-backward re nil t)
  26431. (setq level (org-reduced-level (funcall outline-level)))
  26432. (when (<= level n)
  26433. (looking-at org-complex-heading-regexp)
  26434. (setq head (org-match-string-no-properties 4)
  26435. m (org-imenu-new-marker))
  26436. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26437. (if (>= level last-level)
  26438. (push (cons head m) (aref subs level))
  26439. (push (cons head (aref subs (1+ level))) (aref subs level))
  26440. (loop for i from (1+ level) to n do (aset subs i nil)))
  26441. (setq last-level level)))))
  26442. (aref subs 1)))
  26443. (eval-after-load "imenu"
  26444. '(progn
  26445. (add-hook 'imenu-after-jump-hook
  26446. (lambda () (org-show-context 'org-goto)))))
  26447. ;; Speedbar support
  26448. (defun org-speedbar-set-agenda-restriction ()
  26449. "Restrict future agenda commands to the location at point in speedbar.
  26450. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26451. (interactive)
  26452. (let (p m tp np dir txt w)
  26453. (cond
  26454. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26455. 'org-imenu t))
  26456. (setq m (get-text-property p 'org-imenu-marker))
  26457. (save-excursion
  26458. (save-restriction
  26459. (set-buffer (marker-buffer m))
  26460. (goto-char m)
  26461. (org-agenda-set-restriction-lock 'subtree))))
  26462. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26463. 'speedbar-function 'speedbar-find-file))
  26464. (setq tp (previous-single-property-change
  26465. (1+ p) 'speedbar-function)
  26466. np (next-single-property-change
  26467. tp 'speedbar-function)
  26468. dir (speedbar-line-directory)
  26469. txt (buffer-substring-no-properties (or tp (point-min))
  26470. (or np (point-max))))
  26471. (save-excursion
  26472. (save-restriction
  26473. (set-buffer (find-file-noselect
  26474. (let ((default-directory dir))
  26475. (expand-file-name txt))))
  26476. (unless (org-mode-p)
  26477. (error "Cannot restrict to non-Org-mode file"))
  26478. (org-agenda-set-restriction-lock 'file))))
  26479. (t (error "Don't know how to restrict Org-mode's agenda")))
  26480. (org-move-overlay org-speedbar-restriction-lock-overlay
  26481. (point-at-bol) (point-at-eol))
  26482. (setq current-prefix-arg nil)
  26483. (org-agenda-maybe-redo)))
  26484. (eval-after-load "speedbar"
  26485. '(progn
  26486. (speedbar-add-supported-extension ".org")
  26487. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26488. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26489. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26490. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26491. (add-hook 'speedbar-visiting-tag-hook
  26492. (lambda () (org-show-context 'org-goto)))))
  26493. ;;; Fixes and Hacks
  26494. ;; Make flyspell not check words in links, to not mess up our keymap
  26495. (defun org-mode-flyspell-verify ()
  26496. "Don't let flyspell put overlays at active buttons."
  26497. (not (get-text-property (point) 'keymap)))
  26498. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26499. (eval-after-load "bookmark"
  26500. '(if (boundp 'bookmark-after-jump-hook)
  26501. ;; We can use the hook
  26502. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26503. ;; Hook not available, use advice
  26504. (defadvice bookmark-jump (after org-make-visible activate)
  26505. "Make the position visible."
  26506. (org-bookmark-jump-unhide))))
  26507. (defun org-bookmark-jump-unhide ()
  26508. "Unhide the current position, to show the bookmark location."
  26509. (and (org-mode-p)
  26510. (or (org-invisible-p)
  26511. (save-excursion (goto-char (max (point-min) (1- (point))))
  26512. (org-invisible-p)))
  26513. (org-show-context 'bookmark-jump)))
  26514. ;; Fix a bug in htmlize where there are text properties (face nil)
  26515. (eval-after-load "htmlize"
  26516. '(progn
  26517. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26518. "Make sure there are no nil faces"
  26519. (setq ad-return-value (delq nil ad-return-value)))))
  26520. ;; Make session.el ignore our circular variable
  26521. (eval-after-load "session"
  26522. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26523. ;;;; Experimental code
  26524. (defun org-closed-in-range ()
  26525. "Sparse tree of items closed in a certain time range.
  26526. Still experimental, may disappear in the future."
  26527. (interactive)
  26528. ;; Get the time interval from the user.
  26529. (let* ((time1 (time-to-seconds
  26530. (org-read-date nil 'to-time nil "Starting date: ")))
  26531. (time2 (time-to-seconds
  26532. (org-read-date nil 'to-time nil "End date:")))
  26533. ;; callback function
  26534. (callback (lambda ()
  26535. (let ((time
  26536. (time-to-seconds
  26537. (apply 'encode-time
  26538. (org-parse-time-string
  26539. (match-string 1))))))
  26540. ;; check if time in interval
  26541. (and (>= time time1) (<= time time2))))))
  26542. ;; make tree, check each match with the callback
  26543. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26544. ;;;; Finish up
  26545. (provide 'org)
  26546. (run-hooks 'org-load-hook)
  26547. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26548. ;;; org.el ends here